NAT: update nat_show_config_reply API (VPP-1403)
[vpp.git] / src / plugins / nat / nat.c
index cdf05fd..bb82e92 100755 (executable)
@@ -190,8 +190,6 @@ nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
   clib_bihash_kv_8_8_t kv;
   nat_ed_ses_key_t ed_key;
   clib_bihash_kv_16_8_t ed_kv;
-  int i;
-  snat_address_t *a;
   snat_main_per_thread_data_t *tsm =
     vec_elt_at_index (sm->per_thread_data, thread_index);
 
@@ -271,18 +269,11 @@ nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
   /* Twice NAT address and port for external host */
   if (is_twice_nat_session (s))
     {
-      for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
-        {
-          key.protocol = s->in2out.protocol;
-          key.port = s->ext_host_nat_port;
-          a = sm->twice_nat_addresses + i;
-          if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
-            {
-              snat_free_outside_address_and_port (sm->twice_nat_addresses,
-                                                  thread_index, &key, i);
-              break;
-            }
-        }
+      key.protocol = s->in2out.protocol;
+      key.port = s->ext_host_nat_port;
+      key.addr.as_u32 = s->ext_host_nat_addr.as_u32;
+      snat_free_outside_address_and_port (sm->twice_nat_addresses,
+                                          thread_index, &key);
     }
 
   if (snat_is_session_static (s))
@@ -290,7 +281,7 @@ nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index)
 
   if (s->outside_address_index != ~0)
     snat_free_outside_address_and_port (sm->addresses, thread_index,
-                                        &s->out2in, s->outside_address_index);
+                                        &s->out2in);
 }
 
 snat_user_t *
@@ -328,6 +319,8 @@ nat_user_get_or_create (snat_main_t *sm, ip4_address_t *addr, u32 fib_index,
       /* add user */
       if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1))
         nat_log_warn ("user_hash keay add failed");
+
+      clib_warning("%U %d", format_ip4_address, addr, fib_index);
     }
   else
     {
@@ -1086,7 +1079,7 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
                       nat_free_session_data (sm, s, tsm - sm->per_thread_data);
                       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
 
-                      if (!addr_only)
+                      if (!addr_only && !sm->endpoint_dependent)
                         break;
                     }
                 }
@@ -1178,7 +1171,7 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
 
                       if (!addr_only)
                         {
-                          if ((s->out2in.addr.as_u32 != e_addr.as_u32) &&
+                          if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
                               (clib_net_to_host_u16 (s->out2in.port) != e_port))
                             continue;
                         }
@@ -1192,14 +1185,9 @@ int snat_add_static_mapping(ip4_address_t l_addr, ip4_address_t e_addr,
                       nat_free_session_data (sm, s, tsm - sm->per_thread_data);
                       nat44_delete_session (sm, s, tsm - sm->per_thread_data);
 
-                      if (!addr_only)
+                      if (!addr_only && !sm->endpoint_dependent)
                         break;
                     }
-                  if (addr_only && (u->nstaticsessions == 0) && (u->nsessions == 0))
-                    {
-                      pool_put (tsm->users, u);
-                      clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 0);
-                    }
                 }
             }
         }
@@ -1468,7 +1456,7 @@ int nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port,
                       if (!(is_lb_session (s)))
                         continue;
 
-                      if ((s->in2out.addr.as_u32 != local->addr.as_u32) &&
+                      if ((s->in2out.addr.as_u32 != local->addr.as_u32) ||
                           (clib_net_to_host_u16 (s->in2out.port) != local->port))
                         continue;
 
@@ -1707,7 +1695,7 @@ feature_set:
                       vnet_feature_enable_disable ("ip4-local",
                                                    "nat44-ed-hairpinning",
                                                    sw_if_index, 1, 0, 0);
-                    else
+                    else if (!sm->deterministic)
                       vnet_feature_enable_disable ("ip4-local",
                                                    "nat44-hairpinning",
                                                    sw_if_index, 1, 0, 0);
@@ -1724,7 +1712,7 @@ feature_set:
                       vnet_feature_enable_disable ("ip4-local",
                                                    "nat44-ed-hairpinning",
                                                    sw_if_index, 0, 0, 0);
-                    else
+                    else if (!sm->deterministic)
                       vnet_feature_enable_disable ("ip4-local",
                                                    "nat44-hairpinning",
                                                    sw_if_index, 0, 0, 0);
@@ -1770,7 +1758,7 @@ feature_set:
                 if (sm->endpoint_dependent)
                   vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
                                                sw_if_index, 0, 0, 0);
-                else
+                else if (!sm->deterministic)
                   vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
                                                sw_if_index, 0, 0, 0);
               }
@@ -1794,7 +1782,7 @@ feature_set:
       if (sm->endpoint_dependent)
         vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning",
                                      sw_if_index, 1, 0, 0);
-      else
+      else if (!sm->deterministic)
         vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning",
                                      sw_if_index, 1, 0, 0);
     }
@@ -2097,12 +2085,18 @@ VLIB_INIT_FUNCTION (snat_init);
 
 void snat_free_outside_address_and_port (snat_address_t * addresses,
                                          u32 thread_index,
-                                         snat_session_key_t * k,
-                                         u32 address_index)
+                                         snat_session_key_t * k)
 {
   snat_address_t *a;
+  u32 address_index;
   u16 port_host_byte_order = clib_net_to_host_u16 (k->port);
 
+  for (address_index = 0; address_index < vec_len (addresses); address_index++)
+    {
+      if (addresses[address_index].addr.as_u32 == k->addr.as_u32)
+        break;
+    }
+
   ASSERT (address_index < vec_len (addresses));
 
   a = addresses + address_index;
@@ -2818,6 +2812,18 @@ snat_config (vlib_main_t * vm, unformat_input_t * input)
                                  format_unformat_error, input);
     }
 
+  if (sm->deterministic && sm->endpoint_dependent)
+    return clib_error_return (
+      0, "deterministic and endpoint-dependent modes are mutually exclusive");
+
+  if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent))
+    return clib_error_return (
+      0, "static mapping only mode available only for simple nat");
+
+  if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent))
+    return clib_error_return (
+      0, "out2in dpo mode available only for simple nat");
+
   /* for show commands, etc. */
   sm->translation_buckets = translation_buckets;
   sm->translation_memory_size = translation_memory_size;