qapi: Generalize command policy checking

The code to check command policy can see special feature flag
'deprecated' as command flag QCO_DEPRECATED.  I want to make feature
flag 'unstable' visible there as well, so I can add policy for it.

To let me make it visible, add member @special_features (a bitset of
QapiSpecialFeature) to QmpCommand, and adjust the generator to pass it
through qmp_register_command().  Then replace "QCO_DEPRECATED in
@flags" by QAPI_DEPRECATED in @special_features", and drop
QCO_DEPRECATED.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Acked-by: John Snow <jsnow@redhat.com>
Message-Id: <20211028102520.747396-7-armbru@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2021-10-28 12:25:17 +02:00
parent a130728554
commit 6604e4757a
6 changed files with 17 additions and 12 deletions

View File

@ -25,7 +25,6 @@ typedef enum QmpCommandOptions
QCO_ALLOW_OOB = (1U << 1), QCO_ALLOW_OOB = (1U << 1),
QCO_ALLOW_PRECONFIG = (1U << 2), QCO_ALLOW_PRECONFIG = (1U << 2),
QCO_COROUTINE = (1U << 3), QCO_COROUTINE = (1U << 3),
QCO_DEPRECATED = (1U << 4),
} QmpCommandOptions; } QmpCommandOptions;
typedef struct QmpCommand typedef struct QmpCommand
@ -34,6 +33,7 @@ typedef struct QmpCommand
/* Runs in coroutine context if QCO_COROUTINE is set */ /* Runs in coroutine context if QCO_COROUTINE is set */
QmpCommandFunc *fn; QmpCommandFunc *fn;
QmpCommandOptions options; QmpCommandOptions options;
unsigned special_features;
QTAILQ_ENTRY(QmpCommand) node; QTAILQ_ENTRY(QmpCommand) node;
bool enabled; bool enabled;
const char *disable_reason; const char *disable_reason;
@ -42,7 +42,8 @@ typedef struct QmpCommand
typedef QTAILQ_HEAD(QmpCommandList, QmpCommand) QmpCommandList; typedef QTAILQ_HEAD(QmpCommandList, QmpCommand) QmpCommandList;
void qmp_register_command(QmpCommandList *cmds, const char *name, void qmp_register_command(QmpCommandList *cmds, const char *name,
QmpCommandFunc *fn, QmpCommandOptions options); QmpCommandFunc *fn, QmpCommandOptions options,
unsigned special_features);
const QmpCommand *qmp_find_command(const QmpCommandList *cmds, const QmpCommand *qmp_find_command(const QmpCommandList *cmds,
const char *name); const char *name);
void qmp_disable_command(QmpCommandList *cmds, const char *name, void qmp_disable_command(QmpCommandList *cmds, const char *name,

View File

@ -230,11 +230,13 @@ static void monitor_init_qmp_commands(void)
qmp_init_marshal(&qmp_commands); qmp_init_marshal(&qmp_commands);
qmp_register_command(&qmp_commands, "device_add", qmp_device_add, 0); qmp_register_command(&qmp_commands, "device_add",
qmp_device_add, 0, 0);
QTAILQ_INIT(&qmp_cap_negotiation_commands); QTAILQ_INIT(&qmp_cap_negotiation_commands);
qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities", qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); qmp_marshal_qmp_capabilities,
QCO_ALLOW_PRECONFIG, 0);
} }
/* Set the current CPU defined by the user. Callers must hold BQL. */ /* Set the current CPU defined by the user. Callers must hold BQL. */

View File

@ -176,7 +176,7 @@ QDict *qmp_dispatch(const QmpCommandList *cmds, QObject *request,
"The command %s has not been found", command); "The command %s has not been found", command);
goto out; goto out;
} }
if (cmd->options & QCO_DEPRECATED) { if (cmd->special_features & 1u << QAPI_DEPRECATED) {
switch (compat_policy.deprecated_input) { switch (compat_policy.deprecated_input) {
case COMPAT_POLICY_INPUT_ACCEPT: case COMPAT_POLICY_INPUT_ACCEPT:
break; break;

View File

@ -16,7 +16,8 @@
#include "qapi/qmp/dispatch.h" #include "qapi/qmp/dispatch.h"
void qmp_register_command(QmpCommandList *cmds, const char *name, void qmp_register_command(QmpCommandList *cmds, const char *name,
QmpCommandFunc *fn, QmpCommandOptions options) QmpCommandFunc *fn, QmpCommandOptions options,
unsigned special_features)
{ {
QmpCommand *cmd = g_malloc0(sizeof(*cmd)); QmpCommand *cmd = g_malloc0(sizeof(*cmd));
@ -27,6 +28,7 @@ void qmp_register_command(QmpCommandList *cmds, const char *name,
cmd->fn = fn; cmd->fn = fn;
cmd->enabled = true; cmd->enabled = true;
cmd->options = options; cmd->options = options;
cmd->special_features = special_features;
QTAILQ_INSERT_TAIL(cmds, cmd, node); QTAILQ_INSERT_TAIL(cmds, cmd, node);
} }

View File

@ -26,6 +26,7 @@ from .gen import (
QAPISchemaModularCVisitor, QAPISchemaModularCVisitor,
build_params, build_params,
ifcontext, ifcontext,
gen_special_features,
) )
from .schema import ( from .schema import (
QAPISchema, QAPISchema,
@ -217,9 +218,6 @@ def gen_register_command(name: str,
coroutine: bool) -> str: coroutine: bool) -> str:
options = [] options = []
if 'deprecated' in [f.name for f in features]:
options += ['QCO_DEPRECATED']
if not success_response: if not success_response:
options += ['QCO_NO_SUCCESS_RESP'] options += ['QCO_NO_SUCCESS_RESP']
if allow_oob: if allow_oob:
@ -231,10 +229,11 @@ def gen_register_command(name: str,
ret = mcgen(''' ret = mcgen('''
qmp_register_command(cmds, "%(name)s", qmp_register_command(cmds, "%(name)s",
qmp_marshal_%(c_name)s, %(opts)s); qmp_marshal_%(c_name)s, %(opts)s, %(feats)s);
''', ''',
name=name, c_name=c_name(name), name=name, c_name=c_name(name),
opts=' | '.join(options) or 0) opts=' | '.join(options) or 0,
feats=gen_special_features(features))
return ret return ret

View File

@ -146,7 +146,8 @@ static void init_qmp_commands(void)
QTAILQ_INIT(&qmp_cap_negotiation_commands); QTAILQ_INIT(&qmp_cap_negotiation_commands);
qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities", qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG); qmp_marshal_qmp_capabilities,
QCO_ALLOW_PRECONFIG, 0);
} }
static int getopt_set_loc(int argc, char **argv, const char *optstring, static int getopt_set_loc(int argc, char **argv, const char *optstring,