NAT64: free port when dynamic BIB deleted (VPP-1107) 72/9872/2 v18.04-rc0
authorMatus Fabian <matfabia@cisco.com>
Tue, 19 Dec 2017 11:43:07 +0000 (03:43 -0800)
committerOle Trøan <otroan@employees.org>
Wed, 3 Jan 2018 11:44:40 +0000 (11:44 +0000)
Change-Id: Id897ed61a26a4069678ed4ddac1ba28bf32809c3
Signed-off-by: Matus Fabian <matfabia@cisco.com>
src/plugins/nat/nat64.c
src/plugins/nat/nat64.h
src/plugins/nat/nat64_cli.c
src/plugins/nat/nat64_db.c
src/plugins/nat/nat64_db.h
src/plugins/nat/nat64_in2out.c

index deeb071..e60d9ec 100644 (file)
@@ -241,6 +241,10 @@ nat64_init (vlib_main_t * vm)
   return 0;
 }
 
+static void nat64_free_out_addr_and_port (struct nat64_db_s *db,
+                                         ip4_address_t * addr, u16 port,
+                                         u8 protocol);
+
 void
 nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
                u32 st_memory_size)
@@ -257,7 +261,7 @@ nat64_set_hash (u32 bib_buckets, u32 bib_memory_size, u32 st_buckets,
   vec_foreach (db, nm->db)
     {
       if (nat64_db_init (db, bib_buckets, bib_memory_size, st_buckets,
-                         st_memory_size))
+                         st_memory_size, nat64_free_out_addr_and_port))
         clib_warning ("NAT64 DB init failed");
     }
   /* *INDENT-ON* */
@@ -310,16 +314,16 @@ nat64_add_del_pool_addr (ip4_address_t * addr, u32 vrf_id, u8 is_add)
       if (a->fib_index != ~0)
        fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP6,
                          FIB_SOURCE_PLUGIN_HI);
+      /* Delete sessions using address */
+        /* *INDENT-OFF* */
+        vec_foreach (db, nm->db)
+          nat64_db_free_out_addr (db, &a->addr);
 #define _(N, id, n, s) \
       clib_bitmap_free (a->busy_##n##_port_bitmap);
       foreach_snat_protocol
 #undef _
-       /* Delete sessions using address */
-        /* *INDENT-OFF* */
-        vec_foreach (db, nm->db)
-          nat64_db_free_out_addr (db, &a->addr);
         /* *INDENT-ON* */
-       vec_del1 (nm->addr_pool, i);
+      vec_del1 (nm->addr_pool, i);
     }
 
   /* Add/del external address to FIB */
@@ -510,13 +514,16 @@ nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
   return rv;
 }
 
-void
-nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
-                             snat_protocol_t proto, u32 thread_index)
+static void
+nat64_free_out_addr_and_port (struct nat64_db_s *db, ip4_address_t * addr,
+                             u16 port, u8 protocol)
 {
   nat64_main_t *nm = &nat64_main;
   int i;
   snat_address_t *a;
+  u32 thread_index = db - nm->db;
+  snat_protocol_t proto = ip_proto_to_snat_proto (protocol);
+  u16 port_host_byte_order = clib_net_to_host_u16 (port);
 
   for (i = 0; i < vec_len (nm->addr_pool); i++)
     {
@@ -528,7 +535,7 @@ nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
 #define _(N, j, n, s) \
         case SNAT_PROTOCOL_##N: \
           ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \
-                  port) == 1); \
+                  port_host_byte_order) == 1); \
           clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, port, 0); \
           a->busy_##n##_ports--; \
           a->busy_##n##_ports_per_thread[thread_index]--; \
@@ -691,10 +698,7 @@ nat64_add_del_static_bib_entry (ip6_address_t * in_addr,
        return VNET_API_ERROR_NO_SUCH_ENTRY;
 
       if (!nm->sm->num_workers)
-       {
-         nat64_free_out_addr_and_port (out_addr, out_port, p, thread_index);
-         nat64_db_bib_entry_free (db, bibe);
-       }
+       nat64_db_bib_entry_free (db, bibe);
     }
 
   if (nm->sm->num_workers)
index 0c8fd82..d9d3d76 100644 (file)
@@ -209,19 +209,6 @@ int nat64_alloc_out_addr_and_port (u32 fib_index, snat_protocol_t proto,
                                   ip4_address_t * addr, u16 * port,
                                   u32 thread_index);
 
-/**
- * @brief Free IPv4 address and port pair from NAT64 pool.
- *
- * @param addr         IPv4 address to free.
- * @param port         Port number to free.
- * @param proto        L4 protocol.
- * @param thread_index Thread index.
- *
- * @returns 0 on success, non-zero value otherwise.
- */
-void nat64_free_out_addr_and_port (ip4_address_t * addr, u16 port,
-                                  snat_protocol_t proto, u32 thread_index);
-
 /**
  * @brief Set UDP session timeout.
  *
index 22bfa41..d1acca9 100644 (file)
@@ -116,7 +116,11 @@ nat64_cli_pool_walk (snat_address_t * ap, void *ctx)
   else
     vlib_cli_output (vm, " %U", format_ip4_address, &ap->addr);
 
-  return 0;
+#define _(N, i, n, s) \
+  vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
+  foreach_snat_protocol
+#undef _
+    return 0;
 }
 
 static clib_error_t *
index 97da672..7ce28bc 100644 (file)
@@ -20,7 +20,8 @@
 
 int
 nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
-              u32 st_buckets, u32 st_memory_size)
+              u32 st_buckets, u32 st_memory_size,
+              nat64_db_free_addr_port_function_t free_addr_port_cb)
 {
   clib_bihash_init_24_8 (&db->bib.in2out, "bib-in2out", bib_buckets,
                         bib_memory_size);
@@ -34,6 +35,7 @@ nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
   clib_bihash_init_48_8 (&db->st.out2in, "st-out2in", st_buckets,
                         st_memory_size);
 
+  db->free_addr_port_cb = free_addr_port_cb;
   return 0;
 }
 
@@ -160,6 +162,7 @@ nat64_db_bib_entry_free (nat64_db_t * db, nat64_db_bib_entry_t * bibe)
   kv.key[2] = bibe_key.as_u64[2];
   clib_bihash_add_del_24_8 (&db->bib.out2in, &kv, 0);
 
+  db->free_addr_port_cb (db, &bibe->out_addr, bibe->out_port, bibe->proto);
   /* delete from pool */
   pool_put (bib, bibe);
 
index 7cda8b0..4345fd3 100644 (file)
@@ -117,10 +117,21 @@ typedef struct
   clib_bihash_48_8_t out2in;
 } nat64_db_st_t;
 
-typedef struct
+struct nat64_db_s;
+
+/**
+ * @brief Call back function to free NAT64 pool address and port when BIB
+ * entry is deleted.
+ */
+typedef void (*nat64_db_free_addr_port_function_t) (struct nat64_db_s * db,
+                                                   ip4_address_t * addr,
+                                                   u16 port, u8 proto);
+
+typedef struct nat64_db_s
 {
   nat64_db_bib_t bib;
   nat64_db_st_t st;
+  nat64_db_free_addr_port_function_t free_addr_port_cb;
 } nat64_db_t;
 
 /**
@@ -131,11 +142,13 @@ typedef struct
  * @param bib_memory_size Memory size of BIB hash.
  * @param st_buckets Number of session table hash buckets.
  * @param st_memory_size Memory size of session table hash.
+ * @param free_addr_port_cb Call back function to free address and port.
  *
  * @returns 0 on success, non-zero value otherwise.
  */
 int nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
-                  u32 st_buckets, u32 st_memory_size);
+                  u32 st_buckets, u32 st_memory_size,
+                  nat64_db_free_addr_port_function_t free_addr_port_cb);
 
 /**
  * @brief Create new NAT64 BIB entry.
index 7a0bf85..ef322d5 100644 (file)
@@ -190,8 +190,7 @@ nat64_in2out_tcp_udp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
 
          bibe =
            nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
-                                      sport, clib_host_to_net_u16 (out_port),
-                                      fib_index, proto, 0);
+                                      sport, out_port, fib_index, proto, 0);
          if (!bibe)
            return -1;
        }
@@ -279,8 +278,7 @@ nat64_in2out_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
 
              bibe =
                nat64_db_bib_entry_create (db, &ip6->src_address,
-                                          &out_addr, in_id,
-                                          clib_host_to_net_u16 (out_id),
+                                          &out_addr, in_id, out_id,
                                           fib_index, IP_PROTOCOL_ICMP, 0);
              if (!bibe)
                return -1;
@@ -604,8 +602,7 @@ nat64_in2out_tcp_udp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
 
          bibe =
            nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
-                                      sport, clib_host_to_net_u16 (out_port),
-                                      fib_index, proto, 0);
+                                      sport, out_port, fib_index, proto, 0);
          if (!bibe)
            return -1;
        }
@@ -1435,8 +1432,7 @@ nat64_in2out_reass_node_fn (vlib_main_t * vm,
                        nat64_db_bib_entry_create (db,
                                                   &ip60->src_address,
                                                   &out_addr0, udp0->src_port,
-                                                  clib_host_to_net_u16
-                                                  (out_port0), fib_index0,
+                                                  out_port0, fib_index0,
                                                   l4_protocol0, 0);
                      if (!bibe0)
                        {