From patchwork Wed Nov 3 15:28:08 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 70025 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 DEFEF1007D2 for ; Thu, 4 Nov 2010 02:45:18 +1100 (EST) Received: from localhost ([127.0.0.1]:39871 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDfWV-0003wC-J5 for incoming@patchwork.ozlabs.org; Wed, 03 Nov 2010 11:45:15 -0400 Received: from [140.186.70.92] (port=52173 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDfHb-0001uF-F4 for qemu-devel@nongnu.org; Wed, 03 Nov 2010 11:29:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PDfHY-0007FT-5h for qemu-devel@nongnu.org; Wed, 03 Nov 2010 11:29:50 -0400 Received: from e1.ny.us.ibm.com ([32.97.182.141]:50879) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PDfHY-0007FH-2I for qemu-devel@nongnu.org; Wed, 03 Nov 2010 11:29:48 -0400 Received: from d01relay06.pok.ibm.com (d01relay06.pok.ibm.com [9.56.227.116]) by e1.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id oA3FMIfR017048 for ; Wed, 3 Nov 2010 11:22:18 -0400 Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay06.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oA3FSvbj1773598 for ; Wed, 3 Nov 2010 11:28:57 -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 oA3FSupm008702 for ; Wed, 3 Nov 2010 11:28:57 -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 oA3FSVDI005744; Wed, 3 Nov 2010 11:28:55 -0400 From: Michael Roth To: qemu-devel@nongnu.org Date: Wed, 3 Nov 2010 10:28:08 -0500 Message-Id: <1288798090-7127-14-git-send-email-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0.4 In-Reply-To: <1288798090-7127-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1288798090-7127-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: abeekhof@redhat.com, agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com, aliguori@linux.vnet.ibm.com Subject: [Qemu-devel] [RFC][RESEND][PATCH v1 13/15] virtproxy: add read handler for proxied connections 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 reads data from client/server connections as they become readable, then sends the data over the channel Signed-off-by: Michael Roth --- virtproxy.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 80 insertions(+), 0 deletions(-) diff --git a/virtproxy.c b/virtproxy.c index 86a8e5b..f3f7f46 100644 --- a/virtproxy.c +++ b/virtproxy.c @@ -200,6 +200,86 @@ static VPIForward *get_iforward(const VPDriver *drv, const char *service_id) return NULL; } +/* read handler for proxied connections */ +static void vp_conn_read(void *opaque) +{ + VPConn *conn = opaque; + VPDriver *drv = conn->drv; + VPPacket pkt; + char buf[VP_CONN_DATA_LEN]; + int fd, count, ret; + bool client; + + TRACE("called with opaque: %p, drv: %p", opaque, drv); + + if (conn->state != VP_STATE_CONNECTED) { + LOG("invalid connection state"); + return; + } + + if (conn->type != VP_CONN_CLIENT && conn->type != VP_CONN_SERVER) { + LOG("invalid connection type"); + return; + } + + /* TODO: all fields should be explicitly set so we shouldn't + * need to memset. this might hurt if we beef up VPPacket size + */ + memset(&pkt, 0, sizeof(VPPacket)); + pkt.magic = VP_MAGIC; + + if (conn->type == VP_CONN_CLIENT) { + client = true; + fd = conn->client_fd; + } else { + client = false; + fd = conn->server_fd; + } + + count = read(fd, buf, VP_CONN_DATA_LEN); + if (count == -1) { + LOG("read() failed: %s", strerror(errno)); + return; + } else if (count == 0) { + /* connection closed, tell remote end to clean up */ + TRACE("connection closed"); + pkt.type = VP_PKT_CONTROL; + pkt.payload.msg.type = VP_CONTROL_CLOSE; + if (client) { + /* we're closing the client, have remote close the server conn */ + TRACE("closing connection for client fd %d", conn->client_fd); + pkt.payload.msg.args.close.client_fd = -1; + pkt.payload.msg.args.close.server_fd = conn->server_fd; + } else { + TRACE("closing connection for server fd %d", conn->server_fd); + pkt.payload.msg.args.close.server_fd = -1; + pkt.payload.msg.args.close.client_fd = conn->client_fd;; + } + /* clean up things on our end */ + closesocket(fd); + vp_set_fd_handler(fd, NULL, NULL, NULL); + QLIST_REMOVE(conn, next); + qemu_free(conn); + } else { + TRACE("data read"); + pkt.type = client ? VP_PKT_CLIENT : VP_PKT_SERVER; + pkt.payload.proxied.client_fd = conn->client_fd; + pkt.payload.proxied.server_fd = conn->server_fd; + memcpy(pkt.payload.proxied.data, buf, count); + pkt.payload.proxied.bytes = count; + } + + ret = vp_send_all(drv->channel_fd, (void *)&pkt, sizeof(VPPacket)); + if (ret == -1) { + LOG("error sending data over channel"); + return; + } + if (ret != sizeof(VPPacket)) { + TRACE("buffer full?"); + return; + } +} + /* accept handler for communication channel * * accept()s connection to communication channel (for sockets), and sets