From nobody Fri Oct 24 12:43:57 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1519148715000529.2153802523595; Tue, 20 Feb 2018 09:45:15 -0800 (PST) Received: from localhost ([::1]:56857 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eoByc-00062z-4y for importer@patchew.org; Tue, 20 Feb 2018 12:45:14 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46807) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eoBuE-0002Wg-By for qemu-devel@nongnu.org; Tue, 20 Feb 2018 12:40:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eoBuC-0000dm-Up for qemu-devel@nongnu.org; Tue, 20 Feb 2018 12:40:42 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:39596 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eoBuC-0000dT-PD for qemu-devel@nongnu.org; Tue, 20 Feb 2018 12:40:40 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 53FEC4023EDE for ; Tue, 20 Feb 2018 17:40:40 +0000 (UTC) Received: from thh440s.redhat.com (ovpn-116-114.ams2.redhat.com [10.36.116.114]) by smtp.corp.redhat.com (Postfix) with ESMTP id F098BAB587; Tue, 20 Feb 2018 17:40:37 +0000 (UTC) From: Thomas Huth To: qemu-devel@nongnu.org, Jason Wang , "Dr. David Alan Gilbert" Date: Tue, 20 Feb 2018 18:40:05 +0100 Message-Id: <1519148406-15006-8-git-send-email-thuth@redhat.com> In-Reply-To: <1519148406-15006-1-git-send-email-thuth@redhat.com> References: <1519148406-15006-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 20 Feb 2018 17:40:40 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Tue, 20 Feb 2018 17:40:40 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'thuth@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 7/8] net: Add a new convenience option "--nic" to configure default/on-board NICs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The legacy "-net" option can be quite confusing for the users since most people do not expect to get a "vlan" hub between their emulated guest hardware and the host backend. But so far, we are also not able to get rid of "-net" completely, since it is the only way to configure on-board NICs that can not be instantiated via "-device" yet. It's also a little bit shorter to type "-net nic -net tap" instead of "-device xyz,netdev=3Dn1 -netdev tap,id=3Dn1". So what we need is a new convenience option that is shorter to type than the full -device + -netdev stuff, and which can be used to configure the on-board NICs that can not be handled via -device yet. Thus this patch now provides such a new option "--nic": It adds an entry in the nd_table to configure a on-board / default NIC, creates a host backend and connects the two directly, without a confusing "vlan" hub inbetween. Signed-off-by: Thomas Huth Reviewed-by: Paolo Bonzini --- include/sysemu/sysemu.h | 1 + net/net.c | 78 +++++++++++++++++++++++++++++++++++++++++++++= ++++ qemu-options.hx | 40 +++++++++++++++++++++---- vl.c | 7 +++++ 4 files changed, 120 insertions(+), 6 deletions(-) diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h index 77bb3da..66f0761 100644 --- a/include/sysemu/sysemu.h +++ b/include/sysemu/sysemu.h @@ -197,6 +197,7 @@ extern QemuOptsList bdrv_runtime_opts; extern QemuOptsList qemu_chardev_opts; extern QemuOptsList qemu_device_opts; extern QemuOptsList qemu_netdev_opts; +extern QemuOptsList qemu_nic_opts; extern QemuOptsList qemu_net_opts; extern QemuOptsList qemu_global_opts; extern QemuOptsList qemu_mon_opts; diff --git a/net/net.c b/net/net.c index 2d05808..0bab269 100644 --- a/net/net.c +++ b/net/net.c @@ -1462,6 +1462,67 @@ static int net_init_netdev(void *dummy, QemuOpts *op= ts, Error **errp) return net_client_init(opts, true, errp); } =20 +/* For the convenience "--nic" parameter */ +static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp) +{ + char *mac, *nd_id; + int idx, ret; + NICInfo *ni; + const char *type; + + type =3D qemu_opt_get(opts, "type"); + if (type && g_str_equal(type, "none")) { + return 0; /* Nothing to do, default_net is cleared in vl.c */ + } + + idx =3D nic_get_free_idx(); + if (idx =3D=3D -1 || nb_nics >=3D MAX_NICS) { + error_setg(errp, "no more on-board/default NIC slots available"); + return -1; + } + + if (!type) { + qemu_opt_set(opts, "type", "user", &error_abort); + } + + ni =3D &nd_table[idx]; + memset(ni, 0, sizeof(*ni)); + ni->model =3D qemu_opt_get_del(opts, "model"); + + /* Create an ID if the user did not specify one */ + nd_id =3D g_strdup(qemu_opts_id(opts)); + if (!nd_id) { + nd_id =3D g_strdup_printf("__org.qemu.nic%i\n", idx); + qemu_opts_set_id(opts, nd_id); + } + + /* Handle MAC address */ + mac =3D qemu_opt_get_del(opts, "mac"); + if (mac) { + ret =3D net_parse_macaddr(ni->macaddr.a, mac); + g_free(mac); + if (ret) { + error_setg(errp, "invalid syntax for ethernet address"); + return -1; + } + if (is_multicast_ether_addr(ni->macaddr.a)) { + error_setg(errp, "NIC cannot have multicast MAC address"); + return -1; + } + } + qemu_macaddr_default_if_unset(&ni->macaddr); + + ret =3D net_client_init(opts, true, errp); + if (ret =3D=3D 0) { + ni->netdev =3D qemu_find_netdev(nd_id); + ni->used =3D true; + nb_nics++; + } + + g_free(nd_id); + return ret; +} + int net_init_clients(Error **errp) { net_change_state_entry =3D @@ -1474,6 +1535,10 @@ int net_init_clients(Error **errp) return -1; } =20 + if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp= )) { + return -1; + } + if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, er= rp)) { return -1; } @@ -1549,6 +1614,19 @@ QemuOptsList qemu_netdev_opts =3D { }, }; =20 +QemuOptsList qemu_nic_opts =3D { + .name =3D "nic", + .implied_opt_name =3D "type", + .head =3D QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head), + .desc =3D { + /* + * no elements =3D> accept any params + * validation will happen later + */ + { /* end of list */ } + }, +}; + QemuOptsList qemu_net_opts =3D { .name =3D "net", .implied_opt_name =3D "type", diff --git a/qemu-options.hx b/qemu-options.hx index a9249b6..399905e 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2004,13 +2004,34 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, #endif "-netdev hubport,id=3Dstr,hubid=3Dn[,netdev=3Dnd]\n" " configure a hub port on QEMU VLAN 'n'\n", QEMU_ARCH_A= LL) +DEF("nic", HAS_ARG, QEMU_OPTION_nic, + "--nic [tap|bridge|" +#ifdef CONFIG_SLIRP + "user|" +#endif +#ifdef __linux__ + "l2tpv3|" +#endif +#ifdef CONFIG_VDE + "vde|" +#endif +#ifdef CONFIG_NETMAP + "netmap|" +#endif +#ifdef CONFIG_POSIX + "vhost-user|" +#endif + "socket][,option][,...][mac=3Dmacaddr]\n" + " initialize an on-board / default host NIC (using MAC = address\n" + " macaddr) and connect it to the given host network bac= kend\n" + "--nic none use it alone to have zero network devices (the defaul= t is to\n" + " provided a 'user' network connection)\n", + QEMU_ARCH_ALL) DEF("net", HAS_ARG, QEMU_OPTION_net, "-net nic[,vlan=3Dn][,netdev=3Dnd][,macaddr=3Dmac][,model=3Dtype][,nam= e=3Dstr][,addr=3Dstr][,vectors=3Dv]\n" " configure or create an on-board (or machine default) = NIC and\n" " connect it either to VLAN 'n' or the netdev 'nd' (for= pluggable\n" " NICs please use '-device devtype,netdev=3Dnd' instead= )\n" - "-net none use it alone to have zero network devices. If no -net= option\n" - " is provided, the default is '-net nic -net user'\n" "-net [" #ifdef CONFIG_SLIRP "user|" @@ -2456,10 +2477,17 @@ qemu -m 512 -object memory-backend-file,id=3Dmem,si= ze=3D512M,mem-path=3D/hugetlbfs,sha -device virtio-net-pci,netdev=3Dnet0 @end example =20 -@item -net none -Indicate that no network devices should be configured. It is used to -override the default configuration (@option{-net nic -net user}) which -is activated if no @option{-net} options are provided. +@item --nic [tap|bridge|user|l2tpv3|vde|netmap|vhost-user|socket][,...][,m= ac=3Dmacaddr] + +This option is a shortcut for setting both, the on-board (default) guest N= IC +hardware and the host network backend in one go. The host backend options = are +the same as with the corresponding @option{--netdev} option. The guest NIC +hardware MAC address can be set with @option{mac=3D@var{macaddr}}. + +@item --nic none +Indicate that no network devices should be configured. It is used to overr= ide +the default configuration (default NIC with @option{--net user} backend) w= hich +is activated if no other networking options are provided. ETEXI =20 STEXI diff --git a/vl.c b/vl.c index 698b681..59ffadb 100644 --- a/vl.c +++ b/vl.c @@ -3078,6 +3078,7 @@ int main(int argc, char **argv, char **envp) qemu_add_opts(&qemu_chardev_opts); qemu_add_opts(&qemu_device_opts); qemu_add_opts(&qemu_netdev_opts); + qemu_add_opts(&qemu_nic_opts); qemu_add_opts(&qemu_net_opts); qemu_add_opts(&qemu_rtc_opts); qemu_add_opts(&qemu_global_opts); @@ -3298,6 +3299,12 @@ int main(int argc, char **argv, char **envp) exit(1); } break; + case QEMU_OPTION_nic: + default_net =3D 0; + if (net_client_parse(qemu_find_opts("nic"), optarg) =3D=3D= -1) { + exit(1); + } + break; case QEMU_OPTION_net: default_net =3D 0; if (net_client_parse(qemu_find_opts("net"), optarg) =3D=3D= -1) { --=20 1.8.3.1