[libvirt] [PATCH 22/27] Add API for issuing 'pci_add nic' monitor command

Daniel P. Berrange berrange at redhat.com
Thu Sep 24 15:00:24 UTC 2009


* src/qemu/qemu_conf.c: Remove separator from qemuBuildNicStr()
  args, and remove hardcoded 'nic' prefix. Leave it upto callers
  instead
* src/qemu/qemu_driver.c: Switch over to using the new
  qemuMonitorAddPCINetwork() method for NIC hotplug
* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h: Add new
  qemuMonitorAddPCINetwork API for PCI network device hotplug
---
 src/qemu/qemu_conf.c         |    6 +--
 src/qemu/qemu_conf.h         |    1 -
 src/qemu/qemu_driver.c       |   93 ++++--------------------------------------
 src/qemu/qemu_monitor_text.c |   36 ++++++++++++++++
 src/qemu/qemu_monitor_text.h |    9 ++++
 5 files changed, 55 insertions(+), 90 deletions(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 309f171..c531454 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1263,14 +1263,12 @@ int
 qemuBuildNicStr(virConnectPtr conn,
                 virDomainNetDefPtr net,
                 const char *prefix,
-                char type_sep,
                 int vlan,
                 char **str)
 {
     if (virAsprintf(str,
-                    "%snic%cmacaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s%s%s",
+                    "%smacaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s%s%s",
                     prefix ? prefix : "",
-                    type_sep,
                     net->mac[0], net->mac[1],
                     net->mac[2], net->mac[3],
                     net->mac[4], net->mac[5],
@@ -1988,7 +1986,7 @@ int qemudBuildCommandLine(virConnectPtr conn,
                 qemuAssignNetNames(def, net) < 0)
                 goto no_memory;
 
-            if (qemuBuildNicStr(conn, net, NULL, ',', net->vlan, &nic) < 0)
+            if (qemuBuildNicStr(conn, net, "nic,", net->vlan, &nic) < 0)
                 goto error;
 
             if ((qargv[qargc++] = strdup("-net")) == NULL) {
diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h
index 82eb89f..6ff5f0d 100644
--- a/src/qemu/qemu_conf.h
+++ b/src/qemu/qemu_conf.h
@@ -179,7 +179,6 @@ int         qemuBuildHostNetStr         (virConnectPtr conn,
 int         qemuBuildNicStr             (virConnectPtr conn,
                                          virDomainNetDefPtr net,
                                          const char *prefix,
-                                         char type_sep,
                                          int vlan,
                                          char **str);
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index f8710a6..6363edc 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -4478,72 +4478,6 @@ static int qemudDomainChangeEjectableMedia(virConnectPtr conn,
     return 0;
 }
 
-static int
-qemudParsePciAddReply(virDomainObjPtr vm,
-                      const char *reply,
-                      unsigned *domain,
-                      unsigned *bus,
-                      unsigned *slot)
-{
-    char *s, *e;
-
-    DEBUG("%s: pci_add reply: %s", vm->def->name, reply);
-
-    /* If the command succeeds qemu prints:
-     * OK bus 0, slot XXX...
-     * or
-     * OK domain 0, bus 0, slot XXX
-     */
-    if (!(s = strstr(reply, "OK ")))
-        return -1;
-
-    s += 3;
-
-    if (STRPREFIX(s, "domain ")) {
-        s += strlen("domain ");
-
-        if (virStrToLong_ui(s, &e, 10, domain) == -1) {
-            VIR_WARN(_("Unable to parse domain number '%s'\n"), s);
-            return -1;
-        }
-
-        if (!STRPREFIX(e, ", ")) {
-            VIR_WARN(_("Expected ', ' parsing pci_add reply '%s'\n"), s);
-            return -1;
-        }
-        s = e + 2;
-    }
-
-    if (!STRPREFIX(s, "bus ")) {
-        VIR_WARN(_("Expected 'bus ' parsing pci_add reply '%s'\n"), s);
-        return -1;
-    }
-    s += strlen("bus ");
-
-    if (virStrToLong_ui(s, &e, 10, bus) == -1) {
-        VIR_WARN(_("Unable to parse bus number '%s'\n"), s);
-        return -1;
-    }
-
-    if (!STRPREFIX(e, ", ")) {
-        VIR_WARN(_("Expected ', ' parsing pci_add reply '%s'\n"), s);
-        return -1;
-    }
-    s = e + 2;
-
-    if (!STRPREFIX(s, "slot ")) {
-        VIR_WARN(_("Expected 'slot ' parsing pci_add reply '%s'\n"), s);
-        return -1;
-    }
-    s += strlen("slot ");
-
-    if (virStrToLong_ui(s, &e, 10, slot) == -1) {
-        VIR_WARN(_("Unable to parse slot number '%s'\n"), s);
-        return -1;
-    }
-
-    return 0;
-}
 
 static int qemudDomainAttachPciDiskDevice(virConnectPtr conn,
                                           virDomainObjPtr vm,
@@ -4621,7 +4555,7 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
     char *cmd = NULL, *reply = NULL, *remove_cmd = NULL;
     char *tapfd_name = NULL;
     int i, tapfd = -1;
-    unsigned domain, bus, slot;
+    char *nicstr = NULL;
 
     if (!(qemuCmdFlags & QEMUD_CMD_FLAG_HOST_NET_ADD)) {
         qemudReportError(conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s",
@@ -4693,30 +4627,18 @@ static int qemudDomainAttachNetDevice(virConnectPtr conn,
         close(tapfd);
     tapfd = -1;
 
-    if (qemuBuildNicStr(conn, net,
-                        "pci_add pci_addr=auto ", ' ', net->vlan, &cmd) < 0)
+    if (qemuBuildNicStr(conn, net, NULL, net->vlan, &nicstr) < 0)
         goto try_remove;
 
-    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
-        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                         _("failed to add NIC with '%s'"), cmd);
-        goto try_remove;
-    }
-
-    if (qemudParsePciAddReply(vm, reply, &domain, &bus, &slot) < 0) {
-        qemudReportError(conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                         _("parsing pci_add reply failed: %s"), reply);
+    if (qemuMonitorAddPCINetwork(vm, nicstr,
+                                 &net->pci_addr.domain,
+                                 &net->pci_addr.bus,
+                                 &net->pci_addr.slot) < 0)
         goto try_remove;
-    }
 
-    VIR_FREE(cmd);
-    VIR_FREE(reply);
+    VIR_FREE(nicstr);
     VIR_FREE(remove_cmd);
 
-    net->pci_addr.domain = domain;
-    net->pci_addr.bus    = bus;
-    net->pci_addr.slot   = slot;
-
     vm->def->nets[vm->def->nnets++] = net;
 
     return 0;
@@ -4744,6 +4666,7 @@ try_tapfd_close:
 no_memory:
     virReportOOMError(conn);
 cleanup:
+    VIR_FREE(nicstr);
     VIR_FREE(cmd);
     VIR_FREE(reply);
     VIR_FREE(remove_cmd);
diff --git a/src/qemu/qemu_monitor_text.c b/src/qemu/qemu_monitor_text.c
index 92a2dbd..c6ffd33 100644
--- a/src/qemu/qemu_monitor_text.c
+++ b/src/qemu/qemu_monitor_text.c
@@ -1491,6 +1491,42 @@ cleanup:
 }
 
 
+int qemuMonitorAddPCINetwork(const virDomainObjPtr vm,
+                             const char *nicstr,
+                             unsigned *guestDomain,
+                             unsigned *guestBus,
+                             unsigned *guestSlot)
+{
+    char *cmd;
+    char *reply = NULL;
+    int ret = -1;
+
+    if (virAsprintf(&cmd, "pci_add pci_addr=auto nic %s", nicstr) < 0) {
+        virReportOOMError(NULL);
+        return -1;
+    }
+
+    if (qemudMonitorCommand(vm, cmd, &reply) < 0) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                         _("failed to add NIC with '%s'"), cmd);
+        goto cleanup;
+    }
+
+    if (qemuMonitorParsePciAddReply(vm, reply,
+                                    guestDomain, guestBus, guestSlot) < 0) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+                         _("parsing pci_add reply failed: %s"), reply);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    VIR_FREE(reply);
+    VIR_FREE(cmd);
+    return ret;
+}
+
 
 int qemuMonitorRemovePCIDevice(const virDomainObjPtr vm,
                                unsigned guestDomain,
diff --git a/src/qemu/qemu_monitor_text.h b/src/qemu/qemu_monitor_text.h
index b6dae52..1bd9d6d 100644
--- a/src/qemu/qemu_monitor_text.h
+++ b/src/qemu/qemu_monitor_text.h
@@ -170,6 +170,15 @@ int qemuMonitorAddPCIDisk(const virDomainObjPtr vm,
                           unsigned *guestBus,
                           unsigned *guestSlot);
 
+/* XXX do we really want to hardcode 'nicstr' as the
+ * sendable item here
+ */
+int qemuMonitorAddPCINetwork(const virDomainObjPtr vm,
+                             const char *nicstr,
+                             unsigned *guestDomain,
+                             unsigned *guestBus,
+                             unsigned *guestSlot);
+
 int qemuMonitorRemovePCIDevice(const virDomainObjPtr vm,
                                unsigned guestDomain,
                                unsigned guestBus,
-- 
1.6.2.5




More information about the libvir-list mailing list