2
0

commands.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. ret = mcgen('''
  106. %(proto)s
  107. {
  108. Error *err = NULL;
  109. bool ok = false;
  110. Visitor *v;
  111. ''',
  112. proto=build_marshal_proto(name))
  113. if ret_type:
  114. ret += mcgen('''
  115. %(c_type)s retval;
  116. ''',
  117. c_type=ret_type.c_type())
  118. if have_args:
  119. ret += mcgen('''
  120. %(c_name)s arg = {0};
  121. ''',
  122. c_name=arg_type.c_name())
  123. ret += mcgen('''
  124. v = qobject_input_visitor_new(QOBJECT(args));
  125. if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
  126. goto out;
  127. }
  128. ''')
  129. if have_args:
  130. ret += mcgen('''
  131. if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
  132. ok = visit_check_struct(v, errp);
  133. }
  134. ''',
  135. c_arg_type=arg_type.c_name())
  136. else:
  137. ret += mcgen('''
  138. ok = visit_check_struct(v, errp);
  139. ''')
  140. ret += mcgen('''
  141. visit_end_struct(v, NULL);
  142. if (!ok) {
  143. goto out;
  144. }
  145. ''')
  146. ret += gen_call(name, arg_type, boxed, ret_type)
  147. ret += mcgen('''
  148. out:
  149. visit_free(v);
  150. ''')
  151. ret += mcgen('''
  152. v = qapi_dealloc_visitor_new();
  153. visit_start_struct(v, NULL, NULL, 0, NULL);
  154. ''')
  155. if have_args:
  156. ret += mcgen('''
  157. visit_type_%(c_arg_type)s_members(v, &arg, NULL);
  158. ''',
  159. c_arg_type=arg_type.c_name())
  160. ret += mcgen('''
  161. visit_end_struct(v, NULL);
  162. visit_free(v);
  163. ''')
  164. ret += mcgen('''
  165. }
  166. ''')
  167. return ret
  168. def gen_register_command(name: str,
  169. success_response: bool,
  170. allow_oob: bool,
  171. allow_preconfig: bool,
  172. coroutine: bool) -> str:
  173. options = []
  174. if not success_response:
  175. options += ['QCO_NO_SUCCESS_RESP']
  176. if allow_oob:
  177. options += ['QCO_ALLOW_OOB']
  178. if allow_preconfig:
  179. options += ['QCO_ALLOW_PRECONFIG']
  180. if coroutine:
  181. options += ['QCO_COROUTINE']
  182. if not options:
  183. options = ['QCO_NO_OPTIONS']
  184. ret = mcgen('''
  185. qmp_register_command(cmds, "%(name)s",
  186. qmp_marshal_%(c_name)s, %(opts)s);
  187. ''',
  188. name=name, c_name=c_name(name),
  189. opts=" | ".join(options))
  190. return ret
  191. def gen_registry(registry: str, prefix: str) -> str:
  192. ret = mcgen('''
  193. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
  194. {
  195. QTAILQ_INIT(cmds);
  196. ''',
  197. c_prefix=c_name(prefix, protect=False))
  198. ret += registry
  199. ret += mcgen('''
  200. }
  201. ''')
  202. return ret
  203. class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
  204. def __init__(self, prefix: str):
  205. super().__init__(
  206. prefix, 'qapi-commands',
  207. ' * Schema-defined QAPI/QMP commands', None, __doc__)
  208. self._regy = QAPIGenCCode(None)
  209. self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
  210. def _begin_user_module(self, name: str) -> None:
  211. self._visited_ret_types[self._genc] = set()
  212. commands = self._module_basename('qapi-commands', name)
  213. types = self._module_basename('qapi-types', name)
  214. visit = self._module_basename('qapi-visit', name)
  215. self._genc.add(mcgen('''
  216. #include "qemu/osdep.h"
  217. #include "qapi/visitor.h"
  218. #include "qapi/qmp/qdict.h"
  219. #include "qapi/qobject-output-visitor.h"
  220. #include "qapi/qobject-input-visitor.h"
  221. #include "qapi/dealloc-visitor.h"
  222. #include "qapi/error.h"
  223. #include "%(visit)s.h"
  224. #include "%(commands)s.h"
  225. ''',
  226. commands=commands, visit=visit))
  227. self._genh.add(mcgen('''
  228. #include "%(types)s.h"
  229. ''',
  230. types=types))
  231. def visit_end(self) -> None:
  232. self._add_system_module('init', ' * QAPI Commands initialization')
  233. self._genh.add(mcgen('''
  234. #include "qapi/qmp/dispatch.h"
  235. void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
  236. ''',
  237. c_prefix=c_name(self._prefix, protect=False)))
  238. self._genc.preamble_add(mcgen('''
  239. #include "qemu/osdep.h"
  240. #include "%(prefix)sqapi-commands.h"
  241. #include "%(prefix)sqapi-init-commands.h"
  242. ''',
  243. prefix=self._prefix))
  244. self._genc.add(gen_registry(self._regy.get_content(), self._prefix))
  245. def visit_command(self,
  246. name: str,
  247. info: QAPISourceInfo,
  248. ifcond: List[str],
  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, self._regy):
  269. self._genc.add(gen_marshal_output(ret_type))
  270. with ifcontext(ifcond, self._genh, self._genc, self._regy):
  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. self._regy.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)