[PATCH 1/8] qemu: report TAP device indexes to systemd

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Record the index of each TAP device created and report them to
systemd, so they show up in machinectl status for the VM.
---
 src/qemu/qemu_cgroup.c   | 13 ++++++++-----
 src/qemu/qemu_cgroup.h   |  4 +++-
 src/qemu/qemu_command.c  | 27 ++++++++++++++++++++++-----
 src/qemu/qemu_command.h  |  7 +++++--
 src/qemu/qemu_driver.c   |  7 +++++--
 src/qemu/qemu_hotplug.c  |  4 +++-
 src/qemu/qemu_process.c  |  9 +++++++--
 tests/qemuxml2argvtest.c |  5 ++++-
 tests/qemuxmlnstest.c    |  6 +++++-
 9 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index d71ffbc..fc46450 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -727,7 +727,9 @@ qemuSetupCpuCgroup(virQEMUDriverPtr driver,
 
 static int
 qemuInitCgroup(virQEMUDriverPtr driver,
-               virDomainObjPtr vm)
+               virDomainObjPtr vm,
+               size_t nnicindexes,
+               int *nicindexes)
 {
     int ret = -1;
     qemuDomainObjPrivatePtr priv = vm->privateData;
@@ -769,7 +771,7 @@ qemuInitCgroup(virQEMUDriverPtr driver,
                             NULL,
                             vm->pid,
                             false,
-                            0, NULL,
+                            nnicindexes, nicindexes,
                             vm->def->resource->partition,
                             cfg->cgroupControllers,
                             &priv->cgroup) < 0) {
@@ -855,7 +857,9 @@ qemuConnectCgroup(virQEMUDriverPtr driver,
 
 int
 qemuSetupCgroup(virQEMUDriverPtr driver,
-                virDomainObjPtr vm)
+                virDomainObjPtr vm,
+                size_t nnicindexes,
+                int *nicindexes)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virCapsPtr caps = NULL;
@@ -867,7 +871,7 @@ qemuSetupCgroup(virQEMUDriverPtr driver,
         return -1;
     }
 
-    if (qemuInitCgroup(driver, vm) < 0)
+    if (qemuInitCgroup(driver, vm, nnicindexes, nicindexes) < 0)
         return -1;
 
     if (!priv->cgroup)
@@ -1023,7 +1027,6 @@ qemuSetupCgroupForVcpu(virDomainObjPtr vm)
         /* If we don't know VCPU<->PID mapping or all vcpu runs in the same
          * thread, we cannot control each vcpu.
          */
-        VIR_WARN("Unable to get vcpus' pids.");
         return 0;
     }
 
diff --git a/src/qemu/qemu_cgroup.h b/src/qemu/qemu_cgroup.h
index df7e3a1..a5d9c49 100644
--- a/src/qemu/qemu_cgroup.h
+++ b/src/qemu/qemu_cgroup.h
@@ -45,7 +45,9 @@ int qemuTeardownHostdevCgroup(virDomainObjPtr vm,
 int qemuConnectCgroup(virQEMUDriverPtr driver,
                       virDomainObjPtr vm);
 int qemuSetupCgroup(virQEMUDriverPtr driver,
-                    virDomainObjPtr vm);
+                    virDomainObjPtr vm,
+                    size_t nnicindexes,
+                    int *nicindexes);
 int qemuSetupCpusetMems(virDomainObjPtr vm);
 int qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
                           unsigned long long period,
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index c041ee7..d50bec9 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -290,7 +290,8 @@ qemuNetworkIfaceConnect(virDomainDefPtr def,
                         virDomainNetDefPtr net,
                         virQEMUCapsPtr qemuCaps,
                         int *tapfd,
-                        size_t *tapfdSize)
+                        size_t *tapfdSize,
+                        int *nicindex)
 {
     const char *brname;
     int ret = -1;
@@ -331,6 +332,8 @@ 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
@@ -7383,7 +7386,8 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd,
                               int vlan,
                               int bootindex,
                               virNetDevVPortProfileOp vmop,
-                              bool standalone)
+                              bool standalone,
+                              int *nicindex)
 {
     int ret = -1;
     char *nic = NULL, *host = NULL;
@@ -7397,6 +7401,8 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd,
     virNetDevBandwidthPtr actualBandwidth;
     size_t i;
 
+    *nicindex = -1;
+
     if (actualType == VIR_DOMAIN_NET_TYPE_VHOSTUSER)
         return qemuBuildVhostuserCommandLine(cmd, def, net, qemuCaps);
 
@@ -7433,7 +7439,8 @@ qemuBuildInterfaceCommandLine(virCommandPtr cmd,
         memset(tapfd, -1, tapfdSize * sizeof(tapfd[0]));
 
         if (qemuNetworkIfaceConnect(def, conn, driver, net,
-                                    qemuCaps, tapfd, &tapfdSize) < 0)
+                                    qemuCaps, tapfd,
+                                    &tapfdSize, nicindex) < 0)
             goto cleanup;
     } else if (actualType == VIR_DOMAIN_NET_TYPE_DIRECT) {
         if (VIR_ALLOC(tapfd) < 0 || VIR_ALLOC(tapfdName) < 0)
@@ -7788,7 +7795,9 @@ qemuBuildCommandLine(virConnectPtr conn,
                      qemuBuildCommandLineCallbacksPtr callbacks,
                      bool standalone,
                      bool enableFips,
-                     virBitmapPtr nodeset)
+                     virBitmapPtr nodeset,
+                     size_t *nnicindexes,
+                     int **nicindexes)
 {
     virErrorPtr originalError = NULL;
     size_t i, j;
@@ -7833,6 +7842,9 @@ qemuBuildCommandLine(virConnectPtr conn,
 
     virUUIDFormat(def->uuid, uuid);
 
+    *nnicindexes = 0;
+    *nicindexes = NULL;
+
     emulator = def->emulator;
 
     if (!cfg->privileged) {
@@ -8889,10 +8901,15 @@ qemuBuildCommandLine(virConnectPtr conn,
             else
                 vlan = i;
 
+            if (VIR_EXPAND_N(*nicindexes, *nnicindexes, 1) < 0)
+                goto error;
+
             if (qemuBuildInterfaceCommandLine(cmd, driver, conn, def, net,
                                               qemuCaps, vlan, bootNet, vmop,
-                                              standalone) < 0)
+                                              standalone,
+                                              &((*nicindexes)[*nnicindexes - 1])) < 0)
                 goto error;
+
             last_good_net = i;
             bootNet = 0;
         }
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index dcc7127..7a670cc 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -82,7 +82,9 @@ virCommandPtr qemuBuildCommandLine(virConnectPtr conn,
                                    qemuBuildCommandLineCallbacksPtr callbacks,
                                    bool forXMLToArgv,
                                    bool enableFips,
-                                   virBitmapPtr nodeset)
+                                   virBitmapPtr nodeset,
+                                   size_t *nnicindexes,
+                                   int **nicindexes)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(11);
 
 /* Generate '-device' string for chardev device */
@@ -195,7 +197,8 @@ int qemuNetworkIfaceConnect(virDomainDefPtr def,
                             virDomainNetDefPtr net,
                             virQEMUCapsPtr qemuCaps,
                             int *tapfd,
-                            size_t *tapfdSize)
+                            size_t *tapfdSize,
+                            int *nicindex)
     ATTRIBUTE_NONNULL(2);
 
 int qemuPhysIfaceConnect(virDomainDefPtr def,
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 9eed81d..b800555 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -6310,6 +6310,8 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn,
     size_t i;
     virQEMUDriverConfigPtr cfg;
     virCapsPtr caps = NULL;
+    size_t nnicindexes = 0;
+    int *nicindexes = NULL;
 
     virCheckFlags(0, NULL);
 
@@ -6495,13 +6497,14 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn,
                                      &buildCommandLineCallbacks,
                                      true,
                                      qemuCheckFips(),
-                                     NULL)))
+                                     NULL,
+                                     &nnicindexes, &nicindexes)))
         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 6f62345..6b7d9b8 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -825,6 +825,7 @@ 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;
@@ -894,7 +895,8 @@ int qemuDomainAttachNetDevice(virConnectPtr conn,
             goto cleanup;
         memset(vhostfd, -1, sizeof(*vhostfd) * vhostfdSize);
         if (qemuNetworkIfaceConnect(vm->def, conn, driver, net,
-                                    priv->qemuCaps, tapfd, &tapfdSize) < 0)
+                                    priv->qemuCaps, tapfd, &tapfdSize,
+                                    &nicindex) < 0)
             goto cleanup;
         iface_connected = true;
         if (qemuOpenVhostNet(vm->def, net, priv->qemuCaps, vhostfd, &vhostfdSize) < 0)
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 2aa195f..da61e2b 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -4270,6 +4270,8 @@ int qemuProcessStart(virConnectPtr conn,
     virQEMUDriverConfigPtr cfg;
     virCapsPtr caps = NULL;
     unsigned int hostdev_flags = 0;
+    size_t nnicindexes = 0;
+    int *nicindexes = NULL;
 
     VIR_DEBUG("vm=%p name=%s id=%d pid=%llu",
               vm, vm->def->name, vm->def->id,
@@ -4589,7 +4591,8 @@ int qemuProcessStart(virConnectPtr conn,
                                      migrateFrom, stdin_fd, snapshot, vmop,
                                      &buildCommandLineCallbacks, false,
                                      qemuCheckFips(),
-                                     nodemask)))
+                                     nodemask,
+                                     &nnicindexes, &nicindexes)))
         goto cleanup;
 
     /* now that we know it is about to start call the hook if present */
@@ -4726,7 +4729,7 @@ int qemuProcessStart(virConnectPtr conn,
     }
 
     VIR_DEBUG("Setting up domain cgroup (if required)");
-    if (qemuSetupCgroup(driver, vm) < 0)
+    if (qemuSetupCgroup(driver, vm, nnicindexes, nicindexes) < 0)
         goto cleanup;
 
     /* This must be done after cgroup placement to avoid resetting CPU
@@ -4943,6 +4946,7 @@ int qemuProcessStart(virConnectPtr conn,
     VIR_FORCE_CLOSE(logfile);
     virObjectUnref(cfg);
     virObjectUnref(caps);
+    VIR_FREE(nicindexes);
 
     return 0;
 
@@ -4958,6 +4962,7 @@ int qemuProcessStart(virConnectPtr conn,
     qemuProcessStop(driver, vm, VIR_DOMAIN_SHUTOFF_FAILED, stop_flags);
     virObjectUnref(cfg);
     virObjectUnref(caps);
+    VIR_FREE(nicindexes);
 
     return -1;
 }
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index d1a95c5..8ef2173 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -279,6 +279,8 @@ static int testCompareXMLToArgvFiles(const char *xml,
     char *log = NULL;
     virCommandPtr cmd = NULL;
     size_t i;
+    size_t nnicindexes = 0;
+    int *nicindexes = NULL;
 
     if (!(conn = virGetConnect()))
         goto out;
@@ -363,7 +365,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
                                      VIR_NETDEV_VPORT_PROFILE_OP_NO_OP,
                                      &testCallbacks, false,
                                      (flags & FLAG_FIPS),
-                                     NULL))) {
+                                     NULL, &nnicindexes, &nicindexes))) {
         if (!virtTestOOMActive() &&
             (flags & FLAG_EXPECT_FAILURE)) {
             ret = 0;
@@ -410,6 +412,7 @@ 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 947aa9c..d76f078 100644
--- a/tests/qemuxmlnstest.c
+++ b/tests/qemuxmlnstest.c
@@ -44,6 +44,8 @@ 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;
@@ -119,7 +121,8 @@ static int testCompareXMLToArgvFiles(const char *xml,
                                      vmdef, &monitor_chr, json, extraFlags,
                                      migrateFrom, migrateFd, NULL,
                                      VIR_NETDEV_VPORT_PROFILE_OP_NO_OP,
-                                     &testCallbacks, false, false, NULL)))
+                                     &testCallbacks, false, false, NULL,
+                                     &nnicindexes, &nicindexes)))
         goto fail;
 
     if (!virtTestOOMActive()) {
@@ -155,6 +158,7 @@ static int testCompareXMLToArgvFiles(const char *xml,
     ret = 0;
 
  fail:
+    VIR_FREE(nicindexes);
     VIR_FREE(log);
     VIR_FREE(emulator);
     VIR_FREE(expectargv);
-- 
2.1.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]