diff options
author | Filip Varga <fivarga@cisco.com> | 2021-02-17 14:34:54 +0100 |
---|---|---|
committer | Ole Tr�an <otroan@employees.org> | 2021-02-24 19:25:25 +0000 |
commit | 0eaf4e6784efb2d058fe2f031578251b6bcc0aa8 (patch) | |
tree | cc0c2b485bfee51068fa4970acc9f95ec595e8e5 /src/plugins/nat/nat44-ei/nat44_ei.c | |
parent | 5db2f4a4312112ab57043ce88f10edc4acc141ec (diff) |
nat: Final NAT44 EI/ED split patch
This patch achieves complete separation of
endpoint-dependent and endpoint-independent IPv4 NAT
features. Some common stuff is also moved to NAT
library.
Type: refactor
Change-Id: I52468b7e2b5ac28958a2baf8e2ea01787322e801
Signed-off-by: Filip Varga <fivarga@cisco.com>
Diffstat (limited to 'src/plugins/nat/nat44-ei/nat44_ei.c')
-rw-r--r-- | src/plugins/nat/nat44-ei/nat44_ei.c | 2429 |
1 files changed, 2063 insertions, 366 deletions
diff --git a/src/plugins/nat/nat44-ei/nat44_ei.c b/src/plugins/nat/nat44-ei/nat44_ei.c index 02403d0cd99..fdf90708a09 100644 --- a/src/plugins/nat/nat44-ei/nat44_ei.c +++ b/src/plugins/nat/nat44-ei/nat44_ei.c @@ -15,42 +15,409 @@ * under the License. */ +#include <vnet/plugin/plugin.h> +#include <vpp/app/version.h> + #include <vnet/vnet.h> #include <vnet/ip/ip.h> #include <vnet/ip/ip4.h> -#include <vnet/plugin/plugin.h> -#include <nat/nat.h> -#include <nat/nat_dpo.h> -#include <nat/lib/ipfix_logging.h> -#include <nat/lib/nat_syslog.h> -#include <nat/nat_inlines.h> -#include <nat/nat44/inlines.h> -#include <nat/nat_affinity.h> +#include <vnet/ip/ip_table.h> +#include <vnet/ip/reass/ip4_sv_reass.h> #include <vnet/fib/fib_table.h> #include <vnet/fib/ip4_fib.h> -#include <vnet/ip/reass/ip4_sv_reass.h> -#include <vppinfra/bihash_16_8.h> -#include <nat/nat44/ed_inlines.h> -#include <vnet/ip/ip_table.h> +#include <vnet/plugin/plugin.h> +// nat lib +#include <nat/lib/log.h> +#include <nat/lib/nat_syslog.h> +#include <nat/lib/nat_inlines.h> +#include <nat/lib/ipfix_logging.h> + +#include <nat/nat44-ei/nat44_ei_dpo.h> #include <nat/nat44-ei/nat44_ei_inlines.h> #include <nat/nat44-ei/nat44_ei.h> nat44_ei_main_t nat44_ei_main; +extern vlib_node_registration_t nat44_ei_hairpinning_node; +extern vlib_node_registration_t nat44_ei_hairpin_dst_node; +extern vlib_node_registration_t + nat44_ei_in2out_hairpinning_finish_ip4_lookup_node; +extern vlib_node_registration_t + nat44_ei_in2out_hairpinning_finish_interface_output_node; + +#define skip_if_disabled() \ + do \ + { \ + nat44_ei_main_t *nm = &nat44_ei_main; \ + if (PREDICT_FALSE (!nm->enabled)) \ + return; \ + } \ + while (0) + +#define fail_if_enabled() \ + do \ + { \ + nat44_ei_main_t *nm = &nat44_ei_main; \ + if (PREDICT_FALSE (nm->enabled)) \ + { \ + nat44_ei_log_err ("plugin enabled"); \ + return 1; \ + } \ + } \ + while (0) + +#define fail_if_disabled() \ + do \ + { \ + nat44_ei_main_t *nm = &nat44_ei_main; \ + if (PREDICT_FALSE (!nm->enabled)) \ + { \ + nat44_ei_log_err ("plugin disabled"); \ + return 1; \ + } \ + } \ + while (0) + +/* Hook up input features */ +VNET_FEATURE_INIT (ip4_nat_classify, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-classify", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_in2out, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-in2out", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_out2in, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-out2in", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature", + "ip4-dhcp-client-detect"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output, static) = { + .arc_name = "ip4-output", + .node_name = "nat44-ei-in2out-output", + .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa", + "ip4-sv-reassembly-output-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_in2out_fast, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-in2out-fast", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_out2in_fast, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-out2in-fast", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature", + "ip4-dhcp-client-detect"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_dst, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-hairpin-dst", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-sv-reassembly-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_hairpin_src, static) = { + .arc_name = "ip4-output", + .node_name = "nat44-ei-hairpin-src", + .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa", + "ip4-sv-reassembly-output-feature"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_hairpinning, static) = { + .arc_name = "ip4-local", + .node_name = "nat44-ei-hairpinning", + .runs_before = VNET_FEATURES ("ip4-local-end-of-arc"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_in2out_worker_handoff, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-in2out-worker-handoff", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_out2in_worker_handoff, static) = { + .arc_name = "ip4-unicast", + .node_name = "nat44-ei-out2in-worker-handoff", + .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", + "ip4-dhcp-client-detect"), +}; +VNET_FEATURE_INIT (ip4_nat44_ei_in2out_output_worker_handoff, static) = { + .arc_name = "ip4-output", + .node_name = "nat44-ei-in2out-output-worker-handoff", + .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa", + "ip4-sv-reassembly-output-feature"), +}; + +VLIB_PLUGIN_REGISTER () = { + .version = VPP_BUILD_VER, + .description = "IPv4 Endpoint-Independent NAT (NAT44 EI)", +}; + +#define foreach_nat44_ei_classify_error \ + _ (NEXT_IN2OUT, "next in2out") \ + _ (NEXT_OUT2IN, "next out2in") \ + _ (FRAG_CACHED, "fragment cached") + +typedef enum +{ +#define _(sym, str) NAT44_EI_CLASSIFY_ERROR_##sym, + foreach_nat44_ei_classify_error +#undef _ + NAT44_EI_CLASSIFY_N_ERROR, +} nat44_ei_classify_error_t; + +static char *nat44_ei_classify_error_strings[] = { +#define _(sym, string) string, + foreach_nat44_ei_classify_error +#undef _ +}; + +typedef enum +{ + NAT44_EI_CLASSIFY_NEXT_IN2OUT, + NAT44_EI_CLASSIFY_NEXT_OUT2IN, + NAT44_EI_CLASSIFY_NEXT_DROP, + NAT44_EI_CLASSIFY_N_NEXT, +} nat44_ei_classify_next_t; + +typedef struct +{ + u8 next_in2out; + u8 cached; +} nat44_ei_classify_trace_t; + +void nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, + u32 sw_if_index, int is_add); + +static u8 * +format_nat44_ei_classify_trace (u8 *s, va_list *args) +{ + CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); + CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); + nat44_ei_classify_trace_t *t = va_arg (*args, nat44_ei_classify_trace_t *); + char *next; + + if (t->cached) + s = format (s, "nat44-ei-classify: fragment cached"); + else + { + next = t->next_in2out ? "nat44-ei-in2out" : "nat44-ei-out2in"; + s = format (s, "nat44-ei-classify: next %s", next); + } + + return s; +} + static void nat44_ei_db_free (); static void nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets); +static void nat44_ei_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); + +static void nat44_ei_ip4_add_del_addr_only_sm_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); + +static void nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, + u32 sw_if_index, u32 new_fib_index, + u32 old_fib_index); + +void +nat44_ei_set_node_indexes (nat44_ei_main_t *nm, vlib_main_t *vm) +{ + vlib_node_t *node; + node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-out2in"); + nm->out2in_node_index = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out"); + nm->in2out_node_index = node->index; + node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-in2out-output"); + nm->in2out_output_node_index = node->index; +} + int -nat44_ei_plugin_enable (nat44_ei_config_t c) +nat44_ei_set_workers (uword *bitmap) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + int i, j = 0; + + if (nm->num_workers < 2) + return VNET_API_ERROR_FEATURE_DISABLED; + + if (clib_bitmap_last_set (bitmap) >= nm->num_workers) + return VNET_API_ERROR_INVALID_WORKER; + + vec_free (nm->workers); + clib_bitmap_foreach (i, bitmap) + { + vec_add1 (nm->workers, i); + nm->per_thread_data[nm->first_worker_index + i].snat_thread_index = j; + nm->per_thread_data[nm->first_worker_index + i].thread_index = i; + j++; + } + + nm->port_per_thread = (0xffff - 1024) / _vec_len (nm->workers); + + return 0; +} + +#define nat_validate_simple_counter(c, i) \ + do \ + { \ + vlib_validate_simple_counter (&c, i); \ + vlib_zero_simple_counter (&c, i); \ + } \ + while (0); + +#define nat_init_simple_counter(c, n, sn) \ + do \ + { \ + c.name = n; \ + c.stat_segment_name = sn; \ + nat_validate_simple_counter (c, 0); \ + } \ + while (0); + +static_always_inline void +nat_validate_interface_counters (nat44_ei_main_t *nm, u32 sw_if_index) +{ +#define _(x) \ + nat_validate_simple_counter (nm->counters.fastpath.in2out.x, sw_if_index); \ + nat_validate_simple_counter (nm->counters.fastpath.out2in.x, sw_if_index); \ + nat_validate_simple_counter (nm->counters.slowpath.in2out.x, sw_if_index); \ + nat_validate_simple_counter (nm->counters.slowpath.out2in.x, sw_if_index); + foreach_nat_counter; +#undef _ + nat_validate_simple_counter (nm->counters.hairpinning, sw_if_index); +} + +clib_error_t * +nat44_ei_init (vlib_main_t *vm) { nat44_ei_main_t *nm = &nat44_ei_main; - snat_main_t *sm = &snat_main; + vlib_thread_main_t *tm = vlib_get_thread_main (); + vlib_thread_registration_t *tr; + ip4_add_del_interface_address_callback_t cbi = { 0 }; + ip4_table_bind_callback_t cbt = { 0 }; + u32 i, num_threads = 0; + uword *p, *bitmap = 0; clib_memset (nm, 0, sizeof (*nm)); + // required + nm->vnet_main = vnet_get_main (); + // convenience + nm->ip4_main = &ip4_main; + nm->api_main = vlibapi_get_main (); + nm->ip4_lookup_main = &ip4_main.lookup_main; + + // handoff stuff + nm->fq_out2in_index = ~0; + nm->fq_in2out_index = ~0; + nm->fq_in2out_output_index = ~0; + nm->worker_in2out_cb = nat44_ei_get_in2out_worker_index; + nm->worker_out2in_cb = nat44_ei_get_out2in_worker_index; + + nm->log_level = NAT_LOG_ERROR; + + nat44_ei_set_node_indexes (nm, vm); + nm->log_class = vlib_log_register_class ("nat44-ei", 0); + + nat_init_simple_counter (nm->total_users, "total-users", + "/nat44-ei/total-users"); + nat_init_simple_counter (nm->total_sessions, "total-sessions", + "/nat44-ei/total-sessions"); + nat_init_simple_counter (nm->user_limit_reached, "user-limit-reached", + "/nat44-ei/user-limit-reached"); + +#define _(x) \ + nat_init_simple_counter (nm->counters.fastpath.in2out.x, #x, \ + "/nat44-ei/in2out/fastpath/" #x); \ + nat_init_simple_counter (nm->counters.fastpath.out2in.x, #x, \ + "/nat44-ei/out2in/fastpath/" #x); \ + nat_init_simple_counter (nm->counters.slowpath.in2out.x, #x, \ + "/nat44-ei/in2out/slowpath/" #x); \ + nat_init_simple_counter (nm->counters.slowpath.out2in.x, #x, \ + "/nat44-ei/out2in/slowpath/" #x); + foreach_nat_counter; +#undef _ + nat_init_simple_counter (nm->counters.hairpinning, "hairpinning", + "/nat44-ei/hairpinning"); + + p = hash_get_mem (tm->thread_registrations_by_name, "workers"); + if (p) + { + tr = (vlib_thread_registration_t *) p[0]; + if (tr) + { + nm->num_workers = tr->count; + nm->first_worker_index = tr->first_index; + } + } + num_threads = tm->n_vlib_mains - 1; + nm->port_per_thread = 0xffff - 1024; + vec_validate (nm->per_thread_data, num_threads); + + /* Use all available workers by default */ + if (nm->num_workers > 1) + { + + for (i = 0; i < nm->num_workers; i++) + bitmap = clib_bitmap_set (bitmap, i, 1); + nat44_ei_set_workers (bitmap); + clib_bitmap_free (bitmap); + } + else + nm->per_thread_data[0].snat_thread_index = 0; + + /* callbacks to call when interface address changes. */ + cbi.function = nat44_ei_ip4_add_del_interface_address_cb; + vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi); + cbi.function = nat44_ei_ip4_add_del_addr_only_sm_cb; + vec_add1 (nm->ip4_main->add_del_interface_address_callbacks, cbi); + + /* callbacks to call when interface to table biding changes */ + cbt.function = nat44_ei_update_outside_fib; + vec_add1 (nm->ip4_main->table_bind_callbacks, cbt); + + nm->fib_src_low = fib_source_allocate ( + "nat44-ei-low", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE); + nm->fib_src_hi = fib_source_allocate ("nat44-ei-hi", FIB_SOURCE_PRIORITY_HI, + FIB_SOURCE_BH_SIMPLE); + + // used only by out2in-dpo feature + nat_dpo_module_init (); + nat_ha_init (vm, nm->num_workers, num_threads); + + nm->hairpinning_fq_index = + vlib_frame_queue_main_init (nat44_ei_hairpinning_node.index, 0); + nm->hairpin_dst_fq_index = + vlib_frame_queue_main_init (nat44_ei_hairpin_dst_node.index, 0); + nm->in2out_hairpinning_finish_ip4_lookup_node_fq_index = + vlib_frame_queue_main_init ( + nat44_ei_in2out_hairpinning_finish_ip4_lookup_node.index, 0); + nm->in2out_hairpinning_finish_interface_output_node_fq_index = + vlib_frame_queue_main_init ( + nat44_ei_in2out_hairpinning_finish_interface_output_node.index, 0); + return nat44_ei_api_hookup (vm); +} + +VLIB_INIT_FUNCTION (nat44_ei_init); + +int +nat44_ei_plugin_enable (nat44_ei_config_t c) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + + fail_if_enabled (); + if (!c.users) c.users = 1024; @@ -59,68 +426,449 @@ nat44_ei_plugin_enable (nat44_ei_config_t c) nm->rconfig = c; + if (!nm->frame_queue_nelts) + nm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT; + nm->translations = c.sessions; nm->translation_buckets = nat_calc_bihash_buckets (c.sessions); nm->user_buckets = nat_calc_bihash_buckets (c.users); - // OBSOLETE - - sm->static_mapping_only = c.static_mapping_only; - sm->static_mapping_connection_tracking = c.connection_tracking; - sm->out2in_dpo = c.out2in_dpo; - sm->forwarding_enabled = 0; - sm->mss_clamping = 0; - sm->pat = (!c.static_mapping_only || + nm->pat = (!c.static_mapping_only || (c.static_mapping_only && c.connection_tracking)); - sm->max_users_per_thread = c.users; - sm->max_translations_per_thread = c.sessions; - sm->translation_buckets = nat_calc_bihash_buckets (c.sessions); - sm->max_translations_per_user = - c.user_sessions ? c.user_sessions : sm->max_translations_per_thread; + nm->static_mapping_only = c.static_mapping_only; + nm->static_mapping_connection_tracking = c.connection_tracking; + nm->out2in_dpo = c.out2in_dpo; + nm->forwarding_enabled = 0; + nm->mss_clamping = 0; - sm->inside_vrf_id = c.inside_vrf; - sm->inside_fib_index = fib_table_find_or_create_and_lock ( - FIB_PROTOCOL_IP4, c.inside_vrf, sm->fib_src_hi); + nm->max_users_per_thread = c.users; + nm->max_translations_per_thread = c.sessions; + nm->max_translations_per_user = + c.user_sessions ? c.user_sessions : nm->max_translations_per_thread; - sm->outside_vrf_id = c.outside_vrf; - sm->outside_fib_index = fib_table_find_or_create_and_lock ( - FIB_PROTOCOL_IP4, c.outside_vrf, sm->fib_src_hi); + nm->inside_vrf_id = c.inside_vrf; + nm->inside_fib_index = fib_table_find_or_create_and_lock ( + FIB_PROTOCOL_IP4, c.inside_vrf, nm->fib_src_hi); - sm->worker_in2out_cb = nat44_ei_get_in2out_worker_index; - sm->worker_out2in_cb = nat44_ei_get_out2in_worker_index; + nm->outside_vrf_id = c.outside_vrf; + nm->outside_fib_index = fib_table_find_or_create_and_lock ( + FIB_PROTOCOL_IP4, c.outside_vrf, nm->fib_src_hi); - sm->in2out_node_index = sm->ei_in2out_node_index; - sm->out2in_node_index = sm->ei_out2in_node_index; + nat_reset_timeouts (&nm->timeouts); + nat44_ei_db_init (nm->translations, nm->translation_buckets, + nm->user_buckets); + nat44_ei_set_alloc_default (); - sm->in2out_output_node_index = sm->ei_in2out_output_node_index; + // TODO: zero simple counter for all counters missing - if (sm->pat) + vlib_zero_simple_counter (&nm->total_users, 0); + vlib_zero_simple_counter (&nm->total_sessions, 0); + vlib_zero_simple_counter (&nm->user_limit_reached, 0); + + nat_ha_enable (); + nm->enabled = 1; + + return 0; +} + +void +nat44_ei_addresses_free (nat44_ei_address_t **addresses) +{ + nat44_ei_address_t *ap; + vec_foreach (ap, *addresses) { - sm->icmp_match_in2out_cb = icmp_match_in2out_slow; - sm->icmp_match_out2in_cb = icmp_match_out2in_slow; +#define _(N, i, n, s) vec_free (ap->busy_##n##_ports_per_thread); + foreach_nat_protocol +#undef _ + } + vec_free (*addresses); + *addresses = 0; +} + +int +nat44_ei_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) +{ + const char *feature_name, *del_feature_name; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_interface_t *i; + nat44_ei_address_t *ap; + nat44_ei_static_mapping_t *m; + nat44_ei_outside_fib_t *outside_fib; + u32 fib_index = + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index); + + fail_if_disabled (); + + if (nm->out2in_dpo && !is_inside) + { + nat44_ei_log_err ("error unsupported"); + return VNET_API_ERROR_UNSUPPORTED; + } + + pool_foreach (i, nm->output_feature_interfaces) + { + if (i->sw_if_index == sw_if_index) + { + nat44_ei_log_err ("error interface already configured"); + return VNET_API_ERROR_VALUE_EXIST; + } } + + if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking)) + feature_name = is_inside ? "nat44-ei-in2out-fast" : "nat44-ei-out2in-fast"; else { - sm->icmp_match_in2out_cb = icmp_match_in2out_fast; - sm->icmp_match_out2in_cb = icmp_match_out2in_fast; + if (nm->num_workers > 1) + feature_name = is_inside ? "nat44-ei-in2out-worker-handoff" : + "nat44-ei-out2in-worker-handoff"; + else + feature_name = is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in"; } - nat_reset_timeouts (&sm->timeouts); - nat44_ei_db_init (nm->translations, nm->translation_buckets, - nm->user_buckets); - nat44_ei_set_alloc_default (); - nat_ha_enable (); + if (nm->fq_in2out_index == ~0 && nm->num_workers > 1) + nm->fq_in2out_index = vlib_frame_queue_main_init (nm->in2out_node_index, + nm->frame_queue_nelts); - // TODO: function for reset counters - vlib_zero_simple_counter (&sm->total_users, 0); - vlib_zero_simple_counter (&sm->total_sessions, 0); - vlib_zero_simple_counter (&sm->user_limit_reached, 0); + if (nm->fq_out2in_index == ~0 && nm->num_workers > 1) + nm->fq_out2in_index = vlib_frame_queue_main_init (nm->out2in_node_index, + nm->frame_queue_nelts); - if (!sm->frame_queue_nelts) - sm->frame_queue_nelts = NAT_FQ_NELTS_DEFAULT; + if (!is_inside) + { + vec_foreach (outside_fib, nm->outside_fibs) + { + if (outside_fib->fib_index == fib_index) + { + if (is_del) + { + outside_fib->refcount--; + if (!outside_fib->refcount) + vec_del1 (nm->outside_fibs, + outside_fib - nm->outside_fibs); + } + else + outside_fib->refcount++; + goto feature_set; + } + } + if (!is_del) + { + vec_add2 (nm->outside_fibs, outside_fib, 1); + outside_fib->refcount = 1; + outside_fib->fib_index = fib_index; + } + } - sm->enabled = 1; +feature_set: + pool_foreach (i, nm->interfaces) + { + if (i->sw_if_index == sw_if_index) + { + if (is_del) + { + if (nat44_ei_interface_is_inside (i) && + nat44_ei_interface_is_outside (i)) + { + if (is_inside) + i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_INSIDE; + else + i->flags &= ~NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE; + + if (nm->num_workers > 1) + { + del_feature_name = "nat44-handoff-classify"; + feature_name = !is_inside ? + "nat44-ei-in2out-worker-handoff" : + "nat44-ei-out2in-worker-handoff"; + } + else + { + del_feature_name = "nat44-ei-classify"; + feature_name = + !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in"; + } + + int rv = + ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", del_feature_name, + sw_if_index, 0, 0, 0); + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 1, 0, 0); + if (!is_inside) + { + vnet_feature_enable_disable ("ip4-local", + "nat44-ei-hairpinning", + sw_if_index, 1, 0, 0); + } + } + else + { + int rv = + ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 0); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 0, 0, 0); + pool_put (nm->interfaces, i); + if (is_inside) + { + vnet_feature_enable_disable ("ip4-local", + "nat44-ei-hairpinning", + sw_if_index, 0, 0, 0); + } + } + } + else + { + if ((nat44_ei_interface_is_inside (i) && is_inside) || + (nat44_ei_interface_is_outside (i) && !is_inside)) + return 0; + + if (nm->num_workers > 1) + { + del_feature_name = !is_inside ? + "nat44-ei-in2out-worker-handoff" : + "nat44-ei-out2in-worker-handoff"; + feature_name = "nat44-handoff-classify"; + } + else + { + del_feature_name = + !is_inside ? "nat44-ei-in2out" : "nat44-ei-out2in"; + feature_name = "nat44-ei-classify"; + } + + int rv = + ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", del_feature_name, + sw_if_index, 0, 0, 0); + vnet_feature_enable_disable ("ip4-unicast", feature_name, + sw_if_index, 1, 0, 0); + if (!is_inside) + { + vnet_feature_enable_disable ( + "ip4-local", "nat44-ei-hairpinning", sw_if_index, 0, 0, 0); + } + goto set_flags; + } + + goto fib; + } + } + + if (is_del) + { + nat44_ei_log_err ("error interface couldn't be found"); + return VNET_API_ERROR_NO_SUCH_ENTRY; + } + + pool_get (nm->interfaces, i); + i->sw_if_index = sw_if_index; + i->flags = 0; + nat_validate_interface_counters (nm, sw_if_index); + + vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, + 0); + + int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, 1); + if (rv) + return rv; + + if (is_inside && !nm->out2in_dpo) + { + vnet_feature_enable_disable ("ip4-local", "nat44-ei-hairpinning", + sw_if_index, 1, 0, 0); + } + +set_flags: + if (is_inside) + { + i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE; + return 0; + } + else + i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE; + + /* Add/delete external addresses to FIB */ +fib: + vec_foreach (ap, nm->addresses) + nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del); + + pool_foreach (m, nm->static_mappings) + { + if (!(nat44_ei_is_addr_only_static_mapping (m)) || + (m->local_addr.as_u32 == m->external_addr.as_u32)) + continue; + + nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, + !is_del); + } + + return 0; +} + +int +nat44_ei_interface_add_del_output_feature (u32 sw_if_index, u8 is_inside, + int is_del) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_interface_t *i; + nat44_ei_address_t *ap; + nat44_ei_static_mapping_t *m; + nat44_ei_outside_fib_t *outside_fib; + u32 fib_index = + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, sw_if_index); + + fail_if_disabled (); + + if (nm->static_mapping_only && !(nm->static_mapping_connection_tracking)) + { + nat44_ei_log_err ("error unsupported"); + return VNET_API_ERROR_UNSUPPORTED; + } + + pool_foreach (i, nm->interfaces) + { + if (i->sw_if_index == sw_if_index) + { + nat44_ei_log_err ("error interface already configured"); + return VNET_API_ERROR_VALUE_EXIST; + } + } + + if (!is_inside) + { + vec_foreach (outside_fib, nm->outside_fibs) + { + if (outside_fib->fib_index == fib_index) + { + if (is_del) + { + outside_fib->refcount--; + if (!outside_fib->refcount) + vec_del1 (nm->outside_fibs, + outside_fib - nm->outside_fibs); + } + else + outside_fib->refcount++; + goto feature_set; + } + } + if (!is_del) + { + vec_add2 (nm->outside_fibs, outside_fib, 1); + outside_fib->refcount = 1; + outside_fib->fib_index = fib_index; + } + } + +feature_set: + if (is_inside) + { + int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + rv = + ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-hairpin-dst", + sw_if_index, !is_del, 0, 0); + vnet_feature_enable_disable ("ip4-output", "nat44-ei-hairpin-src", + sw_if_index, !is_del, 0, 0); + goto fq; + } + + if (nm->num_workers > 1) + { + int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + rv = + ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", + "nat44-ei-out2in-worker-handoff", + sw_if_index, !is_del, 0, 0); + vnet_feature_enable_disable ("ip4-output", + "nat44-ei-in2out-output-worker-handoff", + sw_if_index, !is_del, 0, 0); + } + else + { + int rv = ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + rv = + ip4_sv_reass_output_enable_disable_with_refcnt (sw_if_index, !is_del); + if (rv) + return rv; + vnet_feature_enable_disable ("ip4-unicast", "nat44-ei-out2in", + sw_if_index, !is_del, 0, 0); + vnet_feature_enable_disable ("ip4-output", "nat44-ei-in2out-output", + sw_if_index, !is_del, 0, 0); + } + +fq: + if (nm->fq_in2out_output_index == ~0 && nm->num_workers > 1) + nm->fq_in2out_output_index = + vlib_frame_queue_main_init (nm->in2out_output_node_index, 0); + + if (nm->fq_out2in_index == ~0 && nm->num_workers > 1) + nm->fq_out2in_index = + vlib_frame_queue_main_init (nm->out2in_node_index, 0); + + pool_foreach (i, nm->output_feature_interfaces) + { + if (i->sw_if_index == sw_if_index) + { + if (is_del) + pool_put (nm->output_feature_interfaces, i); + else + return VNET_API_ERROR_VALUE_EXIST; + + goto fib; + } + } + + if (is_del) + { + nat44_ei_log_err ("error interface couldn't be found"); + return VNET_API_ERROR_NO_SUCH_ENTRY; + } + + pool_get (nm->output_feature_interfaces, i); + i->sw_if_index = sw_if_index; + i->flags = 0; + nat_validate_interface_counters (nm, sw_if_index); + if (is_inside) + i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE; + else + i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE; + + /* Add/delete external addresses to FIB */ +fib: + if (is_inside) + return 0; + + vec_foreach (ap, nm->addresses) + nat44_ei_add_del_addr_to_fib (&ap->addr, 32, sw_if_index, !is_del); + + pool_foreach (m, nm->static_mappings) + { + if (!((nat44_ei_is_addr_only_static_mapping (m))) || + (m->local_addr.as_u32 == m->external_addr.as_u32)) + continue; + + nat44_ei_add_del_addr_to_fib (&m->external_addr, 32, sw_if_index, + !is_del); + } return 0; } @@ -129,81 +877,347 @@ int nat44_ei_plugin_disable () { nat44_ei_main_t *nm = &nat44_ei_main; - snat_main_t *sm = &snat_main; - snat_interface_t *i, *vec; + nat44_ei_interface_t *i, *vec; int error = 0; // first unregister all nodes from interfaces - vec = vec_dup (sm->interfaces); + vec = vec_dup (nm->interfaces); vec_foreach (i, vec) { - if (nat_interface_is_inside (i)) - error = snat_interface_add_del (i->sw_if_index, 1, 1); - if (nat_interface_is_outside (i)) - error = snat_interface_add_del (i->sw_if_index, 0, 1); + if (nat44_ei_interface_is_inside (i)) + error = nat44_ei_interface_add_del (i->sw_if_index, 1, 1); + if (nat44_ei_interface_is_outside (i)) + error = nat44_ei_interface_add_del (i->sw_if_index, 0, 1); if (error) { - nat_log_err ("error occurred while removing interface %u", - i->sw_if_index); + nat44_ei_log_err ("error occurred while removing interface %u", + i->sw_if_index); } } vec_free (vec); - sm->interfaces = 0; + nm->interfaces = 0; - vec = vec_dup (sm->output_feature_interfaces); + vec = vec_dup (nm->output_feature_interfaces); vec_foreach (i, vec) { - if (nat_interface_is_inside (i)) - error = snat_interface_add_del_output_feature (i->sw_if_index, 1, 1); - if (nat_interface_is_outside (i)) - error = snat_interface_add_del_output_feature (i->sw_if_index, 0, 1); + if (nat44_ei_interface_is_inside (i)) + error = + nat44_ei_interface_add_del_output_feature (i->sw_if_index, 1, 1); + if (nat44_ei_interface_is_outside (i)) + error = + nat44_ei_interface_add_del_output_feature (i->sw_if_index, 0, 1); if (error) { - nat_log_err ("error occurred while removing interface %u", - i->sw_if_index); + nat44_ei_log_err ("error occurred while removing interface %u", + i->sw_if_index); } } vec_free (vec); - sm->output_feature_interfaces = 0; + nm->output_feature_interfaces = 0; nat_ha_disable (); nat44_ei_db_free (); - nat44_addresses_free (&sm->addresses); - nat44_addresses_free (&sm->twice_nat_addresses); + nat44_ei_addresses_free (&nm->addresses); - vec_free (sm->to_resolve); - vec_free (sm->auto_add_sw_if_indices); - vec_free (sm->auto_add_sw_if_indices_twice_nat); + vec_free (nm->to_resolve); + vec_free (nm->auto_add_sw_if_indices); - sm->to_resolve = 0; - sm->auto_add_sw_if_indices = 0; - sm->auto_add_sw_if_indices_twice_nat = 0; + nm->to_resolve = 0; + nm->auto_add_sw_if_indices = 0; - sm->forwarding_enabled = 0; + nm->forwarding_enabled = 0; - sm->enabled = 0; + nm->enabled = 0; clib_memset (&nm->rconfig, 0, sizeof (nm->rconfig)); - clib_memset (&sm->rconfig, 0, sizeof (sm->rconfig)); return error; } +int +nat44_ei_set_outside_address_and_port (nat44_ei_address_t *addresses, + u32 thread_index, ip4_address_t addr, + u16 port, nat_protocol_t protocol) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_address_t *a = 0; + u32 address_index; + u16 port_host_byte_order = clib_net_to_host_u16 (port); + + for (address_index = 0; address_index < vec_len (addresses); address_index++) + { + if (addresses[address_index].addr.as_u32 != addr.as_u32) + continue; + + a = addresses + address_index; + switch (protocol) + { +#define _(N, j, n, s) \ + case NAT_PROTOCOL_##N: \ + if (a->busy_##n##_port_refcounts[port_host_byte_order]) \ + return VNET_API_ERROR_INSTANCE_IN_USE; \ + ++a->busy_##n##_port_refcounts[port_host_byte_order]; \ + a->busy_##n##_ports_per_thread[thread_index]++; \ + a->busy_##n##_ports++; \ + return 0; + foreach_nat_protocol +#undef _ + default : nat_elog_info (nm, "unknown protocol"); + return 1; + } + } + + return VNET_API_ERROR_NO_SUCH_ENTRY; +} + +void +nat44_ei_add_del_address_dpo (ip4_address_t addr, u8 is_add) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + dpo_id_t dpo_v4 = DPO_INVALID; + fib_prefix_t pfx = { + .fp_proto = FIB_PROTOCOL_IP4, + .fp_len = 32, + .fp_addr.ip4.as_u32 = addr.as_u32, + }; + + if (is_add) + { + nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4); + fib_table_entry_special_dpo_add (0, &pfx, nm->fib_src_hi, + FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4); + dpo_reset (&dpo_v4); + } + else + { + fib_table_entry_special_remove (0, &pfx, nm->fib_src_hi); + } +} + +void +nat44_ei_free_outside_address_and_port (nat44_ei_address_t *addresses, + u32 thread_index, ip4_address_t *addr, + u16 port, nat_protocol_t protocol) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_address_t *a; + u32 address_index; + u16 port_host_byte_order = clib_net_to_host_u16 (port); + + for (address_index = 0; address_index < vec_len (addresses); address_index++) + { + if (addresses[address_index].addr.as_u32 == addr->as_u32) + break; + } + + ASSERT (address_index < vec_len (addresses)); + + a = addresses + address_index; + + switch (protocol) + { +#define _(N, i, n, s) \ + case NAT_PROTOCOL_##N: \ + ASSERT (a->busy_##n##_port_refcounts[port_host_byte_order] >= 1); \ + --a->busy_##n##_port_refcounts[port_host_byte_order]; \ + a->busy_##n##_ports--; \ + a->busy_##n##_ports_per_thread[thread_index]--; \ + break; + foreach_nat_protocol +#undef _ + default : nat_elog_info (nm, "unknown protocol"); + return; + } +} + +void +nat44_ei_free_session_data_v2 (nat44_ei_main_t *nm, nat44_ei_session_t *s, + u32 thread_index, u8 is_ha) +{ + clib_bihash_kv_8_8_t kv; + + /* session lookup tables */ + init_nat_i2o_k (&kv, s); + if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0)) + nat_elog_warn (nm, "in2out key del failed"); + init_nat_o2i_k (&kv, s); + if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0)) + nat_elog_warn (nm, "out2in key del failed"); + + if (!is_ha) + nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index, + &s->in2out.addr, s->in2out.port, &s->out2in.addr, + s->out2in.port, s->nat_proto); + + if (nat44_ei_is_unk_proto_session (s)) + return; + + if (!is_ha) + { + /* log NAT event */ + nat_ipfix_logging_nat44_ses_delete ( + thread_index, s->in2out.addr.as_u32, s->out2in.addr.as_u32, + s->nat_proto, s->in2out.port, s->out2in.port, s->in2out.fib_index); + + nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr, + s->ext_host_port, s->nat_proto, s->out2in.fib_index, + thread_index); + } + + if (nat44_ei_is_session_static (s)) + return; + + nat44_ei_free_outside_address_and_port (nm->addresses, thread_index, + &s->out2in.addr, s->out2in.port, + s->nat_proto); +} + +nat44_ei_user_t * +nat44_ei_user_get_or_create (nat44_ei_main_t *nm, ip4_address_t *addr, + u32 fib_index, u32 thread_index) +{ + nat44_ei_user_t *u = 0; + nat44_ei_user_key_t user_key; + clib_bihash_kv_8_8_t kv, value; + nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index]; + dlist_elt_t *per_user_list_head_elt; + + user_key.addr.as_u32 = addr->as_u32; + user_key.fib_index = fib_index; + kv.key = user_key.as_u64; + + /* Ever heard of the "user" = src ip4 address before? */ + if (clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) + { + if (pool_elts (tnm->users) >= nm->max_users_per_thread) + { + vlib_increment_simple_counter (&nm->user_limit_reached, thread_index, + 0, 1); + nat_elog_warn (nm, "maximum user limit reached"); + return NULL; + } + /* no, make a new one */ + pool_get (tnm->users, u); + clib_memset (u, 0, sizeof (*u)); + + u->addr.as_u32 = addr->as_u32; + u->fib_index = fib_index; + + pool_get (tnm->list_pool, per_user_list_head_elt); + + u->sessions_per_user_list_head_index = + per_user_list_head_elt - tnm->list_pool; + + clib_dlist_init (tnm->list_pool, u->sessions_per_user_list_head_index); + + kv.value = u - tnm->users; + + /* add user */ + if (clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 1)) + { + nat_elog_warn (nm, "user_hash key add failed"); + nat44_ei_delete_user_with_no_session (nm, u, thread_index); + return NULL; + } + + vlib_set_simple_counter (&nm->total_users, thread_index, 0, + pool_elts (tnm->users)); + } + else + { + u = pool_elt_at_index (tnm->users, value.value); + } + + return u; +} + +nat44_ei_session_t * +nat44_ei_session_alloc_or_recycle (nat44_ei_main_t *nm, nat44_ei_user_t *u, + u32 thread_index, f64 now) +{ + nat44_ei_session_t *s; + nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index]; + u32 oldest_per_user_translation_list_index, session_index; + dlist_elt_t *oldest_per_user_translation_list_elt; + dlist_elt_t *per_user_translation_list_elt; + + /* Over quota? Recycle the least recently used translation */ + if ((u->nsessions + u->nstaticsessions) >= nm->max_translations_per_user) + { + oldest_per_user_translation_list_index = clib_dlist_remove_head ( + tnm->list_pool, u->sessions_per_user_list_head_index); + + ASSERT (oldest_per_user_translation_list_index != ~0); + + /* Add it back to the end of the LRU list */ + clib_dlist_addtail (tnm->list_pool, u->sessions_per_user_list_head_index, + oldest_per_user_translation_list_index); + /* Get the list element */ + oldest_per_user_translation_list_elt = pool_elt_at_index ( + tnm->list_pool, oldest_per_user_translation_list_index); + + /* Get the session index from the list element */ + session_index = oldest_per_user_translation_list_elt->value; + + /* Get the session */ + s = pool_elt_at_index (tnm->sessions, session_index); + + nat44_ei_free_session_data_v2 (nm, s, thread_index, 0); + if (nat44_ei_is_session_static (s)) + u->nstaticsessions--; + else + u->nsessions--; + s->flags = 0; + s->total_bytes = 0; + s->total_pkts = 0; + s->state = 0; + s->ext_host_addr.as_u32 = 0; + s->ext_host_port = 0; + s->ext_host_nat_addr.as_u32 = 0; + s->ext_host_nat_port = 0; + } + else + { + pool_get (tnm->sessions, s); + clib_memset (s, 0, sizeof (*s)); + + /* Create list elts */ + pool_get (tnm->list_pool, per_user_translation_list_elt); + clib_dlist_init (tnm->list_pool, + per_user_translation_list_elt - tnm->list_pool); + + per_user_translation_list_elt->value = s - tnm->sessions; + s->per_user_index = per_user_translation_list_elt - tnm->list_pool; + s->per_user_list_head_index = u->sessions_per_user_list_head_index; + + clib_dlist_addtail (tnm->list_pool, s->per_user_list_head_index, + per_user_translation_list_elt - tnm->list_pool); + + s->user_index = u - tnm->users; + vlib_set_simple_counter (&nm->total_sessions, thread_index, 0, + pool_elts (tnm->sessions)); + } + + s->ha_last_refreshed = now; + + return s; +} + void -nat44_ei_free_session_data (snat_main_t *sm, snat_session_t *s, +nat44_ei_free_session_data (nat44_ei_main_t *nm, nat44_ei_session_t *s, u32 thread_index, u8 is_ha) { clib_bihash_kv_8_8_t kv; init_nat_i2o_k (&kv, s); - if (clib_bihash_add_del_8_8 (&sm->in2out, &kv, 0)) - nat_elog_warn ("in2out key del failed"); + if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 0)) + nat_elog_warn (nm, "in2out key del failed"); init_nat_o2i_k (&kv, s); - if (clib_bihash_add_del_8_8 (&sm->out2in, &kv, 0)) - nat_elog_warn ("out2in key del failed"); + if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 0)) + nat_elog_warn (nm, "out2in key del failed"); if (!is_ha) { @@ -220,36 +1234,36 @@ nat44_ei_free_session_data (snat_main_t *sm, snat_session_t *s, thread_index); } - if (snat_is_session_static (s)) + if (nat44_ei_is_session_static (s)) return; - snat_free_outside_address_and_port (sm->addresses, thread_index, - &s->out2in.addr, s->out2in.port, - s->nat_proto); + nat44_ei_free_outside_address_and_port (nm->addresses, thread_index, + &s->out2in.addr, s->out2in.port, + s->nat_proto); } static_always_inline void -nat44_ei_user_del_sessions (snat_user_t *u, u32 thread_index) +nat44_ei_user_del_sessions (nat44_ei_user_t *u, u32 thread_index) { dlist_elt_t *elt; - snat_session_t *s; + nat44_ei_session_t *s; - snat_main_t *sm = &snat_main; - snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index]; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index]; // get head elt = - pool_elt_at_index (tsm->list_pool, u->sessions_per_user_list_head_index); + pool_elt_at_index (tnm->list_pool, u->sessions_per_user_list_head_index); // get first element - elt = pool_elt_at_index (tsm->list_pool, elt->next); + elt = pool_elt_at_index (tnm->list_pool, elt->next); while (elt->value != ~0) { - s = pool_elt_at_index (tsm->sessions, elt->value); - elt = pool_elt_at_index (tsm->list_pool, elt->next); + s = pool_elt_at_index (tnm->sessions, elt->value); + elt = pool_elt_at_index (tnm->list_pool, elt->next); - nat44_ei_free_session_data (sm, s, thread_index, 0); - nat44_delete_session (sm, s, thread_index); + nat44_ei_free_session_data (nm, s, thread_index, 0); + nat44_ei_delete_session (nm, s, thread_index); } } @@ -258,28 +1272,25 @@ nat44_ei_user_del (ip4_address_t *addr, u32 fib_index) { int rv = 1; - snat_main_t *sm = &snat_main; - snat_main_per_thread_data_t *tsm; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_main_per_thread_data_t *tnm; - snat_user_key_t user_key; + nat44_ei_user_key_t user_key; clib_bihash_kv_8_8_t kv, value; - if (sm->endpoint_dependent) - return rv; - user_key.addr.as_u32 = addr->as_u32; user_key.fib_index = fib_index; kv.key = user_key.as_u64; - if (sm->num_workers > 1) + if (nm->num_workers > 1) { - vec_foreach (tsm, sm->per_thread_data) + vec_foreach (tnm, nm->per_thread_data) { - if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) + if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) { nat44_ei_user_del_sessions ( - pool_elt_at_index (tsm->users, value.value), - tsm->thread_index); + pool_elt_at_index (tnm->users, value.value), + tnm->thread_index); rv = 0; break; } @@ -287,11 +1298,11 @@ nat44_ei_user_del (ip4_address_t *addr, u32 fib_index) } else { - tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); - if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) + tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers); + if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) { nat44_ei_user_del_sessions ( - pool_elt_at_index (tsm->users, value.value), tsm->thread_index); + pool_elt_at_index (tnm->users, value.value), tnm->thread_index); rv = 0; } } @@ -299,34 +1310,34 @@ nat44_ei_user_del (ip4_address_t *addr, u32 fib_index) } void -nat44_ei_static_mapping_del_sessions (snat_main_t *sm, - snat_main_per_thread_data_t *tsm, - snat_user_key_t u_key, int addr_only, +nat44_ei_static_mapping_del_sessions (nat44_ei_main_t *nm, + nat44_ei_main_per_thread_data_t *tnm, + nat44_ei_user_key_t u_key, int addr_only, ip4_address_t e_addr, u16 e_port) { clib_bihash_kv_8_8_t kv, value; kv.key = u_key.as_u64; u64 user_index; dlist_elt_t *head, *elt; - snat_user_t *u; - snat_session_t *s; + nat44_ei_user_t *u; + nat44_ei_session_t *s; u32 elt_index, head_index, ses_index; - if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) + if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) { user_index = value.value; - u = pool_elt_at_index (tsm->users, user_index); + u = pool_elt_at_index (tnm->users, user_index); if (u->nstaticsessions) { head_index = u->sessions_per_user_list_head_index; - head = pool_elt_at_index (tsm->list_pool, head_index); + head = pool_elt_at_index (tnm->list_pool, head_index); elt_index = head->next; - elt = pool_elt_at_index (tsm->list_pool, elt_index); + elt = pool_elt_at_index (tnm->list_pool, elt_index); ses_index = elt->value; while (ses_index != ~0) { - s = pool_elt_at_index (tsm->sessions, ses_index); - elt = pool_elt_at_index (tsm->list_pool, elt->next); + s = pool_elt_at_index (tnm->sessions, ses_index); + elt = pool_elt_at_index (tnm->list_pool, elt->next); ses_index = elt->value; if (!addr_only) @@ -336,14 +1347,12 @@ nat44_ei_static_mapping_del_sessions (snat_main_t *sm, continue; } - if (is_lb_session (s)) + if (!nat44_ei_is_session_static (s)) continue; - if (!snat_is_session_static (s)) - continue; - - nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0); - nat44_delete_session (sm, s, tsm - sm->per_thread_data); + nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, + 0); + nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data); if (!addr_only) break; @@ -356,18 +1365,18 @@ u32 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0, u8 is_output) { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; u32 next_worker_index = 0; u32 hash; - next_worker_index = sm->first_worker_index; + next_worker_index = nm->first_worker_index; hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) + (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24); - if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers)))) - next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)]; + if (PREDICT_TRUE (is_pow2 (_vec_len (nm->workers)))) + next_worker_index += nm->workers[hash & (_vec_len (nm->workers) - 1)]; else - next_worker_index += sm->workers[hash % _vec_len (sm->workers)]; + next_worker_index += nm->workers[hash % _vec_len (nm->workers)]; return next_worker_index; } @@ -376,22 +1385,22 @@ u32 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0, u32 rx_fib_index0, u8 is_output) { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; udp_header_t *udp; u16 port; clib_bihash_kv_8_8_t kv, value; - snat_static_mapping_t *m; + nat44_ei_static_mapping_t *m; u32 proto; u32 next_worker_index = 0; /* first try static mappings without port */ - if (PREDICT_FALSE (pool_elts (sm->static_mappings))) + if (PREDICT_FALSE (pool_elts (nm->static_mappings))) { init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0); - if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) { - m = pool_elt_at_index (sm->static_mappings, value.value); + m = pool_elt_at_index (nm->static_mappings, value.value); return m->workers[0]; } } @@ -438,33 +1447,34 @@ nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0, } /* try static mappings with port */ - if (PREDICT_FALSE (pool_elts (sm->static_mappings))) + if (PREDICT_FALSE (pool_elts (nm->static_mappings))) { init_nat_k (&kv, ip0->dst_address, port, rx_fib_index0, proto); - if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) { - m = pool_elt_at_index (sm->static_mappings, value.value); + m = pool_elt_at_index (nm->static_mappings, value.value); return m->workers[0]; } } /* worker by outside port */ - next_worker_index = sm->first_worker_index; + next_worker_index = nm->first_worker_index; next_worker_index += - sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread]; + nm->workers[(clib_net_to_host_u16 (port) - 1024) / nm->port_per_thread]; return next_worker_index; } static int -nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, +nat44_ei_alloc_default_cb (nat44_ei_address_t *addresses, u32 fib_index, u32 thread_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread, u32 snat_thread_index) { - int i; - snat_address_t *a, *ga = 0; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_address_t *a, *ga = 0; u32 portnum; + int i; for (i = 0; i < vec_len (addresses); i++) { @@ -480,7 +1490,9 @@ nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, while (1) \ { \ portnum = (port_per_thread * snat_thread_index) + \ - snat_random_port (0, port_per_thread - 1) + 1024; \ + nat_random_port (&nm->random_seed, 0, \ + port_per_thread - 1) + \ + 1024; \ if (a->busy_##n##_port_refcounts[portnum]) \ continue; \ --a->busy_##n##_port_refcounts[portnum]; \ @@ -499,7 +1511,7 @@ nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, break; foreach_nat_protocol #undef _ - default : nat_elog_info ("unknown protocol"); + default : nat_elog_info (nm, "unknown protocol"); return 1; } } @@ -513,8 +1525,9 @@ nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, case NAT_PROTOCOL_##N: \ while (1) \ { \ - portnum = (port_per_thread * snat_thread_index) + \ - snat_random_port (0, port_per_thread - 1) + 1024; \ + portnum = \ + (port_per_thread * snat_thread_index) + \ + nat_random_port (&nm->random_seed, 0, port_per_thread - 1) + 1024; \ if (a->busy_##n##_port_refcounts[portnum]) \ continue; \ ++a->busy_##n##_port_refcounts[portnum]; \ @@ -527,7 +1540,7 @@ nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, break; foreach_nat_protocol #undef _ - default : nat_elog_info ("unknown protocol"); + default : nat_elog_info (nm, "unknown protocol"); return 1; } } @@ -538,16 +1551,16 @@ nat44_ei_alloc_default_cb (snat_address_t *addresses, u32 fib_index, } static int -nat44_ei_alloc_range_cb (snat_address_t *addresses, u32 fib_index, +nat44_ei_alloc_range_cb (nat44_ei_address_t *addresses, u32 fib_index, u32 thread_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread, u32 snat_thread_index) { - snat_main_t *sm = &snat_main; - snat_address_t *a = addresses; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_address_t *a = addresses; u16 portnum, ports; - ports = sm->end_port - sm->start_port + 1; + ports = nm->end_port - nm->start_port + 1; if (!vec_len (addresses)) goto exhausted; @@ -560,7 +1573,8 @@ nat44_ei_alloc_range_cb (snat_address_t *addresses, u32 fib_index, { \ while (1) \ { \ - portnum = snat_random_port (sm->start_port, sm->end_port); \ + portnum = nat_random_port (&nm->random_seed, nm->start_port, \ + nm->end_port); \ if (a->busy_##n##_port_refcounts[portnum]) \ continue; \ ++a->busy_##n##_port_refcounts[portnum]; \ @@ -573,7 +1587,7 @@ nat44_ei_alloc_range_cb (snat_address_t *addresses, u32 fib_index, break; foreach_nat_protocol #undef _ - default : nat_elog_info ("unknown protocol"); + default : nat_elog_info (nm, "unknown protocol"); return 1; } @@ -584,16 +1598,16 @@ exhausted: } static int -nat44_ei_alloc_mape_cb (snat_address_t *addresses, u32 fib_index, +nat44_ei_alloc_mape_cb (nat44_ei_address_t *addresses, u32 fib_index, u32 thread_index, nat_protocol_t proto, ip4_address_t *addr, u16 *port, u16 port_per_thread, u32 snat_thread_index) { - snat_main_t *sm = &snat_main; - snat_address_t *a = addresses; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_address_t *a = addresses; u16 m, ports, portnum, A, j; - m = 16 - (sm->psid_offset + sm->psid_length); - ports = (1 << (16 - sm->psid_length)) - (1 << m); + m = 16 - (nm->psid_offset + nm->psid_length); + ports = (1 << (16 - nm->psid_length)) - (1 << m); if (!vec_len (addresses)) goto exhausted; @@ -606,9 +1620,10 @@ nat44_ei_alloc_mape_cb (snat_address_t *addresses, u32 fib_index, { \ while (1) \ { \ - A = snat_random_port (1, pow2_mask (sm->psid_offset)); \ - j = snat_random_port (0, pow2_mask (m)); \ - portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \ + A = nat_random_port (&nm->random_seed, 1, \ + pow2_mask (nm->psid_offset)); \ + j = nat_random_port (&nm->random_seed, 0, pow2_mask (m)); \ + portnum = A | (nm->psid << nm->psid_offset) | (j << (16 - m)); \ if (a->busy_##n##_port_refcounts[portnum]) \ continue; \ ++a->busy_##n##_port_refcounts[portnum]; \ @@ -621,7 +1636,7 @@ nat44_ei_alloc_mape_cb (snat_address_t *addresses, u32 fib_index, break; foreach_nat_protocol #undef _ - default : nat_elog_info ("unknown protocol"); + default : nat_elog_info (nm, "unknown protocol"); return 1; } @@ -634,33 +1649,33 @@ exhausted: void nat44_ei_set_alloc_default () { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; - sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT; - sm->alloc_addr_and_port = nat44_ei_alloc_default_cb; + nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT; + nm->alloc_addr_and_port = nat44_ei_alloc_default_cb; } void nat44_ei_set_alloc_range (u16 start_port, u16 end_port) { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; - sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE; - sm->alloc_addr_and_port = nat44_ei_alloc_range_cb; - sm->start_port = start_port; - sm->end_port = end_port; + nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE; + nm->alloc_addr_and_port = nat44_ei_alloc_range_cb; + nm->start_port = start_port; + nm->end_port = end_port; } void nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length) { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; - sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE; - sm->alloc_addr_and_port = nat44_ei_alloc_mape_cb; - sm->psid = psid; - sm->psid_offset = psid_offset; - sm->psid_length = psid_length; + nm->addr_and_port_alloc_alg = NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE; + nm->alloc_addr_and_port = nat44_ei_alloc_mape_cb; + nm->psid = psid; + nm->psid_offset = psid_offset; + nm->psid_length = psid_length; } static void @@ -670,10 +1685,10 @@ nat44_ei_add_static_mapping_when_resolved (ip4_address_t l_addr, u16 l_port, int addr_only, int identity_nat, u8 *tag) { - snat_main_t *sm = &snat_main; - snat_static_map_resolve_t *rp; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_map_resolve_t *rp; - vec_add2 (sm->to_resolve, rp, 1); + vec_add2 (nm->to_resolve, rp, 1); clib_memset (rp, 0, sizeof (*rp)); rp->l_addr.as_u32 = l_addr.as_u32; @@ -687,65 +1702,131 @@ nat44_ei_add_static_mapping_when_resolved (ip4_address_t l_addr, u16 l_port, rp->tag = vec_dup (tag); } +void +nat44_ei_delete_session (nat44_ei_main_t *nm, nat44_ei_session_t *ses, + u32 thread_index) +{ + nat44_ei_main_per_thread_data_t *tnm = + vec_elt_at_index (nm->per_thread_data, thread_index); + clib_bihash_kv_8_8_t kv, value; + nat44_ei_user_t *u; + const nat44_ei_user_key_t u_key = { .addr = ses->in2out.addr, + .fib_index = ses->in2out.fib_index }; + const u8 u_static = nat44_ei_is_session_static (ses); + + clib_dlist_remove (tnm->list_pool, ses->per_user_index); + pool_put_index (tnm->list_pool, ses->per_user_index); + + pool_put (tnm->sessions, ses); + vlib_set_simple_counter (&nm->total_sessions, thread_index, 0, + pool_elts (tnm->sessions)); + + kv.key = u_key.as_u64; + if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) + { + u = pool_elt_at_index (tnm->users, value.value); + if (u_static) + u->nstaticsessions--; + else + u->nsessions--; + + nat44_ei_delete_user_with_no_session (nm, u, thread_index); + } +} + int -nat44_ei_del_session (snat_main_t *sm, ip4_address_t *addr, u16 port, +nat44_ei_del_session (nat44_ei_main_t *nm, ip4_address_t *addr, u16 port, nat_protocol_t proto, u32 vrf_id, int is_in) { - snat_main_per_thread_data_t *tsm; + nat44_ei_main_per_thread_data_t *tnm; clib_bihash_kv_8_8_t kv, value; ip4_header_t ip; u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id); - snat_session_t *s; + nat44_ei_session_t *s; clib_bihash_8_8_t *t; - if (sm->endpoint_dependent) - return VNET_API_ERROR_UNSUPPORTED; - ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32; - if (sm->num_workers > 1) - tsm = vec_elt_at_index (sm->per_thread_data, - sm->worker_in2out_cb (&ip, fib_index, 0)); + if (nm->num_workers > 1) + tnm = vec_elt_at_index (nm->per_thread_data, + nm->worker_in2out_cb (&ip, fib_index, 0)); else - tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); + tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers); init_nat_k (&kv, *addr, port, fib_index, proto); - t = is_in ? &sm->in2out : &sm->out2in; + t = is_in ? &nm->in2out : &nm->out2in; if (!clib_bihash_search_8_8 (t, &kv, &value)) { - if (pool_is_free_index (tsm->sessions, value.value)) + if (pool_is_free_index (tnm->sessions, value.value)) return VNET_API_ERROR_UNSPECIFIED; - s = pool_elt_at_index (tsm->sessions, value.value); - nat_free_session_data (sm, s, tsm - sm->per_thread_data, 0); - nat44_delete_session (sm, s, tsm - sm->per_thread_data); + s = pool_elt_at_index (tnm->sessions, value.value); + nat44_ei_free_session_data_v2 (nm, s, tnm - nm->per_thread_data, 0); + nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data); return 0; } return VNET_API_ERROR_NO_SUCH_ENTRY; } +u32 +nat44_ei_get_thread_idx_by_port (u16 e_port) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + u32 thread_idx = nm->num_workers; + if (nm->num_workers > 1) + { + thread_idx = nm->first_worker_index + + nm->workers[(e_port - 1024) / nm->port_per_thread]; + } + return thread_idx; +} + +void +nat44_ei_add_del_addr_to_fib (ip4_address_t *addr, u8 p_len, u32 sw_if_index, + int is_add) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + fib_prefix_t prefix = { + .fp_len = p_len, + .fp_proto = FIB_PROTOCOL_IP4, + .fp_addr = { + .ip4.as_u32 = addr->as_u32, + }, + }; + u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index); + + if (is_add) + fib_table_entry_update_one_path ( + fib_index, &prefix, nm->fib_src_low, + (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL | + FIB_ENTRY_FLAG_EXCLUSIVE), + DPO_PROTO_IP4, NULL, sw_if_index, ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); + else + fib_table_entry_delete (fib_index, &prefix, nm->fib_src_low); +} + int nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, u16 l_port, u16 e_port, nat_protocol_t proto, u32 sw_if_index, u32 vrf_id, u8 addr_only, u8 identity_nat, u8 *tag, u8 is_add) { - snat_main_t *sm = &snat_main; - snat_static_mapping_t *m = 0; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_mapping_t *m = 0; clib_bihash_kv_8_8_t kv, value; - snat_address_t *a = 0; + nat44_ei_address_t *a = 0; u32 fib_index = ~0; - snat_interface_t *interface; - snat_main_per_thread_data_t *tsm; - snat_user_key_t u_key; - snat_user_t *u; + nat44_ei_interface_t *interface; + nat44_ei_main_per_thread_data_t *tnm; + nat44_ei_user_key_t u_key; + nat44_ei_user_t *u; dlist_elt_t *head, *elt; u32 elt_index, head_index; u32 ses_index; u64 user_index; - snat_session_t *s; - snat_static_map_resolve_t *rp, *rp_match = 0; - nat44_lb_addr_port_t *local; + nat44_ei_session_t *s; + nat44_ei_static_map_resolve_t *rp, *rp_match = 0; + nat44_ei_lb_addr_port_t *local; u32 find = ~0; int i; @@ -753,9 +1834,9 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, { ip4_address_t *first_int_addr; - for (i = 0; i < vec_len (sm->to_resolve); i++) + for (i = 0; i < vec_len (nm->to_resolve); i++) { - rp = sm->to_resolve + i; + rp = nm->to_resolve + i; if (rp->sw_if_index != sw_if_index || rp->l_addr.as_u32 != l_addr.as_u32 || rp->vrf_id != vrf_id || rp->addr_only != addr_only) @@ -774,7 +1855,7 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, /* Might be already set... */ first_int_addr = ip4_interface_first_address ( - sm->ip4_main, sw_if_index, 0 /* just want the address */); + nm->ip4_main, sw_if_index, 0 /* just want the address */); if (is_add) { @@ -799,7 +1880,7 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (!rp_match) return VNET_API_ERROR_NO_SUCH_ENTRY; - vec_del1 (sm->to_resolve, i); + vec_del1 (nm->to_resolve, i); if (!first_int_addr) return 0; @@ -812,15 +1893,15 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, } init_nat_k (&kv, e_addr, addr_only ? 0 : e_port, 0, addr_only ? 0 : proto); - if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) - m = pool_elt_at_index (sm->static_mappings, value.value); + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) + m = pool_elt_at_index (nm->static_mappings, value.value); if (is_add) { if (m) { // identity mapping for second vrf - if (is_identity_static_mapping (m)) + if (nat44_ei_is_identity_static_mapping (m)) { pool_foreach (local, m->locals) { @@ -830,10 +1911,10 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, pool_get (m->locals, local); local->vrf_id = vrf_id; local->fib_index = fib_table_find_or_create_and_lock ( - FIB_PROTOCOL_IP4, vrf_id, sm->fib_src_low); + FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low); init_nat_kv (&kv, m->local_addr, m->local_port, local->fib_index, - m->proto, 0, m - sm->static_mappings); - clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1); + m->proto, 0, m - nm->static_mappings); + clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1); return 0; } return VNET_API_ERROR_VALUE_EXIST; @@ -843,34 +1924,34 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (vrf_id != ~0) { fib_index = fib_table_find_or_create_and_lock ( - FIB_PROTOCOL_IP4, vrf_id, sm->fib_src_low); + FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low); } /* If not specified use inside VRF id from NAT44 plugin config */ else { - fib_index = sm->inside_fib_index; - vrf_id = sm->inside_vrf_id; - fib_table_lock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low); + fib_index = nm->inside_fib_index; + vrf_id = nm->inside_vrf_id; + fib_table_lock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low); } if (!identity_nat) { init_nat_k (&kv, l_addr, addr_only ? 0 : l_port, fib_index, addr_only ? 0 : proto); - if (!clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value)) return VNET_API_ERROR_VALUE_EXIST; } /* Find external address in allocated addresses and reserve port for address and port pair mapping when dynamic translations enabled */ - if (!(addr_only || sm->static_mapping_only)) + if (!(addr_only || nm->static_mapping_only)) { - for (i = 0; i < vec_len (sm->addresses); i++) + for (i = 0; i < vec_len (nm->addresses); i++) { - if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) + if (nm->addresses[i].addr.as_u32 == e_addr.as_u32) { - a = sm->addresses + i; + a = nm->addresses + i; /* External port must be unused */ switch (proto) { @@ -882,12 +1963,13 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (e_port > 1024) \ { \ a->busy_##n##_ports++; \ - a->busy_##n##_ports_per_thread[get_thread_idx_by_port (e_port)]++; \ + a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \ + e_port)]++; \ } \ break; foreach_nat_protocol #undef _ - default : nat_elog_info ("unknown protocol"); + default : nat_elog_info (nm, "unknown protocol"); return VNET_API_ERROR_INVALID_VALUE_2; } break; @@ -898,9 +1980,9 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, { if (sw_if_index != ~0) { - for (i = 0; i < vec_len (sm->to_resolve); i++) + for (i = 0; i < vec_len (nm->to_resolve); i++) { - rp = sm->to_resolve + i; + rp = nm->to_resolve + i; if (rp->addr_only) continue; if (rp->sw_if_index != sw_if_index && @@ -909,7 +1991,7 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, rp->e_port != e_port && rp->proto != proto) continue; - vec_del1 (sm->to_resolve, i); + vec_del1 (nm->to_resolve, i); break; } } @@ -917,14 +1999,14 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, } } - pool_get (sm->static_mappings, m); + pool_get (nm->static_mappings, m); clib_memset (m, 0, sizeof (*m)); m->tag = vec_dup (tag); m->local_addr = l_addr; m->external_addr = e_addr; if (addr_only) - m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY; + m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_ADDR_ONLY; else { m->local_port = l_port; @@ -934,7 +2016,7 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (identity_nat) { - m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT; + m->flags |= NAT44_EI_STATIC_MAPPING_FLAG_IDENTITY_NAT; pool_get (m->locals, local); local->vrf_id = vrf_id; local->fib_index = fib_index; @@ -945,58 +2027,59 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, m->fib_index = fib_index; } - if (sm->num_workers > 1) + if (nm->num_workers > 1) { ip4_header_t ip = { .src_address = m->local_addr, }; - vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index, 0)); - tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]); + vec_add1 (m->workers, nm->worker_in2out_cb (&ip, m->fib_index, 0)); + tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]); } else - tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); + tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers); init_nat_kv (&kv, m->local_addr, m->local_port, fib_index, m->proto, 0, - m - sm->static_mappings); - clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1); + m - nm->static_mappings); + clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 1); init_nat_kv (&kv, m->external_addr, m->external_port, 0, m->proto, 0, - m - sm->static_mappings); - clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1); + m - nm->static_mappings); + clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 1); /* Delete dynamic sessions matching local address (+ local port) */ // TODO: based on type of NAT EI/ED - if (!(sm->static_mapping_only)) + if (!(nm->static_mapping_only)) { u_key.addr = m->local_addr; u_key.fib_index = m->fib_index; kv.key = u_key.as_u64; - if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) + if (!clib_bihash_search_8_8 (&tnm->user_hash, &kv, &value)) { user_index = value.value; - u = pool_elt_at_index (tsm->users, user_index); + u = pool_elt_at_index (tnm->users, user_index); if (u->nsessions) { head_index = u->sessions_per_user_list_head_index; - head = pool_elt_at_index (tsm->list_pool, head_index); + head = pool_elt_at_index (tnm->list_pool, head_index); elt_index = head->next; - elt = pool_elt_at_index (tsm->list_pool, elt_index); + elt = pool_elt_at_index (tnm->list_pool, elt_index); ses_index = elt->value; while (ses_index != ~0) { - s = pool_elt_at_index (tsm->sessions, ses_index); - elt = pool_elt_at_index (tsm->list_pool, elt->next); + s = pool_elt_at_index (tnm->sessions, ses_index); + elt = pool_elt_at_index (tnm->list_pool, elt->next); ses_index = elt->value; - if (snat_is_session_static (s)) + if (nat44_ei_is_session_static (s)) continue; if (!addr_only && s->in2out.port != m->local_port) continue; - nat_free_session_data (sm, s, tsm - sm->per_thread_data, - 0); - nat44_delete_session (sm, s, tsm - sm->per_thread_data); + nat44_ei_free_session_data_v2 ( + nm, s, tnm - nm->per_thread_data, 0); + nat44_ei_delete_session (nm, s, + tnm - nm->per_thread_data); if (!addr_only) break; @@ -1018,7 +2101,7 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (identity_nat) { if (vrf_id == ~0) - vrf_id = sm->inside_vrf_id; + vrf_id = nm->inside_vrf_id; pool_foreach (local, m->locals) { @@ -1036,13 +2119,13 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, fib_index = m->fib_index; /* Free external address port */ - if (!(addr_only || sm->static_mapping_only)) + if (!(addr_only || nm->static_mapping_only)) { - for (i = 0; i < vec_len (sm->addresses); i++) + for (i = 0; i < vec_len (nm->addresses); i++) { - if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) + if (nm->addresses[i].addr.as_u32 == e_addr.as_u32) { - a = sm->addresses + i; + a = nm->addresses + i; switch (proto) { #define _(N, j, n, s) \ @@ -1051,7 +2134,8 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, if (e_port > 1024) \ { \ a->busy_##n##_ports--; \ - a->busy_##n##_ports_per_thread[get_thread_idx_by_port (e_port)]--; \ + a->busy_##n##_ports_per_thread[nat44_ei_get_thread_idx_by_port ( \ + e_port)]--; \ } \ break; foreach_nat_protocol @@ -1063,56 +2147,58 @@ nat44_ei_add_del_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, } } - if (sm->num_workers > 1) - tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]); + if (nm->num_workers > 1) + tnm = vec_elt_at_index (nm->per_thread_data, m->workers[0]); else - tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); + tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers); init_nat_k (&kv, m->local_addr, m->local_port, fib_index, m->proto); - clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0); + clib_bihash_add_del_8_8 (&nm->static_mapping_by_local, &kv, 0); /* Delete session(s) for static mapping if exist */ - if (!(sm->static_mapping_only) || - (sm->static_mapping_only && sm->static_mapping_connection_tracking)) + if (!(nm->static_mapping_only) || + (nm->static_mapping_only && nm->static_mapping_connection_tracking)) { u_key.addr = m->local_addr; u_key.fib_index = fib_index; kv.key = u_key.as_u64; - nat44_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only, + nat44_ei_static_mapping_del_sessions (nm, tnm, u_key, addr_only, e_addr, e_port); } - fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, sm->fib_src_low); + fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low); if (pool_elts (m->locals)) return 0; init_nat_k (&kv, m->external_addr, m->external_port, 0, m->proto); - clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0); + clib_bihash_add_del_8_8 (&nm->static_mapping_by_external, &kv, 0); vec_free (m->tag); vec_free (m->workers); /* Delete static mapping from pool */ - pool_put (sm->static_mappings, m); + pool_put (nm->static_mappings, m); } if (!addr_only || (l_addr.as_u32 == e_addr.as_u32)) return 0; /* Add/delete external address to FIB */ - pool_foreach (interface, sm->interfaces) + pool_foreach (interface, nm->interfaces) { - if (nat_interface_is_inside (interface) || sm->out2in_dpo) + if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo) continue; - snat_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, is_add); + nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, + is_add); break; } - pool_foreach (interface, sm->output_feature_interfaces) + pool_foreach (interface, nm->output_feature_interfaces) { - if (nat_interface_is_inside (interface) || sm->out2in_dpo) + if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo) continue; - snat_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, is_add); + nat44_ei_add_del_addr_to_fib (&e_addr, 32, interface->sw_if_index, + is_add); break; } return 0; @@ -1126,24 +2212,24 @@ nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port, u32 *mapping_fib_index, u8 by_external, u8 *is_addr_only, u8 *is_identity_nat) { - snat_main_t *sm = &snat_main; + nat44_ei_main_t *nm = &nat44_ei_main; clib_bihash_kv_8_8_t kv, value; - snat_static_mapping_t *m; + nat44_ei_static_mapping_t *m; u16 port; if (by_external) { init_nat_k (&kv, match_addr, match_port, 0, match_protocol); - if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, + if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) { /* Try address only mapping */ init_nat_k (&kv, match_addr, 0, 0, 0); - if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, + if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) return 1; } - m = pool_elt_at_index (sm->static_mappings, value.value); + m = pool_elt_at_index (nm->static_mappings, value.value); *mapping_fib_index = m->fib_index; *mapping_addr = m->local_addr; @@ -1153,99 +2239,155 @@ nat44_ei_static_mapping_match (ip4_address_t match_addr, u16 match_port, { init_nat_k (&kv, match_addr, match_port, match_fib_index, match_protocol); - if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, &value)) + if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value)) { /* Try address only mapping */ init_nat_k (&kv, match_addr, 0, match_fib_index, 0); - if (clib_bihash_search_8_8 (&sm->static_mapping_by_local, &kv, + if (clib_bihash_search_8_8 (&nm->static_mapping_by_local, &kv, &value)) return 1; } - m = pool_elt_at_index (sm->static_mappings, value.value); + m = pool_elt_at_index (nm->static_mappings, value.value); - *mapping_fib_index = sm->outside_fib_index; + *mapping_fib_index = nm->outside_fib_index; *mapping_addr = m->external_addr; port = m->external_port; } /* Address only mapping doesn't change port */ - if (is_addr_only_static_mapping (m)) + if (nat44_ei_is_addr_only_static_mapping (m)) *mapping_port = match_port; else *mapping_port = port; if (PREDICT_FALSE (is_addr_only != 0)) - *is_addr_only = is_addr_only_static_mapping (m); + *is_addr_only = nat44_ei_is_addr_only_static_mapping (m); if (PREDICT_FALSE (is_identity_nat != 0)) - *is_identity_nat = is_identity_static_mapping (m); + *is_identity_nat = nat44_ei_is_identity_static_mapping (m); return 0; } static void -nat44_ei_worker_db_free (snat_main_per_thread_data_t *tsm) +nat44_ei_worker_db_free (nat44_ei_main_per_thread_data_t *tnm) +{ + pool_free (tnm->list_pool); + pool_free (tnm->lru_pool); + pool_free (tnm->sessions); + pool_free (tnm->users); + + clib_bihash_free_8_8 (&tnm->user_hash); +} + +u8 * +format_nat44_ei_key (u8 *s, va_list *args) { - pool_free (tsm->list_pool); - pool_free (tsm->lru_pool); - pool_free (tsm->sessions); - pool_free (tsm->users); + u64 key = va_arg (*args, u64); - clib_bihash_free_8_8 (&tsm->user_hash); + ip4_address_t addr; + u16 port; + nat_protocol_t protocol; + u32 fib_index; + + split_nat_key (key, &addr, &port, &fib_index, &protocol); + + s = format (s, "%U proto %U port %d fib %d", format_ip4_address, &addr, + format_nat_protocol, protocol, clib_net_to_host_u16 (port), + fib_index); + return s; +} + +u8 * +format_nat44_ei_user_kvp (u8 *s, va_list *args) +{ + clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); + nat44_ei_user_key_t k; + + k.as_u64 = v->key; + + s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr, + k.fib_index, v->value); + + return s; +} + +u8 * +format_nat44_ei_session_kvp (u8 *s, va_list *args) +{ + clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); + + s = + format (s, "%U session-index %llu", format_nat44_ei_key, v->key, v->value); + + return s; +} + +u8 * +format_nat44_ei_static_mapping_kvp (u8 *s, va_list *args) +{ + clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); + + s = format (s, "%U static-mapping-index %llu", format_nat44_ei_key, v->key, + v->value); + + return s; } static void -nat44_ei_worker_db_init (snat_main_per_thread_data_t *tsm, u32 translations, - u32 translation_buckets, u32 user_buckets) +nat44_ei_worker_db_init (nat44_ei_main_per_thread_data_t *tnm, + u32 translations, u32 translation_buckets, + u32 user_buckets) { dlist_elt_t *head; - pool_alloc (tsm->list_pool, translations); - pool_alloc (tsm->lru_pool, translations); - pool_alloc (tsm->sessions, translations); + pool_alloc (tnm->list_pool, translations); + pool_alloc (tnm->lru_pool, translations); + pool_alloc (tnm->sessions, translations); - clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets, 0); + clib_bihash_init_8_8 (&tnm->user_hash, "users", user_buckets, 0); - clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, format_user_kvp); + clib_bihash_set_kvp_format_fn_8_8 (&tnm->user_hash, + format_nat44_ei_user_kvp); - pool_get (tsm->lru_pool, head); - tsm->tcp_trans_lru_head_index = head - tsm->lru_pool; - clib_dlist_init (tsm->lru_pool, tsm->tcp_trans_lru_head_index); + pool_get (tnm->lru_pool, head); + tnm->tcp_trans_lru_head_index = head - tnm->lru_pool; + clib_dlist_init (tnm->lru_pool, tnm->tcp_trans_lru_head_index); - pool_get (tsm->lru_pool, head); - tsm->tcp_estab_lru_head_index = head - tsm->lru_pool; - clib_dlist_init (tsm->lru_pool, tsm->tcp_estab_lru_head_index); + pool_get (tnm->lru_pool, head); + tnm->tcp_estab_lru_head_index = head - tnm->lru_pool; + clib_dlist_init (tnm->lru_pool, tnm->tcp_estab_lru_head_index); - pool_get (tsm->lru_pool, head); - tsm->udp_lru_head_index = head - tsm->lru_pool; - clib_dlist_init (tsm->lru_pool, tsm->udp_lru_head_index); + pool_get (tnm->lru_pool, head); + tnm->udp_lru_head_index = head - tnm->lru_pool; + clib_dlist_init (tnm->lru_pool, tnm->udp_lru_head_index); - pool_get (tsm->lru_pool, head); - tsm->icmp_lru_head_index = head - tsm->lru_pool; - clib_dlist_init (tsm->lru_pool, tsm->icmp_lru_head_index); + pool_get (tnm->lru_pool, head); + tnm->icmp_lru_head_index = head - tnm->lru_pool; + clib_dlist_init (tnm->lru_pool, tnm->icmp_lru_head_index); - pool_get (tsm->lru_pool, head); - tsm->unk_proto_lru_head_index = head - tsm->lru_pool; - clib_dlist_init (tsm->lru_pool, tsm->unk_proto_lru_head_index); + pool_get (tnm->lru_pool, head); + tnm->unk_proto_lru_head_index = head - tnm->lru_pool; + clib_dlist_init (tnm->lru_pool, tnm->unk_proto_lru_head_index); } static void nat44_ei_db_free () { - snat_main_t *sm = &snat_main; - snat_main_per_thread_data_t *tsm; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_main_per_thread_data_t *tnm; - pool_free (sm->static_mappings); - clib_bihash_free_8_8 (&sm->static_mapping_by_local); - clib_bihash_free_8_8 (&sm->static_mapping_by_external); + pool_free (nm->static_mappings); + clib_bihash_free_8_8 (&nm->static_mapping_by_local); + clib_bihash_free_8_8 (&nm->static_mapping_by_external); - if (sm->pat) + if (nm->pat) { - clib_bihash_free_8_8 (&sm->in2out); - clib_bihash_free_8_8 (&sm->out2in); - vec_foreach (tsm, sm->per_thread_data) + clib_bihash_free_8_8 (&nm->in2out); + clib_bihash_free_8_8 (&nm->out2in); + vec_foreach (tnm, nm->per_thread_data) { - nat44_ei_worker_db_free (tsm); + nat44_ei_worker_db_free (tnm); } } } @@ -1253,32 +2395,34 @@ nat44_ei_db_free () static void nat44_ei_db_init (u32 translations, u32 translation_buckets, u32 user_buckets) { - snat_main_t *sm = &snat_main; - snat_main_per_thread_data_t *tsm; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_main_per_thread_data_t *tnm; u32 static_mapping_buckets = 1024; u32 static_mapping_memory_size = 64 << 20; - clib_bihash_init_8_8 (&sm->static_mapping_by_local, + clib_bihash_init_8_8 (&nm->static_mapping_by_local, "static_mapping_by_local", static_mapping_buckets, static_mapping_memory_size); - clib_bihash_init_8_8 (&sm->static_mapping_by_external, + clib_bihash_init_8_8 (&nm->static_mapping_by_external, "static_mapping_by_external", static_mapping_buckets, static_mapping_memory_size); - clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local, - format_static_mapping_kvp); - clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external, - format_static_mapping_kvp); - - if (sm->pat) - { - clib_bihash_init_8_8 (&sm->in2out, "in2out", translation_buckets, 0); - clib_bihash_init_8_8 (&sm->out2in, "out2in", translation_buckets, 0); - clib_bihash_set_kvp_format_fn_8_8 (&sm->in2out, format_session_kvp); - clib_bihash_set_kvp_format_fn_8_8 (&sm->out2in, format_session_kvp); - vec_foreach (tsm, sm->per_thread_data) + clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_local, + format_nat44_ei_static_mapping_kvp); + clib_bihash_set_kvp_format_fn_8_8 (&nm->static_mapping_by_external, + format_nat44_ei_static_mapping_kvp); + + if (nm->pat) + { + clib_bihash_init_8_8 (&nm->in2out, "in2out", translation_buckets, 0); + clib_bihash_init_8_8 (&nm->out2in, "out2in", translation_buckets, 0); + clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out, + format_nat44_ei_session_kvp); + clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in, + format_nat44_ei_session_kvp); + vec_foreach (tnm, nm->per_thread_data) { - nat44_ei_worker_db_init (tsm, translations, translation_buckets, + nat44_ei_worker_db_init (tnm, translations, translation_buckets, user_buckets); } } @@ -1288,32 +2432,585 @@ void nat44_ei_sessions_clear () { nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_main_per_thread_data_t *tnm; - snat_main_per_thread_data_t *tsm; - snat_main_t *sm = &snat_main; - - if (sm->pat) + if (nm->pat) { - clib_bihash_free_8_8 (&sm->in2out); - clib_bihash_free_8_8 (&sm->out2in); - clib_bihash_init_8_8 (&sm->in2out, "in2out", nm->translation_buckets, 0); - clib_bihash_init_8_8 (&sm->out2in, "out2in", nm->translation_buckets, 0); - clib_bihash_set_kvp_format_fn_8_8 (&sm->in2out, format_session_kvp); - clib_bihash_set_kvp_format_fn_8_8 (&sm->out2in, format_session_kvp); - vec_foreach (tsm, sm->per_thread_data) + clib_bihash_free_8_8 (&nm->in2out); + clib_bihash_free_8_8 (&nm->out2in); + clib_bihash_init_8_8 (&nm->in2out, "in2out", nm->translation_buckets, 0); + clib_bihash_init_8_8 (&nm->out2in, "out2in", nm->translation_buckets, 0); + clib_bihash_set_kvp_format_fn_8_8 (&nm->in2out, + format_nat44_ei_session_kvp); + clib_bihash_set_kvp_format_fn_8_8 (&nm->out2in, + format_nat44_ei_session_kvp); + vec_foreach (tnm, nm->per_thread_data) { - nat44_ei_worker_db_free (tsm); - nat44_ei_worker_db_init (tsm, nm->translations, + nat44_ei_worker_db_free (tnm); + nat44_ei_worker_db_init (tnm, nm->translations, nm->translation_buckets, nm->user_buckets); } } - // TODO: function for reset counters - vlib_zero_simple_counter (&sm->total_users, 0); - vlib_zero_simple_counter (&sm->total_sessions, 0); - vlib_zero_simple_counter (&sm->user_limit_reached, 0); + vlib_zero_simple_counter (&nm->total_users, 0); + vlib_zero_simple_counter (&nm->total_sessions, 0); + vlib_zero_simple_counter (&nm->user_limit_reached, 0); } +static void +nat44_ei_update_outside_fib (ip4_main_t *im, uword opaque, u32 sw_if_index, + u32 new_fib_index, u32 old_fib_index) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_outside_fib_t *outside_fib; + nat44_ei_interface_t *i; + u8 is_add = 1; + u8 match = 0; + + if (!nm->enabled || (new_fib_index == old_fib_index) || + (!vec_len (nm->outside_fibs))) + { + return; + } + + pool_foreach (i, nm->interfaces) + { + if (i->sw_if_index == sw_if_index) + { + if (!(nat44_ei_interface_is_outside (i))) + return; + match = 1; + } + } + + pool_foreach (i, nm->output_feature_interfaces) + { + if (i->sw_if_index == sw_if_index) + { + if (!(nat44_ei_interface_is_outside (i))) + return; + match = 1; + } + } + + if (!match) + return; + + vec_foreach (outside_fib, nm->outside_fibs) + { + if (outside_fib->fib_index == old_fib_index) + { + outside_fib->refcount--; + if (!outside_fib->refcount) + vec_del1 (nm->outside_fibs, outside_fib - nm->outside_fibs); + break; + } + } + + vec_foreach (outside_fib, nm->outside_fibs) + { + if (outside_fib->fib_index == new_fib_index) + { + outside_fib->refcount++; + is_add = 0; + break; + } + } + + if (is_add) + { + vec_add2 (nm->outside_fibs, outside_fib, 1); + outside_fib->refcount = 1; + outside_fib->fib_index = new_fib_index; + } +} + +int +nat44_ei_add_address (nat44_ei_main_t *nm, ip4_address_t *addr, u32 vrf_id) +{ + nat44_ei_address_t *ap; + nat44_ei_interface_t *i; + vlib_thread_main_t *tm = vlib_get_thread_main (); + + /* Check if address already exists */ + vec_foreach (ap, nm->addresses) + { + if (ap->addr.as_u32 == addr->as_u32) + { + nat44_ei_log_err ("address exist"); + return VNET_API_ERROR_VALUE_EXIST; + } + } + + vec_add2 (nm->addresses, ap, 1); + + ap->addr = *addr; + if (vrf_id != ~0) + ap->fib_index = fib_table_find_or_create_and_lock ( + FIB_PROTOCOL_IP4, vrf_id, nm->fib_src_low); + else + ap->fib_index = ~0; + +#define _(N, i, n, s) \ + clib_memset (ap->busy_##n##_port_refcounts, 0, \ + sizeof (ap->busy_##n##_port_refcounts)); \ + ap->busy_##n##_ports = 0; \ + ap->busy_##n##_ports_per_thread = 0; \ + vec_validate_init_empty (ap->busy_##n##_ports_per_thread, \ + tm->n_vlib_mains - 1, 0); + foreach_nat_protocol +#undef _ + + /* Add external address to FIB */ + pool_foreach (i, nm->interfaces) + { + if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo) + continue; + + nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1); + break; + } + pool_foreach (i, nm->output_feature_interfaces) + { + if (nat44_ei_interface_is_inside (i) || nm->out2in_dpo) + continue; + + nat44_ei_add_del_addr_to_fib (addr, 32, i->sw_if_index, 1); + break; + } + + return 0; +} + +int +nat44_ei_add_interface_address (nat44_ei_main_t *nm, u32 sw_if_index, + int is_del) +{ + ip4_main_t *ip4_main = nm->ip4_main; + ip4_address_t *first_int_addr; + nat44_ei_static_map_resolve_t *rp; + u32 *indices_to_delete = 0; + int i, j; + u32 *auto_add_sw_if_indices = nm->auto_add_sw_if_indices; + + first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, + 0 /* just want the address */); + + for (i = 0; i < vec_len (auto_add_sw_if_indices); i++) + { + if (auto_add_sw_if_indices[i] == sw_if_index) + { + if (is_del) + { + /* if have address remove it */ + if (first_int_addr) + (void) nat44_ei_del_address (nm, first_int_addr[0], 1); + else + { + for (j = 0; j < vec_len (nm->to_resolve); j++) + { + rp = nm->to_resolve + j; + if (rp->sw_if_index == sw_if_index) + vec_add1 (indices_to_delete, j); + } + if (vec_len (indices_to_delete)) + { + for (j = vec_len (indices_to_delete) - 1; j >= 0; j--) + vec_del1 (nm->to_resolve, j); + vec_free (indices_to_delete); + } + } + vec_del1 (nm->auto_add_sw_if_indices, i); + } + else + return VNET_API_ERROR_VALUE_EXIST; + + return 0; + } + } + + if (is_del) + return VNET_API_ERROR_NO_SUCH_ENTRY; + + /* add to the auto-address list */ + vec_add1 (nm->auto_add_sw_if_indices, sw_if_index); + + /* If the address is already bound - or static - add it now */ + if (first_int_addr) + (void) nat44_ei_add_address (nm, first_int_addr, ~0); + + return 0; +} + +static int +nat44_ei_is_address_used_in_static_mapping (ip4_address_t addr) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_mapping_t *m; + pool_foreach (m, nm->static_mappings) + { + if (nat44_ei_is_addr_only_static_mapping (m) || + nat44_ei_is_identity_static_mapping (m)) + continue; + if (m->external_addr.as_u32 == addr.as_u32) + return 1; + } + return 0; +} + +int +nat44_ei_del_address (nat44_ei_main_t *nm, ip4_address_t addr, u8 delete_sm) +{ + nat44_ei_address_t *a = 0; + nat44_ei_session_t *ses; + u32 *ses_to_be_removed = 0, *ses_index; + nat44_ei_main_per_thread_data_t *tnm; + nat44_ei_interface_t *interface; + nat44_ei_static_mapping_t *m; + int i; + + /* Find SNAT address */ + for (i = 0; i < vec_len (nm->addresses); i++) + { + if (nm->addresses[i].addr.as_u32 == addr.as_u32) + { + a = nm->addresses + i; + break; + } + } + if (!a) + { + nat44_ei_log_err ("no such address"); + return VNET_API_ERROR_NO_SUCH_ENTRY; + } + + if (delete_sm) + { + pool_foreach (m, nm->static_mappings) + { + if (m->external_addr.as_u32 == addr.as_u32) + (void) nat44_ei_add_del_static_mapping ( + m->local_addr, m->external_addr, m->local_port, m->external_port, + m->proto, ~0 /* sw_if_index */, m->vrf_id, + nat44_ei_is_addr_only_static_mapping (m), + nat44_ei_is_identity_static_mapping (m), m->tag, 0); + } + } + else + { + /* Check if address is used in some static mapping */ + if (nat44_ei_is_address_used_in_static_mapping (addr)) + { + nat44_ei_log_err ("address used in static mapping"); + return VNET_API_ERROR_UNSPECIFIED; + } + } + + if (a->fib_index != ~0) + fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, nm->fib_src_low); + + /* Delete sessions using address */ + if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports) + { + vec_foreach (tnm, nm->per_thread_data) + { + pool_foreach (ses, tnm->sessions) + { + if (ses->out2in.addr.as_u32 == addr.as_u32) + { + nat44_ei_free_session_data (nm, ses, + tnm - nm->per_thread_data, 0); + vec_add1 (ses_to_be_removed, ses - tnm->sessions); + } + } + vec_foreach (ses_index, ses_to_be_removed) + { + ses = pool_elt_at_index (tnm->sessions, ses_index[0]); + nat44_ei_delete_session (nm, ses, tnm - nm->per_thread_data); + } + vec_free (ses_to_be_removed); + } + } + +#define _(N, i, n, s) vec_free (a->busy_##n##_ports_per_thread); + foreach_nat_protocol +#undef _ + vec_del1 (nm->addresses, i); + + /* Delete external address from FIB */ + pool_foreach (interface, nm->interfaces) + { + if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo) + continue; + nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0); + break; + } + + pool_foreach (interface, nm->output_feature_interfaces) + { + if (nat44_ei_interface_is_inside (interface) || nm->out2in_dpo) + continue; + nat44_ei_add_del_addr_to_fib (&addr, 32, interface->sw_if_index, 0); + break; + } + + return 0; +} + +static void +nat44_ei_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) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_map_resolve_t *rp; + ip4_address_t l_addr; + int i, j; + int rv; + nat44_ei_address_t *addresses = nm->addresses; + + if (!nm->enabled) + return; + + for (i = 0; i < vec_len (nm->auto_add_sw_if_indices); i++) + { + if (sw_if_index == nm->auto_add_sw_if_indices[i]) + goto match; + } + + return; + +match: + if (!is_delete) + { + /* Don't trip over lease renewal, static config */ + for (j = 0; j < vec_len (addresses); j++) + if (addresses[j].addr.as_u32 == address->as_u32) + return; + + (void) nat44_ei_add_address (nm, address, ~0); + /* Scan static map resolution vector */ + for (j = 0; j < vec_len (nm->to_resolve); j++) + { + rp = nm->to_resolve + j; + if (rp->addr_only) + continue; + /* On this interface? */ + if (rp->sw_if_index == sw_if_index) + { + /* Indetity mapping? */ + if (rp->l_addr.as_u32 == 0) + l_addr.as_u32 = address[0].as_u32; + else + l_addr.as_u32 = rp->l_addr.as_u32; + /* Add the static mapping */ + rv = nat44_ei_add_del_static_mapping ( + l_addr, address[0], rp->l_port, rp->e_port, rp->proto, + ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only, + rp->identity_nat, rp->tag, 1); + if (rv) + nat_elog_notice_X1 ( + nm, "nat44_ei_add_del_static_mapping returned %d", "i4", rv); + } + } + return; + } + else + { + (void) nat44_ei_del_address (nm, address[0], 1); + return; + } +} + +int +nat44_ei_set_frame_queue_nelts (u32 frame_queue_nelts) +{ + fail_if_enabled (); + nat44_ei_main_t *nm = &nat44_ei_main; + nm->frame_queue_nelts = frame_queue_nelts; + return 0; +} + +static void +nat44_ei_ip4_add_del_addr_only_sm_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) +{ + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_map_resolve_t *rp; + nat44_ei_static_mapping_t *m; + clib_bihash_kv_8_8_t kv, value; + int i, rv; + ip4_address_t l_addr; + + if (!nm->enabled) + return; + + for (i = 0; i < vec_len (nm->to_resolve); i++) + { + rp = nm->to_resolve + i; + if (rp->addr_only == 0) + continue; + if (rp->sw_if_index == sw_if_index) + goto match; + } + + return; + +match: + init_nat_k (&kv, *address, rp->addr_only ? 0 : rp->e_port, + nm->outside_fib_index, rp->addr_only ? 0 : rp->proto); + if (clib_bihash_search_8_8 (&nm->static_mapping_by_external, &kv, &value)) + m = 0; + else + m = pool_elt_at_index (nm->static_mappings, value.value); + + if (!is_delete) + { + /* Don't trip over lease renewal, static config */ + if (m) + return; + } + else + { + if (!m) + return; + } + + /* Indetity mapping? */ + if (rp->l_addr.as_u32 == 0) + l_addr.as_u32 = address[0].as_u32; + else + l_addr.as_u32 = rp->l_addr.as_u32; + /* Add the static mapping */ + + rv = nat44_ei_add_del_static_mapping ( + l_addr, address[0], rp->l_port, rp->e_port, rp->proto, + ~0 /* sw_if_index */, rp->vrf_id, rp->addr_only, rp->identity_nat, rp->tag, + !is_delete); + if (rv) + nat_elog_notice_X1 (nm, "nat44_ei_add_del_static_mapping returned %d", + "i4", rv); +} + +VLIB_NODE_FN (nat44_ei_classify_node) +(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame) +{ + u32 n_left_from, *from, *to_next; + nat44_ei_classify_next_t next_index; + nat44_ei_main_t *nm = &nat44_ei_main; + nat44_ei_static_mapping_t *m; + u32 next_in2out = 0, next_out2in = 0; + + from = vlib_frame_vector_args (frame); + n_left_from = frame->n_vectors; + next_index = node->cached_next_index; + + while (n_left_from > 0) + { + u32 n_left_to_next; + + vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); + + while (n_left_from > 0 && n_left_to_next > 0) + { + u32 bi0; + vlib_buffer_t *b0; + u32 next0 = NAT44_EI_CLASSIFY_NEXT_IN2OUT; + ip4_header_t *ip0; + nat44_ei_address_t *ap; + clib_bihash_kv_8_8_t kv0, value0; + + /* speculatively enqueue b0 to the current next frame */ + bi0 = from[0]; + to_next[0] = bi0; + from += 1; + to_next += 1; + n_left_from -= 1; + n_left_to_next -= 1; + + b0 = vlib_get_buffer (vm, bi0); + ip0 = vlib_buffer_get_current (b0); + + vec_foreach (ap, nm->addresses) + { + if (ip0->dst_address.as_u32 == ap->addr.as_u32) + { + next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN; + goto enqueue0; + } + } + + if (PREDICT_FALSE (pool_elts (nm->static_mappings))) + { + init_nat_k (&kv0, ip0->dst_address, 0, 0, 0); + /* try to classify the fragment based on IP header alone */ + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, + &kv0, &value0)) + { + m = pool_elt_at_index (nm->static_mappings, value0.value); + if (m->local_addr.as_u32 != m->external_addr.as_u32) + next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN; + goto enqueue0; + } + init_nat_k (&kv0, ip0->dst_address, + vnet_buffer (b0)->ip.reass.l4_dst_port, 0, + ip_proto_to_nat_proto (ip0->protocol)); + if (!clib_bihash_search_8_8 (&nm->static_mapping_by_external, + &kv0, &value0)) + { + m = pool_elt_at_index (nm->static_mappings, value0.value); + if (m->local_addr.as_u32 != m->external_addr.as_u32) + next0 = NAT44_EI_CLASSIFY_NEXT_OUT2IN; + } + } + + enqueue0: + if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) && + (b0->flags & VLIB_BUFFER_IS_TRACED))) + { + nat44_ei_classify_trace_t *t = + vlib_add_trace (vm, node, b0, sizeof (*t)); + t->cached = 0; + t->next_in2out = next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT ? 1 : 0; + } + + next_in2out += next0 == NAT44_EI_CLASSIFY_NEXT_IN2OUT; + next_out2in += next0 == NAT44_EI_CLASSIFY_NEXT_OUT2IN; + + /* verify speculative enqueue, maybe switch current next frame */ + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, + n_left_to_next, bi0, next0); + } + + vlib_put_next_frame (vm, node, next_index, n_left_to_next); + } + + vlib_node_increment_counter ( + vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_IN2OUT, next_in2out); + vlib_node_increment_counter ( + vm, node->node_index, NAT44_EI_CLASSIFY_ERROR_NEXT_OUT2IN, next_out2in); + return frame->n_vectors; +} + +VLIB_REGISTER_NODE (nat44_ei_classify_node) = { + .name = "nat44-ei-classify", + .vector_size = sizeof (u32), + .format_trace = format_nat44_ei_classify_trace, + .type = VLIB_NODE_TYPE_INTERNAL, + .n_errors = ARRAY_LEN(nat44_ei_classify_error_strings), + .error_strings = nat44_ei_classify_error_strings, + .n_next_nodes = NAT44_EI_CLASSIFY_N_NEXT, + .next_nodes = { + [NAT44_EI_CLASSIFY_NEXT_IN2OUT] = "nat44-ei-in2out", + [NAT44_EI_CLASSIFY_NEXT_OUT2IN] = "nat44-ei-out2in", + [NAT44_EI_CLASSIFY_NEXT_DROP] = "error-drop", + }, +}; + /* * fd.io coding-style-patch-verification: ON * |