[Libvirt-cim] [PATCH 07/47] Fix xml parsing algorithm for parse_net_device()

Xu Wang gesaint at linux.vnet.ibm.com
Tue Oct 8 06:13:41 UTC 2013


Signed-off-by: Xu Wang <gesaint at linux.vnet.ibm.com>
---
 libxkutil/device_parsing.c |  220 +++++++++++++++++++++++++++++++-------------
 1 files changed, 155 insertions(+), 65 deletions(-)

diff --git a/libxkutil/device_parsing.c b/libxkutil/device_parsing.c
index 05da1b4..d93ba7b 100644
--- a/libxkutil/device_parsing.c
+++ b/libxkutil/device_parsing.c
@@ -817,90 +817,180 @@ static int parse_net_device(xmlNode *inode, struct virt_device **vdevs)
 {
         struct virt_device *vdev = NULL;
         struct net_device *ndev = NULL;
-        xmlNode *child = NULL;
+
+        CU_DEBUG("Enter parse_net_device().");
 
         vdev = calloc(1, sizeof(*vdev));
-        if (vdev == NULL)
+        if (vdev == NULL) {
+                CU_DEBUG("calloc failed.");
                 goto err;
+        }
 
         ndev = &(vdev->dev.net);
 
-        ndev->type = get_attr_value(inode, "type");
-        if (ndev->type == NULL)
+        ndev->others = parse_data_to_others(inode, BAD_CAST "devices");
+        if (ndev->others == NULL) {
+                CU_DEBUG("parse xml data to others failed.");
                 goto err;
+        }
+
+        /* fetch out <interface> tag from others. It will be removed
+         * after others management finished. */
+        fetch_from_others(&ndev->others,
+                          "interface",
+                          TYPE_NODE,
+                          "devices");
+
+        ndev->type = fetch_from_others(&ndev->others,
+                                       "type",
+                                       TYPE_PROP,
+                                       (char *)inode->name);
+
+        if (ndev->type == NULL) {
+                CU_DEBUG("no type");
+                goto err;
+        }
+
+        if (seek_in_others(&ndev->others,
+                           "mac",
+                           TYPE_NODE,
+                           (char *)inode->name)) {
+                ndev->mac = fetch_from_others(&ndev->others,
+                                              "address",
+                                              TYPE_PROP,
+                                              "mac");
+
+                if (ndev->mac == NULL) {
+                        CU_DEBUG("no mac address");
+                        goto err;
+                }
+        }
+
+        if (seek_in_others(&ndev->others,
+                           "source",
+                           TYPE_NODE,
+                           (char *)inode->name)) {
+                ndev->source = fetch_from_others(&ndev->others,
+                                                 "bridge",
+                                                 TYPE_PROP,
+                                                 "source");
+
+                if (ndev->source == NULL) {
+                        ndev->source = fetch_from_others(&ndev->others,
+                                                         "network",
+                                                         TYPE_PROP,
+                                                         "source");
 
-        for (child = inode->children; child != NULL; child = child->next) {
-                if (XSTREQ(child->name, "mac")) {
-                        ndev->mac = get_attr_value(child, "address");
-                        if (ndev->mac == NULL)
-                                goto err;
-                } else if (XSTREQ(child->name, "source")) {
-                        ndev->source = get_attr_value(child, "bridge");
-                        if (ndev->source != NULL)
-                                continue;
-                        ndev->source = get_attr_value(child, "network");
                         if (ndev->source != NULL) {
-                                int ret = asprintf(&ndev->poolid, 
+                                int ret = asprintf(&ndev->poolid,
                                                    "NetworkPool/%s",
                                                    ndev->source);
+
                                 if (ret == -1) {
-                                        CU_DEBUG("Failed to get network"
-                                                 " poolid");
+                                        CU_DEBUG("Failed to get network poolid");
+                                        goto err;
+                                }
+                        } else {
+                                ndev->source = fetch_from_others(&ndev->others,
+                                                                 "dev",
+                                                                 TYPE_PROP,
+                                                                 "source");
+
+                                ndev->net_mode = fetch_from_others(&ndev->others,
+                                                                   "mode",
+                                                                   TYPE_PROP,
+                                                                   "source");
+
+                                if ((ndev->source == NULL) || (ndev->net_mode == NULL)) {
+                                        CU_DEBUG("source %s, mode %s", ndev->source, ndev->net_mode);
+                                        goto err;
                                 }
-                                continue;
+
                         }
-                        ndev->source = get_attr_value(child, "dev");
-                        ndev->net_mode = get_attr_value(child, "mode");
-                        if ((ndev->source != NULL) && (ndev->net_mode != NULL))
-                                continue;
+                }
+        }
+
+        if (seek_in_others(&ndev->others,
+                           "target",
+                           TYPE_NODE,
+                           (char *)inode->name)) {
+                ndev->device = fetch_from_others(&ndev->others,
+                                                 "dev",
+                                                 TYPE_PROP,
+                                                 "target");
+
+                if (ndev->device == NULL) {
+                        CU_DEBUG("no dev in target.");
                         goto err;
-                } else if (XSTREQ(child->name, "target")) {
-                        ndev->device = get_attr_value(child, "dev");
-                        if (ndev->device == NULL)
-                                goto err;
-                } else if (XSTREQ(child->name, "model")) {
-                        ndev->model = get_attr_value(child, "type");
-                        if (ndev->model == NULL)
-                                goto err;
-                } else if (XSTREQ(child->name, "filterref")) {
-                        ndev->filter_ref = get_attr_value(child, "filter");
-                } else if (XSTREQ(child->name, "virtualport")) {
-                        parse_vsi_device(child, ndev);
+                }
+        }
+
+        if (seek_in_others(&ndev->others,
+                           "model",
+                           TYPE_NODE,
+                           (char *)inode->name)) {
+                ndev->model = fetch_from_others(&ndev->others,
+                                                "type",
+                                                TYPE_PROP,
+                                                "model");
+                if (ndev->model == NULL) {
+                        CU_DEBUG("no model type.");
+                        goto err;
+                }
+        }
+
+        if (seek_in_others(&ndev->others,
+                           "filterref",
+                           TYPE_NODE,
+                           (char *)inode->name)) {
+                ndev->filter_ref = fetch_from_others(&ndev->others,
+                                                     "filter",
+                                                     TYPE_PROP,
+                                                     "filterref");
+        }
+
+        if (seek_in_others(&ndev->others, "virtualport",
+                           TYPE_NODE, (char *)inode->name)) {
+               parse_vsi_device(&ndev->others, ndev);
+        }
+
 #if LIBVIR_VERSION_NUMBER >= 9000
-                } else if (XSTREQ(child->name, "bandwidth")) {
-                        /* Network QoS bandwidth support */
-                        xmlNode *grandchild = NULL;
-                        for (grandchild = child->children;
-                             grandchild != NULL;
-                             grandchild = grandchild->next) {
-                                if (XSTREQ(grandchild->name, "inbound")) {
-                                          /* Only expose inbound bandwidth */
-                                          char *val;
-
-                                          val = get_attr_value(grandchild,
-                                                               "average");
-                                          if (val != NULL) {
-                                                  sscanf(val, "%" PRIu64,
-                                                     &ndev->reservation);
-                                                  free(val);
-                                          } else
-                                                  ndev->reservation = 0;
-
-                                          val = get_attr_value(grandchild,
-                                                               "peak");
-                                          if (val != NULL) {
-                                                  sscanf(val, "%" PRIu64,
-                                                     &ndev->limit);
-                                                  free(val);
-                                          } else
-                                                  ndev->limit = 0;
-                                          break;
-                                }
-                        }
+        /* Network QoS bandwidth support */
+        /* Only expose inbound bandwidth */
+        char *val;
+        if (seek_in_others(&ndev->others,
+                           "bandwidth",
+                           TYPE_NODE,
+                           (char *)inode->name) &&
+            seek_in_others(&ndev->others,
+                           "inbound",
+                           TYPE_NODE,
+                           "bandwidth")) {
+                val = fetch_from_others(&ndev->others,
+                                        "average",
+                                        TYPE_PROP,
+                                        "inbound");
+
+                if (val != NULL) {
+                        sscanf(val, "%" PRIu64, &ndev->reservation);
+                        free(val);
+                } else {
+                        ndev->reservation = 0;
                 }
-#endif
 
+                val = fetch_from_others(&ndev->others,
+                                        "peak",
+                                        TYPE_PROP,
+                                        "inbound");
+
+                if (val != NULL) {
+                        sscanf(val, "%" PRIu64, &ndev->limit);
+                        free(val);
+                } else {
+                        ndev->limit = 0;
+                }
         }
+#endif
 
         if (ndev->source == NULL)
                 CU_DEBUG("No network source defined, leaving blank\n");
-- 
1.7.1




More information about the Libvirt-cim mailing list