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/dpo/drop_dpo.h>
22 #include <vnet/dpo/load_balance.h>
23 #include <vnet/fib/fib_walk.h>
24 #include <vnet/fib/fib_entry.h>
25 #include <vnet/ip/ip4_inlines.h>
26 #include <vnet/ip/ip6_inlines.h>
29 adj_is_midchain (adj_index_t ai)
35 switch (adj->lookup_next_index)
37 case IP_LOOKUP_NEXT_MIDCHAIN:
38 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
40 case IP_LOOKUP_NEXT_ARP:
41 case IP_LOOKUP_NEXT_GLEAN:
42 case IP_LOOKUP_NEXT_BCAST:
43 case IP_LOOKUP_NEXT_MCAST:
44 case IP_LOOKUP_NEXT_DROP:
45 case IP_LOOKUP_NEXT_PUNT:
46 case IP_LOOKUP_NEXT_LOCAL:
47 case IP_LOOKUP_NEXT_REWRITE:
48 case IP_LOOKUP_NEXT_ICMP_ERROR:
49 case IP_LOOKUP_N_NEXT:
57 adj_get_midchain_node (vnet_link_t link)
61 return (ip4_midchain_node.index);
63 return (ip6_midchain_node.index);
65 return (mpls_midchain_node.index);
66 case VNET_LINK_ETHERNET:
67 return (adj_l2_midchain_node.index);
69 return (adj_nsh_midchain_node.index);
78 adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
85 arc = ip4_main.lookup_main.output_feature_arc_index;
90 arc = ip6_main.lookup_main.output_feature_arc_index;
95 arc = mpls_main.output_feature_arc_index;
98 case VNET_LINK_ETHERNET:
100 arc = ethernet_main.output_feature_arc_index;
105 arc = nsh_main_placeholder.output_feature_arc_index;
113 ASSERT (arc != (u8) ~0);
119 adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
121 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
122 adj_midchain_tx_no_count_node.index :
123 adj_midchain_tx_node.index);
129 * Setup the adj as a mid-chain
132 adj_midchain_teardown (ip_adjacency_t *adj)
134 vlib_main_t *vm = vlib_get_main();
136 dpo_reset(&adj->sub_type.midchain.next_dpo);
138 vlib_worker_thread_barrier_sync(vm);
139 adj->ia_cfg_index = vnet_feature_modify_end_node(
140 adj_midchain_get_feature_arc_index_for_link_type (adj),
141 adj->rewrite_header.sw_if_index,
142 vlib_get_node_by_name (vlib_get_main(),
143 (u8*) "interface-output")->index);
144 vlib_worker_thread_barrier_release(vm);
150 * Setup the adj as a mid-chain
153 adj_midchain_setup (adj_index_t adj_index,
154 adj_midchain_fixup_t fixup,
158 vlib_main_t *vm = vlib_get_main();
162 ASSERT(ADJ_INDEX_INVALID != adj_index);
164 adj = adj_get(adj_index);
166 adj->sub_type.midchain.fixup_func = fixup;
167 adj->sub_type.midchain.fixup_data = data;
168 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
169 adj->ia_flags |= flags;
171 if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
173 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
177 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
179 if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
181 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
184 tx_node = adj_nbr_midchain_get_tx_node(adj);
186 vlib_worker_thread_barrier_sync(vm);
187 adj->ia_cfg_index = vnet_feature_modify_end_node(
188 adj_midchain_get_feature_arc_index_for_link_type (adj),
189 adj->rewrite_header.sw_if_index,
191 vlib_worker_thread_barrier_release(vm);
194 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
195 * The graph arc used/created here is from the midchain-tx node to the
196 * child's registered node. This is because post adj processing the next
197 * node are any output features, then the midchain-tx. from there we
198 * need to get to the stacked child's node.
200 dpo_stack_from_node(tx_node,
201 &adj->sub_type.midchain.next_dpo,
202 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
206 * adj_nbr_midchain_update_rewrite
208 * Update the adjacency's rewrite string. A NULL string implies the
209 * rewrite is reset (i.e. when ARP/ND entry is gone).
210 * NB: the adj being updated may be handling traffic in the DP.
213 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
214 adj_midchain_fixup_t fixup,
215 const void *fixup_data,
221 ASSERT(ADJ_INDEX_INVALID != adj_index);
223 adj = adj_get(adj_index);
226 * one time only update. since we don't support changing the tunnel
227 * src,dst, this is all we need.
229 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
230 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
232 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
236 * update the rewrite with the workers paused.
238 adj_nbr_update_rewrite_internal(adj,
239 IP_LOOKUP_NEXT_MIDCHAIN,
240 adj_get_midchain_node(adj->ia_link),
241 adj_nbr_midchain_get_tx_node(adj),
246 adj_nbr_midchain_update_next_node (adj_index_t adj_index,
252 ASSERT(ADJ_INDEX_INVALID != adj_index);
254 adj = adj_get(adj_index);
255 vm = vlib_get_main();
257 vlib_worker_thread_barrier_sync(vm);
259 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
263 adj->ia_cfg_index = vnet_feature_modify_end_node(
264 adj_midchain_get_feature_arc_index_for_link_type (adj),
265 adj->rewrite_header.sw_if_index,
268 vlib_worker_thread_barrier_release(vm);
272 adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
277 ASSERT(ADJ_INDEX_INVALID != adj_index);
279 adj = adj_get(adj_index);
280 vm = vlib_get_main();
282 vlib_worker_thread_barrier_sync(vm);
284 adj->rewrite_header.next_index =
285 vlib_node_add_next(vlib_get_main(),
287 adj_nbr_midchain_get_tx_node(adj));
289 adj->ia_cfg_index = vnet_feature_modify_end_node(
290 adj_midchain_get_feature_arc_index_for_link_type (adj),
291 adj->rewrite_header.sw_if_index,
292 adj_nbr_midchain_get_tx_node(adj));
294 vlib_worker_thread_barrier_release(vm);
298 * adj_nbr_midchain_unstack
300 * Unstack the adj. stack it on drop
303 adj_nbr_midchain_unstack (adj_index_t adj_index)
305 fib_node_index_t *entry_indicies, tmp;
308 ASSERT(ADJ_INDEX_INVALID != adj_index);
309 adj = adj_get (adj_index);
312 * check to see if this unstacking breaks a recursion loop
314 entry_indicies = NULL;
315 tmp = adj->sub_type.midchain.fei;
316 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
318 if (FIB_NODE_INDEX_INVALID != tmp)
320 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
321 vec_free(entry_indicies);
327 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
328 vnet_link_to_dpo_proto(adj->ia_link),
329 &adj->sub_type.midchain.next_dpo,
330 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
331 CLIB_MEMORY_BARRIER();
335 adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
336 fib_node_index_t fei,
337 fib_forward_chain_type_t fct)
339 fib_node_index_t *entry_indicies;
340 dpo_id_t tmp = DPO_INVALID;
346 * check to see if this stacking will form a recursion loop
348 entry_indicies = NULL;
349 adj->sub_type.midchain.fei = fei;
351 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
354 * loop formed, stack on the drop.
356 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
360 fib_entry_contribute_forwarding (fei, fct, &tmp);
362 if (DPO_LOAD_BALANCE == tmp.dpoi_type)
366 lb = load_balance_get (tmp.dpoi_index);
368 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
369 lb->lb_n_buckets == 1)
372 * do that hash now and stack on the choice.
373 * If the choice is an incomplete adj then we will need a poke when
374 * it becomes complete. This happens since the adj update walk propagates
375 * as far a recursive paths.
377 const dpo_id_t *choice;
380 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
382 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
385 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
387 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
396 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
397 dpo_copy (&tmp, choice);
399 else if (lb->lb_n_buckets > 1)
402 * the client has chosen not to use the stacking to select a
403 * bucket, and there are more than one buckets. there's no
404 * value in using the midchain's fixed rewrite string to select
405 * the path, so force a flow hash on the inner.
407 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
410 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
413 * The client, for reasons unbeknownst to adj, wants to force
414 * a flow hash on the inner, we will oblige.
416 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
420 adj_nbr_midchain_stack (ai, &tmp);
422 vec_free(entry_indicies);
426 * adj_nbr_midchain_stack
429 adj_nbr_midchain_stack (adj_index_t adj_index,
430 const dpo_id_t *next)
434 ASSERT(ADJ_INDEX_INVALID != adj_index);
436 adj = adj_get(adj_index);
438 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
439 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
441 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
442 &adj->sub_type.midchain.next_dpo,
447 adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
448 fib_node_index_t **entry_indicies)
450 fib_node_index_t *entry_index, *entries;
451 ip_adjacency_t * adj;
454 entries = *entry_indicies;
456 vec_foreach(entry_index, entries)
458 if (*entry_index == adj->sub_type.midchain.fei)
461 * The entry this midchain links to is already in the set
462 * of visited entries, this is a loop
464 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
469 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
474 format_adj_midchain (u8* s, va_list *ap)
476 index_t index = va_arg(*ap, index_t);
477 u32 indent = va_arg(*ap, u32);
478 ip_adjacency_t * adj = adj_get(index);
480 s = format (s, "%U", format_vnet_link, adj->ia_link);
481 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
482 s = format(s, " [features]");
483 s = format (s, " via %U",
484 format_ip46_address, &adj->sub_type.nbr.next_hop,
485 adj_proto_to_46(adj->ia_nh_proto));
486 s = format (s, " %U",
488 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
489 s = format (s, "\n%Ustacked-on",
490 format_white_space, indent);
492 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
494 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
497 s = format (s, ":\n%U%U",
498 format_white_space, indent+2,
499 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
505 adj_dpo_lock (dpo_id_t *dpo)
507 adj_lock(dpo->dpoi_index);
510 adj_dpo_unlock (dpo_id_t *dpo)
512 adj_unlock(dpo->dpoi_index);
515 const static dpo_vft_t adj_midchain_dpo_vft = {
516 .dv_lock = adj_dpo_lock,
517 .dv_unlock = adj_dpo_unlock,
518 .dv_format = format_adj_midchain,
519 .dv_get_urpf = adj_dpo_get_urpf,
520 .dv_get_mtu = adj_dpo_get_mtu,
524 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
527 * this means that these graph nodes are ones from which a midchain is the
528 * parent object in the DPO-graph.
530 const static char* const midchain_ip4_nodes[] =
535 const static char* const midchain_ip6_nodes[] =
540 const static char* const midchain_mpls_nodes[] =
545 const static char* const midchain_ethernet_nodes[] =
550 const static char* const midchain_nsh_nodes[] =
556 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
558 [DPO_PROTO_IP4] = midchain_ip4_nodes,
559 [DPO_PROTO_IP6] = midchain_ip6_nodes,
560 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
561 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
562 [DPO_PROTO_NSH] = midchain_nsh_nodes,
566 adj_midchain_module_init (void)
568 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);