gtpu: offload RX flow 82/25182/3
authorChenmin Sun <chenmin.sun@intel.com>
Sun, 16 Feb 2020 18:19:15 +0000 (02:19 +0800)
committerDamjan Marion <dmarion@me.com>
Mon, 17 Feb 2020 12:32:59 +0000 (12:32 +0000)
ip4 gtpu cli/api (using flow infra) to create flows and enable them on
different hardware (currently tested with ice)

to offload a gtpu tunnel onto hw:
set flow-offload gtpu hw TwentyFiveGigabitEthernet3/0/0 rx gtpu_tunnel0

to remove offload:
set flow-offload gtpu hw TwentyFiveGigabitEthernet3/0/0 rx gtpu_tunnel0 del

TODO:ipv6 handling

Type: feature
Signed-off-by: Chenmin Sun <chenmin.sun@intel.com>
Change-Id: I8e356feeb0b16cfeadc1bbbe92f773aa2916e715

src/plugins/gtpu/gtpu.api
src/plugins/gtpu/gtpu.c
src/plugins/gtpu/gtpu.h
src/plugins/gtpu/gtpu_api.c
src/plugins/gtpu/gtpu_decap.c
src/plugins/gtpu/gtpu_test.c

index 052e6a2..f084cc8 100644 (file)
@@ -106,6 +106,23 @@ autoreply define sw_interface_set_gtpu_bypass
   option vat_help = "<intfc> | sw_if_index <id> [ip4 | ip6] [enable | disable]";
 };
 
+/** \brief Offload gtpu rx request
+    @param client_index - opaque cookie to identify the sender
+    @param context - sender context, to match reply w/ request
+    @param hw_if_index - rx hw interface
+    @param sw_if_index - gtpu interface to offload
+    @param enable - if non-zero enable, else disable
+*/
+autoreply define gtpu_offload_rx
+{
+  u32 client_index;
+  u32 context;
+  u32 hw_if_index;
+  u32 sw_if_index;
+  u8 enable;
+  option vat_help = "hw <intfc> rx <tunnel-name> [del]";
+};
+
 /*
  * Local Variables:
  * eval: (c-set-style "gnu")
index 48ac8a7..905ca5e 100644 (file)
@@ -31,7 +31,7 @@
 #include <vnet/plugin/plugin.h>
 #include <vpp/app/version.h>
 #include <gtpu/gtpu.h>
-
+#include <vnet/flow/flow.h>
 
 gtpu_main_t gtpu_main;
 
@@ -419,6 +419,9 @@ int vnet_gtpu_add_del_tunnel
 
       ip_udp_gtpu_rewrite (t, is_ip6);
 
+      /* clear the flow index */
+      t->flow_index = ~0;
+
       /* copy the key */
       if (is_ip6)
        hash_set_mem_alloc (&gtm->gtpu6_tunnel_by_key, &key6,
@@ -602,6 +605,9 @@ int vnet_gtpu_add_del_tunnel
 
       if (!ip46_address_is_multicast (&t->dst))
        {
+         if (t->flow_index != ~0)
+           vnet_flow_del (vnm, t->flow_index);
+
          vtep_addr_unref (&t->src);
          fib_entry_untrack (t->fib_entry_index, t->sibling_index);
        }
@@ -1093,6 +1099,135 @@ VLIB_CLI_COMMAND (set_interface_ip6_gtpu_bypass_command, static) = {
 };
 /* *INDENT-ON* */
 
+int
+vnet_gtpu_add_del_rx_flow (u32 hw_if_index, u32 t_index, int is_add)
+{
+  gtpu_main_t *gtm = &gtpu_main;
+  gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
+  vnet_main_t *vnm = vnet_get_main ();
+  if (is_add)
+    {
+      if (t->flow_index == ~0)
+       {
+         vnet_flow_t flow = {
+           .actions =
+             VNET_FLOW_ACTION_REDIRECT_TO_NODE | VNET_FLOW_ACTION_MARK |
+             VNET_FLOW_ACTION_BUFFER_ADVANCE,
+           .mark_flow_id = t_index + gtm->flow_id_start,
+           .redirect_node_index = gtpu4_flow_input_node.index,
+           .buffer_advance = sizeof (ethernet_header_t)
+             + sizeof (ip4_header_t) + sizeof (udp_header_t),
+           .type = VNET_FLOW_TYPE_IP4_GTPU_IP4,
+           .ip4_gtpu = {
+                        .protocol = IP_PROTOCOL_UDP,
+                        .src_addr.addr = t->dst.ip4,
+                        .src_addr.mask.as_u32 = ~0,
+                        .dst_addr.addr = t->src.ip4,
+                        .dst_addr.mask.as_u32 = ~0,
+                        .teid = t->teid,
+                        }
+           ,
+         };
+         vnet_flow_add (vnm, &flow, &t->flow_index);
+       }
+
+      return vnet_flow_enable (vnm, t->flow_index, hw_if_index);
+    }
+
+  /* flow index is removed when the tunnel is deleted */
+  return vnet_flow_disable (vnm, t->flow_index, hw_if_index);
+}
+
+u32
+vnet_gtpu_get_tunnel_index (u32 sw_if_index)
+{
+  gtpu_main_t *gtm = &gtpu_main;
+
+  if (sw_if_index >= vec_len (gtm->tunnel_index_by_sw_if_index))
+    return ~0;
+  return gtm->tunnel_index_by_sw_if_index[sw_if_index];
+}
+
+static clib_error_t *
+gtpu_offload_command_fn (vlib_main_t * vm,
+                        unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  unformat_input_t _line_input, *line_input = &_line_input;
+
+  /* Get a line of input. */
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  vnet_main_t *vnm = vnet_get_main ();
+  u32 rx_sw_if_index = ~0;
+  u32 hw_if_index = ~0;
+  int is_add = 1;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "hw %U", unformat_vnet_hw_interface, vnm,
+                   &hw_if_index))
+       continue;
+      if (unformat (line_input, "rx %U", unformat_vnet_sw_interface, vnm,
+                   &rx_sw_if_index))
+       continue;
+      if (unformat (line_input, "del"))
+       {
+         is_add = 0;
+         continue;
+       }
+      return clib_error_return (0, "unknown input `%U'",
+                               format_unformat_error, line_input);
+    }
+
+  if (rx_sw_if_index == ~0)
+    return clib_error_return (0, "missing rx interface");
+  if (hw_if_index == ~0)
+    return clib_error_return (0, "missing hw interface");
+
+  u32 t_index = vnet_gtpu_get_tunnel_index (rx_sw_if_index);;
+  if (t_index == ~0)
+    return clib_error_return (0, "%U is not a gtpu tunnel",
+                             format_vnet_sw_if_index_name, vnm,
+                             rx_sw_if_index);
+
+  gtpu_main_t *gtm = &gtpu_main;
+  gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
+
+  /* first support ipv4 hw offload */
+  if (!ip46_address_is_ip4 (&t->dst))
+    return clib_error_return (0, "currently only IPV4 tunnels are supported");
+
+  /* inner protocol should be IPv4 */
+  if (t->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT)
+    return clib_error_return (0,
+                             "currently only inner IPV4 protocol is supported");
+
+  vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
+  ip4_main_t *im = &ip4_main;
+  u32 rx_fib_index =
+    vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
+
+  if (t->encap_fib_index != rx_fib_index)
+    return clib_error_return (0, "interface/tunnel fib mismatch");
+
+  if (vnet_gtpu_add_del_rx_flow (hw_if_index, t_index, is_add))
+    return clib_error_return (0, "error %s flow",
+                             is_add ? "enabling" : "disabling");
+
+  return 0;
+}
+
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (gtpu_offload_command, static) = {
+    .path = "set flow-offload gtpu",
+    .short_help =
+    "set flow-offload gtpu hw <inerface-name> rx <tunnel-name> [del]",
+    .function = gtpu_offload_command_fn,
+};
+/* *INDENT-ON* */
+
 clib_error_t *
 gtpu_init (vlib_main_t * vm)
 {
@@ -1101,6 +1236,9 @@ gtpu_init (vlib_main_t * vm)
   gtm->vnet_main = vnet_get_main ();
   gtm->vlib_main = vm;
 
+  vnet_flow_get_range (gtm->vnet_main, "gtpu", 1024 * 1024,
+                      &gtm->flow_id_start);
+
   /* initialize the ip6 hash */
   gtm->gtpu6_tunnel_by_key = hash_create_mem (0,
                                              sizeof (gtpu6_tunnel_key_t),
index dc36308..1d47f2d 100644 (file)
@@ -81,7 +81,7 @@ typedef CLIB_PACKED(struct
 {
   ip4_header_t ip4;            /* 20 bytes */
   udp_header_t udp;            /* 8 bytes */
-  gtpu_header_t gtpu;         /* 8 bytes */
+  gtpu_header_t gtpu;         /* 12 bytes */
 }) ip4_gtpu_header_t;
 /* *INDENT-ON* */
 
@@ -173,6 +173,8 @@ typedef struct
    * The tunnels sibling index on the FIB entry's dependency list.
    */
   u32 sibling_index;
+
+  u32 flow_index;              /* infra flow index */
 } gtpu_tunnel_t;
 
 #define foreach_gtpu_input_next        \
@@ -231,6 +233,7 @@ typedef struct
   /* convenience */
   vlib_main_t *vlib_main;
   vnet_main_t *vnet_main;
+  u32 flow_id_start;
 } gtpu_main_t;
 
 extern gtpu_main_t gtpu_main;
@@ -239,6 +242,7 @@ extern vlib_node_registration_t gtpu4_input_node;
 extern vlib_node_registration_t gtpu6_input_node;
 extern vlib_node_registration_t gtpu4_encap_node;
 extern vlib_node_registration_t gtpu6_encap_node;
+extern vlib_node_registration_t gtpu4_flow_input_node;
 
 u8 *format_gtpu_encap_trace (u8 * s, va_list * args);
 
@@ -262,6 +266,9 @@ typedef struct
 } gtpu_encap_trace_t;
 
 void vnet_int_gtpu_bypass_mode (u32 sw_if_index, u8 is_ip6, u8 is_enable);
+u32 vnet_gtpu_get_tunnel_index (u32 sw_if_index);
+int vnet_gtpu_add_del_rx_flow (u32 hw_if_index, u32 t_imdex, int is_add);
+
 #endif /* included_vnet_gtpu_h */
 
 
index 0c4a315..50eb0a1 100644 (file)
 #define REPLY_MSG_ID_BASE gtm->msg_id_base
 #include <vlibapi/api_helper_macros.h>
 
+static void
+vl_api_gtpu_offload_rx_t_handler (vl_api_gtpu_offload_rx_t * mp)
+{
+  vl_api_gtpu_offload_rx_reply_t *rmp;
+  int rv = 0;
+  vl_api_interface_index_t hw_if_index = ntohl (mp->hw_if_index);
+  vl_api_interface_index_t sw_if_index = ntohl (mp->sw_if_index);
+
+  if (!vnet_hw_interface_is_valid (vnet_get_main (), hw_if_index))
+    {
+      rv = VNET_API_ERROR_NO_SUCH_ENTRY;
+      goto err;
+    }
+  VALIDATE_SW_IF_INDEX (mp);
+
+  u32 t_index = vnet_gtpu_get_tunnel_index (sw_if_index);
+  if (t_index == ~0)
+    {
+      rv = VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
+      goto err;
+    }
+
+  gtpu_main_t *gtm = &gtpu_main;
+  gtpu_tunnel_t *t = pool_elt_at_index (gtm->tunnels, t_index);
+  if (!ip46_address_is_ip4 (&t->dst))
+    {
+      rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
+      goto err;
+    }
+
+  if (t->decap_next_index != GTPU_INPUT_NEXT_IP4_INPUT)
+    {
+      rv = VNET_API_ERROR_INVALID_ADDRESS_FAMILY;
+      goto err;
+    }
+
+  vnet_main_t *vnm = vnet_get_main ();
+  vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
+  ip4_main_t *im = &ip4_main;
+  u32 rx_fib_index =
+    vec_elt (im->fib_index_by_sw_if_index, hw_if->sw_if_index);
+
+  if (t->encap_fib_index != rx_fib_index)
+    {
+      rv = VNET_API_ERROR_NO_SUCH_FIB;
+      goto err;
+    }
+
+  if (vnet_gtpu_add_del_rx_flow (hw_if_index, t_index, mp->enable))
+    {
+      rv = VNET_API_ERROR_UNSPECIFIED;
+      goto err;
+    }
+  BAD_SW_IF_INDEX_LABEL;
+err:
+
+  REPLY_MACRO (VL_API_GTPU_OFFLOAD_RX_REPLY);
+}
+
 static void
   vl_api_sw_interface_set_gtpu_bypass_t_handler
   (vl_api_sw_interface_set_gtpu_bypass_t * mp)
index 1398ebd..99af730 100644 (file)
@@ -1242,4 +1242,508 @@ clib_error_t * ip6_gtpu_bypass_init (vlib_main_t * vm)
 { return 0; }
 
 VLIB_INIT_FUNCTION (ip6_gtpu_bypass_init);
+
+#define foreach_gtpu_flow_error                                        \
+  _(NONE, "no error")                                                  \
+  _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                                \
+  _(IP_HEADER_ERROR, "Rx ip header errors")                            \
+  _(UDP_CHECKSUM_ERROR, "Rx udp checksum errors")                              \
+  _(UDP_LENGTH_ERROR, "Rx udp length errors")
+
+typedef enum
+{
+#define _(f,s) GTPU_FLOW_ERROR_##f,
+  foreach_gtpu_flow_error
+#undef _
+#define gtpu_error(n,s) GTPU_FLOW_ERROR_##n,
+#include <gtpu/gtpu_error.def>
+#undef gtpu_error
+    GTPU_FLOW_N_ERROR,
+} gtpu_flow_error_t;
+
+static char *gtpu_flow_error_strings[] = {
+#define _(n,s) s,
+  foreach_gtpu_flow_error
+#undef _
+#define gtpu_error(n,s) s,
+#include <gtpu/gtpu_error.def>
+#undef gtpu_error
+#undef _
+
+};
+
+#define gtpu_local_need_csum_check(_b)                         \
+    (!(_b->flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED  \
+       || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
+
+#define gtpu_local_csum_is_valid(_b)  \
+    ((_b->flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT \
+       || _b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM) != 0)
+
+static_always_inline u8
+gtpu_validate_udp_csum (vlib_main_t * vm, vlib_buffer_t *b)
+{
+  u32 flags = b->flags;
+  enum { offset = sizeof(ip4_header_t) + sizeof(udp_header_t)};
+
+  /* Verify UDP checksum */
+  if ((flags & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
+  {
+    vlib_buffer_advance (b, -offset);
+    flags = ip4_tcp_udp_validate_checksum (vm, b);
+    vlib_buffer_advance (b, offset);
+  }
+
+  return (flags & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
+}
+
+static_always_inline u8
+gtpu_check_ip (vlib_buffer_t *b, u16 payload_len)
+{
+  ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) - 
+      sizeof(ip4_header_t) - sizeof(udp_header_t);
+  u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
+  u16 expected = payload_len + sizeof(ip4_header_t) + sizeof(udp_header_t);
+  return ip_len > expected || ip4_hdr->ttl == 0 || ip4_hdr->ip_version_and_header_length != 0x45;
+}
+
+static_always_inline u8
+gtpu_check_ip_udp_len (vlib_buffer_t *b)
+{
+  ip4_header_t * ip4_hdr = vlib_buffer_get_current(b) - 
+      sizeof(ip4_header_t) - sizeof(udp_header_t);
+  udp_header_t * udp_hdr = vlib_buffer_get_current(b) - sizeof(udp_header_t);
+  u16 ip_len = clib_net_to_host_u16 (ip4_hdr->length);
+  u16 udp_len = clib_net_to_host_u16 (udp_hdr->length);
+  return udp_len > ip_len;
+}
+
+static_always_inline u8
+gtpu_err_code (u8 ip_err0, u8 udp_err0, u8 csum_err0)
+{
+  u8 error0 = GTPU_FLOW_ERROR_NONE;
+  if (ip_err0)
+    error0 =  GTPU_FLOW_ERROR_IP_HEADER_ERROR;
+  if (udp_err0)
+    error0 =  GTPU_FLOW_ERROR_UDP_LENGTH_ERROR;
+  if (csum_err0)
+    error0 =  GTPU_FLOW_ERROR_UDP_CHECKSUM_ERROR;
+  return error0;
+}
+
+
+always_inline uword
+gtpu_flow_input (vlib_main_t * vm,
+             vlib_node_runtime_t * node,
+             vlib_frame_t * from_frame)
+{
+  u32 n_left_from, next_index, * from, * to_next;
+  gtpu_main_t * gtm = &gtpu_main;
+  vnet_main_t * vnm = gtm->vnet_main;
+  vnet_interface_main_t * im = &vnm->interface_main;
+  u32 pkts_decapsulated = 0;
+  u32 thread_index = vlib_get_thread_index();
+  u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
+  u8 ip_err0, ip_err1, udp_err0, udp_err1, csum_err0, csum_err1;
+
+  from = vlib_frame_vector_args (from_frame);
+  n_left_from = from_frame->n_vectors;
+
+  next_index = node->cached_next_index;
+  stats_sw_if_index = node->runtime_data[0];
+  stats_n_packets = stats_n_bytes = 0;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index,
+                          to_next, n_left_to_next);
+
+      while (n_left_from >= 4 && n_left_to_next >= 2)
+        {
+          u32 bi0, bi1;
+         vlib_buffer_t * b0, * b1;
+         u32 next0, next1;
+          gtpu_header_t * gtpu0, * gtpu1;
+          u32 gtpu_hdr_len0, gtpu_hdr_len1;
+          u32 tunnel_index0, tunnel_index1;
+          gtpu_tunnel_t * t0, * t1;
+          u32 error0, error1;
+               u32 sw_if_index0, sw_if_index1, len0, len1;
+          u8 has_space0 = 0, has_space1 = 0;
+          u8 ver0, ver1;
+
+         /* Prefetch next iteration. */
+         {
+           vlib_buffer_t * p2, * p3;
+
+           p2 = vlib_get_buffer (vm, from[2]);
+           p3 = vlib_get_buffer (vm, from[3]);
+
+           vlib_prefetch_buffer_header (p2, LOAD);
+           vlib_prefetch_buffer_header (p3, LOAD);
+
+           CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+           CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
+         }
+
+         bi0 = from[0];
+         bi1 = from[1];
+         to_next[0] = bi0;
+         to_next[1] = bi1;
+         from += 2;
+         to_next += 2;
+         n_left_to_next -= 2;
+         n_left_from -= 2;
+
+         b0 = vlib_get_buffer (vm, bi0);
+         b1 = vlib_get_buffer (vm, bi1);
+
+          /* udp leaves current_data pointing at the gtpu header */
+          gtpu0 = vlib_buffer_get_current (b0);
+          gtpu1 = vlib_buffer_get_current (b1);
+
+          len0 = vlib_buffer_length_in_chain (vm, b0);
+          len1 = vlib_buffer_length_in_chain (vm, b1);
+
+          tunnel_index0 = ~0;
+          error0 = 0;
+
+          tunnel_index1 = ~0;
+          error1 = 0;
+
+         ip_err0 = gtpu_check_ip (b0, len0);
+         udp_err0 = gtpu_check_ip_udp_len (b0);
+         ip_err1 = gtpu_check_ip (b1, len1);
+         udp_err1 = gtpu_check_ip_udp_len (b1);
+
+          if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
+            csum_err0 = !gtpu_validate_udp_csum (vm, b0);
+          else
+            csum_err0 = !gtpu_local_csum_is_valid (b0);
+          if (PREDICT_FALSE (gtpu_local_need_csum_check (b1)))
+            csum_err1 = !gtpu_validate_udp_csum (vm, b1);
+          else
+            csum_err1 = !gtpu_local_csum_is_valid (b1);
+
+         if (ip_err0 || udp_err0 || csum_err0)
+           {
+             next0 = GTPU_INPUT_NEXT_DROP;
+             error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
+             goto trace0;
+           }
+
+          /* speculatively load gtp header version field */
+          ver0 = gtpu0->ver_flags;
+
+              /*
+           * Manipulate gtpu header
+           * TBD: Manipulate Sequence Number and N-PDU Number
+           * TBD: Manipulate Next Extension Header
+           */
+          gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
+
+          has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
+         if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
+           {
+             error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
+             next0 = GTPU_INPUT_NEXT_DROP;
+             goto trace0;
+           }
+
+               /* Manipulate packet 0 */
+          ASSERT (b0->flow_id != 0);
+          tunnel_index0 = b0->flow_id - gtm->flow_id_start;
+          t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
+             b0->flow_id = 0;
+
+         /* Pop gtpu header */
+         vlib_buffer_advance (b0, gtpu_hdr_len0);
+
+          next0 = GTPU_INPUT_NEXT_IP4_INPUT;
+          sw_if_index0 = t0->sw_if_index;
+
+          /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
+          vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
+
+          pkts_decapsulated ++;
+          stats_n_packets += 1;
+          stats_n_bytes += len0;
+
+          /* Batch stats increment on the same gtpu tunnel so counter
+              is not incremented per packet */
+          if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
+            {
+              stats_n_packets -= 1;
+              stats_n_bytes -= len0;
+              if (stats_n_packets)
+                   vlib_increment_combined_counter
+                    (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
+                      thread_index, stats_sw_if_index,
+                      stats_n_packets, stats_n_bytes);
+              stats_n_packets = 1;
+              stats_n_bytes = len0;
+              stats_sw_if_index = sw_if_index0;
+            }
+
+trace0:
+          b0->error = error0 ? node->errors[error0] : 0;
+
+          if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
+            {
+              gtpu_rx_trace_t *tr
+                = vlib_add_trace (vm, node, b0, sizeof (*tr));
+              tr->next_index = next0;
+              tr->error = error0;
+              tr->tunnel_index = tunnel_index0;
+              tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
+            }
+
+         if (ip_err1 || udp_err1 || csum_err1)
+           {
+             next1 = GTPU_INPUT_NEXT_DROP;
+             error1 = gtpu_err_code (ip_err1, udp_err1, csum_err1);
+             goto trace1;
+           }
+
+          /* speculatively load gtp header version field */
+         ver1 = gtpu1->ver_flags;
+
+          /*
+           * Manipulate gtpu header
+           * TBD: Manipulate Sequence Number and N-PDU Number
+           * TBD: Manipulate Next Extension Header
+           */
+          gtpu_hdr_len1 = sizeof(gtpu_header_t) - (((ver1 & GTPU_E_S_PN_BIT) == 0) * 4);
+          has_space1 = vlib_buffer_has_space (b1, gtpu_hdr_len1);
+               if (PREDICT_FALSE (((ver1 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space1)))
+                 {
+                 error1 = has_space1 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
+                 next1 = GTPU_INPUT_NEXT_DROP;
+                 goto trace1;
+            }
+
+          /* Manipulate packet 1 */
+          ASSERT (b1->flow_id != 0);
+          tunnel_index1 = b1->flow_id - gtm->flow_id_start;
+          t1 = pool_elt_at_index (gtm->tunnels, tunnel_index1);
+          b1->flow_id = 0;
+
+         /* Pop gtpu header */
+         vlib_buffer_advance (b1, gtpu_hdr_len1);
+
+          next1 = GTPU_INPUT_NEXT_IP4_INPUT;
+          sw_if_index1 = t1->sw_if_index;
+
+          /* Required to make the l2 tag push / pop code work on l2 subifs */
+          /* This won't happen in current implementation as only 
+              ipv4/udp/gtpu/IPV4 type packets can be matched */
+          if (PREDICT_FALSE(next1 == GTPU_INPUT_NEXT_L2_INPUT))
+            vnet_update_l2_len (b1);
+
+          /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
+          vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1;
+
+          pkts_decapsulated ++;
+          stats_n_packets += 1;
+          stats_n_bytes += len1;
+
+         /* Batch stats increment on the same gtpu tunnel so counter
+            is not incremented per packet */
+           if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
+           {
+             stats_n_packets -= 1;
+             stats_n_bytes -= len1;
+             if (stats_n_packets)
+                     vlib_increment_combined_counter
+                      (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
+                       thread_index, stats_sw_if_index,
+                       stats_n_packets, stats_n_bytes);
+             stats_n_packets = 1;
+             stats_n_bytes = len1;
+             stats_sw_if_index = sw_if_index1;
+           }
+
+trace1:
+          b1->error = error1 ? node->errors[error1] : 0;
+
+          if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
+            {
+              gtpu_rx_trace_t *tr
+                = vlib_add_trace (vm, node, b1, sizeof (*tr));
+              tr->next_index = next1;
+              tr->error = error1;
+              tr->tunnel_index = tunnel_index1;
+              tr->teid = has_space1 ? clib_net_to_host_u32(gtpu1->teid) : ~0;
+            }
+
+           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
+                                          to_next, n_left_to_next,
+                                          bi0, bi1, next0, next1);
+        }
+
+      while (n_left_from > 0 && n_left_to_next > 0)
+        {
+          u32 bi0;
+          vlib_buffer_t * b0;
+          u32 next0;
+          gtpu_header_t * gtpu0;
+          u32 gtpu_hdr_len0;
+          u32 error0;
+          u32 tunnel_index0;
+          gtpu_tunnel_t * t0;
+          u32 sw_if_index0, len0;
+          u8 has_space0 = 0;
+          u8 ver0;
+
+          bi0 = from[0];
+          to_next[0] = bi0;
+          from += 1;
+          to_next += 1;
+          n_left_from -= 1;
+          n_left_to_next -= 1;
+
+          b0 = vlib_get_buffer (vm, bi0);
+          len0 = vlib_buffer_length_in_chain (vm, b0);
+
+          tunnel_index0 = ~0;
+          error0 = 0;
+
+         ip_err0 = gtpu_check_ip (b0, len0);
+         udp_err0 = gtpu_check_ip_udp_len (b0);
+          if (PREDICT_FALSE (gtpu_local_need_csum_check (b0)))
+            csum_err0 = !gtpu_validate_udp_csum (vm, b0);
+          else
+            csum_err0 = !gtpu_local_csum_is_valid (b0);
+
+         if (ip_err0 || udp_err0 || csum_err0)
+           {
+             next0 = GTPU_INPUT_NEXT_DROP;
+             error0 = gtpu_err_code (ip_err0, udp_err0, csum_err0);
+             goto trace00;
+           }
+
+          /* udp leaves current_data pointing at the gtpu header */
+          gtpu0 = vlib_buffer_get_current (b0);
+
+          /* speculatively load gtp header version field */
+          ver0 = gtpu0->ver_flags;
+
+          /*
+           * Manipulate gtpu header
+           * TBD: Manipulate Sequence Number and N-PDU Number
+           * TBD: Manipulate Next Extension Header
+           */
+          gtpu_hdr_len0 = sizeof(gtpu_header_t) - (((ver0 & GTPU_E_S_PN_BIT) == 0) * 4);
+
+          has_space0 = vlib_buffer_has_space (b0, gtpu_hdr_len0);
+          if (PREDICT_FALSE (((ver0 & GTPU_VER_MASK) != GTPU_V1_VER) | (!has_space0)))
+            {
+               error0 = has_space0 ? GTPU_ERROR_BAD_VER : GTPU_ERROR_TOO_SMALL;
+               next0 = GTPU_INPUT_NEXT_DROP;
+               goto trace00;
+            }
+
+          ASSERT (b0->flow_id != 0);
+          tunnel_index0 = b0->flow_id - gtm->flow_id_start;
+          t0 = pool_elt_at_index (gtm->tunnels, tunnel_index0);
+             b0->flow_id = 0;
+
+         /* Pop gtpu header */
+         vlib_buffer_advance (b0, gtpu_hdr_len0);
+
+          next0 = GTPU_INPUT_NEXT_IP4_INPUT;
+          sw_if_index0 = t0->sw_if_index;
+
+          /* Set packet input sw_if_index to unicast GTPU tunnel for learning */
+          vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0;
+
+          pkts_decapsulated ++;
+          stats_n_packets += 1;
+          stats_n_bytes += len0;
+
+         /* Batch stats increment on the same gtpu tunnel so counter
+            is not incremented per packet */
+          if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
+            {
+              stats_n_packets -= 1;
+              stats_n_bytes -= len0;
+              if (stats_n_packets)
+                   vlib_increment_combined_counter
+                 (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
+                  thread_index, stats_sw_if_index,
+                  stats_n_packets, stats_n_bytes);
+              stats_n_packets = 1;
+              stats_n_bytes = len0;
+              stats_sw_if_index = sw_if_index0;
+            }
+  trace00:
+            b0->error = error0 ? node->errors[error0] : 0;
+
+            if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
+              {
+                gtpu_rx_trace_t *tr
+                  = vlib_add_trace (vm, node, b0, sizeof (*tr));
+                tr->next_index = next0;
+                tr->error = error0;
+                tr->tunnel_index = tunnel_index0;
+                tr->teid = has_space0 ? clib_net_to_host_u32(gtpu0->teid) : ~0;
+              }
+           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+                                          to_next, n_left_to_next,
+                                          bi0, next0);
+       }
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+
+    /* Do we still need this now that tunnel tx stats is kept? */
+    vlib_node_increment_counter (vm, gtpu4_flow_input_node.index,
+                               GTPU_ERROR_DECAPSULATED,
+                               pkts_decapsulated);
+
+    /* Increment any remaining batch stats */
+    if (stats_n_packets)
+      {
+        vlib_increment_combined_counter
+       (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
+        thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
+        node->runtime_data[0] = stats_sw_if_index;
+      }
+
+    return from_frame->n_vectors;
+}
+
+VLIB_NODE_FN (gtpu4_flow_input_node) (vlib_main_t * vm,
+             vlib_node_runtime_t * node,
+             vlib_frame_t * from_frame)
+{
+       return gtpu_flow_input(vm, node, from_frame);
+}
+
+
+/* *INDENT-OFF* */
+#ifndef CLIB_MULTIARCH_VARIANT
+VLIB_REGISTER_NODE (gtpu4_flow_input_node) = {
+  .name = "gtpu4-flow-input",
+  .type = VLIB_NODE_TYPE_INTERNAL,
+  .vector_size = sizeof (u32),
+
+  .format_trace = format_gtpu_rx_trace,
+
+  .n_errors = GTPU_FLOW_N_ERROR,
+  .error_strings = gtpu_flow_error_strings,
+
+  .n_next_nodes = GTPU_INPUT_N_NEXT,
+  .next_nodes = {
+#define _(s,n) [GTPU_INPUT_NEXT_##s] = n,
+    foreach_gtpu_input_next
+#undef _
+
+  },
+};
+#endif
+/* *INDENT-ON* */
+
 #endif /* CLIB_MARCH_VARIANT */
index 55c5363..c780bd7 100644 (file)
@@ -107,6 +107,12 @@ api_unformat_sw_if_index (unformat_input_t * input, va_list * args)
   return 1;
 }
 
+static uword
+api_unformat_hw_if_index (unformat_input_t * input, va_list * args)
+{
+  return 0;
+}
+
 static int
 api_sw_interface_set_gtpu_bypass (vat_main_t * vam)
 {
@@ -173,6 +179,58 @@ static uword unformat_gtpu_decap_next
   return 1;
 }
 
+static int
+api_gtpu_offload_rx (vat_main_t * vam)
+{
+  unformat_input_t *line_input = vam->input;
+  vl_api_gtpu_offload_rx_t *mp;
+  u32 rx_sw_if_index = ~0;
+  u32 hw_if_index = ~0;
+  int is_add = 1;
+  int ret;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+                 if (unformat (line_input, "hw %U", api_unformat_hw_if_index, vam, &hw_if_index))
+                   ;
+                 else
+                 if (unformat (line_input, "rx %U", api_unformat_sw_if_index, vam, &rx_sw_if_index))
+                   ;
+                 else
+      if (unformat (line_input, "del"))
+      {
+       is_add = 0;
+       continue;
+           }
+      else
+      {
+       errmsg ("parse error '%U'", format_unformat_error, line_input);
+       return -99;
+      }
+    }
+
+  if (rx_sw_if_index == ~0)
+    {
+      errmsg ("missing rx interface");
+      return -99;
+    }
+
+  if (hw_if_index == ~0)
+    {
+      errmsg ("missing hw interface");
+      return -99;
+    }
+
+  M (GTPU_OFFLOAD_RX, mp);
+  mp->hw_if_index = ntohl (hw_if_index);
+  mp->sw_if_index = ntohl (rx_sw_if_index);
+  mp->enable = is_add;
+
+  S (mp);
+  W (ret);
+  return ret;
+}
+
 static int
 api_gtpu_add_del_tunnel (vat_main_t * vam)
 {