[libvirt] [PATCH] Parse ipaddr for bridge network types

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

 



# HG changeset patch
# User john.levon@xxxxxxx
# Date 1232562942 28800
# Node ID 14b7020610538fbaf0cd5db5e713abe5769de2af
# Parent  e0b0295722ecf4645983e3a9a090e0aaabc40da7
Parse ipaddr for bridge network types

As well as supporting round-tripping of the IP address, this fixes a
leak if ipaddr was specified in the Xen domain config.

Signed-off-by: John Levon <john.levon@xxxxxxx>

diff --git a/src/domain_conf.c b/src/domain_conf.c
--- a/src/domain_conf.c
+++ b/src/domain_conf.c
@@ -289,6 +289,7 @@ void virDomainNetDefFree(virDomainNetDef
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
         VIR_FREE(def->data.bridge.brname);
         VIR_FREE(def->data.bridge.script);
+        VIR_FREE(def->data.bridge.ipaddr);
         break;
     }
 
@@ -887,7 +888,8 @@ virDomainNetDefParseXML(virConnectPtr co
                 address = virXMLPropString(cur, "address");
                 port = virXMLPropString(cur, "port");
             } else if ((address == NULL) &&
-                       (def->type == VIR_DOMAIN_NET_TYPE_ETHERNET) &&
+                       (def->type == VIR_DOMAIN_NET_TYPE_ETHERNET ||
+                        def->type == VIR_DOMAIN_NET_TYPE_BRIDGE) &&
                        (xmlStrEqual(cur->name, BAD_CAST "ip"))) {
                 address = virXMLPropString(cur, "address");
             } else if ((ifname == NULL) &&
@@ -953,6 +955,10 @@ virDomainNetDefParseXML(virConnectPtr co
         if (script != NULL) {
             def->data.bridge.script = script;
             script = NULL;
+        }
+        if (address != NULL) {
+            def->data.bridge.ipaddr = address;
+            address = NULL;
         }
         break;
 
@@ -2889,6 +2895,9 @@ virDomainNetDefFormat(virConnectPtr conn
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
         virBufferEscapeString(buf, "      <source bridge='%s'/>\n",
                               def->data.bridge.brname);
+        if (def->data.bridge.ipaddr)
+            virBufferVSprintf(buf, "      <ip address='%s'/>\n",
+                              def->data.bridge.ipaddr);
         if (def->data.bridge.script)
             virBufferEscapeString(buf, "      <script path='%s'/>\n",
                                   def->data.bridge.script);
diff --git a/src/domain_conf.h b/src/domain_conf.h
--- a/src/domain_conf.h
+++ b/src/domain_conf.h
@@ -155,6 +155,7 @@ struct _virDomainNetDef {
         struct {
             char *brname;
             char *script;
+            char *ipaddr;
         } bridge;
     } data;
     char *ifname;
diff --git a/src/xend_internal.c b/src/xend_internal.c
--- a/src/xend_internal.c
+++ b/src/xend_internal.c
@@ -1756,10 +1756,18 @@ xenDaemonParseSxprNets(virConnectPtr con
                     net->type == VIR_DOMAIN_NET_TYPE_BRIDGE &&
                     !(net->data.bridge.script = strdup(tmp2)))
                     goto no_memory;
+                tmp = sexpr_node(node, "device/vif/ip");
+                if (tmp &&
+                    !(net->data.bridge.ipaddr = strdup(tmp)))
+                    goto no_memory;
             } else {
                 net->type = VIR_DOMAIN_NET_TYPE_ETHERNET;
                 if (tmp2 &&
                     !(net->data.ethernet.script = strdup(tmp2)))
+                    goto no_memory;
+                tmp = sexpr_node(node, "device/vif/ip");
+                if (tmp &&
+                    !(net->data.ethernet.ipaddr = strdup(tmp)))
                     goto no_memory;
             }
 
@@ -1793,11 +1801,6 @@ xenDaemonParseSxprNets(virConnectPtr con
                 net->mac[4] = mac[4];
                 net->mac[5] = mac[5];
             }
-
-            tmp = sexpr_node(node, "device/vif/ip");
-            if (tmp &&
-                !(net->data.ethernet.ipaddr = strdup(tmp)))
-                goto no_memory;
 
             if (model &&
                 !(net->model = strdup(model)))
@@ -5104,6 +5107,8 @@ xenDaemonFormatSxprNet(virConnectPtr con
             script = def->data.bridge.script;
 
         virBufferVSprintf(buf, "(script '%s')", script);
+        if (def->data.bridge.ipaddr != NULL)
+            virBufferVSprintf(buf, "(ip '%s')", def->data.bridge.ipaddr);
         break;
 
     case VIR_DOMAIN_NET_TYPE_NETWORK:
diff --git a/src/xm_internal.c b/src/xm_internal.c
--- a/src/xm_internal.c
+++ b/src/xm_internal.c
@@ -1071,6 +1071,9 @@ xenXMDomainConfigParse(virConnectPtr con
                 if (script[0] &&
                     !(net->data.bridge.script = strdup(script)))
                     goto no_memory;
+                if (ip[0] &&
+                    !(net->data.bridge.ipaddr = strdup(ip)))
+                    goto no_memory;
             } else {
                 if (script[0] &&
                     !(net->data.ethernet.script = strdup(script)))
@@ -1832,6 +1835,8 @@ static int xenXMDomainConfigFormatNet(vi
     switch (net->type) {
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
         virBufferVSprintf(&buf, ",bridge=%s", net->data.bridge.brname);
+        if (net->data.bridge.ipaddr)
+            virBufferVSprintf(&buf, ",ip=%s", net->data.bridge.ipaddr);
         break;
 
     case VIR_DOMAIN_NET_TYPE_ETHERNET:
diff --git a/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.sexpr b/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.sexpr
new file mode 100644
--- /dev/null
+++ b/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.sexpr
@@ -0,0 +1,2 @@
+(domain (domid 6)(name 'pvtest')(memory 420)(maxmem 420)(vcpus 2)(uuid '596a5d2171f48fb2e068e2386a5c413e')(on_poweroff 'destroy')(on_reboot 'destroy')(on_crash 'destroy')(image (linux (kernel '/var/lib/xen/vmlinuz.2Dn2YT')(ramdisk '/var/lib/xen/initrd.img.0u-Vhq')(args ' method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  ')))(device (vbd (dev 'xvda')(uname 'file:/root/some.img')(mode 'w')))(device (vif (mac '00:11:22:33:44:55')(bridge 'xenbr2')(script 'vif-bridge')(ip '192.0.2.1')))
+
diff --git a/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.xml b/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.xml
new file mode 100644
--- /dev/null
+++ b/tests/sexpr2xmldata/sexpr2xml-bridge-ipaddr.xml
@@ -0,0 +1,34 @@
+<domain type='xen' id='6'>
+  <name>pvtest</name>
+  <uuid>596a5d21-71f4-8fb2-e068-e2386a5c413e</uuid>
+  <memory>430080</memory>
+  <currentMemory>430080</currentMemory>
+  <vcpu>2</vcpu>
+  <os>
+    <type>linux</type>
+    <kernel>/var/lib/xen/vmlinuz.2Dn2YT</kernel>
+    <initrd>/var/lib/xen/initrd.img.0u-Vhq</initrd>
+    <cmdline> method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  </cmdline>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>destroy</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <driver name='file'/>
+      <source file='/root/some.img'/>
+      <target dev='xvda' bus='xen'/>
+    </disk>
+    <interface type='bridge'>
+      <mac address='00:11:22:33:44:55'/>
+      <source bridge='xenbr2'/>
+      <ip address='192.0.2.1'/>
+      <script path='vif-bridge'/>
+      <target dev='vif6.0'/>
+    </interface>
+    <console type='pty'>
+      <target port='0'/>
+    </console>
+  </devices>
+</domain>
diff --git a/tests/sexpr2xmltest.c b/tests/sexpr2xmltest.c
--- a/tests/sexpr2xmltest.c
+++ b/tests/sexpr2xmltest.c
@@ -117,6 +117,7 @@ mymain(int argc, char **argv)
     DO_TEST("net-routed", "net-routed", 2);
     DO_TEST("net-bridged", "net-bridged", 2);
     DO_TEST("net-e1000", "net-e1000", 2);
+    DO_TEST("bridge-ipaddr", "bridge-ipaddr", 3);
     DO_TEST("no-source-cdrom", "no-source-cdrom", 2);
 
     DO_TEST("fv-utc", "fv-utc", 1);
diff --git a/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.sexpr b/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.sexpr
new file mode 100644
--- /dev/null
+++ b/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.sexpr
@@ -0,0 +1,1 @@
+(vm (name 'pvtest')(memory 420)(maxmem 420)(vcpus 2)(uuid '596a5d21-71f4-8fb2-e068-e2386a5c413e')(on_poweroff 'destroy')(on_reboot 'destroy')(on_crash 'destroy')(image (linux (kernel '/var/lib/xen/vmlinuz.2Dn2YT')(ramdisk '/var/lib/xen/initrd.img.0u-Vhq')(args ' method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  ')))(device (vbd (dev 'xvda')(uname 'file:/root/some.img')(mode 'w')))(device (vif (mac '00:11:22:33:44:55')(bridge 'xenbr2')(script 'vif-bridge')(ip '192.0.2.1'))))
\ No newline at end of file
diff --git a/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.xml b/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.xml
new file mode 100644
--- /dev/null
+++ b/tests/xml2sexprdata/xml2sexpr-bridge-ipaddr.xml
@@ -0,0 +1,30 @@
+<domain type='xen' id='15'>
+  <name>pvtest</name>
+  <uuid>596a5d2171f48fb2e068e2386a5c413e</uuid>
+  <os>
+    <type>linux</type>
+    <kernel>/var/lib/xen/vmlinuz.2Dn2YT</kernel>
+    <initrd>/var/lib/xen/initrd.img.0u-Vhq</initrd>
+    <cmdline> method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  </cmdline>
+  </os>
+  <memory>430080</memory>
+  <vcpu>2</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>destroy</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <source file='/root/some.img'/>
+      <target dev='xvda'/>
+    </disk>
+    <interface type="bridge">
+      <mac address="00:11:22:33:44:55"/>
+      <source bridge="xenbr2"/>
+      <ip address="192.0.2.1"/>
+      <script path="vif-bridge"/>
+      <target dev="vif4.0"/>
+    </interface>
+    <console tty='/dev/pts/4'/>
+  </devices>
+</domain>
+
diff --git a/tests/xml2sexprtest.c b/tests/xml2sexprtest.c
--- a/tests/xml2sexprtest.c
+++ b/tests/xml2sexprtest.c
@@ -126,6 +126,7 @@ mymain(int argc, char **argv)
     DO_TEST("net-routed", "net-routed", "pvtest", 2);
     DO_TEST("net-bridged", "net-bridged", "pvtest", 2);
     DO_TEST("net-e1000", "net-e1000", "pvtest", 2);
+    DO_TEST("bridge-ipaddr", "bridge-ipaddr", "pvtest", 2);
     DO_TEST("no-source-cdrom", "no-source-cdrom", "test", 2);
 
     DO_TEST("fv-utc", "fv-utc", "fvtest", 1);

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]