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.
17 #include <vnet/fib/ip4_fib.h>
19 #include <vnet/gre/packet.h>
20 #include <lb/lbhash.h>
22 #define foreach_lb_error \
24 _(PROTO_NOT_SUPPORTED, "protocol not supported")
28 #define _(sym,str) LB_ERROR_##sym,
34 static char *lb_error_strings[] =
36 #define _(sym,string) string,
52 } lb_nodeport_trace_t;
63 format_lb_trace (u8 * s, va_list * args)
65 lb_main_t *lbm = &lb_main;
66 CLIB_UNUSED(vlib_main_t * vm)
67 = va_arg (*args, vlib_main_t *);
68 CLIB_UNUSED(vlib_node_t * node)
69 = va_arg (*args, vlib_node_t *);
70 lb_trace_t *t = va_arg (*args, lb_trace_t *);
71 if (pool_is_free_index(lbm->vips, t->vip_index))
73 s = format (s, "lb vip[%d]: This VIP was freed since capture\n");
77 s = format (s, "lb vip[%d]: %U\n", t->vip_index, format_lb_vip,
78 &lbm->vips[t->vip_index]);
80 if (pool_is_free_index(lbm->ass, t->as_index))
82 s = format (s, "lb as[%d]: This AS was freed since capture\n");
86 s = format (s, "lb as[%d]: %U\n", t->as_index, format_lb_as,
87 &lbm->ass[t->as_index]);
93 format_lb_nat_trace (u8 * s, va_list * args)
95 lb_main_t *lbm = &lb_main;
96 CLIB_UNUSED(vlib_main_t * vm)
97 = va_arg (*args, vlib_main_t *);
98 CLIB_UNUSED(vlib_node_t * node)
99 = va_arg (*args, vlib_node_t *);
100 lb_nat_trace_t *t = va_arg (*args, lb_nat_trace_t *);
102 if (pool_is_free_index(lbm->vips, t->vip_index))
104 s = format (s, "lb vip[%d]: This VIP was freed since capture\n");
108 s = format (s, "lb vip[%d]: %U\n", t->vip_index, format_lb_vip,
109 &lbm->vips[t->vip_index]);
111 if (pool_is_free_index(lbm->ass, t->as_index))
113 s = format (s, "lb as[%d]: This AS was freed since capture\n");
117 s = format (s, "lb as[%d]: %U\n", t->as_index, format_lb_as,
118 &lbm->ass[t->as_index]);
120 s = format (s, "lb nat: rx_sw_if_index = %d, next_index = %d",
121 t->rx_sw_if_index, t->next_index);
127 lb_get_sticky_table (u32 thread_index)
129 lb_main_t *lbm = &lb_main;
130 lb_hash_t *sticky_ht = lbm->per_cpu[thread_index].sticky_ht;
131 //Check if size changed
133 sticky_ht && (lbm->per_cpu_sticky_buckets != lb_hash_nbuckets(sticky_ht))))
135 //Dereference everything in there
138 lb_hash_foreach_entry(sticky_ht, b, i)
140 vlib_refcount_add (&lbm->as_refcount, thread_index, b->value[i], -1);
141 vlib_refcount_add (&lbm->as_refcount, thread_index, 0, 1);
144 lb_hash_free (sticky_ht);
148 //Create if necessary
149 if (PREDICT_FALSE(sticky_ht == NULL))
151 lbm->per_cpu[thread_index].sticky_ht = lb_hash_alloc (
152 lbm->per_cpu_sticky_buckets, lbm->flow_timeout);
153 sticky_ht = lbm->per_cpu[thread_index].sticky_ht;
154 clib_warning("Regenerated sticky table %p", sticky_ht);
160 sticky_ht->timeout = lbm->flow_timeout;
165 lb_node_get_other_ports4 (ip4_header_t *ip40)
171 lb_node_get_other_ports6 (ip6_header_t *ip60)
176 static_always_inline void
177 lb_node_get_hash (lb_main_t *lbm, vlib_buffer_t *p, u8 is_input_v4,
178 u32 *hash, u32 *vip_idx, u8 per_port_vip)
181 clib_bihash_kv_8_8_t kv, value;
183 /* For vip case, retrieve vip index for ip lookup */
184 *vip_idx = vnet_buffer (p)->ip.adj_index[VLIB_TX];
188 /* For per-port-vip case, ip lookup stores dummy index */
189 key.vip_prefix_index = *vip_idx;
197 ip40 = vlib_buffer_get_current (p);
199 ip40->protocol == IP_PROTOCOL_TCP
200 || ip40->protocol == IP_PROTOCOL_UDP))
201 ports = ((u64) ((udp_header_t *) (ip40 + 1))->src_port << 16)
202 | ((u64) ((udp_header_t *) (ip40 + 1))->dst_port);
204 ports = lb_node_get_other_ports4 (ip40);
206 *hash = lb_hash_hash (*((u64 *) &ip40->address_pair), ports, 0, 0, 0);
210 key.protocol = ip40->protocol;
211 key.port = (u16)(ports & 0xFFFF);
217 ip60 = vlib_buffer_get_current (p);
221 ip60->protocol == IP_PROTOCOL_TCP
222 || ip60->protocol == IP_PROTOCOL_UDP))
223 ports = ((u64) ((udp_header_t *) (ip60 + 1))->src_port << 16)
224 | ((u64) ((udp_header_t *) (ip60 + 1))->dst_port);
226 ports = lb_node_get_other_ports6 (ip60);
228 *hash = lb_hash_hash (ip60->src_address.as_u64[0],
229 ip60->src_address.as_u64[1],
230 ip60->dst_address.as_u64[0],
231 ip60->dst_address.as_u64[1], ports);
235 key.protocol = ip60->protocol;
236 key.port = (u16)(ports & 0xFFFF);
240 /* For per-port-vip case, retrieve vip index for vip_port_filter table */
244 if (clib_bihash_search_8_8(&lbm->vip_index_per_port, &kv, &value) < 0)
246 /* return default vip */
250 *vip_idx = value.value;
254 static_always_inline uword
255 lb_node_fn (vlib_main_t * vm,
256 vlib_node_runtime_t * node,
257 vlib_frame_t * frame,
258 u8 is_input_v4, //Compile-time parameter stating that is input is v4 (or v6)
259 lb_encap_type_t encap_type, //Compile-time parameter is GRE4/GRE6/L3DSR/NAT4/NAT6
260 u8 per_port_vip) //Compile-time parameter stating that is per_port_vip or not
262 lb_main_t *lbm = &lb_main;
263 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
264 u32 thread_index = vm->thread_index;
265 u32 lb_time = lb_hash_time_now (vm);
267 lb_hash_t *sticky_ht = lb_get_sticky_table (thread_index);
268 from = vlib_frame_vector_args (frame);
269 n_left_from = frame->n_vectors;
270 next_index = node->cached_next_index;
273 u32 next_vip_idx0 = ~0;
274 if (PREDICT_TRUE(n_left_from > 0))
276 vlib_buffer_t *p0 = vlib_get_buffer (vm, from[0]);
277 lb_node_get_hash (lbm, p0, is_input_v4, &nexthash0,
278 &next_vip_idx0, per_port_vip);
281 while (n_left_from > 0)
283 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
284 while (n_left_from > 0 && n_left_to_next > 0)
291 u32 available_index0;
293 u32 hash0 = nexthash0;
294 u32 vip_index0 = next_vip_idx0;
297 if (PREDICT_TRUE(n_left_from > 1))
299 vlib_buffer_t *p1 = vlib_get_buffer (vm, from[1]);
300 //Compute next hash and prefetch bucket
301 lb_node_get_hash (lbm, p1, is_input_v4,
302 &nexthash0, &next_vip_idx0,
304 lb_hash_prefetch_bucket (sticky_ht, nexthash0);
305 //Prefetch for encap, next
306 CLIB_PREFETCH(vlib_buffer_get_current (p1) - 64, 64, STORE);
309 if (PREDICT_TRUE(n_left_from > 2))
312 p2 = vlib_get_buffer (vm, from[2]);
313 /* prefetch packet header and data */
314 vlib_prefetch_buffer_header(p2, STORE);
315 CLIB_PREFETCH(vlib_buffer_get_current (p2), 64, STORE);
318 pi0 = to_next[0] = from[0];
324 p0 = vlib_get_buffer (vm, pi0);
326 vip0 = pool_elt_at_index(lbm->vips, vip_index0);
331 ip40 = vlib_buffer_get_current (p0);
332 len0 = clib_net_to_host_u16 (ip40->length);
337 ip60 = vlib_buffer_get_current (p0);
338 len0 = clib_net_to_host_u16 (ip60->payload_length)
339 + sizeof(ip6_header_t);
342 lb_hash_get (sticky_ht, hash0,
344 &available_index0, &asindex0);
346 if (PREDICT_TRUE(asindex0 != 0))
348 //Found an existing entry
349 counter = LB_VIP_COUNTER_NEXT_PACKET;
351 else if (PREDICT_TRUE(available_index0 != ~0))
353 //There is an available slot for a new flow
355 vip0->new_flow_table[hash0 & vip0->new_flow_table_mask].as_index;
356 counter = LB_VIP_COUNTER_FIRST_PACKET;
357 counter = (asindex0 == 0) ? LB_VIP_COUNTER_NO_SERVER : counter;
359 //TODO: There are race conditions with as0 and vip0 manipulation.
360 //Configuration may be changed, vectors resized, etc...
362 //Dereference previously used
364 &lbm->as_refcount, thread_index,
365 lb_hash_available_value (sticky_ht, hash0, available_index0),
367 vlib_refcount_add (&lbm->as_refcount, thread_index, asindex0, 1);
370 //Note that when there is no AS configured, an entry is configured anyway.
371 //But no configured AS is not something that should happen
372 lb_hash_put (sticky_ht, hash0, asindex0,
374 available_index0, lb_time);
378 //Could not store new entry in the table
380 vip0->new_flow_table[hash0 & vip0->new_flow_table_mask].as_index;
381 counter = LB_VIP_COUNTER_UNTRACKED_PACKET;
384 vlib_increment_simple_counter (
385 &lbm->vip_counters[counter], thread_index,
390 if ((encap_type == LB_ENCAP_TYPE_GRE4)
391 || (encap_type == LB_ENCAP_TYPE_GRE6))
394 if (encap_type == LB_ENCAP_TYPE_GRE4) /* encap GRE4*/
397 vlib_buffer_advance (
398 p0, -sizeof(ip4_header_t) - sizeof(gre_header_t));
399 ip40 = vlib_buffer_get_current (p0);
400 gre0 = (gre_header_t *) (ip40 + 1);
401 ip40->src_address = lbm->ip4_src_address;
402 ip40->dst_address = lbm->ass[asindex0].address.ip4;
403 ip40->ip_version_and_header_length = 0x45;
405 ip40->fragment_id = 0;
406 ip40->flags_and_fragment_offset = 0;
407 ip40->length = clib_host_to_net_u16 (
408 len0 + sizeof(gre_header_t) + sizeof(ip4_header_t));
409 ip40->protocol = IP_PROTOCOL_GRE;
410 ip40->checksum = ip4_header_checksum (ip40);
415 vlib_buffer_advance (
416 p0, -sizeof(ip6_header_t) - sizeof(gre_header_t));
417 ip60 = vlib_buffer_get_current (p0);
418 gre0 = (gre_header_t *) (ip60 + 1);
419 ip60->dst_address = lbm->ass[asindex0].address.ip6;
420 ip60->src_address = lbm->ip6_src_address;
421 ip60->hop_limit = 128;
422 ip60->ip_version_traffic_class_and_flow_label =
423 clib_host_to_net_u32 (0x6 << 28);
424 ip60->payload_length = clib_host_to_net_u16 (
425 len0 + sizeof(gre_header_t));
426 ip60->protocol = IP_PROTOCOL_GRE;
429 gre0->flags_and_version = 0;
432 clib_host_to_net_u16 (0x0800) :
433 clib_host_to_net_u16 (0x86DD);
435 else if (encap_type == LB_ENCAP_TYPE_L3DSR) /* encap L3DSR*/
440 u32 old_dst, new_dst;
443 ip40 = vlib_buffer_get_current (p0);
444 old_dst = ip40->dst_address.as_u32;
445 new_dst = lbm->ass[asindex0].address.ip4.as_u32;
446 ip40->dst_address.as_u32 = lbm->ass[asindex0].address.ip4.as_u32;
447 /* Get and rewrite DSCP bit */
449 new_tos = (u8) ((vip0->encap_args.dscp & 0x3F) << 2);
450 ip40->tos = (u8) ((vip0->encap_args.dscp & 0x3F) << 2);
452 csum = ip40->checksum;
453 csum = ip_csum_update (csum, old_tos, new_tos,
455 tos /* changed member */);
456 csum = ip_csum_update (csum, old_dst, new_dst,
458 dst_address /* changed member */);
459 ip40->checksum = ip_csum_fold (csum);
461 /* Recomputing L4 checksum after dst-IP modifying */
462 th0 = ip4_next_header (ip40);
464 th0->checksum = ip4_tcp_udp_compute_checksum (vm, p0, ip40);
466 else if ((encap_type == LB_ENCAP_TYPE_NAT4)
467 || (encap_type == LB_ENCAP_TYPE_NAT6))
473 if ((is_input_v4 == 1) && (encap_type == LB_ENCAP_TYPE_NAT4))
478 ip40 = vlib_buffer_get_current (p0);
479 uh = (udp_header_t *) (ip40 + 1);
480 old_dst = ip40->dst_address.as_u32;
481 ip40->dst_address = lbm->ass[asindex0].address.ip4;
483 csum = ip40->checksum;
484 csum = ip_csum_sub_even (csum, old_dst);
485 csum = ip_csum_add_even (
486 csum, lbm->ass[asindex0].address.ip4.as_u32);
487 ip40->checksum = ip_csum_fold (csum);
489 if (ip40->protocol == IP_PROTOCOL_UDP)
491 uh->dst_port = vip0->encap_args.target_port;
493 csum = ip_csum_sub_even (csum, old_dst);
494 csum = ip_csum_add_even (
495 csum, lbm->ass[asindex0].address.ip4.as_u32);
496 uh->checksum = ip_csum_fold (csum);
503 else if ((is_input_v4 == 0) && (encap_type == LB_ENCAP_TYPE_NAT6))
507 ip6_address_t old_dst;
509 ip60 = vlib_buffer_get_current (p0);
510 uh = (udp_header_t *) (ip60 + 1);
512 old_dst.as_u64[0] = ip60->dst_address.as_u64[0];
513 old_dst.as_u64[1] = ip60->dst_address.as_u64[1];
514 ip60->dst_address.as_u64[0] =
515 lbm->ass[asindex0].address.ip6.as_u64[0];
516 ip60->dst_address.as_u64[1] =
517 lbm->ass[asindex0].address.ip6.as_u64[1];
519 if (PREDICT_TRUE(ip60->protocol == IP_PROTOCOL_UDP))
521 uh->dst_port = vip0->encap_args.target_port;
523 csum = ip_csum_sub_even (csum, old_dst.as_u64[0]);
524 csum = ip_csum_sub_even (csum, old_dst.as_u64[1]);
525 csum = ip_csum_add_even (
526 csum, lbm->ass[asindex0].address.ip6.as_u64[0]);
527 csum = ip_csum_add_even (
528 csum, lbm->ass[asindex0].address.ip6.as_u64[1]);
529 uh->checksum = ip_csum_fold (csum);
537 next0 = lbm->ass[asindex0].dpo.dpoi_next_node;
538 //Note that this is going to error if asindex0 == 0
539 vnet_buffer (p0)->ip.adj_index[VLIB_TX] =
540 lbm->ass[asindex0].dpo.dpoi_index;
542 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
544 lb_trace_t *tr = vlib_add_trace (vm, node, p0, sizeof(*tr));
545 tr->as_index = asindex0;
546 tr->vip_index = vip_index0;
550 vlib_validate_buffer_enqueue_x1(
551 vm, node, next_index, to_next, n_left_to_next, pi0, next0);
553 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
556 return frame->n_vectors;
560 format_nodeport_lb_trace (u8 * s, va_list * args)
562 lb_main_t *lbm = &lb_main;
563 CLIB_UNUSED(vlib_main_t * vm)
564 = va_arg (*args, vlib_main_t *);
565 CLIB_UNUSED(vlib_node_t * node)
566 = va_arg (*args, vlib_node_t *);
567 lb_nodeport_trace_t *t = va_arg (*args, lb_nodeport_trace_t *);
568 if (pool_is_free_index(lbm->vips, t->vip_index))
570 s = format (s, "lb vip[%d]: This VIP was freed since capture\n");
574 s = format (s, "lb vip[%d]: %U\n", t->vip_index, format_lb_vip,
575 &lbm->vips[t->vip_index]);
578 s = format (s, " lb node_port: %d", t->node_port);
584 lb_nodeport_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
585 vlib_frame_t * frame, u8 is_input_v4)
587 lb_main_t *lbm = &lb_main;
588 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
590 from = vlib_frame_vector_args (frame);
591 n_left_from = frame->n_vectors;
592 next_index = node->cached_next_index;
594 while (n_left_from > 0)
596 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
598 while (n_left_from > 0 && n_left_to_next > 0)
602 udp_header_t * udp_0;
605 if (PREDICT_TRUE(n_left_from > 1))
607 vlib_buffer_t *p1 = vlib_get_buffer (vm, from[1]);
608 //Prefetch for encap, next
609 CLIB_PREFETCH(vlib_buffer_get_current (p1) - 64, 64, STORE);
612 if (PREDICT_TRUE(n_left_from > 2))
615 p2 = vlib_get_buffer (vm, from[2]);
616 /* prefetch packet header and data */
617 vlib_prefetch_buffer_header(p2, STORE);
618 CLIB_PREFETCH(vlib_buffer_get_current (p2), 64, STORE);
621 pi0 = to_next[0] = from[0];
627 p0 = vlib_get_buffer (vm, pi0);
632 vlib_buffer_advance (
633 p0, -(word) (sizeof(udp_header_t) + sizeof(ip4_header_t)));
634 ip40 = vlib_buffer_get_current (p0);
635 udp_0 = (udp_header_t *) (ip40 + 1);
640 vlib_buffer_advance (
641 p0, -(word) (sizeof(udp_header_t) + sizeof(ip6_header_t)));
642 ip60 = vlib_buffer_get_current (p0);
643 udp_0 = (udp_header_t *) (ip60 + 1);
646 entry0 = hash_get_mem(lbm->vip_index_by_nodeport, &(udp_0->dst_port));
649 vnet_buffer(p0)->ip.adj_index[VLIB_TX] = entry0 ? entry0[0]
652 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
654 lb_nodeport_trace_t *tr = vlib_add_trace (vm, node, p0,
656 tr->vip_index = entry0 ? entry0[0] : ADJ_INDEX_INVALID;
657 tr->node_port = (u32) clib_net_to_host_u16 (udp_0->dst_port);
660 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
663 LB4_NODEPORT_NEXT_IP4_NAT4 : LB6_NODEPORT_NEXT_IP6_NAT6);
665 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
668 return frame->n_vectors;
673 * @brief Match NAT44 static mapping.
675 * @param sm NAT main.
676 * @param match Address and port to match.
677 * @param index index to the pool.
679 * @returns 0 if match found, otherwise -1.
682 lb_nat44_mapping_match (lb_main_t *lbm, lb_snat4_key_t * match, u32 *index)
684 clib_bihash_kv_8_8_t kv4, value;
685 clib_bihash_8_8_t *mapping_hash = &lbm->mapping_by_as4;
687 kv4.key = match->as_u64;
689 if (clib_bihash_search_8_8 (mapping_hash, &kv4, &value))
694 *index = value.value;
699 * @brief Match NAT66 static mapping.
701 * @param sm NAT main.
702 * @param match Address and port to match.
703 * @param mapping External or local address and port of the matched mapping.
705 * @returns 0 if match found otherwise 1.
708 lb_nat66_mapping_match (lb_main_t *lbm, lb_snat6_key_t * match, u32 *index)
710 clib_bihash_kv_24_8_t kv6, value;
711 lb_snat6_key_t m_key6;
712 clib_bihash_24_8_t *mapping_hash = &lbm->mapping_by_as6;
714 m_key6.addr.as_u64[0] = match->addr.as_u64[0];
715 m_key6.addr.as_u64[1] = match->addr.as_u64[1];
716 m_key6.port = match->port;
718 m_key6.fib_index = 0;
720 kv6.key[0] = m_key6.as_u64[0];
721 kv6.key[1] = m_key6.as_u64[1];
722 kv6.key[2] = m_key6.as_u64[2];
724 if (clib_bihash_search_24_8 (mapping_hash, &kv6, &value))
729 *index = value.value;
734 lb_nat_in2out_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
735 vlib_frame_t * frame, u32 is_nat4)
737 u32 n_left_from, *from, *to_next;
739 u32 pkts_processed = 0;
740 lb_main_t *lbm = &lb_main;
741 u32 stats_node_index;
744 is_nat4 ? lb_nat4_in2out_node.index : lb_nat6_in2out_node.index;
746 from = vlib_frame_vector_args (frame);
747 n_left_from = frame->n_vectors;
748 next_index = node->cached_next_index;
750 while (n_left_from > 0)
754 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
756 while (n_left_from > 0 && n_left_to_next > 0)
763 u16 old_port0, new_port0;
770 /* speculatively enqueue b0 to the current next frame */
778 b0 = vlib_get_buffer (vm, bi0);
779 next0 = LB_NAT4_IN2OUT_NEXT_LOOKUP;
780 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
781 rx_fib_index0 = ip4_fib_table_get_index_for_sw_if_index (
787 u32 old_addr0, new_addr0;
788 lb_snat4_key_t key40;
789 lb_snat_mapping_t *sm40;
792 ip40 = vlib_buffer_get_current (b0);
793 udp0 = ip4_next_header (ip40);
794 tcp0 = (tcp_header_t *) udp0;
795 proto0 = lb_ip_proto_to_nat_proto (ip40->protocol);
797 key40.addr = ip40->src_address;
798 key40.protocol = proto0;
799 key40.port = udp0->src_port;
800 key40.fib_index = rx_fib_index0;
802 if (lb_nat44_mapping_match (lbm, &key40, &index40))
804 next0 = LB_NAT4_IN2OUT_NEXT_DROP;
808 sm40 = pool_elt_at_index(lbm->snat_mappings, index40);
809 new_addr0 = sm40->src_ip.ip4.as_u32;
810 new_port0 = sm40->src_port;
811 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm40->fib_index;
812 old_addr0 = ip40->src_address.as_u32;
813 ip40->src_address.as_u32 = new_addr0;
815 csum = ip40->checksum;
816 csum = ip_csum_sub_even (csum, old_addr0);
817 csum = ip_csum_add_even (csum, new_addr0);
818 ip40->checksum = ip_csum_fold (csum);
820 if (PREDICT_TRUE(proto0 == LB_NAT_PROTOCOL_TCP))
822 old_port0 = tcp0->src_port;
823 tcp0->src_port = new_port0;
825 csum = tcp0->checksum;
826 csum = ip_csum_sub_even (csum, old_addr0);
827 csum = ip_csum_sub_even (csum, old_port0);
828 csum = ip_csum_add_even (csum, new_addr0);
829 csum = ip_csum_add_even (csum, new_port0);
830 tcp0->checksum = ip_csum_fold (csum);
832 else if (PREDICT_TRUE(proto0 == LB_NAT_PROTOCOL_UDP))
834 old_port0 = udp0->src_port;
835 udp0->src_port = new_port0;
837 csum = udp0->checksum;
838 csum = ip_csum_sub_even (csum, old_addr0);
839 csum = ip_csum_sub_even (csum, old_port0);
840 csum = ip_csum_add_even (csum, new_addr0);
841 csum = ip_csum_add_even (csum, new_port0);
842 udp0->checksum = ip_csum_fold (csum);
845 pkts_processed += next0 != LB_NAT4_IN2OUT_NEXT_DROP;
850 ip6_address_t old_addr0, new_addr0;
851 lb_snat6_key_t key60;
852 lb_snat_mapping_t *sm60;
855 ip60 = vlib_buffer_get_current (b0);
856 udp0 = ip6_next_header (ip60);
857 tcp0 = (tcp_header_t *) udp0;
858 proto0 = lb_ip_proto_to_nat_proto (ip60->protocol);
860 key60.addr.as_u64[0] = ip60->src_address.as_u64[0];
861 key60.addr.as_u64[1] = ip60->src_address.as_u64[1];
862 key60.protocol = proto0;
863 key60.port = udp0->src_port;
864 key60.fib_index = rx_fib_index0;
866 if (lb_nat66_mapping_match (lbm, &key60, &index60))
868 next0 = LB_NAT6_IN2OUT_NEXT_DROP;
872 sm60 = pool_elt_at_index(lbm->snat_mappings, index60);
873 new_addr0.as_u64[0] = sm60->src_ip.as_u64[0];
874 new_addr0.as_u64[1] = sm60->src_ip.as_u64[1];
875 new_port0 = sm60->src_port;
876 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sm60->fib_index;
877 old_addr0.as_u64[0] = ip60->src_address.as_u64[0];
878 old_addr0.as_u64[1] = ip60->src_address.as_u64[1];
879 ip60->src_address.as_u64[0] = new_addr0.as_u64[0];
880 ip60->src_address.as_u64[1] = new_addr0.as_u64[1];
882 if (PREDICT_TRUE(proto0 == LB_NAT_PROTOCOL_TCP))
884 old_port0 = tcp0->src_port;
885 tcp0->src_port = new_port0;
887 csum = tcp0->checksum;
888 csum = ip_csum_sub_even (csum, old_addr0.as_u64[0]);
889 csum = ip_csum_sub_even (csum, old_addr0.as_u64[1]);
890 csum = ip_csum_add_even (csum, new_addr0.as_u64[0]);
891 csum = ip_csum_add_even (csum, new_addr0.as_u64[1]);
892 csum = ip_csum_sub_even (csum, old_port0);
893 csum = ip_csum_add_even (csum, new_port0);
894 tcp0->checksum = ip_csum_fold (csum);
896 else if (PREDICT_TRUE(proto0 == LB_NAT_PROTOCOL_UDP))
898 old_port0 = udp0->src_port;
899 udp0->src_port = new_port0;
901 csum = udp0->checksum;
902 csum = ip_csum_sub_even (csum, old_addr0.as_u64[0]);
903 csum = ip_csum_sub_even (csum, old_addr0.as_u64[1]);
904 csum = ip_csum_add_even (csum, new_addr0.as_u64[0]);
905 csum = ip_csum_add_even (csum, new_addr0.as_u64[1]);
906 csum = ip_csum_sub_even (csum, old_port0);
907 csum = ip_csum_add_even (csum, new_port0);
908 udp0->checksum = ip_csum_fold (csum);
911 pkts_processed += next0 != LB_NAT4_IN2OUT_NEXT_DROP;
914 trace0: if (PREDICT_FALSE(
915 (node->flags & VLIB_NODE_FLAG_TRACE) && (b0->flags & VLIB_BUFFER_IS_TRACED)))
917 lb_nat_trace_t *t = vlib_add_trace (vm, node, b0, sizeof(*t));
918 t->rx_sw_if_index = sw_if_index0;
919 t->next_index = next0;
922 /* verify speculative enqueue, maybe switch current next frame */
923 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
924 n_left_to_next, bi0, next0);
927 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
930 vlib_node_increment_counter (vm, stats_node_index,
931 LB_NAT_IN2OUT_ERROR_IN2OUT_PACKETS,
933 return frame->n_vectors;
937 lb6_gre6_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
938 vlib_frame_t * frame)
940 return lb_node_fn (vm, node, frame, 0, LB_ENCAP_TYPE_GRE6, 0);
944 lb6_gre4_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
945 vlib_frame_t * frame)
947 return lb_node_fn (vm, node, frame, 0, LB_ENCAP_TYPE_GRE4, 0);
951 lb4_gre6_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
952 vlib_frame_t * frame)
954 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_GRE6, 0);
958 lb4_gre4_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
959 vlib_frame_t * frame)
961 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_GRE4, 0);
965 lb6_gre6_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
966 vlib_frame_t * frame)
968 return lb_node_fn (vm, node, frame, 0, LB_ENCAP_TYPE_GRE6, 1);
972 lb6_gre4_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
973 vlib_frame_t * frame)
975 return lb_node_fn (vm, node, frame, 0, LB_ENCAP_TYPE_GRE4, 1);
979 lb4_gre6_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
980 vlib_frame_t * frame)
982 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_GRE6, 1);
986 lb4_gre4_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
987 vlib_frame_t * frame)
989 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_GRE4, 1);
993 lb4_l3dsr_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
994 vlib_frame_t * frame)
996 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_L3DSR, 0);
1000 lb4_l3dsr_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1001 vlib_frame_t * frame)
1003 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_L3DSR, 1);
1007 lb6_nat6_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1008 vlib_frame_t * frame)
1010 return lb_node_fn (vm, node, frame, 0, LB_ENCAP_TYPE_NAT6, 1);
1014 lb4_nat4_port_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1015 vlib_frame_t * frame)
1017 return lb_node_fn (vm, node, frame, 1, LB_ENCAP_TYPE_NAT4, 1);
1021 lb_nat4_in2out_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1022 vlib_frame_t * frame)
1024 return lb_nat_in2out_node_fn (vm, node, frame, 1);
1028 lb_nat6_in2out_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1029 vlib_frame_t * frame)
1031 return lb_nat_in2out_node_fn (vm, node, frame, 0);
1034 VLIB_REGISTER_NODE (lb6_gre6_node) =
1036 .function = lb6_gre6_node_fn,
1038 .vector_size = sizeof(u32),
1039 .format_trace = format_lb_trace,
1040 .n_errors = LB_N_ERROR,
1041 .error_strings = lb_error_strings,
1042 .n_next_nodes = LB_N_NEXT,
1044 { [LB_NEXT_DROP] = "error-drop" },
1047 VLIB_REGISTER_NODE (lb6_gre4_node) =
1049 .function = lb6_gre4_node_fn,
1051 .vector_size = sizeof(u32),
1052 .format_trace = format_lb_trace,
1053 .n_errors = LB_N_ERROR,
1054 .error_strings = lb_error_strings,
1055 .n_next_nodes = LB_N_NEXT,
1057 { [LB_NEXT_DROP] = "error-drop" },
1060 VLIB_REGISTER_NODE (lb4_gre6_node) =
1062 .function = lb4_gre6_node_fn,
1064 .vector_size = sizeof(u32),
1065 .format_trace = format_lb_trace,
1066 .n_errors = LB_N_ERROR,
1067 .error_strings = lb_error_strings,
1068 .n_next_nodes = LB_N_NEXT,
1070 { [LB_NEXT_DROP] = "error-drop" },
1073 VLIB_REGISTER_NODE (lb4_gre4_node) =
1075 .function = lb4_gre4_node_fn,
1077 .vector_size = sizeof(u32),
1078 .format_trace = format_lb_trace,
1079 .n_errors = LB_N_ERROR,
1080 .error_strings = lb_error_strings,
1081 .n_next_nodes = LB_N_NEXT,
1083 { [LB_NEXT_DROP] = "error-drop" },
1086 VLIB_REGISTER_NODE (lb6_gre6_port_node) =
1088 .function = lb6_gre6_port_node_fn,
1089 .name = "lb6-gre6-port",
1090 .vector_size = sizeof(u32),
1091 .format_trace = format_lb_trace,
1092 .n_errors = LB_N_ERROR,
1093 .error_strings = lb_error_strings,
1094 .n_next_nodes = LB_N_NEXT,
1096 { [LB_NEXT_DROP] = "error-drop" },
1099 VLIB_REGISTER_NODE (lb6_gre4_port_node) =
1101 .function = lb6_gre4_port_node_fn,
1102 .name = "lb6-gre4-port",
1103 .vector_size = sizeof(u32),
1104 .format_trace = format_lb_trace,
1105 .n_errors = LB_N_ERROR,
1106 .error_strings = lb_error_strings,
1107 .n_next_nodes = LB_N_NEXT,
1109 { [LB_NEXT_DROP] = "error-drop" },
1112 VLIB_REGISTER_NODE (lb4_gre6_port_node) =
1114 .function = lb4_gre6_port_node_fn,
1115 .name = "lb4-gre6-port",
1116 .vector_size = sizeof(u32),
1117 .format_trace = format_lb_trace,
1118 .n_errors = LB_N_ERROR,
1119 .error_strings = lb_error_strings,
1120 .n_next_nodes = LB_N_NEXT,
1122 { [LB_NEXT_DROP] = "error-drop" },
1125 VLIB_REGISTER_NODE (lb4_gre4_port_node) =
1127 .function = lb4_gre4_port_node_fn,
1128 .name = "lb4-gre4-port",
1129 .vector_size = sizeof(u32),
1130 .format_trace = format_lb_trace,
1131 .n_errors = LB_N_ERROR,
1132 .error_strings = lb_error_strings,
1133 .n_next_nodes = LB_N_NEXT,
1135 { [LB_NEXT_DROP] = "error-drop" },
1138 VLIB_REGISTER_NODE (lb4_l3dsr_port_node) =
1140 .function = lb4_l3dsr_port_node_fn,
1141 .name = "lb4-l3dsr-port",
1142 .vector_size = sizeof(u32),
1143 .format_trace = format_lb_trace,
1144 .n_errors = LB_N_ERROR,
1145 .error_strings = lb_error_strings,
1146 .n_next_nodes = LB_N_NEXT,
1148 { [LB_NEXT_DROP] = "error-drop" },
1151 VLIB_REGISTER_NODE (lb4_l3dsr_node) =
1153 .function = lb4_l3dsr_node_fn,
1154 .name = "lb4-l3dsr",
1155 .vector_size = sizeof(u32),
1156 .format_trace = format_lb_trace,
1157 .n_errors = LB_N_ERROR,
1158 .error_strings = lb_error_strings,
1159 .n_next_nodes = LB_N_NEXT,
1161 { [LB_NEXT_DROP] = "error-drop" },
1164 VLIB_REGISTER_NODE (lb6_nat6_port_node) =
1166 .function = lb6_nat6_port_node_fn,
1167 .name = "lb6-nat6-port",
1168 .vector_size = sizeof(u32),
1169 .format_trace = format_lb_trace,
1170 .n_errors = LB_N_ERROR,
1171 .error_strings = lb_error_strings,
1172 .n_next_nodes = LB_N_NEXT,
1174 { [LB_NEXT_DROP] = "error-drop" },
1177 VLIB_REGISTER_NODE (lb4_nat4_port_node) =
1179 .function = lb4_nat4_port_node_fn,
1180 .name = "lb4-nat4-port",
1181 .vector_size = sizeof(u32),
1182 .format_trace = format_lb_trace,
1183 .n_errors = LB_N_ERROR,
1184 .error_strings = lb_error_strings,
1185 .n_next_nodes = LB_N_NEXT,
1187 { [LB_NEXT_DROP] = "error-drop" },
1191 lb4_nodeport_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1192 vlib_frame_t * frame)
1194 return lb_nodeport_node_fn (vm, node, frame, 1);
1198 lb6_nodeport_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1199 vlib_frame_t * frame)
1201 return lb_nodeport_node_fn (vm, node, frame, 0);
1204 VLIB_REGISTER_NODE (lb4_nodeport_node) =
1206 .function = lb4_nodeport_node_fn,
1207 .name = "lb4-nodeport",
1208 .vector_size = sizeof(u32),
1209 .format_trace = format_nodeport_lb_trace,
1210 .n_errors = LB_N_ERROR,
1211 .error_strings = lb_error_strings,
1212 .n_next_nodes = LB4_NODEPORT_N_NEXT,
1215 [LB4_NODEPORT_NEXT_IP4_NAT4] = "lb4-nat4-port",
1216 [LB4_NODEPORT_NEXT_DROP] = "error-drop",
1220 VLIB_REGISTER_NODE (lb6_nodeport_node) =
1222 .function = lb6_nodeport_node_fn,
1223 .name = "lb6-nodeport",
1224 .vector_size = sizeof(u32),
1225 .format_trace = format_nodeport_lb_trace,
1226 .n_errors = LB_N_ERROR,
1227 .error_strings = lb_error_strings,
1228 .n_next_nodes = LB6_NODEPORT_N_NEXT,
1231 [LB6_NODEPORT_NEXT_IP6_NAT6] = "lb6-nat6-port",
1232 [LB6_NODEPORT_NEXT_DROP] = "error-drop",
1236 VNET_FEATURE_INIT (lb_nat4_in2out_node_fn, static) =
1238 .arc_name = "ip4-unicast",
1239 .node_name = "lb-nat4-in2out",
1240 .runs_before = VNET_FEATURES("ip4-lookup"),
1243 VLIB_REGISTER_NODE (lb_nat4_in2out_node) =
1245 .function = lb_nat4_in2out_node_fn,
1246 .name = "lb-nat4-in2out",
1247 .vector_size = sizeof(u32),
1248 .format_trace = format_lb_nat_trace,
1249 .n_errors = LB_N_ERROR,
1250 .error_strings = lb_error_strings,
1251 .n_next_nodes = LB_NAT4_IN2OUT_N_NEXT,
1254 [LB_NAT4_IN2OUT_NEXT_DROP] = "error-drop",
1255 [LB_NAT4_IN2OUT_NEXT_LOOKUP] = "ip4-lookup",
1259 VNET_FEATURE_INIT (lb_nat6_in2out_node_fn, static) =
1261 .arc_name = "ip6-unicast",
1262 .node_name = "lb-nat6-in2out",
1263 .runs_before = VNET_FEATURES("ip6-lookup"),
1266 VLIB_REGISTER_NODE (lb_nat6_in2out_node) =
1268 .function = lb_nat6_in2out_node_fn,
1269 .name = "lb-nat6-in2out",
1270 .vector_size = sizeof(u32),
1271 .format_trace = format_lb_nat_trace,
1272 .n_errors = LB_N_ERROR,
1273 .error_strings = lb_error_strings,
1274 .n_next_nodes = LB_NAT6_IN2OUT_N_NEXT,
1277 [LB_NAT6_IN2OUT_NEXT_DROP] = "error-drop",
1278 [LB_NAT6_IN2OUT_NEXT_LOOKUP] = "ip6-lookup",