qapi: Implement deprecated-input=reject for QMP command arguments

This policy rejects deprecated input, and thus permits "testing the
future".  Implement it for QMP command arguments: reject commands with
deprecated ones.  Example: when QEMU is run with -compat
deprecated-input=reject, then

    {"execute": "eject", "arguments": {"device": "cd"}}

fails like this

    {"error": {"class": "GenericError", "desc": "Deprecated parameter 'device' disabled by policy"}}

When the deprecated parameter is removed, the error will change to

    {"error": {"class": "GenericError", "desc": "Parameter 'device' is unexpected"}}

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20210318155519.1224118-11-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2021-03-18 16:55:18 +01:00
parent d2032598c4
commit db29164103
11 changed files with 101 additions and 2 deletions

View File

@ -17,6 +17,15 @@
extern CompatPolicy compat_policy;
/*
* 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 output visitor for @obj for use with QMP
*

View File

@ -15,6 +15,7 @@
#ifndef QOBJECT_INPUT_VISITOR_H
#define QOBJECT_INPUT_VISITOR_H
#include "qapi/qapi-types-compat.h"
#include "qapi/visitor.h"
typedef struct QObjectInputVisitor QObjectInputVisitor;
@ -58,6 +59,9 @@ typedef struct QObjectInputVisitor QObjectInputVisitor;
*/
Visitor *qobject_input_visitor_new(QObject *obj);
void qobject_input_visitor_set_policy(Visitor *v,
CompatPolicyInput deprecated);
/*
* Create a QObject input visitor for @obj for use with keyval_parse()
*

View File

@ -113,6 +113,9 @@ struct Visitor
The core takes care of the return type in the public interface. */
void (*optional)(Visitor *v, const char *name, bool *present);
/* Optional */
bool (*deprecated_accept)(Visitor *v, const char *name, Error **errp);
/* Optional */
bool (*deprecated)(Visitor *v, const char *name);

View File

@ -459,6 +459,15 @@ void visit_end_alternate(Visitor *v, void **obj);
*/
bool visit_optional(Visitor *v, const char *name, bool *present);
/*
* Should we reject deprecated member @name?
*
* @name must not be NULL. This function is only useful between
* visit_start_struct() and visit_end_struct(), since only objects
* have deprecated members.
*/
bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp);
/*
* Should we visit deprecated member @name?
*

View File

@ -135,6 +135,15 @@ bool visit_optional(Visitor *v, const char *name, bool *present)
return *present;
}
bool visit_deprecated_accept(Visitor *v, const char *name, Error **errp)
{
trace_visit_deprecated_accept(v, name);
if (v->deprecated_accept) {
return v->deprecated_accept(v, name, errp);
}
return true;
}
bool visit_deprecated(Visitor *v, const char *name)
{
trace_visit_deprecated(v, name);

View File

@ -19,6 +19,7 @@
#include "qapi/qmp/dispatch.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qjson.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/qobject-output-visitor.h"
#include "sysemu/runstate.h"
#include "qapi/qmp/qbool.h"
@ -27,6 +28,14 @@
CompatPolicy compat_policy;
Visitor *qobject_input_visitor_new_qmp(QObject *obj)
{
Visitor *v = qobject_input_visitor_new(obj);
qobject_input_visitor_set_policy(v, compat_policy.deprecated_input);
return v;
}
Visitor *qobject_output_visitor_new_qmp(QObject **result)
{
Visitor *v = qobject_output_visitor_new(result);

View File

@ -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;
@ -662,6 +664,23 @@ static void qobject_input_optional(Visitor *v, const char *name, bool *present)
*present = true;
}
static bool qobject_input_deprecated_accept(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);
@ -696,6 +715,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_accept = qobject_input_deprecated_accept;
v->visitor.free = qobject_input_free;
v->root = qobject_ref(obj);
@ -718,6 +738,14 @@ Visitor *qobject_input_visitor_new(QObject *obj)
return &v->visitor;
}
void qobject_input_visitor_set_policy(Visitor *v,
CompatPolicyInput deprecated)
{
QObjectInputVisitor *qiv = to_qiv(v);
qiv->deprecated_policy = deprecated;
}
Visitor *qobject_input_visitor_new_keyval(QObject *obj)
{
QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);

View File

@ -17,6 +17,7 @@ visit_start_alternate(void *v, const char *name, void *obj, size_t size) "v=%p n
visit_end_alternate(void *v, void *obj) "v=%p obj=%p"
visit_optional(void *v, const char *name, bool *present) "v=%p name=%s present=%p"
visit_deprecated_accept(void *v, const char *name) "v=%p name=%s"
visit_deprecated(void *v, const char *name) "v=%p name=%s"
visit_type_enum(void *v, const char *name, int *obj) "v=%p name=%s obj=%p"

View File

@ -154,7 +154,7 @@ def gen_marshal(name: str,
ret += mcgen('''
v = qobject_input_visitor_new(QOBJECT(args));
v = qobject_input_visitor_new_qmp(QOBJECT(args));
if (!visit_start_struct(v, NULL, NULL, 0, errp)) {
goto out;
}
@ -258,7 +258,6 @@ class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor):
#include "qapi/compat-policy.h"
#include "qapi/visitor.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qobject-input-visitor.h"
#include "qapi/dealloc-visitor.h"
#include "qapi/error.h"
#include "%(visit)s.h"

View File

@ -87,6 +87,9 @@ bool visit_type_%(c_name)s_members(Visitor *v, %(c_name)s *obj, Error **errp)
indent.increase()
if deprecated:
ret += mcgen('''
if (!visit_deprecated_accept(v, "%(name)s", errp)) {
return false;
}
if (visit_deprecated(v, "%(name)s")) {
''',
name=memb.name)

View File

@ -303,6 +303,29 @@ static void test_dispatch_cmd_deprecated(void)
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' }";
@ -403,6 +426,8 @@ int main(int argc, char **argv)
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);