The hostdev version of the <driver> subelement appears in four places:
* The domain XML in the <hostdev> and <interface type='hostdev'>
elements (that's 2)
* The network XML inside <forward> when the network is a pool of
SRIOV VFs
* the <networkport> XML, which is used to communicate between the
hypervisor driver and network driver.
In order to make the pending addition of a new attribute to <driver>
in all these cases simpler, this patch refactors the parsing of
<driver> in all four places to use virXMLProp*() and
virXMLFormatElement().
Making all of the different instances of the separate parse/format for
<driver> look nearly identical will make it easier to see that the
upcoming patch that converges all four to use a common
parser/formatter is a functional NOP.
Signed-off-by: Laine Stump <laine(a)redhat.com>
---
src/conf/domain_conf.c | 28 ++++++++++++++++------------
src/conf/network_conf.c | 26 ++++++++++++--------------
src/conf/network_conf.h | 2 +-
src/conf/virnetworkportdef.c | 28 ++++++++++++++++++----------
src/conf/virnetworkportdef.h | 2 +-
5 files changed, 48 insertions(+), 38 deletions(-)
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 523481c92c..6dde17ded3 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -6267,13 +6267,14 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
if (virDomainHostdevSubsysPCIDefParseXML(sourcenode, ctxt, def, flags) < 0)
return -1;
- driver_node = virXPathNode("./driver", ctxt);
- if (virXMLPropEnum(driver_node, "name",
- virDeviceHostdevPCIDriverTypeFromString,
- VIR_XML_PROP_NONZERO,
- &pcisrc->backend) < 0)
- return -1;
-
+ if ((driver_node = virXPathNode("./driver", ctxt))) {
+ if (virXMLPropEnum(driver_node, "name",
+ virDeviceHostdevPCIDriverTypeFromString,
+ VIR_XML_PROP_NONZERO,
+ &pcisrc->backend) < 0) {
+ return -1;
+ }
+ }
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
@@ -23327,14 +23328,11 @@ virDomainHostdevDefFormatSubsysPCI(virBuffer *buf,
unsigned int flags,
bool includeTypeInAddr)
{
+ g_auto(virBuffer) driverAttrBuf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) sourceAttrBuf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) sourceChildBuf = VIR_BUFFER_INIT_CHILD(buf);
virDomainHostdevSubsysPCI *pcisrc = &def->source.subsys.u.pci;
- if (def->writeFiltering != VIR_TRISTATE_BOOL_ABSENT)
- virBufferAsprintf(&sourceAttrBuf, "
writeFiltering='%s'",
- virTristateBoolTypeToString(def->writeFiltering));
-
if (pcisrc->backend != VIR_DEVICE_HOSTDEV_PCI_DRIVER_TYPE_DEFAULT) {
const char *backend
= virDeviceHostdevPCIDriverTypeToString(pcisrc->backend);
@@ -23346,9 +23344,15 @@ virDomainHostdevDefFormatSubsysPCI(virBuffer *buf,
return -1;
}
- virBufferAsprintf(buf, "<driver name='%s'/>\n",
backend);
+ virBufferAsprintf(&driverAttrBuf, " name='%s'", backend);
}
+ virXMLFormatElement(buf, "driver", &driverAttrBuf, NULL);
+
+ if (def->writeFiltering != VIR_TRISTATE_BOOL_ABSENT)
+ virBufferAsprintf(&sourceAttrBuf, "
writeFiltering='%s'",
+ virTristateBoolTypeToString(def->writeFiltering));
+
virPCIDeviceAddressFormat(&sourceChildBuf, pcisrc->addr, includeTypeInAddr);
if (pcisrc->origstates &&
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 1a6fd86180..b9601cb307 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1334,8 +1334,8 @@ virNetworkForwardDefParseXML(const char *networkName,
g_autofree xmlNodePtr *forwardNatNodes = NULL;
g_autofree char *forwardDev = NULL;
g_autofree char *forwardManaged = NULL;
- g_autofree char *forwardDriverName = NULL;
g_autofree char *type = NULL;
+ xmlNodePtr driverNode = NULL;
VIR_XPATH_NODE_AUTORESTORE(ctxt)
ctxt->node = node;
@@ -1356,18 +1356,13 @@ virNetworkForwardDefParseXML(const char *networkName,
def->managed = true;
}
- forwardDriverName = virXPathString("string(./driver/@name)", ctxt);
- if (forwardDriverName) {
- int driverName
- = virNetworkForwardDriverNameTypeFromString(forwardDriverName);
-
- if (driverName <= 0) {
- virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
- _("Unknown forward <driver name='%1$s'/> in
network %2$s"),
- forwardDriverName, networkName);
+ if ((driverNode = virXPathNode("./driver", ctxt))) {
+ if (virXMLPropEnum(driverNode, "name",
+ virNetworkForwardDriverNameTypeFromString,
+ VIR_XML_PROP_NONZERO,
+ &def->driverName) < 0) {
return -1;
}
- def->driverName = driverName;
}
/* bridge and hostdev modes can use a pool of physical interfaces */
@@ -2331,6 +2326,7 @@ virNetworkDefFormatBuf(virBuffer *buf,
if (def->forward.type != VIR_NETWORK_FORWARD_NONE) {
const char *dev = NULL;
const char *mode = virNetworkForwardTypeToString(def->forward.type);
+ g_auto(virBuffer) driverAttrBuf = VIR_BUFFER_INITIALIZER;
if (!def->forward.npfs)
dev = virNetworkDefForwardIf(def, 0);
@@ -2361,8 +2357,7 @@ virNetworkDefFormatBuf(virBuffer *buf,
virBufferAsprintf(buf, "%s>\n", shortforward ? "/" :
"");
virBufferAdjustIndent(buf, 2);
- if (def->forward.driverName
- != VIR_NETWORK_FORWARD_DRIVER_NAME_DEFAULT) {
+ if (def->forward.driverName) {
const char *driverName
= virNetworkForwardDriverNameTypeToString(def->forward.driverName);
if (!driverName) {
@@ -2371,8 +2366,11 @@ virNetworkDefFormatBuf(virBuffer *buf,
def->forward.driverName);
return -1;
}
- virBufferAsprintf(buf, "<driver name='%s'/>\n",
driverName);
+ virBufferAsprintf(&driverAttrBuf, " name='%s'",
driverName);
}
+
+ virXMLFormatElement(buf, "driver", &driverAttrBuf, NULL);
+
if (def->forward.type == VIR_NETWORK_FORWARD_NAT) {
if (virNetworkForwardNatDefFormat(buf, &def->forward) < 0)
return -1;
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index 5a1bdb1284..497ae765f2 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -216,7 +216,7 @@ typedef struct _virNetworkForwardDef virNetworkForwardDef;
struct _virNetworkForwardDef {
int type; /* One of virNetworkForwardType constants */
bool managed; /* managed attribute for hostdev mode */
- int driverName; /* enum virNetworkForwardDriverNameType */
+ virNetworkForwardDriverNameType driverName;
/* If there are multiple forward devices (i.e. a pool of
* interfaces), they will be listed here.
diff --git a/src/conf/virnetworkportdef.c b/src/conf/virnetworkportdef.c
index fc06ef41d5..402c0051ec 100644
--- a/src/conf/virnetworkportdef.c
+++ b/src/conf/virnetworkportdef.c
@@ -87,10 +87,10 @@ virNetworkPortDefParseXML(xmlXPathContextPtr ctxt)
xmlNodePtr addressNode;
xmlNodePtr rxfiltersNode = NULL;
xmlNodePtr plugNode = NULL;
+ xmlNodePtr driverNode = NULL;
g_autofree char *mac = NULL;
g_autofree char *macmgr = NULL;
g_autofree char *mode = NULL;
- g_autofree char *driver = NULL;
def = g_new0(virNetworkPortDef, 1);
@@ -223,14 +223,16 @@ virNetworkPortDefParseXML(xmlXPathContextPtr ctxt)
VIR_XML_PROP_NONE,
&def->plug.hostdevpci.managed) < 0)
return NULL;
- driver = virXPathString("string(./plug/driver/@name)", ctxt);
- if (driver &&
- (def->plug.hostdevpci.driver =
- virNetworkForwardDriverNameTypeFromString(driver)) <= 0) {
- virReportError(VIR_ERR_XML_ERROR, "%s",
- _("Missing network port driver name"));
- return NULL;
+
+ if ((driverNode = virXPathNode("./plug/driver", ctxt))) {
+ if (virXMLPropEnum(driverNode, "name",
+ virNetworkForwardDriverNameTypeFromString,
+ VIR_XML_PROP_NONZERO,
+ &def->plug.hostdevpci.driver) < 0) {
+ return NULL;
+ }
}
+
if (!(addressNode = virXPathNode("./plug/address", ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("Missing network port PCI address"));
@@ -319,6 +321,8 @@ virNetworkPortDefFormatBuf(virBuffer *buf,
virTristateBoolTypeToString(def->trustGuestRxFilters));
if (def->plugtype != VIR_NETWORK_PORT_PLUG_TYPE_NONE) {
+ g_auto(virBuffer) driverAttrBuf = VIR_BUFFER_INITIALIZER;
+
virBufferAsprintf(buf, "<plug type='%s'",
virNetworkPortPlugTypeToString(def->plugtype));
@@ -351,10 +355,14 @@ virNetworkPortDefFormatBuf(virBuffer *buf,
}
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
- if (def->plug.hostdevpci.driver)
- virBufferEscapeString(buf, "<driver
name='%s'/>\n",
+
+ if (def->plug.hostdevpci.driver) {
+ virBufferEscapeString(&driverAttrBuf, "
name='%s'",
virNetworkForwardDriverNameTypeToString(
def->plug.hostdevpci.driver));
+ }
+
+ virXMLFormatElement(buf, "driver", &driverAttrBuf, NULL);
virPCIDeviceAddressFormat(buf, def->plug.hostdevpci.addr, false);
virBufferAdjustIndent(buf, -2);
diff --git a/src/conf/virnetworkportdef.h b/src/conf/virnetworkportdef.h
index 48e73dbefd..bfe1dae9ea 100644
--- a/src/conf/virnetworkportdef.h
+++ b/src/conf/virnetworkportdef.h
@@ -69,7 +69,7 @@ struct _virNetworkPortDef {
} direct;
struct {
virPCIDeviceAddress addr; /* PCI Address of device */
- int driver; /* virNetworkForwardDriverNameType */
+ unsigned int driver; /* virNetworkForwardDriverNameType */
virTristateBool managed;
} hostdevpci;
} plug;
--
2.41.0