From patchwork Fri Nov 27 12:27:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Chen X-Patchwork-Id: 549447 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 610F2140180 for ; Fri, 27 Nov 2015 23:33:04 +1100 (AEDT) Received: from localhost ([::1]:56318 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2ID0-0001K3-60 for incoming@patchwork.ozlabs.org; Fri, 27 Nov 2015 07:33:02 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52491) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2I6g-0007NF-OI for qemu-devel@nongnu.org; Fri, 27 Nov 2015 07:26:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a2I6f-0002wB-MC for qemu-devel@nongnu.org; Fri, 27 Nov 2015 07:26:30 -0500 Received: from [59.151.112.132] (port=43907 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a2I6f-0002gO-5t for qemu-devel@nongnu.org; Fri, 27 Nov 2015 07:26:29 -0500 X-IronPort-AV: E=Sophos;i="5.20,346,1444665600"; d="scan'208";a="918282" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 27 Nov 2015 20:26:10 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 8CE284066D2A; Fri, 27 Nov 2015 20:25:57 +0800 (CST) Received: from G08FNSTD140215.g08.fujitsu.local (10.167.226.56) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Fri, 27 Nov 2015 20:25:57 +0800 From: Zhang Chen To: qemu devel , Jason Wang , Stefan Hajnoczi Date: Fri, 27 Nov 2015 20:27:28 +0800 Message-ID: <1448627251-11186-7-git-send-email-zhangchen.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1448627251-11186-1-git-send-email-zhangchen.fnst@cn.fujitsu.com> References: <1448627251-11186-1-git-send-email-zhangchen.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.56] X-yoursite-MailScanner-Information: Please contact the ISP for more information X-yoursite-MailScanner-ID: 8CE284066D2A.AF10E X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: zhangchen.fnst@cn.fujitsu.com X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: Li Zhijian , Gui jianfeng , "eddie.dong" , "Dr. David Alan Gilbert" , Huang peng , Gong lei , jan.kiszka@siemens.com, Zhang Chen , zhanghailiang Subject: [Qemu-devel] [RFC PATCH 6/9] net/colo-proxy: add packet forward function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: zhangchen The packet recv by primary forward to secondary The packet send by secondary forward to primary Signed-off-by: zhangchen --- net/colo-proxy.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/net/colo-proxy.c b/net/colo-proxy.c index ece5661..08a852f 100644 --- a/net/colo-proxy.c +++ b/net/colo-proxy.c @@ -26,6 +26,110 @@ static char *mode; static bool colo_do_checkpoint; /* + * Packets to be sent by colo forward to + * another colo + * return: >= 0 success + * < 0 failed + */ +static ssize_t colo_forward2another(NetFilterState *nf, + NetClientState *sender, + unsigned flags, + const struct iovec *iov, + int iovcnt, + NetPacketSent *sent_cb, + mode_type mode) +{ + ColoProxyState *s = FILTER_COLO_PROXY(nf); + ssize_t ret = 0; + ssize_t size = 0; + struct iovec sizeiov = { + .iov_base = &size, + .iov_len = 8 + }; + size = iov_size(iov, iovcnt); + if (!size) { + return 0; + } + + if (mode == COLO_PRIMARY_MODE) { + qemu_net_queue_send_iov(s->incoming_queue, sender, flags, + iov, iovcnt, NULL); + } + ret = iov_send(s->sockfd, &sizeiov, 8, 0, 8); + if (ret < 0) { + return ret; + } + ret = iov_send(s->sockfd, iov, iovcnt, 0, size); + return ret; +} + +/* + * recv and handle colo secondary + * forward packets in colo primary + */ +static void colo_proxy_primary_forward_handler(NetFilterState *nf) +{ + ColoProxyState *s = FILTER_COLO_PROXY(nf); + ssize_t len = 0; + ssize_t ret = 0; + struct iovec sizeiov = { + .iov_base = &len, + .iov_len = 8 + }; + if (s->sockfd < 0) { + printf("secondary forward disconnected\n"); + return; + } + iov_recv(s->sockfd, &sizeiov, 8, 0, 8); + DEBUG("primary_forward_handler recv lensbuf lens=%zu\n", len); + + if (len > 0) { + char *recvbuf; + recvbuf = g_malloc0(len); + struct iovec iov = { + .iov_base = recvbuf, + .iov_len = len + }; + iov_recv(s->sockfd, &iov, len, 0, len); + DEBUG("primary_forward_handler primary recvbuf=%s\n", recvbuf); + ret = colo_enqueue_secondary_packet(nf, recvbuf, len); + if (ret) { + DEBUG("colo_enqueue_secondary_packet succese\n"); + } else { + DEBUG("colo_enqueue_secondary_packet failed\n"); + } + g_free(recvbuf); + } +} + +/* + * recv and handle colo primary + * forward packets in colo secondary + */ +static void colo_proxy_secondary_forward_handler(NetFilterState *nf) +{ + ColoProxyState *s = FILTER_COLO_PROXY(nf); + ssize_t len = 0; + struct iovec sizeiov = { + .iov_base = &len, + .iov_len = 8 + }; + iov_recv(s->sockfd, &sizeiov, 8, 0, 8); + if (len > 0) { + char *buf; + buf = g_malloc0(len); + struct iovec iov = { + .iov_base = buf, + .iov_len = len + }; + iov_recv(s->sockfd, &iov, len, 0, len); + qemu_net_queue_send(s->incoming_queue, nf->netdev, + 0, (const uint8_t *)buf, len, NULL); + g_free(buf); + } +} + +/* * colo primary handle host's normal send and * recv packets to primary guest * return: >= 0 success @@ -63,7 +167,8 @@ static ssize_t colo_proxy_primary_handler(NetFilterState *nf, if (direction == NET_FILTER_DIRECTION_RX) { /* TODO: enqueue_primary_packet */ } else { - /* TODO: forward packets to another */ + ret = colo_forward2another(nf, sender, flags, iov, iovcnt, + sent_cb, COLO_PRIMARY_MODE); } return ret; @@ -107,7 +212,8 @@ static ssize_t colo_proxy_secondary_handler(NetFilterState *nf, iovcnt, NULL); return 1; } else { - /* TODO: forward packets to another */ + ret = colo_forward2another(nf, sender, flags, iov, iovcnt, + sent_cb, COLO_SECONDARY_MODE); } } else { @@ -178,7 +284,9 @@ static void colo_accept_incoming(ColoProxyState *s) return; } s->sockfd = acceptsock; - /* TODO: handle the packets that primary forward */ + qemu_set_fd_handler(s->sockfd, + (IOHandler *)colo_proxy_secondary_forward_handler, NULL, + (void *)s); return; } @@ -211,7 +319,9 @@ static ssize_t colo_proxy_primary_setup(NetFilterState *nf) } DEBUG("colo proxy connect success\n"); s->sockfd = sock; - /* TODO: handle the packets that secondary forward */ + qemu_set_fd_handler(s->sockfd, + (IOHandler *)colo_proxy_primary_forward_handler, + NULL, (void *)s); g_free(s->addr); return 1; }