qapi-code-gen.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. = How to use the QAPI code generator =
  2. * Note: as of this writing, QMP does not use QAPI. Eventually QMP
  3. commands will be converted to use QAPI internally. The following
  4. information describes QMP/QAPI as it will exist after the
  5. conversion.
  6. QAPI is a native C API within QEMU which provides management-level
  7. functionality to internal/external users. For external
  8. users/processes, this interface is made available by a JSON-based
  9. QEMU Monitor protocol that is provided by the QMP server.
  10. To map QMP-defined interfaces to the native C QAPI implementations,
  11. a JSON-based schema is used to define types and function
  12. signatures, and a set of scripts is used to generate types/signatures,
  13. and marshaling/dispatch code. The QEMU Guest Agent also uses these
  14. scripts, paired with a separate schema, to generate
  15. marshaling/dispatch code for the guest agent server running in the
  16. guest.
  17. This document will describe how the schemas, scripts, and resulting
  18. code is used.
  19. == QMP/Guest agent schema ==
  20. This file defines the types, commands, and events used by QMP. It should
  21. fully describe the interface used by QMP.
  22. This file is designed to be loosely based on JSON although it's technically
  23. executable Python. While dictionaries are used, they are parsed as
  24. OrderedDicts so that ordering is preserved.
  25. There are two basic syntaxes used, type definitions and command definitions.
  26. The first syntax defines a type and is represented by a dictionary. There are
  27. three kinds of user-defined types that are supported: complex types,
  28. enumeration types and union types.
  29. Generally speaking, types definitions should always use CamelCase for the type
  30. names. Command names should be all lower case with words separated by a hyphen.
  31. === Complex types ===
  32. A complex type is a dictionary containing a single key whose value is a
  33. dictionary. This corresponds to a struct in C or an Object in JSON. An
  34. example of a complex type is:
  35. { 'type': 'MyType',
  36. 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
  37. The use of '*' as a prefix to the name means the member is optional. Optional
  38. members should always be added to the end of the dictionary to preserve
  39. backwards compatibility.
  40. A complex type definition can specify another complex type as its base.
  41. In this case, the fields of the base type are included as top-level fields
  42. of the new complex type's dictionary in the QMP wire format. An example
  43. definition is:
  44. { 'type': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } }
  45. { 'type': 'BlockdevOptionsGenericCOWFormat',
  46. 'base': 'BlockdevOptionsGenericFormat',
  47. 'data': { '*backing': 'str' } }
  48. An example BlockdevOptionsGenericCOWFormat object on the wire could use
  49. both fields like this:
  50. { "file": "/some/place/my-image",
  51. "backing": "/some/place/my-backing-file" }
  52. === Enumeration types ===
  53. An enumeration type is a dictionary containing a single key whose value is a
  54. list of strings. An example enumeration is:
  55. { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
  56. === Union types ===
  57. Union types are used to let the user choose between several different data
  58. types. A union type is defined using a dictionary as explained in the
  59. following paragraphs.
  60. A simple union type defines a mapping from discriminator values to data types
  61. like in this example:
  62. { 'type': 'FileOptions', 'data': { 'filename': 'str' } }
  63. { 'type': 'Qcow2Options',
  64. 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
  65. { 'union': 'BlockdevOptions',
  66. 'data': { 'file': 'FileOptions',
  67. 'qcow2': 'Qcow2Options' } }
  68. In the QMP wire format, a simple union is represented by a dictionary that
  69. contains the 'type' field as a discriminator, and a 'data' field that is of the
  70. specified data type corresponding to the discriminator value:
  71. { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
  72. "lazy-refcounts": true } }
  73. A union definition can specify a complex type as its base. In this case, the
  74. fields of the complex type are included as top-level fields of the union
  75. dictionary in the QMP wire format. An example definition is:
  76. { 'type': 'BlockdevCommonOptions', 'data': { 'readonly': 'bool' } }
  77. { 'union': 'BlockdevOptions',
  78. 'base': 'BlockdevCommonOptions',
  79. 'data': { 'raw': 'RawOptions',
  80. 'qcow2': 'Qcow2Options' } }
  81. And it looks like this on the wire:
  82. { "type": "qcow2",
  83. "readonly": false,
  84. "data" : { "backing-file": "/some/place/my-image",
  85. "lazy-refcounts": true } }
  86. Flat union types avoid the nesting on the wire. They are used whenever a
  87. specific field of the base type is declared as the discriminator ('type' is
  88. then no longer generated). The discriminator must be of enumeration type.
  89. The above example can then be modified as follows:
  90. { 'enum': 'BlockdevDriver', 'data': [ 'raw', 'qcow2' ] }
  91. { 'type': 'BlockdevCommonOptions',
  92. 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } }
  93. { 'union': 'BlockdevOptions',
  94. 'base': 'BlockdevCommonOptions',
  95. 'discriminator': 'driver',
  96. 'data': { 'raw': 'RawOptions',
  97. 'qcow2': 'Qcow2Options' } }
  98. Resulting in this JSON object:
  99. { "driver": "qcow2",
  100. "readonly": false,
  101. "backing-file": "/some/place/my-image",
  102. "lazy-refcounts": true }
  103. A special type of unions are anonymous unions. They don't form a dictionary in
  104. the wire format but allow the direct use of different types in their place. As
  105. they aren't structured, they don't have any explicit discriminator but use
  106. the (QObject) data type of their value as an implicit discriminator. This means
  107. that they are restricted to using only one discriminator value per QObject
  108. type. For example, you cannot have two different complex types in an anonymous
  109. union, or two different integer types.
  110. Anonymous unions are declared using an empty dictionary as their discriminator.
  111. The discriminator values never appear on the wire, they are only used in the
  112. generated C code. Anonymous unions cannot have a base type.
  113. { 'union': 'BlockRef',
  114. 'discriminator': {},
  115. 'data': { 'definition': 'BlockdevOptions',
  116. 'reference': 'str' } }
  117. This example allows using both of the following example objects:
  118. { "file": "my_existing_block_device_id" }
  119. { "file": { "driver": "file",
  120. "readonly": false,
  121. "filename": "/tmp/mydisk.qcow2" } }
  122. === Commands ===
  123. Commands are defined by using a list containing three members. The first
  124. member is the command name, the second member is a dictionary containing
  125. arguments, and the third member is the return type.
  126. An example command is:
  127. { 'command': 'my-command',
  128. 'data': { 'arg1': 'str', '*arg2': 'str' },
  129. 'returns': 'str' }
  130. == Code generation ==
  131. Schemas are fed into 3 scripts to generate all the code/files that, paired
  132. with the core QAPI libraries, comprise everything required to take JSON
  133. commands read in by a QMP/guest agent server, unmarshal the arguments into
  134. the underlying C types, call into the corresponding C function, and map the
  135. response back to a QMP/guest agent response to be returned to the user.
  136. As an example, we'll use the following schema, which describes a single
  137. complex user-defined type (which will produce a C struct, along with a list
  138. node structure that can be used to chain together a list of such types in
  139. case we want to accept/return a list of this type with a command), and a
  140. command which takes that type as a parameter and returns the same type:
  141. mdroth@illuin:~/w/qemu2.git$ cat example-schema.json
  142. { 'type': 'UserDefOne',
  143. 'data': { 'integer': 'int', 'string': 'str' } }
  144. { 'command': 'my-command',
  145. 'data': {'arg1': 'UserDefOne'},
  146. 'returns': 'UserDefOne' }
  147. mdroth@illuin:~/w/qemu2.git$
  148. === scripts/qapi-types.py ===
  149. Used to generate the C types defined by a schema. The following files are
  150. created:
  151. $(prefix)qapi-types.h - C types corresponding to types defined in
  152. the schema you pass in
  153. $(prefix)qapi-types.c - Cleanup functions for the above C types
  154. The $(prefix) is an optional parameter used as a namespace to keep the
  155. generated code from one schema/code-generation separated from others so code
  156. can be generated/used from multiple schemas without clobbering previously
  157. created code.
  158. Example:
  159. mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-types.py \
  160. --output-dir="qapi-generated" --prefix="example-" < example-schema.json
  161. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.c
  162. /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  163. #include "qapi/qapi-dealloc-visitor.h"
  164. #include "example-qapi-types.h"
  165. #include "example-qapi-visit.h"
  166. void qapi_free_UserDefOne(UserDefOne * obj)
  167. {
  168. QapiDeallocVisitor *md;
  169. Visitor *v;
  170. if (!obj) {
  171. return;
  172. }
  173. md = qapi_dealloc_visitor_new();
  174. v = qapi_dealloc_get_visitor(md);
  175. visit_type_UserDefOne(v, &obj, NULL, NULL);
  176. qapi_dealloc_visitor_cleanup(md);
  177. }
  178. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.h
  179. /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  180. #ifndef QAPI_GENERATED_EXAMPLE_QAPI_TYPES
  181. #define QAPI_GENERATED_EXAMPLE_QAPI_TYPES
  182. #include "qapi/qapi-types-core.h"
  183. typedef struct UserDefOne UserDefOne;
  184. typedef struct UserDefOneList
  185. {
  186. UserDefOne *value;
  187. struct UserDefOneList *next;
  188. } UserDefOneList;
  189. struct UserDefOne
  190. {
  191. int64_t integer;
  192. char * string;
  193. };
  194. void qapi_free_UserDefOne(UserDefOne * obj);
  195. #endif
  196. === scripts/qapi-visit.py ===
  197. Used to generate the visitor functions used to walk through and convert
  198. a QObject (as provided by QMP) to a native C data structure and
  199. vice-versa, as well as the visitor function used to dealloc a complex
  200. schema-defined C type.
  201. The following files are generated:
  202. $(prefix)qapi-visit.c: visitor function for a particular C type, used
  203. to automagically convert QObjects into the
  204. corresponding C type and vice-versa, as well
  205. as for deallocating memory for an existing C
  206. type
  207. $(prefix)qapi-visit.h: declarations for previously mentioned visitor
  208. functions
  209. Example:
  210. mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-visit.py \
  211. --output-dir="qapi-generated" --prefix="example-" < example-schema.json
  212. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.c
  213. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  214. #include "example-qapi-visit.h"
  215. void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
  216. {
  217. visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), errp);
  218. visit_type_int(m, (obj && *obj) ? &(*obj)->integer : NULL, "integer", errp);
  219. visit_type_str(m, (obj && *obj) ? &(*obj)->string : NULL, "string", errp);
  220. visit_end_struct(m, errp);
  221. }
  222. void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
  223. {
  224. GenericList *i, **prev = (GenericList **)obj;
  225. visit_start_list(m, name, errp);
  226. for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) {
  227. UserDefOneList *native_i = (UserDefOneList *)i;
  228. visit_type_UserDefOne(m, &native_i->value, NULL, errp);
  229. }
  230. visit_end_list(m, errp);
  231. }
  232. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
  233. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  234. #ifndef QAPI_GENERATED_EXAMPLE_QAPI_VISIT
  235. #define QAPI_GENERATED_EXAMPLE_QAPI_VISIT
  236. #include "qapi/qapi-visit-core.h"
  237. #include "example-qapi-types.h"
  238. void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp);
  239. void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
  240. #endif
  241. mdroth@illuin:~/w/qemu2.git$
  242. (The actual structure of the visit_type_* functions is a bit more complex
  243. in order to propagate errors correctly and avoid leaking memory).
  244. === scripts/qapi-commands.py ===
  245. Used to generate the marshaling/dispatch functions for the commands defined
  246. in the schema. The following files are generated:
  247. $(prefix)qmp-marshal.c: command marshal/dispatch functions for each
  248. QMP command defined in the schema. Functions
  249. generated by qapi-visit.py are used to
  250. convert QObjects received from the wire into
  251. function parameters, and uses the same
  252. visitor functions to convert native C return
  253. values to QObjects from transmission back
  254. over the wire.
  255. $(prefix)qmp-commands.h: Function prototypes for the QMP commands
  256. specified in the schema.
  257. Example:
  258. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-marshal.c
  259. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  260. #include "qemu-objects.h"
  261. #include "qapi/qmp-core.h"
  262. #include "qapi/qapi-visit-core.h"
  263. #include "qapi/qmp-output-visitor.h"
  264. #include "qapi/qmp-input-visitor.h"
  265. #include "qapi/qapi-dealloc-visitor.h"
  266. #include "example-qapi-types.h"
  267. #include "example-qapi-visit.h"
  268. #include "example-qmp-commands.h"
  269. static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
  270. {
  271. QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
  272. QmpOutputVisitor *mo = qmp_output_visitor_new();
  273. Visitor *v;
  274. v = qmp_output_get_visitor(mo);
  275. visit_type_UserDefOne(v, &ret_in, "unused", errp);
  276. v = qapi_dealloc_get_visitor(md);
  277. visit_type_UserDefOne(v, &ret_in, "unused", errp);
  278. qapi_dealloc_visitor_cleanup(md);
  279. *ret_out = qmp_output_get_qobject(mo);
  280. }
  281. static void qmp_marshal_input_my_command(QmpState *qmp__sess, QDict *args, QObject **ret, Error **errp)
  282. {
  283. UserDefOne * retval = NULL;
  284. QmpInputVisitor *mi;
  285. QapiDeallocVisitor *md;
  286. Visitor *v;
  287. UserDefOne * arg1 = NULL;
  288. mi = qmp_input_visitor_new(QOBJECT(args));
  289. v = qmp_input_get_visitor(mi);
  290. visit_type_UserDefOne(v, &arg1, "arg1", errp);
  291. if (error_is_set(errp)) {
  292. goto out;
  293. }
  294. retval = qmp_my_command(arg1, errp);
  295. qmp_marshal_output_my_command(retval, ret, errp);
  296. out:
  297. md = qapi_dealloc_visitor_new();
  298. v = qapi_dealloc_get_visitor(md);
  299. visit_type_UserDefOne(v, &arg1, "arg1", errp);
  300. qapi_dealloc_visitor_cleanup(md);
  301. return;
  302. }
  303. static void qmp_init_marshal(void)
  304. {
  305. qmp_register_command("my-command", qmp_marshal_input_my_command);
  306. }
  307. qapi_init(qmp_init_marshal);
  308. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-commands.h
  309. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  310. #ifndef QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
  311. #define QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
  312. #include "example-qapi-types.h"
  313. #include "error.h"
  314. UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
  315. #endif
  316. mdroth@illuin:~/w/qemu2.git$