]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: domain: Extract code for parsing and formatting iothread mapping definition
authorPeter Krempa <pkrempa@redhat.com>
Fri, 14 Feb 2025 11:13:02 +0000 (12:13 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 20 Feb 2025 14:21:44 +0000 (15:21 +0100)
The code will be also needed for 'virtio-scsi' controller definitions.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_conf.c

index c1ffa117ea415807b63a1136f4e247ee1f82ce27..5630a469beafbbc5df64a8b407b7db31c43de5f1 100644 (file)
@@ -7939,11 +7939,56 @@ virDomainDiskDefGeometryParse(virDomainDiskDef *def,
 
 
 static int
-virDomainDiskDefDriverParseXML(virDomainDiskDef *def,
-                               xmlNodePtr cur)
+virDomainIothreadMappingDefParse(xmlNodePtr driverNode,
+                                 GSList **iothreads)
 {
     xmlNodePtr iothreadsNode;
+    g_autoslist(virDomainIothreadMappingDef) ioth = NULL;
+    g_autoptr(GPtrArray) iothreadNodes = NULL;
+    size_t i;
+
+    if (!(iothreadsNode = virXMLNodeGetSubelement(driverNode, "iothreads")))
+        return 0;
+
+    if (!(iothreadNodes = virXMLNodeGetSubelementList(iothreadsNode, "iothread")))
+        return 0;
+
+    for (i = 0; i < iothreadNodes->len; i++) {
+        xmlNodePtr iothNode = g_ptr_array_index(iothreadNodes, i);
+        g_autoptr(virDomainIothreadMappingDef) iothdef = g_new0(virDomainIothreadMappingDef, 1);
+        g_autoptr(GPtrArray) queueNodes = NULL;
+
+        if (virXMLPropUInt(iothNode, "id", 10, VIR_XML_PROP_REQUIRED,
+                           &iothdef->id) < 0)
+            return -1;
+
+        if ((queueNodes = virXMLNodeGetSubelementList(iothNode, "queue"))) {
+            size_t q;
+
+            iothdef->queues = g_new0(unsigned int, queueNodes->len);
+            iothdef->nqueues = queueNodes->len;
+
+            for (q = 0; q < queueNodes->len; q++) {
+                xmlNodePtr queueNode = g_ptr_array_index(queueNodes, q);
+
+                if (virXMLPropUInt(queueNode, "id", 10, VIR_XML_PROP_REQUIRED,
+                                   &(iothdef->queues[q])) < 0)
+                    return -1;
+            }
+        }
 
+        ioth = g_slist_prepend(ioth, g_steal_pointer(&iothdef));
+    }
+
+    *iothreads = g_slist_reverse(g_steal_pointer(&ioth));
+    return 0;
+}
+
+
+static int
+virDomainDiskDefDriverParseXML(virDomainDiskDef *def,
+                               xmlNodePtr cur)
+{
     def->driverName = virXMLPropString(cur, "name");
 
     if (virXMLPropEnum(cur, "cache", virDomainDiskCacheTypeFromString,
@@ -7990,43 +8035,8 @@ virDomainDiskDefDriverParseXML(virDomainDiskDef *def,
     if (virXMLPropUInt(cur, "iothread", 10, VIR_XML_PROP_NONZERO, &def->iothread) < 0)
         return -1;
 
-    if ((iothreadsNode = virXMLNodeGetSubelement(cur, "iothreads"))) {
-        g_autoslist(virDomainIothreadMappingDef) ioth = NULL;
-        g_autoptr(GPtrArray) iothreadNodes = NULL;
-
-        if ((iothreadNodes = virXMLNodeGetSubelementList(iothreadsNode, "iothread"))) {
-            size_t i;
-
-            for (i = 0; i < iothreadNodes->len; i++) {
-                xmlNodePtr iothNode = g_ptr_array_index(iothreadNodes, i);
-                g_autoptr(virDomainIothreadMappingDef) iothdef = g_new0(virDomainIothreadMappingDef, 1);
-                g_autoptr(GPtrArray) queueNodes = NULL;
-
-                if (virXMLPropUInt(iothNode, "id", 10, VIR_XML_PROP_REQUIRED,
-                                   &iothdef->id) < 0)
-                    return -1;
-
-                if ((queueNodes = virXMLNodeGetSubelementList(iothNode, "queue"))) {
-                    size_t q;
-
-                    iothdef->queues = g_new0(unsigned int, queueNodes->len);
-                    iothdef->nqueues = queueNodes->len;
-
-                    for (q = 0; q < queueNodes->len; q++) {
-                        xmlNodePtr queueNode = g_ptr_array_index(queueNodes, q);
-
-                        if (virXMLPropUInt(queueNode, "id", 10, VIR_XML_PROP_REQUIRED,
-                                           &(iothdef->queues[q])) < 0)
-                            return -1;
-                    }
-                }
-
-                ioth = g_slist_prepend(ioth, g_steal_pointer(&iothdef));
-            }
-
-            def->iothreads = g_slist_reverse(g_steal_pointer(&ioth));
-        }
-    }
+    if (virDomainIothreadMappingDefParse(cur, &def->iothreads) < 0)
+        return -1;
 
     if (virXMLPropEnum(cur, "detect_zeroes",
                        virDomainDiskDetectZeroesTypeFromString,
@@ -23152,6 +23162,37 @@ virDomainDiskDefFormatIotune(virBuffer *buf,
 #undef FORMAT_IOTUNE
 
 
+static void
+virDomainIothreadMappingDefFormat(virBuffer *buf,
+                                  GSList *iothreads)
+{
+    g_auto(virBuffer) iothreadsChildBuf = VIR_BUFFER_INIT_CHILD(buf);
+    GSList *n;
+
+    if (!iothreads)
+        return;
+
+    for (n = iothreads; n; n = n->next) {
+        virDomainIothreadMappingDef *iothDef = n->data;
+        g_auto(virBuffer) iothreadAttrBuf = VIR_BUFFER_INITIALIZER;
+        g_auto(virBuffer) iothreadChildBuf = VIR_BUFFER_INIT_CHILD(&iothreadsChildBuf);
+
+        virBufferAsprintf(&iothreadAttrBuf, " id='%u'", iothDef->id);
+
+        if (iothDef->queues) {
+            size_t q;
+
+            for (q = 0; q < iothDef->nqueues; q++)
+                virBufferAsprintf(&iothreadChildBuf, "<queue id='%u'/>\n", iothDef->queues[q]);
+        }
+
+        virXMLFormatElement(&iothreadsChildBuf, "iothread", &iothreadAttrBuf, &iothreadChildBuf);
+    }
+
+    virXMLFormatElement(buf, "iothreads", NULL, &iothreadsChildBuf);
+}
+
+
 static void
 virDomainDiskDefFormatDriver(virBuffer *buf,
                              virDomainDiskDef *disk)
@@ -23226,29 +23267,7 @@ virDomainDiskDefFormatDriver(virBuffer *buf,
         virXMLFormatElement(&childBuf, "metadata_cache", NULL, &metadataCacheChildBuf);
     }
 
-    if (disk->iothreads) {
-        g_auto(virBuffer) iothreadsChildBuf = VIR_BUFFER_INIT_CHILD(&childBuf);
-        GSList *n;
-
-        for (n = disk->iothreads; n; n = n->next) {
-            virDomainIothreadMappingDef *iothDef = n->data;
-            g_auto(virBuffer) iothreadAttrBuf = VIR_BUFFER_INITIALIZER;
-            g_auto(virBuffer) iothreadChildBuf = VIR_BUFFER_INIT_CHILD(&iothreadsChildBuf);
-
-            virBufferAsprintf(&iothreadAttrBuf, " id='%u'", iothDef->id);
-
-            if (iothDef->queues) {
-                size_t q;
-
-                for (q = 0; q < iothDef->nqueues; q++)
-                    virBufferAsprintf(&iothreadChildBuf, "<queue id='%u'/>\n", iothDef->queues[q]);
-            }
-
-            virXMLFormatElement(&iothreadsChildBuf, "iothread", &iothreadAttrBuf, &iothreadChildBuf);
-        }
-
-        virXMLFormatElement(&childBuf, "iothreads", NULL, &iothreadsChildBuf);
-    }
+    virDomainIothreadMappingDefFormat(&childBuf, disk->iothreads);
 
     virXMLFormatElement(buf, "driver", &attrBuf, &childBuf);
 }