From nobody Thu May 2 12:28:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1573680481; cv=none; d=zoho.com; s=zohoarc; b=jWPOzo2/yaKwz7hxkrFHeRswIfSn/6dQ8gZ7730gJ4Hv9SWg8gU0ArIoTosKmfwlqEKyMoVnZQYGyFBg5aMTcPg5RJlO9kvrpKCNkk9nXI8EUsloGolRGF6QD7QK/mzEfRNO3j5mP+DkmAMLT5ez/xWgnEzHDVTeAQ8jXhUM0x8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573680481; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=46w9GOdwbCDnuQ/KUtJkaDz4gzIQyuI7I06BAlfkD1c=; b=XO8ZO2l3jbMrEi9/UYHEU2Jy1/eHRfIzsK/fzHiOKGKxbFzHfwz9X88ZyOprVh4A6QckupIPXM/UjXEi6VXeGnh8aZuZ2jHPMJxFT5jxT/U/PY2xzoUNYcToc8ujbUbUhwALUkyeHIjdPALEYJV4miYKa3s0RtpICPYwyrH8uFo= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573680481742219.08535992153008; Wed, 13 Nov 2019 13:28:01 -0800 (PST) Received: from localhost ([::1]:51196 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV0BE-0002HO-Ex for importer@patchew.org; Wed, 13 Nov 2019 16:28:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58933) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV093-0001I0-R8 for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iV08z-0004NA-84 for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:45 -0500 Received: from forwardcorp1o.mail.yandex.net ([95.108.205.193]:55820) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iV08y-0004Li-3A for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:40 -0500 Received: from mxbackcorp1o.mail.yandex.net (mxbackcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::301]) by forwardcorp1o.mail.yandex.net (Yandex) with ESMTP id E6EE22E1588; Thu, 14 Nov 2019 00:25:36 +0300 (MSK) Received: from vla1-5826f599457c.qloud-c.yandex.net (vla1-5826f599457c.qloud-c.yandex.net [2a02:6b8:c0d:35a1:0:640:5826:f599]) by mxbackcorp1o.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id uDFz0M5nEs-Pa4a2JrA; Thu, 14 Nov 2019 00:25:36 +0300 Received: from unknown (unknown [2a02:6b8:b080:7204::1:11]) by vla1-5826f599457c.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id dbNdL4rsWi-PZV8sUVT; Thu, 14 Nov 2019 00:25:35 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1573680336; bh=46w9GOdwbCDnuQ/KUtJkaDz4gzIQyuI7I06BAlfkD1c=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=zfNdNW+uM7m/GrvLGH6NoW0/3THRvb4/szvSw42y76R0rER5h5qgjqLo7gQximApm Bb3s46mT82rD9qs/3L4Sg5/Fsj9JPSQ6ePdfKm6hHo1RAu702xlySvidbK3qWqVVV3 RFchoxNw07RPJoz5FRzFrbV0zMRBzvjVZ+FmBQBk= Authentication-Results: mxbackcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Alexey Kirillov To: Jason Wang , Samuel Thibault , Markus Armbruster Subject: [PATCH 1/2] qapi: net: Add query-netdevs command Date: Thu, 14 Nov 2019 00:25:15 +0300 Message-Id: <20191113212516.13606-2-lekiravi@yandex-team.ru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191113212516.13606-1-lekiravi@yandex-team.ru> References: <20191113212516.13606-1-lekiravi@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 95.108.205.193 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Michael S. Tsirkin" , Jan Kiszka , qemu-devel@nongnu.org, Vincenzo Maffione , yc-core@yandex-team.ru, Alexey Kirillov , Giuseppe Lettieri , Luigi Rizzo Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a qmp command that provides information about currently attached network devices and their configuration. Signed-off-by: Alexey Kirillov --- include/net/net.h | 1 + net/hub.c | 8 +++ net/l2tpv3.c | 19 +++++++ net/net.c | 80 +++++++++++++++++++++++++++++ net/netmap.c | 13 +++++ net/slirp.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++ net/socket.c | 71 ++++++++++++++++++++++++++ net/tap-win32.c | 9 ++++ net/tap.c | 103 +++++++++++++++++++++++++++++++++++-- net/vde.c | 26 ++++++++++ net/vhost-user.c | 18 +++++-- qapi/net.json | 85 +++++++++++++++++++++++++++++++ 12 files changed, 551 insertions(+), 8 deletions(-) diff --git a/include/net/net.h b/include/net/net.h index e175ba9677..2c8956c0b3 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -92,6 +92,7 @@ struct NetClientState { char *model; char *name; char info_str[256]; + NetdevInfo *stored_config; unsigned receive_disabled : 1; NetClientDestructor *destructor; unsigned int queue_index; diff --git a/net/hub.c b/net/hub.c index 5795a678ed..37995b5517 100644 --- a/net/hub.c +++ b/net/hub.c @@ -148,6 +148,7 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const = char *name, NetHubPort *port; int id =3D hub->num_ports++; char default_name[128]; + NetdevHubPortOptions *stored; =20 if (!name) { snprintf(default_name, sizeof(default_name), @@ -160,6 +161,13 @@ static NetHubPort *net_hub_port_new(NetHub *hub, const= char *name, port->id =3D id; port->hub =3D hub; =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_HUBPORT; + stored =3D &nc->stored_config->u.hubport; + + stored->hubid =3D hub->id; + QLIST_INSERT_HEAD(&hub->ports, port, next); =20 return port; diff --git a/net/l2tpv3.c b/net/l2tpv3.c index 55fea17c0f..f4e45e7b28 100644 --- a/net/l2tpv3.c +++ b/net/l2tpv3.c @@ -535,6 +535,7 @@ int net_init_l2tpv3(const Netdev *netdev, struct addrinfo hints; struct addrinfo *result =3D NULL; char *srcport, *dstport; + NetdevL2TPv3Options *stored; =20 nc =3D qemu_new_net_client(&net_l2tpv3_info, peer, "l2tpv3", name); =20 @@ -726,6 +727,24 @@ int net_init_l2tpv3(const Netdev *netdev, =20 l2tpv3_read_poll(s, true); =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_L2TPV3; + stored =3D &nc->stored_config->u.l2tpv3; + + memcpy(stored, l2tpv3, sizeof(NetdevL2TPv3Options)); + + stored->src =3D g_strdup(l2tpv3->src); + stored->dst =3D g_strdup(l2tpv3->dst); + + if (l2tpv3->has_srcport) { + stored->srcport =3D g_strdup(l2tpv3->srcport); + } + + if (l2tpv3->has_dstport) { + stored->dstport =3D g_strdup(l2tpv3->dstport); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "l2tpv3: connected"); return 0; diff --git a/net/net.c b/net/net.c index 84aa6d8d00..08bf78a668 100644 --- a/net/net.c +++ b/net/net.c @@ -54,6 +54,7 @@ #include "sysemu/sysemu.h" #include "net/filter.h" #include "qapi/string-output-visitor.h" +#include "qapi/clone-visitor.h" =20 /* Net bridge is currently not supported for W32. */ #if !defined(_WIN32) @@ -283,6 +284,7 @@ NICState *qemu_new_nic(NetClientInfo *info, NetClientState **peers =3D conf->peers.ncs; NICState *nic; int i, queues =3D MAX(1, conf->peers.queues); + NetLegacyNicOptions *stored; =20 assert(info->type =3D=3D NET_CLIENT_DRIVER_NIC); assert(info->size >=3D sizeof(NICState)); @@ -298,6 +300,22 @@ NICState *qemu_new_nic(NetClientInfo *info, nic->ncs[i].queue_index =3D i; } =20 + /* Store startup parameters */ + nic->ncs[0].stored_config =3D g_new0(NetdevInfo, 1); + nic->ncs[0].stored_config->type =3D NET_CLIENT_DRIVER_NIC; + stored =3D &nic->ncs[0].stored_config->u.nic; + + if (peers[0]) { + stored->has_netdev =3D true; + stored->netdev =3D g_strdup(peers[0]->name); + } + + stored->has_macaddr =3D true; + stored->macaddr =3D g_strdup_printf(MAC_FMT, MAC_ARG(conf->macaddr.a)); + + stored->has_model =3D true; + stored->model =3D g_strdup(model); + return nic; } =20 @@ -344,6 +362,7 @@ static void qemu_free_net_client(NetClientState *nc) } g_free(nc->name); g_free(nc->model); + qapi_free_NetdevInfo(nc->stored_config); if (nc->destructor) { nc->destructor(nc); } @@ -1323,6 +1342,67 @@ RxFilterInfoList *qmp_query_rx_filter(bool has_name,= const char *name, return filter_list; } =20 +NetdevInfoList *qmp_x_query_netdevs(Error **errp) +{ + NetdevInfoList *list =3D NULL; + NetClientState *nc; + + QTAILQ_FOREACH(nc, &net_clients, next) { + /* Only look at netdevs, not for each queue */ + if (nc->stored_config) { + NetdevInfoList *node =3D g_new0(NetdevInfoList, 1); + + node->value =3D QAPI_CLONE(NetdevInfo, nc->stored_config); + g_free(node->value->id); /* Need to dealloc default empty id */ + node->value->id =3D g_strdup(nc->name); + + if (nc->info->type =3D=3D NET_CLIENT_DRIVER_NIC) { + NICState *state =3D qemu_get_nic(nc); + assert(state); + + /* NIC have explicit number of queues */ + assert(state->conf); + node->value->queues_count =3D state->conf->peers.queues; + } else { + /* Not a NIC, but can have queues */ + NetClientState *nc_iterable; + + QTAILQ_FOREACH(nc_iterable, &net_clients, next) { + if (strcmp(nc_iterable->name, nc->name) =3D=3D 0) { + node->value->queues_count++; + } + } + } + + node->value->has_peer =3D nc->peer !=3D NULL; + if (node->value->has_peer) { + node->value->peer =3D g_strdup(nc->peer->name); + } + + node->value->has_hub =3D + net_hub_id_for_client(nc, (int *)&node->value->hub) =3D=3D= 0; + + /* + * Copy the current hubport peer id to connected netdev id, + * because it could have been changed at runtime + */ + if (nc->info->type =3D=3D NET_CLIENT_DRIVER_HUBPORT) { + if (node->value->has_peer) { + g_free(node->value->u.hubport.netdev); + + node->value->u.hubport.has_netdev =3D true; + node->value->u.hubport.netdev =3D g_strdup(nc->peer->n= ame); + } + } + + node->next =3D list; + list =3D node; + } + } + + return list; +} + void hmp_info_network(Monitor *mon, const QDict *qdict) { NetClientState *nc, *peer; diff --git a/net/netmap.c b/net/netmap.c index 350f097f91..71feacb92c 100644 --- a/net/netmap.c +++ b/net/netmap.c @@ -411,6 +411,7 @@ int net_init_netmap(const Netdev *netdev, NetClientState *nc; Error *err =3D NULL; NetmapState *s; + NetdevNetmapOptions *stored; =20 nmd =3D netmap_open(netmap_opts, &err); if (err) { @@ -427,6 +428,18 @@ int net_init_netmap(const Netdev *netdev, pstrcpy(s->ifname, sizeof(s->ifname), netmap_opts->ifname); netmap_read_poll(s, true); /* Initially only poll for reads. */ =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_NETMAP; + stored =3D &nc->stored_config->u.netmap; + + stored->ifname =3D g_strdup(netmap_opts->ifname); + + if (netmap_opts->has_devname) { + stored->has_devname =3D true; + stored->devname =3D g_strdup(netmap_opts->devname); + } + return 0; } =20 diff --git a/net/slirp.c b/net/slirp.c index c4334ee876..fb5b87ebed 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -346,6 +346,14 @@ static SaveVMHandlers savevm_slirp_state =3D { .load_state =3D net_slirp_state_load, }; =20 +#define APPEND_STRINGLIST(tail, new_val) \ + do { \ + *(tail) =3D g_new0(StringList, 1); \ + (*(tail))->value =3D g_new0(String, 1); \ + (*(tail))->value->str =3D g_strdup((new_val)); \ + (tail) =3D &((*(tail))->next); \ + } while (0) + static int net_slirp_init(NetClientState *peer, const char *model, const char *name, int restricted, bool ipv4, const char *vnetwork, const char *vho= st, @@ -378,6 +386,9 @@ static int net_slirp_init(NetClientState *peer, const c= har *model, int shift; char *end; struct slirp_config_str *config; + NetdevUserOptions *stored; + StringList **stored_hostfwd; + StringList **stored_guestfwd; =20 if (!ipv4 && (vnetwork || vhost || vnameserver)) { error_setg(errp, "IPv4 disabled but netmask/host/dns provided"); @@ -553,6 +564,112 @@ static int net_slirp_init(NetClientState *peer, const= char *model, =20 nc =3D qemu_new_net_client(&net_slirp_info, peer, model, name); =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_USER; + stored =3D &nc->stored_config->u.user; + + if (vhostname) { + stored->has_hostname =3D true; + stored->hostname =3D g_strdup(vhostname); + } + + stored->has_q_restrict =3D true; + stored->q_restrict =3D restricted; + + stored->has_ipv4 =3D true; + stored->ipv4 =3D ipv4; + + stored->has_ipv6 =3D true; + stored->ipv6 =3D ipv6; + + if (ipv4) { + uint8_t *net_bytes =3D (uint8_t *)&net; + uint8_t *mask_bytes =3D (uint8_t *)&mask; + + stored->has_net =3D true; + stored->net =3D g_strdup_printf("%d.%d.%d.%d/%d.%d.%d.%d", + net_bytes[0], net_bytes[1], + net_bytes[2], net_bytes[3], + mask_bytes[0], mask_bytes[1], + mask_bytes[2], mask_bytes[3]); + + stored->has_host =3D true; + stored->host =3D g_strdup(inet_ntoa(host)); + } + + if (tftp_export) { + stored->has_tftp =3D true; + stored->tftp =3D g_strdup(tftp_export); + } + + if (bootfile) { + stored->has_bootfile =3D true; + stored->bootfile =3D g_strdup(bootfile); + } + + if (vdhcp_start) { + stored->has_dhcpstart =3D true; + stored->dhcpstart =3D g_strdup(vdhcp_start); + } + + if (ipv4) { + stored->has_dns =3D true; + stored->dns =3D g_strdup(inet_ntoa(dns)); + } + + if (dnssearch) { + stored->has_dnssearch =3D true; + StringList **stored_list =3D &stored->dnssearch; + + for (int i =3D 0; dnssearch[i]; i++) { + APPEND_STRINGLIST(stored_list, dnssearch[i]); + } + } + + if (vdomainname) { + stored->has_domainname =3D true; + stored->domainname =3D g_strdup(vdomainname); + } + + if (ipv6) { + char addrstr[INET6_ADDRSTRLEN]; + const char *res; + + stored->has_ipv6_prefix =3D true; + stored->ipv6_prefix =3D g_strdup(vprefix6); + + stored->has_ipv6_prefixlen =3D true; + stored->ipv6_prefixlen =3D vprefix6_len; + + res =3D inet_ntop(AF_INET6, &ip6_host, + addrstr, sizeof(addrstr)); + + stored->has_ipv6_host =3D true; + stored->ipv6_host =3D g_strdup(res); + + res =3D inet_ntop(AF_INET6, &ip6_dns, + addrstr, sizeof(addrstr)); + + stored->has_ipv6_dns =3D true; + stored->ipv6_dns =3D g_strdup(res); + } + + if (smb_export) { + stored->has_smb =3D true; + stored->smb =3D g_strdup(smb_export); + } + + if (vsmbserver) { + stored->has_smbserver =3D true; + stored->smbserver =3D g_strdup(vsmbserver); + } + + if (tftp_server_name) { + stored->has_tftp_server_name =3D true; + stored->tftp_server_name =3D g_strdup(tftp_server_name); + } + snprintf(nc->info_str, sizeof(nc->info_str), "net=3D%s,restrict=3D%s", inet_ntoa(net), restricted ? "on" : "off"); @@ -582,14 +699,23 @@ static int net_slirp_init(NetClientState *peer, const= char *model, s->poll_notifier.notify =3D net_slirp_poll_notify; main_loop_poll_add_notifier(&s->poll_notifier); =20 + stored_hostfwd =3D &stored->hostfwd; + stored_guestfwd =3D &stored->guestfwd; + for (config =3D slirp_configs; config; config =3D config->next) { if (config->flags & SLIRP_CFG_HOSTFWD) { if (slirp_hostfwd(s, config->str, errp) < 0) { goto error; + } else { + stored->has_hostfwd =3D true; + APPEND_STRINGLIST(stored_hostfwd, config->str); } } else { if (slirp_guestfwd(s, config->str, errp) < 0) { goto error; + } else { + stored->has_guestfwd =3D true; + APPEND_STRINGLIST(stored_guestfwd, config->str); } } } diff --git a/net/socket.c b/net/socket.c index c92354049b..2d2d5419ca 100644 --- a/net/socket.c +++ b/net/socket.c @@ -342,6 +342,7 @@ static NetSocketState *net_socket_fd_init_dgram(NetClie= ntState *peer, NetSocketState *s; SocketAddress *sa; SocketAddressType sa_type; + NetdevSocketOptions *stored; =20 sa =3D socket_local_address(fd, errp); if (!sa) { @@ -385,8 +386,19 @@ static NetSocketState *net_socket_fd_init_dgram(NetCli= entState *peer, net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); net_socket_read_poll(s, true); =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_SOCKET; + stored =3D &nc->stored_config->u.socket; + + stored->has_fd =3D true; + stored->fd =3D g_strdup_printf("%d", fd); + /* mcast: save bound address as dst */ if (is_connected && mcast !=3D NULL) { + stored->has_mcast =3D true; + stored->mcast =3D g_strdup(mcast); + s->dgram_dst =3D saddr; snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=3D%d (cloned mcast=3D%s:%d)", @@ -428,6 +440,7 @@ static NetSocketState *net_socket_fd_init_stream(NetCli= entState *peer, { NetClientState *nc; NetSocketState *s; + NetdevSocketOptions *stored; =20 nc =3D qemu_new_net_client(&net_socket_info, peer, model, name); =20 @@ -447,6 +460,15 @@ static NetSocketState *net_socket_fd_init_stream(NetCl= ientState *peer, } else { qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); } + + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_SOCKET; + stored =3D &nc->stored_config->u.socket; + + stored->has_fd =3D true; + stored->fd =3D g_strdup_printf("%d", fd); + return s; } =20 @@ -483,6 +505,7 @@ static void net_socket_accept(void *opaque) struct sockaddr_in saddr; socklen_t len; int fd; + NetdevSocketOptions *stored; =20 for(;;) { len =3D sizeof(saddr); @@ -498,6 +521,13 @@ static void net_socket_accept(void *opaque) s->fd =3D fd; s->nc.link_down =3D false; net_socket_connect(s); + + /* Store additional startup parameters (extend net_socket_listen_init)= */ + stored =3D &s->nc.stored_config->u.socket; + + stored->has_fd =3D true; + stored->fd =3D g_strdup_printf("%d", fd); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: connection from %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); @@ -513,6 +543,7 @@ static int net_socket_listen_init(NetClientState *peer, NetSocketState *s; struct sockaddr_in saddr; int fd, ret; + NetdevSocketOptions *stored; =20 if (parse_host_port(&saddr, host_str, errp) < 0) { return -1; @@ -549,6 +580,15 @@ static int net_socket_listen_init(NetClientState *peer, net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); =20 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); + + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_SOCKET; + stored =3D &nc->stored_config->u.socket; + + stored->has_listen =3D true; + stored->listen =3D g_strdup(host_str); + return 0; } =20 @@ -561,6 +601,7 @@ static int net_socket_connect_init(NetClientState *peer, NetSocketState *s; int fd, connected, ret; struct sockaddr_in saddr; + NetdevSocketOptions *stored; =20 if (parse_host_port(&saddr, host_str, errp) < 0) { return -1; @@ -598,6 +639,12 @@ static int net_socket_connect_init(NetClientState *pee= r, return -1; } =20 + /* Store additional startup parameters (extend net_socket_fd_init) */ + stored =3D &s->nc.stored_config->u.socket; + + stored->has_connect =3D true; + stored->connect =3D g_strdup(host_str); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: connect to %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); @@ -615,6 +662,7 @@ static int net_socket_mcast_init(NetClientState *peer, int fd; struct sockaddr_in saddr; struct in_addr localaddr, *param_localaddr; + NetdevSocketOptions *stored; =20 if (parse_host_port(&saddr, host_str, errp) < 0) { return -1; @@ -643,6 +691,19 @@ static int net_socket_mcast_init(NetClientState *peer, =20 s->dgram_dst =3D saddr; =20 + /* Store additional startup parameters (extend net_socket_fd_init) */ + stored =3D &s->nc.stored_config->u.socket; + + if (!stored->has_mcast) { + stored->has_mcast =3D true; + stored->mcast =3D g_strdup(host_str); + } + + if (localaddr_str) { + stored->has_localaddr =3D true; + stored->localaddr =3D g_strdup(localaddr_str); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: mcast=3D%s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); @@ -660,6 +721,7 @@ static int net_socket_udp_init(NetClientState *peer, NetSocketState *s; int fd, ret; struct sockaddr_in laddr, raddr; + NetdevSocketOptions *stored; =20 if (parse_host_port(&laddr, lhost, errp) < 0) { return -1; @@ -698,6 +760,15 @@ static int net_socket_udp_init(NetClientState *peer, =20 s->dgram_dst =3D raddr; =20 + /* Store additional startup parameters (extend net_socket_fd_init) */ + stored =3D &s->nc.stored_config->u.socket; + + stored->has_localaddr =3D true; + stored->localaddr =3D g_strdup(lhost); + + stored->has_udp =3D true; + stored->udp =3D g_strdup(rhost); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: udp=3D%s:%d", inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); diff --git a/net/tap-win32.c b/net/tap-win32.c index 2b5dcda36e..20ba0b1dc8 100644 --- a/net/tap-win32.c +++ b/net/tap-win32.c @@ -768,6 +768,7 @@ static int tap_win32_init(NetClientState *peer, const c= har *model, NetClientState *nc; TAPState *s; tap_win32_overlapped_t *handle; + NetdevTapOptions *stored; =20 if (tap_win32_open(&handle, ifname) < 0) { printf("tap: Could not open '%s'\n", ifname); @@ -778,6 +779,14 @@ static int tap_win32_init(NetClientState *peer, const = char *model, =20 s =3D DO_UPCAST(TAPState, nc, nc); =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_TAP; + stored =3D &nc->stored_config->u.tap; + + stored->has_ifname =3D true; + stored->ifname =3D g_strdup(ifname); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "tap: ifname=3D%s", ifname); =20 diff --git a/net/tap.c b/net/tap.c index 6207f61f84..fb2631ce5e 100644 --- a/net/tap.c +++ b/net/tap.c @@ -584,6 +584,7 @@ int net_init_bridge(const Netdev *netdev, const char *n= ame, const char *helper, *br; TAPState *s; int fd, vnet_hdr; + NetdevBridgeOptions *stored; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_BRIDGE); bridge =3D &netdev->u.bridge; @@ -600,6 +601,21 @@ int net_init_bridge(const Netdev *netdev, const char *= name, vnet_hdr =3D tap_probe_vnet_hdr(fd); s =3D net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); =20 + /* Store startup parameters */ + s->nc.stored_config =3D g_new0(NetdevInfo, 1); + s->nc.stored_config->type =3D NET_CLIENT_DRIVER_BRIDGE; + stored =3D &s->nc.stored_config->u.bridge; + + if (br) { + stored->has_br =3D true; + stored->br =3D g_strdup(br); + } + + if (helper) { + stored->has_helper =3D true; + stored->helper =3D g_strdup(helper); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=3D%s,br=3D%s"= , helper, br); =20 @@ -647,11 +663,13 @@ static void net_init_tap_one(const NetdevTapOptions *= tap, NetClientState *peer, const char *model, const char *name, const char *ifname, const char *script, const char *downscript, const char *vhostfdna= me, - int vnet_hdr, int fd, Error **errp) + int vnet_hdr, int fd, NetdevInfo **common_sto= red, + Error **errp) { Error *err =3D NULL; TAPState *s =3D net_tap_fd_init(peer, model, name, fd, vnet_hdr); int vhostfd; + NetdevTapOptions *stored; =20 tap_set_sndbuf(s->fd, tap, &err); if (err) { @@ -659,12 +677,65 @@ static void net_init_tap_one(const NetdevTapOptions *= tap, NetClientState *peer, return; } =20 + /* Store startup parameters */ + if (!*common_stored) { + *common_stored =3D g_new0(NetdevInfo, 1); + (*common_stored)->type =3D NET_CLIENT_DRIVER_TAP; + s->nc.stored_config =3D *common_stored; + } + stored =3D &(*common_stored)->u.tap; + + if (tap->has_sndbuf && !stored->has_sndbuf) { + stored->has_sndbuf =3D true; + stored->sndbuf =3D tap->sndbuf; + } + + if (vnet_hdr && !stored->has_vnet_hdr) { + stored->has_vnet_hdr =3D true; + stored->vnet_hdr =3D true; + } + if (tap->has_fd || tap->has_fds) { + if (!stored->has_fds) { + stored->has_fds =3D true; + stored->fds =3D g_strdup_printf("%d", fd); + } else { + char *tmp_s =3D stored->fds; + stored->fds =3D g_strdup_printf("%s:%d", stored->fds, fd); + g_free(tmp_s); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=3D%d", fd); } else if (tap->has_helper) { + if (!stored->has_helper) { + stored->has_helper =3D true; + stored->helper =3D g_strdup(tap->helper); + } + + if (!stored->has_br) { + stored->has_br =3D true; + stored->br =3D tap->has_br ? g_strdup(tap->br) : + g_strdup(DEFAULT_BRIDGE_INTERFACE); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=3D%s", tap->helper); } else { + if (tap->has_ifname && !stored->has_ifname) { + stored->has_ifname =3D true; + stored->ifname =3D g_strdup(ifname); + } + + if (tap->has_script && !stored->has_script) { + stored->has_script =3D true; + stored->script =3D g_strdup(script); + } + + if (tap->has_downscript && !stored->has_downscript) { + stored->has_downscript =3D true; + stored->downscript =3D g_strdup(downscript); + } + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "ifname=3D%s,script=3D%s,downscript=3D%s", ifname, script, downscript); @@ -680,9 +751,20 @@ static void net_init_tap_one(const NetdevTapOptions *t= ap, NetClientState *peer, vhostfdname || (tap->has_vhostforce && tap->vhostforce)) { VhostNetOptions options; =20 + stored->has_vhost =3D true; + stored->vhost =3D true; + + if (tap->has_vhostforce && tap->vhostforce) { + stored->has_vhostforce =3D true; + stored->vhostforce =3D true; + } + options.backend_type =3D VHOST_BACKEND_TYPE_KERNEL; options.net_backend =3D &s->nc; if (tap->has_poll_us) { + stored->has_poll_us =3D true; + stored->poll_us =3D tap->poll_us; + options.busyloop_timeout =3D tap->poll_us; } else { options.busyloop_timeout =3D 0; @@ -715,6 +797,15 @@ static void net_init_tap_one(const NetdevTapOptions *t= ap, NetClientState *peer, } options.opaque =3D (void *)(uintptr_t)vhostfd; =20 + if (!stored->has_vhostfds) { + stored->has_vhostfds =3D true; + stored->vhostfds =3D g_strdup_printf("%d", vhostfd); + } else { + char *tmp_s =3D stored->vhostfds; + stored->vhostfds =3D g_strdup_printf("%s:%d", stored->fds, vho= stfd); + g_free(tmp_s); + } + s->vhost_net =3D vhost_net_init(&options); if (!s->vhost_net) { if (tap->has_vhostforce && tap->vhostforce) { @@ -766,6 +857,7 @@ int net_init_tap(const Netdev *netdev, const char *name, Error *err =3D NULL; const char *vhostfdname; char ifname[128]; + NetdevInfo *common_stored =3D NULL; /* will store configuration */ =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_TAP); tap =3D &netdev->u.tap; @@ -801,7 +893,7 @@ int net_init_tap(const Netdev *netdev, const char *name, =20 net_init_tap_one(tap, peer, "tap", name, NULL, script, downscript, - vhostfdname, vnet_hdr, fd, &err); + vhostfdname, vnet_hdr, fd, &common_stored, &err); if (err) { error_propagate(errp, err); return -1; @@ -857,7 +949,7 @@ int net_init_tap(const Netdev *netdev, const char *name, net_init_tap_one(tap, peer, "tap", name, ifname, script, downscript, tap->has_vhostfds ? vhost_fds[i] : NULL, - vnet_hdr, fd, &err); + vnet_hdr, fd, &common_stored, &err); if (err) { error_propagate(errp, err); ret =3D -1; @@ -896,7 +988,7 @@ free_fail: =20 net_init_tap_one(tap, peer, "bridge", name, ifname, script, downscript, vhostfdname, - vnet_hdr, fd, &err); + vnet_hdr, fd, &common_stored, &err); if (err) { error_propagate(errp, err); close(fd); @@ -935,7 +1027,8 @@ free_fail: net_init_tap_one(tap, peer, "tap", name, ifname, i >=3D 1 ? "no" : script, i >=3D 1 ? "no" : downscript, - vhostfdname, vnet_hdr, fd, &err); + vhostfdname, vnet_hdr, fd, + &common_stored, &err); if (err) { error_propagate(errp, err); close(fd); diff --git a/net/vde.c b/net/vde.c index 99189cccb6..c0ab2bb65c 100644 --- a/net/vde.c +++ b/net/vde.c @@ -84,6 +84,7 @@ static int net_vde_init(NetClientState *peer, const char = *model, VDECONN *vde; char *init_group =3D (char *)group; char *init_sock =3D (char *)sock; + NetdevVdeOptions *stored; =20 struct vde_open_args args =3D { .port =3D port, @@ -108,6 +109,31 @@ static int net_vde_init(NetClientState *peer, const ch= ar *model, =20 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); =20 + /* Store startup parameters */ + nc->stored_config =3D g_new0(NetdevInfo, 1); + nc->stored_config->type =3D NET_CLIENT_DRIVER_VDE; + stored =3D &nc->stored_config->u.vde; + + if (sock) { + stored->has_sock =3D true; + stored->sock =3D g_strdup(sock); + } + + if (port) { + stored->has_port =3D true; + stored->port =3D port; + } + + if (group) { + stored->has_group =3D true; + stored->group =3D g_strdup(group); + } + + if (mode) { + stored->has_mode =3D true; + stored->mode =3D mode; + } + return 0; } =20 diff --git a/net/vhost-user.c b/net/vhost-user.c index 014199d600..ce476e096a 100644 --- a/net/vhost-user.c +++ b/net/vhost-user.c @@ -302,14 +302,15 @@ static void net_vhost_user_event(void *opaque, int ev= ent) } =20 static int net_vhost_user_init(NetClientState *peer, const char *device, - const char *name, Chardev *chr, - int queues) + const char *name, const char *chardev, + Chardev *chr, int queues) { Error *err =3D NULL; NetClientState *nc, *nc0 =3D NULL; NetVhostUserState *s =3D NULL; VhostUserState *user; int i; + NetdevVhostUserOptions *stored; =20 assert(name); assert(queues > 0); @@ -346,6 +347,16 @@ static int net_vhost_user_init(NetClientState *peer, c= onst char *device, =20 assert(s->vhost_net); =20 + /* Store startup parameters */ + nc0->stored_config =3D g_new0(NetdevInfo, 1); + nc0->stored_config->type =3D NET_CLIENT_DRIVER_VHOST_USER; + stored =3D &nc0->stored_config->u.vhost_user; + + stored->chardev =3D g_strdup(chardev); + + stored->has_queues =3D true; + stored->queues =3D queues; + return 0; =20 err: @@ -437,5 +448,6 @@ int net_init_vhost_user(const Netdev *netdev, const cha= r *name, return -1; } =20 - return net_vhost_user_init(peer, "vhost_user", name, chr, queues); + return net_vhost_user_init(peer, "vhost_user", name, + vhost_user_opts->chardev, chr, queues); } diff --git a/qapi/net.json b/qapi/net.json index 335295be50..7ceb00cd8f 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -754,3 +754,88 @@ ## { 'event': 'FAILOVER_NEGOTIATED', 'data': {'device-id': 'str'} } + +## +# @NetdevInfo: +# +# Configuration of a network device. +# +# @id: Device identifier. +# +# @type: Specify the driver used for interpreting remaining arguments. +# +# @peer: Connected network device. +# +# @queues_count: Number of queues. +# +# @hub: hubid of hub, if connected to. +# +# Since: 4.2 +## +{ 'union': 'NetdevInfo', + 'base': { 'id': 'str', + 'type': 'NetClientDriver', + '*peer': 'str', + 'queues_count': 'int', + '*hub': 'int' }, + 'discriminator': 'type', + 'data': { + 'nic': 'NetLegacyNicOptions', + 'user': 'NetdevUserOptions', + 'tap': 'NetdevTapOptions', + 'l2tpv3': 'NetdevL2TPv3Options', + 'socket': 'NetdevSocketOptions', + 'vde': 'NetdevVdeOptions', + 'bridge': 'NetdevBridgeOptions', + 'hubport': 'NetdevHubPortOptions', + 'netmap': 'NetdevNetmapOptions', + 'vhost-user': 'NetdevVhostUserOptions' } } + +## +# @x-query-netdevs: +# +# Get a list of @NetdevInfo for all virtual network devices. +# +# Returns: a list of @NetdevInfo describing each virtual network device. +# +# Since: 4.2 +# +# Example: +# +# -> { "execute": "x-query-netdevs" } +# <- { "return": [ +# { +# "peer": "netdev0", +# "netdev": "netdev0", +# "model": "virtio-net-pci", +# "macaddr": "52:54:00:12:34:56", +# "queues_count": 1, +# "type": "nic", +# "id": "net0" +# }, +# { +# "peer": "net0", +# "ipv6": true, +# "ipv4": true, +# "host": "10.0.2.2", +# "queues_count": 1, +# "ipv6-dns": "fec0::3", +# "ipv6-prefix": "fec0::", +# "net": "10.0.2.0/255.255.255.0", +# "ipv6-host": "fec0::2", +# "type": "user", +# "dns": "10.0.2.3", +# "hostfwd": [ +# { +# "str": "tcp::20004-:22" +# } +# ], +# "ipv6-prefixlen": 64, +# "id": "netdev0", +# "restrict": false +# } +# ] +# } +# +## +{ 'command': 'x-query-netdevs', 'returns': ['NetdevInfo'] } --=20 2.17.1 From nobody Thu May 2 12:28:27 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1573680576; cv=none; d=zoho.com; s=zohoarc; b=NOdWDfRWHvfNdpgPNmAUNlJl3+err2VD/RHKtqDdXPvhTZwd6RHuEKRu1OBE3Yuf/WprPlsjahQw92c7nPFZneE5ai23dlyAT42EmN6XDkIoerh/4ubDS4uENC39MbsD/A15+VKRF3VXE4c+7TrTajzFrDAb8WywlaKHP6R/fEY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1573680576; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=LPUz0oDhexTDd3tx3l9GgCnARWld3cPZd5fQI7FZEvk=; b=ILcd4bZ/RjacojZbEvRqyZAGwRg6DTbuehdEMOAg7QcQ8gAmtqaib55hT8NhqWpZdfd8ugxIGYjwhn2r3a5KdDxzgZksLNM9tAojVqjIjy0vB0/+OkqMCZaR0upY08MXmqsUbbu4q6/dDAnArmpTikinb1Vahr16T1MvaYSbSvY= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1573680576135196.77368939943267; Wed, 13 Nov 2019 13:29:36 -0800 (PST) Received: from localhost ([::1]:51232 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV0Cl-00045I-1U for importer@patchew.org; Wed, 13 Nov 2019 16:29:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:58954) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iV097-0001JG-3Y for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iV095-0004QK-RF for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:49 -0500 Received: from forwardcorp1j.mail.yandex.net ([2a02:6b8:0:1619::183]:38840) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iV093-0004NT-Qa for qemu-devel@nongnu.org; Wed, 13 Nov 2019 16:25:46 -0500 Received: from mxbackcorp2j.mail.yandex.net (mxbackcorp2j.mail.yandex.net [IPv6:2a02:6b8:0:1619::119]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 826E42E0951; Thu, 14 Nov 2019 00:25:41 +0300 (MSK) Received: from vla1-5826f599457c.qloud-c.yandex.net (vla1-5826f599457c.qloud-c.yandex.net [2a02:6b8:c0d:35a1:0:640:5826:f599]) by mxbackcorp2j.mail.yandex.net (mxbackcorp/Yandex) with ESMTP id G84mh3mGGb-PcteMKMm; Thu, 14 Nov 2019 00:25:41 +0300 Received: from unknown (unknown [2a02:6b8:b080:7204::1:11]) by vla1-5826f599457c.qloud-c.yandex.net (smtpcorp/Yandex) with ESMTPSA id dbNdL4rsWi-PcV8tkM4; Thu, 14 Nov 2019 00:25:38 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1573680341; bh=LPUz0oDhexTDd3tx3l9GgCnARWld3cPZd5fQI7FZEvk=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=EiT0PY9shD+O9XzhjsJV2538aOshA+u7q8vNxc+uhnmkCmVgVWW8QP3Bq8iWVhwxf kqhLMVzrkf5gyw8G0gOzuJjYqcpzlWKkple4cl/IqP8cYYz0rH3jDxZ0/+c72o3a7Z 208zkQyfv2HnFMu8QkUjbD6fMVvDU8xv8V0JmrS4= Authentication-Results: mxbackcorp2j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Alexey Kirillov To: Jason Wang , Samuel Thibault , Markus Armbruster Subject: [PATCH 2/2] tests: Add tests for query-netdevs command Date: Thu, 14 Nov 2019 00:25:16 +0300 Message-Id: <20191113212516.13606-3-lekiravi@yandex-team.ru> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191113212516.13606-1-lekiravi@yandex-team.ru> References: <20191113212516.13606-1-lekiravi@yandex-team.ru> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a02:6b8:0:1619::183 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Michael S. Tsirkin" , Jan Kiszka , qemu-devel@nongnu.org, Vincenzo Maffione , yc-core@yandex-team.ru, Alexey Kirillov , Giuseppe Lettieri , Luigi Rizzo Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Alexey Kirillov --- tests/Makefile.include | 2 + tests/test-query-netdevs.c | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tests/test-query-netdevs.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 534ee48743..4d199e463b 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -297,6 +297,7 @@ check-qtest-s390x-y +=3D tests/migration-test$(EXESUF) check-qtest-generic-y +=3D tests/machine-none-test$(EXESUF) check-qtest-generic-y +=3D tests/qom-test$(EXESUF) check-qtest-generic-y +=3D tests/test-hmp$(EXESUF) +check-qtest-generic-y +=3D tests/test-query-netdevs$(EXESUF) =20 qapi-schema +=3D alternate-any.json qapi-schema +=3D alternate-array.json @@ -844,6 +845,7 @@ tests/numa-test$(EXESUF): tests/numa-test.o tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o tests/boot-sector.o test= s/acpi-utils.o tests/cdrom-test$(EXESUF): tests/cdrom-test.o tests/boot-sector.o $(libqos= -obj-y) tests/arm-cpu-features$(EXESUF): tests/arm-cpu-features.o +tests/test-query-netdevs$(EXESUF): tests/test-query-netdevs.o =20 tests/migration/stress$(EXESUF): tests/migration/stress.o $(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< ,"L= INK","$(TARGET_DIR)$@") diff --git a/tests/test-query-netdevs.c b/tests/test-query-netdevs.c new file mode 100644 index 0000000000..2afde36114 --- /dev/null +++ b/tests/test-query-netdevs.c @@ -0,0 +1,114 @@ +/* + * QTest testcase for the query-netdevs + * + * Copyright Yandex N.V., 2019 + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +#include "qemu/osdep.h" + +#include "libqtest.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qlist.h" + +/* + * Events can get in the way of responses we are actually waiting for. + */ +GCC_FMT_ATTR(2, 3) +static QObject *wait_command(QTestState *who, const char *command, ...) +{ + va_list ap; + QDict *response; + QObject *result; + + va_start(ap, command); + qtest_qmp_vsend(who, command, ap); + va_end(ap); + + response =3D qtest_qmp_receive(who); + + result =3D qdict_get(response, "return"); + g_assert(result); + qobject_ref(result); + qobject_unref(response); + + return result; +} + +static void qmp_query_netdevs_no_error(QTestState *qts, + size_t netdevs_count) +{ + QObject *resp; + QList *netdevs; + + resp =3D wait_command(qts, "{'execute': 'x-query-netdevs'}"); + + netdevs =3D qobject_to(QList, resp); + g_assert(netdevs); + g_assert(qlist_size(netdevs) =3D=3D netdevs_count); + + qobject_unref(resp); +} + +static void test_query_netdevs(void) +{ + const char *arch =3D qtest_get_arch(); + size_t correction =3D 0; + QObject *resp; + QTestState *state; + + if (strcmp(arch, "arm") =3D=3D 0 || + strcmp(arch, "aarch64") =3D=3D 0 || + strcmp(arch, "tricore") =3D=3D 0) { + g_test_skip("Not supported without machine type"); + return; + } + + /* Archs with default not unpluggable netdev */ + if (strcmp(arch, "cris") =3D=3D 0 || + strcmp(arch, "microblaze") =3D=3D 0 || + strcmp(arch, "microblazeel") =3D=3D 0 || + strcmp(arch, "sparc") =3D=3D 0) { + correction =3D 1; + } + + state =3D qtest_init( + "-nodefaults " + "-netdev user,id=3Dslirp0"); + g_assert(state); + + qmp_query_netdevs_no_error(state, 1 + correction); + + resp =3D wait_command(state, + "{'execute': 'netdev_add', 'arguments': {" + " 'id': 'slirp1'," + " 'type': 'user'}}"); + qobject_unref(resp); + + qmp_query_netdevs_no_error(state, 2 + correction); + + resp =3D wait_command(state, + "{'execute': 'netdev_del', 'arguments': {" + " 'id': 'slirp1'}}"); + qobject_unref(resp); + + qmp_query_netdevs_no_error(state, 1 + correction); + + qtest_quit(state); +} + +int main(int argc, char **argv) +{ + int ret =3D 0; + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/net/qapi/query_netdevs", + test_query_netdevs); + + ret =3D g_test_run(); + + return ret; +} --=20 2.17.1