app/testpmd: fix TSO when using outer checksum offloads
[dpdk.git] / app / test-pmd / csumonly.c
index 0b89d89..0a7af79 100644 (file)
@@ -93,14 +93,20 @@ struct testpmd_offload_info {
        uint16_t l3_len;
        uint16_t l4_len;
        uint8_t l4_proto;
-       uint8_t l4_tun_len;
        uint8_t is_tunnel;
        uint16_t outer_ethertype;
        uint16_t outer_l2_len;
        uint16_t outer_l3_len;
+       uint8_t outer_l4_proto;
        uint16_t tso_segsz;
 };
 
+/* simplified GRE header (flags must be 0) */
+struct simple_gre_hdr {
+       uint16_t flags;
+       uint16_t proto;
+};
+
 static uint16_t
 get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags)
 {
@@ -191,6 +197,121 @@ parse_ethernet(struct ether_hdr *eth_hdr, struct testpmd_offload_info *info)
        }
 }
 
+/* Parse a vxlan header */
+static void
+parse_vxlan(struct udp_hdr *udp_hdr, struct testpmd_offload_info *info,
+       uint64_t mbuf_olflags)
+{
+       struct ether_hdr *eth_hdr;
+
+       /* check udp destination port, 4789 is the default vxlan port
+        * (rfc7348) or that the rx offload flag is set (i40e only
+        * currently) */
+       if (udp_hdr->dst_port != _htons(4789) &&
+               (mbuf_olflags & (PKT_RX_TUNNEL_IPV4_HDR |
+                       PKT_RX_TUNNEL_IPV6_HDR)) == 0)
+               return;
+
+       info->is_tunnel = 1;
+       info->outer_ethertype = info->ethertype;
+       info->outer_l2_len = info->l2_len;
+       info->outer_l3_len = info->l3_len;
+       info->outer_l4_proto = info->l4_proto;
+
+       eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
+               sizeof(struct udp_hdr) +
+               sizeof(struct vxlan_hdr));
+
+       parse_ethernet(eth_hdr, info);
+       info->l2_len += ETHER_VXLAN_HLEN; /* add udp + vxlan */
+}
+
+/* Parse a gre header */
+static void
+parse_gre(struct simple_gre_hdr *gre_hdr, struct testpmd_offload_info *info)
+{
+       struct ether_hdr *eth_hdr;
+       struct ipv4_hdr *ipv4_hdr;
+       struct ipv6_hdr *ipv6_hdr;
+
+       /* if flags != 0; it's not supported */
+       if (gre_hdr->flags != 0)
+               return;
+
+       if (gre_hdr->proto == _htons(ETHER_TYPE_IPv4)) {
+               info->is_tunnel = 1;
+               info->outer_ethertype = info->ethertype;
+               info->outer_l2_len = info->l2_len;
+               info->outer_l3_len = info->l3_len;
+               info->outer_l4_proto = info->l4_proto;
+
+               ipv4_hdr = (struct ipv4_hdr *)((char *)gre_hdr +
+                       sizeof(struct simple_gre_hdr));
+
+               parse_ipv4(ipv4_hdr, info);
+               info->ethertype = _htons(ETHER_TYPE_IPv4);
+               info->l2_len = 0;
+
+       } else if (gre_hdr->proto == _htons(ETHER_TYPE_IPv6)) {
+               info->is_tunnel = 1;
+               info->outer_ethertype = info->ethertype;
+               info->outer_l2_len = info->l2_len;
+               info->outer_l3_len = info->l3_len;
+               info->outer_l4_proto = info->l4_proto;
+
+               ipv6_hdr = (struct ipv6_hdr *)((char *)gre_hdr +
+                       sizeof(struct simple_gre_hdr));
+
+               info->ethertype = _htons(ETHER_TYPE_IPv6);
+               parse_ipv6(ipv6_hdr, info);
+               info->l2_len = 0;
+
+       } else if (gre_hdr->proto == _htons(0x6558)) { /* ETH_P_TEB in linux */
+               info->is_tunnel = 1;
+               info->outer_ethertype = info->ethertype;
+               info->outer_l2_len = info->l2_len;
+               info->outer_l3_len = info->l3_len;
+               info->outer_l4_proto = info->l4_proto;
+
+               eth_hdr = (struct ether_hdr *)((char *)gre_hdr +
+                       sizeof(struct simple_gre_hdr));
+
+               parse_ethernet(eth_hdr, info);
+       } else
+               return;
+
+       info->l2_len += sizeof(struct simple_gre_hdr);
+}
+
+
+/* Parse an encapsulated ip or ipv6 header */
+static void
+parse_encap_ip(void *encap_ip, struct testpmd_offload_info *info)
+{
+       struct ipv4_hdr *ipv4_hdr = encap_ip;
+       struct ipv6_hdr *ipv6_hdr = encap_ip;
+       uint8_t ip_version;
+
+       ip_version = (ipv4_hdr->version_ihl & 0xf0) >> 4;
+
+       if (ip_version != 4 && ip_version != 6)
+               return;
+
+       info->is_tunnel = 1;
+       info->outer_ethertype = info->ethertype;
+       info->outer_l2_len = info->l2_len;
+       info->outer_l3_len = info->l3_len;
+
+       if (ip_version == 4) {
+               parse_ipv4(ipv4_hdr, info);
+               info->ethertype = _htons(ETHER_TYPE_IPv4);
+       } else {
+               parse_ipv6(ipv6_hdr, info);
+               info->ethertype = _htons(ETHER_TYPE_IPv6);
+       }
+       info->l2_len = 0;
+}
+
 /* modify the IPv4 or IPv4 source address of a packet */
 static void
 change_ip_addresses(void *l3_hdr, uint16_t ethertype)
@@ -290,7 +411,7 @@ process_inner_cksums(void *l3_hdr, const struct testpmd_offload_info *info,
  * packet */
 static uint64_t
 process_outer_cksums(void *outer_l3_hdr, struct testpmd_offload_info *info,
-uint16_t testpmd_ol_flags)
+       uint16_t testpmd_ol_flags)
 {
        struct ipv4_hdr *ipv4_hdr = outer_l3_hdr;
        struct ipv6_hdr *ipv6_hdr = outer_l3_hdr;
@@ -308,6 +429,9 @@ uint16_t testpmd_ol_flags)
        } else if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM)
                ol_flags |= PKT_TX_OUTER_IPV6;
 
+       if (info->outer_l4_proto != IPPROTO_UDP)
+               return ol_flags;
+
        /* outer UDP checksum is always done in software as we have no
         * hardware supporting it today, and no API for it. */
 
@@ -341,6 +465,9 @@ uint16_t testpmd_ol_flags)
  *   Ether / (vlan) / IP|IP6 / UDP|TCP|SCTP .
  *   Ether / (vlan) / outer IP|IP6 / outer UDP / VxLAN / Ether / IP|IP6 /
  *           UDP|TCP|SCTP
+ *   Ether / (vlan) / outer IP|IP6 / GRE / Ether / IP|IP6 / UDP|TCP|SCTP
+ *   Ether / (vlan) / outer IP|IP6 / GRE / IP|IP6 / UDP|TCP|SCTP
+ *   Ether / (vlan) / outer IP|IP6 / IP|IP6 / UDP|TCP|SCTP
  *
  * The testpmd command line for this forward engine sets the flags
  * TESTPMD_TX_OFFLOAD_* in ports[tx_port].tx_ol_flags. They control
@@ -356,7 +483,6 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
        struct rte_mbuf *m;
        struct ether_hdr *eth_hdr;
        void *l3_hdr = NULL, *outer_l3_hdr = NULL; /* can be IPv4 or IPv6 */
-       struct udp_hdr *udp_hdr;
        uint16_t nb_rx;
        uint16_t nb_tx;
        uint16_t i;
@@ -411,38 +537,31 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
                parse_ethernet(eth_hdr, &info);
                l3_hdr = (char *)eth_hdr + info.l2_len;
 
-               /* check if it's a supported tunnel (only vxlan for now) */
-               if ((testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) &&
-                       info.l4_proto == IPPROTO_UDP) {
-                       udp_hdr = (struct udp_hdr *)((char *)l3_hdr + info.l3_len);
-
-                       /* check udp destination port, 4789 is the default
-                        * vxlan port (rfc7348) */
-                       if (udp_hdr->dst_port == _htons(4789)) {
-                               info.l4_tun_len = ETHER_VXLAN_HLEN;
-                               info.is_tunnel = 1;
-
-                       /* currently, this flag is set by i40e only if the
-                        * packet is vxlan */
-                       } else if (m->ol_flags & (PKT_RX_TUNNEL_IPV4_HDR |
-                                       PKT_RX_TUNNEL_IPV6_HDR))
-                               info.is_tunnel = 1;
-
-                       if (info.is_tunnel == 1) {
-                               info.outer_ethertype = info.ethertype;
-                               info.outer_l2_len = info.l2_len;
-                               info.outer_l3_len = info.l3_len;
-                               outer_l3_hdr = l3_hdr;
-
-                               eth_hdr = (struct ether_hdr *)((char *)udp_hdr +
-                                       sizeof(struct udp_hdr) +
-                                       sizeof(struct vxlan_hdr));
-
-                               parse_ethernet(eth_hdr, &info);
-                               l3_hdr = (char *)eth_hdr + info.l2_len;
+               /* check if it's a supported tunnel */
+               if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_PARSE_TUNNEL) {
+                       if (info.l4_proto == IPPROTO_UDP) {
+                               struct udp_hdr *udp_hdr;
+                               udp_hdr = (struct udp_hdr *)((char *)l3_hdr +
+                                       info.l3_len);
+                               parse_vxlan(udp_hdr, &info, m->ol_flags);
+                       } else if (info.l4_proto == IPPROTO_GRE) {
+                               struct simple_gre_hdr *gre_hdr;
+                               gre_hdr = (struct simple_gre_hdr *)
+                                       ((char *)l3_hdr + info.l3_len);
+                               parse_gre(gre_hdr, &info);
+                       } else if (info.l4_proto == IPPROTO_IPIP) {
+                               void *encap_ip_hdr;
+                               encap_ip_hdr = (char *)l3_hdr + info.l3_len;
+                               parse_encap_ip(encap_ip_hdr, &info);
                        }
                }
 
+               /* update l3_hdr and outer_l3_hdr if a tunnel was parsed */
+               if (info.is_tunnel) {
+                       outer_l3_hdr = l3_hdr;
+                       l3_hdr = (char *)l3_hdr + info.outer_l3_len + info.l2_len;
+               }
+
                /* step 2: change all source IPs (v4 or v6) so we need
                 * to recompute the chksums even if they were correct */
 
@@ -472,8 +591,9 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
                        if (testpmd_ol_flags & TESTPMD_TX_OFFLOAD_OUTER_IP_CKSUM) {
                                m->outer_l2_len = info.outer_l2_len;
                                m->outer_l3_len = info.outer_l3_len;
-                               m->l2_len = info.l4_tun_len + info.l2_len;
+                               m->l2_len = info.l2_len;
                                m->l3_len = info.l3_len;
+                               m->l4_len = info.l4_len;
                        }
                        else {
                                /* if there is a outer UDP cksum
@@ -482,9 +602,7 @@ pkt_burst_checksum_forward(struct fwd_stream *fs)
                                   the payload will be modified by the
                                   hardware */
                                m->l2_len = info.outer_l2_len +
-                                       info.outer_l3_len +
-                                       sizeof(struct udp_hdr) +
-                                       sizeof(struct vxlan_hdr) + info.l2_len;
+                                       info.outer_l3_len + info.l2_len;
                                m->l3_len = info.l3_len;
                                m->l4_len = info.l4_len;
                        }