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>
26 #include <vppinfra/hash.h>
27 #include <vppinfra/error.h>
28 #include <vppinfra/elog.h>
35 } snat_in2out_trace_t;
38 u32 next_worker_index;
40 } snat_in2out_worker_handoff_trace_t;
42 /* packet trace format function */
43 static u8 * format_snat_in2out_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_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
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,
53 t->sw_if_index, t->next_index, t->session_index);
58 static u8 * format_snat_in2out_fast_trace (u8 * s, va_list * args)
60 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
61 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
62 snat_in2out_trace_t * t = va_arg (*args, snat_in2out_trace_t *);
64 s = format (s, "SANT_IN2OUT_FAST: sw_if_index %d, next index %d",
65 t->sw_if_index, t->next_index);
70 static u8 * format_snat_in2out_worker_handoff_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 snat_in2out_worker_handoff_trace_t * t =
75 va_arg (*args, snat_in2out_worker_handoff_trace_t *);
78 m = t->do_handoff ? "next worker" : "same worker";
79 s = format (s, "SNAT_IN2OUT_WORKER_HANDOFF: %s %d", m, t->next_worker_index);
84 vlib_node_registration_t snat_in2out_node;
85 vlib_node_registration_t snat_in2out_slowpath_node;
86 vlib_node_registration_t snat_in2out_fast_node;
87 vlib_node_registration_t snat_in2out_worker_handoff_node;
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,
99 foreach_snat_in2out_error
102 } snat_in2out_error_t;
104 static char * snat_in2out_error_strings[] = {
105 #define _(sym,string) string,
106 foreach_snat_in2out_error
111 SNAT_IN2OUT_NEXT_LOOKUP,
112 SNAT_IN2OUT_NEXT_DROP,
113 SNAT_IN2OUT_NEXT_SLOW_PATH,
115 } snat_in2out_next_t;
117 static u32 slow_path (snat_main_t *sm, vlib_buffer_t *b0,
120 snat_session_key_t * key0,
121 snat_session_t ** sessionp,
122 vlib_node_runtime_t * node,
127 snat_user_key_t user_key;
129 clib_bihash_kv_8_8_t kv0, value0;
130 u32 oldest_per_user_translation_list_index;
131 dlist_elt_t * oldest_per_user_translation_list_elt;
132 dlist_elt_t * per_user_translation_list_elt;
133 dlist_elt_t * per_user_list_head_elt;
135 snat_session_key_t key1;
136 u32 address_index = ~0;
137 u32 outside_fib_index;
139 snat_static_mapping_key_t worker_by_out_key;
141 p = hash_get (sm->ip4_main->fib_index_by_table_id, sm->outside_vrf_id);
144 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_OUTSIDE_FIB];
145 return SNAT_IN2OUT_NEXT_DROP;
147 outside_fib_index = p[0];
149 user_key.addr = ip0->src_address;
150 user_key.fib_index = rx_fib_index0;
151 kv0.key = user_key.as_u64;
153 /* Ever heard of the "user" = src ip4 address before? */
154 if (clib_bihash_search_8_8 (&sm->user_hash, &kv0, &value0))
156 /* no, make a new one */
157 pool_get (sm->per_thread_data[cpu_index].users, u);
158 memset (u, 0, sizeof (*u));
159 u->addr = ip0->src_address;
161 pool_get (sm->per_thread_data[cpu_index].list_pool, per_user_list_head_elt);
163 u->sessions_per_user_list_head_index = per_user_list_head_elt -
164 sm->per_thread_data[cpu_index].list_pool;
166 clib_dlist_init (sm->per_thread_data[cpu_index].list_pool,
167 u->sessions_per_user_list_head_index);
169 kv0.value = u - sm->per_thread_data[cpu_index].users;
172 clib_bihash_add_del_8_8 (&sm->user_hash, &kv0, 1 /* is_add */);
176 u = pool_elt_at_index (sm->per_thread_data[cpu_index].users,
180 /* Over quota? Recycle the least recently used dynamic translation */
181 if (u->nsessions >= sm->max_translations_per_user)
183 /* Remove the oldest dynamic translation */
185 oldest_per_user_translation_list_index =
186 clib_dlist_remove_head (sm->per_thread_data[cpu_index].list_pool,
187 u->sessions_per_user_list_head_index);
189 ASSERT (oldest_per_user_translation_list_index != ~0);
191 /* add it back to the end of the LRU list */
192 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
193 u->sessions_per_user_list_head_index,
194 oldest_per_user_translation_list_index);
195 /* Get the list element */
196 oldest_per_user_translation_list_elt =
197 pool_elt_at_index (sm->per_thread_data[cpu_index].list_pool,
198 oldest_per_user_translation_list_index);
200 /* Get the session index from the list element */
201 session_index = oldest_per_user_translation_list_elt->value;
203 /* Get the session */
204 s = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
206 } while (snat_is_session_static (s));
208 /* Remove in2out, out2in keys */
209 kv0.key = s->in2out.as_u64;
210 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 0 /* is_add */))
211 clib_warning ("in2out key delete failed");
212 kv0.key = s->out2in.as_u64;
213 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 0 /* is_add */))
214 clib_warning ("out2in key delete failed");
216 snat_free_outside_address_and_port
217 (sm, &s->out2in, s->outside_address_index);
218 s->outside_address_index = ~0;
220 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
224 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
225 return SNAT_IN2OUT_NEXT_DROP;
227 s->outside_address_index = address_index;
231 u8 static_mapping = 1;
233 /* First try to match static mapping by local address and port */
234 if (snat_static_mapping_match (sm, *key0, &key1, 0))
237 /* Try to create dynamic translation */
238 if (snat_alloc_outside_address_and_port (sm, &key1, &address_index))
240 b0->error = node->errors[SNAT_IN2OUT_ERROR_OUT_OF_PORTS];
241 return SNAT_IN2OUT_NEXT_DROP;
245 /* Create a new session */
246 pool_get (sm->per_thread_data[cpu_index].sessions, s);
247 memset (s, 0, sizeof (*s));
249 s->outside_address_index = address_index;
253 u->nstaticsessions++;
254 s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
261 /* Create list elts */
262 pool_get (sm->per_thread_data[cpu_index].list_pool,
263 per_user_translation_list_elt);
264 clib_dlist_init (sm->per_thread_data[cpu_index].list_pool,
265 per_user_translation_list_elt -
266 sm->per_thread_data[cpu_index].list_pool);
268 per_user_translation_list_elt->value =
269 s - sm->per_thread_data[cpu_index].sessions;
270 s->per_user_index = per_user_translation_list_elt -
271 sm->per_thread_data[cpu_index].list_pool;
272 s->per_user_list_head_index = u->sessions_per_user_list_head_index;
274 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
275 s->per_user_list_head_index,
276 per_user_translation_list_elt -
277 sm->per_thread_data[cpu_index].list_pool);
282 s->out2in.protocol = key0->protocol;
283 s->out2in.fib_index = outside_fib_index;
286 /* Add to translation hashes */
287 kv0.key = s->in2out.as_u64;
288 kv0.value = s - sm->per_thread_data[cpu_index].sessions;
289 if (clib_bihash_add_del_8_8 (&sm->in2out, &kv0, 1 /* is_add */))
290 clib_warning ("in2out key add failed");
292 kv0.key = s->out2in.as_u64;
293 kv0.value = s - sm->per_thread_data[cpu_index].sessions;
295 if (clib_bihash_add_del_8_8 (&sm->out2in, &kv0, 1 /* is_add */))
296 clib_warning ("out2in key add failed");
298 /* Add to translated packets worker lookup */
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;
302 kv0.key = worker_by_out_key.as_u64;
303 kv0.value = cpu_index;
304 clib_bihash_add_del_8_8 (&sm->worker_by_out, &kv0, 1);
308 static inline u32 icmp_in2out_slow_path (snat_main_t *sm,
311 icmp46_header_t * icmp0,
314 vlib_node_runtime_t * node,
319 snat_session_key_t key0;
320 icmp_echo_header_t *echo0;
321 clib_bihash_kv_8_8_t kv0, value0;
323 u32 new_addr0, old_addr0;
324 u16 old_id0, new_id0;
326 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
328 if (PREDICT_FALSE(icmp0->type != ICMP4_echo_request))
330 b0->error = node->errors[SNAT_IN2OUT_ERROR_BAD_ICMP_TYPE];
331 return SNAT_IN2OUT_NEXT_DROP;
334 echo0 = (icmp_echo_header_t *)(icmp0+1);
336 key0.addr = ip0->src_address;
337 key0.port = echo0->identifier;
338 key0.protocol = SNAT_PROTOCOL_ICMP;
339 key0.fib_index = rx_fib_index0;
341 kv0.key = key0.as_u64;
343 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
345 ip4_address_t * first_int_addr;
347 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
350 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
351 0 /* just want the address */);
352 rt->cached_sw_if_index = sw_if_index0;
354 rt->cached_ip4_address = first_int_addr->as_u32;
356 rt->cached_ip4_address = 0;
359 /* Don't NAT packet aimed at the intfc address */
360 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
361 rt->cached_ip4_address))
364 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
365 &s0, node, next0, cpu_index);
367 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
371 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
374 old_addr0 = ip0->src_address.as_u32;
375 ip0->src_address = s0->out2in.addr;
376 new_addr0 = ip0->src_address.as_u32;
377 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
379 sum0 = ip0->checksum;
380 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
382 src_address /* changed member */);
383 ip0->checksum = ip_csum_fold (sum0);
385 old_id0 = echo0->identifier;
386 new_id0 = s0->out2in.port;
387 echo0->identifier = new_id0;
389 sum0 = icmp0->checksum;
390 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
392 icmp0->checksum = ip_csum_fold (sum0);
395 s0->last_heard = now;
397 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
398 /* Per-user LRU list maintenance for dynamic translations */
399 if (!snat_is_session_static (s0))
401 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
403 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
404 s0->per_user_list_head_index,
414 * Hairpinning allows two endpoints on the internal side of the NAT to
415 * communicate even if they only use each other's external IP addresses
418 * @param sm SNAT main.
419 * @param b0 Vlib buffer.
420 * @param ip0 IP header.
421 * @param udp0 UDP header.
422 * @param tcp0 TCP header.
423 * @param proto0 SNAT protocol.
426 snat_hairpinning (snat_main_t *sm,
433 snat_session_key_t key0, sm0;
434 snat_static_mapping_key_t k0;
436 clib_bihash_kv_8_8_t kv0, value0;
438 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
439 u16 new_dst_port0, old_dst_port0;
441 key0.addr = ip0->dst_address;
442 key0.port = udp0->dst_port;
443 key0.protocol = proto0;
444 key0.fib_index = sm->outside_fib_index;
445 kv0.key = key0.as_u64;
447 /* Check if destination is in active sessions */
448 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
450 /* or static mappings */
451 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
453 new_dst_addr0 = sm0.addr.as_u32;
454 new_dst_port0 = sm0.port;
455 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
461 if (sm->num_workers > 1)
463 k0.addr = ip0->dst_address;
464 k0.port = udp0->dst_port;
465 k0.fib_index = sm->outside_fib_index;
467 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
473 ti = sm->num_workers;
475 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
476 new_dst_addr0 = s0->in2out.addr.as_u32;
477 new_dst_port0 = s0->in2out.port;
478 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
481 /* Destination is behind the same NAT, use internal address and port */
484 old_dst_addr0 = ip0->dst_address.as_u32;
485 ip0->dst_address.as_u32 = new_dst_addr0;
486 sum0 = ip0->checksum;
487 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
488 ip4_header_t, dst_address);
489 ip0->checksum = ip_csum_fold (sum0);
491 old_dst_port0 = tcp0->ports.dst;
492 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
494 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
496 tcp0->ports.dst = new_dst_port0;
497 sum0 = tcp0->checksum;
498 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
499 ip4_header_t, dst_address);
500 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
501 ip4_header_t /* cheat */, length);
502 tcp0->checksum = ip_csum_fold(sum0);
506 udp0->dst_port = new_dst_port0;
514 snat_in2out_node_fn_inline (vlib_main_t * vm,
515 vlib_node_runtime_t * node,
516 vlib_frame_t * frame, int is_slow_path)
518 u32 n_left_from, * from, * to_next;
519 snat_in2out_next_t next_index;
520 u32 pkts_processed = 0;
521 snat_main_t * sm = &snat_main;
522 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
523 f64 now = vlib_time_now (vm);
524 u32 stats_node_index;
525 u32 cpu_index = os_get_cpu_number ();
527 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
528 snat_in2out_node.index;
530 from = vlib_frame_vector_args (frame);
531 n_left_from = frame->n_vectors;
532 next_index = node->cached_next_index;
534 while (n_left_from > 0)
538 vlib_get_next_frame (vm, node, next_index,
539 to_next, n_left_to_next);
541 while (n_left_from >= 4 && n_left_to_next >= 2)
544 vlib_buffer_t * b0, * b1;
546 u32 sw_if_index0, sw_if_index1;
547 ip4_header_t * ip0, * ip1;
548 ip_csum_t sum0, sum1;
549 u32 new_addr0, old_addr0, new_addr1, old_addr1;
550 u16 old_port0, new_port0, old_port1, new_port1;
551 udp_header_t * udp0, * udp1;
552 tcp_header_t * tcp0, * tcp1;
553 icmp46_header_t * icmp0, * icmp1;
554 snat_session_key_t key0, key1;
555 u32 rx_fib_index0, rx_fib_index1;
557 snat_session_t * s0 = 0, * s1 = 0;
558 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
560 /* Prefetch next iteration. */
562 vlib_buffer_t * p2, * p3;
564 p2 = vlib_get_buffer (vm, from[2]);
565 p3 = vlib_get_buffer (vm, from[3]);
567 vlib_prefetch_buffer_header (p2, LOAD);
568 vlib_prefetch_buffer_header (p3, LOAD);
570 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
571 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
574 /* speculatively enqueue b0 and b1 to the current next frame */
575 to_next[0] = bi0 = from[0];
576 to_next[1] = bi1 = from[1];
582 b0 = vlib_get_buffer (vm, bi0);
583 b1 = vlib_get_buffer (vm, bi1);
585 ip0 = vlib_buffer_get_current (b0);
586 udp0 = ip4_next_header (ip0);
587 tcp0 = (tcp_header_t *) udp0;
588 icmp0 = (icmp46_header_t *) udp0;
590 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
591 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
594 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
597 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
598 ? SNAT_PROTOCOL_UDP : proto0;
599 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
600 ? SNAT_PROTOCOL_TCP : proto0;
601 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
602 ? SNAT_PROTOCOL_ICMP : proto0;
604 /* Next configured feature, probably ip4-lookup */
607 if (PREDICT_FALSE (proto0 == ~0))
610 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
612 next0 = icmp_in2out_slow_path
613 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
614 node, next0, now, cpu_index);
620 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
622 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
627 key0.addr = ip0->src_address;
628 key0.port = udp0->src_port;
629 key0.protocol = proto0;
630 key0.fib_index = rx_fib_index0;
632 kv0.key = key0.as_u64;
634 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
638 ip4_address_t * first_int_addr;
640 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
643 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
644 0 /* just want the address */);
645 rt->cached_sw_if_index = sw_if_index0;
647 rt->cached_ip4_address = first_int_addr->as_u32;
649 rt->cached_ip4_address = 0;
652 /* Don't NAT packet aimed at the intfc address */
653 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
654 rt->cached_ip4_address))
657 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
658 &s0, node, next0, cpu_index);
659 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
664 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
669 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
672 old_addr0 = ip0->src_address.as_u32;
673 ip0->src_address = s0->out2in.addr;
674 new_addr0 = ip0->src_address.as_u32;
675 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
677 sum0 = ip0->checksum;
678 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
680 src_address /* changed member */);
681 ip0->checksum = ip_csum_fold (sum0);
683 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
685 old_port0 = tcp0->ports.src;
686 tcp0->ports.src = s0->out2in.port;
687 new_port0 = tcp0->ports.src;
689 sum0 = tcp0->checksum;
690 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
692 dst_address /* changed member */);
693 sum0 = ip_csum_update (sum0, old_port0, new_port0,
694 ip4_header_t /* cheat */,
695 length /* changed member */);
696 tcp0->checksum = ip_csum_fold(sum0);
700 old_port0 = udp0->src_port;
701 udp0->src_port = s0->out2in.port;
706 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
709 s0->last_heard = now;
711 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
712 /* Per-user LRU list maintenance for dynamic translation */
713 if (!snat_is_session_static (s0))
715 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
717 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
718 s0->per_user_list_head_index,
723 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
724 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
726 snat_in2out_trace_t *t =
727 vlib_add_trace (vm, node, b0, sizeof (*t));
728 t->is_slow_path = is_slow_path;
729 t->sw_if_index = sw_if_index0;
730 t->next_index = next0;
731 t->session_index = ~0;
733 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
736 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
738 ip1 = vlib_buffer_get_current (b1);
739 udp1 = ip4_next_header (ip1);
740 tcp1 = (tcp_header_t *) udp1;
741 icmp1 = (icmp46_header_t *) udp1;
743 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
744 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
748 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
749 ? SNAT_PROTOCOL_UDP : proto1;
750 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
751 ? SNAT_PROTOCOL_TCP : proto1;
752 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
753 ? SNAT_PROTOCOL_ICMP : proto1;
755 /* Next configured feature, probably ip4-lookup */
758 if (PREDICT_FALSE (proto1 == ~0))
761 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
763 next1 = icmp_in2out_slow_path
764 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
765 next1, now, cpu_index);
771 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
773 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
778 key1.addr = ip1->src_address;
779 key1.port = udp1->src_port;
780 key1.protocol = proto1;
781 key1.fib_index = rx_fib_index1;
783 kv1.key = key1.as_u64;
785 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
789 ip4_address_t * first_int_addr;
791 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index1))
794 ip4_interface_first_address (sm->ip4_main, sw_if_index1,
795 0 /* just want the address */);
796 rt->cached_sw_if_index = sw_if_index1;
798 rt->cached_ip4_address = first_int_addr->as_u32;
800 rt->cached_ip4_address = 0;
803 /* Don't NAT packet aimed at the intfc address */
804 if (PREDICT_FALSE(ip1->dst_address.as_u32 ==
805 rt->cached_ip4_address))
808 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
809 &s1, node, next1, cpu_index);
810 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
815 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
820 s1 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
823 old_addr1 = ip1->src_address.as_u32;
824 ip1->src_address = s1->out2in.addr;
825 new_addr1 = ip1->src_address.as_u32;
826 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
828 sum1 = ip1->checksum;
829 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
831 src_address /* changed member */);
832 ip1->checksum = ip_csum_fold (sum1);
834 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
836 old_port1 = tcp1->ports.src;
837 tcp1->ports.src = s1->out2in.port;
838 new_port1 = tcp1->ports.src;
840 sum1 = tcp1->checksum;
841 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
843 dst_address /* changed member */);
844 sum1 = ip_csum_update (sum1, old_port1, new_port1,
845 ip4_header_t /* cheat */,
846 length /* changed member */);
847 tcp1->checksum = ip_csum_fold(sum1);
851 old_port1 = udp1->src_port;
852 udp1->src_port = s1->out2in.port;
857 snat_hairpinning (sm, b1, ip1, udp1, tcp1, proto1);
860 s1->last_heard = now;
862 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
863 /* Per-user LRU list maintenance for dynamic translation */
864 if (!snat_is_session_static (s1))
866 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
868 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
869 s1->per_user_list_head_index,
874 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
875 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
877 snat_in2out_trace_t *t =
878 vlib_add_trace (vm, node, b1, sizeof (*t));
879 t->sw_if_index = sw_if_index1;
880 t->next_index = next1;
881 t->session_index = ~0;
883 t->session_index = s1 - sm->per_thread_data[cpu_index].sessions;
886 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
888 /* verify speculative enqueues, maybe switch current next frame */
889 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
890 to_next, n_left_to_next,
891 bi0, bi1, next0, next1);
894 while (n_left_from > 0 && n_left_to_next > 0)
902 u32 new_addr0, old_addr0;
903 u16 old_port0, new_port0;
906 icmp46_header_t * icmp0;
907 snat_session_key_t key0;
910 snat_session_t * s0 = 0;
911 clib_bihash_kv_8_8_t kv0, value0;
913 /* speculatively enqueue b0 to the current next frame */
921 b0 = vlib_get_buffer (vm, bi0);
922 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
924 ip0 = vlib_buffer_get_current (b0);
925 udp0 = ip4_next_header (ip0);
926 tcp0 = (tcp_header_t *) udp0;
927 icmp0 = (icmp46_header_t *) udp0;
929 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
930 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
934 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
935 ? SNAT_PROTOCOL_UDP : proto0;
936 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
937 ? SNAT_PROTOCOL_TCP : proto0;
938 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
939 ? SNAT_PROTOCOL_ICMP : proto0;
941 /* Next configured feature, probably ip4-lookup */
944 if (PREDICT_FALSE (proto0 == ~0))
947 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
949 next0 = icmp_in2out_slow_path
950 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
951 next0, now, cpu_index);
957 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
959 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
964 key0.addr = ip0->src_address;
965 key0.port = udp0->src_port;
966 key0.protocol = proto0;
967 key0.fib_index = rx_fib_index0;
969 kv0.key = key0.as_u64;
971 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
975 ip4_address_t * first_int_addr;
977 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
980 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
981 0 /* just want the address */);
982 rt->cached_sw_if_index = sw_if_index0;
984 rt->cached_ip4_address = first_int_addr->as_u32;
986 rt->cached_ip4_address = 0;
989 /* Don't NAT packet aimed at the intfc address */
990 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
991 rt->cached_ip4_address))
994 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
995 &s0, node, next0, cpu_index);
996 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
1001 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
1006 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
1009 old_addr0 = ip0->src_address.as_u32;
1010 ip0->src_address = s0->out2in.addr;
1011 new_addr0 = ip0->src_address.as_u32;
1012 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1014 sum0 = ip0->checksum;
1015 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1017 src_address /* changed member */);
1018 ip0->checksum = ip_csum_fold (sum0);
1020 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1022 old_port0 = tcp0->ports.src;
1023 tcp0->ports.src = s0->out2in.port;
1024 new_port0 = tcp0->ports.src;
1026 sum0 = tcp0->checksum;
1027 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1029 dst_address /* changed member */);
1030 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1031 ip4_header_t /* cheat */,
1032 length /* changed member */);
1033 tcp0->checksum = ip_csum_fold(sum0);
1037 old_port0 = udp0->src_port;
1038 udp0->src_port = s0->out2in.port;
1043 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1046 s0->last_heard = now;
1048 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1049 /* Per-user LRU list maintenance for dynamic translation */
1050 if (!snat_is_session_static (s0))
1052 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
1053 s0->per_user_index);
1054 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
1055 s0->per_user_list_head_index,
1056 s0->per_user_index);
1060 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1061 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1063 snat_in2out_trace_t *t =
1064 vlib_add_trace (vm, node, b0, sizeof (*t));
1065 t->is_slow_path = is_slow_path;
1066 t->sw_if_index = sw_if_index0;
1067 t->next_index = next0;
1068 t->session_index = ~0;
1070 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
1073 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1075 /* verify speculative enqueue, maybe switch current next frame */
1076 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1077 to_next, n_left_to_next,
1081 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1084 vlib_node_increment_counter (vm, stats_node_index,
1085 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1087 return frame->n_vectors;
1091 snat_in2out_fast_path_fn (vlib_main_t * vm,
1092 vlib_node_runtime_t * node,
1093 vlib_frame_t * frame)
1095 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
1098 VLIB_REGISTER_NODE (snat_in2out_node) = {
1099 .function = snat_in2out_fast_path_fn,
1100 .name = "snat-in2out",
1101 .vector_size = sizeof (u32),
1102 .format_trace = format_snat_in2out_trace,
1103 .type = VLIB_NODE_TYPE_INTERNAL,
1105 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1106 .error_strings = snat_in2out_error_strings,
1108 .runtime_data_bytes = sizeof (snat_runtime_t),
1110 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1112 /* edit / add dispositions here */
1114 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1115 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1116 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1120 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1123 snat_in2out_slow_path_fn (vlib_main_t * vm,
1124 vlib_node_runtime_t * node,
1125 vlib_frame_t * frame)
1127 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
1130 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1131 .function = snat_in2out_slow_path_fn,
1132 .name = "snat-in2out-slowpath",
1133 .vector_size = sizeof (u32),
1134 .format_trace = format_snat_in2out_trace,
1135 .type = VLIB_NODE_TYPE_INTERNAL,
1137 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1138 .error_strings = snat_in2out_error_strings,
1140 .runtime_data_bytes = sizeof (snat_runtime_t),
1142 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1144 /* edit / add dispositions here */
1146 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1147 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1148 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1152 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1155 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
1156 vlib_node_runtime_t * node,
1157 vlib_frame_t * frame)
1159 snat_main_t *sm = &snat_main;
1160 vlib_thread_main_t *tm = vlib_get_thread_main ();
1161 u32 n_left_from, *from, *to_next = 0;
1162 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
1163 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
1165 vlib_frame_queue_elt_t *hf = 0;
1166 vlib_frame_t *f = 0;
1168 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
1169 u32 next_worker_index = 0;
1170 u32 current_worker_index = ~0;
1171 u32 cpu_index = os_get_cpu_number ();
1173 ASSERT (vec_len (sm->workers));
1175 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
1177 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
1179 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
1180 sm->first_worker_index + sm->num_workers - 1,
1181 (vlib_frame_queue_t *) (~0));
1184 from = vlib_frame_vector_args (frame);
1185 n_left_from = frame->n_vectors;
1187 while (n_left_from > 0)
1194 snat_user_key_t key0;
1195 clib_bihash_kv_8_8_t kv0, value0;
1202 b0 = vlib_get_buffer (vm, bi0);
1204 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1205 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1207 ip0 = vlib_buffer_get_current (b0);
1209 key0.addr = ip0->src_address;
1210 key0.fib_index = rx_fib_index0;
1212 kv0.key = key0.as_u64;
1214 /* Ever heard of of the "user" before? */
1215 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1217 /* No, assign next available worker (RR) */
1218 next_worker_index = sm->first_worker_index +
1219 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1221 /* add non-traslated packets worker lookup */
1222 kv0.value = next_worker_index;
1223 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1226 next_worker_index = value0.value;
1228 if (PREDICT_FALSE (next_worker_index != cpu_index))
1232 if (next_worker_index != current_worker_index)
1235 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1237 hf = vlib_get_worker_handoff_queue_elt (sm->fq_in2out_index,
1239 handoff_queue_elt_by_worker_index);
1241 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
1242 to_next_worker = &hf->buffer_index[hf->n_vectors];
1243 current_worker_index = next_worker_index;
1246 /* enqueue to correct worker thread */
1247 to_next_worker[0] = bi0;
1249 n_left_to_next_worker--;
1251 if (n_left_to_next_worker == 0)
1253 hf->n_vectors = VLIB_FRAME_SIZE;
1254 vlib_put_frame_queue_elt (hf);
1255 current_worker_index = ~0;
1256 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
1263 /* if this is 1st frame */
1266 f = vlib_get_frame_to_node (vm, snat_in2out_node.index);
1267 to_next = vlib_frame_vector_args (f);
1275 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1276 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1278 snat_in2out_worker_handoff_trace_t *t =
1279 vlib_add_trace (vm, node, b0, sizeof (*t));
1280 t->next_worker_index = next_worker_index;
1281 t->do_handoff = do_handoff;
1286 vlib_put_frame_to_node (vm, snat_in2out_node.index, f);
1289 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1291 /* Ship frames to the worker nodes */
1292 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
1294 if (handoff_queue_elt_by_worker_index[i])
1296 hf = handoff_queue_elt_by_worker_index[i];
1298 * It works better to let the handoff node
1299 * rate-adapt, always ship the handoff queue element.
1301 if (1 || hf->n_vectors == hf->last_n_vectors)
1303 vlib_put_frame_queue_elt (hf);
1304 handoff_queue_elt_by_worker_index[i] = 0;
1307 hf->last_n_vectors = hf->n_vectors;
1309 congested_handoff_queue_by_worker_index[i] =
1310 (vlib_frame_queue_t *) (~0);
1313 current_worker_index = ~0;
1314 return frame->n_vectors;
1317 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
1318 .function = snat_in2out_worker_handoff_fn,
1319 .name = "snat-in2out-worker-handoff",
1320 .vector_size = sizeof (u32),
1321 .format_trace = format_snat_in2out_worker_handoff_trace,
1322 .type = VLIB_NODE_TYPE_INTERNAL,
1331 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn);
1333 static inline u32 icmp_in2out_static_map (snat_main_t *sm,
1336 icmp46_header_t * icmp0,
1338 vlib_node_runtime_t * node,
1342 snat_session_key_t key0, sm0;
1343 icmp_echo_header_t *echo0;
1344 u32 new_addr0, old_addr0;
1345 u16 old_id0, new_id0;
1347 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1349 echo0 = (icmp_echo_header_t *)(icmp0+1);
1351 key0.addr = ip0->src_address;
1352 key0.port = echo0->identifier;
1353 key0.fib_index = rx_fib_index0;
1355 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1357 ip4_address_t * first_int_addr;
1359 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1362 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1363 0 /* just want the address */);
1364 rt->cached_sw_if_index = sw_if_index0;
1366 rt->cached_ip4_address = first_int_addr->as_u32;
1368 rt->cached_ip4_address = 0;
1371 /* Don't NAT packet aimed at the intfc address */
1372 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1373 rt->cached_ip4_address))
1376 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1377 return SNAT_IN2OUT_NEXT_DROP;
1380 new_addr0 = sm0.addr.as_u32;
1382 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1383 old_addr0 = ip0->src_address.as_u32;
1384 ip0->src_address.as_u32 = new_addr0;
1386 sum0 = ip0->checksum;
1387 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1389 src_address /* changed member */);
1390 ip0->checksum = ip_csum_fold (sum0);
1392 if (PREDICT_FALSE(new_id0 != echo0->identifier))
1394 old_id0 = echo0->identifier;
1395 echo0->identifier = new_id0;
1397 sum0 = icmp0->checksum;
1398 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
1400 icmp0->checksum = ip_csum_fold (sum0);
1407 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
1408 vlib_node_runtime_t * node,
1409 vlib_frame_t * frame)
1411 u32 n_left_from, * from, * to_next;
1412 snat_in2out_next_t next_index;
1413 u32 pkts_processed = 0;
1414 snat_main_t * sm = &snat_main;
1415 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1416 u32 stats_node_index;
1418 stats_node_index = snat_in2out_fast_node.index;
1420 from = vlib_frame_vector_args (frame);
1421 n_left_from = frame->n_vectors;
1422 next_index = node->cached_next_index;
1424 while (n_left_from > 0)
1428 vlib_get_next_frame (vm, node, next_index,
1429 to_next, n_left_to_next);
1431 while (n_left_from > 0 && n_left_to_next > 0)
1439 u32 new_addr0, old_addr0;
1440 u16 old_port0, new_port0;
1441 udp_header_t * udp0;
1442 tcp_header_t * tcp0;
1443 icmp46_header_t * icmp0;
1444 snat_session_key_t key0, sm0;
1448 /* speculatively enqueue b0 to the current next frame */
1454 n_left_to_next -= 1;
1456 b0 = vlib_get_buffer (vm, bi0);
1457 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1459 ip0 = vlib_buffer_get_current (b0);
1460 udp0 = ip4_next_header (ip0);
1461 tcp0 = (tcp_header_t *) udp0;
1462 icmp0 = (icmp46_header_t *) udp0;
1464 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1465 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1468 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
1469 ? SNAT_PROTOCOL_UDP : proto0;
1470 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
1471 ? SNAT_PROTOCOL_TCP : proto0;
1472 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
1473 ? SNAT_PROTOCOL_ICMP : proto0;
1475 if (PREDICT_FALSE (proto0 == ~0))
1478 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1480 ip4_address_t * first_int_addr;
1482 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1485 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1486 0 /* just want the address */);
1487 rt->cached_sw_if_index = sw_if_index0;
1488 rt->cached_ip4_address = first_int_addr->as_u32;
1491 /* Don't NAT packet aimed at the intfc address */
1492 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1493 rt->cached_ip4_address))
1496 next0 = icmp_in2out_static_map
1497 (sm, b0, ip0, icmp0, sw_if_index0, node, next0, rx_fib_index0);
1501 key0.addr = ip0->src_address;
1502 key0.port = udp0->src_port;
1503 key0.fib_index = rx_fib_index0;
1505 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1507 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1508 next0= SNAT_IN2OUT_NEXT_DROP;
1512 new_addr0 = sm0.addr.as_u32;
1513 new_port0 = sm0.port;
1514 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1515 old_addr0 = ip0->src_address.as_u32;
1516 ip0->src_address.as_u32 = new_addr0;
1518 sum0 = ip0->checksum;
1519 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1521 src_address /* changed member */);
1522 ip0->checksum = ip_csum_fold (sum0);
1524 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
1526 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1528 old_port0 = tcp0->ports.src;
1529 tcp0->ports.src = new_port0;
1531 sum0 = tcp0->checksum;
1532 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1534 dst_address /* changed member */);
1535 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1536 ip4_header_t /* cheat */,
1537 length /* changed member */);
1538 tcp0->checksum = ip_csum_fold(sum0);
1542 old_port0 = udp0->src_port;
1543 udp0->src_port = new_port0;
1549 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1551 sum0 = tcp0->checksum;
1552 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1554 dst_address /* changed member */);
1555 tcp0->checksum = ip_csum_fold(sum0);
1560 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1563 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1564 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1566 snat_in2out_trace_t *t =
1567 vlib_add_trace (vm, node, b0, sizeof (*t));
1568 t->sw_if_index = sw_if_index0;
1569 t->next_index = next0;
1572 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1574 /* verify speculative enqueue, maybe switch current next frame */
1575 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1576 to_next, n_left_to_next,
1580 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1583 vlib_node_increment_counter (vm, stats_node_index,
1584 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1586 return frame->n_vectors;
1590 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1591 .function = snat_in2out_fast_static_map_fn,
1592 .name = "snat-in2out-fast",
1593 .vector_size = sizeof (u32),
1594 .format_trace = format_snat_in2out_fast_trace,
1595 .type = VLIB_NODE_TYPE_INTERNAL,
1597 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1598 .error_strings = snat_in2out_error_strings,
1600 .runtime_data_bytes = sizeof (snat_runtime_t),
1602 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1604 /* edit / add dispositions here */
1606 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1607 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1608 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1612 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);