Skip to contentSkip to conversation

Support Receive-Segment-Offload(RSC) for WHQL test of Window guest

PATCH In review [email protected] 3 participants 25 messages Mar 15, 2016 – Mar 18, 2016

lore
Share & tools
lore
#0 lore raw

cover Support Receive-Segment-Offload(RSC) for WHQL test of Window guest

From: Wei Xu <[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:
http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

 hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  75 +++++
 3 files changed, 677 insertions(+), 1 deletion(-)
Signature · 1 line
2.5.0
#1 lore raw

PATCH 1/2 virtio-net rsc: support coalescing ipv4 tcp traffic

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.

The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2, this is to notify upper layer TCP starting retransmission due to the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other flags, while it broke the test because windows set it to the same even it's not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass' and 'finalize', 'bypass' means should be sent out directly, and 'finalize' means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because this normally happens upon a connection is going to be closed, an 'URG' packet also finalize current coalescing unit while there maybe protocol difference to different OS.

Statistics can be used to monitor the basic coalescing status, the 'out of order' and 'out of window' means how many retransmitting packets, thus describe the performance intuitively.

---
 hw/net/virtio-net.c            | 486 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  72 ++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +485 -1
index 5798f87..c23b45f 100644
@@ -15,10 +15,12 @@
 #include "qemu/iov.h"
 #include "hw/virtio/virtio.h"
 #include "net/net.h"
+#include "net/eth.h"
 #include "net/checksum.h"
 #include "net/tap.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/sockets.h"
 #include "hw/virtio/virtio-net.h"
 #include "net/vhost_net.h"
 #include "hw/virtio/virtio-bus.h"
@@ -38,6 +40,35 @@
 #define endof(container, field) \
     (offsetof(container, field) + sizeof(((container *)0)->field))
 
+#define ETH_HDR_SZ (sizeof(struct eth_header))
+#define IP4_HDR_SZ (sizeof(struct ip_header))
+#define TCP_HDR_SZ (sizeof(struct tcp_header))
+#define ETH_IP4_HDR_SZ (ETH_HDR_SZ + IP4_HDR_SZ)
+
+#define IP4_ADDR_SIZE   8                   /* ipv4 saddr + daddr */
+#define TCP_PORT_SIZE   4                   /* sport + dport */
+
+/* IPv4 max payload, 16 bits in the header */
+#define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
+#define MAX_TCP_PAYLOAD 65535
+
+/* max payload with virtio header */
+#define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
+                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+
+#define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
+
+/* Purge coalesced packets timer interval */
+#define RSC_TIMER_INTERVAL  300000
+
+/* Switcher to enable/disable rsc */
+static bool virtio_net_rsc_bypass = 1;
+
+/* This value affects the performance a lot, and should be tuned carefully,
+   '300000'(300us) is the recommended value to pass the WHQL test, '50000' can
+   gain 2x netperf throughput with tso/gso/gro 'off'. */
+static uint32_t virtio_net_rsc_timeout = RSC_TIMER_INTERVAL;
+
 typedef struct VirtIOFeature {
     uint32_t flags;
     size_t end;
@@ -1089,7 +1120,8 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
     return 0;
 }
 
-static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
+static ssize_t virtio_net_do_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
@@ -1685,6 +1717,456 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    uint16_t ip_hdrlen;
+
+    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
+}
+
+static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
+{
+    uint32_t sum;
+
+    ip->ip_sum = 0;
+    sum = net_checksum_add_cont(IP4_HDR_SZ, (uint8_t *)ip, 0);
+    ip->ip_sum = cpu_to_be16(net_checksum_finish(sum));
+}
+
+static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
+{
+    int ret;
+
+    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
+    QTAILQ_REMOVE(&chain->buffers, seg, next);
+    g_free(seg->buf);
+    g_free(seg);
+
+    return ret;
+}
+
+static void virtio_net_rsc_purge(void *opq)
+{
+    NetRscChain *chain = (NetRscChain *)opq;
+    NetRscSeg *seg, *rn;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
+        if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.purge_failed++;
+            continue;
+        }
+    }
+
+    chain->stat.timer++;
+    if (!QTAILQ_EMPTY(&chain->buffers)) {
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+    }
+}
+
+static void virtio_net_rsc_cleanup(VirtIONet *n)
+{
+    NetRscChain *chain, *rn_chain;
+    NetRscSeg *seg, *rn_seg;
+
+    QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
+        QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
+            QTAILQ_REMOVE(&chain->buffers, seg, next);
+            g_free(seg->buf);
+            g_free(seg);
+        }
+
+        timer_del(chain->drain_timer);
+        timer_free(chain->drain_timer);
+        QTAILQ_REMOVE(&n->rsc_chains, chain, next);
+        g_free(chain);
+    }
+}
+
+static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
+                                     const uint8_t *buf, size_t size)
+{
+    NetRscSeg *seg;
+
+    seg = g_malloc(sizeof(NetRscSeg));
+    seg->buf = g_malloc(MAX_VIRTIO_PAYLOAD);
+
+    memmove(seg->buf, buf, size);
+    seg->size = size;
+    seg->dup_ack_count = 0;
+    seg->is_coalesced = 0;
+    seg->nc = nc;
+
+    QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
+    chain->stat.cache++;
+
+    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+}
+
+static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
+                                 const uint8_t *buf, struct tcp_header *n_tcp,
+                                 struct tcp_header *o_tcp)
+{
+    uint32_t nack, oack;
+    uint16_t nwin, owin;
+
+    nack = htonl(n_tcp->th_ack);
+    nwin = htons(n_tcp->th_win);
+    oack = htonl(o_tcp->th_ack);
+    owin = htons(o_tcp->th_win);
+
+    if ((nack - oack) >= MAX_TCP_PAYLOAD) {
+        chain->stat.ack_out_of_win++;
+        return RSC_FINAL;
+    } else if (nack == oack) {
+        /* duplicated ack or window probe */
+        if (nwin == owin) {
+            /* duplicated ack, add dup ack count due to whql test up to 1 */
+            chain->stat.dup_ack++;
+
+            if (seg->dup_ack_count == 0) {
+                seg->dup_ack_count++;
+                chain->stat.dup_ack1++;
+                return RSC_COALESCE;
+            } else {
+                /* Spec says should send it directly */
+                chain->stat.dup_ack2++;
+                return RSC_FINAL;
+            }
+        } else {
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain, NetRscSeg *seg,
+                                    const uint8_t *buf, NetRscUnit *n_unit)
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup ack or etc */
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp, o_unit->tcp);
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is different,
+           so use the field value to update and record the new data len */
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
+           for windows guest, while this may change the behavior for linux
+           guest. */
+        o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
+
+        o_unit->tcp->th_ack = n_unit->tcp->th_ack;
+        o_unit->tcp->th_win = n_unit->tcp->th_win;
+
+        memmove(seg->buf + seg->size, data, n_unit->payload);
+        seg->size += n_unit->payload;
+        chain->stat.coalesced++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
+        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+        chain->stat.no_match++;
+        return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
+/* Pakcets with 'SYN' should bypass, other flag should be sent after drain
+ * to prevent out of order */
+static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
+                                         struct tcp_header *tcp)
+{
+    uint16_t tcp_flag;
+
+    tcp_flag = htons(tcp->th_offset_flags) & 0x3F;
+    if (tcp_flag & TH_SYN) {
+        chain->stat.tcp_syn++;
+        return RSC_BYPASS;
+    }
+
+    if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) {
+        chain->stat.tcp_ctrl_drain++;
+        return RSC_FINAL;
+    }
+
+    return RSC_WANT;
+}
+
+static bool virtio_net_rsc_empty_cache(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size)
+{
+    if (QTAILQ_EMPTY(&chain->buffers)) {
+        chain->stat.empty_cache++;
+        virtio_net_rsc_cache_buf(chain, nc, buf, size);
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+        return 1;
+    }
+
+    return 0;
+}
+
+static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    int ret;
+    NetRscSeg *seg, *nseg;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
+        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+
+        if (ret == RSC_FINAL) {
+            if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+                /* Send failed */
+                chain->stat.final_failed++;
+                return 0;
+            }
+
+            /* Send current packet */
+            return virtio_net_do_receive(nc, buf, size);
+        } else if (ret == RSC_NO_MATCH) {
+            continue;
+        } else {
+            /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
+            seg->is_coalesced = 1;
+            return size;
+        }
+    }
+
+    chain->stat.no_match_cache++;
+    virtio_net_rsc_cache_buf(chain, nc, buf, size);
+    return size;
+}
+
+/* Drain a connection data, this is to avoid out of order segments */
+static size_t virtio_net_rsc_drain_flow(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 (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.drain_failed++;
+        }
+
+        break;
+    }
+
+    return virtio_net_do_receive(nc, buf, size);
+}
+
+static int32_t virtio_net_rsc_sanity_check4(NetRscChain *chain,
+                        struct ip_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP4_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    /* Not an ipv4 one */
+    if (((0xF0 & ip->ip_ver_len) >> 4) != IP_HEADER_VERSION_4) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip option */
+    if (IP4_HEADER_LEN != (0xF & ip->ip_ver_len)) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip fragment */
+    if (!(htons(ip->ip_off) & IP_DF)) {
+        chain->stat.ip_frag++;
+        return RSC_BYPASS;
+    }
+
+    if (ip->ip_p != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip_len);
+    if (ip_len < (IP4_HDR_SZ + TCP_HDR_SZ)
+        || ip_len > (size - chain->hdr_size - ETH_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit4(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 12), IP4_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP4_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
+static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
+                                            NetClientState *nc, uint16_t proto)
+{
+    NetRscChain *chain;
+
+    /* Only handle IPv4/6 */
+    if (proto != (uint16_t)ETH_P_IP) {
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        return virtio_net_rsc_receive(nc, buf, size);
+    }
+}
+
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
@@ -1814,6 +2296,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc = qemu_get_queue(n->nic);
     nc->rxfilter_notify_enabled = 1;
 
+    QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
                     virtio_net_save, virtio_net_load, n);
@@ -1848,6 +2331,7 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
+    virtio_net_rsc_cleanup(n);
 }
 
 static void virtio_net_instance_init(Object *obj)
include/hw/virtio/virtio-net.h +1 -0
index 0cabdb6..6939e92 100644
@@ -59,6 +59,7 @@ typedef struct VirtIONet {
     VirtIONetQueue *vqs;
     VirtQueue *ctrl_vq;
     NICState *nic;
+    QTAILQ_HEAD(, NetRscChain) rsc_chains;
     uint32_t tx_timeout;
     int32_t tx_burst;
     uint32_t has_vnet_hdr;
include/hw/virtio/virtio.h +72 -0

78 lines not on this page · raw message

Signature · 1 line
2.5.0
#2 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 76 lines · On Tue, Mar 15, 2016 at 05:17:03PM +0800, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.
The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2, this is to notify upper layer TCP starting retransmission due to the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other flags, while it broke the test because windows set it to the same even it's not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass' and 'finalize', 'bypass' means should be sent out directly, and 'finalize' means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because this normally happens upon a connection is going to be closed, an 'URG' packet also finalize current coalescing unit while there maybe protocol difference to different OS.

Statistics can be used to monitor the basic coalescing status, the 'out of order' and 'out of window' means how many retransmitting packets, thus describe the performance intuitively.

---
 hw/net/virtio-net.c            | 486 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  72 ++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +2 -0
index 5798f87..c23b45f 100644
@@ -15,10 +15,12 @@
 #include "qemu/iov.h"
 #include "hw/virtio/virtio.h"
 #include "net/net.h"
+#include "net/eth.h"
 #include "net/checksum.h"
 #include "net/tap.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/sockets.h"
 #include "hw/virtio/virtio-net.h"
 #include "net/vhost_net.h"
 #include "hw/virtio/virtio-bus.h"

hw/net/virtio-net.c line 46

Quoted 7 lines
hw/net/virtio-net.c +4 -0
@@ -38,6 +40,35 @@
 #define endof(container, field) \
     (offsetof(container, field) + sizeof(((container *)0)->field))
 
+#define ETH_HDR_SZ (sizeof(struct eth_header))
+#define IP4_HDR_SZ (sizeof(struct ip_header))
+#define TCP_HDR_SZ (sizeof(struct tcp_header))
+#define ETH_IP4_HDR_SZ (ETH_HDR_SZ + IP4_HDR_SZ)

It's better to open-code these imho.

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+
+#define IP4_ADDR_SIZE   8                   /* ipv4 saddr + daddr */
+#define TCP_PORT_SIZE   4                   /* sport + dport */
+
+/* IPv4 max payload, 16 bits in the header */
+#define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)

hw/net/virtio-net.c line 62

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+#define MAX_TCP_PAYLOAD 65535
+
+/* max payload with virtio header */
+#define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
+                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+
+#define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
+
+/* Purge coalesced packets timer interval */
+#define RSC_TIMER_INTERVAL  300000

Pls prefix local macros with VIRTIO_NET_

hw/net/virtio-net.c line 69

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+
+/* Switcher to enable/disable rsc */
+static bool virtio_net_rsc_bypass = 1;
+
+/* This value affects the performance a lot, and should be tuned carefully,
+   '300000'(300us) is the recommended value to pass the WHQL test, '50000' can
+   gain 2x netperf throughput with tso/gso/gro 'off'. */

So either tests pass or we get good performance, but not both? Hmm.

hw/net/virtio-net.c line 70

Quoted 1 line
hw/net/virtio-net.c +1 -0
+static uint32_t virtio_net_rsc_timeout = RSC_TIMER_INTERVAL;

This would beed to be tunable.

Quoted 584 lines
hw/net/virtio-net.c +455 -1
+
 typedef struct VirtIOFeature {
     uint32_t flags;
     size_t end;
@@ -1089,7 +1120,8 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
     return 0;
 }
 
-static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
+static ssize_t virtio_net_do_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
@@ -1685,6 +1717,456 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    uint16_t ip_hdrlen;
+
+    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
+}
+
+static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
+{
+    uint32_t sum;
+
+    ip->ip_sum = 0;
+    sum = net_checksum_add_cont(IP4_HDR_SZ, (uint8_t *)ip, 0);
+    ip->ip_sum = cpu_to_be16(net_checksum_finish(sum));
+}
+
+static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
+{
+    int ret;
+
+    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
+    QTAILQ_REMOVE(&chain->buffers, seg, next);
+    g_free(seg->buf);
+    g_free(seg);
+
+    return ret;
+}
+
+static void virtio_net_rsc_purge(void *opq)
+{
+    NetRscChain *chain = (NetRscChain *)opq;
+    NetRscSeg *seg, *rn;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
+        if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.purge_failed++;
+            continue;
+        }
+    }
+
+    chain->stat.timer++;
+    if (!QTAILQ_EMPTY(&chain->buffers)) {
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+    }
+}
+
+static void virtio_net_rsc_cleanup(VirtIONet *n)
+{
+    NetRscChain *chain, *rn_chain;
+    NetRscSeg *seg, *rn_seg;
+
+    QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
+        QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
+            QTAILQ_REMOVE(&chain->buffers, seg, next);
+            g_free(seg->buf);
+            g_free(seg);
+        }
+
+        timer_del(chain->drain_timer);
+        timer_free(chain->drain_timer);
+        QTAILQ_REMOVE(&n->rsc_chains, chain, next);
+        g_free(chain);
+    }
+}
+
+static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
+                                     const uint8_t *buf, size_t size)
+{
+    NetRscSeg *seg;
+
+    seg = g_malloc(sizeof(NetRscSeg));
+    seg->buf = g_malloc(MAX_VIRTIO_PAYLOAD);
+
+    memmove(seg->buf, buf, size);
+    seg->size = size;
+    seg->dup_ack_count = 0;
+    seg->is_coalesced = 0;
+    seg->nc = nc;
+
+    QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
+    chain->stat.cache++;
+
+    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+}
+
+static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
+                                 const uint8_t *buf, struct tcp_header *n_tcp,
+                                 struct tcp_header *o_tcp)
+{
+    uint32_t nack, oack;
+    uint16_t nwin, owin;
+
+    nack = htonl(n_tcp->th_ack);
+    nwin = htons(n_tcp->th_win);
+    oack = htonl(o_tcp->th_ack);
+    owin = htons(o_tcp->th_win);
+
+    if ((nack - oack) >= MAX_TCP_PAYLOAD) {
+        chain->stat.ack_out_of_win++;
+        return RSC_FINAL;
+    } else if (nack == oack) {
+        /* duplicated ack or window probe */
+        if (nwin == owin) {
+            /* duplicated ack, add dup ack count due to whql test up to 1 */
+            chain->stat.dup_ack++;
+
+            if (seg->dup_ack_count == 0) {
+                seg->dup_ack_count++;
+                chain->stat.dup_ack1++;
+                return RSC_COALESCE;
+            } else {
+                /* Spec says should send it directly */
+                chain->stat.dup_ack2++;
+                return RSC_FINAL;
+            }
+        } else {
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain, NetRscSeg *seg,
+                                    const uint8_t *buf, NetRscUnit *n_unit)
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup ack or etc */
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp, o_unit->tcp);
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is different,
+           so use the field value to update and record the new data len */
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
+           for windows guest, while this may change the behavior for linux
+           guest. */
+        o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
+
+        o_unit->tcp->th_ack = n_unit->tcp->th_ack;
+        o_unit->tcp->th_win = n_unit->tcp->th_win;
+
+        memmove(seg->buf + seg->size, data, n_unit->payload);
+        seg->size += n_unit->payload;
+        chain->stat.coalesced++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
+        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+        chain->stat.no_match++;
+        return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
+/* Pakcets with 'SYN' should bypass, other flag should be sent after drain
+ * to prevent out of order */
+static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
+                                         struct tcp_header *tcp)
+{
+    uint16_t tcp_flag;
+
+    tcp_flag = htons(tcp->th_offset_flags) & 0x3F;
+    if (tcp_flag & TH_SYN) {
+        chain->stat.tcp_syn++;
+        return RSC_BYPASS;
+    }
+
+    if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) {
+        chain->stat.tcp_ctrl_drain++;
+        return RSC_FINAL;
+    }
+
+    return RSC_WANT;
+}
+
+static bool virtio_net_rsc_empty_cache(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size)
+{
+    if (QTAILQ_EMPTY(&chain->buffers)) {
+        chain->stat.empty_cache++;
+        virtio_net_rsc_cache_buf(chain, nc, buf, size);
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+        return 1;
+    }
+
+    return 0;
+}
+
+static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    int ret;
+    NetRscSeg *seg, *nseg;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
+        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+
+        if (ret == RSC_FINAL) {
+            if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+                /* Send failed */
+                chain->stat.final_failed++;
+                return 0;
+            }
+
+            /* Send current packet */
+            return virtio_net_do_receive(nc, buf, size);
+        } else if (ret == RSC_NO_MATCH) {
+            continue;
+        } else {
+            /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
+            seg->is_coalesced = 1;
+            return size;
+        }
+    }
+
+    chain->stat.no_match_cache++;
+    virtio_net_rsc_cache_buf(chain, nc, buf, size);
+    return size;
+}
+
+/* Drain a connection data, this is to avoid out of order segments */
+static size_t virtio_net_rsc_drain_flow(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 (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.drain_failed++;
+        }
+
+        break;
+    }
+
+    return virtio_net_do_receive(nc, buf, size);
+}
+
+static int32_t virtio_net_rsc_sanity_check4(NetRscChain *chain,
+                        struct ip_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP4_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    /* Not an ipv4 one */
+    if (((0xF0 & ip->ip_ver_len) >> 4) != IP_HEADER_VERSION_4) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip option */
+    if (IP4_HEADER_LEN != (0xF & ip->ip_ver_len)) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip fragment */
+    if (!(htons(ip->ip_off) & IP_DF)) {
+        chain->stat.ip_frag++;
+        return RSC_BYPASS;
+    }
+
+    if (ip->ip_p != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip_len);
+    if (ip_len < (IP4_HDR_SZ + TCP_HDR_SZ)
+        || ip_len > (size - chain->hdr_size - ETH_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit4(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 12), IP4_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP4_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
+static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
+                                            NetClientState *nc, uint16_t proto)
+{
+    NetRscChain *chain;
+
+    /* Only handle IPv4/6 */
+    if (proto != (uint16_t)ETH_P_IP) {
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        return virtio_net_rsc_receive(nc, buf, size);
+    }
+}
+
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
@@ -1814,6 +2296,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc = qemu_get_queue(n->nic);
     nc->rxfilter_notify_enabled = 1;
 
+    QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
                     virtio_net_save, virtio_net_load, n);
@@ -1848,6 +2331,7 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
+    virtio_net_rsc_cleanup(n);
 }
 
 static void virtio_net_instance_init(Object *obj)
include/hw/virtio/virtio-net.h +1 -0
index 0cabdb6..6939e92 100644
@@ -59,6 +59,7 @@ typedef struct VirtIONet {
     VirtIONetQueue *vqs;
     VirtQueue *ctrl_vq;
     NICState *nic;
+    QTAILQ_HEAD(, NetRscChain) rsc_chains;
     uint32_t tx_timeout;
     int32_t tx_burst;
     uint32_t has_vnet_hdr;
include/hw/virtio/virtio.h +72 -0
index 2b5b248..3b1dfa8 100644
@@ -128,6 +128,78 @@ typedef struct VirtioDeviceClass {
     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
 } VirtioDeviceClass;
 
+/* Coalesced packets type & status */
+typedef enum {
+    RSC_COALESCE,           /* Data been coalesced */
+    RSC_FINAL,              /* Will terminate current connection */
+    RSC_NO_MATCH,           /* No matched in the buffer pool */
+    RSC_BYPASS,             /* Packet to be bypass, not tcp, tcp ctrl, etc */
+    RSC_WANT                /* Data want to be coalesced */
+} COALESCE_STATUS;
+
+typedef struct NetRscStat {
+    uint32_t received;
+    uint32_t coalesced;
+    uint32_t over_size;
+    uint32_t cache;
+    uint32_t empty_cache;
+    uint32_t no_match_cache;
+    uint32_t win_update;
+    uint32_t no_match;
+    uint32_t tcp_syn;
+    uint32_t tcp_ctrl_drain;
+    uint32_t dup_ack;
+    uint32_t dup_ack1;
+    uint32_t dup_ack2;
+    uint32_t pure_ack;
+    uint32_t ack_out_of_win;
+    uint32_t data_out_of_win;
+    uint32_t data_out_of_order;
+    uint32_t data_after_pure_ack;
+    uint32_t bypass_not_tcp;
+    uint32_t tcp_option;
+    uint32_t tcp_larger_option;
+    uint32_t ip_frag;
+    uint32_t ip_hacked;
+    uint32_t ip_option;
+    uint32_t purge_failed;
+    uint32_t drain_failed;
+    uint32_t final_failed;
+    int64_t  timer;
+} NetRscStat;
+
+/* Rsc unit general info used to checking if can coalescing */
+typedef struct NetRscUnit {
+   struct ip_header *ip;   /* ip header */
+   uint16_t *ip_plen;      /* data len pointer in ip header field */
+   struct tcp_header *tcp; /* tcp header */
+   uint16_t tcp_hdrlen;    /* tcp header len */
+   uint16_t payload;       /* pure payload without virtio/eth/ip/tcp */
+} NetRscUnit;
+
+/* Coalesced segmant */
+typedef struct NetRscSeg {
+    QTAILQ_ENTRY(NetRscSeg) next;
+    void *buf;
+    size_t size;
+    uint32_t dup_ack_count;
+    bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
+    NetRscUnit unit;
+    NetClientState *nc;
+} NetRscSeg;
+
+
+/* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
+typedef struct NetRscChain {
+   QTAILQ_ENTRY(NetRscChain) next;
+   uint16_t hdr_size;
+   uint16_t proto;
+   uint16_t max_payload;
+   QEMUTimer *drain_timer;
+   QTAILQ_HEAD(, NetRscSeg) buffers;
+   NetRscStat stat;
+} NetRscChain;
+
 void virtio_instance_init_common(Object *proxy_obj, void *data,
                                  size_t vdev_size, const char *vdev_name);
 
Signature · 1 line
2.5.0
author
#3 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

----- Original Message -----
From: "Michael S. Tsirkin" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Tuesday, March 15, 2016 6:00:03 PM
Subject: Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 76 lines · On Tue, Mar 15, 2016 at 05:17:03PM +0800, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.
The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2, this is to notify upper layer TCP starting retransmission due to the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other flags, while it broke the test because windows set it to the same even it's not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass' and 'finalize', 'bypass' means should be sent out directly, and 'finalize' means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because this normally happens upon a connection is going to be closed, an 'URG' packet also finalize current coalescing unit while there maybe protocol difference to different OS.

Statistics can be used to monitor the basic coalescing status, the 'out of order' and 'out of window' means how many retransmitting packets, thus describe the performance intuitively.

---
 hw/net/virtio-net.c            | 486 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  72 ++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +2 -0
index 5798f87..c23b45f 100644
@@ -15,10 +15,12 @@
 #include "qemu/iov.h"
 #include "hw/virtio/virtio.h"
 #include "net/net.h"
+#include "net/eth.h"
 #include "net/checksum.h"
 #include "net/tap.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/sockets.h"
 #include "hw/virtio/virtio-net.h"
 #include "net/vhost_net.h"
 #include "hw/virtio/virtio-bus.h"

hw/net/virtio-net.c line 46

Quoted 7 lines
hw/net/virtio-net.c +4 -0
@@ -38,6 +40,35 @@
 #define endof(container, field) \
     (offsetof(container, field) + sizeof(((container *)0)->field))
 
+#define ETH_HDR_SZ (sizeof(struct eth_header))
+#define IP4_HDR_SZ (sizeof(struct ip_header))
+#define TCP_HDR_SZ (sizeof(struct tcp_header))
+#define ETH_IP4_HDR_SZ (ETH_HDR_SZ + IP4_HDR_SZ)

It's better to open-code these imho.

okay.

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+
+#define IP4_ADDR_SIZE   8                   /* ipv4 saddr + daddr */
+#define TCP_PORT_SIZE   4                   /* sport + dport */
+
+/* IPv4 max payload, 16 bits in the header */
+#define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)

hw/net/virtio-net.c line 62

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+#define MAX_TCP_PAYLOAD 65535
+
+/* max payload with virtio header */
+#define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
+                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+
+#define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
+
+/* Purge coalesced packets timer interval */
+#define RSC_TIMER_INTERVAL  300000

Pls prefix local macros with VIRTIO_NET_

sure.

hw/net/virtio-net.c line 69

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+
+/* Switcher to enable/disable rsc */
+static bool virtio_net_rsc_bypass = 1;
+
+/* This value affects the performance a lot, and should be tuned carefully,
+   '300000'(300us) is the recommended value to pass the WHQL test, '50000' can
+   gain 2x netperf throughput with tso/gso/gro 'off'. */

So either tests pass or we get good performance, but not both? Hmm.

Yes, the test case send 6 data packets every 100us, and then capture and checking if some of the packets are coalesced, so the interval less than 100us is beat by the case definitely, actually it's really depends on the interval to pass the case, i tried 200/400/500+(us) but there are still a few cases failed, don't know how the internal of the test case is, maybe it is to bypass 'tso/gso' like feature in windows stack not to coalesce packets before sending out.

hw/net/virtio-net.c line 70

Quoted 1 line
hw/net/virtio-net.c +1 -0
+static uint32_t virtio_net_rsc_timeout = RSC_TIMER_INTERVAL;

This would beed to be tunable.
Yes, I have a question, can it be controlled by 'ethtool' or 'qmp/hmp'?

Quoted 584 lines
hw/net/virtio-net.c +455 -1
+
 typedef struct VirtIOFeature {
     uint32_t flags;
     size_t end;
@@ -1089,7 +1120,8 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
     return 0;
 }
 
-static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
+static ssize_t virtio_net_do_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
@@ -1685,6 +1717,456 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    uint16_t ip_hdrlen;
+
+    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
+}
+
+static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
+{
+    uint32_t sum;
+
+    ip->ip_sum = 0;
+    sum = net_checksum_add_cont(IP4_HDR_SZ, (uint8_t *)ip, 0);
+    ip->ip_sum = cpu_to_be16(net_checksum_finish(sum));
+}
+
+static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
+{
+    int ret;
+
+    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
+    QTAILQ_REMOVE(&chain->buffers, seg, next);
+    g_free(seg->buf);
+    g_free(seg);
+
+    return ret;
+}
+
+static void virtio_net_rsc_purge(void *opq)
+{
+    NetRscChain *chain = (NetRscChain *)opq;
+    NetRscSeg *seg, *rn;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
+        if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.purge_failed++;
+            continue;
+        }
+    }
+
+    chain->stat.timer++;
+    if (!QTAILQ_EMPTY(&chain->buffers)) {
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+    }
+}
+
+static void virtio_net_rsc_cleanup(VirtIONet *n)
+{
+    NetRscChain *chain, *rn_chain;
+    NetRscSeg *seg, *rn_seg;
+
+    QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
+        QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
+            QTAILQ_REMOVE(&chain->buffers, seg, next);
+            g_free(seg->buf);
+            g_free(seg);
+        }
+
+        timer_del(chain->drain_timer);
+        timer_free(chain->drain_timer);
+        QTAILQ_REMOVE(&n->rsc_chains, chain, next);
+        g_free(chain);
+    }
+}
+
+static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
+                                     const uint8_t *buf, size_t size)
+{
+    NetRscSeg *seg;
+
+    seg = g_malloc(sizeof(NetRscSeg));
+    seg->buf = g_malloc(MAX_VIRTIO_PAYLOAD);
+
+    memmove(seg->buf, buf, size);
+    seg->size = size;
+    seg->dup_ack_count = 0;
+    seg->is_coalesced = 0;
+    seg->nc = nc;
+
+    QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
+    chain->stat.cache++;
+
+    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+}
+
+static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
+                                 const uint8_t *buf, struct tcp_header *n_tcp,
+                                 struct tcp_header *o_tcp)
+{
+    uint32_t nack, oack;
+    uint16_t nwin, owin;
+
+    nack = htonl(n_tcp->th_ack);
+    nwin = htons(n_tcp->th_win);
+    oack = htonl(o_tcp->th_ack);
+    owin = htons(o_tcp->th_win);
+
+    if ((nack - oack) >= MAX_TCP_PAYLOAD) {
+        chain->stat.ack_out_of_win++;
+        return RSC_FINAL;
+    } else if (nack == oack) {
+        /* duplicated ack or window probe */
+        if (nwin == owin) {
+            /* duplicated ack, add dup ack count due to whql test up to 1 */
+            chain->stat.dup_ack++;
+
+            if (seg->dup_ack_count == 0) {
+                seg->dup_ack_count++;
+                chain->stat.dup_ack1++;
+                return RSC_COALESCE;
+            } else {
+                /* Spec says should send it directly */
+                chain->stat.dup_ack2++;
+                return RSC_FINAL;
+            }
+        } else {
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain, NetRscSeg *seg,
+                                    const uint8_t *buf, NetRscUnit *n_unit)
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup ack or etc */
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp, o_unit->tcp);
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is different,
+           so use the field value to update and record the new data len */
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
+           for windows guest, while this may change the behavior for linux
+           guest. */
+        o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
+
+        o_unit->tcp->th_ack = n_unit->tcp->th_ack;
+        o_unit->tcp->th_win = n_unit->tcp->th_win;
+
+        memmove(seg->buf + seg->size, data, n_unit->payload);
+        seg->size += n_unit->payload;
+        chain->stat.coalesced++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
+        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+        chain->stat.no_match++;
+        return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
+/* Pakcets with 'SYN' should bypass, other flag should be sent after drain
+ * to prevent out of order */
+static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
+                                         struct tcp_header *tcp)
+{
+    uint16_t tcp_flag;
+
+    tcp_flag = htons(tcp->th_offset_flags) & 0x3F;
+    if (tcp_flag & TH_SYN) {
+        chain->stat.tcp_syn++;
+        return RSC_BYPASS;
+    }
+
+    if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) {
+        chain->stat.tcp_ctrl_drain++;
+        return RSC_FINAL;
+    }
+
+    return RSC_WANT;
+}
+
+static bool virtio_net_rsc_empty_cache(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size)
+{
+    if (QTAILQ_EMPTY(&chain->buffers)) {
+        chain->stat.empty_cache++;
+        virtio_net_rsc_cache_buf(chain, nc, buf, size);
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+        return 1;
+    }
+
+    return 0;
+}
+
+static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    int ret;
+    NetRscSeg *seg, *nseg;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
+        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+
+        if (ret == RSC_FINAL) {
+            if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+                /* Send failed */
+                chain->stat.final_failed++;
+                return 0;
+            }
+
+            /* Send current packet */
+            return virtio_net_do_receive(nc, buf, size);
+        } else if (ret == RSC_NO_MATCH) {
+            continue;
+        } else {
+            /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
+            seg->is_coalesced = 1;
+            return size;
+        }
+    }
+
+    chain->stat.no_match_cache++;
+    virtio_net_rsc_cache_buf(chain, nc, buf, size);
+    return size;
+}
+
+/* Drain a connection data, this is to avoid out of order segments */
+static size_t virtio_net_rsc_drain_flow(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 (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.drain_failed++;
+        }
+
+        break;
+    }
+
+    return virtio_net_do_receive(nc, buf, size);
+}
+
+static int32_t virtio_net_rsc_sanity_check4(NetRscChain *chain,
+                        struct ip_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP4_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    /* Not an ipv4 one */
+    if (((0xF0 & ip->ip_ver_len) >> 4) != IP_HEADER_VERSION_4) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip option */
+    if (IP4_HEADER_LEN != (0xF & ip->ip_ver_len)) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip fragment */
+    if (!(htons(ip->ip_off) & IP_DF)) {
+        chain->stat.ip_frag++;
+        return RSC_BYPASS;
+    }
+
+    if (ip->ip_p != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip_len);
+    if (ip_len < (IP4_HDR_SZ + TCP_HDR_SZ)
+        || ip_len > (size - chain->hdr_size - ETH_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit4(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 12), IP4_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP4_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
+static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
+                                            NetClientState *nc, uint16_t proto)
+{
+    NetRscChain *chain;
+
+    /* Only handle IPv4/6 */
+    if (proto != (uint16_t)ETH_P_IP) {
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        return virtio_net_rsc_receive(nc, buf, size);
+    }
+}
+
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
@@ -1814,6 +2296,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc = qemu_get_queue(n->nic);
     nc->rxfilter_notify_enabled = 1;
 
+    QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
                     virtio_net_save, virtio_net_load, n);
@@ -1848,6 +2331,7 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
+    virtio_net_rsc_cleanup(n);
 }
 
 static void virtio_net_instance_init(Object *obj)
include/hw/virtio/virtio-net.h +1 -0
index 0cabdb6..6939e92 100644
@@ -59,6 +59,7 @@ typedef struct VirtIONet {
     VirtIONetQueue *vqs;
     VirtQueue *ctrl_vq;
     NICState *nic;
+    QTAILQ_HEAD(, NetRscChain) rsc_chains;
     uint32_t tx_timeout;
     int32_t tx_burst;
     uint32_t has_vnet_hdr;
include/hw/virtio/virtio.h +72 -0
index 2b5b248..3b1dfa8 100644
@@ -128,6 +128,78 @@ typedef struct VirtioDeviceClass {
     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
 } VirtioDeviceClass;
 
+/* Coalesced packets type & status */
+typedef enum {
+    RSC_COALESCE,           /* Data been coalesced */
+    RSC_FINAL,              /* Will terminate current connection */
+    RSC_NO_MATCH,           /* No matched in the buffer pool */
+    RSC_BYPASS,             /* Packet to be bypass, not tcp, tcp ctrl, etc */
+    RSC_WANT                /* Data want to be coalesced */
+} COALESCE_STATUS;
+
+typedef struct NetRscStat {
+    uint32_t received;
+    uint32_t coalesced;
+    uint32_t over_size;
+    uint32_t cache;
+    uint32_t empty_cache;
+    uint32_t no_match_cache;
+    uint32_t win_update;
+    uint32_t no_match;
+    uint32_t tcp_syn;
+    uint32_t tcp_ctrl_drain;
+    uint32_t dup_ack;
+    uint32_t dup_ack1;
+    uint32_t dup_ack2;
+    uint32_t pure_ack;
+    uint32_t ack_out_of_win;
+    uint32_t data_out_of_win;
+    uint32_t data_out_of_order;
+    uint32_t data_after_pure_ack;
+    uint32_t bypass_not_tcp;
+    uint32_t tcp_option;
+    uint32_t tcp_larger_option;
+    uint32_t ip_frag;
+    uint32_t ip_hacked;
+    uint32_t ip_option;
+    uint32_t purge_failed;
+    uint32_t drain_failed;
+    uint32_t final_failed;
+    int64_t  timer;
+} NetRscStat;
+
+/* Rsc unit general info used to checking if can coalescing */
+typedef struct NetRscUnit {
+   struct ip_header *ip;   /* ip header */
+   uint16_t *ip_plen;      /* data len pointer in ip header field */
+   struct tcp_header *tcp; /* tcp header */
+   uint16_t tcp_hdrlen;    /* tcp header len */
+   uint16_t payload;       /* pure payload without virtio/eth/ip/tcp */
+} NetRscUnit;
+
+/* Coalesced segmant */
+typedef struct NetRscSeg {
+    QTAILQ_ENTRY(NetRscSeg) next;
+    void *buf;
+    size_t size;
+    uint32_t dup_ack_count;
+    bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
+    NetRscUnit unit;
+    NetClientState *nc;
+} NetRscSeg;
+
+
+/* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
+typedef struct NetRscChain {
+   QTAILQ_ENTRY(NetRscChain) next;
+   uint16_t hdr_size;
+   uint16_t proto;
+   uint16_t max_payload;
+   QEMUTimer *drain_timer;
+   QTAILQ_HEAD(, NetRscSeg) buffers;
+   NetRscStat stat;
+} NetRscChain;
+
 void virtio_instance_init_common(Object *proxy_obj, void *data,
                                  size_t vdev_size, const char *vdev_name);
 
Signature · 1 line
2.5.0
#4 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

In reply to [email protected]
Quoted 42 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.

The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2, this is to notify upper layer TCP starting retransmission due to the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other flags, while it broke the test because windows set it to the same even it's not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass' and 'finalize', 'bypass' means should be sent out directly, and 'finalize' means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

Quoted 5 lines

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because this normally happens upon a connection is going to be closed, an 'URG' packet also finalize current coalescing unit while there maybe protocol difference to different OS.

But URG packet should be sent as quickly as possible regardless of ordering, no?

Quoted 38 lines

Statistics can be used to monitor the basic coalescing status, the 'out of order' and 'out of window' means how many retransmitting packets, thus describe the performance intuitively.

---
 hw/net/virtio-net.c            | 486 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  72 ++++++
 3 files changed, 558 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +7 -0
index 5798f87..c23b45f 100644
@@ -15,10 +15,12 @@
 #include "qemu/iov.h"
 #include "hw/virtio/virtio.h"
 #include "net/net.h"
+#include "net/eth.h"
 #include "net/checksum.h"
 #include "net/tap.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/sockets.h"
 #include "hw/virtio/virtio-net.h"
 #include "net/vhost_net.h"
 #include "hw/virtio/virtio-bus.h"
@@ -38,6 +40,35 @@
 #define endof(container, field) \
     (offsetof(container, field) + sizeof(((container *)0)->field))
 
+#define ETH_HDR_SZ (sizeof(struct eth_header))
+#define IP4_HDR_SZ (sizeof(struct ip_header))
+#define TCP_HDR_SZ (sizeof(struct tcp_header))
+#define ETH_IP4_HDR_SZ (ETH_HDR_SZ + IP4_HDR_SZ)
+

hw/net/virtio-net.c line 57

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+#define IP4_ADDR_SIZE   8                   /* ipv4 saddr + daddr */
+#define TCP_PORT_SIZE   4                   /* sport + dport */
+
+/* IPv4 max payload, 16 bits in the header */
+#define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
+#define MAX_TCP_PAYLOAD 65535
+
+/* max payload with virtio header */
+#define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
+                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)

Should we use guest_hdr_len instead of sizeof() here? Consider the vnet_hdr will be extended in the future.

hw/net/virtio-net.c line 59

Quoted 2 lines
hw/net/virtio-net.c +2 -0
+
+#define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */

type, should be 'length'

Quoted 16 lines
hw/net/virtio-net.c +12 -0
+
+/* Purge coalesced packets timer interval */
+#define RSC_TIMER_INTERVAL  300000
+
+/* Switcher to enable/disable rsc */
+static bool virtio_net_rsc_bypass = 1;
+
+/* This value affects the performance a lot, and should be tuned carefully,
+   '300000'(300us) is the recommended value to pass the WHQL test, '50000' can
+   gain 2x netperf throughput with tso/gso/gro 'off'. */
+static uint32_t virtio_net_rsc_timeout = RSC_TIMER_INTERVAL;
+
 typedef struct VirtIOFeature {
     uint32_t flags;
     size_t end;

hw/net/virtio-net.c static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) · line 1124

Quoted 6 lines
hw/net/virtio-net.c +2 -1
@@ -1089,7 +1120,8 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
     return 0;
 }
 
-static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
+static ssize_t virtio_net_do_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)

indentation should also changed here.

Quoted 79 lines
hw/net/virtio-net.c +72 -0
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
     VirtIONetQueue *q = virtio_net_get_subqueue(nc);
@@ -1685,6 +1717,456 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
     return 0;
 }
 
+static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    uint16_t ip_hdrlen;
+
+    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
+}
+
+static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
+{
+    uint32_t sum;
+
+    ip->ip_sum = 0;
+    sum = net_checksum_add_cont(IP4_HDR_SZ, (uint8_t *)ip, 0);
+    ip->ip_sum = cpu_to_be16(net_checksum_finish(sum));
+}
+
+static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
+{
+    int ret;
+
+    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
+    QTAILQ_REMOVE(&chain->buffers, seg, next);
+    g_free(seg->buf);
+    g_free(seg);
+
+    return ret;
+}
+
+static void virtio_net_rsc_purge(void *opq)
+{
+    NetRscChain *chain = (NetRscChain *)opq;
+    NetRscSeg *seg, *rn;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
+        if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.purge_failed++;
+            continue;
+        }
+    }
+
+    chain->stat.timer++;
+    if (!QTAILQ_EMPTY(&chain->buffers)) {
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+    }
+}
+
+static void virtio_net_rsc_cleanup(VirtIONet *n)
+{
+    NetRscChain *chain, *rn_chain;
+    NetRscSeg *seg, *rn_seg;
+
+    QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
+        QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
+            QTAILQ_REMOVE(&chain->buffers, seg, next);
+            g_free(seg->buf);
+            g_free(seg);
+        }
+
+        timer_del(chain->drain_timer);
+        timer_free(chain->drain_timer);
+        QTAILQ_REMOVE(&n->rsc_chains, chain, next);
+        g_free(chain);
+    }
+}

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1801

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+
+static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
+                                     const uint8_t *buf, size_t size)
+{
+    NetRscSeg *seg;
+
+    seg = g_malloc(sizeof(NetRscSeg));
+    seg->buf = g_malloc(MAX_VIRTIO_PAYLOAD);
+
+    memmove(seg->buf, buf, size);

Can seg->buf overlap with buf? If not, why use memmove() instead of memcpy()?

Quoted 42 lines
hw/net/virtio-net.c +42 -0
+    seg->size = size;
+    seg->dup_ack_count = 0;
+    seg->is_coalesced = 0;
+    seg->nc = nc;
+
+    QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
+    chain->stat.cache++;
+
+    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+}
+
+static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
+                                 const uint8_t *buf, struct tcp_header *n_tcp,
+                                 struct tcp_header *o_tcp)
+{
+    uint32_t nack, oack;
+    uint16_t nwin, owin;
+
+    nack = htonl(n_tcp->th_ack);
+    nwin = htons(n_tcp->th_win);
+    oack = htonl(o_tcp->th_ack);
+    owin = htons(o_tcp->th_win);
+
+    if ((nack - oack) >= MAX_TCP_PAYLOAD) {
+        chain->stat.ack_out_of_win++;
+        return RSC_FINAL;
+    } else if (nack == oack) {
+        /* duplicated ack or window probe */
+        if (nwin == owin) {
+            /* duplicated ack, add dup ack count due to whql test up to 1 */
+            chain->stat.dup_ack++;
+
+            if (seg->dup_ack_count == 0) {
+                seg->dup_ack_count++;
+                chain->stat.dup_ack1++;
+                return RSC_COALESCE;
+            } else {
+                /* Spec says should send it directly */
+                chain->stat.dup_ack2++;
+                return RSC_FINAL;
+            }
+        } else {

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1853

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;

Looks like there're something I missed. The spec said:

"In other words, any pure ACK that is not a duplicate ACK or a window update triggers an exception and must not be coalesced. All such pure ACKs must be indicated as individual segments."

Does it mean we *should not* coalesce windows update and pure ack? (Since it can wakeup transmission)?

Quoted 13 lines
hw/net/virtio-net.c +13 -0
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain, NetRscSeg *seg,
+                                    const uint8_t *buf, NetRscUnit *n_unit)
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1876

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {

What if n_unit->tcp_hdrlen > o_uint->tcp_hdr_len ?

Quoted 31 lines
hw/net/virtio-net.c +31 -0
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup ack or etc */
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp, o_unit->tcp);
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1917

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+        /* Here comes the right data, the payload lengh in v4/v6 is different,
+           so use the field value to update and record the new data len */
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
+           for windows guest, while this may change the behavior for linux
+           guest. */

This needs more thought, 'can' probably means don't. (Linux GRO won't merge PUSH packet).

Quoted 39 lines
hw/net/virtio-net.c +39 -0
+        o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
+
+        o_unit->tcp->th_ack = n_unit->tcp->th_ack;
+        o_unit->tcp->th_win = n_unit->tcp->th_win;
+
+        memmove(seg->buf + seg->size, data, n_unit->payload);
+        seg->size += n_unit->payload;
+        chain->stat.coalesced++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
+        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+        chain->stat.no_match++;
+        return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
+/* Pakcets with 'SYN' should bypass, other flag should be sent after drain
+ * to prevent out of order */
+static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
+                                         struct tcp_header *tcp)
+{
+    uint16_t tcp_flag;
+
+    tcp_flag = htons(tcp->th_offset_flags) & 0x3F;
+    if (tcp_flag & TH_SYN) {
+        chain->stat.tcp_syn++;
+        return RSC_BYPASS;
+    }
+

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1966

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+    if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) {
+        chain->stat.tcp_ctrl_drain++;
+        return RSC_FINAL;
+    }
+
+    return RSC_WANT;
+}
+
+static bool virtio_net_rsc_empty_cache(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size)

indentation looks wrong.

Quoted 5 lines
hw/net/virtio-net.c +5 -0
+{
+    if (QTAILQ_EMPTY(&chain->buffers)) {
+        chain->stat.empty_cache++;
+        virtio_net_rsc_cache_buf(chain, nc, buf, size);
+        timer_mod(chain->drain_timer,

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1981

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+        return 1;
+    }
+
+    return 0;
+}
+
+static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size, NetRscUnit *unit)
+{

and here.

Quoted 52 lines
hw/net/virtio-net.c +52 -0
+    int ret;
+    NetRscSeg *seg, *nseg;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
+        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+
+        if (ret == RSC_FINAL) {
+            if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+                /* Send failed */
+                chain->stat.final_failed++;
+                return 0;
+            }
+
+            /* Send current packet */
+            return virtio_net_do_receive(nc, buf, size);
+        } else if (ret == RSC_NO_MATCH) {
+            continue;
+        } else {
+            /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
+            seg->is_coalesced = 1;
+            return size;
+        }
+    }
+
+    chain->stat.no_match_cache++;
+    virtio_net_rsc_cache_buf(chain, nc, buf, size);
+    return size;
+}
+
+/* Drain a connection data, this is to avoid out of order segments */
+static size_t virtio_net_rsc_drain_flow(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 (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.drain_failed++;
+        }
+
+        break;
+    }
+
+    return virtio_net_do_receive(nc, buf, size);
+}
+
+static int32_t virtio_net_rsc_sanity_check4(NetRscChain *chain,

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2043

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+                        struct ip_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP4_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    /* Not an ipv4 one */
+    if (((0xF0 & ip->ip_ver_len) >> 4) != IP_HEADER_VERSION_4) {

I've replied this several times, please use a consistent style. E.g "ip->ip_ver_len & 0xF0".

Quoted 58 lines
hw/net/virtio-net.c +58 -0
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip option */
+    if (IP4_HEADER_LEN != (0xF & ip->ip_ver_len)) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip fragment */
+    if (!(htons(ip->ip_off) & IP_DF)) {
+        chain->stat.ip_frag++;
+        return RSC_BYPASS;
+    }
+
+    if (ip->ip_p != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip_len);
+    if (ip_len < (IP4_HDR_SZ + TCP_HDR_SZ)
+        || ip_len > (size - chain->hdr_size - ETH_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit4(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 12), IP4_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP4_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2111

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
+static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
+                                            NetClientState *nc, uint16_t proto)
+{
+    NetRscChain *chain;
+
+    /* Only handle IPv4/6 */
+    if (proto != (uint16_t)ETH_P_IP) {

The code is conflict with the comment above.

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2122

Quoted 11 lines
hw/net/virtio-net.c +11 -0
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;

Why introduce a specified field for instead of just use n->guest_hdr_len?

Quoted 32 lines
hw/net/virtio-net.c +32 -0
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

Quoted 119 lines
hw/net/virtio-net.c +7 -0
+    } else {
+        return virtio_net_rsc_receive(nc, buf, size);
+    }
+}
+
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
@@ -1814,6 +2296,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
     nc = qemu_get_queue(n->nic);
     nc->rxfilter_notify_enabled = 1;
 
+    QTAILQ_INIT(&n->rsc_chains);
     n->qdev = dev;
     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
                     virtio_net_save, virtio_net_load, n);
@@ -1848,6 +2331,7 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
     g_free(n->vqs);
     qemu_del_nic(n->nic);
     virtio_cleanup(vdev);
+    virtio_net_rsc_cleanup(n);
 }
 
 static void virtio_net_instance_init(Object *obj)
include/hw/virtio/virtio-net.h +1 -0
index 0cabdb6..6939e92 100644
@@ -59,6 +59,7 @@ typedef struct VirtIONet {
     VirtIONetQueue *vqs;
     VirtQueue *ctrl_vq;
     NICState *nic;
+    QTAILQ_HEAD(, NetRscChain) rsc_chains;
     uint32_t tx_timeout;
     int32_t tx_burst;
     uint32_t has_vnet_hdr;
include/hw/virtio/virtio.h +72 -0
index 2b5b248..3b1dfa8 100644
@@ -128,6 +128,78 @@ typedef struct VirtioDeviceClass {
     int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
 } VirtioDeviceClass;
 
+/* Coalesced packets type & status */
+typedef enum {
+    RSC_COALESCE,           /* Data been coalesced */
+    RSC_FINAL,              /* Will terminate current connection */
+    RSC_NO_MATCH,           /* No matched in the buffer pool */
+    RSC_BYPASS,             /* Packet to be bypass, not tcp, tcp ctrl, etc */
+    RSC_WANT                /* Data want to be coalesced */
+} COALESCE_STATUS;
+
+typedef struct NetRscStat {
+    uint32_t received;
+    uint32_t coalesced;
+    uint32_t over_size;
+    uint32_t cache;
+    uint32_t empty_cache;
+    uint32_t no_match_cache;
+    uint32_t win_update;
+    uint32_t no_match;
+    uint32_t tcp_syn;
+    uint32_t tcp_ctrl_drain;
+    uint32_t dup_ack;
+    uint32_t dup_ack1;
+    uint32_t dup_ack2;
+    uint32_t pure_ack;
+    uint32_t ack_out_of_win;
+    uint32_t data_out_of_win;
+    uint32_t data_out_of_order;
+    uint32_t data_after_pure_ack;
+    uint32_t bypass_not_tcp;
+    uint32_t tcp_option;
+    uint32_t tcp_larger_option;
+    uint32_t ip_frag;
+    uint32_t ip_hacked;
+    uint32_t ip_option;
+    uint32_t purge_failed;
+    uint32_t drain_failed;
+    uint32_t final_failed;
+    int64_t  timer;
+} NetRscStat;
+
+/* Rsc unit general info used to checking if can coalescing */
+typedef struct NetRscUnit {
+   struct ip_header *ip;   /* ip header */
+   uint16_t *ip_plen;      /* data len pointer in ip header field */
+   struct tcp_header *tcp; /* tcp header */
+   uint16_t tcp_hdrlen;    /* tcp header len */
+   uint16_t payload;       /* pure payload without virtio/eth/ip/tcp */
+} NetRscUnit;
+
+/* Coalesced segmant */
+typedef struct NetRscSeg {
+    QTAILQ_ENTRY(NetRscSeg) next;
+    void *buf;
+    size_t size;
+    uint32_t dup_ack_count;
+    bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
+    NetRscUnit unit;
+    NetClientState *nc;
+} NetRscSeg;
+
+
+/* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
+typedef struct NetRscChain {
+   QTAILQ_ENTRY(NetRscChain) next;
+   uint16_t hdr_size;
+   uint16_t proto;
+   uint16_t max_payload;
+   QEMUTimer *drain_timer;
+   QTAILQ_HEAD(, NetRscSeg) buffers;
+   NetRscStat stat;
+} NetRscChain;
+
 void virtio_instance_init_common(Object *proxy_obj, void *data,
                                  size_t vdev_size, const char *vdev_name);
 
author
#5 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 44 lines · Jason Wang
Quoted 42 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.

The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2, this is to notify upper layer TCP starting retransmission due to the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other flags, while it broke the test because windows set it to the same even it's not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass' and 'finalize', 'bypass' means should be sent out directly, and 'finalize' means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

Quoted 7 lines
Quoted 5 lines

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because this normally happens upon a connection is going to be closed, an 'URG' packet also finalize current coalescing unit while there maybe protocol difference to different OS.

But URG packet should be sent as quickly as possible regardless of ordering, no?

Yes, you right, URG will terminate the current 'SCU', i'll amend the commit log.

Quoted 42 lines
Quoted 42 lines

Statistics can be used to monitor the basic coalescing status, the 'out of order' and 'out of window' means how many retransmitting packets, thus describe the performance intuitively.

---
  hw/net/virtio-net.c            | 486 ++++++++++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-net.h |   1 +
  include/hw/virtio/virtio.h     |  72 ++++++
  3 files changed, 558 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +11 -0
index 5798f87..c23b45f 100644
@@ -15,10 +15,12 @@
  #include "qemu/iov.h"
  #include "hw/virtio/virtio.h"
  #include "net/net.h"
+#include "net/eth.h"
  #include "net/checksum.h"
  #include "net/tap.h"
  #include "qemu/error-report.h"
  #include "qemu/timer.h"
+#include "qemu/sockets.h"
  #include "hw/virtio/virtio-net.h"
  #include "net/vhost_net.h"
  #include "hw/virtio/virtio-bus.h"
@@ -38,6 +40,35 @@
  #define endof(container, field) \
      (offsetof(container, field) + sizeof(((container *)0)->field))
 
+#define ETH_HDR_SZ (sizeof(struct eth_header))
+#define IP4_HDR_SZ (sizeof(struct ip_header))
+#define TCP_HDR_SZ (sizeof(struct tcp_header))
+#define ETH_IP4_HDR_SZ (ETH_HDR_SZ + IP4_HDR_SZ)
+
+#define IP4_ADDR_SIZE   8                   /* ipv4 saddr + daddr */
+#define TCP_PORT_SIZE   4                   /* sport + dport */
+
+/* IPv4 max payload, 16 bits in the header */
Quoted 8 lines

hw/net/virtio-net.c line 57

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+#define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
+#define MAX_TCP_PAYLOAD 65535
+
+/* max payload with virtio header */
+#define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
+                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)

Should we use guest_hdr_len instead of sizeof() here? Consider the vnet_hdr will be extended in the future.

Sure.

Quoted 4 lines

hw/net/virtio-net.c line 59

Quoted 3 lines
hw/net/virtio-net.c +2 -0
+
+#define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */

type, should be 'length'

ok.

Quoted 20 lines
Quoted 20 lines
hw/net/virtio-net.c +12 -0
+
+/* Purge coalesced packets timer interval */
+#define RSC_TIMER_INTERVAL  300000
+
+/* Switcher to enable/disable rsc */
+static bool virtio_net_rsc_bypass = 1;
+
+/* This value affects the performance a lot, and should be tuned carefully,
+   '300000'(300us) is the recommended value to pass the WHQL test, '50000' can
+   gain 2x netperf throughput with tso/gso/gro 'off'. */
+static uint32_t virtio_net_rsc_timeout = RSC_TIMER_INTERVAL;
+
  typedef struct VirtIOFeature {
      uint32_t flags;
      size_t end;
@@ -1089,7 +1120,8 @@ static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
      return 0;
  }
 
Quoted 4 lines

hw/net/virtio-net.c static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) · line 1124

Quoted 3 lines
hw/net/virtio-net.c +2 -1
-static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
+static ssize_t virtio_net_do_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)

indentation should also changed here.

ok.

Quoted 84 lines
Quoted 84 lines
hw/net/virtio-net.c +76 -0
  {
      VirtIONet *n = qemu_get_nic_opaque(nc);
      VirtIONetQueue *q = virtio_net_get_subqueue(nc);
@@ -1685,6 +1717,456 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
      return 0;
  }
 
+static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    uint16_t ip_hdrlen;
+
+    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
+}
+
+static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
+{
+    uint32_t sum;
+
+    ip->ip_sum = 0;
+    sum = net_checksum_add_cont(IP4_HDR_SZ, (uint8_t *)ip, 0);
+    ip->ip_sum = cpu_to_be16(net_checksum_finish(sum));
+}
+
+static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
+{
+    int ret;
+
+    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
+    QTAILQ_REMOVE(&chain->buffers, seg, next);
+    g_free(seg->buf);
+    g_free(seg);
+
+    return ret;
+}
+
+static void virtio_net_rsc_purge(void *opq)
+{
+    NetRscChain *chain = (NetRscChain *)opq;
+    NetRscSeg *seg, *rn;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn) {
+        if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.purge_failed++;
+            continue;
+        }
+    }
+
+    chain->stat.timer++;
+    if (!QTAILQ_EMPTY(&chain->buffers)) {
+        timer_mod(chain->drain_timer,
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+    }
+}
+
+static void virtio_net_rsc_cleanup(VirtIONet *n)
+{
+    NetRscChain *chain, *rn_chain;
+    NetRscSeg *seg, *rn_seg;
+
+    QTAILQ_FOREACH_SAFE(chain, &n->rsc_chains, next, rn_chain) {
+        QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, rn_seg) {
+            QTAILQ_REMOVE(&chain->buffers, seg, next);
+            g_free(seg->buf);
+            g_free(seg);
+        }
+
+        timer_del(chain->drain_timer);
+        timer_free(chain->drain_timer);
+        QTAILQ_REMOVE(&n->rsc_chains, chain, next);
+        g_free(chain);
+    }
+}
+
+static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
+                                     const uint8_t *buf, size_t size)
+{
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1801

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+    NetRscSeg *seg;
+
+    seg = g_malloc(sizeof(NetRscSeg));
+    seg->buf = g_malloc(MAX_VIRTIO_PAYLOAD);
+
+    memmove(seg->buf, buf, size);

Can seg->buf overlap with buf? If not, why use memmove() instead of memcpy()?

Yes, they will not overlap, memcpy() is good for it.

Quoted 55 lines
Quoted 43 lines
hw/net/virtio-net.c +42 -0
+    seg->size = size;
+    seg->dup_ack_count = 0;
+    seg->is_coalesced = 0;
+    seg->nc = nc;
+
+    QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
+    chain->stat.cache++;
+
+    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+}
+
+static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
+                                 const uint8_t *buf, struct tcp_header *n_tcp,
+                                 struct tcp_header *o_tcp)
+{
+    uint32_t nack, oack;
+    uint16_t nwin, owin;
+
+    nack = htonl(n_tcp->th_ack);
+    nwin = htons(n_tcp->th_win);
+    oack = htonl(o_tcp->th_ack);
+    owin = htons(o_tcp->th_win);
+
+    if ((nack - oack) >= MAX_TCP_PAYLOAD) {
+        chain->stat.ack_out_of_win++;
+        return RSC_FINAL;
+    } else if (nack == oack) {
+        /* duplicated ack or window probe */
+        if (nwin == owin) {
+            /* duplicated ack, add dup ack count due to whql test up to 1 */
+            chain->stat.dup_ack++;
+
+            if (seg->dup_ack_count == 0) {
+                seg->dup_ack_count++;
+                chain->stat.dup_ack1++;
+                return RSC_COALESCE;
+            } else {
+                /* Spec says should send it directly */
+                chain->stat.dup_ack2++;
+                return RSC_FINAL;
+            }
+        } else {

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1853

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;

Looks like there're something I missed. The spec said:

Quoted 6 lines

"In other words, any pure ACK that is not a duplicate ACK or a window update triggers an exception and must not be coalesced. All such pure ACKs must be indicated as individual segments."

Does it mean we *should not* coalesce windows update and pure ack? (Since it can wakeup transmission)?

It's also a little bit inexplicit and flexible due to the spec, please see the flowchart I on the same page.

Comments about the flowchart:
------------------------------------------------------------------------
The first of the following two flowcharts describes the rules for coalescing segments and updating the TCP headers.
This flowchart refers to mechanisms for distinguishing valid duplicate ACKs and window updates. The second flowchart describes these mechanisms.
------------------------------------------------------------------------
As show in the flowchart, only status 'C' will break current scu and get finalized, both 'A' and 'B' can be coalesced afaik.

Quoted 17 lines
Quoted 17 lines
hw/net/virtio-net.c +16 -0
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain, NetRscSeg *seg,
+                                    const uint8_t *buf, NetRscUnit *n_unit)
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1876

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {

What if n_unit->tcp_hdrlen > o_uint->tcp_hdr_len ?

do you mean '<'? that also means some option changed, maybe i should include it.

Quoted 36 lines
Quoted 36 lines
hw/net/virtio-net.c +35 -0
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup ack or etc */
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp, o_unit->tcp);
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is different,
+           so use the field value to update and record the new data len */
+        o_unit->payload += n_unit->payload; /* update new data len */
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1917

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be coalesced
+           for windows guest, while this may change the behavior for linux
+           guest. */

This needs more thought, 'can' probably means don't. (Linux GRO won't merge PUSH packet).

Yes, since it's mainly for win guest, how about take this as this first and do more test and see how to handle it?

Quoted 43 lines
Quoted 43 lines
hw/net/virtio-net.c +42 -0
+        o_unit->tcp->th_offset_flags = n_unit->tcp->th_offset_flags;
+
+        o_unit->tcp->th_ack = n_unit->tcp->th_ack;
+        o_unit->tcp->th_win = n_unit->tcp->th_win;
+
+        memmove(seg->buf + seg->size, data, n_unit->payload);
+        seg->size += n_unit->payload;
+        chain->stat.coalesced++;
+        return RSC_COALESCE;
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
+        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+        chain->stat.no_match++;
+        return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
+/* Pakcets with 'SYN' should bypass, other flag should be sent after drain
+ * to prevent out of order */
+static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
+                                         struct tcp_header *tcp)
+{
+    uint16_t tcp_flag;
+
+    tcp_flag = htons(tcp->th_offset_flags) & 0x3F;
+    if (tcp_flag & TH_SYN) {
+        chain->stat.tcp_syn++;
+        return RSC_BYPASS;
+    }
+
+    if (tcp_flag & (TH_FIN | TH_URG | TH_RST)) {
+        chain->stat.tcp_ctrl_drain++;
+        return RSC_FINAL;
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1966

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+    }
+
+    return RSC_WANT;
+}
+
+static bool virtio_net_rsc_empty_cache(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size)

indentation looks wrong.

ok.

Quoted 17 lines
Quoted 6 lines
hw/net/virtio-net.c +5 -0
+{
+    if (QTAILQ_EMPTY(&chain->buffers)) {
+        chain->stat.empty_cache++;
+        virtio_net_rsc_cache_buf(chain, nc, buf, size);
+        timer_mod(chain->drain_timer,

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1981

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+              qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + virtio_net_rsc_timeout);
+        return 1;
+    }
+
+    return 0;
+}
+
+static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
+                          const uint8_t *buf, size_t size, NetRscUnit *unit)
+{

and here.

ok.

Quoted 57 lines
Quoted 57 lines
hw/net/virtio-net.c +56 -0
+    int ret;
+    NetRscSeg *seg, *nseg;
+
+    QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
+        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+
+        if (ret == RSC_FINAL) {
+            if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+                /* Send failed */
+                chain->stat.final_failed++;
+                return 0;
+            }
+
+            /* Send current packet */
+            return virtio_net_do_receive(nc, buf, size);
+        } else if (ret == RSC_NO_MATCH) {
+            continue;
+        } else {
+            /* Coalesced, mark coalesced flag to tell calc cksum for ipv4 */
+            seg->is_coalesced = 1;
+            return size;
+        }
+    }
+
+    chain->stat.no_match_cache++;
+    virtio_net_rsc_cache_buf(chain, nc, buf, size);
+    return size;
+}
+
+/* Drain a connection data, this is to avoid out of order segments */
+static size_t virtio_net_rsc_drain_flow(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 (virtio_net_rsc_drain_seg(chain, seg) == 0) {
+            chain->stat.drain_failed++;
+        }
+
+        break;
+    }
+
+    return virtio_net_do_receive(nc, buf, size);
+}
+
+static int32_t virtio_net_rsc_sanity_check4(NetRscChain *chain,
+                        struct ip_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2043

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+    if (size < (chain->hdr_size + ETH_IP4_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    /* Not an ipv4 one */
+    if (((0xF0 & ip->ip_ver_len) >> 4) != IP_HEADER_VERSION_4) {

I've replied this several times, please use a consistent style. E.g "ip->ip_ver_len & 0xF0".

Sorry, i used to think you meant the 'IP_HEADER_VERSION_4'.

Quoted 62 lines
Quoted 62 lines
hw/net/virtio-net.c +61 -0
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip option */
+    if (IP4_HEADER_LEN != (0xF & ip->ip_ver_len)) {
+        chain->stat.ip_option++;
+        return RSC_BYPASS;
+    }
+
+    /* Don't handle packets with ip fragment */
+    if (!(htons(ip->ip_off) & IP_DF)) {
+        chain->stat.ip_frag++;
+        return RSC_BYPASS;
+    }
+
+    if (ip->ip_p != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip_len);
+    if (ip_len < (IP4_HDR_SZ + TCP_HDR_SZ)
+        || ip_len > (size - chain->hdr_size - ETH_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit4(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 12), IP4_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP4_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2111

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
+                                            NetClientState *nc, uint16_t proto)
+{
+    NetRscChain *chain;
+
+    /* Only handle IPv4/6 */
+    if (proto != (uint16_t)ETH_P_IP) {

The code is conflict with the comment above.

ok.

Quoted 13 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2122

Quoted 12 lines
hw/net/virtio-net.c +11 -0
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;

Why introduce a specified field for instead of just use n->guest_hdr_len?

this is to reduce invoking times to find VirtIONet by 'nc', there are a few places will use it.

Quoted 37 lines
Quoted 37 lines
hw/net/virtio-net.c +36 -0
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

yes, not sure which way is good to support this, hmp/qmp/ethtool, this is gonna to support win guest, so need a well-compatible interface, any comments?

Quoted 122 lines
Quoted 120 lines
hw/net/virtio-net.c +7 -0
+    } else {
+        return virtio_net_rsc_receive(nc, buf, size);
+    }
+}
+
  static NetClientInfo net_virtio_info = {
      .type = NET_CLIENT_OPTIONS_KIND_NIC,
      .size = sizeof(NICState),
@@ -1814,6 +2296,7 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
      nc = qemu_get_queue(n->nic);
      nc->rxfilter_notify_enabled = 1;
 
+    QTAILQ_INIT(&n->rsc_chains);
      n->qdev = dev;
      register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
                      virtio_net_save, virtio_net_load, n);
@@ -1848,6 +2331,7 @@ static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
      g_free(n->vqs);
      qemu_del_nic(n->nic);
      virtio_cleanup(vdev);
+    virtio_net_rsc_cleanup(n);
  }
 
  static void virtio_net_instance_init(Object *obj)
include/hw/virtio/virtio-net.h +1 -0
index 0cabdb6..6939e92 100644
@@ -59,6 +59,7 @@ typedef struct VirtIONet {
      VirtIONetQueue *vqs;
      VirtQueue *ctrl_vq;
      NICState *nic;
+    QTAILQ_HEAD(, NetRscChain) rsc_chains;
      uint32_t tx_timeout;
      int32_t tx_burst;
      uint32_t has_vnet_hdr;
include/hw/virtio/virtio.h +72 -0
index 2b5b248..3b1dfa8 100644
@@ -128,6 +128,78 @@ typedef struct VirtioDeviceClass {
      int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id);
  } VirtioDeviceClass;
 
+/* Coalesced packets type & status */
+typedef enum {
+    RSC_COALESCE,           /* Data been coalesced */
+    RSC_FINAL,              /* Will terminate current connection */
+    RSC_NO_MATCH,           /* No matched in the buffer pool */
+    RSC_BYPASS,             /* Packet to be bypass, not tcp, tcp ctrl, etc */
+    RSC_WANT                /* Data want to be coalesced */
+} COALESCE_STATUS;
+
+typedef struct NetRscStat {
+    uint32_t received;
+    uint32_t coalesced;
+    uint32_t over_size;
+    uint32_t cache;
+    uint32_t empty_cache;
+    uint32_t no_match_cache;
+    uint32_t win_update;
+    uint32_t no_match;
+    uint32_t tcp_syn;
+    uint32_t tcp_ctrl_drain;
+    uint32_t dup_ack;
+    uint32_t dup_ack1;
+    uint32_t dup_ack2;
+    uint32_t pure_ack;
+    uint32_t ack_out_of_win;
+    uint32_t data_out_of_win;
+    uint32_t data_out_of_order;
+    uint32_t data_after_pure_ack;
+    uint32_t bypass_not_tcp;
+    uint32_t tcp_option;
+    uint32_t tcp_larger_option;
+    uint32_t ip_frag;
+    uint32_t ip_hacked;
+    uint32_t ip_option;
+    uint32_t purge_failed;
+    uint32_t drain_failed;
+    uint32_t final_failed;
+    int64_t  timer;
+} NetRscStat;
+
+/* Rsc unit general info used to checking if can coalescing */
+typedef struct NetRscUnit {
+   struct ip_header *ip;   /* ip header */
+   uint16_t *ip_plen;      /* data len pointer in ip header field */
+   struct tcp_header *tcp; /* tcp header */
+   uint16_t tcp_hdrlen;    /* tcp header len */
+   uint16_t payload;       /* pure payload without virtio/eth/ip/tcp */
+} NetRscUnit;
+
+/* Coalesced segmant */
+typedef struct NetRscSeg {
+    QTAILQ_ENTRY(NetRscSeg) next;
+    void *buf;
+    size_t size;
+    uint32_t dup_ack_count;
+    bool is_coalesced;      /* need recal ipv4 header checksum, mark here */
+    NetRscUnit unit;
+    NetClientState *nc;
+} NetRscSeg;
+
+
+/* Chain is divided by protocol(ipv4/v6) and NetClientInfo */
+typedef struct NetRscChain {
+   QTAILQ_ENTRY(NetRscChain) next;
+   uint16_t hdr_size;
+   uint16_t proto;
+   uint16_t max_payload;
+   QEMUTimer *drain_timer;
+   QTAILQ_HEAD(, NetRscSeg) buffers;
+   NetRscStat stat;
+} NetRscChain;
+
  void virtio_instance_init_common(Object *proxy_obj, void *data,
                                   size_t vdev_size, const char *vdev_name);
 
#6 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 83 lines · Wei Xu
Quoted 61 lines · Jason Wang
Quoted 51 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the
performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.

The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check
and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2,
this is to notify upper layer TCP starting retransmission due to
the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other
flags, while it broke the test because windows set it to the same even it's
not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass'
and 'finalize', 'bypass' means should be sent out directly, and 'finalize'
means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

Quoted 8 lines

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because
this normally happens upon a connection is going to be closed, an 'URG' packet
also finalize current coalescing unit while there maybe protocol difference to
different OS.

Quoted 2 lines

But URG packet should be sent as quickly as possible regardless of ordering, no?

Yes, you right, URG will terminate the current 'SCU', i'll amend the commit log.

Quoted 14 lines
Quoted 14 lines

Statistics can be used to monitor the basic coalescing status, the 'out of order'
and 'out of window' means how many retransmitting packets, thus describe the
performance intuitively.

---
  hw/net/virtio-net.c            | 486
++++++++++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-net.h |   1 +
  include/hw/virtio/virtio.h     |  72 ++++++
  3 files changed, 558 insertions(+), 1 deletion(-)

[...]

Quoted 23 lines
Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1853

Quoted 11 lines
hw/net/virtio-net.c +11 -0
+        } else {
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;
Quoted 8 lines

Looks like there're something I missed. The spec said:

"In other words, any pure ACK that is not a duplicate ACK or a window update triggers an exception and must not be coalesced. All such pure ACKs must be indicated as individual segments."

Does it mean we *should not* coalesce windows update and pure ack? (Since it can wakeup transmission)?

It's also a little bit inexplicit and flexible due to the spec, please see the flowchart I on the same page.

Quoted 10 lines

Comments about the flowchart:
------------------------------------------------------------------------
The first of the following two flowcharts describes the rules for coalescing segments and updating the TCP headers.
This flowchart refers to mechanisms for distinguishing valid duplicate ACKs and window updates. The second flowchart describes these mechanisms.
------------------------------------------------------------------------
As show in the flowchart, only status 'C' will break current scu and get finalized, both 'A' and 'B' can be coalesced afaik.

Interesting, looks like you're right.

Quoted 27 lines
Quoted 19 lines
Quoted 19 lines
hw/net/virtio-net.c +4 -0
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain,

NetRscSeg *seg,
+ const uint8_t *buf, NetRscUnit
*n_unit)

hw/net/virtio-net.c +11 -0
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1876

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {

What if n_unit->tcp_hdrlen > o_uint->tcp_hdr_len ?

Quoted 2 lines

do you mean '<'? that also means some option changed, maybe i should include it.

Yes.

Quoted 50 lines
Quoted 42 lines
Quoted 42 lines
hw/net/virtio-net.c +14 -0
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup

ack or etc */

hw/net/virtio-net.c +5 -0
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp,

o_unit->tcp);

hw/net/virtio-net.c +13 -0
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is

different,
+ so use the field value to update and record the new data
len */

hw/net/virtio-net.c +4 -0
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
Quoted 8 lines
Quoted 6 lines
(quoted diff) +2 -0
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be

coalesced
+ for windows guest, while this may change the behavior
for linux
+ guest. */

This needs more thought, 'can' probably means don't. (Linux GRO won't merge PUSH packet).

Quoted 2 lines

Yes, since it's mainly for win guest, how about take this as this first and do more test and see how to handle it?

Right, this is not an issue probably but just an optimization for latency.

[...]

Quoted 14 lines
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2122

Quoted 12 lines
hw/net/virtio-net.c +11 -0
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;

Why introduce a specified field for instead of just use n->guest_hdr_len?

Quoted 2 lines

this is to reduce invoking times to find VirtIONet by 'nc', there are a few places will use it.

Okay, then I think you'd better keep a pointer to VirtIONet structure instead. (Consider you may want to refer more data from it, we don't want to duplicate fields in two places).

Quoted 45 lines
Quoted 37 lines
Quoted 37 lines
hw/net/virtio-net.c +36 -0
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 6 lines
hw/net/virtio-net.c +6 -0
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

Quoted 3 lines

yes, not sure which way is good to support this, hmp/qmp/ethtool, this is gonna to support win guest,
so need a well-compatible interface, any comments?

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

[...]

author
#7 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 216 lines · Jason Wang
Quoted 80 lines · Wei Xu
Quoted 60 lines · Jason Wang
Quoted 51 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

All the data packets in a tcp connection will be cached to a big buffer in every receive interval, and will be sent out via a timer, the 'virtio_net_rsc_timeout' controls the interval, the value will influent the
performance and response of tcp connection extremely, 50000(50us) is a experience value to gain a performance improvement, since the whql test sends packets every 100us, so '300000(300us)' can pass the test case, this is also the default value, it's gonna to be tunable.

The timer will only be triggered if the packets pool is not empty, and it'll drain off all the cached packets

'NetRscChain' is used to save the segments of different protocols in a VirtIONet device.

The main handler of TCP includes TCP window update, duplicated ACK check
and the real data coalescing if the new segment passed sanity check and is identified as an 'wanted' one.

An 'wanted' segment means:
1. Segment is within current window and the sequence is the expected one.
2. ACK of the segment is in the valid window.
3. If the ACK in the segment is a duplicated one, then it must less than 2,
this is to notify upper layer TCP starting retransmission due to
the spec.

Sanity check includes:
1. Incorrect version in IP header
2. IP options & IP fragment
3. Not a TCP packets
4. Sanity size check to prevent buffer overflow attack.

There maybe more cases should be considered such as ip identification other
flags, while it broke the test because windows set it to the same even it's
not a fragment.

Normally it includes 2 typical ways to handle a TCP control flag, 'bypass'
and 'finalize', 'bypass' means should be sent out directly, and 'finalize'
means the packets should also be bypassed, and this should be done after searching for the same connection packets in the pool and sending all of them out, this is to avoid out of data.

Quoted 8 lines

All the 'SYN' packets will be bypassed since this always begin a new' connection, other flags such 'FIN/RST' will trigger a finalization, because
this normally happens upon a connection is going to be closed, an 'URG' packet
also finalize current coalescing unit while there maybe protocol difference to
different OS.

Quoted 2 lines

But URG packet should be sent as quickly as possible regardless of ordering, no?

Yes, you right, URG will terminate the current 'SCU', i'll amend the commit log.

Quoted 13 lines
Quoted 13 lines

Statistics can be used to monitor the basic coalescing status, the 'out of order'
and 'out of window' means how many retransmitting packets, thus describe the
performance intuitively.

---
   hw/net/virtio-net.c            | 486
++++++++++++++++++++++++++++++++++++++++-
   include/hw/virtio/virtio-net.h |   1 +
   include/hw/virtio/virtio.h     |  72 ++++++
   3 files changed, 558 insertions(+), 1 deletion(-)

[...]

Quoted 22 lines
Quoted 11 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1853

Quoted 11 lines
hw/net/virtio-net.c +11 -0
+        } else {
+            /* Coalesce window update */
+            o_tcp->th_win = n_tcp->th_win;
+            chain->stat.win_update++;
+            return RSC_COALESCE;
+        }
+    } else {
+        /* pure ack, update ack */
+        o_tcp->th_ack = n_tcp->th_ack;
+        chain->stat.pure_ack++;
+        return RSC_COALESCE;
Quoted 8 lines

Looks like there're something I missed. The spec said:

"In other words, any pure ACK that is not a duplicate ACK or a window update triggers an exception and must not be coalesced. All such pure ACKs must be indicated as individual segments."

Does it mean we *should not* coalesce windows update and pure ack? (Since it can wakeup transmission)?

It's also a little bit inexplicit and flexible due to the spec, please see the flowchart I on the same page.

Quoted 10 lines

Comments about the flowchart:
------------------------------------------------------------------------
The first of the following two flowcharts describes the rules for coalescing segments and updating the TCP headers.
This flowchart refers to mechanisms for distinguishing valid duplicate ACKs and window updates. The second flowchart describes these mechanisms.
------------------------------------------------------------------------
As show in the flowchart, only status 'C' will break current scu and get finalized, both 'A' and 'B' can be coalesced afaik.

Interesting, looks like you're right.

Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
hw/net/virtio-net.c +4 -0
+    }
+}
+
+static int32_t virtio_net_rsc_coalesce_data(NetRscChain *chain,

NetRscSeg *seg,
+ const uint8_t *buf, NetRscUnit
*n_unit)

hw/net/virtio-net.c +11 -0
+{
+    void *data;
+    uint16_t o_ip_len;
+    uint32_t nseq, oseq;
+    NetRscUnit *o_unit;
+
+    o_unit = &seg->unit;
+    o_ip_len = htons(*o_unit->ip_plen);
+    nseq = htonl(n_unit->tcp->th_seq);
+    oseq = htonl(o_unit->tcp->th_seq);
+
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1876

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+    if (n_unit->tcp_hdrlen > TCP_HDR_SZ) {
+        /* Log this only for debugging observation */
+        chain->stat.tcp_option++;
+    }
+
+    /* Ignore packet with more/larger tcp options */
+    if (n_unit->tcp_hdrlen > o_unit->tcp_hdrlen) {

What if n_unit->tcp_hdrlen > o_uint->tcp_hdr_len ?

Quoted 2 lines

do you mean '<'? that also means some option changed, maybe i should include it.

Yes.

Quoted 49 lines
Quoted 41 lines
Quoted 41 lines
hw/net/virtio-net.c +14 -0
+        chain->stat.tcp_larger_option++;
+        return RSC_FINAL;
+    }
+
+    /* out of order or retransmitted. */
+    if ((nseq - oseq) > MAX_TCP_PAYLOAD) {
+        chain->stat.data_out_of_win++;
+        return RSC_FINAL;
+    }
+
+    data = ((uint8_t *)n_unit->tcp) + n_unit->tcp_hdrlen;
+    if (nseq == oseq) {
+        if ((0 == o_unit->payload) && n_unit->payload) {
+            /* From no payload to payload, normal case, not a dup

ack or etc */

hw/net/virtio-net.c +5 -0
+            chain->stat.data_after_pure_ack++;
+            goto coalesce;
+        } else {
+            return virtio_net_rsc_handle_ack(chain, seg, buf,
+                                             n_unit->tcp,

o_unit->tcp);

hw/net/virtio-net.c +13 -0
+        }
+    } else if ((nseq - oseq) != o_unit->payload) {
+        /* Not a consistent packet, out of order */
+        chain->stat.data_out_of_order++;
+        return RSC_FINAL;
+    } else {
+coalesce:
+        if ((o_ip_len + n_unit->payload) > chain->max_payload) {
+            chain->stat.over_size++;
+            return RSC_FINAL;
+        }
+
+        /* Here comes the right data, the payload lengh in v4/v6 is

different,
+ so use the field value to update and record the new data
len */

hw/net/virtio-net.c +4 -0
+        o_unit->payload += n_unit->payload; /* update new data len */
+
+        /* update field in ip header */
+        *o_unit->ip_plen = htons(o_ip_len + n_unit->payload);
Quoted 8 lines
Quoted 6 lines
(quoted diff) +2 -0
+
+        /* Bring 'PUSH' big, the whql test guide says 'PUSH' can be

coalesced
+ for windows guest, while this may change the behavior
for linux
+ guest. */

This needs more thought, 'can' probably means don't. (Linux GRO won't merge PUSH packet).

Quoted 2 lines

Yes, since it's mainly for win guest, how about take this as this first and do more test and see how to handle it?

Right, this is not an issue probably but just an optimization for latency.

[...]

Quoted 13 lines
Quoted 13 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2122

Quoted 11 lines
hw/net/virtio-net.c +11 -0
+        return NULL;
+    }
+
+    QTAILQ_FOREACH(chain, &n->rsc_chains, next) {
+        if (chain->proto == proto) {
+            return chain;
+        }
+    }
+
+    chain = g_malloc(sizeof(*chain));
+    chain->hdr_size = n->guest_hdr_len;

Why introduce a specified field for instead of just use n->guest_hdr_len?

Quoted 5 lines
Quoted 2 lines

this is to reduce invoking times to find VirtIONet by 'nc', there are a few places will use it.

Okay, then I think you'd better keep a pointer to VirtIONet structure instead. (Consider you may want to refer more data from it, we don't want to duplicate fields in two places).

sure, that's a good idea.

Quoted 45 lines
Quoted 45 lines
Quoted 45 lines
Quoted 33 lines
hw/net/virtio-net.c +32 -0
+    chain->proto = proto;
+    chain->max_payload = MAX_IP4_PAYLOAD;
+    chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                      virtio_net_rsc_purge, chain);
+    memset(&chain->stat, 0, sizeof(chain->stat));
+
+    QTAILQ_INIT(&chain->buffers);
+    QTAILQ_INSERT_TAIL(&n->rsc_chains, chain, next);
+
+    return chain;
+}
+
+static ssize_t virtio_net_rsc_receive(NetClientState *nc,
+                                      const uint8_t *buf, size_t size)
+{
+    uint16_t proto;
+    NetRscChain *chain;
+    struct eth_header *eth;
+    VirtIONet *n;
+
+    n = qemu_get_nic_opaque(nc);
+    if (size < (n->guest_hdr_len + ETH_HDR_SZ)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    eth = (struct eth_header *)(buf + n->guest_hdr_len);
+    proto = htons(eth->h_proto);
+
+    chain = virtio_net_rsc_lookup_chain(n, nc, proto);
+    if (!chain) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else {

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+        chain->stat.received++;
+        return virtio_net_rsc_receive4(chain, nc, buf, size);
+    }
+}
+
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

Quoted 5 lines
Quoted 3 lines

yes, not sure which way is good to support this, hmp/qmp/ethtool, this is gonna to support win guest,
so need a well-compatible interface, any comments?

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

Looks this feature should be turn on/off dynamically due to the spec, so maybe this should be managed from the guest, is there any reference code for this?

Quoted 2 lines

[...]

#8 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 8 lines · Wei Xu
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 6 lines
hw/net/virtio-net.c +5 -0
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

yes, not sure which way is good to support this, hmp/qmp/ethtool, this is gonna to support win guest,
so need a well-compatible interface, any comments?

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

Looks this feature should be turn on/off dynamically due to the spec, so maybe this should be managed from the guest, is there any reference code for this?

Then you may want to look at implementation of
VIRTIO_NET_F_CTRL_GUEST_OFFLOADS.

author
#9 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 12 lines · Jason Wang
Quoted 10 lines · Wei Xu
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 2164

Quoted 5 lines
hw/net/virtio-net.c +5 -0
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types. And also need some work on virtio spec I think.

yes, not sure which way is good to support this, hmp/qmp/ethtool, this is gonna to support win guest,
so need a well-compatible interface, any comments?

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

Looks this feature should be turn on/off dynamically due to the spec, so maybe this should be managed from the guest, is there any reference code for this?

Then you may want to look at implementation of
VIRTIO_NET_F_CTRL_GUEST_OFFLOADS.

Have a short look at it, do you know how to control the feature bit? both when lauching vm and changing it during runtime?

#10 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 19 lines · Wei Xu
Quoted 16 lines · Jason Wang
Quoted 14 lines · Wei Xu
Quoted 14 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types.
And also need some work on virtio spec I think.

Quoted 4 lines

yes, not sure which way is good to support this, hmp/qmp/ethtool, this
is gonna to support win guest,
so need a well-compatible interface, any comments?

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Looks this feature should be turn on/off dynamically due to the spec, so maybe this should be managed from the guest, is there any reference code for this?

Then you may want to look at implementation of
VIRTIO_NET_F_CTRL_GUEST_OFFLOADS.

Have a short look at it, do you know how to control the feature bit? both when lauching vm and changing it during runtime?

Virtio spec and maybe windows driver source code can give you the answer.

author
#11 lore raw

Re: [Qemu-devel] [ Patch 1/2] virtio-net rsc: support coalescing ipv4 tcp traffic

Quoted 19 lines · Jason Wang
Quoted 17 lines · Wei Xu
Quoted 15 lines · Jason Wang
Quoted 14 lines · Wei Xu
Quoted 14 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+static ssize_t virtio_net_receive(NetClientState *nc,
+                                  const uint8_t *buf, size_t size)
+{
+    if (virtio_net_rsc_bypass) {
+        return virtio_net_do_receive(nc, buf, size);

You need a feature bit for this and compat it for older machine types.
And also need some work on virtio spec I think.

Quoted 4 lines

yes, not sure which way is good to support this, hmp/qmp/ethtool, this
is gonna to support win guest,
so need a well-compatible interface, any comments?

I think this should be implemented through feature bits/negotiation instead of something like ethtool.

Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Looks this feature should be turn on/off dynamically due to the spec, so maybe this should be managed from the guest, is there any reference code for this?

Then you may want to look at implementation of
VIRTIO_NET_F_CTRL_GUEST_OFFLOADS.

Have a short look at it, do you know how to control the feature bit? both when lauching vm and changing it during runtime?

Virtio spec and maybe windows driver source code can give you the answer.

OK, will check it out.

#12 lore raw

PATCH 2/2 virtio-net rsc: support coalescing ipv6 tcp traffic

In reply to [email protected]

From: Wei Xu <[email protected]>

Most things like ipv4 except there is a significant difference between ipv4 and ipv6, the fragment lenght in ipv4 header includes itself, while it's not included for ipv6, thus means ipv6 can carry a real '65535' unit.

---
 hw/net/virtio-net.c        | 146 ++++++++++++++++++++++++++++++++++++++++-----
 include/hw/virtio/virtio.h |   5 +-
 2 files changed, 135 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +131 -15
index c23b45f..ef61b74 100644
@@ -52,9 +52,14 @@
 #define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
 #define MAX_TCP_PAYLOAD 65535
 
-/* max payload with virtio header */
+#define IP6_HDR_SZ (sizeof(struct ip6_header))
+#define ETH_IP6_HDR_SZ (ETH_HDR_SZ + IP6_HDR_SZ)
+#define IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
+#define MAX_IP6_PAYLOAD MAX_TCP_PAYLOAD
+
+/* ip6 max payload, payload in ipv6 don't include the  header */
 #define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
-                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+                                + ETH_IP6_HDR_SZ + MAX_IP6_PAYLOAD)
 
 #define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
 
@@ -1722,14 +1727,27 @@ static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
 {
     uint16_t ip_hdrlen;
 
-    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
-    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
-    unit->ip_plen = &unit->ip->ip_len;
-    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->u_ip.ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->u_ip.ip->ip_ver_len) << 2);
+    unit->ip_plen = &unit->u_ip.ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip) + ip_hdrlen);
     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
 }
 
+static void virtio_net_rsc_extract_unit6(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    unit->u_ip.ip6 = (struct ip6_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    unit->ip_plen = &(unit->u_ip.ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip6)\
+                                    + IP6_HDR_SZ);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    /* There is a difference between payload lenght in ipv4 and v6,
+       ip header is excluded in ipv6 */
+    unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
+}
+
 static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
 {
     uint32_t sum;
@@ -1743,7 +1761,10 @@ static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
 {
     int ret;
 
-    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    if ((chain->proto == ETH_P_IP) && seg->is_coalesced) {
+        virtio_net_rsc_ipv4_checksum(seg->unit.u_ip.ip);
+    }
+
     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
     QTAILQ_REMOVE(&chain->buffers, seg, next);
     g_free(seg->buf);
@@ -1807,7 +1828,11 @@ static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
     chain->stat.cache++;
 
-    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    if (chain->proto == ETH_P_IP) {
+        virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    } else {
+        virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
+    }
 }
 
 static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
@@ -1930,8 +1955,8 @@ coalesce:
 static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
                         const uint8_t *buf, size_t size, NetRscUnit *unit)
 {
-    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
-        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+    if ((unit->u_ip.ip->ip_src ^ seg->unit.u_ip.ip->ip_src)
+        || (unit->u_ip.ip->ip_dst ^ seg->unit.u_ip.ip->ip_dst)
         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
         chain->stat.no_match++;
@@ -1941,6 +1966,22 @@ static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
 }
 
+static int32_t virtio_net_rsc_coalesce6(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if (memcmp(&unit->u_ip.ip6->ip6_src, &seg->unit.u_ip.ip6->ip6_src,
+               sizeof(struct in6_address))
+        || memcmp(&unit->u_ip.ip6->ip6_dst, &seg->unit.u_ip.ip6->ip6_dst,
+                  sizeof(struct in6_address))
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+            chain->stat.no_match++;
+            return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
 /* Pakcets with 'SYN' should bypass, other flag should be sent after drain
  * to prevent out of order */
 static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
@@ -1983,7 +2024,11 @@ static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
     NetRscSeg *seg, *nseg;
 
     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
-        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        if (chain->proto == ETH_P_IP) {
+            ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        } else {
+            ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
+        }
 
         if (ret == RSC_FINAL) {
             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
@@ -2082,7 +2127,8 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
 
     chain = (NetRscChain *)opq;
     virtio_net_rsc_extract_unit4(chain, buf, &unit);
-    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain,
+                                                 unit.u_ip.ip, buf, size)) {
         return virtio_net_do_receive(nc, buf, size);
     }
 
@@ -2102,13 +2148,74 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
 }
 
+static int32_t virtio_net_rsc_sanity_check6(NetRscChain *chain, 
+                        struct ip6_header *ip, const uint8_t *buf, size_t size)
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP6_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    if (((0xF0 & ip->ip6_ctlun.ip6_un1.ip6_un1_flow) >> 4)
+        != IP_HEADER_VERSION_6) {
+        return RSC_BYPASS;
+    }
+
+    /* Both option and protocol is checked in this */
+    if (ip->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */
+    ip_len = htons(ip->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    if (ip_len < TCP_HDR_SZ
+        || ip_len > (size - chain->hdr_size - ETH_IP6_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive6(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit6(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check6(chain,
+                                                 unit.u_ip.ip6, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 8), IP6_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP6_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
 static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
                                             NetClientState *nc, uint16_t proto)
 {
     NetRscChain *chain;
 
     /* Only handle IPv4/6 */
-    if (proto != (uint16_t)ETH_P_IP) {
+    if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
         return NULL;
     }
 
@@ -2121,7 +2228,11 @@ static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
     chain = g_malloc(sizeof(*chain));
     chain->hdr_size = n->guest_hdr_len;
     chain->proto = proto;
-    chain->max_payload = MAX_IP4_PAYLOAD;
+    if (proto == (uint16_t)ETH_P_IP) {
+        chain->max_payload = MAX_IP4_PAYLOAD;
+    } else {
+        chain->max_payload = MAX_IP6_PAYLOAD;
+    }
     chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                       virtio_net_rsc_purge, chain);
     memset(&chain->stat, 0, sizeof(chain->stat));
@@ -2153,7 +2264,12 @@ static ssize_t virtio_net_rsc_receive(NetClientState *nc,
         return virtio_net_do_receive(nc, buf, size);
     } else {
         chain->stat.received++;
-        return virtio_net_rsc_receive4(chain, nc, buf, size);
+
+        if (proto == (uint16_t)ETH_P_IP) {
+            return virtio_net_rsc_receive4(chain, nc, buf, size);
+        } else  {
+            return virtio_net_rsc_receive6(chain, nc, buf, size);
+        }
     }
 }
 
include/hw/virtio/virtio.h +4 -1
index 3b1dfa8..13d20a4 100644
@@ -170,7 +170,10 @@ typedef struct NetRscStat {
 
 /* Rsc unit general info used to checking if can coalescing */
 typedef struct NetRscUnit {
-   struct ip_header *ip;   /* ip header */
+    union {
+        struct ip_header *ip;   /* ip header */
+        struct ip6_header *ip6; /* ip6 header */
+    } u_ip;
    uint16_t *ip_plen;      /* data len pointer in ip header field */
    struct tcp_header *tcp; /* tcp header */
    uint16_t tcp_hdrlen;    /* tcp header len */
Signature · 1 line
2.5.0
#13 lore raw

Re: [Qemu-devel] [ Patch 2/2] virtio-net rsc: support coalescing ipv6 tcp traffic

Quoted 43 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

Most things like ipv4 except there is a significant difference between ipv4 and ipv6, the fragment lenght in ipv4 header includes itself, while it's not included for ipv6, thus means ipv6 can carry a real '65535' unit.

---
 hw/net/virtio-net.c        | 146 ++++++++++++++++++++++++++++++++++++++++-----
 include/hw/virtio/virtio.h |   5 +-
 2 files changed, 135 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +9 -6
index c23b45f..ef61b74 100644
@@ -52,9 +52,14 @@
 #define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
 #define MAX_TCP_PAYLOAD 65535
 
-/* max payload with virtio header */
+#define IP6_HDR_SZ (sizeof(struct ip6_header))
+#define ETH_IP6_HDR_SZ (ETH_HDR_SZ + IP6_HDR_SZ)
+#define IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
+#define MAX_IP6_PAYLOAD MAX_TCP_PAYLOAD
+
+/* ip6 max payload, payload in ipv6 don't include the  header */
 #define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
-                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+                                + ETH_IP6_HDR_SZ + MAX_IP6_PAYLOAD)
 
 #define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
 
@@ -1722,14 +1727,27 @@ static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
 {
     uint16_t ip_hdrlen;
 
-    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
-    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
-    unit->ip_plen = &unit->ip->ip_len;
-    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->u_ip.ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->u_ip.ip->ip_ver_len) << 2);

hw/net/virtio-net.c static void virtio_net_rsc_extract_unit4(NetRscChain *chain, · line 1741

Quoted 10 lines
hw/net/virtio-net.c +6 -0
+    unit->ip_plen = &unit->u_ip.ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip) + ip_hdrlen);
     unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
     unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
 }
 
+static void virtio_net_rsc_extract_unit6(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    unit->u_ip.ip6 = (struct ip6_header *)(buf + chain->hdr_size + ETH_HDR_SZ);

The u_ip seems a little bit redundant. How about use a simple void * and cast it to ipv4/ipv6 in proto specific callbacks?

The introducing of u_ip leads unnecessary ipv4 codes changes for ipv6 coalescing implementation.

Quoted 25 lines
hw/net/virtio-net.c +13 -1
+    unit->ip_plen = &(unit->u_ip.ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip6)\
+                                    + IP6_HDR_SZ);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    /* There is a difference between payload lenght in ipv4 and v6,
+       ip header is excluded in ipv6 */
+    unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
+}
+
 static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
 {
     uint32_t sum;
@@ -1743,7 +1761,10 @@ static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
 {
     int ret;
 
-    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    if ((chain->proto == ETH_P_IP) && seg->is_coalesced) {
+        virtio_net_rsc_ipv4_checksum(seg->unit.u_ip.ip);
+    }
+
     ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
     QTAILQ_REMOVE(&chain->buffers, seg, next);
     g_free(seg->buf);

hw/net/virtio-net.c static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc, · line 1833

Quoted 7 lines
hw/net/virtio-net.c +3 -1
@@ -1807,7 +1828,11 @@ static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
     QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
     chain->stat.cache++;
 
-    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    if (chain->proto == ETH_P_IP) {
+        virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    } else {

A switch and a g_assert_not_reached() is better than this.

Quoted 63 lines
hw/net/virtio-net.c +27 -4
+        virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
+    }
 }
 
 static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
@@ -1930,8 +1955,8 @@ coalesce:
 static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
                         const uint8_t *buf, size_t size, NetRscUnit *unit)
 {
-    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
-        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+    if ((unit->u_ip.ip->ip_src ^ seg->unit.u_ip.ip->ip_src)
+        || (unit->u_ip.ip->ip_dst ^ seg->unit.u_ip.ip->ip_dst)
         || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
         || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
         chain->stat.no_match++;
@@ -1941,6 +1966,22 @@ static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
     return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
 }
 
+static int32_t virtio_net_rsc_coalesce6(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if (memcmp(&unit->u_ip.ip6->ip6_src, &seg->unit.u_ip.ip6->ip6_src,
+               sizeof(struct in6_address))
+        || memcmp(&unit->u_ip.ip6->ip6_dst, &seg->unit.u_ip.ip6->ip6_dst,
+                  sizeof(struct in6_address))
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+            chain->stat.no_match++;
+            return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
 /* Pakcets with 'SYN' should bypass, other flag should be sent after drain
  * to prevent out of order */
 static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
@@ -1983,7 +2024,11 @@ static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
     NetRscSeg *seg, *nseg;
 
     QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
-        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        if (chain->proto == ETH_P_IP) {
+            ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        } else {
+            ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
+        }
 
         if (ret == RSC_FINAL) {
             if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
@@ -2082,7 +2127,8 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
 
     chain = (NetRscChain *)opq;
     virtio_net_rsc_extract_unit4(chain, buf, &unit);
-    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain,
+                                                 unit.u_ip.ip, buf, size)) {
         return virtio_net_do_receive(nc, buf, size);
     }
 

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2152

Quoted 5 lines
hw/net/virtio-net.c +2 -0
@@ -2102,13 +2148,74 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
     return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
 }
 
+static int32_t virtio_net_rsc_sanity_check6(NetRscChain *chain, 
+                        struct ip6_header *ip, const uint8_t *buf, size_t size)

Indentation is wrong here.

Quoted 9 lines
hw/net/virtio-net.c +9 -0
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP6_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    if (((0xF0 & ip->ip6_ctlun.ip6_un1.ip6_un1_flow) >> 4)
+        != IP_HEADER_VERSION_6) {

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2171

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+        return RSC_BYPASS;
+    }
+
+    /* Both option and protocol is checked in this */
+    if (ip->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */

The comment is useless.

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2184

Quoted 13 lines
hw/net/virtio-net.c +13 -0
+    ip_len = htons(ip->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    if (ip_len < TCP_HDR_SZ
+        || ip_len > (size - chain->hdr_size - ETH_IP6_HDR_SZ)) {
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive6(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{

Rather similar to ipv4 version, need to unify the code.

Quoted 81 lines
hw/net/virtio-net.c +39 -3
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit6(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check6(chain,
+                                                 unit.u_ip.ip6, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 8), IP6_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP6_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
 static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
                                             NetClientState *nc, uint16_t proto)
 {
     NetRscChain *chain;
 
     /* Only handle IPv4/6 */
-    if (proto != (uint16_t)ETH_P_IP) {
+    if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
         return NULL;
     }
 
@@ -2121,7 +2228,11 @@ static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
     chain = g_malloc(sizeof(*chain));
     chain->hdr_size = n->guest_hdr_len;
     chain->proto = proto;
-    chain->max_payload = MAX_IP4_PAYLOAD;
+    if (proto == (uint16_t)ETH_P_IP) {
+        chain->max_payload = MAX_IP4_PAYLOAD;
+    } else {
+        chain->max_payload = MAX_IP6_PAYLOAD;
+    }
     chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                       virtio_net_rsc_purge, chain);
     memset(&chain->stat, 0, sizeof(chain->stat));
@@ -2153,7 +2264,12 @@ static ssize_t virtio_net_rsc_receive(NetClientState *nc,
         return virtio_net_do_receive(nc, buf, size);
     } else {
         chain->stat.received++;
-        return virtio_net_rsc_receive4(chain, nc, buf, size);
+
+        if (proto == (uint16_t)ETH_P_IP) {
+            return virtio_net_rsc_receive4(chain, nc, buf, size);
+        } else  {
+            return virtio_net_rsc_receive6(chain, nc, buf, size);
+        }
     }
 }
 
include/hw/virtio/virtio.h +4 -1
index 3b1dfa8..13d20a4 100644
@@ -170,7 +170,10 @@ typedef struct NetRscStat {
 
 /* Rsc unit general info used to checking if can coalescing */
 typedef struct NetRscUnit {
-   struct ip_header *ip;   /* ip header */
+    union {
+        struct ip_header *ip;   /* ip header */
+        struct ip6_header *ip6; /* ip6 header */
+    } u_ip;
    uint16_t *ip_plen;      /* data len pointer in ip header field */
    struct tcp_header *tcp; /* tcp header */
    uint16_t tcp_hdrlen;    /* tcp header len */
author
#14 lore raw

Re: [Qemu-devel] [ Patch 2/2] virtio-net rsc: support coalescing ipv6 tcp traffic

Quoted 58 lines · Jason Wang
Quoted 43 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

Most things like ipv4 except there is a significant difference between ipv4 and ipv6, the fragment lenght in ipv4 header includes itself, while it's not included for ipv6, thus means ipv6 can carry a real '65535' unit.

---
  hw/net/virtio-net.c        | 146 ++++++++++++++++++++++++++++++++++++++++-----
  include/hw/virtio/virtio.h |   5 +-
  2 files changed, 135 insertions(+), 16 deletions(-)
hw/net/virtio-net.c +9 -6
index c23b45f..ef61b74 100644
@@ -52,9 +52,14 @@
  #define MAX_IP4_PAYLOAD (65535 - IP4_HDR_SZ)
  #define MAX_TCP_PAYLOAD 65535
 
-/* max payload with virtio header */
+#define IP6_HDR_SZ (sizeof(struct ip6_header))
+#define ETH_IP6_HDR_SZ (ETH_HDR_SZ + IP6_HDR_SZ)
+#define IP6_ADDR_SIZE   32      /* ipv6 saddr + daddr */
+#define MAX_IP6_PAYLOAD MAX_TCP_PAYLOAD
+
+/* ip6 max payload, payload in ipv6 don't include the  header */
  #define MAX_VIRTIO_PAYLOAD  (sizeof(struct virtio_net_hdr_mrg_rxbuf) \
-                                + ETH_HDR_SZ + MAX_TCP_PAYLOAD)
+                                + ETH_IP6_HDR_SZ + MAX_IP6_PAYLOAD)
 
  #define IP4_HEADER_LEN 5 /* header lenght value in ip header without option */
 
@@ -1722,14 +1727,27 @@ static void virtio_net_rsc_extract_unit4(NetRscChain *chain,
  {
      uint16_t ip_hdrlen;
 
-    unit->ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
-    ip_hdrlen = ((0xF & unit->ip->ip_ver_len) << 2);
-    unit->ip_plen = &unit->ip->ip_len;
-    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->ip) + ip_hdrlen);
+    unit->u_ip.ip = (struct ip_header *)(buf + chain->hdr_size + ETH_HDR_SZ);
+    ip_hdrlen = ((0xF & unit->u_ip.ip->ip_ver_len) << 2);

hw/net/virtio-net.c static void virtio_net_rsc_extract_unit4(NetRscChain *chain, · line 1741

Quoted 10 lines
hw/net/virtio-net.c +6 -0
+    unit->ip_plen = &unit->u_ip.ip->ip_len;
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip) + ip_hdrlen);
      unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
      unit->payload = htons(*unit->ip_plen) - ip_hdrlen - unit->tcp_hdrlen;
  }
 
+static void virtio_net_rsc_extract_unit6(NetRscChain *chain,
+                                         const uint8_t *buf, NetRscUnit* unit)
+{
+    unit->u_ip.ip6 = (struct ip6_header *)(buf + chain->hdr_size + ETH_HDR_SZ);

The u_ip seems a little bit redundant. How about use a simple void * and cast it to ipv4/ipv6 in proto specific callbacks?

Quoted 2 lines

The introducing of u_ip leads unnecessary ipv4 codes changes for ipv6 coalescing implementation.

Sure.

Quoted 28 lines
Quoted 28 lines
hw/net/virtio-net.c +13 -1
+    unit->ip_plen = &(unit->u_ip.ip6->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    unit->tcp = (struct tcp_header *)(((uint8_t *)unit->u_ip.ip6)\
+                                    + IP6_HDR_SZ);
+    unit->tcp_hdrlen = (htons(unit->tcp->th_offset_flags) & 0xF000) >> 10;
+    /* There is a difference between payload lenght in ipv4 and v6,
+       ip header is excluded in ipv6 */
+    unit->payload = htons(*unit->ip_plen) - unit->tcp_hdrlen;
+}
+
  static void virtio_net_rsc_ipv4_checksum(struct ip_header *ip)
  {
      uint32_t sum;
@@ -1743,7 +1761,10 @@ static size_t virtio_net_rsc_drain_seg(NetRscChain *chain, NetRscSeg *seg)
  {
      int ret;
 
-    virtio_net_rsc_ipv4_checksum(seg->unit.ip);
+    if ((chain->proto == ETH_P_IP) && seg->is_coalesced) {
+        virtio_net_rsc_ipv4_checksum(seg->unit.u_ip.ip);
+    }
+
      ret = virtio_net_do_receive(seg->nc, seg->buf, seg->size);
      QTAILQ_REMOVE(&chain->buffers, seg, next);
      g_free(seg->buf);
@@ -1807,7 +1828,11 @@ static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc,
      QTAILQ_INSERT_TAIL(&chain->buffers, seg, next);
      chain->stat.cache++;
 
Quoted 5 lines

hw/net/virtio-net.c static void virtio_net_rsc_cache_buf(NetRscChain *chain, NetClientState *nc, · line 1833

Quoted 4 lines
hw/net/virtio-net.c +3 -1
-    virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    if (chain->proto == ETH_P_IP) {
+        virtio_net_rsc_extract_unit4(chain, seg->buf, &seg->unit);
+    } else {

A switch and a g_assert_not_reached() is better than this.

sure.

Quoted 63 lines
Quoted 63 lines
hw/net/virtio-net.c +27 -4
+        virtio_net_rsc_extract_unit6(chain, seg->buf, &seg->unit);
+    }
  }
 
  static int32_t virtio_net_rsc_handle_ack(NetRscChain *chain, NetRscSeg *seg,
@@ -1930,8 +1955,8 @@ coalesce:
  static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
                          const uint8_t *buf, size_t size, NetRscUnit *unit)
  {
-    if ((unit->ip->ip_src ^ seg->unit.ip->ip_src)
-        || (unit->ip->ip_dst ^ seg->unit.ip->ip_dst)
+    if ((unit->u_ip.ip->ip_src ^ seg->unit.u_ip.ip->ip_src)
+        || (unit->u_ip.ip->ip_dst ^ seg->unit.u_ip.ip->ip_dst)
          || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
          || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
          chain->stat.no_match++;
@@ -1941,6 +1966,22 @@ static int32_t virtio_net_rsc_coalesce4(NetRscChain *chain, NetRscSeg *seg,
      return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
  }
 
+static int32_t virtio_net_rsc_coalesce6(NetRscChain *chain, NetRscSeg *seg,
+                        const uint8_t *buf, size_t size, NetRscUnit *unit)
+{
+    if (memcmp(&unit->u_ip.ip6->ip6_src, &seg->unit.u_ip.ip6->ip6_src,
+               sizeof(struct in6_address))
+        || memcmp(&unit->u_ip.ip6->ip6_dst, &seg->unit.u_ip.ip6->ip6_dst,
+                  sizeof(struct in6_address))
+        || (unit->tcp->th_sport ^ seg->unit.tcp->th_sport)
+        || (unit->tcp->th_dport ^ seg->unit.tcp->th_dport)) {
+            chain->stat.no_match++;
+            return RSC_NO_MATCH;
+    }
+
+    return virtio_net_rsc_coalesce_data(chain, seg, buf, unit);
+}
+
  /* Pakcets with 'SYN' should bypass, other flag should be sent after drain
   * to prevent out of order */
  static int virtio_net_rsc_tcp_ctrl_check(NetRscChain *chain,
@@ -1983,7 +2024,11 @@ static size_t virtio_net_rsc_do_coalesce(NetRscChain *chain, NetClientState *nc,
      NetRscSeg *seg, *nseg;
 
      QTAILQ_FOREACH_SAFE(seg, &chain->buffers, next, nseg) {
-        ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        if (chain->proto == ETH_P_IP) {
+            ret = virtio_net_rsc_coalesce4(chain, seg, buf, size, unit);
+        } else {
+            ret = virtio_net_rsc_coalesce6(chain, seg, buf, size, unit);
+        }
 
          if (ret == RSC_FINAL) {
              if (virtio_net_rsc_drain_seg(chain, seg) == 0) {
@@ -2082,7 +2127,8 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
 
      chain = (NetRscChain *)opq;
      virtio_net_rsc_extract_unit4(chain, buf, &unit);
-    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain, unit.ip, buf, size)) {
+    if (RSC_WANT != virtio_net_rsc_sanity_check4(chain,
+                                                 unit.u_ip.ip, buf, size)) {
          return virtio_net_do_receive(nc, buf, size);
      }
 
Quoted 7 lines

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2152

Quoted 6 lines
hw/net/virtio-net.c +2 -0
@@ -2102,13 +2148,74 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc,
      return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
  }
 
+static int32_t virtio_net_rsc_sanity_check6(NetRscChain *chain,
+                        struct ip6_header *ip, const uint8_t *buf, size_t size)

Indentation is wrong here.

ok.

Quoted 13 lines
Quoted 13 lines
hw/net/virtio-net.c +12 -0
+{
+    uint16_t ip_len;
+
+    if (size < (chain->hdr_size + ETH_IP6_HDR_SZ + TCP_HDR_SZ)) {
+        return RSC_BYPASS;
+    }
+
+    if (((0xF0 & ip->ip6_ctlun.ip6_un1.ip6_un1_flow) >> 4)
+        != IP_HEADER_VERSION_6) {
+        return RSC_BYPASS;
+    }
+
Quoted 8 lines

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2171

Quoted 7 lines
hw/net/virtio-net.c +7 -0
+    /* Both option and protocol is checked in this */
+    if (ip->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_TCP) {
+        chain->stat.bypass_not_tcp++;
+        return RSC_BYPASS;
+    }
+
+    /* Sanity check */

The comment is useless.

ok.

Quoted 15 lines
Quoted 4 lines
hw/net/virtio-net.c +3 -0
+    ip_len = htons(ip->ip6_ctlun.ip6_un1.ip6_un1_plen);
+    if (ip_len < TCP_HDR_SZ
+        || ip_len > (size - chain->hdr_size - ETH_IP6_HDR_SZ)) {

hw/net/virtio-net.c static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, · line 2184

Quoted 10 lines
hw/net/virtio-net.c +10 -0
+        chain->stat.ip_hacked++;
+        return RSC_BYPASS;
+    }
+
+    return RSC_WANT;
+}
+
+static size_t virtio_net_rsc_receive6(void *opq, NetClientState* nc,
+                                      const uint8_t *buf, size_t size)
+{

Rather similar to ipv4 version, need to unify the code.

ok.

Quoted 82 lines
Quoted 82 lines
hw/net/virtio-net.c +39 -3
+    int32_t ret;
+    NetRscChain *chain;
+    NetRscUnit unit;
+
+    chain = (NetRscChain *)opq;
+    virtio_net_rsc_extract_unit6(chain, buf, &unit);
+    if (RSC_WANT != virtio_net_rsc_sanity_check6(chain,
+                                                 unit.u_ip.ip6, buf, size)) {
+        return virtio_net_do_receive(nc, buf, size);
+    }
+
+    ret = virtio_net_rsc_tcp_ctrl_check(chain, unit.tcp);
+    if (ret == RSC_BYPASS) {
+        return virtio_net_do_receive(nc, buf, size);
+    } else if (ret == RSC_FINAL) {
+        return virtio_net_rsc_drain_flow(chain, nc, buf, size,
+                        ((chain->hdr_size + ETH_HDR_SZ) + 8), IP6_ADDR_SIZE,
+                        (chain->hdr_size + ETH_IP6_HDR_SZ), TCP_PORT_SIZE);
+    }
+
+    if (virtio_net_rsc_empty_cache(chain, nc, buf, size)) {
+        return size;
+    }
+
+    return virtio_net_rsc_do_coalesce(chain, nc, buf, size, &unit);
+}
+
  static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
                                              NetClientState *nc, uint16_t proto)
  {
      NetRscChain *chain;
 
      /* Only handle IPv4/6 */
-    if (proto != (uint16_t)ETH_P_IP) {
+    if ((proto != (uint16_t)ETH_P_IP) && (proto != (uint16_t)ETH_P_IPV6)) {
          return NULL;
      }
 
@@ -2121,7 +2228,11 @@ static NetRscChain *virtio_net_rsc_lookup_chain(VirtIONet * n,
      chain = g_malloc(sizeof(*chain));
      chain->hdr_size = n->guest_hdr_len;
      chain->proto = proto;
-    chain->max_payload = MAX_IP4_PAYLOAD;
+    if (proto == (uint16_t)ETH_P_IP) {
+        chain->max_payload = MAX_IP4_PAYLOAD;
+    } else {
+        chain->max_payload = MAX_IP6_PAYLOAD;
+    }
      chain->drain_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
                                        virtio_net_rsc_purge, chain);
      memset(&chain->stat, 0, sizeof(chain->stat));
@@ -2153,7 +2264,12 @@ static ssize_t virtio_net_rsc_receive(NetClientState *nc,
          return virtio_net_do_receive(nc, buf, size);
      } else {
          chain->stat.received++;
-        return virtio_net_rsc_receive4(chain, nc, buf, size);
+
+        if (proto == (uint16_t)ETH_P_IP) {
+            return virtio_net_rsc_receive4(chain, nc, buf, size);
+        } else  {
+            return virtio_net_rsc_receive6(chain, nc, buf, size);
+        }
      }
  }
 
include/hw/virtio/virtio.h +4 -1
index 3b1dfa8..13d20a4 100644
@@ -170,7 +170,10 @@ typedef struct NetRscStat {
 
  /* Rsc unit general info used to checking if can coalescing */
  typedef struct NetRscUnit {
-   struct ip_header *ip;   /* ip header */
+    union {
+        struct ip_header *ip;   /* ip header */
+        struct ip6_header *ip6; /* ip6 header */
+    } u_ip;
     uint16_t *ip_plen;      /* data len pointer in ip header field */
     struct tcp_header *tcp; /* tcp header */
     uint16_t tcp_hdrlen;    /* tcp header len */
#15 lore raw
In reply to [email protected]
Quoted 36 lines · On Tue, Mar 15, 2016 at 05:17:02PM +0800, [email protected] wrote:

From: Wei Xu <[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:
http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Quoted 6 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

So at this point this is still an RFC, pls label as such in the subject.
Also, commit log of each patch should also include info on how to activate a feature.

thanks!

Quoted 11 lines

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

 hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  75 +++++
 3 files changed, 677 insertions(+), 1 deletion(-)
Signature · 1 line
2.5.0
author
#16 lore raw

----- Original Message -----
From: "Michael S. Tsirkin" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected], [email protected], [email protected]
Sent: Tuesday, March 15, 2016 6:01:12 PM
Subject: Re: [Qemu-devel] [ Patch 0/2] Support Receive-Segment-Offload(RSC) for WHQL test of Window guest

Quoted 36 lines · On Tue, Mar 15, 2016 at 05:17:02PM +0800, [email protected] wrote:

From: Wei Xu <[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:
http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Quoted 6 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

So at this point this is still an RFC, pls label as such in the subject.
Also, commit log of each patch should also include info on how to activate a feature.

OK, thanks mst.

thanks!

Quoted 11 lines

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

 hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  75 +++++
 3 files changed, 677 insertions(+), 1 deletion(-)
Signature · 1 line
2.5.0
#17 lore raw
In reply to [email protected]
Quoted 28 lines · On 03/15/2016 05:17 PM, [email protected] wrote:

From: Wei Xu <[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:
http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 7 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Quoted 17 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

 hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  75 +++++
 3 files changed, 677 insertions(+), 1 deletion(-)
author
#18 lore raw
Quoted 36 lines · Jason Wang
Quoted 28 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:

   http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 7 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Quoted 2 lines

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Yes, there is something wrong with my previous description, i add some debug code and did new test, the packets are received by virtio_net_receive() and are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case did some hacking on the filter, it installed another lightweight filter, i'm not sure how these packets go in the guest, maybe they are received but dropped by driver or stack, etc.

I tried 'dropmonitor', it's very interesting but it helps very limitedly for windows guest, i can only use it with qemu on the host.

Quoted 16 lines
Quoted 16 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

  hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-net.h |   1 +
  include/hw/virtio/virtio.h     |  75 +++++
  3 files changed, 677 insertions(+), 1 deletion(-)
#19 lore raw
Quoted 41 lines · Wei Xu
Quoted 36 lines · Jason Wang
Quoted 28 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:
http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 7 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Quoted 2 lines

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Quoted 7 lines

Yes, there is something wrong with my previous description, i add some debug code and did new test, the packets are received by virtio_net_receive() and are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case did some hacking on the filter, it installed another lightweight filter, i'm not sure how these packets go in the guest, maybe they are received but dropped by driver or stack, etc.

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Quoted 18 lines

I tried 'dropmonitor', it's very interesting but it helps very limitedly for windows guest, i can only use it with qemu on the host.

Quoted 16 lines
Quoted 16 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

 hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   1 +
 include/hw/virtio/virtio.h     |  75 +++++
 3 files changed, 677 insertions(+), 1 deletion(-)
author
#20 lore raw
Quoted 48 lines · Michael S. Tsirkin
Quoted 40 lines · Wei Xu
Quoted 36 lines · Jason Wang
Quoted 28 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also benifits other guest works as a kernel 'gro' like feature with userspace implementation. Feature information:

   http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio is slow than vhost-net, there is about 1x to 3x performance improvement to userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 7 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot receive any packet in WHQL test, looks like the test traffic is not sent from on the support machine, test device can access both host and another linux guest, tried a lot of ways to work it out but failed, maybe debug from windows guest driver side can help figuring it out.

Quoted 2 lines

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Quoted 7 lines

Yes, there is something wrong with my previous description, i add some debug code and did new test, the packets are received by virtio_net_receive() and are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case did some hacking on the filter, it installed another lightweight filter, i'm not sure how these packets go in the guest, maybe they are received but dropped by driver or stack, etc.

Quoted 2 lines

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Yes, but this is to win guest, i tried to build a windows debug binary but failed, is there any possible missing path in virtio between pushed it to vring and notified the guest successfully? i'm sure at this by debugging it with gdb.

Quoted 19 lines
Quoted 19 lines

I tried 'dropmonitor', it's very interesting but it helps very limitedly for windows guest, i can only use it with qemu on the host.

Quoted 16 lines
Quoted 16 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup, this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

  hw/net/virtio-net.c            | 602 ++++++++++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-net.h |   1 +
  include/hw/virtio/virtio.h     |  75 +++++
  3 files changed, 677 insertions(+), 1 deletion(-)
#21 lore raw
Quoted 66 lines · Wei Xu
Quoted 65 lines · Michael S. Tsirkin
Quoted 64 lines · Wei Xu
Quoted 48 lines · Jason Wang
Quoted 36 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also
benifits other guest works as a kernel 'gro' like feature with userspace implementation.
Feature information:

   http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio
is slow than vhost-net, there is about 1x to 3x performance improvement to
userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get
less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test
the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control
the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 11 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot
receive any packet in WHQL test, looks like the test traffic is not sent from
on the support machine, test device can access both host and another linux
guest, tried a lot of ways to work it out but failed, maybe debug from windows
guest driver side can help figuring it out.

Quoted 3 lines

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Yes, there is something wrong with my previous description, i add some debug
code and did new test, the packets are received by virtio_net_receive() and
are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case
did some hacking on the filter, it installed another lightweight filter, i'm
not sure how these packets go in the guest, maybe they are received but dropped by driver or stack, etc.

Quoted 6 lines
Quoted 2 lines

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Yes, but this is to win guest, i tried to build a windows debug binary but failed, is there any possible missing path in virtio between pushed it to vring and notified the guest successfully? i'm sure at this by debugging it with gdb.

Is the packet always dropped or does it help if you turn off some configuration (e.g checksum offloads) works?

Quoted 24 lines
Quoted 22 lines
Quoted 22 lines

I tried 'dropmonitor', it's very interesting but it helps very limitedly for
windows guest, i can only use it with qemu on the host.

Quoted 18 lines
Quoted 18 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup,
this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

  hw/net/virtio-net.c            | 602
++++++++++++++++++++++++++++++++++++++++-
  include/hw/virtio/virtio-net.h |   1 +
  include/hw/virtio/virtio.h     |  75 +++++
  3 files changed, 677 insertions(+), 1 deletion(-)
author
#22 lore raw
Quoted 67 lines · Jason Wang
Quoted 65 lines · Wei Xu
Quoted 64 lines · Michael S. Tsirkin
Quoted 63 lines · Wei Xu
Quoted 48 lines · Jason Wang
Quoted 36 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also
benifits other guest works as a kernel 'gro' like feature with userspace implementation.
Feature information:

    http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio
is slow than vhost-net, there is about 1x to 3x performance improvement to
userspace virtio, this is done by turning this feature on and disable 'tso/gso/gro' on corresponding tap interface and guest interface, while get
less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test
the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control
the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 11 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot
receive any packet in WHQL test, looks like the test traffic is not sent from
on the support machine, test device can access both host and another linux
guest, tried a lot of ways to work it out but failed, maybe debug from windows
guest driver side can help figuring it out.

Quoted 3 lines

I think you need figure out where was the packet dropped first. If the packet was not dropped by windows guest, you may want to try dropmonitor.

Yes, there is something wrong with my previous description, i add some debug
code and did new test, the packets are received by virtio_net_receive() and
are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case
did some hacking on the filter, it installed another lightweight filter, i'm
not sure how these packets go in the guest, maybe they are received but dropped by driver or stack, etc.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Yes, but this is to win guest, i tried to build a windows debug binary but failed, is there any possible missing path in virtio between pushed it to vring and notified the guest successfully? i'm sure at this by debugging it with gdb.

Is the packet always dropped or does it help if you turn off some configuration (e.g checksum offloads) works?

Yes, only the test packets are dropped, there is no checksum for ipv6 header, i remembered i disabled checksum offloads and changed other features(RSS) in the guest but it doesn't help, is there any other tunable values for qemu?

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines
Quoted 22 lines

I tried 'dropmonitor', it's very interesting but it helps very limitedly for
windows guest, i can only use it with qemu on the host.

Quoted 18 lines
Quoted 18 lines

Note:
A 'MessageDevice' nic chose as 'Realtek' will panic the system sometimes during setup,
this can be figured out by replacing it with an 'e1000' nic.

Todo:
More sanity check and tcp 'ecn' and 'window' scale test.

Wei Xu (2):
virtio-net rsc: support coalescing ipv4 tcp traffic virtio-net rsc: support coalescing ipv6 tcp traffic

   hw/net/virtio-net.c            | 602
++++++++++++++++++++++++++++++++++++++++-
   include/hw/virtio/virtio-net.h |   1 +
   include/hw/virtio/virtio.h     |  75 +++++
   3 files changed, 677 insertions(+), 1 deletion(-)
#23 lore raw
Quoted 80 lines · Wei Xu
Quoted 77 lines · Jason Wang
Quoted 75 lines · Wei Xu
Quoted 68 lines · Michael S. Tsirkin
Quoted 55 lines · Wei Xu
Quoted 50 lines · Jason Wang
Quoted 38 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also
benifits other guest works as a kernel 'gro' like feature with userspace implementation.
Feature information:

http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio
is slow than vhost-net, there is about 1x to 3x performance improvement to
userspace virtio, this is done by turning this feature on and disable
'tso/gso/gro' on corresponding tap interface and guest interface, while get
less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test
the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control
the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 11 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot
receive any packet in WHQL test, looks like the test traffic is not sent from
on the support machine, test device can access both host and another linux
guest, tried a lot of ways to work it out but failed, maybe debug from windows
guest driver side can help figuring it out.

Quoted 4 lines

I think you need figure out where was the packet dropped first. If the
packet was not dropped by windows guest, you may want to try dropmonitor.

Quoted 12 lines

Yes, there is something wrong with my previous description, i add some debug
code and did new test, the packets are received by virtio_net_receive() and
are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case
did some hacking on the filter, it installed another lightweight filter, i'm
not sure how these packets go in the guest, maybe they are received but
dropped by driver or stack, etc.

Quoted 2 lines

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Yes, but this is to win guest, i tried to build a windows debug binary but failed, is there any possible missing path in virtio between pushed it to vring and notified the guest successfully? i'm sure at this by debugging it with gdb.

Quoted 7 lines
Quoted 2 lines

Is the packet always dropped or does it help if you turn off some configuration (e.g checksum offloads) works?

Yes, only the test packets are dropped, there is no checksum for ipv6 header,
i remembered i disabled checksum offloads and changed other features(RSS) in the guest but it doesn't help, is there any other tunable values for qemu?

-device virtio-net-pci,? can gives you all the properties.

author
#24 lore raw
Quoted 80 lines · Jason Wang
Quoted 78 lines · Wei Xu
Quoted 76 lines · Jason Wang
Quoted 75 lines · Wei Xu
Quoted 68 lines · Michael S. Tsirkin
Quoted 55 lines · Wei Xu
Quoted 50 lines · Jason Wang
Quoted 38 lines · On 03/15/2016 05:17 PM,[email protected] wrote:

From: Wei Xu<[email protected]>

Fixed issues based on rfc patch v2:
1. Removed big param list, replace it with 'NetRscUnit'
2. Different virtio header size
3. Modify callback function to direct call.
4. Needn't check the failure of g_malloc()
5. Other code format adjustment, macro naming, etc

This patch is to support WHQL test for Windows guest, while this feature also
benifits other guest works as a kernel 'gro' like feature with userspace implementation.
Feature information:

http://msdn.microsoft.com/en-us/library/windows/hardware/jj853324

Both IPv4 and IPv6 are supported, though performance with userspace virtio
is slow than vhost-net, there is about 1x to 3x performance improvement to
userspace virtio, this is done by turning this feature on and disable
'tso/gso/gro' on corresponding tap interface and guest interface, while get
less improment with all these feature on.

Test steps:
Although this feature is mainly used for window guest, i used linux guest to help test
the feature, to make things simple, i used 3 steps to test the patch as i moved on.
1. With a tcp socket client/server pair running on 2 linux guest, thus i can control
the traffic and debugging the code as i want.
2. Netperf on linux guest test the throughput.
3. WHQL test with 2 Windows guests.

Quoted 11 lines

Current status:
IPv4 pass all the above tests.
IPv6 just passed test step 1 and 2 as described ahead, the virtio nic cannot
receive any packet in WHQL test, looks like the test traffic is not sent from
on the support machine, test device can access both host and another linux
guest, tried a lot of ways to work it out but failed, maybe debug from windows
guest driver side can help figuring it out.

Quoted 4 lines

I think you need figure out where was the packet dropped first. If the
packet was not dropped by windows guest, you may want to try dropmonitor.

Quoted 12 lines

Yes, there is something wrong with my previous description, i add some debug
code and did new test, the packets are received by virtio_net_receive() and
are finished putting to the vring with no error and sent to win guest already, but wireshark on win guest doesn't get it, because the test case
did some hacking on the filter, it installed another lightweight filter, i'm
not sure how these packets go in the guest, maybe they are received but
dropped by driver or stack, etc.

Quoted 2 lines

Add some debug output in the driver, rebuild it and see packets as they are received and passed up the stack.

Yes, but this is to win guest, i tried to build a windows debug binary but failed, is there any possible missing path in virtio between pushed it to vring and notified the guest successfully? i'm sure at this by debugging it with gdb.

Quoted 9 lines
Quoted 7 lines
Quoted 2 lines

Is the packet always dropped or does it help if you turn off some configuration (e.g checksum offloads) works?

Yes, only the test packets are dropped, there is no checksum for ipv6 header,
i remembered i disabled checksum offloads and changed other features(RSS) in the guest but it doesn't help, is there any other tunable values for qemu?

-device virtio-net-pci,? can gives you all the properties.

ok, thanks a lot.