2
0

commands.py 7.8 KB

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