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 <vnet/adj/adj_nbr.h>
17 #include <vnet/adj/adj_internal.h>
18 #include <vnet/adj/adj_l2.h>
19 #include <vnet/adj/adj_nsh.h>
20 #include <vnet/adj/adj_midchain.h>
21 #include <vnet/ethernet/arp_packet.h>
22 #include <vnet/dpo/drop_dpo.h>
23 #include <vnet/dpo/load_balance.h>
24 #include <vnet/fib/fib_walk.h>
25 #include <vnet/fib/fib_entry.h>
28 * @brief Trace data for packets traversing the midchain tx node
30 typedef struct adj_midchain_tx_trace_t_
33 * @brief the midchain adj we are traversing
36 } adj_midchain_tx_trace_t;
39 adj_midchain_tx_inline (vlib_main_t * vm,
40 vlib_node_runtime_t * node,
44 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
45 u16 nexts[VLIB_FRAME_SIZE], *next;
46 u32 * from, n_left, thread_index;
47 vnet_main_t *vnm = vnet_get_main ();
48 vnet_interface_main_t *im = &vnm->interface_main;
50 thread_index = vm->thread_index;
51 n_left = frame->n_vectors;
52 from = vlib_frame_vector_args (frame);
54 vlib_get_buffers (vm, from, bufs, n_left);
61 u32 adj_index0, adj_index1, adj_index2, adj_index3;
62 const ip_adjacency_t *adj0, *adj1, *adj2, *adj3;
63 const dpo_id_t *dpo0, *dpo1, *dpo2, *dpo3;
65 /* Prefetch next iteration. */
67 vlib_prefetch_buffer_header (b[4], LOAD);
68 vlib_prefetch_buffer_header (b[5], LOAD);
69 vlib_prefetch_buffer_header (b[6], LOAD);
70 vlib_prefetch_buffer_header (b[7], LOAD);
73 /* Follow the DPO on which the midchain is stacked */
74 adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
75 adj_index1 = vnet_buffer(b[1])->ip.adj_index[VLIB_TX];
76 adj_index2 = vnet_buffer(b[2])->ip.adj_index[VLIB_TX];
77 adj_index3 = vnet_buffer(b[3])->ip.adj_index[VLIB_TX];
79 adj0 = adj_get(adj_index0);
80 adj1 = adj_get(adj_index1);
81 adj2 = adj_get(adj_index2);
82 adj3 = adj_get(adj_index3);
84 dpo0 = &adj0->sub_type.midchain.next_dpo;
85 dpo1 = &adj1->sub_type.midchain.next_dpo;
86 dpo2 = &adj2->sub_type.midchain.next_dpo;
87 dpo3 = &adj3->sub_type.midchain.next_dpo;
89 next[0] = dpo0->dpoi_next_node;
90 next[1] = dpo1->dpoi_next_node;
91 next[2] = dpo2->dpoi_next_node;
92 next[3] = dpo3->dpoi_next_node;
94 vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
95 vnet_buffer(b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
96 vnet_buffer(b[2])->ip.adj_index[VLIB_TX] = dpo2->dpoi_index;
97 vnet_buffer(b[3])->ip.adj_index[VLIB_TX] = dpo3->dpoi_index;
101 vlib_increment_combined_counter (im->combined_sw_if_counters
102 + VNET_INTERFACE_COUNTER_TX,
104 adj0->rewrite_header.sw_if_index,
106 vlib_buffer_length_in_chain (vm, b[0]));
107 vlib_increment_combined_counter (im->combined_sw_if_counters
108 + VNET_INTERFACE_COUNTER_TX,
110 adj1->rewrite_header.sw_if_index,
112 vlib_buffer_length_in_chain (vm, b[1]));
113 vlib_increment_combined_counter (im->combined_sw_if_counters
114 + VNET_INTERFACE_COUNTER_TX,
116 adj2->rewrite_header.sw_if_index,
118 vlib_buffer_length_in_chain (vm, b[2]));
119 vlib_increment_combined_counter (im->combined_sw_if_counters
120 + VNET_INTERFACE_COUNTER_TX,
122 adj3->rewrite_header.sw_if_index,
124 vlib_buffer_length_in_chain (vm, b[3]));
127 if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
129 if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
131 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
135 if (PREDICT_FALSE(b[1]->flags & VLIB_BUFFER_IS_TRACED))
137 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
141 if (PREDICT_FALSE(b[2]->flags & VLIB_BUFFER_IS_TRACED))
143 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
147 if (PREDICT_FALSE(b[3]->flags & VLIB_BUFFER_IS_TRACED))
149 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
161 const ip_adjacency_t * adj0;
162 const dpo_id_t *dpo0;
165 /* Follow the DPO on which the midchain is stacked */
166 adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
167 adj0 = adj_get(adj_index0);
168 dpo0 = &adj0->sub_type.midchain.next_dpo;
169 next[0] = dpo0->dpoi_next_node;
170 vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
174 vlib_increment_combined_counter (im->combined_sw_if_counters
175 + VNET_INTERFACE_COUNTER_TX,
177 adj0->rewrite_header.sw_if_index,
179 vlib_buffer_length_in_chain (vm, b[0]));
182 if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
184 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
194 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
196 return frame->n_vectors;
200 format_adj_midchain_tx_trace (u8 * s, va_list * args)
202 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
203 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
204 adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
206 s = format(s, "adj-midchain:[%d]:%U", tr->ai,
207 format_ip_adjacency, tr->ai,
208 FORMAT_IP_ADJACENCY_NONE);
214 adj_midchain_tx (vlib_main_t * vm,
215 vlib_node_runtime_t * node,
216 vlib_frame_t * frame)
218 return (adj_midchain_tx_inline(vm, node, frame, 1));
221 VLIB_REGISTER_NODE (adj_midchain_tx_node) = {
222 .function = adj_midchain_tx,
223 .name = "adj-midchain-tx",
224 .vector_size = sizeof (u32),
226 .format_trace = format_adj_midchain_tx_trace,
235 adj_midchain_tx_no_count (vlib_main_t * vm,
236 vlib_node_runtime_t * node,
237 vlib_frame_t * frame)
239 return (adj_midchain_tx_inline(vm, node, frame, 0));
242 VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node) = {
243 .function = adj_midchain_tx_no_count,
244 .name = "adj-midchain-tx-no-count",
245 .vector_size = sizeof (u32),
247 .format_trace = format_adj_midchain_tx_trace,
248 .sibling_of = "adj-midchain-tx",
251 #ifndef CLIB_MARCH_VARIANT
254 adj_is_midchain (adj_index_t ai)
260 switch (adj->lookup_next_index)
262 case IP_LOOKUP_NEXT_MIDCHAIN:
263 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
265 case IP_LOOKUP_NEXT_ARP:
266 case IP_LOOKUP_NEXT_GLEAN:
267 case IP_LOOKUP_NEXT_BCAST:
268 case IP_LOOKUP_NEXT_MCAST:
269 case IP_LOOKUP_NEXT_DROP:
270 case IP_LOOKUP_NEXT_PUNT:
271 case IP_LOOKUP_NEXT_LOCAL:
272 case IP_LOOKUP_NEXT_REWRITE:
273 case IP_LOOKUP_NEXT_ICMP_ERROR:
274 case IP_LOOKUP_N_NEXT:
282 adj_get_midchain_node (vnet_link_t link)
286 return (ip4_midchain_node.index);
288 return (ip6_midchain_node.index);
290 return (mpls_midchain_node.index);
291 case VNET_LINK_ETHERNET:
292 return (adj_l2_midchain_node.index);
294 return (adj_nsh_midchain_node.index);
303 adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
306 switch (adj->ia_link)
310 arc = ip4_main.lookup_main.output_feature_arc_index;
315 arc = ip6_main.lookup_main.output_feature_arc_index;
320 arc = mpls_main.output_feature_arc_index;
323 case VNET_LINK_ETHERNET:
325 arc = ethernet_main.output_feature_arc_index;
330 arc = nsh_main_dummy.output_feature_arc_index;
338 ASSERT (arc != (u8) ~0);
344 adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
346 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
347 adj_midchain_tx_no_count_node.index :
348 adj_midchain_tx_node.index);
354 * Setup the adj as a mid-chain
357 adj_midchain_teardown (ip_adjacency_t *adj)
359 vlib_main_t *vm = vlib_get_main();
361 dpo_reset(&adj->sub_type.midchain.next_dpo);
363 vlib_worker_thread_barrier_sync(vm);
364 adj->ia_cfg_index = vnet_feature_modify_end_node(
365 adj_midchain_get_feature_arc_index_for_link_type (adj),
366 adj->rewrite_header.sw_if_index,
367 vlib_get_node_by_name (vlib_get_main(),
368 (u8*) "interface-output")->index);
369 vlib_worker_thread_barrier_release(vm);
375 * Setup the adj as a mid-chain
378 adj_midchain_setup (adj_index_t adj_index,
379 adj_midchain_fixup_t fixup,
383 vlib_main_t *vm = vlib_get_main();
387 ASSERT(ADJ_INDEX_INVALID != adj_index);
389 adj = adj_get(adj_index);
391 adj->sub_type.midchain.fixup_func = fixup;
392 adj->sub_type.midchain.fixup_data = data;
393 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
394 adj->ia_flags |= flags;
396 if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
398 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
402 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
405 tx_node = adj_nbr_midchain_get_tx_node(adj);
407 vlib_worker_thread_barrier_sync(vm);
408 adj->ia_cfg_index = vnet_feature_modify_end_node(
409 adj_midchain_get_feature_arc_index_for_link_type (adj),
410 adj->rewrite_header.sw_if_index,
412 vlib_worker_thread_barrier_release(vm);
415 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
416 * The graph arc used/created here is from the midchain-tx node to the
417 * child's registered node. This is because post adj processing the next
418 * node are any output features, then the midchain-tx. from there we
419 * need to get to the stacked child's node.
421 dpo_stack_from_node(tx_node,
422 &adj->sub_type.midchain.next_dpo,
423 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
427 * adj_nbr_midchain_update_rewrite
429 * Update the adjacency's rewrite string. A NULL string implies the
430 * rewrite is reset (i.e. when ARP/ND entry is gone).
431 * NB: the adj being updated may be handling traffic in the DP.
434 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
435 adj_midchain_fixup_t fixup,
436 const void *fixup_data,
442 ASSERT(ADJ_INDEX_INVALID != adj_index);
444 adj = adj_get(adj_index);
447 * one time only update. since we don't support changing the tunnel
448 * src,dst, this is all we need.
450 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
451 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
453 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
457 * update the rewrite with the workers paused.
459 adj_nbr_update_rewrite_internal(adj,
460 IP_LOOKUP_NEXT_MIDCHAIN,
461 adj_get_midchain_node(adj->ia_link),
462 adj_nbr_midchain_get_tx_node(adj),
467 adj_nbr_midchain_update_next_node (adj_index_t adj_index,
473 ASSERT(ADJ_INDEX_INVALID != adj_index);
475 adj = adj_get(adj_index);
476 vm = vlib_get_main();
478 vlib_worker_thread_barrier_sync(vm);
480 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
484 adj->ia_cfg_index = vnet_feature_modify_end_node(
485 adj_midchain_get_feature_arc_index_for_link_type (adj),
486 adj->rewrite_header.sw_if_index,
489 vlib_worker_thread_barrier_release(vm);
493 adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
498 ASSERT(ADJ_INDEX_INVALID != adj_index);
500 adj = adj_get(adj_index);
501 vm = vlib_get_main();
503 vlib_worker_thread_barrier_sync(vm);
505 adj->rewrite_header.next_index =
506 vlib_node_add_next(vlib_get_main(),
508 adj_nbr_midchain_get_tx_node(adj));
510 adj->ia_cfg_index = vnet_feature_modify_end_node(
511 adj_midchain_get_feature_arc_index_for_link_type (adj),
512 adj->rewrite_header.sw_if_index,
513 adj_nbr_midchain_get_tx_node(adj));
515 vlib_worker_thread_barrier_release(vm);
519 * adj_nbr_midchain_unstack
521 * Unstack the adj. stack it on drop
524 adj_nbr_midchain_unstack (adj_index_t adj_index)
526 fib_node_index_t *entry_indicies, tmp;
529 ASSERT(ADJ_INDEX_INVALID != adj_index);
530 adj = adj_get (adj_index);
533 * check to see if this unstacking breaks a recursion loop
535 entry_indicies = NULL;
536 tmp = adj->sub_type.midchain.fei;
537 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
539 if (FIB_NODE_INDEX_INVALID != tmp)
541 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
542 vec_free(entry_indicies);
548 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
549 vnet_link_to_dpo_proto(adj->ia_link),
550 &adj->sub_type.midchain.next_dpo,
551 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
552 CLIB_MEMORY_BARRIER();
556 adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
557 fib_node_index_t fei,
558 fib_forward_chain_type_t fct)
560 fib_node_index_t *entry_indicies;
561 dpo_id_t tmp = DPO_INVALID;
567 * check to see if this stacking will form a recursion loop
569 entry_indicies = NULL;
570 adj->sub_type.midchain.fei = fei;
572 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
575 * loop formed, stack on the drop.
577 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
581 fib_entry_contribute_forwarding (fei, fct, &tmp);
583 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) &&
584 (DPO_LOAD_BALANCE == tmp.dpoi_type))
587 * do that hash now and stack on the choice.
588 * If the choice is an incomplete adj then we will need a poke when
589 * it becomes complete. This happens since the adj update walk propagates
590 * as far a recursive paths.
592 const dpo_id_t *choice;
596 lb = load_balance_get (tmp.dpoi_index);
598 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
600 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
603 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
605 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
614 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
615 dpo_copy (&tmp, choice);
618 adj_nbr_midchain_stack (ai, &tmp);
620 vec_free(entry_indicies);
624 * adj_nbr_midchain_stack
627 adj_nbr_midchain_stack (adj_index_t adj_index,
628 const dpo_id_t *next)
632 ASSERT(ADJ_INDEX_INVALID != adj_index);
634 adj = adj_get(adj_index);
636 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
637 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
639 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
640 &adj->sub_type.midchain.next_dpo,
645 adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
646 fib_node_index_t **entry_indicies)
648 fib_node_index_t *entry_index, *entries;
649 ip_adjacency_t * adj;
652 entries = *entry_indicies;
654 vec_foreach(entry_index, entries)
656 if (*entry_index == adj->sub_type.midchain.fei)
659 * The entry this midchain links to is already in the set
660 * of visited entries, this is a loop
662 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
667 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
672 format_adj_midchain (u8* s, va_list *ap)
674 index_t index = va_arg(*ap, index_t);
675 u32 indent = va_arg(*ap, u32);
676 ip_adjacency_t * adj = adj_get(index);
678 s = format (s, "%U", format_vnet_link, adj->ia_link);
679 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
680 s = format(s, " [features]");
681 s = format (s, " via %U",
682 format_ip46_address, &adj->sub_type.nbr.next_hop,
683 adj_proto_to_46(adj->ia_nh_proto));
684 s = format (s, " %U",
686 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
687 s = format (s, "\n%Ustacked-on",
688 format_white_space, indent);
690 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
692 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
695 s = format (s, ":\n%U%U",
696 format_white_space, indent+2,
697 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
703 adj_dpo_lock (dpo_id_t *dpo)
705 adj_lock(dpo->dpoi_index);
708 adj_dpo_unlock (dpo_id_t *dpo)
710 adj_unlock(dpo->dpoi_index);
713 const static dpo_vft_t adj_midchain_dpo_vft = {
714 .dv_lock = adj_dpo_lock,
715 .dv_unlock = adj_dpo_unlock,
716 .dv_format = format_adj_midchain,
717 .dv_get_urpf = adj_dpo_get_urpf,
721 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
724 * this means that these graph nodes are ones from which a midchain is the
725 * parent object in the DPO-graph.
727 const static char* const midchain_ip4_nodes[] =
732 const static char* const midchain_ip6_nodes[] =
737 const static char* const midchain_mpls_nodes[] =
742 const static char* const midchain_ethernet_nodes[] =
747 const static char* const midchain_nsh_nodes[] =
753 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
755 [DPO_PROTO_IP4] = midchain_ip4_nodes,
756 [DPO_PROTO_IP6] = midchain_ip6_nodes,
757 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
758 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
759 [DPO_PROTO_NSH] = midchain_nsh_nodes,
763 adj_midchain_module_init (void)
765 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);