From patchwork Wed Nov 3 11:35:31 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 69977 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A2113B6F0D for ; Wed, 3 Nov 2010 22:43:30 +1100 (EST) Received: from localhost ([127.0.0.1]:59296 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDbkV-0002i1-DI for incoming@patchwork.ozlabs.org; Wed, 03 Nov 2010 07:43:27 -0400 Received: from [140.186.70.92] (port=48364 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDbdi-0006nm-FF for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PDbda-0005QJ-4E for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:26 -0400 Received: from e4.ny.us.ibm.com ([32.97.182.144]:32805) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PDbdZ-0005Q3-VN for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:18 -0400 Received: from d01relay07.pok.ibm.com (d01relay07.pok.ibm.com [9.56.227.147]) by e4.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id oA3BJsUY019691 for ; Wed, 3 Nov 2010 07:19:54 -0400 Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay07.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oA3BaGg81773592 for ; Wed, 3 Nov 2010 07:36:16 -0400 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id oA3BaFdt013710 for ; Wed, 3 Nov 2010 07:36:16 -0400 Received: from localhost.localdomain (sig-9-76-99-21.mts.ibm.com [9.76.99.21]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id oA3Ba4ih013140; Wed, 3 Nov 2010 07:36:15 -0400 From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 3 Nov 2010 06:35:31 -0500 Message-Id: <1288784139-1110-3-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1288784139-1110-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1288784139-1110-1-git-send-email-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: aliguori@linux.vnet.ibm.com, agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com Subject: [Qemu-devel] [RFC][PATCH v2 02/10] virtagent: base definitions for host/guest RPC daemon X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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 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 + * Michael Roth + * + * 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 + * + * 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);