[libvirt] [PATCH 06/10] Convert NICs over to use -device & -netdev where possible

Daniel P. Berrange berrange at redhat.com
Tue Dec 15 15:14:46 UTC 2009


The current syntax uses a pair of args

   -net nic,macaddr=52:54:00:56:6c:55,vlan=3,model=pcnet,name=pcnet.0
   -net user,vlan=3,name=user.2

The new syntax does not  need the vlan craziness anymore, and
so has a simplified pair of args

   -netdev user,id=user.2
   -device pcnet,netdev=user.2,mac=52:54:00:56:6c:55
---
 src/qemu/qemu_conf.c |  166 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 137 insertions(+), 29 deletions(-)

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 2480df4..464f582 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1490,6 +1490,35 @@ qemuBuildNicStr(virConnectPtr conn,
     return 0;
 }
 
+static int
+qemuBuildNicDevStr(virConnectPtr conn,
+                   virDomainNetDefPtr net,
+                   char **str)
+{
+    const char *nic;
+
+    if (!net->model) {
+        nic = "rtl8139";
+    } else if (STREQ(net->model, "virtio")) {
+        nic = "virtio-net-pci";
+    } else {
+        nic = net->model;
+    }
+
+    if (virAsprintf(str,
+                    "%s,netdev=%s,mac=%02x:%02x:%02x:%02x:%02x:%02x",
+                    nic,
+                    net->hostnet_name,
+                    net->mac[0], net->mac[1],
+                    net->mac[2], net->mac[3],
+                    net->mac[4], net->mac[5]) < 0) {
+        virReportOOMError(conn);
+        return -1;
+    }
+
+    return 0;
+}
+
 int
 qemuBuildHostNetStr(virConnectPtr conn,
                     virDomainNetDefPtr net,
@@ -1587,6 +1616,87 @@ qemuBuildHostNetStr(virConnectPtr conn,
 }
 
 static int
+qemuBuildNetDevStr(virConnectPtr conn,
+                   virDomainNetDefPtr net,
+                   const char *tapfd,
+                   char **str)
+{
+    switch (net->type) {
+    case VIR_DOMAIN_NET_TYPE_NETWORK:
+    case VIR_DOMAIN_NET_TYPE_BRIDGE:
+        if (virAsprintf(str, "tap,fd=%s,id=%s",
+                        tapfd, net->hostnet_name) < 0) {
+            virReportOOMError(conn);
+            return -1;
+        }
+        break;
+
+    case VIR_DOMAIN_NET_TYPE_ETHERNET:
+        {
+            virBuffer buf = VIR_BUFFER_INITIALIZER;
+
+            virBufferAddLit(&buf, "tap");
+            if (net->ifname)
+                virBufferVSprintf(&buf, ",ifname=%s", net->ifname);
+            if (net->data.ethernet.script)
+                virBufferVSprintf(&buf, ",script=%s",
+                                  net->data.ethernet.script);
+            if (net->hostnet_name)
+                virBufferVSprintf(&buf, ",id=%s",
+                                  net->hostnet_name);
+            if (virBufferError(&buf)) {
+                virBufferFreeAndReset(&buf);
+                virReportOOMError(conn);
+                return -1;
+            }
+
+            *str = virBufferContentAndReset(&buf);
+        }
+        break;
+
+    case VIR_DOMAIN_NET_TYPE_CLIENT:
+    case VIR_DOMAIN_NET_TYPE_SERVER:
+    case VIR_DOMAIN_NET_TYPE_MCAST:
+        {
+            const char *mode = NULL;
+
+            switch (net->type) {
+            case VIR_DOMAIN_NET_TYPE_CLIENT:
+                mode = "connect";
+                break;
+            case VIR_DOMAIN_NET_TYPE_SERVER:
+                mode = "listen";
+                break;
+            case VIR_DOMAIN_NET_TYPE_MCAST:
+                mode = "mcast";
+                break;
+            }
+
+            if (virAsprintf(str, "socket,%s=%s:%d,id=%s",
+                            mode,
+                            net->data.socket.address,
+                            net->data.socket.port,
+                            net->hostnet_name) < 0) {
+                virReportOOMError(conn);
+                return -1;
+            }
+        }
+        break;
+
+    case VIR_DOMAIN_NET_TYPE_USER:
+    default:
+        if (virAsprintf(str, "user,id=%s",
+                        net->hostnet_name) < 0) {
+            virReportOOMError(conn);
+            return -1;
+        }
+        break;
+    }
+
+    return 0;
+}
+
+static int
 qemuBuildSoundDevStr(virConnectPtr conn,
                      virDomainSoundDefPtr sound,
                      char **str)
@@ -2279,26 +2389,9 @@ int qemudBuildCommandLine(virConnectPtr conn,
         for (i = 0 ; i < def->nnets ; i++) {
             virDomainNetDefPtr net = def->nets[i];
             char *nic, *host;
-            char *tapfd_name = NULL;
+            char tapfd_name[50];
 
             net->vlan = i;
-
-            ADD_ARG_SPACE;
-            if ((qemuCmdFlags & QEMUD_CMD_FLAG_NET_NAME) &&
-                qemuAssignNetNames(def, net) < 0)
-                goto no_memory;
-
-            if (qemuBuildNicStr(conn, net, "nic,", net->vlan, &nic) < 0)
-                goto error;
-
-            if ((qargv[qargc++] = strdup("-net")) == NULL) {
-                VIR_FREE(nic);
-                goto no_memory;
-            }
-            ADD_ARG(nic);
-
-
-            ADD_ARG_SPACE;
             if (net->type == VIR_DOMAIN_NET_TYPE_NETWORK ||
                 net->type == VIR_DOMAIN_NET_TYPE_BRIDGE) {
                 int tapfd = qemudNetworkIfaceConnect(conn, driver, net, qemuCmdFlags);
@@ -2312,23 +2405,38 @@ int qemudBuildCommandLine(virConnectPtr conn,
 
                 (*tapfds)[(*ntapfds)++] = tapfd;
 
-                if (virAsprintf(&tapfd_name, "%d", tapfd) < 0)
+                if (snprintf(tapfd_name, sizeof(tapfd_name), "%d", tapfd) >= sizeof(tapfd_name))
                     goto no_memory;
             }
 
-            if (qemuBuildHostNetStr(conn, net, ',',
-                                    net->vlan, tapfd_name, &host) < 0) {
-                VIR_FREE(tapfd_name);
-                goto error;
+            if ((qemuCmdFlags & QEMUD_CMD_FLAG_NET_NAME) ||
+                (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE)) {
+                if (qemuAssignNetNames(def, net) < 0)
+                    goto no_memory;
             }
 
-            if ((qargv[qargc++] = strdup("-net")) == NULL) {
-                VIR_FREE(host);
-                goto no_memory;
-            }
-            ADD_ARG(host);
+            if (qemuCmdFlags & QEMUD_CMD_FLAG_DEVICE) {
+                ADD_ARG_LIT("-netdev");
+                if (qemuBuildNetDevStr(conn, net, tapfd_name, &host) < 0)
+                    goto error;
+                ADD_ARG(host);
+
+                ADD_ARG_LIT("-device");
+                if (qemuBuildNicDevStr(conn, net, &nic) < 0)
+                    goto error;
+                ADD_ARG(nic);
+            } else {
+                ADD_ARG_LIT("-net");
+                if (qemuBuildNicStr(conn, net, "nic,", net->vlan, &nic) < 0)
+                    goto error;
+                ADD_ARG(nic);
 
-            VIR_FREE(tapfd_name);
+                ADD_ARG_LIT("-net");
+                if (qemuBuildHostNetStr(conn, net, ',',
+                                        net->vlan, tapfd_name, &host) < 0)
+                    goto error;
+                ADD_ARG(host);
+            }
         }
     }
 
-- 
1.6.5.2




More information about the libvir-list mailing list