blob: 88648fea0a9f45348b45ad86188750a23ba1d092 [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 Ranns0bfe5d82016-08-25 15:29:12 +010026
Neale Ranns5e575b12016-10-03 09:40:25 +010027/**
28 * The two midchain tx feature node indices
29 */
Neale Ranns924d03a2016-10-19 08:25:46 +010030static u32 adj_midchain_tx_feature_node[VNET_LINK_NUM];
31static u32 adj_midchain_tx_no_count_feature_node[VNET_LINK_NUM];
Neale Ranns5e575b12016-10-03 09:40:25 +010032
Neale Ranns3ebebc32020-02-18 13:56:24 +000033static u32 *adj_midchain_feat_count_per_sw_if_index[VNET_LINK_NUM];
34
Neale Ranns5e575b12016-10-03 09:40:25 +010035/**
36 * @brief Trace data for packets traversing the midchain tx node
37 */
38typedef struct adj_midchain_tx_trace_t_
39{
40 /**
41 * @brief the midchain adj we are traversing
42 */
43 adj_index_t ai;
44} adj_midchain_tx_trace_t;
45
46always_inline uword
Damjan Marion77766a12016-11-02 01:21:05 +010047adj_midchain_tx_inline (vlib_main_t * vm,
Neale Ranns5e575b12016-10-03 09:40:25 +010048 vlib_node_runtime_t * node,
49 vlib_frame_t * frame,
50 int interface_count)
51{
52 u32 * from, * to_next, n_left_from, n_left_to_next;
53 u32 next_index;
54 vnet_main_t *vnm = vnet_get_main ();
55 vnet_interface_main_t *im = &vnm->interface_main;
Damjan Marion586afd72017-04-05 19:18:20 +020056 u32 thread_index = vm->thread_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010057
58 /* Vector of buffer / pkt indices we're supposed to process */
59 from = vlib_frame_vector_args (frame);
60
61 /* Number of buffers / pkts */
62 n_left_from = frame->n_vectors;
63
64 /* Speculatively send the first buffer to the last disposition we used */
65 next_index = node->cached_next_index;
66
67 while (n_left_from > 0)
68 {
69 /* set up to enqueue to our disposition with index = next_index */
70 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
71
Neale Ranns43161a82017-08-12 02:12:00 -070072 while (n_left_from >= 8 && n_left_to_next > 4)
Neale Rannsd3b85b02016-10-22 15:17:21 -070073 {
Neale Ranns25edf142019-03-22 08:12:48 +000074 const ip_adjacency_t *adj0, *adj1, *adj2, *adj3;
75 const dpo_id_t *dpo0, *dpo1, *dpo2, *dpo3;
76 vlib_buffer_t * b0, *b1, *b2, *b3;
Neale Rannsd3b85b02016-10-22 15:17:21 -070077 u32 bi0, adj_index0, next0;
Neale Rannsd3b85b02016-10-22 15:17:21 -070078 u32 bi1, adj_index1, next1;
Neale Ranns43161a82017-08-12 02:12:00 -070079 u32 bi2, adj_index2, next2;
Neale Ranns43161a82017-08-12 02:12:00 -070080 u32 bi3, adj_index3, next3;
Neale Rannsd3b85b02016-10-22 15:17:21 -070081
82 /* Prefetch next iteration. */
83 {
Neale Ranns43161a82017-08-12 02:12:00 -070084 vlib_buffer_t * p4, * p5;
85 vlib_buffer_t * p6, * p7;
Neale Rannsd3b85b02016-10-22 15:17:21 -070086
Neale Ranns43161a82017-08-12 02:12:00 -070087 p4 = vlib_get_buffer (vm, from[4]);
88 p5 = vlib_get_buffer (vm, from[5]);
89 p6 = vlib_get_buffer (vm, from[6]);
90 p7 = vlib_get_buffer (vm, from[7]);
Neale Rannsd3b85b02016-10-22 15:17:21 -070091
Neale Ranns43161a82017-08-12 02:12:00 -070092 vlib_prefetch_buffer_header (p4, LOAD);
93 vlib_prefetch_buffer_header (p5, LOAD);
94 vlib_prefetch_buffer_header (p6, LOAD);
95 vlib_prefetch_buffer_header (p7, LOAD);
Neale Rannsd3b85b02016-10-22 15:17:21 -070096 }
97
98 bi0 = from[0];
99 to_next[0] = bi0;
100 bi1 = from[1];
101 to_next[1] = bi1;
Neale Ranns43161a82017-08-12 02:12:00 -0700102 bi2 = from[2];
103 to_next[2] = bi2;
104 bi3 = from[3];
105 to_next[3] = bi3;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700106
Neale Ranns43161a82017-08-12 02:12:00 -0700107 from += 4;
108 to_next += 4;
109 n_left_from -= 4;
110 n_left_to_next -= 4;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700111
112 b0 = vlib_get_buffer(vm, bi0);
113 b1 = vlib_get_buffer(vm, bi1);
Neale Ranns43161a82017-08-12 02:12:00 -0700114 b2 = vlib_get_buffer(vm, bi2);
115 b3 = vlib_get_buffer(vm, bi3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700116
117 /* Follow the DPO on which the midchain is stacked */
118 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
119 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
Neale Ranns43161a82017-08-12 02:12:00 -0700120 adj_index2 = vnet_buffer(b2)->ip.adj_index[VLIB_TX];
121 adj_index3 = vnet_buffer(b3)->ip.adj_index[VLIB_TX];
Neale Rannsd3b85b02016-10-22 15:17:21 -0700122
123 adj0 = adj_get(adj_index0);
124 adj1 = adj_get(adj_index1);
Neale Ranns43161a82017-08-12 02:12:00 -0700125 adj2 = adj_get(adj_index2);
126 adj3 = adj_get(adj_index3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700127
128 dpo0 = &adj0->sub_type.midchain.next_dpo;
129 dpo1 = &adj1->sub_type.midchain.next_dpo;
Neale Ranns43161a82017-08-12 02:12:00 -0700130 dpo2 = &adj2->sub_type.midchain.next_dpo;
131 dpo3 = &adj3->sub_type.midchain.next_dpo;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700132
133 next0 = dpo0->dpoi_next_node;
134 next1 = dpo1->dpoi_next_node;
Neale Ranns43161a82017-08-12 02:12:00 -0700135 next2 = dpo2->dpoi_next_node;
136 next3 = dpo3->dpoi_next_node;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700137
Neale Ranns43161a82017-08-12 02:12:00 -0700138 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
139 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
140 vnet_buffer(b2)->ip.adj_index[VLIB_TX] = dpo2->dpoi_index;
141 vnet_buffer(b3)->ip.adj_index[VLIB_TX] = dpo3->dpoi_index;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700142
143 if (interface_count)
144 {
145 vlib_increment_combined_counter (im->combined_sw_if_counters
146 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200147 thread_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700148 adj0->rewrite_header.sw_if_index,
149 1,
150 vlib_buffer_length_in_chain (vm, b0));
151 vlib_increment_combined_counter (im->combined_sw_if_counters
152 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200153 thread_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700154 adj1->rewrite_header.sw_if_index,
155 1,
156 vlib_buffer_length_in_chain (vm, b1));
Neale Ranns43161a82017-08-12 02:12:00 -0700157 vlib_increment_combined_counter (im->combined_sw_if_counters
158 + VNET_INTERFACE_COUNTER_TX,
159 thread_index,
160 adj2->rewrite_header.sw_if_index,
161 1,
162 vlib_buffer_length_in_chain (vm, b2));
163 vlib_increment_combined_counter (im->combined_sw_if_counters
164 + VNET_INTERFACE_COUNTER_TX,
165 thread_index,
166 adj3->rewrite_header.sw_if_index,
167 1,
168 vlib_buffer_length_in_chain (vm, b3));
Neale Rannsd3b85b02016-10-22 15:17:21 -0700169 }
170
171 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
172 {
173 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
174 b0, sizeof (*tr));
175 tr->ai = adj_index0;
176 }
177 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
178 {
179 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
180 b1, sizeof (*tr));
181 tr->ai = adj_index1;
182 }
Neale Ranns43161a82017-08-12 02:12:00 -0700183 if (PREDICT_FALSE(b2->flags & VLIB_BUFFER_IS_TRACED))
184 {
185 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
186 b2, sizeof (*tr));
187 tr->ai = adj_index2;
188 }
189 if (PREDICT_FALSE(b3->flags & VLIB_BUFFER_IS_TRACED))
190 {
191 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
192 b3, sizeof (*tr));
193 tr->ai = adj_index3;
194 }
Neale Rannsd3b85b02016-10-22 15:17:21 -0700195
Neale Ranns43161a82017-08-12 02:12:00 -0700196 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700197 to_next, n_left_to_next,
Neale Ranns43161a82017-08-12 02:12:00 -0700198 bi0, bi1, bi2, bi3,
199 next0, next1, next2, next3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700200 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100201 while (n_left_from > 0 && n_left_to_next > 0)
202 {
203 u32 bi0, adj_index0, next0;
204 const ip_adjacency_t * adj0;
205 const dpo_id_t *dpo0;
206 vlib_buffer_t * b0;
207
208 bi0 = from[0];
209 to_next[0] = bi0;
210 from += 1;
211 to_next += 1;
212 n_left_from -= 1;
213 n_left_to_next -= 1;
214
215 b0 = vlib_get_buffer(vm, bi0);
216
217 /* Follow the DPO on which the midchain is stacked */
218 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
219 adj0 = adj_get(adj_index0);
220 dpo0 = &adj0->sub_type.midchain.next_dpo;
221 next0 = dpo0->dpoi_next_node;
Neale Ranns43161a82017-08-12 02:12:00 -0700222 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100223
224 if (interface_count)
225 {
226 vlib_increment_combined_counter (im->combined_sw_if_counters
227 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200228 thread_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100229 adj0->rewrite_header.sw_if_index,
230 1,
231 vlib_buffer_length_in_chain (vm, b0));
232 }
233
234 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
235 {
236 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
237 b0, sizeof (*tr));
238 tr->ai = adj_index0;
239 }
240
241 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
242 to_next, n_left_to_next,
243 bi0, next0);
244 }
245
246 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247 }
248
Neale Ranns5e575b12016-10-03 09:40:25 +0100249 return frame->n_vectors;
250}
251
252static u8 *
253format_adj_midchain_tx_trace (u8 * s, va_list * args)
254{
255 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
256 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
257 adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
258
259 s = format(s, "adj-midchain:[%d]:%U", tr->ai,
Neale Rannsb80c5362016-10-08 13:03:40 +0100260 format_ip_adjacency, tr->ai,
Neale Ranns5e575b12016-10-03 09:40:25 +0100261 FORMAT_IP_ADJACENCY_NONE);
262
263 return (s);
264}
265
266static uword
267adj_midchain_tx (vlib_main_t * vm,
268 vlib_node_runtime_t * node,
269 vlib_frame_t * frame)
270{
Damjan Marion77766a12016-11-02 01:21:05 +0100271 return (adj_midchain_tx_inline(vm, node, frame, 1));
Neale Ranns5e575b12016-10-03 09:40:25 +0100272}
273
274VLIB_REGISTER_NODE (adj_midchain_tx_node, static) = {
275 .function = adj_midchain_tx,
276 .name = "adj-midchain-tx",
277 .vector_size = sizeof (u32),
278
279 .format_trace = format_adj_midchain_tx_trace,
280
281 .n_next_nodes = 1,
282 .next_nodes = {
283 [0] = "error-drop",
284 },
285};
286
287static uword
288adj_midchain_tx_no_count (vlib_main_t * vm,
289 vlib_node_runtime_t * node,
290 vlib_frame_t * frame)
291{
Damjan Marion77766a12016-11-02 01:21:05 +0100292 return (adj_midchain_tx_inline(vm, node, frame, 0));
Neale Ranns5e575b12016-10-03 09:40:25 +0100293}
294
295VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node, static) = {
296 .function = adj_midchain_tx_no_count,
297 .name = "adj-midchain-tx-no-count",
298 .vector_size = sizeof (u32),
299
300 .format_trace = format_adj_midchain_tx_trace,
301
302 .n_next_nodes = 1,
303 .next_nodes = {
304 [0] = "error-drop",
305 },
306};
307
Damjan Marion8b3191e2016-11-09 19:54:20 +0100308VNET_FEATURE_INIT (adj_midchain_tx_ip4, static) = {
309 .arc_name = "ip4-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100310 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100311 .runs_before = VNET_FEATURES ("interface-output"),
312 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP4],
Neale Ranns5e575b12016-10-03 09:40:25 +0100313};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100314VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip4, static) = {
315 .arc_name = "ip4-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100316 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100317 .runs_before = VNET_FEATURES ("interface-output"),
318 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP4],
Neale Ranns5e575b12016-10-03 09:40:25 +0100319};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100320VNET_FEATURE_INIT (adj_midchain_tx_ip6, static) = {
321 .arc_name = "ip6-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100322 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100323 .runs_before = VNET_FEATURES ("interface-output"),
324 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP6],
Neale Ranns5e575b12016-10-03 09:40:25 +0100325};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100326VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip6, static) = {
327 .arc_name = "ip6-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100328 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100329 .runs_before = VNET_FEATURES ("interface-output"),
330 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP6],
Neale Ranns5e575b12016-10-03 09:40:25 +0100331};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100332VNET_FEATURE_INIT (adj_midchain_tx_mpls, static) = {
333 .arc_name = "mpls-output",
Damjan Marion77766a12016-11-02 01:21:05 +0100334 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100335 .runs_before = VNET_FEATURES ("interface-output"),
336 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_MPLS],
Neale Ranns5e575b12016-10-03 09:40:25 +0100337};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100338VNET_FEATURE_INIT (adj_midchain_tx_no_count_mpls, static) = {
339 .arc_name = "mpls-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100340 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100341 .runs_before = VNET_FEATURES ("interface-output"),
342 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_MPLS],
Neale Ranns5e575b12016-10-03 09:40:25 +0100343};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100344VNET_FEATURE_INIT (adj_midchain_tx_ethernet, static) = {
345 .arc_name = "ethernet-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100346 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100347 .runs_before = VNET_FEATURES ("error-drop"),
348 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_ETHERNET],
Neale Ranns5e575b12016-10-03 09:40:25 +0100349};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100350VNET_FEATURE_INIT (adj_midchain_tx_no_count_ethernet, static) = {
351 .arc_name = "ethernet-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100352 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100353 .runs_before = VNET_FEATURES ("error-drop"),
354 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_ETHERNET],
Neale Ranns5e575b12016-10-03 09:40:25 +0100355};
Florin Corasce1b4c72017-01-26 14:25:34 -0800356VNET_FEATURE_INIT (adj_midchain_tx_nsh, static) = {
357 .arc_name = "nsh-output",
358 .node_name = "adj-midchain-tx",
359 .runs_before = VNET_FEATURES ("error-drop"),
360 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_NSH],
361};
362VNET_FEATURE_INIT (adj_midchain_tx_no_count_nsh, static) = {
363 .arc_name = "nsh-output",
364 .node_name = "adj-midchain-tx-no-count",
365 .runs_before = VNET_FEATURES ("error-drop"),
366 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_NSH],
367};
Neale Ranns5e575b12016-10-03 09:40:25 +0100368
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100369static inline u32
Neale Ranns924d03a2016-10-19 08:25:46 +0100370adj_get_midchain_node (vnet_link_t link)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100371{
372 switch (link) {
Neale Ranns924d03a2016-10-19 08:25:46 +0100373 case VNET_LINK_IP4:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374 return (ip4_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100375 case VNET_LINK_IP6:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100376 return (ip6_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100377 case VNET_LINK_MPLS:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100378 return (mpls_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100379 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100380 return (adj_l2_midchain_node.index);
Florin Corasce1b4c72017-01-26 14:25:34 -0800381 case VNET_LINK_NSH:
382 return (adj_nsh_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100383 case VNET_LINK_ARP:
384 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100385 }
386 ASSERT(0);
387 return (0);
388}
389
Damjan Marion8b3191e2016-11-09 19:54:20 +0100390static u8
391adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
Neale Ranns5e575b12016-10-03 09:40:25 +0100392{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800393 u8 arc = (u8) ~0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100394 switch (adj->ia_link)
395 {
Neale Ranns924d03a2016-10-19 08:25:46 +0100396 case VNET_LINK_IP4:
Neale Ranns5e575b12016-10-03 09:40:25 +0100397 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100398 arc = ip4_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100399 break;
400 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100401 case VNET_LINK_IP6:
Neale Ranns5e575b12016-10-03 09:40:25 +0100402 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100403 arc = ip6_main.lookup_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100404 break;
405 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100406 case VNET_LINK_MPLS:
Neale Ranns5e575b12016-10-03 09:40:25 +0100407 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100408 arc = mpls_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100409 break;
410 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100411 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100412 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100413 arc = ethernet_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100414 break;
415 }
Florin Corasce1b4c72017-01-26 14:25:34 -0800416 case VNET_LINK_NSH:
417 {
418 arc = nsh_main_dummy.output_feature_arc_index;
419 break;
420 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100421 case VNET_LINK_ARP:
422 ASSERT(0);
423 break;
Neale Ranns5e575b12016-10-03 09:40:25 +0100424 }
425
Damjan Marion8b3191e2016-11-09 19:54:20 +0100426 ASSERT (arc != (u8) ~0);
427
428 return (arc);
Neale Ranns5e575b12016-10-03 09:40:25 +0100429}
430
Neale Rannsfa5d1982017-02-20 14:19:51 -0800431static u32
432adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
433{
434 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
435 adj_midchain_tx_no_count_node.index :
436 adj_midchain_tx_node.index);
437}
438
Neale Ranns43161a82017-08-12 02:12:00 -0700439static u32
440adj_nbr_midchain_get_feature_node (ip_adjacency_t *adj)
441{
442 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT)
443 {
444 return (adj_midchain_tx_no_count_feature_node[adj->ia_link]);
445 }
446
447 return (adj_midchain_tx_feature_node[adj->ia_link]);
448}
449
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100450/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800451 * adj_midchain_setup
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100452 *
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800453 * Setup the adj as a mid-chain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100454 */
455void
Neale Ranns3ebebc32020-02-18 13:56:24 +0000456adj_midchain_teardown (ip_adjacency_t *adj)
457{
458 u32 feature_index;
459 u8 arc_index;
460
461 dpo_reset(&adj->sub_type.midchain.next_dpo);
462
463 arc_index = adj_midchain_get_feature_arc_index_for_link_type (adj);
464 feature_index = adj_nbr_midchain_get_feature_node(adj);
465
466 if (0 == --adj_midchain_feat_count_per_sw_if_index[adj->ia_link][adj->rewrite_header.sw_if_index])
467 {
468 vnet_feature_enable_disable_with_index (arc_index, feature_index,
469 adj->rewrite_header.sw_if_index,
470 0, 0, 0);
471 }
472}
473
474/**
475 * adj_midchain_setup
476 *
477 * Setup the adj as a mid-chain
478 */
479void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800480adj_midchain_setup (adj_index_t adj_index,
481 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800482 const void *data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800483 adj_flags_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100484{
Neale Rannsfa5d1982017-02-20 14:19:51 -0800485 u32 feature_index, tx_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100486 ip_adjacency_t *adj;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100487 u8 arc_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100488
489 ASSERT(ADJ_INDEX_INVALID != adj_index);
490
491 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100492
Neale Ranns5e575b12016-10-03 09:40:25 +0100493 adj->sub_type.midchain.fixup_func = fixup;
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800494 adj->sub_type.midchain.fixup_data = data;
Neale Ranns521a8d72018-12-06 13:46:49 +0000495 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
Neale Rannsfa5d1982017-02-20 14:19:51 -0800496 adj->ia_flags |= flags;
Neale Ranns5e575b12016-10-03 09:40:25 +0100497
Damjan Marion8b3191e2016-11-09 19:54:20 +0100498 arc_index = adj_midchain_get_feature_arc_index_for_link_type (adj);
Neale Ranns43161a82017-08-12 02:12:00 -0700499 feature_index = adj_nbr_midchain_get_feature_node(adj);
Neale Rannsfa5d1982017-02-20 14:19:51 -0800500 tx_node = adj_nbr_midchain_get_tx_node(adj);
Neale Ranns5e575b12016-10-03 09:40:25 +0100501
Neale Ranns3ebebc32020-02-18 13:56:24 +0000502 vec_validate (adj_midchain_feat_count_per_sw_if_index[adj->ia_link],
503 adj->rewrite_header.sw_if_index);
504
505 if (0 == adj_midchain_feat_count_per_sw_if_index[adj->ia_link][adj->rewrite_header.sw_if_index]++)
506 {
507 vnet_feature_enable_disable_with_index (arc_index, feature_index,
508 adj->rewrite_header.sw_if_index,
509 1 /* enable */, 0, 0);
510 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100511
Neale Rannsb80c5362016-10-08 13:03:40 +0100512 /*
513 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
514 * The graph arc used/created here is from the midchain-tx node to the
515 * child's registered node. This is because post adj processing the next
516 * node are any output features, then the midchain-tx. from there we
517 * need to get to the stacked child's node.
518 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800519 dpo_stack_from_node(tx_node,
Neale Ranns43161a82017-08-12 02:12:00 -0700520 &adj->sub_type.midchain.next_dpo,
521 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800522}
523
524/**
525 * adj_nbr_midchain_update_rewrite
526 *
527 * Update the adjacency's rewrite string. A NULL string implies the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700528 * rewrite is reset (i.e. when ARP/ND entry is gone).
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800529 * NB: the adj being updated may be handling traffic in the DP.
530 */
531void
532adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
533 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800534 const void *fixup_data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800535 adj_flags_t flags,
536 u8 *rewrite)
537{
538 ip_adjacency_t *adj;
539
540 ASSERT(ADJ_INDEX_INVALID != adj_index);
541
542 adj = adj_get(adj_index);
543
544 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700545 * one time only update. since we don't support changing the tunnel
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800546 * src,dst, this is all we need.
547 */
Neale Ranns174959c2020-02-03 10:33:51 +0000548 if (adj->lookup_next_index != IP_LOOKUP_NEXT_MIDCHAIN &&
Neale Ranns14053c92019-12-29 23:55:18 +0000549 adj->lookup_next_index != IP_LOOKUP_NEXT_MCAST_MIDCHAIN)
550 {
551 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
552 }
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100553
Neale Rannsb80c5362016-10-08 13:03:40 +0100554 /*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700555 * update the rewrite with the workers paused.
Neale Rannsb80c5362016-10-08 13:03:40 +0100556 */
557 adj_nbr_update_rewrite_internal(adj,
558 IP_LOOKUP_NEXT_MIDCHAIN,
559 adj_get_midchain_node(adj->ia_link),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800560 adj_nbr_midchain_get_tx_node(adj),
Neale Rannsb80c5362016-10-08 13:03:40 +0100561 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100562}
563
564/**
Neale Ranns5e575b12016-10-03 09:40:25 +0100565 * adj_nbr_midchain_unstack
566 *
567 * Unstack the adj. stack it on drop
568 */
569void
570adj_nbr_midchain_unstack (adj_index_t adj_index)
571{
Neale Ranns521a8d72018-12-06 13:46:49 +0000572 fib_node_index_t *entry_indicies, tmp;
Neale Ranns5e575b12016-10-03 09:40:25 +0100573 ip_adjacency_t *adj;
574
575 ASSERT(ADJ_INDEX_INVALID != adj_index);
Neale Ranns521a8d72018-12-06 13:46:49 +0000576 adj = adj_get (adj_index);
Neale Ranns5e575b12016-10-03 09:40:25 +0100577
Neale Ranns521a8d72018-12-06 13:46:49 +0000578 /*
579 * check to see if this unstacking breaks a recursion loop
580 */
581 entry_indicies = NULL;
582 tmp = adj->sub_type.midchain.fei;
583 adj->sub_type.midchain.fei = FIB_NODE_INDEX_INVALID;
584
585 if (FIB_NODE_INDEX_INVALID != tmp)
586 {
587 fib_entry_recursive_loop_detect(tmp, &entry_indicies);
588 vec_free(entry_indicies);
589 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100590
591 /*
592 * stack on the drop
593 */
594 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
Neale Ranns43161a82017-08-12 02:12:00 -0700595 vnet_link_to_dpo_proto(adj->ia_link),
596 &adj->sub_type.midchain.next_dpo,
597 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns5e575b12016-10-03 09:40:25 +0100598 CLIB_MEMORY_BARRIER();
599}
600
Neale Ranns521a8d72018-12-06 13:46:49 +0000601void
602adj_nbr_midchain_stack_on_fib_entry (adj_index_t ai,
603 fib_node_index_t fei,
604 fib_forward_chain_type_t fct)
605{
606 fib_node_index_t *entry_indicies;
607 dpo_id_t tmp = DPO_INVALID;
608 ip_adjacency_t *adj;
609
610 adj = adj_get (ai);
611
612 /*
613 * check to see if this stacking will form a recursion loop
614 */
615 entry_indicies = NULL;
616 adj->sub_type.midchain.fei = fei;
617
618 if (fib_entry_recursive_loop_detect(adj->sub_type.midchain.fei, &entry_indicies))
619 {
620 /*
621 * loop formed, stack on the drop.
622 */
623 dpo_copy(&tmp, drop_dpo_get(fib_forw_chain_type_to_dpo_proto(fct)));
624 }
625 else
626 {
627 fib_entry_contribute_forwarding (fei, fct, &tmp);
628
629 if ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_IP_STACK) &&
630 (DPO_LOAD_BALANCE == tmp.dpoi_type))
631 {
632 /*
633 * do that hash now and stack on the choice.
634 * If the choice is an incomplete adj then we will need a poke when
635 * it becomes complete. This happens since the adj update walk propagates
636 * as far a recursive paths.
637 */
638 const dpo_id_t *choice;
639 load_balance_t *lb;
640 int hash;
641
642 lb = load_balance_get (tmp.dpoi_index);
643
644 if (FIB_FORW_CHAIN_TYPE_UNICAST_IP4 == fct)
645 {
646 hash = ip4_compute_flow_hash ((ip4_header_t *) adj_get_rewrite (ai),
647 lb->lb_hash_config);
648 }
649 else if (FIB_FORW_CHAIN_TYPE_UNICAST_IP6 == fct)
650 {
651 hash = ip6_compute_flow_hash ((ip6_header_t *) adj_get_rewrite (ai),
652 lb->lb_hash_config);
653 }
654 else
655 {
656 hash = 0;
657 ASSERT(0);
658 }
659
660 choice = load_balance_get_bucket_i (lb, hash & lb->lb_n_buckets_minus_1);
661 dpo_copy (&tmp, choice);
662 }
663 }
664 adj_nbr_midchain_stack (ai, &tmp);
665 dpo_reset(&tmp);
666 vec_free(entry_indicies);
667}
668
Neale Ranns5e575b12016-10-03 09:40:25 +0100669/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100670 * adj_nbr_midchain_stack
671 */
672void
673adj_nbr_midchain_stack (adj_index_t adj_index,
674 const dpo_id_t *next)
675{
676 ip_adjacency_t *adj;
677
678 ASSERT(ADJ_INDEX_INVALID != adj_index);
679
680 adj = adj_get(adj_index);
681
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800682 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
683 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100684
Neale Rannsfa5d1982017-02-20 14:19:51 -0800685 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
Neale Ranns5e575b12016-10-03 09:40:25 +0100686 &adj->sub_type.midchain.next_dpo,
687 next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100688}
689
Neale Ranns521a8d72018-12-06 13:46:49 +0000690int
691adj_ndr_midchain_recursive_loop_detect (adj_index_t ai,
692 fib_node_index_t **entry_indicies)
693{
694 fib_node_index_t *entry_index, *entries;
695 ip_adjacency_t * adj;
696
697 adj = adj_get(ai);
698 entries = *entry_indicies;
699
700 vec_foreach(entry_index, entries)
701 {
702 if (*entry_index == adj->sub_type.midchain.fei)
703 {
704 /*
705 * The entry this midchain links to is already in the set
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700706 * of visited entries, this is a loop
Neale Ranns521a8d72018-12-06 13:46:49 +0000707 */
708 adj->ia_flags |= ADJ_FLAG_MIDCHAIN_LOOPED;
709 return (1);
710 }
711 }
712
713 adj->ia_flags &= ~ADJ_FLAG_MIDCHAIN_LOOPED;
714 return (0);
715}
716
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100717u8*
718format_adj_midchain (u8* s, va_list *ap)
719{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400720 index_t index = va_arg(*ap, index_t);
721 u32 indent = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100722 ip_adjacency_t * adj = adj_get(index);
723
Neale Ranns924d03a2016-10-19 08:25:46 +0100724 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Ranns25edf142019-03-22 08:12:48 +0000725 if (adj->rewrite_header.flags & VNET_REWRITE_HAS_FEATURES)
726 s = format(s, " [features]");
Neale Rannsc819fc62018-02-16 02:44:05 -0800727 s = format (s, " via %U",
728 format_ip46_address, &adj->sub_type.nbr.next_hop,
729 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100730 s = format (s, " %U",
Neale Ranns5e575b12016-10-03 09:40:25 +0100731 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400732 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
Neale Ranns521a8d72018-12-06 13:46:49 +0000733 s = format (s, "\n%Ustacked-on",
734 format_white_space, indent);
735
736 if (FIB_NODE_INDEX_INVALID != adj->sub_type.midchain.fei)
737 {
738 s = format (s, " entry:%d", adj->sub_type.midchain.fei);
739
740 }
741 s = format (s, ":\n%U%U",
Neale Ranns43161a82017-08-12 02:12:00 -0700742 format_white_space, indent+2,
743 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100744
745 return (s);
746}
747
748static void
749adj_dpo_lock (dpo_id_t *dpo)
750{
751 adj_lock(dpo->dpoi_index);
752}
753static void
754adj_dpo_unlock (dpo_id_t *dpo)
755{
756 adj_unlock(dpo->dpoi_index);
757}
758
759const static dpo_vft_t adj_midchain_dpo_vft = {
760 .dv_lock = adj_dpo_lock,
761 .dv_unlock = adj_dpo_unlock,
762 .dv_format = format_adj_midchain,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700763 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100764};
765
766/**
767 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
768 * object.
769 *
770 * this means that these graph nodes are ones from which a midchain is the
771 * parent object in the DPO-graph.
772 */
773const static char* const midchain_ip4_nodes[] =
774{
775 "ip4-midchain",
776 NULL,
777};
778const static char* const midchain_ip6_nodes[] =
779{
780 "ip6-midchain",
781 NULL,
782};
783const static char* const midchain_mpls_nodes[] =
784{
785 "mpls-midchain",
786 NULL,
787};
Neale Ranns5e575b12016-10-03 09:40:25 +0100788const static char* const midchain_ethernet_nodes[] =
789{
790 "adj-l2-midchain",
791 NULL,
792};
Florin Corasb69111e2017-02-13 23:55:27 -0800793const static char* const midchain_nsh_nodes[] =
794{
795 "adj-nsh-midchain",
796 NULL,
797};
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100798
799const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
800{
801 [DPO_PROTO_IP4] = midchain_ip4_nodes,
802 [DPO_PROTO_IP6] = midchain_ip6_nodes,
803 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +0100804 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
Florin Corasb69111e2017-02-13 23:55:27 -0800805 [DPO_PROTO_NSH] = midchain_nsh_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100806};
807
808void
809adj_midchain_module_init (void)
810{
811 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
812}