[libvirt] [PATCH 5/5] interface: clean up virInterfaceDefDevFormat

Laine Stump laine at laine.org
Thu Jun 19 10:48:35 UTC 2014


This modifies the formatting function of virInterface to be a proper
mirror of the parse function, including the addition of a
"parentIfType" arg so that we can decide whether or not it is
appropriate to emit the elements that are only in toplevel interfaces,
as well as the <link> element (which isn't allowed for bridge
interfaces).

Since the restructuring of the code necessarily changes the order of
some of the elements, some test case data had to be updated.
---
 src/conf/interface_conf.c                       | 52 ++++++++++++-------------
 tests/interfaceschemadata/bond.xml              |  2 +-
 tests/interfaceschemadata/bridge-no-address.xml |  2 +-
 tests/interfaceschemadata/bridge.xml            |  2 +-
 tests/interfaceschemadata/ethernet-dhcp.xml     |  4 +-
 tests/interfaceschemadata/vlan.xml              |  2 +-
 6 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c
index 883053f..141b4a2 100644
--- a/src/conf/interface_conf.c
+++ b/src/conf/interface_conf.c
@@ -41,7 +41,8 @@ VIR_ENUM_IMPL(virInterface,
 static virInterfaceDefPtr
 virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType);
 static int
-virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def);
+virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def,
+                         virInterfaceType parentIfType);
 
 static
 void virInterfaceIpDefFree(virInterfaceIpDefPtr def)
@@ -870,7 +871,8 @@ virInterfaceBridgeDefFormat(virBufferPtr buf, const virInterfaceDef *def)
     virBufferAdjustIndent(buf, 2);
 
     for (i = 0; i < def->data.bridge.nbItf; i++) {
-        if (virInterfaceDefDevFormat(buf, def->data.bridge.itf[i]) < 0)
+        if (virInterfaceDefDevFormat(buf, def->data.bridge.itf[i],
+                                     VIR_INTERFACE_TYPE_BRIDGE) < 0)
             ret = -1;
     }
 
@@ -932,7 +934,8 @@ virInterfaceBondDefFormat(virBufferPtr buf, const virInterfaceDef *def)
         virBufferAddLit(buf, "/>\n");
     }
     for (i = 0; i < def->data.bond.nbItf; i++) {
-        if (virInterfaceDefDevFormat(buf, def->data.bond.itf[i]) < 0)
+        if (virInterfaceDefDevFormat(buf, def->data.bond.itf[i],
+                                     VIR_INTERFACE_TYPE_BOND) < 0)
             ret = -1;
     }
 
@@ -1035,7 +1038,8 @@ virInterfaceStartmodeDefFormat(virBufferPtr buf,
 }
 
 static int
-virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def)
+virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def,
+                         virInterfaceType parentIfType)
 {
     const char *type = NULL;
 
@@ -1063,39 +1067,33 @@ virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def)
     virBufferAddLit(buf, ">\n");
     virBufferAdjustIndent(buf, 2);
 
+    if (parentIfType == VIR_INTERFACE_TYPE_LAST) {
+        /* these elements are only valid on top-level interfaces - IP
+         * address info ("protocol") only makes sense for the
+         * top-level, and subordinate interfaces inherit the toplevel
+         * setting for mtu and start mode, which cannot be overridden.
+         */
+        virInterfaceStartmodeDefFormat(buf, def->startmode);
+        if (def->mtu)
+            virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
+        virInterfaceProtocolDefFormat(buf, def);
+    }
+
+    if (def->type != VIR_INTERFACE_TYPE_BRIDGE) {
+        virInterfaceLinkFormat(buf, &def->lnk);
+    }
     switch (def->type) {
         case VIR_INTERFACE_TYPE_ETHERNET:
-            virInterfaceStartmodeDefFormat(buf, def->startmode);
-            if (def->mac != NULL)
+            if (def->mac)
                 virBufferAsprintf(buf, "<mac address='%s'/>\n", def->mac);
-            virInterfaceLinkFormat(buf, &def->lnk);
-            if (def->mtu != 0)
-                virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
-            virInterfaceProtocolDefFormat(buf, def);
             break;
         case VIR_INTERFACE_TYPE_BRIDGE:
-            virInterfaceStartmodeDefFormat(buf, def->startmode);
-            if (def->mtu != 0)
-                virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
-            virInterfaceProtocolDefFormat(buf, def);
             virInterfaceBridgeDefFormat(buf, def);
             break;
         case VIR_INTERFACE_TYPE_BOND:
-            virInterfaceStartmodeDefFormat(buf, def->startmode);
-            virInterfaceLinkFormat(buf, &def->lnk);
-            if (def->mtu != 0)
-                virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
-            virInterfaceProtocolDefFormat(buf, def);
             virInterfaceBondDefFormat(buf, def);
             break;
         case VIR_INTERFACE_TYPE_VLAN:
-            virInterfaceStartmodeDefFormat(buf, def->startmode);
-            if (def->mac != NULL)
-                virBufferAsprintf(buf, "<mac address='%s'/>\n", def->mac);
-            virInterfaceLinkFormat(buf, &def->lnk);
-            if (def->mtu != 0)
-                virBufferAsprintf(buf, "<mtu size='%d'/>\n", def->mtu);
-            virInterfaceProtocolDefFormat(buf, def);
             virInterfaceVlanDefFormat(buf, def);
             break;
     }
@@ -1116,7 +1114,7 @@ char *virInterfaceDefFormat(const virInterfaceDef *def)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
 
-    if (virInterfaceDefDevFormat(&buf, def) < 0) {
+    if (virInterfaceDefDevFormat(&buf, def, VIR_INTERFACE_TYPE_LAST) < 0) {
         virBufferFreeAndReset(&buf);
         return NULL;
     }
diff --git a/tests/interfaceschemadata/bond.xml b/tests/interfaceschemadata/bond.xml
index cbc1dfa..3d24bea 100644
--- a/tests/interfaceschemadata/bond.xml
+++ b/tests/interfaceschemadata/bond.xml
@@ -1,10 +1,10 @@
 <interface type='bond' name='bond0'>
   <start mode='none'/>
-  <link speed='1000' state='up'/>
   <protocol family='ipv4'>
     <ip address='192.168.50.7' prefix='24'/>
     <route gateway='192.168.50.1'/>
   </protocol>
+  <link speed='1000' state='up'/>
   <bond mode='active-backup'>
     <miimon freq='100' updelay='10' carrier='ioctl'/>
     <interface type='ethernet' name='eth1'>
diff --git a/tests/interfaceschemadata/bridge-no-address.xml b/tests/interfaceschemadata/bridge-no-address.xml
index 68b8c94..fd6c1a7 100644
--- a/tests/interfaceschemadata/bridge-no-address.xml
+++ b/tests/interfaceschemadata/bridge-no-address.xml
@@ -3,8 +3,8 @@
   <mtu size='1500'/>
   <bridge stp='off'>
     <interface type='ethernet' name='eth0'>
-      <mac address='ab:bb:cc:dd:ee:ff'/>
       <link speed='1000' state='up'/>
+      <mac address='ab:bb:cc:dd:ee:ff'/>
     </interface>
     <interface type='ethernet' name='eth1'>
     </interface>
diff --git a/tests/interfaceschemadata/bridge.xml b/tests/interfaceschemadata/bridge.xml
index c865116..ece087e 100644
--- a/tests/interfaceschemadata/bridge.xml
+++ b/tests/interfaceschemadata/bridge.xml
@@ -6,8 +6,8 @@
   </protocol>
   <bridge stp='off' delay='0.01'>
     <interface type='ethernet' name='eth0'>
-      <mac address='ab:bb:cc:dd:ee:ff'/>
       <link speed='10'/>
+      <mac address='ab:bb:cc:dd:ee:ff'/>
     </interface>
     <interface type='ethernet' name='eth1'>
     </interface>
diff --git a/tests/interfaceschemadata/ethernet-dhcp.xml b/tests/interfaceschemadata/ethernet-dhcp.xml
index c124372..9a8a160 100644
--- a/tests/interfaceschemadata/ethernet-dhcp.xml
+++ b/tests/interfaceschemadata/ethernet-dhcp.xml
@@ -1,9 +1,9 @@
 <interface type='ethernet' name='eth0'>
   <start mode='none'/>
-  <mac address='aa:bb:cc:dd:ee:ff'/>
-  <link state='down'/>
   <mtu size='1492'/>
   <protocol family='ipv4'>
     <dhcp peerdns='no'/>
   </protocol>
+  <link state='down'/>
+  <mac address='aa:bb:cc:dd:ee:ff'/>
 </interface>
diff --git a/tests/interfaceschemadata/vlan.xml b/tests/interfaceschemadata/vlan.xml
index 6432b96..3013777 100644
--- a/tests/interfaceschemadata/vlan.xml
+++ b/tests/interfaceschemadata/vlan.xml
@@ -1,9 +1,9 @@
 <interface type='vlan' name='eth0.42'>
   <start mode='onboot'/>
-  <link state='lowerlayerdown'/>
   <protocol family='ipv4'>
     <dhcp peerdns='no'/>
   </protocol>
+  <link state='lowerlayerdown'/>
   <vlan tag='42'>
     <interface name='eth0'/>
   </vlan>
-- 
1.9.3




More information about the libvir-list mailing list