提交 0b33d7c9 编写于 作者: L Laine Stump

interface: clean up virInterfaceDefDevFormat

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.
上级 3aa81973
...@@ -41,7 +41,8 @@ VIR_ENUM_IMPL(virInterface, ...@@ -41,7 +41,8 @@ VIR_ENUM_IMPL(virInterface,
static virInterfaceDefPtr static virInterfaceDefPtr
virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType); virInterfaceDefParseXML(xmlXPathContextPtr ctxt, int parentIfType);
static int static int
virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def); virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def,
virInterfaceType parentIfType);
static static
void virInterfaceIpDefFree(virInterfaceIpDefPtr def) void virInterfaceIpDefFree(virInterfaceIpDefPtr def)
...@@ -870,7 +871,8 @@ virInterfaceBridgeDefFormat(virBufferPtr buf, const virInterfaceDef *def) ...@@ -870,7 +871,8 @@ virInterfaceBridgeDefFormat(virBufferPtr buf, const virInterfaceDef *def)
virBufferAdjustIndent(buf, 2); virBufferAdjustIndent(buf, 2);
for (i = 0; i < def->data.bridge.nbItf; i++) { 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; ret = -1;
} }
...@@ -932,7 +934,8 @@ virInterfaceBondDefFormat(virBufferPtr buf, const virInterfaceDef *def) ...@@ -932,7 +934,8 @@ virInterfaceBondDefFormat(virBufferPtr buf, const virInterfaceDef *def)
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }
for (i = 0; i < def->data.bond.nbItf; i++) { 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; ret = -1;
} }
...@@ -1035,7 +1038,8 @@ virInterfaceStartmodeDefFormat(virBufferPtr buf, ...@@ -1035,7 +1038,8 @@ virInterfaceStartmodeDefFormat(virBufferPtr buf,
} }
static int static int
virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def) virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def,
virInterfaceType parentIfType)
{ {
const char *type = NULL; const char *type = NULL;
...@@ -1063,39 +1067,33 @@ virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def) ...@@ -1063,39 +1067,33 @@ virInterfaceDefDevFormat(virBufferPtr buf, const virInterfaceDef *def)
virBufferAddLit(buf, ">\n"); virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2); 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) { switch (def->type) {
case VIR_INTERFACE_TYPE_ETHERNET: case VIR_INTERFACE_TYPE_ETHERNET:
virInterfaceStartmodeDefFormat(buf, def->startmode); if (def->mac)
if (def->mac != NULL)
virBufferAsprintf(buf, "<mac address='%s'/>\n", 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; break;
case VIR_INTERFACE_TYPE_BRIDGE: 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); virInterfaceBridgeDefFormat(buf, def);
break; break;
case VIR_INTERFACE_TYPE_BOND: 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); virInterfaceBondDefFormat(buf, def);
break; break;
case VIR_INTERFACE_TYPE_VLAN: 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); virInterfaceVlanDefFormat(buf, def);
break; break;
} }
...@@ -1116,7 +1114,7 @@ char *virInterfaceDefFormat(const virInterfaceDef *def) ...@@ -1116,7 +1114,7 @@ char *virInterfaceDefFormat(const virInterfaceDef *def)
{ {
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
if (virInterfaceDefDevFormat(&buf, def) < 0) { if (virInterfaceDefDevFormat(&buf, def, VIR_INTERFACE_TYPE_LAST) < 0) {
virBufferFreeAndReset(&buf); virBufferFreeAndReset(&buf);
return NULL; return NULL;
} }
......
<interface type='bond' name='bond0'> <interface type='bond' name='bond0'>
<start mode='none'/> <start mode='none'/>
<link speed='1000' state='up'/>
<protocol family='ipv4'> <protocol family='ipv4'>
<ip address='192.168.50.7' prefix='24'/> <ip address='192.168.50.7' prefix='24'/>
<route gateway='192.168.50.1'/> <route gateway='192.168.50.1'/>
</protocol> </protocol>
<link speed='1000' state='up'/>
<bond mode='active-backup'> <bond mode='active-backup'>
<miimon freq='100' updelay='10' carrier='ioctl'/> <miimon freq='100' updelay='10' carrier='ioctl'/>
<interface type='ethernet' name='eth1'> <interface type='ethernet' name='eth1'>
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
<mtu size='1500'/> <mtu size='1500'/>
<bridge stp='off'> <bridge stp='off'>
<interface type='ethernet' name='eth0'> <interface type='ethernet' name='eth0'>
<mac address='ab:bb:cc:dd:ee:ff'/>
<link speed='1000' state='up'/> <link speed='1000' state='up'/>
<mac address='ab:bb:cc:dd:ee:ff'/>
</interface> </interface>
<interface type='ethernet' name='eth1'> <interface type='ethernet' name='eth1'>
</interface> </interface>
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
</protocol> </protocol>
<bridge stp='off' delay='0.01'> <bridge stp='off' delay='0.01'>
<interface type='ethernet' name='eth0'> <interface type='ethernet' name='eth0'>
<mac address='ab:bb:cc:dd:ee:ff'/>
<link speed='10'/> <link speed='10'/>
<mac address='ab:bb:cc:dd:ee:ff'/>
</interface> </interface>
<interface type='ethernet' name='eth1'> <interface type='ethernet' name='eth1'>
</interface> </interface>
......
<interface type='ethernet' name='eth0'> <interface type='ethernet' name='eth0'>
<start mode='none'/> <start mode='none'/>
<mac address='aa:bb:cc:dd:ee:ff'/>
<link state='down'/>
<mtu size='1492'/> <mtu size='1492'/>
<protocol family='ipv4'> <protocol family='ipv4'>
<dhcp peerdns='no'/> <dhcp peerdns='no'/>
</protocol> </protocol>
<link state='down'/>
<mac address='aa:bb:cc:dd:ee:ff'/>
</interface> </interface>
<interface type='vlan' name='eth0.42'> <interface type='vlan' name='eth0.42'>
<start mode='onboot'/> <start mode='onboot'/>
<link state='lowerlayerdown'/>
<protocol family='ipv4'> <protocol family='ipv4'>
<dhcp peerdns='no'/> <dhcp peerdns='no'/>
</protocol> </protocol>
<link state='lowerlayerdown'/>
<vlan tag='42'> <vlan tag='42'>
<interface name='eth0'/> <interface name='eth0'/>
</vlan> </vlan>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册