Unify and cleanup usage of hash_set/unset_mem by various tunnels 53/9953/3
authorJohn Lo <loj@cisco.com>
Thu, 4 Jan 2018 21:39:42 +0000 (16:39 -0500)
committerDave Barach <openvpp@barachs.net>
Fri, 5 Jan 2018 16:50:02 +0000 (16:50 +0000)
Move the functions hash_set_key_copy() and hash_unset_key_free()
which are dupilicated in various tunnel support code modules to
hash.h as hash_set_mem_alloc() and hash_unset_mem_free() to be
used by all.

Change-Id: I40723cabe29072ab7feb1804c221f28606d8e4fe
Signed-off-by: John Lo <loj@cisco.com>
src/plugins/gtpu/gtpu.c
src/vnet/geneve/geneve.c
src/vnet/vxlan-gpe/vxlan_gpe.c
src/vnet/vxlan/vxlan.c
src/vppinfra/hash.h

index 89868d9..80069a7 100755 (executable)
@@ -294,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)
 {
@@ -323,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;
 }
 
@@ -338,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;
 }
 
@@ -370,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
@@ -381,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
@@ -442,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);
 
@@ -621,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))
        {
index 01f0cd4..56db5ee 100644 (file)
@@ -288,25 +288,6 @@ geneve_decap_next_is_valid (geneve_main_t * vxm, u32 is_ip6,
   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)
 {
@@ -317,7 +298,7 @@ vtep_addr_ref (ip46_address_t * ip)
     return ++(*vtep);
   ip46_address_is_ip4 (ip) ?
     hash_set (geneve_main.vtep4, ip->ip4.as_u32, 1) :
-    hash_set_key_copy (&geneve_main.vtep6, &ip->ip6, 1);
+    hash_set_mem_alloc (&geneve_main.vtep6, &ip->ip6, 1);
   return 1;
 }
 
@@ -332,7 +313,7 @@ vtep_addr_unref (ip46_address_t * ip)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
     hash_unset (geneve_main.vtep4, ip->ip4.as_u32) :
-    hash_unset_key_free (&geneve_main.vtep6, &ip->ip6);
+    hash_unset_mem_free (&geneve_main.vtep6, &ip->ip6);
   return 0;
 }
 
@@ -365,7 +346,7 @@ mcast_shared_add (ip46_address_t * remote,
     .mfib_entry_index = mfei,
   };
 
-  hash_set_key_copy (&geneve_main.mcast_shared, remote, new_ep.as_u64);
+  hash_set_mem_alloc (&geneve_main.mcast_shared, remote, new_ep.as_u64);
 }
 
 static inline void
@@ -376,7 +357,7 @@ mcast_shared_remove (ip46_address_t * remote)
   adj_unlock (ep.mcast_adj_index);
   mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_GENEVE);
 
-  hash_unset_key_free (&geneve_main.mcast_shared, remote);
+  hash_unset_mem_free (&geneve_main.mcast_shared, remote);
 }
 
 static inline fib_protocol_t
@@ -445,8 +426,8 @@ int vnet_geneve_add_del_tunnel
 
       /* copy the key */
       if (is_ip6)
-       hash_set_key_copy (&vxm->geneve6_tunnel_by_key, &key6,
-                          t - vxm->tunnels);
+       hash_set_mem_alloc (&vxm->geneve6_tunnel_by_key, &key6,
+                           t - vxm->tunnels);
       else
        hash_set (vxm->geneve4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
 
@@ -624,7 +605,7 @@ int vnet_geneve_add_del_tunnel
       if (!is_ip6)
        hash_unset (vxm->geneve4_tunnel_by_key, key4.as_u64);
       else
-       hash_unset_key_free (&vxm->geneve6_tunnel_by_key, &key6);
+       hash_unset_mem_free (&vxm->geneve6_tunnel_by_key, &key6);
 
       if (!ip46_address_is_multicast (&t->remote))
        {
index 65a7ba3..b0c6a68 100644 (file)
@@ -381,25 +381,6 @@ vxlan6_gpe_rewrite (vxlan_gpe_tunnel_t * t, u32 extension_size,
   return (0);
 }
 
-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)
 {
@@ -410,7 +391,7 @@ vtep_addr_ref (ip46_address_t * ip)
     return ++(*vtep);
   ip46_address_is_ip4 (ip) ?
     hash_set (vxlan_gpe_main.vtep4, ip->ip4.as_u32, 1) :
-    hash_set_key_copy (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
+    hash_set_mem_alloc (&vxlan_gpe_main.vtep6, &ip->ip6, 1);
   return 1;
 }
 
@@ -425,7 +406,7 @@ vtep_addr_unref (ip46_address_t * ip)
     return *vtep;
   ip46_address_is_ip4 (ip) ?
     hash_unset (vxlan_gpe_main.vtep4, ip->ip4.as_u32) :
-    hash_unset_key_free (&vxlan_gpe_main.vtep6, &ip->ip6);
+    hash_unset_mem_free (&vxlan_gpe_main.vtep6, &ip->ip6);
   return 0;
 }
 
@@ -459,7 +440,7 @@ mcast_shared_add (ip46_address_t * remote,
     .mfib_entry_index = mfei,
   };
 
-  hash_set_key_copy (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
+  hash_set_mem_alloc (&vxlan_gpe_main.mcast_shared, remote, new_ep.as_u64);
 }
 
 static inline void
@@ -470,7 +451,7 @@ mcast_shared_remove (ip46_address_t * remote)
   adj_unlock (ep.mcast_adj_index);
   mfib_table_entry_delete_index (ep.mfib_entry_index, MFIB_SOURCE_VXLAN_GPE);
 
-  hash_unset_key_free (&vxlan_gpe_main.mcast_shared, remote);
+  hash_unset_mem_free (&vxlan_gpe_main.mcast_shared, remote);
 }
 
 static inline fib_protocol_t
@@ -535,15 +516,18 @@ int vnet_vxlan_gpe_add_del_tunnel
       memset (t, 0, sizeof (*t));
 
       /* copy from arg structure */
+/* *INDENT-OFF* */
 #define _(x) t->x = a->x;
       foreach_gpe_copy_field;
       if (!a->is_ip6)
        foreach_copy_ipv4
-       else
+      else
        foreach_copy_ipv6
 #undef _
-         if (!a->is_ip6)
-         t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
+/* *INDENT-ON* */
+
+      if (!a->is_ip6)
+       t->flags |= VXLAN_GPE_TUNNEL_IS_IPV4;
 
       if (!a->is_ip6)
        {
@@ -743,7 +727,7 @@ int vnet_vxlan_gpe_add_del_tunnel
       if (!is_ip6)
        hash_unset (ngm->vxlan4_gpe_tunnel_by_key, key4.as_u64);
       else
-       hash_unset_key_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
+       hash_unset_mem_free (&ngm->vxlan6_gpe_tunnel_by_key, &key6);
 
       if (!ip46_address_is_multicast (&t->remote))
        {
@@ -940,14 +924,17 @@ vxlan_gpe_add_del_tunnel_command_fn (vlib_main_t * vm,
   a->is_add = is_add;
   a->is_ip6 = ipv6_set;
 
+/* *INDENT-OFF* */
 #define _(x) a->x = x;
   foreach_gpe_copy_field;
   if (ipv4_set)
     foreach_copy_ipv4
-    else
+  else
     foreach_copy_ipv6
 #undef _
-      rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
+/* *INDENT-ON* */
+
+  rv = vnet_vxlan_gpe_add_del_tunnel (a, &sw_if_index);
 
   switch (rv)
     {
index a6e799f..5926062 100644 (file)
@@ -263,23 +263,6 @@ vxlan_decap_next_is_valid (vxlan_main_t * vxm, 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)
 {
@@ -290,7 +273,7 @@ vtep_addr_ref(ip46_address_t *ip)
                return ++(*vtep);
        ip46_address_is_ip4(ip) ?
                         hash_set (vxlan_main.vtep4, ip->ip4.as_u32, 1) :
-                        hash_set_key_copy (&vxlan_main.vtep6, &ip->ip6, 1);
+                        hash_set_mem_alloc (&vxlan_main.vtep6, &ip->ip6, 1);
        return 1;
 }
 
@@ -305,7 +288,7 @@ vtep_addr_unref(ip46_address_t *ip)
                return *vtep;
        ip46_address_is_ip4(ip) ?
                hash_unset (vxlan_main.vtep4, ip->ip4.as_u32) :
-               hash_unset_key_free (&vxlan_main.vtep6, &ip->ip6);
+               hash_unset_mem_free (&vxlan_main.vtep6, &ip->ip6);
        return 0;
 }
 
@@ -336,7 +319,7 @@ mcast_shared_add(ip46_address_t *dst,
         .mfib_entry_index = mfei,
     };
 
-    hash_set_key_copy (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
+    hash_set_mem_alloc (&vxlan_main.mcast_shared, dst, new_ep.as_u64);
 }
 
 static inline void
@@ -348,7 +331,7 @@ mcast_shared_remove(ip46_address_t *dst)
     mfib_table_entry_delete_index(ep.mfib_entry_index,
                                   MFIB_SOURCE_VXLAN);
 
-    hash_unset_key_free (&vxlan_main.mcast_shared, dst);
+    hash_unset_mem_free (&vxlan_main.mcast_shared, dst);
 }
 
 static inline fib_protocol_t
@@ -415,7 +398,8 @@ int vnet_vxlan_add_del_tunnel
 
       /* copy the key */
       if (is_ip6)
-        hash_set_key_copy (&vxm->vxlan6_tunnel_by_key, &key6, t - vxm->tunnels);
+        hash_set_mem_alloc (&vxm->vxlan6_tunnel_by_key, &key6, 
+                           t - vxm->tunnels);
       else
         hash_set (vxm->vxlan4_tunnel_by_key, key4.as_u64, t - vxm->tunnels);
 
@@ -588,7 +572,7 @@ int vnet_vxlan_add_del_tunnel
       if (!is_ip6)
         hash_unset (vxm->vxlan4_tunnel_by_key, key4.as_u64);
       else
-       hash_unset_key_free (&vxm->vxlan6_tunnel_by_key, &key6);
+       hash_unset_mem_free (&vxm->vxlan6_tunnel_by_key, &key6);
 
       if (!ip46_address_is_multicast(&t->dst))
         {
index 4db5a57..6206dd2 100644 (file)
@@ -273,12 +273,33 @@ uword hash_bytes (void *v);
 /* Public macro to set (key, value) for pointer key */
 #define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0)
 
+/* Public inline funcion allocate and copy key to use in hash for pointer key */
+always_inline void
+hash_set_mem_alloc (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);
+}
+
 /* Public macro to set (key, 0) for pointer key */
 #define hash_set1_mem(h,key)     hash_set3 ((h), pointer_to_uword (key), 0, 0)
 
 /* Public macro to unset (key, value) for pointer key */
 #define hash_unset_mem(h,key)    ((h) = _hash_unset ((h), pointer_to_uword (key),0))
 
+/* Public inline funcion to unset pointer key and then free the key memory */
+always_inline void
+hash_unset_mem_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);
+}
+
 /* internal routine to free a hash table */
 extern void *_hash_free (void *v);