From nobody Sun May 12 09:07:57 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 1701299718870601.7181446206217; Wed, 29 Nov 2023 15:15:18 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 401591B4E; Wed, 29 Nov 2023 18:15:18 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 24C441B0A; Wed, 29 Nov 2023 18:11:31 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id CB8C41A97; Wed, 29 Nov 2023 18:11:16 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 705711A9A for ; Wed, 29 Nov 2023 18:11:15 -0500 (EST) Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 1011020B74C1; Wed, 29 Nov 2023 15:11:13 -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 1011020B74C1 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [libvirt PATCH 1/5] conf: Drop unused parameter Date: Wed, 29 Nov 2023 17:11:02 -0600 Message-ID: <20231129231106.73443-2-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231129231106.73443-1-prapal@linux.microsoft.com> References: <20231129231106.73443-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: YM7MRJOMT3OSV2CDGZIYZMXA4MPBROZR X-Message-ID-Hash: YM7MRJOMT3OSV2CDGZIYZMXA4MPBROZR 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: 1701299720165000001 Drop unused parameter from virDomainNetReleaseActualDevice method. Signed-off-by: Praveen K Paladugu --- 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 22ad43e1d7..054ce2106f 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -30211,7 +30211,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 ed07859bc5..429b134770 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -4403,9 +4403,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 0c4beffd6a..d6431cd674 100644 --- a/src/libxl/libxl_domain.c +++ b/src/libxl/libxl_domain.c @@ -742,7 +742,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 c98d2d737a..50fca69758 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -3433,7 +3433,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); @@ -3904,7 +3904,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 25cbfc57c9..4c43fb0da9 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -4111,7 +4111,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 4e2fc724c0..45c055ea7f 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)); } @@ -4111,7 +4111,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)); } @@ -4147,7 +4147,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; @@ -4727,7 +4727,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)); } @@ -4845,7 +4845,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 f32e82bbd1..b28d53f280 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -8564,7 +8564,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.41.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sun May 12 09:07:57 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 1701300251574153.41059330702456; Wed, 29 Nov 2023 15:24:11 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 652851B20; Wed, 29 Nov 2023 18:24:10 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id D06571ACD; Wed, 29 Nov 2023 18:12:00 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 3D3971AAC; Wed, 29 Nov 2023 18:11:23 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 05D2B1A99 for ; Wed, 29 Nov 2023 18:11:16 -0500 (EST) Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 8E91D20B74C2; Wed, 29 Nov 2023 15:11:14 -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 8E91D20B74C2 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [libvirt PATCH 2/5] hypervisor: Move domain interface mgmt methods Date: Wed, 29 Nov 2023 17:11:03 -0600 Message-ID: <20231129231106.73443-3-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231129231106.73443-1-prapal@linux.microsoft.com> References: <20231129231106.73443-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: 3NPWAWXP4YQ5LNSA7TXZDJVSHQVU52Q7 X-Message-ID-Hash: 3NPWAWXP4YQ5LNSA7TXZDJVSHQVU52Q7 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: 1701300252009000001 Move domain interface management methods from qemu to hypervisor. This refactoring allows the domain management methods to be shared by ch driver. This commit does not introduce any functional changes. Signed-off-by: Praveen K Paladugu --- 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 | 6 +- src/qemu/qemu_hotplug.c | 5 +- src/qemu/qemu_interface.c | 339 +--------------------- src/qemu/qemu_interface.h | 11 - src/qemu/qemu_process.c | 73 +---- 10 files changed, 532 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..a7d5d46a8c --- /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, + virDomainInterfaceIsVnetCompatModel(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, + virDomainNetGetActualD= irectDev(net), + virDomainNetGetActualD= irectMode(net), + virDomainNetGetActualV= irtPortProfile(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 553b01b8c0..680a90034a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1630,6 +1630,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 d54149ed2d..c3dd777089 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" @@ -8575,7 +8576,10 @@ qemuBuildInterfaceConnect(virDomainObj *vm, break; =20 case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (qemuInterfaceEthernetConnect(vm->def, priv->driver, net, + if (virDomainInterfaceEthernetConnect(vm->def, net, + priv->driver->ebtables, + priv->driver->config->macFilter, + priv->driver->privileged, tapfd, tapfdSize) < 0) return -1; vhostfd =3D true; diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 45c055ea7f..0ce7b175c4 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); @@ -4778,7 +4779,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 6eed3e6bd7..47d7ec03fd 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 b28d53f280..aad406f679 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" @@ -3121,7 +3122,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)); @@ -3184,7 +3185,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)); @@ -8362,11 +8363,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, " @@ -8505,71 +8504,13 @@ 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); } - retry: if ((ret =3D virDomainCgroupRemoveCgroup(vm, priv->cgroup, priv->machi= neName)) < 0) { if (ret =3D=3D -EBUSY && (retries++ < 5)) { --=20 2.41.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sun May 12 09:07:57 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 1701299949849790.2858275615087; Wed, 29 Nov 2023 15:19:09 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 1AFEC1B8F; Wed, 29 Nov 2023 18:19:09 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 67ADF1ADF; Wed, 29 Nov 2023 18:11:51 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id B30411A9C; Wed, 29 Nov 2023 18:11:18 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 529A31A9F for ; Wed, 29 Nov 2023 18:11:16 -0500 (EST) Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 1FB5120B74C3; Wed, 29 Nov 2023 15:11:15 -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 1FB5120B74C3 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [libvirt PATCH 3/5] util: Add util methods required by ch networking Date: Wed, 29 Nov 2023 17:11:04 -0600 Message-ID: <20231129231106.73443-4-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231129231106.73443-1-prapal@linux.microsoft.com> References: <20231129231106.73443-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: 57JJ3RPVG5HNDRDV3FUP7QWU3XZNW72P X-Message-ID-Hash: 57JJ3RPVG5HNDRDV3FUP7QWU3XZNW72P 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: 1701299951066000001 virSocketSendMsgWithFDs method send fds along with payload using SCM_RIGHTS. virSocketRecvHttpResponse method polls, receives http response on the input socket and returns the http response code. These methods are required to add network suppport in ch driver. Signed-off-by: Praveen K Paladugu --- po/POTFILES | 1 + src/libvirt_private.syms | 2 + src/util/virsocket.c | 109 +++++++++++++++++++++++++++++++++++++++ src/util/virsocket.h | 3 ++ 4 files changed, 115 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 680a90034a..e643cea774 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3371,7 +3371,9 @@ virSecureEraseString; =20 # util/virsocket.h virSocketRecvFD; +virSocketRecvHttpResponse; virSocketSendFD; +virSocketSendMsgWithFDs; =20 =20 # util/virsocketaddr.h diff --git a/src/util/virsocket.c b/src/util/virsocket.c index cd6f7ecd1b..b11e53a215 100644 --- a/src/util/virsocket.c +++ b/src/util/virsocket.c @@ -22,8 +22,14 @@ #include "virsocket.h" #include "virutil.h" #include "virfile.h" +#include "virlog.h" =20 #include +#include + +#define PKT_TIMEOUT_MS 500 /* ms */ + +VIR_LOG_INIT("util.virsocket"); =20 #ifdef WIN32 =20 @@ -482,6 +488,109 @@ 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, or -1 on error. + */ +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); + if (!cmsg) { + VIR_ERROR(_("Couldn't fit control msg header 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) + return -1; + + return ret; +} + +/** + * virSocketRecvHttpResponse: + * @sock: socket to receive http response on + * + * This function polls @sock for HTTP response + * Returns HTTP response code from received message, or -1 on error. + */ +int virSocketRecvHttpResponse(int sock) +{ + struct pollfd pfds[1]; + /* This is only used for responses from ch guests, which fit within + * 1024 buffer + */ + char buf[1024]; + int response_code, ret; + + 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) { + VIR_ERROR(_("Poll on sock %1$d failed"), sock); + } else if (ret =3D=3D 0) { + VIR_ERROR(_("Poll on sock %1$d timed out"), sock); + } + return -1; + } + + do { + ret =3D recv(sock, buf, sizeof(buf), 0); + } while (ret < 0 && errno =3D=3D EINTR); + + if (ret < 0) { + VIR_ERROR(_("recv on sock %1$d failed"), sock); + return -1; + } + + /* Parse the HTTP response code */ + ret =3D sscanf(buf, "HTTP/1.%*d %d", &response_code); + if (ret !=3D 1) { + VIR_ERROR(_("Failed to parse HTTP response code")); + return -1; + } + + return response_code; +} + #else /* WIN32 */ int virSocketSendFD(int sock G_GNUC_UNUSED, int fd G_GNUC_UNUSED) diff --git a/src/util/virsocket.h b/src/util/virsocket.h index 419da8b3ae..9190a6a569 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); +int virSocketRecvHttpResponse(int sock); =20 #ifdef WIN32 =20 --=20 2.41.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sun May 12 09:07:57 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 170129981798380.23726744823603; Wed, 29 Nov 2023 15:16:57 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id 6A8F51AF0; Wed, 29 Nov 2023 18:16:57 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 593091AF4; Wed, 29 Nov 2023 18:11:43 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id 911FB1AA5; Wed, 29 Nov 2023 18:11:18 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id CC5A51A9A for ; Wed, 29 Nov 2023 18:11:16 -0500 (EST) Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 9123520B74C0; Wed, 29 Nov 2023 15:11:15 -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 9123520B74C0 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [libvirt PATCH 4/5] ch: Introduce version based cap for network support Date: Wed, 29 Nov 2023 17:11:05 -0600 Message-ID: <20231129231106.73443-5-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231129231106.73443-1-prapal@linux.microsoft.com> References: <20231129231106.73443-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: KAJQWWBPAPUPCQDPCILT77FSC7Z7QSCV X-Message-ID-Hash: KAJQWWBPAPUPCQDPCILT77FSC7Z7QSCV 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: 1701299818562000001 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 --- 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.41.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org From nobody Sun May 12 09:07:57 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 1701300488556990.6063587945433; Wed, 29 Nov 2023 15:28:08 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 996) id DB1281ADE; Wed, 29 Nov 2023 18:28:07 -0500 (EST) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 566511AFE; Wed, 29 Nov 2023 18:12:10 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 996) id A1BAB1ACC; Wed, 29 Nov 2023 18:11:25 -0500 (EST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lists.libvirt.org (Postfix) with ESMTP id 2B63D1AA3 for ; Wed, 29 Nov 2023 18:11:17 -0500 (EST) Received: from home-fedora.. (072-191-074-189.res.spectrum.com [72.191.74.189]) by linux.microsoft.com (Postfix) with ESMTPSA id 1341220B74C1; Wed, 29 Nov 2023 15:11:16 -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 1341220B74C1 From: Praveen K Paladugu To: devel@lists.libvirt.org Subject: [libvirt PATCH 5/5] ch: Enable ETHERNET Network mode support Date: Wed, 29 Nov 2023 17:11:06 -0600 Message-ID: <20231129231106.73443-6-prapal@linux.microsoft.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231129231106.73443-1-prapal@linux.microsoft.com> References: <20231129231106.73443-1-prapal@linux.microsoft.com> MIME-Version: 1.0 Message-ID-Hash: N7EARAHM44TUFAB5CIHUMGY2MSKHUETS X-Message-ID-Hash: N7EARAHM44TUFAB5CIHUMGY2MSKHUETS 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: 1701300490509000001 enable VIR_DOMAIN_NET_TYPE_ETHERNET network support for ch guests. Tested with following interface config: Signed-off-by: Praveen K Paladugu --- 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 | 104 +++++++++++++++++++++ src/ch/ch_interface.h | 35 ++++++++ src/ch/ch_monitor.c | 205 +++++++++++++++--------------------------- src/ch/ch_monitor.h | 7 +- src/ch/ch_process.c | 160 ++++++++++++++++++++++++++++++++- src/ch/meson.build | 2 + 10 files changed, 424 insertions(+), 138 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..2bf29d9f0a 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..61976b073f --- /dev/null +++ b/src/ch/ch_interface.c @@ -0,0 +1,104 @@ +/* + * 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 *nnicindexes) +{ + + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + + switch (actualType) { + case VIR_DOMAIN_NET_TYPE_ETHERNET: + + if (virDomainInterfaceEthernetConnect(vm, net, driver->ebtables, f= alse, + 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..21d4b52c93 --- /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..03470bf50f 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) -{ - virDomainNetType netType =3D virDomainNetGetActualType(netdef); - char macaddr[VIR_MAC_STRING_BUFLEN]; - g_autoptr(virJSONValue) net =3D NULL; =20 - // 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; =20 - 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= ")); - } =20 - /* network and bridge use a tap device, and direct uses a - * macvtap device - */ - if (nicindexes && nnicindexes && netdef->ifname) { - int nicindex =3D 0; +/** + * 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) +{ + char macaddr[VIR_MAC_STRING_BUFLEN]; + g_autoptr(virJSONValue) net_json =3D virJSONValueNewObject(); + + virDomainNetType actualType =3D virDomainNetGetActualType(net); + + if (actualType =3D=3D VIR_DOMAIN_NET_TYPE_ETHERNET) { + + /* + * else case for below is already checked in + * virCHDomainValidateActualNetDef. No need to check again. + */ + if (net->guestIP.nips =3D=3D 1) { + const virNetDevIPAddr *ip =3D net->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; =20 - if (virNetDevGetIndex(netdef->ifname, &nicindex) < 0) + if (virJSONValueObjectAppendString(net_json, "ip", addr) < 0) return -1; - - VIR_APPEND_ELEMENT(*nicindexes, *nnicindexes, nicindex); + if (virSocketAddrPrefixToNetmask(ip->prefix, &netmask, + AF_INET) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Failed to translate net prefix %1$d to netm= ask"), + ip->prefix); + return -1; } - 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")); + if (!(netmaskStr =3D virSocketAddrFormat(&netmask))) 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) + if (virJSONValueObjectAppendString(net_json, "mask", netmaskSt= r) < 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; + } + + } =20 + if (virJSONValueObjectAppendString(net_json, "mac", + virMacAddrFormat(&net->mac, macaddr)) < 0) + return -1; =20 - if (netdef->virtio !=3D NULL) { - if (netdef->virtio->iommu =3D=3D VIR_TRISTATE_SWITCH_ON) { - if (virJSONValueObjectAppendBoolean(net, "iommu", true) < 0) + if (net->virtio !=3D NULL) { + if (net->virtio->iommu =3D=3D VIR_TRISTATE_SWITCH_ON) { + if (virJSONValueObjectAppendBoolean(net_json, "iommu", true) <= 0) return -1; } } - if (netdef->driver.virtio.queues) { - if (virJSONValueObjectAppendNumberInt(net, "num_queues", netdef->d= river.virtio.queues) < 0) + + /* 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.queues) < 0) 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) { + 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_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); + net->driver.virtio.rx_queue_size, + net->driver.virtio.tx_queue_size); return -1; } - if (virJSONValueObjectAppendNumberInt(net, "queue_size", netdef->d= river.virtio.rx_queue_size) < 0) + if (virJSONValueObjectAppendNumberInt(net_json, + "queue_size", net->driver.virtio.rx_queue_size= ) < 0) return -1; } =20 - if (virJSONValueArrayAppend(nets, &net) < 0) - return -1; - - return 0; -} - -static int -virCHMonitorBuildNetsJson(virJSONValue *content, - virDomainDef *vmdef, - size_t *nnicindexes, - int **nicindexes) -{ - g_autoptr(virJSONValue) nets =3D NULL; - size_t i; - - if (vmdef->nnets > 0) { - nets =3D virJSONValueNewArray(); - - for (i =3D 0; i < vmdef->nnets; i++) { - if (virCHMonitorBuildNetJson(nets, vmdef->nets[i], - nnicindexes, nicindexes) < 0) - return -1; - } - if (virJSONValueObjectAppend(content, "net", &nets) < 0) + 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..c734d164a1 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,142 @@ 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; + 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; + int ret; + + if (!virBitmapIsBitSet(driver->chCaps, CH_MULTIFD_IN_ADDNET)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Guest networking is not supported by this version 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; + 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(*tapfds)); + + /* 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); + + ret =3D virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd= _len); + if (ret < 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 */ + http_res =3D virSocketRecvHttpResponse(mon_sockfd); + if (http_res < 0) { + VIR_ERROR(_("Failed while receiving response from CH")); + 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 +616,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 +627,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 +646,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 +695,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 +706,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.41.0 _______________________________________________ Devel mailing list -- devel@lists.libvirt.org To unsubscribe send an email to devel-leave@lists.libvirt.org