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