From 4bbe1029f2fb6cd1c102794951a944c62fdbd0e6 Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Fri, 20 Feb 2015 14:52:37 -0500 Subject: [PATCH] qemu: fix ifindex array reported to systemd Commit f7afeddc added code to report to systemd an array of interface indexes for all tap devices used by a guest. Unfortunately it not only didn't add code to report the ifindexes for macvtap interfaces (interface type='direct') or the tap devices used by type='ethernet', it ended up sending "-1" as the ifindex for each macvtap or hostdev interface. This resulted in a failure to start any domain that had a macvtap or hostdev interface (or actually any type other than "network" or "bridge"). This patch does the following with the nicindexes array: 1) Modify qemuBuildInterfaceCommandLine() to only fill in the nicindexes array if given a non-NULL pointer to an array (and modifies the test jig calls to the function to send NULL). This is because there are tests in the test suite that have type='ethernet' and still have an ifname specified, but that device of course doesn't actually exist on the test system, so attempts to call virNetDevGetIndex() will fail. 2) Even then, only add an entry to the nicindexes array for appropriate types, and to do so for all appropriate types ("network", "bridge", and "direct"), but only if the ifname is known (since that is required to call virNetDevGetIndex(). --- src/qemu/qemu_command.c | 64 ++++++++++++++++++++++++++++++---------- src/qemu/qemu_command.h | 5 ++-- src/qemu/qemu_driver.c | 6 +--- src/qemu/qemu_hotplug.c | 4 +-- tests/qemuxml2argvtest.c | 5 +--- tests/qemuxmlnstest.c | 5 +--- 6 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index dd06f09997..24b2ad95b4 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -289,8 +289,7 @@ qemuNetworkIfaceConnect(virDomainDefPtr def, virDomainNetDefPtr net, virQEMUCapsPtr qemuCaps, int *tapfd, - size_t *tapfdSize, - int *nicindex) + size_t *tapfdSize) { const char *brname; int ret = -1; @@ -337,8 +336,6 @@ qemuNetworkIfaceConnect(virDomainDefPtr def, virDomainAuditNetDevice(def, net, tunpath, false); goto cleanup; } - if (virNetDevGetIndex(net->ifname, nicindex) < 0) - goto cleanup; if (virDomainNetGetActualBridgeMACTableManager(net) == VIR_NETWORK_BRIDGE_MAC_TABLE_MANAGER_LIBVIRT) { /* libvirt is managing the FDB of the bridge this device @@ -7757,7 +7754,8 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd, int bootindex, virNetDevVPortProfileOp vmop, bool standalone, - int *nicindex) + size_t *nnicindexes, + int **nicindexes) { int ret = -1; char *nic = NULL, *host = NULL; @@ -7771,8 +7769,6 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd, virNetDevBandwidthPtr actualBandwidth; size_t i; - *nicindex = -1; - if (actualType == VIR_DOMAIN_NET_TYPE_VHOSTUSER) return qemuBuildVhostuserCommandLine(cmd, def, net, qemuCaps); @@ -7819,7 +7815,7 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd, if (qemuNetworkIfaceConnect(def, driver, net, qemuCaps, tapfd, - &tapfdSize, nicindex) < 0) + &tapfdSize) < 0) goto cleanup; } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) { if (VIR_ALLOC(tapfd) < 0 || VIR_ALLOC(tapfdName) < 0) @@ -7831,6 +7827,49 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd, goto cleanup; } + /* For types whose implementations use a netdev on the host, add + * an entry to nicindexes for passing on to systemd. + */ + switch ((virDomainNetType)actualType) { + case VIR_DOMAIN_NET_TYPE_ETHERNET: + case VIR_DOMAIN_NET_TYPE_NETWORK: + case VIR_DOMAIN_NET_TYPE_BRIDGE: + case VIR_DOMAIN_NET_TYPE_DIRECT: + { + int nicindex; + + /* network and bridge use a tap device, and direct uses a + * macvtap device + */ + if (nicindexes && nnicindexes && net->ifname) { + if (virNetDevGetIndex(net->ifname, &nicindex) < 0 || + VIR_APPEND_ELEMENT(*nicindexes, *nnicindexes, nicindex) < 0) + goto cleanup; + } + 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_LAST: + /* These types don't use a network device on the host, but + * instead use some other type of connection to the emulated + * device in the qemu process. + * + * (Note that hostdev can't be considered as "using a network + * device", because by the time it is being used, it has been + * detached from the hostside network driver so it doesn't show + * up in the list of interfaces on the host - it's just some + * PCI device.) + */ + break; + } + /* Set bandwidth or warn if requested and not supported. */ actualBandwidth = virDomainNetGetActualBandwidth(net); if (actualBandwidth) { @@ -8221,9 +8260,6 @@ qemuBuildCommandLine(virConnectPtr conn, virUUIDFormat(def->uuid, uuid); - *nnicindexes = 0; - *nicindexes = NULL; - emulator = def->emulator; if (!cfg->privileged) { @@ -9282,13 +9318,9 @@ qemuBuildCommandLine(virConnectPtr conn, else vlan = i; - if (VIR_EXPAND_N(*nicindexes, *nnicindexes, 1) < 0) - goto error; - if (qemuBuildInterfaceCommandLine(cmd, driver, def, net, qemuCaps, vlan, bootNet, vmop, - standalone, - &((*nicindexes)[*nnicindexes - 1])) < 0) + standalone, nnicindexes, nicindexes) < 0) goto error; last_good_net = i; diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index 89e83515ee..ee81f92077 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -1,7 +1,7 @@ /* * qemu_command.h: QEMU command generation * - * Copyright (C) 2006-2014 Red Hat, Inc. + * Copyright (C) 2006-2015 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -208,8 +208,7 @@ int qemuNetworkIfaceConnect(virDomainDefPtr def, virDomainNetDefPtr net, virQEMUCapsPtr qemuCaps, int *tapfd, - size_t *tapfdSize, - int *nicindex) + size_t *tapfdSize) ATTRIBUTE_NONNULL(2); int qemuPhysIfaceConnect(virDomainDefPtr def, diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 4057abdf7c..e9aa0b5fee 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6447,8 +6447,6 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn, size_t i; virQEMUDriverConfigPtr cfg; virCapsPtr caps = NULL; - size_t nnicindexes = 0; - int *nicindexes = NULL; virCheckFlags(0, NULL); @@ -6634,14 +6632,12 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn, &buildCommandLineCallbacks, true, qemuCheckFips(), - NULL, - &nnicindexes, &nicindexes))) + NULL, NULL, NULL))) goto cleanup; ret = virCommandToString(cmd); cleanup: - VIR_FREE(nicindexes); virObjectUnref(qemuCaps); virCommandFree(cmd); virDomainDefFree(def); diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 51f3573f09..08047ce1f4 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -846,7 +846,6 @@ int qemuDomainAttachNetDevice(virConnectPtr conn, qemuDomainObjPrivatePtr priv = vm->privateData; char **tapfdName = NULL; int *tapfd = NULL; - int nicindex = -1; size_t tapfdSize = 0; char **vhostfdName = NULL; int *vhostfd = NULL; @@ -916,8 +915,7 @@ int qemuDomainAttachNetDevice(virConnectPtr conn, goto cleanup; memset(vhostfd, -1, sizeof(*vhostfd) * vhostfdSize); if (qemuNetworkIfaceConnect(vm->def, driver, net, - priv->qemuCaps, tapfd, &tapfdSize, - &nicindex) < 0) + priv->qemuCaps, tapfd, &tapfdSize) < 0) goto cleanup; iface_connected = true; if (qemuOpenVhostNet(vm->def, net, priv->qemuCaps, vhostfd, &vhostfdSize) < 0) diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 16f325e2bd..7eba5c95fb 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -263,8 +263,6 @@ static int testCompareXMLToArgvFiles(const char *xml, char *log = NULL; virCommandPtr cmd = NULL; size_t i; - size_t nnicindexes = 0; - int *nicindexes = NULL; virBitmapPtr nodeset = NULL; if (!(conn = virGetConnect())) @@ -355,7 +353,7 @@ static int testCompareXMLToArgvFiles(const char *xml, VIR_NETDEV_VPORT_PROFILE_OP_NO_OP, &testCallbacks, false, (flags & FLAG_FIPS), - nodeset, &nnicindexes, &nicindexes))) { + nodeset, NULL, NULL))) { if (!virtTestOOMActive() && (flags & FLAG_EXPECT_FAILURE)) { ret = 0; @@ -402,7 +400,6 @@ static int testCompareXMLToArgvFiles(const char *xml, ret = 0; out: - VIR_FREE(nicindexes); VIR_FREE(log); VIR_FREE(expectargv); VIR_FREE(actualargv); diff --git a/tests/qemuxmlnstest.c b/tests/qemuxmlnstest.c index a068135d16..4220737e84 100644 --- a/tests/qemuxmlnstest.c +++ b/tests/qemuxmlnstest.c @@ -44,8 +44,6 @@ static int testCompareXMLToArgvFiles(const char *xml, char *log = NULL; char *emulator = NULL; virCommandPtr cmd = NULL; - size_t nnicindexes = 0; - int *nicindexes = NULL; if (!(conn = virGetConnect())) goto fail; @@ -122,7 +120,7 @@ static int testCompareXMLToArgvFiles(const char *xml, migrateFrom, migrateFd, NULL, VIR_NETDEV_VPORT_PROFILE_OP_NO_OP, &testCallbacks, false, false, NULL, - &nnicindexes, &nicindexes))) + NULL, NULL))) goto fail; if (!virtTestOOMActive()) { @@ -158,7 +156,6 @@ static int testCompareXMLToArgvFiles(const char *xml, ret = 0; fail: - VIR_FREE(nicindexes); VIR_FREE(log); VIR_FREE(emulator); VIR_FREE(expectargv); -- GitLab