commands.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. Sequence,
  17. Set,
  18. )
  19. from .common import c_name, mcgen
  20. from .gen import (
  21. QAPIGenC,
  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. class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
  195. def __init__(self, prefix: str):
  196. super().__init__(
  197. prefix, 'qapi-commands',
  198. ' * Schema-defined QAPI/QMP commands', None, __doc__)
  199. self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
  200. def _begin_user_module(self, name: str) -> None:
  201. self._visited_ret_types[self._genc] = set()
  202. commands = self._module_basename('qapi-commands', name)
  203. types = self._module_basename('qapi-types', name)
  204. visit = self._module_basename('qapi-visit', name)
  205. self._genc.add(mcgen('''
  206. #include "qemu/osdep.h"
  207. #include "qapi/visitor.h"
  208. #include "qapi/qmp/qdict.h"
  209. #include "qapi/qobject-output-visitor.h"
  210. #include "qapi/qobject-input-visitor.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: Sequence[str],
  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(name, success_response,
  275. allow_oob, allow_preconfig,
  276. 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)