monitor: New commands netdev_add, netdev_del
v3 cover monitor: New commands netdev_add, netdev_del
v2: Avoid loss of help in parse_option_size(). Rebased.
v3: Fix detection of duplicate netdev id.
Markus Armbruster (11):
error: Put error definitions back in alphabetical order
error: New QERR_DUPLICATE_ID
error: Convert qemu_opts_create() to QError
error: New QERR_INVALID_PARAMETER_VALUE
error: Convert qemu_opts_set() to QError
error: Drop extra messages after qemu_opts_set() and
qemu_opts_parse()
error: Use QERR_INVALID_PARAMETER_VALUE instead of
QERR_INVALID_PARAMETER
error: Convert qemu_opts_validate() to QError
error: Convert net_client_init() to QError
error: New QERR_DEVICE_IN_USE
monitor: New commands netdev_add, netdev_del
hw/pci-hotplug.c | 2 - hw/qdev.c | 2 +- monitor.c | 6 ++- net.c | 95 +++++++++++++++++++++++++++++++++++++++++------------ net.h | 2 + qemu-config.c | 1 - qemu-monitor.hx | 30 +++++++++++++++++ qemu-option.c | 24 ++++++-------- qerror.c | 20 ++++++++++- qerror.h | 17 ++++++++-- vl.c | 5 --- 11 files changed, 152 insertions(+), 52 deletions(-)
PATCH v3 1/11 error: Put error definitions back in alphabetical order
Add suitable comments to help keerp them in order.
--- qerror.c | 12 ++++++++---- qerror.h | 8 +++++--- 2 files changed, 13 insertions(+), 7 deletions(-)
qerror.c +8 -4
@@ -38,6 +38,10 @@ static const QType qerror_type = {* for example: * * "running out of foo: %(foo)%%" +* +* Please keep the entries in alphabetical order. +* Use "sed -n '/^static.*qerror_table\[\]/,/^};/s/QERR_/&/gp' qerror.c | sort -c" +* to check. */ static const QErrorStringTable qerror_table[] = { {
@@ -65,10 +69,6 @@ static const QErrorStringTable qerror_table[] = { .desc = "Device '%(device)' could not be initialized", }, { - .error_fmt = QERR_DEVICE_NOT_ENCRYPTED, - .desc = "Device '%(device)' is not encrypted", - }, - { .error_fmt = QERR_DEVICE_LOCKED, .desc = "Device '%(device)' is locked", },
@@ -81,6 +81,10 @@ static const QErrorStringTable qerror_table[] = { .desc = "Device '%(device)' has not been activated by the guest", }, { + .error_fmt = QERR_DEVICE_NOT_ENCRYPTED, + .desc = "Device '%(device)' is not encrypted", + }, + { .error_fmt = QERR_DEVICE_NOT_FOUND, .desc = "Device '%(device)' not found", },
qerror.h +5 -3
@@ -46,6 +46,8 @@ QError *qobject_to_qerror(const QObject *obj);/* * QError class list +* Please keep the definitions in alphabetical order. +* Use "grep '^#define QERR_' qerror.h | sort -c" to check. */ #define QERR_BAD_BUS_FOR_DEVICE \ "{ 'class': 'BadBusForDevice', 'data': { 'device': %s, 'bad_bus_type': %s } }"
@@ -62,9 +64,6 @@ QError *qobject_to_qerror(const QObject *obj);#define QERR_DEVICE_ENCRYPTED \ "{ 'class': 'DeviceEncrypted', 'data': { 'device': %s } }" -#define QERR_DEVICE_NOT_ENCRYPTED \ - "{ 'class': 'DeviceNotEncrypted', 'data': { 'device': %s } }" -#define QERR_DEVICE_INIT_FAILED \ "{ 'class': 'DeviceInitFailed', 'data': { 'device': %s } }"
@@ -77,6 +76,9 @@ QError *qobject_to_qerror(const QObject *obj);#define QERR_DEVICE_NOT_ACTIVE \ "{ 'class': 'DeviceNotActive', 'data': { 'device': %s } }" +#define QERR_DEVICE_NOT_ENCRYPTED \ + "{ 'class': 'DeviceNotEncrypted', 'data': { 'device': %s } }" +#define QERR_DEVICE_NOT_FOUND \ "{ 'class': 'DeviceNotFound', 'data': { 'device': %s } }"
Signature · 1 line
1.6.6.1
PATCH v3 2/11 error: New QERR_DUPLICATE_ID
In reply to Markus Armbruster--- qerror.c | 4 ++++ qerror.h | 3 +++ 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
@@ -97,6 +97,10 @@ static const QErrorStringTable qerror_table[] = { .desc = "Device '%(device)' has no child bus", }, { + .error_fmt = QERR_DUPLICATE_ID, + .desc = "Duplicate ID '%(id)' for %(object)", + }, + { .error_fmt = QERR_FD_NOT_FOUND, .desc = "File descriptor named '%(name)' not found", },
qerror.h +3 -0
@@ -88,6 +88,9 @@ QError *qobject_to_qerror(const QObject *obj);#define QERR_DEVICE_NO_BUS \ "{ 'class': 'DeviceNoBus', 'data': { 'device': %s } }" +#define QERR_DUPLICATE_ID \ + "{ 'class': 'DuplicateId', 'data': { 'id': %s, 'object': %s } }" +#define QERR_FD_NOT_FOUND \ "{ 'class': 'FdNotFound', 'data': { 'name': %s } }"
Signature · 1 line
1.6.6.1
PATCH v3 3/11 error: Convert qemu_opts_create() to QError
In reply to Markus ArmbrusterFixes device_add to report duplicate ID properly in QMP, as DuplicateId instead of UndefinedError.
--- qemu-option.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
qemu-option.c +2 -2
@@ -30,6 +30,7 @@#include "qemu-error.h" #include "qemu-objects.h" #include "qemu-option.h" +#include "qerror.h" /* * Extracts the name of an option from the parameter string (p points at the
@@ -643,8 +644,7 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exist opts =qemu_opts_find (list, id);if (opts !=NULL ) {if (fail_if_exists) { -fprintf (stderr,"tried to create id \"%s\" twice for \"%s\"\n" , - id, list->name); +qerror_report (QERR_DUPLICATE_ID , id, list->name);return NULL ; }else {return opts;
Signature · 1 line
1.6.6.1
PATCH v3 4/11 error: New QERR_INVALID_PARAMETER_VALUE
In reply to Markus Armbruster--- qerror.c | 4 ++++ qerror.h | 3 +++ 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
@@ -121,6 +121,10 @@ static const QErrorStringTable qerror_table[] = { .desc = "Invalid parameter type, expected: %(expected)", }, { + .error_fmt = QERR_INVALID_PARAMETER_VALUE, + .desc = "Parameter '%(name)' expects %(expected)", + }, + { .error_fmt = QERR_INVALID_PASSWORD, .desc = "Password incorrect", },
qerror.h +3 -0
@@ -106,6 +106,9 @@ QError *qobject_to_qerror(const QObject *obj);#define QERR_INVALID_PARAMETER_TYPE \ "{ 'class': 'InvalidParameterType', 'data': { 'name': %s,'expected': %s } }" +#define QERR_INVALID_PARAMETER_VALUE \ + "{ 'class': 'InvalidParameterValue', 'data': { 'name': %s, 'expected': %s } }" +#define QERR_INVALID_PASSWORD \ "{ 'class': 'InvalidPassword', 'data': {} }"
Signature · 1 line
1.6.6.1
PATCH v3 5/11 error: Convert qemu_opts_set() to QError
In reply to Markus Armbruster--- qemu-option.c | 17 +++++++---------- 1 files changed, 7 insertions(+), 10 deletions(-)
qemu-option.c +7 -10
@@ -176,7 +176,7 @@ static int parse_option_bool(const char *name, const char *value, int *ret) }else if (!strcmp (value,"off" )) { *ret =0 ; }else { -fprintf (stderr,"Option '%s': Use 'on' or 'off'\n" , name); +qerror_report (QERR_INVALID_PARAMETER_VALUE , name,"'on' or 'off'" );return -1 ; } }else {
@@ -193,12 +193,12 @@ static int parse_option_number(const char *name, const char *value, uint64_t *reif (value !=NULL ) { number =strtoull (value, &postfix,0 );if (*postfix !='\0' ) { -fprintf (stderr,"Option '%s' needs a number as parameter\n" , name); +qerror_report (QERR_INVALID_PARAMETER_VALUE , name,"a number" );return -1 ; } *ret = number; }else { -fprintf (stderr,"Option '%s' needs a parameter\n" , name); +qerror_report (QERR_INVALID_PARAMETER_VALUE , name,"a number" );return -1 ; }return 0 ;
@@ -226,13 +226,13 @@ static int parse_option_size(const char *name, const char *value, uint64_t *ret) *ret = (uint64_t ) sizef;break ;default : -fprintf (stderr,"Option '%s' needs size as parameter\n" , name); -fprintf (stderr,"You may use k, M, G or T suffixes for " +qerror_report (QERR_INVALID_PARAMETER_VALUE , name,"a size" ); +error_printf_unless_qmp ("You may use k, M, G or T suffixes for " "kilobytes, megabytes, gigabytes and terabytes.\n" );return -1 ; } }else { -fprintf (stderr,"Option '%s' needs a parameter\n" , name); +qerror_report (QERR_INVALID_PARAMETER_VALUE , name,"a size" );return -1 ; }return 0 ;
@@ -581,8 +581,7 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value)if (i ==0 ) {/* empty list -> allow any */ ; }else { -fprintf (stderr,"option \"%s\" is not valid for %s\n" , - name, opts->list->name); +qerror_report (QERR_INVALID_PARAMETER , name);return -1 ; } }
@@ -598,8 +597,6 @@ int qemu_opt_set(QemuOpts *opts, const char *name, const char *value) opt->str =qemu_strdup (value); }if (qemu_opt_parse (opt) <0 ) { -fprintf (stderr,"Failed to parse \"%s\" for \"%s.%s\"\n" , opt->str, - opts->list->name, opt->name);qemu_opt_del (opt);return -1 ; }
Signature · 1 line
1.6.6.1
PATCH v3 6/11 error: Drop extra messages after qemu_opts_set() and qemu_opts_parse()
In reply to Markus ArmbrusterBoth functions report errors nicely enough now, no need for additional messages.
--- hw/pci-hotplug.c | 2 -- net.c | 2 -- qemu-config.c | 1 - vl.c | 5 ----- 4 files changed, 0 insertions(+), 10 deletions(-)
hw/pci-hotplug.c +0 -2
@@ -56,8 +56,6 @@ static PCIDevice *qemu_pci_hot_add_nic(Monitor *mon, opts =qemu_opts_parse (&qemu_net_opts, opts_str ? opts_str :"" ,0 );if (!opts) { -monitor_printf (mon,"parsing network options '%s' failed\n" , - opts_str ? opts_str :"" );return NULL ; }
net.c +0 -2
@@ -1161,8 +1161,6 @@ void net_host_device_add(Monitor *mon, const QDict *qdict) opts =qemu_opts_parse (&qemu_net_opts, opts_str ? opts_str :"" ,0 );if (!opts) { -monitor_printf (mon,"parsing network options '%s' failed\n" , - opts_str ? opts_str :"" );return ; }
qemu-config.c +0 -1
@@ -472,7 +472,6 @@ int qemu_config_parse(FILE *fp, const char *fname) goto out; }if (qemu_opt_set (opts, arg, value) !=0 ) { -error_report ("failed to set \"%s\" for %s" , arg, group); goto out; }continue ;
vl.c +0 -5
@@ -766,8 +766,6 @@ QemuOpts *drive_add(const char *file, const char *fmt, ...) opts =qemu_opts_parse (&qemu_drive_opts, optstr,0 );if (!opts) { -fprintf (stderr,"%s: huh? duplicate? (%s)\n" , - __FUNCTION__, optstr);return NULL ; }if (file)
@@ -4244,7 +4242,6 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_mon: opts =qemu_opts_parse (&qemu_mon_opts, optarg,1 );if (!opts) { -fprintf (stderr,"parse error: %s\n" , optarg);exit (1 ); } default_monitor =0 ;
@@ -4252,7 +4249,6 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_chardev: opts =qemu_opts_parse (&qemu_chardev_opts, optarg,1 );if (!opts) { -fprintf (stderr,"parse error: %s\n" , optarg);exit (1 ); }break ;
@@ -4464,7 +4460,6 @@ int main(int argc, char **argv, char **envp)case QEMU_OPTION_rtc: opts =qemu_opts_parse (&qemu_rtc_opts, optarg,0 );if (!opts) { -fprintf (stderr,"parse error: %s\n" , optarg);exit (1 ); }configure_rtc (opts);
Signature · 1 line
1.6.6.1
PATCH v3 7/11 error: Use QERR_INVALID_PARAMETER_VALUE instead of QERR_INVALID_PARAMETER
In reply to Markus Armbruster--- hw/qdev.c | 2 +- monitor.c | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-)
hw/qdev.c +1 -1
@@ -207,7 +207,7 @@ DeviceState *qdev_device_add(QemuOpts *opts)/* find driver */ info =qdev_find_info (NULL , driver);if (!info || info->no_user) { -qerror_report (QERR_INVALID_PARAMETER ,"driver" ); +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"driver" ,"a driver name" );error_printf_unless_qmp ("Try with argument '?' for a list.\n" );return NULL ; }
monitor.c +4 -2
@@ -970,7 +970,8 @@ static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data) {int index =qdict_get_int (qdict,"index" );if (mon_set_cpu (index) <0 ) { -qerror_report (QERR_INVALID_PARAMETER ,"index" ); +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"index" , +"a CPU number" );return -1 ; }return 0 ;
@@ -2489,7 +2490,8 @@ static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data) }if (qemu_isdigit (fdname[0 ])) { -qerror_report (QERR_INVALID_PARAMETER ,"fdname" ); +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"fdname" , +"a name not starting with a digit" );return -1 ; }
Signature · 1 line
1.6.6.1
PATCH v3 8/11 error: Convert qemu_opts_validate() to QError
In reply to Markus Armbruster--- qemu-option.c | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
qemu-option.c +1 -2
@@ -877,8 +877,7 @@ int qemu_opts_validate(QemuOpts *opts, const QemuOptDesc *desc) } }if (desc[i].name ==NULL ) { -fprintf (stderr,"option \"%s\" is not valid for %s\n" , - opt->name, opts->list->name); +qerror_report (QERR_INVALID_PARAMETER , opt->name);return -1 ; }
Signature · 1 line
1.6.6.1
PATCH v3 9/11 error: Convert net_client_init() to QError
In reply to Markus ArmbrusterThe conversion is shallow: client type init() methods aren't converted. Converting them is a big job for relatively little practical benefit, so leave it for later.
--- net.c | 38 ++++++++++++++++++-------------------- 1 files changed, 18 insertions(+), 20 deletions(-)
net.c +18 -20
@@ -1057,18 +1057,12 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)int i; type =qemu_opt_get (opts,"type" ); +if (!type) { +qerror_report (QERR_MISSING_PARAMETER ,"type" ); +return -1 ; + } -if (!is_netdev) { -if (!type) { -error_report ("No type specified for -net" ); -return -1 ; - } - }else { -if (!type) { -error_report ("No type specified for -netdev" ); -return -1 ; - } - +if (is_netdev) { if (strcmp(type, "tap") != 0 &&#ifdef CONFIG_SLIRP strcmp(type, "user") != 0 &&
@@ -1077,21 +1071,21 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) strcmp(type, "vde") != 0 && #endif strcmp(type, "socket") != 0) { -error_report ("The '%s' network backend type is not valid with -netdev" , - type); +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"type" , +"a netdev backend type" );return -1 ; }if (qemu_opt_get (opts,"vlan" )) { -error_report ("The 'vlan' parameter is not valid with -netdev" ); +qerror_report (QERR_INVALID_PARAMETER ,"vlan" );return -1 ; }if (qemu_opt_get (opts,"name" )) { -error_report ("The 'name' parameter is not valid with -netdev" ); +qerror_report (QERR_INVALID_PARAMETER ,"name" );return -1 ; }if (!qemu_opts_id (opts)) { -error_report ("The id= parameter is required with -netdev" ); +qerror_report (QERR_MISSING_PARAMETER ,"id" );return -1 ; } }
@@ -1117,14 +1111,18 @@ int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) }if (net_client_types[i].init) { -return net_client_types[i].init (opts, mon, name, vlan); - }else { -return 0 ; +if (net_client_types[i].init (opts, mon, name, vlan) <0 ) { +/* TODO push error reporting into init() methods */ +qerror_report (QERR_DEVICE_INIT_FAILED , type); +return -1 ; + } } +return 0 ; } } -error_report ("Invalid -net type '%s'" , type); +qerror_report (QERR_INVALID_PARAMETER_VALUE ,"type" , +"a network client type" );return -1 ; }
Signature · 1 line
1.6.6.1
PATCH v3 10/11 error: New QERR_DEVICE_IN_USE
In reply to Markus Armbruster--- qerror.c | 4 ++++ qerror.h | 3 +++ 2 files changed, 7 insertions(+), 0 deletions(-)
qerror.c +4 -0
@@ -69,6 +69,10 @@ static const QErrorStringTable qerror_table[] = { .desc = "Device '%(device)' could not be initialized", }, { + .error_fmt = QERR_DEVICE_IN_USE, + .desc = "Device '%(device)' is in use", + }, + { .error_fmt = QERR_DEVICE_LOCKED, .desc = "Device '%(device)' is locked", },
qerror.h +3 -0
@@ -67,6 +67,9 @@ QError *qobject_to_qerror(const QObject *obj);#define QERR_DEVICE_INIT_FAILED \ "{ 'class': 'DeviceInitFailed', 'data': { 'device': %s } }" +#define QERR_DEVICE_IN_USE \ + "{ 'class': 'DeviceInUse', 'data': { 'device': %s } }" +#define QERR_DEVICE_LOCKED \ "{ 'class': 'DeviceLocked', 'data': { 'device': %s } }"
Signature · 1 line
1.6.6.1
PATCH v3 11/11 monitor: New commands netdev_add, netdev_del
In reply to Markus ArmbrusterMonitor commands to go with -netdev.
--- net.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ net.h | 2 ++ qemu-monitor.hx | 30 ++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 0 deletions(-)
net.c +55 -0
@@ -1186,6 +1186,61 @@ void net_host_device_remove(Monitor *mon, const QDict *qdict)qemu_del_vlan_client (vc); } +/** +* do_netdev_add(): Add a host network device +* +* Argument qdict contains +* - "type": the device type, "tap", "user", ... +* - "id": the device's ID (must be unique) +* - device options +* +* Example: +* +* { "type": "user", "id": "netdev1", "hostname": "a-guest" } +*/ +int do_netdev_add (Monitor *mon,const QDict *qdict,QObject **ret_data) +{ +QemuOpts *opts; +int res; + + opts =qemu_opts_from_qdict (&qemu_netdev_opts, qdict); +if (!opts) { +return -1 ; + } + + res =net_client_init (mon, opts,1 ); +return res; +} + +/** +* do_netdev_del(): Delete a host network device +* +* Argument qdict contains +* - "id": the device's ID +* +* Example: +* +* { "id": "netdev1" } +*/ +int do_netdev_del (Monitor *mon,const QDict *qdict,QObject **ret_data) +{ +const char *id =qdict_get_str (qdict,"id" ); +VLANClientState *vc; + + vc =qemu_find_netdev (id); +if (!vc || vc->info->type ==NET_CLIENT_TYPE_NIC ) { +qerror_report (QERR_DEVICE_NOT_FOUND , id); +return -1 ; + } +if (vc->peer) { +qerror_report (QERR_DEVICE_IN_USE , id); +return -1 ; + } +qemu_del_vlan_client (vc); +qemu_opts_del (qemu_opts_find (&qemu_netdev_opts, id)); +return 0 ; +} +void net_set_boot_mask (int net_boot_mask) {int i;
net.h +2 -0
@@ -166,6 +166,8 @@ void net_cleanup(void);void net_set_boot_mask (int boot_mask);void net_host_device_add (Monitor *mon,const QDict *qdict);void net_host_device_remove (Monitor *mon,const QDict *qdict); +int do_netdev_add (Monitor *mon,const QDict *qdict,QObject **ret_data); +int do_netdev_del (Monitor *mon,const QDict *qdict,QObject **ret_data);#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
qemu-monitor.hx +30 -0
@@ -913,6 +913,36 @@ STEXI Remove host VLAN client. ETEXI + { + .name = "netdev_add", + .args_type = "netdev:O", + .params = "[user|tap|socket],id=str[,prop=value][,...]", + .help = "add host network device", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_netdev_add, + }, + +STEXI +@item netdev_add +@findex netdev_add +Add host network device. +ETEXI + + { + .name = "netdev_del", + .args_type = "id:s", + .params = "id", + .help = "remove host network device", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_netdev_del, + }, + +STEXI +@item netdev_del +@findex netdev_del +Remove host network device. +ETEXI + #ifdef CONFIG_SLIRP { .name = "hostfwd_add",
Signature · 1 line
1.6.6.1
Quoted 3 lines · On Thu, 25 Mar 2010 17:22:29 +0100 Markus Armbruster <[email protected]> wrote:
v2: Avoid loss of help in
parse_option_size(). Rebased.v3: Fix detection of duplicate netdev id.
Yes, it's fixed. Looks good.