QMP: Introduce query-netdev
v2 cover QMP: Introduce query-netdev
This series implement the previously discussed QMP command query-netdev.
Regards,
changelog from v1
-----------------
- Fixed wrong usage of qemu_free() with vc->info_dict, now using QDECREF()
- Fixed QString leak in netdev_iter()
changelog from last protocol spec
---------------------------------
When "type" is "tap", the attribute "sndbuf" have been removed. "sndbuf" is not available on all platforms and most of the tap_set_sndbuf() implementations are stubs.
PATCH v2 1/7 QMP: Introduce the documentation for query-netdev and info netdev
These commands show the information about active backend network devices.
--- qemu-monitor.hx | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 100 insertions(+), 0 deletions(-)
qemu-monitor.hx +100 -0
@@ -1674,6 +1674,106 @@ show the various VLANs and the associated devices ETEXI STEXI +@item info netdev +show information about the current backend network devices +ETEXI +SQMP +query-netdev +------------ + +Each device is represented by a json-object. The returned value is a json-array +of all devices. + +Each json-object contains the following: + +- "id": the device's ID, must be unique (json-string) +- "type": device type (json-string) + - Possible values: "tap", "user", "vde" +- "peer": ID of the frontend guest device, if not available means that this + netdev is not connected to a guest device yet (json-string, optional) +- "info": json-object containing the configuration information about the device. + - When "type" is "tap", the following values might be available: + - "fd": available if connected to an already opened TAP interface + (json-int, optional) + - "script": path to an script used to configure the interface, + available only if an script is used. (json-string, optional, only + present if "fd" is not present) + - "downscript": path to an script used to deconfigure the interface, + available only if an script is used. (json-string, optional, only + present if "fd" is not present) + - "ifname": name of the attached host interface (json-string, only + present if "fd" is not present) + - "vhost": vhost acceleration status, true if enabled, false + otherwise (json-boolean) + - "vnet_hdr": true if the IFF_VNET_HDR flag must be set and is in + use, false otherwise (json-boolean) + - "vhostfd": fd used to connect the device to an already opened + vhost net device (json-int, optional) + - When "type" is "vde", the following values might be available: + - "sock": path to the VDE listening socket (json-string) + - "port": port number connected to virtual switch (json-int) + - "mode": permission mode, in octal (json-int) + - "group": group name (json-string, optional) + - When "type" is "user", the following values might be available: + - "hostname": client hostname reported by the builtin DHCP server, + (json-string, optional) + - "restrict": true if guest is isolated from the host, + false otherwise (json-boolean) + - "net": network address (json-string) + - "netmask": netmask (json-string) + - "host": guest-visible address of the host (json-string) + - "tftp": root directory of the built-in TFTP server (json-string, + optional) + - "bootfile": BOOTP filename (json-string, optional) + - "dhcpstart": the first of the 16 IPs the built-in DHCP server can + assign (json-string) + - "dns": guest-visible address of the virtual nameserver + (json-string) + - "smb": root directory of the built-in SMB server (json-string, + optional) + - "smbserver": IP address of the built-in SMB server (json-string, + optional) + - "hostfwd": guest port number to forward incoming TCP or UDP + connections (json-int, optional) + - "guestfwd": IP address and port to forward guest TCP connections + (json-int, optional) + +Example: + +-> { "execute": "query-netdev" } +<- { + "return": [ + { + "id": "tap.0", + "type": "tap", + "peer": "virtio-net-pci.0", + "info": { + "script": "/etc/qemu-ifup", + "downscript": "/etc/qemu-ifdown", + "ifname": "tap0", + "vnet_hdr": false, + "vhost": true + }, + }, + { + "id": "user.0", + "type": "user", + "peer": "e1000.0", + "info": { + "restrict": false, + "net": "10.0.2.0", + "netmask": "255.255.255.0", + "host": 10.0.2.2", + "dhcpstart": "10.2.0.15", + "dns": "10.0.2.3" + }, + }, + ] + } + +EQMP + +STEXI @item info chardev show the character devices ETEXI
Signature · 1 line
1.7.1
PATCH v2 2/7 QObject API: introduce qdict_to_qstring() function
In reply to Miguel Di Ciurcio FilhoThis is a helper function that converts a QDict to a QString, using the format:
key1=value1 SEP key2=value2 SEP key3=value3
Handy for debugging and formating the Monitor output.
--- qdict.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qdict.h | 2 ++ 2 files changed, 62 insertions(+), 0 deletions(-)
qdict.c +60 -0
@@ -267,6 +267,66 @@ const char *qdict_get_str(const QDict *qdict, const char *key)return qstring_get_str (qobject_to_qstring (obj)); } +struct qstring_pack { +QString *str; +size_t total_keys; +size_t current_key; +const char *separator; +}; + +static void qdict_to_qstring_iter (const char *key,QObject *obj,void *opaque) +{ +struct qstring_pack *pack = opaque; +qstring_append (pack->str, key); +qstring_append (pack->str,"=" ); +switch (qobject_type (obj)) { +case QTYPE_QSTRING : +qstring_append (pack->str,qstring_get_str (qobject_to_qstring (obj))); +break ; +case QTYPE_QINT : +qstring_append_int (pack->str,qint_get_int (qobject_to_qint (obj))); +break ; +case QTYPE_QBOOL : +qstring_append (pack->str,qbool_get_int (qobject_to_qbool (obj)) ?"true" : +"false" ); +break ; +default : +qstring_append (pack->str,"NULL" ); + } + + pack->current_key++; + +if (pack->current_key < pack->total_keys) { +qstring_append (pack->str, pack->separator); + } +} + +/** +* qdict_to_qstring(): Format a string with the keys and values of a QDict. +* +* Nested lists and dicts are not supported, yet. +* +* Return a pointer to a QString, with the following format: +* key1=value1 SEP key2=value2 SEP key3=value3 +*/ +QString *qdict_to_qstring (const QDict *qdict,const char *separator) +{ +struct qstring_pack *pack; +QString *str; + str =qstring_new (); + + pack =qemu_malloc (sizeof (*pack)); + pack->str = str; + pack->current_key =0 ; + pack->total_keys =qdict_size (qdict); + pack->separator = separator; + +qdict_iter (qdict, qdict_to_qstring_iter, pack); + +qemu_free (pack); + +return str; +}/** * qdict_get_try_int(): Try to get integer mapped by 'key' *
qdict.h +2 -0
@@ -15,6 +15,7 @@#include "qobject.h" #include "qlist.h" +#include "qstring.h" #include "qemu-queue.h" #include <stdint.h>
@@ -55,6 +56,7 @@ int qdict_get_bool(const QDict *qdict, const char *key);QList *qdict_get_qlist (const QDict *qdict,const char *key);QDict *qdict_get_qdict (const QDict *qdict,const char *key);const char *qdict_get_str (const QDict *qdict,const char *key); +QString *qdict_to_qstring (const QDict *qdict,const char *separator);int64_t qdict_get_try_int (const QDict *qdict,const char *key,int64_t err_value);const char *qdict_get_try_str (const QDict *qdict,const char *key);
Signature · 1 line
1.7.1
PATCH v2 3/7 net: Introduce VLANClientState->info_dict
In reply to Miguel Di Ciurcio FilhoThere is no standard format when formatting VLANClientState.info_str, so it is difficult to extract information and transmit it over QMP.
This patch adds info_dict, a QDict to better handle this information.
--- net.c | 1 + net.h | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
net.c +1 -0
@@ -301,6 +301,7 @@ void qemu_del_vlan_client(VLANClientState *vc)qemu_free (vc->name);qemu_free (vc->model); +QDECREF (vc->info_dict);qemu_free (vc); }
net.h +1 -0
@@ -65,6 +65,7 @@ struct VLANClientState {char *model;char *name;char info_str[256 ]; +QDict *info_dict; unsigned receive_disabled : 1; };
Signature · 1 line
1.7.1
PATCH v2 4/7 net: tap/tap-win32: introduce info_dict
In reply to Miguel Di Ciurcio Filho--- net/tap-win32.c | 7 +++++++ net/tap.c | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletions(-)
net/tap-win32.c +7 -0
@@ -32,6 +32,8 @@#include "net.h" #include "sysemu.h" #include "qemu-error.h" +#include "qdict.h" +#include "qstring.h" #include <stdio.h> #include <windows.h> #include <winioctl.h>
@@ -693,6 +695,11 @@ static int tap_win32_init(VLANState *vlan, const char *model,snprintf (s->nc.info_str,sizeof (s->nc.info_str),"tap: ifname=%s" , ifname); + nc->info_dict = qdict_new() +qdict_put (nc->info_dict,"ifname" ,qstring_from_str (ifname)); +qdict_put (nc->info_dict,"vhost" ,qbool_from_int (0 )); +qdict_put (nc->info_dict,"vnet_hdr" ,qbool_from_int (0 )); + s->handle = handle;qemu_add_wait_object (s->handle->tap_semaphore, tap_win32_send, s);
net/tap.c +21 -1
@@ -39,6 +39,8 @@#include "qemu-char.h" #include "qemu-common.h" #include "qemu-error.h" +#include "qint.h" +#include "qbool.h" #include "net/tap-linux.h"
@@ -448,6 +450,11 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlanif (qemu_opt_get (opts,"fd" )) {snprintf (s->nc.info_str,sizeof (s->nc.info_str),"fd=%d" , fd); + +assert (s->nc.info_dict ==NULL ); + s->nc.info_dict =qdict_new (); +qdict_put (s->nc.info_dict,"fd" ,qint_from_int (fd)); + }else {const char *ifname, *script, *downscript;
@@ -459,12 +466,23 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan"ifname=%s,script=%s,downscript=%s" , ifname, script, downscript); -if (strcmp (downscript,"no" ) !=0 ) { +assert (s->nc.info_dict ==NULL ); + s->nc.info_dict =qdict_new (); +qdict_put (s->nc.info_dict,"ifname" ,qstring_from_str (ifname)); + +if (strcmp (downscript,"no" ) !=0 && downscript[0 ] !='\0' ) {snprintf (s->down_script,sizeof (s->down_script),"%s" , downscript);snprintf (s->down_script_arg,sizeof (s->down_script_arg),"%s" , ifname); +qdict_put (s->nc.info_dict,"downscript" ,qstring_from_str (downscript)); + } + +if (strcmp (script,"no" ) !=0 && script[0 ] !='\0' ) { +qdict_put (s->nc.info_dict,"script" ,qstring_from_str (script)); } } +qdict_put (s->nc.info_dict,"vnet_hdr" ,qbool_from_int (s->has_vnet_hdr)); +if (qemu_opt_get_bool (opts,"vhost" , !!qemu_opt_get (opts,"vhostfd" ))) {int vhostfd, r;if (qemu_opt_get (opts,"vhostfd" )) {
@@ -473,6 +491,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlanreturn -1 ; } vhostfd = r; +qdict_put (s->nc.info_dict,"vhostfd" ,qint_from_int (vhostfd)); }else { vhostfd =-1 ; }
@@ -486,6 +505,7 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlanreturn -1 ; } +qdict_put (s->nc.info_dict,"vhost" ,qbool_from_int (s->vhost_net ?1 :0 ));return 0 ; }
Signature · 1 line
1.7.1
PATCH v2 5/7 net: vde: introduce info_dict
In reply to Miguel Di Ciurcio Filho--- net/vde.c | 14 ++++++++++++++ 1 files changed, 14 insertions(+), 0 deletions(-)
net/vde.c +14 -0
@@ -31,6 +31,9 @@#include "qemu-char.h" #include "qemu-common.h" #include "qemu-option.h" +#include "qdict.h" +#include "qstring.h" +#include "qint.h" #include "sysemu.h" typedef struct VDEState {
@@ -102,6 +105,17 @@ static int net_vde_init(VLANState *vlan, const char *model,snprintf (nc->info_str,sizeof (nc->info_str),"sock=%s,fd=%d" , sock,vde_datafd (vde)); +assert (nc->info_dict ==NULL ); + nc->info_dict =qdict_new (); +qdict_put (nc->info_dict,"sock" ,qstring_from_str (sock)); +qdict_put (nc->info_dict,"fd" ,qint_from_int (vde_datafd (vde))); +qdict_put (nc->info_dict,"port" ,qint_from_int (port)); +qdict_put (nc->info_dict,"mode" ,qint_from_int (mode)); + +if (group) { +qdict_put (nc->info_dict,"group" ,qstring_from_str (group)); + } + s =DO_UPCAST (VDEState, nc, nc); s->vde = vde;
Signature · 1 line
1.7.1
PATCH v2 6/7 net: slirp: introduce info_dict
In reply to Miguel Di Ciurcio Filho--- net/slirp.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 files changed, 39 insertions(+), 3 deletions(-)
net/slirp.c +39 -3
@@ -32,6 +32,9 @@#include "monitor.h" #include "sysemu.h" #include "qemu_socket.h" +#include "qdict.h" +#include "qbool.h" +#include "qstring.h" #include "slirp/libslirp.h" static int get_str_sep (char *buf,int buf_size,const char **pp,int sep)
@@ -243,6 +246,26 @@ static int net_slirp_init(VLANState *vlan, const char *model,snprintf (nc->info_str,sizeof (nc->info_str),"net=%s, restricted=%c" ,inet_ntoa (net), restricted ?'y' :'n' ); + nc->info_dict =qdict_new (); +qdict_put (nc->info_dict,"net" ,qstring_from_str (inet_ntoa (net))); +qdict_put (nc->info_dict,"host" ,qstring_from_str (inet_ntoa (host))); +qdict_put (nc->info_dict,"dns" ,qstring_from_str (inet_ntoa (dns))); +qdict_put (nc->info_dict,"dhcpstart" ,qstring_from_str (inet_ntoa (dhcp))); +qdict_put (nc->info_dict,"netmask" ,qstring_from_str (inet_ntoa (mask))); +qdict_put (nc->info_dict,"restrict" ,qbool_from_int (restricted)); + +if (vhostname) { +qdict_put (nc->info_dict,"hostname" ,qstring_from_str (vhostname)); + } + +if (tftp_export) { +qdict_put (nc->info_dict,"tftp" ,qstring_from_str (tftp_export)); + } + +if (bootfile) { +qdict_put (nc->info_dict,"bootfile" ,qstring_from_str (bootfile)); + } + s =DO_UPCAST (SlirpState, nc, nc); s->slirp = slirp_init(restricted, net, mask, host, vhostname,
@@ -252,12 +275,20 @@ static int net_slirp_init(VLANState *vlan, const char *model,for (config = slirp_configs; config; config = config->next) {if (config->flags &SLIRP_CFG_HOSTFWD ) {if (slirp_hostfwd (s, config->str, - config->flags &SLIRP_CFG_LEGACY ) <0 ) + config->flags &SLIRP_CFG_LEGACY ) <0 ) { goto error; + }else { +qdict_put (nc->info_dict,"hostfwd" , +qstring_from_str (config->str)); + } }else {if (slirp_guestfwd (s, config->str, - config->flags &SLIRP_CFG_LEGACY ) <0 ) + config->flags &SLIRP_CFG_LEGACY ) <0 ) { goto error; + }else { +qdict_put (nc->info_dict,"guestfwd" , +qstring_from_str (config->str)); + } } }#ifndef _WIN32
@@ -265,8 +296,13 @@ static int net_slirp_init(VLANState *vlan, const char *model, smb_export = legacy_smb_export; }if (smb_export) { -if (slirp_smb (s, smb_export, smbsrv) <0 ) +if (slirp_smb (s, smb_export, smbsrv) <0 ) { goto error; + }else { +qdict_put (nc->info_dict,"smb" ,qstring_from_str (smb_export)); +qdict_put (nc->info_dict,"smbserver" , +qstring_from_str (inet_ntoa (smbsrv))); + } }#endif
Signature · 1 line
1.7.1
PATCH v2 7/7 monitor/net: introduce 'info netdev' with QMP support
In reply to Miguel Di Ciurcio Filho--- monitor.c | 8 +++++++ net.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ net.h | 2 + 3 files changed, 81 insertions(+), 0 deletions(-)
monitor.c +8 -0
@@ -2314,6 +2314,14 @@ static const mon_cmd_t info_cmds[] = { .mhandler.info = do_info_network, }, { + .name = "netdev", + .args_type = "", + .params = "", + .help = "show information about network backend devices", + .user_print = do_info_netdev_print, + .mhandler.info_new = do_info_netdev, + }, + { .name = "chardev", .args_type = "", .params = "",
net.c +71 -0
83 lines not on this page · raw message
net.h +2 -0
@@ -118,6 +118,8 @@ int qemu_find_nic_model(NICInfo *nd, const char * const *models, const char *default_model);void do_info_network (Monitor *mon); +void do_info_netdev_print (Monitor *mon,const QObject *ret_data); +void do_info_netdev (Monitor *mon,QObject **ret_data);int do_set_link (Monitor *mon,const QDict *qdict,QObject **ret_data);/* NIC info */
Signature · 1 line
1.7.1
Quoted 1 line · On Wed, 30 Jun 2010 11:52:57 -0300 Miguel Di Ciurcio Filho…
This series implement the previously discussed QMP command query-netdev.
Looks good to me now, applied to the monitor queue.
Thanks.