]> git.ipfire.org Git - thirdparty/qemu.git/blame - scripts/qapi/commands.py
Merge tag 'pull-target-arm-20250704' of https://gitlab.com/pm215/qemu into staging
[thirdparty/qemu.git] / scripts / qapi / commands.py
CommitLineData
5ddeec83
MA
1"""
2QAPI command marshaller generator
3
4Copyright IBM, Corp. 2011
5Copyright (C) 2014-2018 Red Hat, Inc.
6
7Authors:
8 Anthony Liguori <aliguori@us.ibm.com>
9 Michael Roth <mdroth@linux.vnet.ibm.com>
10 Markus Armbruster <armbru@redhat.com>
11
12This work is licensed under the terms of the GNU GPL, version 2.
13See the COPYING file in the top-level directory.
14"""
c17d9908 15
7304721f
JS
16from typing import (
17 Dict,
18 List,
19 Optional,
20 Set,
21)
22
e6a34cd7
JS
23from .common import c_name, mcgen
24from .gen import (
7304721f 25 QAPIGenC,
e6a34cd7
JS
26 QAPISchemaModularCVisitor,
27 build_params,
ba27dccc 28 gen_features,
90254ec4 29 ifcontext,
e6a34cd7 30)
7304721f
JS
31from .schema import (
32 QAPISchema,
33 QAPISchemaFeature,
f17539c8 34 QAPISchemaIfCond,
7304721f
JS
35 QAPISchemaObjectType,
36 QAPISchemaType,
37)
38from .source import QAPISourceInfo
c17d9908 39
e98859a9 40
7304721f
JS
41def gen_command_decl(name: str,
42 arg_type: Optional[QAPISchemaObjectType],
43 boxed: bool,
cf9d4e68
PB
44 ret_type: Optional[QAPISchemaType],
45 coroutine: bool) -> str:
c17d9908 46 return mcgen('''
cf9d4e68 47%(c_type)s %(coroutine_fn)sqmp_%(c_name)s(%(params)s);
c17d9908 48''',
e98859a9 49 c_type=(ret_type and ret_type.c_type()) or 'void',
cf9d4e68 50 coroutine_fn='coroutine_fn ' if coroutine else '',
e98859a9 51 c_name=c_name(name),
086ee7a6 52 params=build_params(arg_type, boxed, 'Error **errp'))
e98859a9 53
c17d9908 54
7304721f
JS
55def gen_call(name: str,
56 arg_type: Optional[QAPISchemaObjectType],
57 boxed: bool,
bd2017bc
VSO
58 ret_type: Optional[QAPISchemaType],
59 gen_tracing: bool) -> str:
e98859a9
MA
60 ret = ''
61
62 argstr = ''
48825ca4 63 if boxed:
675b214b 64 assert arg_type
c818408e 65 argstr = '&arg, '
48825ca4 66 elif arg_type:
3ff2a5a3 67 assert not arg_type.branches
e98859a9 68 for memb in arg_type.members:
de3b3f52 69 assert not memb.ifcond.is_present()
44ea9d9b 70 if memb.need_has():
386230a2
EB
71 argstr += 'arg.has_%s, ' % c_name(memb.name)
72 argstr += 'arg.%s, ' % c_name(memb.name)
e98859a9
MA
73
74 lhs = ''
75 if ret_type:
76 lhs = 'retval = '
77
bd2017bc
VSO
78 name = c_name(name)
79 upper = name.upper()
80
81 if gen_tracing:
82 ret += mcgen('''
83
84 if (trace_event_get_state_backends(TRACE_QMP_ENTER_%(upper)s)) {
85 g_autoptr(GString) req_json = qobject_to_json(QOBJECT(args));
86
87 trace_qmp_enter_%(name)s(req_json->str);
88 }
7df18461 89''',
bd2017bc
VSO
90 upper=upper, name=name)
91
92 ret += mcgen('''
f1538019 93
bd2017bc 94 %(lhs)sqmp_%(name)s(%(args)s&err);
c17d9908 95''',
bd2017bc 96 name=name, args=argstr, lhs=lhs)
167d913f
VSO
97
98 ret += mcgen('''
fa274ed6 99 if (err) {
bd2017bc
VSO
100''')
101
102 if gen_tracing:
103 ret += mcgen('''
104 trace_qmp_exit_%(name)s(error_get_pretty(err), false);
105''',
106 name=name)
107
108 ret += mcgen('''
167d913f 109 error_propagate(errp, err);
fa274ed6
EB
110 goto out;
111 }
167d913f
VSO
112''')
113
114 if ret_type:
115 ret += mcgen('''
e02bca28 116
cdd2b228 117 qmp_marshal_output_%(c_name)s(retval, ret, errp);
c17d9908 118''',
56d92b00 119 c_name=ret_type.c_name())
bd2017bc
VSO
120
121 if gen_tracing:
122 if ret_type:
123 ret += mcgen('''
124
125 if (trace_event_get_state_backends(TRACE_QMP_EXIT_%(upper)s)) {
126 g_autoptr(GString) ret_json = qobject_to_json(*ret);
127
128 trace_qmp_exit_%(name)s(ret_json->str, true);
129 }
7df18461 130''',
bd2017bc
VSO
131 upper=upper, name=name)
132 else:
133 ret += mcgen('''
134
135 trace_qmp_exit_%(name)s("{}", true);
7df18461 136''',
bd2017bc
VSO
137 name=name)
138
1f9a7a1a 139 return ret
c17d9908 140
e98859a9 141
7304721f 142def gen_marshal_output(ret_type: QAPISchemaType) -> str:
f1538019 143 return mcgen('''
ee446028 144
42c0dd12
JS
145static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in,
146 QObject **ret_out, Error **errp)
c17d9908 147{
c17d9908
MR
148 Visitor *v;
149
91fa93e5 150 v = qobject_output_visitor_new_qmp(ret_out);
cdd2b228 151 if (visit_type_%(c_name)s(v, "unused", &ret_in, errp)) {
3b098d56 152 visit_complete(v, ret_out);
c17d9908 153 }
2c0ef9f4
EB
154 visit_free(v);
155 v = qapi_dealloc_visitor_new();
51e72bc1 156 visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
2c0ef9f4 157 visit_free(v);
c17d9908
MR
158}
159''',
56d92b00 160 c_type=ret_type.c_type(), c_name=ret_type.c_name())
c17d9908 161
e98859a9 162
cf9d4e68
PB
163def build_marshal_proto(name: str,
164 coroutine: bool) -> str:
165 return ('void %(coroutine_fn)sqmp_marshal_%(c_name)s(%(params)s)' % {
166 'coroutine_fn': 'coroutine_fn ' if coroutine else '',
167 'c_name': c_name(name),
168 'params': 'QDict *args, QObject **ret, Error **errp',
169 })
776574d6 170
e98859a9 171
cf9d4e68
PB
172def gen_marshal_decl(name: str,
173 coroutine: bool) -> str:
f1538019
MA
174 return mcgen('''
175%(proto)s;
176''',
cf9d4e68 177 proto=build_marshal_proto(name, coroutine))
f1538019 178
776574d6 179
bd2017bc
VSO
180def gen_trace(name: str) -> str:
181 return mcgen('''
182qmp_enter_%(name)s(const char *json) "%%s"
183qmp_exit_%(name)s(const char *result, bool succeeded) "%%s %%d"
184''',
185 name=c_name(name))
186
187
7304721f
JS
188def gen_marshal(name: str,
189 arg_type: Optional[QAPISchemaObjectType],
190 boxed: bool,
bd2017bc 191 ret_type: Optional[QAPISchemaType],
cf9d4e68
PB
192 gen_tracing: bool,
193 coroutine: bool) -> str:
675b214b 194 have_args = boxed or (arg_type and not arg_type.is_empty())
ec9697ab
JS
195 if have_args:
196 assert arg_type is not None
197 arg_type_c_name = arg_type.c_name()
a0067da1 198
c17d9908 199 ret = mcgen('''
ee446028 200
f1538019 201%(proto)s
c17d9908 202{
c1ff0e6c 203 Error *err = NULL;
cdd2b228 204 bool ok = false;
2061487b 205 Visitor *v;
c17d9908 206''',
cf9d4e68 207 proto=build_marshal_proto(name, coroutine))
c17d9908 208
c1ff0e6c
EB
209 if ret_type:
210 ret += mcgen('''
211 %(c_type)s retval;
212''',
213 c_type=ret_type.c_type())
214
a0067da1 215 if have_args:
c1ff0e6c 216 ret += mcgen('''
c1ff0e6c 217 %(c_name)s arg = {0};
a0067da1 218''',
ec9697ab 219 c_name=arg_type_c_name)
a0067da1
MAL
220
221 ret += mcgen('''
2061487b 222
db291641 223 v = qobject_input_visitor_new_qmp(QOBJECT(args));
cdd2b228 224 if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
ed841535
EB
225 goto out;
226 }
89bf68f9
MA
227''')
228
229 if have_args:
230 ret += mcgen('''
cdd2b228
MA
231 if (visit_type_%(c_arg_type)s_members(v, &arg, errp)) {
232 ok = visit_check_struct(v, errp);
15c2f669 233 }
89bf68f9 234''',
ec9697ab 235 c_arg_type=arg_type_c_name)
89bf68f9
MA
236 else:
237 ret += mcgen('''
cdd2b228 238 ok = visit_check_struct(v, errp);
89bf68f9
MA
239''')
240
241 ret += mcgen('''
1158bb2a 242 visit_end_struct(v, NULL);
cdd2b228 243 if (!ok) {
c1ff0e6c
EB
244 goto out;
245 }
89bf68f9 246''')
c1ff0e6c 247
bd2017bc 248 ret += gen_call(name, arg_type, boxed, ret_type, gen_tracing)
1f9a7a1a 249
a0067da1 250 ret += mcgen('''
c17d9908
MR
251
252out:
a0067da1 253 visit_free(v);
1f9a7a1a 254''')
a0067da1 255
a0067da1 256 ret += mcgen('''
2c0ef9f4 257 v = qapi_dealloc_visitor_new();
ed841535 258 visit_start_struct(v, NULL, NULL, 0, NULL);
89bf68f9
MA
259''')
260
261 if have_args:
262 ret += mcgen('''
263 visit_type_%(c_arg_type)s_members(v, &arg, NULL);
264''',
ec9697ab 265 c_arg_type=arg_type_c_name)
89bf68f9
MA
266
267 ret += mcgen('''
1158bb2a 268 visit_end_struct(v, NULL);
2c0ef9f4 269 visit_free(v);
89bf68f9 270''')
a0067da1 271
1f9a7a1a 272 ret += mcgen('''
485febc6 273}
1f9a7a1a 274''')
c17d9908
MR
275 return ret
276
e98859a9 277
7304721f 278def gen_register_command(name: str,
d2032598 279 features: List[QAPISchemaFeature],
7304721f
JS
280 success_response: bool,
281 allow_oob: bool,
282 allow_preconfig: bool,
283 coroutine: bool) -> str:
876c6751
PX
284 options = []
285
ee446028 286 if not success_response:
876c6751
PX
287 options += ['QCO_NO_SUCCESS_RESP']
288 if allow_oob:
289 options += ['QCO_ALLOW_OOB']
d6fe3d02
IM
290 if allow_preconfig:
291 options += ['QCO_ALLOW_PRECONFIG']
04f22362
KW
292 if coroutine:
293 options += ['QCO_COROUTINE']
876c6751 294
ee446028 295 ret = mcgen('''
c2613949 296 qmp_register_command(cmds, "%(name)s",
6604e475 297 qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
c17d9908 298''',
e98859a9 299 name=name, c_name=c_name(name),
6604e475 300 opts=' | '.join(options) or 0,
ba27dccc 301 feats=gen_features(features))
ee446028
MA
302 return ret
303
e98859a9 304
252dc310 305class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
bd2017bc 306 def __init__(self, prefix: str, gen_tracing: bool):
2cae67bc
MA
307 super().__init__(
308 prefix, 'qapi-commands',
bd2017bc
VSO
309 ' * Schema-defined QAPI/QMP commands', None, __doc__,
310 gen_tracing=gen_tracing)
7304721f 311 self._visited_ret_types: Dict[QAPIGenC, Set[QAPISchemaType]] = {}
bd2017bc 312 self._gen_tracing = gen_tracing
252dc310 313
7304721f 314 def _begin_user_module(self, name: str) -> None:
252dc310 315 self._visited_ret_types[self._genc] = set()
9af23989
MA
316 commands = self._module_basename('qapi-commands', name)
317 types = self._module_basename('qapi-types', name)
318 visit = self._module_basename('qapi-visit', name)
71b3f045 319 self._genc.add(mcgen('''
9167ebd9 320#include "qemu/osdep.h"
91fa93e5 321#include "qapi/compat-policy.h"
4180978c 322#include "qapi/visitor.h"
407bc4bf 323#include "qobject/qdict.h"
4180978c 324#include "qapi/dealloc-visitor.h"
e688df6b 325#include "qapi/error.h"
9af23989
MA
326#include "%(visit)s.h"
327#include "%(commands)s.h"
4180978c 328''',
9af23989 329 commands=commands, visit=visit))
bd2017bc
VSO
330
331 if self._gen_tracing and commands != 'qapi-commands':
332 self._genc.add(mcgen('''
407bc4bf 333#include "qobject/qjson.h"
bd2017bc
VSO
334#include "trace/trace-%(nm)s_trace_events.h"
335''',
336 nm=c_name(commands, protect=False)))
337 # We use c_name(commands, protect=False) to turn '-' into '_', to
338 # match .underscorify() in trace/meson.build
339
71b3f045 340 self._genh.add(mcgen('''
9af23989 341#include "%(types)s.h"
4180978c
MA
342
343''',
9af23989 344 types=types))
71b3f045 345
c6cd7e41 346 def visit_begin(self, schema: QAPISchema) -> None:
4ab0ff6d 347 self._add_module('./init', ' * QAPI Commands initialization')
00ca24ff 348 self._genh.add(mcgen('''
153b0989 349#include "qapi/qmp-registry.h"
00ca24ff 350
252dc310
MA
351void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
352''',
8ec0e1a4 353 c_prefix=c_name(self._prefix, protect=False)))
c6cd7e41 354 self._genc.add(mcgen('''
00ca24ff
MA
355#include "qemu/osdep.h"
356#include "%(prefix)sqapi-commands.h"
357#include "%(prefix)sqapi-init-commands.h"
2ebb09f3 358#include "%(prefix)sqapi-features.h"
c6cd7e41
MA
359
360void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
361{
362 QTAILQ_INIT(cmds);
363
00ca24ff 364''',
c6cd7e41
MA
365 prefix=self._prefix,
366 c_prefix=c_name(self._prefix, protect=False)))
367
368 def visit_end(self) -> None:
369 with self._temp_module('./init'):
370 self._genc.add(mcgen('''
371}
372'''))
71b3f045 373
7304721f
JS
374 def visit_command(self,
375 name: str,
4a82e468 376 info: Optional[QAPISourceInfo],
f17539c8 377 ifcond: QAPISchemaIfCond,
7304721f
JS
378 features: List[QAPISchemaFeature],
379 arg_type: Optional[QAPISchemaObjectType],
380 ret_type: Optional[QAPISchemaType],
381 gen: bool,
382 success_response: bool,
383 boxed: bool,
384 allow_oob: bool,
385 allow_preconfig: bool,
386 coroutine: bool) -> None:
71b3f045
MA
387 if not gen:
388 return
1f7b9f31
MAL
389 # FIXME: If T is a user-defined type, the user is responsible
390 # for making this work, i.e. to make T's condition the
391 # conjunction of the T-returning commands' conditions. If T
392 # is a built-in type, this isn't possible: the
393 # qmp_marshal_output_T() will be generated unconditionally.
252dc310
MA
394 if ret_type and ret_type not in self._visited_ret_types[self._genc]:
395 self._visited_ret_types[self._genc].add(ret_type)
1f7b9f31 396 with ifcontext(ret_type.ifcond,
c6cd7e41 397 self._genh, self._genc):
1f7b9f31 398 self._genc.add(gen_marshal_output(ret_type))
c6cd7e41 399 with ifcontext(ifcond, self._genh, self._genc):
cf9d4e68
PB
400 self._genh.add(gen_command_decl(name, arg_type, boxed,
401 ret_type, coroutine))
402 self._genh.add(gen_marshal_decl(name, coroutine))
bd2017bc 403 self._genc.add(gen_marshal(name, arg_type, boxed, ret_type,
cf9d4e68 404 self._gen_tracing, coroutine))
bd2017bc
VSO
405 if self._gen_tracing:
406 self._gen_trace_events.add(gen_trace(name))
c6cd7e41
MA
407 with self._temp_module('./init'):
408 with ifcontext(ifcond, self._genh, self._genc):
d2032598
MA
409 self._genc.add(gen_register_command(
410 name, features, success_response, allow_oob,
411 allow_preconfig, coroutine))
71b3f045 412
26df4e7f 413
7304721f
JS
414def gen_commands(schema: QAPISchema,
415 output_dir: str,
bd2017bc
VSO
416 prefix: str,
417 gen_tracing: bool) -> None:
418 vis = QAPISchemaGenCommandVisitor(prefix, gen_tracing)
26df4e7f 419 schema.visit(vis)
71b3f045 420 vis.write(output_dir)