From patchwork Mon Jan 25 22:24:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Xu X-Patchwork-Id: 8116831 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C127E9F818 for ; Mon, 25 Jan 2016 23:08:12 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E7B1220389 for ; Mon, 25 Jan 2016 23:08:11 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3233D20260 for ; Mon, 25 Jan 2016 23:08:11 +0000 (UTC) Received: from localhost ([::1]:41215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNqF0-0002yO-Gr for patchwork-qemu-devel@patchwork.kernel.org; Mon, 25 Jan 2016 18:08:10 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34637) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNpaI-0003n1-52 for qemu-devel@nongnu.org; Mon, 25 Jan 2016 17:26:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aNpaH-0008Gi-1w for qemu-devel@nongnu.org; Mon, 25 Jan 2016 17:26:06 -0500 Received: from mx1.redhat.com ([209.132.183.28]:40528) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aNpaG-0008Gd-Qg for qemu-devel@nongnu.org; Mon, 25 Jan 2016 17:26:04 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 52CEA5709 for ; Mon, 25 Jan 2016 22:26:04 +0000 (UTC) Received: from wei-thinkpad.nay.redhat.com (vpn1-5-80.pek2.redhat.com [10.72.5.80]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0PMOxvt001754; Mon, 25 Jan 2016 17:25:57 -0500 From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Tue, 26 Jan 2016 06:24:47 +0800 Message-Id: <1453760690-21221-8-git-send-email-wexu@redhat.com> In-Reply-To: <1453760690-21221-1-git-send-email-wexu@redhat.com> References: <1453760690-21221-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 X-Mailman-Approved-At: Mon, 25 Jan 2016 18:03:51 -0500 Cc: victork@redhat.com, mst@redhat.com, jasowang@redhat.com, yvugenfi@redhat.com, Wei Xu , marcel@redhat.com, dfleytma@redhat.com Subject: [Qemu-devel] [RFC Patch 07/10] TCP control packet handling. 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wei Xu All the 'SYN' packets will be bypassed, and flag with 'FIN/RST' set will signal all the cached packets in the same connection to be purged, this is to avoid out of data on the line. Signed-off-by: Wei Xu --- hw/net/virtio-net.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 1ca3dd5..042b538 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -41,6 +41,12 @@ #define VIRTIO_HEADER 12 /* Virtio net header size */ #define IP_OFFSET (VIRTIO_HEADER + sizeof(struct eth_header)) + +#define IP4_ADDR_OFFSET (IP_OFFSET + 12) /* ipv4 address start */ +#define TCP4_OFFSET (IP_OFFSET + sizeof(struct ip_header)) /* tcp4 header */ +#define TCP4_PORT_OFFSET TCP4_OFFSET /* tcp4 port offset */ +#define IP4_ADDR_SIZE 8 /* ipv4 saddr + daddr */ +#define TCP_PORT_SIZE 4 /* sport + dport */ #define TCP_WINDOW 65535 /* ip4 max payload, 16 bits in the header */ @@ -1850,6 +1856,27 @@ static int32_t virtio_net_rsc_try_coalesce4(NetRscChain *chain, o_data, &o_ip->ip_len, MAX_IP4_PAYLOAD); } + +/* Pakcets with 'SYN' should bypass, other flag should be sent after drain + * to prevent out of order */ +static int virtio_net_rsc_parse_tcp_ctrl(uint8_t *ip, uint16_t offset) +{ + uint16_t tcp_flag; + struct tcp_header *tcp; + + tcp = (struct tcp_header *)(ip + offset); + tcp_flag = htons(tcp->th_offset_flags) & 0x3F; + if (tcp_flag & TH_SYN) { + return RSC_BYPASS; + } + + if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) { + return RSC_FINAL; + } + + return 0; +} + static size_t virtio_net_rsc_callback(NetRscChain *chain, NetClientState *nc, const uint8_t *buf, size_t size, VirtioNetCoalesce *coalesce) { @@ -1895,12 +1922,51 @@ static size_t virtio_net_rsc_callback(NetRscChain *chain, NetClientState *nc, return virtio_net_rsc_cache_buf(chain, nc, buf, size); } +/* Drain a connection data, this is to avoid out of order segments */ +static size_t virtio_net_rsc_drain_one(NetRscChain *chain, NetClientState *nc, + const uint8_t *buf, size_t size, uint16_t ip_start, + uint16_t ip_size, uint16_t tcp_port, uint16_t port_size) +{ + NetRscSeg *seg, *nseg; + + QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) { + if (memcmp(buf + ip_start, seg->buf + ip_start, ip_size) + || memcmp(buf + tcp_port, seg->buf + tcp_port, port_size)) { + continue; + } + if ((chain->proto == ETH_P_IP) && seg->is_coalesced) { + virtio_net_rsc_ipv4_checksum(seg); + } + + virtio_net_do_receive(seg->nc, seg->buf, seg->size); + + QTAILQ_REMOVE(&chain->buffers, seg, next); + g_free(seg->buf); + g_free(seg); + break; + } + + return virtio_net_do_receive(nc, buf, size); +} static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, const uint8_t *buf, size_t size) { + int32_t ret; + struct ip_header *ip; NetRscChain *chain; chain = (NetRscChain *)opq; + ip = (struct ip_header *)(buf + IP_OFFSET); + + ret = virtio_net_rsc_parse_tcp_ctrl((uint8_t *)ip, + (0xF & ip->ip_ver_len) << 2); + if (RSC_BYPASS == ret) { + return virtio_net_do_receive(nc, buf, size); + } else if (RSC_FINAL == ret) { + return virtio_net_rsc_drain_one(chain, nc, buf, size, IP4_ADDR_OFFSET, + IP4_ADDR_SIZE, TCP4_PORT_OFFSET, TCP_PORT_SIZE); + } + return virtio_net_rsc_callback(chain, nc, buf, size, virtio_net_rsc_try_coalesce4); }