commands.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. QAPISchemaIfCond,
  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_qmp(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_qmp(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. features: List[QAPISchemaFeature],
  173. success_response: bool,
  174. allow_oob: bool,
  175. allow_preconfig: bool,
  176. coroutine: bool) -> str:
  177. options = []
  178. if 'deprecated' in [f.name for f in features]:
  179. options += ['QCO_DEPRECATED']
  180. if not success_response:
  181. options += ['QCO_NO_SUCCESS_RESP']
  182. if allow_oob:
  183. options += ['QCO_ALLOW_OOB']
  184. if allow_preconfig:
  185. options += ['QCO_ALLOW_PRECONFIG']
  186. if coroutine:
  187. options += ['QCO_COROUTINE']
  188. if not options:
  189. options = ['QCO_NO_OPTIONS']
  190. ret = mcgen('''
  191. qmp_register_command(cmds, "%(name)s",
  192. qmp_marshal_%(c_name)s, %(opts)s);
  193. ''',
  194. name=name, c_name=c_name(name),
  195. opts=" | ".join(options))
  196. return ret
  197. class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
  198. def __init__(self, prefix: str):
  199. super().__init__(
  200. prefix, 'qapi-commands',
  201. ' * Schema-defined QAPI/QMP commands', None, __doc__)
  202. self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
  203. def _begin_user_module(self, name: str) -> None:
  204. self._visited_ret_types[self._genc] = set()
  205. commands = self._module_basename('qapi-commands', name)
  206. types = self._module_basename('qapi-types', name)
  207. visit = self._module_basename('qapi-visit', name)
  208. self._genc.add(mcgen('''
  209. #include "qemu/osdep.h"
  210. #include "qapi/compat-policy.h"
  211. #include "qapi/visitor.h"
  212. #include "qapi/qmp/qdict.h"
  213. #include "qapi/dealloc-visitor.h"
  214. #include "qapi/error.h"
  215. #include "%(visit)s.h"
  216. #include "%(commands)s.h"
  217. ''',
  218. commands=commands, visit=visit))
  219. self._genh.add(mcgen('''
  220. #include "%(types)s.h"
  221. ''',
  222. types=types))
  223. def visit_begin(self, schema: QAPISchema) -> None:
  224. self._add_module('./init', ' * QAPI Commands initialization')
  225. self._genh.add(mcgen('''
  226. #include "qapi/qmp/dispatch.h"
  227. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
  228. ''',
  229. c_prefix=c_name(self._prefix, protect=False)))
  230. self._genc.add(mcgen('''
  231. #include "qemu/osdep.h"
  232. #include "%(prefix)sqapi-commands.h"
  233. #include "%(prefix)sqapi-init-commands.h"
  234. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
  235. {
  236. QTAILQ_INIT(cmds);
  237. ''',
  238. prefix=self._prefix,
  239. c_prefix=c_name(self._prefix, protect=False)))
  240. def visit_end(self) -> None:
  241. with self._temp_module('./init'):
  242. self._genc.add(mcgen('''
  243. }
  244. '''))
  245. def visit_command(self,
  246. name: str,
  247. info: Optional[QAPISourceInfo],
  248. ifcond: QAPISchemaIfCond,
  249. features: List[QAPISchemaFeature],
  250. arg_type: Optional[QAPISchemaObjectType],
  251. ret_type: Optional[QAPISchemaType],
  252. gen: bool,
  253. success_response: bool,
  254. boxed: bool,
  255. allow_oob: bool,
  256. allow_preconfig: bool,
  257. coroutine: bool) -> None:
  258. if not gen:
  259. return
  260. # FIXME: If T is a user-defined type, the user is responsible
  261. # for making this work, i.e. to make T's condition the
  262. # conjunction of the T-returning commands' conditions. If T
  263. # is a built-in type, this isn't possible: the
  264. # qmp_marshal_output_T() will be generated unconditionally.
  265. if ret_type and ret_type not in self._visited_ret_types[self._genc]:
  266. self._visited_ret_types[self._genc].add(ret_type)
  267. with ifcontext(ret_type.ifcond,
  268. self._genh, self._genc):
  269. self._genc.add(gen_marshal_output(ret_type))
  270. with ifcontext(ifcond, self._genh, self._genc):
  271. self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
  272. self._genh.add(gen_marshal_decl(name))
  273. self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
  274. with self._temp_module('./init'):
  275. with ifcontext(ifcond, self._genh, self._genc):
  276. self._genc.add(gen_register_command(
  277. name, features, success_response, allow_oob,
  278. allow_preconfig, coroutine))
  279. def gen_commands(schema: QAPISchema,
  280. output_dir: str,
  281. prefix: str) -> None:
  282. vis = QAPISchemaGenCommandVisitor(prefix)
  283. schema.visit(vis)
  284. vis.write(output_dir)