Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 1 | = How to use the QAPI code generator = |
| 2 | |
Eric Blake | 6fb5545 | 2015-05-04 09:04:58 -0600 | [diff] [blame] | 3 | Copyright IBM Corp. 2011 |
| 4 | Copyright (C) 2012-2015 Red Hat, Inc. |
| 5 | |
| 6 | This work is licensed under the terms of the GNU GPL, version 2 or |
| 7 | later. See the COPYING file in the top-level directory. |
| 8 | |
| 9 | == Introduction == |
| 10 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 11 | QAPI is a native C API within QEMU which provides management-level |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 12 | functionality to internal and external users. For external |
| 13 | users/processes, this interface is made available by a JSON-based wire |
| 14 | format for the QEMU Monitor Protocol (QMP) for controlling qemu, as |
| 15 | well as the QEMU Guest Agent (QGA) for communicating with the guest. |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 16 | The remainder of this document uses "Client JSON Protocol" when |
| 17 | referring to the wire contents of a QMP or QGA connection. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 18 | |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 19 | To map Client JSON Protocol interfaces to the native C QAPI |
| 20 | implementations, a JSON-based schema is used to define types and |
| 21 | function signatures, and a set of scripts is used to generate types, |
| 22 | signatures, and marshaling/dispatch code. This document will describe |
| 23 | how the schemas, scripts, and resulting code are used. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 24 | |
| 25 | |
| 26 | == QMP/Guest agent schema == |
| 27 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 28 | A QAPI schema file is designed to be loosely based on JSON |
| 29 | (http://www.ietf.org/rfc/rfc7159.txt) with changes for quoting style |
| 30 | and the use of comments; a QAPI schema file is then parsed by a python |
| 31 | code generation program. A valid QAPI schema consists of a series of |
| 32 | top-level expressions, with no commas between them. Where |
| 33 | dictionaries (JSON objects) are used, they are parsed as python |
| 34 | OrderedDicts so that ordering is preserved (for predictable layout of |
| 35 | generated C structs and parameter lists). Ordering doesn't matter |
| 36 | between top-level expressions or the keys within an expression, but |
| 37 | does matter within dictionary values for 'data' and 'returns' members |
| 38 | of a single expression. QAPI schema input is written using 'single |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 39 | quotes' instead of JSON's "double quotes" (in contrast, Client JSON |
| 40 | Protocol uses no comments, and while input accepts 'single quotes' as |
| 41 | an extension, output is strict JSON using only "double quotes"). As |
| 42 | in JSON, trailing commas are not permitted in arrays or dictionaries. |
| 43 | Input must be ASCII (although QMP supports full Unicode strings, the |
| 44 | QAPI parser does not). At present, there is no place where a QAPI |
| 45 | schema requires the use of JSON numbers or null. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 46 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 47 | Comments are allowed; anything between an unquoted # and the following |
| 48 | newline is ignored. Although there is not yet a documentation |
| 49 | generator, a form of stylized comments has developed for consistently |
| 50 | documenting details about an expression and when it was added to the |
| 51 | schema. The documentation is delimited between two lines of ##, then |
| 52 | the first line names the expression, an optional overview is provided, |
| 53 | then individual documentation about each member of 'data' is provided, |
| 54 | and finally, a 'Since: x.y.z' tag lists the release that introduced |
| 55 | the expression. Optional fields are tagged with the phrase |
| 56 | '#optional', often with their default value; and extensions added |
| 57 | after the expression was first released are also given a '(since |
| 58 | x.y.z)' comment. For example: |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 59 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 60 | ## |
| 61 | # @BlockStats: |
| 62 | # |
| 63 | # Statistics of a virtual block device or a block backing device. |
| 64 | # |
| 65 | # @device: #optional If the stats are for a virtual block device, the name |
| 66 | # corresponding to the virtual block device. |
| 67 | # |
| 68 | # @stats: A @BlockDeviceStats for the device. |
| 69 | # |
| 70 | # @parent: #optional This describes the file block device if it has one. |
| 71 | # |
| 72 | # @backing: #optional This describes the backing block device if it has one. |
| 73 | # (Since 2.0) |
| 74 | # |
| 75 | # Since: 0.14.0 |
| 76 | ## |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 77 | { 'struct': 'BlockStats', |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 78 | 'data': {'*device': 'str', 'stats': 'BlockDeviceStats', |
| 79 | '*parent': 'BlockStats', |
| 80 | '*backing': 'BlockStats'} } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 81 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 82 | The schema sets up a series of types, as well as commands and events |
| 83 | that will use those types. Forward references are allowed: the parser |
| 84 | scans in two passes, where the first pass learns all type names, and |
| 85 | the second validates the schema and generates the code. This allows |
| 86 | the definition of complex structs that can have mutually recursive |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 87 | types, and allows for indefinite nesting of Client JSON Protocol that |
| 88 | satisfies the schema. A type name should not be defined more than |
| 89 | once. It is permissible for the schema to contain additional types |
| 90 | not used by any commands or events in the Client JSON Protocol, for |
| 91 | the side effect of generated C code used internally. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 92 | |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 93 | There are seven top-level expressions recognized by the parser: |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 94 | 'include', 'command', 'struct', 'enum', 'union', 'alternate', and |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 95 | 'event'. There are several groups of types: simple types (a number of |
| 96 | built-in types, such as 'int' and 'str'; as well as enumerations), |
| 97 | complex types (structs and two flavors of unions), and alternate types |
| 98 | (a choice between other types). The 'command' and 'event' expressions |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 99 | can refer to existing types by name, or list an anonymous type as a |
| 100 | dictionary. Listing a type name inside an array refers to a |
| 101 | single-dimension array of that type; multi-dimension arrays are not |
| 102 | directly supported (although an array of a complex struct that |
| 103 | contains an array member is possible). |
| 104 | |
| 105 | Types, commands, and events share a common namespace. Therefore, |
| 106 | generally speaking, type definitions should always use CamelCase for |
| 107 | user-defined type names, while built-in types are lowercase. Type |
| 108 | definitions should not end in 'Kind', as this namespace is used for |
Eric Blake | 255960d | 2015-10-26 16:34:43 -0600 | [diff] [blame] | 109 | creating implicit C enums for visiting union types, or in 'List', as |
| 110 | this namespace is used for creating array types. Command names, |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 111 | and field names within a type, should be all lower case with words |
| 112 | separated by a hyphen. However, some existing older commands and |
| 113 | complex types use underscore; when extending such expressions, |
| 114 | consistency is preferred over blindly avoiding underscore. Event |
Eric Blake | 9fb081e | 2015-10-26 16:34:44 -0600 | [diff] [blame] | 115 | names should be ALL_CAPS with words separated by underscore. Field |
| 116 | names cannot start with 'has-' or 'has_', as this is reserved for |
| 117 | tracking optional fields. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 118 | |
| 119 | Any name (command, event, type, field, or enum value) beginning with |
| 120 | "x-" is marked experimental, and may be withdrawn or changed |
| 121 | incompatibly in a future release. Downstream vendors may add |
| 122 | extensions; such extensions should begin with a prefix matching |
| 123 | "__RFQDN_" (for the reverse-fully-qualified-domain-name of the |
| 124 | vendor), even if the rest of the name uses dash (example: |
| 125 | __com.redhat_drive-mirror). Other than downstream extensions (with |
| 126 | leading underscore and the use of dots), all names should begin with a |
| 127 | letter, and contain only ASCII letters, digits, dash, and underscore. |
Eric Blake | 9fb081e | 2015-10-26 16:34:44 -0600 | [diff] [blame] | 128 | Names beginning with 'q_' are reserved for the generator: QMP names |
| 129 | that resemble C keywords or other problematic strings will be munged |
| 130 | in C to use this prefix. For example, a field named "default" in |
| 131 | qapi becomes "q_default" in the generated C code. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 132 | |
| 133 | In the rest of this document, usage lines are given for each |
| 134 | expression type, with literal strings written in lower case and |
| 135 | placeholders written in capitals. If a literal string includes a |
| 136 | prefix of '*', that key/value pair can be omitted from the expression. |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 137 | For example, a usage statement that includes '*base':STRUCT-NAME |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 138 | means that an expression has an optional key 'base', which if present |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 139 | must have a value that forms a struct name. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 140 | |
| 141 | |
| 142 | === Built-in Types === |
| 143 | |
Markus Armbruster | f133f2d | 2015-09-16 13:06:22 +0200 | [diff] [blame] | 144 | The following types are predefined, and map to C as follows: |
| 145 | |
| 146 | Schema C JSON |
| 147 | str char * any JSON string, UTF-8 |
| 148 | number double any JSON number |
| 149 | int int64_t a JSON number without fractional part |
| 150 | that fits into the C integer type |
| 151 | int8 int8_t likewise |
| 152 | int16 int16_t likewise |
| 153 | int32 int32_t likewise |
| 154 | int64 int64_t likewise |
| 155 | uint8 uint8_t likewise |
| 156 | uint16 uint16_t likewise |
| 157 | uint32 uint32_t likewise |
| 158 | uint64 uint64_t likewise |
| 159 | size uint64_t like uint64_t, except StringInputVisitor |
| 160 | accepts size suffixes |
| 161 | bool bool JSON true or false |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 162 | any QObject * any JSON value |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 163 | |
LluÃs Vilanova | a719a27 | 2014-05-07 20:46:15 +0200 | [diff] [blame] | 164 | |
| 165 | === Includes === |
| 166 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 167 | Usage: { 'include': STRING } |
| 168 | |
LluÃs Vilanova | a719a27 | 2014-05-07 20:46:15 +0200 | [diff] [blame] | 169 | The QAPI schema definitions can be modularized using the 'include' directive: |
| 170 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 171 | { 'include': 'path/to/file.json' } |
LluÃs Vilanova | a719a27 | 2014-05-07 20:46:15 +0200 | [diff] [blame] | 172 | |
| 173 | The directive is evaluated recursively, and include paths are relative to the |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 174 | file using the directive. Multiple includes of the same file are |
Markus Armbruster | 4247f83 | 2015-06-09 15:24:36 +0200 | [diff] [blame] | 175 | idempotent. No other keys should appear in the expression, and the include |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 176 | value should be a string. |
| 177 | |
| 178 | As a matter of style, it is a good idea to have all files be |
| 179 | self-contained, but at the moment, nothing prevents an included file |
| 180 | from making a forward reference to a type that is only introduced by |
| 181 | an outer file. The parser may be made stricter in the future to |
| 182 | prevent incomplete include files. |
LluÃs Vilanova | a719a27 | 2014-05-07 20:46:15 +0200 | [diff] [blame] | 183 | |
| 184 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 185 | === Struct types === |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 186 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 187 | Usage: { 'struct': STRING, 'data': DICT, '*base': STRUCT-NAME } |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 188 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 189 | A struct is a dictionary containing a single 'data' key whose |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 190 | value is a dictionary. This corresponds to a struct in C or an Object |
| 191 | in JSON. Each value of the 'data' dictionary must be the name of a |
| 192 | type, or a one-element array containing a type name. An example of a |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 193 | struct is: |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 194 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 195 | { 'struct': 'MyType', |
Stefan Hajnoczi | acf8394 | 2011-10-28 15:58:26 +0100 | [diff] [blame] | 196 | 'data': { 'member1': 'str', 'member2': 'int', '*member3': 'str' } } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 197 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 198 | The use of '*' as a prefix to the name means the member is optional in |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 199 | the corresponding JSON protocol usage. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 200 | |
Eric Blake | cc16265 | 2014-05-07 09:57:41 +0800 | [diff] [blame] | 201 | The default initialization value of an optional argument should not be changed |
| 202 | between versions of QEMU unless the new default maintains backward |
| 203 | compatibility to the user-visible behavior of the old default. |
| 204 | |
| 205 | With proper documentation, this policy still allows some flexibility; for |
| 206 | example, documenting that a default of 0 picks an optimal buffer size allows |
| 207 | one release to declare the optimal size at 512 while another release declares |
| 208 | the optimal size at 4096 - the user-visible behavior is not the bytes used by |
| 209 | the buffer, but the fact that the buffer was optimal size. |
| 210 | |
| 211 | On input structures (only mentioned in the 'data' side of a command), changing |
| 212 | from mandatory to optional is safe (older clients will supply the option, and |
| 213 | newer clients can benefit from the default); changing from optional to |
| 214 | mandatory is backwards incompatible (older clients may be omitting the option, |
| 215 | and must continue to work). |
| 216 | |
| 217 | On output structures (only mentioned in the 'returns' side of a command), |
| 218 | changing from mandatory to optional is in general unsafe (older clients may be |
| 219 | expecting the field, and could crash if it is missing), although it can be done |
| 220 | if the only way that the optional argument will be omitted is when it is |
| 221 | triggered by the presence of a new input flag to the command that older clients |
| 222 | don't know to send. Changing from optional to mandatory is safe. |
| 223 | |
| 224 | A structure that is used in both input and output of various commands |
| 225 | must consider the backwards compatibility constraints of both directions |
| 226 | of use. |
Kevin Wolf | 622f557 | 2013-09-19 11:56:36 +0200 | [diff] [blame] | 227 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 228 | A struct definition can specify another struct as its base. |
Kevin Wolf | 622f557 | 2013-09-19 11:56:36 +0200 | [diff] [blame] | 229 | In this case, the fields of the base type are included as top-level fields |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 230 | of the new struct's dictionary in the Client JSON Protocol wire |
| 231 | format. An example definition is: |
Kevin Wolf | 622f557 | 2013-09-19 11:56:36 +0200 | [diff] [blame] | 232 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 233 | { 'struct': 'BlockdevOptionsGenericFormat', 'data': { 'file': 'str' } } |
| 234 | { 'struct': 'BlockdevOptionsGenericCOWFormat', |
Kevin Wolf | 622f557 | 2013-09-19 11:56:36 +0200 | [diff] [blame] | 235 | 'base': 'BlockdevOptionsGenericFormat', |
| 236 | 'data': { '*backing': 'str' } } |
| 237 | |
| 238 | An example BlockdevOptionsGenericCOWFormat object on the wire could use |
| 239 | both fields like this: |
| 240 | |
| 241 | { "file": "/some/place/my-image", |
| 242 | "backing": "/some/place/my-backing-file" } |
| 243 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 244 | |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 245 | === Enumeration types === |
| 246 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 247 | Usage: { 'enum': STRING, 'data': ARRAY-OF-STRING } |
Daniel P. Berrange | 351d36e | 2015-08-26 14:21:20 +0100 | [diff] [blame] | 248 | { 'enum': STRING, '*prefix': STRING, 'data': ARRAY-OF-STRING } |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 249 | |
| 250 | An enumeration type is a dictionary containing a single 'data' key |
| 251 | whose value is a list of strings. An example enumeration is: |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 252 | |
| 253 | { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] } |
| 254 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 255 | Nothing prevents an empty enumeration, although it is probably not |
| 256 | useful. The list of strings should be lower case; if an enum name |
| 257 | represents multiple words, use '-' between words. The string 'max' is |
| 258 | not allowed as an enum value, and values should not be repeated. |
| 259 | |
Daniel P. Berrange | 351d36e | 2015-08-26 14:21:20 +0100 | [diff] [blame] | 260 | The enum constants will be named by using a heuristic to turn the |
| 261 | type name into a set of underscore separated words. For the example |
| 262 | above, 'MyEnum' will turn into 'MY_ENUM' giving a constant name |
| 263 | of 'MY_ENUM_VALUE1' for the first value. If the default heuristic |
| 264 | does not result in a desirable name, the optional 'prefix' field |
| 265 | can be used when defining the enum. |
| 266 | |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 267 | The enumeration values are passed as strings over the Client JSON |
| 268 | Protocol, but are encoded as C enum integral values in generated code. |
| 269 | While the C code starts numbering at 0, it is better to use explicit |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 270 | comparisons to enum values than implicit comparisons to 0; the C code |
| 271 | will also include a generated enum member ending in _MAX for tracking |
| 272 | the size of the enum, useful when using common functions for |
| 273 | converting between strings and enum values. Since the wire format |
| 274 | always passes by name, it is acceptable to reorder or add new |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 275 | enumeration members in any location without breaking clients of Client |
| 276 | JSON Protocol; however, removing enum values would break |
| 277 | compatibility. For any struct that has a field that will only contain |
| 278 | a finite set of string values, using an enum type for that field is |
| 279 | better than open-coding the field to be type 'str'. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 280 | |
| 281 | |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 282 | === Union types === |
| 283 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 284 | Usage: { 'union': STRING, 'data': DICT } |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 285 | or: { 'union': STRING, 'data': DICT, 'base': STRUCT-NAME, |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 286 | 'discriminator': ENUM-MEMBER-OF-BASE } |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 287 | |
| 288 | Union types are used to let the user choose between several different |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 289 | variants for an object. There are two flavors: simple (no |
| 290 | discriminator or base), flat (both discriminator and base). A union |
| 291 | type is defined using a data dictionary as explained in the following |
| 292 | paragraphs. |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 293 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 294 | A simple union type defines a mapping from automatic discriminator |
| 295 | values to data types like in this example: |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 296 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 297 | { 'struct': 'FileOptions', 'data': { 'filename': 'str' } } |
| 298 | { 'struct': 'Qcow2Options', |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 299 | 'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } } |
| 300 | |
| 301 | { 'union': 'BlockdevOptions', |
| 302 | 'data': { 'file': 'FileOptions', |
| 303 | 'qcow2': 'Qcow2Options' } } |
| 304 | |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 305 | In the Client JSON Protocol, a simple union is represented by a |
| 306 | dictionary that contains the 'type' field as a discriminator, and a |
| 307 | 'data' field that is of the specified data type corresponding to the |
| 308 | discriminator value, as in these examples: |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 309 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 310 | { "type": "file", "data" : { "filename": "/some/place/my-image" } } |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 311 | { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image", |
| 312 | "lazy-refcounts": true } } |
| 313 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 314 | The generated C code uses a struct containing a union. Additionally, |
| 315 | an implicit C enum 'NameKind' is created, corresponding to the union |
| 316 | 'Name', for accessing the various branches of the union. No branch of |
| 317 | the union can be named 'max', as this would collide with the implicit |
| 318 | enum. The value for each branch can be of any type. |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 319 | |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 320 | A flat union definition specifies a struct as its base, and |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 321 | avoids nesting on the wire. All branches of the union must be |
| 322 | complex types, and the top-level fields of the union dictionary on |
| 323 | the wire will be combination of fields from both the base type and the |
| 324 | appropriate branch type (when merging two dictionaries, there must be |
| 325 | no keys in common). The 'discriminator' field must be the name of an |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 326 | enum-typed member of the base struct. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 327 | |
| 328 | The following example enhances the above simple union example by |
| 329 | adding a common field 'readonly', renaming the discriminator to |
| 330 | something more applicable, and reducing the number of {} required on |
| 331 | the wire: |
Kevin Wolf | 50f2bdc | 2013-07-03 15:58:57 +0200 | [diff] [blame] | 332 | |
Markus Armbruster | 94a3f0a | 2015-09-03 10:18:06 +0200 | [diff] [blame] | 333 | { 'enum': 'BlockdevDriver', 'data': [ 'file', 'qcow2' ] } |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 334 | { 'struct': 'BlockdevCommonOptions', |
Wenchao Xia | bceae76 | 2014-03-06 17:08:56 -0800 | [diff] [blame] | 335 | 'data': { 'driver': 'BlockdevDriver', 'readonly': 'bool' } } |
Kevin Wolf | 50f2bdc | 2013-07-03 15:58:57 +0200 | [diff] [blame] | 336 | { 'union': 'BlockdevOptions', |
| 337 | 'base': 'BlockdevCommonOptions', |
| 338 | 'discriminator': 'driver', |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 339 | 'data': { 'file': 'FileOptions', |
Kevin Wolf | 50f2bdc | 2013-07-03 15:58:57 +0200 | [diff] [blame] | 340 | 'qcow2': 'Qcow2Options' } } |
| 341 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 342 | Resulting in these JSON objects: |
Kevin Wolf | 50f2bdc | 2013-07-03 15:58:57 +0200 | [diff] [blame] | 343 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 344 | { "driver": "file", "readonly": true, |
| 345 | "filename": "/some/place/my-image" } |
| 346 | { "driver": "qcow2", "readonly": false, |
| 347 | "backing-file": "/some/place/my-image", "lazy-refcounts": true } |
| 348 | |
| 349 | Notice that in a flat union, the discriminator name is controlled by |
| 350 | the user, but because it must map to a base member with enum type, the |
| 351 | code generator can ensure that branches exist for all values of the |
| 352 | enum (although the order of the keys need not match the declaration of |
| 353 | the enum). In the resulting generated C data types, a flat union is |
| 354 | represented as a struct with the base member fields included directly, |
| 355 | and then a union of structures for each branch of the struct. |
| 356 | |
| 357 | A simple union can always be re-written as a flat union where the base |
| 358 | class has a single member named 'type', and where each branch of the |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 359 | union has a struct with a single member named 'data'. That is, |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 360 | |
| 361 | { 'union': 'Simple', 'data': { 'one': 'str', 'two': 'int' } } |
| 362 | |
| 363 | is identical on the wire to: |
| 364 | |
| 365 | { 'enum': 'Enum', 'data': ['one', 'two'] } |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 366 | { 'struct': 'Base', 'data': { 'type': 'Enum' } } |
| 367 | { 'struct': 'Branch1', 'data': { 'data': 'str' } } |
| 368 | { 'struct': 'Branch2', 'data': { 'data': 'int' } } |
Markus Armbruster | 94a3f0a | 2015-09-03 10:18:06 +0200 | [diff] [blame] | 369 | { 'union': 'Flat', 'base': 'Base', 'discriminator': 'type', |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 370 | 'data': { 'one': 'Branch1', 'two': 'Branch2' } } |
Kevin Wolf | 50f2bdc | 2013-07-03 15:58:57 +0200 | [diff] [blame] | 371 | |
| 372 | |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 373 | === Alternate types === |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 374 | |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 375 | Usage: { 'alternate': STRING, 'data': DICT } |
| 376 | |
| 377 | An alternate type is one that allows a choice between two or more JSON |
| 378 | data types (string, integer, number, or object, but currently not |
| 379 | array) on the wire. The definition is similar to a simple union type, |
| 380 | where each branch of the union names a QAPI type. For example: |
| 381 | |
| 382 | { 'alternate': 'BlockRef', |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 383 | 'data': { 'definition': 'BlockdevOptions', |
| 384 | 'reference': 'str' } } |
| 385 | |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 386 | Just like for a simple union, an implicit C enum 'NameKind' is created |
| 387 | to enumerate the branches for the alternate 'Name'. |
| 388 | |
| 389 | Unlike a union, the discriminator string is never passed on the wire |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 390 | for the Client JSON Protocol. Instead, the value's JSON type serves |
| 391 | as an implicit discriminator, which in turn means that an alternate |
| 392 | can only express a choice between types represented differently in |
| 393 | JSON. If a branch is typed as the 'bool' built-in, the alternate |
| 394 | accepts true and false; if it is typed as any of the various numeric |
| 395 | built-ins, it accepts a JSON number; if it is typed as a 'str' |
| 396 | built-in or named enum type, it accepts a JSON string; and if it is |
| 397 | typed as a complex type (struct or union), it accepts a JSON object. |
| 398 | Two different complex types, for instance, aren't permitted, because |
| 399 | both are represented as a JSON object. |
Eric Blake | 7b1b98c | 2015-05-04 09:05:12 -0600 | [diff] [blame] | 400 | |
| 401 | The example alternate declaration above allows using both of the |
| 402 | following example objects: |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 403 | |
| 404 | { "file": "my_existing_block_device_id" } |
| 405 | { "file": { "driver": "file", |
| 406 | "readonly": false, |
Eric Blake | 63922c6 | 2013-10-19 17:52:33 +0100 | [diff] [blame] | 407 | "filename": "/tmp/mydisk.qcow2" } } |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 408 | |
| 409 | |
Kevin Wolf | 5163149 | 2013-07-16 13:17:27 +0200 | [diff] [blame] | 410 | === Commands === |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 411 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 412 | Usage: { 'command': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT, |
Markus Armbruster | 9b090d4 | 2015-07-31 17:59:38 +0200 | [diff] [blame] | 413 | '*returns': TYPE-NAME, |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 414 | '*gen': false, '*success-response': false } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 415 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 416 | Commands are defined by using a dictionary containing several members, |
| 417 | where three members are most common. The 'command' member is a |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 418 | mandatory string, and determines the "execute" value passed in a |
| 419 | Client JSON Protocol command exchange. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 420 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 421 | The 'data' argument maps to the "arguments" dictionary passed in as |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 422 | part of a Client JSON Protocol command. The 'data' member is optional |
| 423 | and defaults to {} (an empty dictionary). If present, it must be the |
Markus Armbruster | 315932b | 2015-07-01 10:12:24 +0200 | [diff] [blame] | 424 | string name of a complex type, or a dictionary that declares an |
| 425 | anonymous type with the same semantics as a 'struct' expression, with |
| 426 | one exception noted below when 'gen' is used. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 427 | |
| 428 | The 'returns' member describes what will appear in the "return" field |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 429 | of a Client JSON Protocol reply on successful completion of a command. |
| 430 | The member is optional from the command declaration; if absent, the |
| 431 | "return" field will be an empty dictionary. If 'returns' is present, |
| 432 | it must be the string name of a complex or built-in type, a |
| 433 | one-element array containing the name of a complex or built-in type, |
Markus Armbruster | 9b090d4 | 2015-07-31 17:59:38 +0200 | [diff] [blame] | 434 | with one exception noted below when 'gen' is used. Although it is |
| 435 | permitted to have the 'returns' member name a built-in type or an |
| 436 | array of built-in types, any command that does this cannot be extended |
| 437 | to return additional information in the future; thus, new commands |
| 438 | should strongly consider returning a dictionary-based type or an array |
| 439 | of dictionaries, even if the dictionary only contains one field at the |
| 440 | present. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 441 | |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 442 | All commands in Client JSON Protocol use a dictionary to report |
| 443 | failure, with no way to specify that in QAPI. Where the error return |
| 444 | is different than the usual GenericError class in order to help the |
| 445 | client react differently to certain error conditions, it is worth |
| 446 | documenting this in the comments before the command declaration. |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 447 | |
| 448 | Some example commands: |
| 449 | |
| 450 | { 'command': 'my-first-command', |
| 451 | 'data': { 'arg1': 'str', '*arg2': 'str' } } |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 452 | { 'struct': 'MyType', 'data': { '*value': 'str' } } |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 453 | { 'command': 'my-second-command', |
| 454 | 'returns': [ 'MyType' ] } |
| 455 | |
Eric Blake | 363b426 | 2015-05-04 09:05:35 -0600 | [diff] [blame] | 456 | which would validate this Client JSON Protocol transaction: |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 457 | |
| 458 | => { "execute": "my-first-command", |
| 459 | "arguments": { "arg1": "hello" } } |
| 460 | <= { "return": { } } |
| 461 | => { "execute": "my-second-command" } |
| 462 | <= { "return": [ { "value": "one" }, { } ] } |
| 463 | |
| 464 | In rare cases, QAPI cannot express a type-safe representation of a |
Markus Armbruster | 2d21291 | 2015-09-16 13:06:27 +0200 | [diff] [blame] | 465 | corresponding Client JSON Protocol command. You then have to suppress |
| 466 | generation of a marshalling function by including a key 'gen' with |
| 467 | boolean value false, and instead write your own function. Please try |
| 468 | to avoid adding new commands that rely on this, and instead use |
| 469 | type-safe unions. For an example of this usage: |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 470 | |
| 471 | { 'command': 'netdev_add', |
Markus Armbruster | b8a9832 | 2015-09-16 13:06:26 +0200 | [diff] [blame] | 472 | 'data': {'type': 'str', 'id': 'str'}, |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 473 | 'gen': false } |
| 474 | |
| 475 | Normally, the QAPI schema is used to describe synchronous exchanges, |
| 476 | where a response is expected. But in some cases, the action of a |
| 477 | command is expected to change state in a way that a successful |
| 478 | response is not possible (although the command will still return a |
| 479 | normal dictionary error on failure). When a successful reply is not |
| 480 | possible, the command expression should include the optional key |
| 481 | 'success-response' with boolean value false. So far, only QGA makes |
| 482 | use of this field. |
| 483 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 484 | |
Wenchao Xia | 21cd70d | 2014-06-18 08:43:28 +0200 | [diff] [blame] | 485 | === Events === |
| 486 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 487 | Usage: { 'event': STRING, '*data': COMPLEX-TYPE-NAME-OR-DICT } |
| 488 | |
| 489 | Events are defined with the keyword 'event'. It is not allowed to |
| 490 | name an event 'MAX', since the generator also produces a C enumeration |
| 491 | of all event names with a generated _MAX value at the end. When |
| 492 | 'data' is also specified, additional info will be included in the |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 493 | event, with similar semantics to a 'struct' expression. Finally there |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 494 | will be C API generated in qapi-event.h; when called by QEMU code, a |
| 495 | message with timestamp will be emitted on the wire. |
Wenchao Xia | 21cd70d | 2014-06-18 08:43:28 +0200 | [diff] [blame] | 496 | |
| 497 | An example event is: |
| 498 | |
| 499 | { 'event': 'EVENT_C', |
| 500 | 'data': { '*a': 'int', 'b': 'str' } } |
| 501 | |
| 502 | Resulting in this JSON object: |
| 503 | |
| 504 | { "event": "EVENT_C", |
| 505 | "data": { "b": "test string" }, |
| 506 | "timestamp": { "seconds": 1267020223, "microseconds": 435656 } } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 507 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 508 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 509 | == Client JSON Protocol introspection == |
| 510 | |
| 511 | Clients of a Client JSON Protocol commonly need to figure out what |
| 512 | exactly the server (QEMU) supports. |
| 513 | |
| 514 | For this purpose, QMP provides introspection via command |
| 515 | query-qmp-schema. QGA currently doesn't support introspection. |
| 516 | |
| 517 | query-qmp-schema returns a JSON array of SchemaInfo objects. These |
| 518 | objects together describe the wire ABI, as defined in the QAPI schema. |
Eric Blake | f545504 | 2015-11-05 23:35:36 -0700 | [diff] [blame] | 519 | There is no specified order to the SchemaInfo objects returned; a |
| 520 | client must search for a particular name throughout the entire array |
| 521 | to learn more about that name, but is at least guaranteed that there |
| 522 | will be no collisions between type, command, and event names. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 523 | |
| 524 | However, the SchemaInfo can't reflect all the rules and restrictions |
| 525 | that apply to QMP. It's interface introspection (figuring out what's |
| 526 | there), not interface specification. The specification is in the QAPI |
| 527 | schema. To understand how QMP is to be used, you need to study the |
| 528 | QAPI schema. |
| 529 | |
| 530 | Like any other command, query-qmp-schema is itself defined in the QAPI |
| 531 | schema, along with the SchemaInfo type. This text attempts to give an |
| 532 | overview how things work. For details you need to consult the QAPI |
| 533 | schema. |
| 534 | |
| 535 | SchemaInfo objects have common members "name" and "meta-type", and |
| 536 | additional variant members depending on the value of meta-type. |
| 537 | |
| 538 | Each SchemaInfo object describes a wire ABI entity of a certain |
| 539 | meta-type: a command, event or one of several kinds of type. |
| 540 | |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 541 | SchemaInfo for commands and events have the same name as in the QAPI |
| 542 | schema. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 543 | |
| 544 | Command and event names are part of the wire ABI, but type names are |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 545 | not. Therefore, the SchemaInfo for types have auto-generated |
| 546 | meaningless names. For readability, the examples in this section use |
| 547 | meaningful type names instead. |
| 548 | |
| 549 | To examine a type, start with a command or event using it, then follow |
| 550 | references by name. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 551 | |
| 552 | QAPI schema definitions not reachable that way are omitted. |
| 553 | |
| 554 | The SchemaInfo for a command has meta-type "command", and variant |
| 555 | members "arg-type" and "ret-type". On the wire, the "arguments" |
| 556 | member of a client's "execute" command must conform to the object type |
| 557 | named by "arg-type". The "return" member that the server passes in a |
| 558 | success response conforms to the type named by "ret-type". |
| 559 | |
| 560 | If the command takes no arguments, "arg-type" names an object type |
| 561 | without members. Likewise, if the command returns nothing, "ret-type" |
| 562 | names an object type without members. |
| 563 | |
| 564 | Example: the SchemaInfo for command query-qmp-schema |
| 565 | |
| 566 | { "name": "query-qmp-schema", "meta-type": "command", |
| 567 | "arg-type": ":empty", "ret-type": "SchemaInfoList" } |
| 568 | |
| 569 | Type ":empty" is an object type without members, and type |
| 570 | "SchemaInfoList" is the array of SchemaInfo type. |
| 571 | |
| 572 | The SchemaInfo for an event has meta-type "event", and variant member |
| 573 | "arg-type". On the wire, a "data" member that the server passes in an |
| 574 | event conforms to the object type named by "arg-type". |
| 575 | |
| 576 | If the event carries no additional information, "arg-type" names an |
| 577 | object type without members. The event may not have a data member on |
| 578 | the wire then. |
| 579 | |
| 580 | Each command or event defined with dictionary-valued 'data' in the |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 581 | QAPI schema implicitly defines an object type. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 582 | |
| 583 | Example: the SchemaInfo for EVENT_C from section Events |
| 584 | |
| 585 | { "name": "EVENT_C", "meta-type": "event", |
| 586 | "arg-type": ":obj-EVENT_C-arg" } |
| 587 | |
| 588 | Type ":obj-EVENT_C-arg" is an implicitly defined object type with |
| 589 | the two members from the event's definition. |
| 590 | |
| 591 | The SchemaInfo for struct and union types has meta-type "object". |
| 592 | |
| 593 | The SchemaInfo for a struct type has variant member "members". |
| 594 | |
| 595 | The SchemaInfo for a union type additionally has variant members "tag" |
| 596 | and "variants". |
| 597 | |
| 598 | "members" is a JSON array describing the object's common members, if |
| 599 | any. Each element is a JSON object with members "name" (the member's |
| 600 | name), "type" (the name of its type), and optionally "default". The |
| 601 | member is optional if "default" is present. Currently, "default" can |
| 602 | only have value null. Other values are reserved for future |
Eric Blake | f545504 | 2015-11-05 23:35:36 -0700 | [diff] [blame] | 603 | extensions. The "members" array is in no particular order; clients |
| 604 | must search the entire object when learning whether a particular |
| 605 | member is supported. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 606 | |
| 607 | Example: the SchemaInfo for MyType from section Struct types |
| 608 | |
| 609 | { "name": "MyType", "meta-type": "object", |
| 610 | "members": [ |
| 611 | { "name": "member1", "type": "str" }, |
| 612 | { "name": "member2", "type": "int" }, |
| 613 | { "name": "member3", "type": "str", "default": null } ] } |
| 614 | |
| 615 | "tag" is the name of the common member serving as type tag. |
| 616 | "variants" is a JSON array describing the object's variant members. |
| 617 | Each element is a JSON object with members "case" (the value of type |
| 618 | tag this element applies to) and "type" (the name of an object type |
Eric Blake | f545504 | 2015-11-05 23:35:36 -0700 | [diff] [blame] | 619 | that provides the variant members for this type tag value). The |
| 620 | "variants" array is in no particular order, and is not guaranteed to |
| 621 | list cases in the same order as the corresponding "tag" enum type. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 622 | |
| 623 | Example: the SchemaInfo for flat union BlockdevOptions from section |
| 624 | Union types |
| 625 | |
| 626 | { "name": "BlockdevOptions", "meta-type": "object", |
| 627 | "members": [ |
| 628 | { "name": "driver", "type": "BlockdevDriver" }, |
| 629 | { "name": "readonly", "type": "bool"} ], |
| 630 | "tag": "driver", |
| 631 | "variants": [ |
| 632 | { "case": "file", "type": "FileOptions" }, |
| 633 | { "case": "qcow2", "type": "Qcow2Options" } ] } |
| 634 | |
| 635 | Note that base types are "flattened": its members are included in the |
| 636 | "members" array. |
| 637 | |
| 638 | A simple union implicitly defines an enumeration type for its implicit |
| 639 | discriminator (called "type" on the wire, see section Union types). |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 640 | |
| 641 | A simple union implicitly defines an object type for each of its |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 642 | variants. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 643 | |
| 644 | Example: the SchemaInfo for simple union BlockdevOptions from section |
| 645 | Union types |
| 646 | |
| 647 | { "name": "BlockdevOptions", "meta-type": "object", |
| 648 | "members": [ |
| 649 | { "name": "kind", "type": "BlockdevOptionsKind" } ], |
| 650 | "tag": "type", |
| 651 | "variants": [ |
| 652 | { "case": "file", "type": ":obj-FileOptions-wrapper" }, |
| 653 | { "case": "qcow2", "type": ":obj-Qcow2Options-wrapper" } ] } |
| 654 | |
| 655 | Enumeration type "BlockdevOptionsKind" and the object types |
| 656 | ":obj-FileOptions-wrapper", ":obj-Qcow2Options-wrapper" are |
| 657 | implicitly defined. |
| 658 | |
| 659 | The SchemaInfo for an alternate type has meta-type "alternate", and |
| 660 | variant member "members". "members" is a JSON array. Each element is |
| 661 | a JSON object with member "type", which names a type. Values of the |
Eric Blake | f545504 | 2015-11-05 23:35:36 -0700 | [diff] [blame] | 662 | alternate type conform to exactly one of its member types. There is |
| 663 | no guarantee on the order in which "members" will be listed. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 664 | |
| 665 | Example: the SchemaInfo for BlockRef from section Alternate types |
| 666 | |
| 667 | { "name": "BlockRef", "meta-type": "alternate", |
| 668 | "members": [ |
| 669 | { "type": "BlockdevOptions" }, |
| 670 | { "type": "str" } ] } |
| 671 | |
| 672 | The SchemaInfo for an array type has meta-type "array", and variant |
| 673 | member "element-type", which names the array's element type. Array |
Eric Blake | ce5fcb4 | 2015-11-05 23:35:35 -0700 | [diff] [blame] | 674 | types are implicitly defined. For convenience, the array's name may |
| 675 | resemble the element type; however, clients should examine member |
| 676 | "element-type" instead of making assumptions based on parsing member |
| 677 | "name". |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 678 | |
| 679 | Example: the SchemaInfo for ['str'] |
| 680 | |
Eric Blake | ce5fcb4 | 2015-11-05 23:35:35 -0700 | [diff] [blame] | 681 | { "name": "[str]", "meta-type": "array", |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 682 | "element-type": "str" } |
| 683 | |
| 684 | The SchemaInfo for an enumeration type has meta-type "enum" and |
Eric Blake | f545504 | 2015-11-05 23:35:36 -0700 | [diff] [blame] | 685 | variant member "values". The values are listed in no particular |
| 686 | order; clients must search the entire enum when learning whether a |
| 687 | particular value is supported. |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 688 | |
| 689 | Example: the SchemaInfo for MyEnum from section Enumeration types |
| 690 | |
| 691 | { "name": "MyEnum", "meta-type": "enum", |
| 692 | "values": [ "value1", "value2", "value3" ] } |
| 693 | |
| 694 | The SchemaInfo for a built-in type has the same name as the type in |
| 695 | the QAPI schema (see section Built-in Types), with one exception |
| 696 | detailed below. It has variant member "json-type" that shows how |
| 697 | values of this type are encoded on the wire. |
| 698 | |
| 699 | Example: the SchemaInfo for str |
| 700 | |
| 701 | { "name": "str", "meta-type": "builtin", "json-type": "string" } |
| 702 | |
| 703 | The QAPI schema supports a number of integer types that only differ in |
| 704 | how they map to C. They are identical as far as SchemaInfo is |
| 705 | concerned. Therefore, they get all mapped to a single type "int" in |
| 706 | SchemaInfo. |
| 707 | |
| 708 | As explained above, type names are not part of the wire ABI. Not even |
| 709 | the names of built-in types. Clients should examine member |
| 710 | "json-type" instead of hard-coding names of built-in types. |
| 711 | |
| 712 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 713 | == Code generation == |
| 714 | |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 715 | Schemas are fed into four scripts to generate all the code/files that, |
| 716 | paired with the core QAPI libraries, comprise everything required to |
| 717 | take JSON commands read in by a Client JSON Protocol server, unmarshal |
| 718 | the arguments into the underlying C types, call into the corresponding |
| 719 | C function, and map the response back to a Client JSON Protocol |
| 720 | response to be returned to the user. |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 721 | |
| 722 | As an example, we'll use the following schema, which describes a single |
| 723 | complex user-defined type (which will produce a C struct, along with a list |
| 724 | node structure that can be used to chain together a list of such types in |
| 725 | case we want to accept/return a list of this type with a command), and a |
| 726 | command which takes that type as a parameter and returns the same type: |
| 727 | |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 728 | $ cat example-schema.json |
Eric Blake | 3b2a8b8 | 2015-05-04 09:05:26 -0600 | [diff] [blame] | 729 | { 'struct': 'UserDefOne', |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 730 | 'data': { 'integer': 'int', 'string': 'str' } } |
| 731 | |
| 732 | { 'command': 'my-command', |
| 733 | 'data': {'arg1': 'UserDefOne'}, |
| 734 | 'returns': 'UserDefOne' } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 735 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 736 | { 'event': 'MY_EVENT' } |
| 737 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 738 | === scripts/qapi-types.py === |
| 739 | |
| 740 | Used to generate the C types defined by a schema. The following files are |
| 741 | created: |
| 742 | |
| 743 | $(prefix)qapi-types.h - C types corresponding to types defined in |
| 744 | the schema you pass in |
| 745 | $(prefix)qapi-types.c - Cleanup functions for the above C types |
| 746 | |
| 747 | The $(prefix) is an optional parameter used as a namespace to keep the |
| 748 | generated code from one schema/code-generation separated from others so code |
| 749 | can be generated/used from multiple schemas without clobbering previously |
| 750 | created code. |
| 751 | |
| 752 | Example: |
| 753 | |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 754 | $ python scripts/qapi-types.py --output-dir="qapi-generated" \ |
Markus Armbruster | 16d80f6 | 2015-04-02 13:32:16 +0200 | [diff] [blame] | 755 | --prefix="example-" example-schema.json |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 756 | $ cat qapi-generated/example-qapi-types.c |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 757 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 758 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 759 | void qapi_free_UserDefOne(UserDefOne *obj) |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 760 | { |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 761 | QapiDeallocVisitor *qdv; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 762 | Visitor *v; |
| 763 | |
| 764 | if (!obj) { |
| 765 | return; |
| 766 | } |
| 767 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 768 | qdv = qapi_dealloc_visitor_new(); |
| 769 | v = qapi_dealloc_get_visitor(qdv); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 770 | visit_type_UserDefOne(v, &obj, NULL, NULL); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 771 | qapi_dealloc_visitor_cleanup(qdv); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 772 | } |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 773 | |
| 774 | void qapi_free_UserDefOneList(UserDefOneList *obj) |
| 775 | { |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 776 | QapiDeallocVisitor *qdv; |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 777 | Visitor *v; |
| 778 | |
| 779 | if (!obj) { |
| 780 | return; |
| 781 | } |
| 782 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 783 | qdv = qapi_dealloc_visitor_new(); |
| 784 | v = qapi_dealloc_get_visitor(qdv); |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 785 | visit_type_UserDefOneList(v, &obj, NULL, NULL); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 786 | qapi_dealloc_visitor_cleanup(qdv); |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 787 | } |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 788 | $ cat qapi-generated/example-qapi-types.h |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 789 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 790 | |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 791 | #ifndef EXAMPLE_QAPI_TYPES_H |
| 792 | #define EXAMPLE_QAPI_TYPES_H |
| 793 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 794 | [Built-in types omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 795 | |
| 796 | typedef struct UserDefOne UserDefOne; |
| 797 | |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 798 | typedef struct UserDefOneList UserDefOneList; |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 799 | |
Markus Armbruster | 3a864e7 | 2015-07-01 16:55:15 +0200 | [diff] [blame] | 800 | struct UserDefOne { |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 801 | int64_t integer; |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 802 | char *string; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 803 | }; |
| 804 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 805 | void qapi_free_UserDefOne(UserDefOne *obj); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 806 | |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 807 | struct UserDefOneList { |
| 808 | union { |
| 809 | UserDefOne *value; |
| 810 | uint64_t padding; |
| 811 | }; |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 812 | UserDefOneList *next; |
Markus Armbruster | 2b162cc | 2015-09-16 13:06:09 +0200 | [diff] [blame] | 813 | }; |
| 814 | |
| 815 | void qapi_free_UserDefOneList(UserDefOneList *obj); |
| 816 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 817 | #endif |
| 818 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 819 | === scripts/qapi-visit.py === |
| 820 | |
| 821 | Used to generate the visitor functions used to walk through and convert |
| 822 | a QObject (as provided by QMP) to a native C data structure and |
| 823 | vice-versa, as well as the visitor function used to dealloc a complex |
| 824 | schema-defined C type. |
| 825 | |
| 826 | The following files are generated: |
| 827 | |
| 828 | $(prefix)qapi-visit.c: visitor function for a particular C type, used |
| 829 | to automagically convert QObjects into the |
| 830 | corresponding C type and vice-versa, as well |
| 831 | as for deallocating memory for an existing C |
| 832 | type |
| 833 | |
| 834 | $(prefix)qapi-visit.h: declarations for previously mentioned visitor |
| 835 | functions |
| 836 | |
| 837 | Example: |
| 838 | |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 839 | $ python scripts/qapi-visit.py --output-dir="qapi-generated" |
Markus Armbruster | 16d80f6 | 2015-04-02 13:32:16 +0200 | [diff] [blame] | 840 | --prefix="example-" example-schema.json |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 841 | $ cat qapi-generated/example-qapi-visit.c |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 842 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 843 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 844 | static void visit_type_UserDefOne_fields(Visitor *v, UserDefOne **obj, Error **errp) |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 845 | { |
| 846 | Error *err = NULL; |
Markus Armbruster | 3a864e7 | 2015-07-01 16:55:15 +0200 | [diff] [blame] | 847 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 848 | visit_type_int(v, &(*obj)->integer, "integer", &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 849 | if (err) { |
| 850 | goto out; |
| 851 | } |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 852 | visit_type_str(v, &(*obj)->string, "string", &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 853 | if (err) { |
| 854 | goto out; |
| 855 | } |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 856 | |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 857 | out: |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 858 | error_propagate(errp, err); |
| 859 | } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 860 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 861 | void visit_type_UserDefOne(Visitor *v, UserDefOne **obj, const char *name, Error **errp) |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 862 | { |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 863 | Error *err = NULL; |
| 864 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 865 | visit_start_struct(v, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 866 | if (!err) { |
| 867 | if (*obj) { |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 868 | visit_type_UserDefOne_fields(v, obj, errp); |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 869 | } |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 870 | visit_end_struct(v, &err); |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 871 | } |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 872 | error_propagate(errp, err); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 873 | } |
| 874 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 875 | void visit_type_UserDefOneList(Visitor *v, UserDefOneList **obj, const char *name, Error **errp) |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 876 | { |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 877 | Error *err = NULL; |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 878 | GenericList *i, **prev; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 879 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 880 | visit_start_list(v, name, &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 881 | if (err) { |
| 882 | goto out; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 883 | } |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 884 | |
| 885 | for (prev = (GenericList **)obj; |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 886 | !err && (i = visit_next_list(v, prev, &err)) != NULL; |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 887 | prev = &i) { |
| 888 | UserDefOneList *native_i = (UserDefOneList *)i; |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 889 | visit_type_UserDefOne(v, &native_i->value, NULL, &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | error_propagate(errp, err); |
| 893 | err = NULL; |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 894 | visit_end_list(v, &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 895 | out: |
| 896 | error_propagate(errp, err); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 897 | } |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 898 | $ cat qapi-generated/example-qapi-visit.h |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 899 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 900 | |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 901 | #ifndef EXAMPLE_QAPI_VISIT_H |
| 902 | #define EXAMPLE_QAPI_VISIT_H |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 903 | |
Eric Blake | e790e66 | 2015-05-04 09:04:59 -0600 | [diff] [blame] | 904 | [Visitors for built-in types omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 905 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 906 | void visit_type_UserDefOne(Visitor *v, UserDefOne **obj, const char *name, Error **errp); |
| 907 | void visit_type_UserDefOneList(Visitor *v, UserDefOneList **obj, const char *name, Error **errp); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 908 | |
| 909 | #endif |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 910 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 911 | === scripts/qapi-commands.py === |
| 912 | |
| 913 | Used to generate the marshaling/dispatch functions for the commands defined |
| 914 | in the schema. The following files are generated: |
| 915 | |
| 916 | $(prefix)qmp-marshal.c: command marshal/dispatch functions for each |
| 917 | QMP command defined in the schema. Functions |
| 918 | generated by qapi-visit.py are used to |
Stefan Weil | 2542bfd | 2011-08-28 21:45:40 +0200 | [diff] [blame] | 919 | convert QObjects received from the wire into |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 920 | function parameters, and uses the same |
| 921 | visitor functions to convert native C return |
| 922 | values to QObjects from transmission back |
| 923 | over the wire. |
| 924 | |
| 925 | $(prefix)qmp-commands.h: Function prototypes for the QMP commands |
| 926 | specified in the schema. |
| 927 | |
| 928 | Example: |
| 929 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 930 | $ python scripts/qapi-commands.py --output-dir="qapi-generated" |
Markus Armbruster | 16d80f6 | 2015-04-02 13:32:16 +0200 | [diff] [blame] | 931 | --prefix="example-" example-schema.json |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 932 | $ cat qapi-generated/example-qmp-marshal.c |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 933 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 934 | |
Markus Armbruster | 56d92b0 | 2015-09-16 13:06:21 +0200 | [diff] [blame] | 935 | static void qmp_marshal_output_UserDefOne(UserDefOne *ret_in, QObject **ret_out, Error **errp) |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 936 | { |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 937 | Error *err = NULL; |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 938 | QmpOutputVisitor *qov = qmp_output_visitor_new(); |
| 939 | QapiDeallocVisitor *qdv; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 940 | Visitor *v; |
| 941 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 942 | v = qmp_output_get_visitor(qov); |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 943 | visit_type_UserDefOne(v, &ret_in, "unused", &err); |
| 944 | if (err) { |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 945 | goto out; |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 946 | } |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 947 | *ret_out = qmp_output_get_qobject(qov); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 948 | |
| 949 | out: |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 950 | error_propagate(errp, err); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 951 | qmp_output_visitor_cleanup(qov); |
| 952 | qdv = qapi_dealloc_visitor_new(); |
| 953 | v = qapi_dealloc_get_visitor(qdv); |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 954 | visit_type_UserDefOne(v, &ret_in, "unused", NULL); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 955 | qapi_dealloc_visitor_cleanup(qdv); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 956 | } |
| 957 | |
Markus Armbruster | 7fad30f | 2015-09-16 13:06:19 +0200 | [diff] [blame] | 958 | static void qmp_marshal_my_command(QDict *args, QObject **ret, Error **errp) |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 959 | { |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 960 | Error *err = NULL; |
Markus Armbruster | 3f99144 | 2015-07-31 18:51:18 +0200 | [diff] [blame] | 961 | UserDefOne *retval; |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 962 | QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args)); |
| 963 | QapiDeallocVisitor *qdv; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 964 | Visitor *v; |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 965 | UserDefOne *arg1 = NULL; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 966 | |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 967 | v = qmp_input_get_visitor(qiv); |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 968 | visit_type_UserDefOne(v, &arg1, "arg1", &err); |
| 969 | if (err) { |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 970 | goto out; |
| 971 | } |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 972 | |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 973 | retval = qmp_my_command(arg1, &err); |
| 974 | if (err) { |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 975 | goto out; |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 976 | } |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 977 | |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 978 | qmp_marshal_output_UserDefOne(retval, ret, &err); |
Markus Armbruster | 297a364 | 2014-05-07 09:53:54 +0200 | [diff] [blame] | 979 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 980 | out: |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 981 | error_propagate(errp, err); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 982 | qmp_input_visitor_cleanup(qiv); |
| 983 | qdv = qapi_dealloc_visitor_new(); |
| 984 | v = qapi_dealloc_get_visitor(qdv); |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 985 | visit_type_UserDefOne(v, &arg1, "arg1", NULL); |
Eric Blake | f8b7f1a | 2015-09-29 16:21:09 -0600 | [diff] [blame] | 986 | qapi_dealloc_visitor_cleanup(qdv); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | static void qmp_init_marshal(void) |
| 990 | { |
Markus Armbruster | 7fad30f | 2015-09-16 13:06:19 +0200 | [diff] [blame] | 991 | qmp_register_command("my-command", qmp_marshal_my_command, QCO_NO_OPTIONS); |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | qapi_init(qmp_init_marshal); |
Markus Armbruster | 87a560c | 2014-05-14 17:27:23 +0200 | [diff] [blame] | 995 | $ cat qapi-generated/example-qmp-commands.h |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 996 | [Uninteresting stuff omitted...] |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 997 | |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 998 | #ifndef EXAMPLE_QMP_COMMANDS_H |
| 999 | #define EXAMPLE_QMP_COMMANDS_H |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 1000 | |
| 1001 | #include "example-qapi-types.h" |
Markus Armbruster | 6e2bb3e | 2014-05-07 09:53:43 +0200 | [diff] [blame] | 1002 | #include "qapi/qmp/qdict.h" |
| 1003 | #include "qapi/error.h" |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 1004 | |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1005 | UserDefOne *qmp_my_command(UserDefOne *arg1, Error **errp); |
| 1006 | |
| 1007 | #endif |
| 1008 | |
| 1009 | === scripts/qapi-event.py === |
| 1010 | |
| 1011 | Used to generate the event-related C code defined by a schema. The |
| 1012 | following files are created: |
| 1013 | |
| 1014 | $(prefix)qapi-event.h - Function prototypes for each event type, plus an |
| 1015 | enumeration of all event names |
| 1016 | $(prefix)qapi-event.c - Implementation of functions to send an event |
| 1017 | |
| 1018 | Example: |
| 1019 | |
| 1020 | $ python scripts/qapi-event.py --output-dir="qapi-generated" |
Markus Armbruster | 16d80f6 | 2015-04-02 13:32:16 +0200 | [diff] [blame] | 1021 | --prefix="example-" example-schema.json |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1022 | $ cat qapi-generated/example-qapi-event.c |
| 1023 | [Uninteresting stuff omitted...] |
| 1024 | |
| 1025 | void qapi_event_send_my_event(Error **errp) |
| 1026 | { |
| 1027 | QDict *qmp; |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 1028 | Error *err = NULL; |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1029 | QMPEventFuncEmit emit; |
| 1030 | emit = qmp_event_get_func_emit(); |
| 1031 | if (!emit) { |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | qmp = qmp_event_build_dict("MY_EVENT"); |
| 1036 | |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 1037 | emit(EXAMPLE_QAPI_EVENT_MY_EVENT, qmp, &err); |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1038 | |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 1039 | error_propagate(errp, err); |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1040 | QDECREF(qmp); |
| 1041 | } |
| 1042 | |
Markus Armbruster | efd2eaa | 2015-09-16 13:06:12 +0200 | [diff] [blame] | 1043 | const char *const example_QAPIEvent_lookup[] = { |
| 1044 | [EXAMPLE_QAPI_EVENT_MY_EVENT] = "MY_EVENT", |
| 1045 | [EXAMPLE_QAPI_EVENT_MAX] = NULL, |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1046 | }; |
| 1047 | $ cat qapi-generated/example-qapi-event.h |
| 1048 | [Uninteresting stuff omitted...] |
| 1049 | |
| 1050 | #ifndef EXAMPLE_QAPI_EVENT_H |
| 1051 | #define EXAMPLE_QAPI_EVENT_H |
| 1052 | |
| 1053 | #include "qapi/error.h" |
| 1054 | #include "qapi/qmp/qdict.h" |
| 1055 | #include "example-qapi-types.h" |
| 1056 | |
| 1057 | |
| 1058 | void qapi_event_send_my_event(Error **errp); |
| 1059 | |
Markus Armbruster | 3a864e7 | 2015-07-01 16:55:15 +0200 | [diff] [blame] | 1060 | typedef enum example_QAPIEvent { |
Eric Blake | 59a2c4c | 2014-09-26 09:20:33 -0600 | [diff] [blame] | 1061 | EXAMPLE_QAPI_EVENT_MY_EVENT = 0, |
| 1062 | EXAMPLE_QAPI_EVENT_MAX = 1, |
Markus Armbruster | 016a335 | 2015-07-01 12:59:40 +0200 | [diff] [blame] | 1063 | } example_QAPIEvent; |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 1064 | |
Markus Armbruster | efd2eaa | 2015-09-16 13:06:12 +0200 | [diff] [blame] | 1065 | extern const char *const example_QAPIEvent_lookup[]; |
| 1066 | |
Michael Roth | b84da83 | 2011-07-19 14:50:46 -0500 | [diff] [blame] | 1067 | #endif |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1068 | |
| 1069 | === scripts/qapi-introspect.py === |
| 1070 | |
| 1071 | Used to generate the introspection C code for a schema. The following |
| 1072 | files are created: |
| 1073 | |
| 1074 | $(prefix)qmp-introspect.c - Defines a string holding a JSON |
| 1075 | description of the schema. |
| 1076 | $(prefix)qmp-introspect.h - Declares the above string. |
| 1077 | |
| 1078 | Example: |
| 1079 | |
| 1080 | $ python scripts/qapi-introspect.py --output-dir="qapi-generated" |
| 1081 | --prefix="example-" example-schema.json |
| 1082 | $ cat qapi-generated/example-qmp-introspect.c |
| 1083 | [Uninteresting stuff omitted...] |
| 1084 | |
| 1085 | const char example_qmp_schema_json[] = "[" |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 1086 | "{\"arg-type\": \"0\", \"meta-type\": \"event\", \"name\": \"MY_EVENT\"}, " |
| 1087 | "{\"arg-type\": \"1\", \"meta-type\": \"command\", \"name\": \"my-command\", \"ret-type\": \"2\"}, " |
| 1088 | "{\"members\": [], \"meta-type\": \"object\", \"name\": \"0\"}, " |
| 1089 | "{\"members\": [{\"name\": \"arg1\", \"type\": \"2\"}], \"meta-type\": \"object\", \"name\": \"1\"}, " |
| 1090 | "{\"members\": [{\"name\": \"integer\", \"type\": \"int\"}, {\"name\": \"string\", \"type\": \"str\"}], \"meta-type\": \"object\", \"name\": \"2\"}, " |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1091 | "{\"json-type\": \"int\", \"meta-type\": \"builtin\", \"name\": \"int\"}, " |
Markus Armbruster | 1a9a507 | 2015-09-16 13:06:29 +0200 | [diff] [blame] | 1092 | "{\"json-type\": \"string\", \"meta-type\": \"builtin\", \"name\": \"str\"}]"; |
Markus Armbruster | 39a1815 | 2015-09-16 13:06:28 +0200 | [diff] [blame] | 1093 | $ cat qapi-generated/example-qmp-introspect.h |
| 1094 | [Uninteresting stuff omitted...] |
| 1095 | |
| 1096 | #ifndef EXAMPLE_QMP_INTROSPECT_H |
| 1097 | #define EXAMPLE_QMP_INTROSPECT_H |
| 1098 | |
| 1099 | extern const char example_qmp_schema_json[]; |
| 1100 | |
| 1101 | #endif |