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;
353 rt->cached_ip4_address = first_int_addr->as_u32;
356 /* Don't NAT packet aimed at the intfc address */
357 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
358 rt->cached_ip4_address))
361 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
362 &s0, node, next0, cpu_index);
364 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
368 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
371 old_addr0 = ip0->src_address.as_u32;
372 ip0->src_address = s0->out2in.addr;
373 new_addr0 = ip0->src_address.as_u32;
374 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
376 sum0 = ip0->checksum;
377 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
379 src_address /* changed member */);
380 ip0->checksum = ip_csum_fold (sum0);
382 old_id0 = echo0->identifier;
383 new_id0 = s0->out2in.port;
384 echo0->identifier = new_id0;
386 sum0 = icmp0->checksum;
387 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
389 icmp0->checksum = ip_csum_fold (sum0);
392 s0->last_heard = now;
394 s0->total_bytes += vlib_buffer_length_in_chain (sm->vlib_main, b0);
395 /* Per-user LRU list maintenance for dynamic translations */
396 if (!snat_is_session_static (s0))
398 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
400 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
401 s0->per_user_list_head_index,
411 * Hairpinning allows two endpoints on the internal side of the NAT to
412 * communicate even if they only use each other's external IP addresses
415 * @param sm SNAT main.
416 * @param b0 Vlib buffer.
417 * @param ip0 IP header.
418 * @param udp0 UDP header.
419 * @param tcp0 TCP header.
420 * @param proto0 SNAT protocol.
423 snat_hairpinning (snat_main_t *sm,
430 snat_session_key_t key0, sm0;
431 snat_static_mapping_key_t k0;
433 clib_bihash_kv_8_8_t kv0, value0;
435 u32 new_dst_addr0 = 0, old_dst_addr0, ti = 0, si;
436 u16 new_dst_port0, old_dst_port0;
438 key0.addr = ip0->dst_address;
439 key0.port = udp0->dst_port;
440 key0.protocol = proto0;
441 key0.fib_index = sm->outside_fib_index;
442 kv0.key = key0.as_u64;
444 /* Check if destination is in active sessions */
445 if (clib_bihash_search_8_8 (&sm->out2in, &kv0, &value0))
447 /* or static mappings */
448 if (!snat_static_mapping_match(sm, key0, &sm0, 1))
450 new_dst_addr0 = sm0.addr.as_u32;
451 new_dst_port0 = sm0.port;
452 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
458 if (sm->num_workers > 1)
460 k0.addr = ip0->dst_address;
461 k0.port = udp0->dst_port;
462 k0.fib_index = sm->outside_fib_index;
464 if (clib_bihash_search_8_8 (&sm->worker_by_out, &kv0, &value0))
470 ti = sm->num_workers;
472 s0 = pool_elt_at_index (sm->per_thread_data[ti].sessions, si);
473 new_dst_addr0 = s0->in2out.addr.as_u32;
474 new_dst_port0 = s0->in2out.port;
475 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
478 /* Destination is behind the same NAT, use internal address and port */
481 old_dst_addr0 = ip0->dst_address.as_u32;
482 ip0->dst_address.as_u32 = new_dst_addr0;
483 sum0 = ip0->checksum;
484 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
485 ip4_header_t, dst_address);
486 ip0->checksum = ip_csum_fold (sum0);
488 old_dst_port0 = tcp0->ports.dst;
489 if (PREDICT_TRUE(new_dst_port0 != old_dst_port0))
491 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
493 tcp0->ports.dst = new_dst_port0;
494 sum0 = tcp0->checksum;
495 sum0 = ip_csum_update (sum0, old_dst_addr0, new_dst_addr0,
496 ip4_header_t, dst_address);
497 sum0 = ip_csum_update (sum0, old_dst_port0, new_dst_port0,
498 ip4_header_t /* cheat */, length);
499 tcp0->checksum = ip_csum_fold(sum0);
503 udp0->dst_port = new_dst_port0;
511 snat_in2out_node_fn_inline (vlib_main_t * vm,
512 vlib_node_runtime_t * node,
513 vlib_frame_t * frame, int is_slow_path)
515 u32 n_left_from, * from, * to_next;
516 snat_in2out_next_t next_index;
517 u32 pkts_processed = 0;
518 snat_main_t * sm = &snat_main;
519 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
520 f64 now = vlib_time_now (vm);
521 u32 stats_node_index;
522 u32 cpu_index = os_get_cpu_number ();
524 stats_node_index = is_slow_path ? snat_in2out_slowpath_node.index :
525 snat_in2out_node.index;
527 from = vlib_frame_vector_args (frame);
528 n_left_from = frame->n_vectors;
529 next_index = node->cached_next_index;
531 while (n_left_from > 0)
535 vlib_get_next_frame (vm, node, next_index,
536 to_next, n_left_to_next);
538 while (n_left_from >= 4 && n_left_to_next >= 2)
541 vlib_buffer_t * b0, * b1;
543 u32 sw_if_index0, sw_if_index1;
544 ip4_header_t * ip0, * ip1;
545 ip_csum_t sum0, sum1;
546 u32 new_addr0, old_addr0, new_addr1, old_addr1;
547 u16 old_port0, new_port0, old_port1, new_port1;
548 udp_header_t * udp0, * udp1;
549 tcp_header_t * tcp0, * tcp1;
550 icmp46_header_t * icmp0, * icmp1;
551 snat_session_key_t key0, key1;
552 u32 rx_fib_index0, rx_fib_index1;
554 snat_session_t * s0 = 0, * s1 = 0;
555 clib_bihash_kv_8_8_t kv0, value0, kv1, value1;
557 /* Prefetch next iteration. */
559 vlib_buffer_t * p2, * p3;
561 p2 = vlib_get_buffer (vm, from[2]);
562 p3 = vlib_get_buffer (vm, from[3]);
564 vlib_prefetch_buffer_header (p2, LOAD);
565 vlib_prefetch_buffer_header (p3, LOAD);
567 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
568 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
571 /* speculatively enqueue b0 and b1 to the current next frame */
572 to_next[0] = bi0 = from[0];
573 to_next[1] = bi1 = from[1];
579 b0 = vlib_get_buffer (vm, bi0);
580 b1 = vlib_get_buffer (vm, bi1);
582 ip0 = vlib_buffer_get_current (b0);
583 udp0 = ip4_next_header (ip0);
584 tcp0 = (tcp_header_t *) udp0;
585 icmp0 = (icmp46_header_t *) udp0;
587 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
588 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
591 next0 = next1 = SNAT_IN2OUT_NEXT_LOOKUP;
594 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
595 ? SNAT_PROTOCOL_UDP : proto0;
596 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
597 ? SNAT_PROTOCOL_TCP : proto0;
598 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
599 ? SNAT_PROTOCOL_ICMP : proto0;
601 /* Next configured feature, probably ip4-lookup */
604 if (PREDICT_FALSE (proto0 == ~0))
607 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
609 next0 = icmp_in2out_slow_path
610 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0,
611 node, next0, now, cpu_index);
617 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
619 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
624 key0.addr = ip0->src_address;
625 key0.port = udp0->src_port;
626 key0.protocol = proto0;
627 key0.fib_index = rx_fib_index0;
629 kv0.key = key0.as_u64;
631 if (PREDICT_FALSE (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0) != 0))
635 ip4_address_t * first_int_addr;
637 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
640 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
641 0 /* just want the address */);
642 rt->cached_sw_if_index = sw_if_index0;
643 rt->cached_ip4_address = first_int_addr->as_u32;
646 /* Don't NAT packet aimed at the intfc address */
647 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
648 rt->cached_ip4_address))
651 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
652 &s0, node, next0, cpu_index);
653 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
658 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
663 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
666 old_addr0 = ip0->src_address.as_u32;
667 ip0->src_address = s0->out2in.addr;
668 new_addr0 = ip0->src_address.as_u32;
669 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
671 sum0 = ip0->checksum;
672 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
674 src_address /* changed member */);
675 ip0->checksum = ip_csum_fold (sum0);
677 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
679 old_port0 = tcp0->ports.src;
680 tcp0->ports.src = s0->out2in.port;
681 new_port0 = tcp0->ports.src;
683 sum0 = tcp0->checksum;
684 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
686 dst_address /* changed member */);
687 sum0 = ip_csum_update (sum0, old_port0, new_port0,
688 ip4_header_t /* cheat */,
689 length /* changed member */);
690 tcp0->checksum = ip_csum_fold(sum0);
694 old_port0 = udp0->src_port;
695 udp0->src_port = s0->out2in.port;
700 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
703 s0->last_heard = now;
705 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
706 /* Per-user LRU list maintenance for dynamic translation */
707 if (!snat_is_session_static (s0))
709 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
711 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
712 s0->per_user_list_head_index,
717 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
718 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
720 snat_in2out_trace_t *t =
721 vlib_add_trace (vm, node, b0, sizeof (*t));
722 t->is_slow_path = is_slow_path;
723 t->sw_if_index = sw_if_index0;
724 t->next_index = next0;
725 t->session_index = ~0;
727 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
730 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
732 ip1 = vlib_buffer_get_current (b1);
733 udp1 = ip4_next_header (ip1);
734 tcp1 = (tcp_header_t *) udp1;
735 icmp1 = (icmp46_header_t *) udp1;
737 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
738 rx_fib_index1 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
742 proto1 = (ip1->protocol == IP_PROTOCOL_UDP)
743 ? SNAT_PROTOCOL_UDP : proto1;
744 proto1 = (ip1->protocol == IP_PROTOCOL_TCP)
745 ? SNAT_PROTOCOL_TCP : proto1;
746 proto1 = (ip1->protocol == IP_PROTOCOL_ICMP)
747 ? SNAT_PROTOCOL_ICMP : proto1;
749 /* Next configured feature, probably ip4-lookup */
752 if (PREDICT_FALSE (proto1 == ~0))
755 if (PREDICT_FALSE (proto1 == SNAT_PROTOCOL_ICMP))
757 next1 = icmp_in2out_slow_path
758 (sm, b1, ip1, icmp1, sw_if_index1, rx_fib_index1, node,
759 next1, now, cpu_index);
765 if (PREDICT_FALSE (proto1 == ~0 || proto1 == SNAT_PROTOCOL_ICMP))
767 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
772 key1.addr = ip1->src_address;
773 key1.port = udp1->src_port;
774 key1.protocol = proto1;
775 key1.fib_index = rx_fib_index1;
777 kv1.key = key1.as_u64;
779 if (PREDICT_FALSE(clib_bihash_search_8_8 (&sm->in2out, &kv1, &value1) != 0))
783 ip4_address_t * first_int_addr;
785 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index1))
788 ip4_interface_first_address (sm->ip4_main, sw_if_index1,
789 0 /* just want the address */);
790 rt->cached_sw_if_index = sw_if_index1;
791 rt->cached_ip4_address = first_int_addr->as_u32;
794 /* Don't NAT packet aimed at the intfc address */
795 if (PREDICT_FALSE(ip1->dst_address.as_u32 ==
796 rt->cached_ip4_address))
799 next1 = slow_path (sm, b1, ip1, rx_fib_index1, &key1,
800 &s1, node, next1, cpu_index);
801 if (PREDICT_FALSE (next1 == SNAT_IN2OUT_NEXT_DROP))
806 next1 = SNAT_IN2OUT_NEXT_SLOW_PATH;
811 s1 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
814 old_addr1 = ip1->src_address.as_u32;
815 ip1->src_address = s1->out2in.addr;
816 new_addr1 = ip1->src_address.as_u32;
817 vnet_buffer(b1)->sw_if_index[VLIB_TX] = s1->out2in.fib_index;
819 sum1 = ip1->checksum;
820 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
822 src_address /* changed member */);
823 ip1->checksum = ip_csum_fold (sum1);
825 if (PREDICT_TRUE(proto1 == SNAT_PROTOCOL_TCP))
827 old_port1 = tcp1->ports.src;
828 tcp1->ports.src = s1->out2in.port;
829 new_port1 = tcp1->ports.src;
831 sum1 = tcp1->checksum;
832 sum1 = ip_csum_update (sum1, old_addr1, new_addr1,
834 dst_address /* changed member */);
835 sum1 = ip_csum_update (sum1, old_port1, new_port1,
836 ip4_header_t /* cheat */,
837 length /* changed member */);
838 tcp1->checksum = ip_csum_fold(sum1);
842 old_port1 = udp1->src_port;
843 udp1->src_port = s1->out2in.port;
848 snat_hairpinning (sm, b1, ip1, udp1, tcp1, proto1);
851 s1->last_heard = now;
853 s1->total_bytes += vlib_buffer_length_in_chain (vm, b1);
854 /* Per-user LRU list maintenance for dynamic translation */
855 if (!snat_is_session_static (s1))
857 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
859 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
860 s1->per_user_list_head_index,
865 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
866 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
868 snat_in2out_trace_t *t =
869 vlib_add_trace (vm, node, b1, sizeof (*t));
870 t->sw_if_index = sw_if_index1;
871 t->next_index = next1;
872 t->session_index = ~0;
874 t->session_index = s1 - sm->per_thread_data[cpu_index].sessions;
877 pkts_processed += next1 != SNAT_IN2OUT_NEXT_DROP;
879 /* verify speculative enqueues, maybe switch current next frame */
880 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
881 to_next, n_left_to_next,
882 bi0, bi1, next0, next1);
885 while (n_left_from > 0 && n_left_to_next > 0)
893 u32 new_addr0, old_addr0;
894 u16 old_port0, new_port0;
897 icmp46_header_t * icmp0;
898 snat_session_key_t key0;
901 snat_session_t * s0 = 0;
902 clib_bihash_kv_8_8_t kv0, value0;
904 /* speculatively enqueue b0 to the current next frame */
912 b0 = vlib_get_buffer (vm, bi0);
913 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
915 ip0 = vlib_buffer_get_current (b0);
916 udp0 = ip4_next_header (ip0);
917 tcp0 = (tcp_header_t *) udp0;
918 icmp0 = (icmp46_header_t *) udp0;
920 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
921 rx_fib_index0 = vec_elt (sm->ip4_main->fib_index_by_sw_if_index,
925 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
926 ? SNAT_PROTOCOL_UDP : proto0;
927 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
928 ? SNAT_PROTOCOL_TCP : proto0;
929 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
930 ? SNAT_PROTOCOL_ICMP : proto0;
932 /* Next configured feature, probably ip4-lookup */
935 if (PREDICT_FALSE (proto0 == ~0))
938 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
940 next0 = icmp_in2out_slow_path
941 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
942 next0, now, cpu_index);
948 if (PREDICT_FALSE (proto0 == ~0 || proto0 == SNAT_PROTOCOL_ICMP))
950 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
955 key0.addr = ip0->src_address;
956 key0.port = udp0->src_port;
957 key0.protocol = proto0;
958 key0.fib_index = rx_fib_index0;
960 kv0.key = key0.as_u64;
962 if (clib_bihash_search_8_8 (&sm->in2out, &kv0, &value0))
966 ip4_address_t * first_int_addr;
968 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
971 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
972 0 /* just want the address */);
973 rt->cached_sw_if_index = sw_if_index0;
974 rt->cached_ip4_address = first_int_addr->as_u32;
977 /* Don't NAT packet aimed at the intfc address */
978 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
979 rt->cached_ip4_address))
982 next0 = slow_path (sm, b0, ip0, rx_fib_index0, &key0,
983 &s0, node, next0, cpu_index);
984 if (PREDICT_FALSE (next0 == SNAT_IN2OUT_NEXT_DROP))
989 next0 = SNAT_IN2OUT_NEXT_SLOW_PATH;
994 s0 = pool_elt_at_index (sm->per_thread_data[cpu_index].sessions,
997 old_addr0 = ip0->src_address.as_u32;
998 ip0->src_address = s0->out2in.addr;
999 new_addr0 = ip0->src_address.as_u32;
1000 vnet_buffer(b0)->sw_if_index[VLIB_TX] = s0->out2in.fib_index;
1002 sum0 = ip0->checksum;
1003 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1005 src_address /* changed member */);
1006 ip0->checksum = ip_csum_fold (sum0);
1008 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1010 old_port0 = tcp0->ports.src;
1011 tcp0->ports.src = s0->out2in.port;
1012 new_port0 = tcp0->ports.src;
1014 sum0 = tcp0->checksum;
1015 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1017 dst_address /* changed member */);
1018 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1019 ip4_header_t /* cheat */,
1020 length /* changed member */);
1021 tcp0->checksum = ip_csum_fold(sum0);
1025 old_port0 = udp0->src_port;
1026 udp0->src_port = s0->out2in.port;
1031 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1034 s0->last_heard = now;
1036 s0->total_bytes += vlib_buffer_length_in_chain (vm, b0);
1037 /* Per-user LRU list maintenance for dynamic translation */
1038 if (!snat_is_session_static (s0))
1040 clib_dlist_remove (sm->per_thread_data[cpu_index].list_pool,
1041 s0->per_user_index);
1042 clib_dlist_addtail (sm->per_thread_data[cpu_index].list_pool,
1043 s0->per_user_list_head_index,
1044 s0->per_user_index);
1048 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1049 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1051 snat_in2out_trace_t *t =
1052 vlib_add_trace (vm, node, b0, sizeof (*t));
1053 t->is_slow_path = is_slow_path;
1054 t->sw_if_index = sw_if_index0;
1055 t->next_index = next0;
1056 t->session_index = ~0;
1058 t->session_index = s0 - sm->per_thread_data[cpu_index].sessions;
1061 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1063 /* verify speculative enqueue, maybe switch current next frame */
1064 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1065 to_next, n_left_to_next,
1069 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1072 vlib_node_increment_counter (vm, stats_node_index,
1073 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1075 return frame->n_vectors;
1079 snat_in2out_fast_path_fn (vlib_main_t * vm,
1080 vlib_node_runtime_t * node,
1081 vlib_frame_t * frame)
1083 return snat_in2out_node_fn_inline (vm, node, frame, 0 /* is_slow_path */);
1086 VLIB_REGISTER_NODE (snat_in2out_node) = {
1087 .function = snat_in2out_fast_path_fn,
1088 .name = "snat-in2out",
1089 .vector_size = sizeof (u32),
1090 .format_trace = format_snat_in2out_trace,
1091 .type = VLIB_NODE_TYPE_INTERNAL,
1093 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1094 .error_strings = snat_in2out_error_strings,
1096 .runtime_data_bytes = sizeof (snat_runtime_t),
1098 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1100 /* edit / add dispositions here */
1102 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1103 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1104 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1108 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_node, snat_in2out_fast_path_fn);
1111 snat_in2out_slow_path_fn (vlib_main_t * vm,
1112 vlib_node_runtime_t * node,
1113 vlib_frame_t * frame)
1115 return snat_in2out_node_fn_inline (vm, node, frame, 1 /* is_slow_path */);
1118 VLIB_REGISTER_NODE (snat_in2out_slowpath_node) = {
1119 .function = snat_in2out_slow_path_fn,
1120 .name = "snat-in2out-slowpath",
1121 .vector_size = sizeof (u32),
1122 .format_trace = format_snat_in2out_trace,
1123 .type = VLIB_NODE_TYPE_INTERNAL,
1125 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1126 .error_strings = snat_in2out_error_strings,
1128 .runtime_data_bytes = sizeof (snat_runtime_t),
1130 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1132 /* edit / add dispositions here */
1134 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1135 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1136 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1140 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_slowpath_node, snat_in2out_slow_path_fn);
1143 snat_in2out_worker_handoff_fn (vlib_main_t * vm,
1144 vlib_node_runtime_t * node,
1145 vlib_frame_t * frame)
1147 snat_main_t *sm = &snat_main;
1148 vlib_thread_main_t *tm = vlib_get_thread_main ();
1149 u32 n_left_from, *from, *to_next = 0;
1150 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
1151 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
1153 vlib_frame_queue_elt_t *hf = 0;
1154 vlib_frame_t *f = 0;
1156 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
1157 u32 next_worker_index = 0;
1158 u32 current_worker_index = ~0;
1159 u32 cpu_index = os_get_cpu_number ();
1161 ASSERT (vec_len (sm->workers));
1163 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
1165 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
1167 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
1168 sm->first_worker_index + sm->num_workers - 1,
1169 (vlib_frame_queue_t *) (~0));
1172 from = vlib_frame_vector_args (frame);
1173 n_left_from = frame->n_vectors;
1175 while (n_left_from > 0)
1182 snat_user_key_t key0;
1183 clib_bihash_kv_8_8_t kv0, value0;
1190 b0 = vlib_get_buffer (vm, bi0);
1192 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1193 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1195 ip0 = vlib_buffer_get_current (b0);
1197 key0.addr = ip0->src_address;
1198 key0.fib_index = rx_fib_index0;
1200 kv0.key = key0.as_u64;
1202 /* Ever heard of of the "user" before? */
1203 if (clib_bihash_search_8_8 (&sm->worker_by_in, &kv0, &value0))
1205 /* No, assign next available worker (RR) */
1206 next_worker_index = sm->first_worker_index +
1207 sm->workers[sm->next_worker++ % vec_len (sm->workers)];
1209 /* add non-traslated packets worker lookup */
1210 kv0.value = next_worker_index;
1211 clib_bihash_add_del_8_8 (&sm->worker_by_in, &kv0, 1);
1214 next_worker_index = value0.value;
1216 if (PREDICT_FALSE (next_worker_index != cpu_index))
1220 if (next_worker_index != current_worker_index)
1223 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1225 hf = vlib_get_worker_handoff_queue_elt (sm->fq_in2out_index,
1227 handoff_queue_elt_by_worker_index);
1229 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
1230 to_next_worker = &hf->buffer_index[hf->n_vectors];
1231 current_worker_index = next_worker_index;
1234 /* enqueue to correct worker thread */
1235 to_next_worker[0] = bi0;
1237 n_left_to_next_worker--;
1239 if (n_left_to_next_worker == 0)
1241 hf->n_vectors = VLIB_FRAME_SIZE;
1242 vlib_put_frame_queue_elt (hf);
1243 current_worker_index = ~0;
1244 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
1251 /* if this is 1st frame */
1254 f = vlib_get_frame_to_node (vm, snat_in2out_node.index);
1255 to_next = vlib_frame_vector_args (f);
1263 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1264 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1266 snat_in2out_worker_handoff_trace_t *t =
1267 vlib_add_trace (vm, node, b0, sizeof (*t));
1268 t->next_worker_index = next_worker_index;
1269 t->do_handoff = do_handoff;
1274 vlib_put_frame_to_node (vm, snat_in2out_node.index, f);
1277 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1279 /* Ship frames to the worker nodes */
1280 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
1282 if (handoff_queue_elt_by_worker_index[i])
1284 hf = handoff_queue_elt_by_worker_index[i];
1286 * It works better to let the handoff node
1287 * rate-adapt, always ship the handoff queue element.
1289 if (1 || hf->n_vectors == hf->last_n_vectors)
1291 vlib_put_frame_queue_elt (hf);
1292 handoff_queue_elt_by_worker_index[i] = 0;
1295 hf->last_n_vectors = hf->n_vectors;
1297 congested_handoff_queue_by_worker_index[i] =
1298 (vlib_frame_queue_t *) (~0);
1301 current_worker_index = ~0;
1302 return frame->n_vectors;
1305 VLIB_REGISTER_NODE (snat_in2out_worker_handoff_node) = {
1306 .function = snat_in2out_worker_handoff_fn,
1307 .name = "snat-in2out-worker-handoff",
1308 .vector_size = sizeof (u32),
1309 .format_trace = format_snat_in2out_worker_handoff_trace,
1310 .type = VLIB_NODE_TYPE_INTERNAL,
1319 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_worker_handoff_node, snat_in2out_worker_handoff_fn);
1321 static inline u32 icmp_in2out_static_map (snat_main_t *sm,
1324 icmp46_header_t * icmp0,
1326 vlib_node_runtime_t * node,
1330 snat_session_key_t key0, sm0;
1331 icmp_echo_header_t *echo0;
1332 u32 new_addr0, old_addr0;
1333 u16 old_id0, new_id0;
1335 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1337 echo0 = (icmp_echo_header_t *)(icmp0+1);
1339 key0.addr = ip0->src_address;
1340 key0.port = echo0->identifier;
1341 key0.fib_index = rx_fib_index0;
1343 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1345 ip4_address_t * first_int_addr;
1347 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1350 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1351 0 /* just want the address */);
1352 rt->cached_sw_if_index = sw_if_index0;
1353 rt->cached_ip4_address = first_int_addr->as_u32;
1356 /* Don't NAT packet aimed at the intfc address */
1357 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1358 rt->cached_ip4_address))
1361 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1362 return SNAT_IN2OUT_NEXT_DROP;
1365 new_addr0 = sm0.addr.as_u32;
1367 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1368 old_addr0 = ip0->src_address.as_u32;
1369 ip0->src_address.as_u32 = new_addr0;
1371 sum0 = ip0->checksum;
1372 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1374 src_address /* changed member */);
1375 ip0->checksum = ip_csum_fold (sum0);
1377 if (PREDICT_FALSE(new_id0 != echo0->identifier))
1379 old_id0 = echo0->identifier;
1380 echo0->identifier = new_id0;
1382 sum0 = icmp0->checksum;
1383 sum0 = ip_csum_update (sum0, old_id0, new_id0, icmp_echo_header_t,
1385 icmp0->checksum = ip_csum_fold (sum0);
1392 snat_in2out_fast_static_map_fn (vlib_main_t * vm,
1393 vlib_node_runtime_t * node,
1394 vlib_frame_t * frame)
1396 u32 n_left_from, * from, * to_next;
1397 snat_in2out_next_t next_index;
1398 u32 pkts_processed = 0;
1399 snat_main_t * sm = &snat_main;
1400 snat_runtime_t * rt = (snat_runtime_t *)node->runtime_data;
1401 u32 stats_node_index;
1403 stats_node_index = snat_in2out_fast_node.index;
1405 from = vlib_frame_vector_args (frame);
1406 n_left_from = frame->n_vectors;
1407 next_index = node->cached_next_index;
1409 while (n_left_from > 0)
1413 vlib_get_next_frame (vm, node, next_index,
1414 to_next, n_left_to_next);
1416 while (n_left_from > 0 && n_left_to_next > 0)
1424 u32 new_addr0, old_addr0;
1425 u16 old_port0, new_port0;
1426 udp_header_t * udp0;
1427 tcp_header_t * tcp0;
1428 icmp46_header_t * icmp0;
1429 snat_session_key_t key0, sm0;
1433 /* speculatively enqueue b0 to the current next frame */
1439 n_left_to_next -= 1;
1441 b0 = vlib_get_buffer (vm, bi0);
1442 next0 = SNAT_IN2OUT_NEXT_LOOKUP;
1444 ip0 = vlib_buffer_get_current (b0);
1445 udp0 = ip4_next_header (ip0);
1446 tcp0 = (tcp_header_t *) udp0;
1447 icmp0 = (icmp46_header_t *) udp0;
1449 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
1450 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index(sw_if_index0);
1453 proto0 = (ip0->protocol == IP_PROTOCOL_UDP)
1454 ? SNAT_PROTOCOL_UDP : proto0;
1455 proto0 = (ip0->protocol == IP_PROTOCOL_TCP)
1456 ? SNAT_PROTOCOL_TCP : proto0;
1457 proto0 = (ip0->protocol == IP_PROTOCOL_ICMP)
1458 ? SNAT_PROTOCOL_ICMP : proto0;
1460 if (PREDICT_FALSE (proto0 == ~0))
1463 if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
1465 ip4_address_t * first_int_addr;
1467 if (PREDICT_FALSE(rt->cached_sw_if_index != sw_if_index0))
1470 ip4_interface_first_address (sm->ip4_main, sw_if_index0,
1471 0 /* just want the address */);
1472 rt->cached_sw_if_index = sw_if_index0;
1473 rt->cached_ip4_address = first_int_addr->as_u32;
1476 /* Don't NAT packet aimed at the intfc address */
1477 if (PREDICT_FALSE(ip0->dst_address.as_u32 ==
1478 rt->cached_ip4_address))
1481 next0 = icmp_in2out_static_map
1482 (sm, b0, ip0, icmp0, sw_if_index0, node, next0, rx_fib_index0);
1486 key0.addr = ip0->src_address;
1487 key0.port = udp0->src_port;
1488 key0.fib_index = rx_fib_index0;
1490 if (snat_static_mapping_match(sm, key0, &sm0, 0))
1492 b0->error = node->errors[SNAT_IN2OUT_ERROR_NO_TRANSLATION];
1493 next0= SNAT_IN2OUT_NEXT_DROP;
1497 new_addr0 = sm0.addr.as_u32;
1498 new_port0 = sm0.port;
1499 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm0.fib_index;
1500 old_addr0 = ip0->src_address.as_u32;
1501 ip0->src_address.as_u32 = new_addr0;
1503 sum0 = ip0->checksum;
1504 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1506 src_address /* changed member */);
1507 ip0->checksum = ip_csum_fold (sum0);
1509 if (PREDICT_FALSE(new_port0 != udp0->dst_port))
1511 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1513 old_port0 = tcp0->ports.src;
1514 tcp0->ports.src = new_port0;
1516 sum0 = tcp0->checksum;
1517 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1519 dst_address /* changed member */);
1520 sum0 = ip_csum_update (sum0, old_port0, new_port0,
1521 ip4_header_t /* cheat */,
1522 length /* changed member */);
1523 tcp0->checksum = ip_csum_fold(sum0);
1527 old_port0 = udp0->src_port;
1528 udp0->src_port = new_port0;
1534 if (PREDICT_TRUE(proto0 == SNAT_PROTOCOL_TCP))
1536 sum0 = tcp0->checksum;
1537 sum0 = ip_csum_update (sum0, old_addr0, new_addr0,
1539 dst_address /* changed member */);
1540 tcp0->checksum = ip_csum_fold(sum0);
1545 snat_hairpinning (sm, b0, ip0, udp0, tcp0, proto0);
1548 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
1549 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1551 snat_in2out_trace_t *t =
1552 vlib_add_trace (vm, node, b0, sizeof (*t));
1553 t->sw_if_index = sw_if_index0;
1554 t->next_index = next0;
1557 pkts_processed += next0 != SNAT_IN2OUT_NEXT_DROP;
1559 /* verify speculative enqueue, maybe switch current next frame */
1560 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1561 to_next, n_left_to_next,
1565 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1568 vlib_node_increment_counter (vm, stats_node_index,
1569 SNAT_IN2OUT_ERROR_IN2OUT_PACKETS,
1571 return frame->n_vectors;
1575 VLIB_REGISTER_NODE (snat_in2out_fast_node) = {
1576 .function = snat_in2out_fast_static_map_fn,
1577 .name = "snat-in2out-fast",
1578 .vector_size = sizeof (u32),
1579 .format_trace = format_snat_in2out_fast_trace,
1580 .type = VLIB_NODE_TYPE_INTERNAL,
1582 .n_errors = ARRAY_LEN(snat_in2out_error_strings),
1583 .error_strings = snat_in2out_error_strings,
1585 .runtime_data_bytes = sizeof (snat_runtime_t),
1587 .n_next_nodes = SNAT_IN2OUT_N_NEXT,
1589 /* edit / add dispositions here */
1591 [SNAT_IN2OUT_NEXT_DROP] = "error-drop",
1592 [SNAT_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1593 [SNAT_IN2OUT_NEXT_SLOW_PATH] = "snat-in2out-slowpath",
1597 VLIB_NODE_FUNCTION_MULTIARCH (snat_in2out_fast_node, snat_in2out_fast_static_map_fn);