2
0

qapi-code-gen.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. two kinds of types that are supported: complex user-defined types, and enums.
  28. A complex type is a dictionary containing a single key who's value is a
  29. dictionary. This corresponds to a struct in C or an Object in JSON. An
  30. example of a complex type is:
  31. { 'type': 'MyType',
  32. 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } }
  33. The use of '*' as a prefix to the name means the member is optional. Optional
  34. members should always be added to the end of the dictionary to preserve
  35. backwards compatibility.
  36. An enumeration type is a dictionary containing a single key who's value is a
  37. list of strings. An example enumeration is:
  38. { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
  39. Generally speaking, complex types and enums should always use CamelCase for
  40. the type names.
  41. Commands are defined by using a list containing three members. The first
  42. member is the command name, the second member is a dictionary containing
  43. arguments, and the third member is the return type.
  44. An example command is:
  45. { 'command': 'my-command',
  46. 'data': { 'arg1': 'str', '*arg2': 'str' },
  47. 'returns': 'str' }
  48. Command names should be all lower case with words separated by a hyphen.
  49. == Code generation ==
  50. Schemas are fed into 3 scripts to generate all the code/files that, paired
  51. with the core QAPI libraries, comprise everything required to take JSON
  52. commands read in by a QMP/guest agent server, unmarshal the arguments into
  53. the underlying C types, call into the corresponding C function, and map the
  54. response back to a QMP/guest agent response to be returned to the user.
  55. As an example, we'll use the following schema, which describes a single
  56. complex user-defined type (which will produce a C struct, along with a list
  57. node structure that can be used to chain together a list of such types in
  58. case we want to accept/return a list of this type with a command), and a
  59. command which takes that type as a parameter and returns the same type:
  60. mdroth@illuin:~/w/qemu2.git$ cat example-schema.json
  61. { 'type': 'UserDefOne',
  62. 'data': { 'integer': 'int', 'string': 'str' } }
  63. { 'command': 'my-command',
  64. 'data': {'arg1': 'UserDefOne'},
  65. 'returns': 'UserDefOne' }
  66. mdroth@illuin:~/w/qemu2.git$
  67. === scripts/qapi-types.py ===
  68. Used to generate the C types defined by a schema. The following files are
  69. created:
  70. $(prefix)qapi-types.h - C types corresponding to types defined in
  71. the schema you pass in
  72. $(prefix)qapi-types.c - Cleanup functions for the above C types
  73. The $(prefix) is an optional parameter used as a namespace to keep the
  74. generated code from one schema/code-generation separated from others so code
  75. can be generated/used from multiple schemas without clobbering previously
  76. created code.
  77. Example:
  78. mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-types.py \
  79. --output-dir="qapi-generated" --prefix="example-" < example-schema.json
  80. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.c
  81. /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  82. #include "qapi/qapi-dealloc-visitor.h"
  83. #include "example-qapi-types.h"
  84. #include "example-qapi-visit.h"
  85. void qapi_free_UserDefOne(UserDefOne * obj)
  86. {
  87. QapiDeallocVisitor *md;
  88. Visitor *v;
  89. if (!obj) {
  90. return;
  91. }
  92. md = qapi_dealloc_visitor_new();
  93. v = qapi_dealloc_get_visitor(md);
  94. visit_type_UserDefOne(v, &obj, NULL, NULL);
  95. qapi_dealloc_visitor_cleanup(md);
  96. }
  97. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.h
  98. /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
  99. #ifndef QAPI_GENERATED_EXAMPLE_QAPI_TYPES
  100. #define QAPI_GENERATED_EXAMPLE_QAPI_TYPES
  101. #include "qapi/qapi-types-core.h"
  102. typedef struct UserDefOne UserDefOne;
  103. typedef struct UserDefOneList
  104. {
  105. UserDefOne *value;
  106. struct UserDefOneList *next;
  107. } UserDefOneList;
  108. struct UserDefOne
  109. {
  110. int64_t integer;
  111. char * string;
  112. };
  113. void qapi_free_UserDefOne(UserDefOne * obj);
  114. #endif
  115. === scripts/qapi-visit.py ===
  116. Used to generate the visitor functions used to walk through and convert
  117. a QObject (as provided by QMP) to a native C data structure and
  118. vice-versa, as well as the visitor function used to dealloc a complex
  119. schema-defined C type.
  120. The following files are generated:
  121. $(prefix)qapi-visit.c: visitor function for a particular C type, used
  122. to automagically convert QObjects into the
  123. corresponding C type and vice-versa, as well
  124. as for deallocating memory for an existing C
  125. type
  126. $(prefix)qapi-visit.h: declarations for previously mentioned visitor
  127. functions
  128. Example:
  129. mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-visit.py \
  130. --output-dir="qapi-generated" --prefix="example-" < example-schema.json
  131. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.c
  132. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  133. #include "example-qapi-visit.h"
  134. void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
  135. {
  136. visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), errp);
  137. visit_type_int(m, (obj && *obj) ? &(*obj)->integer : NULL, "integer", errp);
  138. visit_type_str(m, (obj && *obj) ? &(*obj)->string : NULL, "string", errp);
  139. visit_end_struct(m, errp);
  140. }
  141. void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
  142. {
  143. GenericList *i;
  144. visit_start_list(m, name, errp);
  145. for (i = visit_next_list(m, (GenericList **)obj, errp); i; i = visit_next_list(m, &i, errp)) {
  146. UserDefOneList *native_i = (UserDefOneList *)i;
  147. visit_type_UserDefOne(m, &native_i->value, NULL, errp);
  148. }
  149. visit_end_list(m, errp);
  150. }
  151. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
  152. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  153. #ifndef QAPI_GENERATED_EXAMPLE_QAPI_VISIT
  154. #define QAPI_GENERATED_EXAMPLE_QAPI_VISIT
  155. #include "qapi/qapi-visit-core.h"
  156. #include "example-qapi-types.h"
  157. void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp);
  158. void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
  159. #endif
  160. mdroth@illuin:~/w/qemu2.git$
  161. === scripts/qapi-commands.py ===
  162. Used to generate the marshaling/dispatch functions for the commands defined
  163. in the schema. The following files are generated:
  164. $(prefix)qmp-marshal.c: command marshal/dispatch functions for each
  165. QMP command defined in the schema. Functions
  166. generated by qapi-visit.py are used to
  167. convert QObjects received from the wire into
  168. function parameters, and uses the same
  169. visitor functions to convert native C return
  170. values to QObjects from transmission back
  171. over the wire.
  172. $(prefix)qmp-commands.h: Function prototypes for the QMP commands
  173. specified in the schema.
  174. Example:
  175. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-marshal.c
  176. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  177. #include "qemu-objects.h"
  178. #include "qapi/qmp-core.h"
  179. #include "qapi/qapi-visit-core.h"
  180. #include "qapi/qmp-output-visitor.h"
  181. #include "qapi/qmp-input-visitor.h"
  182. #include "qapi/qapi-dealloc-visitor.h"
  183. #include "example-qapi-types.h"
  184. #include "example-qapi-visit.h"
  185. #include "example-qmp-commands.h"
  186. static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
  187. {
  188. QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
  189. QmpOutputVisitor *mo = qmp_output_visitor_new();
  190. Visitor *v;
  191. v = qmp_output_get_visitor(mo);
  192. visit_type_UserDefOne(v, &ret_in, "unused", errp);
  193. v = qapi_dealloc_get_visitor(md);
  194. visit_type_UserDefOne(v, &ret_in, "unused", errp);
  195. qapi_dealloc_visitor_cleanup(md);
  196. *ret_out = qmp_output_get_qobject(mo);
  197. }
  198. static void qmp_marshal_input_my_command(QmpState *qmp__sess, QDict *args, QObject **ret, Error **errp)
  199. {
  200. UserDefOne * retval = NULL;
  201. QmpInputVisitor *mi;
  202. QapiDeallocVisitor *md;
  203. Visitor *v;
  204. UserDefOne * arg1 = NULL;
  205. mi = qmp_input_visitor_new(QOBJECT(args));
  206. v = qmp_input_get_visitor(mi);
  207. visit_type_UserDefOne(v, &arg1, "arg1", errp);
  208. if (error_is_set(errp)) {
  209. goto out;
  210. }
  211. retval = qmp_my_command(arg1, errp);
  212. qmp_marshal_output_my_command(retval, ret, errp);
  213. out:
  214. md = qapi_dealloc_visitor_new();
  215. v = qapi_dealloc_get_visitor(md);
  216. visit_type_UserDefOne(v, &arg1, "arg1", errp);
  217. qapi_dealloc_visitor_cleanup(md);
  218. return;
  219. }
  220. static void qmp_init_marshal(void)
  221. {
  222. qmp_register_command("my-command", qmp_marshal_input_my_command);
  223. }
  224. qapi_init(qmp_init_marshal);
  225. mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-commands.h
  226. /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
  227. #ifndef QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
  228. #define QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
  229. #include "example-qapi-types.h"
  230. #include "error.h"
  231. UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
  232. #endif
  233. mdroth@illuin:~/w/qemu2.git$