NAT64: free port when dynamic BIB deleted (VPP-1107)
[vpp.git] / src / plugins / nat / nat64_db.c
index da73cee..7ce28bc 100644 (file)
 #include <nat/nat64_db.h>
 
 int
-nat64_db_init (nat64_db_t * db)
+nat64_db_init (nat64_db_t * db, u32 bib_buckets, u32 bib_memory_size,
+              u32 st_buckets, u32 st_memory_size,
+              nat64_db_free_addr_port_function_t free_addr_port_cb)
 {
-  u32 bib_buckets = 1024;
-  u32 bib_memory_size = 128 << 20;
-  u32 st_buckets = 2048;
-  u32 st_memory_size = 256 << 20;
-
   clib_bihash_init_24_8 (&db->bib.in2out, "bib-in2out", bib_buckets,
                         bib_memory_size);
 
@@ -38,6 +35,7 @@ nat64_db_init (nat64_db_t * db)
   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;
 }
 
@@ -164,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);
 
@@ -529,6 +528,52 @@ nat64_db_st_entry_find (nat64_db_t * db, ip46_address_t * l_addr,
   return ste;
 }
 
+u32
+nat64_db_st_entry_get_index (nat64_db_t * db, nat64_db_st_entry_t * ste)
+{
+  nat64_db_st_entry_t *st;
+
+  switch (ip_proto_to_snat_proto (ste->proto))
+    {
+/* *INDENT-OFF* */
+#define _(N, i, n, s) \
+    case SNAT_PROTOCOL_##N: \
+      st = db->st._##n##_st; \
+      break;
+      foreach_snat_protocol
+#undef _
+/* *INDENT-ON* */
+    default:
+      st = db->st._unk_proto_st;
+      return (u32) ~ 0;
+    }
+
+  return ste - st;
+}
+
+nat64_db_st_entry_t *
+nat64_db_st_entry_by_index (nat64_db_t * db, u8 proto, u32 ste_index)
+{
+  nat64_db_st_entry_t *st;
+
+  switch (ip_proto_to_snat_proto (proto))
+    {
+/* *INDENT-OFF* */
+#define _(N, i, n, s) \
+    case SNAT_PROTOCOL_##N: \
+      st = db->st._##n##_st; \
+      break;
+      foreach_snat_protocol
+#undef _
+/* *INDENT-ON* */
+    default:
+      st = db->st._unk_proto_st;
+      break;
+    }
+
+  return pool_elt_at_index (st, ste_index);
+}
+
 void
 nad64_db_st_free_expired (nat64_db_t * db, u32 now)
 {