[libvirt] [PATCH 16/16] LXC from native: map vlan network type

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


The problem with VLAN is that the user still has to manually create the
vlan interface on the host. Then the generated configuration will use
it as a nerwork hostdev device. So the generated configurations of the
following two fragments are equivalent.

lxc.network.type = phys
lxc.network.link = eth0.5

lxc.network.type = vlan
lxc.network.link = eth0
lxc.network.vlan.id = 5
---
 src/lxc/lxc_native.c                               | 23 ++++++++++++++---
 .../lxcconf2xmldata/lxcconf2xml-vlannetwork.config | 13 ++++++++++
 tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.xml  | 30 ++++++++++++++++++++++
 tests/lxcconf2xmltest.c                            |  1 +
 4 files changed, 63 insertions(+), 4 deletions(-)
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.config
 create mode 100644 tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.xml

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 5ba6f94..68ab965 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -602,20 +602,31 @@ lxcAddNetworkDefinition(virDomainDefPtr def,
                         const char *link,
                         const char *mac,
                         const char *flag,
-                        const char *macvlanmode)
+                        const char *macvlanmode,
+                        const char *vlanid)
 {
     virDomainNetDefPtr net = NULL;
     virDomainHostdevDefPtr hostdev = NULL;
+    bool isPhys, isVlan = false;
 
     if ((type == NULL) || STREQ(type, "empty") || STREQ(type, ""))
         return 0;
 
-    if (type != NULL && STREQ(type, "phys")) {
+    isPhys = STREQ(type, "phys");
+    isVlan = STREQ(type, "vlan");
+    if (type != NULL && (isPhys || isVlan)) {
         if (!(hostdev = lxcCreateHostdevDef(VIR_DOMAIN_HOSTDEV_MODE_CAPABILITIES,
                                             VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET,
                                             link)))
             goto error;
 
+        /* This still requires the user to manually setup the vlan interface
+         * on the host */
+        if (isVlan && !(link && vlanid &&
+                    virAsprintf(&hostdev->source.caps.u.net.iface,
+                                "%s.%s", link, vlanid) >= 0))
+            goto error;
+
         if (VIR_EXPAND_N(def->hostdevs, def->nhostdevs, 1) < 0)
             goto error;
         def->hostdevs[def->nhostdevs - 1] = hostdev;
@@ -645,6 +656,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
     char *mac = NULL;
     char *flag = NULL;
     char *macvlanmode = NULL;
+    char *vlanid = NULL;
     bool nonetwork = true;
     int status;
 
@@ -655,7 +667,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
             if (STREQ(property->key, "lxc.network.type")) {
                 /* Store the previous NIC */
                 status = lxcAddNetworkDefinition(def, type, link, mac, flag,
-                                                 macvlanmode);
+                                                 macvlanmode, vlanid);
                 if (status < 0)
                     return -1;
                 else if (status > 0)
@@ -667,6 +679,7 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
                 mac = NULL;
                 flag = NULL;
                 macvlanmode = NULL;
+                vlanid = NULL;
 
                 /* Keep the new value */
                 type = property->value;
@@ -679,11 +692,13 @@ lxcConvertNetworkSettings(virDomainDefPtr def, virPropertiesPtr properties)
                 flag = property->value;
             else if (STREQ(property->key, "lxc.network.macvlan.mode"))
                 macvlanmode = property->value;
+            else if (STREQ(property->key, "lxc.network.vlan.id"))
+                vlanid = property->value;
         }
 
         /* Add the last network definition found */
         status = lxcAddNetworkDefinition(def, type, link, mac, flag,
-                                         macvlanmode);
+                                         macvlanmode, vlanid);
         if (status < 0)
             return -1;
         else if (status > 0)
diff --git a/tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.config b/tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.config
new file mode 100644
index 0000000..574e975
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.config
@@ -0,0 +1,13 @@
+# Template used to create this container: opensuse
+# Template script checksum (SHA-1): 27307e0a95bd81b2c0bd82d6f87fdbe83be075ef
+
+lxc.network.type = vlan
+lxc.network.flags = up
+lxc.network.link = eth0
+lxc.network.hwaddr = 02:00:15:8f:05:c1
+lxc.network.vlan.id = 2
+
+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-vlannetwork.xml b/tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.xml
new file mode 100644
index 0000000..31e1513
--- /dev/null
+++ b/tests/lxcconf2xmldata/lxcconf2xml-vlannetwork.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>
+    <hostdev mode='capabilities' type='net'>
+      <source>
+        <interface>eth0.2</interface>
+      </source>
+    </hostdev>
+  </devices>
+</domain>
diff --git a/tests/lxcconf2xmltest.c b/tests/lxcconf2xmltest.c
index 60fdb01..86cbc3f 100644
--- a/tests/lxcconf2xmltest.c
+++ b/tests/lxcconf2xmltest.c
@@ -110,6 +110,7 @@ mymain(void)
     DO_TEST("nonetwork");
     DO_TEST("physnetwork");
     DO_TEST("macvlannetwork");
+    DO_TEST("vlannetwork");
     DO_TEST("idmap");
     DO_TEST("memtune");
     DO_TEST("cputune");
-- 
1.8.5.2




More information about the libvir-list mailing list