2 * Copyright (c) 2017 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.
17 * @brief NAT64 IPv6 to IPv4 translation (inside to outside network)
20 #include <nat/nat64.h>
21 #include <nat/nat_reass.h>
22 #include <nat/nat_inlines.h>
23 #include <vnet/ip/ip6_to_ip4.h>
24 #include <vnet/fib/fib_table.h>
31 } nat64_in2out_trace_t;
34 format_nat64_in2out_trace (u8 * s, va_list * args)
36 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38 nat64_in2out_trace_t *t = va_arg (*args, nat64_in2out_trace_t *);
41 tag = t->is_slow_path ? "NAT64-in2out-slowpath" : "NAT64-in2out";
44 format (s, "%s: sw_if_index %d, next index %d", tag, t->sw_if_index,
55 } nat64_in2out_reass_trace_t;
58 format_nat64_in2out_reass_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 nat64_in2out_reass_trace_t *t =
63 va_arg (*args, nat64_in2out_reass_trace_t *);
66 format (s, "NAT64-in2out-reass: sw_if_index %d, next index %d, status %s",
67 t->sw_if_index, t->next_index,
68 t->cached ? "cached" : "translated");
73 vlib_node_registration_t nat64_in2out_node;
74 vlib_node_registration_t nat64_in2out_slowpath_node;
75 vlib_node_registration_t nat64_in2out_reass_node;
76 vlib_node_registration_t nat64_in2out_handoff_node;
78 #define foreach_nat64_in2out_error \
79 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
80 _(IN2OUT_PACKETS, "good in2out packets processed") \
81 _(NO_TRANSLATION, "no translation") \
82 _(UNKNOWN, "unknown") \
83 _(DROP_FRAGMENT, "Drop fragment") \
84 _(MAX_REASS, "Maximum reassemblies exceeded") \
85 _(MAX_FRAG, "Maximum fragments per reassembly exceeded")
90 #define _(sym,str) NAT64_IN2OUT_ERROR_##sym,
91 foreach_nat64_in2out_error
94 } nat64_in2out_error_t;
96 static char *nat64_in2out_error_strings[] = {
97 #define _(sym,string) string,
98 foreach_nat64_in2out_error
104 NAT64_IN2OUT_NEXT_IP4_LOOKUP,
105 NAT64_IN2OUT_NEXT_IP6_LOOKUP,
106 NAT64_IN2OUT_NEXT_DROP,
107 NAT64_IN2OUT_NEXT_SLOWPATH,
108 NAT64_IN2OUT_NEXT_REASS,
110 } nat64_in2out_next_t;
112 typedef struct nat64_in2out_set_ctx_t_
117 } nat64_in2out_set_ctx_t;
120 * @brief Check whether is a hairpinning.
122 * If the destination IP address of the packet is an IPv4 address assigned to
123 * the NAT64 itself, then the packet is a hairpin packet.
125 * param dst_addr Destination address of the packet.
127 * @returns 1 if hairpinning, otherwise 0.
129 static_always_inline int
130 is_hairpinning (ip6_address_t * dst_addr)
132 nat64_main_t *nm = &nat64_main;
135 for (i = 0; i < vec_len (nm->addr_pool); i++)
137 if (nm->addr_pool[i].addr.as_u32 == dst_addr->as_u32[3])
145 nat64_in2out_tcp_udp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
148 nat64_main_t *nm = &nat64_main;
149 nat64_in2out_set_ctx_t *ctx = arg;
150 nat64_db_bib_entry_t *bibe;
151 nat64_db_st_entry_t *ste;
152 ip46_address_t saddr, daddr;
153 u32 sw_if_index, fib_index;
154 udp_header_t *udp = ip6_next_header (ip6);
155 u8 proto = ip6->protocol;
156 u16 sport = udp->src_port;
157 u16 dport = udp->dst_port;
158 nat64_db_t *db = &nm->db[ctx->thread_index];
160 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
162 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
164 saddr.as_u64[0] = ip6->src_address.as_u64[0];
165 saddr.as_u64[1] = ip6->src_address.as_u64[1];
166 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
167 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
170 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
175 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
181 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
186 ip4_address_t out_addr;
187 if (nat64_alloc_out_addr_and_port
188 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
189 &out_port, ctx->thread_index))
193 nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
194 sport, out_port, fib_index, proto, 0);
199 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
201 nat64_db_st_entry_create (db, bibe, &ip6->dst_address,
207 nat64_session_reset_timeout (ste, ctx->vm);
209 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
210 udp->src_port = bibe->out_port;
212 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
214 if (proto == IP_PROTOCOL_TCP)
218 tcp_header_t *tcp = ip6_next_header (ip6);
220 checksum = &tcp->checksum;
221 csum = ip_csum_sub_even (*checksum, sport);
222 csum = ip_csum_add_even (csum, udp->src_port);
223 *checksum = ip_csum_fold (csum);
230 nat64_in2out_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
232 nat64_main_t *nm = &nat64_main;
233 nat64_in2out_set_ctx_t *ctx = arg;
234 nat64_db_bib_entry_t *bibe;
235 nat64_db_st_entry_t *ste;
236 ip46_address_t saddr, daddr;
237 u32 sw_if_index, fib_index;
238 icmp46_header_t *icmp = ip6_next_header (ip6);
239 nat64_db_t *db = &nm->db[ctx->thread_index];
241 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
243 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
245 saddr.as_u64[0] = ip6->src_address.as_u64[0];
246 saddr.as_u64[1] = ip6->src_address.as_u64[1];
247 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
248 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
250 if (icmp->type == ICMP4_echo_request || icmp->type == ICMP4_echo_reply)
252 u16 in_id = ((u16 *) (icmp))[2];
254 nat64_db_st_entry_find (db, &saddr, &daddr, in_id, 0,
255 IP_PROTOCOL_ICMP, fib_index, 1);
260 nat64_db_bib_entry_by_index (db, IP_PROTOCOL_ICMP,
268 nat64_db_bib_entry_find (db, &saddr, in_id,
269 IP_PROTOCOL_ICMP, fib_index, 1);
274 ip4_address_t out_addr;
275 if (nat64_alloc_out_addr_and_port
276 (fib_index, SNAT_PROTOCOL_ICMP, &out_addr, &out_id,
281 nat64_db_bib_entry_create (db, &ip6->src_address,
282 &out_addr, in_id, out_id,
283 fib_index, IP_PROTOCOL_ICMP, 0);
288 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
290 nat64_db_st_entry_create (db, bibe, &ip6->dst_address,
296 nat64_session_reset_timeout (ste, ctx->vm);
298 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
299 ((u16 *) (icmp))[2] = bibe->out_port;
301 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
305 if (!vec_len (nm->addr_pool))
308 ip4->src_address.as_u32 = nm->addr_pool[0].addr.as_u32;
309 nat64_extract_ip4 (&ip6->dst_address, &ip4->dst_address, fib_index);
316 nat64_in2out_inner_icmp_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
319 nat64_main_t *nm = &nat64_main;
320 nat64_in2out_set_ctx_t *ctx = arg;
321 nat64_db_st_entry_t *ste;
322 nat64_db_bib_entry_t *bibe;
323 ip46_address_t saddr, daddr;
324 u32 sw_if_index, fib_index;
325 u8 proto = ip6->protocol;
326 nat64_db_t *db = &nm->db[ctx->thread_index];
328 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
330 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
332 saddr.as_u64[0] = ip6->src_address.as_u64[0];
333 saddr.as_u64[1] = ip6->src_address.as_u64[1];
334 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
335 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
337 if (proto == IP_PROTOCOL_ICMP6)
339 icmp46_header_t *icmp = ip6_next_header (ip6);
340 u16 in_id = ((u16 *) (icmp))[2];
341 proto = IP_PROTOCOL_ICMP;
344 (icmp->type == ICMP4_echo_request
345 || icmp->type == ICMP4_echo_reply))
349 nat64_db_st_entry_find (db, &daddr, &saddr, in_id, 0, proto,
354 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
358 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
359 ((u16 *) (icmp))[2] = bibe->out_port;
360 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
364 udp_header_t *udp = ip6_next_header (ip6);
365 tcp_header_t *tcp = ip6_next_header (ip6);
369 u16 sport = udp->src_port;
370 u16 dport = udp->dst_port;
373 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
378 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
382 ip4->dst_address.as_u32 = bibe->out_addr.as_u32;
383 udp->dst_port = bibe->out_port;
384 ip4->src_address.as_u32 = ste->out_r_addr.as_u32;
386 if (proto == IP_PROTOCOL_TCP)
387 checksum = &tcp->checksum;
389 checksum = &udp->checksum;
390 csum = ip_csum_sub_even (*checksum, dport);
391 csum = ip_csum_add_even (csum, udp->dst_port);
392 *checksum = ip_csum_fold (csum);
398 typedef struct unk_proto_st_walk_ctx_t_
400 ip6_address_t src_addr;
401 ip6_address_t dst_addr;
402 ip4_address_t out_addr;
406 } unk_proto_st_walk_ctx_t;
409 unk_proto_st_walk (nat64_db_st_entry_t * ste, void *arg)
411 nat64_main_t *nm = &nat64_main;
412 unk_proto_st_walk_ctx_t *ctx = arg;
413 nat64_db_bib_entry_t *bibe;
414 ip46_address_t saddr, daddr;
415 nat64_db_t *db = &nm->db[ctx->thread_index];
417 if (ip46_address_is_equal (&ste->in_r_addr, &ctx->dst_addr))
419 bibe = nat64_db_bib_entry_by_index (db, ste->proto, ste->bibe_index);
423 if (ip46_address_is_equal (&bibe->in_addr, &ctx->src_addr)
424 && bibe->fib_index == ctx->fib_index)
426 memset (&saddr, 0, sizeof (saddr));
427 saddr.ip4.as_u32 = bibe->out_addr.as_u32;
428 memset (&daddr, 0, sizeof (daddr));
429 nat64_extract_ip4 (&ctx->dst_addr, &daddr.ip4, ctx->fib_index);
431 if (nat64_db_st_entry_find
432 (db, &daddr, &saddr, 0, 0, ctx->proto, ctx->fib_index, 0))
435 ctx->out_addr.as_u32 = bibe->out_addr.as_u32;
444 nat64_in2out_unk_proto_set_cb (ip6_header_t * ip6, ip4_header_t * ip4,
447 nat64_main_t *nm = &nat64_main;
448 nat64_in2out_set_ctx_t *s_ctx = arg;
449 nat64_db_bib_entry_t *bibe;
450 nat64_db_st_entry_t *ste;
451 ip46_address_t saddr, daddr, addr;
452 u32 sw_if_index, fib_index;
453 u8 proto = ip6->protocol;
455 nat64_db_t *db = &nm->db[s_ctx->thread_index];
457 sw_if_index = vnet_buffer (s_ctx->b)->sw_if_index[VLIB_RX];
459 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
461 saddr.as_u64[0] = ip6->src_address.as_u64[0];
462 saddr.as_u64[1] = ip6->src_address.as_u64[1];
463 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
464 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
467 nat64_db_st_entry_find (db, &saddr, &daddr, 0, 0, proto, fib_index, 1);
471 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
477 bibe = nat64_db_bib_entry_find (db, &saddr, 0, proto, fib_index, 1);
481 /* Choose same out address as for TCP/UDP session to same dst */
482 unk_proto_st_walk_ctx_t ctx = {
483 .src_addr.as_u64[0] = ip6->src_address.as_u64[0],
484 .src_addr.as_u64[1] = ip6->src_address.as_u64[1],
485 .dst_addr.as_u64[0] = ip6->dst_address.as_u64[0],
486 .dst_addr.as_u64[1] = ip6->dst_address.as_u64[1],
487 .out_addr.as_u32 = 0,
488 .fib_index = fib_index,
490 .thread_index = s_ctx->thread_index,
493 nat64_db_st_walk (db, IP_PROTOCOL_TCP, unk_proto_st_walk, &ctx);
495 if (!ctx.out_addr.as_u32)
496 nat64_db_st_walk (db, IP_PROTOCOL_UDP, unk_proto_st_walk, &ctx);
498 /* Verify if out address is not already in use for protocol */
499 memset (&addr, 0, sizeof (addr));
500 addr.ip4.as_u32 = ctx.out_addr.as_u32;
501 if (nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
502 ctx.out_addr.as_u32 = 0;
504 if (!ctx.out_addr.as_u32)
506 for (i = 0; i < vec_len (nm->addr_pool); i++)
508 addr.ip4.as_u32 = nm->addr_pool[i].addr.as_u32;
509 if (!nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
514 if (!ctx.out_addr.as_u32)
518 nat64_db_bib_entry_create (db, &ip6->src_address,
519 &ctx.out_addr, 0, 0, fib_index, proto,
525 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
527 nat64_db_st_entry_create (db, bibe, &ip6->dst_address, &daddr.ip4, 0);
532 nat64_session_reset_timeout (ste, s_ctx->vm);
534 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
535 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
543 nat64_in2out_tcp_udp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
544 ip6_header_t * ip6, u32 thread_index)
546 nat64_main_t *nm = &nat64_main;
547 nat64_db_bib_entry_t *bibe;
548 nat64_db_st_entry_t *ste;
549 ip46_address_t saddr, daddr;
550 u32 sw_if_index, fib_index;
551 udp_header_t *udp = ip6_next_header (ip6);
552 tcp_header_t *tcp = ip6_next_header (ip6);
553 u8 proto = ip6->protocol;
554 u16 sport = udp->src_port;
555 u16 dport = udp->dst_port;
558 nat64_db_t *db = &nm->db[thread_index];
560 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
562 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
564 saddr.as_u64[0] = ip6->src_address.as_u64[0];
565 saddr.as_u64[1] = ip6->src_address.as_u64[1];
566 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
567 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
569 if (proto == IP_PROTOCOL_UDP)
570 checksum = &udp->checksum;
572 checksum = &tcp->checksum;
574 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
575 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
576 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
577 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
578 csum = ip_csum_sub_even (csum, sport);
579 csum = ip_csum_sub_even (csum, dport);
582 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
587 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
593 bibe = nat64_db_bib_entry_find (db, &saddr, sport, proto, fib_index, 1);
598 ip4_address_t out_addr;
599 if (nat64_alloc_out_addr_and_port
600 (fib_index, ip_proto_to_snat_proto (proto), &out_addr,
601 &out_port, thread_index))
605 nat64_db_bib_entry_create (db, &ip6->src_address, &out_addr,
606 sport, out_port, fib_index, proto, 0);
611 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
613 nat64_db_st_entry_create (db, bibe, &ip6->dst_address,
619 nat64_session_reset_timeout (ste, vm);
621 sport = udp->src_port = bibe->out_port;
622 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, fib_index);
624 memset (&daddr, 0, sizeof (daddr));
625 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
629 vec_foreach (db, nm->db)
631 bibe = nat64_db_bib_entry_find (db, &daddr, dport, proto, 0, 0);
641 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
642 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
643 udp->dst_port = bibe->in_port;
645 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
646 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
647 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
648 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
649 csum = ip_csum_add_even (csum, udp->src_port);
650 csum = ip_csum_add_even (csum, udp->dst_port);
651 *checksum = ip_csum_fold (csum);
657 nat64_in2out_icmp_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
658 ip6_header_t * ip6, u32 thread_index)
660 nat64_main_t *nm = &nat64_main;
661 nat64_db_bib_entry_t *bibe;
662 nat64_db_st_entry_t *ste;
663 icmp46_header_t *icmp = ip6_next_header (ip6);
664 ip6_header_t *inner_ip6;
665 ip46_address_t saddr, daddr;
666 u32 sw_if_index, fib_index;
670 u16 *checksum, sport, dport;
672 nat64_db_t *db = &nm->db[thread_index];
674 if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
677 inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
679 proto = inner_ip6->protocol;
681 if (proto == IP_PROTOCOL_ICMP6)
684 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
686 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
688 saddr.as_u64[0] = inner_ip6->src_address.as_u64[0];
689 saddr.as_u64[1] = inner_ip6->src_address.as_u64[1];
690 daddr.as_u64[0] = inner_ip6->dst_address.as_u64[0];
691 daddr.as_u64[1] = inner_ip6->dst_address.as_u64[1];
693 udp = ip6_next_header (inner_ip6);
694 tcp = ip6_next_header (inner_ip6);
696 sport = udp->src_port;
697 dport = udp->dst_port;
699 if (proto == IP_PROTOCOL_UDP)
700 checksum = &udp->checksum;
702 checksum = &tcp->checksum;
704 csum = ip_csum_sub_even (*checksum, inner_ip6->src_address.as_u64[0]);
705 csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[1]);
706 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[0]);
707 csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[1]);
708 csum = ip_csum_sub_even (csum, sport);
709 csum = ip_csum_sub_even (csum, dport);
712 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
717 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
721 dport = udp->dst_port = bibe->out_port;
722 nat64_compose_ip6 (&inner_ip6->dst_address, &bibe->out_addr, fib_index);
724 memset (&saddr, 0, sizeof (saddr));
725 memset (&daddr, 0, sizeof (daddr));
726 saddr.ip4.as_u32 = ste->out_r_addr.as_u32;
727 daddr.ip4.as_u32 = bibe->out_addr.as_u32;
731 vec_foreach (db, nm->db)
733 ste = nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
744 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
748 inner_ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
749 inner_ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
750 udp->src_port = bibe->in_port;
752 csum = ip_csum_add_even (csum, inner_ip6->src_address.as_u64[0]);
753 csum = ip_csum_add_even (csum, inner_ip6->src_address.as_u64[1]);
754 csum = ip_csum_add_even (csum, inner_ip6->dst_address.as_u64[0]);
755 csum = ip_csum_add_even (csum, inner_ip6->dst_address.as_u64[1]);
756 csum = ip_csum_add_even (csum, udp->src_port);
757 csum = ip_csum_add_even (csum, udp->dst_port);
758 *checksum = ip_csum_fold (csum);
760 if (!vec_len (nm->addr_pool))
763 nat64_compose_ip6 (&ip6->src_address, &nm->addr_pool[0].addr, fib_index);
764 ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
765 ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
768 csum = ip_csum_with_carry (0, ip6->payload_length);
769 csum = ip_csum_with_carry (csum, clib_host_to_net_u16 (ip6->protocol));
770 csum = ip_csum_with_carry (csum, ip6->src_address.as_u64[0]);
771 csum = ip_csum_with_carry (csum, ip6->src_address.as_u64[1]);
772 csum = ip_csum_with_carry (csum, ip6->dst_address.as_u64[0]);
773 csum = ip_csum_with_carry (csum, ip6->dst_address.as_u64[1]);
775 ip_incremental_checksum (csum, icmp,
776 clib_net_to_host_u16 (ip6->payload_length));
777 icmp->checksum = ~ip_csum_fold (csum);
783 nat64_in2out_unk_proto_hairpinning (vlib_main_t * vm, vlib_buffer_t * b,
784 ip6_header_t * ip6, u32 thread_index)
786 nat64_main_t *nm = &nat64_main;
787 nat64_db_bib_entry_t *bibe;
788 nat64_db_st_entry_t *ste;
789 ip46_address_t saddr, daddr, addr;
790 u32 sw_if_index, fib_index;
791 u8 proto = ip6->protocol;
793 nat64_db_t *db = &nm->db[thread_index];
795 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
797 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
799 saddr.as_u64[0] = ip6->src_address.as_u64[0];
800 saddr.as_u64[1] = ip6->src_address.as_u64[1];
801 daddr.as_u64[0] = ip6->dst_address.as_u64[0];
802 daddr.as_u64[1] = ip6->dst_address.as_u64[1];
805 nat64_db_st_entry_find (db, &saddr, &daddr, 0, 0, proto, fib_index, 1);
809 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
815 bibe = nat64_db_bib_entry_find (db, &saddr, 0, proto, fib_index, 1);
819 /* Choose same out address as for TCP/UDP session to same dst */
820 unk_proto_st_walk_ctx_t ctx = {
821 .src_addr.as_u64[0] = ip6->src_address.as_u64[0],
822 .src_addr.as_u64[1] = ip6->src_address.as_u64[1],
823 .dst_addr.as_u64[0] = ip6->dst_address.as_u64[0],
824 .dst_addr.as_u64[1] = ip6->dst_address.as_u64[1],
825 .out_addr.as_u32 = 0,
826 .fib_index = fib_index,
828 .thread_index = thread_index,
831 nat64_db_st_walk (db, IP_PROTOCOL_TCP, unk_proto_st_walk, &ctx);
833 if (!ctx.out_addr.as_u32)
834 nat64_db_st_walk (db, IP_PROTOCOL_UDP, unk_proto_st_walk, &ctx);
836 /* Verify if out address is not already in use for protocol */
837 memset (&addr, 0, sizeof (addr));
838 addr.ip4.as_u32 = ctx.out_addr.as_u32;
839 if (nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
840 ctx.out_addr.as_u32 = 0;
842 if (!ctx.out_addr.as_u32)
844 for (i = 0; i < vec_len (nm->addr_pool); i++)
846 addr.ip4.as_u32 = nm->addr_pool[i].addr.as_u32;
847 if (!nat64_db_bib_entry_find (db, &addr, 0, proto, 0, 0))
852 if (!ctx.out_addr.as_u32)
856 nat64_db_bib_entry_create (db, &ip6->src_address,
857 &ctx.out_addr, 0, 0, fib_index, proto,
863 nat64_extract_ip4 (&ip6->dst_address, &daddr.ip4, fib_index);
865 nat64_db_st_entry_create (db, bibe, &ip6->dst_address, &daddr.ip4, 0);
870 nat64_session_reset_timeout (ste, vm);
872 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, fib_index);
874 memset (&daddr, 0, sizeof (daddr));
875 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
879 vec_foreach (db, nm->db)
881 bibe = nat64_db_bib_entry_find (db, &daddr, 0, proto, 0, 0);
891 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
892 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
898 nat64_in2out_node_fn_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
899 vlib_frame_t * frame, u8 is_slow_path)
901 u32 n_left_from, *from, *to_next;
902 nat64_in2out_next_t next_index;
903 u32 pkts_processed = 0;
904 u32 stats_node_index;
905 u32 thread_index = vlib_get_thread_index ();
908 is_slow_path ? nat64_in2out_slowpath_node.index : nat64_in2out_node.index;
910 from = vlib_frame_vector_args (frame);
911 n_left_from = frame->n_vectors;
912 next_index = node->cached_next_index;
914 while (n_left_from > 0)
918 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
920 while (n_left_from > 0 && n_left_to_next > 0)
926 u16 l4_offset0, frag_offset0;
929 nat64_in2out_set_ctx_t ctx0;
931 /* speculatively enqueue b0 to the current next frame */
939 b0 = vlib_get_buffer (vm, bi0);
940 ip60 = vlib_buffer_get_current (b0);
944 ctx0.thread_index = thread_index;
946 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
950 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
953 next0 = NAT64_IN2OUT_NEXT_DROP;
954 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
958 proto0 = ip_proto_to_snat_proto (l4_protocol0);
962 if (PREDICT_TRUE (proto0 == ~0))
964 if (is_hairpinning (&ip60->dst_address))
966 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
967 if (nat64_in2out_unk_proto_hairpinning
968 (vm, b0, ip60, thread_index))
970 next0 = NAT64_IN2OUT_NEXT_DROP;
972 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
977 if (ip6_to_ip4 (b0, nat64_in2out_unk_proto_set_cb, &ctx0))
979 next0 = NAT64_IN2OUT_NEXT_DROP;
981 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
989 if (PREDICT_FALSE (proto0 == ~0))
991 next0 = NAT64_IN2OUT_NEXT_SLOWPATH;
997 (ip60->protocol == IP_PROTOCOL_IPV6_FRAGMENTATION))
999 next0 = NAT64_IN2OUT_NEXT_REASS;
1003 if (proto0 == SNAT_PROTOCOL_ICMP)
1005 if (is_hairpinning (&ip60->dst_address))
1007 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1008 if (nat64_in2out_icmp_hairpinning
1009 (vm, b0, ip60, thread_index))
1011 next0 = NAT64_IN2OUT_NEXT_DROP;
1013 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1019 (b0, nat64_in2out_icmp_set_cb, &ctx0,
1020 nat64_in2out_inner_icmp_set_cb, &ctx0))
1022 next0 = NAT64_IN2OUT_NEXT_DROP;
1023 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1027 else if (proto0 == SNAT_PROTOCOL_TCP || proto0 == SNAT_PROTOCOL_UDP)
1029 if (is_hairpinning (&ip60->dst_address))
1031 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1032 if (nat64_in2out_tcp_udp_hairpinning
1033 (vm, b0, ip60, thread_index))
1035 next0 = NAT64_IN2OUT_NEXT_DROP;
1037 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1042 if (ip6_to_ip4_tcp_udp
1043 (b0, nat64_in2out_tcp_udp_set_cb, &ctx0, 0))
1045 next0 = NAT64_IN2OUT_NEXT_DROP;
1046 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1052 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1053 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1055 nat64_in2out_trace_t *t =
1056 vlib_add_trace (vm, node, b0, sizeof (*t));
1057 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1058 t->next_index = next0;
1059 t->is_slow_path = is_slow_path;
1062 pkts_processed += next0 != NAT64_IN2OUT_NEXT_DROP;
1064 /* verify speculative enqueue, maybe switch current next frame */
1065 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
1066 n_left_to_next, bi0, next0);
1068 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1070 vlib_node_increment_counter (vm, stats_node_index,
1071 NAT64_IN2OUT_ERROR_IN2OUT_PACKETS,
1073 return frame->n_vectors;
1077 nat64_in2out_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1078 vlib_frame_t * frame)
1080 return nat64_in2out_node_fn_inline (vm, node, frame, 0);
1084 VLIB_REGISTER_NODE (nat64_in2out_node) = {
1085 .function = nat64_in2out_node_fn,
1086 .name = "nat64-in2out",
1087 .vector_size = sizeof (u32),
1088 .format_trace = format_nat64_in2out_trace,
1089 .type = VLIB_NODE_TYPE_INTERNAL,
1090 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1091 .error_strings = nat64_in2out_error_strings,
1092 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1093 /* edit / add dispositions here */
1095 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1096 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1097 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1098 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1099 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1104 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_node, nat64_in2out_node_fn);
1107 nat64_in2out_slowpath_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1108 vlib_frame_t * frame)
1110 return nat64_in2out_node_fn_inline (vm, node, frame, 1);
1114 VLIB_REGISTER_NODE (nat64_in2out_slowpath_node) = {
1115 .function = nat64_in2out_slowpath_node_fn,
1116 .name = "nat64-in2out-slowpath",
1117 .vector_size = sizeof (u32),
1118 .format_trace = format_nat64_in2out_trace,
1119 .type = VLIB_NODE_TYPE_INTERNAL,
1120 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1121 .error_strings = nat64_in2out_error_strings,
1122 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1123 /* edit / add dispositions here */
1125 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1126 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1127 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1128 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1129 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1134 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_slowpath_node,
1135 nat64_in2out_slowpath_node_fn);
1137 typedef struct nat64_in2out_frag_set_ctx_t_
1145 } nat64_in2out_frag_set_ctx_t;
1148 nat64_in2out_frag_set_cb (ip6_header_t * ip6, ip4_header_t * ip4, void *arg)
1150 nat64_main_t *nm = &nat64_main;
1151 nat64_in2out_frag_set_ctx_t *ctx = arg;
1152 nat64_db_st_entry_t *ste;
1153 nat64_db_bib_entry_t *bibe;
1155 nat64_db_t *db = &nm->db[ctx->thread_index];
1157 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1161 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1165 nat64_session_reset_timeout (ste, ctx->vm);
1167 if (ctx->first_frag)
1169 udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1171 if (ctx->proto == IP_PROTOCOL_TCP)
1175 tcp_header_t *tcp = (tcp_header_t *) udp;
1177 checksum = &tcp->checksum;
1178 csum = ip_csum_sub_even (*checksum, tcp->src_port);
1179 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[0]);
1180 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1181 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1182 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1183 csum = ip_csum_add_even (csum, bibe->out_port);
1184 csum = ip_csum_add_even (csum, bibe->out_addr.as_u32);
1185 csum = ip_csum_add_even (csum, ste->out_r_addr.as_u32);
1186 *checksum = ip_csum_fold (csum);
1189 udp->src_port = bibe->out_port;
1192 ip4->src_address.as_u32 = bibe->out_addr.as_u32;
1193 ip4->dst_address.as_u32 = ste->out_r_addr.as_u32;
1199 nat64_in2out_frag_hairpinning (vlib_buffer_t * b, ip6_header_t * ip6,
1200 nat64_in2out_frag_set_ctx_t * ctx)
1202 nat64_main_t *nm = &nat64_main;
1203 nat64_db_st_entry_t *ste;
1204 nat64_db_bib_entry_t *bibe;
1205 udp_header_t *udp = (udp_header_t *) u8_ptr_add (ip6, ctx->l4_offset);
1206 tcp_header_t *tcp = (tcp_header_t *) udp;
1207 u16 sport = udp->src_port;
1208 u16 dport = udp->dst_port;
1211 ip46_address_t daddr;
1212 nat64_db_t *db = &nm->db[ctx->thread_index];
1214 if (ctx->first_frag)
1216 if (ctx->proto == IP_PROTOCOL_UDP)
1217 checksum = &udp->checksum;
1219 checksum = &tcp->checksum;
1221 csum = ip_csum_sub_even (*checksum, ip6->src_address.as_u64[0]);
1222 csum = ip_csum_sub_even (csum, ip6->src_address.as_u64[1]);
1223 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[0]);
1224 csum = ip_csum_sub_even (csum, ip6->dst_address.as_u64[1]);
1225 csum = ip_csum_sub_even (csum, sport);
1226 csum = ip_csum_sub_even (csum, dport);
1229 ste = nat64_db_st_entry_by_index (db, ctx->proto, ctx->sess_index);
1233 bibe = nat64_db_bib_entry_by_index (db, ctx->proto, ste->bibe_index);
1237 nat64_session_reset_timeout (ste, ctx->vm);
1239 sport = bibe->out_port;
1240 dport = ste->r_port;
1242 nat64_compose_ip6 (&ip6->src_address, &bibe->out_addr, bibe->fib_index);
1244 memset (&daddr, 0, sizeof (daddr));
1245 daddr.ip4.as_u32 = ste->out_r_addr.as_u32;
1249 vec_foreach (db, nm->db)
1251 bibe = nat64_db_bib_entry_find (db, &daddr, dport, ctx->proto, 0, 0);
1261 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
1262 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
1264 if (ctx->first_frag)
1266 udp->dst_port = bibe->in_port;
1267 udp->src_port = sport;
1268 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
1269 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
1270 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
1271 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
1272 csum = ip_csum_add_even (csum, udp->src_port);
1273 csum = ip_csum_add_even (csum, udp->dst_port);
1274 *checksum = ip_csum_fold (csum);
1281 nat64_in2out_reass_node_fn (vlib_main_t * vm,
1282 vlib_node_runtime_t * node, vlib_frame_t * frame)
1284 u32 n_left_from, *from, *to_next;
1285 nat64_in2out_next_t next_index;
1286 u32 pkts_processed = 0;
1287 u32 *fragments_to_drop = 0;
1288 u32 *fragments_to_loopback = 0;
1289 nat64_main_t *nm = &nat64_main;
1290 u32 thread_index = vlib_get_thread_index ();
1292 from = vlib_frame_vector_args (frame);
1293 n_left_from = frame->n_vectors;
1294 next_index = node->cached_next_index;
1296 while (n_left_from > 0)
1300 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1302 while (n_left_from > 0 && n_left_to_next > 0)
1309 u16 l4_offset0, frag_offset0;
1311 nat_reass_ip6_t *reass0;
1312 ip6_frag_hdr_t *frag0;
1313 nat64_db_bib_entry_t *bibe0;
1314 nat64_db_st_entry_t *ste0;
1316 snat_protocol_t proto0;
1317 u32 sw_if_index0, fib_index0;
1318 ip46_address_t saddr0, daddr0;
1319 nat64_in2out_frag_set_ctx_t ctx0;
1320 nat64_db_t *db = &nm->db[thread_index];
1322 /* speculatively enqueue b0 to the current next frame */
1328 n_left_to_next -= 1;
1330 b0 = vlib_get_buffer (vm, bi0);
1331 next0 = NAT64_IN2OUT_NEXT_IP4_LOOKUP;
1333 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
1335 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6,
1338 ctx0.thread_index = thread_index;
1340 if (PREDICT_FALSE (nat_reass_is_drop_frag (1)))
1342 next0 = NAT64_IN2OUT_NEXT_DROP;
1343 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1347 ip60 = (ip6_header_t *) vlib_buffer_get_current (b0);
1351 (ip60, b0->current_length, &l4_protocol0, &l4_offset0,
1354 next0 = NAT64_IN2OUT_NEXT_DROP;
1355 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1360 (!(l4_protocol0 == IP_PROTOCOL_TCP
1361 || l4_protocol0 == IP_PROTOCOL_UDP)))
1363 next0 = NAT64_IN2OUT_NEXT_DROP;
1364 b0->error = node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT];
1368 udp0 = (udp_header_t *) u8_ptr_add (ip60, l4_offset0);
1369 frag0 = (ip6_frag_hdr_t *) u8_ptr_add (ip60, frag_offset0);
1370 proto0 = ip_proto_to_snat_proto (l4_protocol0);
1372 reass0 = nat_ip6_reass_find_or_create (ip60->src_address,
1374 frag0->identification,
1376 1, &fragments_to_drop);
1378 if (PREDICT_FALSE (!reass0))
1380 next0 = NAT64_IN2OUT_NEXT_DROP;
1381 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_REASS];
1385 if (PREDICT_TRUE (ip6_frag_hdr_offset (frag0)))
1387 ctx0.first_frag = 0;
1388 if (PREDICT_FALSE (reass0->sess_index == (u32) ~ 0))
1390 if (nat_ip6_reass_add_fragment (reass0, bi0))
1392 b0->error = node->errors[NAT64_IN2OUT_ERROR_MAX_FRAG];
1393 next0 = NAT64_IN2OUT_NEXT_DROP;
1402 ctx0.first_frag = 1;
1404 saddr0.as_u64[0] = ip60->src_address.as_u64[0];
1405 saddr0.as_u64[1] = ip60->src_address.as_u64[1];
1406 daddr0.as_u64[0] = ip60->dst_address.as_u64[0];
1407 daddr0.as_u64[1] = ip60->dst_address.as_u64[1];
1410 nat64_db_st_entry_find (db, &saddr0, &daddr0,
1411 udp0->src_port, udp0->dst_port,
1412 l4_protocol0, fib_index0, 1);
1416 nat64_db_bib_entry_find (db, &saddr0, udp0->src_port,
1417 l4_protocol0, fib_index0, 1);
1421 ip4_address_t out_addr0;
1422 if (nat64_alloc_out_addr_and_port
1423 (fib_index0, proto0, &out_addr0, &out_port0,
1426 next0 = NAT64_IN2OUT_NEXT_DROP;
1428 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1433 nat64_db_bib_entry_create (db,
1435 &out_addr0, udp0->src_port,
1436 out_port0, fib_index0,
1440 next0 = NAT64_IN2OUT_NEXT_DROP;
1442 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1446 nat64_extract_ip4 (&ip60->dst_address, &daddr0.ip4,
1449 nat64_db_st_entry_create (db, bibe0,
1450 &ip60->dst_address, &daddr0.ip4,
1454 next0 = NAT64_IN2OUT_NEXT_DROP;
1456 node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1460 reass0->sess_index = nat64_db_st_entry_get_index (db, ste0);
1462 nat_ip6_reass_get_frags (reass0, &fragments_to_loopback);
1465 ctx0.sess_index = reass0->sess_index;
1466 ctx0.proto = l4_protocol0;
1468 ctx0.l4_offset = l4_offset0;
1470 if (PREDICT_FALSE (is_hairpinning (&ip60->dst_address)))
1472 next0 = NAT64_IN2OUT_NEXT_IP6_LOOKUP;
1473 if (nat64_in2out_frag_hairpinning (b0, ip60, &ctx0))
1475 next0 = NAT64_IN2OUT_NEXT_DROP;
1476 b0->error = node->errors[NAT64_IN2OUT_ERROR_NO_TRANSLATION];
1482 if (ip6_to_ip4_fragmented (b0, nat64_in2out_frag_set_cb, &ctx0))
1484 next0 = NAT64_IN2OUT_NEXT_DROP;
1485 b0->error = node->errors[NAT64_IN2OUT_ERROR_UNKNOWN];
1492 ((node->flags & VLIB_NODE_FLAG_TRACE)
1493 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1495 nat64_in2out_reass_trace_t *t =
1496 vlib_add_trace (vm, node, b0, sizeof (*t));
1497 t->cached = cached0;
1498 t->sw_if_index = sw_if_index0;
1499 t->next_index = next0;
1509 pkts_processed += next0 != NAT64_IN2OUT_NEXT_DROP;
1511 /* verify speculative enqueue, maybe switch current next frame */
1512 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1513 to_next, n_left_to_next,
1517 if (n_left_from == 0 && vec_len (fragments_to_loopback))
1519 from = vlib_frame_vector_args (frame);
1520 u32 len = vec_len (fragments_to_loopback);
1521 if (len <= VLIB_FRAME_SIZE)
1523 clib_memcpy (from, fragments_to_loopback,
1524 sizeof (u32) * len);
1526 vec_reset_length (fragments_to_loopback);
1531 fragments_to_loopback + (len -
1533 sizeof (u32) * VLIB_FRAME_SIZE);
1534 n_left_from = VLIB_FRAME_SIZE;
1535 _vec_len (fragments_to_loopback) = len - VLIB_FRAME_SIZE;
1540 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1543 vlib_node_increment_counter (vm, nat64_in2out_reass_node.index,
1544 NAT64_IN2OUT_ERROR_IN2OUT_PACKETS,
1547 nat_send_all_to_node (vm, fragments_to_drop, node,
1548 &node->errors[NAT64_IN2OUT_ERROR_DROP_FRAGMENT],
1549 NAT64_IN2OUT_NEXT_DROP);
1551 vec_free (fragments_to_drop);
1552 vec_free (fragments_to_loopback);
1553 return frame->n_vectors;
1557 VLIB_REGISTER_NODE (nat64_in2out_reass_node) = {
1558 .function = nat64_in2out_reass_node_fn,
1559 .name = "nat64-in2out-reass",
1560 .vector_size = sizeof (u32),
1561 .format_trace = format_nat64_in2out_reass_trace,
1562 .type = VLIB_NODE_TYPE_INTERNAL,
1563 .n_errors = ARRAY_LEN (nat64_in2out_error_strings),
1564 .error_strings = nat64_in2out_error_strings,
1565 .n_next_nodes = NAT64_IN2OUT_N_NEXT,
1566 /* edit / add dispositions here */
1568 [NAT64_IN2OUT_NEXT_DROP] = "error-drop",
1569 [NAT64_IN2OUT_NEXT_IP4_LOOKUP] = "ip4-lookup",
1570 [NAT64_IN2OUT_NEXT_IP6_LOOKUP] = "ip6-lookup",
1571 [NAT64_IN2OUT_NEXT_SLOWPATH] = "nat64-in2out-slowpath",
1572 [NAT64_IN2OUT_NEXT_REASS] = "nat64-in2out-reass",
1577 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_reass_node,
1578 nat64_in2out_reass_node_fn);
1582 u32 next_worker_index;
1584 } nat64_in2out_handoff_trace_t;
1587 format_nat64_in2out_handoff_trace (u8 * s, va_list * args)
1589 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1590 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1591 nat64_in2out_handoff_trace_t *t =
1592 va_arg (*args, nat64_in2out_handoff_trace_t *);
1595 m = t->do_handoff ? "next worker" : "same worker";
1596 s = format (s, "NAT64-IN2OUT-HANDOFF: %s %d", m, t->next_worker_index);
1602 nat64_in2out_handoff_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1603 vlib_frame_t * frame)
1605 nat64_main_t *nm = &nat64_main;
1606 vlib_thread_main_t *tm = vlib_get_thread_main ();
1607 u32 n_left_from, *from, *to_next = 0, *to_next_drop = 0;
1608 static __thread vlib_frame_queue_elt_t **handoff_queue_elt_by_worker_index;
1609 static __thread vlib_frame_queue_t **congested_handoff_queue_by_worker_index
1611 vlib_frame_queue_elt_t *hf = 0;
1612 vlib_frame_queue_t *fq;
1613 vlib_frame_t *f = 0, *d = 0;
1615 u32 n_left_to_next_worker = 0, *to_next_worker = 0;
1616 u32 next_worker_index = 0;
1617 u32 current_worker_index = ~0;
1618 u32 thread_index = vlib_get_thread_index ();
1622 fq_index = nm->fq_in2out_index;
1623 to_node_index = nat64_in2out_node.index;
1625 if (PREDICT_FALSE (handoff_queue_elt_by_worker_index == 0))
1627 vec_validate (handoff_queue_elt_by_worker_index, tm->n_vlib_mains - 1);
1629 vec_validate_init_empty (congested_handoff_queue_by_worker_index,
1630 tm->n_vlib_mains - 1,
1631 (vlib_frame_queue_t *) (~0));
1634 from = vlib_frame_vector_args (frame);
1635 n_left_from = frame->n_vectors;
1637 while (n_left_from > 0)
1648 b0 = vlib_get_buffer (vm, bi0);
1650 ip0 = vlib_buffer_get_current (b0);
1652 next_worker_index = nat64_get_worker_in2out (&ip0->src_address);
1654 if (PREDICT_FALSE (next_worker_index != thread_index))
1658 if (next_worker_index != current_worker_index)
1661 is_vlib_frame_queue_congested (fq_index, next_worker_index,
1663 congested_handoff_queue_by_worker_index);
1667 /* if this is 1st frame */
1670 d = vlib_get_frame_to_node (vm, nm->error_node_index);
1671 to_next_drop = vlib_frame_vector_args (d);
1674 to_next_drop[0] = bi0;
1681 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1684 vlib_get_worker_handoff_queue_elt (fq_index,
1686 handoff_queue_elt_by_worker_index);
1687 n_left_to_next_worker = VLIB_FRAME_SIZE - hf->n_vectors;
1688 to_next_worker = &hf->buffer_index[hf->n_vectors];
1689 current_worker_index = next_worker_index;
1692 ASSERT (to_next_worker != 0);
1694 /* enqueue to correct worker thread */
1695 to_next_worker[0] = bi0;
1697 n_left_to_next_worker--;
1699 if (n_left_to_next_worker == 0)
1701 hf->n_vectors = VLIB_FRAME_SIZE;
1702 vlib_put_frame_queue_elt (hf);
1703 current_worker_index = ~0;
1704 handoff_queue_elt_by_worker_index[next_worker_index] = 0;
1711 /* if this is 1st frame */
1714 f = vlib_get_frame_to_node (vm, to_node_index);
1715 to_next = vlib_frame_vector_args (f);
1725 ((node->flags & VLIB_NODE_FLAG_TRACE)
1726 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1728 nat64_in2out_handoff_trace_t *t =
1729 vlib_add_trace (vm, node, b0, sizeof (*t));
1730 t->next_worker_index = next_worker_index;
1731 t->do_handoff = do_handoff;
1736 vlib_put_frame_to_node (vm, to_node_index, f);
1739 vlib_put_frame_to_node (vm, nm->error_node_index, d);
1742 hf->n_vectors = VLIB_FRAME_SIZE - n_left_to_next_worker;
1744 /* Ship frames to the worker nodes */
1745 for (i = 0; i < vec_len (handoff_queue_elt_by_worker_index); i++)
1747 if (handoff_queue_elt_by_worker_index[i])
1749 hf = handoff_queue_elt_by_worker_index[i];
1751 * It works better to let the handoff node
1752 * rate-adapt, always ship the handoff queue element.
1754 if (1 || hf->n_vectors == hf->last_n_vectors)
1756 vlib_put_frame_queue_elt (hf);
1757 handoff_queue_elt_by_worker_index[i] = 0;
1760 hf->last_n_vectors = hf->n_vectors;
1762 congested_handoff_queue_by_worker_index[i] =
1763 (vlib_frame_queue_t *) (~0);
1766 current_worker_index = ~0;
1767 return frame->n_vectors;
1771 VLIB_REGISTER_NODE (nat64_in2out_handoff_node) = {
1772 .function = nat64_in2out_handoff_node_fn,
1773 .name = "nat64-in2out-handoff",
1774 .vector_size = sizeof (u32),
1775 .format_trace = format_nat64_in2out_handoff_trace,
1776 .type = VLIB_NODE_TYPE_INTERNAL,
1786 VLIB_NODE_FUNCTION_MULTIARCH (nat64_in2out_handoff_node,
1787 nat64_in2out_handoff_node_fn);
1790 * fd.io coding-style-patch-verification: ON
1793 * eval: (c-set-style "gnu")