nat: simplify bihash buckets/mem config
[vpp.git] / src / plugins / nat / nat44_cli.c
index f613af0..18f51b1 100644 (file)
@@ -228,6 +228,7 @@ nat44_show_hash_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
   vlib_cli_output (vm, "%U",
                   format_bihash_8_8, &sm->static_mapping_by_external,
                   verbose);
+  vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed, verbose);
   vec_foreach_index (i, sm->per_thread_data)
   {
     tsm = vec_elt_at_index (sm->per_thread_data, i);
@@ -237,8 +238,6 @@ nat44_show_hash_commnad_fn (vlib_main_t * vm, unformat_input_t * input,
       {
        vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->in2out_ed,
                         verbose);
-       vlib_cli_output (vm, "%U", format_bihash_16_8, &tsm->out2in_ed,
-                        verbose);
       }
     else
       {
@@ -359,10 +358,7 @@ nat_set_mss_clamping_command_fn (vlib_main_t * vm, unformat_input_t * input,
       if (unformat (line_input, "disable"))
        sm->mss_clamping = 0;
       else if (unformat (line_input, "%d", &mss))
-       {
-         sm->mss_clamping = (u16) mss;
-         sm->mss_value_net = clib_host_to_net_u16 (sm->mss_clamping);
-       }
+       sm->mss_clamping = (u16) mss;
       else
        {
          error = clib_error_return (0, "unknown input '%U'",
@@ -647,8 +643,8 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
   if (sm->deterministic || !sm->endpoint_dependent)
     return clib_error_return (0, UNSUPPORTED_IN_DET_OR_NON_ED_MODE_STR);
 
-  // print session configuration values
-  vlib_cli_output (vm, "max translations: %u", sm->max_translations);
+  vlib_cli_output (vm, "max translations per thread: %u",
+                  sm->max_translations_per_thread);
   vlib_cli_output (vm, "max translations per user: %u",
                   sm->max_translations_per_user);
 
@@ -679,18 +675,18 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
             if (now >= sess_timeout_time)
               timed_out++;
 
-            switch (s->in2out.protocol)
+            switch (s->nat_proto)
               {
-              case SNAT_PROTOCOL_ICMP:
+              case NAT_PROTOCOL_ICMP:
                 icmp_sessions++;
                 break;
-              case SNAT_PROTOCOL_TCP:
+              case NAT_PROTOCOL_TCP:
                 tcp_sessions++;
                 if (s->state)
                   {
-                    if (s->tcp_close_timestamp)
+                    if (s->tcp_closed_timestamp)
                       {
-                        if (now >= s->tcp_close_timestamp)
+                        if (now >= s->tcp_closed_timestamp)
                           {
                             ++transitory_closed;
                           }
@@ -704,7 +700,7 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
                 else
                   established++;
                 break;
-              case SNAT_PROTOCOL_UDP:
+              case NAT_PROTOCOL_UDP:
               default:
                 udp_sessions++;
                 break;
@@ -725,18 +721,18 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
         if (now >= sess_timeout_time)
           timed_out++;
 
-        switch (s->in2out.protocol)
+        switch (s->nat_proto)
           {
-          case SNAT_PROTOCOL_ICMP:
+          case NAT_PROTOCOL_ICMP:
             icmp_sessions++;
             break;
-          case SNAT_PROTOCOL_TCP:
+          case NAT_PROTOCOL_TCP:
             tcp_sessions++;
             if (s->state)
               {
-                if (s->tcp_close_timestamp)
+                if (s->tcp_closed_timestamp)
                   {
-                    if (now >= s->tcp_close_timestamp)
+                    if (now >= s->tcp_closed_timestamp)
                       {
                         ++transitory_closed;
                       }
@@ -750,7 +746,7 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
             else
               established++;
             break;
-          case SNAT_PROTOCOL_UDP:
+          case NAT_PROTOCOL_UDP:
           default:
             udp_sessions++;
             break;
@@ -758,6 +754,31 @@ nat44_show_summary_command_fn (vlib_main_t * vm, unformat_input_t * input,
       }));
       /* *INDENT-ON* */
       count = pool_elts (tsm->sessions);
+      if (sm->endpoint_dependent)
+       {
+         dlist_elt_t *oldest_elt;
+         u32 oldest_index;
+#define _(n, d)                                                          \
+  oldest_index =                                                         \
+      clib_dlist_remove_head (tsm->lru_pool, tsm->n##_lru_head_index);   \
+  if (~0 != oldest_index)                                                \
+    {                                                                    \
+      oldest_elt = pool_elt_at_index (tsm->lru_pool, oldest_index);      \
+      s = pool_elt_at_index (tsm->sessions, oldest_elt->value);          \
+      sess_timeout_time =                                                \
+          s->last_heard + (f64)nat44_session_get_timeout (sm, s);        \
+      vlib_cli_output (vm, d " LRU min session timeout %llu (now %llu)", \
+                       sess_timeout_time, now);                          \
+      clib_dlist_addhead (tsm->lru_pool, tsm->n##_lru_head_index,        \
+                          oldest_index);                                 \
+    }
+         _(tcp_estab, "established tcp");
+         _(tcp_trans, "transitory tcp");
+         _(udp, "udp");
+         _(unk_proto, "unknown protocol");
+         _(icmp, "icmp");
+#undef _
+       }
     }
 
   vlib_cli_output (vm, "total timed out sessions: %u", timed_out);
@@ -796,7 +817,7 @@ nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
         vlib_cli_output (vm, "  tenant VRF independent");
     #define _(N, i, n, s) \
       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
-      foreach_snat_protocol
+      foreach_nat_protocol
     #undef _
     }
   vlib_cli_output (vm, "NAT44 twice-nat pool addresses:");
@@ -810,7 +831,7 @@ nat44_show_addresses_command_fn (vlib_main_t * vm, unformat_input_t * input,
         vlib_cli_output (vm, "  tenant VRF independent");
     #define _(N, i, n, s) \
       vlib_cli_output (vm, "  %d busy %s ports", ap->busy_##n##_ports, s);
-      foreach_snat_protocol
+      foreach_nat_protocol
     #undef _
     }
   /* *INDENT-ON* */
@@ -975,7 +996,7 @@ add_static_mapping_command_fn (vlib_main_t * vm,
   u32 sw_if_index = ~0;
   vnet_main_t *vnm = vnet_get_main ();
   int rv;
-  snat_protocol_t proto = ~0;
+  nat_protocol_t proto = NAT_PROTOCOL_OTHER;
   u8 proto_set = 0;
   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
   u8 out2in_only = 0;
@@ -1011,7 +1032,7 @@ add_static_mapping_command_fn (vlib_main_t * vm,
        ;
       else if (unformat (line_input, "vrf %u", &vrf_id))
        ;
-      else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
+      else if (unformat (line_input, "%U", unformat_nat_protocol, &proto))
        proto_set = 1;
       else if (unformat (line_input, "twice-nat"))
        twice_nat = TWICE_NAT;
@@ -1035,13 +1056,24 @@ add_static_mapping_command_fn (vlib_main_t * vm,
       goto done;
     }
 
-  if (!addr_only && !proto_set)
+  if (addr_only)
     {
-      error = clib_error_return (0, "missing protocol");
+      if (proto_set)
+       {
+         error =
+           clib_error_return (0,
+                              "address only mapping doesn't support protocol");
+         goto done;
+       }
+    }
+  else if (!proto_set)
+    {
+      error = clib_error_return (0, "protocol is required");
       goto done;
     }
 
-  rv = snat_add_static_mapping (l_addr, e_addr, (u16) l_port, (u16) e_port,
+  rv = snat_add_static_mapping (l_addr, e_addr, clib_host_to_net_u16 (l_port),
+                               clib_host_to_net_u16 (e_port),
                                vrf_id, addr_only, sw_if_index, proto, is_add,
                                twice_nat, out2in_only, 0, 0);
 
@@ -1092,7 +1124,7 @@ add_identity_mapping_command_fn (vlib_main_t * vm,
   u32 sw_if_index = ~0;
   vnet_main_t *vnm = vnet_get_main ();
   int rv;
-  snat_protocol_t proto;
+  nat_protocol_t proto;
 
   if (sm->deterministic)
     return clib_error_return (0, UNSUPPORTED_IN_DET_MODE_STR);
@@ -1112,7 +1144,7 @@ add_identity_mapping_command_fn (vlib_main_t * vm,
        ;
       else if (unformat (line_input, "vrf %u", &vrf_id))
        ;
-      else if (unformat (line_input, "%U %u", unformat_snat_protocol, &proto,
+      else if (unformat (line_input, "%U %u", unformat_nat_protocol, &proto,
                         &port))
        addr_only = 0;
       else if (unformat (line_input, "del"))
@@ -1125,9 +1157,10 @@ add_identity_mapping_command_fn (vlib_main_t * vm,
        }
     }
 
-  rv = snat_add_static_mapping (addr, addr, (u16) port, (u16) port,
-                               vrf_id, addr_only, sw_if_index, proto, is_add,
-                               0, 0, 0, 1);
+  rv =
+    snat_add_static_mapping (addr, addr, clib_host_to_net_u16 (port),
+                            clib_host_to_net_u16 (port), vrf_id, addr_only,
+                            sw_if_index, proto, is_add, 0, 0, 0, 1);
 
   switch (rv)
     {
@@ -1168,7 +1201,7 @@ add_lb_static_mapping_command_fn (vlib_main_t * vm,
   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0, affinity = 0;
   int is_add = 1;
   int rv;
-  snat_protocol_t proto;
+  nat_protocol_t proto;
   u8 proto_set = 0;
   nat44_lb_addr_port_t *locals = 0, local;
   twice_nat_type_t twice_nat = TWICE_NAT_DISABLED;
@@ -1206,7 +1239,7 @@ add_lb_static_mapping_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
                         &e_addr, &e_port))
        ;
-      else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
+      else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
                         &proto))
        proto_set = 1;
       else if (unformat (line_input, "twice-nat"))
@@ -1283,7 +1316,7 @@ add_lb_backend_command_fn (vlib_main_t * vm,
   u32 l_port = 0, e_port = 0, vrf_id = 0, probability = 0;
   int is_add = 1;
   int rv;
-  snat_protocol_t proto;
+  nat_protocol_t proto;
   u8 proto_set = 0;
 
   if (sm->deterministic)
@@ -1305,7 +1338,7 @@ add_lb_backend_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "external %U:%u", unformat_ip4_address,
                         &e_addr, &e_port))
        ;
-      else if (unformat (line_input, "protocol %U", unformat_snat_protocol,
+      else if (unformat (line_input, "protocol %U", unformat_nat_protocol,
                         &proto))
        proto_set = 1;
       else if (unformat (line_input, "del"))
@@ -1657,7 +1690,7 @@ nat44_del_session_command_fn (vlib_main_t * vm,
   clib_error_t *error = 0;
   ip4_address_t addr, eh_addr;
   u32 port = 0, eh_port = 0, vrf_id = sm->outside_vrf_id;
-  snat_protocol_t proto;
+  nat_protocol_t proto;
   int rv;
 
   if (sm->deterministic)
@@ -1671,7 +1704,7 @@ nat44_del_session_command_fn (vlib_main_t * vm,
     {
       if (unformat
          (line_input, "%U:%u %U", unformat_ip4_address, &addr, &port,
-          unformat_snat_protocol, &proto))
+          unformat_nat_protocol, &proto))
        ;
       else if (unformat (line_input, "in"))
        {
@@ -1700,10 +1733,13 @@ nat44_del_session_command_fn (vlib_main_t * vm,
 
   if (is_ed)
     rv =
-      nat44_del_ed_session (sm, &addr, port, &eh_addr, eh_port,
-                           snat_proto_to_ip_proto (proto), vrf_id, is_in);
+      nat44_del_ed_session (sm, &addr, clib_host_to_net_u16 (port), &eh_addr,
+                           clib_host_to_net_u16 (eh_port),
+                           nat_proto_to_ip_proto (proto), vrf_id, is_in);
   else
-    rv = nat44_del_session (sm, &addr, port, proto, vrf_id, is_in);
+    rv =
+      nat44_del_session (sm, &addr, clib_host_to_net_u16 (port), proto,
+                        vrf_id, is_in);
 
   switch (rv)
     {
@@ -2519,16 +2555,19 @@ VLIB_CLI_COMMAND (nat44_show_interfaces_command, static) = {
  *  vpp# nat44 add static mapping tcp local 10.0.0.3 6303 external 4.4.4.4 3606
  * If not runnig "static mapping only" NAT plugin mode use before:
  *  vpp# nat44 add address 4.4.4.4
- * To create static mapping between local and external address use:
+ * To create address only static mapping between local and external address use:
  *  vpp# nat44 add static mapping local 10.0.0.3 external 4.4.4.4
+ * To create ICMP static mapping between local and external with ICMP echo
+ * identifier 10 use:
+ *  vpp# nat44 add static mapping icmp local 10.0.0.3 10 external 4.4.4.4 10
  * @cliexend
 ?*/
 VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
   .path = "nat44 add static mapping",
   .function = add_static_mapping_command_fn,
   .short_help =
-    "nat44 add static mapping tcp|udp|icmp local <addr> [<port>] "
-    "external <addr> [<port>] [vrf <table-id>] [twice-nat|self-twice-nat] "
+    "nat44 add static mapping tcp|udp|icmp local <addr> [<port|icmp-echo-id>] "
+    "external <addr> [<port|icmp-echo-id>] [vrf <table-id>] [twice-nat|self-twice-nat] "
     "[out2in-only] [del]",
 };