aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/plugins/nat/CMakeLists.txt13
-rw-r--r--src/plugins/nat/in2out_ed.c10
-rw-r--r--src/plugins/nat/nat.c846
-rw-r--r--src/plugins/nat/nat.h79
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei.c534
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei.h81
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_ha.c (renamed from src/plugins/nat/nat_ha.c)206
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_ha.h (renamed from src/plugins/nat/nat_ha.h)5
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_ha_doc.md (renamed from src/plugins/nat/nat_ha_doc.md)0
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_in2out.c (renamed from src/plugins/nat/in2out.c)36
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_inlines.h51
-rw-r--r--src/plugins/nat/nat44-ei/nat44_ei_out2in.c (renamed from src/plugins/nat/out2in.c)25
-rw-r--r--src/plugins/nat/nat44/inlines.h9
-rw-r--r--src/plugins/nat/nat44_api.c11
-rw-r--r--src/plugins/nat/nat44_cli.c13
-rw-r--r--src/plugins/nat/nat_inlines.h4
-rw-r--r--src/plugins/nat/out2in_ed.c102
17 files changed, 999 insertions, 1026 deletions
diff --git a/src/plugins/nat/CMakeLists.txt b/src/plugins/nat/CMakeLists.txt
index 0c28fa31da5..83c148f9658 100644
--- a/src/plugins/nat/CMakeLists.txt
+++ b/src/plugins/nat/CMakeLists.txt
@@ -29,9 +29,7 @@ add_vpp_plugin(nat
SOURCES
nat.c
nat44_api.c
- in2out.c
in2out_ed.c
- out2in.c
out2in_ed.c
nat_dpo.c
nat44_cli.c
@@ -40,16 +38,19 @@ add_vpp_plugin(nat
nat44_classify.c
nat_affinity.c
nat_format.c
- nat_ha.c
+ nat44-ei/nat44_ei.c
+ nat44-ei/nat44_ei_ha.c
+ nat44-ei/nat44_ei_in2out.c
+ nat44-ei/nat44_ei_out2in.c
MULTIARCH_SOURCES
- in2out.c
in2out_ed.c
+ out2in_ed.c
nat44_classify.c
nat44_hairpinning.c
nat44_handoff.c
- out2in.c
- out2in_ed.c
+ nat44-ei/nat44_ei_in2out.c
+ nat44-ei/nat44_ei_out2in.c
API_FILES
nat44.api
diff --git a/src/plugins/nat/in2out_ed.c b/src/plugins/nat/in2out_ed.c
index 776efdf1395..ed9ad04bdb2 100644
--- a/src/plugins/nat/in2out_ed.c
+++ b/src/plugins/nat/in2out_ed.c
@@ -29,7 +29,6 @@
#include <nat/nat_inlines.h>
#include <nat/nat44/inlines.h>
#include <nat/lib/nat_syslog.h>
-#include <nat/nat_ha.h>
#include <nat/nat44/ed_inlines.h>
#include <nat/lib/nat_inlines.h>
@@ -133,10 +132,6 @@ nat44_i2o_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
&s->ext_host_addr, s->ext_host_port,
s->nat_proto, is_twice_nat_session (s));
- nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
- s->ext_host_port, s->nat_proto, s->out2in.fib_index,
- ctx->thread_index);
-
if (is_twice_nat_session (s))
{
for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
@@ -482,11 +477,6 @@ slow_path_ed (snat_main_t * sm,
&s->ext_host_addr, s->ext_host_port, s->nat_proto,
0);
- nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
- s->out2in.port, &s->ext_host_addr, s->ext_host_port,
- &s->ext_host_nat_addr, s->ext_host_nat_port,
- s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
-
per_vrf_sessions_register_session (s, thread_index);
return next;
diff --git a/src/plugins/nat/nat.c b/src/plugins/nat/nat.c
index eeaa443bfe4..156b6ddf369 100644
--- a/src/plugins/nat/nat.c
+++ b/src/plugins/nat/nat.c
@@ -26,7 +26,6 @@
#include <nat/nat_inlines.h>
#include <nat/nat44/inlines.h>
#include <nat/nat_affinity.h>
-#include <nat/nat_ha.h>
#include <vnet/fib/fib_table.h>
#include <vnet/fib/ip4_fib.h>
#include <vnet/ip/reass/ip4_sv_reass.h>
@@ -34,6 +33,9 @@
#include <nat/nat44/ed_inlines.h>
#include <vnet/ip/ip_table.h>
+#include <nat/nat44-ei/nat44_ei_ha.h>
+#include <nat/nat44-ei/nat44_ei.h>
+
#include <vpp/app/version.h>
snat_main_t snat_main;
@@ -187,14 +189,6 @@ static u32
nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
u8 is_output);
-static u32
-snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
- u32 rx_fib_index0, u8 is_output);
-
-static u32
-snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
- u8 is_output);
-
static u32 nat_calc_bihash_buckets (u32 n_elts);
u8 *
@@ -255,124 +249,6 @@ format_ed_session_kvp (u8 * s, va_list * args)
}
void
-nat44_ei_free_session_data (snat_main_t * sm, snat_session_t * s,
- u32 thread_index, u8 is_ha)
-{
- clib_bihash_kv_8_8_t kv;
-
- snat_main_per_thread_data_t *tsm =
- vec_elt_at_index (sm->per_thread_data, thread_index);
-
- init_nat_i2o_k (&kv, s);
- if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
- nat_elog_warn ("in2out key del failed");
-
- init_nat_o2i_k (&kv, s);
- if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
- nat_elog_warn ("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);
-
- 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 (snat_is_session_static (s))
- return;
-
- snat_free_outside_address_and_port (sm->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)
-{
- dlist_elt_t *elt;
- snat_session_t *s;
-
- snat_main_t *sm = &snat_main;
- snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
-
- // get head
- elt = pool_elt_at_index (tsm->list_pool,
- u->sessions_per_user_list_head_index);
- // get first element
- elt = pool_elt_at_index (tsm->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);
-
- nat44_ei_free_session_data (sm, s, thread_index, 0);
- nat44_delete_session (sm, s, thread_index);
- }
-}
-
-int
-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;
-
- snat_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)
- {
- /* *INDENT-OFF* */
- vec_foreach (tsm, sm->per_thread_data)
- {
- if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
- {
- nat44_ei_user_del_sessions (
- pool_elt_at_index (tsm->users, value.value),
- tsm->thread_index);
- rv = 0;
- break;
- }
- }
- /* *INDENT-ON* */
- }
- else
- {
- tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
- if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
- {
- nat44_ei_user_del_sessions (pool_elt_at_index
- (tsm->users, value.value),
- tsm->thread_index);
- rv = 0;
- }
- }
- return rv;
-}
-
-void
nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index,
u8 is_ha)
{
@@ -817,60 +693,6 @@ get_thread_idx_by_port (u16 e_port)
}
void
-nat_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,
- 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;
- u32 elt_index, head_index, ses_index;
-
- if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
- {
- user_index = value.value;
- u = pool_elt_at_index (tsm->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);
- elt_index = head->next;
- elt = pool_elt_at_index (tsm->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);
- ses_index = elt->value;
-
- if (!addr_only)
- {
- if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
- (s->out2in.port != e_port))
- continue;
- }
-
- if (is_lb_session (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);
-
- if (!addr_only)
- break;
- }
- }
- }
-}
-
-void
nat_ed_static_mapping_del_sessions (snat_main_t * sm,
snat_main_per_thread_data_t * tsm,
ip4_address_t l_addr,
@@ -1317,8 +1139,8 @@ snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr,
u_key.addr = m->local_addr;
u_key.fib_index = fib_index;
kv.key = u_key.as_u64;
- nat_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
- e_addr, e_port);
+ nat44_ei_static_mapping_del_sessions (sm, tsm, u_key, addr_only,
+ e_addr, e_port);
}
}
@@ -2573,12 +2395,6 @@ nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im,
u32 address_length,
u32 if_address_index, u32 is_delete);
-static int
-nat_alloc_addr_and_port_default (snat_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);
-
void
test_key_calc_split ()
{
@@ -2871,10 +2687,6 @@ nat44_plugin_enable (nat44_config_t c)
}
}
- // reset to defaults:
- sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
- sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
- //
nat_reset_timeouts (&sm->timeouts);
// nat44 feature configuration
@@ -2928,22 +2740,20 @@ nat44_plugin_enable (nat44_config_t c)
clib_bihash_set_kvp_format_fn_16_8 (&sm->out2in_ed,
format_ed_session_kvp);
-
nat_affinity_enable ();
-
- nat_ha_enable (nat_ha_sadd_ed_cb, nat_ha_sdel_ed_cb, nat_ha_sref_ed_cb);
}
else
{
- sm->worker_out2in_cb = snat_get_worker_out2in_cb;
- sm->worker_in2out_cb = snat_get_worker_in2out_cb;
+ sm->worker_out2in_cb = nat44_ei_get_out2in_worker_index;
+ sm->worker_in2out_cb = nat44_ei_get_in2out_worker_index;
+
sm->out2in_node_index = sm->ei_out2in_node_index;
sm->in2out_node_index = sm->ei_in2out_node_index;
sm->in2out_output_node_index = sm->ei_in2out_output_node_index;
sm->icmp_match_out2in_cb = icmp_match_out2in_slow;
sm->icmp_match_in2out_cb = icmp_match_in2out_slow;
- nat_ha_enable (nat_ha_sadd_cb, nat_ha_sdel_cb, nat_ha_sref_cb);
+ nat44_ei_plugin_enable ();
}
// c.static_mapping & c.connection_tracking require
@@ -3018,7 +2828,10 @@ nat44_plugin_disable ()
return 1;
}
- nat_ha_disable ();
+ if (!sm->endpoint_dependent)
+ {
+ nat44_ei_plugin_disable ();
+ }
// first unregister all nodes from interfaces
vec = vec_dup (sm->interfaces);
@@ -3142,10 +2955,10 @@ snat_free_outside_address_and_port (snat_address_t * addresses,
}
}
-static int
-nat_set_outside_address_and_port (snat_address_t * addresses,
- u32 thread_index, ip4_address_t addr,
- u16 port, nat_protocol_t protocol)
+int
+nat_set_outside_address_and_port (snat_address_t *addresses, u32 thread_index,
+ ip4_address_t addr, u16 port,
+ nat_protocol_t protocol)
{
snat_address_t *a = 0;
u32 address_index;
@@ -3339,208 +3152,6 @@ end:
return 0;
}
-int
-snat_alloc_outside_address_and_port (snat_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;
-
- return sm->alloc_addr_and_port (addresses, fib_index, thread_index, proto,
- addr, port, port_per_thread,
- snat_thread_index);
-}
-
-static int
-nat_alloc_addr_and_port_default (snat_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;
- u32 portnum;
-
- for (i = 0; i < vec_len (addresses); i++)
- {
- a = addresses + i;
- switch (proto)
- {
-#define _(N, j, n, s) \
- case NAT_PROTOCOL_##N: \
- if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
- { \
- if (a->fib_index == fib_index) \
- { \
- while (1) \
- { \
- portnum = (port_per_thread * \
- snat_thread_index) + \
- snat_random_port(0, port_per_thread - 1) + 1024; \
- if (a->busy_##n##_port_refcounts[portnum]) \
- continue; \
- --a->busy_##n##_port_refcounts[portnum]; \
- a->busy_##n##_ports_per_thread[thread_index]++; \
- a->busy_##n##_ports++; \
- *addr = a->addr; \
- *port = clib_host_to_net_u16(portnum); \
- return 0; \
- } \
- } \
- else if (a->fib_index == ~0) \
- { \
- ga = a; \
- } \
- } \
- break;
- foreach_nat_protocol
-#undef _
- default:
- nat_elog_info ("unknown protocol");
- return 1;
- }
-
- }
-
- if (ga)
- {
- a = ga;
- switch (proto)
- {
-#define _(N, j, n, s) \
- case NAT_PROTOCOL_##N: \
- while (1) \
- { \
- portnum = (port_per_thread * \
- snat_thread_index) + \
- snat_random_port(0, port_per_thread - 1) + 1024; \
- if (a->busy_##n##_port_refcounts[portnum]) \
- continue; \
- ++a->busy_##n##_port_refcounts[portnum]; \
- a->busy_##n##_ports_per_thread[thread_index]++; \
- a->busy_##n##_ports++; \
- *addr = a->addr; \
- *port = clib_host_to_net_u16(portnum); \
- return 0; \
- }
- break;
- foreach_nat_protocol
-#undef _
- default:
- nat_elog_info ("unknown protocol");
- return 1;
- }
- }
-
- /* Totally out of translations to use... */
- nat_ipfix_logging_addresses_exhausted (thread_index, 0);
- return 1;
-}
-
-static int
-nat_alloc_addr_and_port_mape (snat_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;
- u16 m, ports, portnum, A, j;
- m = 16 - (sm->psid_offset + sm->psid_length);
- ports = (1 << (16 - sm->psid_length)) - (1 << m);
-
- if (!vec_len (addresses))
- goto exhausted;
-
- switch (proto)
- {
-#define _(N, i, n, s) \
- case NAT_PROTOCOL_##N: \
- if (a->busy_##n##_ports < ports) \
- { \
- 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)); \
- if (a->busy_##n##_port_refcounts[portnum]) \
- continue; \
- ++a->busy_##n##_port_refcounts[portnum]; \
- a->busy_##n##_ports++; \
- *addr = a->addr; \
- *port = clib_host_to_net_u16 (portnum); \
- return 0; \
- } \
- } \
- break;
- foreach_nat_protocol
-#undef _
- default:
- nat_elog_info ("unknown protocol");
- return 1;
- }
-
-exhausted:
- /* Totally out of translations to use... */
- nat_ipfix_logging_addresses_exhausted (thread_index, 0);
- return 1;
-}
-
-static int
-nat_alloc_addr_and_port_range (snat_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;
- u16 portnum, ports;
-
- ports = sm->end_port - sm->start_port + 1;
-
- if (!vec_len (addresses))
- goto exhausted;
-
- switch (proto)
- {
-#define _(N, i, n, s) \
- case NAT_PROTOCOL_##N: \
- if (a->busy_##n##_ports < ports) \
- { \
- while (1) \
- { \
- portnum = snat_random_port(sm->start_port, sm->end_port); \
- if (a->busy_##n##_port_refcounts[portnum]) \
- continue; \
- ++a->busy_##n##_port_refcounts[portnum]; \
- a->busy_##n##_ports++; \
- *addr = a->addr; \
- *port = clib_host_to_net_u16 (portnum); \
- return 0; \
- } \
- } \
- break;
- foreach_nat_protocol
-#undef _
- default:
- nat_elog_info ("unknown protocol");
- return 1;
- }
-
-exhausted:
- /* Totally out of translations to use... */
- nat_ipfix_logging_addresses_exhausted (thread_index, 0);
- return 1;
-}
-
void
nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
{
@@ -3566,110 +3177,6 @@ nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add)
}
static u32
-snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0,
- u8 is_output)
-{
- snat_main_t *sm = &snat_main;
- u32 next_worker_index = 0;
- u32 hash;
-
- next_worker_index = sm->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)];
- else
- next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
-
- return next_worker_index;
-}
-
-static u32
-snat_get_worker_out2in_cb (vlib_buffer_t * b, ip4_header_t * ip0,
- u32 rx_fib_index0, u8 is_output)
-{
- snat_main_t *sm = &snat_main;
- udp_header_t *udp;
- u16 port;
- clib_bihash_kv_8_8_t kv, value;
- snat_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)))
- {
- init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
- if (!clib_bihash_search_8_8
- (&sm->static_mapping_by_external, &kv, &value))
- {
- m = pool_elt_at_index (sm->static_mappings, value.value);
- return m->workers[0];
- }
- }
-
- proto = ip_proto_to_nat_proto (ip0->protocol);
- udp = ip4_next_header (ip0);
- port = udp->dst_port;
-
- /* unknown protocol */
- if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
- {
- /* use current thread */
- return vlib_get_thread_index ();
- }
-
- if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
- {
- icmp46_header_t *icmp = (icmp46_header_t *) udp;
- icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
- if (!icmp_type_is_error_message
- (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
- port = vnet_buffer (b)->ip.reass.l4_src_port;
- else
- {
- /* if error message, then it's not fragmented and we can access it */
- ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
- proto = ip_proto_to_nat_proto (inner_ip->protocol);
- void *l4_header = ip4_next_header (inner_ip);
- switch (proto)
- {
- case NAT_PROTOCOL_ICMP:
- icmp = (icmp46_header_t *) l4_header;
- echo = (icmp_echo_header_t *) (icmp + 1);
- port = echo->identifier;
- break;
- case NAT_PROTOCOL_UDP:
- case NAT_PROTOCOL_TCP:
- port = ((tcp_udp_header_t *) l4_header)->src_port;
- break;
- default:
- return vlib_get_thread_index ();
- }
- }
- }
-
- /* try static mappings with port */
- if (PREDICT_FALSE (pool_elts (sm->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, &value))
- {
- m = pool_elt_at_index (sm->static_mappings, value.value);
- return m->workers[0];
- }
- }
-
- /* worker by outside port */
- next_worker_index = sm->first_worker_index;
- next_worker_index +=
- sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
- return next_worker_index;
-}
-
-static u32
nat44_ed_get_worker_in2out_cb (ip4_header_t * ip, u32 rx_fib_index,
u8 is_output)
{
@@ -3928,293 +3435,6 @@ done:
return next_worker_index;
}
-void
-nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
- ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port,
- ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
- u32 fib_index, u16 flags, u32 thread_index)
-{
- snat_main_t *sm = &snat_main;
- snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
- snat_user_t *u;
- snat_session_t *s;
- clib_bihash_kv_8_8_t kv;
- vlib_main_t *vm = vlib_get_main ();
- f64 now = vlib_time_now (vm);
- nat_outside_fib_t *outside_fib;
- fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
- fib_prefix_t pfx = {
- .fp_proto = FIB_PROTOCOL_IP4,
- .fp_len = 32,
- .fp_addr = {
- .ip4.as_u32 = eh_addr->as_u32,
- },
- };
-
- if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
- {
- if (nat_set_outside_address_and_port
- (sm->addresses, thread_index, *out_addr, out_port, proto))
- return;
- }
-
- u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
- if (!u)
- return;
-
- s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
- if (!s)
- return;
-
- if (sm->endpoint_dependent)
- {
- nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
- }
-
- s->out2in.addr.as_u32 = out_addr->as_u32;
- s->out2in.port = out_port;
- s->nat_proto = proto;
- s->last_heard = now;
- s->flags = flags;
- s->ext_host_addr.as_u32 = eh_addr->as_u32;
- s->ext_host_port = eh_port;
- user_session_increment (sm, u, snat_is_session_static (s));
- switch (vec_len (sm->outside_fibs))
- {
- case 0:
- s->out2in.fib_index = sm->outside_fib_index;
- break;
- case 1:
- s->out2in.fib_index = sm->outside_fibs[0].fib_index;
- break;
- default:
- /* *INDENT-OFF* */
- vec_foreach (outside_fib, sm->outside_fibs)
- {
- fei = fib_table_lookup (outside_fib->fib_index, &pfx);
- if (FIB_NODE_INDEX_INVALID != fei)
- {
- if (fib_entry_get_resolving_interface (fei) != ~0)
- {
- s->out2in.fib_index = outside_fib->fib_index;
- break;
- }
- }
- }
- /* *INDENT-ON* */
- break;
- }
- init_nat_o2i_kv (&kv, s, s - tsm->sessions);
- if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
- nat_elog_warn ("out2in key add failed");
-
- s->in2out.addr.as_u32 = in_addr->as_u32;
- s->in2out.port = in_port;
- s->in2out.fib_index = fib_index;
- init_nat_i2o_kv (&kv, s, s - tsm->sessions);
- if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
- nat_elog_warn ("in2out key add failed");
-}
-
-void
-nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
- u32 ti)
-{
- snat_main_t *sm = &snat_main;
- clib_bihash_kv_8_8_t kv, value;
- u32 thread_index;
- snat_session_t *s;
- snat_main_per_thread_data_t *tsm;
-
- if (sm->num_workers > 1)
- thread_index =
- sm->first_worker_index +
- (sm->workers[(clib_net_to_host_u16 (out_port) -
- 1024) / sm->port_per_thread]);
- else
- thread_index = sm->num_workers;
- tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
-
- init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
- if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
- return;
-
- s = pool_elt_at_index (tsm->sessions, value.value);
- nat_free_session_data (sm, s, thread_index, 1);
- nat44_delete_session (sm, s, thread_index);
-}
-
-void
-nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
- u32 total_pkts, u64 total_bytes, u32 thread_index)
-{
- snat_main_t *sm = &snat_main;
- clib_bihash_kv_8_8_t kv, value;
- snat_session_t *s;
- snat_main_per_thread_data_t *tsm;
-
- tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
-
- init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
- if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
- return;
-
- s = pool_elt_at_index (tsm->sessions, value.value);
- s->total_pkts = total_pkts;
- s->total_bytes = total_bytes;
-}
-
-void
-nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
- ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port,
- ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
- u32 fib_index, u16 flags, u32 thread_index)
-{
- snat_main_t *sm = &snat_main;
- snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
- snat_session_t *s;
- clib_bihash_kv_16_8_t kv;
- vlib_main_t *vm = vlib_get_main ();
- f64 now = vlib_time_now (vm);
- nat_outside_fib_t *outside_fib;
- fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
- fib_prefix_t pfx = {
- .fp_proto = FIB_PROTOCOL_IP4,
- .fp_len = 32,
- .fp_addr = {
- .ip4.as_u32 = eh_addr->as_u32,
- },
- };
-
-
- if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
- {
- if (nat_set_outside_address_and_port
- (sm->addresses, thread_index, *out_addr, out_port, proto))
- return;
- }
-
- if (flags & SNAT_SESSION_FLAG_TWICE_NAT)
- {
- if (nat_set_outside_address_and_port
- (sm->addresses, thread_index, *ehn_addr, ehn_port, proto))
- return;
- }
-
- s = nat_ed_session_alloc (sm, thread_index, now, proto);
- if (!s)
- return;
-
- s->last_heard = now;
- s->flags = flags;
- s->ext_host_nat_addr.as_u32 = s->ext_host_addr.as_u32 = eh_addr->as_u32;
- s->ext_host_nat_port = s->ext_host_port = eh_port;
- if (is_twice_nat_session (s))
- {
- s->ext_host_nat_addr.as_u32 = ehn_addr->as_u32;
- s->ext_host_nat_port = ehn_port;
- }
- switch (vec_len (sm->outside_fibs))
- {
- case 0:
- s->out2in.fib_index = sm->outside_fib_index;
- break;
- case 1:
- s->out2in.fib_index = sm->outside_fibs[0].fib_index;
- break;
- default:
- /* *INDENT-OFF* */
- vec_foreach (outside_fib, sm->outside_fibs)
- {
- fei = fib_table_lookup (outside_fib->fib_index, &pfx);
- if (FIB_NODE_INDEX_INVALID != fei)
- {
- if (fib_entry_get_resolving_interface (fei) != ~0)
- {
- s->out2in.fib_index = outside_fib->fib_index;
- break;
- }
- }
- }
- /* *INDENT-ON* */
- break;
- }
- s->nat_proto = proto;
- s->out2in.addr.as_u32 = out_addr->as_u32;
- s->out2in.port = out_port;
-
- s->in2out.addr.as_u32 = in_addr->as_u32;
- s->in2out.port = in_port;
- s->in2out.fib_index = fib_index;
-
- init_ed_kv (&kv, *in_addr, in_port, s->ext_host_nat_addr,
- s->ext_host_nat_port, fib_index, nat_proto_to_ip_proto (proto),
- thread_index, s - tsm->sessions);
- if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
- nat_elog_warn ("in2out key add failed");
-
- init_ed_kv (&kv, *out_addr, out_port, *eh_addr, eh_port,
- s->out2in.fib_index, nat_proto_to_ip_proto (proto),
- thread_index, s - tsm->sessions);
- if (clib_bihash_add_del_16_8 (&sm->out2in_ed, &kv, 1))
- nat_elog_warn ("out2in key add failed");
-}
-
-void
-nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto,
- u32 fib_index, u32 ti)
-{
- snat_main_t *sm = &snat_main;
- clib_bihash_kv_16_8_t kv, value;
- u32 thread_index;
- snat_session_t *s;
- snat_main_per_thread_data_t *tsm;
-
- if (sm->num_workers > 1)
- thread_index =
- sm->first_worker_index +
- (sm->workers[(clib_net_to_host_u16 (out_port) -
- 1024) / sm->port_per_thread]);
- else
- thread_index = sm->num_workers;
- tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
-
- init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
- if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
- return;
-
- s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
- nat_free_session_data (sm, s, thread_index, 1);
- nat44_delete_session (sm, s, thread_index);
-}
-
-void
-nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto,
- u32 fib_index, u32 total_pkts, u64 total_bytes,
- u32 thread_index)
-{
- snat_main_t *sm = &snat_main;
- clib_bihash_kv_16_8_t kv, value;
- snat_session_t *s;
- snat_main_per_thread_data_t *tsm;
-
- tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
-
- init_ed_k (&kv, *out_addr, out_port, *eh_addr, eh_port, fib_index, proto);
- if (clib_bihash_search_16_8 (&sm->out2in_ed, &kv, &value))
- return;
-
- s = pool_elt_at_index (tsm->sessions, ed_value_get_session_index (&value));
- s->total_pkts = total_pkts;
- s->total_bytes = total_bytes;
-}
-
static u32
nat_calc_bihash_buckets (u32 n_elts)
{
@@ -4694,38 +3914,6 @@ nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port,
return 0;
}
-void
-nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length)
-{
- snat_main_t *sm = &snat_main;
-
- sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE;
- sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape;
- sm->psid = psid;
- sm->psid_offset = psid_offset;
- sm->psid_length = psid_length;
-}
-
-void
-nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port)
-{
- snat_main_t *sm = &snat_main;
-
- sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE;
- sm->alloc_addr_and_port = nat_alloc_addr_and_port_range;
- sm->start_port = start_port;
- sm->end_port = end_port;
-}
-
-void
-nat_set_alloc_addr_and_port_default (void)
-{
- snat_main_t *sm = &snat_main;
-
- sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
- sm->alloc_addr_and_port = nat_alloc_addr_and_port_default;
-}
-
VLIB_NODE_FN (nat_default_node) (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * frame)
diff --git a/src/plugins/nat/nat.h b/src/plugins/nat/nat.h
index daebe451f4f..af40409d267 100644
--- a/src/plugins/nat/nat.h
+++ b/src/plugins/nat/nat.h
@@ -1371,14 +1371,6 @@ void nat44_db_init (snat_main_per_thread_data_t * tsm);
void nat44_db_free (snat_main_per_thread_data_t * tsm);
/**
- * @brief Delete specific NAT44 EI user and his sessions
- *
- * @param addr IPv4 address
- * @param fib_index FIB table index
- */
-int nat44_ei_user_del (ip4_address_t * addr, u32 fib_index);
-
-/**
* @brief Free all NAT44 sessions
*/
void nat44_sessions_clear ();
@@ -1412,29 +1404,6 @@ snat_session_t *nat_session_alloc_or_recycle (snat_main_t * sm,
u32 thread_index, f64 now);
/**
- * @brief Set address and port assignment algorithm for MAP-E CE
- *
- * @param psid Port Set Identifier value
- * @param psid_offset number of offset bits
- * @param psid_length length of PSID
- */
-void nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset,
- u16 psid_length);
-
-/**
- * @brief Set address and port assignment algorithm for port range
- *
- * @param start_port beginning of the port range
- * @param end_port end of the port range
- */
-void nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port);
-
-/**
- * @brief Set address and port assignment algorithm to default/standard
- */
-void nat_set_alloc_addr_and_port_default (void);
-
-/**
* @brief Free outside address and port pair
*
* @param addresses vector of outside addresses
@@ -1447,26 +1416,6 @@ snat_free_outside_address_and_port (snat_address_t * addresses,
ip4_address_t * addr,
u16 port, nat_protocol_t protocol);
-/**
- * @brief Alloc outside address and port
- *
- * @param addresses vector of outside addresses
- * @param fib_index FIB table index
- * @param thread_index thread index
- * @param port_per_thread number of ports per thread
- * @param snat_thread_index NAT thread index
- *
- * @return 0 on success, non-zero value otherwise
- */
-int snat_alloc_outside_address_and_port (snat_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);
-
void expire_per_vrf_sessions (u32 fib_index);
/**
@@ -1518,23 +1467,7 @@ int snat_static_mapping_match (snat_main_t * sm,
void snat_add_del_addr_to_fib (ip4_address_t * addr,
u8 p_len, u32 sw_if_index, int is_add);
-void
-nat_ha_sadd_cb (ip4_address_t * in_addr, u16 in_port,
- ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port,
- ip4_address_t * ehn_addr, u16 ehn_port, u8 proto,
- u32 fib_index, u16 flags, u32 thread_index);
-
-void
-nat_ha_sdel_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
- u32 ti);
-
-void
-nat_ha_sref_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto, u32 fib_index,
- u32 total_pkts, u64 total_bytes, u32 thread_index);
-
+#if 0
void
nat_ha_sadd_ed_cb (ip4_address_t * in_addr, u16 in_port,
ip4_address_t * out_addr, u16 out_port,
@@ -1548,15 +1481,15 @@ nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
u32 fib_index, u32 ti);
void
-nat_ha_sdel_ed_cb (ip4_address_t * out_addr, u16 out_port,
- ip4_address_t * eh_addr, u16 eh_port, u8 proto,
- u32 fib_index, u32 ti);
-
-void
nat_ha_sref_ed_cb (ip4_address_t * out_addr, u16 out_port,
ip4_address_t * eh_addr, u16 eh_port, u8 proto,
u32 fib_index, u32 total_pkts, u64 total_bytes,
u32 thread_index);
+#endif
+
+int nat_set_outside_address_and_port (snat_address_t *addresses,
+ u32 thread_index, ip4_address_t addr,
+ u16 port, nat_protocol_t protocol);
/*
* Why is this here? Because we don't need to touch this layer to
diff --git a/src/plugins/nat/nat44-ei/nat44_ei.c b/src/plugins/nat/nat44-ei/nat44_ei.c
new file mode 100644
index 00000000000..f50ccb9ba1b
--- /dev/null
+++ b/src/plugins/nat/nat44-ei/nat44_ei.c
@@ -0,0 +1,534 @@
+/*
+ * nat44_ei.c - nat44 endpoint dependent plugin
+ *
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+#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/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 <nat/nat44-ei/nat44_ei_inlines.h>
+#include <nat/nat44-ei/nat44_ei.h>
+
+int
+nat44_ei_plugin_enable ()
+{
+ nat44_ei_set_alloc_default ();
+ nat_ha_enable ();
+ return 0;
+}
+
+void
+nat44_ei_plugin_disable ()
+{
+ nat_ha_disable ();
+}
+
+void
+nat44_ei_free_session_data (snat_main_t *sm, snat_session_t *s,
+ u32 thread_index, u8 is_ha)
+{
+ clib_bihash_kv_8_8_t kv;
+
+ snat_main_per_thread_data_t *tsm =
+ vec_elt_at_index (sm->per_thread_data, thread_index);
+
+ init_nat_i2o_k (&kv, s);
+ if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0))
+ nat_elog_warn ("in2out key del failed");
+
+ init_nat_o2i_k (&kv, s);
+ if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0))
+ nat_elog_warn ("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);
+
+ 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 (snat_is_session_static (s))
+ return;
+
+ snat_free_outside_address_and_port (sm->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)
+{
+ dlist_elt_t *elt;
+ snat_session_t *s;
+
+ snat_main_t *sm = &snat_main;
+ snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
+
+ // get head
+ elt =
+ pool_elt_at_index (tsm->list_pool, u->sessions_per_user_list_head_index);
+ // get first element
+ elt = pool_elt_at_index (tsm->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);
+
+ nat44_ei_free_session_data (sm, s, thread_index, 0);
+ nat44_delete_session (sm, s, thread_index);
+ }
+}
+
+int
+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;
+
+ snat_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)
+ {
+ vec_foreach (tsm, sm->per_thread_data)
+ {
+ if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
+ {
+ nat44_ei_user_del_sessions (
+ pool_elt_at_index (tsm->users, value.value),
+ tsm->thread_index);
+ rv = 0;
+ break;
+ }
+ }
+ }
+ else
+ {
+ tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers);
+ if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
+ {
+ nat44_ei_user_del_sessions (
+ pool_elt_at_index (tsm->users, value.value), tsm->thread_index);
+ rv = 0;
+ }
+ }
+ return rv;
+}
+
+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,
+ 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;
+ u32 elt_index, head_index, ses_index;
+
+ if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value))
+ {
+ user_index = value.value;
+ u = pool_elt_at_index (tsm->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);
+ elt_index = head->next;
+ elt = pool_elt_at_index (tsm->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);
+ ses_index = elt->value;
+
+ if (!addr_only)
+ {
+ if ((s->out2in.addr.as_u32 != e_addr.as_u32) ||
+ (s->out2in.port != e_port))
+ continue;
+ }
+
+ if (is_lb_session (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);
+
+ if (!addr_only)
+ break;
+ }
+ }
+ }
+}
+
+u32
+nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
+ u8 is_output)
+{
+ snat_main_t *sm = &snat_main;
+ u32 next_worker_index = 0;
+ u32 hash;
+
+ next_worker_index = sm->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)];
+ else
+ next_worker_index += sm->workers[hash % _vec_len (sm->workers)];
+
+ return next_worker_index;
+}
+
+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;
+ udp_header_t *udp;
+ u16 port;
+ clib_bihash_kv_8_8_t kv, value;
+ snat_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)))
+ {
+ init_nat_k (&kv, ip0->dst_address, 0, rx_fib_index0, 0);
+ if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv,
+ &value))
+ {
+ m = pool_elt_at_index (sm->static_mappings, value.value);
+ return m->workers[0];
+ }
+ }
+
+ proto = ip_proto_to_nat_proto (ip0->protocol);
+ udp = ip4_next_header (ip0);
+ port = udp->dst_port;
+
+ /* unknown protocol */
+ if (PREDICT_FALSE (proto == NAT_PROTOCOL_OTHER))
+ {
+ /* use current thread */
+ return vlib_get_thread_index ();
+ }
+
+ if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP))
+ {
+ icmp46_header_t *icmp = (icmp46_header_t *) udp;
+ icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1);
+ if (!icmp_type_is_error_message (
+ vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags))
+ port = vnet_buffer (b)->ip.reass.l4_src_port;
+ else
+ {
+ /* if error message, then it's not fragmented and we can access it */
+ ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1);
+ proto = ip_proto_to_nat_proto (inner_ip->protocol);
+ void *l4_header = ip4_next_header (inner_ip);
+ switch (proto)
+ {
+ case NAT_PROTOCOL_ICMP:
+ icmp = (icmp46_header_t *) l4_header;
+ echo = (icmp_echo_header_t *) (icmp + 1);
+ port = echo->identifier;
+ break;
+ case NAT_PROTOCOL_UDP:
+ case NAT_PROTOCOL_TCP:
+ port = ((tcp_udp_header_t *) l4_header)->src_port;
+ break;
+ default:
+ return vlib_get_thread_index ();
+ }
+ }
+ }
+
+ /* try static mappings with port */
+ if (PREDICT_FALSE (pool_elts (sm->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,
+ &value))
+ {
+ m = pool_elt_at_index (sm->static_mappings, value.value);
+ return m->workers[0];
+ }
+ }
+
+ /* worker by outside port */
+ next_worker_index = sm->first_worker_index;
+ next_worker_index +=
+ sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread];
+ return next_worker_index;
+}
+
+static int
+nat44_ei_alloc_default_cb (snat_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;
+ u32 portnum;
+
+ for (i = 0; i < vec_len (addresses); i++)
+ {
+ a = addresses + i;
+ switch (proto)
+ {
+#define _(N, j, n, s) \
+ case NAT_PROTOCOL_##N: \
+ if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
+ { \
+ if (a->fib_index == fib_index) \
+ { \
+ while (1) \
+ { \
+ portnum = (port_per_thread * snat_thread_index) + \
+ snat_random_port (0, port_per_thread - 1) + 1024; \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ --a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports_per_thread[thread_index]++; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ } \
+ } \
+ else if (a->fib_index == ~0) \
+ { \
+ ga = a; \
+ } \
+ } \
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+ }
+
+ if (ga)
+ {
+ a = ga;
+ switch (proto)
+ {
+#define _(N, j, n, s) \
+ case NAT_PROTOCOL_##N: \
+ while (1) \
+ { \
+ portnum = (port_per_thread * snat_thread_index) + \
+ snat_random_port (0, port_per_thread - 1) + 1024; \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ ++a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports_per_thread[thread_index]++; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ }
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+ }
+
+ /* Totally out of translations to use... */
+ nat_ipfix_logging_addresses_exhausted (thread_index, 0);
+ return 1;
+}
+
+static int
+nat44_ei_alloc_range_cb (snat_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;
+ u16 portnum, ports;
+
+ ports = sm->end_port - sm->start_port + 1;
+
+ if (!vec_len (addresses))
+ goto exhausted;
+
+ switch (proto)
+ {
+#define _(N, i, n, s) \
+ case NAT_PROTOCOL_##N: \
+ if (a->busy_##n##_ports < ports) \
+ { \
+ while (1) \
+ { \
+ portnum = snat_random_port (sm->start_port, sm->end_port); \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ ++a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ } \
+ } \
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+
+exhausted:
+ /* Totally out of translations to use... */
+ nat_ipfix_logging_addresses_exhausted (thread_index, 0);
+ return 1;
+}
+
+static int
+nat44_ei_alloc_mape_cb (snat_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;
+ u16 m, ports, portnum, A, j;
+ m = 16 - (sm->psid_offset + sm->psid_length);
+ ports = (1 << (16 - sm->psid_length)) - (1 << m);
+
+ if (!vec_len (addresses))
+ goto exhausted;
+
+ switch (proto)
+ {
+#define _(N, i, n, s) \
+ case NAT_PROTOCOL_##N: \
+ if (a->busy_##n##_ports < ports) \
+ { \
+ 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)); \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ ++a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ } \
+ } \
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+
+exhausted:
+ /* Totally out of translations to use... */
+ nat_ipfix_logging_addresses_exhausted (thread_index, 0);
+ return 1;
+}
+
+void
+nat44_ei_set_alloc_default ()
+{
+ snat_main_t *sm = &snat_main;
+
+ sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT;
+ sm->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;
+
+ 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;
+}
+
+void
+nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length)
+{
+ snat_main_t *sm = &snat_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;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nat/nat44-ei/nat44_ei.h b/src/plugins/nat/nat44-ei/nat44_ei.h
new file mode 100644
index 00000000000..ac430bee5c3
--- /dev/null
+++ b/src/plugins/nat/nat44-ei/nat44_ei.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * @file nat44_ei.h
+ * NAT44 endpoint independent plugin declarations
+ */
+#ifndef __included_nat44_ei_h__
+#define __included_nat44_ei_h__
+
+int nat44_ei_plugin_enable ();
+
+void nat44_ei_plugin_disable ();
+
+/**
+ * @brief Delete specific NAT44 EI user and his sessions
+ *
+ * @param addr IPv4 address
+ * @param fib_index FIB table index
+ */
+int nat44_ei_user_del (ip4_address_t *addr, u32 fib_index);
+
+/**
+ * @brief Delete session for static mapping
+ *
+ * @param addr IPv4 address
+ * @param fib_index FIB table 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, ip4_address_t e_addr,
+ u16 e_port);
+
+u32 nat44_ei_get_in2out_worker_index (ip4_header_t *ip0, u32 rx_fib_index0,
+ u8 is_output);
+
+u32 nat44_ei_get_out2in_worker_index (vlib_buffer_t *b, ip4_header_t *ip0,
+ u32 rx_fib_index0, u8 is_output);
+
+/**
+ * @brief Set address and port assignment algorithm to default/standard
+ */
+void nat44_ei_set_alloc_default (void);
+
+/**
+ * @brief Set address and port assignment algorithm for MAP-E CE
+ *
+ * @param psid Port Set Identifier value
+ * @param psid_offset number of offset bits
+ * @param psid_length length of PSID
+ */
+void nat44_ei_set_alloc_mape (u16 psid, u16 psid_offset, u16 psid_length);
+
+/**
+ * @brief Set address and port assignment algorithm for port range
+ *
+ * @param start_port beginning of the port range
+ * @param end_port end of the port range
+ */
+void nat44_ei_set_alloc_range (u16 start_port, u16 end_port);
+
+#endif /* __included_nat44_ei_h__ */
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nat/nat_ha.c b/src/plugins/nat/nat44-ei/nat44_ei_ha.c
index 08b065dec1d..0b904bf079b 100644
--- a/src/plugins/nat/nat_ha.c
+++ b/src/plugins/nat/nat44-ei/nat44_ei_ha.c
@@ -13,7 +13,9 @@
* limitations under the License.
*/
-#include "nat_ha.h"
+#include <nat/nat_inlines.h>
+#include <nat/nat44/ed_inlines.h>
+#include <nat/nat44-ei/nat44_ei_ha.h>
#include <vnet/udp/udp_local.h>
#include <nat/nat.h>
#include <vppinfra/atomics.h>
@@ -150,22 +152,155 @@ typedef struct nat_ha_main_s
nat_ha_resync_event_cb_t event_callback;
u32 client_index;
u32 pid;
- /* call back functions for received HA events on failover */
- nat_ha_sadd_cb_t sadd_cb;
- nat_ha_sdel_cb_t sdel_cb;
- nat_ha_sref_cb_t sref_cb;
/* per thread data */
u32 num_workers;
nat_ha_per_thread_data_t *per_thread_data;
+
+ u32 ha_handoff_node_index;
+ u32 ha_process_node_index;
+ u32 ha_worker_node_index;
+ u32 ha_node_index;
+
/* worker handoff frame-queue index */
u32 fq_index;
} nat_ha_main_t;
nat_ha_main_t nat_ha_main;
-vlib_node_registration_t nat_ha_process_node;
-vlib_node_registration_t nat_ha_worker_node;
-vlib_node_registration_t nat_ha_node;
-vlib_node_registration_t nat_ha_handoff_node;
+
+static_always_inline void
+nat44_ei_ha_sadd (ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr,
+ u16 out_port, ip4_address_t *eh_addr, u16 eh_port,
+ ip4_address_t *ehn_addr, u16 ehn_port, u8 proto,
+ u32 fib_index, u16 flags, u32 thread_index)
+{
+ snat_main_t *sm = &snat_main;
+ snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
+ snat_user_t *u;
+ snat_session_t *s;
+ clib_bihash_kv_8_8_t kv;
+ vlib_main_t *vm = vlib_get_main ();
+ f64 now = vlib_time_now (vm);
+ nat_outside_fib_t *outside_fib;
+ fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
+ fib_prefix_t pfx = {
+ .fp_proto = FIB_PROTOCOL_IP4,
+ .fp_len = 32,
+ .fp_addr = {
+ .ip4.as_u32 = eh_addr->as_u32,
+ },
+ };
+
+ if (!(flags & SNAT_SESSION_FLAG_STATIC_MAPPING))
+ {
+ if (nat_set_outside_address_and_port (sm->addresses, thread_index,
+ *out_addr, out_port, proto))
+ return;
+ }
+
+ u = nat_user_get_or_create (sm, in_addr, fib_index, thread_index);
+ if (!u)
+ return;
+
+ s = nat_session_alloc_or_recycle (sm, u, thread_index, now);
+ if (!s)
+ return;
+
+ if (sm->endpoint_dependent)
+ {
+ nat_ed_lru_insert (tsm, s, now, nat_proto_to_ip_proto (proto));
+ }
+
+ s->out2in.addr.as_u32 = out_addr->as_u32;
+ s->out2in.port = out_port;
+ s->nat_proto = proto;
+ s->last_heard = now;
+ s->flags = flags;
+ s->ext_host_addr.as_u32 = eh_addr->as_u32;
+ s->ext_host_port = eh_port;
+ user_session_increment (sm, u, snat_is_session_static (s));
+ switch (vec_len (sm->outside_fibs))
+ {
+ case 0:
+ s->out2in.fib_index = sm->outside_fib_index;
+ break;
+ case 1:
+ s->out2in.fib_index = sm->outside_fibs[0].fib_index;
+ break;
+ default:
+ vec_foreach (outside_fib, sm->outside_fibs)
+ {
+ fei = fib_table_lookup (outside_fib->fib_index, &pfx);
+ if (FIB_NODE_INDEX_INVALID != fei)
+ {
+ if (fib_entry_get_resolving_interface (fei) != ~0)
+ {
+ s->out2in.fib_index = outside_fib->fib_index;
+ break;
+ }
+ }
+ }
+ break;
+ }
+ init_nat_o2i_kv (&kv, s, s - tsm->sessions);
+ if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 1))
+ nat_elog_warn ("out2in key add failed");
+
+ s->in2out.addr.as_u32 = in_addr->as_u32;
+ s->in2out.port = in_port;
+ s->in2out.fib_index = fib_index;
+ init_nat_i2o_kv (&kv, s, s - tsm->sessions);
+ if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 1))
+ nat_elog_warn ("in2out key add failed");
+}
+
+static_always_inline void
+nat44_ei_ha_sdel (ip4_address_t *out_addr, u16 out_port,
+ ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
+ u32 ti)
+{
+ snat_main_t *sm = &snat_main;
+ clib_bihash_kv_8_8_t kv, value;
+ u32 thread_index;
+ snat_session_t *s;
+ snat_main_per_thread_data_t *tsm;
+
+ if (sm->num_workers > 1)
+ thread_index = sm->first_worker_index +
+ (sm->workers[(clib_net_to_host_u16 (out_port) - 1024) /
+ sm->port_per_thread]);
+ else
+ thread_index = sm->num_workers;
+ tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
+
+ init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
+ if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
+ return;
+
+ s = pool_elt_at_index (tsm->sessions, value.value);
+ nat_free_session_data (sm, s, thread_index, 1);
+ nat44_delete_session (sm, s, thread_index);
+}
+
+static_always_inline void
+nat44_ei_ha_sref (ip4_address_t *out_addr, u16 out_port,
+ ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
+ u32 total_pkts, u64 total_bytes, u32 thread_index)
+{
+ snat_main_t *sm = &snat_main;
+ clib_bihash_kv_8_8_t kv, value;
+ snat_session_t *s;
+ snat_main_per_thread_data_t *tsm;
+
+ tsm = vec_elt_at_index (sm->per_thread_data, thread_index);
+
+ init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
+ if (clib_bihash_search_8_8 (&tsm->out2in, &kv, &value))
+ return;
+
+ s = pool_elt_at_index (tsm->sessions, value.value);
+ s->total_pkts = total_pkts;
+ s->total_bytes = total_bytes;
+}
static void
nat_ha_resync_fin (void)
@@ -311,15 +446,9 @@ nat_ha_resend_scan (f64 now, u32 thread_index)
}
void
-nat_ha_enable (nat_ha_sadd_cb_t sadd_cb,
- nat_ha_sdel_cb_t sdel_cb, nat_ha_sref_cb_t sref_cb)
+nat_ha_enable ()
{
nat_ha_main_t *ha = &nat_ha_main;
-
- ha->sadd_cb = sadd_cb;
- ha->sdel_cb = sdel_cb;
- ha->sref_cb = sref_cb;
-
ha->enabled = 1;
}
@@ -332,11 +461,28 @@ nat_ha_disable ()
}
void
+nat_ha_set_node_indexes (nat_ha_main_t *ha, vlib_main_t *vm)
+{
+ vlib_node_t *node;
+
+ node = vlib_get_node_by_name (vm, (u8 *) "nat-ha-handoff");
+ ha->ha_handoff_node_index = node->index;
+ node = vlib_get_node_by_name (vm, (u8 *) "nat-ha-process");
+ ha->ha_process_node_index = node->index;
+ node = vlib_get_node_by_name (vm, (u8 *) "nat-ha-worker");
+ ha->ha_worker_node_index = node->index;
+ node = vlib_get_node_by_name (vm, (u8 *) "nat-ha");
+ ha->ha_node_index = node->index;
+}
+
+void
nat_ha_init (vlib_main_t * vm, u32 num_workers, u32 num_threads)
{
nat_ha_main_t *ha = &nat_ha_main;
clib_memset (ha, 0, sizeof (*ha));
+ nat_ha_set_node_indexes (ha, vm);
+
ha->vlib_main = vm;
ha->fq_index = ~0;
@@ -370,13 +516,13 @@ nat_ha_set_listener (ip4_address_t * addr, u16 port, u32 path_mtu)
if (ha->num_workers > 1)
{
if (ha->fq_index == ~0)
- ha->fq_index = vlib_frame_queue_main_init (nat_ha_node.index, 0);
+ ha->fq_index = vlib_frame_queue_main_init (ha->ha_node_index, 0);
udp_register_dst_port (ha->vlib_main, port,
- nat_ha_handoff_node.index, 1);
+ ha->ha_handoff_node_index, 1);
}
else
{
- udp_register_dst_port (ha->vlib_main, port, nat_ha_node.index, 1);
+ udp_register_dst_port (ha->vlib_main, port, ha->ha_node_index, 1);
}
nat_elog_info_X1 ("HA listening on port %d for state sync", "i4", port);
}
@@ -404,7 +550,7 @@ nat_ha_set_failover (ip4_address_t * addr, u16 port,
ha->dst_port = port;
ha->session_refresh_interval = session_refresh_interval;
- vlib_process_signal_event (ha->vlib_main, nat_ha_process_node.index, 1, 0);
+ vlib_process_signal_event (ha->vlib_main, ha->ha_process_node_index, 1, 0);
return 0;
}
@@ -438,9 +584,9 @@ nat_ha_recv_add (nat_ha_event_t * event, f64 now, u32 thread_index)
fib_index = clib_net_to_host_u32 (event->fib_index);
flags = clib_net_to_host_u16 (event->flags);
- ha->sadd_cb (&in_addr, event->in_port, &out_addr, event->out_port, &eh_addr,
- event->eh_port, &ehn_addr, event->ehn_port, event->protocol,
- fib_index, flags, thread_index);
+ nat44_ei_ha_sadd (&in_addr, event->in_port, &out_addr, event->out_port,
+ &eh_addr, event->eh_port, &ehn_addr, event->ehn_port,
+ event->protocol, fib_index, flags, thread_index);
}
static_always_inline void
@@ -457,8 +603,8 @@ nat_ha_recv_del (nat_ha_event_t * event, u32 thread_index)
eh_addr.as_u32 = event->eh_addr;
fib_index = clib_net_to_host_u32 (event->fib_index);
- ha->sdel_cb (&out_addr, event->out_port, &eh_addr, event->eh_port,
- event->protocol, fib_index, thread_index);
+ nat44_ei_ha_sdel (&out_addr, event->out_port, &eh_addr, event->eh_port,
+ event->protocol, fib_index, thread_index);
}
static_always_inline void
@@ -478,9 +624,9 @@ nat_ha_recv_refresh (nat_ha_event_t * event, f64 now, u32 thread_index)
total_pkts = clib_net_to_host_u32 (event->total_pkts);
total_bytes = clib_net_to_host_u64 (event->total_bytes);
- ha->sref_cb (&out_addr, event->out_port, &eh_addr, event->eh_port,
- event->protocol, fib_index, total_pkts, total_bytes,
- thread_index);
+ nat44_ei_ha_sref (&out_addr, event->out_port, &eh_addr, event->eh_port,
+ event->protocol, fib_index, total_pkts, total_bytes,
+ thread_index);
}
/* process received NAT HA event */
@@ -1018,8 +1164,8 @@ nat_ha_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
vlib_put_next_frame (vm, node, next_index, n_left_to_next);
}
- vlib_node_increment_counter (vm, nat_ha_node.index,
- NAT_HA_ERROR_PROCESSED, pkts_processed);
+ vlib_node_increment_counter (vm, ha->ha_node_index, NAT_HA_ERROR_PROCESSED,
+ pkts_processed);
return frame->n_vectors;
}
diff --git a/src/plugins/nat/nat_ha.h b/src/plugins/nat/nat44-ei/nat44_ei_ha.h
index 6bc5be23a0a..c466d4c9288 100644
--- a/src/plugins/nat/nat_ha.h
+++ b/src/plugins/nat/nat44-ei/nat44_ei_ha.h
@@ -39,10 +39,9 @@ typedef void (*nat_ha_sref_cb_t) (ip4_address_t * out_addr, u16 out_port,
u64 total_bytes, u32 thread_index);
/**
- * @brief Enable NAT HA, set callbacks
+ * @brief Enable NAT HA
*/
-void nat_ha_enable (nat_ha_sadd_cb_t sadd_cb, nat_ha_sdel_cb_t sdel_cb,
- nat_ha_sref_cb_t sref_cb);
+void nat_ha_enable ();
/**
* @brief Disable NAT HA
diff --git a/src/plugins/nat/nat_ha_doc.md b/src/plugins/nat/nat44-ei/nat44_ei_ha_doc.md
index f0ea209e250..f0ea209e250 100644
--- a/src/plugins/nat/nat_ha_doc.md
+++ b/src/plugins/nat/nat44-ei/nat44_ei_ha_doc.md
diff --git a/src/plugins/nat/in2out.c b/src/plugins/nat/nat44-ei/nat44_ei_in2out.c
index 309329f12d9..1c341318957 100644
--- a/src/plugins/nat/in2out.c
+++ b/src/plugins/nat/nat44-ei/nat44_ei_in2out.c
@@ -27,9 +27,8 @@
#include <nat/nat.h>
#include <nat/lib/ipfix_logging.h>
#include <nat/nat_inlines.h>
-#include <nat/nat44/inlines.h>
#include <nat/lib/nat_syslog.h>
-#include <nat/nat_ha.h>
+#include <nat/nat44-ei/nat44_ei_inlines.h>
#include <vppinfra/hash.h>
#include <vppinfra/error.h>
@@ -261,7 +260,7 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
u16 sm_port;
u32 sm_fib_index;
- if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
+ if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
{
b0->error = node->errors[SNAT_IN2OUT_ERROR_MAX_SESSIONS_EXCEEDED];
nat_ipfix_logging_max_sessions (thread_index,
@@ -276,13 +275,10 @@ slow_path (snat_main_t * sm, vlib_buffer_t * b0,
&sm_port, &sm_fib_index, 0, 0, 0, 0, 0, &identity_nat, 0))
{
/* Try to create dynamic translation */
- if (snat_alloc_outside_address_and_port (sm->addresses, rx_fib_index0,
- thread_index,
- nat_proto,
- &sm_addr, &sm_port,
- sm->port_per_thread,
- sm->per_thread_data
- [thread_index].snat_thread_index))
+ if (sm->alloc_addr_and_port (
+ sm->addresses, rx_fib_index0, thread_index, nat_proto, &sm_addr,
+ &sm_port, sm->port_per_thread,
+ sm->per_thread_data[thread_index].snat_thread_index))
{
b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
return SNAT_IN2OUT_NEXT_DROP;
@@ -817,9 +813,8 @@ icmp_in2out_slow_path (snat_main_t * sm,
if (PREDICT_TRUE (next0 != SNAT_IN2OUT_NEXT_DROP && s0))
{
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain
- (vm, b0), thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
}
@@ -1113,9 +1108,8 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain (vm, b0),
- thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
trace00:
@@ -1337,9 +1331,8 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s1, now,
- vlib_buffer_length_in_chain (vm, b1),
- thread_index);
+ nat44_ei_session_update_counters (
+ s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s1, thread_index);
trace01:
@@ -1591,9 +1584,8 @@ snat_in2out_node_fn_inline (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain (vm, b0),
- thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
diff --git a/src/plugins/nat/nat44-ei/nat44_ei_inlines.h b/src/plugins/nat/nat44-ei/nat44_ei_inlines.h
new file mode 100644
index 00000000000..f7089ea0c21
--- /dev/null
+++ b/src/plugins/nat/nat44-ei/nat44_ei_inlines.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __included_nat44_ei_inlines_h__
+#define __included_nat44_ei_inlines_h__
+
+#include <nat/nat44-ei/nat44_ei_ha.h>
+
+static_always_inline u8
+nat44_ei_maximum_sessions_exceeded (snat_main_t *sm, u32 thread_index)
+{
+ if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
+ sm->max_translations_per_thread)
+ return 1;
+ return 0;
+}
+
+always_inline void
+nat44_ei_session_update_counters (snat_session_t *s, f64 now, uword bytes,
+ u32 thread_index)
+{
+ s->last_heard = now;
+ s->total_pkts++;
+ s->total_bytes += bytes;
+ nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
+ s->ext_host_port, s->nat_proto, s->out2in.fib_index,
+ s->total_pkts, s->total_bytes, thread_index,
+ &s->ha_last_refreshed, now);
+}
+
+#endif /* __included_nat44_ei_inlines_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/nat/out2in.c b/src/plugins/nat/nat44-ei/nat44_ei_out2in.c
index c8c4542ab83..8220f0dc9d8 100644
--- a/src/plugins/nat/out2in.c
+++ b/src/plugins/nat/nat44-ei/nat44_ei_out2in.c
@@ -27,9 +27,8 @@
#include <nat/nat.h>
#include <nat/lib/ipfix_logging.h>
#include <nat/nat_inlines.h>
-#include <nat/nat44/inlines.h>
#include <nat/lib/nat_syslog.h>
-#include <nat/nat_ha.h>
+#include <nat/nat44-ei/nat44_ei_inlines.h>
#include <vppinfra/hash.h>
#include <vppinfra/error.h>
@@ -182,7 +181,7 @@ create_session_for_static_mapping (snat_main_t * sm,
udp_header_t *udp0;
nat44_is_idle_session_ctx_t ctx0;
- if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
+ if (PREDICT_FALSE (nat44_ei_maximum_sessions_exceeded (sm, thread_index)))
{
b0->error = node->errors[SNAT_OUT2IN_ERROR_MAX_SESSIONS_EXCEEDED];
nat_elog_notice ("maximum sessions exceeded");
@@ -675,9 +674,8 @@ icmp_out2in_slow_path (snat_main_t * sm,
if (PREDICT_TRUE (next0 != SNAT_OUT2IN_NEXT_DROP && s0))
{
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain
- (vm, b0), thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
}
@@ -930,9 +928,8 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain (vm, b0),
- thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
trace0:
@@ -1118,9 +1115,8 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s1, now,
- vlib_buffer_length_in_chain (vm, b1),
- thread_index);
+ nat44_ei_session_update_counters (
+ s1, now, vlib_buffer_length_in_chain (vm, b1), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s1, thread_index);
trace1:
@@ -1334,9 +1330,8 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm,
}
/* Accounting */
- nat44_session_update_counters (s0, now,
- vlib_buffer_length_in_chain (vm, b0),
- thread_index);
+ nat44_ei_session_update_counters (
+ s0, now, vlib_buffer_length_in_chain (vm, b0), thread_index);
/* Per-user LRU list maintenance */
nat44_session_update_lru (sm, s0, thread_index);
trace00:
diff --git a/src/plugins/nat/nat44/inlines.h b/src/plugins/nat/nat44/inlines.h
index 97dfb6c4542..fde228e8b5e 100644
--- a/src/plugins/nat/nat44/inlines.h
+++ b/src/plugins/nat/nat44/inlines.h
@@ -23,15 +23,6 @@
#include <nat/nat.h>
static_always_inline u8
-nat44_maximum_sessions_exceeded (snat_main_t * sm, u32 thread_index)
-{
- if (pool_elts (sm->per_thread_data[thread_index].sessions) >=
- sm->max_translations_per_thread)
- return 1;
- return 0;
-}
-
-static_always_inline u8
nat44_ed_maximum_sessions_exceeded (snat_main_t * sm,
u32 fib_index, u32 thread_index)
{
diff --git a/src/plugins/nat/nat44_api.c b/src/plugins/nat/nat44_api.c
index 5bc4b4cb4e6..cfe2211acd3 100644
--- a/src/plugins/nat/nat44_api.c
+++ b/src/plugins/nat/nat44_api.c
@@ -31,7 +31,8 @@
#include <nat/nat44.api_enum.h>
#include <nat/nat44.api_types.h>
-#include <nat/nat_ha.h>
+#include <nat/nat44-ei/nat44_ei_ha.h>
+#include <nat/nat44-ei/nat44_ei.h>
#include <nat/nat_inlines.h>
#include <nat/nat44/inlines.h>
@@ -350,11 +351,11 @@ static void
switch (mp->alg)
{
case NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
- nat_set_alloc_addr_and_port_default ();
+ nat44_ei_set_alloc_default ();
break;
case NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE:
- nat_set_alloc_addr_and_port_mape (ntohs (mp->psid), mp->psid_offset,
- mp->psid_length);
+ nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
+ mp->psid_length);
break;
case NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE:
port_start = ntohs (mp->start_port);
@@ -364,7 +365,7 @@ static void
rv = VNET_API_ERROR_INVALID_VALUE;
goto send_reply;
}
- nat_set_alloc_addr_and_port_range (port_start, port_end);
+ nat44_ei_set_alloc_range (port_start, port_end);
break;
default:
rv = VNET_API_ERROR_INVALID_VALUE;
diff --git a/src/plugins/nat/nat44_cli.c b/src/plugins/nat/nat44_cli.c
index ca396fd22fe..7415b5fe76f 100644
--- a/src/plugins/nat/nat44_cli.c
+++ b/src/plugins/nat/nat44_cli.c
@@ -24,7 +24,9 @@
#include <nat/nat44/inlines.h>
#include <nat/nat_affinity.h>
#include <vnet/fib/fib_table.h>
-#include <nat/nat_ha.h>
+
+#include <nat/nat44-ei/nat44_ei_ha.h>
+#include <nat/nat44-ei/nat44_ei.h>
#define UNSUPPORTED_IN_ED_MODE_STR \
"This command is unsupported in endpoint dependent mode"
@@ -354,13 +356,13 @@ nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (line_input, "default"))
- nat_set_alloc_addr_and_port_default ();
+ nat44_ei_set_alloc_default ();
else
if (unformat
(line_input, "map-e psid %d psid-offset %d psid-len %d", &psid,
&psid_offset, &psid_length))
- nat_set_alloc_addr_and_port_mape ((u16) psid, (u16) psid_offset,
- (u16) psid_length);
+ nat44_ei_set_alloc_mape ((u16) psid, (u16) psid_offset,
+ (u16) psid_length);
else
if (unformat
(line_input, "port-range %d - %d", &port_start, &port_end))
@@ -372,8 +374,7 @@ nat44_set_alloc_addr_and_port_alg_command_fn (vlib_main_t * vm,
"The end-port must be greater than start-port");
goto done;
}
- nat_set_alloc_addr_and_port_range ((u16) port_start,
- (u16) port_end);
+ nat44_ei_set_alloc_range ((u16) port_start, (u16) port_end);
}
else
{
diff --git a/src/plugins/nat/nat_inlines.h b/src/plugins/nat/nat_inlines.h
index ccb9cdf1221..401f1e5747f 100644
--- a/src/plugins/nat/nat_inlines.h
+++ b/src/plugins/nat/nat_inlines.h
@@ -21,7 +21,7 @@
#include <vnet/fib/ip4_fib.h>
#include <nat/nat.h>
-#include <nat/nat_ha.h>
+//#include <nat/nat44-ei/nat44_ei_ha.h>
always_inline u64
calc_nat_key (ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
@@ -422,10 +422,12 @@ nat44_session_update_counters (snat_session_t * s, f64 now, uword bytes,
s->last_heard = now;
s->total_pkts++;
s->total_bytes += bytes;
+#if 0
nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
s->ext_host_port, s->nat_proto, s->out2in.fib_index,
s->total_pkts, s->total_bytes, thread_index,
&s->ha_last_refreshed, now);
+#endif
}
/** \brief Per-user LRU list maintenance */
diff --git a/src/plugins/nat/out2in_ed.c b/src/plugins/nat/out2in_ed.c
index 95e2ea972af..beb259eee33 100644
--- a/src/plugins/nat/out2in_ed.c
+++ b/src/plugins/nat/out2in_ed.c
@@ -29,7 +29,6 @@
#include <nat/nat_inlines.h>
#include <nat/nat44/inlines.h>
#include <nat/lib/nat_syslog.h>
-#include <nat/nat_ha.h>
#include <nat/nat44/ed_inlines.h>
static char *nat_out2in_ed_error_strings[] = {
@@ -157,10 +156,6 @@ nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
&s->ext_host_addr, s->ext_host_port,
s->nat_proto, is_twice_nat_session (s));
- nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
- s->ext_host_port, s->nat_proto, s->out2in.fib_index,
- ctx->thread_index);
-
if (is_twice_nat_session (s))
{
for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
@@ -239,6 +234,87 @@ nat_alloc_addr_and_port_exact (snat_address_t * a,
return 1;
}
+static_always_inline int
+nat44_ed_alloc_outside_addr_and_port (snat_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;
+ u32 portnum;
+
+ for (i = 0; i < vec_len (addresses); i++)
+ {
+ a = addresses + i;
+ switch (proto)
+ {
+#define _(N, j, n, s) \
+ case NAT_PROTOCOL_##N: \
+ if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \
+ { \
+ if (a->fib_index == fib_index) \
+ { \
+ while (1) \
+ { \
+ portnum = (port_per_thread * snat_thread_index) + \
+ snat_random_port (0, port_per_thread - 1) + 1024; \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ --a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports_per_thread[thread_index]++; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ } \
+ } \
+ else if (a->fib_index == ~0) \
+ { \
+ ga = a; \
+ } \
+ } \
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+ }
+
+ if (ga)
+ {
+ a = ga;
+ switch (proto)
+ {
+#define _(N, j, n, s) \
+ case NAT_PROTOCOL_##N: \
+ while (1) \
+ { \
+ portnum = (port_per_thread * snat_thread_index) + \
+ snat_random_port (0, port_per_thread - 1) + 1024; \
+ if (a->busy_##n##_port_refcounts[portnum]) \
+ continue; \
+ ++a->busy_##n##_port_refcounts[portnum]; \
+ a->busy_##n##_ports_per_thread[thread_index]++; \
+ a->busy_##n##_ports++; \
+ *addr = a->addr; \
+ *port = clib_host_to_net_u16 (portnum); \
+ return 0; \
+ }
+ break;
+ foreach_nat_protocol
+#undef _
+ default : nat_elog_info ("unknown protocol");
+ return 1;
+ }
+ }
+
+ /* Totally out of translations to use... */
+ nat_ipfix_logging_addresses_exhausted (thread_index, 0);
+ return 1;
+}
static snat_session_t *
create_session_for_static_mapping_ed (snat_main_t * sm,
@@ -342,13 +418,10 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
}
else
{
- rc =
- snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
- thread_index, nat_proto,
- &s->ext_host_nat_addr,
- &s->ext_host_nat_port,
- sm->port_per_thread,
- tsm->snat_thread_index);
+ rc = nat44_ed_alloc_outside_addr_and_port (
+ sm->twice_nat_addresses, 0, thread_index, nat_proto,
+ &s->ext_host_nat_addr, &s->ext_host_nat_port, sm->port_per_thread,
+ tsm->snat_thread_index);
}
if (rc)
@@ -390,11 +463,6 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
&s->ext_host_addr, s->ext_host_port,
s->nat_proto, is_twice_nat_session (s));
- nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
- s->out2in.port, &s->ext_host_addr, s->ext_host_port,
- &s->ext_host_nat_addr, s->ext_host_nat_port,
- s->nat_proto, s->in2out.fib_index, s->flags, thread_index, 0);
-
per_vrf_sessions_register_session (s, thread_index);
return s;