conf: normalize hostdev <driver> parsing to simplify adding new attr

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@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Laine Stump 2024-01-04 20:12:51 -05:00
parent a435e7e6c8
commit e7d31d8b00
5 changed files with 49 additions and 40 deletions

View File

@ -6283,13 +6283,14 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
if (virDomainHostdevSubsysPCIDefParseXML(sourcenode, ctxt, def, flags) < 0)
return -1;
driver_node = virXPathNode("./driver", ctxt);
if ((driver_node = virXPathNode("./driver", ctxt))) {
if (virXMLPropEnum(driver_node, "name",
virDeviceHostdevPCIDriverNameTypeFromString,
VIR_XML_PROP_NONZERO,
&pcisrc->backend) < 0)
&pcisrc->backend) < 0) {
return -1;
}
}
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
@ -23423,14 +23424,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_NAME_DEFAULT) {
const char *backend = virDeviceHostdevPCIDriverNameTypeToString(pcisrc->backend);
@ -23441,9 +23439,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 &&

View File

@ -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 */
@ -2329,6 +2324,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);
@ -2359,8 +2355,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) {
@ -2369,8 +2364,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;

View File

@ -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.

View File

@ -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"));
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,13 @@ virNetworkPortDefFormatBuf(virBuffer *buf,
}
virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 2);
if (def->plug.hostdevpci.driver)
virBufferEscapeString(buf, "<driver name='%s'/>\n",
virNetworkForwardDriverNameTypeToString(
def->plug.hostdevpci.driver));
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);

View File

@ -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;