qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC][PATCH v5 05/21] virtagent: base client definitions


From: Michael Roth
Subject: [Qemu-devel] [RFC][PATCH v5 05/21] virtagent: base client definitions
Date: Fri, 3 Dec 2010 12:03:06 -0600

Functions for managing client capabilities and creating client RPC jobs.

Signed-off-by: Michael Roth <address@hidden>
---
 qerror.c    |    8 +++
 qerror.h    |    6 +++
 virtagent.c |  141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 virtagent.h |   32 +++++++++++++
 4 files changed, 187 insertions(+), 0 deletions(-)
 create mode 100644 virtagent.c
 create mode 100644 virtagent.h

diff --git a/qerror.c b/qerror.c
index ac2cdaf..c765a57 100644
--- a/qerror.c
+++ b/qerror.c
@@ -200,6 +200,14 @@ static const QErrorStringTable qerror_table[] = {
         .error_fmt = QERR_VNC_SERVER_FAILED,
         .desc      = "Could not start VNC server on %(target)",
     },
+    {
+        .error_fmt = QERR_RPC_FAILED,
+        .desc      = "An RPC error has occurred",
+    },
+    {
+        .error_fmt = QERR_VA_FAILED,
+        .desc      = "An error was reported by virtagent",
+    },
     {}
 };
 
diff --git a/qerror.h b/qerror.h
index 943a24b..059c0dc 100644
--- a/qerror.h
+++ b/qerror.h
@@ -165,4 +165,10 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_VNC_SERVER_FAILED \
     "{ 'class': 'VNCServerFailed', 'data': { 'target': %s } }"
 
+#define QERR_RPC_FAILED \
+    "{ 'class': 'RPCFailed', 'data': { 'code': %i, 'message': %s } }"
+
+#define QERR_VA_FAILED \
+    "{ 'class': 'VirtagentFailed', 'data': { 'code': %i, 'message': %s } }"
+
 #endif /* QERROR_H */
diff --git a/virtagent.c b/virtagent.c
new file mode 100644
index 0000000..34d8545
--- /dev/null
+++ b/virtagent.c
@@ -0,0 +1,141 @@
+/*
+ * virtagent - host/guest RPC client functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Adam Litke        <address@hidden>
+ *  Michael Roth      <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu_socket.h"
+#include "virtagent-common.h"
+
+static VAClientData *va_client_data;
+
+static void va_set_capabilities(QList *qlist)
+{
+    TRACE("called");
+
+    if (va_client_data == NULL) {
+        LOG("client is uninitialized, unable to set capabilities");
+        return;
+    }
+
+    if (va_client_data->supported_methods != NULL) {
+        qobject_decref(QOBJECT(va_client_data->supported_methods));
+        va_client_data->supported_methods = NULL;
+        TRACE("capabilities reset");
+    }
+
+    if (qlist != NULL) {
+        va_client_data->supported_methods = qlist_copy(qlist);
+        TRACE("capabilities set");
+    }
+}
+
+typedef struct VACmpState {
+    const char *method;
+    bool found;
+} VACmpState;
+
+static void va_cmp_capability_iter(QObject *obj, void *opaque)
+{
+    QString *method = qobject_to_qstring(obj);
+    const char *method_str = NULL;
+    VACmpState *cmp_state = opaque;
+
+    if (method) {
+        method_str = qstring_get_str(method);
+    }
+
+    if (method_str && opaque) {
+        if (strcmp(method_str, cmp_state->method) == 0) {
+            cmp_state->found = 1;
+        }
+    }
+}
+
+static bool va_has_capability(const char *method)
+{
+    VACmpState cmp_state;
+
+    if (method == NULL) {
+        return false;
+    }
+
+    /* we can assume method introspection is available */
+    if (strcmp(method, "system.listMethods") == 0) {
+        return true;
+    }
+    /* assume hello is available to we can probe for/notify the host
+     * rpc server
+     */
+    if (strcmp(method, "va.hello") == 0) {
+        return true;
+    }
+
+    /* compare method against the last retrieved supported method list */
+    cmp_state.method = method;
+    cmp_state.found = false;
+    if (va_client_data->supported_methods) {
+        qlist_iter(va_client_data->supported_methods,
+                   va_cmp_capability_iter,
+                   (void *)&cmp_state);
+    }
+
+    return cmp_state.found;
+}
+
+int va_client_init(VAClientData *client_data)
+{
+    client_data->supported_methods = NULL;
+    va_client_data = client_data;
+
+    return 0;
+}
+
+static int va_rpc_has_error(xmlrpc_env *env)
+{
+    if (env->fault_occurred) {
+        qerror_report(QERR_RPC_FAILED, env->fault_code, env->fault_string);
+        return -1;
+    }
+    return 0;
+}
+
+static int va_do_rpc(xmlrpc_env *const env, const char *function,
+                     xmlrpc_value *params, VAClientCallback *cb,
+                     MonitorCompletion *mon_cb, void *mon_data)
+{
+    xmlrpc_mem_block *req_xml;
+    int ret;
+
+    if (!va_has_capability(function)) {
+        LOG("guest agent does not have required capability");
+        ret = -1;
+        goto out;
+    }
+
+    req_xml = XMLRPC_MEMBLOCK_NEW(char, env, 0);
+    xmlrpc_serialize_call(env, req_xml, function, params);
+    if (va_rpc_has_error(env)) {
+        ret = -EINVAL;
+        goto out_free;
+    }
+
+    ret = va_client_job_add(req_xml, cb, mon_cb, mon_data);
+    if (ret) {
+        goto out_free;
+    }
+
+    return ret;
+out_free:
+    XMLRPC_MEMBLOCK_FREE(char, req_xml);
+out:
+    return ret;
+}
diff --git a/virtagent.h b/virtagent.h
new file mode 100644
index 0000000..af36b6a
--- /dev/null
+++ b/virtagent.h
@@ -0,0 +1,32 @@
+/*
+ * virt-agent - host/guest RPC client functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Adam Litke        <address@hidden>
+ *  Michael Roth      <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef VIRTAGENT_H
+#define VIRTAGENT_H
+
+#include "monitor.h"
+
+#define GUEST_AGENT_PATH_CLIENT "/tmp/virtagent-guest-client.sock"
+#define HOST_AGENT_PATH_CLIENT "/tmp/virtagent-host-client.sock"
+#define VA_MAX_CHUNK_SIZE 4096 /* max bytes at a time for get/send file */
+
+typedef void (VAClientCallback)(const char *resp_data, size_t resp_data_len,
+                                MonitorCompletion *mon_cb, void *mon_data);
+typedef struct VAClientData {
+    QList *supported_methods;
+} VAClientData;
+
+int va_client_init(VAClientData *client_data);
+
+#endif /* VIRTAGENT_H */
-- 
1.7.0.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]