[libvirt] [PATCH 08/16] LXC from native: convert macvlan network configuration

Cédric Bosdonnat cbosdonnat at suse.com
Tue Jan 14 13:49:57 UTC 2014


---
 src/lxc/lxc_native.c                               | 38 +++++++++++++++++-----
 .../lxcconf2xml-macvlannetwork.config              | 14 ++++++++
 .../lxcconf2xmldata/lxcconf2xml-macvlannetwork.xml | 30 +++++++++++++++++
 tests/lxcconf2xmltest.c                            |  1 +
 4 files changed, 74 insertions(+), 9 deletions(-)
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.config
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.xml

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 6ea4998..a99bc1c 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -521,9 +521,11 @@ static virDomainNetDefPtr
 lxcCreateNetDef(const char *type,
                 const char *link,
                 const char *mac,
-                const char *flag)
+                const char *flag,
+                const char *macvlanmode)
 {
     virDomainNetDefPtr net = NULL;
+    virMacAddr macAddr;
 
     if (VIR_ALLOC(net) < 0)
         goto error;
@@ -535,9 +537,11 @@ lxcCreateNetDef(const char *type,
             net->linkstate = VIR_DOMAIN_NET_INTERFACE_LINK_STATE_DOWN;
     }
 
-    if (STREQ(type, "veth")) {
-        virMacAddr macAddr;
 
+    if (mac && virMacAddrParse(mac, &macAddr) == 0)
+        net->mac = macAddr;
+
+    if (STREQ(type, "veth")) {
         if (!link)
             goto error;
 
@@ -546,9 +550,18 @@ lxcCreateNetDef(const char *type,
         if (VIR_STRDUP(net->data.bridge.brname, link) < 0)
             goto error;
 
-        if (mac && virMacAddrParse(mac, &macAddr) == 0)
-            net->mac = macAddr;
+    } else if (STREQ(type, "macvlan")) {
+        net->type = VIR_DOMAIN_NET_TYPE_DIRECT;
+
+        if (VIR_STRDUP(net->data.direct.linkdev, link) < 0)
+            goto error;
 
+        if (!macvlanmode || STREQ(macvlanmode, "private"))
+            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_PRIVATE;
+        else if (STREQ(macvlanmode, "vepa"))
+            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_VEPA;
+        else if (STREQ(macvlanmode, "bridget"))
+            net->data.direct.mode = VIR_NETDEV_MACVLAN_MODE_BRIDGE;
     }
 
     return net;
@@ -583,7 +596,8 @@ lxcAddNetworkDefinition(virDomainDefPtr def,
                         const char *type,
                         const char *link,
                         const char *mac,
-                        const char *flag)
+                        const char *flag,
+                        const char *macvlanmode)
 {
     virDomainNetDefPtr net = NULL;
     virDomainHostdevDefPtr hostdev = NULL;
@@ -601,7 +615,7 @@ lxcAddNetworkDefinition(virDomainDefPtr def,
             goto error;
         def->hostdevs[def->nhostdevs - 1] = hostdev;
     } else {
-        if (!(net = lxcCreateNetDef(type, link, mac, flag)))
+        if (!(net = lxcCreateNetDef(type, link, mac, flag, macvlanmode)))
             goto error;
 
         if (VIR_EXPAND_N(def->nets, def->nnets, 1) < 0)
@@ -625,6 +639,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
     char *link = NULL;
     char *mac = NULL;
     char *flag = NULL;
+    char *macvlanmode = NULL;
     bool nonetwork = true;
     int status;
 
@@ -634,7 +649,8 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
                 property = property->next) {
             if (STREQ(property->key, "lxc.network.type")) {
                 /* Store the previous NIC */
-                status = lxcAddNetworkDefinition(def, type, link, mac, flag);
+                status = lxcAddNetworkDefinition(def, type, link, mac, flag,
+                                                 macvlanmode);
                 if (status < 0)
                     return -1;
                 else if (status > 0)
@@ -645,6 +661,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
                 link = NULL;
                 mac = NULL;
                 flag = NULL;
+                macvlanmode = NULL;
 
                 /* Keep the new value */
                 type = property->value;
@@ -655,10 +672,13 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
                 mac = property->value;
             else if (STREQ(property->key, "lxc.network.flags"))
                 flag = property->value;
+            else if (STREQ(property->key, "lxc.network.macvlan.mode"))
+                macvlanmode = property->value;
         }
 
         /* Add the last network definition found */
-        status = lxcAddNetworkDefinition(def, type, link, mac, flag);
+        status = lxcAddNetworkDefinition(def, type, link, mac, flag,
+                                         macvlanmode);
         if (status < 0)
             return -1;
         else if (status > 0)
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.config
new file mode 100644
index 0000000..3fa2c78
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.config
@@ -0,0 +1,14 @@
+# Template used to create this container: opensuse
+# Template script checksum (SHA-1): 27307e0a95bd81b2c0bd82d6f87fdbe83be075ef
+
+lxc.network.type = macvlan
+lxc.network.flags = up
+lxc.network.link = eth0
+lxc.network.hwaddr = 02:00:15:8f:05:c1
+lxc.network.macvlan.mode = vepa
+
+#remove next line if host DNS configuration should not be available to container
+lxc.rootfs = /var/lib/lxc/migrate_test/rootfs
+lxc.utsname = migrate_test
+lxc.autodev=1
+lxc.mount = /var/lib/lxc/migrate_test/fstab
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.xml
new file mode 100644
index 0000000..6e52f75
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-macvlannetwork.xml
@@ -0,0 +1,30 @@
+<domain type='lxc'>
+  <name>migrate_test</name>
+  <uuid>c7a5fdbd-edaf-9455-926a-d65c16db1809</uuid>
+  <memory unit='KiB'>4035770</memory>
+  <currentMemory unit='KiB'>0</currentMemory>
+  <vcpu placement='static' current='0'>1</vcpu>
+  <os>
+    <type>exe</type>
+    <init>/sbin/init</init>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <filesystem type='mount' accessmode='passthrough'>
+      <source dir='/var/lib/lxc/migrate_test/rootfs'/>
+      <target dir='/'/>
+    </filesystem>
+    <filesystem type='ram' accessmode='passthrough'>
+      <source usage='2017885' units='KiB'/>
+      <target dir='/run'/>
+    </filesystem>
+    <interface type='direct'>
+      <mac address='02:00:15:8f:05:c1'/>
+      <source dev='eth0' mode='vepa'/>
+      <link state='up'/>
+    </interface>
+  </devices>
+</domain>
diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c
index fe2e3bc..8d23a11 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -109,6 +109,7 @@ mymain(void)
     DO_TEST("simple");
     DO_TEST("nonetwork");
     DO_TEST("physnetwork");
+    DO_TEST("macvlannetwork");
 
     return ret;
 }
-- 
1.8.5.2




More information about the libvir-list mailing list