Patchwork [16/17] net: Convert do_info_network() to QObject

login
register
mail settings
Submitter Luiz Capitulino
Date Nov. 17, 2009, 8:32 p.m.
Message ID <1258489944-12159-17-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/38701/
State New
Headers show

Comments

Luiz Capitulino - Nov. 17, 2009, 8:32 p.m.
Each VLAN is represented by a QDict, the returned QObject is a QList
of all VLANs.

This commit should not change user output.

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c |    3 +-
 net.c     |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 net.h     |    3 +-
 3 files changed, 72 insertions(+), 6 deletions(-)

Patch

diff --git a/monitor.c b/monitor.c
index fb2f1c8..de7a0f1 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2033,7 +2033,8 @@  static const mon_cmd_t info_cmds[] = {
         .args_type  = "",
         .params     = "",
         .help       = "show the network state",
-        .mhandler.info = do_info_network,
+        .user_print = do_info_network_print,
+        .mhandler.info_new = do_info_network,
     },
     {
         .name       = "chardev",
diff --git a/net.c b/net.c
index 9ea66e3..a0312af 100644
--- a/net.c
+++ b/net.c
@@ -106,6 +106,7 @@ 
 #include "qemu_socket.h"
 #include "qemu-log.h"
 #include "qemu-config.h"
+#include "qemu-objects.h"
 
 #include "slirp/libslirp.h"
 
@@ -2740,19 +2741,82 @@  void net_set_boot_mask(int net_boot_mask)
     }
 }
 
-void do_info_network(Monitor *mon)
+static void info_clients_iter(QObject *data, void *opaque)
 {
+    QDict *qdict;
+    Monitor *mon = opaque;
+
+    qdict = qobject_to_qdict(data);
+    monitor_printf(mon, "  %s: %s\n", qdict_get_str(qdict, "name"),
+                                      qdict_get_str(qdict, "info"));
+}
+
+static void info_network_iter(QObject *data, void *opaque)
+{
+    QDict *qdict;
+    Monitor *mon = opaque;
+
+    qdict = qobject_to_qdict(data);
+    monitor_printf(mon, "VLAN %" PRId64 " devices:\n",
+                   qdict_get_int(qdict, "id"));
+
+    qlist_iter(qdict_get_qlist(qdict, "devices"), info_clients_iter, opaque);
+}
+
+void do_info_network_print(Monitor *mon, const QObject *data)
+{
+    qlist_iter(qobject_to_qlist(data), info_network_iter, mon);
+}
+
+/**
+ * do_info_network(): Show VLANs and associated devices information
+ *
+ * Each VLAN is represented by a QDict, the returned QObject is a QList
+ * of all VLANs.
+ *
+ * Devices are also represented by a QDict and each VLAN contains a QList
+ * of its devices. 
+ *
+ * The VLAN QDict contains the following:
+ *
+ * - "id": VLAN id
+ * - "devices": a QList of all devices
+ *
+ *  The device QDict contains the following:
+ *
+ * - "name": device's name
+ * - "info": device's specific info string
+ *
+ * Example:
+ *
+ * [ { "id": 1, "devices": [
+ *     { "name": "e1000.1", "info": "model=e1000,macaddr=52:54:00:12:34:56" },
+ *     { "name": "socket.0", "info": "socket: connect to 127.0.0.1:8010" } ] } ]
+ */
+void do_info_network(Monitor *mon, QObject **ret_data)
+{
+    QObject *obj;
     VLANState *vlan;
+    QList *vlan_list;
+
+    vlan_list = qlist_new();
 
     QTAILQ_FOREACH(vlan, &vlans, next) {
         VLANClientState *vc;
-
-        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
+        QList *clients = qlist_new();
 
         QTAILQ_FOREACH(vc, &vlan->clients, next) {
-            monitor_printf(mon, "  %s: %s\n", vc->name, vc->info_str);
+            obj = qobject_from_jsonf("{ 'name': %s, 'info': %s }",
+                                     vc->name, vc->info_str);
+            qlist_append_obj(clients, obj);
         }
+
+        obj = qobject_from_jsonf("{ 'id': %d, 'devices': %p }",
+                                 vlan->id, QOBJECT(clients));
+        qlist_append_obj(vlan_list, obj);
     }
+
+    *ret_data = QOBJECT(vlan_list);
 }
 
 void do_set_link(Monitor *mon, const QDict *qdict)
diff --git a/net.h b/net.h
index 4ffce91..66da212 100644
--- a/net.h
+++ b/net.h
@@ -105,7 +105,8 @@  void qemu_check_nic_model(NICInfo *nd, const char *model);
 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
                         const char *default_model);
 
-void do_info_network(Monitor *mon);
+void do_info_network_print(Monitor *mon, const QObject *data);
+void do_info_network(Monitor *mon, QObject **ret_data);
 void do_set_link(Monitor *mon, const QDict *qdict);
 
 void do_info_usernet(Monitor *mon);