[dpdk-dev,v4,8/8] virtio/lib:add guest offload handle

Message ID 1447224046-1169-9-git-send-email-jijiang.liu@intel.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

Jijiang Liu Nov. 11, 2015, 6:40 a.m. UTC
  Enqueue guest offload(CSUM and TSO) handle.

Signed-off-by: Jijiang Liu <jijiang.liu@intel.com>
---
 lib/librte_vhost/vhost_rxtx.c |   45 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 2 deletions(-)
  

Comments

Yuanhan Liu Nov. 11, 2015, 8:23 a.m. UTC | #1
Regarding to your patch title, there are two minor pits:

- the prefix should be "vhost" but not "virtio/lib".

- you should add an extra space after ":"

On Wed, Nov 11, 2015 at 02:40:46PM +0800, Jijiang Liu wrote:
> Enqueue guest offload(CSUM and TSO) handle.

(ALL) Your patch lacks some explanation. And I don't think it's about
guest offload handling, it's about setting the right offload fields for
RX side, such as VIRTIO_NET_HDR_F_NEEDS_CSUM.

And you need spend few words to state why that is required. Something
like following might help others to review:

    For packet going through from one VM to another VM without passing
    the NIC, and the VM claiming that it supports checksum offload,
    no one will actually calculate the checksum, hence, the packet
    will be dropped at TCP layer, due to checksum validation is failed.

    However, 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.

    At 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 |   45 +++++++++++++++++++++++++++++++++++++++-
>  1 files changed, 43 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/librte_vhost/vhost_rxtx.c b/lib/librte_vhost/vhost_rxtx.c
> index 9e70990..468fed8 100644
> --- a/lib/librte_vhost/vhost_rxtx.c
> +++ b/lib/librte_vhost/vhost_rxtx.c
> @@ -54,6 +54,42 @@ 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)
> +{

As virtio_hdr is set per mbuf, you'd better reset net_hdr first before
setting it. Otherwise, if this mbuf has no offload related stuff, you
may still get a net_hdr with offload related fields set, due to last
mbuf has that.

I know the chance is rare, but it's for code logic.


	--yliu
  
Thomas Monjalon Nov. 11, 2015, 10:19 a.m. UTC | #2
Yuanhan,
You deserve a "review award"!
Thanks a lot

2015-11-11 16:23, Yuanhan Liu:
> Regarding to your patch title, there are two minor pits:
> 
> - the prefix should be "vhost" but not "virtio/lib".
> 
> - you should add an extra space after ":"
> 
> On Wed, Nov 11, 2015 at 02:40:46PM +0800, Jijiang Liu wrote:
> > Enqueue guest offload(CSUM and TSO) handle.
> 
> (ALL) Your patch lacks some explanation. And I don't think it's about
> guest offload handling, it's about setting the right offload fields for
> RX side, such as VIRTIO_NET_HDR_F_NEEDS_CSUM.
> 
> And you need spend few words to state why that is required. Something
> like following might help others to review:
> 
>     For packet going through from one VM to another VM without passing
>     the NIC, and the VM claiming that it supports checksum offload,
>     no one will actually calculate the checksum, hence, the packet
>     will be dropped at TCP layer, due to checksum validation is failed.
> 
>     However, 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.
> 
>     At 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 |   45 +++++++++++++++++++++++++++++++++++++++-
> >  1 files changed, 43 insertions(+), 2 deletions(-)
> > 
> > diff --git a/lib/librte_vhost/vhost_rxtx.c b/lib/librte_vhost/vhost_rxtx.c
> > index 9e70990..468fed8 100644
> > --- a/lib/librte_vhost/vhost_rxtx.c
> > +++ b/lib/librte_vhost/vhost_rxtx.c
> > @@ -54,6 +54,42 @@ 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)
> > +{
> 
> As virtio_hdr is set per mbuf, you'd better reset net_hdr first before
> setting it. Otherwise, if this mbuf has no offload related stuff, you
> may still get a net_hdr with offload related fields set, due to last
> mbuf has that.
> 
> I know the chance is rare, but it's for code logic.
> 
> 
> 	--yliu
  

Patch

diff --git a/lib/librte_vhost/vhost_rxtx.c b/lib/librte_vhost/vhost_rxtx.c
index 9e70990..468fed8 100644
--- a/lib/librte_vhost/vhost_rxtx.c
+++ b/lib/librte_vhost/vhost_rxtx.c
@@ -54,6 +54,42 @@  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)
+{
+	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 +103,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 +175,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 +258,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 +334,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);