]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: normalize hostdev <driver> parsing to simplify adding new attr
authorLaine Stump <laine@redhat.com>
Fri, 5 Jan 2024 01:12:51 +0000 (20:12 -0500)
committerLaine Stump <laine@redhat.com>
Mon, 8 Jan 2024 04:57:09 +0000 (23:57 -0500)
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>
src/conf/domain_conf.c
src/conf/network_conf.c
src/conf/network_conf.h
src/conf/virnetworkportdef.c
src/conf/virnetworkportdef.h

index f0e131797a72bbc18b7519cbea5ae992c99ffac3..d09a0af9a5a8e566fba1c981afb6f859d4b4063b 100644 (file)
@@ -6283,13 +6283,14 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
         if (virDomainHostdevSubsysPCIDefParseXML(sourcenode, ctxt, def, flags) < 0)
             return -1;
 
-        driver_node = virXPathNode("./driver", ctxt);
-        if (virXMLPropEnum(driver_node, "name",
-                           virDeviceHostdevPCIDriverNameTypeFromString,
-                           VIR_XML_PROP_NONZERO,
-                           &pcisrc->backend) < 0)
-            return -1;
-
+        if ((driver_node = virXPathNode("./driver", ctxt))) {
+            if (virXMLPropEnum(driver_node, "name",
+                               virDeviceHostdevPCIDriverNameTypeFromString,
+                               VIR_XML_PROP_NONZERO,
+                               &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 &&
index 0449b6f07c7325650d336df63e5c0add0928530a..984ff564b2dce2fee1fa41b59c06c2f92dc11a75 100644 (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;
index 5a1bdb12845755e8bd9f54129f153454972fc9dc..497ae765f26839904f945be97be15425a664ef35 100644 (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.
index fc06ef41d5f62ab1ef2ac8467549c54117cadbf4..ac17313f834eb8c4abdc8a7061738a9641a99039 100644 (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"));
-            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,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);
index 48e73dbefde9b9ab8b1befc476e8cb7377424726..bfe1dae9eadda600b8ddb74cb0cf0ac420e133d2 100644 (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;