DPDK patches and discussions
 help / color / mirror / Atom feed
From: Olivier Matz <olivier.matz@6wind.com>
To: dev@dpdk.org, yuanhan.liu@linux.intel.com
Cc: konstantin.ananyev@intel.com, sugesh.chandran@intel.com,
	bruce.richardson@intel.com, jianfeng.tan@intel.com,
	helin.zhang@intel.com, adrien.mazarguil@6wind.com,
	stephen@networkplumber.org, dprovan@bivio.net,
	xiao.w.wang@intel.com, maxime.coquelin@redhat.com
Subject: [dpdk-dev] [PATCH v3 12/12] net/virtio: add Tso support
Date: Thu, 13 Oct 2016 16:16:11 +0200	[thread overview]
Message-ID: <1476368171-18176-13-git-send-email-olivier.matz@6wind.com> (raw)
In-Reply-To: <1476368171-18176-1-git-send-email-olivier.matz@6wind.com>

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---
 drivers/net/virtio/virtio_ethdev.c |   6 ++
 drivers/net/virtio/virtio_ethdev.h |   2 +
 drivers/net/virtio/virtio_rxtx.c   | 133 +++++++++++++++++++++++++++++++++++--
 3 files changed, 136 insertions(+), 5 deletions(-)

diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c
index 109f855..969edb6 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -1572,6 +1572,7 @@ virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complet
 static void
 virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 {
+	uint64_t tso_mask;
 	struct virtio_hw *hw = dev->data->dev_private;
 
 	if (dev->pci_dev)
@@ -1599,6 +1600,11 @@ virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 			DEV_TX_OFFLOAD_UDP_CKSUM |
 			DEV_TX_OFFLOAD_TCP_CKSUM;
 	}
+
+	tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
+		(1ULL << VIRTIO_NET_F_HOST_TSO6);
+	if ((hw->guest_features & tso_mask) == tso_mask)
+		dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
 }
 
 /*
diff --git a/drivers/net/virtio/virtio_ethdev.h b/drivers/net/virtio/virtio_ethdev.h
index d55e7ed..f77f618 100644
--- a/drivers/net/virtio/virtio_ethdev.h
+++ b/drivers/net/virtio/virtio_ethdev.h
@@ -63,6 +63,8 @@
 	 1u << VIRTIO_NET_F_CTRL_RX	  |	\
 	 1u << VIRTIO_NET_F_CTRL_VLAN	  |	\
 	 1u << VIRTIO_NET_F_CSUM	  |	\
+	 1u << VIRTIO_NET_F_HOST_TSO4	  |	\
+	 1u << VIRTIO_NET_F_HOST_TSO6	  |	\
 	 1u << VIRTIO_NET_F_MRG_RXBUF	  |	\
 	 1u << VIRTIO_RING_F_INDIRECT_DESC |    \
 	 1ULL << VIRTIO_F_VERSION_1)
diff --git a/drivers/net/virtio/virtio_rxtx.c b/drivers/net/virtio/virtio_rxtx.c
index 0fa635a..4b01ea3 100644
--- a/drivers/net/virtio/virtio_rxtx.c
+++ b/drivers/net/virtio/virtio_rxtx.c
@@ -209,10 +209,117 @@ virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
 	return 0;
 }
 
+/* When doing TSO, the IP length is not included in the pseudo header
+ * checksum of the packet given to the PMD, but for virtio it is
+ * expected.
+ */
+static void
+virtio_tso_fix_cksum(struct rte_mbuf *m)
+{
+	/* common case: header is not fragmented */
+	if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
+			m->l4_len)) {
+		struct ipv4_hdr *iph;
+		struct ipv6_hdr *ip6h;
+		struct tcp_hdr *th;
+		uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
+		uint32_t tmp;
+
+		iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
+		th = RTE_PTR_ADD(iph, m->l3_len);
+		if ((iph->version_ihl >> 4) == 4) {
+			iph->hdr_checksum = 0;
+			iph->hdr_checksum = rte_ipv4_cksum(iph);
+			ip_len = iph->total_length;
+			ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
+				m->l3_len);
+		} else {
+			ip6h = (struct ipv6_hdr *)iph;
+			ip_paylen = ip6h->payload_len;
+		}
+
+		/* calculate the new phdr checksum not including ip_paylen */
+		prev_cksum = th->cksum;
+		tmp = prev_cksum;
+		tmp += ip_paylen;
+		tmp = (tmp & 0xffff) + (tmp >> 16);
+		new_cksum = tmp;
+
+		/* replace it in the packet */
+		th->cksum = new_cksum;
+	} else {
+		const struct ipv4_hdr *iph;
+		struct ipv4_hdr iph_copy;
+		union {
+			uint16_t u16;
+			uint8_t u8[2];
+		} prev_cksum, new_cksum, ip_len, ip_paylen, ip_csum;
+		uint32_t tmp;
+
+		/* Same code than above, but we use rte_pktmbuf_read()
+		 * or we read/write in mbuf data one byte at a time to
+		 * avoid issues if the packet is multi segmented.
+		 */
+
+		uint8_t ip_version;
+
+		ip_version = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+			m->l2_len) >> 4;
+
+		/* calculate ip checksum (API imposes to set it to 0)
+		 * and get ip payload len */
+		if (ip_version == 4) {
+			*rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 10) = 0;
+			*rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 11) = 0;
+			iph = rte_pktmbuf_read(m, m->l2_len,
+				sizeof(*iph), &iph_copy);
+			ip_csum.u16 = rte_ipv4_cksum(iph);
+			*rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 10) = ip_csum.u8[0];
+			*rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 11) = ip_csum.u8[1];
+
+			ip_len.u8[0] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 2);
+			ip_len.u8[1] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 3);
+
+			ip_paylen.u16 = rte_cpu_to_be_16(
+				rte_be_to_cpu_16(ip_len.u16) - m->l3_len);
+		} else {
+			ip_paylen.u8[0] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 4);
+			ip_paylen.u8[1] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+				m->l2_len + 5);
+		}
+
+		/* calculate the new phdr checksum not including ip_paylen */
+		/* get phdr cksum at offset 16 of TCP header */
+		prev_cksum.u8[0] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+			m->l2_len + m->l3_len + 16);
+		prev_cksum.u8[1] = *rte_pktmbuf_mtod_offset(m, uint8_t *,
+			m->l2_len + m->l3_len + 17);
+		tmp = prev_cksum.u16;
+		tmp += ip_paylen.u16;
+		tmp = (tmp & 0xffff) + (tmp >> 16);
+		new_cksum.u16 = tmp;
+
+		/* replace it in the packet */
+		*rte_pktmbuf_mtod_offset(m, uint8_t *,
+			m->l2_len + m->l3_len + 16) = new_cksum.u8[0];
+		*rte_pktmbuf_mtod_offset(m, uint8_t *,
+			m->l2_len + m->l3_len + 17) = new_cksum.u8[1];
+	}
+}
+
 static inline int
 tx_offload_enabled(struct virtio_hw *hw)
 {
-	return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM);
+	return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
+		vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
+		vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
 }
 
 static inline void
@@ -274,8 +381,11 @@ virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
 		idx = start_dp[idx].next;
 	}
 
+	/* Checksum Offload / TSO */
 	if (offload) {
-		/* Checksum Offload */
+		if (cookie->ol_flags & PKT_TX_TCP_SEG)
+			cookie->ol_flags |= PKT_TX_TCP_CKSUM;
+
 		switch (cookie->ol_flags & PKT_TX_L4_MASK) {
 		case PKT_TX_UDP_CKSUM:
 			hdr->csum_start = cookie->l2_len + cookie->l3_len;
@@ -297,9 +407,22 @@ virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
 			break;
 		}
 
-		hdr->gso_type = 0;
-		hdr->gso_size = 0;
-		hdr->hdr_len = 0;
+		/* TCP Segmentation Offload */
+		if (cookie->ol_flags & PKT_TX_TCP_SEG) {
+			virtio_tso_fix_cksum(cookie);
+			hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
+				VIRTIO_NET_HDR_GSO_TCPV6 :
+				VIRTIO_NET_HDR_GSO_TCPV4;
+			hdr->gso_size = cookie->tso_segsz;
+			hdr->hdr_len =
+				cookie->l2_len +
+				cookie->l3_len +
+				cookie->l4_len;
+		} else {
+			hdr->gso_type = 0;
+			hdr->gso_size = 0;
+			hdr->hdr_len = 0;
+		}
 	}
 
 	do {
-- 
2.8.1

  parent reply	other threads:[~2016-10-13 14:16 UTC|newest]

Thread overview: 97+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-21  8:08 [dpdk-dev] [PATCH 00/12] net/virtio: add offload support Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 01/12] virtio: move device initialization in a function Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 02/12] virtio: setup and start cq in configure callback Olivier Matz
2016-07-21 21:15   ` Stephen Hemminger
2016-07-22  7:54     ` Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 03/12] virtio: reinitialize the device " Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 04/12] mbuf: add function to calculate a checksum Olivier Matz
2016-07-21 10:51   ` Ananyev, Konstantin
2016-07-21 16:26     ` Don Provan
2016-07-21 16:46       ` Olivier Matz
2016-07-22  8:24     ` Olivier Matz
2016-08-29 14:52       ` Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 05/12] mbuf: add new Rx checksum mbuf flags Olivier Matz
2016-07-21 21:22   ` Stephen Hemminger
2016-07-22  8:03     ` Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 06/12] app/testpmd: fix checksum stats in csum engine Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 07/12] mbuf: new flag for LRO Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 08/12] app/testpmd: display lro segment size Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 09/12] virtio: add Rx checksum offload support Olivier Matz
2016-07-27  9:52   ` Wang, Xiao W
2016-07-21  8:08 ` [dpdk-dev] [PATCH 10/12] virtio: add Tx " Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 11/12] virtio: add Lro support Olivier Matz
2016-07-21  8:08 ` [dpdk-dev] [PATCH 12/12] virtio: add Tso support Olivier Matz
2016-10-03  9:00 ` [dpdk-dev] [PATCH v2 00/12] net/virtio: add offload support Olivier Matz
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 01/12] virtio: move device initialization in a function Olivier Matz
2016-10-11 12:30     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 02/12] virtio: setup and start cq in configure callback Olivier Matz
2016-10-11 12:47     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 03/12] virtio: reinitialize the device " Olivier Matz
2016-10-11 13:13     ` Maxime Coquelin
2016-10-12 14:41     ` Yuanhan Liu
2016-10-12 16:01       ` Olivier MATZ
2016-10-13  7:54         ` Yuanhan Liu
2016-10-13 13:57           ` Olivier MATZ
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 04/12] net: add function to calculate a checksum in a mbuf Olivier Matz
2016-10-11 13:25     ` Maxime Coquelin
2016-10-11 13:33       ` Olivier MATZ
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 05/12] mbuf: add new Rx checksum mbuf flags Olivier Matz
2016-10-11 13:43     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 06/12] app/testpmd: fix checksum stats in csum engine Olivier Matz
2016-10-11 13:46     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 07/12] mbuf: new flag for LRO Olivier Matz
2016-10-11 13:48     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 08/12] app/testpmd: display lro segment size Olivier Matz
2016-10-11 13:49     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 09/12] virtio: add Rx checksum offload support Olivier Matz
2016-10-03 12:51     ` Maxime Coquelin
2016-10-05 11:56       ` Olivier Matz
2016-10-05 13:27         ` Maxime Coquelin
2016-10-05 13:30           ` Olivier Matz
2016-10-12 13:02           ` Yuanhan Liu
2016-10-12 15:55             ` Olivier MATZ
2016-10-11 14:04     ` Maxime Coquelin
2016-10-11 14:29       ` Olivier MATZ
2016-10-11 14:36         ` Maxime Coquelin
2016-10-11 14:49           ` Olivier MATZ
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 10/12] virtio: add Tx " Olivier Matz
2016-10-07  7:25     ` Maxime Coquelin
2016-10-07 16:36       ` Olivier Matz
2016-10-13  8:38     ` Yuanhan Liu
2016-10-13 13:58       ` Olivier MATZ
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 11/12] virtio: add Lro support Olivier Matz
2016-10-11 14:21     ` Maxime Coquelin
2016-10-03  9:00   ` [dpdk-dev] [PATCH v2 12/12] virtio: add Tso support Olivier Matz
2016-10-13  8:18     ` Yuanhan Liu
2016-10-13 14:02       ` Olivier MATZ
2016-10-13 14:16         ` Yuanhan Liu
2016-10-13 14:52           ` Olivier MATZ
2016-10-13 15:01             ` Yuanhan Liu
2016-10-13 15:15               ` Olivier MATZ
2016-10-13 15:29                 ` Yuanhan Liu
2016-10-13 15:45                   ` Olivier Matz
2016-10-13 16:01                     ` Yuanhan Liu
2016-10-13 15:04             ` Yuanhan Liu
2016-10-13 23:33       ` Stephen Hemminger
2016-10-18 14:07         ` Olivier Matz
2016-10-11 11:35   ` [dpdk-dev] [PATCH v2 00/12] net/virtio: add offload support Yuanhan Liu
2016-10-11 12:14     ` Olivier MATZ
2016-10-11 15:37       ` Yuanhan Liu
2016-10-13 14:15 ` [dpdk-dev] [PATCH v3 " Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 01/12] net/virtio: move device initialization in a function Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 02/12] net/virtio: setup and start cq in configure callback Olivier Matz
2016-11-02  1:38     ` Yao, Lei A
2016-11-08 14:58       ` Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 03/12] net/virtio: reinitialize the device " Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 04/12] net: add function to calculate a checksum in a mbuf Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 05/12] mbuf: add new Rx checksum mbuf flags Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 06/12] app/testpmd: adapt checksum stats in csum engine Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 07/12] mbuf: new flag for LRO Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 08/12] app/testpmd: display lro segment size Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 09/12] net/virtio: add Rx checksum offload support Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 10/12] net/virtio: add Tx " Olivier Matz
2016-10-13 14:16   ` [dpdk-dev] [PATCH v3 11/12] net/virtio: add Lro support Olivier Matz
2016-10-13 14:16   ` Olivier Matz [this message]
2016-10-13 16:05     ` [dpdk-dev] [PATCH v3 12/12] net/virtio: add Tso support Yuanhan Liu
2016-10-13 18:50       ` Thomas Monjalon
2016-10-13 19:58         ` Olivier Matz

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=1476368171-18176-13-git-send-email-olivier.matz@6wind.com \
    --to=olivier.matz@6wind.com \
    --cc=adrien.mazarguil@6wind.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=dprovan@bivio.net \
    --cc=helin.zhang@intel.com \
    --cc=jianfeng.tan@intel.com \
    --cc=konstantin.ananyev@intel.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=stephen@networkplumber.org \
    --cc=sugesh.chandran@intel.com \
    --cc=xiao.w.wang@intel.com \
    --cc=yuanhan.liu@linux.intel.com \
    /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).