commands.py 7.8 KB

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