summaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/out2in.c
diff options
context:
space:
mode:
authorMatus Fabian <matfabia@cisco.com>2018-08-23 00:33:35 -0700
committerOle Trøan <otroan@employees.org>2018-08-27 12:17:25 +0000
commit878c646aea9b9ccf68011ffd964694c43bbe5fdd (patch)
tree4acfaf8c3e35e2c2e334ae8495fdd52c49b5a791 /src/plugins/nat/out2in.c
parentd2dcd200fe2e94f6408155f6c38e7f570dbe1183 (diff)
NAT44: add support for session timeout (VPP-1272)
NAT44 (vanilla/simple and endpoint-dependent mode) now lazily delete expired sessions. When inserting to session lookup hash and bucket is full, expired session is overwritten. Change-Id: Ib1b34959f60f0ca4f5b13525b1d41dd2f992288d Signed-off-by: Matus Fabian <matfabia@cisco.com>
Diffstat (limited to 'src/plugins/nat/out2in.c')
-rwxr-xr-xsrc/plugins/nat/out2in.c217
1 files changed, 183 insertions, 34 deletions
diff --git a/src/plugins/nat/out2in.c b/src/plugins/nat/out2in.c
index d5fad807570..46a8a1ed078 100755
--- a/src/plugins/nat/out2in.c
+++ b/src/plugins/nat/out2in.c
@@ -140,6 +140,43 @@ typedef enum {
SNAT_OUT2IN_N_NEXT,
} snat_out2in_next_t;
+int
+nat44_o2i_is_idle_session_cb (clib_bihash_kv_8_8_t * kv, void * arg)
+{
+ snat_main_t *sm = &snat_main;
+ nat44_is_idle_session_ctx_t *ctx = arg;
+ snat_session_t *s;
+ u64 sess_timeout_time;
+ snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
+ ctx->thread_index);
+ clib_bihash_kv_8_8_t s_kv;
+
+ s = pool_elt_at_index (tsm->sessions, kv->value);
+ sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
+ if (ctx->now >= sess_timeout_time)
+ {
+ s_kv.key = s->in2out.as_u64;
+ if (clib_bihash_add_del_8_8 (&tsm->in2out, &s_kv, 0))
+ nat_log_warn ("out2in key del failed");
+
+ snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
+ s->out2in.addr.as_u32,
+ s->in2out.protocol,
+ s->in2out.port,
+ s->out2in.port,
+ s->in2out.fib_index);
+
+ if (!snat_is_session_static (s))
+ snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
+ &s->out2in);
+
+ nat44_delete_session (sm, s, ctx->thread_index);
+ return 1;
+ }
+
+ return 0;
+}
+
/**
* @brief Create session for static mapping.
*
@@ -160,13 +197,15 @@ create_session_for_static_mapping (snat_main_t *sm,
snat_session_key_t in2out,
snat_session_key_t out2in,
vlib_node_runtime_t * node,
- u32 thread_index)
+ u32 thread_index,
+ f64 now)
{
snat_user_t *u;
snat_session_t *s;
clib_bihash_kv_8_8_t kv0;
ip4_header_t *ip0;
udp_header_t *udp0;
+ nat44_is_idle_session_ctx_t ctx0;
if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
{
@@ -203,16 +242,20 @@ create_session_for_static_mapping (snat_main_t *sm,
s->in2out.protocol = out2in.protocol;
/* Add to translation hashes */
+ ctx0.now = now;
+ ctx0.thread_index = thread_index;
kv0.key = s->in2out.as_u64;
kv0.value = s - sm->per_thread_data[thread_index].sessions;
- if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].in2out, &kv0,
- 1 /* is_add */))
+ if (clib_bihash_add_or_overwrite_stale_8_8 (
+ &sm->per_thread_data[thread_index].in2out, &kv0,
+ nat44_i2o_is_idle_session_cb, &ctx0))
nat_log_notice ("in2out key add failed");
kv0.key = s->out2in.as_u64;
- if (clib_bihash_add_del_8_8 (&sm->per_thread_data[thread_index].out2in, &kv0,
- 1 /* is_add */))
+ if (clib_bihash_add_or_overwrite_stale_8_8 (
+ &sm->per_thread_data[thread_index].out2in, &kv0,
+ nat44_o2i_is_idle_session_cb, &ctx0))
nat_log_notice ("out2in key add failed");
/* log NAT event */
@@ -222,7 +265,7 @@ create_session_for_static_mapping (snat_main_t *sm,
s->in2out.port,
s->out2in.port,
s->in2out.fib_index);
- return s;
+ return s;
}
static_always_inline
@@ -356,7 +399,8 @@ u32 icmp_match_out2in_slow(snat_main_t *sm, vlib_node_runtime_t *node,
/* Create session initiated by host from external network */
s0 = create_session_for_static_mapping(sm, b0, sm0, key0,
- node, thread_index);
+ node, thread_index,
+ vlib_time_now (sm->vlib_main));
if (!s0)
{
@@ -801,7 +845,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
/* Create session initiated by host from external network */
s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
- thread_index);
+ thread_index, now);
if (!s0)
{
next0 = SNAT_OUT2IN_NEXT_DROP;
@@ -952,7 +996,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
/* Create session initiated by host from external network */
s1 = create_session_for_static_mapping(sm, b1, sm1, key1, node,
- thread_index);
+ thread_index, now);
if (!s1)
{
next1 = SNAT_OUT2IN_NEXT_DROP;
@@ -1139,7 +1183,7 @@ snat_out2in_node_fn (vlib_main_t * vm,
/* Create session initiated by host from external network */
s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
- thread_index);
+ thread_index, now);
if (!s0)
{
next0 = SNAT_OUT2IN_NEXT_DROP;
@@ -1364,7 +1408,7 @@ nat44_out2in_reass_node_fn (vlib_main_t * vm,
/* Create session initiated by host from external network */
s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
- thread_index);
+ thread_index, now);
if (!s0)
{
b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
@@ -1564,6 +1608,109 @@ format_nat44_ed_out2in_trace (u8 * s, va_list * args)
return s;
}
+static inline u32
+icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
+ ip4_header_t * ip0, icmp46_header_t * icmp0,
+ u32 sw_if_index0, u32 rx_fib_index0,
+ vlib_node_runtime_t * node, u32 next0, f64 now,
+ u32 thread_index, snat_session_t ** p_s0)
+{
+ next0 = icmp_out2in(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
+ next0, thread_index, p_s0, 0);
+ snat_session_t * s0 = *p_s0;
+ if (PREDICT_TRUE(next0 != SNAT_OUT2IN_NEXT_DROP && s0))
+ {
+ /* Accounting */
+ nat44_session_update_counters (s0, now,
+ vlib_buffer_length_in_chain (sm->vlib_main, b0));
+ }
+ return next0;
+}
+
+int
+nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void * arg)
+{
+ snat_main_t *sm = &snat_main;
+ nat44_is_idle_session_ctx_t *ctx = arg;
+ snat_session_t *s;
+ u64 sess_timeout_time;
+ nat_ed_ses_key_t ed_key;
+ clib_bihash_kv_16_8_t ed_kv;
+ int i;
+ snat_address_t *a;
+ snat_session_key_t key;
+ snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
+ ctx->thread_index);
+
+ s = pool_elt_at_index (tsm->sessions, kv->value);
+ sess_timeout_time = s->last_heard + (f64)nat44_session_get_timeout(sm, s);
+ if (ctx->now >= sess_timeout_time)
+ {
+ ed_key.l_addr = s->in2out.addr;
+ ed_key.r_addr = s->ext_host_addr;
+ ed_key.fib_index = s->out2in.fib_index;
+ if (snat_is_unk_proto_session (s))
+ {
+ ed_key.proto = s->in2out.port;
+ ed_key.r_port = 0;
+ ed_key.l_port = 0;
+ }
+ else
+ {
+ ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol);
+ ed_key.l_port = s->in2out.port;
+ ed_key.r_port = s->ext_host_port;
+ }
+ if (is_twice_nat_session (s))
+ {
+ ed_key.r_addr = s->ext_host_nat_addr;
+ ed_key.r_port = s->ext_host_nat_port;
+ }
+ ed_kv.key[0] = ed_key.as_u64[0];
+ ed_kv.key[1] = ed_key.as_u64[1];
+ if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
+ nat_log_warn ("in2out_ed key del failed");
+
+ if (snat_is_unk_proto_session (s))
+ goto delete;
+
+ snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
+ s->out2in.addr.as_u32,
+ s->in2out.protocol,
+ s->in2out.port,
+ s->out2in.port,
+ s->in2out.fib_index);
+
+ if (is_twice_nat_session (s))
+ {
+ for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
+ {
+ key.protocol = s->in2out.protocol;
+ key.port = s->ext_host_nat_port;
+ a = sm->twice_nat_addresses + i;
+ if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
+ {
+ snat_free_outside_address_and_port (sm->twice_nat_addresses,
+ ctx->thread_index, &key);
+ break;
+ }
+ }
+ }
+
+ if (snat_is_session_static (s))
+ goto delete;
+
+ if (s->outside_address_index != ~0)
+ snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
+ &s->out2in);
+ delete:
+ nat44_delete_session (sm, s, ctx->thread_index);
+ return 1;
+ }
+
+ return 0;
+}
+
static snat_session_t *
create_session_for_static_mapping_ed (snat_main_t * sm,
vlib_buffer_t *b,
@@ -1572,7 +1719,8 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
vlib_node_runtime_t * node,
u32 thread_index,
twice_nat_type_t twice_nat,
- u8 is_lb)
+ u8 is_lb,
+ f64 now)
{
snat_session_t *s;
snat_user_t *u;
@@ -1582,6 +1730,7 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
clib_bihash_kv_16_8_t kv;
snat_session_key_t eh_key;
u32 address_index;
+ nat44_is_idle_session_ctx_t ctx;
if (PREDICT_FALSE (maximum_sessions_exceeded(sm, thread_index)))
{
@@ -1597,7 +1746,7 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
return 0;
}
- s = nat_session_alloc_or_recycle (sm, u, thread_index);
+ s = nat_ed_session_alloc (sm, u, thread_index);
if (!s)
{
nat44_delete_user_with_no_session (sm, u, thread_index);
@@ -1624,7 +1773,11 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
make_ed_kv (&kv, &e_key.addr, &s->ext_host_addr, ip->protocol,
e_key.fib_index, e_key.port, s->ext_host_port);
kv.value = s - tsm->sessions;
- if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 1))
+ ctx.now = now;
+ ctx.thread_index = thread_index;
+ if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
+ nat44_o2i_ed_is_idle_session_cb,
+ &ctx))
nat_log_notice ("out2in-ed key add failed");
if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
@@ -1655,7 +1808,9 @@ create_session_for_static_mapping_ed (snat_main_t * sm,
l_key.fib_index, l_key.port, s->ext_host_port);
}
kv.value = s - tsm->sessions;
- if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
+ if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
+ nat44_i2o_ed_is_idle_session_cb,
+ &ctx))
nat_log_notice ("in2out-ed key add failed");
return s;
@@ -1779,7 +1934,7 @@ create_bypass_for_fwd(snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
return;
}
- s = nat_session_alloc_or_recycle (sm, u, thread_index);
+ s = nat_ed_session_alloc (sm, u, thread_index);
if (!s)
{
nat44_delete_user_with_no_session (sm, u, thread_index);
@@ -1810,8 +1965,6 @@ create_bypass_for_fwd(snat_main_t * sm, ip4_header_t * ip, u32 rx_fib_index,
return;
}
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s, thread_index);
/* Accounting */
nat44_session_update_counters (s, now, 0);
}
@@ -1891,7 +2044,8 @@ icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
/* Create session initiated by host from external network */
s = create_session_for_static_mapping_ed(sm, b, l_key, e_key, node,
- thread_index, 0, 0);
+ thread_index, 0, 0,
+ vlib_time_now (sm->vlib_main));
if (!s)
{
@@ -1981,7 +2135,7 @@ nat44_ed_out2in_unknown_proto (snat_main_t *sm,
}
/* Create a new session */
- s = nat_session_alloc_or_recycle (sm, u, thread_index);
+ s = nat_ed_session_alloc (sm, u, thread_index);
if (!s)
{
nat44_delete_user_with_no_session (sm, u, thread_index);
@@ -2023,8 +2177,6 @@ nat44_ed_out2in_unknown_proto (snat_main_t *sm,
/* Accounting */
nat44_session_update_counters (s, now,
vlib_buffer_length_in_chain (vm, b));
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s, thread_index);
return s;
}
@@ -2136,7 +2288,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
{
- next0 = icmp_out2in_slow_path
+ next0 = icmp_out2in_ed_slow_path
(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
next0, now, thread_index, &s0);
goto trace00;
@@ -2210,7 +2362,8 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
s0 = create_session_for_static_mapping_ed(sm, b0, l_key0,
e_key0, node,
thread_index,
- twice_nat0, is_lb0);
+ twice_nat0, is_lb0,
+ now);
if (!s0)
{
@@ -2281,8 +2434,6 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
/* Accounting */
nat44_session_update_counters (s0, now,
vlib_buffer_length_in_chain (vm, b0));
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s0, thread_index);
trace00:
if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
@@ -2339,7 +2490,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
{
- next1 = icmp_out2in_slow_path
+ next1 = icmp_out2in_ed_slow_path
(sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
next1, now, thread_index, &s1);
goto trace01;
@@ -2413,7 +2564,8 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
s1 = create_session_for_static_mapping_ed(sm, b1, l_key1,
e_key1, node,
thread_index,
- twice_nat1, is_lb1);
+ twice_nat1, is_lb1,
+ now);
if (!s1)
{
@@ -2484,8 +2636,6 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
/* Accounting */
nat44_session_update_counters (s1, now,
vlib_buffer_length_in_chain (vm, b1));
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s1, thread_index);
trace01:
if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
@@ -2574,7 +2724,7 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
{
- next0 = icmp_out2in_slow_path
+ next0 = icmp_out2in_ed_slow_path
(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
next0, now, thread_index, &s0);
goto trace0;
@@ -2648,7 +2798,8 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
s0 = create_session_for_static_mapping_ed(sm, b0, l_key0,
e_key0, node,
thread_index,
- twice_nat0, is_lb0);
+ twice_nat0, is_lb0,
+ now);
if (!s0)
{
@@ -2719,8 +2870,6 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm,
/* Accounting */
nat44_session_update_counters (s0, now,
vlib_buffer_length_in_chain (vm, b0));
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s0, thread_index);
trace0:
if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)