Patchwork [RFC,v2,02/10] virtagent: base definitions for host/guest RPC daemon

login
register
mail settings
Submitter Michael Roth
Date Nov. 3, 2010, 11:35 a.m.
Message ID <1288784139-1110-3-git-send-email-mdroth@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/69977/
State New
Headers show

Comments

Michael Roth - Nov. 3, 2010, 11:35 a.m.
Basic skeleton code for RPC daemon loop. This is shared by both the
guest-side RPC server as well as the host-side one (the advertised RPCs
for each by guest/host-specific arrays).

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 virtagent-daemon.c |  156 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 virtagent-daemon.h |   20 +++++++
 2 files changed, 176 insertions(+), 0 deletions(-)
 create mode 100644 virtagent-daemon.c
 create mode 100644 virtagent-daemon.h

Patch

diff --git a/virtagent-daemon.c b/virtagent-daemon.c
new file mode 100644
index 0000000..71a36d4
--- /dev/null
+++ b/virtagent-daemon.c
@@ -0,0 +1,156 @@ 
+/*
+ * virt-agent - host/guest RPC daemon functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Adam Litke        <aglitke@linux.vnet.ibm.com>
+ *  Michael Roth      <mdroth@linux.vnet.ibm.com>
+ *
+ * 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-daemon.h"
+#include "virtagent-common.h"
+
+static int va_accept(int listen_fd) {
+    struct sockaddr_in saddr;
+    struct sockaddr *addr;
+    socklen_t len;
+    int fd;
+
+    while (1) {
+        len = sizeof(saddr);
+        addr = (struct sockaddr *)&saddr;
+        fd = qemu_accept(listen_fd, addr, &len);
+        if (fd < 0 && errno != EINTR) {
+            LOG("accept() failed");
+            break;
+        } else if (fd >= 0) {
+            TRACE("accepted connection");
+            break;
+        }
+    }
+    return fd;
+}
+
+typedef struct RPCFunction {
+    xmlrpc_value *(*func)(xmlrpc_env *env, xmlrpc_value *param, void *unused);
+    const char *func_name;
+} RPCFunction;
+
+static RPCFunction guest_functions[] = {
+    { NULL, NULL }
+};
+static RPCFunction host_functions[] = {
+    { NULL, NULL }
+};
+
+static void va_register_functions(xmlrpc_env *env, xmlrpc_registry *registry,
+                                  RPCFunction *list)
+{
+    int i;
+    for (i = 0; list[i].func != NULL; ++i) {
+        TRACE("adding func: %s", list[i].func_name);
+        xmlrpc_registry_add_method(env, registry, NULL, list[i].func_name,
+                                   list[i].func, NULL);
+    }
+}
+
+typedef struct VARPCServerState {
+    int listen_fd;
+    xmlrpc_env env;
+    xmlrpc_registry *registry;
+} VARPCServerState;
+
+static void va_accept_handler(void *opaque);
+
+static void va_rpc_send_cb(void *opaque)
+{
+    VARPCData *rpc_data = opaque;
+    VARPCServerState *s = rpc_data->opaque;
+
+    TRACE("called");
+    if (rpc_data->status != VA_RPC_STATUS_OK) {
+        LOG("error sending RPC response");
+    } else {
+        TRACE("RPC completed");
+    }
+
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(s->listen_fd, va_accept_handler, NULL, s);
+}
+
+static void va_rpc_read_cb(void *opaque)
+{
+    VARPCData *rpc_data = opaque;
+    VARPCServerState *s = rpc_data->opaque;
+
+    TRACE("called");
+    if (rpc_data->status != VA_RPC_STATUS_OK) {
+        LOG("error reading RPC request");
+        goto out_bad;
+    }
+
+    rpc_data->send_resp_xml = 
+        xmlrpc_registry_process_call(&s->env, s->registry, NULL,
+                                     rpc_data->req_xml, rpc_data->req_xml_len);
+    if (rpc_data->send_resp_xml == NULL) {
+        LOG("error handling RPC request");
+        goto out_bad;
+    }
+
+    rpc_data->cb = va_rpc_send_cb;
+    return;
+
+out_bad:
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(s->listen_fd, va_accept_handler, NULL, s);
+}
+
+static void va_accept_handler(void *opaque)
+{
+    VARPCServerState *s = opaque;
+    VARPCData *rpc_data;
+    int ret, fd;
+
+    TRACE("called");
+    fd = va_accept(s->listen_fd);
+    if (fd < 0) {
+        TRACE("connection error: %s", strerror(errno));
+        return;
+    }
+    ret = fcntl(fd, F_GETFL);
+    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
+
+    TRACE("RPC client connected, reading RPC request...");
+    rpc_data = qemu_mallocz(sizeof(VARPCData));
+    rpc_data->cb = va_rpc_read_cb;
+    rpc_data->opaque = s;
+    ret = va_rpc_read_request(rpc_data, fd);
+    if (ret != 0) {
+        LOG("error setting up read handler");
+        qemu_free(rpc_data);
+        return;
+    }
+    vp_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
+}
+
+int va_server_start(int listen_fd, bool is_host)
+{
+    VARPCServerState *s;
+    RPCFunction *func_list = is_host ? host_functions : guest_functions;
+
+    s = qemu_mallocz(sizeof(VARPCServerState));
+    s->listen_fd = listen_fd;
+    xmlrpc_env_init(&s->env);
+    s->registry = xmlrpc_registry_new(&s->env);
+    va_register_functions(&s->env, s->registry, func_list);
+
+    TRACE("waiting for RPC request...");
+    vp_set_fd_handler(s->listen_fd, va_accept_handler, NULL, s);
+
+    return 0;
+}
diff --git a/virtagent-daemon.h b/virtagent-daemon.h
new file mode 100644
index 0000000..da926b3
--- /dev/null
+++ b/virtagent-daemon.h
@@ -0,0 +1,20 @@ 
+/*
+ * virt-agent - host/guest RPC daemon functions
+ *
+ * Copyright IBM Corp. 2010
+ *
+ * Authors:
+ *  Michael Roth      <mdroth@linux.vnet.ibm.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+#define GUEST_AGENT_SERVICE_ID "virtagent"
+#define GUEST_AGENT_PATH "/tmp/virtagent-guest.sock"
+#define HOST_AGENT_SERVICE_ID "virtagent-host"
+#define HOST_AGENT_PATH "/tmp/virtagent-host.sock"
+#define VA_GETFILE_MAX 1 << 30
+#define VA_FILEBUF_LEN 16384
+
+int va_server_start(int listen_fd, bool is_host);