gso: add vxlan tunnel support 41/23041/26
authorMohsin Kazmi <sykazmi@cisco.com>
Fri, 17 Apr 2020 16:50:56 +0000 (16:50 +0000)
committerAndrew Yourtchenko <ayourtch@gmail.com>
Wed, 22 Apr 2020 15:03:34 +0000 (15:03 +0000)
Type: feature

Change-Id: I85f6ec77187a4983c66c5e22fd39fbb2cef82902
Signed-off-by: Mohsin Kazmi <sykazmi@cisco.com>
MAINTAINERS
src/vnet/CMakeLists.txt
src/vnet/devices/virtio/device.c
src/vnet/devices/virtio/vhost_user_output.c
src/vnet/gso/FEATURE.yaml
src/vnet/gso/gso.c
src/vnet/gso/gso.h
src/vnet/gso/hdr_offset_parser.h [new file with mode: 0644]
src/vnet/gso/node.c
src/vnet/interface_output.h
test/test_gso.py

index cf36e7a..83a2f61 100644 (file)
@@ -237,7 +237,7 @@ VNET GSO
 I:     gso
 M:     Andrew Yourtchenko <ayourtch@gmail.com>
 M:     Mohsin Kazmi <sykazmi@cisco.com>
-F:     src/vnet/interface_output.c
+F:     src/vnet/gso/
 
 Plugin - MAP
 I:     map
index 69e041b..bf1dba7 100644 (file)
@@ -1004,6 +1004,7 @@ list(APPEND VNET_SOURCES
 )
 
 list(APPEND VNET_HEADERS
+  gso/hdr_offset_parser.h
   gso/gso.h
 )
 
index 367372f..d110946 100644 (file)
@@ -23,7 +23,7 @@
 #include <vlib/unix/unix.h>
 #include <vnet/vnet.h>
 #include <vnet/ethernet/ethernet.h>
-#include <vnet/gso/gso.h>
+#include <vnet/gso/hdr_offset_parser.h>
 #include <vnet/ip/ip4_packet.h>
 #include <vnet/ip/ip6_packet.h>
 #include <vnet/tcp/tcp_packet.h>
@@ -173,7 +173,8 @@ set_checksum_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
   if (b->flags & VNET_BUFFER_F_IS_IP4)
     {
       ip4_header_t *ip4;
-      gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
+      generic_header_offset_t gho = { 0 };
+      vnet_generic_header_offset_parser (b, &gho);
       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
       hdr->csum_start = gho.l4_hdr_offset;     // 0x22;
       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
@@ -196,7 +197,8 @@ set_checksum_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
     }
   else if (b->flags & VNET_BUFFER_F_IS_IP6)
     {
-      gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
+      generic_header_offset_t gho = { 0 };
+      vnet_generic_header_offset_parser (b, &gho);
       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
       hdr->csum_start = gho.l4_hdr_offset;     // 0x36;
       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
@@ -216,10 +218,11 @@ set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
   if (b->flags & VNET_BUFFER_F_IS_IP4)
     {
       ip4_header_t *ip4;
-      gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 0);
+      generic_header_offset_t gho = { 0 };
+      vnet_generic_header_offset_parser (b, &gho);
       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
       hdr->gso_size = vnet_buffer2 (b)->gso_size;
-      hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
+      hdr->hdr_len = gho.hdr_sz;
       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
       hdr->csum_start = gho.l4_hdr_offset;     // 0x22;
       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
@@ -234,10 +237,11 @@ set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
     }
   else if (b->flags & VNET_BUFFER_F_IS_IP6)
     {
-      gso_header_offset_t gho = vnet_gso_header_offset_parser (b, 1);
+      generic_header_offset_t gho = { 0 };
+      vnet_generic_header_offset_parser (b, &gho);
       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
       hdr->gso_size = vnet_buffer2 (b)->gso_size;
-      hdr->hdr_len = gho.l4_hdr_offset + gho.l4_hdr_sz;
+      hdr->hdr_len = gho.hdr_sz;
       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
       hdr->csum_start = gho.l4_hdr_offset;     // 0x36;
       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
index e1f42ce..b6abe36 100644 (file)
@@ -44,7 +44,7 @@
 #include <vnet/devices/virtio/vhost_user.h>
 #include <vnet/devices/virtio/vhost_user_inline.h>
 
-#include <vnet/gso/gso.h>
+#include <vnet/gso/hdr_offset_parser.h>
 /*
  * On the transmit side, we keep processing the buffers from vlib in the while
  * loop and prepare the copy order to be executed later. However, the static
@@ -236,8 +236,8 @@ static_always_inline void
 vhost_user_handle_tx_offload (vhost_user_intf_t * vui, vlib_buffer_t * b,
                              virtio_net_hdr_t * hdr)
 {
-  gso_header_offset_t gho =
-    vnet_gso_header_offset_parser (b, b->flags & VNET_BUFFER_F_IS_IP6);
+  generic_header_offset_t gho = { 0 };
+  vnet_generic_header_offset_parser (b, &gho);
   if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
     {
       ip4_header_t *ip4;
index 4edca9c..b3296d6 100644 (file)
@@ -4,9 +4,10 @@ maintainer: ayourtch@gmail.com sykazmi@cisco.com
 features:
   - Basic GSO support
   - GSO for VLAN tagged packets
+  - GSO for VXLAN tunnel
   - Provide inline function to get header offsets
 description: "Generic Segmentation Offload"
 missing:
-  - Tunnels i.e. VXLAN
+  - Thorough Testing, IPIP, GRE, Geneve, IPSec
 state: experimental
 properties: [API, CLI]
index cf90d22..c741b17 100644 (file)
@@ -16,6 +16,7 @@
 #include <vlib/vlib.h>
 #include <vnet/vnet.h>
 #include <vppinfra/error.h>
+#include <vnet/ethernet/ethernet.h>
 #include <vnet/feature/feature.h>
 #include <vnet/l2/l2_in_out_feat_arc.h>
 #include <vnet/gso/gso.h>
index 0e46c36..8e174df 100644 (file)
 #ifndef included_gso_h
 #define included_gso_h
 
-#include <vnet/ethernet/ethernet.h>
-#include <vnet/ip/ip4_packet.h>
-#include <vnet/ip/ip6_packet.h>
-#include <vnet/udp/udp_packet.h>
 #include <vnet/vnet.h>
 
-typedef struct
-{
-  i16 l2_hdr_offset;
-  i16 l3_hdr_offset;
-  i16 l4_hdr_offset;
-  u16 l4_hdr_sz;
-  i16 outer_l2_hdr_offset;
-  i16 outer_l3_hdr_offset;
-  i16 outer_l4_hdr_offset;
-} gso_header_offset_t;
-
 typedef struct
 {
   vlib_main_t *vlib_main;
@@ -44,84 +29,6 @@ extern gso_main_t gso_main;
 
 int vnet_sw_interface_gso_enable_disable (u32 sw_if_index, u8 enable);
 
-static_always_inline gso_header_offset_t
-vnet_gso_header_offset_parser (vlib_buffer_t * b0, int is_ip6)
-{
-  gso_header_offset_t gho = { 0 };
-  u8 l4_proto = 0;
-  u8 l4_hdr_sz = 0;
-
-  if (PREDICT_TRUE ((b0->flags & (VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
-                                 VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
-                                 VNET_BUFFER_F_L4_HDR_OFFSET_VALID)) ==
-                   (VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
-                    VNET_BUFFER_F_L3_HDR_OFFSET_VALID |
-                    VNET_BUFFER_F_L4_HDR_OFFSET_VALID)))
-    {
-      gho.l2_hdr_offset = vnet_buffer (b0)->l2_hdr_offset;
-      gho.l3_hdr_offset = vnet_buffer (b0)->l3_hdr_offset;
-      gho.l4_hdr_offset = vnet_buffer (b0)->l4_hdr_offset;
-      gho.l4_hdr_sz = vnet_buffer2 (b0)->gso_l4_hdr_sz;
-      return gho;
-    }
-
-  ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
-  u16 ethertype = clib_net_to_host_u16 (eh->type);
-  u16 l2hdr_sz = sizeof (ethernet_header_t);
-
-  if (ethernet_frame_is_tagged (ethertype))
-    {
-      ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
-
-      ethertype = clib_net_to_host_u16 (vlan->type);
-      l2hdr_sz += sizeof (*vlan);
-      if (ethertype == ETHERNET_TYPE_VLAN)
-       {
-         vlan++;
-         ethertype = clib_net_to_host_u16 (vlan->type);
-         l2hdr_sz += sizeof (*vlan);
-       }
-    }
-
-  gho.l2_hdr_offset = b0->current_data;
-  gho.l3_hdr_offset = l2hdr_sz;
-
-  if (PREDICT_TRUE (is_ip6 == 0))
-    {
-      ip4_header_t *ip4 =
-       (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
-      gho.l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
-      l4_proto = ip4->protocol;
-    }
-  else if (PREDICT_TRUE (is_ip6))
-    {
-      ip6_header_t *ip6 =
-       (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
-      /* FIXME IPv6 EH traversal */
-      gho.l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
-      l4_proto = ip6->protocol;
-    }
-  if (l4_proto == IP_PROTOCOL_TCP)
-    {
-      tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
-                                           gho.l4_hdr_offset);
-      l4_hdr_sz = tcp_header_bytes (tcp);
-    }
-  else if (l4_proto == IP_PROTOCOL_UDP)
-    {
-      udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
-                                           gho.l4_hdr_offset);
-      l4_hdr_sz = sizeof (*udp);
-    }
-
-  if (b0->flags & (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_IS_IP6))
-    {
-      gho.l4_hdr_sz = l4_hdr_sz;
-    }
-
-  return gho;
-}
-
 #endif /* included_gso_h */
 
 /*
diff --git a/src/vnet/gso/hdr_offset_parser.h b/src/vnet/gso/hdr_offset_parser.h
new file mode 100644 (file)
index 0000000..8f9e0cd
--- /dev/null
@@ -0,0 +1,408 @@
+/*
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef included_hdr_offset_parser_h
+#define included_hdr_offset_parser_h
+
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/ip/ip4_packet.h>
+#include <vnet/ip/ip6_packet.h>
+#include <vnet/udp/udp.h>
+#include <vnet/udp/udp_packet.h>
+#include <vnet/vnet.h>
+#include <vnet/vxlan/vxlan_packet.h>
+
+#define foreach_gho_flag        \
+  _( 0, IP4)                    \
+  _( 1, IP6)                    \
+  _( 2, TCP)                    \
+  _( 3, UDP)                    \
+  _( 4, OUTER_IP4)              \
+  _( 5, OUTER_IP6)              \
+  _( 6, OUTER_TCP)              \
+  _( 7, OUTER_UDP)              \
+  _( 8, VXLAN_TUNNEL)           \
+  _( 9, GRE_TUNNEL)             \
+  _( 10, IPIP_TUNNEL)           \
+  _( 11, GENEVE_TUNNEL)
+
+typedef enum gho_flag_t_
+{
+#define _(bit, name) GHO_F_##name  = (1 << bit),
+  foreach_gho_flag
+#undef _
+} gho_flag_t;
+
+#define GHO_F_TUNNEL (GHO_F_VXLAN_TUNNEL |  \
+                      GHO_F_GENEVE_TUNNEL | \
+                      GHO_F_IPIP_TUNNEL |   \
+                      GHO_F_GRE_TUNNEL)
+
+#define GHO_F_OUTER_HDR (GHO_F_OUTER_IP4 | \
+                         GHO_F_OUTER_IP6 | \
+                         GHO_F_OUTER_TCP | \
+                         GHO_F_OUTER_UDP)
+
+#define GHO_F_INNER_HDR (GHO_F_IP4 | \
+                         GHO_F_IP6 | \
+                         GHO_F_UDP | \
+                         GHO_F_TCP)
+
+typedef struct
+{
+  i16 outer_l2_hdr_offset;
+  i16 outer_l3_hdr_offset;
+  i16 outer_l4_hdr_offset;
+  u16 outer_l4_hdr_sz;
+  u16 outer_hdr_sz;
+  i16 l2_hdr_offset;
+  i16 l3_hdr_offset;
+  i16 l4_hdr_offset;
+  u16 l4_hdr_sz;
+  u16 hdr_sz;
+  gho_flag_t gho_flags;
+} generic_header_offset_t;
+
+static_always_inline u8 *
+format_generic_header_offset (u8 * s, va_list * args)
+{
+  generic_header_offset_t *gho = va_arg (*args, generic_header_offset_t *);
+
+  s = format (s, "\n\t");
+  if (gho->gho_flags & GHO_F_TUNNEL)
+    {
+      if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
+       s = format (s, "vxlan-tunnel ");
+      else if (gho->gho_flags & GHO_F_IPIP_TUNNEL)
+       s = format (s, "ipip-tunnel ");
+      else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
+       s = format (s, "gre-tunnel ");
+      else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
+       s = format (s, "geneve-tunnel ");
+
+      if (gho->gho_flags & GHO_F_OUTER_IP4)
+       s = format (s, "outer-ipv4 ");
+      else if (gho->gho_flags & GHO_F_OUTER_IP6)
+       s = format (s, "outer-ipv6 ");
+
+      if (gho->gho_flags & GHO_F_OUTER_UDP)
+       s = format (s, "outer-udp ");
+      else if (gho->gho_flags & GHO_F_OUTER_TCP)
+       s = format (s, "outer-tcp ");
+
+      s = format (s, "outer-hdr-sz %u outer-l2-hdr-offset %d "
+                 "outer-l3-hdr-offset %d outer-l4-hdr-offset %d "
+                 "outer-l4-hdr-sz %u\n\t",
+                 gho->outer_hdr_sz, gho->outer_l2_hdr_offset,
+                 gho->outer_l3_hdr_offset, gho->outer_l4_hdr_offset,
+                 gho->outer_l4_hdr_sz);
+    }
+
+  if (gho->gho_flags & GHO_F_IP4)
+    s = format (s, "ipv4 ");
+  else if (gho->gho_flags & GHO_F_IP6)
+    s = format (s, "ipv6 ");
+
+  if (gho->gho_flags & GHO_F_TCP)
+    s = format (s, "tcp ");
+  else if (gho->gho_flags & GHO_F_UDP)
+    s = format (s, "udp ");
+
+  s = format (s, "hdr-sz %u l2-hdr-offset %d "
+             "l3-hdr-offset %d l4-hdr-offset %d "
+             "l4-hdr-sz %u",
+             gho->hdr_sz, gho->l2_hdr_offset, gho->l3_hdr_offset,
+             gho->l4_hdr_offset, gho->l4_hdr_sz);
+
+  return s;
+}
+
+static_always_inline void
+vnet_get_inner_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
+{
+  if ((gho->gho_flags & GHO_F_TUNNEL)
+      && (gho->gho_flags & GHO_F_OUTER_HDR)
+      && (b0->current_data == gho->outer_l2_hdr_offset))
+    vlib_buffer_advance (b0, gho->outer_hdr_sz);
+}
+
+static_always_inline void
+vnet_get_outer_header (vlib_buffer_t * b0, generic_header_offset_t * gho)
+{
+  if ((gho->gho_flags & GHO_F_TUNNEL)
+      && (gho->gho_flags & GHO_F_OUTER_HDR)
+      && (b0->current_data == gho->l2_hdr_offset))
+    vlib_buffer_advance (b0, -gho->outer_hdr_sz);
+}
+
+static_always_inline void
+vnet_geneve_inner_header_parser_inline (vlib_buffer_t * b0,
+                                       generic_header_offset_t * gho)
+{
+  /* not supported yet */
+  if ((gho->gho_flags & GHO_F_GENEVE_TUNNEL) == 0)
+    return;
+
+  ASSERT (0);
+}
+
+static_always_inline void
+vnet_gre_inner_header_parser_inline (vlib_buffer_t * b0,
+                                    generic_header_offset_t * gho)
+{
+  /* not supported yet */
+  if ((gho->gho_flags & GHO_F_GRE_TUNNEL) == 0)
+    return;
+
+  ASSERT (0);
+}
+
+static_always_inline void
+vnet_ipip_inner_header_parser_inline (vlib_buffer_t * b0,
+                                     generic_header_offset_t * gho)
+{
+  /* not supported yet */
+  if ((gho->gho_flags & GHO_F_IPIP_TUNNEL) == 0)
+    return;
+
+  ASSERT (0);
+}
+
+static_always_inline void
+vnet_vxlan_inner_header_parser_inline (vlib_buffer_t * b0,
+                                      generic_header_offset_t * gho)
+{
+  u8 l4_proto = 0;
+  u8 l4_hdr_sz = 0;
+
+  if ((gho->gho_flags & GHO_F_VXLAN_TUNNEL) == 0)
+    return;
+
+  gho->outer_l2_hdr_offset = gho->l2_hdr_offset;
+  gho->outer_l3_hdr_offset = gho->l3_hdr_offset;
+  gho->outer_l4_hdr_offset = gho->l4_hdr_offset;
+  gho->outer_l4_hdr_sz = gho->l4_hdr_sz;
+  gho->outer_hdr_sz = gho->hdr_sz;
+
+  gho->l2_hdr_offset = 0;
+  gho->l3_hdr_offset = 0;
+  gho->l4_hdr_offset = 0;
+  gho->l4_hdr_sz = 0;
+  gho->hdr_sz = 0;
+
+  if (gho->gho_flags & GHO_F_IP4)
+    {
+      gho->gho_flags |= GHO_F_OUTER_IP4;
+    }
+  else if (gho->gho_flags & GHO_F_IP6)
+    {
+      gho->gho_flags |= GHO_F_OUTER_IP6;
+    }
+
+  if (gho->gho_flags & GHO_F_UDP)
+    {
+      gho->gho_flags |= GHO_F_OUTER_UDP;
+    }
+
+  gho->gho_flags &= ~GHO_F_INNER_HDR;
+
+  vnet_get_inner_header (b0, gho);
+
+  gho->l2_hdr_offset = b0->current_data;
+
+  ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
+  u16 ethertype = clib_net_to_host_u16 (eh->type);
+  u16 l2hdr_sz = sizeof (ethernet_header_t);
+
+  if (ethernet_frame_is_tagged (ethertype))
+    {
+      ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
+
+      ethertype = clib_net_to_host_u16 (vlan->type);
+      l2hdr_sz += sizeof (*vlan);
+      if (ethertype == ETHERNET_TYPE_VLAN)
+       {
+         vlan++;
+         ethertype = clib_net_to_host_u16 (vlan->type);
+         l2hdr_sz += sizeof (*vlan);
+       }
+    }
+
+  gho->l3_hdr_offset = l2hdr_sz;
+
+  if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
+    {
+      ip4_header_t *ip4 =
+       (ip4_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
+      gho->l4_hdr_offset = gho->l3_hdr_offset + ip4_header_bytes (ip4);
+      l4_proto = ip4->protocol;
+      gho->gho_flags |= GHO_F_IP4;
+    }
+  else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
+    {
+      ip6_header_t *ip6 =
+       (ip6_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
+      /* FIXME IPv6 EH traversal */
+      gho->l4_hdr_offset = gho->l3_hdr_offset + sizeof (ip6_header_t);
+      l4_proto = ip6->protocol;
+      gho->gho_flags |= GHO_F_IP6;
+    }
+  if (l4_proto == IP_PROTOCOL_TCP)
+    {
+      tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
+                                           gho->l4_hdr_offset);
+      l4_hdr_sz = tcp_header_bytes (tcp);
+
+      gho->gho_flags |= GHO_F_TCP;
+
+    }
+  else if (l4_proto == IP_PROTOCOL_UDP)
+    {
+      udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
+                                           gho->l4_hdr_offset);
+      l4_hdr_sz = sizeof (*udp);
+
+      gho->gho_flags |= GHO_F_UDP;
+    }
+
+  gho->l4_hdr_sz = l4_hdr_sz;
+  gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
+
+  vnet_get_outer_header (b0, gho);
+}
+
+static_always_inline void
+vnet_generic_inner_header_parser_inline (vlib_buffer_t * b0,
+                                        generic_header_offset_t * gho)
+{
+
+  if (gho->gho_flags & GHO_F_VXLAN_TUNNEL)
+    vnet_vxlan_inner_header_parser_inline (b0, gho);
+  else if (gho->gho_flags & GHO_F_IPIP_TUNNEL)
+    vnet_ipip_inner_header_parser_inline (b0, gho);
+  else if (gho->gho_flags & GHO_F_GRE_TUNNEL)
+    vnet_gre_inner_header_parser_inline (b0, gho);
+  else if (gho->gho_flags & GHO_F_GENEVE_TUNNEL)
+    vnet_geneve_inner_header_parser_inline (b0, gho);
+}
+
+static_always_inline void
+vnet_generic_outer_header_parser_inline (vlib_buffer_t * b0,
+                                        generic_header_offset_t * gho)
+{
+  u8 l4_proto = 0;
+  u8 l4_hdr_sz = 0;
+
+  ethernet_header_t *eh = (ethernet_header_t *) vlib_buffer_get_current (b0);
+  u16 ethertype = clib_net_to_host_u16 (eh->type);
+  u16 l2hdr_sz = sizeof (ethernet_header_t);
+
+  if (ethernet_frame_is_tagged (ethertype))
+    {
+      ethernet_vlan_header_t *vlan = (ethernet_vlan_header_t *) (eh + 1);
+
+      ethertype = clib_net_to_host_u16 (vlan->type);
+      l2hdr_sz += sizeof (*vlan);
+      if (ethertype == ETHERNET_TYPE_VLAN)
+       {
+         vlan++;
+         ethertype = clib_net_to_host_u16 (vlan->type);
+         l2hdr_sz += sizeof (*vlan);
+       }
+    }
+
+  gho->l2_hdr_offset = b0->current_data;
+  gho->l3_hdr_offset = l2hdr_sz;
+
+  if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4))
+    {
+      ip4_header_t *ip4 =
+       (ip4_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
+      gho->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4);
+      l4_proto = ip4->protocol;
+      gho->gho_flags |= GHO_F_IP4;
+    }
+  else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6))
+    {
+      ip6_header_t *ip6 =
+       (ip6_header_t *) (vlib_buffer_get_current (b0) + l2hdr_sz);
+      /* FIXME IPv6 EH traversal */
+      gho->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t);
+      l4_proto = ip6->protocol;
+      gho->gho_flags |= GHO_F_IP6;
+    }
+  if (l4_proto == IP_PROTOCOL_TCP)
+    {
+      tcp_header_t *tcp = (tcp_header_t *) (vlib_buffer_get_current (b0) +
+                                           gho->l4_hdr_offset);
+      l4_hdr_sz = tcp_header_bytes (tcp);
+
+      gho->gho_flags |= GHO_F_TCP;
+    }
+  else if (l4_proto == IP_PROTOCOL_UDP)
+    {
+      udp_header_t *udp = (udp_header_t *) (vlib_buffer_get_current (b0) +
+                                           gho->l4_hdr_offset);
+      l4_hdr_sz = sizeof (*udp);
+
+      gho->gho_flags |= GHO_F_UDP;
+
+      if (UDP_DST_PORT_vxlan == clib_net_to_host_u16 (udp->dst_port))
+       {
+         gho->gho_flags |= GHO_F_VXLAN_TUNNEL;
+         gho->hdr_sz += sizeof (vxlan_header_t);
+       }
+      else if (UDP_DST_PORT_geneve == clib_net_to_host_u16 (udp->dst_port))
+       {
+         gho->gho_flags |= GHO_F_GENEVE_TUNNEL;
+       }
+    }
+  else if ((l4_proto == IP_PROTOCOL_IP_IN_IP)
+          || (l4_proto == IP_PROTOCOL_IPV6))
+    {
+      l4_hdr_sz = 0;
+      gho->gho_flags |= GHO_F_IPIP_TUNNEL;
+    }
+  else if (l4_proto == IP_PROTOCOL_GRE)
+    {
+      l4_hdr_sz = 0;
+      gho->gho_flags |= GHO_F_GRE_TUNNEL;
+    }
+
+  gho->l4_hdr_sz = l4_hdr_sz;
+  gho->hdr_sz += gho->l4_hdr_offset + l4_hdr_sz;
+}
+
+static_always_inline void
+vnet_generic_header_offset_parser (vlib_buffer_t * b0,
+                                  generic_header_offset_t * gho)
+{
+  vnet_generic_outer_header_parser_inline (b0, gho);
+
+  if (gho->gho_flags & GHO_F_TUNNEL)
+    {
+      vnet_generic_inner_header_parser_inline (b0, gho);
+    }
+}
+
+#endif /* included_hdr_offset_parser_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index b3125fe..663f9cc 100644 (file)
@@ -19,6 +19,7 @@
 #include <vnet/ethernet/ethernet.h>
 #include <vnet/feature/feature.h>
 #include <vnet/gso/gso.h>
+#include <vnet/gso/hdr_offset_parser.h>
 #include <vnet/ip/icmp46_packet.h>
 #include <vnet/ip/ip4.h>
 #include <vnet/ip/ip6.h>
@@ -29,6 +30,7 @@ typedef struct
   u32 flags;
   u16 gso_size;
   u8 gso_l4_hdr_sz;
+  generic_header_offset_t gho;
 } gso_trace_t;
 
 static u8 *
@@ -40,32 +42,126 @@ format_gso_trace (u8 * s, va_list * args)
 
   if (t->flags & VNET_BUFFER_F_GSO)
     {
-      s = format (s, "gso_sz %d gso_l4_hdr_sz %d",
-                 t->gso_size, t->gso_l4_hdr_sz);
+      s = format (s, "gso_sz %d gso_l4_hdr_sz %d %U",
+                 t->gso_size, t->gso_l4_hdr_sz, format_generic_header_offset,
+                 &t->gho);
     }
   else
     {
-      s = format (s, "non-gso buffer");
+      s =
+       format (s, "non-gso buffer %U", format_generic_header_offset,
+               &t->gho);
     }
 
   return s;
 }
 
+static_always_inline void
+tso_segment_vxlan_tunnel_headers_fixup (vlib_main_t * vm, vlib_buffer_t * b,
+                                       generic_header_offset_t * gho)
+{
+  u8 proto = 0;
+  ip4_header_t *ip4 = 0;
+  ip6_header_t *ip6 = 0;
+  udp_header_t *udp = 0;
+
+  ip4 =
+    (ip4_header_t *) (vlib_buffer_get_current (b) + gho->outer_l3_hdr_offset);
+  ip6 =
+    (ip6_header_t *) (vlib_buffer_get_current (b) + gho->outer_l3_hdr_offset);
+  udp =
+    (udp_header_t *) (vlib_buffer_get_current (b) + gho->outer_l4_hdr_offset);
+
+  if (gho->gho_flags & GHO_F_OUTER_IP4)
+    {
+      proto = ip4->protocol;
+      ip4->length =
+       clib_host_to_net_u16 (b->current_length - gho->outer_l3_hdr_offset);
+      ip4->checksum = ip4_header_checksum (ip4);
+    }
+  else if (gho->gho_flags & GHO_F_OUTER_IP6)
+    {
+      proto = ip6->protocol;
+      ip6->payload_length =
+       clib_host_to_net_u16 (b->current_length - gho->outer_l4_hdr_offset);
+    }
+  if (proto == IP_PROTOCOL_UDP)
+    {
+      int bogus;
+      udp->length =
+       clib_host_to_net_u16 (b->current_length - gho->outer_l4_hdr_offset);
+      udp->checksum = 0;
+      if (gho->gho_flags & GHO_F_OUTER_IP6)
+       {
+         udp->checksum =
+           ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
+       }
+      else if (gho->gho_flags & GHO_F_OUTER_IP4)
+       {
+         udp->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
+       }
+      b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
+    }
+}
+
+static_always_inline u16
+tso_segment_vxlan_tunnel_fixup (vlib_main_t * vm,
+                               vnet_interface_per_thread_data_t * ptd,
+                               vlib_buffer_t * sb0,
+                               generic_header_offset_t * gho)
+{
+  u16 n_tx_bufs = vec_len (ptd->split_buffers);
+  u16 i = 0, n_tx_bytes = 0;
+
+  while (i < n_tx_bufs)
+    {
+      vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[i]);
+      vnet_get_outer_header (b0, gho);
+      clib_memcpy_fast (vlib_buffer_get_current (b0),
+                       vlib_buffer_get_current (sb0), gho->outer_hdr_sz);
+
+      tso_segment_vxlan_tunnel_headers_fixup (vm, b0, gho);
+      n_tx_bytes += gho->outer_hdr_sz;
+      i++;
+    }
+  return n_tx_bytes;
+}
+
 static_always_inline u16
 tso_alloc_tx_bufs (vlib_main_t * vm,
                   vnet_interface_per_thread_data_t * ptd,
                   vlib_buffer_t * b0, u32 n_bytes_b0, u16 l234_sz,
-                  u16 gso_size, gso_header_offset_t * gho)
+                  u16 gso_size, u16 first_data_size,
+                  generic_header_offset_t * gho)
 {
-  u16 size =
+  u16 n_alloc, size;
+  u16 first_packet_length = l234_sz + first_data_size;
+
+  /*
+   * size is the amount of data per segmented buffer except the 1st
+   * segmented buffer.
+   * l2_hdr_offset is an offset == current_data of vlib_buffer_t.
+   * l234_sz is hdr_sz from l2_hdr_offset.
+   */
+  size =
     clib_min (gso_size, vlib_buffer_get_default_data_size (vm) - l234_sz
              - gho->l2_hdr_offset);
 
-  /* rounded-up division */
-  u16 n_bufs = (n_bytes_b0 - l234_sz + (size - 1)) / size;
-  u16 n_alloc;
+  /*
+   * First segmented buffer length is calculated separately.
+   * As it may contain less data than gso_size (when gso_size is
+   * greater than current_length of 1st buffer from GSO chained
+   * buffers) and/or size calculated above.
+   */
+  u16 n_bufs = 1;
+
+  /*
+   * Total packet length minus first packet length including l234 header.
+   * rounded-up division
+   */
+  ASSERT (n_bytes_b0 > first_packet_length);
+  n_bufs += ((n_bytes_b0 - first_packet_length + (size - 1)) / size);
 
-  ASSERT (n_bufs > 0);
   vec_validate (ptd->split_buffers, n_bufs - 1);
 
   n_alloc = vlib_buffer_alloc (vm, ptd->split_buffers, n_bufs);
@@ -96,7 +192,7 @@ tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
                            vlib_buffer_t * b0, u16 template_data_sz,
                            u16 gso_size, u8 ** p_dst_ptr, u16 * p_dst_left,
                            u32 next_tcp_seq, u32 flags,
-                           gso_header_offset_t * gho)
+                           generic_header_offset_t * gho)
 {
   tso_init_buf_from_template_base (nb0, b0, flags, template_data_sz);
 
@@ -112,8 +208,9 @@ tso_init_buf_from_template (vlib_main_t * vm, vlib_buffer_t * nb0,
 }
 
 static_always_inline void
-tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6,
-                        gso_header_offset_t * gho)
+tso_fixup_segmented_buf (vlib_main_t * vm, vlib_buffer_t * b0, u8 tcp_flags,
+                        int is_ip6, generic_header_offset_t * gho,
+                        u32 do_csums)
 {
   ip4_header_t *ip4 =
     (ip4_header_t *) (vlib_buffer_get_current (b0) + gho->l3_hdr_offset);
@@ -125,13 +222,37 @@ tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6,
   tcp->flags = tcp_flags;
 
   if (is_ip6)
-    ip6->payload_length =
-      clib_host_to_net_u16 (b0->current_length -
-                           (gho->l4_hdr_offset - gho->l2_hdr_offset));
+    {
+      ip6->payload_length =
+       clib_host_to_net_u16 (b0->current_length -
+                             (gho->l4_hdr_offset - gho->l2_hdr_offset));
+      if (do_csums && (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM))
+       {
+         int bogus = 0;
+         tcp->checksum = 0;
+         tcp->checksum =
+           ip6_tcp_udp_icmp_compute_checksum (vm, b0, ip6, &bogus);
+         b0->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
+       }
+    }
   else
-    ip4->length =
-      clib_host_to_net_u16 (b0->current_length -
-                           (gho->l3_hdr_offset - gho->l2_hdr_offset));
+    {
+      ip4->length =
+       clib_host_to_net_u16 (b0->current_length -
+                             (gho->l3_hdr_offset - gho->l2_hdr_offset));
+      if (do_csums)
+       {
+         if (b0->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
+           ip4->checksum = ip4_header_checksum (ip4);
+         if (b0->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
+           {
+             tcp->checksum = 0;
+             tcp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip4);
+           }
+         b0->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
+         b0->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
+       }
+    }
 }
 
 /**
@@ -145,13 +266,13 @@ tso_fixup_segmented_buf (vlib_buffer_t * b0, u8 tcp_flags, int is_ip6,
 
 static_always_inline u32
 tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
-                   u32 sbi0, vlib_buffer_t * sb0, gso_header_offset_t * gho,
-                   u32 n_bytes_b0, int is_ip6)
+                   u32 sbi0, vlib_buffer_t * sb0,
+                   generic_header_offset_t * gho, u32 n_bytes_b0, int is_ip6,
+                   u32 do_csums)
 {
   u32 n_tx_bytes = 0;
   u16 gso_size = vnet_buffer2 (sb0)->gso_size;
 
-  int l4_hdr_sz = gho->l4_hdr_sz;
   u8 save_tcp_flags = 0;
   u8 tcp_flags_no_fin_psh = 0;
   u32 next_tcp_seq = 0;
@@ -166,12 +287,13 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
 
   u32 default_bflags =
     sb0->flags & ~(VNET_BUFFER_F_GSO | VLIB_BUFFER_NEXT_PRESENT);
-  u16 l234_sz = gho->l4_hdr_offset + l4_hdr_sz - gho->l2_hdr_offset;
+  u16 l234_sz = gho->hdr_sz;
   int first_data_size = clib_min (gso_size, sb0->current_length - l234_sz);
   next_tcp_seq += first_data_size;
 
   if (PREDICT_FALSE
-      (!tso_alloc_tx_bufs (vm, ptd, sb0, n_bytes_b0, l234_sz, gso_size, gho)))
+      (!tso_alloc_tx_bufs
+       (vm, ptd, sb0, n_bytes_b0, l234_sz, gso_size, first_data_size, gho)))
     return 0;
 
   vlib_buffer_t *b0 = vlib_get_buffer (vm, ptd->split_buffers[0]);
@@ -194,7 +316,8 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
       src_ptr = vlib_buffer_get_current (sb0) + l234_sz + first_data_size;
       src_left = sb0->current_length - l234_sz - first_data_size;
 
-      tso_fixup_segmented_buf (b0, tcp_flags_no_fin_psh, is_ip6, gho);
+      tso_fixup_segmented_buf (vm, b0, tcp_flags_no_fin_psh, is_ip6, gho,
+                              do_csums);
 
       /* grab a second buffer and prepare the loop */
       ASSERT (dbi < vec_len (ptd->split_buffers));
@@ -243,8 +366,8 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
          if (0 == dst_left && total_src_left)
            {
              n_tx_bytes += cdb0->current_length;
-             tso_fixup_segmented_buf (cdb0, tcp_flags_no_fin_psh, is_ip6,
-                                      gho);
+             tso_fixup_segmented_buf (vm, cdb0, tcp_flags_no_fin_psh, is_ip6,
+                                      gho, do_csums);
              ASSERT (dbi < vec_len (ptd->split_buffers));
              cdb0 = vlib_get_buffer (vm, ptd->split_buffers[dbi++]);
              tso_init_buf_from_template (vm, cdb0, b0, l234_sz,
@@ -253,7 +376,8 @@ tso_segment_buffer (vlib_main_t * vm, vnet_interface_per_thread_data_t * ptd,
            }
        }
 
-      tso_fixup_segmented_buf (cdb0, save_tcp_flags, is_ip6, gho);
+      tso_fixup_segmented_buf (vm, cdb0, save_tcp_flags, is_ip6, gho,
+                              do_csums);
 
       n_tx_bytes += cdb0->current_length;
     }
@@ -372,6 +496,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                t0->flags = b[0]->flags & VNET_BUFFER_F_GSO;
                t0->gso_size = vnet_buffer2 (b[0])->gso_size;
                t0->gso_l4_hdr_sz = vnet_buffer2 (b[0])->gso_l4_hdr_sz;
+               vnet_generic_header_offset_parser (b[0], &t0->gho);
              }
            if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
              {
@@ -379,6 +504,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                t1->flags = b[1]->flags & VNET_BUFFER_F_GSO;
                t1->gso_size = vnet_buffer2 (b[1])->gso_size;
                t1->gso_l4_hdr_sz = vnet_buffer2 (b[1])->gso_l4_hdr_sz;
+               vnet_generic_header_offset_parser (b[1], &t1->gho);
              }
            if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
              {
@@ -386,6 +512,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                t2->flags = b[2]->flags & VNET_BUFFER_F_GSO;
                t2->gso_size = vnet_buffer2 (b[2])->gso_size;
                t2->gso_l4_hdr_sz = vnet_buffer2 (b[2])->gso_l4_hdr_sz;
+               vnet_generic_header_offset_parser (b[2], &t2->gho);
              }
            if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
              {
@@ -393,6 +520,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                t3->flags = b[3]->flags & VNET_BUFFER_F_GSO;
                t3->gso_size = vnet_buffer2 (b[3])->gso_size;
                t3->gso_l4_hdr_sz = vnet_buffer2 (b[3])->gso_l4_hdr_sz;
+               vnet_generic_header_offset_parser (b[3], &t3->gho);
              }
 
            from += 4;
@@ -419,6 +547,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
          vnet_hw_interface_t *hi0;
          u32 next0 = 0;
          u32 do_segmentation0 = 0;
+         u32 do_csums = 0;
 
          swif0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
          if (PREDICT_FALSE (hi->sw_if_index != swif0))
@@ -444,6 +573,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
              t0->flags = b[0]->flags & VNET_BUFFER_F_GSO;
              t0->gso_size = vnet_buffer2 (b[0])->gso_size;
              t0->gso_l4_hdr_sz = vnet_buffer2 (b[0])->gso_l4_hdr_sz;
+             vnet_generic_header_offset_parser (b[0], &t0->gho);
            }
 
          if (do_segmentation0)
@@ -458,14 +588,39 @@ vnet_gso_node_inline (vlib_main_t * vm,
                  to_next -= 1;
                  n_left_to_next += 1;
                  /* undo the counting. */
-                 gso_header_offset_t gho;
+                 generic_header_offset_t gho = { 0 };
                  u32 n_bytes_b0 = vlib_buffer_length_in_chain (vm, b[0]);
                  u32 n_tx_bytes = 0;
 
-                 gho = vnet_gso_header_offset_parser (b[0], is_ip6);
+                 vnet_generic_header_offset_parser (b[0], &gho);
+
+                 if (PREDICT_FALSE (gho.gho_flags & GHO_F_TUNNEL))
+                   {
+                     if (PREDICT_FALSE
+                         ((gho.gho_flags & GHO_F_VXLAN_TUNNEL) == 0))
+                       {
+                         /* not supported yet */
+                         drop_one_buffer_and_count (vm, vnm, node, from - 1,
+                                                    hi->sw_if_index,
+                                                    VNET_INTERFACE_OUTPUT_ERROR_UNHANDLED_GSO_TYPE);
+                         b += 1;
+                         continue;
+                       }
+
+                     vnet_get_inner_header (b[0], &gho);
+
+                     n_bytes_b0 -= gho.outer_hdr_sz;
+                     /*
+                      * In case of tunnel encapsulated packet, we will
+                      * calculate the checksums for segmented inner packets.
+                      */
+                     do_csums = 1;
+                     is_ip6 = (gho.gho_flags & GHO_F_IP6) != 0;
+                   }
+
                  n_tx_bytes =
                    tso_segment_buffer (vm, ptd, bi0, b[0], &gho, n_bytes_b0,
-                                       is_ip6);
+                                       is_ip6, do_csums);
 
                  if (PREDICT_FALSE (n_tx_bytes == 0))
                    {
@@ -476,6 +631,13 @@ vnet_gso_node_inline (vlib_main_t * vm,
                      continue;
                    }
 
+                 if (PREDICT_FALSE (gho.gho_flags & GHO_F_VXLAN_TUNNEL))
+                   {
+                     vnet_get_outer_header (b[0], &gho);
+                     n_tx_bytes +=
+                       tso_segment_vxlan_tunnel_fixup (vm, ptd, b[0], &gho);
+                   }
+
                  u16 n_tx_bufs = vec_len (ptd->split_buffers);
                  u32 *from_seg = ptd->split_buffers;
 
@@ -489,6 +651,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                            {
                              sbi0 = to_next[0] = from_seg[0];
                              sb0 = vlib_get_buffer (vm, sbi0);
+                             ASSERT (sb0->current_length > 0);
                              to_next += 1;
                              from_seg += 1;
                              n_left_to_next -= 1;
@@ -509,6 +672,7 @@ vnet_gso_node_inline (vlib_main_t * vm,
                        {
                          sbi0 = to_next[0] = from_seg[0];
                          sb0 = vlib_get_buffer (vm, sbi0);
+                         ASSERT (sb0->current_length > 0);
                          to_next += 1;
                          from_seg += 1;
                          n_left_to_next -= 1;
index 198ca28..ef7aaa8 100644 (file)
 #define __INTERFACE_INLINES_H__
 
 #include <vnet/vnet.h>
-#include <vnet/gso/gso.h>
+#include <vnet/gso/hdr_offset_parser.h>
+
+static_always_inline void
+vnet_calc_ip4_checksums (vlib_main_t * vm, vlib_buffer_t * b,
+                        ip4_header_t * ip4, tcp_header_t * th,
+                        udp_header_t * uh)
+{
+  if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
+    ip4->checksum = ip4_header_checksum (ip4);
+  if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
+    {
+      th->checksum = 0;
+      th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
+    }
+  if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
+    {
+      uh->checksum = 0;
+      uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
+    }
+}
+
+static_always_inline void
+vnet_calc_ip6_checksums (vlib_main_t * vm, vlib_buffer_t * b,
+                        ip6_header_t * ip6, tcp_header_t * th,
+                        udp_header_t * uh)
+{
+  int bogus;
+  if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
+    {
+      th->checksum = 0;
+      th->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
+    }
+  if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
+    {
+      uh->checksum = 0;
+      uh->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
+    }
+}
 
 static_always_inline void
 vnet_calc_checksums_inline (vlib_main_t * vm, vlib_buffer_t * b,
@@ -52,59 +89,51 @@ vnet_calc_checksums_inline (vlib_main_t * vm, vlib_buffer_t * b,
   tcp_header_t *th;
   udp_header_t *uh;
 
-  ASSERT (!(is_ip4 && is_ip6));
-
   if (with_gso)
     {
-      gso_header_offset_t gho;
-      gho = vnet_gso_header_offset_parser (b, is_ip6);
+      generic_header_offset_t gho = { 0 };
+      vnet_generic_header_offset_parser (b, &gho);
+
+      ASSERT (gho.gho_flags ^ (GHO_F_IP4 | GHO_F_IP6));
+
+      vnet_get_inner_header (b, &gho);
+
       ip4 = (ip4_header_t *)
        (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
       ip6 = (ip6_header_t *)
        (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
       th = (tcp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
       uh = (udp_header_t *) (vlib_buffer_get_current (b) + gho.l4_hdr_offset);
+
+      if (gho.gho_flags & GHO_F_IP4)
+       {
+         vnet_calc_ip4_checksums (vm, b, ip4, th, uh);
+       }
+      else if (gho.gho_flags & GHO_F_IP6)
+       {
+         vnet_calc_ip6_checksums (vm, b, ip6, th, uh);
+       }
+
+      vnet_get_outer_header (b, &gho);
     }
   else
     {
+      ASSERT (!(is_ip4 && is_ip6));
+
       ip4 = (ip4_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
       ip6 = (ip6_header_t *) (b->data + vnet_buffer (b)->l3_hdr_offset);
       th = (tcp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
       uh = (udp_header_t *) (b->data + vnet_buffer (b)->l4_hdr_offset);
-    }
 
-  if (is_ip4)
-    {
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
-       ip4->checksum = ip4_header_checksum (ip4);
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
+      if (is_ip4)
        {
-         th->checksum = 0;
-         th->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
+         vnet_calc_ip4_checksums (vm, b, ip4, th, uh);
        }
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
+      if (is_ip6)
        {
-         uh->checksum = 0;
-         uh->checksum = ip4_tcp_udp_compute_checksum (vm, b, ip4);
+         vnet_calc_ip6_checksums (vm, b, ip6, th, uh);
        }
     }
-  if (is_ip6)
-    {
-      int bogus;
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
-       {
-         th->checksum = 0;
-         th->checksum =
-           ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
-       }
-      if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
-       {
-         uh->checksum = 0;
-         uh->checksum =
-           ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
-       }
-    }
-
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_TCP_CKSUM;
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_UDP_CKSUM;
   b->flags &= ~VNET_BUFFER_F_OFFLOAD_IP_CKSUM;
index 06ae7dd..c5e537c 100644 (file)
@@ -12,6 +12,7 @@ import unittest
 
 from scapy.packet import Raw
 from scapy.layers.inet6 import IPv6, Ether, IP, UDP, ICMPv6PacketTooBig
+from scapy.layers.inet6 import ipv6nh, IPerror6
 from scapy.layers.inet import TCP, ICMP
 from scapy.layers.vxlan import VXLAN
 from scapy.data import ETH_P_IP, ETH_P_IPV6, ETH_P_ARP
@@ -21,6 +22,7 @@ from vpp_object import VppObject
 from vpp_interface import VppInterface
 from vpp_ip import DpoProto
 from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto
+from vpp_vxlan_tunnel import VppVxlanTunnel
 from socket import AF_INET, AF_INET6, inet_pton
 from util import reassemble4
 
@@ -39,6 +41,13 @@ class TestGSO(VppTestCase):
     @classmethod
     def setUpClass(self):
         super(TestGSO, self).setUpClass()
+        res = self.create_pg_interfaces(range(2))
+        res_gso = self.create_pg_interfaces(range(2, 4), 1, 1460)
+        self.create_pg_interfaces(range(4, 5), 1, 8940)
+        self.pg_interfaces.append(res[0])
+        self.pg_interfaces.append(res[1])
+        self.pg_interfaces.append(res_gso[0])
+        self.pg_interfaces.append(res_gso[1])
 
     @classmethod
     def tearDownClass(self):
@@ -46,6 +55,24 @@ class TestGSO(VppTestCase):
 
     def setUp(self):
         super(TestGSO, self).setUp()
+        for i in self.pg_interfaces:
+            i.admin_up()
+            i.config_ip4()
+            i.config_ip6()
+            i.disable_ipv6_ra()
+            i.resolve_arp()
+            i.resolve_ndp()
+
+        self.single_tunnel_bd = 10
+        self.vxlan = VppVxlanTunnel(self, src=self.pg0.local_ip4,
+                                    dst=self.pg0.remote_ip4,
+                                    vni=self.single_tunnel_bd)
+        self.vxlan.add_vpp_config()
+
+        self.vxlan2 = VppVxlanTunnel(self, src=self.pg0.local_ip6,
+                                     dst=self.pg0.remote_ip6,
+                                     vni=self.single_tunnel_bd)
+        self.vxlan2.add_vpp_config()
 
     def tearDown(self):
         super(TestGSO, self).tearDown()
@@ -60,15 +87,6 @@ class TestGSO(VppTestCase):
         #
         # Send jumbo frame with gso disabled and DF bit is set
         #
-        self.create_pg_interfaces(range(2))
-        for i in self.pg_interfaces:
-            i.admin_up()
-            i.config_ip4()
-            i.config_ip6()
-            i.disable_ipv6_ra()
-            i.resolve_arp()
-            i.resolve_ndp()
-
         p4 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) /
               IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4,
                  flags='DF') /
@@ -89,15 +107,6 @@ class TestGSO(VppTestCase):
         # Send jumbo frame with gso enabled and DF bit is set
         # input and output interfaces support GSO
         #
-        self.create_pg_interfaces(range(2, 4), 1, 1460)
-        for i in self.pg_interfaces:
-            i.admin_up()
-            i.config_ip4()
-            i.config_ip6()
-            i.disable_ipv6_ra()
-            i.resolve_arp()
-            i.resolve_ndp()
-
         p41 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
                IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4,
                   flags='DF') /
@@ -115,45 +124,77 @@ class TestGSO(VppTestCase):
             self.assertEqual(rx[TCP].sport, 1234)
             self.assertEqual(rx[TCP].dport, 1234)
 
+        #
+        # ipv6
+        #
+        p61 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
+               IPv6(src=self.pg2.remote_ip6, dst=self.pg3.remote_ip6) /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        rxs = self.send_and_expect(self.pg2, [p61], self.pg3)
+
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg3.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg3.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg2.remote_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg3.remote_ip6)
+            self.assertEqual(rx[IPv6].plen, 65220)  # 65200 + 20 (TCP)
+            self.assertEqual(rx[TCP].sport, 1234)
+            self.assertEqual(rx[TCP].dport, 1234)
+
         #
         # Send jumbo frame with gso enabled only on input interface
         # and DF bit is set. GSO packet will be chunked into gso_size
         # data payload
         #
-        self.create_pg_interfaces(range(4, 5))
-        for i in self.pg_interfaces:
-            i.admin_up()
-            i.config_ip4()
-            i.config_ip6()
-            i.disable_ipv6_ra()
-            i.resolve_arp()
-            i.resolve_ndp()
-
-        self.vapi.feature_gso_enable_disable(self.pg4.sw_if_index)
+        self.vapi.feature_gso_enable_disable(self.pg0.sw_if_index)
         p42 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
-               IP(src=self.pg2.remote_ip4, dst=self.pg4.remote_ip4,
+               IP(src=self.pg2.remote_ip4, dst=self.pg0.remote_ip4,
                   flags='DF') /
                TCP(sport=1234, dport=1234) /
                Raw(b'\xa5' * 65200))
 
-        rxs = self.send_and_expect(self.pg2, [p42], self.pg4, 45)
+        rxs = self.send_and_expect(self.pg2, [p42], self.pg0, 45)
         size = 0
         for rx in rxs:
-            self.assertEqual(rx[Ether].src, self.pg4.local_mac)
-            self.assertEqual(rx[Ether].dst, self.pg4.remote_mac)
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
             self.assertEqual(rx[IP].src, self.pg2.remote_ip4)
-            self.assertEqual(rx[IP].dst, self.pg4.remote_ip4)
+            self.assertEqual(rx[IP].dst, self.pg0.remote_ip4)
             self.assertEqual(rx[TCP].sport, 1234)
             self.assertEqual(rx[TCP].dport, 1234)
 
         size = rxs[44][TCP].seq + rxs[44][IP].len - 20 - 20
         self.assertEqual(size, 65200)
 
+        #
+        # ipv6
+        #
+        p62 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
+               IPv6(src=self.pg2.remote_ip6, dst=self.pg0.remote_ip6) /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        rxs = self.send_and_expect(self.pg2, [p62], self.pg0, 45)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg2.remote_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6)
+            self.assertEqual(rx[TCP].sport, 1234)
+            self.assertEqual(rx[TCP].dport, 1234)
+
+        size = rxs[44][TCP].seq + rxs[44][IPv6].plen - 20
+        self.assertEqual(size, 65200)
+
         #
         # Send jumbo frame with gso enabled only on input interface
         # and DF bit is unset. GSO packet will be fragmented.
         #
         self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0])
+        self.vapi.feature_gso_enable_disable(self.pg1.sw_if_index)
 
         p43 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
                IP(src=self.pg2.remote_ip4, dst=self.pg1.remote_ip4) /
@@ -171,38 +212,197 @@ class TestGSO(VppTestCase):
         size -= 20  # TCP header
         self.assertEqual(size, 65200)
 
+        #
+        # IPv6
+        # Send jumbo frame with gso enabled only on input interface.
+        # ICMPv6 Packet Too Big will be sent back to sender.
+        #
+        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [1280, 0, 0, 0])
+        p63 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) /
+               IPv6(src=self.pg2.remote_ip6, dst=self.pg1.remote_ip6) /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        rxs = self.send_and_expect(self.pg2, [p63], self.pg2, 1)
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg2.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg2.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg2.local_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg2.remote_ip6)
+            self.assertEqual(rx[IPv6].plen, 1240)  # MTU - IPv6 header
+            self.assertEqual(ipv6nh[rx[IPv6].nh], "ICMPv6")
+            self.assertEqual(rx[ICMPv6PacketTooBig].mtu, 1280)
+            self.assertEqual(rx[IPerror6].src, self.pg2.remote_ip6)
+            self.assertEqual(rx[IPerror6].dst, self.pg1.remote_ip6)
+            self.assertEqual(rx[IPerror6].plen - 20, 65200)
+
         #
         # Send jumbo frame with gso enabled only on input interface with 9K MTU
-        # and DF bit is unset. GSO packet will be fragmented. GSO size will be
-        # 8960.
+        # and DF bit is unset. GSO packet will be fragmented. MSS is 8960. GSO
+        # size will be min(MSS, 2048 - 14 - 20) vlib_buffer_t size
         #
         self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [9000, 0, 0, 0])
-        self.create_pg_interfaces(range(5, 6), 1, 8960)
-        for i in self.pg_interfaces:
-            i.admin_up()
-            i.config_ip4()
-            i.config_ip6()
-            i.disable_ipv6_ra()
-            i.resolve_arp()
-            i.resolve_ndp()
-
-        self.vapi.sw_interface_set_mtu(self.pg5.sw_if_index, [9000, 0, 0, 0])
-        self.vapi.feature_gso_enable_disable(self.pg1.sw_if_index)
-        p44 = (Ether(src=self.pg5.remote_mac, dst=self.pg5.local_mac) /
-               IP(src=self.pg5.remote_ip4, dst=self.pg1.remote_ip4) /
+        self.vapi.sw_interface_set_mtu(self.pg4.sw_if_index, [9000, 0, 0, 0])
+        p44 = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
+               IP(src=self.pg4.remote_ip4, dst=self.pg1.remote_ip4) /
                TCP(sport=1234, dport=1234) /
                Raw(b'\xa5' * 65200))
 
         self.pg1.enable_capture()
-        rxs = self.send_and_expect(self.pg5, [p44], self.pg1, 33)
+        rxs = self.send_and_expect(self.pg4, [p44], self.pg1, 33)
         size = 0
         for rx in rxs:
             self.assertEqual(rx[Ether].src, self.pg1.local_mac)
             self.assertEqual(rx[Ether].dst, self.pg1.remote_mac)
-            self.assertEqual(rx[IP].src, self.pg5.remote_ip4)
+            self.assertEqual(rx[IP].src, self.pg4.remote_ip4)
             self.assertEqual(rx[IP].dst, self.pg1.remote_ip4)
         size = rxs[32][TCP].seq + rxs[32][IP].len - 20 - 20
         self.assertEqual(size, 65200)
 
+        #
+        # IPv6
+        #
+        p64 = (Ether(src=self.pg4.remote_mac, dst=self.pg4.local_mac) /
+               IPv6(src=self.pg4.remote_ip6, dst=self.pg1.remote_ip6) /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        self.pg1.enable_capture()
+        rxs = self.send_and_expect(self.pg4, [p64], self.pg1, 34)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg1.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg1.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg4.remote_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg1.remote_ip6)
+        size = rxs[33][TCP].seq + rxs[33][IPv6].plen - 20
+        self.assertEqual(size, 65200)
+
+    def test_gso_vxlan(self):
+        """ GSO VXLAN test """
+        self.logger.info(self.vapi.cli("sh int addr"))
+        #
+        # Send jumbo frame with gso enabled only on input interface and
+        # create VXLAN VTEP on VPP pg0, and put vxlan_tunnel0 and pg2
+        # into BD.
+        #
+        self.vapi.sw_interface_set_l2_bridge(
+            rx_sw_if_index=self.vxlan.sw_if_index, bd_id=self.single_tunnel_bd)
+        self.vapi.sw_interface_set_l2_bridge(
+            rx_sw_if_index=self.pg2.sw_if_index, bd_id=self.single_tunnel_bd)
+        self.vapi.feature_gso_enable_disable(self.pg0.sw_if_index)
+
+        #
+        # IPv4/IPv4 - VXLAN
+        #
+        p45 = (Ether(src=self.pg2.remote_mac, dst="02:fe:60:1e:a2:79") /
+               IP(src=self.pg2.remote_ip4, dst="172.16.3.3", flags='DF') /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        self.pg0.enable_capture()
+        rxs = self.send_and_expect(self.pg2, [p45], self.pg0, 45)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
+            self.assertEqual(rx[IP].src, self.pg0.local_ip4)
+            self.assertEqual(rx[IP].dst, self.pg0.remote_ip4)
+            self.assertEqual(rx[VXLAN].vni, 10)
+            inner = rx[VXLAN].payload
+            self.assertEqual(inner[Ether].src, self.pg2.remote_mac)
+            self.assertEqual(inner[Ether].dst, "02:fe:60:1e:a2:79")
+            self.assertEqual(inner[IP].src, self.pg2.remote_ip4)
+            self.assertEqual(inner[IP].dst, "172.16.3.3")
+            size += inner[IP].len - 20 - 20
+        self.assertEqual(size, 65200)
+
+        #
+        # IPv4/IPv6 - VXLAN
+        #
+        p65 = (Ether(src=self.pg2.remote_mac, dst="02:fe:60:1e:a2:79") /
+               IPv6(src=self.pg2.remote_ip6, dst="fd01:3::3") /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        self.pg0.enable_capture()
+        rxs = self.send_and_expect(self.pg2, [p65], self.pg0, 45)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
+            self.assertEqual(rx[IP].src, self.pg0.local_ip4)
+            self.assertEqual(rx[IP].dst, self.pg0.remote_ip4)
+            self.assertEqual(rx[VXLAN].vni, 10)
+            inner = rx[VXLAN].payload
+            self.assertEqual(inner[Ether].src, self.pg2.remote_mac)
+            self.assertEqual(inner[Ether].dst, "02:fe:60:1e:a2:79")
+            self.assertEqual(inner[IPv6].src, self.pg2.remote_ip6)
+            self.assertEqual(inner[IPv6].dst, "fd01:3::3")
+            size += inner[IPv6].plen - 20
+        self.assertEqual(size, 65200)
+
+        #
+        # disable ipv4/vxlan
+        #
+        self.vxlan.remove_vpp_config()
+
+        #
+        # enable ipv6/vxlan
+        #
+        self.vapi.sw_interface_set_l2_bridge(
+            rx_sw_if_index=self.vxlan2.sw_if_index,
+            bd_id=self.single_tunnel_bd)
+
+        #
+        # IPv6/IPv4 - VXLAN
+        #
+        p46 = (Ether(src=self.pg2.remote_mac, dst="02:fe:60:1e:a2:79") /
+               IP(src=self.pg2.remote_ip4, dst="172.16.3.3", flags='DF') /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        self.pg0.enable_capture()
+        rxs = self.send_and_expect(self.pg2, [p46], self.pg0, 45)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg0.local_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6)
+            self.assertEqual(rx[VXLAN].vni, 10)
+            inner = rx[VXLAN].payload
+            self.assertEqual(inner[Ether].src, self.pg2.remote_mac)
+            self.assertEqual(inner[Ether].dst, "02:fe:60:1e:a2:79")
+            self.assertEqual(inner[IP].src, self.pg2.remote_ip4)
+            self.assertEqual(inner[IP].dst, "172.16.3.3")
+            size += inner[IP].len - 20 - 20
+        self.assertEqual(size, 65200)
+
+        #
+        # IPv6/IPv6 - VXLAN
+        #
+        p66 = (Ether(src=self.pg2.remote_mac, dst="02:fe:60:1e:a2:79") /
+               IPv6(src=self.pg2.remote_ip6, dst="fd01:3::3") /
+               TCP(sport=1234, dport=1234) /
+               Raw(b'\xa5' * 65200))
+
+        self.pg0.enable_capture()
+        rxs = self.send_and_expect(self.pg2, [p66], self.pg0, 45)
+        size = 0
+        for rx in rxs:
+            self.assertEqual(rx[Ether].src, self.pg0.local_mac)
+            self.assertEqual(rx[Ether].dst, self.pg0.remote_mac)
+            self.assertEqual(rx[IPv6].src, self.pg0.local_ip6)
+            self.assertEqual(rx[IPv6].dst, self.pg0.remote_ip6)
+            self.assertEqual(rx[VXLAN].vni, 10)
+            inner = rx[VXLAN].payload
+            self.assertEqual(inner[Ether].src, self.pg2.remote_mac)
+            self.assertEqual(inner[Ether].dst, "02:fe:60:1e:a2:79")
+            self.assertEqual(inner[IPv6].src, self.pg2.remote_ip6)
+            self.assertEqual(inner[IPv6].dst, "fd01:3::3")
+            size += inner[IPv6].plen - 20
+        self.assertEqual(size, 65200)
+
 if __name__ == '__main__':
     unittest.main(testRunner=VppTestRunner)