blob: 3d879e9d7fc51bc38f5310965a23913cd7f6e3f3 [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/ethernet/arp_packet.h>
22#include <vnet/dpo/drop_dpo.h>
Neale Ranns521a8d72018-12-06 13:46:49 +000023#include <vnet/dpo/load_balance.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010024#include <vnet/fib/fib_walk.h>
Neale Ranns521a8d72018-12-06 13:46:49 +000025#include <vnet/fib/fib_entry.h>
Neale Rannse4031132020-10-26 13:00:06 +000026#include <vnet/ip/ip4_inlines.h>
27#include <vnet/ip/ip6_inlines.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010028
Neale Ranns5e575b12016-10-03 09:40:25 +010029/**
Neale Ranns5e575b12016-10-03 09:40:25 +010030 * @brief Trace data for packets traversing the midchain tx node
31 */
32typedef struct adj_midchain_tx_trace_t_
33{
34 /**
35 * @brief the midchain adj we are traversing
36 */
37 adj_index_t ai;
38} adj_midchain_tx_trace_t;
39
40always_inline uword
Damjan Marion77766a12016-11-02 01:21:05 +010041adj_midchain_tx_inline (vlib_main_t * vm,
Neale Ranns5e575b12016-10-03 09:40:25 +010042 vlib_node_runtime_t * node,
43 vlib_frame_t * frame,
44 int interface_count)
45{
Neale Ranns4ec36c52020-03-31 09:21:29 -040046 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
47 u16 nexts[VLIB_FRAME_SIZE], *next;
48 u32 * from, n_left, thread_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010049 vnet_main_t *vnm = vnet_get_main ();
50 vnet_interface_main_t *im = &vnm->interface_main;
Neale Ranns5e575b12016-10-03 09:40:25 +010051
Neale Ranns4ec36c52020-03-31 09:21:29 -040052 thread_index = vm->thread_index;
53 n_left = frame->n_vectors;
Neale Ranns5e575b12016-10-03 09:40:25 +010054 from = vlib_frame_vector_args (frame);
55
Neale Ranns4ec36c52020-03-31 09:21:29 -040056 vlib_get_buffers (vm, from, bufs, n_left);
Neale Ranns5e575b12016-10-03 09:40:25 +010057
Neale Ranns4ec36c52020-03-31 09:21:29 -040058 next = nexts;
59 b = bufs;
Neale Ranns5e575b12016-10-03 09:40:25 +010060
Neale Ranns4ec36c52020-03-31 09:21:29 -040061 while (n_left > 8)
Neale Ranns5e575b12016-10-03 09:40:25 +010062 {
Neale Ranns4ec36c52020-03-31 09:21:29 -040063 u32 adj_index0, adj_index1, adj_index2, adj_index3;
64 const ip_adjacency_t *adj0, *adj1, *adj2, *adj3;
65 const dpo_id_t *dpo0, *dpo1, *dpo2, *dpo3;
Neale Ranns5e575b12016-10-03 09:40:25 +010066
Neale Ranns4ec36c52020-03-31 09:21:29 -040067 /* Prefetch next iteration. */
68 {
69 vlib_prefetch_buffer_header (b[4], LOAD);
70 vlib_prefetch_buffer_header (b[5], LOAD);
71 vlib_prefetch_buffer_header (b[6], LOAD);
72 vlib_prefetch_buffer_header (b[7], LOAD);
73 }
Neale Rannsd3b85b02016-10-22 15:17:21 -070074
Neale Ranns4ec36c52020-03-31 09:21:29 -040075 /* Follow the DPO on which the midchain is stacked */
76 adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
77 adj_index1 = vnet_buffer(b[1])->ip.adj_index[VLIB_TX];
78 adj_index2 = vnet_buffer(b[2])->ip.adj_index[VLIB_TX];
79 adj_index3 = vnet_buffer(b[3])->ip.adj_index[VLIB_TX];
Neale Rannsd3b85b02016-10-22 15:17:21 -070080
Neale Ranns4ec36c52020-03-31 09:21:29 -040081 adj0 = adj_get(adj_index0);
82 adj1 = adj_get(adj_index1);
83 adj2 = adj_get(adj_index2);
84 adj3 = adj_get(adj_index3);
Neale Rannsd3b85b02016-10-22 15:17:21 -070085
Neale Ranns4ec36c52020-03-31 09:21:29 -040086 dpo0 = &adj0->sub_type.midchain.next_dpo;
87 dpo1 = &adj1->sub_type.midchain.next_dpo;
88 dpo2 = &adj2->sub_type.midchain.next_dpo;
89 dpo3 = &adj3->sub_type.midchain.next_dpo;
Neale Rannsd3b85b02016-10-22 15:17:21 -070090
Neale Ranns4ec36c52020-03-31 09:21:29 -040091 next[0] = dpo0->dpoi_next_node;
92 next[1] = dpo1->dpoi_next_node;
93 next[2] = dpo2->dpoi_next_node;
94 next[3] = dpo3->dpoi_next_node;
Neale Rannsd3b85b02016-10-22 15:17:21 -070095
Neale Ranns4ec36c52020-03-31 09:21:29 -040096 vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
97 vnet_buffer(b[1])->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
98 vnet_buffer(b[2])->ip.adj_index[VLIB_TX] = dpo2->dpoi_index;
99 vnet_buffer(b[3])->ip.adj_index[VLIB_TX] = dpo3->dpoi_index;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700100
Neale Ranns4ec36c52020-03-31 09:21:29 -0400101 if (interface_count)
102 {
103 vlib_increment_combined_counter (im->combined_sw_if_counters
104 + VNET_INTERFACE_COUNTER_TX,
105 thread_index,
106 adj0->rewrite_header.sw_if_index,
107 1,
108 vlib_buffer_length_in_chain (vm, b[0]));
109 vlib_increment_combined_counter (im->combined_sw_if_counters
110 + VNET_INTERFACE_COUNTER_TX,
111 thread_index,
112 adj1->rewrite_header.sw_if_index,
113 1,
114 vlib_buffer_length_in_chain (vm, b[1]));
115 vlib_increment_combined_counter (im->combined_sw_if_counters
116 + VNET_INTERFACE_COUNTER_TX,
117 thread_index,
118 adj2->rewrite_header.sw_if_index,
119 1,
120 vlib_buffer_length_in_chain (vm, b[2]));
121 vlib_increment_combined_counter (im->combined_sw_if_counters
122 + VNET_INTERFACE_COUNTER_TX,
123 thread_index,
124 adj3->rewrite_header.sw_if_index,
125 1,
126 vlib_buffer_length_in_chain (vm, b[3]));
127 }
Neale Rannsd3b85b02016-10-22 15:17:21 -0700128
Neale Ranns4ec36c52020-03-31 09:21:29 -0400129 if (PREDICT_FALSE(node->flags & VLIB_NODE_FLAG_TRACE))
130 {
131 if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
132 {
133 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
134 b[0], sizeof (*tr));
135 tr->ai = adj_index0;
136 }
137 if (PREDICT_FALSE(b[1]->flags & VLIB_BUFFER_IS_TRACED))
138 {
139 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
140 b[1], sizeof (*tr));
141 tr->ai = adj_index1;
142 }
143 if (PREDICT_FALSE(b[2]->flags & VLIB_BUFFER_IS_TRACED))
144 {
145 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
146 b[2], sizeof (*tr));
147 tr->ai = adj_index2;
148 }
149 if (PREDICT_FALSE(b[3]->flags & VLIB_BUFFER_IS_TRACED))
150 {
151 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
152 b[3], sizeof (*tr));
153 tr->ai = adj_index3;
154 }
155 }
156 n_left -= 4;
157 b += 4;
158 next += 4;
Neale Ranns5e575b12016-10-03 09:40:25 +0100159 }
160
Neale Ranns4ec36c52020-03-31 09:21:29 -0400161 while (n_left)
162 {
163 const ip_adjacency_t * adj0;
164 const dpo_id_t *dpo0;
165 u32 adj_index0;
166
167 /* Follow the DPO on which the midchain is stacked */
168 adj_index0 = vnet_buffer(b[0])->ip.adj_index[VLIB_TX];
169 adj0 = adj_get(adj_index0);
170 dpo0 = &adj0->sub_type.midchain.next_dpo;
171 next[0] = dpo0->dpoi_next_node;
172 vnet_buffer(b[0])->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
173
174 if (interface_count)
175 {
176 vlib_increment_combined_counter (im->combined_sw_if_counters
177 + VNET_INTERFACE_COUNTER_TX,
178 thread_index,
179 adj0->rewrite_header.sw_if_index,
180 1,
181 vlib_buffer_length_in_chain (vm, b[0]));
182 }
183
184 if (PREDICT_FALSE(b[0]->flags & VLIB_BUFFER_IS_TRACED))
185 {
186 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
187 b[0], sizeof (*tr));
188 tr->ai = adj_index0;
189 }
190
191 n_left -= 1;
192 b += 1;
193 next += 1;
194 }
195
196 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
197
Neale Ranns5e575b12016-10-03 09:40:25 +0100198 return frame->n_vectors;
199}
200
201static u8 *
202format_adj_midchain_tx_trace (u8 * s, va_list * args)
203{
204 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
205 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
206 adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
207
208 s = format(s, "adj-midchain:[%d]:%U", tr->ai,
Neale Rannsb80c5362016-10-08 13:03:40 +0100209 format_ip_adjacency, tr->ai,
Neale Ranns5e575b12016-10-03 09:40:25 +0100210 FORMAT_IP_ADJACENCY_NONE);
211
212 return (s);
213}
214
215static uword
216adj_midchain_tx (vlib_main_t * vm,
217 vlib_node_runtime_t * node,
218 vlib_frame_t * frame)
219{
Damjan Marion77766a12016-11-02 01:21:05 +0100220 return (adj_midchain_tx_inline(vm, node, frame, 1));
Neale Ranns5e575b12016-10-03 09:40:25 +0100221}
222
Neale Ranns4ec36c52020-03-31 09:21:29 -0400223VLIB_REGISTER_NODE (adj_midchain_tx_node) = {
Neale Ranns5e575b12016-10-03 09:40:25 +0100224 .function = adj_midchain_tx,
225 .name = "adj-midchain-tx",
226 .vector_size = sizeof (u32),
227
228 .format_trace = format_adj_midchain_tx_trace,
229
230 .n_next_nodes = 1,
231 .next_nodes = {
232 [0] = "error-drop",
233 },
234};
235
236static uword
237adj_midchain_tx_no_count (vlib_main_t * vm,
238 vlib_node_runtime_t * node,
239 vlib_frame_t * frame)
240{
Damjan Marion77766a12016-11-02 01:21:05 +0100241 return (adj_midchain_tx_inline(vm, node, frame, 0));
Neale Ranns5e575b12016-10-03 09:40:25 +0100242}
243
Neale Ranns4ec36c52020-03-31 09:21:29 -0400244VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node) = {
Neale Ranns5e575b12016-10-03 09:40:25 +0100245 .function = adj_midchain_tx_no_count,
246 .name = "adj-midchain-tx-no-count",
247 .vector_size = sizeof (u32),
248
249 .format_trace = format_adj_midchain_tx_trace,
Neale Ranns4ec36c52020-03-31 09:21:29 -0400250 .sibling_of = "adj-midchain-tx",
Neale Ranns5e575b12016-10-03 09:40:25 +0100251};
252
Neale Ranns4ec36c52020-03-31 09:21:29 -0400253#ifndef CLIB_MARCH_VARIANT
254
255u8
256adj_is_midchain (adj_index_t ai)
257{
258 ip_adjacency_t *adj;
259
260 adj = adj_get(ai);
261
262 switch (adj->lookup_next_index)
263 {
264 case IP_LOOKUP_NEXT_MIDCHAIN:
265 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
266 return (1);
267 case IP_LOOKUP_NEXT_ARP:
268 case IP_LOOKUP_NEXT_GLEAN:
269 case IP_LOOKUP_NEXT_BCAST:
270 case IP_LOOKUP_NEXT_MCAST:
271 case IP_LOOKUP_NEXT_DROP:
272 case IP_LOOKUP_NEXT_PUNT:
273 case IP_LOOKUP_NEXT_LOCAL:
274 case IP_LOOKUP_NEXT_REWRITE:
275 case IP_LOOKUP_NEXT_ICMP_ERROR:
276 case IP_LOOKUP_N_NEXT:
277 return (0);
278 }
279
280 return (0);
281}
Neale Ranns5e575b12016-10-03 09:40:25 +0100282
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100283static inline u32
Neale Ranns924d03a2016-10-19 08:25:46 +0100284adj_get_midchain_node (vnet_link_t link)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100285{
286 switch (link) {
Neale Ranns924d03a2016-10-19 08:25:46 +0100287 case VNET_LINK_IP4:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100288 return (ip4_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100289 case VNET_LINK_IP6:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100290 return (ip6_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100291 case VNET_LINK_MPLS:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100292 return (mpls_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100293 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100294 return (adj_l2_midchain_node.index);
Florin Corasce1b4c72017-01-26 14:25:34 -0800295 case VNET_LINK_NSH:
296 return (adj_nsh_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100297 case VNET_LINK_ARP:
298 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100299 }
300 ASSERT(0);
301 return (0);
302}
303
Damjan Marion8b3191e2016-11-09 19:54:20 +0100304static u8
305adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
Neale Ranns5e575b12016-10-03 09:40:25 +0100306{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800307 u8 arc = (u8) ~0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100308 switch (adj->ia_link)
309 {
Neale Ranns924d03a2016-10-19 08:25:46 +0100310 case VNET_LINK_IP4:
Neale Ranns5e575b12016-10-03 09:40:25 +0100311 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100312 arc = ip4_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100313 break;
314 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100315 case VNET_LINK_IP6:
Neale Ranns5e575b12016-10-03 09:40:25 +0100316 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100317 arc = ip6_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100318 break;
319 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100320 case VNET_LINK_MPLS:
Neale Ranns5e575b12016-10-03 09:40:25 +0100321 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100322 arc = mpls_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100323 break;
324 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100325 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100326 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100327 arc = ethernet_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100328 break;
329 }
Florin Corasce1b4c72017-01-26 14:25:34 -0800330 case VNET_LINK_NSH:
331 {
Dave Barach11fb09e2020-08-06 12:10:09 -0400332 arc = nsh_main_placeholder.output_feature_arc_index;
Florin Corasce1b4c72017-01-26 14:25:34 -0800333 break;
334 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100335 case VNET_LINK_ARP:
336 ASSERT(0);
337 break;
Neale Ranns5e575b12016-10-03 09:40:25 +0100338 }
339
Damjan Marion8b3191e2016-11-09 19:54:20 +0100340 ASSERT (arc != (u8) ~0);
341
342 return (arc);
Neale Ranns5e575b12016-10-03 09:40:25 +0100343}
344
Neale Rannsfa5d1982017-02-20 14:19:51 -0800345static u32
346adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
347{
348 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
349 adj_midchain_tx_no_count_node.index :
350 adj_midchain_tx_node.index);
351}
352
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100353/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800354 * adj_midchain_setup
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100355 *
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800356 * Setup the adj as a mid-chain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100357 */
358void
Neale Ranns3ebebc32020-02-18 13:56:24 +0000359adj_midchain_teardown (ip_adjacency_t *adj)
360{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400361 vlib_main_t *vm = vlib_get_main();
Neale Ranns3ebebc32020-02-18 13:56:24 +0000362
363 dpo_reset(&adj->sub_type.midchain.next_dpo);
364
Neale Ranns4ec36c52020-03-31 09:21:29 -0400365 vlib_worker_thread_barrier_sync(vm);
Neale Ranns5d0136f2020-05-12 08:51:02 +0000366 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400367 adj_midchain_get_feature_arc_index_for_link_type (adj),
368 adj->rewrite_header.sw_if_index,
369 vlib_get_node_by_name (vlib_get_main(),
370 (u8*) "interface-output")->index);
371 vlib_worker_thread_barrier_release(vm);
Neale Ranns3ebebc32020-02-18 13:56:24 +0000372}
373
374/**
375 * adj_midchain_setup
376 *
377 * Setup the adj as a mid-chain
378 */
379void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800380adj_midchain_setup (adj_index_t adj_index,
381 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800382 const void *data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800383 adj_flags_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100384{
Neale Ranns4ec36c52020-03-31 09:21:29 -0400385 vlib_main_t *vm = vlib_get_main();
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100386 ip_adjacency_t *adj;
Neale Ranns4ec36c52020-03-31 09:21:29 -0400387 u32 tx_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100388
389 ASSERT(ADJ_INDEX_INVALID != adj_index);
390
391 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100392
Neale Ranns5e575b12016-10-03 09:40:25 +0100393 adj->sub_type.midchain.fixup_func = fixup;
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800394 adj->sub_type.midchain.fixup_data = data;
Neale Ranns521a8d72018-12-06 13:46:49 +0000395 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
Neale Rannsfa5d1982017-02-20 14:19:51 -0800396 adj->ia_flags |= flags;
Neale Ranns5e575b12016-10-03 09:40:25 +0100397
Neale Ranns4ec36c52020-03-31 09:21:29 -0400398 if (flags & ADJ_FLAG_MIDCHAIN_FIXUP_IP4O4_HDR)
399 {
400 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_IP4_O_4;
401 }
402 else
403 {
404 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_IP4_O_4;
405 }
Neale Ranns5c544c82020-11-17 09:47:07 +0000406 if (!(flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH))
407 {
408 adj->rewrite_header.flags &= ~VNET_REWRITE_FIXUP_FLOW_HASH;
409 }
Neale Ranns4ec36c52020-03-31 09:21:29 -0400410
Neale Rannsfa5d1982017-02-20 14:19:51 -0800411 tx_node = adj_nbr_midchain_get_tx_node(adj);
Neale Ranns5e575b12016-10-03 09:40:25 +0100412
Neale Ranns4ec36c52020-03-31 09:21:29 -0400413 vlib_worker_thread_barrier_sync(vm);
Neale Ranns5d0136f2020-05-12 08:51:02 +0000414 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400415 adj_midchain_get_feature_arc_index_for_link_type (adj),
416 adj->rewrite_header.sw_if_index,
417 tx_node);
418 vlib_worker_thread_barrier_release(vm);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100419
Neale Rannsb80c5362016-10-08 13:03:40 +0100420 /*
421 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
422 * The graph arc used/created here is from the midchain-tx node to the
423 * child's registered node. This is because post adj processing the next
424 * node are any output features, then the midchain-tx. from there we
425 * need to get to the stacked child's node.
426 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800427 dpo_stack_from_node(tx_node,
Neale Ranns43161a82017-08-12 02:12:00 -0700428 &adj->sub_type.midchain.next_dpo,
429 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800430}
431
432/**
433 * adj_nbr_midchain_update_rewrite
434 *
435 * Update the adjacency's rewrite string. A NULL string implies the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700436 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800437 * NB: the adj being updated may be handling traffic in the DP.
438 */
439void
440adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
441 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800442 const void *fixup_data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800443 adj_flags_t flags,
444 u8 *rewrite)
445{
446 ip_adjacency_t *adj;
447
448 ASSERT(ADJ_INDEX_INVALID != adj_index);
449
450 adj = adj_get(adj_index);
451
452 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700453 * one time only update. since we don't support changing the tunnel
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800454 * src,dst, this is all we need.
455 */
Neale Ranns174959c2020-02-03 10:33:51 +0000456 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
Neale Ranns14053c92019-12-29 23:55:18 +0000457 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
458 {
459 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
460 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100461
Neale Rannsb80c5362016-10-08 13:03:40 +0100462 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700463 * update the rewrite with the workers paused.
Neale Rannsb80c5362016-10-08 13:03:40 +0100464 */
465 adj_nbr_update_rewrite_internal(adj,
466 IP_LOOKUP_NEXT_MIDCHAIN,
467 adj_get_midchain_node(adj->ia_link),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800468 adj_nbr_midchain_get_tx_node(adj),
Neale Rannsb80c5362016-10-08 13:03:40 +0100469 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100470}
471
Neale Ranns4ec36c52020-03-31 09:21:29 -0400472void
473adj_nbr_midchain_update_next_node (adj_index_t adj_index,
474 u32 next_node)
475{
476 ip_adjacency_t *adj;
477 vlib_main_t * vm;
478
479 ASSERT(ADJ_INDEX_INVALID != adj_index);
480
481 adj = adj_get(adj_index);
482 vm = vlib_get_main();
483
484 vlib_worker_thread_barrier_sync(vm);
485
486 adj->rewrite_header.next_index = vlib_node_add_next(vlib_get_main(),
487 adj->ia_node_index,
488 next_node);
489
Neale Ranns5d0136f2020-05-12 08:51:02 +0000490 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400491 adj_midchain_get_feature_arc_index_for_link_type (adj),
492 adj->rewrite_header.sw_if_index,
493 next_node);
494
495 vlib_worker_thread_barrier_release(vm);
496}
497
498void
Neale Ranns5d0136f2020-05-12 08:51:02 +0000499adj_nbr_midchain_reset_next_node (adj_index_t adj_index)
Neale Ranns4ec36c52020-03-31 09:21:29 -0400500{
501 ip_adjacency_t *adj;
502 vlib_main_t * vm;
503
504 ASSERT(ADJ_INDEX_INVALID != adj_index);
505
506 adj = adj_get(adj_index);
507 vm = vlib_get_main();
508
509 vlib_worker_thread_barrier_sync(vm);
510
511 adj->rewrite_header.next_index =
512 vlib_node_add_next(vlib_get_main(),
513 adj->ia_node_index,
514 adj_nbr_midchain_get_tx_node(adj));
515
Neale Ranns5d0136f2020-05-12 08:51:02 +0000516 adj->ia_cfg_index = vnet_feature_modify_end_node(
Neale Ranns4ec36c52020-03-31 09:21:29 -0400517 adj_midchain_get_feature_arc_index_for_link_type (adj),
518 adj->rewrite_header.sw_if_index,
519 adj_nbr_midchain_get_tx_node(adj));
520
521 vlib_worker_thread_barrier_release(vm);
522}
523
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100524/**
Neale Ranns5e575b12016-10-03 09:40:25 +0100525 * adj_nbr_midchain_unstack
526 *
527 * Unstack the adj. stack it on drop
528 */
529void
530adj_nbr_midchain_unstack (adj_index_t adj_index)
531{
Neale Ranns521a8d72018-12-06 13:46:49 +0000532 fib_node_index_t *entry_indicies, tmp;
Neale Ranns5e575b12016-10-03 09:40:25 +0100533 ip_adjacency_t *adj;
534
535 ASSERT(ADJ_INDEX_INVALID != adj_index);
Neale Ranns521a8d72018-12-06 13:46:49 +0000536 adj = adj_get (adj_index);
Neale Ranns5e575b12016-10-03 09:40:25 +0100537
Neale Ranns521a8d72018-12-06 13:46:49 +0000538 /*
539 * check to see if this unstacking breaks a recursion loop
540 */
541 entry_indicies = NULL;
542 tmp = adj->sub_type.midchain.fei;
543 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
544
545 if (FIB_NODE_INDEX_INVALID != tmp)
546 {
547 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
548 vec_free(entry_indicies);
549 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100550
551 /*
552 * stack on the drop
553 */
554 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
Neale Ranns43161a82017-08-12 02:12:00 -0700555 vnet_link_to_dpo_proto(adj->ia_link),
556 &adj->sub_type.midchain.next_dpo,
557 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns5e575b12016-10-03 09:40:25 +0100558 CLIB_MEMORY_BARRIER();
559}
560
Neale Ranns521a8d72018-12-06 13:46:49 +0000561void
562adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
563 fib_node_index_t fei,
564 fib_forward_chain_type_t fct)
565{
566 fib_node_index_t *entry_indicies;
567 dpo_id_t tmp = DPO_INVALID;
568 ip_adjacency_t *adj;
569
570 adj = adj_get (ai);
571
572 /*
573 * check to see if this stacking will form a recursion loop
574 */
575 entry_indicies = NULL;
576 adj->sub_type.midchain.fei = fei;
577
578 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
579 {
580 /*
581 * loop formed, stack on the drop.
582 */
583 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
584 }
585 else
586 {
587 fib_entry_contribute_forwarding (fei, fct, &tmp);
588
Neale Ranns5c544c82020-11-17 09:47:07 +0000589 if (DPO_LOAD_BALANCE == tmp.dpoi_type)
Neale Ranns521a8d72018-12-06 13:46:49 +0000590 {
Neale Ranns521a8d72018-12-06 13:46:49 +0000591 load_balance_t *lb;
Neale Ranns521a8d72018-12-06 13:46:49 +0000592
593 lb = load_balance_get (tmp.dpoi_index);
594
Neale Ranns5c544c82020-11-17 09:47:07 +0000595 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) ||
596 lb->lb_n_buckets == 1)
Neale Ranns521a8d72018-12-06 13:46:49 +0000597 {
Neale Ranns5c544c82020-11-17 09:47:07 +0000598 /*
599 * do that hash now and stack on the choice.
600 * If the choice is an incomplete adj then we will need a poke when
601 * it becomes complete. This happens since the adj update walk propagates
602 * as far a recursive paths.
603 */
604 const dpo_id_t *choice;
605 int hash;
Neale Ranns521a8d72018-12-06 13:46:49 +0000606
Neale Ranns5c544c82020-11-17 09:47:07 +0000607 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
608 {
609 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
610 lb->lb_hash_config);
611 }
612 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
613 {
614 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
615 lb->lb_hash_config);
616 }
617 else
618 {
619 hash = 0;
620 ASSERT(0);
621 }
622
623 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
624 dpo_copy (&tmp, choice);
625 }
Neale Ranns65d789e2021-02-08 15:24:56 +0000626 else if (lb->lb_n_buckets > 1)
Neale Ranns5c544c82020-11-17 09:47:07 +0000627 {
Neale Ranns65d789e2021-02-08 15:24:56 +0000628 /*
629 * the client has chosen not to use the stacking to select a
630 * bucket, and there are more than one buckets. there's no
631 * value in using the midchain's fixed rewrite string to select
632 * the path, so force a flow hash on the inner.
633 */
634 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
635 }
636
637 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_FIXUP_FLOW_HASH)
638 {
639 /*
640 * The client, for reasons unbeknownst to adj, wants to force
641 * a flow hash on the inner, we will oblige.
642 */
Neale Ranns5c544c82020-11-17 09:47:07 +0000643 adj->rewrite_header.flags |= VNET_REWRITE_FIXUP_FLOW_HASH;
644 }
Neale Ranns521a8d72018-12-06 13:46:49 +0000645 }
646 }
647 adj_nbr_midchain_stack (ai, &tmp);
648 dpo_reset(&tmp);
649 vec_free(entry_indicies);
650}
651
Neale Ranns5e575b12016-10-03 09:40:25 +0100652/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100653 * adj_nbr_midchain_stack
654 */
655void
656adj_nbr_midchain_stack (adj_index_t adj_index,
657 const dpo_id_t *next)
658{
659 ip_adjacency_t *adj;
660
661 ASSERT(ADJ_INDEX_INVALID != adj_index);
662
663 adj = adj_get(adj_index);
664
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800665 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
666 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100667
Neale Rannsfa5d1982017-02-20 14:19:51 -0800668 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
Neale Ranns5e575b12016-10-03 09:40:25 +0100669 &adj->sub_type.midchain.next_dpo,
670 next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100671}
672
Neale Ranns521a8d72018-12-06 13:46:49 +0000673int
674adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
675 fib_node_index_t **entry_indicies)
676{
677 fib_node_index_t *entry_index, *entries;
678 ip_adjacency_t * adj;
679
680 adj = adj_get(ai);
681 entries = *entry_indicies;
682
683 vec_foreach(entry_index, entries)
684 {
685 if (*entry_index == adj->sub_type.midchain.fei)
686 {
687 /*
688 * The entry this midchain links to is already in the set
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700689 * of visited entries, this is a loop
Neale Ranns521a8d72018-12-06 13:46:49 +0000690 */
691 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
692 return (1);
693 }
694 }
695
696 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
697 return (0);
698}
699
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100700u8*
701format_adj_midchain (u8* s, va_list *ap)
702{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400703 index_t index = va_arg(*ap, index_t);
704 u32 indent = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100705 ip_adjacency_t * adj = adj_get(index);
706
Neale Ranns924d03a2016-10-19 08:25:46 +0100707 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Ranns25edf142019-03-22 08:12:48 +0000708 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
709 s = format(s, " [features]");
Neale Rannsc819fc62018-02-16 02:44:05 -0800710 s = format (s, " via %U",
711 format_ip46_address, &adj->sub_type.nbr.next_hop,
712 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100713 s = format (s, " %U",
Neale Ranns5e575b12016-10-03 09:40:25 +0100714 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400715 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
Neale Ranns521a8d72018-12-06 13:46:49 +0000716 s = format (s, "\n%Ustacked-on",
717 format_white_space, indent);
718
719 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
720 {
721 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
722
723 }
724 s = format (s, ":\n%U%U",
Neale Ranns43161a82017-08-12 02:12:00 -0700725 format_white_space, indent+2,
726 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100727
728 return (s);
729}
730
731static void
732adj_dpo_lock (dpo_id_t *dpo)
733{
734 adj_lock(dpo->dpoi_index);
735}
736static void
737adj_dpo_unlock (dpo_id_t *dpo)
738{
739 adj_unlock(dpo->dpoi_index);
740}
741
742const static dpo_vft_t adj_midchain_dpo_vft = {
743 .dv_lock = adj_dpo_lock,
744 .dv_unlock = adj_dpo_unlock,
745 .dv_format = format_adj_midchain,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700746 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns8f5fef22020-12-21 08:29:34 +0000747 .dv_get_mtu = adj_dpo_get_mtu,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100748};
749
750/**
751 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
752 * object.
753 *
754 * this means that these graph nodes are ones from which a midchain is the
755 * parent object in the DPO-graph.
756 */
757const static char* const midchain_ip4_nodes[] =
758{
759 "ip4-midchain",
760 NULL,
761};
762const static char* const midchain_ip6_nodes[] =
763{
764 "ip6-midchain",
765 NULL,
766};
767const static char* const midchain_mpls_nodes[] =
768{
769 "mpls-midchain",
770 NULL,
771};
Neale Ranns5e575b12016-10-03 09:40:25 +0100772const static char* const midchain_ethernet_nodes[] =
773{
774 "adj-l2-midchain",
775 NULL,
776};
Florin Corasb69111e2017-02-13 23:55:27 -0800777const static char* const midchain_nsh_nodes[] =
778{
779 "adj-nsh-midchain",
780 NULL,
781};
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100782
783const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
784{
785 [DPO_PROTO_IP4] = midchain_ip4_nodes,
786 [DPO_PROTO_IP6] = midchain_ip6_nodes,
787 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +0100788 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
Florin Corasb69111e2017-02-13 23:55:27 -0800789 [DPO_PROTO_NSH] = midchain_nsh_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100790};
791
792void
793adj_midchain_module_init (void)
794{
795 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
796}
Neale Ranns4ec36c52020-03-31 09:21:29 -0400797
798#endif