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>
27 #include <vppinfra/hash.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/elog.h>
35 } snat_out2in_trace_t;
38 u32 next_worker_index;
40 } snat_out2in_worker_handoff_trace_t;
42 /* packet trace format function */
43 static u8 * format_snat_out2in_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 snat_out2in_trace_t * t = va_arg (*args, snat_out2in_trace_t *);
49 s = format (s, "SNAT_OUT2IN: sw_if_index %d, next index %d, session index %d",
50 t->sw_if_index, t->next_index, t->session_index);
54 static u8 * format_snat_out2in_fast_trace (u8 * s, va_list * args)
56 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
57 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
58 snat_out2in_trace_t * t = va_arg (*args, snat_out2in_trace_t *);
60 s = format (s, "SNAT_OUT2IN_FAST: sw_if_index %d, next index %d",
61 t->sw_if_index, t->next_index);
65 static u8 * format_snat_out2in_worker_handoff_trace (u8 * s, va_list * args)
67 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69 snat_out2in_worker_handoff_trace_t * t =
70 va_arg (*args, snat_out2in_worker_handoff_trace_t *);
73 m = t->do_handoff ? "next worker" : "same worker";
74 s = format (s, "SNAT_OUT2IN_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
79 vlib_node_registration_t snat_out2in_node;
80 vlib_node_registration_t snat_out2in_fast_node;
81 vlib_node_registration_t snat_out2in_worker_handoff_node;
83 #define foreach_snat_out2in_error \
84 _(UNSUPPORTED_PROTOCOL, "Unsupported protocol") \
85 _(OUT2IN_PACKETS, "Good out2in packets processed") \
86 _(BAD_ICMP_TYPE, "icmp type not echo-reply") \
87 _(NO_TRANSLATION, "No translation")
90 #define _(sym,str) SNAT_OUT2IN_ERROR_##sym,
91 foreach_snat_out2in_error
94 } snat_out2in_error_t;
96 static char * snat_out2in_error_strings[] = {
97 #define _(sym,string) string,
98 foreach_snat_out2in_error
103 SNAT_OUT2IN_NEXT_DROP,
104 SNAT_OUT2IN_NEXT_LOOKUP,
106 } snat_out2in_next_t;
109 * @brief Create session for static mapping.
111 * Create NAT session initiated by host from external network with static
114 * @param sm SNAT main.
115 * @param b0 Vlib buffer.
116 * @param in2out In2out SNAT session key.
117 * @param out2in Out2in SNAT session key.
118 * @param node Vlib node.
120 * @returns SNAT session if successfully created otherwise 0.
122 static inline snat_session_t *
123 create_session_for_static_mapping (snat_main_t *sm,
125 snat_session_key_t in2out,
126 snat_session_key_t out2in,
127 vlib_node_runtime_t * node,
131 snat_user_key_t user_key;
133 clib_bihash_kv_8_8_t kv0, value0;
134 dlist_elt_t * per_user_translation_list_elt;
135 dlist_elt_t * per_user_list_head_elt;
137 user_key.addr = in2out.addr;
138 user_key.fib_index = in2out.fib_index;
139 kv0.key = user_key.as_u64;
141 /* Ever heard of the "user" = inside ip4 address before? */
142 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
144 /* no, make a new one */
145 pool_get (sm->per_thread_data[cpu_index].users, u);
146 memset (u, 0, sizeof (*u));
147 u->addr = in2out.addr;
149 pool_get (sm->per_thread_data[cpu_index].list_pool,
150 per_user_list_head_elt);
152 u->sessions_per_user_list_head_index = per_user_list_head_elt -
153 sm->per_thread_data[cpu_index].list_pool;
155 clib_dlist_init (sm->per_thread_data[cpu_index].list_pool,
156 u->sessions_per_user_list_head_index);
158 kv0.value = u - sm->per_thread_data[cpu_index].users;
161 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
163 /* add non-traslated packets worker lookup */
164 kv0.value = cpu_index;
165 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
169 u = pool_elt_at_index (sm->per_thread_data[cpu_index].users,
173 pool_get (sm->per_thread_data[cpu_index].sessions, s);
174 memset (s, 0, sizeof (*s));
176 s->outside_address_index = ~0;
177 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
178 u->nstaticsessions++;
180 /* Create list elts */
181 pool_get (sm->per_thread_data[cpu_index].list_pool,
182 per_user_translation_list_elt);
183 clib_dlist_init (sm->per_thread_data[cpu_index].list_pool,
184 per_user_translation_list_elt -
185 sm->per_thread_data[cpu_index].list_pool);
187 per_user_translation_list_elt->value =
188 s - sm->per_thread_data[cpu_index].sessions;
190 per_user_translation_list_elt - sm->per_thread_data[cpu_index].list_pool;
191 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
193 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
194 s->per_user_list_head_index,
195 per_user_translation_list_elt -
196 sm->per_thread_data[cpu_index].list_pool);
200 s->in2out.protocol = out2in.protocol;
202 /* Add to translation hashes */
203 kv0.key = s->in2out.as_u64;
204 kv0.value = s - sm->per_thread_data[cpu_index].sessions;
205 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
206 clib_warning ("in2out key add failed");
208 kv0.key = s->out2in.as_u64;
209 kv0.value = s - sm->per_thread_data[cpu_index].sessions;
211 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
212 clib_warning ("out2in key add failed");
215 snat_ipfix_logging_nat44_ses_create(s->in2out.addr.as_u32,
216 s->out2in.addr.as_u32,
220 s->in2out.fib_index);
224 static inline u32 icmp_out2in_slow_path (snat_main_t *sm,
227 icmp46_header_t * icmp0,
230 vlib_node_runtime_t * node,
234 snat_session_key_t key0, sm0;
235 icmp_echo_header_t *echo0;
236 clib_bihash_kv_8_8_t kv0, value0;
238 u32 new_addr0, old_addr0;
239 u16 old_id0, new_id0;
241 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
243 echo0 = (icmp_echo_header_t *)(icmp0+1);
245 key0.addr = ip0->dst_address;
246 key0.port = echo0->identifier;
247 key0.protocol = SNAT_PROTOCOL_ICMP;
248 key0.fib_index = rx_fib_index0;
250 kv0.key = key0.as_u64;
252 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
254 /* Try to match static mapping by external address and port,
255 destination address and port in packet */
256 if (snat_static_mapping_match(sm, key0, &sm0, 1))
258 ip4_address_t * first_int_addr;
260 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
263 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
264 0 /* just want the address */);
265 rt->cached_sw_if_index = sw_if_index0;
267 rt->cached_ip4_address = first_int_addr->as_u32;
269 rt->cached_ip4_address = 0;
272 /* Don't NAT packet aimed at the intfc address */
273 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
274 rt->cached_ip4_address))
277 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
278 return SNAT_OUT2IN_NEXT_DROP;
281 /* Create session initiated by host from external network */
282 s0 = create_session_for_static_mapping(sm, b0, sm0, key0,
285 return SNAT_OUT2IN_NEXT_DROP;
288 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
291 old_addr0 = ip0->dst_address.as_u32;
292 ip0->dst_address = s0->in2out.addr;
293 new_addr0 = ip0->dst_address.as_u32;
294 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
296 sum0 = ip0->checksum;
297 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
299 dst_address /* changed member */);
300 ip0->checksum = ip_csum_fold (sum0);
302 old_id0 = echo0->identifier;
303 new_id0 = s0->in2out.port;
304 echo0->identifier = new_id0;
306 sum0 = icmp0->checksum;
307 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
309 icmp0->checksum = ip_csum_fold (sum0);
312 s0->last_heard = now;
314 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
315 /* Per-user LRU list maintenance for dynamic translation */
316 if (!snat_is_session_static (s0))
318 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
320 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
321 s0->per_user_list_head_index,
329 snat_out2in_node_fn (vlib_main_t * vm,
330 vlib_node_runtime_t * node,
331 vlib_frame_t * frame)
333 u32 n_left_from, * from, * to_next;
334 snat_out2in_next_t next_index;
335 u32 pkts_processed = 0;
336 snat_main_t * sm = &snat_main;
337 f64 now = vlib_time_now (vm);
338 u32 cpu_index = os_get_cpu_number ();
340 from = vlib_frame_vector_args (frame);
341 n_left_from = frame->n_vectors;
342 next_index = node->cached_next_index;
344 while (n_left_from > 0)
348 vlib_get_next_frame (vm, node, next_index,
349 to_next, n_left_to_next);
351 while (n_left_from >= 4 && n_left_to_next >= 2)
354 vlib_buffer_t * b0, * b1;
355 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
356 u32 next1 = SNAT_OUT2IN_NEXT_LOOKUP;
357 u32 sw_if_index0, sw_if_index1;
358 ip4_header_t * ip0, *ip1;
359 ip_csum_t sum0, sum1;
360 u32 new_addr0, old_addr0;
361 u16 new_port0, old_port0;
362 u32 new_addr1, old_addr1;
363 u16 new_port1, old_port1;
364 udp_header_t * udp0, * udp1;
365 tcp_header_t * tcp0, * tcp1;
366 icmp46_header_t * icmp0, * icmp1;
367 snat_session_key_t key0, key1, sm0, sm1;
368 u32 rx_fib_index0, rx_fib_index1;
370 snat_session_t * s0 = 0, * s1 = 0;
371 clib_bihash_kv_8_8_t kv0, kv1, value0, value1;
373 /* Prefetch next iteration. */
375 vlib_buffer_t * p2, * p3;
377 p2 = vlib_get_buffer (vm, from[2]);
378 p3 = vlib_get_buffer (vm, from[3]);
380 vlib_prefetch_buffer_header (p2, LOAD);
381 vlib_prefetch_buffer_header (p3, LOAD);
383 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
384 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
387 /* speculatively enqueue b0 and b1 to the current next frame */
388 to_next[0] = bi0 = from[0];
389 to_next[1] = bi1 = from[1];
395 b0 = vlib_get_buffer (vm, bi0);
396 b1 = vlib_get_buffer (vm, bi1);
398 ip0 = vlib_buffer_get_current (b0);
399 udp0 = ip4_next_header (ip0);
400 tcp0 = (tcp_header_t *) udp0;
401 icmp0 = (icmp46_header_t *) udp0;
403 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
404 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
408 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
409 ? SNAT_PROTOCOL_UDP : proto0;
410 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
411 ? SNAT_PROTOCOL_TCP : proto0;
412 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
413 ? SNAT_PROTOCOL_ICMP : proto0;
415 if (PREDICT_FALSE (proto0 == ~0))
418 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
420 next0 = icmp_out2in_slow_path
421 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
422 next0, now, cpu_index);
426 key0.addr = ip0->dst_address;
427 key0.port = udp0->dst_port;
428 key0.protocol = proto0;
429 key0.fib_index = rx_fib_index0;
431 kv0.key = key0.as_u64;
433 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
435 /* Try to match static mapping by external address and port,
436 destination address and port in packet */
437 if (snat_static_mapping_match(sm, key0, &sm0, 1))
439 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
443 /* Create session initiated by host from external network */
444 s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
450 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
453 old_addr0 = ip0->dst_address.as_u32;
454 ip0->dst_address = s0->in2out.addr;
455 new_addr0 = ip0->dst_address.as_u32;
456 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
458 sum0 = ip0->checksum;
459 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
461 dst_address /* changed member */);
462 ip0->checksum = ip_csum_fold (sum0);
464 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
466 old_port0 = tcp0->ports.dst;
467 tcp0->ports.dst = s0->in2out.port;
468 new_port0 = tcp0->ports.dst;
470 sum0 = tcp0->checksum;
471 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
473 dst_address /* changed member */);
475 sum0 = ip_csum_update (sum0, old_port0, new_port0,
476 ip4_header_t /* cheat */,
477 length /* changed member */);
478 tcp0->checksum = ip_csum_fold(sum0);
482 old_port0 = udp0->dst_port;
483 udp0->dst_port = s0->in2out.port;
488 s0->last_heard = now;
490 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
491 /* Per-user LRU list maintenance for dynamic translation */
492 if (!snat_is_session_static (s0))
494 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
496 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
497 s0->per_user_list_head_index,
502 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
503 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
505 snat_out2in_trace_t *t =
506 vlib_add_trace (vm, node, b0, sizeof (*t));
507 t->sw_if_index = sw_if_index0;
508 t->next_index = next0;
509 t->session_index = ~0;
511 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
514 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
517 ip1 = vlib_buffer_get_current (b1);
518 udp1 = ip4_next_header (ip1);
519 tcp1 = (tcp_header_t *) udp1;
520 icmp1 = (icmp46_header_t *) udp1;
522 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
523 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
527 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
528 ? SNAT_PROTOCOL_UDP : proto1;
529 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
530 ? SNAT_PROTOCOL_TCP : proto1;
531 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
532 ? SNAT_PROTOCOL_ICMP : proto1;
534 if (PREDICT_FALSE (proto1 == ~0))
537 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
539 next1 = icmp_out2in_slow_path
540 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
541 next1, now, cpu_index);
545 key1.addr = ip1->dst_address;
546 key1.port = udp1->dst_port;
547 key1.protocol = proto1;
548 key1.fib_index = rx_fib_index1;
550 kv1.key = key1.as_u64;
552 if (clib_bihash_search_8_8 (&sm->out2in, &kv1, &value1))
554 /* Try to match static mapping by external address and port,
555 destination address and port in packet */
556 if (snat_static_mapping_match(sm, key1, &sm1, 1))
558 b1->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
562 /* Create session initiated by host from external network */
563 s1 = create_session_for_static_mapping(sm, b1, sm1, key1, node,
569 s1 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
572 old_addr1 = ip1->dst_address.as_u32;
573 ip1->dst_address = s1->in2out.addr;
574 new_addr1 = ip1->dst_address.as_u32;
575 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->in2out.fib_index;
577 sum1 = ip1->checksum;
578 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
580 dst_address /* changed member */);
581 ip1->checksum = ip_csum_fold (sum1);
583 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
585 old_port1 = tcp1->ports.dst;
586 tcp1->ports.dst = s1->in2out.port;
587 new_port1 = tcp1->ports.dst;
589 sum1 = tcp1->checksum;
590 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
592 dst_address /* changed member */);
594 sum1 = ip_csum_update (sum1, old_port1, new_port1,
595 ip4_header_t /* cheat */,
596 length /* changed member */);
597 tcp1->checksum = ip_csum_fold(sum1);
601 old_port1 = udp1->dst_port;
602 udp1->dst_port = s1->in2out.port;
607 s1->last_heard = now;
609 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
610 /* Per-user LRU list maintenance for dynamic translation */
611 if (!snat_is_session_static (s1))
613 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
615 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
616 s1->per_user_list_head_index,
621 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
622 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
624 snat_out2in_trace_t *t =
625 vlib_add_trace (vm, node, b1, sizeof (*t));
626 t->sw_if_index = sw_if_index1;
627 t->next_index = next1;
628 t->session_index = ~0;
630 t->session_index = s1 - sm->per_thread_data[cpu_index].sessions;
633 pkts_processed += next1 != SNAT_OUT2IN_NEXT_DROP;
635 /* verify speculative enqueues, maybe switch current next frame */
636 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
637 to_next, n_left_to_next,
638 bi0, bi1, next0, next1);
641 while (n_left_from > 0 && n_left_to_next > 0)
645 u32 next0 = SNAT_OUT2IN_NEXT_LOOKUP;
649 u32 new_addr0, old_addr0;
650 u16 new_port0, old_port0;
653 icmp46_header_t * icmp0;
654 snat_session_key_t key0, sm0;
657 snat_session_t * s0 = 0;
658 clib_bihash_kv_8_8_t kv0, value0;
660 /* speculatively enqueue b0 to the current next frame */
668 b0 = vlib_get_buffer (vm, bi0);
670 ip0 = vlib_buffer_get_current (b0);
671 udp0 = ip4_next_header (ip0);
672 tcp0 = (tcp_header_t *) udp0;
673 icmp0 = (icmp46_header_t *) udp0;
675 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
676 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
680 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
681 ? SNAT_PROTOCOL_UDP : proto0;
682 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
683 ? SNAT_PROTOCOL_TCP : proto0;
684 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
685 ? SNAT_PROTOCOL_ICMP : proto0;
687 if (PREDICT_FALSE (proto0 == ~0))
690 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
692 next0 = icmp_out2in_slow_path
693 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
694 next0, now, cpu_index);
698 key0.addr = ip0->dst_address;
699 key0.port = udp0->dst_port;
700 key0.protocol = proto0;
701 key0.fib_index = rx_fib_index0;
703 kv0.key = key0.as_u64;
705 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
707 /* Try to match static mapping by external address and port,
708 destination address and port in packet */
709 if (snat_static_mapping_match(sm, key0, &sm0, 1))
711 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
715 /* Create session initiated by host from external network */
716 s0 = create_session_for_static_mapping(sm, b0, sm0, key0, node,
722 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
725 old_addr0 = ip0->dst_address.as_u32;
726 ip0->dst_address = s0->in2out.addr;
727 new_addr0 = ip0->dst_address.as_u32;
728 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
730 sum0 = ip0->checksum;
731 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
733 dst_address /* changed member */);
734 ip0->checksum = ip_csum_fold (sum0);
736 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
738 old_port0 = tcp0->ports.dst;
739 tcp0->ports.dst = s0->in2out.port;
740 new_port0 = tcp0->ports.dst;
742 sum0 = tcp0->checksum;
743 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
745 dst_address /* changed member */);
747 sum0 = ip_csum_update (sum0, old_port0, new_port0,
748 ip4_header_t /* cheat */,
749 length /* changed member */);
750 tcp0->checksum = ip_csum_fold(sum0);
754 old_port0 = udp0->dst_port;
755 udp0->dst_port = s0->in2out.port;
760 s0->last_heard = now;
762 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
763 /* Per-user LRU list maintenance for dynamic translation */
764 if (!snat_is_session_static (s0))
766 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
768 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
769 s0->per_user_list_head_index,
774 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
775 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
777 snat_out2in_trace_t *t =
778 vlib_add_trace (vm, node, b0, sizeof (*t));
779 t->sw_if_index = sw_if_index0;
780 t->next_index = next0;
781 t->session_index = ~0;
783 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
786 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
788 /* verify speculative enqueue, maybe switch current next frame */
789 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
790 to_next, n_left_to_next,
794 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
797 vlib_node_increment_counter (vm, snat_out2in_node.index,
798 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
800 return frame->n_vectors;
803 VLIB_REGISTER_NODE (snat_out2in_node) = {
804 .function = snat_out2in_node_fn,
805 .name = "snat-out2in",
806 .vector_size = sizeof (u32),
807 .format_trace = format_snat_out2in_trace,
808 .type = VLIB_NODE_TYPE_INTERNAL,
810 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
811 .error_strings = snat_out2in_error_strings,
813 .runtime_data_bytes = sizeof (snat_runtime_t),
815 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
817 /* edit / add dispositions here */
819 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
820 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
823 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_node, snat_out2in_node_fn);
826 snat_out2in_worker_handoff_fn (vlib_main_t * vm,
827 vlib_node_runtime_t * node,
828 vlib_frame_t * frame)
830 snat_main_t *sm = &snat_main;
831 vlib_thread_main_t *tm = vlib_get_thread_main ();
832 u32 n_left_from, *from, *to_next = 0;
833 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
834 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
836 vlib_frame_queue_elt_t *hf = 0;
839 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
840 u32 next_worker_index = 0;
841 u32 current_worker_index = ~0;
842 u32 cpu_index = os_get_cpu_number ();
844 ASSERT (vec_len (sm->workers));
846 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
848 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
850 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
851 sm->first_worker_index + sm->num_workers - 1,
852 (vlib_frame_queue_t *) (~0));
855 from = vlib_frame_vector_args (frame);
856 n_left_from = frame->n_vectors;
858 while (n_left_from > 0)
866 snat_static_mapping_key_t key0;
867 clib_bihash_kv_8_8_t kv0, value0;
874 b0 = vlib_get_buffer (vm, bi0);
876 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
877 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
879 ip0 = vlib_buffer_get_current (b0);
880 udp0 = ip4_next_header (ip0);
882 key0.addr = ip0->dst_address;
883 key0.port = udp0->dst_port;
884 key0.fib_index = rx_fib_index0;
886 if (PREDICT_FALSE(ip0->protocol == IP_PROTOCOL_ICMP))
888 icmp46_header_t * icmp0 = (icmp46_header_t *) udp0;
889 icmp_echo_header_t *echo0 = (icmp_echo_header_t *)(icmp0+1);
890 key0.port = echo0->identifier;
893 kv0.key = key0.as_u64;
895 /* Ever heard of of the "user" before? */
896 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
899 kv0.key = key0.as_u64;
901 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
903 /* No, assign next available worker (RR) */
904 next_worker_index = sm->first_worker_index;
905 if (vec_len (sm->workers))
908 sm->workers[sm->next_worker++ % _vec_len (sm->workers)];
913 /* Static mapping without port */
914 next_worker_index = value0.value;
917 /* Add to translated packets worker lookup */
918 kv0.value = next_worker_index;
919 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
922 next_worker_index = value0.value;
924 if (PREDICT_FALSE (next_worker_index != cpu_index))
928 if (next_worker_index != current_worker_index)
931 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
933 hf = vlib_get_worker_handoff_queue_elt (sm->fq_out2in_index,
935 handoff_queue_elt_by_worker_index);
937 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
938 to_next_worker = &hf->buffer_index[hf->n_vectors];
939 current_worker_index = next_worker_index;
942 /* enqueue to correct worker thread */
943 to_next_worker[0] = bi0;
945 n_left_to_next_worker--;
947 if (n_left_to_next_worker == 0)
949 hf->n_vectors = VLIB_FRAME_SIZE;
950 vlib_put_frame_queue_elt (hf);
951 current_worker_index = ~0;
952 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
959 /* if this is 1st frame */
962 f = vlib_get_frame_to_node (vm, snat_out2in_node.index);
963 to_next = vlib_frame_vector_args (f);
971 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
972 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
974 snat_out2in_worker_handoff_trace_t *t =
975 vlib_add_trace (vm, node, b0, sizeof (*t));
976 t->next_worker_index = next_worker_index;
977 t->do_handoff = do_handoff;
982 vlib_put_frame_to_node (vm, snat_out2in_node.index, f);
985 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
987 /* Ship frames to the worker nodes */
988 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
990 if (handoff_queue_elt_by_worker_index[i])
992 hf = handoff_queue_elt_by_worker_index[i];
994 * It works better to let the handoff node
995 * rate-adapt, always ship the handoff queue element.
997 if (1 || hf->n_vectors == hf->last_n_vectors)
999 vlib_put_frame_queue_elt (hf);
1000 handoff_queue_elt_by_worker_index[i] = 0;
1003 hf->last_n_vectors = hf->n_vectors;
1005 congested_handoff_queue_by_worker_index[i] =
1006 (vlib_frame_queue_t *) (~0);
1009 current_worker_index = ~0;
1010 return frame->n_vectors;
1013 VLIB_REGISTER_NODE (snat_out2in_worker_handoff_node) = {
1014 .function = snat_out2in_worker_handoff_fn,
1015 .name = "snat-out2in-worker-handoff",
1016 .vector_size = sizeof (u32),
1017 .format_trace = format_snat_out2in_worker_handoff_trace,
1018 .type = VLIB_NODE_TYPE_INTERNAL,
1027 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_worker_handoff_node, snat_out2in_worker_handoff_fn);
1029 static inline u32 icmp_out2in_fast (snat_main_t *sm,
1032 icmp46_header_t * icmp0,
1034 vlib_node_runtime_t * node,
1038 snat_session_key_t key0, sm0;
1039 icmp_echo_header_t *echo0;
1040 u32 new_addr0, old_addr0;
1041 u16 old_id0, new_id0;
1043 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1045 echo0 = (icmp_echo_header_t *)(icmp0+1);
1047 key0.addr = ip0->dst_address;
1048 key0.port = echo0->identifier;
1049 key0.fib_index = rx_fib_index0;
1051 if (snat_static_mapping_match(sm, key0, &sm0, 1))
1053 ip4_address_t * first_int_addr;
1055 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1058 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1059 0 /* just want the address */);
1060 rt->cached_sw_if_index = sw_if_index0;
1062 rt->cached_ip4_address = first_int_addr->as_u32;
1064 rt->cached_ip4_address = 0;
1067 /* Don't NAT packet aimed at the intfc address */
1068 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1069 rt->cached_ip4_address))
1072 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1073 return SNAT_OUT2IN_NEXT_DROP;
1076 new_addr0 = sm0.addr.as_u32;
1078 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1080 old_addr0 = ip0->dst_address.as_u32;
1081 ip0->dst_address.as_u32 = new_addr0;
1083 sum0 = ip0->checksum;
1084 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1086 dst_address /* changed member */);
1087 ip0->checksum = ip_csum_fold (sum0);
1089 if (PREDICT_FALSE(new_id0 != echo0->identifier))
1091 old_id0 = echo0->identifier;
1092 echo0->identifier = new_id0;
1094 sum0 = icmp0->checksum;
1095 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
1097 icmp0->checksum = ip_csum_fold (sum0);
1104 snat_out2in_fast_node_fn (vlib_main_t * vm,
1105 vlib_node_runtime_t * node,
1106 vlib_frame_t * frame)
1108 u32 n_left_from, * from, * to_next;
1109 snat_out2in_next_t next_index;
1110 u32 pkts_processed = 0;
1111 snat_main_t * sm = &snat_main;
1113 from = vlib_frame_vector_args (frame);
1114 n_left_from = frame->n_vectors;
1115 next_index = node->cached_next_index;
1117 while (n_left_from > 0)
1121 vlib_get_next_frame (vm, node, next_index,
1122 to_next, n_left_to_next);
1124 while (n_left_from > 0 && n_left_to_next > 0)
1128 u32 next0 = SNAT_OUT2IN_NEXT_DROP;
1132 u32 new_addr0, old_addr0;
1133 u16 new_port0, old_port0;
1134 udp_header_t * udp0;
1135 tcp_header_t * tcp0;
1136 icmp46_header_t * icmp0;
1137 snat_session_key_t key0, sm0;
1141 /* speculatively enqueue b0 to the current next frame */
1147 n_left_to_next -= 1;
1149 b0 = vlib_get_buffer (vm, bi0);
1151 ip0 = vlib_buffer_get_current (b0);
1152 udp0 = ip4_next_header (ip0);
1153 tcp0 = (tcp_header_t *) udp0;
1154 icmp0 = (icmp46_header_t *) udp0;
1156 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1157 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1159 vnet_feature_next (sw_if_index0, &next0, b0);
1162 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
1163 ? SNAT_PROTOCOL_UDP : proto0;
1164 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
1165 ? SNAT_PROTOCOL_TCP : proto0;
1166 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
1167 ? SNAT_PROTOCOL_ICMP : proto0;
1169 if (PREDICT_FALSE (proto0 == ~0))
1172 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1174 next0 = icmp_out2in_fast
1175 (sm, b0, ip0, icmp0, sw_if_index0, node, next0, rx_fib_index0);
1179 key0.addr = ip0->dst_address;
1180 key0.port = udp0->dst_port;
1181 key0.fib_index = rx_fib_index0;
1183 if (snat_static_mapping_match(sm, key0, &sm0, 1))
1185 b0->error = node->errors[SNAT_OUT2IN_ERROR_NO_TRANSLATION];
1189 new_addr0 = sm0.addr.as_u32;
1190 new_port0 = sm0.port;
1191 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1192 old_addr0 = ip0->dst_address.as_u32;
1193 ip0->dst_address.as_u32 = new_addr0;
1195 sum0 = ip0->checksum;
1196 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1198 dst_address /* changed member */);
1199 ip0->checksum = ip_csum_fold (sum0);
1201 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
1203 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1205 old_port0 = tcp0->ports.dst;
1206 tcp0->ports.dst = new_port0;
1208 sum0 = tcp0->checksum;
1209 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1211 dst_address /* changed member */);
1213 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1214 ip4_header_t /* cheat */,
1215 length /* changed member */);
1216 tcp0->checksum = ip_csum_fold(sum0);
1220 old_port0 = udp0->dst_port;
1221 udp0->dst_port = new_port0;
1227 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1229 sum0 = tcp0->checksum;
1230 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1232 dst_address /* changed member */);
1234 tcp0->checksum = ip_csum_fold(sum0);
1240 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1241 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1243 snat_out2in_trace_t *t =
1244 vlib_add_trace (vm, node, b0, sizeof (*t));
1245 t->sw_if_index = sw_if_index0;
1246 t->next_index = next0;
1249 pkts_processed += next0 != SNAT_OUT2IN_NEXT_DROP;
1251 /* verify speculative enqueue, maybe switch current next frame */
1252 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1253 to_next, n_left_to_next,
1257 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1260 vlib_node_increment_counter (vm, snat_out2in_fast_node.index,
1261 SNAT_OUT2IN_ERROR_OUT2IN_PACKETS,
1263 return frame->n_vectors;
1266 VLIB_REGISTER_NODE (snat_out2in_fast_node) = {
1267 .function = snat_out2in_fast_node_fn,
1268 .name = "snat-out2in-fast",
1269 .vector_size = sizeof (u32),
1270 .format_trace = format_snat_out2in_fast_trace,
1271 .type = VLIB_NODE_TYPE_INTERNAL,
1273 .n_errors = ARRAY_LEN(snat_out2in_error_strings),
1274 .error_strings = snat_out2in_error_strings,
1276 .runtime_data_bytes = sizeof (snat_runtime_t),
1278 .n_next_nodes = SNAT_OUT2IN_N_NEXT,
1280 /* edit / add dispositions here */
1282 [SNAT_OUT2IN_NEXT_LOOKUP] = "ip4-lookup",
1283 [SNAT_OUT2IN_NEXT_DROP] = "error-drop",
1286 VLIB_NODE_FUNCTION_MULTIARCH (snat_out2in_fast_node, snat_out2in_fast_node_fn);