blob: 9f709ad13be8e20b5b1e655b5864f88740c05bc8 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/adj/adj_nbr.h>
17#include <vnet/adj/adj_internal.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010018#include <vnet/adj/adj_l2.h>
Florin Corasce1b4c72017-01-26 14:25:34 -080019#include <vnet/adj/adj_nsh.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010020#include <vnet/adj/adj_midchain.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010021#include <vnet/dpo/drop_dpo.h>
Neale Ranns521a8d72018-12-06 13:46:49 +000022#include <vnet/dpo/load_balance.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/fib/fib_walk.h>
Neale Ranns521a8d72018-12-06 13:46:49 +000024#include <vnet/fib/fib_entry.h>
Neale Rannse4031132020-10-26 13:00:06 +000025#include <vnet/ip/ip4_inlines.h>
26#include <vnet/ip/ip6_inlines.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010027
Neale Ranns4ec36c52020-03-31 09:21:29 -040028u8
29adj_is_midchain (adj_index_t ai)
30{
31 ip_adjacency_t *adj;
32
33 adj = adj_get(ai);
34
35 switch (adj->lookup_next_index)
36 {
37 case IP_LOOKUP_NEXT_MIDCHAIN:
38 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
39 return (1);
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:
50 return (0);
51 }
52
53 return (0);
54}
Neale Ranns5e575b12016-10-03 09:40:25 +010055
Neale Ranns0bfe5d82016-08-25 15:29:12 +010056static inline u32
Neale Ranns924d03a2016-10-19 08:25:46 +010057adj_get_midchain_node (vnet_link_t link)
Neale Ranns0bfe5d82016-08-25 15:29:12 +010058{
59 switch (link) {
Neale Ranns924d03a2016-10-19 08:25:46 +010060 case VNET_LINK_IP4:
Neale Ranns0bfe5d82016-08-25 15:29:12 +010061 return (ip4_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +010062 case VNET_LINK_IP6:
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063 return (ip6_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +010064 case VNET_LINK_MPLS:
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065 return (mpls_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +010066 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +010067 return (adj_l2_midchain_node.index);
Florin Corasce1b4c72017-01-26 14:25:34 -080068 case VNET_LINK_NSH:
69 return (adj_nsh_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +010070 case VNET_LINK_ARP:
71 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010072 }
73 ASSERT(0);
74 return (0);
75}
76
Damjan Marion8b3191e2016-11-09 19:54:20 +010077static u8
78adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
Neale Ranns5e575b12016-10-03 09:40:25 +010079{
Neale Ranns0f26c5a2017-03-01 15:12:11 -080080 u8 arc = (u8) ~0;
Neale Ranns5e575b12016-10-03 09:40:25 +010081 switch (adj->ia_link)
82 {
Neale Ranns924d03a2016-10-19 08:25:46 +010083 case VNET_LINK_IP4:
Neale Ranns5e575b12016-10-03 09:40:25 +010084 {
Damjan Marion8b3191e2016-11-09 19:54:20 +010085 arc = ip4_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010086 break;
87 }
Neale Ranns924d03a2016-10-19 08:25:46 +010088 case VNET_LINK_IP6:
Neale Ranns5e575b12016-10-03 09:40:25 +010089 {
Damjan Marion8b3191e2016-11-09 19:54:20 +010090 arc = ip6_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010091 break;
92 }
Neale Ranns924d03a2016-10-19 08:25:46 +010093 case VNET_LINK_MPLS:
Neale Ranns5e575b12016-10-03 09:40:25 +010094 {
Damjan Marion8b3191e2016-11-09 19:54:20 +010095 arc = mpls_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010096 break;
97 }
Neale Ranns924d03a2016-10-19 08:25:46 +010098 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +010099 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100100 arc = ethernet_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100101 break;
102 }
Florin Corasce1b4c72017-01-26 14:25:34 -0800103 case VNET_LINK_NSH:
104 {
Dave Barach11fb09e2020-08-06 12:10:09 -0400105 arc = nsh_main_placeholder.output_feature_arc_index;
Florin Corasce1b4c72017-01-26 14:25:34 -0800106 break;
107 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100108 case VNET_LINK_ARP:
109 ASSERT(0);
110 break;
Neale Ranns5e575b12016-10-03 09:40:25 +0100111 }
112
Damjan Marion8b3191e2016-11-09 19:54:20 +0100113 ASSERT (arc != (u8) ~0);
114
115 return (arc);
Neale Ranns5e575b12016-10-03 09:40:25 +0100116}
117
Neale Rannsfa5d1982017-02-20 14:19:51 -0800118static u32
119adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
120{
121 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
122 adj_midchain_tx_no_count_node.index :
123 adj_midchain_tx_node.index);
124}
125
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100126/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800127 * adj_midchain_setup
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100128 *
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800129 * Setup the adj as a mid-chain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100130 */
131void
Neale Ranns3ebebc32020-02-18 13:56:24 +0000132adj_midchain_teardown (ip_adjacency_t *adj)
133{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400134 vlib_main_t *vm = vlib_get_main();
Neale Ranns3ebebc32020-02-18 13:56:24 +0000135
136 dpo_reset(&adj->sub_type.midchain.next_dpo);
137
Neale Ranns4ec36c52020-03-31 09:21:29 -0400138 vlib_worker_thread_barrier_sync(vm);
Neale Ranns5d0136f2020-05-12 08:51:02 +0000139 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400140 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);
Neale Ranns3ebebc32020-02-18 13:56:24 +0000145}
146
147/**
148 * adj_midchain_setup
149 *
150 * Setup the adj as a mid-chain
151 */
152void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800153adj_midchain_setup (adj_index_t adj_index,
154 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800155 const void *data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800156 adj_flags_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100157{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400158 vlib_main_t *vm = vlib_get_main();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100159 ip_adjacency_t *adj;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400160 u32 tx_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161
162 ASSERT(ADJ_INDEX_INVALID != adj_index);
163
164 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100165
Neale Ranns5e575b12016-10-03 09:40:25 +0100166 adj->sub_type.midchain.fixup_func = fixup;
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800167 adj->sub_type.midchain.fixup_data = data;
Neale Ranns521a8d72018-12-06 13:46:49 +0000168 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
Neale Rannsfa5d1982017-02-20 14:19:51 -0800169 adj->ia_flags |= flags;
Neale Ranns5e575b12016-10-03 09:40:25 +0100170
Neale Ranns4ec36c52020-03-31 09:21:29 -0400171 if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
172 {
173 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
174 }
175 else
176 {
177 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
178 }
Neale Ranns5c544c82020-11-17 09:47:07 +0000179 if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
180 {
181 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
182 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400183
Neale Rannsfa5d1982017-02-20 14:19:51 -0800184 tx_node = adj_nbr_midchain_get_tx_node(adj);
Neale Ranns5e575b12016-10-03 09:40:25 +0100185
Neale Ranns4ec36c52020-03-31 09:21:29 -0400186 vlib_worker_thread_barrier_sync(vm);
Neale Ranns5d0136f2020-05-12 08:51:02 +0000187 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400188 adj_midchain_get_feature_arc_index_for_link_type (adj),
189 adj->rewrite_header.sw_if_index,
190 tx_node);
191 vlib_worker_thread_barrier_release(vm);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100192
Neale Rannsb80c5362016-10-08 13:03:40 +0100193 /*
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.
199 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800200 dpo_stack_from_node(tx_node,
Neale Ranns43161a82017-08-12 02:12:00 -0700201 &adj->sub_type.midchain.next_dpo,
202 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800203}
204
205/**
206 * adj_nbr_midchain_update_rewrite
207 *
208 * Update the adjacency's rewrite string. A NULL string implies the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700209 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800210 * NB: the adj being updated may be handling traffic in the DP.
211 */
212void
213adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
214 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800215 const void *fixup_data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800216 adj_flags_t flags,
217 u8 *rewrite)
218{
219 ip_adjacency_t *adj;
220
221 ASSERT(ADJ_INDEX_INVALID != adj_index);
222
223 adj = adj_get(adj_index);
224
225 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700226 * one time only update. since we don't support changing the tunnel
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800227 * src,dst, this is all we need.
228 */
Neale Ranns174959c2020-02-03 10:33:51 +0000229 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
Neale Ranns14053c92019-12-29 23:55:18 +0000230 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
231 {
232 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
233 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100234
Neale Rannsb80c5362016-10-08 13:03:40 +0100235 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700236 * update the rewrite with the workers paused.
Neale Rannsb80c5362016-10-08 13:03:40 +0100237 */
238 adj_nbr_update_rewrite_internal(adj,
239 IP_LOOKUP_NEXT_MIDCHAIN,
240 adj_get_midchain_node(adj->ia_link),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800241 adj_nbr_midchain_get_tx_node(adj),
Neale Rannsb80c5362016-10-08 13:03:40 +0100242 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100243}
244
Neale Ranns4ec36c52020-03-31 09:21:29 -0400245void
246adj_nbr_midchain_update_next_node (adj_index_t adj_index,
247 u32 next_node)
248{
249 ip_adjacency_t *adj;
250 vlib_main_t * vm;
251
252 ASSERT(ADJ_INDEX_INVALID != adj_index);
253
254 adj = adj_get(adj_index);
255 vm = vlib_get_main();
256
257 vlib_worker_thread_barrier_sync(vm);
258
259 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
260 adj->ia_node_index,
261 next_node);
262
Neale Ranns5d0136f2020-05-12 08:51:02 +0000263 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400264 adj_midchain_get_feature_arc_index_for_link_type (adj),
265 adj->rewrite_header.sw_if_index,
266 next_node);
267
268 vlib_worker_thread_barrier_release(vm);
269}
270
271void
Neale Ranns5d0136f2020-05-12 08:51:02 +0000272adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
Neale Ranns4ec36c52020-03-31 09:21:29 -0400273{
274 ip_adjacency_t *adj;
275 vlib_main_t * vm;
276
277 ASSERT(ADJ_INDEX_INVALID != adj_index);
278
279 adj = adj_get(adj_index);
280 vm = vlib_get_main();
281
282 vlib_worker_thread_barrier_sync(vm);
283
284 adj->rewrite_header.next_index =
285 vlib_node_add_next(vlib_get_main(),
286 adj->ia_node_index,
287 adj_nbr_midchain_get_tx_node(adj));
288
Neale Ranns5d0136f2020-05-12 08:51:02 +0000289 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400290 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));
293
294 vlib_worker_thread_barrier_release(vm);
295}
296
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100297/**
Neale Ranns5e575b12016-10-03 09:40:25 +0100298 * adj_nbr_midchain_unstack
299 *
300 * Unstack the adj. stack it on drop
301 */
302void
303adj_nbr_midchain_unstack (adj_index_t adj_index)
304{
Neale Ranns521a8d72018-12-06 13:46:49 +0000305 fib_node_index_t *entry_indicies, tmp;
Neale Ranns5e575b12016-10-03 09:40:25 +0100306 ip_adjacency_t *adj;
307
308 ASSERT(ADJ_INDEX_INVALID != adj_index);
Neale Ranns521a8d72018-12-06 13:46:49 +0000309 adj = adj_get (adj_index);
Neale Ranns5e575b12016-10-03 09:40:25 +0100310
Neale Ranns521a8d72018-12-06 13:46:49 +0000311 /*
312 * check to see if this unstacking breaks a recursion loop
313 */
314 entry_indicies = NULL;
315 tmp = adj->sub_type.midchain.fei;
316 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
317
318 if (FIB_NODE_INDEX_INVALID != tmp)
319 {
320 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
321 vec_free(entry_indicies);
322 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100323
324 /*
325 * stack on the drop
326 */
327 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
Neale Ranns43161a82017-08-12 02:12:00 -0700328 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)));
Neale Ranns5e575b12016-10-03 09:40:25 +0100331 CLIB_MEMORY_BARRIER();
332}
333
Neale Ranns521a8d72018-12-06 13:46:49 +0000334void
335adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
336 fib_node_index_t fei,
337 fib_forward_chain_type_t fct)
338{
339 fib_node_index_t *entry_indicies;
340 dpo_id_t tmp = DPO_INVALID;
341 ip_adjacency_t *adj;
342
343 adj = adj_get (ai);
344
345 /*
346 * check to see if this stacking will form a recursion loop
347 */
348 entry_indicies = NULL;
349 adj->sub_type.midchain.fei = fei;
350
351 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
352 {
353 /*
354 * loop formed, stack on the drop.
355 */
356 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
357 }
358 else
359 {
360 fib_entry_contribute_forwarding (fei, fct, &tmp);
361
Neale Ranns5c544c82020-11-17 09:47:07 +0000362 if (DPO_LOAD_BALANCE == tmp.dpoi_type)
Neale Ranns521a8d72018-12-06 13:46:49 +0000363 {
Neale Ranns521a8d72018-12-06 13:46:49 +0000364 load_balance_t *lb;
Neale Ranns521a8d72018-12-06 13:46:49 +0000365
366 lb = load_balance_get (tmp.dpoi_index);
367
Neale Ranns5c544c82020-11-17 09:47:07 +0000368 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
369 lb->lb_n_buckets == 1)
Neale Ranns521a8d72018-12-06 13:46:49 +0000370 {
Neale Ranns5c544c82020-11-17 09:47:07 +0000371 /*
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.
376 */
377 const dpo_id_t *choice;
378 int hash;
Neale Ranns521a8d72018-12-06 13:46:49 +0000379
Neale Ranns5c544c82020-11-17 09:47:07 +0000380 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
381 {
382 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
383 lb->lb_hash_config);
384 }
385 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
386 {
387 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
388 lb->lb_hash_config);
389 }
390 else
391 {
392 hash = 0;
393 ASSERT(0);
394 }
395
396 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
397 dpo_copy (&tmp, choice);
398 }
Neale Ranns65d789e2021-02-08 15:24:56 +0000399 else if (lb->lb_n_buckets > 1)
Neale Ranns5c544c82020-11-17 09:47:07 +0000400 {
Neale Ranns65d789e2021-02-08 15:24:56 +0000401 /*
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.
406 */
407 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
408 }
409
410 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
411 {
412 /*
413 * The client, for reasons unbeknownst to adj, wants to force
414 * a flow hash on the inner, we will oblige.
415 */
Neale Ranns5c544c82020-11-17 09:47:07 +0000416 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
417 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000418 }
419 }
420 adj_nbr_midchain_stack (ai, &tmp);
421 dpo_reset(&tmp);
422 vec_free(entry_indicies);
423}
424
Neale Ranns5e575b12016-10-03 09:40:25 +0100425/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100426 * adj_nbr_midchain_stack
427 */
428void
429adj_nbr_midchain_stack (adj_index_t adj_index,
430 const dpo_id_t *next)
431{
432 ip_adjacency_t *adj;
433
434 ASSERT(ADJ_INDEX_INVALID != adj_index);
435
436 adj = adj_get(adj_index);
437
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800438 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
439 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100440
Neale Rannsfa5d1982017-02-20 14:19:51 -0800441 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
Neale Ranns5e575b12016-10-03 09:40:25 +0100442 &adj->sub_type.midchain.next_dpo,
443 next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100444}
445
Neale Ranns521a8d72018-12-06 13:46:49 +0000446int
447adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
448 fib_node_index_t **entry_indicies)
449{
450 fib_node_index_t *entry_index, *entries;
451 ip_adjacency_t * adj;
452
453 adj = adj_get(ai);
454 entries = *entry_indicies;
455
456 vec_foreach(entry_index, entries)
457 {
458 if (*entry_index == adj->sub_type.midchain.fei)
459 {
460 /*
461 * The entry this midchain links to is already in the set
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700462 * of visited entries, this is a loop
Neale Ranns521a8d72018-12-06 13:46:49 +0000463 */
464 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
465 return (1);
466 }
467 }
468
469 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
470 return (0);
471}
472
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100473u8*
474format_adj_midchain (u8* s, va_list *ap)
475{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400476 index_t index = va_arg(*ap, index_t);
477 u32 indent = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100478 ip_adjacency_t * adj = adj_get(index);
479
Neale Ranns924d03a2016-10-19 08:25:46 +0100480 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Ranns25edf142019-03-22 08:12:48 +0000481 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
482 s = format(s, " [features]");
Neale Rannsc819fc62018-02-16 02:44:05 -0800483 s = format (s, " via %U",
484 format_ip46_address, &adj->sub_type.nbr.next_hop,
485 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100486 s = format (s, " %U",
Neale Ranns5e575b12016-10-03 09:40:25 +0100487 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400488 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
Neale Ranns521a8d72018-12-06 13:46:49 +0000489 s = format (s, "\n%Ustacked-on",
490 format_white_space, indent);
491
492 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
493 {
494 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
495
496 }
497 s = format (s, ":\n%U%U",
Neale Ranns43161a82017-08-12 02:12:00 -0700498 format_white_space, indent+2,
499 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100500
501 return (s);
502}
503
504static void
505adj_dpo_lock (dpo_id_t *dpo)
506{
507 adj_lock(dpo->dpoi_index);
508}
509static void
510adj_dpo_unlock (dpo_id_t *dpo)
511{
512 adj_unlock(dpo->dpoi_index);
513}
514
515const 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,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700519 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns8f5fef22020-12-21 08:29:34 +0000520 .dv_get_mtu = adj_dpo_get_mtu,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100521};
522
523/**
524 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
525 * object.
526 *
527 * this means that these graph nodes are ones from which a midchain is the
528 * parent object in the DPO-graph.
529 */
530const static char* const midchain_ip4_nodes[] =
531{
532 "ip4-midchain",
533 NULL,
534};
535const static char* const midchain_ip6_nodes[] =
536{
537 "ip6-midchain",
538 NULL,
539};
540const static char* const midchain_mpls_nodes[] =
541{
542 "mpls-midchain",
543 NULL,
544};
Neale Ranns5e575b12016-10-03 09:40:25 +0100545const static char* const midchain_ethernet_nodes[] =
546{
547 "adj-l2-midchain",
548 NULL,
549};
Florin Corasb69111e2017-02-13 23:55:27 -0800550const static char* const midchain_nsh_nodes[] =
551{
552 "adj-nsh-midchain",
553 NULL,
554};
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100555
556const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
557{
558 [DPO_PROTO_IP4] = midchain_ip4_nodes,
559 [DPO_PROTO_IP6] = midchain_ip6_nodes,
560 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +0100561 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
Florin Corasb69111e2017-02-13 23:55:27 -0800562 [DPO_PROTO_NSH] = midchain_nsh_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100563};
564
565void
566adj_midchain_module_init (void)
567{
568 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
569}