From patchwork Wed Nov 25 18:49:08 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark McLoughlin X-Patchwork-Id: 39452 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 DB9691007D3 for ; Thu, 26 Nov 2009 07:23:40 +1100 (EST) Received: from localhost ([127.0.0.1]:49249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NDOOn-0002Ur-RT for incoming@patchwork.ozlabs.org; Wed, 25 Nov 2009 15:23:37 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NDNaK-0007Mo-K4 for qemu-devel@nongnu.org; Wed, 25 Nov 2009 14:31:28 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1NDNaE-0007F2-Vy for qemu-devel@nongnu.org; Wed, 25 Nov 2009 14:31:27 -0500 Received: from [199.232.76.173] (port=42938 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NDNaE-0007Ei-OM for qemu-devel@nongnu.org; Wed, 25 Nov 2009 14:31:22 -0500 Received: from mx1.redhat.com ([209.132.183.28]:17529) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1NDNaE-0004G3-4h for qemu-devel@nongnu.org; Wed, 25 Nov 2009 14:31:22 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nAPIqO16001637 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 25 Nov 2009 13:52:24 -0500 Received: from blaa.localdomain (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id nAPIqMlN030217; Wed, 25 Nov 2009 13:52:23 -0500 Received: by blaa.localdomain (Postfix, from userid 500) id 0F480B0AB; Wed, 25 Nov 2009 18:49:41 +0000 (GMT) From: Mark McLoughlin To: qemu-devel@nongnu.org Date: Wed, 25 Nov 2009 18:49:08 +0000 Message-Id: <1259174977-26212-16-git-send-email-markmc@redhat.com> In-Reply-To: <1259174977-26212-1-git-send-email-markmc@redhat.com> References: <1259174977-26212-1-git-send-email-markmc@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Cc: Mark McLoughlin Subject: [Qemu-devel] [PATCH 15/44] net: convert socket to NetClientInfo 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 Signed-off-by: Mark McLoughlin --- net/socket.c | 74 +++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 45 insertions(+), 29 deletions(-) diff --git a/net/socket.c b/net/socket.c index 551fc2b..7331d87 100644 --- a/net/socket.c +++ b/net/socket.c @@ -33,7 +33,7 @@ #include "sysemu.h" typedef struct NetSocketState { - VLANClientState *vc; + VLANClientState nc; int fd; int state; /* 0 = getting length, 1 = getting data */ unsigned int index; @@ -50,9 +50,9 @@ typedef struct NetSocketListenState { } NetSocketListenState; /* XXX: we consider we can send the whole packet without blocking */ -static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) +static ssize_t net_socket_receive(VLANClientState *nc, const uint8_t *buf, size_t size) { - NetSocketState *s = vc->opaque; + NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); uint32_t len; len = htonl(size); @@ -60,9 +60,9 @@ static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_ return send_all(s->fd, buf, size); } -static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) +static ssize_t net_socket_receive_dgram(VLANClientState *nc, const uint8_t *buf, size_t size) { - NetSocketState *s = vc->opaque; + NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); return sendto(s->fd, (const void *)buf, size, 0, (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); @@ -124,7 +124,7 @@ static void net_socket_send(void *opaque) buf += l; size -= l; if (s->index >= s->packet_len) { - qemu_send_packet(s->vc, s->buf, s->packet_len); + qemu_send_packet(&s->nc, s->buf, s->packet_len); s->index = 0; s->state = 0; } @@ -146,7 +146,7 @@ static void net_socket_send_dgram(void *opaque) qemu_set_fd_handler(s->fd, NULL, NULL, NULL); return; } - qemu_send_packet(s->vc, s->buf, size); + qemu_send_packet(&s->nc, s->buf, size); } static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) @@ -209,14 +209,20 @@ fail: return -1; } -static void net_socket_cleanup(VLANClientState *vc) +static void net_socket_cleanup(VLANClientState *nc) { - NetSocketState *s = vc->opaque; + NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); qemu_set_fd_handler(s->fd, NULL, NULL, NULL); close(s->fd); - qemu_free(s); } +static NetClientInfo net_dgram_socket_info = { + .type = NET_CLIENT_TYPE_SOCKET, + .size = sizeof(NetSocketState), + .receive = net_socket_receive_dgram, + .cleanup = net_socket_cleanup, +}; + static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, const char *model, const char *name, @@ -225,6 +231,7 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, struct sockaddr_in saddr; int newfd; socklen_t saddr_len; + VLANClientState *nc; NetSocketState *s; /* fd passed: multicast: "learn" dgram_dst address from bound address and save it @@ -258,22 +265,22 @@ static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, } } - s = qemu_mallocz(sizeof(NetSocketState)); + nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name); + + snprintf(nc->info_str, sizeof(nc->info_str), + "socket: fd=%d (%s mcast=%s:%d)", + fd, is_connected ? "cloned" : "", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + + s = DO_UPCAST(NetSocketState, nc, nc); + s->fd = fd; - s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, - vlan, NULL, model, name, NULL, - net_socket_receive_dgram, NULL, NULL, - net_socket_cleanup, s); qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); /* mcast: save bound address as dst */ if (is_connected) s->dgram_dst=saddr; - snprintf(s->vc->info_str, sizeof(s->vc->info_str), - "socket: fd=%d (%s mcast=%s:%d)", - fd, is_connected? "cloned" : "", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); return s; } @@ -283,20 +290,29 @@ static void net_socket_connect(void *opaque) qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); } +static NetClientInfo net_socket_info = { + .type = NET_CLIENT_TYPE_SOCKET, + .size = sizeof(NetSocketState), + .receive = net_socket_receive, + .cleanup = net_socket_cleanup, +}; + static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, const char *model, const char *name, int fd, int is_connected) { + VLANClientState *nc; NetSocketState *s; - s = qemu_mallocz(sizeof(NetSocketState)); + + nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name); + + snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); + + s = DO_UPCAST(NetSocketState, nc, nc); + s->fd = fd; - s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, - vlan, NULL, model, name, NULL, - net_socket_receive, NULL, NULL, - net_socket_cleanup, s); - snprintf(s->vc->info_str, sizeof(s->vc->info_str), - "socket: fd=%d", fd); + if (is_connected) { net_socket_connect(s); } else { @@ -350,7 +366,7 @@ static void net_socket_accept(void *opaque) if (!s1) { closesocket(fd); } else { - snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), + snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), "socket: connection from %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); } @@ -443,7 +459,7 @@ static int net_socket_connect_init(VLANState *vlan, s = net_socket_fd_init(vlan, model, name, fd, connected); if (!s) return -1; - snprintf(s->vc->info_str, sizeof(s->vc->info_str), + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: connect to %s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); return 0; @@ -472,7 +488,7 @@ static int net_socket_mcast_init(VLANState *vlan, s->dgram_dst = saddr; - snprintf(s->vc->info_str, sizeof(s->vc->info_str), + snprintf(s->nc.info_str, sizeof(s->nc.info_str), "socket: mcast=%s:%d", inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); return 0;