From nobody Mon May 13 03:42:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705440608802252.72192184847347; Tue, 16 Jan 2024 13:30:08 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id C58771B57; Tue, 16 Jan 2024 16:30:07 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 7A2FB1D5C; Tue, 16 Jan 2024 16:26:17 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 646841D50; Tue, 16 Jan 2024 16:25:57 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id B0A1D1D50 for ; Tue, 16 Jan 2024 16:25:54 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 6710020DF578; Tue, 16 Jan 2024 13:25:53 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6710020DF578 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 1/5] conf: Drop unused parameter Date: Tue, 16 Jan 2024 15:25:39 -0600 Message-ID: <20240116212543.24149-2-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: 47IVR3AGR242S4M23427Y6P3M2T2FW7B X-Message-ID-Hash: 47IVR3AGR242S4M23427Y6P3M2T2FW7B X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705440610094100001 Drop unused parameter from virDomainNetReleaseActualDevice method. Signed-off-by: Praveen K Paladugu Reviewed-by: Michal Privoznik --- src/conf/domain_conf.c | 1 - src/conf/domain_conf.h | 3 +-- src/libxl/libxl_domain.c | 2 +- src/libxl/libxl_driver.c | 4 ++-- src/lxc/lxc_driver.c | 2 +- src/lxc/lxc_process.c | 4 ++-- src/qemu/qemu_hotplug.c | 10 +++++----- src/qemu/qemu_process.c | 2 +- 8 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6211d2a51b..9b42ac3e38 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -30276,7 +30276,6 @@ virDomainNetNotifyActualDevice(virConnectPtr conn, =20 int virDomainNetReleaseActualDevice(virConnectPtr conn, - virDomainDef *dom G_GNUC_UNUSED, virDomainNetDef *iface) { virNetworkPtr net =3D NULL; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index d176bda5f8..95d8fc379d 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -4406,9 +4406,8 @@ virDomainNetNotifyActualDevice(virConnectPtr conn, =20 int virDomainNetReleaseActualDevice(virConnectPtr conn, - virDomainDef *dom, virDomainNetDef *iface) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + ATTRIBUTE_NONNULL(1); =20 int virDomainNetBandwidthUpdate(virDomainNetDef *iface, diff --git a/src/libxl/libxl_domain.c b/src/libxl/libxl_domain.c index 670fd2881e..ad2ad1ce0e 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -749,7 +749,7 @@ libxlNetworkUnwindDevices(virDomainDef *def) g_autoptr(virConnect) conn =3D virGetConnectNetwork(); =20 if (conn) - virDomainNetReleaseActualDevice(conn, def, net); + virDomainNetReleaseActualDevice(conn, net); else VIR_WARN("Unable to release network device '%s'", NULL= STR(net->ifname)); } diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 6c843b9054..e42a3dc0a9 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -3440,7 +3440,7 @@ libxlDomainAttachNetDevice(libxlDriverPrivate *driver, } else { virDomainNetRemoveHostdev(vm->def, net); if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK && conn) - virDomainNetReleaseActualDevice(conn, vm->def, net); + virDomainNetReleaseActualDevice(conn, net); } virObjectUnref(cfg); virErrorRestore(&save_err); @@ -3911,7 +3911,7 @@ libxlDomainDetachNetDevice(libxlDriverPrivate *driver, if (detach->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { g_autoptr(virConnect) conn =3D virGetConnectNetwork(); if (conn) - virDomainNetReleaseActualDevice(conn, vm->def, detach); + virDomainNetReleaseActualDevice(conn, detach); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= detach->ifname)); } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index c281998652..39992bdf96 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -4110,7 +4110,7 @@ lxcDomainDetachDeviceNetLive(virDomainObj *vm, if (detach->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { g_autoptr(virConnect) conn =3D virGetConnectNetwork(); if (conn) - virDomainNetReleaseActualDevice(conn, vm->def, detach); + virDomainNetReleaseActualDevice(conn, detach); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= detach->ifname)); } diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index 20cd3a3d57..bfdcefd01b 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -221,7 +221,7 @@ static void virLXCProcessCleanup(virLXCDriver *driver, } if (iface->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (conn || (conn =3D virGetConnectNetwork())) - virDomainNetReleaseActualDevice(conn, vm->def, iface); + virDomainNetReleaseActualDevice(conn, iface); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= iface->ifname)); } @@ -643,7 +643,7 @@ virLXCProcessSetupInterfaces(virLXCDriver *driver, virDomainNetGetActualBridgeName(iface), iface->ifname)); if (iface->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK && netconn) - virDomainNetReleaseActualDevice(netconn, def, iface); + virDomainNetReleaseActualDevice(netconn, iface); } virErrorRestore(&save_err); } diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 0e45bd53e1..bf1ac241ff 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1435,7 +1435,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver, =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (conn) - virDomainNetReleaseActualDevice(conn, vm->def, net); + virDomainNetReleaseActualDevice(conn, net); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= net->ifname)); } @@ -4162,7 +4162,7 @@ qemuDomainChangeNet(virQEMUDriver *driver, * no need to change the pointer in the hostdev structure */ if (olddev->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (conn || (conn =3D virGetConnectNetwork())) - virDomainNetReleaseActualDevice(conn, vm->def, olddev); + virDomainNetReleaseActualDevice(conn, olddev); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= olddev->ifname)); } @@ -4198,7 +4198,7 @@ qemuDomainChangeNet(virQEMUDriver *driver, * replace the entire device object. */ if (newdev && newdev->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK && conn) - virDomainNetReleaseActualDevice(conn, vm->def, newdev); + virDomainNetReleaseActualDevice(conn, newdev); virErrorRestore(&save_err); =20 return ret; @@ -4778,7 +4778,7 @@ qemuDomainRemoveHostDevice(virQEMUDriver *driver, if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { g_autoptr(virConnect) conn =3D virGetConnectNetwork(); if (conn) - virDomainNetReleaseActualDevice(conn, vm->def, net); + virDomainNetReleaseActualDevice(conn, net); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= net->ifname)); } @@ -4896,7 +4896,7 @@ qemuDomainRemoveNetDevice(virQEMUDriver *driver, if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { g_autoptr(virConnect) conn =3D virGetConnectNetwork(); if (conn) - virDomainNetReleaseActualDevice(conn, vm->def, net); + virDomainNetReleaseActualDevice(conn, net); else VIR_WARN("Unable to release network device '%s'", NULLSTR(net-= >ifname)); } else if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_ETHERNET) { diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 3563ad215c..3abd3ad23a 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8579,7 +8579,7 @@ void qemuProcessStop(virQEMUDriver *driver, virDomainNetRemoveHostdev(def, net); if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (conn || (conn =3D virGetConnectNetwork())) - virDomainNetReleaseActualDevice(conn, vm->def, net); + virDomainNetReleaseActualDevice(conn, net); else VIR_WARN("Unable to release network device '%s'", NULLSTR(= net->ifname)); } --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Mon May 13 03:42:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705441076774747.7865735299159; Tue, 16 Jan 2024 13:37:56 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 8E6AD1D9E; Tue, 16 Jan 2024 16:37:55 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 3DD4F1E1A; Tue, 16 Jan 2024 16:26:22 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id D232B1D54; Tue, 16 Jan 2024 16:26:00 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id EE2AF1D5B for ; Tue, 16 Jan 2024 16:25:54 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id CB01520DF57D; Tue, 16 Jan 2024 13:25:53 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com CB01520DF57D From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 2/5] hypervisor: Move domain interface mgmt methods Date: Tue, 16 Jan 2024 15:25:40 -0600 Message-ID: <20240116212543.24149-3-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: Y24OMMPFAF4O6GVQHDM4CMRZ2AE6LQA5 X-Message-ID-Hash: Y24OMMPFAF4O6GVQHDM4CMRZ2AE6LQA5 X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705441078372100001 Move domain interface management methods from qemu to hypervisor. This refactoring allows the domain management methods to be shared between CH and qemu drivers. This commit does not introduce any functional changes. Signed-off-by: Praveen K Paladugu Reviewed-by: Michal Privoznik --- po/POTFILES | 1 + src/hypervisor/domain_interface.c | 457 ++++++++++++++++++++++++++++++ src/hypervisor/domain_interface.h | 45 +++ src/hypervisor/meson.build | 1 + src/libvirt_private.syms | 9 + src/qemu/qemu_command.c | 8 +- src/qemu/qemu_hotplug.c | 5 +- src/qemu/qemu_interface.c | 339 +--------------------- src/qemu/qemu_interface.h | 11 - src/qemu/qemu_process.c | 72 +---- 10 files changed, 533 insertions(+), 415 deletions(-) create mode 100644 src/hypervisor/domain_interface.c create mode 100644 src/hypervisor/domain_interface.h diff --git a/po/POTFILES b/po/POTFILES index 3a51aea5cb..023c041f61 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -92,6 +92,7 @@ src/hyperv/hyperv_util.c src/hyperv/hyperv_wmi.c src/hypervisor/domain_cgroup.c src/hypervisor/domain_driver.c +src/hypervisor/domain_interface.c src/hypervisor/virhostdev.c src/interface/interface_backend_netcf.c src/interface/interface_backend_udev.c diff --git a/src/hypervisor/domain_interface.c b/src/hypervisor/domain_inte= rface.c new file mode 100644 index 0000000000..853814fe78 --- /dev/null +++ b/src/hypervisor/domain_interface.c @@ -0,0 +1,457 @@ +/* + * Copyright (C) 2015-2016 Red Hat, Inc. + * Copyright IBM Corp. 2014 + * + * domain_interface.c: methods to manage guest/domain interfaces + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "datatypes.h" +#include "domain_audit.h" +#include "domain_conf.h" +#include "domain_driver.h" +#include "domain_interface.h" +#include "domain_nwfilter.h" +#include "network_conf.h" +#include "viralloc.h" +#include "virconftypes.h" +#include "virebtables.h" +#include "virfile.h" +#include "virlog.h" +#include "virmacaddr.h" +#include "virnetdevbridge.h" +#include "virnetdevmidonet.h" +#include "virnetdevopenvswitch.h" +#include "virnetdevtap.h" + + +#define VIR_FROM_THIS VIR_FROM_DOMAIN + +VIR_LOG_INIT("domain.interface"); + +bool +virDomainInterfaceIsVnetCompatModel(const virDomainNetDef *net) +{ + return (virDomainNetIsVirtioModel(net) || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_E1000E || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_IGB || + net->model =3D=3D VIR_DOMAIN_NET_MODEL_VMXNET3); +} + +/* virDomainInterfaceEthernetConnect: + * @def: the definition of the VM + * @net: a net definition in the VM + * @ebtables: ebtales context + * @macFilter: whether driver support mac Filtering + * @privileged: whether running as privileged user + * @tapfd: returned array of file descriptors for the domain interface + * @tapfdsize: returned number of file descriptors in @tapfd + * + * Called *only* called if actualType is VIR_DOMAIN_NET_TYPE_ETHERNET + * (i.e. if the connection is made with a tap device) + */ +int +virDomainInterfaceEthernetConnect(virDomainDef *def, + virDomainNetDef *net, + ebtablesContext *ebtables, + bool macFilter, + bool privileged, + int *tapfd, + size_t tapfdSize) +{ + virMacAddr tapmac; + int ret =3D -1; + unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; + bool template_ifname =3D false; + const char *tunpath =3D "/dev/net/tun"; + const char *auditdev =3D tunpath; + + if (net->backend.tap) { + tunpath =3D net->backend.tap; + if (!privileged) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("cannot use custom tap device in session mode= ")); + goto cleanup; + } + } + + if (virDomainInterfaceIsVnetCompatModel(net)) + tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; + + if (net->managed_tap =3D=3D VIR_TRISTATE_BOOL_NO) { + if (!net->ifname) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("target dev must be supplied when managed=3D'= no'")); + goto cleanup; + } + if (virNetDevExists(net->ifname) !=3D 1) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("target managed=3D'no' but specified dev does= n't exist")); + goto cleanup; + } + + tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING; + + if (virNetDevMacVLanIsMacvtap(net->ifname)) { + auditdev =3D net->ifname; + if (virNetDevMacVLanTapOpen(net->ifname, tapfd, tapfdSize) < 0) + goto cleanup; + if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, + virDomainInterfaceIsVnetCompatMod= el(net)) < 0) { + goto cleanup; + } + } else { + if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, + tap_create_flags) < 0) + goto cleanup; + } + } else { + + if (!net->ifname) + template_ifname =3D true; + + if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, + tap_create_flags) < 0) { + goto cleanup; + } + + /* The tap device's MAC address cannot match the MAC address + * used by the guest. This results in "received packet on + * vnetX with own address as source address" error logs from + * the kernel. + */ + virMacAddrSet(&tapmac, &net->mac); + if (tapmac.addr[0] =3D=3D 0xFE) + tapmac.addr[0] =3D 0xFA; + else + tapmac.addr[0] =3D 0xFE; + + if (virNetDevSetMAC(net->ifname, &tapmac) < 0) + goto cleanup; + + if (virNetDevSetOnline(net->ifname, true) < 0) + goto cleanup; + } + + if (net->script && + virNetDevRunEthernetScript(net->ifname, net->script) < 0) + goto cleanup; + + if (macFilter && + ebtablesAddForwardAllowIn(ebtables, + net->ifname, + &net->mac) < 0) + goto cleanup; + + if (net->filter && + virDomainConfNWFilterInstantiate(def->name, def->uuid, net, false)= < 0) { + goto cleanup; + } + + virDomainAuditNetDevice(def, net, auditdev, true); + + ret =3D 0; + + cleanup: + if (ret < 0) { + size_t i; + + virDomainAuditNetDevice(def, net, auditdev, false); + for (i =3D 0; i < tapfdSize && tapfd[i] >=3D 0; i++) + VIR_FORCE_CLOSE(tapfd[i]); + if (template_ifname) + VIR_FREE(net->ifname); + } + + return ret; +} + +/** + * virDomainInterfaceStartDevice: + * @net: net device to start + * + * Based upon the type of device provided, perform the appropriate + * work to completely activate the device and make it reachable from + * the rest of the network. + */ +int +virDomainInterfaceStartDevice(virDomainNetDef *net) +{ + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: + if (virDomainNetGetActualBridgeMACTableManager(net) =3D=3D + VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { + /* libvirt is managing the FDB of the bridge this device + * is attaching to, so we have turned off learning and + * unicast_flood on the device to prevent the kernel from + * adding any FDB entries for it. This means we need to + * add an fdb entry ourselves, using the MAC address from + * the interface config. + */ + if (virNetDevBridgeFDBAdd(&net->mac, net->ifname, + VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | + VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) + return -1; + } + break; + + case VIR_DOMAIN_NET_TYPE_DIRECT: { + const char *physdev =3D virDomainNetGetActualDirectDev(net); + bool isOnline =3D true; + + /* set the physdev online if necessary. It may already be up, + * in which case we shouldn't re-up it just in case that causes + * some sort of "blip" in the physdev's status. + */ + if (physdev && virNetDevGetOnline(physdev, &isOnline) < 0) + return -1; + if (!isOnline && virNetDevSetOnline(physdev, true) < 0) + return -1; + + /* macvtap devices share their MAC address with the guest + * domain, and if they are set online prior to the domain CPUs + * being started, the host may send out traffic from this + * device that could confuse other entities on the network (in + * particular, if this new domain is the destination of a + * migration, and the source domain is still running, another + * host may mistakenly direct traffic for the guest to the + * destination domain rather than source domain). To prevent + * this, we create the macvtap device with IFF_UP false + * (i.e. "offline") then wait to bring it online until just as + * we are starting the domain CPUs. + */ + if (virNetDevSetOnline(net->ifname, true) < 0) + return -1; + break; + } + + case VIR_DOMAIN_NET_TYPE_ETHERNET: + if (virNetDevIPInfoAddToDev(net->ifname, &net->hostIP) < 0) + return -1; + + break; + + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + /* these types all require no action */ + break; + } + + return 0; +} + +/** + * virDomainInterfaceStartDevices: + * @def: domain definition + * + * Set all ifaces associated with this domain to the online state. + */ +int +virDomainInterfaceStartDevices(virDomainDef *def) +{ + size_t i; + + for (i =3D 0; i < def->nnets; i++) { + if (virDomainInterfaceStartDevice(def->nets[i]) < 0) + return -1; + } + return 0; +} + +/** + * virDomainInterfaceStopDevice: + * @net: net device to stop + * + * Based upon the type of device provided, perform the appropriate + * work to deactivate the device so that packets aren't forwarded to + * it from the rest of the network. + */ +int +virDomainInterfaceStopDevice(virDomainNetDef *net) +{ + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: + if (virDomainNetGetActualBridgeMACTableManager(net) =3D=3D + VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { + /* remove the FDB entries that were added during + * virDomainInterfaceStartDevices() + */ + if (virNetDevBridgeFDBDel(&net->mac, net->ifname, + VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | + VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) + return -1; + } + break; + + case VIR_DOMAIN_NET_TYPE_DIRECT: { + const char *physdev =3D virDomainNetGetActualDirectDev(net); + + /* macvtap interfaces need to be marked !IFF_UP (ie "down") to + * prevent any host-generated traffic sent from this interface + * from putting bad info into the arp caches of other machines + * on this network. + */ + if (virNetDevSetOnline(net->ifname, false) < 0) + return -1; + + /* also mark the physdev down for passthrough macvtap, as the + * physdev has the same MAC address as the macvtap device. + */ + if (virDomainNetGetActualDirectMode(net) =3D=3D + VIR_NETDEV_MACVLAN_MODE_PASSTHRU && + physdev && virNetDevSetOnline(physdev, false) < 0) + return -1; + break; + } + + case VIR_DOMAIN_NET_TYPE_ETHERNET: + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + /* these types all require no action */ + break; + } + + return 0; +} + +/** + * virDomainInterfaceStopDevices: + * @def: domain definition + * + * Make all interfaces associated with this domain inaccessible from + * the rest of the network. + */ +int +virDomainInterfaceStopDevices(virDomainDef *def) +{ + size_t i; + + for (i =3D 0; i < def->nnets; i++) { + if (virDomainInterfaceStopDevice(def->nets[i]) < 0) + return -1; + } + return 0; +} + +/** + * virDomainInterfaceDeleteDevice: + * @def: domain definition + * @net: a net definition in the VM + * @priv_net_created: whether private network created + * @stateDir: driver's stateDir + * + * Disconnect and delete domain interfaces. + */ + +void +virDomainInterfaceDeleteDevice(virDomainDef *def, + virDomainNetDef *net, + bool priv_net_created, + char *stateDir) +{ + const virNetDevVPortProfile *vport =3D NULL; + g_autoptr(virConnect) conn =3D NULL; + + vport =3D virDomainNetGetActualVirtPortProfile(net); + switch (virDomainNetGetActualType(net)) { + case VIR_DOMAIN_NET_TYPE_DIRECT: + if (priv_net_created) { + virNetDevMacVLanDeleteWithVPortProfile(net->ifname, &net->mac, + virDomainNetGetActualDi= rectDev(net), + virDomainNetGetActualDi= rectMode(net), + virDomainNetGetActualVi= rtPortProfile(net), + stateDir); + } + break; + case VIR_DOMAIN_NET_TYPE_ETHERNET: + if (net->managed_tap !=3D VIR_TRISTATE_BOOL_NO && net->ifname) { + ignore_value(virNetDevTapDelete(net->ifname, net->backend.tap)= ); + VIR_FREE(net->ifname); + } + break; + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: +#ifdef VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP + if (!(vport && vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE= _OPENVSWITCH)) + ignore_value(virNetDevTapDelete(net->ifname, net->backend.tap)= ); +#endif + break; + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + /* No special cleanup procedure for these types. */ + break; + } + /* release the physical device (or any other resources used by + * this interface in the network driver + */ + if (vport) { + if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE_MIDONET) { + ignore_value(virNetDevMidonetUnbindPort(vport)); + } else if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE_OPE= NVSWITCH) { + ignore_value(virNetDevOpenvswitchRemovePort( + virDomainNetGetActualBridgeName(net), + net->ifname)); + } + } + + /* kick the device out of the hostdev list too */ + virDomainNetRemoveHostdev(def, net); + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { + if (conn || (conn =3D virGetConnectNetwork())) + virDomainNetReleaseActualDevice(conn, net); + else + VIR_WARN("Unable to release network device '%s'", NULLSTR(net-= >ifname)); + } + +} diff --git a/src/hypervisor/domain_interface.h b/src/hypervisor/domain_inte= rface.h new file mode 100644 index 0000000000..3d15e000cc --- /dev/null +++ b/src/hypervisor/domain_interface.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015-2016 Red Hat, Inc. + * Copyright IBM Corp. 2014 + * + * domain_interface.h: methods to manage guest/domain interfaces + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "virebtables.h" + +int +virDomainInterfaceEthernetConnect(virDomainDef *def, + virDomainNetDef *net, + ebtablesContext *ebtables, + bool macFilter, + bool privileged, + int *tapfd, + size_t tapfdSize); + +bool +virDomainInterfaceIsVnetCompatModel(const virDomainNetDef *net); + +int virDomainInterfaceStartDevice(virDomainNetDef *net); +int virDomainInterfaceStartDevices(virDomainDef *def); +int virDomainInterfaceStopDevice(virDomainNetDef *net); +int virDomainInterfaceStopDevices(virDomainDef *def); +void virDomainInterfaceDeleteDevice(virDomainDef *def, + virDomainNetDef *net, + bool priv_net_created, + char *stateDir); diff --git a/src/hypervisor/meson.build b/src/hypervisor/meson.build index f35565b16b..819a9a82a2 100644 --- a/src/hypervisor/meson.build +++ b/src/hypervisor/meson.build @@ -1,6 +1,7 @@ hypervisor_sources =3D [ 'domain_cgroup.c', 'domain_driver.c', + 'domain_interface.c', 'virclosecallbacks.c', 'virhostdev.c', ] diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index fc26109029..80d43ed15d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1631,6 +1631,15 @@ virDomainDriverNodeDeviceReset; virDomainDriverParseBlkioDeviceStr; virDomainDriverSetupPersistentDefBlkioParams; =20 +# hypervisor/domain_interface.h +virDomainInterfaceDeleteDevice; +virDomainInterfaceEthernetConnect; +virDomainInterfaceIsVnetCompatModel; +virDomainInterfaceStartDevice; +virDomainInterfaceStartDevices; +virDomainInterfaceStopDevice; +virDomainInterfaceStopDevices; + =20 # hypervisor/virclosecallbacks.h virCloseCallbacksDomainAdd; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 712feb7b81..9289ba5595 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -43,6 +43,7 @@ #include "domain_nwfilter.h" #include "domain_addr.h" #include "domain_conf.h" +#include "domain_interface.h" #include "netdev_bandwidth_conf.h" #include "virnetdevopenvswitch.h" #include "device_conf.h" @@ -8624,8 +8625,11 @@ qemuBuildInterfaceConnect(virDomainObj *vm, break; =20 case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (qemuInterfaceEthernetConnect(vm->def, priv->driver, net, - tapfd, tapfdSize) < 0) + if (virDomainInterfaceEthernetConnect(vm->def, net, + priv->driver->ebtables, + priv->driver->config->macFil= ter, + priv->driver->privileged, + tapfd, tapfdSize) < 0) return -1; vhostfd =3D true; break; diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index bf1ac241ff..4341ad18b5 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -39,6 +39,7 @@ #include "qemu_virtiofs.h" #include "domain_audit.h" #include "domain_cgroup.h" +#include "domain_interface.h" #include "netdev_bandwidth_conf.h" #include "domain_nwfilter.h" #include "virlog.h" @@ -1278,7 +1279,7 @@ qemuDomainAttachNetDevice(virQEMUDriver *driver, } =20 /* Set device online immediately */ - if (qemuInterfaceStartDevice(net) < 0) + if (virDomainInterfaceStartDevice(net) < 0) goto cleanup; =20 qemuDomainInterfaceSetDefaultQDisc(driver, net); @@ -4829,7 +4830,7 @@ qemuDomainRemoveNetDevice(virQEMUDriver *driver, * affect the parent device (e.g. macvtap passthrough mode sets * the parent device offline) */ - ignore_value(qemuInterfaceStopDevice(net)); + ignore_value(virDomainInterfaceStopDevice(net)); =20 qemuDomainObjEnterMonitor(vm); if (qemuMonitorRemoveNetdev(priv->mon, hostnet_name) < 0) { diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c index 8856bb95a8..c2007c7043 100644 --- a/src/qemu/qemu_interface.c +++ b/src/qemu/qemu_interface.c @@ -24,6 +24,7 @@ #include "network_conf.h" #include "domain_audit.h" #include "domain_nwfilter.h" +#include "domain_interface.h" #include "qemu_interface.h" #include "viralloc.h" #include "virlog.h" @@ -41,211 +42,6 @@ =20 VIR_LOG_INIT("qemu.qemu_interface"); =20 -/** - * qemuInterfaceStartDevice: - * @net: net device to start - * - * Based upon the type of device provided, perform the appropriate - * work to completely activate the device and make it reachable from - * the rest of the network. - */ -int -qemuInterfaceStartDevice(virDomainNetDef *net) -{ - virDomainNetType actualType =3D virDomainNetGetActualType(net); - - switch (actualType) { - case VIR_DOMAIN_NET_TYPE_BRIDGE: - case VIR_DOMAIN_NET_TYPE_NETWORK: - if (virDomainNetGetActualBridgeMACTableManager(net) - =3D=3D VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { - /* libvirt is managing the FDB of the bridge this device - * is attaching to, so we have turned off learning and - * unicast_flood on the device to prevent the kernel from - * adding any FDB entries for it. This means we need to - * add an fdb entry ourselves, using the MAC address from - * the interface config. - */ - if (virNetDevBridgeFDBAdd(&net->mac, net->ifname, - VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | - VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) - return -1; - } - break; - - case VIR_DOMAIN_NET_TYPE_DIRECT: { - const char *physdev =3D virDomainNetGetActualDirectDev(net); - bool isOnline =3D true; - - /* set the physdev online if necessary. It may already be up, - * in which case we shouldn't re-up it just in case that causes - * some sort of "blip" in the physdev's status. - */ - if (physdev && virNetDevGetOnline(physdev, &isOnline) < 0) - return -1; - if (!isOnline && virNetDevSetOnline(physdev, true) < 0) - return -1; - - /* macvtap devices share their MAC address with the guest - * domain, and if they are set online prior to the domain CPUs - * being started, the host may send out traffic from this - * device that could confuse other entities on the network (in - * particular, if this new domain is the destination of a - * migration, and the source domain is still running, another - * host may mistakenly direct traffic for the guest to the - * destination domain rather than source domain). To prevent - * this, we create the macvtap device with IFF_UP false - * (i.e. "offline") then wait to bring it online until just as - * we are starting the domain CPUs. - */ - if (virNetDevSetOnline(net->ifname, true) < 0) - return -1; - break; - } - - case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (virNetDevIPInfoAddToDev(net->ifname, &net->hostIP) < 0) - return -1; - - break; - - case VIR_DOMAIN_NET_TYPE_USER: - case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - case VIR_DOMAIN_NET_TYPE_SERVER: - case VIR_DOMAIN_NET_TYPE_CLIENT: - case VIR_DOMAIN_NET_TYPE_MCAST: - case VIR_DOMAIN_NET_TYPE_UDP: - case VIR_DOMAIN_NET_TYPE_INTERNAL: - case VIR_DOMAIN_NET_TYPE_HOSTDEV: - case VIR_DOMAIN_NET_TYPE_VDPA: - case VIR_DOMAIN_NET_TYPE_NULL: - case VIR_DOMAIN_NET_TYPE_VDS: - case VIR_DOMAIN_NET_TYPE_LAST: - /* these types all require no action */ - break; - } - - return 0; -} - -/** - * qemuInterfaceStartDevices: - * @def: domain definition - * - * Set all ifaces associated with this domain to the online state. - */ -int -qemuInterfaceStartDevices(virDomainDef *def) -{ - size_t i; - - for (i =3D 0; i < def->nnets; i++) { - if (qemuInterfaceStartDevice(def->nets[i]) < 0) - return -1; - } - return 0; -} - - -/** - * qemuInterfaceStopDevice: - * @net: net device to stop - * - * Based upon the type of device provided, perform the appropriate - * work to deactivate the device so that packets aren't forwarded to - * it from the rest of the network. - */ -int -qemuInterfaceStopDevice(virDomainNetDef *net) -{ - virDomainNetType actualType =3D virDomainNetGetActualType(net); - - switch (actualType) { - case VIR_DOMAIN_NET_TYPE_BRIDGE: - case VIR_DOMAIN_NET_TYPE_NETWORK: - if (virDomainNetGetActualBridgeMACTableManager(net) - =3D=3D VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { - /* remove the FDB entries that were added during - * qemuInterfaceStartDevices() - */ - if (virNetDevBridgeFDBDel(&net->mac, net->ifname, - VIR_NETDEVBRIDGE_FDB_FLAG_MASTER | - VIR_NETDEVBRIDGE_FDB_FLAG_TEMP) < 0) - return -1; - } - break; - - case VIR_DOMAIN_NET_TYPE_DIRECT: { - const char *physdev =3D virDomainNetGetActualDirectDev(net); - - /* macvtap interfaces need to be marked !IFF_UP (ie "down") to - * prevent any host-generated traffic sent from this interface - * from putting bad info into the arp caches of other machines - * on this network. - */ - if (virNetDevSetOnline(net->ifname, false) < 0) - return -1; - - /* also mark the physdev down for passthrough macvtap, as the - * physdev has the same MAC address as the macvtap device. - */ - if (virDomainNetGetActualDirectMode(net) =3D=3D - VIR_NETDEV_MACVLAN_MODE_PASSTHRU && - physdev && virNetDevSetOnline(physdev, false) < 0) - return -1; - break; - } - - case VIR_DOMAIN_NET_TYPE_ETHERNET: - case VIR_DOMAIN_NET_TYPE_USER: - case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - case VIR_DOMAIN_NET_TYPE_SERVER: - case VIR_DOMAIN_NET_TYPE_CLIENT: - case VIR_DOMAIN_NET_TYPE_MCAST: - case VIR_DOMAIN_NET_TYPE_UDP: - case VIR_DOMAIN_NET_TYPE_INTERNAL: - case VIR_DOMAIN_NET_TYPE_HOSTDEV: - case VIR_DOMAIN_NET_TYPE_VDPA: - case VIR_DOMAIN_NET_TYPE_NULL: - case VIR_DOMAIN_NET_TYPE_VDS: - case VIR_DOMAIN_NET_TYPE_LAST: - /* these types all require no action */ - break; - } - - return 0; -} - -/** - * qemuInterfaceStopDevices: - * @def: domain definition - * - * Make all interfaces associated with this domain inaccessible from - * the rest of the network. - */ -int -qemuInterfaceStopDevices(virDomainDef *def) -{ - size_t i; - - for (i =3D 0; i < def->nnets; i++) { - if (qemuInterfaceStopDevice(def->nets[i]) < 0) - return -1; - } - return 0; -} - - -static bool -qemuInterfaceIsVnetCompatModel(const virDomainNetDef *net) -{ - return (virDomainNetIsVirtioModel(net) || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_E1000E || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_IGB || - net->model =3D=3D VIR_DOMAIN_NET_MODEL_VMXNET3); -} - - /** * qemuInterfaceDirectConnect: * @def: the definition of the VM (needed by 802.1Qbh and audit) @@ -271,7 +67,7 @@ qemuInterfaceDirectConnect(virDomainDef *def, unsigned int macvlan_create_flags =3D VIR_NETDEV_MACVLAN_CREATE_WITH_T= AP; qemuDomainNetworkPrivate *netpriv =3D QEMU_DOMAIN_NETWORK_PRIVATE(net); =20 - if (qemuInterfaceIsVnetCompatModel(net)) + if (virDomainInterfaceIsVnetCompatModel(net)) macvlan_create_flags |=3D VIR_NETDEV_MACVLAN_VNET_HDR; =20 if (virNetDevMacVLanCreateWithVPortProfile(net->ifname, @@ -409,133 +205,6 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig = *cfg, return *tapfd < 0 ? -1 : 0; } =20 - -/* qemuInterfaceEthernetConnect: - * @def: the definition of the VM - * @driver: qemu driver data - * @net: pointer to the VM's interface description - * @tapfd: array of file descriptor return value for the new device - * @tapfdsize: number of file descriptors in @tapfd - * - * Called *only* called if actualType is VIR_DOMAIN_NET_TYPE_ETHERNET - * (i.e. if the connection is made with a tap device) - */ -int -qemuInterfaceEthernetConnect(virDomainDef *def, - virQEMUDriver *driver, - virDomainNetDef *net, - int *tapfd, - size_t tapfdSize) -{ - virMacAddr tapmac; - int ret =3D -1; - unsigned int tap_create_flags =3D VIR_NETDEV_TAP_CREATE_IFUP; - bool template_ifname =3D false; - g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); - const char *tunpath =3D "/dev/net/tun"; - const char *auditdev =3D tunpath; - - if (net->backend.tap) { - tunpath =3D net->backend.tap; - if (!driver->privileged) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("cannot use custom tap device in session mode= ")); - goto cleanup; - } - } - - if (qemuInterfaceIsVnetCompatModel(net)) - tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; - - if (net->managed_tap =3D=3D VIR_TRISTATE_BOOL_NO) { - if (!net->ifname) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("target dev must be supplied when managed=3D'= no'")); - goto cleanup; - } - if (virNetDevExists(net->ifname) !=3D 1) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("target managed=3D'no' but specified dev does= n't exist")); - goto cleanup; - } - - tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_ALLOW_EXISTING; - - if (virNetDevMacVLanIsMacvtap(net->ifname)) { - auditdev =3D net->ifname; - if (virNetDevMacVLanTapOpen(net->ifname, tapfd, tapfdSize) < 0) - goto cleanup; - if (virNetDevMacVLanTapSetup(tapfd, tapfdSize, - qemuInterfaceIsVnetCompatModel(ne= t)) < 0) { - goto cleanup; - } - } else { - if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, - tap_create_flags) < 0) - goto cleanup; - } - } else { - - if (!net->ifname) - template_ifname =3D true; - - if (virNetDevTapCreate(&net->ifname, tunpath, tapfd, tapfdSize, - tap_create_flags) < 0) { - goto cleanup; - } - - /* The tap device's MAC address cannot match the MAC address - * used by the guest. This results in "received packet on - * vnetX with own address as source address" error logs from - * the kernel. - */ - virMacAddrSet(&tapmac, &net->mac); - if (tapmac.addr[0] =3D=3D 0xFE) - tapmac.addr[0] =3D 0xFA; - else - tapmac.addr[0] =3D 0xFE; - - if (virNetDevSetMAC(net->ifname, &tapmac) < 0) - goto cleanup; - - if (virNetDevSetOnline(net->ifname, true) < 0) - goto cleanup; - } - - if (net->script && - virNetDevRunEthernetScript(net->ifname, net->script) < 0) - goto cleanup; - - if (cfg->macFilter && - ebtablesAddForwardAllowIn(driver->ebtables, - net->ifname, - &net->mac) < 0) - goto cleanup; - - if (net->filter && - virDomainConfNWFilterInstantiate(def->name, def->uuid, net, false)= < 0) { - goto cleanup; - } - - virDomainAuditNetDevice(def, net, auditdev, true); - - ret =3D 0; - - cleanup: - if (ret < 0) { - size_t i; - - virDomainAuditNetDevice(def, net, auditdev, false); - for (i =3D 0; i < tapfdSize && tapfd[i] >=3D 0; i++) - VIR_FORCE_CLOSE(tapfd[i]); - if (template_ifname) - VIR_FREE(net->ifname); - } - - return ret; -} - - /* qemuInterfaceBridgeConnect: * @def: the definition of the VM * @driver: qemu driver data @@ -578,7 +247,7 @@ qemuInterfaceBridgeConnect(virDomainDef *def, if (!net->ifname) template_ifname =3D true; =20 - if (qemuInterfaceIsVnetCompatModel(net)) + if (virDomainInterfaceIsVnetCompatModel(net)) tap_create_flags |=3D VIR_NETDEV_TAP_CREATE_VNET_HDR; =20 if (driver->privileged) { @@ -598,7 +267,7 @@ qemuInterfaceBridgeConnect(virDomainDef *def, * is attaching to, so we need to turn off learning and * unicast_flood on the device to prevent the kernel from * adding any FDB entries for it. We will add an fdb - * entry ourselves (during qemuInterfaceStartDevices(), + * entry ourselves (during virDomainInterfaceStartDevices(), * using the MAC address from the interface config. */ if (virNetDevBridgePortSetLearning(brname, net->ifname, false)= < 0) diff --git a/src/qemu/qemu_interface.h b/src/qemu/qemu_interface.h index 67cbada36e..aee5f9efb0 100644 --- a/src/qemu/qemu_interface.h +++ b/src/qemu/qemu_interface.h @@ -25,11 +25,6 @@ #include "qemu_domain.h" #include "qemu_slirp.h" =20 -int qemuInterfaceStartDevice(virDomainNetDef *net); -int qemuInterfaceStartDevices(virDomainDef *def); -int qemuInterfaceStopDevice(virDomainNetDef *net); -int qemuInterfaceStopDevices(virDomainDef *def); - int qemuInterfaceDirectConnect(virDomainDef *def, virQEMUDriver *driver, virDomainNetDef *net, @@ -37,12 +32,6 @@ int qemuInterfaceDirectConnect(virDomainDef *def, size_t tapfdSize, virNetDevVPortProfileOp vmop); =20 -int qemuInterfaceEthernetConnect(virDomainDef *def, - virQEMUDriver *driver, - virDomainNetDef *net, - int *tapfd, - size_t tapfdSize); - int qemuInterfaceBridgeConnect(virDomainDef *def, virQEMUDriver *driver, virDomainNetDef *net, diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 3abd3ad23a..a8e3c4b004 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -74,6 +74,7 @@ #include "virhostcpu.h" #include "domain_audit.h" #include "domain_cgroup.h" +#include "domain_interface.h" #include "domain_nwfilter.h" #include "domain_postparse.h" #include "domain_validate.h" @@ -3120,7 +3121,7 @@ qemuProcessStartCPUs(virQEMUDriver *driver, virDomain= Obj *vm, g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); =20 /* Bring up netdevs before starting CPUs */ - if (qemuInterfaceStartDevices(vm->def) < 0) + if (virDomainInterfaceStartDevices(vm->def) < 0) return -1; =20 VIR_DEBUG("Using lock state '%s'", NULLSTR(priv->lockState)); @@ -3183,7 +3184,7 @@ int qemuProcessStopCPUs(virQEMUDriver *driver, goto cleanup; =20 /* de-activate netdevs after stopping CPUs */ - ignore_value(qemuInterfaceStopDevices(vm->def)); + ignore_value(virDomainInterfaceStopDevices(vm->def)); =20 if (vm->job->current) ignore_value(virTimeMillisNow(&vm->job->current->stopped)); @@ -8377,11 +8378,9 @@ void qemuProcessStop(virQEMUDriver *driver, qemuDomainObjPrivate *priv =3D vm->privateData; virErrorPtr orig_err; virDomainDef *def =3D vm->def; - const virNetDevVPortProfile *vport =3D NULL; size_t i; g_autofree char *timestamp =3D NULL; g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); - g_autoptr(virConnect) conn =3D NULL; bool outgoingMigration; =20 VIR_DEBUG("Shutting down vm=3D%p name=3D%s id=3D%d pid=3D%lld, " @@ -8520,69 +8519,12 @@ void qemuProcessStop(virQEMUDriver *driver, } =20 qemuHostdevReAttachDomainDevices(driver, vm->def); - for (i =3D 0; i < def->nnets; i++) { virDomainNetDef *net =3D def->nets[i]; - vport =3D virDomainNetGetActualVirtPortProfile(net); - switch (virDomainNetGetActualType(net)) { - case VIR_DOMAIN_NET_TYPE_DIRECT: - if (QEMU_DOMAIN_NETWORK_PRIVATE(net)->created) { - virNetDevMacVLanDeleteWithVPortProfile(net->ifname, &net->= mac, - virDomainNetGetActu= alDirectDev(net), - virDomainNetGetActu= alDirectMode(net), - virDomainNetGetActu= alVirtPortProfile(net), - cfg->stateDir); - } - break; - case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (net->managed_tap !=3D VIR_TRISTATE_BOOL_NO && net->ifname)= { - ignore_value(virNetDevTapDelete(net->ifname, net->backend.= tap)); - VIR_FREE(net->ifname); - } - break; - case VIR_DOMAIN_NET_TYPE_BRIDGE: - case VIR_DOMAIN_NET_TYPE_NETWORK: -#ifdef VIR_NETDEV_TAP_REQUIRE_MANUAL_CLEANUP - if (!(vport && vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PRO= FILE_OPENVSWITCH)) - ignore_value(virNetDevTapDelete(net->ifname, net->backend.= tap)); -#endif - break; - case VIR_DOMAIN_NET_TYPE_USER: - case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - case VIR_DOMAIN_NET_TYPE_SERVER: - case VIR_DOMAIN_NET_TYPE_CLIENT: - case VIR_DOMAIN_NET_TYPE_MCAST: - case VIR_DOMAIN_NET_TYPE_INTERNAL: - case VIR_DOMAIN_NET_TYPE_HOSTDEV: - case VIR_DOMAIN_NET_TYPE_UDP: - case VIR_DOMAIN_NET_TYPE_VDPA: - case VIR_DOMAIN_NET_TYPE_NULL: - case VIR_DOMAIN_NET_TYPE_VDS: - case VIR_DOMAIN_NET_TYPE_LAST: - /* No special cleanup procedure for these types. */ - break; - } - /* release the physical device (or any other resources used by - * this interface in the network driver - */ - if (vport) { - if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE_MIDONE= T) { - ignore_value(virNetDevMidonetUnbindPort(vport)); - } else if (vport->virtPortType =3D=3D VIR_NETDEV_VPORT_PROFILE= _OPENVSWITCH) { - ignore_value(virNetDevOpenvswitchRemovePort( - virDomainNetGetActualBridgeName(net), - net->ifname)); - } - } - - /* kick the device out of the hostdev list too */ - virDomainNetRemoveHostdev(def, net); - if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { - if (conn || (conn =3D virGetConnectNetwork())) - virDomainNetReleaseActualDevice(conn, net); - else - VIR_WARN("Unable to release network device '%s'", NULLSTR(= net->ifname)); - } + virDomainInterfaceDeleteDevice(def, + net, + QEMU_DOMAIN_NETWORK_PRIVATE(net)->c= reated, + cfg->stateDir); } =20 retry: --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Mon May 13 03:42:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705440736309470.2942953640661; Tue, 16 Jan 2024 13:32:16 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 39D201E25; Tue, 16 Jan 2024 16:32:15 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 92EA41DE3; Tue, 16 Jan 2024 16:26:19 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 817231D54; Tue, 16 Jan 2024 16:25:58 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 613AA1D6A for ; Tue, 16 Jan 2024 16:25:55 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 46BF120DF584; Tue, 16 Jan 2024 13:25:54 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 46BF120DF584 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 3/5] util: Add util methods required by ch networking Date: Tue, 16 Jan 2024 15:25:41 -0600 Message-ID: <20240116212543.24149-4-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: JCCHVAB7CGK2E4ZJO5C5R3MUL7HJVVAC X-Message-ID-Hash: JCCHVAB7CGK2E4ZJO5C5R3MUL7HJVVAC X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705440736750100001 virSocketSendMsgWithFDs method send fds along with payload using SCM_RIGHTS. virSocketRecv method polls, receives and sends the response to callers. These methods are required to add network suppport in ch driver. Signed-off-by: Praveen K Paladugu Reviewed-by: Michal Privoznik --- po/POTFILES | 1 + src/libvirt_private.syms | 2 + src/util/virsocket.c | 116 +++++++++++++++++++++++++++++++++++++++ src/util/virsocket.h | 3 + 4 files changed, 122 insertions(+) diff --git a/po/POTFILES b/po/POTFILES index 023c041f61..b594a8dd39 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -326,6 +326,7 @@ src/util/virscsi.c src/util/virscsihost.c src/util/virscsivhost.c src/util/virsecret.c +src/util/virsocket.c src/util/virsocketaddr.c src/util/virstoragefile.c src/util/virstring.c diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 80d43ed15d..2c9b2635cd 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3371,8 +3371,10 @@ virSecureEraseString; =20 =20 # util/virsocket.h +virSocketRecv; virSocketRecvFD; virSocketSendFD; +virSocketSendMsgWithFDs; =20 =20 # util/virsocketaddr.h diff --git a/src/util/virsocket.c b/src/util/virsocket.c index cd6f7ecd1b..8019fd1199 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -19,11 +19,18 @@ =20 #include =20 +#include "virerror.h" #include "virsocket.h" #include "virutil.h" #include "virfile.h" +#include "virlog.h" =20 #include +#include + +#define PKT_TIMEOUT_MS 500 /* ms */ + +#define VIR_FROM_THIS VIR_FROM_NONE =20 #ifdef WIN32 =20 @@ -482,6 +489,108 @@ virSocketRecvFD(int sock, int fdflags) =20 return fd; } + +/** + * virSocketSendMsgWithFDs: + * @sock: socket to send payload and fds to + * @payload: payload to send + * @fds: array of fds to send + * @fds_len: len of fds array + + * Send @fds along with @payload to @sock using SCM_RIGHTS. + * Return number of bytes sent on success. + * On error, set errno and return -1. + */ +int +virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fd= s_len) +{ + struct msghdr msg; + struct iovec iov[1]; /* Send a single payload, so set vector len to 1 = */ + int ret; + char control[CMSG_SPACE(sizeof(int)*fds_len)]; + struct cmsghdr *cmsg; + + memset(&msg, 0, sizeof(msg)); + memset(control, 0, sizeof(control)); + + iov[0].iov_base =3D (void *) payload; + iov[0].iov_len =3D strlen(payload); + + msg.msg_iov =3D iov; + msg.msg_iovlen =3D 1; + + msg.msg_control =3D control; + msg.msg_controllen =3D sizeof(control); + + cmsg =3D CMSG_FIRSTHDR(&msg); + /* check to eliminate "potential null pointer dereference" errors duri= ng build */ + if (!cmsg) { + virReportSystemError(EFAULT, "%s", _("Couldn't fit control msg hea= der in msg") ); + return -1; + } + + cmsg->cmsg_len =3D CMSG_LEN(sizeof(int) * fds_len); + cmsg->cmsg_level =3D SOL_SOCKET; + cmsg->cmsg_type =3D SCM_RIGHTS; + memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * fds_len); + + do { + ret =3D sendmsg(sock, &msg, 0); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) { + virReportSystemError(errno, "%s", _("sendmsg failed") ); + return -1; + } + + return ret; +} + +/** + * virSocketRecv: + * @sock: socket to poll and receive response on + * + * This function polls @sock for response + * Returns received response or NULL on error. + */ +char * +virSocketRecv(int sock) +{ + struct pollfd pfds[1]; + char *buf =3D NULL; + size_t buf_len =3D 1024; + int ret; + + buf =3D g_new0(char, buf_len); + + pfds[0].fd =3D sock; + pfds[0].events =3D POLLIN; + + do { + ret =3D poll(pfds, G_N_ELEMENTS(pfds), PKT_TIMEOUT_MS); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret <=3D 0) { + if (ret < 0) { + virReportSystemError(errno, _("Poll on sock %1$d failed"), soc= k); + } else if (ret =3D=3D 0) { + virReportSystemError(errno, _("Poll on sock %1$d timed out"), = sock); + } + return NULL; + } + + do { + ret =3D recv(sock, buf, buf_len-1, 0); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) { + virReportSystemError(errno, _("recv on sock %1$d failed"), sock); + return NULL; + } + + return g_steal_pointer(&buf); +} + #else /* WIN32 */ int virSocketSendFD(int sock G_GNUC_UNUSED, int fd G_GNUC_UNUSED) @@ -496,4 +605,11 @@ virSocketRecvFD(int sock G_GNUC_UNUSED, int fdflags G_= GNUC_UNUSED) errno =3D ENOSYS; return -1; } + +int +virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fd= s_len) +{ + errno =3D ENOSYS; + return -1; +} #endif /* WIN32 */ diff --git a/src/util/virsocket.h b/src/util/virsocket.h index 419da8b3ae..00fbf52603 100644 --- a/src/util/virsocket.h +++ b/src/util/virsocket.h @@ -22,6 +22,9 @@ =20 int virSocketSendFD(int sock, int fd); int virSocketRecvFD(int sock, int fdflags); +int virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, + size_t fd_len); +char * virSocketRecv(int sock); =20 #ifdef WIN32 =20 --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Mon May 13 03:42:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705441442578737.6387245104872; Tue, 16 Jan 2024 13:44:02 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 782041B83; Tue, 16 Jan 2024 16:44:01 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 080D31E4A; Tue, 16 Jan 2024 16:26:34 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 3F6381D50; Tue, 16 Jan 2024 16:26:04 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id A05361D5C for ; Tue, 16 Jan 2024 16:25:55 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id A58BD20DF587; Tue, 16 Jan 2024 13:25:54 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A58BD20DF587 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 4/5] ch: Introduce version based cap for network support Date: Tue, 16 Jan 2024 15:25:42 -0600 Message-ID: <20240116212543.24149-5-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: HVTRDKS6VLRBGR3T4BD56OCJTUPMCV47 X-Message-ID-Hash: HVTRDKS6VLRBGR3T4BD56OCJTUPMCV47 X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705441443606100001 This capability checks if ch can receive multiple fds along with net-add api. This capability is required to enable multiple queues for domain/guest interfaces. Signed-off-by: Praveen K Paladugu Reviewed-by: Michal Privoznik --- src/ch/ch_capabilities.c | 9 +++++++++ src/ch/ch_capabilities.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/ch/ch_capabilities.c b/src/ch/ch_capabilities.c index b10485820c..b9e62d2d5b 100644 --- a/src/ch/ch_capabilities.c +++ b/src/ch/ch_capabilities.c @@ -50,6 +50,15 @@ virCHCapsInitCHVersionCaps(int version) if (version >=3D 18000000) virCHCapsSet(chCaps, CH_SERIAL_CONSOLE_IN_PARALLEL); =20 + /* Starting Version 22, add-net api can accept multiple FDs in the req= uest + * This is required to be able to configure queues for virtio-net devi= ces + * from libvirt. + * This capability will be used to gate networking support for ch gues= ts. + * https://github.com/cloud-hypervisor/cloud-hypervisor/releases/tag/v= 22.0 + */ + if (version >=3D 22000000) + virCHCapsSet(chCaps, CH_MULTIFD_IN_ADDNET); + return g_steal_pointer(&chCaps); =20 } diff --git a/src/ch/ch_capabilities.h b/src/ch/ch_capabilities.h index 703a6dbfe2..ffb8881a11 100644 --- a/src/ch/ch_capabilities.h +++ b/src/ch/ch_capabilities.h @@ -26,6 +26,7 @@ typedef enum { /* 0 */ CH_KERNEL_API_DEPRCATED, /* Use `payload` in place of `kernel` api */ CH_SERIAL_CONSOLE_IN_PARALLEL, /* Serial and Console ports can work in= parallel */ + CH_MULTIFD_IN_ADDNET, /* Cloud-hypervisor can accept multiple FDs in a= dd-net api */ =20 CH_CAPS_LAST /* this must always be the last item */ } virCHCapsFlags; --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Mon May 13 03:42:04 2024 Delivered-To: importer@patchew.org Received-SPF: none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; spf=none (zohomail.com: 8.43.85.245 is neither permitted nor denied by domain of lists.libvirt.org) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linux.microsoft.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1705441354611149.68536176204725; Tue, 16 Jan 2024 13:42:34 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 5F9D01E4E; Tue, 16 Jan 2024 16:42:33 -0500 (EST) Received: from lists.libvirt.org.85.43.8.in-addr.arpa (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 2D5641E34; Tue, 16 Jan 2024 16:26:25 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 01EAE1D50; Tue, 16 Jan 2024 16:26:00 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id C59D81D76 for ; Tue, 16 Jan 2024 16:25:55 -0500 (EST) Received: from home.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 12FFD20DF574; Tue, 16 Jan 2024 13:25:55 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 12FFD20DF574 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [PATCH v2 5/5] ch: Enable ETHERNET Network mode support Date: Tue, 16 Jan 2024 15:25:43 -0600 Message-ID: <20240116212543.24149-6-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240116212543.24149-1-prapal@linux.microsoft.com> References: <20240116212543.24149-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: HFL56CVK6RBY7CEOUTMANY4S3ILUUKO5 X-Message-ID-Hash: HFL56CVK6RBY7CEOUTMANY4S3ILUUKO5 X-MailFrom: prapal@linux.microsoft.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: liuwe@microsoft.com X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1705441355328100001 enable VIR_DOMAIN_NET_TYPE_ETHERNET network support for ch guests. Tested with following interface config: Signed-off-by: Praveen K Paladugu Reviewed-by: Michal Privoznik --- po/POTFILES | 1 + src/ch/ch_conf.h | 4 + src/ch/ch_domain.c | 41 ++++++++ src/ch/ch_domain.h | 3 + src/ch/ch_interface.c | 100 ++++++++++++++++++++ src/ch/ch_interface.h | 35 +++++++ src/ch/ch_monitor.c | 213 ++++++++++++++++-------------------------- src/ch/ch_monitor.h | 7 +- src/ch/ch_process.c | 166 +++++++++++++++++++++++++++++++- src/ch/meson.build | 2 + 10 files changed, 430 insertions(+), 142 deletions(-) create mode 100644 src/ch/ch_interface.c create mode 100644 src/ch/ch_interface.h diff --git a/po/POTFILES b/po/POTFILES index b594a8dd39..e48b9023e2 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -21,6 +21,7 @@ src/bhyve/bhyve_process.c src/ch/ch_conf.c src/ch/ch_domain.c src/ch/ch_driver.c +src/ch/ch_interface.c src/ch/ch_monitor.c src/ch/ch_process.c src/conf/backup_conf.c diff --git a/src/ch/ch_conf.h b/src/ch/ch_conf.h index 5b9b42540d..579eca894e 100644 --- a/src/ch/ch_conf.h +++ b/src/ch/ch_conf.h @@ -23,6 +23,7 @@ #include "virdomainobjlist.h" #include "virthread.h" #include "ch_capabilities.h" +#include "virebtables.h" =20 #define CH_DRIVER_NAME "CH" #define CH_CMD "cloud-hypervisor" @@ -75,6 +76,9 @@ struct _virCHDriver =20 /* pid file FD, ensures two copies of the driver can't use the same ro= ot */ int lockFD; + + /* Immutable pointer, lockless APIs. Pointless abstraction */ + ebtablesContext *ebtables; }; =20 virCaps *virCHDriverCapsInit(void); diff --git a/src/ch/ch_domain.c b/src/ch/ch_domain.c index c0d6c75b1d..25610a9459 100644 --- a/src/ch/ch_domain.c +++ b/src/ch/ch_domain.c @@ -22,6 +22,7 @@ =20 #include "ch_domain.h" #include "domain_driver.h" +#include "domain_validate.h" #include "virchrdev.h" #include "virlog.h" #include "virtime.h" @@ -355,3 +356,43 @@ virCHDomainObjFromDomain(virDomainPtr domain) =20 return vm; } + +int +virCHDomainValidateActualNetDef(virDomainNetDef *net) +{ + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + /* hypervisor-agnostic validation */ + if (virDomainActualNetDefValidate(net) < 0) + return -1; + + /* CH specific validation */ + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_ETHERNET: + if (net->guestIP.nips > 1) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("ethernet type supports a single guest ip")); + return -1; + } + break; + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_NETWORK: + case VIR_DOMAIN_NET_TYPE_DIRECT: + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + default: + break; + } + + return 0; +} diff --git a/src/ch/ch_domain.h b/src/ch/ch_domain.h index 4990914e9f..8dea2b2123 100644 --- a/src/ch/ch_domain.h +++ b/src/ch/ch_domain.h @@ -75,3 +75,6 @@ virCHDomainGetMachineName(virDomainObj *vm); =20 virDomainObj * virCHDomainObjFromDomain(virDomainPtr domain); + +int +virCHDomainValidateActualNetDef(virDomainNetDef *net); diff --git a/src/ch/ch_interface.c b/src/ch/ch_interface.c new file mode 100644 index 0000000000..ba90103c4e --- /dev/null +++ b/src/ch/ch_interface.c @@ -0,0 +1,100 @@ + +/* + * Copyright Microsoft Corp. 2023 + * + * ch_interface.c: methods to connect guest interfaces to appropriate host + * backends + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "domain_conf.h" +#include "domain_interface.h" +#include "virebtables.h" +#include "viralloc.h" +#include "ch_interface.h" +#include "virjson.h" +#include "virlog.h" + + +#define VIR_FROM_THIS VIR_FROM_CH + +VIR_LOG_INIT("ch.ch_interface"); + +/** + * virCHConnetNetworkInterfaces: + * @driver: pointer to ch driver object + * @vm: pointer to domain definition + * @net: pointer to a guest net + * @nicindexes: returned array of FDs of guest interfaces + * @nnicindexes: returned number of guest interfaces + * + * + * Returns 0 on success, -1 on error. + */ +int +virCHConnetNetworkInterfaces(virCHDriver *driver, + virDomainDef *vm, + virDomainNetDef *net, + int *tapfds, int **nicindexes, size_t *nnicin= dexes) +{ + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_ETHERNET: + + if (virDomainInterfaceEthernetConnect(vm, net, + driver->ebtables, false, + driver->privileged, tapfds, + net->driver.virtio.queues) <= 0) + return -1; + + G_GNUC_FALLTHROUGH; + case VIR_DOMAIN_NET_TYPE_NETWORK: + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_DIRECT: + if (nicindexes && nnicindexes && net->ifname) { + int nicindex =3D 0; + + if (virNetDevGetIndex(net->ifname, &nicindex) < 0) + return -1; + + VIR_APPEND_ELEMENT(*nicindexes, *nnicindexes, nicindex); + } + + break; + case VIR_DOMAIN_NET_TYPE_USER: + case VIR_DOMAIN_NET_TYPE_SERVER: + case VIR_DOMAIN_NET_TYPE_CLIENT: + case VIR_DOMAIN_NET_TYPE_MCAST: + case VIR_DOMAIN_NET_TYPE_VHOSTUSER: + case VIR_DOMAIN_NET_TYPE_INTERNAL: + case VIR_DOMAIN_NET_TYPE_HOSTDEV: + case VIR_DOMAIN_NET_TYPE_UDP: + case VIR_DOMAIN_NET_TYPE_VDPA: + case VIR_DOMAIN_NET_TYPE_NULL: + case VIR_DOMAIN_NET_TYPE_VDS: + case VIR_DOMAIN_NET_TYPE_LAST: + default: + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Unsupported Network type %1$d"), actualType); + return -1; + } + + return 0; +} diff --git a/src/ch/ch_interface.h b/src/ch/ch_interface.h new file mode 100644 index 0000000000..df87d4511f --- /dev/null +++ b/src/ch/ch_interface.h @@ -0,0 +1,35 @@ +/* + * Copyright Microsoft Corp. 2023 + * + * ch_interface.c: methods to connect guest interfaces to appropriate host + * backends + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + + +#include "ch_conf.h" +#include "virconftypes.h" + + +int +virCHConnetNetworkInterfaces(virCHDriver *driver, + virDomainDef *vmdef, + virDomainNetDef *netdef, + int *tapfds, + int **nicindexes, + size_t *nnicindexes); diff --git a/src/ch/ch_monitor.c b/src/ch/ch_monitor.c index 6f960c3a51..12f2468cc7 100644 --- a/src/ch/ch_monitor.c +++ b/src/ch/ch_monitor.c @@ -24,7 +24,11 @@ #include #include =20 +#include "datatypes.h" +#include "ch_conf.h" +#include "ch_interface.h" #include "ch_monitor.h" +#include "domain_interface.h" #include "viralloc.h" #include "vircommand.h" #include "virerror.h" @@ -258,148 +262,98 @@ virCHMonitorBuildDisksJson(virJSONValue *content, vi= rDomainDef *vmdef) return 0; } =20 -static int -virCHMonitorBuildNetJson(virJSONValue *nets, - virDomainNetDef *netdef, - size_t *nnicindexes, - int **nicindexes) +/** + * virCHMonitorBuildNetJson: + * @net: pointer to a guest network definition + * @jsonstr: returned network json + * + * Build net json to send to CH + * Returns 0 on success or -1 in case of error + */ +int +virCHMonitorBuildNetJson(virDomainNetDef *net, char **jsonstr) { - virDomainNetType netType =3D virDomainNetGetActualType(netdef); char macaddr[VIR_MAC_STRING_BUFLEN]; - g_autoptr(virJSONValue) net =3D NULL; - - // check net type at first - net =3D virJSONValueNewObject(); - - switch (netType) { - case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (netdef->guestIP.nips =3D=3D 1) { - const virNetDevIPAddr *ip =3D netdef->guestIP.ips[0]; - g_autofree char *addr =3D NULL; - virSocketAddr netmask; - g_autofree char *netmaskStr =3D NULL; - - if (!(addr =3D virSocketAddrFormat(&ip->address))) - return -1; - if (virJSONValueObjectAppendString(net, "ip", addr) < 0) - return -1; - - if (virSocketAddrPrefixToNetmask(ip->prefix, &netmask, AF_= INET) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to translate net prefix %1$d = to netmask"), - ip->prefix); - return -1; - } - if (!(netmaskStr =3D virSocketAddrFormat(&netmask))) - return -1; - if (virJSONValueObjectAppendString(net, "mask", netmaskStr= ) < 0) - return -1; - } else if (netdef->guestIP.nips > 1) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("ethernet type supports a single guest ip= ")); - } + g_autoptr(virJSONValue) net_json =3D virJSONValueNewObject(); + virDomainNetType actualType =3D virDomainNetGetActualType(net); =20 - /* network and bridge use a tap device, and direct uses a - * macvtap device - */ - if (nicindexes && nnicindexes && netdef->ifname) { - int nicindex =3D 0; =20 - if (virNetDevGetIndex(netdef->ifname, &nicindex) < 0) - return -1; =20 - VIR_APPEND_ELEMENT(*nicindexes, *nnicindexes, nicindex); - } - break; - case VIR_DOMAIN_NET_TYPE_VHOSTUSER: - if ((virDomainChrType)netdef->data.vhostuser->type !=3D VIR_DO= MAIN_CHR_TYPE_UNIX) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("vhost_user type support UNIX socket in this = CH")); - return -1; - } else { - if (virJSONValueObjectAppendString(net, "vhost_socket", ne= tdef->data.vhostuser->data.nix.path) < 0) - return -1; - if (virJSONValueObjectAppendBoolean(net, "vhost_user", tru= e) < 0) - return -1; - } - break; - case VIR_DOMAIN_NET_TYPE_BRIDGE: - case VIR_DOMAIN_NET_TYPE_NETWORK: - case VIR_DOMAIN_NET_TYPE_DIRECT: - case VIR_DOMAIN_NET_TYPE_USER: - case VIR_DOMAIN_NET_TYPE_SERVER: - case VIR_DOMAIN_NET_TYPE_CLIENT: - case VIR_DOMAIN_NET_TYPE_MCAST: - case VIR_DOMAIN_NET_TYPE_INTERNAL: - case VIR_DOMAIN_NET_TYPE_HOSTDEV: - case VIR_DOMAIN_NET_TYPE_UDP: - case VIR_DOMAIN_NET_TYPE_VDPA: - case VIR_DOMAIN_NET_TYPE_NULL: - case VIR_DOMAIN_NET_TYPE_VDS: - case VIR_DOMAIN_NET_TYPE_LAST: - default: - virReportEnumRangeError(virDomainNetType, netType); - return -1; - } - - if (netdef->ifname !=3D NULL) { - if (virJSONValueObjectAppendString(net, "tap", netdef->ifname) < 0) - return -1; - } - if (virJSONValueObjectAppendString(net, "mac", virMacAddrFormat(&netde= f->mac, macaddr)) < 0) - return -1; + if (actualType =3D=3D VIR_DOMAIN_NET_TYPE_ETHERNET) { + const virNetDevIPAddr *ip; + g_autofree char *addr =3D NULL; + virSocketAddr netmask; + g_autofree char *netmaskStr =3D NULL; + + /* nips should be 1, this is already validated in + virCHDomainValidateActualNetDef */ + if (net->guestIP.nips !=3D 1) + return -1; =20 + ip =3D net->guestIP.ips[0]; =20 - if (netdef->virtio !=3D NULL) { - if (netdef->virtio->iommu =3D=3D VIR_TRISTATE_SWITCH_ON) { - if (virJSONValueObjectAppendBoolean(net, "iommu", true) < 0) - return -1; - } - } - if (netdef->driver.virtio.queues) { - if (virJSONValueObjectAppendNumberInt(net, "num_queues", netdef->d= river.virtio.queues) < 0) + + if (!(addr =3D virSocketAddrFormat(&ip->address))) return -1; - } =20 - if (netdef->driver.virtio.rx_queue_size || netdef->driver.virtio.tx_qu= eue_size) { - if (netdef->driver.virtio.rx_queue_size !=3D netdef->driver.virtio= .tx_queue_size) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("virtio rx_queue_size option %1$d is not same with tx_que= ue_size %2$d"), - netdef->driver.virtio.rx_queue_size, - netdef->driver.virtio.tx_queue_size); + if (virJSONValueObjectAppendString(net_json, "ip", addr) < 0) + return -1; + + if (virSocketAddrPrefixToNetmask(ip->prefix, &netmask, AF_INET) < = 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to translate net prefix %1$d to netma= sk"), + ip->prefix); return -1; } - if (virJSONValueObjectAppendNumberInt(net, "queue_size", netdef->d= river.virtio.rx_queue_size) < 0) + + if (!(netmaskStr =3D virSocketAddrFormat(&netmask))) + return -1; + if (virJSONValueObjectAppendString(net_json, "mask", netmaskStr) <= 0) return -1; } =20 - if (virJSONValueArrayAppend(nets, &net) < 0) + if (virJSONValueObjectAppendString(net_json, "mac", + virMacAddrFormat(&net->mac, macaddr= )) < 0) return -1; =20 - return 0; -} - -static int -virCHMonitorBuildNetsJson(virJSONValue *content, - virDomainDef *vmdef, - size_t *nnicindexes, - int **nicindexes) -{ - g_autoptr(virJSONValue) nets =3D NULL; - size_t i; + if (net->virtio !=3D NULL) { + if (net->virtio->iommu =3D=3D VIR_TRISTATE_SWITCH_ON) { + if (virJSONValueObjectAppendBoolean(net_json, "iommu", true) <= 0) + return -1; + } + } =20 - if (vmdef->nnets > 0) { - nets =3D virJSONValueNewArray(); + /* Cloud-Hypervisor expects number of queues. 1 for rx and 1 for tx. + * Multiply queue pairs by 2 to provide total number of queues to CH + */ + if (net->driver.virtio.queues) { + if (virJSONValueObjectAppendNumberInt(net_json, "num_queues", + 2 * net->driver.virtio.queue= s) < 0) + return -1; + } =20 - for (i =3D 0; i < vmdef->nnets; i++) { - if (virCHMonitorBuildNetJson(nets, vmdef->nets[i], - nnicindexes, nicindexes) < 0) - return -1; + if (net->driver.virtio.rx_queue_size || net->driver.virtio.tx_queue_si= ze) { + if (net->driver.virtio.rx_queue_size !=3D + net->driver.virtio.tx_queue_si= ze) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("virtio rx_queue_size option %1$d is not same= with tx_queue_size %2$d"), + net->driver.virtio.rx_queue_size, + net->driver.virtio.tx_queue_size); + return -1; } - if (virJSONValueObjectAppend(content, "net", &nets) < 0) + if (virJSONValueObjectAppendNumberInt(net_json, "queue_size", + net->driver.virtio.rx_queue_= size) < 0) + return -1; + } + + if (net->mtu) { + if (virJSONValueObjectAppendNumberInt(net_json, "mtu", net->mtu) <= 0) return -1; } =20 + if (!(*jsonstr =3D virJSONValueToString(net_json, false))) + return -1; + return 0; } =20 @@ -456,11 +410,8 @@ virCHMonitorBuildDevicesJson(virJSONValue *content, } =20 static int -virCHMonitorBuildVMJson(virCHDriver *driver, - virDomainDef *vmdef, - char **jsonstr, - size_t *nnicindexes, - int **nicindexes) +virCHMonitorBuildVMJson(virCHDriver *driver, virDomainDef *vmdef, + char **jsonstr) { g_autoptr(virJSONValue) content =3D virJSONValueNewObject(); =20 @@ -490,10 +441,6 @@ virCHMonitorBuildVMJson(virCHDriver *driver, if (virCHMonitorBuildDisksJson(content, vmdef) < 0) return -1; =20 - - if (virCHMonitorBuildNetsJson(content, vmdef, nnicindexes, nicindexes)= < 0) - return -1; - if (virCHMonitorBuildDevicesJson(content, vmdef) < 0) return -1; =20 @@ -877,10 +824,7 @@ virCHMonitorShutdownVMM(virCHMonitor *mon) } =20 int -virCHMonitorCreateVM(virCHDriver *driver, - virCHMonitor *mon, - size_t *nnicindexes, - int **nicindexes) +virCHMonitorCreateVM(virCHDriver *driver, virCHMonitor *mon) { g_autofree char *url =3D NULL; int responseCode =3D 0; @@ -892,8 +836,7 @@ virCHMonitorCreateVM(virCHDriver *driver, headers =3D curl_slist_append(headers, "Accept: application/json"); headers =3D curl_slist_append(headers, "Content-Type: application/json= "); =20 - if (virCHMonitorBuildVMJson(driver, mon->vm->def, &payload, - nnicindexes, nicindexes) !=3D 0) + if (virCHMonitorBuildVMJson(driver, mon->vm->def, &payload) !=3D 0) return -1; =20 VIR_WITH_OBJECT_LOCK_GUARD(mon) { diff --git a/src/ch/ch_monitor.h b/src/ch/ch_monitor.h index bbfa77cdff..47b4e7abbd 100644 --- a/src/ch/ch_monitor.h +++ b/src/ch/ch_monitor.h @@ -104,10 +104,7 @@ void virCHMonitorClose(virCHMonitor *mon); G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCHMonitor, virCHMonitorClose); =20 =20 -int virCHMonitorCreateVM(virCHDriver *driver, - virCHMonitor *mon, - size_t *nnicindexes, - int **nicindexes); +int virCHMonitorCreateVM(virCHDriver *driver, virCHMonitor *mon); int virCHMonitorBootVM(virCHMonitor *mon); int virCHMonitorShutdownVM(virCHMonitor *mon); int virCHMonitorRebootVM(virCHMonitor *mon); @@ -123,3 +120,5 @@ size_t virCHMonitorGetThreadInfo(virCHMonitor *mon, boo= l refresh, virCHMonitorThreadInfo **threads); int virCHMonitorGetIOThreads(virCHMonitor *mon, virDomainIOThreadInfo ***iothreads); +int +virCHMonitorBuildNetJson(virDomainNetDef *netdef, char **jsonstr); diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c index f3bb4a7280..5bf24932d0 100644 --- a/src/ch/ch_process.c +++ b/src/ch/ch_process.c @@ -27,9 +27,13 @@ #include "ch_monitor.h" #include "ch_process.h" #include "domain_cgroup.h" +#include "domain_interface.h" #include "virerror.h" +#include "virfile.h" #include "virjson.h" #include "virlog.h" +#include "virstring.h" +#include "ch_interface.h" =20 #define VIR_FROM_THIS VIR_FROM_CH =20 @@ -448,13 +452,148 @@ virCHProcessSetupVcpus(virDomainObj *vm) return 0; } =20 +/** + * chProcessAddNetworkDevices: + * @driver: pointer to ch driver object + * @mon: pointer to the monitor object + * @vmdef: pointer to domain definition + * @nicindexes: returned array of FDs of guest interfaces + * @nnicindexes: returned number of network indexes + * + * Send tap fds to CH process via AddNet api. Capture the network indexes = of + * guest interfaces in nicindexes. + * + * Returns 0 on success, -1 on error. + */ +static int +chProcessAddNetworkDevices(virCHDriver *driver, + virCHMonitor *mon, + virDomainDef *vmdef, + int **nicindexes, + size_t *nnicindexes) +{ + size_t i, j, tapfd_len; + int mon_sockfd, http_res, ret; + g_autofree int *tapfds =3D NULL; + g_autoptr(virJSONValue) net =3D NULL; + struct sockaddr_un server_addr; + g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) http_headers =3D VIR_BUFFER_INITIALIZER; + + if (!virBitmapIsBitSet(driver->chCaps, CH_MULTIFD_IN_ADDNET)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Guest networking is not supported by this versio= n of ch")); + return -1; + } + + mon_sockfd =3D socket(AF_UNIX, SOCK_STREAM, 0); + if (mon_sockfd < 0) { + virReportSystemError(errno, "%s", _("Failed to open a UNIX socket"= )); + return -1; + } + + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sun_family =3D AF_UNIX; + if (virStrcpyStatic(server_addr.sun_path, mon->socketpath) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("UNIX socket path '%1$s' too long"), mon->socketp= ath); + goto err; + } + + if (connect(mon_sockfd, (struct sockaddr *)&server_addr, + sizeof(server_addr)) =3D=3D -1) { + virReportSystemError(errno, "%s", _("Failed to connect to mon sock= et")); + goto err; + } + + virBufferAddLit(&http_headers, "PUT /api/v1/vm.add-net HTTP/1.1\r\n"); + virBufferAddLit(&http_headers, "Host: localhost\r\n"); + virBufferAddLit(&http_headers, "Content-Type: application/json\r\n"); + + for (i =3D 0; i < vmdef->nnets; i++) { + g_autofree char *payload =3D NULL; + g_autofree char *response =3D NULL; + + if (vmdef->nets[i]->driver.virtio.queues =3D=3D 0) { + /* "queues" here refers to queue pairs. When 0, initialize + * queue pairs to 1. + */ + vmdef->nets[i]->driver.virtio.queues =3D 1; + } + tapfd_len =3D vmdef->nets[i]->driver.virtio.queues; + + if (virCHDomainValidateActualNetDef(vmdef->nets[i]) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("net definition failed validation")); + goto err; + } + + tapfds =3D g_new0(int, tapfd_len); + memset(tapfds, -1, (tapfd_len) * sizeof(int)); + + /* Connect Guest interfaces */ + if (virCHConnetNetworkInterfaces(driver, vmdef, vmdef->nets[i], ta= pfds, + nicindexes, nnicindexes) < 0) + goto err; + + if (virCHMonitorBuildNetJson(vmdef->nets[i], &payload) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to build net json")); + goto err; + } + + VIR_DEBUG("payload sent with net-add request to CH =3D %s", payloa= d); + + virBufferAsprintf(&buf, "%s", virBufferCurrentContent(&http_header= s)); + virBufferAsprintf(&buf, "Content-Length: %ld\r\n\r\n", strlen(payl= oad)); + virBufferAsprintf(&buf, "%s", payload); + payload =3D virBufferContentAndReset(&buf); + + if (virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd_len= ) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to send net-add request to CH")); + goto err; + } + + /* Close sent tap fds in Libvirt, as they have been dup()ed in CH = */ + for (j =3D 0; j < tapfd_len; j++) { + VIR_FORCE_CLOSE(tapfds[j]); + } + + /* Process the response from CH */ + response =3D virSocketRecv(mon_sockfd); + if (response =3D=3D NULL) { + VIR_ERROR(_("Failed while receiving response from CH")); + goto err; + } + + /* Parse the HTTP response code */ + ret =3D sscanf(response, "HTTP/1.%*d %d", &http_res); + if (ret !=3D 1) { + VIR_ERROR(_("Failed to parse HTTP response code")); + goto err; + } + if (http_res !=3D 204 && http_res !=3D 200) { + VIR_ERROR(_("Unexpected response from CH: %1$d"), http_res); + goto err; + } + } + + VIR_FORCE_CLOSE(mon_sockfd); + return 0; + + err: + VIR_FORCE_CLOSE(mon_sockfd); + return -1; +} + /** * virCHProcessStart: * @driver: pointer to driver structure * @vm: pointer to virtual machine structure * @reason: reason for switching vm to running state * - * Starts Cloud-Hypervisor listen on a local socket + * Starts Cloud-Hypervisor listening on a local socket * * Returns 0 on success or -1 in case of error */ @@ -483,8 +622,7 @@ virCHProcessStart(virCHDriver *driver, goto cleanup; } =20 - if (virCHMonitorCreateVM(driver, priv->monitor, - &nnicindexes, &nicindexes) < 0) { + if (virCHMonitorCreateVM(driver, priv->monitor) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to create guest VM")); goto cleanup; @@ -495,6 +633,13 @@ virCHProcessStart(virCHDriver *driver, vm->def->id =3D vm->pid; priv->machineName =3D virCHDomainGetMachineName(vm); =20 + if (chProcessAddNetworkDevices(driver, priv->monitor, vm->def, + &nicindexes, &nnicindexes) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed while adding guest interfaces")); + goto cleanup; + } + if (virDomainCgroupSetupCgroup("ch", vm, nnicindexes, nicindexes, &priv->cgroup, @@ -507,6 +652,10 @@ virCHProcessStart(virCHDriver *driver, if (virCHProcessInitCpuAffinity(vm) < 0) goto cleanup; =20 + /* Bring up netdevs before starting CPUs */ + if (virDomainInterfaceStartDevices(vm->def) < 0) + return -1; + if (virCHMonitorBootVM(priv->monitor) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to boot guest VM")); @@ -552,6 +701,9 @@ virCHProcessStop(virCHDriver *driver G_GNUC_UNUSED, int ret; int retries =3D 0; virCHDomainObjPrivate *priv =3D vm->privateData; + virCHDriverConfig *cfg =3D virCHDriverGetConfig(driver); + virDomainDef *def =3D vm->def; + size_t i; =20 VIR_DEBUG("Stopping VM name=3D%s pid=3D%d reason=3D%d", vm->def->name, (int)vm->pid, (int)reason); @@ -560,6 +712,14 @@ virCHProcessStop(virCHDriver *driver G_GNUC_UNUSED, g_clear_pointer(&priv->monitor, virCHMonitorClose); } =20 + /* de-activate netdevs after stopping vm */ + ignore_value(virDomainInterfaceStopDevices(vm->def)); + + for (i =3D 0; i < def->nnets; i++) { + virDomainNetDef *net =3D def->nets[i]; + virDomainInterfaceDeleteDevice(def, net, false, cfg->stateDir); + } + retry: if ((ret =3D virDomainCgroupRemoveCgroup(vm, priv->cgroup, diff --git a/src/ch/meson.build b/src/ch/meson.build index 6c8bf9c43f..633966aac7 100644 --- a/src/ch/meson.build +++ b/src/ch/meson.build @@ -7,6 +7,8 @@ ch_driver_sources =3D [ 'ch_domain.h', 'ch_driver.c', 'ch_driver.h', + 'ch_interface.c', + 'ch_interface.h', 'ch_monitor.c', 'ch_monitor.h', 'ch_process.c', --=20 2.43.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org