- elt_index = elt->next;
- elt = pool_elt_at_index (sm->list_pool, elt_index);
- session_index = elt->value;
- }
- }
-
- return s;
-}
-
-u8 * format_snat_static_mapping (u8 * s, va_list * args)
-{
- snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
- nat44_lb_addr_port_t *local;
-
- if (m->addr_only)
- s = format (s, "local %U external %U vrf %d",
- format_ip4_address, &m->local_addr,
- format_ip4_address, &m->external_addr,
- m->vrf_id);
- else
- {
- if (vec_len (m->locals))
- {
- s = format (s, "%U vrf %d external %U:%d",
- format_snat_protocol, m->proto,
- m->vrf_id,
- format_ip4_address, &m->external_addr, m->external_port);
- vec_foreach (local, m->locals)
- s = format (s, "\n local %U:%d probability %d\%",
- format_ip4_address, &local->addr, local->port,
- local->probability);
- }
- else
- s = format (s, "%U local %U:%d external %U:%d vrf %d",
- format_snat_protocol, m->proto,
- format_ip4_address, &m->local_addr, m->local_port,
- format_ip4_address, &m->external_addr, m->external_port,
- m->vrf_id);
- }
- return s;
-}
-
-u8 * format_snat_static_map_to_resolve (u8 * s, va_list * args)
-{
- snat_static_map_resolve_t *m = va_arg (*args, snat_static_map_resolve_t *);
- vnet_main_t *vnm = vnet_get_main();
-
- if (m->addr_only)
- s = format (s, "local %U external %U vrf %d",
- format_ip4_address, &m->l_addr,
- format_vnet_sw_interface_name, vnm,
- vnet_get_sw_interface (vnm, m->sw_if_index),
- m->vrf_id);
- else
- s = format (s, "%U local %U:%d external %U:%d vrf %d",
- format_snat_protocol, m->proto,
- format_ip4_address, &m->l_addr, m->l_port,
- format_vnet_sw_interface_name, vnm,
- vnet_get_sw_interface (vnm, m->sw_if_index), m->e_port,
- m->vrf_id);
-
- return s;
-}
-
-u8 * format_det_map_ses (u8 * s, va_list * args)
-{
- snat_det_map_t * det_map = va_arg (*args, snat_det_map_t *);
- ip4_address_t in_addr, out_addr;
- u32 in_offset, out_offset;
- snat_det_session_t * ses = va_arg (*args, snat_det_session_t *);
- u32 * i = va_arg (*args, u32 *);
-
- u32 user_index = *i / SNAT_DET_SES_PER_USER;
- in_addr.as_u32 = clib_host_to_net_u32 (
- clib_net_to_host_u32(det_map->in_addr.as_u32) + user_index);
- in_offset = clib_net_to_host_u32(in_addr.as_u32) -
- clib_net_to_host_u32(det_map->in_addr.as_u32);
- out_offset = in_offset / det_map->sharing_ratio;
- out_addr.as_u32 = clib_host_to_net_u32(
- clib_net_to_host_u32(det_map->out_addr.as_u32) + out_offset);
- s = format (s, "in %U:%d out %U:%d external host %U:%d state: %U expire: %d\n",
- format_ip4_address, &in_addr,
- clib_net_to_host_u16 (ses->in_port),
- format_ip4_address, &out_addr,
- clib_net_to_host_u16 (ses->out.out_port),
- format_ip4_address, &ses->out.ext_host_addr,
- clib_net_to_host_u16 (ses->out.ext_host_port),
- format_snat_session_state, ses->state,
- ses->expire);
-
- return s;
-}
-
-static clib_error_t *
-show_snat_command_fn (vlib_main_t * vm,
- unformat_input_t * input,
- vlib_cli_command_t * cmd)
-{
- int verbose = 0;
- snat_main_t * sm = &snat_main;
- snat_user_t * u;
- snat_static_mapping_t *m;
- snat_interface_t *i;
- snat_address_t * ap;
- vnet_main_t *vnm = vnet_get_main();
- snat_main_per_thread_data_t *tsm;
- u32 users_num = 0, sessions_num = 0, *worker, *sw_if_index;
- uword j = 0;
- snat_static_map_resolve_t *rp;
- snat_det_map_t * dm;
- snat_det_session_t * ses;
-
- if (unformat (input, "detail"))
- verbose = 1;
- else if (unformat (input, "verbose"))
- verbose = 2;
-
- if (sm->static_mapping_only)
- {
- if (sm->static_mapping_connection_tracking)
- vlib_cli_output (vm, "NAT plugin mode: static mapping only connection "
- "tracking");
- else
- vlib_cli_output (vm, "NAT plugin mode: static mapping only");
- }
- else if (sm->deterministic)
- {
- vlib_cli_output (vm, "NAT plugin mode: deterministic mapping");
- }
- else
- {
- vlib_cli_output (vm, "NAT plugin mode: dynamic translations enabled");
- }
-
- if (verbose > 0)
- {
- pool_foreach (i, sm->interfaces,
- ({
- vlib_cli_output (vm, "%U %s", format_vnet_sw_interface_name, vnm,
- vnet_get_sw_interface (vnm, i->sw_if_index),
- i->is_inside ? "in" : "out");
- }));
-
- pool_foreach (i, sm->output_feature_interfaces,
- ({
- vlib_cli_output (vm, "%U output-feature %s",
- format_vnet_sw_interface_name, vnm,
- vnet_get_sw_interface (vnm, i->sw_if_index),
- i->is_inside ? "in" : "out");
- }));
-
- if (vec_len (sm->auto_add_sw_if_indices))
- {
- vlib_cli_output (vm, "NAT44 pool addresses interfaces:");
- vec_foreach (sw_if_index, sm->auto_add_sw_if_indices)
- {
- vlib_cli_output (vm, "%U", format_vnet_sw_interface_name, vnm,
- vnet_get_sw_interface (vnm, *sw_if_index));
- }
- }
-
- vec_foreach (ap, sm->addresses)
- {
- vlib_cli_output (vm, "%U", format_ip4_address, &ap->addr);
- if (ap->fib_index != ~0)
- vlib_cli_output (vm, " tenant VRF: %u",
- ip4_fib_get(ap->fib_index)->table_id);
- else
- 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
-#undef _
- }
- }
-
- if (sm->num_workers > 1)
- {
- vlib_cli_output (vm, "%d workers", vec_len (sm->workers));
- if (verbose > 0)
- {
- vec_foreach (worker, sm->workers)
- {
- vlib_worker_thread_t *w =
- vlib_worker_threads + *worker + sm->first_worker_index;
- vlib_cli_output (vm, " %s", w->name);
- }
- }
- }
-
- if (sm->deterministic)
- {
- vlib_cli_output (vm, "udp timeout: %dsec", sm->udp_timeout);
- vlib_cli_output (vm, "tcp-established timeout: %dsec",
- sm->tcp_established_timeout);
- vlib_cli_output (vm, "tcp-transitory timeout: %dsec",
- sm->tcp_transitory_timeout);
- vlib_cli_output (vm, "icmp timeout: %dsec", sm->icmp_timeout);
- vlib_cli_output (vm, "%d deterministic mappings",
- pool_elts (sm->det_maps));
- if (verbose > 0)
- {
- pool_foreach (dm, sm->det_maps,
- ({
- vlib_cli_output (vm, "in %U/%d out %U/%d\n",
- format_ip4_address, &dm->in_addr, dm->in_plen,
- format_ip4_address, &dm->out_addr, dm->out_plen);
- vlib_cli_output (vm, " outside address sharing ratio: %d\n",
- dm->sharing_ratio);
- vlib_cli_output (vm, " number of ports per inside host: %d\n",
- dm->ports_per_host);
- vlib_cli_output (vm, " sessions number: %d\n", dm->ses_num);
- if (verbose > 1)
- {
- vec_foreach_index (j, dm->sessions)
- {
- ses = vec_elt_at_index (dm->sessions, j);
- if (ses->in_port)
- vlib_cli_output (vm, " %U", format_det_map_ses, dm, ses,
- &j);
- }
- }
- }));
- }
- }
- else
- {
- if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))
- {
- vlib_cli_output (vm, "%d static mappings",
- pool_elts (sm->static_mappings));
-
- if (verbose > 0)
- {
- pool_foreach (m, sm->static_mappings,
- ({
- vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
- }));
- }
- }
- else
- {
- vec_foreach (tsm, sm->per_thread_data)
- {
- users_num += pool_elts (tsm->users);
- sessions_num += pool_elts (tsm->sessions);
- }
-
- vlib_cli_output (vm, "%d users, %d outside addresses, %d active sessions,"
- " %d static mappings",
- users_num,
- vec_len (sm->addresses),
- sessions_num,
- pool_elts (sm->static_mappings));
-
- if (verbose > 0)
- {
- vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->in2out,
- verbose - 1);
- vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->out2in,
- verbose - 1);
- vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->in2out_ed,
- verbose - 1);
- vlib_cli_output (vm, "%U", format_bihash_16_8, &sm->out2in_ed,
- verbose - 1);
- vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_in,
- verbose - 1);
- vlib_cli_output (vm, "%U", format_bihash_8_8, &sm->worker_by_out,
- verbose - 1);
- vec_foreach_index (j, sm->per_thread_data)
- {
- tsm = vec_elt_at_index (sm->per_thread_data, j);
-
- if (pool_elts (tsm->users) == 0)
- continue;
-
- vlib_worker_thread_t *w = vlib_worker_threads + j;
- vlib_cli_output (vm, "Thread %d (%s at lcore %u):", j, w->name,
- w->lcore_id);
- vlib_cli_output (vm, " %d list pool elements",
- pool_elts (tsm->list_pool));
-
- pool_foreach (u, tsm->users,
- ({
- vlib_cli_output (vm, " %U", format_snat_user, tsm, u,
- verbose - 1);
- }));
- }
-
- if (pool_elts (sm->static_mappings))
- {
- vlib_cli_output (vm, "static mappings:");
- pool_foreach (m, sm->static_mappings,
- ({
- vlib_cli_output (vm, "%U", format_snat_static_mapping, m);
- }));
- for (j = 0; j < vec_len (sm->to_resolve); j++)
- {
- rp = sm->to_resolve + j;
- vlib_cli_output (vm, "%U",
- format_snat_static_map_to_resolve, rp);
- }
- }
- }
- }
- }
- return 0;
-}
-
-VLIB_CLI_COMMAND (show_snat_command, static) = {
- .path = "show nat44",
- .short_help = "show nat44",
- .function = show_snat_command_fn,
-};
-
-
-static void
-snat_ip4_add_del_interface_address_cb (ip4_main_t * im,
- uword opaque,
- u32 sw_if_index,
- ip4_address_t * address,
- u32 address_length,
- u32 if_address_index,
- u32 is_delete)