2011-07-19 23:50:42 +04:00
|
|
|
#
|
|
|
|
# QAPI command marshaller generator
|
|
|
|
#
|
|
|
|
# Copyright IBM, Corp. 2011
|
2016-03-18 01:48:26 +03:00
|
|
|
# Copyright (C) 2014-2016 Red Hat, Inc.
|
2011-07-19 23:50:42 +04:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
# Michael Roth <mdroth@linux.vnet.ibm.com>
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 11:53:54 +04:00
|
|
|
# Markus Armbruster <armbru@redhat.com>
|
2011-07-19 23:50:42 +04:00
|
|
|
#
|
2014-03-01 11:40:34 +04:00
|
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
|
|
# See the COPYING file in the top-level directory.
|
2011-07-19 23:50:42 +04:00
|
|
|
|
|
|
|
from qapi import *
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 11:53:54 +04:00
|
|
|
import re
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
|
|
|
def gen_command_decl(name, arg_type, ret_type):
|
2011-07-19 23:50:42 +04:00
|
|
|
return mcgen('''
|
2015-09-16 14:06:20 +03:00
|
|
|
%(c_type)s qmp_%(c_name)s(%(params)s);
|
2011-07-19 23:50:42 +04:00
|
|
|
''',
|
2015-09-16 14:06:16 +03:00
|
|
|
c_type=(ret_type and ret_type.c_type()) or 'void',
|
|
|
|
c_name=c_name(name),
|
2015-09-16 14:06:20 +03:00
|
|
|
params=gen_params(arg_type, 'Error **errp'))
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
def gen_call(name, arg_type, ret_type):
|
|
|
|
ret = ''
|
|
|
|
|
|
|
|
argstr = ''
|
|
|
|
if arg_type:
|
2016-03-18 01:48:26 +03:00
|
|
|
assert not arg_type.variants
|
2015-09-16 14:06:16 +03:00
|
|
|
for memb in arg_type.members:
|
2015-09-16 14:06:11 +03:00
|
|
|
if memb.optional:
|
qapi-commands: Utilize implicit struct visits
Rather than generate inline per-member visits, take advantage
of the 'visit_type_FOO_members()' function for command
marshalling. This is possible now that implicit structs can be
visited like any other. Generate call arguments from a stack-
allocated struct, rather than a list of local variables:
|@@ -57,26 +57,15 @@ void qmp_marshal_add_fd(QDict *args, QOb
| QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
| QapiDeallocVisitor *qdv;
| Visitor *v;
|- bool has_fdset_id = false;
|- int64_t fdset_id = 0;
|- bool has_opaque = false;
|- char *opaque = NULL;
|+ q_obj_add_fd_arg arg = {0};
|
| v = qmp_input_get_visitor(qiv);
|- if (visit_optional(v, "fdset-id", &has_fdset_id)) {
|- visit_type_int(v, "fdset-id", &fdset_id, &err);
|- if (err) {
|- goto out;
|- }
|- }
|- if (visit_optional(v, "opaque", &has_opaque)) {
|- visit_type_str(v, "opaque", &opaque, &err);
|- if (err) {
|- goto out;
|- }
|+ visit_type_q_obj_add_fd_arg_members(v, &arg, &err);
|+ if (err) {
|+ goto out;
| }
|
|- retval = qmp_add_fd(has_fdset_id, fdset_id, has_opaque, opaque, &err);
|+ retval = qmp_add_fd(arg.has_fdset_id, arg.fdset_id, arg.has_opaque, arg.opaque, &err);
| if (err) {
| goto out;
| }
|@@ -88,12 +77,7 @@ out:
| qmp_input_visitor_cleanup(qiv);
| qdv = qapi_dealloc_visitor_new();
| v = qapi_dealloc_get_visitor(qdv);
|- if (visit_optional(v, "fdset-id", &has_fdset_id)) {
|- visit_type_int(v, "fdset-id", &fdset_id, NULL);
|- }
|- if (visit_optional(v, "opaque", &has_opaque)) {
|- visit_type_str(v, "opaque", &opaque, NULL);
|- }
|+ visit_type_q_obj_add_fd_arg_members(v, &arg, NULL);
| qapi_dealloc_visitor_cleanup(qdv);
| }
This also has the nice side effect of eliminating a chance of
collision between argument QMP names and local variables.
This patch also paves the way for some followup simplifications
in the generator, in subsequent patches.
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1458254921-17042-9-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-03-18 01:48:33 +03:00
|
|
|
argstr += 'arg.has_%s, ' % c_name(memb.name)
|
|
|
|
argstr += 'arg.%s, ' % c_name(memb.name)
|
2015-09-16 14:06:16 +03:00
|
|
|
|
|
|
|
lhs = ''
|
|
|
|
if ret_type:
|
|
|
|
lhs = 'retval = '
|
|
|
|
|
2011-07-19 23:50:42 +04:00
|
|
|
ret = mcgen('''
|
2015-09-16 14:06:18 +03:00
|
|
|
|
2015-09-30 01:21:12 +03:00
|
|
|
%(lhs)sqmp_%(c_name)s(%(args)s&err);
|
2011-07-19 23:50:42 +04:00
|
|
|
''',
|
2015-09-16 14:06:16 +03:00
|
|
|
c_name=c_name(name), args=argstr, lhs=lhs)
|
2011-07-19 23:50:42 +04:00
|
|
|
if ret_type:
|
2015-09-30 01:21:13 +03:00
|
|
|
ret += gen_err_check()
|
2015-06-27 18:21:12 +03:00
|
|
|
ret += mcgen('''
|
|
|
|
|
2015-09-30 01:21:12 +03:00
|
|
|
qmp_marshal_output_%(c_name)s(retval, ret, &err);
|
2011-07-19 23:50:42 +04:00
|
|
|
''',
|
2015-09-16 14:06:21 +03:00
|
|
|
c_name=ret_type.c_name())
|
2015-06-27 18:49:34 +03:00
|
|
|
return ret
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2015-09-16 14:06:21 +03:00
|
|
|
def gen_marshal_output(ret_type):
|
2015-09-16 14:06:18 +03:00
|
|
|
return mcgen('''
|
2015-09-16 14:06:11 +03:00
|
|
|
|
2015-09-16 14:06:21 +03:00
|
|
|
static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp)
|
2011-07-19 23:50:42 +04:00
|
|
|
{
|
2015-09-30 01:21:08 +03:00
|
|
|
Error *err = NULL;
|
2015-09-30 01:21:09 +03:00
|
|
|
QmpOutputVisitor *qov = qmp_output_visitor_new();
|
|
|
|
QapiDeallocVisitor *qdv;
|
2011-07-19 23:50:42 +04:00
|
|
|
Visitor *v;
|
|
|
|
|
2015-09-30 01:21:09 +03:00
|
|
|
v = qmp_output_get_visitor(qov);
|
qapi: Swap visit_* arguments for consistent 'name' placement
JSON uses "name":value, but many of our visitor interfaces were
called with visit_type_FOO(v, &value, name, errp). This can be
a bit confusing to have to mentally swap the parameter order to
match JSON order. It's particularly bad for visit_start_struct(),
where the 'name' parameter is smack in the middle of the
otherwise-related group of 'obj, kind, size' parameters! It's
time to do a global swap of the parameter ordering, so that the
'name' parameter is always immediately after the Visitor argument.
Additional reason in favor of the swap: the existing include/qjson.h
prefers listing 'name' first in json_prop_*(), and I have plans to
unify that file with the qapi visitors; listing 'name' first in
qapi will minimize churn to the (admittedly few) qjson.h clients.
Later patches will then fix docs, object.h, visitor-impl.h, and
those clients to match.
Done by first patching scripts/qapi*.py by hand to make generated
files do what I want, then by running the following Coccinelle
script to affect the rest of the code base:
$ spatch --sp-file script `git grep -l '\bvisit_' -- '**/*.[ch]'`
I then had to apply some touchups (Coccinelle insisted on TAB
indentation in visitor.h, and botched the signature of
visit_type_enum() by rewriting 'const char *const strings[]' to
the syntactically invalid 'const char*const[] strings'). The
movement of parameters is sufficient to provoke compiler errors
if any callers were missed.
// Part 1: Swap declaration order
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_start_struct
-(TV v, TObj OBJ, T1 ARG1, const char *name, T2 ARG2, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type bool, TV, T1;
identifier ARG1;
@@
bool visit_optional
-(TV v, T1 ARG1, const char *name)
+(TV v, const char *name, T1 ARG1)
{ ... }
@@
type TV, TErr, TObj, T1;
identifier OBJ, ARG1;
@@
void visit_get_next_type
-(TV v, TObj OBJ, T1 ARG1, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, TErr errp)
{ ... }
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_type_enum
-(TV v, TObj OBJ, T1 ARG1, T2 ARG2, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type TV, TErr, TObj;
identifier OBJ;
identifier VISIT_TYPE =~ "^visit_type_";
@@
void VISIT_TYPE
-(TV v, TObj OBJ, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, TErr errp)
{ ... }
// Part 2: swap caller order
@@
expression V, NAME, OBJ, ARG1, ARG2, ERR;
identifier VISIT_TYPE =~ "^visit_type_";
@@
(
-visit_start_struct(V, OBJ, ARG1, NAME, ARG2, ERR)
+visit_start_struct(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-visit_optional(V, ARG1, NAME)
+visit_optional(V, NAME, ARG1)
|
-visit_get_next_type(V, OBJ, ARG1, NAME, ERR)
+visit_get_next_type(V, NAME, OBJ, ARG1, ERR)
|
-visit_type_enum(V, OBJ, ARG1, ARG2, NAME, ERR)
+visit_type_enum(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-VISIT_TYPE(V, OBJ, NAME, ERR)
+VISIT_TYPE(V, NAME, OBJ, ERR)
)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-19-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 16:48:54 +03:00
|
|
|
visit_type_%(c_name)s(v, "unused", &ret_in, &err);
|
2015-09-30 01:21:08 +03:00
|
|
|
if (err) {
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 11:53:54 +04:00
|
|
|
goto out;
|
2011-07-19 23:50:42 +04:00
|
|
|
}
|
2015-09-30 01:21:09 +03:00
|
|
|
*ret_out = qmp_output_get_qobject(qov);
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 11:53:54 +04:00
|
|
|
|
|
|
|
out:
|
2015-09-30 01:21:08 +03:00
|
|
|
error_propagate(errp, err);
|
2015-09-30 01:21:09 +03:00
|
|
|
qmp_output_visitor_cleanup(qov);
|
|
|
|
qdv = qapi_dealloc_visitor_new();
|
|
|
|
v = qapi_dealloc_get_visitor(qdv);
|
qapi: Swap visit_* arguments for consistent 'name' placement
JSON uses "name":value, but many of our visitor interfaces were
called with visit_type_FOO(v, &value, name, errp). This can be
a bit confusing to have to mentally swap the parameter order to
match JSON order. It's particularly bad for visit_start_struct(),
where the 'name' parameter is smack in the middle of the
otherwise-related group of 'obj, kind, size' parameters! It's
time to do a global swap of the parameter ordering, so that the
'name' parameter is always immediately after the Visitor argument.
Additional reason in favor of the swap: the existing include/qjson.h
prefers listing 'name' first in json_prop_*(), and I have plans to
unify that file with the qapi visitors; listing 'name' first in
qapi will minimize churn to the (admittedly few) qjson.h clients.
Later patches will then fix docs, object.h, visitor-impl.h, and
those clients to match.
Done by first patching scripts/qapi*.py by hand to make generated
files do what I want, then by running the following Coccinelle
script to affect the rest of the code base:
$ spatch --sp-file script `git grep -l '\bvisit_' -- '**/*.[ch]'`
I then had to apply some touchups (Coccinelle insisted on TAB
indentation in visitor.h, and botched the signature of
visit_type_enum() by rewriting 'const char *const strings[]' to
the syntactically invalid 'const char*const[] strings'). The
movement of parameters is sufficient to provoke compiler errors
if any callers were missed.
// Part 1: Swap declaration order
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_start_struct
-(TV v, TObj OBJ, T1 ARG1, const char *name, T2 ARG2, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type bool, TV, T1;
identifier ARG1;
@@
bool visit_optional
-(TV v, T1 ARG1, const char *name)
+(TV v, const char *name, T1 ARG1)
{ ... }
@@
type TV, TErr, TObj, T1;
identifier OBJ, ARG1;
@@
void visit_get_next_type
-(TV v, TObj OBJ, T1 ARG1, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, TErr errp)
{ ... }
@@
type TV, TErr, TObj, T1, T2;
identifier OBJ, ARG1, ARG2;
@@
void visit_type_enum
-(TV v, TObj OBJ, T1 ARG1, T2 ARG2, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, T1 ARG1, T2 ARG2, TErr errp)
{ ... }
@@
type TV, TErr, TObj;
identifier OBJ;
identifier VISIT_TYPE =~ "^visit_type_";
@@
void VISIT_TYPE
-(TV v, TObj OBJ, const char *name, TErr errp)
+(TV v, const char *name, TObj OBJ, TErr errp)
{ ... }
// Part 2: swap caller order
@@
expression V, NAME, OBJ, ARG1, ARG2, ERR;
identifier VISIT_TYPE =~ "^visit_type_";
@@
(
-visit_start_struct(V, OBJ, ARG1, NAME, ARG2, ERR)
+visit_start_struct(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-visit_optional(V, ARG1, NAME)
+visit_optional(V, NAME, ARG1)
|
-visit_get_next_type(V, OBJ, ARG1, NAME, ERR)
+visit_get_next_type(V, NAME, OBJ, ARG1, ERR)
|
-visit_type_enum(V, OBJ, ARG1, ARG2, NAME, ERR)
+visit_type_enum(V, NAME, OBJ, ARG1, ARG2, ERR)
|
-VISIT_TYPE(V, OBJ, NAME, ERR)
+VISIT_TYPE(V, NAME, OBJ, ERR)
)
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1454075341-13658-19-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2016-01-29 16:48:54 +03:00
|
|
|
visit_type_%(c_name)s(v, "unused", &ret_in, NULL);
|
2015-09-30 01:21:09 +03:00
|
|
|
qapi_dealloc_visitor_cleanup(qdv);
|
2011-07-19 23:50:42 +04:00
|
|
|
}
|
|
|
|
''',
|
2015-09-16 14:06:21 +03:00
|
|
|
c_type=ret_type.c_type(), c_name=ret_type.c_name())
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2015-09-16 14:06:18 +03:00
|
|
|
def gen_marshal_proto(name):
|
2015-09-16 14:06:19 +03:00
|
|
|
ret = 'void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' % c_name(name)
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 19:25:50 +03:00
|
|
|
if not middle_mode:
|
2015-09-16 14:06:16 +03:00
|
|
|
ret = 'static ' + ret
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 19:25:50 +03:00
|
|
|
return ret
|
2011-09-02 21:34:46 +04:00
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2015-09-16 14:06:18 +03:00
|
|
|
def gen_marshal_decl(name):
|
|
|
|
return mcgen('''
|
|
|
|
%(proto)s;
|
|
|
|
''',
|
|
|
|
proto=gen_marshal_proto(name))
|
|
|
|
|
2011-09-02 21:34:46 +04:00
|
|
|
|
2015-09-16 14:06:18 +03:00
|
|
|
def gen_marshal(name, arg_type, ret_type):
|
2011-07-19 23:50:42 +04:00
|
|
|
ret = mcgen('''
|
2015-09-16 14:06:11 +03:00
|
|
|
|
2015-09-16 14:06:18 +03:00
|
|
|
%(proto)s
|
2011-07-19 23:50:42 +04:00
|
|
|
{
|
2016-03-18 01:48:34 +03:00
|
|
|
Error *err = NULL;
|
2011-07-19 23:50:42 +04:00
|
|
|
''',
|
2015-09-16 14:06:18 +03:00
|
|
|
proto=gen_marshal_proto(name))
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2016-03-18 01:48:34 +03:00
|
|
|
if ret_type:
|
|
|
|
ret += mcgen('''
|
|
|
|
%(c_type)s retval;
|
|
|
|
''',
|
|
|
|
c_type=ret_type.c_type())
|
|
|
|
|
|
|
|
if arg_type and arg_type.members:
|
|
|
|
ret += mcgen('''
|
|
|
|
QmpInputVisitor *qiv = qmp_input_visitor_new_strict(QOBJECT(args));
|
|
|
|
QapiDeallocVisitor *qdv;
|
|
|
|
Visitor *v;
|
|
|
|
%(c_name)s arg = {0};
|
|
|
|
|
|
|
|
v = qmp_input_get_visitor(qiv);
|
|
|
|
visit_type_%(c_name)s_members(v, &arg, &err);
|
|
|
|
if (err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
''',
|
|
|
|
c_name=arg_type.c_name())
|
|
|
|
|
|
|
|
else:
|
|
|
|
ret += mcgen('''
|
|
|
|
|
|
|
|
(void)args;
|
|
|
|
''')
|
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
ret += gen_call(name, arg_type, ret_type)
|
2015-06-27 18:49:34 +03:00
|
|
|
|
2016-03-18 01:48:34 +03:00
|
|
|
# 'goto out' produced above for arg_type, and by gen_call() for ret_type
|
2015-10-27 01:34:42 +03:00
|
|
|
if (arg_type and arg_type.members) or ret_type:
|
qapi: Replace uncommon use of the error API by the common one
We commonly use the error API like this:
err = NULL;
foo(..., &err);
if (err) {
goto out;
}
bar(..., &err);
Every error source is checked separately. The second function is only
called when the first one succeeds. Both functions are free to pass
their argument to error_set(). Because error_set() asserts no error
has been set, this effectively means they must not be called with an
error set.
The qapi-generated code uses the error API differently:
// *errp was initialized to NULL somewhere up the call chain
frob(..., errp);
gnat(..., errp);
Errors accumulate in *errp: first error wins, subsequent errors get
dropped. To make this work, the second function does nothing when
called with an error set. Requires non-null errp, or else the second
function can't see the first one fail.
This usage has also bled into visitor tests, and two device model
object property getters rtc_get_date() and balloon_stats_get_all().
With the "accumulate" technique, you need fewer error checks in
callers, and buy that with an error check in every callee. Can be
nice.
However, mixing the two techniques is confusing. You can't use the
"accumulate" technique with functions designed for the "check
separately" technique. You can use the "check separately" technique
with functions designed for the "accumulate" technique, but then
error_set() can't catch you setting an error more than once.
Standardize on the "check separately" technique for now, because it's
overwhelmingly prevalent.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
2014-05-07 11:53:54 +04:00
|
|
|
ret += mcgen('''
|
2011-07-19 23:50:42 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
''')
|
|
|
|
ret += mcgen('''
|
2015-09-30 01:21:08 +03:00
|
|
|
error_propagate(errp, err);
|
2015-06-27 18:49:34 +03:00
|
|
|
''')
|
2016-03-18 01:48:34 +03:00
|
|
|
if arg_type and arg_type.members:
|
|
|
|
ret += mcgen('''
|
|
|
|
qmp_input_visitor_cleanup(qiv);
|
|
|
|
qdv = qapi_dealloc_visitor_new();
|
|
|
|
v = qapi_dealloc_get_visitor(qdv);
|
|
|
|
visit_type_%(c_name)s_members(v, &arg, NULL);
|
|
|
|
qapi_dealloc_visitor_cleanup(qdv);
|
|
|
|
''',
|
|
|
|
c_name=arg_type.c_name())
|
|
|
|
|
2015-06-27 18:49:34 +03:00
|
|
|
ret += mcgen('''
|
qmp: Wean off qerror_report()
The traditional QMP command handler interface
int qmp_FOO(Monitor *mon, const QDict *params, QObject **ret_data);
doesn't provide for returning an Error object. Instead, the handler
is expected to stash it in the monitor with qerror_report().
When we rebased QMP on top of QAPI, we didn't change this interface.
Instead, commit 776574d introduced "middle mode" as a temporary aid
for converting existing QMP commands to QAPI one by one. More than
three years later, we're still using it.
Middle mode has two effects:
* Instead of the native input marshallers
static void qmp_marshal_input_FOO(QDict *, QObject **, Error **)
it generates input marshallers conforming to the traditional QMP
command handler interface.
* It suppresses generation of code to register them with
qmp_register_command()
This permits giving them internal linkage.
As long as we need qmp-commands.hx, we can't use the registry behind
qmp_register_command(), so the latter has to stay for now.
The former has to go to get rid of qerror_report(). Changing all QMP
commands to fit the QAPI mold in one go was impractical back when we
started, but by now there are just a few stragglers left:
do_qmp_capabilities(), qmp_qom_set(), qmp_qom_get(), qmp_object_add(),
qmp_netdev_add(), do_device_add().
Switch middle mode to generate native input marshallers, and adapt the
stragglers. Simplifies both the monitor code and the stragglers.
Rename do_qmp_capabilities() to qmp_capabilities(), and
do_device_add() to qmp_device_add, because that's how QMP command
handlers are named today.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Luiz Capitulino <lcapitulino@redhat.com>
2015-03-13 19:25:50 +03:00
|
|
|
}
|
2015-06-27 18:49:34 +03:00
|
|
|
''')
|
2011-07-19 23:50:42 +04:00
|
|
|
return ret
|
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2015-09-16 14:06:11 +03:00
|
|
|
def gen_register_command(name, success_response):
|
|
|
|
options = 'QCO_NO_OPTIONS'
|
|
|
|
if not success_response:
|
|
|
|
options = 'QCO_NO_SUCCESS_RESP'
|
2012-05-08 21:24:44 +04:00
|
|
|
|
2015-09-16 14:06:11 +03:00
|
|
|
ret = mcgen('''
|
2015-09-30 01:21:12 +03:00
|
|
|
qmp_register_command("%(name)s", qmp_marshal_%(c_name)s, %(opts)s);
|
2011-07-19 23:50:42 +04:00
|
|
|
''',
|
2015-09-16 14:06:16 +03:00
|
|
|
name=name, c_name=c_name(name),
|
|
|
|
opts=options)
|
2015-09-16 14:06:11 +03:00
|
|
|
return ret
|
|
|
|
|
2015-09-16 14:06:16 +03:00
|
|
|
|
2015-09-16 14:06:11 +03:00
|
|
|
def gen_registry(registry):
|
2011-07-19 23:50:42 +04:00
|
|
|
ret = mcgen('''
|
2015-09-16 14:06:11 +03:00
|
|
|
|
2011-07-19 23:50:42 +04:00
|
|
|
static void qmp_init_marshal(void)
|
|
|
|
{
|
2015-06-27 18:49:34 +03:00
|
|
|
''')
|
|
|
|
ret += registry
|
|
|
|
ret += mcgen('''
|
2011-07-19 23:50:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qapi_init(qmp_init_marshal);
|
2015-06-27 18:49:34 +03:00
|
|
|
''')
|
2011-07-19 23:50:42 +04:00
|
|
|
return ret
|
|
|
|
|
2015-09-16 14:06:11 +03:00
|
|
|
|
|
|
|
class QAPISchemaGenCommandVisitor(QAPISchemaVisitor):
|
|
|
|
def __init__(self):
|
|
|
|
self.decl = None
|
|
|
|
self.defn = None
|
|
|
|
self._regy = None
|
2015-09-16 14:06:21 +03:00
|
|
|
self._visited_ret_types = None
|
2015-09-16 14:06:11 +03:00
|
|
|
|
|
|
|
def visit_begin(self, schema):
|
|
|
|
self.decl = ''
|
|
|
|
self.defn = ''
|
|
|
|
self._regy = ''
|
2015-09-16 14:06:21 +03:00
|
|
|
self._visited_ret_types = set()
|
2015-09-16 14:06:11 +03:00
|
|
|
|
|
|
|
def visit_end(self):
|
|
|
|
if not middle_mode:
|
|
|
|
self.defn += gen_registry(self._regy)
|
|
|
|
self._regy = None
|
2015-09-16 14:06:21 +03:00
|
|
|
self._visited_ret_types = None
|
2015-09-16 14:06:11 +03:00
|
|
|
|
|
|
|
def visit_command(self, name, info, arg_type, ret_type,
|
|
|
|
gen, success_response):
|
|
|
|
if not gen:
|
|
|
|
return
|
2015-09-16 14:06:16 +03:00
|
|
|
self.decl += gen_command_decl(name, arg_type, ret_type)
|
2015-09-16 14:06:21 +03:00
|
|
|
if ret_type and ret_type not in self._visited_ret_types:
|
|
|
|
self._visited_ret_types.add(ret_type)
|
|
|
|
self.defn += gen_marshal_output(ret_type)
|
2015-09-16 14:06:11 +03:00
|
|
|
if middle_mode:
|
2015-09-16 14:06:18 +03:00
|
|
|
self.decl += gen_marshal_decl(name)
|
|
|
|
self.defn += gen_marshal(name, arg_type, ret_type)
|
2015-09-16 14:06:11 +03:00
|
|
|
if not middle_mode:
|
|
|
|
self._regy += gen_register_command(name, success_response)
|
|
|
|
|
|
|
|
|
2011-09-02 21:34:46 +04:00
|
|
|
middle_mode = False
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-04-02 14:12:21 +03:00
|
|
|
(input_file, output_dir, do_c, do_h, prefix, opts) = \
|
|
|
|
parse_command_line("m", ["middle"])
|
2011-12-27 18:02:16 +04:00
|
|
|
|
2011-07-19 23:50:42 +04:00
|
|
|
for o, a in opts:
|
2015-04-02 14:12:21 +03:00
|
|
|
if o in ("-m", "--middle"):
|
2011-09-02 21:34:46 +04:00
|
|
|
middle_mode = True
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-04-02 15:46:39 +03:00
|
|
|
c_comment = '''
|
|
|
|
/*
|
|
|
|
* schema-defined QMP->QAPI command dispatch
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
h_comment = '''
|
|
|
|
/*
|
|
|
|
* schema-defined QAPI function prototypes
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
'''
|
|
|
|
|
|
|
|
(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
|
|
|
|
'qmp-marshal.c', 'qmp-commands.h',
|
|
|
|
c_comment, h_comment)
|
|
|
|
|
2015-04-02 15:52:55 +03:00
|
|
|
fdef.write(mcgen('''
|
2016-02-08 18:36:46 +03:00
|
|
|
#include "qemu/osdep.h"
|
2015-04-02 15:52:55 +03:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu/module.h"
|
|
|
|
#include "qapi/qmp/types.h"
|
|
|
|
#include "qapi/qmp/dispatch.h"
|
|
|
|
#include "qapi/visitor.h"
|
|
|
|
#include "qapi/qmp-output-visitor.h"
|
|
|
|
#include "qapi/qmp-input-visitor.h"
|
|
|
|
#include "qapi/dealloc-visitor.h"
|
|
|
|
#include "%(prefix)sqapi-types.h"
|
|
|
|
#include "%(prefix)sqapi-visit.h"
|
|
|
|
#include "%(prefix)sqmp-commands.h"
|
|
|
|
|
|
|
|
''',
|
2015-09-16 14:06:16 +03:00
|
|
|
prefix=prefix))
|
2015-04-02 15:52:55 +03:00
|
|
|
|
|
|
|
fdecl.write(mcgen('''
|
|
|
|
#include "%(prefix)sqapi-types.h"
|
|
|
|
#include "qapi/qmp/qdict.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
|
|
|
|
''',
|
2015-09-16 14:06:11 +03:00
|
|
|
prefix=prefix))
|
2015-04-02 12:41:22 +03:00
|
|
|
|
2015-09-16 14:06:11 +03:00
|
|
|
schema = QAPISchema(input_file)
|
|
|
|
gen = QAPISchemaGenCommandVisitor()
|
|
|
|
schema.visit(gen)
|
|
|
|
fdef.write(gen.defn)
|
|
|
|
fdecl.write(gen.decl)
|
2011-07-19 23:50:42 +04:00
|
|
|
|
2015-04-02 15:46:39 +03:00
|
|
|
close_output(fdef, fdecl)
|