xref: /openbmc/qemu/scripts/qapi/commands.py (revision 438c78da)
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"""
15
16from qapi.common import *
17
18
19def gen_command_decl(name, arg_type, boxed, ret_type):
20    return mcgen('''
21%(c_type)s qmp_%(c_name)s(%(params)s);
22''',
23                 c_type=(ret_type and ret_type.c_type()) or 'void',
24                 c_name=c_name(name),
25                 params=build_params(arg_type, boxed, 'Error **errp'))
26
27
28def gen_call(name, arg_type, boxed, ret_type):
29    ret = ''
30
31    argstr = ''
32    if boxed:
33        assert arg_type and not arg_type.is_empty()
34        argstr = '&arg, '
35    elif arg_type:
36        assert not arg_type.variants
37        for memb in arg_type.members:
38            if memb.optional:
39                argstr += 'arg.has_%s, ' % c_name(memb.name)
40            argstr += 'arg.%s, ' % c_name(memb.name)
41
42    lhs = ''
43    if ret_type:
44        lhs = 'retval = '
45
46    ret = mcgen('''
47
48    %(lhs)sqmp_%(c_name)s(%(args)s&err);
49''',
50                c_name=c_name(name), args=argstr, lhs=lhs)
51    if ret_type:
52        ret += mcgen('''
53    if (err) {
54        goto out;
55    }
56
57    qmp_marshal_output_%(c_name)s(retval, ret, &err);
58''',
59                     c_name=ret_type.c_name())
60    return ret
61
62
63def gen_marshal_output(ret_type):
64    return mcgen('''
65
66static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
67{
68    Error *err = NULL;
69    Visitor *v;
70
71    v = qobject_output_visitor_new(ret_out);
72    visit_type_%(c_name)s(v, "unused", &ret_in, &err);
73    if (!err) {
74        visit_complete(v, ret_out);
75    }
76    error_propagate(errp, err);
77    visit_free(v);
78    v = qapi_dealloc_visitor_new();
79    visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
80    visit_free(v);
81}
82''',
83                 c_type=ret_type.c_type(), c_name=ret_type.c_name())
84
85
86def build_marshal_proto(name):
87    return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)'
88            % c_name(name))
89
90
91def gen_marshal_decl(name):
92    return mcgen('''
93%(proto)s;
94''',
95                 proto=build_marshal_proto(name))
96
97
98def gen_marshal(name, arg_type, boxed, ret_type):
99    have_args = arg_type and not arg_type.is_empty()
100
101    ret = mcgen('''
102
103%(proto)s
104{
105    Error *err = NULL;
106''',
107                proto=build_marshal_proto(name))
108
109    if ret_type:
110        ret += mcgen('''
111    %(c_type)s retval;
112''',
113                     c_type=ret_type.c_type())
114
115    if have_args:
116        visit_members = ('visit_type_%s_members(v, &arg, &err);'
117                         % arg_type.c_name())
118        ret += mcgen('''
119    Visitor *v;
120    %(c_name)s arg = {0};
121
122''',
123                     c_name=arg_type.c_name())
124    else:
125        visit_members = ''
126        ret += mcgen('''
127    Visitor *v = NULL;
128
129    if (args) {
130''')
131        push_indent()
132
133    ret += mcgen('''
134    v = qobject_input_visitor_new(QOBJECT(args));
135    visit_start_struct(v, NULL, NULL, 0, &err);
136    if (err) {
137        goto out;
138    }
139    %(visit_members)s
140    if (!err) {
141        visit_check_struct(v, &err);
142    }
143    visit_end_struct(v, NULL);
144    if (err) {
145        goto out;
146    }
147''',
148                 visit_members=visit_members)
149
150    if not have_args:
151        pop_indent()
152        ret += mcgen('''
153    }
154''')
155
156    ret += gen_call(name, arg_type, boxed, ret_type)
157
158    ret += mcgen('''
159
160out:
161    error_propagate(errp, err);
162    visit_free(v);
163''')
164
165    if have_args:
166        visit_members = ('visit_type_%s_members(v, &arg, NULL);'
167                         % arg_type.c_name())
168    else:
169        visit_members = ''
170        ret += mcgen('''
171    if (args) {
172''')
173        push_indent()
174
175    ret += mcgen('''
176    v = qapi_dealloc_visitor_new();
177    visit_start_struct(v, NULL, NULL, 0, NULL);
178    %(visit_members)s
179    visit_end_struct(v, NULL);
180    visit_free(v);
181''',
182                 visit_members=visit_members)
183
184    if not have_args:
185        pop_indent()
186        ret += mcgen('''
187    }
188''')
189
190    ret += mcgen('''
191}
192''')
193    return ret
194
195
196def gen_register_command(name, success_response, allow_oob, allow_preconfig):
197    options = []
198
199    if not success_response:
200        options += ['QCO_NO_SUCCESS_RESP']
201    if allow_oob:
202        options += ['QCO_ALLOW_OOB']
203    if allow_preconfig:
204        options += ['QCO_ALLOW_PRECONFIG']
205
206    if not options:
207        options = ['QCO_NO_OPTIONS']
208
209    options = " | ".join(options)
210
211    ret = mcgen('''
212    qmp_register_command(cmds, "%(name)s",
213                         qmp_marshal_%(c_name)s, %(opts)s);
214''',
215                name=name, c_name=c_name(name),
216                opts=options)
217    return ret
218
219
220def gen_registry(registry, prefix):
221    ret = mcgen('''
222
223void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds)
224{
225    QTAILQ_INIT(cmds);
226
227''',
228                c_prefix=c_name(prefix, protect=False))
229    ret += registry
230    ret += mcgen('''
231}
232''')
233    return ret
234
235
236class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
237
238    def __init__(self, prefix):
239        QAPISchemaModularCVisitor.__init__(
240            self, prefix, 'qapi-commands',
241            ' * Schema-defined QAPI/QMP commands', __doc__)
242        self._regy = QAPIGenCCode()
243        self._visited_ret_types = {}
244
245    def _begin_module(self, name):
246        self._visited_ret_types[self._genc] = set()
247        commands = self._module_basename('qapi-commands', name)
248        types = self._module_basename('qapi-types', name)
249        visit = self._module_basename('qapi-visit', name)
250        self._genc.add(mcgen('''
251#include "qemu/osdep.h"
252#include "qemu-common.h"
253#include "qemu/module.h"
254#include "qapi/visitor.h"
255#include "qapi/qmp/qdict.h"
256#include "qapi/qobject-output-visitor.h"
257#include "qapi/qobject-input-visitor.h"
258#include "qapi/dealloc-visitor.h"
259#include "qapi/error.h"
260#include "%(visit)s.h"
261#include "%(commands)s.h"
262
263''',
264                             commands=commands, visit=visit))
265        self._genh.add(mcgen('''
266#include "%(types)s.h"
267#include "qapi/qmp/dispatch.h"
268
269''',
270                             types=types))
271
272    def visit_end(self):
273        (genc, genh) = self._module[self._main_module]
274        genh.add(mcgen('''
275void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds);
276''',
277                       c_prefix=c_name(self._prefix, protect=False)))
278        genc.add(gen_registry(self._regy.get_content(), self._prefix))
279
280    def visit_command(self, name, info, ifcond, arg_type, ret_type, gen,
281                      success_response, boxed, allow_oob, allow_preconfig):
282        if not gen:
283            return
284        # FIXME: If T is a user-defined type, the user is responsible
285        # for making this work, i.e. to make T's condition the
286        # conjunction of the T-returning commands' conditions.  If T
287        # is a built-in type, this isn't possible: the
288        # qmp_marshal_output_T() will be generated unconditionally.
289        if ret_type and ret_type not in self._visited_ret_types[self._genc]:
290            self._visited_ret_types[self._genc].add(ret_type)
291            with ifcontext(ret_type.ifcond,
292                           self._genh, self._genc, self._regy):
293                self._genc.add(gen_marshal_output(ret_type))
294        with ifcontext(ifcond, self._genh, self._genc, self._regy):
295            self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type))
296            self._genh.add(gen_marshal_decl(name))
297            self._genc.add(gen_marshal(name, arg_type, boxed, ret_type))
298            self._regy.add(gen_register_command(name, success_response,
299                                                allow_oob, allow_preconfig))
300
301
302def gen_commands(schema, output_dir, prefix):
303    vis = QAPISchemaGenCommandVisitor(prefix)
304    schema.visit(vis)
305    vis.write(output_dir)
306