commands.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. """
  2. QAPI command marshaller generator
  3. Copyright IBM, Corp. 2011
  4. Copyright (C) 2014-2018 Red Hat, Inc.
  5. Copyright (c) 2019 osy
  6. Authors:
  7. Anthony Liguori <aliguori@us.ibm.com>
  8. Michael Roth <mdroth@linux.vnet.ibm.com>
  9. Markus Armbruster <armbru@redhat.com>
  10. osy <dev@getutm.app>
  11. This work is licensed under the terms of the GNU GPL, version 2.
  12. See the COPYING file in the top-level directory.
  13. """
  14. from typing import (
  15. Dict,
  16. List,
  17. Optional,
  18. Set,
  19. )
  20. from .common import c_name, mcgen
  21. from .gen import (
  22. QAPIGenC,
  23. QAPIGenCCode,
  24. QAPISchemaModularCVisitor,
  25. build_params,
  26. ifcontext,
  27. )
  28. from .schema import (
  29. QAPISchema,
  30. QAPISchemaFeature,
  31. QAPISchemaObjectType,
  32. QAPISchemaType,
  33. )
  34. from .source import QAPISourceInfo
  35. def gen_command_decl(name: str,
  36. arg_type: Optional[QAPISchemaObjectType],
  37. boxed: bool,
  38. ret_type: Optional[QAPISchemaType],
  39. proto: bool = True) -> str:
  40. return mcgen('''
  41. %(c_type)s qmp_%(c_name)s(%(params)s)%(proto)s
  42. ''',
  43. proto=';' if proto else '',
  44. c_type=(ret_type and ret_type.c_type()) or 'void',
  45. c_name=c_name(name),
  46. params=build_params(arg_type, boxed, 'Error **errp, void *ctx'))
  47. def gen_marshal_rpc(ret_type: QAPISchemaType) -> str:
  48. return mcgen('''
  49. static %(c_type)s qmp_marshal_rpc_%(c_name)s(CFDictionaryRef args, Error **errp, void *ctx)
  50. {
  51. Error *err = NULL;
  52. Visitor *v;
  53. CFDictionaryRef cfret;
  54. %(c_type)s ret = {0};
  55. qmp_rpc_call(args, &cfret, &err, ctx);
  56. if (err) {
  57. error_propagate(errp, err);
  58. return ret;
  59. }
  60. v = cf_input_visitor_new(cfret);
  61. visit_start_struct(v, "command", NULL, 0, &err);
  62. if (err) {
  63. error_propagate(errp, err);
  64. return ret;
  65. }
  66. visit_type_%(c_name)s(v, "return", &ret, &err);
  67. error_propagate(errp, err);
  68. visit_end_struct(v, NULL);
  69. visit_free(v);
  70. CFRelease(cfret);
  71. return ret;
  72. }
  73. ''',
  74. c_type=ret_type.c_type(), c_name=ret_type.c_name())
  75. def gen_rpc_call(name: str,
  76. arg_type: Optional[QAPISchemaObjectType],
  77. boxed: bool,
  78. ret_type: Optional[QAPISchemaType]) -> str:
  79. have_args = boxed or (arg_type and not arg_type.is_empty())
  80. ret = mcgen('''
  81. %(proto)s
  82. {
  83. const char *cmdname = "%(name)s";
  84. CFDictionaryRef cfargs;
  85. Error *err = NULL;
  86. Visitor *v = NULL;
  87. ''',
  88. name=name, proto=gen_command_decl(name, arg_type, boxed, ret_type, proto=False))
  89. if ret_type:
  90. ret += mcgen('''
  91. %(c_type)s ret = {0};
  92. ''',
  93. c_type=ret_type.c_type())
  94. if have_args:
  95. if boxed:
  96. visit_type = ('visit_type_%s(v, "arguments", &argp, &err);'
  97. % arg_type.c_name())
  98. ret += mcgen('''
  99. %(c_name)s *argp = arg;
  100. ''',
  101. c_name=arg_type.c_name())
  102. else:
  103. visit_type = ('visit_type_%s(v, "arguments", &argp, &err);'
  104. % arg_type.c_name())
  105. ret += mcgen('''
  106. %(c_name)s _arg = {
  107. ''',
  108. c_name=arg_type.c_name())
  109. if arg_type:
  110. assert not arg_type.variants
  111. for memb in arg_type.members:
  112. if memb.optional:
  113. ret += mcgen('''
  114. .has_%(c_name)s = has_%(c_name)s,
  115. ''',
  116. c_name=c_name(memb.name))
  117. ret += mcgen('''
  118. .%(c_name)s = %(cast)s%(c_name)s,
  119. ''',
  120. cast='(char *)' if memb.type.name == 'str' else '', c_name=c_name(memb.name))
  121. ret += mcgen('''
  122. };
  123. %(c_name)s *argp = &_arg;
  124. ''',
  125. c_name=arg_type.c_name())
  126. else:
  127. visit_type = ''
  128. ret += mcgen('''
  129. ''')
  130. ret += mcgen('''
  131. v = cf_output_visitor_new((CFTypeRef *)&cfargs);
  132. visit_start_struct(v, "command", NULL, 0, &err);
  133. if (err) {
  134. goto out;
  135. }
  136. visit_type_str(v, "execute", (char **)&cmdname, &err);
  137. if (err) {
  138. goto out;
  139. }
  140. %(visit_type)s
  141. if (err) {
  142. goto out;
  143. }
  144. visit_end_struct(v, NULL);
  145. visit_complete(v, &cfargs);
  146. ''',
  147. visit_type=visit_type)
  148. if ret_type:
  149. ret += mcgen('''
  150. ret = qmp_marshal_rpc_%(c_type)s(cfargs, &err, ctx);
  151. ''',
  152. c_type=ret_type.c_name())
  153. else:
  154. ret += mcgen('''
  155. qmp_rpc_call(cfargs, NULL, &err, ctx);
  156. ''')
  157. ret += mcgen('''
  158. CFRelease(cfargs);
  159. out:
  160. error_propagate(errp, err);
  161. visit_free(v);
  162. ''')
  163. if ret_type:
  164. ret += mcgen('''
  165. return ret;
  166. ''')
  167. ret += mcgen('''
  168. }
  169. ''')
  170. return ret
  171. class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
  172. def __init__(self, prefix: str):
  173. super().__init__(
  174. prefix, 'qapi-commands',
  175. ' * Schema-defined QAPI/QMP commands', None, __doc__)
  176. self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
  177. def _begin_user_module(self, name: str) -> None:
  178. self._visited_ret_types[self._genc] = set()
  179. commands = self._module_basename('qapi-commands', name)
  180. types = self._module_basename('qapi-types', name)
  181. visit = self._module_basename('qapi-visit', name)
  182. self._genc.add(mcgen('''
  183. #include "qemu-compat.h"
  184. #include "cf-output-visitor.h"
  185. #include "cf-input-visitor.h"
  186. #include "dealloc-visitor.h"
  187. #include "error.h"
  188. #include "%(visit)s.h"
  189. #include "%(commands)s.h"
  190. ''',
  191. commands=commands, visit=visit))
  192. self._genh.add(mcgen('''
  193. #include "%(types)s.h"
  194. ''',
  195. types=types))
  196. def visit_command(self,
  197. name: str,
  198. info: QAPISourceInfo,
  199. ifcond: List[str],
  200. features: List[QAPISchemaFeature],
  201. arg_type: Optional[QAPISchemaObjectType],
  202. ret_type: Optional[QAPISchemaType],
  203. gen: bool,
  204. success_response: bool,
  205. boxed: bool,
  206. allow_oob: bool,
  207. allow_preconfig: bool,
  208. coroutine: bool) -> None:
  209. if not gen:
  210. return
  211. # FIXME: If T is a user-defined type, the user is responsible
  212. # for making this work, i.e. to make T's condition the
  213. # conjunction of the T-returning commands' conditions. If T
  214. # is a built-in type, this isn't possible: the
  215. # qmp_marshal_output_T() will be generated unconditionally.
  216. if ret_type and ret_type not in self._visited_ret_types[self._genc]:
  217. self._visited_ret_types[self._genc].add(ret_type)
  218. with ifcontext(ret_type.ifcond,
  219. self._genh, self._genc):
  220. self._genc.add(gen_marshal_rpc(ret_type))
  221. with ifcontext(ifcond, self._genh, self._genc):
  222. self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
  223. self._genc.add(gen_rpc_call(name, arg_type, boxed, ret_type))
  224. def gen_commands(schema: QAPISchema,
  225. output_dir: str,
  226. prefix: str) -> None:
  227. vis = QAPISchemaGenCommandVisitor(prefix)
  228. schema.visit(vis)
  229. vis.write(output_dir)