2
0

commands.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. """
  2. QAPI command marshaller generator
  3. Copyright IBM, Corp. 2011
  4. Copyright (C) 2014-2018 Red Hat, Inc.
  5. Authors:
  6. Anthony Liguori <aliguori@us.ibm.com>
  7. Michael Roth <mdroth@linux.vnet.ibm.com>
  8. Markus Armbruster <armbru@redhat.com>
  9. This work is licensed under the terms of the GNU GPL, version 2.
  10. See the COPYING file in the top-level directory.
  11. """
  12. from typing import (
  13. Dict,
  14. List,
  15. Optional,
  16. Set,
  17. )
  18. from .common import c_name, mcgen
  19. from .gen import (
  20. QAPIGenC,
  21. QAPISchemaModularCVisitor,
  22. build_params,
  23. ifcontext,
  24. gen_special_features,
  25. )
  26. from .schema import (
  27. QAPISchema,
  28. QAPISchemaFeature,
  29. QAPISchemaIfCond,
  30. QAPISchemaObjectType,
  31. QAPISchemaType,
  32. )
  33. from .source import QAPISourceInfo
  34. def gen_command_decl(name: str,
  35. arg_type: Optional[QAPISchemaObjectType],
  36. boxed: bool,
  37. ret_type: Optional[QAPISchemaType]) -> str:
  38. return mcgen('''
  39. %(c_type)s qmp_%(c_name)s(%(params)s);
  40. ''',
  41. c_type=(ret_type and ret_type.c_type()) or 'void',
  42. c_name=c_name(name),
  43. params=build_params(arg_type, boxed, 'Error **errp'))
  44. def gen_call(name: str,
  45. arg_type: Optional[QAPISchemaObjectType],
  46. boxed: bool,
  47. ret_type: Optional[QAPISchemaType]) -> str:
  48. ret = ''
  49. argstr = ''
  50. if boxed:
  51. assert arg_type
  52. argstr = '&arg, '
  53. elif arg_type:
  54. assert not arg_type.variants
  55. for memb in arg_type.members:
  56. if memb.optional:
  57. argstr += 'arg.has_%s, ' % c_name(memb.name)
  58. argstr += 'arg.%s, ' % c_name(memb.name)
  59. lhs = ''
  60. if ret_type:
  61. lhs = 'retval = '
  62. ret = mcgen('''
  63. %(lhs)sqmp_%(c_name)s(%(args)s&err);
  64. error_propagate(errp, err);
  65. ''',
  66. c_name=c_name(name), args=argstr, lhs=lhs)
  67. if ret_type:
  68. ret += mcgen('''
  69. if (err) {
  70. goto out;
  71. }
  72. qmp_marshal_output_%(c_name)s(retval, ret, errp);
  73. ''',
  74. c_name=ret_type.c_name())
  75. return ret
  76. def gen_marshal_output(ret_type: QAPISchemaType) -> str:
  77. return mcgen('''
  78. static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in,
  79. QObject **ret_out, Error **errp)
  80. {
  81. Visitor *v;
  82. v = qobject_output_visitor_new_qmp(ret_out);
  83. if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) {
  84. visit_complete(v, ret_out);
  85. }
  86. visit_free(v);
  87. v = qapi_dealloc_visitor_new();
  88. visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
  89. visit_free(v);
  90. }
  91. ''',
  92. c_type=ret_type.c_type(), c_name=ret_type.c_name())
  93. def build_marshal_proto(name: str) -> str:
  94. return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
  95. % c_name(name))
  96. def gen_marshal_decl(name: str) -> str:
  97. return mcgen('''
  98. %(proto)s;
  99. ''',
  100. proto=build_marshal_proto(name))
  101. def gen_marshal(name: str,
  102. arg_type: Optional[QAPISchemaObjectType],
  103. boxed: bool,
  104. ret_type: Optional[QAPISchemaType]) -> str:
  105. have_args = boxed or (arg_type and not arg_type.is_empty())
  106. if have_args:
  107. assert arg_type is not None
  108. arg_type_c_name = arg_type.c_name()
  109. ret = mcgen('''
  110. %(proto)s
  111. {
  112. Error *err = NULL;
  113. bool ok = false;
  114. Visitor *v;
  115. ''',
  116. proto=build_marshal_proto(name))
  117. if ret_type:
  118. ret += mcgen('''
  119. %(c_type)s retval;
  120. ''',
  121. c_type=ret_type.c_type())
  122. if have_args:
  123. ret += mcgen('''
  124. %(c_name)s arg = {0};
  125. ''',
  126. c_name=arg_type_c_name)
  127. ret += mcgen('''
  128. v = qobject_input_visitor_new_qmp(QOBJECT(args));
  129. if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
  130. goto out;
  131. }
  132. ''')
  133. if have_args:
  134. ret += mcgen('''
  135. if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
  136. ok = visit_check_struct(v, errp);
  137. }
  138. ''',
  139. c_arg_type=arg_type_c_name)
  140. else:
  141. ret += mcgen('''
  142. ok = visit_check_struct(v, errp);
  143. ''')
  144. ret += mcgen('''
  145. visit_end_struct(v, NULL);
  146. if (!ok) {
  147. goto out;
  148. }
  149. ''')
  150. ret += gen_call(name, arg_type, boxed, ret_type)
  151. ret += mcgen('''
  152. out:
  153. visit_free(v);
  154. ''')
  155. ret += mcgen('''
  156. v = qapi_dealloc_visitor_new();
  157. visit_start_struct(v, NULL, NULL, 0, NULL);
  158. ''')
  159. if have_args:
  160. ret += mcgen('''
  161. visit_type_%(c_arg_type)s_members(v, &arg, NULL);
  162. ''',
  163. c_arg_type=arg_type_c_name)
  164. ret += mcgen('''
  165. visit_end_struct(v, NULL);
  166. visit_free(v);
  167. ''')
  168. ret += mcgen('''
  169. }
  170. ''')
  171. return ret
  172. def gen_register_command(name: str,
  173. features: List[QAPISchemaFeature],
  174. success_response: bool,
  175. allow_oob: bool,
  176. allow_preconfig: bool,
  177. coroutine: bool) -> str:
  178. options = []
  179. if not success_response:
  180. options += ['QCO_NO_SUCCESS_RESP']
  181. if allow_oob:
  182. options += ['QCO_ALLOW_OOB']
  183. if allow_preconfig:
  184. options += ['QCO_ALLOW_PRECONFIG']
  185. if coroutine:
  186. options += ['QCO_COROUTINE']
  187. ret = mcgen('''
  188. qmp_register_command(cmds, "%(name)s",
  189. qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
  190. ''',
  191. name=name, c_name=c_name(name),
  192. opts=' | '.join(options) or 0,
  193. feats=gen_special_features(features))
  194. return ret
  195. class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
  196. def __init__(self, prefix: str):
  197. super().__init__(
  198. prefix, 'qapi-commands',
  199. ' * Schema-defined QAPI/QMP commands', None, __doc__)
  200. self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
  201. def _begin_user_module(self, name: str) -> None:
  202. self._visited_ret_types[self._genc] = set()
  203. commands = self._module_basename('qapi-commands', name)
  204. types = self._module_basename('qapi-types', name)
  205. visit = self._module_basename('qapi-visit', name)
  206. self._genc.add(mcgen('''
  207. #include "qemu/osdep.h"
  208. #include "qapi/compat-policy.h"
  209. #include "qapi/visitor.h"
  210. #include "qapi/qmp/qdict.h"
  211. #include "qapi/dealloc-visitor.h"
  212. #include "qapi/error.h"
  213. #include "%(visit)s.h"
  214. #include "%(commands)s.h"
  215. ''',
  216. commands=commands, visit=visit))
  217. self._genh.add(mcgen('''
  218. #include "%(types)s.h"
  219. ''',
  220. types=types))
  221. def visit_begin(self, schema: QAPISchema) -> None:
  222. self._add_module('./init', ' * QAPI Commands initialization')
  223. self._genh.add(mcgen('''
  224. #include "qapi/qmp/dispatch.h"
  225. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
  226. ''',
  227. c_prefix=c_name(self._prefix, protect=False)))
  228. self._genc.add(mcgen('''
  229. #include "qemu/osdep.h"
  230. #include "%(prefix)sqapi-commands.h"
  231. #include "%(prefix)sqapi-init-commands.h"
  232. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
  233. {
  234. QTAILQ_INIT(cmds);
  235. ''',
  236. prefix=self._prefix,
  237. c_prefix=c_name(self._prefix, protect=False)))
  238. def visit_end(self) -> None:
  239. with self._temp_module('./init'):
  240. self._genc.add(mcgen('''
  241. }
  242. '''))
  243. def visit_command(self,
  244. name: str,
  245. info: Optional[QAPISourceInfo],
  246. ifcond: QAPISchemaIfCond,
  247. features: List[QAPISchemaFeature],
  248. arg_type: Optional[QAPISchemaObjectType],
  249. ret_type: Optional[QAPISchemaType],
  250. gen: bool,
  251. success_response: bool,
  252. boxed: bool,
  253. allow_oob: bool,
  254. allow_preconfig: bool,
  255. coroutine: bool) -> None:
  256. if not gen:
  257. return
  258. # FIXME: If T is a user-defined type, the user is responsible
  259. # for making this work, i.e. to make T's condition the
  260. # conjunction of the T-returning commands' conditions. If T
  261. # is a built-in type, this isn't possible: the
  262. # qmp_marshal_output_T() will be generated unconditionally.
  263. if ret_type and ret_type not in self._visited_ret_types[self._genc]:
  264. self._visited_ret_types[self._genc].add(ret_type)
  265. with ifcontext(ret_type.ifcond,
  266. self._genh, self._genc):
  267. self._genc.add(gen_marshal_output(ret_type))
  268. with ifcontext(ifcond, self._genh, self._genc):
  269. self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
  270. self._genh.add(gen_marshal_decl(name))
  271. self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
  272. with self._temp_module('./init'):
  273. with ifcontext(ifcond, self._genh, self._genc):
  274. self._genc.add(gen_register_command(
  275. name, features, success_response, allow_oob,
  276. allow_preconfig, coroutine))
  277. def gen_commands(schema: QAPISchema,
  278. output_dir: str,
  279. prefix: str) -> None:
  280. vis = QAPISchemaGenCommandVisitor(prefix)
  281. schema.visit(vis)
  282. vis.write(output_dir)