commands.py 9.2 KB

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