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 IPv4 to IPv6 translation (otside to inside network)
20 #include <nat/nat64.h>
21 #include <nat/nat_inlines.h>
22 #include <vnet/ip/ip4_to_ip6.h>
23 #include <vnet/fib/ip4_fib.h>
24 #include <vnet/udp/udp.h>
30 } nat64_out2in_trace_t;
33 format_nat64_out2in_trace (u8 * s, va_list * args)
35 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37 nat64_out2in_trace_t *t = va_arg (*args, nat64_out2in_trace_t *);
40 format (s, "NAT64-out2in: sw_if_index %d, next index %d", t->sw_if_index,
46 #define foreach_nat64_out2in_error \
47 _(UNSUPPORTED_PROTOCOL, "unsupported protocol") \
48 _(OUT2IN_PACKETS, "good out2in packets processed") \
49 _(NO_TRANSLATION, "no translation") \
50 _(UNKNOWN, "unknown") \
51 _(DROP_FRAGMENT, "drop fragment") \
52 _(TCP_PACKETS, "TCP packets") \
53 _(UDP_PACKETS, "UDP packets") \
54 _(ICMP_PACKETS, "ICMP packets") \
55 _(OTHER_PACKETS, "other protocol packets") \
56 _(FRAGMENTS, "fragments") \
57 _(CACHED_FRAGMENTS, "cached fragments") \
58 _(PROCESSED_FRAGMENTS, "processed fragments")
63 #define _(sym,str) NAT64_OUT2IN_ERROR_##sym,
64 foreach_nat64_out2in_error
67 } nat64_out2in_error_t;
69 static char *nat64_out2in_error_strings[] = {
70 #define _(sym,string) string,
71 foreach_nat64_out2in_error
77 NAT64_OUT2IN_NEXT_IP6_LOOKUP,
78 NAT64_OUT2IN_NEXT_IP4_LOOKUP,
79 NAT64_OUT2IN_NEXT_DROP,
81 } nat64_out2in_next_t;
83 typedef struct nat64_out2in_set_ctx_t_
88 } nat64_out2in_set_ctx_t;
91 nat64_out2in_tcp_udp (vlib_main_t * vm, vlib_buffer_t * b,
92 nat64_out2in_set_ctx_t * ctx)
100 ip4_address_t old_src, old_dst;
102 nat64_main_t *nm = &nat64_main;
103 nat64_db_bib_entry_t *bibe;
104 nat64_db_st_entry_t *ste;
105 ip46_address_t saddr;
106 ip46_address_t daddr;
107 ip6_address_t ip6_saddr;
108 u8 proto = vnet_buffer (b)->ip.reass.ip_proto;
109 u16 dport = vnet_buffer (b)->ip.reass.l4_dst_port;
110 u16 sport = vnet_buffer (b)->ip.reass.l4_src_port;
111 u32 sw_if_index, fib_index;
112 nat64_db_t *db = &nm->db[ctx->thread_index];
114 ip4 = vlib_buffer_get_current (b);
116 udp_header_t *udp = ip4_next_header (ip4);
117 tcp_header_t *tcp = ip4_next_header (ip4);
118 if (!vnet_buffer (b)->ip.reass.is_non_first_fragment)
120 if (ip4->protocol == IP_PROTOCOL_UDP)
122 checksum = &udp->checksum;
123 //UDP checksum is optional over IPv4 but mandatory for IPv6
124 //We do not check udp->length sanity but use our safe computed value instead
125 if (PREDICT_FALSE (!*checksum))
128 clib_host_to_net_u16 (ip4->length) - sizeof (*ip4);
129 csum = ip_incremental_checksum (0, udp, udp_len);
131 ip_csum_with_carry (csum, clib_host_to_net_u16 (udp_len));
133 ip_csum_with_carry (csum,
134 clib_host_to_net_u16 (IP_PROTOCOL_UDP));
136 ip_csum_with_carry (csum, *((u64 *) (&ip4->src_address)));
137 *checksum = ~ip_csum_fold (csum);
142 checksum = &tcp->checksum;
146 old_src.as_u32 = ip4->src_address.as_u32;
147 old_dst.as_u32 = ip4->dst_address.as_u32;
149 // Deal with fragmented packets
150 u16 frag_offset = ip4_get_fragment_offset (ip4);
151 if (PREDICT_FALSE (ip4_get_fragment_more (ip4) || frag_offset))
154 (ip6_header_t *) u8_ptr_add (ip4,
155 sizeof (*ip4) - sizeof (*ip6) -
158 (ip6_frag_hdr_t *) u8_ptr_add (ip4, sizeof (*ip4) - sizeof (*frag));
159 frag_id = frag_id_4to6 (ip4->fragment_id);
160 vlib_buffer_advance (b, sizeof (*ip4) - sizeof (*ip6) - sizeof (*frag));
164 ip6 = (ip6_header_t *) (((u8 *) ip4) + sizeof (*ip4) - sizeof (*ip6));
165 vlib_buffer_advance (b, sizeof (*ip4) - sizeof (*ip6));
169 ip6->ip_version_traffic_class_and_flow_label =
170 clib_host_to_net_u32 ((6 << 28) + (ip4->tos << 20));
171 ip6->payload_length = u16_net_add (ip4->length, -sizeof (*ip4));
172 ip6->hop_limit = ip4->ttl;
173 ip6->protocol = ip4->protocol;
175 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
176 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
178 clib_memset (&saddr, 0, sizeof (saddr));
179 saddr.ip4.as_u32 = ip4->src_address.as_u32;
180 clib_memset (&daddr, 0, sizeof (daddr));
181 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
184 nat64_db_st_entry_find (db, &daddr, &saddr, dport, sport, proto,
188 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
194 bibe = nat64_db_bib_entry_find (db, &daddr, dport, proto, fib_index, 0);
199 nat64_compose_ip6 (&ip6_saddr, &old_src, bibe->fib_index);
201 nat64_db_st_entry_create (ctx->thread_index, db, bibe, &ip6_saddr,
207 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
208 db->st.st_entries_num);
211 ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
212 ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
214 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
215 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
217 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
219 nat64_session_reset_timeout (ste, ctx->vm);
221 if (PREDICT_FALSE (frag != NULL))
223 frag->next_hdr = ip6->protocol;
224 frag->identification = frag_id;
226 frag->fragment_offset_and_more =
227 ip6_frag_hdr_offset_and_more (frag_offset, 1);
228 ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
229 ip6->payload_length = u16_net_add (ip6->payload_length, sizeof (*frag));
232 if (!vnet_buffer (b)->ip.reass.is_non_first_fragment)
234 udp->dst_port = bibe->in_port;
236 if (proto == IP_PROTOCOL_TCP)
238 nat64_tcp_session_set_state (ste, tcp, 0);
241 csum = ip_csum_sub_even (*checksum, dport);
242 csum = ip_csum_add_even (csum, udp->dst_port);
243 csum = ip_csum_sub_even (csum, old_src.as_u32);
244 csum = ip_csum_sub_even (csum, old_dst.as_u32);
245 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[0]);
246 csum = ip_csum_add_even (csum, ip6->src_address.as_u64[1]);
247 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[0]);
248 csum = ip_csum_add_even (csum, ip6->dst_address.as_u64[1]);
249 *checksum = ip_csum_fold (csum);
256 nat64_out2in_icmp_set_cb (vlib_buffer_t * b, ip4_header_t * ip4,
257 ip6_header_t * ip6, void *arg)
259 nat64_main_t *nm = &nat64_main;
260 nat64_out2in_set_ctx_t *ctx = arg;
261 nat64_db_bib_entry_t *bibe;
262 nat64_db_st_entry_t *ste;
263 ip46_address_t saddr, daddr;
264 ip6_address_t ip6_saddr;
265 u32 sw_if_index, fib_index;
266 icmp46_header_t *icmp = ip4_next_header (ip4);
267 nat64_db_t *db = &nm->db[ctx->thread_index];
269 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
270 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
272 clib_memset (&saddr, 0, sizeof (saddr));
273 saddr.ip4.as_u32 = ip4->src_address.as_u32;
274 clib_memset (&daddr, 0, sizeof (daddr));
275 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
277 if (icmp->type == ICMP6_echo_request || icmp->type == ICMP6_echo_reply)
279 u16 out_id = ((u16 *) (icmp))[2];
281 nat64_db_st_entry_find (db, &daddr, &saddr, out_id, 0,
282 IP_PROTOCOL_ICMP, fib_index, 0);
287 nat64_db_bib_entry_by_index (db, IP_PROTOCOL_ICMP,
295 nat64_db_bib_entry_find (db, &daddr, out_id,
296 IP_PROTOCOL_ICMP, fib_index, 0);
300 nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
302 nat64_db_st_entry_create (ctx->thread_index, db,
303 bibe, &ip6_saddr, &saddr.ip4, 0);
308 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
309 db->st.st_entries_num);
312 nat64_session_reset_timeout (ste, ctx->vm);
314 ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
315 ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
317 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
318 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
319 ((u16 *) (icmp))[2] = bibe->in_port;
321 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
325 ip6_header_t *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
327 nat64_compose_ip6 (&ip6->src_address, &ip4->src_address,
328 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX]);
329 ip6->dst_address.as_u64[0] = inner_ip6->src_address.as_u64[0];
330 ip6->dst_address.as_u64[1] = inner_ip6->src_address.as_u64[1];
337 nat64_out2in_inner_icmp_set_cb (vlib_buffer_t * b, ip4_header_t * ip4,
338 ip6_header_t * ip6, void *arg)
340 nat64_main_t *nm = &nat64_main;
341 nat64_out2in_set_ctx_t *ctx = arg;
342 nat64_db_bib_entry_t *bibe;
343 nat64_db_st_entry_t *ste;
344 ip46_address_t saddr, daddr;
345 u32 sw_if_index, fib_index;
346 u8 proto = ip4->protocol;
347 nat64_db_t *db = &nm->db[ctx->thread_index];
349 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
351 fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, sw_if_index);
353 clib_memset (&saddr, 0, sizeof (saddr));
354 saddr.ip4.as_u32 = ip4->src_address.as_u32;
355 clib_memset (&daddr, 0, sizeof (daddr));
356 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
358 if (proto == IP_PROTOCOL_ICMP6)
360 icmp46_header_t *icmp = ip4_next_header (ip4);
361 u16 out_id = ((u16 *) (icmp))[2];
362 proto = IP_PROTOCOL_ICMP;
365 (icmp->type == ICMP6_echo_request
366 || icmp->type == ICMP6_echo_reply))
370 nat64_db_st_entry_find (db, &saddr, &daddr, out_id, 0, proto,
375 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
379 ip6->dst_address.as_u64[0] = ste->in_r_addr.as_u64[0];
380 ip6->dst_address.as_u64[1] = ste->in_r_addr.as_u64[1];
381 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
382 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
383 ((u16 *) (icmp))[2] = bibe->in_port;
385 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
389 udp_header_t *udp = ip4_next_header (ip4);
390 tcp_header_t *tcp = ip4_next_header (ip4);
391 u16 dport = udp->dst_port;
392 u16 sport = udp->src_port;
397 nat64_db_st_entry_find (db, &saddr, &daddr, sport, dport, proto,
402 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
406 nat64_compose_ip6 (&ip6->dst_address, &daddr.ip4, bibe->fib_index);
407 ip6->src_address.as_u64[0] = bibe->in_addr.as_u64[0];
408 ip6->src_address.as_u64[1] = bibe->in_addr.as_u64[1];
409 udp->src_port = bibe->in_port;
411 if (proto == IP_PROTOCOL_UDP)
412 checksum = &udp->checksum;
414 checksum = &tcp->checksum;
417 csum = ip_csum_sub_even (*checksum, sport);
418 csum = ip_csum_add_even (csum, udp->src_port);
419 *checksum = ip_csum_fold (csum);
422 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
429 nat64_out2in_unk_proto (vlib_main_t * vm, vlib_buffer_t * p,
430 nat64_out2in_set_ctx_t * ctx)
432 ip4_header_t *ip4 = vlib_buffer_get_current (p);
434 ip6_frag_hdr_t *frag;
437 nat64_main_t *nm = &nat64_main;
438 nat64_db_bib_entry_t *bibe;
439 nat64_db_st_entry_t *ste;
440 ip46_address_t saddr, daddr;
441 ip6_address_t ip6_saddr;
442 u32 sw_if_index, fib_index;
443 u8 proto = ip4->protocol;
444 nat64_db_t *db = &nm->db[ctx->thread_index];
446 // Deal with fragmented packets
447 u16 frag_offset = ip4_get_fragment_offset (ip4);
448 if (PREDICT_FALSE (ip4_get_fragment_more (ip4) || frag_offset))
451 (ip6_header_t *) u8_ptr_add (ip4,
452 sizeof (*ip4) - sizeof (*ip6) -
455 (ip6_frag_hdr_t *) u8_ptr_add (ip4, sizeof (*ip4) - sizeof (*frag));
456 frag_id = frag_id_4to6 (ip4->fragment_id);
457 vlib_buffer_advance (p, sizeof (*ip4) - sizeof (*ip6) - sizeof (*frag));
461 ip6 = (ip6_header_t *) (((u8 *) ip4) + sizeof (*ip4) - sizeof (*ip6));
462 vlib_buffer_advance (p, sizeof (*ip4) - sizeof (*ip6));
466 ip6->ip_version_traffic_class_and_flow_label =
467 clib_host_to_net_u32 ((6 << 28) + (ip4->tos << 20));
468 ip6->payload_length = u16_net_add (ip4->length, -sizeof (*ip4));
469 ip6->hop_limit = ip4->ttl;
470 ip6->protocol = ip4->protocol;
472 if (PREDICT_FALSE (frag != NULL))
474 frag->next_hdr = ip6->protocol;
475 frag->identification = frag_id;
477 frag->fragment_offset_and_more =
478 ip6_frag_hdr_offset_and_more (frag_offset, 1);
479 ip6->protocol = IP_PROTOCOL_IPV6_FRAGMENTATION;
480 ip6->payload_length = u16_net_add (ip6->payload_length, sizeof (*frag));
483 sw_if_index = vnet_buffer (ctx->b)->sw_if_index[VLIB_RX];
484 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
486 clib_memset (&saddr, 0, sizeof (saddr));
487 saddr.ip4.as_u32 = ip4->src_address.as_u32;
488 clib_memset (&daddr, 0, sizeof (daddr));
489 daddr.ip4.as_u32 = ip4->dst_address.as_u32;
492 nat64_db_st_entry_find (db, &daddr, &saddr, 0, 0, proto, fib_index, 0);
495 bibe = nat64_db_bib_entry_by_index (db, proto, ste->bibe_index);
501 bibe = nat64_db_bib_entry_find (db, &daddr, 0, proto, fib_index, 0);
506 nat64_compose_ip6 (&ip6_saddr, &ip4->src_address, bibe->fib_index);
507 ste = nat64_db_st_entry_create (ctx->thread_index, db,
508 bibe, &ip6_saddr, &saddr.ip4, 0);
513 vlib_set_simple_counter (&nm->total_sessions, ctx->thread_index, 0,
514 db->st.st_entries_num);
517 nat64_session_reset_timeout (ste, ctx->vm);
519 ip6->src_address.as_u64[0] = ste->in_r_addr.as_u64[0];
520 ip6->src_address.as_u64[1] = ste->in_r_addr.as_u64[1];
522 ip6->dst_address.as_u64[0] = bibe->in_addr.as_u64[0];
523 ip6->dst_address.as_u64[1] = bibe->in_addr.as_u64[1];
525 vnet_buffer (ctx->b)->sw_if_index[VLIB_TX] = bibe->fib_index;
530 VLIB_NODE_FN (nat64_out2in_node) (vlib_main_t * vm,
531 vlib_node_runtime_t * node,
532 vlib_frame_t * frame)
534 u32 n_left_from, *from, *to_next;
535 nat64_out2in_next_t next_index;
536 nat64_main_t *nm = &nat64_main;
537 u32 pkts_processed = 0;
538 u32 thread_index = vm->thread_index;
539 u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
542 from = vlib_frame_vector_args (frame);
543 n_left_from = frame->n_vectors;
544 next_index = node->cached_next_index;
545 while (n_left_from > 0)
549 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
551 while (n_left_from > 0 && n_left_to_next > 0)
558 nat64_out2in_set_ctx_t ctx0;
561 /* speculatively enqueue b0 to the current next frame */
569 b0 = vlib_get_buffer (vm, bi0);
570 ip40 = vlib_buffer_get_current (b0);
574 ctx0.thread_index = thread_index;
576 next0 = NAT64_OUT2IN_NEXT_IP6_LOOKUP;
578 proto0 = ip_proto_to_nat_proto (ip40->protocol);
580 if (PREDICT_FALSE (proto0 == NAT_PROTOCOL_OTHER))
582 if (nat64_out2in_unk_proto (vm, b0, &ctx0))
584 next0 = NAT64_OUT2IN_NEXT_DROP;
585 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
591 if (proto0 == NAT_PROTOCOL_ICMP)
595 (b0, nat64_out2in_icmp_set_cb, &ctx0,
596 nat64_out2in_inner_icmp_set_cb, &ctx0))
598 next0 = NAT64_OUT2IN_NEXT_DROP;
599 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
605 if (proto0 == NAT_PROTOCOL_TCP)
610 if (nat64_out2in_tcp_udp (vm, b0, &ctx0))
612 udp0 = ip4_next_header (ip40);
614 * Send DHCP packets to the ipv4 stack, or we won't
615 * be able to use dhcp client on the outside interface
617 if ((proto0 == NAT_PROTOCOL_UDP)
618 && (udp0->dst_port ==
619 clib_host_to_net_u16 (UDP_DST_PORT_dhcp_to_client)))
621 next0 = NAT64_OUT2IN_NEXT_IP4_LOOKUP;
624 next0 = NAT64_OUT2IN_NEXT_DROP;
625 b0->error = node->errors[NAT64_OUT2IN_ERROR_NO_TRANSLATION];
631 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
632 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
634 nat64_out2in_trace_t *t =
635 vlib_add_trace (vm, node, b0, sizeof (*t));
636 t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
637 t->next_index = next0;
640 pkts_processed += next0 == NAT64_OUT2IN_NEXT_IP6_LOOKUP;
642 /* verify speculative enqueue, maybe switch current next frame */
643 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
644 n_left_to_next, bi0, next0);
646 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
648 vlib_node_increment_counter (vm, nm->out2in_node_index,
649 NAT64_OUT2IN_ERROR_OUT2IN_PACKETS,
651 vlib_node_increment_counter (vm, nm->out2in_node_index,
652 NAT64_OUT2IN_ERROR_TCP_PACKETS, tcp_packets);
653 vlib_node_increment_counter (vm, nm->out2in_node_index,
654 NAT64_OUT2IN_ERROR_UDP_PACKETS, udp_packets);
655 vlib_node_increment_counter (vm, nm->out2in_node_index,
656 NAT64_OUT2IN_ERROR_ICMP_PACKETS, icmp_packets);
657 vlib_node_increment_counter (vm, nm->out2in_node_index,
658 NAT64_OUT2IN_ERROR_OTHER_PACKETS,
660 vlib_node_increment_counter (vm, nm->out2in_node_index,
661 NAT64_OUT2IN_ERROR_FRAGMENTS, fragments);
663 return frame->n_vectors;
667 VLIB_REGISTER_NODE (nat64_out2in_node) = {
668 .name = "nat64-out2in",
669 .vector_size = sizeof (u32),
670 .format_trace = format_nat64_out2in_trace,
671 .type = VLIB_NODE_TYPE_INTERNAL,
672 .n_errors = ARRAY_LEN (nat64_out2in_error_strings),
673 .error_strings = nat64_out2in_error_strings,
674 .n_next_nodes = NAT64_OUT2IN_N_NEXT,
675 /* edit / add dispositions here */
677 [NAT64_OUT2IN_NEXT_DROP] = "error-drop",
678 [NAT64_OUT2IN_NEXT_IP6_LOOKUP] = "ip6-lookup",
679 [NAT64_OUT2IN_NEXT_IP4_LOOKUP] = "ip4-lookup",
684 typedef struct nat64_out2in_frag_set_ctx_t_
692 } nat64_out2in_frag_set_ctx_t;
694 #define foreach_nat64_out2in_handoff_error \
695 _(CONGESTION_DROP, "congestion drop") \
696 _(SAME_WORKER, "same worker") \
697 _(DO_HANDOFF, "do handoff")
701 #define _(sym,str) NAT64_OUT2IN_HANDOFF_ERROR_##sym,
702 foreach_nat64_out2in_handoff_error
704 NAT64_OUT2IN_HANDOFF_N_ERROR,
705 } nat64_out2in_handoff_error_t;
707 static char *nat64_out2in_handoff_error_strings[] = {
708 #define _(sym,string) string,
709 foreach_nat64_out2in_handoff_error
715 u32 next_worker_index;
716 } nat64_out2in_handoff_trace_t;
719 format_nat64_out2in_handoff_trace (u8 * s, va_list * args)
721 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
722 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
723 nat64_out2in_handoff_trace_t *t =
724 va_arg (*args, nat64_out2in_handoff_trace_t *);
727 format (s, "NAT64-OUT2IN-HANDOFF: next-worker %d", t->next_worker_index);
732 VLIB_NODE_FN (nat64_out2in_handoff_node) (vlib_main_t * vm,
733 vlib_node_runtime_t * node,
734 vlib_frame_t * frame)
736 nat64_main_t *nm = &nat64_main;
737 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
738 u32 n_enq, n_left_from, *from;
739 u16 thread_indices[VLIB_FRAME_SIZE], *ti;
741 u32 thread_index = vm->thread_index;
742 u32 do_handoff = 0, same_worker = 0;
744 from = vlib_frame_vector_args (frame);
745 n_left_from = frame->n_vectors;
746 vlib_get_buffers (vm, from, bufs, n_left_from);
751 fq_index = nm->fq_out2in_index;
753 while (n_left_from > 0)
757 ip0 = vlib_buffer_get_current (b[0]);
758 ti[0] = nat64_get_worker_out2in (b[0], ip0);
760 if (ti[0] != thread_index)
766 ((node->flags & VLIB_NODE_FLAG_TRACE)
767 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
769 nat64_out2in_handoff_trace_t *t =
770 vlib_add_trace (vm, node, b[0], sizeof (*t));
771 t->next_worker_index = ti[0];
780 vlib_buffer_enqueue_to_thread (vm, fq_index, from, thread_indices,
781 frame->n_vectors, 1);
783 if (n_enq < frame->n_vectors)
784 vlib_node_increment_counter (vm, node->node_index,
785 NAT64_OUT2IN_HANDOFF_ERROR_CONGESTION_DROP,
786 frame->n_vectors - n_enq);
787 vlib_node_increment_counter (vm, node->node_index,
788 NAT64_OUT2IN_HANDOFF_ERROR_SAME_WORKER,
790 vlib_node_increment_counter (vm, node->node_index,
791 NAT64_OUT2IN_HANDOFF_ERROR_DO_HANDOFF,
794 return frame->n_vectors;
798 VLIB_REGISTER_NODE (nat64_out2in_handoff_node) = {
799 .name = "nat64-out2in-handoff",
800 .vector_size = sizeof (u32),
801 .format_trace = format_nat64_out2in_handoff_trace,
802 .type = VLIB_NODE_TYPE_INTERNAL,
803 .n_errors = ARRAY_LEN(nat64_out2in_handoff_error_strings),
804 .error_strings = nat64_out2in_handoff_error_strings,
815 * fd.io coding-style-patch-verification: ON
818 * eval: (c-set-style "gnu")