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.
*
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)))
{
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 */
s->in2out.port,
s->out2in.port,
s->in2out.fib_index);
- return s;
+ return s;
}
static_always_inline
{
/* Try to match static mapping by external address and port,
destination address and port in packet */
- if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0))
{
if (!sm->forwarding_enabled)
{
/* 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)
{
}
key0.fib_index = rx_fib_index0;
- if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, &is_addr_only, 0, 0, 0))
{
/* Don't NAT packet aimed at the intfc address */
if (is_interface_addr(sm, node, sw_if_index0, ip0->dst_address.as_u32))
{
/* Try to match static mapping by external address and port,
destination address and port in packet */
- if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
/* 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;
{
/* Try to match static mapping by external address and port,
destination address and port in packet */
- if (snat_static_mapping_match(sm, key1, &sm1, 1, 0, 0, 0))
+ if (snat_static_mapping_match(sm, key1, &sm1, 1, 0, 0, 0, 0))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
/* 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;
{
/* Try to match static mapping by external address and port,
destination address and port in packet */
- if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
/* 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;
{
/* Try to match static mapping by external address and port,
destination address and port in packet */
- if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
/* 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];
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,
vlib_node_runtime_t * node,
u32 thread_index,
twice_nat_type_t twice_nat,
- u8 is_lb)
+ lb_nat_type_t lb_nat,
+ f64 now)
{
snat_session_t *s;
snat_user_t *u;
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)))
{
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);
s->ext_host_addr.as_u32 = ip->src_address.as_u32;
s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
- if (is_lb)
+ if (lb_nat)
s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
+ if (lb_nat == AFFINITY_LB_NAT)
+ s->flags |= SNAT_SESSION_FLAG_AFFINITY;
s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
s->outside_address_index = ~0;
s->out2in = e_key;
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 &&
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;
static int
next_src_nat (snat_main_t * sm, ip4_header_t * ip, u8 proto, u16 src_port,
- u16 dst_port, u32 thread_index)
+ u16 dst_port, u32 thread_index, u32 rx_fib_index)
{
clib_bihash_kv_16_8_t kv, value;
snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
make_ed_kv (&kv, &ip->src_address, &ip->dst_address, proto,
- sm->inside_fib_index, src_port, dst_port);
+ rx_fib_index, src_port, dst_port);
if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
return 1;
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);
return;
}
- /* Per-user LRU list maintenance */
- nat44_session_update_lru (sm, s, thread_index);
/* Accounting */
nat44_session_update_counters (s, now, 0);
}
e_key.port = key.l_port;
e_key.protocol = ip_proto_to_snat_proto (key.proto);
e_key.fib_index = rx_fib_index;
- if (snat_static_mapping_match(sm, e_key, &l_key, 1, &is_addr_only, 0, 0))
+ if (snat_static_mapping_match(sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0))
{
if (!sm->forwarding_enabled)
{
else
{
dont_translate = 1;
- if (next_src_nat(sm, ip, key.proto, key.l_port, key.r_port, thread_index))
+ if (next_src_nat(sm, ip, key.proto, key.l_port, key.r_port,
+ thread_index, rx_fib_index))
{
next = NAT44_ED_OUT2IN_NEXT_IN2OUT;
goto out;
/* 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)
{
new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
- u = nat_user_get_or_create (sm, &ip->src_address, m->fib_index,
+ u = nat_user_get_or_create (sm, &m->local_addr, m->fib_index,
thread_index);
if (!u)
{
}
/* 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);
/* 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;
}
clib_bihash_kv_16_8_t kv0, value0, kv1, value1;
ip_csum_t sum0, sum1;
snat_session_key_t e_key0, l_key0, e_key1, l_key1;
- u8 is_lb0, is_lb1;
+ lb_nat_type_t lb_nat0, lb_nat1;
twice_nat_type_t twice_nat0, twice_nat1;
/* Prefetch next iteration. */
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;
e_key0.protocol = proto0;
e_key0.fib_index = rx_fib_index0;
if (snat_static_mapping_match(sm, e_key0, &l_key0, 1, 0,
- &twice_nat0, &is_lb0))
+ &twice_nat0, &lb_nat0, &ip0->src_address))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
{
if (next_src_nat(sm, ip0, ip0->protocol,
udp0->src_port, udp0->dst_port,
- thread_index))
+ thread_index, rx_fib_index0))
{
next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
goto trace00;
s0 = create_session_for_static_mapping_ed(sm, b0, l_key0,
e_key0, node,
thread_index,
- twice_nat0, is_lb0);
+ twice_nat0,
+ lb_nat0,
+ now);
if (!s0)
{
/* 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)
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;
e_key1.protocol = proto1;
e_key1.fib_index = rx_fib_index1;
if (snat_static_mapping_match(sm, e_key1, &l_key1, 1, 0,
- &twice_nat1, &is_lb1))
+ &twice_nat1, &lb_nat1, &ip1->src_address))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
{
if (next_src_nat(sm, ip1, ip1->protocol,
udp1->src_port, udp1->dst_port,
- thread_index))
+ thread_index, rx_fib_index1))
{
next1 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
goto trace01;
s1 = create_session_for_static_mapping_ed(sm, b1, l_key1,
e_key1, node,
thread_index,
- twice_nat1, is_lb1);
+ twice_nat1,
+ lb_nat1,
+ now);
if (!s1)
{
/* 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)
clib_bihash_kv_16_8_t kv0, value0;
ip_csum_t sum0;
snat_session_key_t e_key0, l_key0;
- u8 is_lb0;
+ lb_nat_type_t lb_nat0;
twice_nat_type_t twice_nat0;
/* speculatively enqueue b0 to the current next frame */
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;
e_key0.protocol = proto0;
e_key0.fib_index = rx_fib_index0;
if (snat_static_mapping_match(sm, e_key0, &l_key0, 1, 0,
- &twice_nat0, &is_lb0))
+ &twice_nat0, &lb_nat0, &ip0->src_address))
{
/*
* Send DHCP packets to the ipv4 stack, or we won't
{
if (next_src_nat(sm, ip0, ip0->protocol,
udp0->src_port, udp0->dst_port,
- thread_index))
+ thread_index, rx_fib_index0))
{
next0 = NAT44_ED_OUT2IN_NEXT_IN2OUT;
goto trace0;
s0 = create_session_for_static_mapping_ed(sm, b0, l_key0,
e_key0, node,
thread_index,
- twice_nat0, is_lb0);
+ twice_nat0,
+ lb_nat0,
+ now);
if (!s0)
{
/* 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)
key0.port = udp0->dst_port;
key0.fib_index = rx_fib_index0;
- if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0))
+ if (snat_static_mapping_match(sm, key0, &sm0, 1, 0, 0, 0, 0))
{
b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
goto trace00;