DPDK patches and discussions
 help / color / mirror / Atom feed
From: Jijiang Liu <jijiang.liu@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v5 2/4] vhost/lib: add guest offload setting
Date: Thu, 12 Nov 2015 20:07:04 +0800	[thread overview]
Message-ID: <1447330026-16685-3-git-send-email-jijiang.liu@intel.com> (raw)
In-Reply-To: <1447330026-16685-1-git-send-email-jijiang.liu@intel.com>

Add guest offload setting in vhost lib.

Refer to the feature bits description in the Virtual I/O Device (VIRTIO) Version 1.0 below, 

1. VIRTIO_NET_F_GUEST_CSUM (1) Driver handles packets with partial checksum.

2. If the VIRTIO_NET_F_GUEST_CSUM feature was negotiated, the VIRTIO_NET_HDR_F_NEEDS_- CSUM bit in flags MAY be set: if so, the checksum on the packet is incomplete and csum_start and csum_offset indicate how to calculate it (see Packet Transmission point 1).

3. If the VIRTIO_NET_F_GUEST_TSO4, TSO6 or UFO options were negotiated, then gso_type MAY be something other than VIRTIO_NET_HDR_GSO_NONE, and gso_size field indicates the desired MSS (see Packet Transmission point 2).

In order to support these features, the following changes are added,
 
1. Extend 'VHOST_SUPPORTED_FEATURES' macro to add the offload features negotiation.
 
2. Enqueue these offloads: convert some fields in mbuf to the fields in virtio_net_hdr.

There are more explanations for the implementation.

For VM2VM case, there is no need to do checksum, for we
think the data should be reliable enough, and setting VIRTIO_NET_HDR_F_NEEDS_CSUM
at RX side will let the TCP layer to bypass the checksum validation,
so that the RX side could receive the packet in the end.

In terms of us-vhost, at vhost RX side, the offload information is inherited from mbuf, which is
in turn inherited from TX side. If we can still get those info at RX
side, it means the packet is from another VM at same host.  So, it's
safe to set the VIRTIO_NET_HDR_F_NEEDS_CSUM, to skip checksum validation.

Signed-off-by: Jijiang Liu <jijiang.liu@intel.com>
---
 lib/librte_vhost/vhost_rxtx.c |   47 +++++++++++++++++++++++++++++++++++++++-
 lib/librte_vhost/virtio-net.c |    5 +++-
 2 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/lib/librte_vhost/vhost_rxtx.c b/lib/librte_vhost/vhost_rxtx.c
index 47d5f85..9d97e19 100644
--- a/lib/librte_vhost/vhost_rxtx.c
+++ b/lib/librte_vhost/vhost_rxtx.c
@@ -54,6 +54,44 @@ is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
 	return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
 }
 
+static void
+virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
+{
+	memset(net_hdr, 0, sizeof(struct virtio_net_hdr));
+
+	if (m_buf->ol_flags & PKT_TX_L4_MASK) {
+		net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
+		net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
+
+		switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
+		case PKT_TX_TCP_CKSUM:
+			net_hdr->csum_offset = (offsetof(struct tcp_hdr,
+						cksum));
+			break;
+		case PKT_TX_UDP_CKSUM:
+			net_hdr->csum_offset = (offsetof(struct udp_hdr,
+						dgram_cksum));
+			break;
+		case PKT_TX_SCTP_CKSUM:
+			net_hdr->csum_offset = (offsetof(struct sctp_hdr,
+						cksum));
+			break;
+		}
+	}
+
+	if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
+		if (m_buf->ol_flags & PKT_TX_IPV4)
+			net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
+		else
+			net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
+		net_hdr->gso_size = m_buf->tso_segsz;
+		net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
+					+ m_buf->l4_len;
+	}
+
+	return;
+}
+
 /**
  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
  * be received from the physical port or from another virtio device. A packet
@@ -67,7 +105,7 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 {
 	struct vhost_virtqueue *vq;
 	struct vring_desc *desc;
-	struct rte_mbuf *buff;
+	struct rte_mbuf *buff, *first_buff;
 	/* The virtio_hdr is initialised to 0. */
 	struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
 	uint64_t buff_addr = 0;
@@ -139,6 +177,7 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 		desc = &vq->desc[head[packet_success]];
 
 		buff = pkts[packet_success];
+		first_buff = buff;
 
 		/* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
 		buff_addr = gpa_to_vva(dev, desc->addr);
@@ -221,7 +260,9 @@ virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
 
 		if (unlikely(uncompleted_pkt == 1))
 			continue;
-
+		
+		virtio_enqueue_offload(first_buff, &virtio_hdr.hdr);
+		
 		rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
 			(const void *)&virtio_hdr, vq->vhost_hlen);
 
@@ -295,6 +336,8 @@ copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
 	LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
 		dev->device_fh, virtio_hdr.num_buffers);
 
+	virtio_enqueue_offload(pkt, &virtio_hdr.hdr);	
+
 	rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
 		(const void *)&virtio_hdr, vq->vhost_hlen);
 
diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c
index 81bd309..839a333 100644
--- a/lib/librte_vhost/virtio-net.c
+++ b/lib/librte_vhost/virtio-net.c
@@ -80,7 +80,10 @@ static struct virtio_net_config_ll *ll_root;
 				(1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
 				(1ULL << VIRTIO_NET_F_HOST_TSO4) | \
 				(1ULL << VIRTIO_NET_F_HOST_TSO6) | \
-				(1ULL << VIRTIO_NET_F_CSUM))
+				(1ULL << VIRTIO_NET_F_CSUM)    | \
+				(1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
+				(1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
+				(1ULL << VIRTIO_NET_F_GUEST_TSO6))
 
 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
 
-- 
1.7.7.6

  parent reply	other threads:[~2015-11-12 12:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-12 12:07 [dpdk-dev] [PATCH v5 0/4] add virtio offload support in us-vhost Jijiang Liu
2015-11-12 12:07 ` [dpdk-dev] [PATCH v5 1/4] vhost/lib: add vhost TX offload capabilities in vhost lib Jijiang Liu
2015-11-13  7:01   ` Yuanhan Liu
2015-11-16  7:56     ` Liu, Jijiang
2015-11-12 12:07 ` Jijiang Liu [this message]
2015-11-12 12:07 ` [dpdk-dev] [PATCH v5 3/4] sample/vhost: remove the ipv4_hdr structure defination Jijiang Liu
2015-11-12 12:07 ` [dpdk-dev] [PATCH v5 4/4] example/vhost: add virtio offload test in vhost sample Jijiang Liu
2015-11-13  6:43 ` [dpdk-dev] [PATCH v5 0/4] add virtio offload support in us-vhost Yuanhan Liu
2015-11-13  7:35 ` Xu, Qian Q
2015-12-18  1:19   ` Liu, Jijiang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447330026-16685-3-git-send-email-jijiang.liu@intel.com \
    --to=jijiang.liu@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).