libqos: use microseconds instead of iterations for virtio timeout
[qemu.git] / scripts / qapi-commands.py
blob053ba85b5f17af721f192dc73f86e78fdedfee3a
2 # QAPI command marshaller generator
4 # Copyright IBM, Corp. 2011
5 # Copyright (C) 2014 Red Hat, Inc.
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Michael Roth <mdroth@linux.vnet.ibm.com>
10 # Markus Armbruster <armbru@redhat.com>
12 # This work is licensed under the terms of the GNU GPL, version 2.
13 # See the COPYING file in the top-level directory.
15 from ordereddict import OrderedDict
16 from qapi import *
17 import re
18 import sys
19 import os
20 import getopt
21 import errno
23 def type_visitor(name):
24 if type(name) == list:
25 return 'visit_type_%sList' % name[0]
26 else:
27 return 'visit_type_%s' % name
29 def generate_command_decl(name, args, ret_type):
30 arglist=""
31 for argname, argtype, optional, structured in parse_args(args):
32 argtype = c_type(argtype, is_param=True)
33 if optional:
34 arglist += "bool has_%s, " % c_var(argname)
35 arglist += "%s %s, " % (argtype, c_var(argname))
36 return mcgen('''
37 %(ret_type)s qmp_%(name)s(%(args)sError **errp);
38 ''',
39 ret_type=c_type(ret_type), name=c_fun(name), args=arglist).strip()
41 def gen_err_check(errvar):
42 if errvar:
43 return mcgen('''
44 if (local_err) {
45 goto out;
47 ''')
48 return ''
50 def gen_sync_call(name, args, ret_type, indent=0):
51 ret = ""
52 arglist=""
53 retval=""
54 if ret_type:
55 retval = "retval = "
56 for argname, argtype, optional, structured in parse_args(args):
57 if optional:
58 arglist += "has_%s, " % c_var(argname)
59 arglist += "%s, " % (c_var(argname))
60 push_indent(indent)
61 ret = mcgen('''
62 %(retval)sqmp_%(name)s(%(args)s&local_err);
64 ''',
65 name=c_fun(name), args=arglist, retval=retval).rstrip()
66 if ret_type:
67 ret += "\n" + gen_err_check('local_err')
68 ret += "\n" + mcgen(''''
69 %(marshal_output_call)s
70 ''',
71 marshal_output_call=gen_marshal_output_call(name, ret_type)).rstrip()
72 pop_indent(indent)
73 return ret.rstrip()
76 def gen_marshal_output_call(name, ret_type):
77 if not ret_type:
78 return ""
79 return "qmp_marshal_output_%s(retval, ret, &local_err);" % c_fun(name)
81 def gen_visitor_input_containers_decl(args, obj):
82 ret = ""
84 push_indent()
85 if len(args) > 0:
86 ret += mcgen('''
87 QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
88 QapiDeallocVisitor *md;
89 Visitor *v;
90 ''',
91 obj=obj)
92 pop_indent()
94 return ret.rstrip()
96 def gen_visitor_input_vars_decl(args):
97 ret = ""
98 push_indent()
99 for argname, argtype, optional, structured in parse_args(args):
100 if optional:
101 ret += mcgen('''
102 bool has_%(argname)s = false;
103 ''',
104 argname=c_var(argname))
105 if is_c_ptr(argtype):
106 ret += mcgen('''
107 %(argtype)s %(argname)s = NULL;
108 ''',
109 argname=c_var(argname), argtype=c_type(argtype))
110 else:
111 ret += mcgen('''
112 %(argtype)s %(argname)s = {0};
113 ''',
114 argname=c_var(argname), argtype=c_type(argtype))
116 pop_indent()
117 return ret.rstrip()
119 def gen_visitor_input_block(args, dealloc=False):
120 ret = ""
121 errparg = '&local_err'
122 errarg = 'local_err'
124 if len(args) == 0:
125 return ret
127 push_indent()
129 if dealloc:
130 errparg = 'NULL'
131 errarg = None;
132 ret += mcgen('''
133 qmp_input_visitor_cleanup(mi);
134 md = qapi_dealloc_visitor_new();
135 v = qapi_dealloc_get_visitor(md);
136 ''')
137 else:
138 ret += mcgen('''
139 v = qmp_input_get_visitor(mi);
140 ''')
142 for argname, argtype, optional, structured in parse_args(args):
143 if optional:
144 ret += mcgen('''
145 visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
146 ''',
147 c_name=c_var(argname), name=argname, errp=errparg)
148 ret += gen_err_check(errarg)
149 ret += mcgen('''
150 if (has_%(c_name)s) {
151 ''',
152 c_name=c_var(argname))
153 push_indent()
154 ret += mcgen('''
155 %(visitor)s(v, &%(c_name)s, "%(name)s", %(errp)s);
156 ''',
157 c_name=c_var(argname), name=argname, argtype=argtype,
158 visitor=type_visitor(argtype), errp=errparg)
159 ret += gen_err_check(errarg)
160 if optional:
161 pop_indent()
162 ret += mcgen('''
164 ''')
166 if dealloc:
167 ret += mcgen('''
168 qapi_dealloc_visitor_cleanup(md);
169 ''')
170 pop_indent()
171 return ret.rstrip()
173 def gen_marshal_output(name, args, ret_type, middle_mode):
174 if not ret_type:
175 return ""
177 ret = mcgen('''
178 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
180 Error *local_err = NULL;
181 QmpOutputVisitor *mo = qmp_output_visitor_new();
182 QapiDeallocVisitor *md;
183 Visitor *v;
185 v = qmp_output_get_visitor(mo);
186 %(visitor)s(v, &ret_in, "unused", &local_err);
187 if (local_err) {
188 goto out;
190 *ret_out = qmp_output_get_qobject(mo);
192 out:
193 error_propagate(errp, local_err);
194 qmp_output_visitor_cleanup(mo);
195 md = qapi_dealloc_visitor_new();
196 v = qapi_dealloc_get_visitor(md);
197 %(visitor)s(v, &ret_in, "unused", NULL);
198 qapi_dealloc_visitor_cleanup(md);
200 ''',
201 c_ret_type=c_type(ret_type), c_name=c_fun(name),
202 visitor=type_visitor(ret_type))
204 return ret
206 def gen_marshal_input_decl(name, args, ret_type, middle_mode):
207 if middle_mode:
208 return 'int qmp_marshal_input_%s(Monitor *mon, const QDict *qdict, QObject **ret)' % c_fun(name)
209 else:
210 return 'static void qmp_marshal_input_%s(QDict *args, QObject **ret, Error **errp)' % c_fun(name)
214 def gen_marshal_input(name, args, ret_type, middle_mode):
215 hdr = gen_marshal_input_decl(name, args, ret_type, middle_mode)
217 ret = mcgen('''
218 %(header)s
220 Error *local_err = NULL;
221 ''',
222 header=hdr)
224 if middle_mode:
225 ret += mcgen('''
226 QDict *args = (QDict *)qdict;
227 ''')
229 if ret_type:
230 if is_c_ptr(ret_type):
231 retval = " %s retval = NULL;" % c_type(ret_type)
232 else:
233 retval = " %s retval;" % c_type(ret_type)
234 ret += mcgen('''
235 %(retval)s
236 ''',
237 retval=retval)
239 if len(args) > 0:
240 ret += mcgen('''
241 %(visitor_input_containers_decl)s
242 %(visitor_input_vars_decl)s
244 %(visitor_input_block)s
246 ''',
247 visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
248 visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
249 visitor_input_block=gen_visitor_input_block(args))
250 else:
251 ret += mcgen('''
253 (void)args;
254 ''')
256 ret += mcgen('''
257 %(sync_call)s
258 ''',
259 sync_call=gen_sync_call(name, args, ret_type, indent=4))
260 if re.search('^ *goto out\\;', ret, re.MULTILINE):
261 ret += mcgen('''
263 out:
264 ''')
265 if not middle_mode:
266 ret += mcgen('''
267 error_propagate(errp, local_err);
268 ''')
269 ret += mcgen('''
270 %(visitor_input_block_cleanup)s
271 ''',
272 visitor_input_block_cleanup=gen_visitor_input_block(args,
273 dealloc=True))
275 if middle_mode:
276 ret += mcgen('''
278 if (local_err) {
279 qerror_report_err(local_err);
280 error_free(local_err);
281 return -1;
283 return 0;
284 ''')
285 else:
286 ret += mcgen('''
287 return;
288 ''')
290 ret += mcgen('''
292 ''')
294 return ret
296 def option_value_matches(opt, val, cmd):
297 if opt in cmd and cmd[opt] == val:
298 return True
299 return False
301 def gen_registry(commands):
302 registry=""
303 push_indent()
304 for cmd in commands:
305 options = 'QCO_NO_OPTIONS'
306 if option_value_matches('success-response', 'no', cmd):
307 options = 'QCO_NO_SUCCESS_RESP'
309 registry += mcgen('''
310 qmp_register_command("%(name)s", qmp_marshal_input_%(c_name)s, %(opts)s);
311 ''',
312 name=cmd['command'], c_name=c_fun(cmd['command']),
313 opts=options)
314 pop_indent()
315 ret = mcgen('''
316 static void qmp_init_marshal(void)
318 %(registry)s
321 qapi_init(qmp_init_marshal);
322 ''',
323 registry=registry.rstrip())
324 return ret
326 def gen_command_decl_prologue(header, guard, prefix=""):
327 ret = mcgen('''
328 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
331 * schema-defined QAPI function prototypes
333 * Copyright IBM, Corp. 2011
335 * Authors:
336 * Anthony Liguori <aliguori@us.ibm.com>
338 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
339 * See the COPYING.LIB file in the top-level directory.
343 #ifndef %(guard)s
344 #define %(guard)s
346 #include "%(prefix)sqapi-types.h"
347 #include "qapi/qmp/qdict.h"
348 #include "qapi/error.h"
350 ''',
351 header=basename(header), guard=guardname(header), prefix=prefix)
352 return ret
354 def gen_command_def_prologue(prefix="", proxy=False):
355 ret = mcgen('''
356 /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
359 * schema-defined QMP->QAPI command dispatch
361 * Copyright IBM, Corp. 2011
363 * Authors:
364 * Anthony Liguori <aliguori@us.ibm.com>
366 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
367 * See the COPYING.LIB file in the top-level directory.
371 #include "qemu-common.h"
372 #include "qemu/module.h"
373 #include "qapi/qmp/qerror.h"
374 #include "qapi/qmp/types.h"
375 #include "qapi/qmp/dispatch.h"
376 #include "qapi/visitor.h"
377 #include "qapi/qmp-output-visitor.h"
378 #include "qapi/qmp-input-visitor.h"
379 #include "qapi/dealloc-visitor.h"
380 #include "%(prefix)sqapi-types.h"
381 #include "%(prefix)sqapi-visit.h"
383 ''',
384 prefix=prefix)
385 if not proxy:
386 ret += '#include "%sqmp-commands.h"' % prefix
387 return ret + "\n\n"
390 try:
391 opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:i:o:m",
392 ["source", "header", "prefix=",
393 "input-file=", "output-dir=",
394 "type=", "middle"])
395 except getopt.GetoptError, err:
396 print str(err)
397 sys.exit(1)
399 output_dir = ""
400 prefix = ""
401 dispatch_type = "sync"
402 c_file = 'qmp-marshal.c'
403 h_file = 'qmp-commands.h'
404 middle_mode = False
406 do_c = False
407 do_h = False
409 for o, a in opts:
410 if o in ("-p", "--prefix"):
411 prefix = a
412 elif o in ("-i", "--input-file"):
413 input_file = a
414 elif o in ("-o", "--output-dir"):
415 output_dir = a + "/"
416 elif o in ("-t", "--type"):
417 dispatch_type = a
418 elif o in ("-m", "--middle"):
419 middle_mode = True
420 elif o in ("-c", "--source"):
421 do_c = True
422 elif o in ("-h", "--header"):
423 do_h = True
425 if not do_c and not do_h:
426 do_c = True
427 do_h = True
429 c_file = output_dir + prefix + c_file
430 h_file = output_dir + prefix + h_file
432 def maybe_open(really, name, opt):
433 if really:
434 return open(name, opt)
435 else:
436 import StringIO
437 return StringIO.StringIO()
439 try:
440 os.makedirs(output_dir)
441 except os.error, e:
442 if e.errno != errno.EEXIST:
443 raise
445 exprs = parse_schema(input_file)
446 commands = filter(lambda expr: expr.has_key('command'), exprs)
447 commands = filter(lambda expr: not expr.has_key('gen'), commands)
449 if dispatch_type == "sync":
450 fdecl = maybe_open(do_h, h_file, 'w')
451 fdef = maybe_open(do_c, c_file, 'w')
452 ret = gen_command_decl_prologue(header=basename(h_file), guard=guardname(h_file), prefix=prefix)
453 fdecl.write(ret)
454 ret = gen_command_def_prologue(prefix=prefix)
455 fdef.write(ret)
457 for cmd in commands:
458 arglist = []
459 ret_type = None
460 if cmd.has_key('data'):
461 arglist = cmd['data']
462 if cmd.has_key('returns'):
463 ret_type = cmd['returns']
464 ret = generate_command_decl(cmd['command'], arglist, ret_type) + "\n"
465 fdecl.write(ret)
466 if ret_type:
467 ret = gen_marshal_output(cmd['command'], arglist, ret_type, middle_mode) + "\n"
468 fdef.write(ret)
470 if middle_mode:
471 fdecl.write('%s;\n' % gen_marshal_input_decl(cmd['command'], arglist, ret_type, middle_mode))
473 ret = gen_marshal_input(cmd['command'], arglist, ret_type, middle_mode) + "\n"
474 fdef.write(ret)
476 fdecl.write("\n#endif\n");
478 if not middle_mode:
479 ret = gen_registry(commands)
480 fdef.write(ret)
482 fdef.flush()
483 fdef.close()
484 fdecl.flush()
485 fdecl.close()