Unify and cleanup usage of hash_set/unset_mem by various tunnels
[vpp.git] / src / plugins / gtpu / gtpu.c
old mode 100644 (file)
new mode 100755 (executable)
index 6a5c5a3..80069a7
@@ -1,6 +1,6 @@
 /*
  *------------------------------------------------------------------
- * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Copyright (c) 2017 Intel 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:
 
 gtpu_main_t gtpu_main;
 
+/* *INDENT-OFF* */
+VNET_FEATURE_INIT (ip4_gtpu_bypass, static) = {
+  .arc_name = "ip4-unicast",
+  .node_name = "ip4-gtpu-bypass",
+  .runs_before = VNET_FEATURES ("ip4-lookup"),
+};
+
+VNET_FEATURE_INIT (ip6_gtpu_bypass, static) = {
+  .arc_name = "ip6-unicast",
+  .node_name = "ip6-gtpu-bypass",
+  .runs_before = VNET_FEATURES ("ip6-lookup"),
+};
+/* *INDENT-on* */
+
 static u8 *
 format_decap_next (u8 * s, va_list * args)
 {
@@ -45,6 +59,10 @@ format_decap_next (u8 * s, va_list * args)
       return format (s, "drop");
     case GTPU_INPUT_NEXT_L2_INPUT:
       return format (s, "l2");
+    case GTPU_INPUT_NEXT_IP4_INPUT:
+      return format (s, "ip4");
+    case GTPU_INPUT_NEXT_IP6_INPUT:
+      return format (s, "ip6");
     default:
       return format (s, "index %d", next_index);
     }
@@ -121,6 +139,7 @@ VNET_HW_INTERFACE_CLASS (gtpu_hw_class) =
   .name = "GTPU",
   .format_header = format_gtpu_header_with_length,
   .build_rewrite = default_build_rewrite,
+  .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
 };
 /* *INDENT-ON* */
 
@@ -275,25 +294,6 @@ gtpu_decap_next_is_valid (gtpu_main_t * gtm, u32 is_ip6, u32 decap_next_index)
   return decap_next_index < r->n_next_nodes;
 }
 
-static void
-hash_set_key_copy (uword ** h, void *key, uword v)
-{
-  size_t ksz = hash_header (*h)->user;
-  void *copy = clib_mem_alloc (ksz);
-  clib_memcpy (copy, key, ksz);
-  hash_set_mem (*h, copy, v);
-}
-
-static void
-hash_unset_key_free (uword ** h, void *key)
-{
-  hash_pair_t *hp = hash_get_pair_mem (*h, key);
-  ASSERT (hp);
-  key = uword_to_pointer (hp->key, void *);
-  hash_unset_mem (*h, key);
-  clib_mem_free (key);
-}
-
 static uword
 vtep_addr_ref (ip46_address_t * ip)
 {
@@ -304,7 +304,7 @@ vtep_addr_ref (ip46_address_t * ip)
     return ++(*vtep);
   ip46_address_is_ip4 (ip) ?
     hash_set (gtpu_main.vtep4, ip->ip4.as_u32, 1) :
-    hash_set_key_copy (&gtpu_main.vtep6, &ip->ip6, 1);
+    hash_set_mem_alloc (&gtpu_main.vtep6, &ip->ip6, 1);
   return 1;
 }
 
@@ -319,7 +319,7 @@ vtep_addr_unref (ip46_address_t * ip)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
     hash_unset (gtpu_main.vtep4, ip->ip4.as_u32) :
-    hash_unset_key_free (&gtpu_main.vtep6, &ip->ip6);
+    hash_unset_mem_free (&gtpu_main.vtep6, &ip->ip6);
   return 0;
 }
 
@@ -351,7 +351,7 @@ mcast_shared_add (ip46_address_t * dst, fib_node_index_t mfei, adj_index_t ai)
     .mfib_entry_index = mfei,
   };
 
-  hash_set_key_copy (&gtpu_main.mcast_shared, dst, new_ep.as_u64);
+  hash_set_mem_alloc (&gtpu_main.mcast_shared, dst, new_ep.as_u64);
 }
 
 static inline void
@@ -362,7 +362,7 @@ mcast_shared_remove (ip46_address_t * dst)
   adj_unlock (ep.mcast_adj_index);
   mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GTPU);
 
-  hash_unset_key_free (&gtpu_main.mcast_shared, dst);
+  hash_unset_mem_free (&gtpu_main.mcast_shared, dst);
 }
 
 static inline fib_protocol_t
@@ -423,8 +423,8 @@ int vnet_gtpu_add_del_tunnel
 
       /* copy the key */
       if (is_ip6)
-       hash_set_key_copy (&gtm->gtpu6_tunnel_by_key, &key6,
-                          t - gtm->tunnels);
+       hash_set_mem_alloc (&gtm->gtpu6_tunnel_by_key, &key6,
+                           t - gtm->tunnels);
       else
        hash_set (gtm->gtpu4_tunnel_by_key, key4.as_u64, t - gtm->tunnels);
 
@@ -516,7 +516,7 @@ int vnet_gtpu_add_del_tunnel
              fib_node_index_t mfei;
              adj_index_t ai;
              fib_route_path_t path = {
-               .frp_proto = fp,
+               .frp_proto = fib_proto_to_dpo (fp),
                .frp_addr = zero_addr,
                .frp_sw_if_index = 0xffffffff,
                .frp_fib_index = ~0,
@@ -602,7 +602,7 @@ int vnet_gtpu_add_del_tunnel
       if (!is_ip6)
        hash_unset (gtm->gtpu4_tunnel_by_key, key4.as_u64);
       else
-       hash_unset_key_free (&gtm->gtpu6_tunnel_by_key, &key6);
+       hash_unset_mem_free (&gtm->gtpu6_tunnel_by_key, &key6);
 
       if (!ip46_address_is_multicast (&t->dst))
        {
@@ -649,12 +649,17 @@ unformat_decap_next (unformat_input_t * input, va_list * args)
 
   if (unformat (input, "l2"))
     *result = GTPU_INPUT_NEXT_L2_INPUT;
+  else if (unformat (input, "ip4"))
+    *result = GTPU_INPUT_NEXT_IP4_INPUT;
+  else if (unformat (input, "ip6"))
+    *result = GTPU_INPUT_NEXT_IP6_INPUT;
   else if (unformat (input, "node %U", unformat_vlib_node, vm, &node_index))
     *result = get_decap_next_for_node (node_index, ipv4_set);
   else if (unformat (input, "%d", &tmp))
     *result = tmp;
   else
     return 0;
+
   return 1;
 }
 
@@ -871,7 +876,7 @@ VLIB_CLI_COMMAND (create_gtpu_tunnel_command, static) = {
   .short_help =
   "create gtpu tunnel src <local-vtep-addr>"
   " {dst <remote-vtep-addr>|group <mcast-vtep-addr> <intf-name>} teid <nn>"
-  " [encap-vrf-id <nn>] [decap-next [l2|node <name>]] [del]",
+  " [encap-vrf-id <nn>] [decap-next [l2|ip4|ip6|node <name>]] [del]",
   .function = gtpu_add_del_tunnel_command_fn,
 };
 /* *INDENT-ON* */