render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
9119d9
From d3a5d3a03e6dd4230c44656f130acbf1f9821be5 Mon Sep 17 00:00:00 2001
9119d9
Message-Id: <d3a5d3a03e6dd4230c44656f130acbf1f9821be5@dist-git>
9119d9
From: Pavel Hrdina <phrdina@redhat.com>
9119d9
Date: Wed, 14 Jan 2015 13:38:26 +0100
9119d9
Subject: [PATCH] qemu_monitor: introduce new function to get QOM path
9119d9
9119d9
The search is done recursively only through QOM object that has a type
9119d9
prefixed with "child<" as this indicate that the QOM is a parent for
9119d9
other QOM objects.
9119d9
9119d9
The usage is that you give known device name with starting path where to
9119d9
search.
9119d9
9119d9
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
9119d9
(cherry picked from commit cc41c64878590d7016e0dfa488d345e8634c3bf2)
9119d9
9119d9
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1180574
9119d9
9119d9
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
9119d9
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
9119d9
---
9119d9
 src/qemu/qemu_monitor.c | 178 ++++++++++++++++++++++++++++--------------------
9119d9
 1 file changed, 105 insertions(+), 73 deletions(-)
9119d9
9119d9
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
9119d9
index f797f4f..df7bb45 100644
9119d9
--- a/src/qemu/qemu_monitor.c
9119d9
+++ b/src/qemu/qemu_monitor.c
9119d9
@@ -1012,38 +1012,106 @@ qemuMonitorSetOptions(qemuMonitorPtr mon, virJSONValuePtr options)
9119d9
     mon->options = options;
9119d9
 }
9119d9
 
9119d9
-/* Search the qom objects for the balloon driver object by it's known name
9119d9
- * of "virtio-balloon-pci".  The entry for the driver will be found in the
9119d9
- * returned 'type' field using the syntax "child<virtio-balloon-pci>".
9119d9
- *
9119d9
- * Once found, check the entry to ensure it has the correct property listed.
9119d9
- * If it does not, then obtaining statistics from qemu will not be possible.
9119d9
- * This feature was added to qemu 1.5.
9119d9
+
9119d9
+/**
9119d9
+ * Search the qom objects by it's known name.  The name is compared against
9119d9
+ * filed 'type' formatted as 'link<%name>'.
9119d9
  *
9119d9
  * This procedure will be call recursively until found or the qom-list is
9119d9
  * exhausted.
9119d9
  *
9119d9
  * Returns:
9119d9
  *
9119d9
- *   1  - Found
9119d9
- *   0  - Not found still looking
9119d9
+ *   0  - Found
9119d9
  *  -1  - Error bail out
9119d9
+ *  -2  - Not found
9119d9
  *
9119d9
  * NOTE: This assumes we have already called qemuDomainObjEnterMonitor()
9119d9
  */
9119d9
 static int
9119d9
-qemuMonitorFindBalloonObjectPath(qemuMonitorPtr mon,
9119d9
-                                 virDomainObjPtr vm,
9119d9
-                                 const char *curpath)
9119d9
+qemuMonitorFindObjectPath(qemuMonitorPtr mon,
9119d9
+                          const char *curpath,
9119d9
+                          const char *name,
9119d9
+                          char **path)
9119d9
 {
9119d9
-    ssize_t i, j, npaths = 0, nprops = 0;
9119d9
-    int ret = 0;
9119d9
+    ssize_t i, npaths = 0;
9119d9
+    int ret = -2;
9119d9
     char *nextpath = NULL;
9119d9
+    char *type = NULL;
9119d9
     qemuMonitorJSONListPathPtr *paths = NULL;
9119d9
+
9119d9
+    if (virAsprintf(&type, "link<%s>", name) < 0)
9119d9
+        return -1;
9119d9
+
9119d9
+    VIR_DEBUG("Searching for '%s' Object Path starting at '%s'", type, curpath);
9119d9
+
9119d9
+    npaths = qemuMonitorJSONGetObjectListPaths(mon, curpath, &paths);
9119d9
+    if (npaths < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    for (i = 0; i < npaths && ret == -2; i++) {
9119d9
+
9119d9
+        if (STREQ_NULLABLE(paths[i]->type, type)) {
9119d9
+            VIR_DEBUG("Path to '%s' is '%s/%s'", type, curpath, paths[i]->name);
9119d9
+            ret = 0;
9119d9
+            if (virAsprintf(path, "%s/%s", curpath, paths[i]->name) < 0) {
9119d9
+                *path = NULL;
9119d9
+                ret = -1;
9119d9
+            }
9119d9
+            goto cleanup;
9119d9
+        }
9119d9
+
9119d9
+        /* Type entries that begin with "child<" are a branch that can be
9119d9
+         * traversed looking for more entries
9119d9
+         */
9119d9
+        if (paths[i]->type && STRPREFIX(paths[i]->type, "child<")) {
9119d9
+            if (virAsprintf(&nextpath, "%s/%s", curpath, paths[i]->name) < 0) {
9119d9
+                ret = -1;
9119d9
+                goto cleanup;
9119d9
+            }
9119d9
+
9119d9
+            ret = qemuMonitorFindObjectPath(mon, nextpath, name, path);
9119d9
+        }
9119d9
+    }
9119d9
+
9119d9
+ cleanup:
9119d9
+    for (i = 0; i < npaths; i++)
9119d9
+        qemuMonitorJSONListPathFree(paths[i]);
9119d9
+    VIR_FREE(paths);
9119d9
+    VIR_FREE(nextpath);
9119d9
+    VIR_FREE(type);
9119d9
+    return ret;
9119d9
+}
9119d9
+
9119d9
+
9119d9
+/**
9119d9
+ * Search the qom objects for the balloon driver object by it's known name
9119d9
+ * of "virtio-balloon-pci".  The entry for the driver will be found by using
9119d9
+ * function "qemuMonitorFindObjectPath".
9119d9
+ *
9119d9
+ * Once found, check the entry to ensure it has the correct property listed.
9119d9
+ * If it does not, then obtaining statistics from QEMU will not be possible.
9119d9
+ * This feature was added to QEMU 1.5.
9119d9
+ *
9119d9
+ * Returns:
9119d9
+ *
9119d9
+ *   0  - Found
9119d9
+ *  -1  - Not found or error
9119d9
+ *
9119d9
+ * NOTE: This assumes we have already called qemuDomainObjEnterMonitor()
9119d9
+ */
9119d9
+static int
9119d9
+qemuMonitorFindBalloonObjectPath(qemuMonitorPtr mon,
9119d9
+                                 const char *curpath)
9119d9
+{
9119d9
+    ssize_t i, nprops = 0;
9119d9
+    int ret = -1;
9119d9
+    char *path = NULL;
9119d9
     qemuMonitorJSONListPathPtr *bprops = NULL;
9119d9
+    virDomainObjPtr vm = mon->vm;
9119d9
 
9119d9
     if (mon->balloonpath) {
9119d9
-        return 1;
9119d9
+        return 0;
9119d9
     } else if (mon->ballooninit) {
9119d9
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
                        _("Cannot determine balloon device path"));
9119d9
@@ -1059,70 +1127,34 @@ qemuMonitorFindBalloonObjectPath(qemuMonitorPtr mon,
9119d9
         return -1;
9119d9
     }
9119d9
 
9119d9
-    VIR_DEBUG("Searching for Balloon Object Path starting at %s", curpath);
9119d9
-
9119d9
-    npaths = qemuMonitorJSONGetObjectListPaths(mon, curpath, &paths);
9119d9
-    if (npaths < 0)
9119d9
+    if (qemuMonitorFindObjectPath(mon, curpath, "virtio-balloon-pci", &path) < 0)
9119d9
         return -1;
9119d9
 
9119d9
-    for (i = 0; i < npaths && ret == 0; i++) {
9119d9
-
9119d9
-        if (STREQ_NULLABLE(paths[i]->type, "link<virtio-balloon-pci>")) {
9119d9
-            VIR_DEBUG("Path to <virtio-balloon-pci> is '%s/%s'",
9119d9
-                      curpath, paths[i]->name);
9119d9
-            if (virAsprintf(&nextpath, "%s/%s", curpath, paths[i]->name) < 0) {
9119d9
-                ret = -1;
9119d9
-                goto cleanup;
9119d9
-            }
9119d9
-
9119d9
-            /* Now look at the each of the property entries to determine
9119d9
-             * whether "guest-stats-polling-interval" exists.  If not,
9119d9
-             * then this version of qemu/kvm does not support the feature.
9119d9
-             */
9119d9
-            nprops = qemuMonitorJSONGetObjectListPaths(mon, nextpath, &bprops);
9119d9
-            if (nprops < 0) {
9119d9
-                ret = -1;
9119d9
-                goto cleanup;
9119d9
-            }
9119d9
-
9119d9
-            for (j = 0; j < nprops; j++) {
9119d9
-                if (STREQ(bprops[j]->name, "guest-stats-polling-interval")) {
9119d9
-                    VIR_DEBUG("Found Balloon Object Path %s", nextpath);
9119d9
-                    mon->balloonpath = nextpath;
9119d9
-                    nextpath = NULL;
9119d9
-                    ret = 1;
9119d9
-                    goto cleanup;
9119d9
-                }
9119d9
-            }
9119d9
-
9119d9
-            /* If we get here, we found the path, but not the property */
9119d9
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
-                           _("Property 'guest-stats-polling-interval' "
9119d9
-                             "not found on memory balloon driver."));
9119d9
-            ret = -1;
9119d9
+    nprops = qemuMonitorJSONGetObjectListPaths(mon, path, &bprops);
9119d9
+    if (nprops < 0)
9119d9
+        goto cleanup;
9119d9
+
9119d9
+    for (i = 0; i < nprops; i++) {
9119d9
+        if (STREQ(bprops[i]->name, "guest-stats-polling-interval")) {
9119d9
+            VIR_DEBUG("Found Balloon Object Path %s", path);
9119d9
+            mon->balloonpath = path;
9119d9
+            path = NULL;
9119d9
+            ret = 0;
9119d9
             goto cleanup;
9119d9
         }
9119d9
-
9119d9
-        /* Type entries that begin with "child<" are a branch that can be
9119d9
-         * traversed looking for more entries
9119d9
-         */
9119d9
-        if (paths[i]->type && STRPREFIX(paths[i]->type, "child<")) {
9119d9
-            if (virAsprintf(&nextpath, "%s/%s", curpath, paths[i]->name) < 0) {
9119d9
-                ret = -1;
9119d9
-                goto cleanup;
9119d9
-            }
9119d9
-            ret = qemuMonitorFindBalloonObjectPath(mon, vm, nextpath);
9119d9
-        }
9119d9
     }
9119d9
 
9119d9
+
9119d9
+    /* If we get here, we found the path, but not the property */
9119d9
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
9119d9
+                   _("Property 'guest-stats-polling-interval' "
9119d9
+                     "not found on memory balloon driver."));
9119d9
+
9119d9
  cleanup:
9119d9
-    for (i = 0; i < npaths; i++)
9119d9
-        qemuMonitorJSONListPathFree(paths[i]);
9119d9
-    VIR_FREE(paths);
9119d9
-    for (j = 0; j < nprops; j++)
9119d9
-        qemuMonitorJSONListPathFree(bprops[j]);
9119d9
+    for (i = 0; i < nprops; i++)
9119d9
+        qemuMonitorJSONListPathFree(bprops[i]);
9119d9
     VIR_FREE(bprops);
9119d9
-    VIR_FREE(nextpath);
9119d9
+    VIR_FREE(path);
9119d9
     return ret;
9119d9
 }
9119d9
 
9119d9
@@ -1636,7 +1668,7 @@ int qemuMonitorGetMemoryStats(qemuMonitorPtr mon,
9119d9
     }
9119d9
 
9119d9
     if (mon->json) {
9119d9
-        ignore_value(qemuMonitorFindBalloonObjectPath(mon, mon->vm, "/"));
9119d9
+        ignore_value(qemuMonitorFindBalloonObjectPath(mon, "/"));
9119d9
         mon->ballooninit = true;
9119d9
         ret = qemuMonitorJSONGetMemoryStats(mon, mon->balloonpath,
9119d9
                                             stats, nr_stats);
9119d9
@@ -1664,7 +1696,7 @@ int qemuMonitorSetMemoryStatsPeriod(qemuMonitorPtr mon,
9119d9
         return -1;
9119d9
     }
9119d9
 
9119d9
-    if (qemuMonitorFindBalloonObjectPath(mon, mon->vm, "/") == 1) {
9119d9
+    if (qemuMonitorFindBalloonObjectPath(mon, "/") == 0) {
9119d9
         ret = qemuMonitorJSONSetMemoryStatsPeriod(mon, mon->balloonpath,
9119d9
                                                   period);
9119d9
     }
9119d9
-- 
9119d9
2.2.1
9119d9