From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: aliguori@linux.vnet.ibm.com, ryanh@us.ibm.com,
agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com,
abeekhof@redhat.com
Subject: [Qemu-devel] [RFC][PATCH v4 02/18] virtagent: base definitions for host/guest RPC server
Date: Tue, 16 Nov 2010 10:01:44 -0600 [thread overview]
Message-ID: <1289923320-5638-3-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1289923320-5638-1-git-send-email-mdroth@linux.vnet.ibm.com>
Basic skeleton code for RPC server. 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 | 209 ++++++++++++++++++++++++++++++++++++++++++++++++++++
virtagent-daemon.h | 22 ++++++
2 files changed, 231 insertions(+), 0 deletions(-)
create mode 100644 virtagent-daemon.c
create mode 100644 virtagent-daemon.h
diff --git a/virtagent-daemon.c b/virtagent-daemon.c
new file mode 100644
index 0000000..78d550f
--- /dev/null
+++ b/virtagent-daemon.c
@@ -0,0 +1,209 @@
+/*
+ * 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 <syslog.h>
+#include "qemu_socket.h"
+#include "virtagent-daemon.h"
+#include "virtagent-common.h"
+#include "virtagent.h"
+
+static bool va_enable_syslog = false; /* enable syslog'ing of RPCs */
+
+#define SLOG(msg, ...) do { \
+ char msg_buf[1024]; \
+ if (!va_enable_syslog) { \
+ break; \
+ } \
+ sprintf(msg_buf, msg, ## __VA_ARGS__); \
+ syslog(LOG_INFO, "virtagent, %s", msg_buf); \
+} while(0)
+
+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 {
+ VPDriver *vp;
+ int listen_fd;
+ xmlrpc_env env;
+ xmlrpc_registry *registry;
+} VARPCServerState;
+
+/* only one virtagent server instance can exist at a time */
+static VARPCServerState *server_state = NULL;
+
+static void va_accept_handler(void *opaque);
+
+static void va_rpc_send_cb(void *opaque)
+{
+ VARPCData *rpc_data = opaque;
+ VARPCServerState *s = server_state;
+
+ 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 = server_state;
+
+ 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)
+{
+ VARPCData *rpc_data;
+ int ret, fd;
+
+ TRACE("called");
+ fd = va_accept(server_state->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;
+ 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(server_state->listen_fd, NULL, NULL, NULL);
+}
+
+int va_server_init(VPDriver *vp_drv, bool is_host)
+{
+ RPCFunction *func_list = is_host ? host_functions : guest_functions;
+ QemuOpts *opts;
+ int ret, fd;
+ const char *path, *service_id;
+
+ if (server_state) {
+ LOG("virtagent server already initialized");
+ return -1;
+ }
+ va_enable_syslog = !is_host; /* enable logging for guest agent */
+
+ server_state = qemu_mallocz(sizeof(VARPCServerState));
+ service_id = is_host ? HOST_AGENT_SERVICE_ID : GUEST_AGENT_SERVICE_ID;
+ /* TODO: host agent path needs to be made unique amongst multiple
+ * qemu instances
+ */
+ path = is_host ? HOST_AGENT_PATH : GUEST_AGENT_PATH;
+
+ /* setup listening socket for server */
+ opts = qemu_opts_create(qemu_find_opts("net"), "va_server_opts", 0);
+ qemu_opt_set(opts, "path", path);
+ fd = unix_listen_opts(opts);
+ qemu_opts_del(opts);
+ if (fd < 0) {
+ LOG("error setting up listening socket");
+ goto out_bad;
+ }
+
+ /* tell virtproxy to forward incoming virtagent connections to the socket */
+ ret = vp_set_iforward(vp_drv, service_id, path, NULL, false);
+ if (ret < 0) {
+ LOG("error setting up virtproxy iforward");
+ goto out_bad;
+ }
+
+ server_state->vp = vp_drv;
+ server_state->listen_fd = fd;
+ xmlrpc_env_init(&server_state->env);
+ server_state->registry = xmlrpc_registry_new(&server_state->env);
+ va_register_functions(&server_state->env, server_state->registry, func_list);
+
+ TRACE("waiting for RPC request...");
+ vp_set_fd_handler(server_state->listen_fd, va_accept_handler, NULL,
+ server_state);
+
+ return 0;
+
+out_bad:
+ qemu_free(server_state);
+ server_state = NULL;
+ return -1;
+}
diff --git a/virtagent-daemon.h b/virtagent-daemon.h
new file mode 100644
index 0000000..6c3436a
--- /dev/null
+++ b/virtagent-daemon.h
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ *
+ */
+#include "virtproxy.h"
+
+#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_init(VPDriver *vp_drv, bool is_host);
--
1.7.0.4
next prev parent reply other threads:[~2010-11-16 16:02 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-11-16 16:01 [Qemu-devel] [RFC][PATCH v4 00/18] virtagent: host/guest RPC communication agent Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 01/18] virtagent: add common rpc transport defs Michael Roth
2010-11-18 13:53 ` Jes Sorensen
2010-11-18 16:33 ` Michael Roth
2010-11-16 16:01 ` Michael Roth [this message]
2010-11-18 13:57 ` [Qemu-devel] [RFC][PATCH v4 02/18] virtagent: base definitions for host/guest RPC server Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 03/18] virtagent: qemu-vp, integrate virtagent server Michael Roth
2010-11-18 14:02 ` Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 04/18] virtagent: base RPC client definitions Michael Roth
2010-11-18 14:10 ` Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 05/18] virtagent: add getfile RPC Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 06/18] virtagent: add agent_viewfile command Michael Roth
2010-11-18 14:13 ` Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 07/18] virtagent: add getdmesg RPC Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 08/18] virtagent: add agent_viewdmesg command Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 09/18] virtagent: add va_shutdown RPC Michael Roth
2010-11-18 14:17 ` Jes Sorensen
2010-11-18 15:35 ` Anthony Liguori
2010-11-18 15:41 ` Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 10/18] virtagent: add agent_shutdown monitor command Michael Roth
2010-11-18 14:19 ` Jes Sorensen
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 11/18] virtagent: add va_ping RPC Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 12/18] virtagent: add agent_ping monitor command Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 13/18] virtagent: add agent_capabilities monitor function Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 14/18] virtagent: add client capabilities init function Michael Roth
2010-11-18 14:22 ` Jes Sorensen
2010-11-18 16:43 ` Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 15/18] virtagent: add va_hello RPC function Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 16/18] virtagent: add va_send_hello() client function Michael Roth
2010-11-16 16:01 ` [Qemu-devel] [RFC][PATCH v4 17/18] virtagent: qemu-vp, va_send_hello() on startup Michael Roth
2010-11-18 14:22 ` Jes Sorensen
2010-11-16 16:02 ` [Qemu-devel] [RFC][PATCH v4 18/18] virtagent: Makefile/configure changes to build virtagent bits Michael Roth
2010-11-18 13:50 ` [Qemu-devel] [RFC][PATCH v4 00/18] virtagent: host/guest RPC communication agent Jes Sorensen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1289923320-5638-3-git-send-email-mdroth@linux.vnet.ibm.com \
--to=mdroth@linux.vnet.ibm.com \
--cc=abeekhof@redhat.com \
--cc=agl@linux.vnet.ibm.com \
--cc=aliguori@linux.vnet.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=ryanh@us.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).