2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vnet/pg/pg.h>
19 #include <vnet/handoff.h>
21 #include <vnet/ip/ip.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <snat/snat.h>
25 #include <snat/snat_ipfix_logging.h>
26 #include <snat/snat_det.h>
28 #include <vppinfra/hash.h>
29 #include <vppinfra/error.h>
30 #include <vppinfra/elog.h>
37 } snat_in2out_trace_t;
40 u32 next_worker_index;
42 } snat_in2out_worker_handoff_trace_t;
44 /* packet trace format function */
45 static u8 * format_snat_in2out_trace (u8 * s, va_list * args)
47 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
48 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
49 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
52 tag = t->is_slow_path ? "SNAT_IN2OUT_SLOW_PATH" : "SNAT_IN2OUT_FAST_PATH";
54 s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
55 t->sw_if_index, t->next_index, t->session_index);
60 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
62 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
63 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
64 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
66 s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
67 t->sw_if_index, t->next_index);
72 static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args)
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 snat_in2out_worker_handoff_trace_t * t =
77 va_arg (*args, snat_in2out_worker_handoff_trace_t *);
80 m = t->do_handoff ? "next worker" : "same worker";
81 s = format (s, "SNAT_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
86 vlib_node_registration_t snat_in2out_node;
87 vlib_node_registration_t snat_in2out_slowpath_node;
88 vlib_node_registration_t snat_in2out_fast_node;
89 vlib_node_registration_t snat_in2out_worker_handoff_node;
90 vlib_node_registration_t snat_det_in2out_node;
92 #define foreach_snat_in2out_error \
93 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
94 _(IN2OUT_PACKETS, "Good in2out packets processed") \
95 _(OUT_OF_PORTS, "Out of ports") \
96 _(BAD_OUTSIDE_FIB, "Outside VRF ID not found") \
97 _(BAD_ICMP_TYPE, "icmp type not echo-request") \
98 _(NO_TRANSLATION, "No translation")
101 #define _(sym,str) SNAT_IN2OUT_ERROR_##sym,
102 foreach_snat_in2out_error
105 } snat_in2out_error_t;
107 static char * snat_in2out_error_strings[] = {
108 #define _(sym,string) string,
109 foreach_snat_in2out_error
114 SNAT_IN2OUT_NEXT_LOOKUP,
115 SNAT_IN2OUT_NEXT_DROP,
116 SNAT_IN2OUT_NEXT_SLOW_PATH,
117 SNAT_IN2OUT_NEXT_ICMP_ERROR,
119 } snat_in2out_next_t;
122 * @brief Check if packet should be translated
124 * Packets aimed at outside interface and external addresss with active session
125 * should be translated.
127 * @param sm SNAT main
128 * @param rt SNAT runtime data
129 * @param sw_if_index0 index of the inside interface
130 * @param ip0 IPv4 header
131 * @param proto0 SNAT protocol
132 * @param rx_fib_index0 RX FIB index
134 * @returns 0 if packet should be translated otherwise 1
137 snat_not_translate (snat_main_t * sm, snat_runtime_t * rt, u32 sw_if_index0,
138 ip4_header_t * ip0, u32 proto0, u32 rx_fib_index0)
140 ip4_address_t * first_int_addr;
141 udp_header_t * udp0 = ip4_next_header (ip0);
142 snat_session_key_t key0, sm0;
143 clib_bihash_kv_8_8_t kv0, value0;
144 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
146 .fp_proto = FIB_PROTOCOL_IP4,
149 .ip4.as_u32 = ip0->dst_address.as_u32,
153 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
156 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
157 0 /* just want the address */);
158 rt->cached_sw_if_index = sw_if_index0;
160 rt->cached_ip4_address = first_int_addr->as_u32;
162 rt->cached_ip4_address = 0;
165 /* Don't NAT packet aimed at the intfc address */
166 if (PREDICT_FALSE(ip0->dst_address.as_u32 == rt->cached_ip4_address))
169 key0.addr = ip0->dst_address;
170 key0.port = udp0->dst_port;
171 key0.protocol = proto0;
172 key0.fib_index = sm->outside_fib_index;
173 kv0.key = key0.as_u64;
175 /* NAT packet aimed at external address if */
176 /* has active sessions */
177 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
179 /* or is static mappings */
180 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
186 fei = fib_table_lookup (rx_fib_index0, &pfx);
187 if (FIB_NODE_INDEX_INVALID != fei)
189 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
190 if (sw_if_index == ~0)
192 fei = fib_table_lookup (sm->outside_fib_index, &pfx);
193 if (FIB_NODE_INDEX_INVALID != fei)
194 sw_if_index = fib_entry_get_resolving_interface (fei);
197 pool_foreach (i, sm->interfaces,
199 /* NAT packet aimed at outside interface */
200 if ((i->is_inside == 0) && (sw_if_index == i->sw_if_index))
208 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
211 snat_session_key_t * key0,
212 snat_session_t ** sessionp,
213 vlib_node_runtime_t * node,
218 snat_user_key_t user_key;
220 clib_bihash_kv_8_8_t kv0, value0;
221 u32 oldest_per_user_translation_list_index;
222 dlist_elt_t * oldest_per_user_translation_list_elt;
223 dlist_elt_t * per_user_translation_list_elt;
224 dlist_elt_t * per_user_list_head_elt;
226 snat_session_key_t key1;
227 u32 address_index = ~0;
228 u32 outside_fib_index;
230 snat_worker_key_t worker_by_out_key;
232 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
235 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
236 return SNAT_IN2OUT_NEXT_DROP;
238 outside_fib_index = p[0];
240 key1.protocol = key0->protocol;
241 user_key.addr = ip0->src_address;
242 user_key.fib_index = rx_fib_index0;
243 kv0.key = user_key.as_u64;
245 /* Ever heard of the "user" = src ip4 address before? */
246 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
248 /* no, make a new one */
249 pool_get (sm->per_thread_data[thread_index].users, u);
250 memset (u, 0, sizeof (*u));
251 u->addr = ip0->src_address;
252 u->fib_index = rx_fib_index0;
254 pool_get (sm->per_thread_data[thread_index].list_pool, per_user_list_head_elt);
256 u->sessions_per_user_list_head_index = per_user_list_head_elt -
257 sm->per_thread_data[thread_index].list_pool;
259 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
260 u->sessions_per_user_list_head_index);
262 kv0.value = u - sm->per_thread_data[thread_index].users;
265 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
269 u = pool_elt_at_index (sm->per_thread_data[thread_index].users,
273 /* Over quota? Recycle the least recently used dynamic translation */
274 if (u->nsessions >= sm->max_translations_per_user)
276 /* Remove the oldest dynamic translation */
278 oldest_per_user_translation_list_index =
279 clib_dlist_remove_head (sm->per_thread_data[thread_index].list_pool,
280 u->sessions_per_user_list_head_index);
282 ASSERT (oldest_per_user_translation_list_index != ~0);
284 /* add it back to the end of the LRU list */
285 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
286 u->sessions_per_user_list_head_index,
287 oldest_per_user_translation_list_index);
288 /* Get the list element */
289 oldest_per_user_translation_list_elt =
290 pool_elt_at_index (sm->per_thread_data[thread_index].list_pool,
291 oldest_per_user_translation_list_index);
293 /* Get the session index from the list element */
294 session_index = oldest_per_user_translation_list_elt->value;
296 /* Get the session */
297 s = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
299 } while (snat_is_session_static (s));
301 /* Remove in2out, out2in keys */
302 kv0.key = s->in2out.as_u64;
303 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */))
304 clib_warning ("in2out key delete failed");
305 kv0.key = s->out2in.as_u64;
306 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */))
307 clib_warning ("out2in key delete failed");
310 snat_ipfix_logging_nat44_ses_delete(s->in2out.addr.as_u32,
311 s->out2in.addr.as_u32,
315 s->in2out.fib_index);
317 snat_free_outside_address_and_port
318 (sm, &s->out2in, s->outside_address_index);
319 s->outside_address_index = ~0;
321 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0, &key1,
326 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
327 return SNAT_IN2OUT_NEXT_DROP;
329 s->outside_address_index = address_index;
333 u8 static_mapping = 1;
335 /* First try to match static mapping by local address and port */
336 if (snat_static_mapping_match (sm, *key0, &key1, 0))
339 /* Try to create dynamic translation */
340 if (snat_alloc_outside_address_and_port (sm, rx_fib_index0, &key1,
343 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
344 return SNAT_IN2OUT_NEXT_DROP;
348 /* Create a new session */
349 pool_get (sm->per_thread_data[thread_index].sessions, s);
350 memset (s, 0, sizeof (*s));
352 s->outside_address_index = address_index;
356 u->nstaticsessions++;
357 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
364 /* Create list elts */
365 pool_get (sm->per_thread_data[thread_index].list_pool,
366 per_user_translation_list_elt);
367 clib_dlist_init (sm->per_thread_data[thread_index].list_pool,
368 per_user_translation_list_elt -
369 sm->per_thread_data[thread_index].list_pool);
371 per_user_translation_list_elt->value =
372 s - sm->per_thread_data[thread_index].sessions;
373 s->per_user_index = per_user_translation_list_elt -
374 sm->per_thread_data[thread_index].list_pool;
375 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
377 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
378 s->per_user_list_head_index,
379 per_user_translation_list_elt -
380 sm->per_thread_data[thread_index].list_pool);
385 s->out2in.protocol = key0->protocol;
386 s->out2in.fib_index = outside_fib_index;
389 /* Add to translation hashes */
390 kv0.key = s->in2out.as_u64;
391 kv0.value = s - sm->per_thread_data[thread_index].sessions;
392 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
393 clib_warning ("in2out key add failed");
395 kv0.key = s->out2in.as_u64;
396 kv0.value = s - sm->per_thread_data[thread_index].sessions;
398 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
399 clib_warning ("out2in key add failed");
401 /* Add to translated packets worker lookup */
402 worker_by_out_key.addr = s->out2in.addr;
403 worker_by_out_key.port = s->out2in.port;
404 worker_by_out_key.fib_index = s->out2in.fib_index;
405 kv0.key = worker_by_out_key.as_u64;
406 kv0.value = thread_index;
407 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
410 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
411 s->out2in.addr.as_u32,
415 s->in2out.fib_index);
420 snat_in2out_error_t icmp_get_key(ip4_header_t *ip0,
421 snat_session_key_t *p_key0)
423 icmp46_header_t *icmp0;
424 snat_session_key_t key0;
425 icmp_echo_header_t *echo0, *inner_echo0 = 0;
426 ip4_header_t *inner_ip0 = 0;
428 icmp46_header_t *inner_icmp0;
430 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
431 echo0 = (icmp_echo_header_t *)(icmp0+1);
433 if (!icmp_is_error_message (icmp0))
435 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request))
436 return SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE;
437 key0.protocol = SNAT_PROTOCOL_ICMP;
438 key0.addr = ip0->src_address;
439 key0.port = echo0->identifier;
443 inner_ip0 = (ip4_header_t *)(echo0+1);
444 l4_header = ip4_next_header (inner_ip0);
445 key0.protocol = ip_proto_to_snat_proto (inner_ip0->protocol);
446 key0.addr = inner_ip0->dst_address;
447 switch (key0.protocol)
449 case SNAT_PROTOCOL_ICMP:
450 inner_icmp0 = (icmp46_header_t*)l4_header;
451 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
452 key0.port = inner_echo0->identifier;
454 case SNAT_PROTOCOL_UDP:
455 case SNAT_PROTOCOL_TCP:
456 key0.port = ((tcp_udp_header_t*)l4_header)->dst_port;
459 return SNAT_IN2OUT_ERROR_UNSUPPORTED_PROTOCOL;
463 return -1; /* success */
467 * Get address and port values to be used for packet SNAT translation
468 * and create session if needed
470 * @param[in,out] sm SNAT main
471 * @param[in,out] node SNAT node runtime
472 * @param[in] thread_index thread index
473 * @param[in,out] b0 buffer containing packet to be translated
474 * @param[out] p_key address and port before NAT translation
475 * @param[out] p_value address and port after NAT translation
476 * @param[out] p_dont_translate if packet should not be translated
477 * @param d optional parameter
479 u32 icmp_match_in2out_slow(snat_main_t *sm, vlib_node_runtime_t *node,
480 u32 thread_index, vlib_buffer_t *b0,
481 snat_session_key_t *p_key,
482 snat_session_key_t *p_value,
483 u8 *p_dont_translate, void *d)
487 icmp46_header_t *icmp0;
490 snat_session_key_t key0;
491 snat_session_t *s0 = 0;
492 u8 dont_translate = 0;
493 clib_bihash_kv_8_8_t kv0, value0;
497 rt = (snat_runtime_t *) node->runtime_data;
498 ip0 = vlib_buffer_get_current (b0);
499 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
500 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
501 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
503 err = icmp_get_key (ip0, &key0);
506 b0->error = node->errors[err];
507 next0 = SNAT_IN2OUT_NEXT_DROP;
510 key0.fib_index = rx_fib_index0;
512 kv0.key = key0.as_u64;
514 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
516 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0,
517 IP_PROTOCOL_ICMP, rx_fib_index0)))
523 if (icmp_is_error_message (icmp0))
525 next0 = SNAT_IN2OUT_NEXT_DROP;
529 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
530 &s0, node, next0, thread_index);
532 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
536 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
542 *p_value = s0->out2in;
543 *p_dont_translate = dont_translate;
545 *(snat_session_t**)d = s0;
550 * Get address and port values to be used for packet SNAT translation
552 * @param[in] sm SNAT main
553 * @param[in,out] node SNAT node runtime
554 * @param[in] thread_index thread index
555 * @param[in,out] b0 buffer containing packet to be translated
556 * @param[out] p_key address and port before NAT translation
557 * @param[out] p_value address and port after NAT translation
558 * @param[out] p_dont_translate if packet should not be translated
559 * @param d optional parameter
561 u32 icmp_match_in2out_fast(snat_main_t *sm, vlib_node_runtime_t *node,
562 u32 thread_index, vlib_buffer_t *b0,
563 snat_session_key_t *p_key,
564 snat_session_key_t *p_value,
565 u8 *p_dont_translate, void *d)
569 icmp46_header_t *icmp0;
572 snat_session_key_t key0;
573 snat_session_key_t sm0;
574 u8 dont_translate = 0;
578 rt = (snat_runtime_t *) node->runtime_data;
579 ip0 = vlib_buffer_get_current (b0);
580 icmp0 = (icmp46_header_t *) ip4_next_header (ip0);
581 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
582 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (sw_if_index0);
584 err = icmp_get_key (ip0, &key0);
587 b0->error = node->errors[err];
588 next0 = SNAT_IN2OUT_NEXT_DROP;
591 key0.fib_index = rx_fib_index0;
593 if (snat_static_mapping_match(sm, key0, &sm0, 0))
595 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0,
596 IP_PROTOCOL_ICMP, rx_fib_index0)))
602 if (icmp_is_error_message (icmp0))
604 next0 = SNAT_IN2OUT_NEXT_DROP;
608 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
609 next0 = SNAT_IN2OUT_NEXT_DROP;
617 *p_dont_translate = dont_translate;
621 static inline u32 icmp_in2out (snat_main_t *sm,
624 icmp46_header_t * icmp0,
627 vlib_node_runtime_t * node,
632 snat_session_key_t key0, sm0;
633 icmp_echo_header_t *echo0, *inner_echo0 = 0;
634 ip4_header_t *inner_ip0;
636 icmp46_header_t *inner_icmp0;
638 u32 new_addr0, old_addr0;
639 u16 old_id0, new_id0;
644 echo0 = (icmp_echo_header_t *)(icmp0+1);
646 next0_tmp = sm->icmp_match_in2out_cb(sm, node, thread_index, b0,
647 &key0, &sm0, &dont_translate, d);
650 if (next0 == SNAT_IN2OUT_NEXT_DROP || dont_translate)
653 sum0 = ip_incremental_checksum (0, icmp0,
654 ntohs(ip0->length) - ip4_header_bytes (ip0));
655 checksum0 = ~ip_csum_fold (sum0);
656 if (PREDICT_FALSE(checksum0 != 0 && checksum0 != 0xffff))
658 next0 = SNAT_IN2OUT_NEXT_DROP;
662 old_addr0 = ip0->src_address.as_u32;
663 new_addr0 = ip0->src_address.as_u32 = sm0.addr.as_u32;
664 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
666 sum0 = ip0->checksum;
667 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
668 src_address /* changed member */);
669 ip0->checksum = ip_csum_fold (sum0);
671 if (!icmp_is_error_message (icmp0))
674 if (PREDICT_FALSE(new_id0 != echo0->identifier))
676 old_id0 = echo0->identifier;
678 echo0->identifier = new_id0;
680 sum0 = icmp0->checksum;
681 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
683 icmp0->checksum = ip_csum_fold (sum0);
688 inner_ip0 = (ip4_header_t *)(echo0+1);
689 l4_header = ip4_next_header (inner_ip0);
691 if (!ip4_header_checksum_is_valid (inner_ip0))
693 next0 = SNAT_IN2OUT_NEXT_DROP;
697 old_addr0 = inner_ip0->dst_address.as_u32;
698 inner_ip0->dst_address = sm0.addr;
699 new_addr0 = inner_ip0->dst_address.as_u32;
701 sum0 = icmp0->checksum;
702 sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
703 dst_address /* changed member */);
704 icmp0->checksum = ip_csum_fold (sum0);
706 switch (key0.protocol)
708 case SNAT_PROTOCOL_ICMP:
709 inner_icmp0 = (icmp46_header_t*)l4_header;
710 inner_echo0 = (icmp_echo_header_t *)(inner_icmp0+1);
712 old_id0 = inner_echo0->identifier;
714 inner_echo0->identifier = new_id0;
716 sum0 = icmp0->checksum;
717 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
719 icmp0->checksum = ip_csum_fold (sum0);
721 case SNAT_PROTOCOL_UDP:
722 case SNAT_PROTOCOL_TCP:
723 old_id0 = ((tcp_udp_header_t*)l4_header)->dst_port;
725 ((tcp_udp_header_t*)l4_header)->dst_port = new_id0;
727 sum0 = icmp0->checksum;
728 sum0 = ip_csum_update (sum0, old_id0, new_id0, tcp_udp_header_t,
730 icmp0->checksum = ip_csum_fold (sum0);
744 * Hairpinning allows two endpoints on the internal side of the NAT to
745 * communicate even if they only use each other's external IP addresses
748 * @param sm SNAT main.
749 * @param b0 Vlib buffer.
750 * @param ip0 IP header.
751 * @param udp0 UDP header.
752 * @param tcp0 TCP header.
753 * @param proto0 SNAT protocol.
756 snat_hairpinning (snat_main_t *sm,
763 snat_session_key_t key0, sm0;
764 snat_worker_key_t k0;
766 clib_bihash_kv_8_8_t kv0, value0;
768 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
769 u16 new_dst_port0, old_dst_port0;
771 key0.addr = ip0->dst_address;
772 key0.port = udp0->dst_port;
773 key0.protocol = proto0;
774 key0.fib_index = sm->outside_fib_index;
775 kv0.key = key0.as_u64;
777 /* Check if destination is in active sessions */
778 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
780 /* or static mappings */
781 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
783 new_dst_addr0 = sm0.addr.as_u32;
784 new_dst_port0 = sm0.port;
785 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
791 if (sm->num_workers > 1)
793 k0.addr = ip0->dst_address;
794 k0.port = udp0->dst_port;
795 k0.fib_index = sm->outside_fib_index;
797 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
803 ti = sm->num_workers;
805 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
806 new_dst_addr0 = s0->in2out.addr.as_u32;
807 new_dst_port0 = s0->in2out.port;
808 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
811 /* Destination is behind the same NAT, use internal address and port */
814 old_dst_addr0 = ip0->dst_address.as_u32;
815 ip0->dst_address.as_u32 = new_dst_addr0;
816 sum0 = ip0->checksum;
817 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
818 ip4_header_t, dst_address);
819 ip0->checksum = ip_csum_fold (sum0);
821 old_dst_port0 = tcp0->dst;
822 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
824 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
826 tcp0->dst = new_dst_port0;
827 sum0 = tcp0->checksum;
828 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
829 ip4_header_t, dst_address);
830 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
831 ip4_header_t /* cheat */, length);
832 tcp0->checksum = ip_csum_fold(sum0);
836 udp0->dst_port = new_dst_port0;
843 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
846 icmp46_header_t * icmp0,
849 vlib_node_runtime_t * node,
853 snat_session_t ** p_s0)
855 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
856 next0, thread_index, p_s0);
857 snat_session_t * s0 = *p_s0;
858 if (PREDICT_TRUE(next0 != SNAT_IN2OUT_NEXT_DROP && s0))
861 s0->last_heard = now;
863 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
864 /* Per-user LRU list maintenance for dynamic translations */
865 if (!snat_is_session_static (s0))
867 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
869 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
870 s0->per_user_list_head_index,
878 snat_in2out_node_fn_inline (vlib_main_t * vm,
879 vlib_node_runtime_t * node,
880 vlib_frame_t * frame, int is_slow_path)
882 u32 n_left_from, * from, * to_next;
883 snat_in2out_next_t next_index;
884 u32 pkts_processed = 0;
885 snat_main_t * sm = &snat_main;
886 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
887 f64 now = vlib_time_now (vm);
888 u32 stats_node_index;
889 u32 thread_index = vlib_get_thread_index ();
891 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
892 snat_in2out_node.index;
894 from = vlib_frame_vector_args (frame);
895 n_left_from = frame->n_vectors;
896 next_index = node->cached_next_index;
898 while (n_left_from > 0)
902 vlib_get_next_frame (vm, node, next_index,
903 to_next, n_left_to_next);
905 while (n_left_from >= 4 && n_left_to_next >= 2)
908 vlib_buffer_t * b0, * b1;
910 u32 sw_if_index0, sw_if_index1;
911 ip4_header_t * ip0, * ip1;
912 ip_csum_t sum0, sum1;
913 u32 new_addr0, old_addr0, new_addr1, old_addr1;
914 u16 old_port0, new_port0, old_port1, new_port1;
915 udp_header_t * udp0, * udp1;
916 tcp_header_t * tcp0, * tcp1;
917 icmp46_header_t * icmp0, * icmp1;
918 snat_session_key_t key0, key1;
919 u32 rx_fib_index0, rx_fib_index1;
921 snat_session_t * s0 = 0, * s1 = 0;
922 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
924 /* Prefetch next iteration. */
926 vlib_buffer_t * p2, * p3;
928 p2 = vlib_get_buffer (vm, from[2]);
929 p3 = vlib_get_buffer (vm, from[3]);
931 vlib_prefetch_buffer_header (p2, LOAD);
932 vlib_prefetch_buffer_header (p3, LOAD);
934 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
935 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
938 /* speculatively enqueue b0 and b1 to the current next frame */
939 to_next[0] = bi0 = from[0];
940 to_next[1] = bi1 = from[1];
946 b0 = vlib_get_buffer (vm, bi0);
947 b1 = vlib_get_buffer (vm, bi1);
949 ip0 = vlib_buffer_get_current (b0);
950 udp0 = ip4_next_header (ip0);
951 tcp0 = (tcp_header_t *) udp0;
952 icmp0 = (icmp46_header_t *) udp0;
954 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
955 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
958 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
960 if (PREDICT_FALSE(ip0->ttl == 1))
962 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
963 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
964 ICMP4_time_exceeded_ttl_exceeded_in_transit,
966 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
970 proto0 = ip_proto_to_snat_proto (ip0->protocol);
972 /* Next configured feature, probably ip4-lookup */
975 if (PREDICT_FALSE (proto0 == ~0))
978 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
980 next0 = icmp_in2out_slow_path
981 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
982 node, next0, now, thread_index, &s0);
988 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
990 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
995 key0.addr = ip0->src_address;
996 key0.port = udp0->src_port;
997 key0.protocol = proto0;
998 key0.fib_index = rx_fib_index0;
1000 kv0.key = key0.as_u64;
1002 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
1006 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0,
1007 proto0, rx_fib_index0)))
1010 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1011 &s0, node, next0, thread_index);
1012 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1017 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1022 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1025 old_addr0 = ip0->src_address.as_u32;
1026 ip0->src_address = s0->out2in.addr;
1027 new_addr0 = ip0->src_address.as_u32;
1028 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1030 sum0 = ip0->checksum;
1031 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1033 src_address /* changed member */);
1034 ip0->checksum = ip_csum_fold (sum0);
1036 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1038 old_port0 = tcp0->src_port;
1039 tcp0->src_port = s0->out2in.port;
1040 new_port0 = tcp0->src_port;
1042 sum0 = tcp0->checksum;
1043 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1045 dst_address /* changed member */);
1046 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1047 ip4_header_t /* cheat */,
1048 length /* changed member */);
1049 tcp0->checksum = ip_csum_fold(sum0);
1053 old_port0 = udp0->src_port;
1054 udp0->src_port = s0->out2in.port;
1059 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1062 s0->last_heard = now;
1064 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1065 /* Per-user LRU list maintenance for dynamic translation */
1066 if (!snat_is_session_static (s0))
1068 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1069 s0->per_user_index);
1070 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1071 s0->per_user_list_head_index,
1072 s0->per_user_index);
1076 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1077 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1079 snat_in2out_trace_t *t =
1080 vlib_add_trace (vm, node, b0, sizeof (*t));
1081 t->is_slow_path = is_slow_path;
1082 t->sw_if_index = sw_if_index0;
1083 t->next_index = next0;
1084 t->session_index = ~0;
1086 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1089 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1091 ip1 = vlib_buffer_get_current (b1);
1092 udp1 = ip4_next_header (ip1);
1093 tcp1 = (tcp_header_t *) udp1;
1094 icmp1 = (icmp46_header_t *) udp1;
1096 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1097 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1100 if (PREDICT_FALSE(ip1->ttl == 1))
1102 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1103 icmp4_error_set_vnet_buffer (b1, ICMP4_time_exceeded,
1104 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1106 next1 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1110 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1112 /* Next configured feature, probably ip4-lookup */
1115 if (PREDICT_FALSE (proto1 == ~0))
1118 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
1120 next1 = icmp_in2out_slow_path
1121 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
1122 next1, now, thread_index, &s1);
1128 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
1130 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1135 key1.addr = ip1->src_address;
1136 key1.port = udp1->src_port;
1137 key1.protocol = proto1;
1138 key1.fib_index = rx_fib_index1;
1140 kv1.key = key1.as_u64;
1142 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
1146 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index1, ip1,
1147 proto1, rx_fib_index1)))
1150 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
1151 &s1, node, next1, thread_index);
1152 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
1157 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1162 s1 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1165 old_addr1 = ip1->src_address.as_u32;
1166 ip1->src_address = s1->out2in.addr;
1167 new_addr1 = ip1->src_address.as_u32;
1168 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
1170 sum1 = ip1->checksum;
1171 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1173 src_address /* changed member */);
1174 ip1->checksum = ip_csum_fold (sum1);
1176 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1178 old_port1 = tcp1->src_port;
1179 tcp1->src_port = s1->out2in.port;
1180 new_port1 = tcp1->src_port;
1182 sum1 = tcp1->checksum;
1183 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
1185 dst_address /* changed member */);
1186 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1187 ip4_header_t /* cheat */,
1188 length /* changed member */);
1189 tcp1->checksum = ip_csum_fold(sum1);
1193 old_port1 = udp1->src_port;
1194 udp1->src_port = s1->out2in.port;
1199 snat_hairpinning (sm, b1, ip1, udp1, tcp1, proto1);
1202 s1->last_heard = now;
1204 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
1205 /* Per-user LRU list maintenance for dynamic translation */
1206 if (!snat_is_session_static (s1))
1208 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1209 s1->per_user_index);
1210 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1211 s1->per_user_list_head_index,
1212 s1->per_user_index);
1216 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1217 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1219 snat_in2out_trace_t *t =
1220 vlib_add_trace (vm, node, b1, sizeof (*t));
1221 t->sw_if_index = sw_if_index1;
1222 t->next_index = next1;
1223 t->session_index = ~0;
1225 t->session_index = s1 - sm->per_thread_data[thread_index].sessions;
1228 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1230 /* verify speculative enqueues, maybe switch current next frame */
1231 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1232 to_next, n_left_to_next,
1233 bi0, bi1, next0, next1);
1236 while (n_left_from > 0 && n_left_to_next > 0)
1244 u32 new_addr0, old_addr0;
1245 u16 old_port0, new_port0;
1246 udp_header_t * udp0;
1247 tcp_header_t * tcp0;
1248 icmp46_header_t * icmp0;
1249 snat_session_key_t key0;
1252 snat_session_t * s0 = 0;
1253 clib_bihash_kv_8_8_t kv0, value0;
1255 /* speculatively enqueue b0 to the current next frame */
1261 n_left_to_next -= 1;
1263 b0 = vlib_get_buffer (vm, bi0);
1264 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1266 ip0 = vlib_buffer_get_current (b0);
1267 udp0 = ip4_next_header (ip0);
1268 tcp0 = (tcp_header_t *) udp0;
1269 icmp0 = (icmp46_header_t *) udp0;
1271 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1272 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
1275 if (PREDICT_FALSE(ip0->ttl == 1))
1277 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
1278 icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
1279 ICMP4_time_exceeded_ttl_exceeded_in_transit,
1281 next0 = SNAT_IN2OUT_NEXT_ICMP_ERROR;
1285 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1287 /* Next configured feature, probably ip4-lookup */
1290 if (PREDICT_FALSE (proto0 == ~0))
1293 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1295 next0 = icmp_in2out_slow_path
1296 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1297 next0, now, thread_index, &s0);
1303 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
1305 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1310 key0.addr = ip0->src_address;
1311 key0.port = udp0->src_port;
1312 key0.protocol = proto0;
1313 key0.fib_index = rx_fib_index0;
1315 kv0.key = key0.as_u64;
1317 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
1321 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0,
1322 proto0, rx_fib_index0)))
1325 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
1326 &s0, node, next0, thread_index);
1328 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1333 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1338 s0 = pool_elt_at_index (sm->per_thread_data[thread_index].sessions,
1341 old_addr0 = ip0->src_address.as_u32;
1342 ip0->src_address = s0->out2in.addr;
1343 new_addr0 = ip0->src_address.as_u32;
1344 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1346 sum0 = ip0->checksum;
1347 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1349 src_address /* changed member */);
1350 ip0->checksum = ip_csum_fold (sum0);
1352 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1354 old_port0 = tcp0->src_port;
1355 tcp0->src_port = s0->out2in.port;
1356 new_port0 = tcp0->src_port;
1358 sum0 = tcp0->checksum;
1359 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1361 dst_address /* changed member */);
1362 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1363 ip4_header_t /* cheat */,
1364 length /* changed member */);
1365 tcp0->checksum = ip_csum_fold(sum0);
1369 old_port0 = udp0->src_port;
1370 udp0->src_port = s0->out2in.port;
1375 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1378 s0->last_heard = now;
1380 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1381 /* Per-user LRU list maintenance for dynamic translation */
1382 if (!snat_is_session_static (s0))
1384 clib_dlist_remove (sm->per_thread_data[thread_index].list_pool,
1385 s0->per_user_index);
1386 clib_dlist_addtail (sm->per_thread_data[thread_index].list_pool,
1387 s0->per_user_list_head_index,
1388 s0->per_user_index);
1392 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1393 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1395 snat_in2out_trace_t *t =
1396 vlib_add_trace (vm, node, b0, sizeof (*t));
1397 t->is_slow_path = is_slow_path;
1398 t->sw_if_index = sw_if_index0;
1399 t->next_index = next0;
1400 t->session_index = ~0;
1402 t->session_index = s0 - sm->per_thread_data[thread_index].sessions;
1405 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1407 /* verify speculative enqueue, maybe switch current next frame */
1408 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1409 to_next, n_left_to_next,
1413 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1416 vlib_node_increment_counter (vm, stats_node_index,
1417 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1419 return frame->n_vectors;
1423 snat_in2out_fast_path_fn (vlib_main_t * vm,
1424 vlib_node_runtime_t * node,
1425 vlib_frame_t * frame)
1427 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
1430 VLIB_REGISTER_NODE (snat_in2out_node) = {
1431 .function = snat_in2out_fast_path_fn,
1432 .name = "snat-in2out",
1433 .vector_size = sizeof (u32),
1434 .format_trace = format_snat_in2out_trace,
1435 .type = VLIB_NODE_TYPE_INTERNAL,
1437 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1438 .error_strings = snat_in2out_error_strings,
1440 .runtime_data_bytes = sizeof (snat_runtime_t),
1442 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1444 /* edit / add dispositions here */
1446 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1447 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1448 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1449 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1453 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1456 snat_in2out_slow_path_fn (vlib_main_t * vm,
1457 vlib_node_runtime_t * node,
1458 vlib_frame_t * frame)
1460 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
1463 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1464 .function = snat_in2out_slow_path_fn,
1465 .name = "snat-in2out-slowpath",
1466 .vector_size = sizeof (u32),
1467 .format_trace = format_snat_in2out_trace,
1468 .type = VLIB_NODE_TYPE_INTERNAL,
1470 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1471 .error_strings = snat_in2out_error_strings,
1473 .runtime_data_bytes = sizeof (snat_runtime_t),
1475 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1477 /* edit / add dispositions here */
1479 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1480 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1481 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1482 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
1486 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1488 /**************************/
1489 /*** deterministic mode ***/
1490 /**************************/
1492 snat_det_in2out_node_fn (vlib_main_t * vm,
1493 vlib_node_runtime_t * node,
1494 vlib_frame_t * frame)
1496 u32 n_left_from, * from, * to_next;
1497 snat_in2out_next_t next_index;
1498 u32 pkts_processed = 0;
1499 snat_main_t * sm = &snat_main;
1500 u32 now = (u32) vlib_time_now (vm);
1502 from = vlib_frame_vector_args (frame);
1503 n_left_from = frame->n_vectors;
1504 next_index = node->cached_next_index;
1506 while (n_left_from > 0)
1510 vlib_get_next_frame (vm, node, next_index,
1511 to_next, n_left_to_next);
1513 while (n_left_from >= 4 && n_left_to_next >= 2)
1516 vlib_buffer_t * b0, * b1;
1518 u32 sw_if_index0, sw_if_index1;
1519 ip4_header_t * ip0, * ip1;
1520 ip_csum_t sum0, sum1;
1521 ip4_address_t new_addr0, old_addr0, new_addr1, old_addr1;
1522 u16 old_port0, new_port0, lo_port0, i0;
1523 u16 old_port1, new_port1, lo_port1, i1;
1524 udp_header_t * udp0, * udp1;
1525 tcp_header_t * tcp0, * tcp1;
1527 snat_det_out_key_t key0, key1;
1528 snat_det_map_t * dm0, * dm1;
1529 snat_det_session_t * ses0 = 0, * ses1 = 0;
1531 /* Prefetch next iteration. */
1533 vlib_buffer_t * p2, * p3;
1535 p2 = vlib_get_buffer (vm, from[2]);
1536 p3 = vlib_get_buffer (vm, from[3]);
1538 vlib_prefetch_buffer_header (p2, LOAD);
1539 vlib_prefetch_buffer_header (p3, LOAD);
1541 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
1542 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
1545 /* speculatively enqueue b0 and b1 to the current next frame */
1546 to_next[0] = bi0 = from[0];
1547 to_next[1] = bi1 = from[1];
1551 n_left_to_next -= 2;
1553 b0 = vlib_get_buffer (vm, bi0);
1554 b1 = vlib_get_buffer (vm, bi1);
1556 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1557 next1 = SNAT_IN2OUT_NEXT_LOOKUP;
1559 ip0 = vlib_buffer_get_current (b0);
1560 udp0 = ip4_next_header (ip0);
1561 tcp0 = (tcp_header_t *) udp0;
1563 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1565 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
1566 if (PREDICT_FALSE(!dm0))
1568 clib_warning("no match for internal host %U",
1569 format_ip4_address, &ip0->src_address);
1570 next0 = SNAT_IN2OUT_NEXT_DROP;
1571 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1575 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
1577 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src);
1578 if (PREDICT_FALSE(!ses0))
1580 key0.ext_host_addr = ip0->dst_address;
1581 key0.ext_host_port = tcp0->dst;
1582 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
1584 key0.out_port = clib_host_to_net_u16 (lo_port0 +
1585 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
1587 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
1590 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
1593 if (PREDICT_FALSE(!ses0))
1595 next0 = SNAT_IN2OUT_NEXT_DROP;
1596 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1601 new_port0 = ses0->out.out_port;
1602 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1604 old_addr0.as_u32 = ip0->src_address.as_u32;
1605 ip0->src_address.as_u32 = new_addr0.as_u32;
1606 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1608 sum0 = ip0->checksum;
1609 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1611 src_address /* changed member */);
1612 ip0->checksum = ip_csum_fold (sum0);
1614 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1616 if (tcp0->flags & TCP_FLAG_SYN)
1617 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
1618 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
1619 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1620 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
1621 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
1622 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
1623 snat_det_ses_close(dm0, ses0);
1624 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1625 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
1626 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
1627 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1629 old_port0 = tcp0->src;
1630 tcp0->src = new_port0;
1632 sum0 = tcp0->checksum;
1633 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1635 dst_address /* changed member */);
1636 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1637 ip4_header_t /* cheat */,
1638 length /* changed member */);
1639 tcp0->checksum = ip_csum_fold(sum0);
1643 ses0->state = SNAT_SESSION_UDP_ACTIVE;
1644 old_port0 = udp0->src_port;
1645 udp0->src_port = new_port0;
1651 case SNAT_SESSION_UDP_ACTIVE:
1652 ses0->expire = now + SNAT_UDP_TIMEOUT;
1654 case SNAT_SESSION_TCP_SYN_SENT:
1655 case SNAT_SESSION_TCP_FIN_WAIT:
1656 case SNAT_SESSION_TCP_CLOSE_WAIT:
1657 case SNAT_SESSION_TCP_LAST_ACK:
1658 ses0->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
1660 case SNAT_SESSION_TCP_ESTABLISHED:
1661 ses0->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
1666 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1667 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1669 snat_in2out_trace_t *t =
1670 vlib_add_trace (vm, node, b0, sizeof (*t));
1671 t->is_slow_path = 0;
1672 t->sw_if_index = sw_if_index0;
1673 t->next_index = next0;
1674 t->session_index = ~0;
1676 t->session_index = ses0 - dm0->sessions;
1679 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1681 ip1 = vlib_buffer_get_current (b1);
1682 udp1 = ip4_next_header (ip1);
1683 tcp1 = (tcp_header_t *) udp1;
1685 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
1687 dm1 = snat_det_map_by_user(sm, &ip1->src_address);
1688 if (PREDICT_FALSE(!dm1))
1690 clib_warning("no match for internal host %U",
1691 format_ip4_address, &ip0->src_address);
1692 next1 = SNAT_IN2OUT_NEXT_DROP;
1693 b1->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1697 snat_det_forward(dm1, &ip1->src_address, &new_addr1, &lo_port1);
1699 ses1 = snat_det_find_ses_by_in(dm1, &ip1->src_address, tcp1->src);
1700 if (PREDICT_FALSE(!ses1))
1702 key1.ext_host_addr = ip1->dst_address;
1703 key1.ext_host_port = tcp1->dst;
1704 for (i1 = 0; i1 < dm1->ports_per_host; i1++)
1706 key1.out_port = clib_host_to_net_u16 (lo_port1 +
1707 ((i1 + clib_net_to_host_u16 (tcp1->src)) % dm1->ports_per_host));
1709 if (snat_det_get_ses_by_out (dm1, &ip1->src_address, key1.as_u64))
1712 ses1 = snat_det_ses_create(dm1, &ip1->src_address, tcp1->src, &key1);
1715 if (PREDICT_FALSE(!ses1))
1717 next1 = SNAT_IN2OUT_NEXT_DROP;
1718 b1->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1723 new_port1 = ses1->out.out_port;
1724 proto1 = ip_proto_to_snat_proto (ip1->protocol);
1726 old_addr1.as_u32 = ip1->src_address.as_u32;
1727 ip1->src_address.as_u32 = new_addr1.as_u32;
1728 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1730 sum1 = ip1->checksum;
1731 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1733 src_address /* changed member */);
1734 ip1->checksum = ip_csum_fold (sum1);
1736 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
1738 if (tcp1->flags & TCP_FLAG_SYN)
1739 ses1->state = SNAT_SESSION_TCP_SYN_SENT;
1740 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_SYN_SENT)
1741 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1742 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_ESTABLISHED)
1743 ses1->state = SNAT_SESSION_TCP_FIN_WAIT;
1744 else if (tcp1->flags & TCP_FLAG_ACK && ses1->state == SNAT_SESSION_TCP_FIN_WAIT)
1745 snat_det_ses_close(dm1, ses1);
1746 else if (tcp1->flags & TCP_FLAG_FIN && ses1->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1747 ses1->state = SNAT_SESSION_TCP_LAST_ACK;
1748 else if (tcp1->flags == 0 && ses1->state == SNAT_SESSION_UNKNOWN)
1749 ses1->state = SNAT_SESSION_TCP_ESTABLISHED;
1751 old_port1 = tcp1->src;
1752 tcp1->src = new_port1;
1754 sum1 = tcp1->checksum;
1755 sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32,
1757 dst_address /* changed member */);
1758 sum1 = ip_csum_update (sum1, old_port1, new_port1,
1759 ip4_header_t /* cheat */,
1760 length /* changed member */);
1761 tcp1->checksum = ip_csum_fold(sum1);
1765 ses1->state = SNAT_SESSION_UDP_ACTIVE;
1766 old_port1 = udp1->src_port;
1767 udp1->src_port = new_port1;
1773 case SNAT_SESSION_UDP_ACTIVE:
1774 ses1->expire = now + SNAT_UDP_TIMEOUT;
1776 case SNAT_SESSION_TCP_SYN_SENT:
1777 case SNAT_SESSION_TCP_FIN_WAIT:
1778 case SNAT_SESSION_TCP_CLOSE_WAIT:
1779 case SNAT_SESSION_TCP_LAST_ACK:
1780 ses1->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
1782 case SNAT_SESSION_TCP_ESTABLISHED:
1783 ses1->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
1788 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1789 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
1791 snat_in2out_trace_t *t =
1792 vlib_add_trace (vm, node, b1, sizeof (*t));
1793 t->is_slow_path = 0;
1794 t->sw_if_index = sw_if_index1;
1795 t->next_index = next1;
1796 t->session_index = ~0;
1798 t->session_index = ses1 - dm1->sessions;
1801 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
1803 /* verify speculative enqueues, maybe switch current next frame */
1804 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
1805 to_next, n_left_to_next,
1806 bi0, bi1, next0, next1);
1809 while (n_left_from > 0 && n_left_to_next > 0)
1817 ip4_address_t new_addr0, old_addr0;
1818 u16 old_port0, new_port0, lo_port0, i0;
1819 udp_header_t * udp0;
1820 tcp_header_t * tcp0;
1822 snat_det_out_key_t key0;
1823 snat_det_map_t * dm0;
1824 snat_det_session_t * ses0 = 0;
1826 /* speculatively enqueue b0 to the current next frame */
1832 n_left_to_next -= 1;
1834 b0 = vlib_get_buffer (vm, bi0);
1835 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1837 ip0 = vlib_buffer_get_current (b0);
1838 udp0 = ip4_next_header (ip0);
1839 tcp0 = (tcp_header_t *) udp0;
1841 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1843 dm0 = snat_det_map_by_user(sm, &ip0->src_address);
1844 if (PREDICT_FALSE(!dm0))
1846 clib_warning("no match for internal host %U",
1847 format_ip4_address, &ip0->src_address);
1848 next0 = SNAT_IN2OUT_NEXT_DROP;
1849 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1853 snat_det_forward(dm0, &ip0->src_address, &new_addr0, &lo_port0);
1855 ses0 = snat_det_find_ses_by_in(dm0, &ip0->src_address, tcp0->src);
1856 if (PREDICT_FALSE(!ses0))
1858 key0.ext_host_addr = ip0->dst_address;
1859 key0.ext_host_port = tcp0->dst;
1860 for (i0 = 0; i0 < dm0->ports_per_host; i0++)
1862 key0.out_port = clib_host_to_net_u16 (lo_port0 +
1863 ((i0 + clib_net_to_host_u16 (tcp0->src)) % dm0->ports_per_host));
1865 if (snat_det_get_ses_by_out (dm0, &ip0->src_address, key0.as_u64))
1868 ses0 = snat_det_ses_create(dm0, &ip0->src_address, tcp0->src, &key0);
1871 if (PREDICT_FALSE(!ses0))
1873 next0 = SNAT_IN2OUT_NEXT_DROP;
1874 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
1879 new_port0 = ses0->out.out_port;
1880 proto0 = ip_proto_to_snat_proto (ip0->protocol);
1882 old_addr0.as_u32 = ip0->src_address.as_u32;
1883 ip0->src_address.as_u32 = new_addr0.as_u32;
1884 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm->outside_fib_index;
1886 sum0 = ip0->checksum;
1887 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1889 src_address /* changed member */);
1890 ip0->checksum = ip_csum_fold (sum0);
1892 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1894 if (tcp0->flags & TCP_FLAG_SYN)
1895 ses0->state = SNAT_SESSION_TCP_SYN_SENT;
1896 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_SYN_SENT)
1897 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1898 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_ESTABLISHED)
1899 ses0->state = SNAT_SESSION_TCP_FIN_WAIT;
1900 else if (tcp0->flags & TCP_FLAG_ACK && ses0->state == SNAT_SESSION_TCP_FIN_WAIT)
1901 snat_det_ses_close(dm0, ses0);
1902 else if (tcp0->flags & TCP_FLAG_FIN && ses0->state == SNAT_SESSION_TCP_CLOSE_WAIT)
1903 ses0->state = SNAT_SESSION_TCP_LAST_ACK;
1904 else if (tcp0->flags == 0 && ses0->state == SNAT_SESSION_UNKNOWN)
1905 ses0->state = SNAT_SESSION_TCP_ESTABLISHED;
1907 old_port0 = tcp0->src;
1908 tcp0->src = new_port0;
1910 sum0 = tcp0->checksum;
1911 sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32,
1913 dst_address /* changed member */);
1914 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1915 ip4_header_t /* cheat */,
1916 length /* changed member */);
1917 tcp0->checksum = ip_csum_fold(sum0);
1921 ses0->state = SNAT_SESSION_UDP_ACTIVE;
1922 old_port0 = udp0->src_port;
1923 udp0->src_port = new_port0;
1929 case SNAT_SESSION_UDP_ACTIVE:
1930 ses0->expire = now + SNAT_UDP_TIMEOUT;
1932 case SNAT_SESSION_TCP_SYN_SENT:
1933 case SNAT_SESSION_TCP_FIN_WAIT:
1934 case SNAT_SESSION_TCP_CLOSE_WAIT:
1935 case SNAT_SESSION_TCP_LAST_ACK:
1936 ses0->expire = now + SNAT_TCP_TRANSITORY_TIMEOUT;
1938 case SNAT_SESSION_TCP_ESTABLISHED:
1939 ses0->expire = now + SNAT_TCP_ESTABLISHED_TIMEOUT;
1944 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1945 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1947 snat_in2out_trace_t *t =
1948 vlib_add_trace (vm, node, b0, sizeof (*t));
1949 t->is_slow_path = 0;
1950 t->sw_if_index = sw_if_index0;
1951 t->next_index = next0;
1952 t->session_index = ~0;
1954 t->session_index = ses0 - dm0->sessions;
1957 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1959 /* verify speculative enqueue, maybe switch current next frame */
1960 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1961 to_next, n_left_to_next,
1965 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1968 vlib_node_increment_counter (vm, snat_det_in2out_node.index,
1969 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1971 return frame->n_vectors;
1974 VLIB_REGISTER_NODE (snat_det_in2out_node) = {
1975 .function = snat_det_in2out_node_fn,
1976 .name = "snat-det-in2out",
1977 .vector_size = sizeof (u32),
1978 .format_trace = format_snat_in2out_trace,
1979 .type = VLIB_NODE_TYPE_INTERNAL,
1981 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1982 .error_strings = snat_in2out_error_strings,
1984 .runtime_data_bytes = sizeof (snat_runtime_t),
1988 /* edit / add dispositions here */
1990 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1991 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1995 VLIB_NODE_FUNCTION_MULTIARCH (snat_det_in2out_node, snat_det_in2out_node_fn);
1997 /**********************/
1998 /*** worker handoff ***/
1999 /**********************/
2001 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
2002 vlib_node_runtime_t * node,
2003 vlib_frame_t * frame)
2005 snat_main_t *sm = &snat_main;
2006 vlib_thread_main_t *tm = vlib_get_thread_main ();
2007 u32 n_left_from, *from, *to_next = 0;
2008 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
2009 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
2011 vlib_frame_queue_elt_t *hf = 0;
2012 vlib_frame_t *f = 0;
2014 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
2015 u32 next_worker_index = 0;
2016 u32 current_worker_index = ~0;
2017 u32 thread_index = vlib_get_thread_index ();
2019 ASSERT (vec_len (sm->workers));
2021 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
2023 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
2025 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
2026 sm->first_worker_index + sm->num_workers - 1,
2027 (vlib_frame_queue_t *) (~0));
2030 from = vlib_frame_vector_args (frame);
2031 n_left_from = frame->n_vectors;
2033 while (n_left_from > 0)
2046 b0 = vlib_get_buffer (vm, bi0);
2048 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
2049 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2051 ip0 = vlib_buffer_get_current (b0);
2053 next_worker_index = sm->worker_in2out_cb(ip0, rx_fib_index0);
2055 if (PREDICT_FALSE (next_worker_index != thread_index))
2059 if (next_worker_index != current_worker_index)
2062 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2064 hf = vlib_get_worker_handoff_queue_elt (sm->fq_in2out_index,
2066 handoff_queue_elt_by_worker_index);
2068 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
2069 to_next_worker = &hf->buffer_index[hf->n_vectors];
2070 current_worker_index = next_worker_index;
2073 /* enqueue to correct worker thread */
2074 to_next_worker[0] = bi0;
2076 n_left_to_next_worker--;
2078 if (n_left_to_next_worker == 0)
2080 hf->n_vectors = VLIB_FRAME_SIZE;
2081 vlib_put_frame_queue_elt (hf);
2082 current_worker_index = ~0;
2083 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
2090 /* if this is 1st frame */
2093 f = vlib_get_frame_to_node (vm, sm->in2out_node_index);
2094 to_next = vlib_frame_vector_args (f);
2102 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
2103 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2105 snat_in2out_worker_handoff_trace_t *t =
2106 vlib_add_trace (vm, node, b0, sizeof (*t));
2107 t->next_worker_index = next_worker_index;
2108 t->do_handoff = do_handoff;
2113 vlib_put_frame_to_node (vm, sm->in2out_node_index, f);
2116 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
2118 /* Ship frames to the worker nodes */
2119 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
2121 if (handoff_queue_elt_by_worker_index[i])
2123 hf = handoff_queue_elt_by_worker_index[i];
2125 * It works better to let the handoff node
2126 * rate-adapt, always ship the handoff queue element.
2128 if (1 || hf->n_vectors == hf->last_n_vectors)
2130 vlib_put_frame_queue_elt (hf);
2131 handoff_queue_elt_by_worker_index[i] = 0;
2134 hf->last_n_vectors = hf->n_vectors;
2136 congested_handoff_queue_by_worker_index[i] =
2137 (vlib_frame_queue_t *) (~0);
2140 current_worker_index = ~0;
2141 return frame->n_vectors;
2144 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
2145 .function = snat_in2out_worker_handoff_fn,
2146 .name = "snat-in2out-worker-handoff",
2147 .vector_size = sizeof (u32),
2148 .format_trace = format_snat_in2out_worker_handoff_trace,
2149 .type = VLIB_NODE_TYPE_INTERNAL,
2158 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn);
2161 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
2162 vlib_node_runtime_t * node,
2163 vlib_frame_t * frame)
2165 u32 n_left_from, * from, * to_next;
2166 snat_in2out_next_t next_index;
2167 u32 pkts_processed = 0;
2168 snat_main_t * sm = &snat_main;
2169 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
2170 u32 stats_node_index;
2172 stats_node_index = snat_in2out_fast_node.index;
2174 from = vlib_frame_vector_args (frame);
2175 n_left_from = frame->n_vectors;
2176 next_index = node->cached_next_index;
2178 while (n_left_from > 0)
2182 vlib_get_next_frame (vm, node, next_index,
2183 to_next, n_left_to_next);
2185 while (n_left_from > 0 && n_left_to_next > 0)
2193 u32 new_addr0, old_addr0;
2194 u16 old_port0, new_port0;
2195 udp_header_t * udp0;
2196 tcp_header_t * tcp0;
2197 icmp46_header_t * icmp0;
2198 snat_session_key_t key0, sm0;
2202 /* speculatively enqueue b0 to the current next frame */
2208 n_left_to_next -= 1;
2210 b0 = vlib_get_buffer (vm, bi0);
2211 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
2213 ip0 = vlib_buffer_get_current (b0);
2214 udp0 = ip4_next_header (ip0);
2215 tcp0 = (tcp_header_t *) udp0;
2216 icmp0 = (icmp46_header_t *) udp0;
2218 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
2219 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
2221 proto0 = ip_proto_to_snat_proto (ip0->protocol);
2223 if (PREDICT_FALSE (proto0 == ~0))
2226 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
2228 if (PREDICT_FALSE(snat_not_translate(sm, rt, sw_if_index0, ip0,
2229 proto0, rx_fib_index0)))
2232 next0 = icmp_in2out(sm, b0, ip0, icmp0, sw_if_index0,
2233 rx_fib_index0, node, next0, ~0, 0);
2237 key0.addr = ip0->src_address;
2238 key0.port = udp0->src_port;
2239 key0.fib_index = rx_fib_index0;
2241 if (snat_static_mapping_match(sm, key0, &sm0, 0))
2243 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
2244 next0= SNAT_IN2OUT_NEXT_DROP;
2248 new_addr0 = sm0.addr.as_u32;
2249 new_port0 = sm0.port;
2250 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
2251 old_addr0 = ip0->src_address.as_u32;
2252 ip0->src_address.as_u32 = new_addr0;
2254 sum0 = ip0->checksum;
2255 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2257 src_address /* changed member */);
2258 ip0->checksum = ip_csum_fold (sum0);
2260 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
2262 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2264 old_port0 = tcp0->src_port;
2265 tcp0->src_port = new_port0;
2267 sum0 = tcp0->checksum;
2268 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2270 dst_address /* changed member */);
2271 sum0 = ip_csum_update (sum0, old_port0, new_port0,
2272 ip4_header_t /* cheat */,
2273 length /* changed member */);
2274 tcp0->checksum = ip_csum_fold(sum0);
2278 old_port0 = udp0->src_port;
2279 udp0->src_port = new_port0;
2285 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
2287 sum0 = tcp0->checksum;
2288 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
2290 dst_address /* changed member */);
2291 tcp0->checksum = ip_csum_fold(sum0);
2296 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
2299 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
2300 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
2302 snat_in2out_trace_t *t =
2303 vlib_add_trace (vm, node, b0, sizeof (*t));
2304 t->sw_if_index = sw_if_index0;
2305 t->next_index = next0;
2308 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
2310 /* verify speculative enqueue, maybe switch current next frame */
2311 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
2312 to_next, n_left_to_next,
2316 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
2319 vlib_node_increment_counter (vm, stats_node_index,
2320 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
2322 return frame->n_vectors;
2326 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
2327 .function = snat_in2out_fast_static_map_fn,
2328 .name = "snat-in2out-fast",
2329 .vector_size = sizeof (u32),
2330 .format_trace = format_snat_in2out_fast_trace,
2331 .type = VLIB_NODE_TYPE_INTERNAL,
2333 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
2334 .error_strings = snat_in2out_error_strings,
2336 .runtime_data_bytes = sizeof (snat_runtime_t),
2338 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
2340 /* edit / add dispositions here */
2342 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
2343 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
2344 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
2345 [SNAT_IN2OUT_NEXT_ICMP_ERROR] = "ip4-icmp-error",
2349 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);