This policy makes deprecated commands fail like this: ---> {"execute": "query-cpus"} <--- {"error": {"class": "CommandNotFound", "desc": "Deprecated command query-cpus disabled by policy"}} When the command is removed, the error will change to <--- {"error": {"class": "CommandNotFound", "desc": "The command query-cpus has not been found"}} It makes commands with deprecated arguments fail like this: ---> {"execute": "block-commit", "arguments": {"device": "virtio0", "top": "/tmp/snap1.qcow2"}} <--- {"error": {"class": "GenericError", "desc": "Deprecated parameter 'top' disabled by policy"}} When the argument is removed, the error will change to <--- {"error": {"class": "GenericError", "desc": "Parameter 'top' is unexpected"}} The policy thus permits "testing the future". Signed-off-by: Markus Armbruster <armbru@xxxxxxxxxx> --- include/qapi/qmp/dispatch.h | 1 + include/qapi/qobject-input-visitor.h | 9 +++++ include/qapi/visitor-impl.h | 2 +- include/qapi/visitor.h | 2 +- qapi/qapi-visit-core.c | 4 +-- qapi/qmp-dispatch.c | 13 ++++++++ qapi/qobject-input-visitor.c | 28 ++++++++++++++++ qapi/qobject-output-visitor.c | 3 +- tests/test-qmp-cmds.c | 49 ++++++++++++++++++++++++++++ scripts/qapi/commands.py | 12 ++++--- scripts/qapi/visit.py | 10 +++--- 11 files changed, 120 insertions(+), 13 deletions(-) diff --git a/include/qapi/qmp/dispatch.h b/include/qapi/qmp/dispatch.h index 9aa426a398..ef256f2bb7 100644 --- a/include/qapi/qmp/dispatch.h +++ b/include/qapi/qmp/dispatch.h @@ -24,6 +24,7 @@ typedef enum QmpCommandOptions QCO_NO_SUCCESS_RESP = (1U << 0), QCO_ALLOW_OOB = (1U << 1), QCO_ALLOW_PRECONFIG = (1U << 2), + QCO_DEPRECATED = (1U << 3), } QmpCommandOptions; typedef struct QmpCommand diff --git a/include/qapi/qobject-input-visitor.h b/include/qapi/qobject-input-visitor.h index 95985e25e5..cbc54de4ac 100644 --- a/include/qapi/qobject-input-visitor.h +++ b/include/qapi/qobject-input-visitor.h @@ -58,6 +58,15 @@ typedef struct QObjectInputVisitor QObjectInputVisitor; */ Visitor *qobject_input_visitor_new(QObject *obj); +/* + * Create a QObject input visitor for @obj for use with QMP + * + * This is like qobject_input_visitor_new(), except it obeys the + * policy for handling deprecated management interfaces set with + * -compat. + */ +Visitor *qobject_input_visitor_new_qmp(QObject *obj); + /* * Create a QObject input visitor for @obj for use with keyval_parse() * diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h index a6b26b7a5b..ccc159a0d2 100644 --- a/include/qapi/visitor-impl.h +++ b/include/qapi/visitor-impl.h @@ -111,7 +111,7 @@ struct Visitor void (*optional)(Visitor *v, const char *name, bool *present); /* Optional */ - bool (*deprecated)(Visitor *v, const char *name); + bool (*deprecated)(Visitor *v, const char *name, Error **errp); /* Must be set */ VisitorType type; diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index c89d51b2a4..2a3c4d0407 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -456,7 +456,7 @@ bool visit_optional(Visitor *v, const char *name, bool *present); * visit_start_struct() and visit_end_struct(), since only objects * have deprecated members. */ -bool visit_deprecated(Visitor *v, const char *name); +bool visit_deprecated(Visitor *v, const char *name, Error **errp); /* * Visit an enum value. diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index 501b3ccdef..71e4978a6f 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -137,11 +137,11 @@ bool visit_optional(Visitor *v, const char *name, bool *present) return *present; } -bool visit_deprecated(Visitor *v, const char *name) +bool visit_deprecated(Visitor *v, const char *name, Error **errp) { trace_visit_deprecated(v, name); if (v->deprecated) { - return v->deprecated(v, name); + return v->deprecated(v, name, errp); } return true; } diff --git a/qapi/qmp-dispatch.c b/qapi/qmp-dispatch.c index 80beab517f..516ee9b0b7 100644 --- a/qapi/qmp-dispatch.c +++ b/qapi/qmp-dispatch.c @@ -132,6 +132,19 @@ QDict *qmp_dispatch(QmpCommandList *cmds, QObject *request, "The command %s has not been found", command); goto out; } + if (cmd->options & QCO_DEPRECATED) { + switch (compat_policy.deprecated_input) { + case COMPAT_POLICY_INPUT_ACCEPT: + break; + case COMPAT_POLICY_INPUT_REJECT: + error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND, + "Deprecated command %s disabled by policy", + command); + goto out; + default: + abort(); + } + } if (!cmd->enabled) { error_set(&err, ERROR_CLASS_COMMAND_NOT_FOUND, "The command %s has been disabled for this instance", diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index 32236cbcb1..6ea93f5a7a 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -14,6 +14,7 @@ #include "qemu/osdep.h" #include <math.h> +#include "qapi/compat-policy.h" #include "qapi/error.h" #include "qapi/qobject-input-visitor.h" #include "qapi/visitor-impl.h" @@ -43,6 +44,7 @@ typedef struct StackObject { struct QObjectInputVisitor { Visitor visitor; + CompatPolicyInput deprecated_policy; /* Root of visit at visitor creation. */ QObject *root; @@ -640,6 +642,23 @@ static void qobject_input_optional(Visitor *v, const char *name, bool *present) *present = true; } +static bool qobject_input_deprecated(Visitor *v, const char *name, + Error **errp) +{ + QObjectInputVisitor *qiv = to_qiv(v); + + switch (qiv->deprecated_policy) { + case COMPAT_POLICY_INPUT_ACCEPT: + return true; + case COMPAT_POLICY_INPUT_REJECT: + error_setg(errp, "Deprecated parameter '%s' disabled by policy", + name); + return false; + default: + abort(); + } +} + static void qobject_input_free(Visitor *v) { QObjectInputVisitor *qiv = to_qiv(v); @@ -674,6 +693,7 @@ static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj) v->visitor.end_list = qobject_input_end_list; v->visitor.start_alternate = qobject_input_start_alternate; v->visitor.optional = qobject_input_optional; + v->visitor.deprecated = qobject_input_deprecated; v->visitor.free = qobject_input_free; v->root = qobject_ref(obj); @@ -696,6 +716,14 @@ Visitor *qobject_input_visitor_new(QObject *obj) return &v->visitor; } +Visitor *qobject_input_visitor_new_qmp(QObject *obj) +{ + QObjectInputVisitor *v = to_qiv(qobject_input_visitor_new(obj)); + + v->deprecated_policy = compat_policy.deprecated_input; + return &v->visitor; +} + Visitor *qobject_input_visitor_new_keyval(QObject *obj) { QObjectInputVisitor *v = qobject_input_visitor_base_new(obj); diff --git a/qapi/qobject-output-visitor.c b/qapi/qobject-output-visitor.c index 84cee17596..73983ca5cc 100644 --- a/qapi/qobject-output-visitor.c +++ b/qapi/qobject-output-visitor.c @@ -201,7 +201,8 @@ static void qobject_output_type_null(Visitor *v, const char *name, qobject_output_add(qov, name, qnull()); } -static bool qobject_output_deprecated(Visitor *v, const char *name) +static bool qobject_output_deprecated(Visitor *v, const char *name, + Error **errp) { QObjectOutputVisitor *qov = to_qov(v); diff --git a/tests/test-qmp-cmds.c b/tests/test-qmp-cmds.c index 82d599630c..4ca658acf9 100644 --- a/tests/test-qmp-cmds.c +++ b/tests/test-qmp-cmds.c @@ -277,6 +277,51 @@ static void test_dispatch_cmd_io(void) qobject_unref(ret3); } +static void test_dispatch_cmd_deprecated(void) +{ + const char *cmd = "{ 'execute': 'test-command-features1' }"; + QDict *ret; + + memset(&compat_policy, 0, sizeof(compat_policy)); + + /* accept */ + ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); + assert(ret && qdict_size(ret) == 0); + qobject_unref(ret); + + compat_policy.has_deprecated_input = true; + compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT; + ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); + assert(ret && qdict_size(ret) == 0); + qobject_unref(ret); + + compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT; + do_qmp_dispatch_error(false, ERROR_CLASS_COMMAND_NOT_FOUND, cmd); +} + +static void test_dispatch_cmd_arg_deprecated(void) +{ + const char *cmd = "{ 'execute': 'test-features0'," + " 'arguments': { 'fs1': { 'foo': 42 } } }"; + QDict *ret; + + memset(&compat_policy, 0, sizeof(compat_policy)); + + /* accept */ + ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); + assert(ret && qdict_size(ret) == 1); + qobject_unref(ret); + + compat_policy.has_deprecated_input = true; + compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT; + ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); + assert(ret && qdict_size(ret) == 1); + qobject_unref(ret); + + compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT; + do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, cmd); +} + static void test_dispatch_cmd_ret_deprecated(void) { const char *cmd = "{ 'execute': 'test-features0' }"; @@ -375,6 +420,10 @@ int main(int argc, char **argv) g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io); g_test_add_func("/qmp/dispatch_cmd_success_response", test_dispatch_cmd_success_response); + g_test_add_func("/qmp/dispatch_cmd_deprecated", + test_dispatch_cmd_deprecated); + g_test_add_func("/qmp/dispatch_cmd_arg_deprecated", + test_dispatch_cmd_arg_deprecated); g_test_add_func("/qmp/dispatch_cmd_ret_deprecated", test_dispatch_cmd_ret_deprecated); g_test_add_func("/qmp/dealloc_types", test_dealloc_types); diff --git a/scripts/qapi/commands.py b/scripts/qapi/commands.py index 35b79c554d..3fb4ed42ed 100644 --- a/scripts/qapi/commands.py +++ b/scripts/qapi/commands.py @@ -132,7 +132,7 @@ def gen_marshal(name, arg_type, boxed, ret_type): push_indent() ret += mcgen(''' - v = qobject_input_visitor_new(QOBJECT(args)); + v = qobject_input_visitor_new_qmp(QOBJECT(args)); visit_start_struct(v, NULL, NULL, 0, &err); if (err) { goto out; @@ -194,9 +194,13 @@ out: return ret -def gen_register_command(name, success_response, allow_oob, allow_preconfig): +def gen_register_command(name, features, + success_response, allow_oob, allow_preconfig): options = [] + if 'deprecated' in [f.name for f in features]: + options += ['QCO_DEPRECATED'] + if not success_response: options += ['QCO_NO_SUCCESS_RESP'] if allow_oob: @@ -302,8 +306,8 @@ void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type)) self._genh.add(gen_marshal_decl(name)) self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) - self._regy.add(gen_register_command(name, success_response, - allow_oob, allow_preconfig)) + self._regy.add(gen_register_command( + name, features, success_response, allow_oob, allow_preconfig)) def gen_commands(schema, output_dir, prefix): diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 21df3abed2..9119eb015b 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -66,15 +66,12 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) push_indent() if deprecated: ret += mcgen(''' - if (visit_deprecated(v, "%(name)s")) { + if (visit_deprecated(v, "%(name)s", &err)) { ''', name=memb.name) push_indent() ret += mcgen(''' visit_type_%(c_type)s(v, "%(name)s", &obj->%(c_name)s, &err); - if (err) { - goto out; - } ''', c_type=memb.type.c_name(), name=memb.name, c_name=c_name(memb.name)) @@ -82,6 +79,11 @@ void visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp) pop_indent() ret += mcgen(''' } +''') + ret += mcgen(''' + if (err) { + goto out; + } ''') if memb.optional: pop_indent() -- 2.21.1