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 (const ip_adjacency_t *adj)
83 return ip4_main.lookup_main.output_feature_arc_index;
85 return ip6_main.lookup_main.output_feature_arc_index;
87 return mpls_main.output_feature_arc_index;
88 case VNET_LINK_ETHERNET:
89 return ethernet_main.output_feature_arc_index;
99 adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
101 return (adj_midchain_tx.index);
105 adj_nbr_midchain_get_next_node (ip_adjacency_t *adj)
107 return (vnet_feature_get_end_node(adj_midchain_get_feature_arc_index(adj),
108 adj->rewrite_header.sw_if_index));
114 * Setup the adj as a mid-chain
117 adj_midchain_teardown (ip_adjacency_t *adj)
119 dpo_reset(&adj->sub_type.midchain.next_dpo);
125 * Setup the adj as a mid-chain
128 adj_midchain_setup (adj_index_t adj_index,
129 adj_midchain_fixup_t fixup,
135 ASSERT(ADJ_INDEX_INVALID != adj_index);
137 adj = adj_get(adj_index);
139 adj->sub_type.midchain.fixup_func = fixup;
140 adj->sub_type.midchain.fixup_data = data;
141 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
142 adj->ia_flags |= flags;
144 if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
146 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
150 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
152 if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
154 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
158 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
159 * The graph arc used/created here is from the midchain-tx node to the
160 * child's registered node. This is because post adj processing the next
161 * node are any output features, then the midchain-tx. from there we
162 * need to get to the stacked child's node.
164 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
165 &adj->sub_type.midchain.next_dpo,
166 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
170 * adj_nbr_midchain_update_rewrite
172 * Update the adjacency's rewrite string. A NULL string implies the
173 * rewrite is reset (i.e. when ARP/ND entry is gone).
174 * NB: the adj being updated may be handling traffic in the DP.
177 adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
178 adj_midchain_fixup_t fixup,
179 const void *fixup_data,
185 ASSERT(ADJ_INDEX_INVALID != adj_index);
187 adj = adj_get(adj_index);
190 * one time only update. since we don't support changing the tunnel
191 * src,dst, this is all we need.
193 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
194 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
196 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
200 * update the rewrite with the workers paused.
202 adj_nbr_update_rewrite_internal(adj,
203 IP_LOOKUP_NEXT_MIDCHAIN,
204 adj_get_midchain_node(adj->ia_link),
205 adj_nbr_midchain_get_next_node(adj),
210 adj_nbr_midchain_update_next_node (adj_index_t adj_index,
216 ASSERT(ADJ_INDEX_INVALID != adj_index);
218 adj = adj_get(adj_index);
219 vm = vlib_get_main();
221 vlib_worker_thread_barrier_sync(vm);
223 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
227 vlib_worker_thread_barrier_release(vm);
231 adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
236 ASSERT(ADJ_INDEX_INVALID != adj_index);
238 adj = adj_get(adj_index);
239 vm = vlib_get_main();
241 vlib_worker_thread_barrier_sync(vm);
243 adj->rewrite_header.next_index =
244 vlib_node_add_next(vlib_get_main(),
246 adj_nbr_midchain_get_next_node(adj));
248 vlib_worker_thread_barrier_release(vm);
252 * adj_nbr_midchain_unstack
254 * Unstack the adj. stack it on drop
257 adj_nbr_midchain_unstack (adj_index_t adj_index)
259 fib_node_index_t *entry_indicies, tmp;
262 ASSERT(ADJ_INDEX_INVALID != adj_index);
263 adj = adj_get (adj_index);
266 * check to see if this unstacking breaks a recursion loop
268 entry_indicies = NULL;
269 tmp = adj->sub_type.midchain.fei;
270 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
272 if (FIB_NODE_INDEX_INVALID != tmp)
274 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
275 vec_free(entry_indicies);
281 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
282 vnet_link_to_dpo_proto(adj->ia_link),
283 &adj->sub_type.midchain.next_dpo,
284 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
285 CLIB_MEMORY_BARRIER();
289 adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
290 fib_node_index_t fei,
291 fib_forward_chain_type_t fct)
293 fib_node_index_t *entry_indicies;
294 dpo_id_t tmp = DPO_INVALID;
300 * check to see if this stacking will form a recursion loop
302 entry_indicies = NULL;
303 adj->sub_type.midchain.fei = fei;
305 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
308 * loop formed, stack on the drop.
310 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
314 fib_entry_contribute_forwarding (fei, fct, &tmp);
316 if (DPO_LOAD_BALANCE == tmp.dpoi_type)
320 lb = load_balance_get (tmp.dpoi_index);
322 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
323 lb->lb_n_buckets == 1)
326 * do that hash now and stack on the choice.
327 * If the choice is an incomplete adj then we will need a poke when
328 * it becomes complete. This happens since the adj update walk propagates
329 * as far a recursive paths.
331 const dpo_id_t *choice;
334 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
336 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
339 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
341 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
350 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
351 dpo_copy (&tmp, choice);
353 else if (lb->lb_n_buckets > 1)
356 * the client has chosen not to use the stacking to select a
357 * bucket, and there are more than one buckets. there's no
358 * value in using the midchain's fixed rewrite string to select
359 * the path, so force a flow hash on the inner.
361 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
364 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
367 * The client, for reasons unbeknownst to adj, wants to force
368 * a flow hash on the inner, we will oblige.
370 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
374 adj_nbr_midchain_stack (ai, &tmp);
376 vec_free(entry_indicies);
380 * adj_nbr_midchain_stack
383 adj_nbr_midchain_stack (adj_index_t adj_index,
384 const dpo_id_t *next)
388 ASSERT(ADJ_INDEX_INVALID != adj_index);
390 adj = adj_get(adj_index);
392 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
393 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
395 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
396 &adj->sub_type.midchain.next_dpo,
401 adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
402 fib_node_index_t **entry_indicies)
404 fib_node_index_t *entry_index, *entries;
405 ip_adjacency_t * adj;
408 entries = *entry_indicies;
410 vec_foreach(entry_index, entries)
412 if (*entry_index == adj->sub_type.midchain.fei)
415 * The entry this midchain links to is already in the set
416 * of visited entries, this is a loop
418 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
423 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
428 format_adj_midchain (u8* s, va_list *ap)
430 index_t index = va_arg(*ap, index_t);
431 u32 indent = va_arg(*ap, u32);
432 ip_adjacency_t * adj = adj_get(index);
434 s = format (s, "%U", format_vnet_link, adj->ia_link);
435 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
436 s = format(s, " [features]");
437 s = format (s, " via %U",
438 format_ip46_address, &adj->sub_type.nbr.next_hop,
439 adj_proto_to_46(adj->ia_nh_proto));
440 s = format (s, " %U",
442 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
443 s = format (s, "\n%Ustacked-on",
444 format_white_space, indent);
446 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
448 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
451 s = format (s, ":\n%U%U",
452 format_white_space, indent+2,
453 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
459 adj_dpo_lock (dpo_id_t *dpo)
461 adj_lock(dpo->dpoi_index);
464 adj_dpo_unlock (dpo_id_t *dpo)
466 adj_unlock(dpo->dpoi_index);
469 const static dpo_vft_t adj_midchain_dpo_vft = {
470 .dv_lock = adj_dpo_lock,
471 .dv_unlock = adj_dpo_unlock,
472 .dv_format = format_adj_midchain,
473 .dv_get_urpf = adj_dpo_get_urpf,
474 .dv_get_mtu = adj_dpo_get_mtu,
478 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
481 * this means that these graph nodes are ones from which a midchain is the
482 * parent object in the DPO-graph.
484 const static char* const midchain_ip4_nodes[] =
489 const static char* const midchain_ip6_nodes[] =
494 const static char* const midchain_mpls_nodes[] =
499 const static char* const midchain_ethernet_nodes[] =
504 const static char* const midchain_nsh_nodes[] =
510 const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
512 [DPO_PROTO_IP4] = midchain_ip4_nodes,
513 [DPO_PROTO_IP6] = midchain_ip6_nodes,
514 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
515 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
516 [DPO_PROTO_NSH] = midchain_nsh_nodes,
520 adj_midchain_module_init (void)
522 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);