From: Shivaprasad G Bhat <sb...@linux.vnet.ibm.com>

virDomainDeviceDefParseXMLMany will be used to parse '<devices>'
XML elements that will be used to supply multiple hostdevs
for hotplug/unplug. The idea is to parse each dev individually,
by calling the same code that already handles it today. The
result is returned in a new virDomainDeviceDefListPtr pointer
type, which is an array of device definitions.

This idea of calling existing functions to handles a single
device definition in a loop is the central idea of how PCI
multifunction hotplug and hotunplug is implemented in the
next patches. Roughly speaking, we'll use virDomainDeviceDefListPtr
lists to call (almost) the same hotplug/unplug mechanics we have
today for each hostdev in the list.

Signed-off-by: Shivaprasad G Bhat <sb...@linux.vnet.ibm.com>
Signed-off-by: Daniel Henrique Barboza <danielhb...@gmail.com>
---
 src/conf/domain_conf.c   | 166 +++++++++++++++++++++++++++++++++++----
 src/conf/domain_conf.h   |  32 ++++++++
 src/libvirt_private.syms |   5 ++
 3 files changed, 189 insertions(+), 14 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index fcd390ace4..4c74ee7229 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -1351,6 +1351,90 @@ virDomainXMLOptionDispose(void *obj)
         (xmlopt->config.privFree)(xmlopt->config.priv);
 }
 
+/* virDomainDeviceDefListAddCopy - add a *copy* of the device to this list */
+static int
+virDomainDeviceDefListAddCopy(virDomainDeviceDefListPtr list,
+                              virDomainDeviceDefPtr dev,
+                              virDomainDeviceDefListDataPtr data,
+                              void *parseOpaque)
+{
+    virDomainDeviceDefPtr copy = virDomainDeviceDefCopy(dev,
+                                                        data->def,
+                                                        data->xmlopt,
+                                                        parseOpaque);
+
+    if (!copy)
+        return -1;
+
+    if (VIR_APPEND_ELEMENT(list->devs, list->count, copy) < 0) {
+        virDomainDeviceDefFree(copy);
+        return -1;
+    }
+
+    return 0;
+}
+
+void virDomainDeviceDefListFree(virDomainDeviceDefListPtr list)
+{
+    size_t i;
+
+    if (!list)
+        return;
+    for (i = 0; i < list->count; i++)
+        virDomainDeviceDefFree(list->devs[i]);
+    VIR_FREE(list->devs);
+}
+
+void virDomainDeviceDefListFreeShallow(virDomainDeviceDefListPtr list)
+{
+    size_t i;
+
+    if (!list)
+        return;
+    for (i = 0; i < list->count; i++)
+        VIR_FREE(list->devs[i]);
+}
+
+
+/* virDomainDeviceDefListIter - Iterate through the list with the callback*/
+int
+virDomainDeviceDefListIterate(virDomainDeviceDefListPtr list,
+                              virDomainDeviceDefListIterCallback cb,
+                              void *data)
+{
+    size_t i;
+
+    for (i = 0; i < list->count; i++)
+        if (cb(list->devs[i], data))
+            return -1;
+
+    return 0;
+}
+
+virDomainDeviceDefListPtr
+virDomainDeviceDefListCopy(virDomainDeviceDefListPtr list,
+                           virDomainDeviceDefListDataPtr data,
+                           void *parseOpaque)
+{
+    size_t i;
+    virDomainDeviceDefListPtr devlist = NULL;
+
+    if (list && (VIR_ALLOC(devlist) < 0))
+        goto cleanup;
+
+    for (i = 0; i < list->count; i++) {
+        if (virDomainDeviceDefListAddCopy(devlist, list->devs[i],
+                                          data, parseOpaque) < 0)
+            goto cleanup;
+    }
+
+    return devlist;
+ cleanup:
+    virDomainDeviceDefListFree(devlist);
+    return NULL;
+}
+
+
 /**
  * virDomainKeyWrapCipherDefParseXML:
  *
@@ -16873,23 +16957,17 @@ virDomainVsockDefParseXML(virDomainXMLOptionPtr 
xmlopt,
     return g_steal_pointer(&vsock);
 }
 
-virDomainDeviceDefPtr
-virDomainDeviceDefParse(const char *xmlStr,
-                        const virDomainDef *def,
-                        virDomainXMLOptionPtr xmlopt,
-                        void *parseOpaque,
-                        unsigned int flags)
+
+static virDomainDeviceDefPtr
+virDomainDeviceDefParseXML(xmlNodePtr node,
+                           const virDomainDef *def,
+                           virDomainXMLOptionPtr xmlopt,
+                           xmlXPathContextPtr ctxt,
+                           void *parseOpaque,
+                           unsigned int flags)
 {
-    g_autoptr(xmlDoc) xml = NULL;
-    xmlNodePtr node;
-    g_autoptr(xmlXPathContext) ctxt = NULL;
     g_autoptr(virDomainDeviceDef) dev = NULL;
 
-    if (!(xml = virXMLParseStringCtxt(xmlStr, _("(device_definition)"), 
&ctxt)))
-        return NULL;
-
-    node = ctxt->node;
-
     if (VIR_ALLOC(dev) < 0)
         return NULL;
 
@@ -17046,6 +17124,29 @@ virDomainDeviceDefParse(const char *xmlStr,
 }
 
 
+virDomainDeviceDefPtr
+virDomainDeviceDefParse(const char *xmlStr,
+                        const virDomainDef *def,
+                        virDomainXMLOptionPtr xmlopt,
+                        void *parseOpaque,
+                        unsigned int flags)
+{
+    g_autoptr(xmlDoc) xml = NULL;
+    xmlNodePtr node;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
+    virDomainDeviceDefPtr dev = NULL;
+
+    if (!(xml = virXMLParseStringCtxt(xmlStr, _("(device_definition)"), 
&ctxt)))
+        return NULL;
+
+    node = ctxt->node;
+
+    dev = virDomainDeviceDefParseXML(node, def, xmlopt, ctxt, parseOpaque, 
flags);
+
+    return dev;
+ }
+
+
 virDomainDiskDefPtr
 virDomainDiskDefParse(const char *xmlStr,
                       virDomainXMLOptionPtr xmlopt,
@@ -32365,6 +32466,43 @@ virDomainDiskTranslateSourcePool(virDomainDiskDefPtr 
def)
     return 0;
 }
 
+virDomainDeviceDefListPtr
+virDomainDeviceDefParseXMLMany(const char *xml,
+                               const virDomainDef *def,
+                               virDomainXMLOptionPtr xmlopt,
+                               void *parseOpaque,
+                               unsigned int flags)
+{
+    g_autoptr(xmlXPathContext) ctxt = NULL;
+    g_autoptr(xmlDoc) xmlPtr = NULL;
+    xmlNodePtr node, root;
+    virDomainDeviceDefPtr dev = NULL;
+    virDomainDeviceDefListPtr devlist;
+
+    if (!(xmlPtr = virXMLParseStringCtxt(xml, _("(device_definition)"), 
&ctxt)))
+        return NULL;
+
+    if (VIR_ALLOC(devlist) < 0)
+        return NULL;
+
+    root = xmlDocGetRootElement(xmlPtr);
+    node = root->children;
+    while (node) {
+        if (node->type == XML_ELEMENT_NODE) {
+            dev = virDomainDeviceDefParseXML(node, def, xmlopt, ctxt,
+                                             parseOpaque, flags);
+            if (VIR_APPEND_ELEMENT(devlist->devs, devlist->count, dev) < 0) {
+                virDomainDeviceDefFree(dev);
+                virDomainDeviceDefListFree(devlist);
+                return NULL;
+            }
+            dev = NULL;
+        }
+        node = node->next;
+    }
+
+    return devlist;
+}
 
 /**
  * virDomainDiskGetDetectZeroesMode:
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 634865842a..e31fac8a4a 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2748,6 +2748,33 @@ typedef enum {
 typedef int (*virDomainDefPostParseBasicCallback)(virDomainDefPtr def,
                                                   void *opaque);
 
+typedef struct _virDomainDeviceDefListData virDomainDeviceDefListData;
+typedef virDomainDeviceDefListData *virDomainDeviceDefListDataPtr;
+struct _virDomainDeviceDefListData {
+    const virDomainDef *def;
+    virDomainXMLOptionPtr xmlopt;
+};
+
+struct virDomainDeviceDefList {
+    virDomainDeviceDefPtr *devs;
+    size_t count;
+};
+typedef struct virDomainDeviceDefList *virDomainDeviceDefListPtr;
+
+typedef int (*virDomainDeviceDefListIterCallback)(virDomainDeviceDefPtr dev,
+                                                  void *opaque);
+int virDomainDeviceDefListIterate(virDomainDeviceDefListPtr devlist,
+                                  virDomainDeviceDefListIterCallback cb,
+                                  void *data);
+virDomainDeviceDefListPtr
+virDomainDeviceDefListCopy(virDomainDeviceDefListPtr list,
+                           virDomainDeviceDefListDataPtr data,
+                           void *parseOpaque);
+
+void virDomainDeviceDefListFree(virDomainDeviceDefListPtr list);
+void virDomainDeviceDefListFreeShallow(virDomainDeviceDefListPtr list);
+
+
 /* Called once after everything else has been parsed, for adjusting
  * overall domain defaults.
  * @parseOpaque is opaque data passed by virDomainDefParse* caller,
@@ -3155,6 +3182,11 @@ virDomainDeviceDefPtr virDomainDeviceDefParse(const char 
*xmlStr,
                                               virDomainXMLOptionPtr xmlopt,
                                               void *parseOpaque,
                                               unsigned int flags);
+virDomainDeviceDefListPtr virDomainDeviceDefParseXMLMany(const char *xmlStr,
+                                                         const virDomainDef 
*def,
+                                                         virDomainXMLOptionPtr 
xmlopt,
+                                                         void *parseOpaque,
+                                                         unsigned int flags);
 virDomainDiskDefPtr virDomainDiskDefParse(const char *xmlStr,
                                           virDomainXMLOptionPtr xmlopt,
                                           unsigned int flags);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 323291367f..29c6ac8e5e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -343,7 +343,12 @@ virDomainDeleteConfig;
 virDomainDeviceAliasIsUserAlias;
 virDomainDeviceDefCopy;
 virDomainDeviceDefFree;
+virDomainDeviceDefListCopy;
+virDomainDeviceDefListFree;
+virDomainDeviceDefListFreeShallow;
+virDomainDeviceDefListIterate;
 virDomainDeviceDefParse;
+virDomainDeviceDefParseXMLMany;
 virDomainDeviceFindSCSIController;
 virDomainDeviceGetInfo;
 virDomainDeviceInfoIterate;
-- 
2.26.2

Reply via email to