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

[libvirt] [PATCH 03/14] Factor qemuBuildNicStr() out from qemuBuildCommandLine()



Re-factor this code so that it can be used for NIC hotplug
too. The awkward arguments are needed to allow use to do
"pci_add auto nic macaddr=..."

* src/qemu_conf.c: factor the nic string formatting code into
  its own function
---
 src/qemu_conf.c |   37 +++++++++++++++++++++++++++----------
 1 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/src/qemu_conf.c b/src/qemu_conf.c
index 4043d70..679dac3 100644
--- a/src/qemu_conf.c
+++ b/src/qemu_conf.c
@@ -833,6 +833,30 @@ qemudNetworkIfaceConnect(virConnectPtr conn,
     return NULL;
 }
 
+static 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",
+                    prefix ? prefix : "",
+                    type_sep,
+                    net->mac[0], net->mac[1],
+                    net->mac[2], net->mac[3],
+                    net->mac[4], net->mac[5],
+                    vlan,
+                    (net->model ? ",model=" : ""),
+                    (net->model ? net->model : "")) < 0) {
+        virReportOOMError(conn);
+        return -1;
+    }
+
+    return 0;
+}
 
 static int qemudBuildCommandLineChrDevStr(virDomainChrDefPtr dev,
                                           char *buf,
@@ -1366,21 +1390,14 @@ int qemudBuildCommandLine(virConnectPtr conn,
     } else {
         int vlan = 0;
         for (i = 0 ; i < def->nnets ; i++) {
-            char nic[100];
             virDomainNetDefPtr net = def->nets[i];
+            char *nic;
 
-            if (snprintf(nic, sizeof(nic),
-                         "nic,macaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s",
-                         net->mac[0], net->mac[1],
-                         net->mac[2], net->mac[3],
-                         net->mac[4], net->mac[5],
-                         vlan,
-                         (net->model ? ",model=" : ""),
-                         (net->model ? net->model : "")) >= sizeof(nic))
+            if (qemuBuildNicStr(conn, net, NULL, ',', vlan, &nic) < 0)
                 goto error;
 
             ADD_ARG_LIT("-net");
-            ADD_ARG_LIT(nic);
+            ADD_ARG(nic);
             ADD_ARG_LIT("-net");
 
             switch (net->type) {
-- 
1.6.2.5


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