conf: node_device: refactor mdev_types XML parsing

Extract PCI code from virNodeDevPCICapMdevTypesParseXML to make
method virNodeDevCapMdevTypesParseXML generic for later reuse.

Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Boris Fiuczynski 2020-10-23 19:31:48 +02:00 committed by Ján Tomko
parent 9f0d3981ce
commit c218c6ec7a

View File

@ -768,6 +768,72 @@ virNodeDevCapDRMParseXML(xmlXPathContextPtr ctxt,
}
static int
virNodeDevCapMdevTypesParseXML(xmlXPathContextPtr ctxt,
virMediatedDeviceTypePtr **mdev_types,
size_t *nmdev_types)
{
int ret = -1;
xmlNodePtr orignode = NULL;
xmlNodePtr *nodes = NULL;
int ntypes = -1;
virMediatedDeviceTypePtr type = NULL;
size_t i;
if ((ntypes = virXPathNodeSet("./type", ctxt, &nodes)) < 0)
goto cleanup;
if (nmdev_types == 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing <type> element in <capability> element"));
goto cleanup;
}
orignode = ctxt->node;
for (i = 0; i < ntypes; i++) {
ctxt->node = nodes[i];
type = g_new0(virMediatedDeviceType, 1);
if (!(type->id = virXPathString("string(./@id[1])", ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing 'id' attribute for mediated device's "
"<type> element"));
goto cleanup;
}
if (!(type->device_api = virXPathString("string(./deviceAPI[1])", ctxt))) {
virReportError(VIR_ERR_XML_ERROR,
_("missing device API for mediated device type '%s'"),
type->id);
goto cleanup;
}
if (virXPathUInt("number(./availableInstances)", ctxt,
&type->available_instances) < 0) {
virReportError(VIR_ERR_XML_ERROR,
_("missing number of available instances for "
"mediated device type '%s'"),
type->id);
goto cleanup;
}
type->name = virXPathString("string(./name)", ctxt);
if (VIR_APPEND_ELEMENT(*mdev_types,
*nmdev_types, type) < 0)
goto cleanup;
}
ret = 0;
cleanup:
VIR_FREE(nodes);
virMediatedDeviceTypeFree(type);
ctxt->node = orignode;
return ret;
}
static int
virNodeDevCapCCWParseXML(xmlXPathContextPtr ctxt,
virNodeDeviceDefPtr def,
@ -1532,72 +1598,6 @@ virNodeDevPCICapSRIOVVirtualParseXML(xmlXPathContextPtr ctxt,
}
static int
virNodeDevPCICapMdevTypesParseXML(xmlXPathContextPtr ctxt,
virNodeDevCapPCIDevPtr pci_dev)
{
int ret = -1;
xmlNodePtr orignode = NULL;
xmlNodePtr *nodes = NULL;
int nmdev_types = -1;
virMediatedDeviceTypePtr type = NULL;
size_t i;
if ((nmdev_types = virXPathNodeSet("./type", ctxt, &nodes)) < 0)
goto cleanup;
if (nmdev_types == 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing <type> element in <capability> element"));
goto cleanup;
}
orignode = ctxt->node;
for (i = 0; i < nmdev_types; i++) {
ctxt->node = nodes[i];
type = g_new0(virMediatedDeviceType, 1);
if (!(type->id = virXPathString("string(./@id[1])", ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing 'id' attribute for mediated device's "
"<type> element"));
goto cleanup;
}
if (!(type->device_api = virXPathString("string(./deviceAPI[1])", ctxt))) {
virReportError(VIR_ERR_XML_ERROR,
_("missing device API for mediated device type '%s'"),
type->id);
goto cleanup;
}
if (virXPathUInt("number(./availableInstances)", ctxt,
&type->available_instances) < 0) {
virReportError(VIR_ERR_XML_ERROR,
_("missing number of available instances for "
"mediated device type '%s'"),
type->id);
goto cleanup;
}
type->name = virXPathString("string(./name)", ctxt);
if (VIR_APPEND_ELEMENT(pci_dev->mdev_types,
pci_dev->nmdev_types, type) < 0)
goto cleanup;
}
pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;
ret = 0;
cleanup:
VIR_FREE(nodes);
virMediatedDeviceTypeFree(type);
ctxt->node = orignode;
return ret;
}
static int
virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt,
xmlNodePtr node,
@ -1620,9 +1620,12 @@ virNodeDevPCICapabilityParseXML(xmlXPathContextPtr ctxt,
} else if (STREQ(type, "virt_functions") &&
virNodeDevPCICapSRIOVVirtualParseXML(ctxt, pci_dev) < 0) {
goto cleanup;
} else if (STREQ(type, "mdev_types") &&
virNodeDevPCICapMdevTypesParseXML(ctxt, pci_dev) < 0) {
goto cleanup;
} else if (STREQ(type, "mdev_types")) {
if (virNodeDevCapMdevTypesParseXML(ctxt,
&pci_dev->mdev_types,
&pci_dev->nmdev_types) < 0)
goto cleanup;
pci_dev->flags |= VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;
} else {
int hdrType = virPCIHeaderTypeFromString(type);