50 tag = t->
is_slow_path ?
"SNAT_IN2OUT_SLOW_PATH" :
"SNAT_IN2OUT_FAST_PATH";
52 s =
format (s,
"%s: sw_if_index %d, next index %d, session %d", tag,
64 s =
format (s,
"SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
78 m = t->
do_handoff ?
"next worker" :
"same worker";
89 #define foreach_snat_in2out_error \ 90 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \ 91 _(IN2OUT_PACKETS, "Good in2out packets processed") \ 92 _(OUT_OF_PORTS, "Out of ports") \ 93 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \ 94 _(BAD_ICMP_TYPE, "icmp type not echo-request") \ 95 _(NO_TRANSLATION, "No translation") 98 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym, 105 #define _(sym,string) string, 121 snat_session_t ** sessionp,
130 u32 oldest_per_user_translation_list_index;
131 dlist_elt_t * oldest_per_user_translation_list_elt;
136 u32 address_index = ~0;
137 u32 outside_fib_index;
144 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
147 outside_fib_index = p[0];
154 if (clib_bihash_search_8_8 (&sm->
user_hash, &kv0, &value0))
158 memset (u, 0,
sizeof (*u));
172 clib_bihash_add_del_8_8 (&sm->
user_hash, &kv0, 1 );
185 oldest_per_user_translation_list_index =
189 ASSERT (oldest_per_user_translation_list_index != ~0);
194 oldest_per_user_translation_list_index);
196 oldest_per_user_translation_list_elt =
198 oldest_per_user_translation_list_index);
201 session_index = oldest_per_user_translation_list_elt->
value;
209 kv0.
key = s->in2out.as_u64;
210 if (clib_bihash_add_del_8_8 (&sm->
in2out, &kv0, 0 ))
212 kv0.
key = s->out2in.as_u64;
213 if (clib_bihash_add_del_8_8 (&sm->
out2in, &kv0, 0 ))
217 (sm, &s->
out2in, s->outside_address_index);
218 s->outside_address_index = ~0;
224 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
227 s->outside_address_index = address_index;
231 u8 static_mapping = 1;
240 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
247 memset (s, 0,
sizeof (*s));
249 s->outside_address_index = address_index;
263 per_user_translation_list_elt);
265 per_user_translation_list_elt -
268 per_user_translation_list_elt->
value =
270 s->per_user_index = per_user_translation_list_elt -
275 s->per_user_list_head_index,
276 per_user_translation_list_elt -
283 s->out2in.fib_index = outside_fib_index;
287 kv0.
key = s->in2out.as_u64;
289 if (clib_bihash_add_del_8_8 (&sm->
in2out, &kv0, 1 ))
292 kv0.
key = s->out2in.as_u64;
295 if (clib_bihash_add_del_8_8 (&sm->
out2in, &kv0, 1 ))
299 worker_by_out_key.
addr = s->out2in.addr;
300 worker_by_out_key.
port = s->out2in.port;
301 worker_by_out_key.
fib_index = s->out2in.fib_index;
303 kv0.
value = cpu_index;
311 icmp46_header_t * icmp0,
323 u32 new_addr0, old_addr0;
324 u16 old_id0, new_id0;
330 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
343 if (clib_bihash_search_8_8 (&sm->
in2out, &kv0, &value0))
361 next0 =
slow_path (sm, b0, ip0, rx_fib_index0, &key0,
362 &s0, node, next0, cpu_index);
383 new_id0 = s0->out2in.port;
386 sum0 = icmp0->checksum;
392 s0->last_heard = now;
401 s0->per_user_list_head_index,
435 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
436 u16 new_dst_port0, old_dst_port0;
445 if (clib_bihash_search_8_8 (&sm->
out2in, &kv0, &value0))
451 new_dst_port0 = sm0.
port;
464 if (clib_bihash_search_8_8 (&sm->
worker_by_out, &kv0, &value0))
473 new_dst_addr0 = s0->in2out.addr.as_u32;
474 new_dst_port0 = s0->in2out.port;
488 old_dst_port0 = tcp0->
ports.dst;
493 tcp0->
ports.dst = new_dst_port0;
515 u32 n_left_from, * from, * to_next;
517 u32 pkts_processed = 0;
521 u32 stats_node_index;
531 while (n_left_from > 0)
536 to_next, n_left_to_next);
538 while (n_left_from >= 4 && n_left_to_next >= 2)
543 u32 sw_if_index0, sw_if_index1;
546 u32 new_addr0, old_addr0, new_addr1, old_addr1;
547 u16 old_port0, new_port0, old_port1, new_port1;
550 icmp46_header_t * icmp0, * icmp1;
552 u32 rx_fib_index0, rx_fib_index1;
554 snat_session_t * s0 = 0, * s1 = 0;
572 to_next[0] = bi0 = from[0];
573 to_next[1] = bi1 = from[1];
585 icmp0 = (icmp46_header_t *) udp0;
594 proto0 = (ip0->
protocol == IP_PROTOCOL_UDP)
596 proto0 = (ip0->
protocol == IP_PROTOCOL_TCP)
598 proto0 = (ip0->
protocol == IP_PROTOCOL_ICMP)
610 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
611 node, next0, now, cpu_index);
651 next0 =
slow_path (sm, b0, ip0, rx_fib_index0, &key0,
652 &s0, node, next0, cpu_index);
679 old_port0 = tcp0->
ports.src;
680 tcp0->
ports.src = s0->out2in.port;
681 new_port0 = tcp0->
ports.src;
703 s0->last_heard = now;
712 s0->per_user_list_head_index,
735 icmp1 = (icmp46_header_t *) udp1;
742 proto1 = (ip1->
protocol == IP_PROTOCOL_UDP)
744 proto1 = (ip1->
protocol == IP_PROTOCOL_TCP)
746 proto1 = (ip1->
protocol == IP_PROTOCOL_ICMP)
758 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
759 next1, now, cpu_index);
799 next1 =
slow_path (sm, b1, ip1, rx_fib_index1, &key1,
800 &s1, node, next1, cpu_index);
827 old_port1 = tcp1->
ports.src;
828 tcp1->
ports.src = s1->out2in.port;
829 new_port1 = tcp1->
ports.src;
851 s1->last_heard = now;
860 s1->per_user_list_head_index,
881 to_next, n_left_to_next,
882 bi0, bi1, next0, next1);
885 while (n_left_from > 0 && n_left_to_next > 0)
893 u32 new_addr0, old_addr0;
894 u16 old_port0, new_port0;
897 icmp46_header_t * icmp0;
901 snat_session_t * s0 = 0;
918 icmp0 = (icmp46_header_t *) udp0;
925 proto0 = (ip0->
protocol == IP_PROTOCOL_UDP)
927 proto0 = (ip0->
protocol == IP_PROTOCOL_TCP)
929 proto0 = (ip0->
protocol == IP_PROTOCOL_ICMP)
941 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
942 next0, now, cpu_index);
962 if (clib_bihash_search_8_8 (&sm->
in2out, &kv0, &value0))
982 next0 =
slow_path (sm, b0, ip0, rx_fib_index0, &key0,
983 &s0, node, next0, cpu_index);
1010 old_port0 = tcp0->
ports.src;
1011 tcp0->
ports.src = s0->out2in.port;
1012 new_port0 = tcp0->
ports.src;
1034 s0->last_heard = now;
1041 s0->per_user_index);
1043 s0->per_user_list_head_index,
1044 s0->per_user_index);
1065 to_next, n_left_to_next,
1073 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1088 .name =
"snat-in2out",
1089 .vector_size =
sizeof (
u32),
1120 .name =
"snat-in2out-slowpath",
1121 .vector_size =
sizeof (
u32),
1149 u32 n_left_from, *from, *to_next = 0;
1156 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
1157 u32 next_worker_index = 0;
1158 u32 current_worker_index = ~0;
1175 while (n_left_from > 0)
1203 if (clib_bihash_search_8_8 (&sm->
worker_by_in, &kv0, &value0))
1210 kv0.
value = next_worker_index;
1214 next_worker_index = value0.
value;
1220 if (next_worker_index != current_worker_index)
1227 handoff_queue_elt_by_worker_index);
1231 current_worker_index = next_worker_index;
1235 to_next_worker[0] = bi0;
1237 n_left_to_next_worker--;
1239 if (n_left_to_next_worker == 0)
1243 current_worker_index = ~0;
1244 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
1280 for (i = 0; i <
vec_len (handoff_queue_elt_by_worker_index); i++)
1282 if (handoff_queue_elt_by_worker_index[i])
1284 hf = handoff_queue_elt_by_worker_index[
i];
1292 handoff_queue_elt_by_worker_index[
i] = 0;
1297 congested_handoff_queue_by_worker_index[
i] =
1301 current_worker_index = ~0;
1307 .name =
"snat-in2out-worker-handoff",
1308 .vector_size =
sizeof (
u32),
1324 icmp46_header_t * icmp0,
1332 u32 new_addr0, old_addr0;
1333 u16 old_id0, new_id0;
1361 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1382 sum0 = icmp0->checksum;
1396 u32 n_left_from, * from, * to_next;
1398 u32 pkts_processed = 0;
1401 u32 stats_node_index;
1409 while (n_left_from > 0)
1414 to_next, n_left_to_next);
1416 while (n_left_from > 0 && n_left_to_next > 0)
1424 u32 new_addr0, old_addr0;
1425 u16 old_port0, new_port0;
1428 icmp46_header_t * icmp0;
1439 n_left_to_next -= 1;
1447 icmp0 = (icmp46_header_t *) udp0;
1453 proto0 = (ip0->
protocol == IP_PROTOCOL_UDP)
1455 proto0 = (ip0->
protocol == IP_PROTOCOL_TCP)
1457 proto0 = (ip0->
protocol == IP_PROTOCOL_ICMP)
1482 (sm, b0, ip0, icmp0, sw_if_index0, node, next0, rx_fib_index0);
1492 b0->
error = node->
errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1498 new_port0 = sm0.
port;
1513 old_port0 = tcp0->
ports.src;
1514 tcp0->
ports.src = new_port0;
1561 to_next, n_left_to_next,
1569 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1577 .name =
"snat-in2out-fast",
1578 .vector_size =
sizeof (
u32),
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
u32 sessions_per_user_list_head_index
u32 max_translations_per_user
static u8 * format_snat_in2out_trace(u8 *s, va_list *args)
sll srl srl sll sra u16x4 i
static uword snat_in2out_slow_path_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
static void clib_dlist_init(dlist_elt_t *pool, u32 index)
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
static u32 slow_path(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, u32 rx_fib_index0, snat_session_key_t *key0, snat_session_t **sessionp, vlib_node_runtime_t *node, u32 next0, u32 cpu_index)
static u8 * format_snat_in2out_fast_trace(u8 *s, va_list *args)
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
static f64 vlib_time_now(vlib_main_t *vm)
static char * snat_in2out_error_strings[]
static uword snat_in2out_worker_handoff_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
struct _vlib_node_registration vlib_node_registration_t
static uword snat_in2out_node_fn_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_slow_path)
u32 * fib_index_by_sw_if_index
Table index indexed by software interface.
u32 buffer_index[VLIB_FRAME_SIZE]
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
VLIB_NODE_FUNCTION_MULTIARCH(snat_in2out_node, snat_in2out_fast_path_fn)
static void snat_hairpinning(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, udp_header_t *udp0, tcp_header_t *tcp0, u32 proto0)
Hairpinning.
u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index)
vlib_node_registration_t snat_in2out_fast_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_fast_node)
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
snat_main_per_thread_data_t * per_thread_data
int snat_static_mapping_match(snat_main_t *sm, snat_session_key_t match, snat_session_key_t *mapping, u8 by_external)
Match SNAT static mapping.
static u8 * format_snat_in2out_worker_handoff_trace(u8 *s, va_list *args)
#define clib_warning(format, args...)
static void * ip4_next_header(ip4_header_t *i)
vlib_node_registration_t snat_in2out_slowpath_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_slowpath_node)
#define foreach_snat_in2out_error
clib_bihash_8_8_t worker_by_in
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
static u32 icmp_in2out_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 cpu_index)
static void clib_dlist_addtail(dlist_elt_t *pool, u32 head_index, u32 new_index)
uword os_get_cpu_number(void)
static vlib_frame_queue_elt_t * vlib_get_worker_handoff_queue_elt(u32 frame_queue_index, u32 vlib_worker_index, vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index)
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
vlib_error_t error
Error code for buffers to be enqueued to error handler.
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
#define CLIB_PREFETCH(addr, size, type)
vlib_node_registration_t snat_in2out_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_node)
clib_bihash_8_8_t user_hash
static vlib_thread_main_t * vlib_get_thread_main()
int snat_alloc_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 *address_indexp)
static uword snat_in2out_fast_static_map_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
8 octet key, 8 octet key value pair
#define VLIB_NODE_FLAG_TRACE
static void clib_dlist_remove(dlist_elt_t *pool, u32 index)
#define VLIB_BUFFER_IS_TRACED
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
#define vec_elt(v, i)
Get vector value at index i.
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_node_registration_t snat_in2out_worker_handoff_node
(constructor) VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node)
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
#define ip_csum_update(sum, old, new, type, field)
void snat_free_outside_address_and_port(snat_main_t *sm, snat_session_key_t *k, u32 address_index)
static void vlib_put_frame_queue_elt(vlib_frame_queue_elt_t *hf)
static uword snat_in2out_fast_path_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
#define VLIB_REGISTER_NODE(x,...)
#define SNAT_SESSION_FLAG_STATIC_MAPPING
static u32 icmp_in2out_static_map(snat_main_t *sm, vlib_buffer_t *b0, ip4_header_t *ip0, icmp46_header_t *icmp0, u32 sw_if_index0, vlib_node_runtime_t *node, u32 next0, u32 rx_fib_index0)
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
#define CLIB_CACHE_LINE_BYTES
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
snat_session_t * sessions
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
#define snat_is_session_static(s)
Check if SNAT session is created from static mapping.
static u16 ip_csum_fold(ip_csum_t c)
clib_bihash_8_8_t worker_by_out
static u32 clib_dlist_remove_head(dlist_elt_t *pool, u32 head_index)