blob: 370fa4652f8ee69e70efcefacfdf769b0ab28fe5 [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>
23#include <vnet/fib/fib_walk.h>
24
Neale Ranns5e575b12016-10-03 09:40:25 +010025/**
26 * The two midchain tx feature node indices
27 */
Neale Ranns924d03a2016-10-19 08:25:46 +010028static u32 adj_midchain_tx_feature_node[VNET_LINK_NUM];
29static u32 adj_midchain_tx_no_count_feature_node[VNET_LINK_NUM];
Neale Ranns5e575b12016-10-03 09:40:25 +010030
31/**
32 * @brief Trace data for packets traversing the midchain tx node
33 */
34typedef struct adj_midchain_tx_trace_t_
35{
36 /**
37 * @brief the midchain adj we are traversing
38 */
39 adj_index_t ai;
40} adj_midchain_tx_trace_t;
41
42always_inline uword
Damjan Marion77766a12016-11-02 01:21:05 +010043adj_midchain_tx_inline (vlib_main_t * vm,
Neale Ranns5e575b12016-10-03 09:40:25 +010044 vlib_node_runtime_t * node,
45 vlib_frame_t * frame,
46 int interface_count)
47{
48 u32 * from, * to_next, n_left_from, n_left_to_next;
49 u32 next_index;
50 vnet_main_t *vnm = vnet_get_main ();
51 vnet_interface_main_t *im = &vnm->interface_main;
Damjan Marion586afd72017-04-05 19:18:20 +020052 u32 thread_index = vm->thread_index;
Neale Ranns5e575b12016-10-03 09:40:25 +010053
54 /* Vector of buffer / pkt indices we're supposed to process */
55 from = vlib_frame_vector_args (frame);
56
57 /* Number of buffers / pkts */
58 n_left_from = frame->n_vectors;
59
60 /* Speculatively send the first buffer to the last disposition we used */
61 next_index = node->cached_next_index;
62
63 while (n_left_from > 0)
64 {
65 /* set up to enqueue to our disposition with index = next_index */
66 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
67
Neale Ranns43161a82017-08-12 02:12:00 -070068 while (n_left_from >= 8 && n_left_to_next > 4)
Neale Rannsd3b85b02016-10-22 15:17:21 -070069 {
70 u32 bi0, adj_index0, next0;
71 const ip_adjacency_t * adj0;
72 const dpo_id_t *dpo0;
73 vlib_buffer_t * b0;
74 u32 bi1, adj_index1, next1;
75 const ip_adjacency_t * adj1;
76 const dpo_id_t *dpo1;
77 vlib_buffer_t * b1;
Neale Ranns43161a82017-08-12 02:12:00 -070078 u32 bi2, adj_index2, next2;
79 const ip_adjacency_t * adj2;
80 const dpo_id_t *dpo2;
81 vlib_buffer_t * b2;
82 u32 bi3, adj_index3, next3;
83 const ip_adjacency_t * adj3;
84 const dpo_id_t *dpo3;
85 vlib_buffer_t * b3;
Neale Rannsd3b85b02016-10-22 15:17:21 -070086
87 /* Prefetch next iteration. */
88 {
Neale Ranns43161a82017-08-12 02:12:00 -070089 vlib_buffer_t * p4, * p5;
90 vlib_buffer_t * p6, * p7;
Neale Rannsd3b85b02016-10-22 15:17:21 -070091
Neale Ranns43161a82017-08-12 02:12:00 -070092 p4 = vlib_get_buffer (vm, from[4]);
93 p5 = vlib_get_buffer (vm, from[5]);
94 p6 = vlib_get_buffer (vm, from[6]);
95 p7 = vlib_get_buffer (vm, from[7]);
Neale Rannsd3b85b02016-10-22 15:17:21 -070096
Neale Ranns43161a82017-08-12 02:12:00 -070097 vlib_prefetch_buffer_header (p4, LOAD);
98 vlib_prefetch_buffer_header (p5, LOAD);
99 vlib_prefetch_buffer_header (p6, LOAD);
100 vlib_prefetch_buffer_header (p7, LOAD);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700101 }
102
103 bi0 = from[0];
104 to_next[0] = bi0;
105 bi1 = from[1];
106 to_next[1] = bi1;
Neale Ranns43161a82017-08-12 02:12:00 -0700107 bi2 = from[2];
108 to_next[2] = bi2;
109 bi3 = from[3];
110 to_next[3] = bi3;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700111
Neale Ranns43161a82017-08-12 02:12:00 -0700112 from += 4;
113 to_next += 4;
114 n_left_from -= 4;
115 n_left_to_next -= 4;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700116
117 b0 = vlib_get_buffer(vm, bi0);
118 b1 = vlib_get_buffer(vm, bi1);
Neale Ranns43161a82017-08-12 02:12:00 -0700119 b2 = vlib_get_buffer(vm, bi2);
120 b3 = vlib_get_buffer(vm, bi3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700121
122 /* Follow the DPO on which the midchain is stacked */
123 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
124 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
Neale Ranns43161a82017-08-12 02:12:00 -0700125 adj_index2 = vnet_buffer(b2)->ip.adj_index[VLIB_TX];
126 adj_index3 = vnet_buffer(b3)->ip.adj_index[VLIB_TX];
Neale Rannsd3b85b02016-10-22 15:17:21 -0700127
128 adj0 = adj_get(adj_index0);
129 adj1 = adj_get(adj_index1);
Neale Ranns43161a82017-08-12 02:12:00 -0700130 adj2 = adj_get(adj_index2);
131 adj3 = adj_get(adj_index3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700132
133 dpo0 = &adj0->sub_type.midchain.next_dpo;
134 dpo1 = &adj1->sub_type.midchain.next_dpo;
Neale Ranns43161a82017-08-12 02:12:00 -0700135 dpo2 = &adj2->sub_type.midchain.next_dpo;
136 dpo3 = &adj3->sub_type.midchain.next_dpo;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700137
138 next0 = dpo0->dpoi_next_node;
139 next1 = dpo1->dpoi_next_node;
Neale Ranns43161a82017-08-12 02:12:00 -0700140 next2 = dpo2->dpoi_next_node;
141 next3 = dpo3->dpoi_next_node;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700142
Neale Ranns43161a82017-08-12 02:12:00 -0700143 vnet_buffer(b1)->ip.adj_index[VLIB_TX] = dpo1->dpoi_index;
144 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
145 vnet_buffer(b2)->ip.adj_index[VLIB_TX] = dpo2->dpoi_index;
146 vnet_buffer(b3)->ip.adj_index[VLIB_TX] = dpo3->dpoi_index;
Neale Rannsd3b85b02016-10-22 15:17:21 -0700147
148 if (interface_count)
149 {
150 vlib_increment_combined_counter (im->combined_sw_if_counters
151 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200152 thread_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700153 adj0->rewrite_header.sw_if_index,
154 1,
155 vlib_buffer_length_in_chain (vm, b0));
156 vlib_increment_combined_counter (im->combined_sw_if_counters
157 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200158 thread_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700159 adj1->rewrite_header.sw_if_index,
160 1,
161 vlib_buffer_length_in_chain (vm, b1));
Neale Ranns43161a82017-08-12 02:12:00 -0700162 vlib_increment_combined_counter (im->combined_sw_if_counters
163 + VNET_INTERFACE_COUNTER_TX,
164 thread_index,
165 adj2->rewrite_header.sw_if_index,
166 1,
167 vlib_buffer_length_in_chain (vm, b2));
168 vlib_increment_combined_counter (im->combined_sw_if_counters
169 + VNET_INTERFACE_COUNTER_TX,
170 thread_index,
171 adj3->rewrite_header.sw_if_index,
172 1,
173 vlib_buffer_length_in_chain (vm, b3));
Neale Rannsd3b85b02016-10-22 15:17:21 -0700174 }
175
176 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
177 {
178 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
179 b0, sizeof (*tr));
180 tr->ai = adj_index0;
181 }
182 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
183 {
184 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
185 b1, sizeof (*tr));
186 tr->ai = adj_index1;
187 }
Neale Ranns43161a82017-08-12 02:12:00 -0700188 if (PREDICT_FALSE(b2->flags & VLIB_BUFFER_IS_TRACED))
189 {
190 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
191 b2, sizeof (*tr));
192 tr->ai = adj_index2;
193 }
194 if (PREDICT_FALSE(b3->flags & VLIB_BUFFER_IS_TRACED))
195 {
196 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
197 b3, sizeof (*tr));
198 tr->ai = adj_index3;
199 }
Neale Rannsd3b85b02016-10-22 15:17:21 -0700200
Neale Ranns43161a82017-08-12 02:12:00 -0700201 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
Neale Rannsd3b85b02016-10-22 15:17:21 -0700202 to_next, n_left_to_next,
Neale Ranns43161a82017-08-12 02:12:00 -0700203 bi0, bi1, bi2, bi3,
204 next0, next1, next2, next3);
Neale Rannsd3b85b02016-10-22 15:17:21 -0700205 }
Neale Ranns5e575b12016-10-03 09:40:25 +0100206 while (n_left_from > 0 && n_left_to_next > 0)
207 {
208 u32 bi0, adj_index0, next0;
209 const ip_adjacency_t * adj0;
210 const dpo_id_t *dpo0;
211 vlib_buffer_t * b0;
212
213 bi0 = from[0];
214 to_next[0] = bi0;
215 from += 1;
216 to_next += 1;
217 n_left_from -= 1;
218 n_left_to_next -= 1;
219
220 b0 = vlib_get_buffer(vm, bi0);
221
222 /* Follow the DPO on which the midchain is stacked */
223 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
224 adj0 = adj_get(adj_index0);
225 dpo0 = &adj0->sub_type.midchain.next_dpo;
226 next0 = dpo0->dpoi_next_node;
Neale Ranns43161a82017-08-12 02:12:00 -0700227 vnet_buffer(b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100228
229 if (interface_count)
230 {
231 vlib_increment_combined_counter (im->combined_sw_if_counters
232 + VNET_INTERFACE_COUNTER_TX,
Damjan Marion586afd72017-04-05 19:18:20 +0200233 thread_index,
Neale Ranns5e575b12016-10-03 09:40:25 +0100234 adj0->rewrite_header.sw_if_index,
235 1,
236 vlib_buffer_length_in_chain (vm, b0));
237 }
238
239 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
240 {
241 adj_midchain_tx_trace_t *tr = vlib_add_trace (vm, node,
242 b0, sizeof (*tr));
243 tr->ai = adj_index0;
244 }
245
246 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
247 to_next, n_left_to_next,
248 bi0, next0);
249 }
250
251 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
252 }
253
Neale Ranns5e575b12016-10-03 09:40:25 +0100254 return frame->n_vectors;
255}
256
257static u8 *
258format_adj_midchain_tx_trace (u8 * s, va_list * args)
259{
260 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
261 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
262 adj_midchain_tx_trace_t *tr = va_arg (*args, adj_midchain_tx_trace_t*);
263
264 s = format(s, "adj-midchain:[%d]:%U", tr->ai,
Neale Rannsb80c5362016-10-08 13:03:40 +0100265 format_ip_adjacency, tr->ai,
Neale Ranns5e575b12016-10-03 09:40:25 +0100266 FORMAT_IP_ADJACENCY_NONE);
267
268 return (s);
269}
270
271static uword
272adj_midchain_tx (vlib_main_t * vm,
273 vlib_node_runtime_t * node,
274 vlib_frame_t * frame)
275{
Damjan Marion77766a12016-11-02 01:21:05 +0100276 return (adj_midchain_tx_inline(vm, node, frame, 1));
Neale Ranns5e575b12016-10-03 09:40:25 +0100277}
278
279VLIB_REGISTER_NODE (adj_midchain_tx_node, static) = {
280 .function = adj_midchain_tx,
281 .name = "adj-midchain-tx",
282 .vector_size = sizeof (u32),
283
284 .format_trace = format_adj_midchain_tx_trace,
285
286 .n_next_nodes = 1,
287 .next_nodes = {
288 [0] = "error-drop",
289 },
290};
291
292static uword
293adj_midchain_tx_no_count (vlib_main_t * vm,
294 vlib_node_runtime_t * node,
295 vlib_frame_t * frame)
296{
Damjan Marion77766a12016-11-02 01:21:05 +0100297 return (adj_midchain_tx_inline(vm, node, frame, 0));
Neale Ranns5e575b12016-10-03 09:40:25 +0100298}
299
300VLIB_REGISTER_NODE (adj_midchain_tx_no_count_node, static) = {
301 .function = adj_midchain_tx_no_count,
302 .name = "adj-midchain-tx-no-count",
303 .vector_size = sizeof (u32),
304
305 .format_trace = format_adj_midchain_tx_trace,
306
307 .n_next_nodes = 1,
308 .next_nodes = {
309 [0] = "error-drop",
310 },
311};
312
Damjan Marion8b3191e2016-11-09 19:54:20 +0100313VNET_FEATURE_INIT (adj_midchain_tx_ip4, static) = {
314 .arc_name = "ip4-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100315 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100316 .runs_before = VNET_FEATURES ("interface-output"),
317 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP4],
Neale Ranns5e575b12016-10-03 09:40:25 +0100318};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100319VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip4, static) = {
320 .arc_name = "ip4-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100321 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100322 .runs_before = VNET_FEATURES ("interface-output"),
323 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP4],
Neale Ranns5e575b12016-10-03 09:40:25 +0100324};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100325VNET_FEATURE_INIT (adj_midchain_tx_ip6, static) = {
326 .arc_name = "ip6-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100327 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100328 .runs_before = VNET_FEATURES ("interface-output"),
329 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_IP6],
Neale Ranns5e575b12016-10-03 09:40:25 +0100330};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100331VNET_FEATURE_INIT (adj_midchain_tx_no_count_ip6, static) = {
332 .arc_name = "ip6-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100333 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100334 .runs_before = VNET_FEATURES ("interface-output"),
335 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_IP6],
Neale Ranns5e575b12016-10-03 09:40:25 +0100336};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100337VNET_FEATURE_INIT (adj_midchain_tx_mpls, static) = {
338 .arc_name = "mpls-output",
Damjan Marion77766a12016-11-02 01:21:05 +0100339 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100340 .runs_before = VNET_FEATURES ("interface-output"),
341 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_MPLS],
Neale Ranns5e575b12016-10-03 09:40:25 +0100342};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100343VNET_FEATURE_INIT (adj_midchain_tx_no_count_mpls, static) = {
344 .arc_name = "mpls-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100345 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100346 .runs_before = VNET_FEATURES ("interface-output"),
347 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_MPLS],
Neale Ranns5e575b12016-10-03 09:40:25 +0100348};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100349VNET_FEATURE_INIT (adj_midchain_tx_ethernet, static) = {
350 .arc_name = "ethernet-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100351 .node_name = "adj-midchain-tx",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100352 .runs_before = VNET_FEATURES ("error-drop"),
353 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_ETHERNET],
Neale Ranns5e575b12016-10-03 09:40:25 +0100354};
Damjan Marion8b3191e2016-11-09 19:54:20 +0100355VNET_FEATURE_INIT (adj_midchain_tx_no_count_ethernet, static) = {
356 .arc_name = "ethernet-output",
Neale Ranns5e575b12016-10-03 09:40:25 +0100357 .node_name = "adj-midchain-tx-no-count",
Damjan Marion8b3191e2016-11-09 19:54:20 +0100358 .runs_before = VNET_FEATURES ("error-drop"),
359 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_ETHERNET],
Neale Ranns5e575b12016-10-03 09:40:25 +0100360};
Florin Corasce1b4c72017-01-26 14:25:34 -0800361VNET_FEATURE_INIT (adj_midchain_tx_nsh, static) = {
362 .arc_name = "nsh-output",
363 .node_name = "adj-midchain-tx",
364 .runs_before = VNET_FEATURES ("error-drop"),
365 .feature_index_ptr = &adj_midchain_tx_feature_node[VNET_LINK_NSH],
366};
367VNET_FEATURE_INIT (adj_midchain_tx_no_count_nsh, static) = {
368 .arc_name = "nsh-output",
369 .node_name = "adj-midchain-tx-no-count",
370 .runs_before = VNET_FEATURES ("error-drop"),
371 .feature_index_ptr = &adj_midchain_tx_no_count_feature_node[VNET_LINK_NSH],
372};
Neale Ranns5e575b12016-10-03 09:40:25 +0100373
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100374static inline u32
Neale Ranns924d03a2016-10-19 08:25:46 +0100375adj_get_midchain_node (vnet_link_t link)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100376{
377 switch (link) {
Neale Ranns924d03a2016-10-19 08:25:46 +0100378 case VNET_LINK_IP4:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100379 return (ip4_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100380 case VNET_LINK_IP6:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100381 return (ip6_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100382 case VNET_LINK_MPLS:
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100383 return (mpls_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100384 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100385 return (adj_l2_midchain_node.index);
Florin Corasce1b4c72017-01-26 14:25:34 -0800386 case VNET_LINK_NSH:
387 return (adj_nsh_midchain_node.index);
Neale Ranns924d03a2016-10-19 08:25:46 +0100388 case VNET_LINK_ARP:
389 break;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100390 }
391 ASSERT(0);
392 return (0);
393}
394
Damjan Marion8b3191e2016-11-09 19:54:20 +0100395static u8
396adj_midchain_get_feature_arc_index_for_link_type (const ip_adjacency_t *adj)
Neale Ranns5e575b12016-10-03 09:40:25 +0100397{
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800398 u8 arc = (u8) ~0;
Neale Ranns5e575b12016-10-03 09:40:25 +0100399 switch (adj->ia_link)
400 {
Neale Ranns924d03a2016-10-19 08:25:46 +0100401 case VNET_LINK_IP4:
Neale Ranns5e575b12016-10-03 09:40:25 +0100402 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100403 arc = ip4_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_IP6:
Neale Ranns5e575b12016-10-03 09:40:25 +0100407 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100408 arc = ip6_main.lookup_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_MPLS:
Neale Ranns5e575b12016-10-03 09:40:25 +0100412 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100413 arc = mpls_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100414 break;
415 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100416 case VNET_LINK_ETHERNET:
Neale Ranns5e575b12016-10-03 09:40:25 +0100417 {
Damjan Marion8b3191e2016-11-09 19:54:20 +0100418 arc = ethernet_main.output_feature_arc_index;
Neale Ranns5e575b12016-10-03 09:40:25 +0100419 break;
420 }
Florin Corasce1b4c72017-01-26 14:25:34 -0800421 case VNET_LINK_NSH:
422 {
423 arc = nsh_main_dummy.output_feature_arc_index;
424 break;
425 }
Neale Ranns924d03a2016-10-19 08:25:46 +0100426 case VNET_LINK_ARP:
427 ASSERT(0);
428 break;
Neale Ranns5e575b12016-10-03 09:40:25 +0100429 }
430
Damjan Marion8b3191e2016-11-09 19:54:20 +0100431 ASSERT (arc != (u8) ~0);
432
433 return (arc);
Neale Ranns5e575b12016-10-03 09:40:25 +0100434}
435
Neale Rannsfa5d1982017-02-20 14:19:51 -0800436static u32
437adj_nbr_midchain_get_tx_node (ip_adjacency_t *adj)
438{
439 return ((adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT) ?
440 adj_midchain_tx_no_count_node.index :
441 adj_midchain_tx_node.index);
442}
443
Neale Ranns43161a82017-08-12 02:12:00 -0700444static u32
445adj_nbr_midchain_get_feature_node (ip_adjacency_t *adj)
446{
447 if (adj->ia_flags & ADJ_FLAG_MIDCHAIN_NO_COUNT)
448 {
449 return (adj_midchain_tx_no_count_feature_node[adj->ia_link]);
450 }
451
452 return (adj_midchain_tx_feature_node[adj->ia_link]);
453}
454
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100455/**
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800456 * adj_midchain_setup
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100457 *
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800458 * Setup the adj as a mid-chain
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100459 */
460void
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800461adj_midchain_setup (adj_index_t adj_index,
462 adj_midchain_fixup_t fixup,
463 adj_flags_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100464{
Neale Rannsfa5d1982017-02-20 14:19:51 -0800465 u32 feature_index, tx_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100466 ip_adjacency_t *adj;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100467 u8 arc_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100468
469 ASSERT(ADJ_INDEX_INVALID != adj_index);
470
471 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100472
Neale Ranns5e575b12016-10-03 09:40:25 +0100473 adj->sub_type.midchain.fixup_func = fixup;
Neale Rannsfa5d1982017-02-20 14:19:51 -0800474 adj->ia_flags |= flags;
Neale Ranns5e575b12016-10-03 09:40:25 +0100475
Damjan Marion8b3191e2016-11-09 19:54:20 +0100476 arc_index = adj_midchain_get_feature_arc_index_for_link_type (adj);
Neale Ranns43161a82017-08-12 02:12:00 -0700477 feature_index = adj_nbr_midchain_get_feature_node(adj);
Neale Rannsfa5d1982017-02-20 14:19:51 -0800478 tx_node = adj_nbr_midchain_get_tx_node(adj);
Neale Ranns5e575b12016-10-03 09:40:25 +0100479
Damjan Marion8b3191e2016-11-09 19:54:20 +0100480 vnet_feature_enable_disable_with_index (arc_index, feature_index,
481 adj->rewrite_header.sw_if_index,
482 1 /* enable */, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100483
Neale Rannsb80c5362016-10-08 13:03:40 +0100484 /*
485 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
486 * The graph arc used/created here is from the midchain-tx node to the
487 * child's registered node. This is because post adj processing the next
488 * node are any output features, then the midchain-tx. from there we
489 * need to get to the stacked child's node.
490 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800491 dpo_stack_from_node(tx_node,
Neale Ranns43161a82017-08-12 02:12:00 -0700492 &adj->sub_type.midchain.next_dpo,
493 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800494}
495
496/**
497 * adj_nbr_midchain_update_rewrite
498 *
499 * Update the adjacency's rewrite string. A NULL string implies the
500 * rewrite is reset (i.e. when ARP/ND etnry is gone).
501 * NB: the adj being updated may be handling traffic in the DP.
502 */
503void
504adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
505 adj_midchain_fixup_t fixup,
506 adj_flags_t flags,
507 u8 *rewrite)
508{
509 ip_adjacency_t *adj;
510
511 ASSERT(ADJ_INDEX_INVALID != adj_index);
512
513 adj = adj_get(adj_index);
514
515 /*
516 * one time only update. since we don't support chainging the tunnel
517 * src,dst, this is all we need.
518 */
519 ASSERT(adj->lookup_next_index == IP_LOOKUP_NEXT_ARP);
520 /*
521 * tunnels can always provide a rewrite.
522 */
523 ASSERT(NULL != rewrite);
524
525 adj_midchain_setup(adj_index, fixup, flags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100526
Neale Rannsb80c5362016-10-08 13:03:40 +0100527 /*
528 * update the rewirte with the workers paused.
529 */
530 adj_nbr_update_rewrite_internal(adj,
531 IP_LOOKUP_NEXT_MIDCHAIN,
532 adj_get_midchain_node(adj->ia_link),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800533 adj_nbr_midchain_get_tx_node(adj),
Neale Rannsb80c5362016-10-08 13:03:40 +0100534 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100535}
536
537/**
Neale Ranns5e575b12016-10-03 09:40:25 +0100538 * adj_nbr_midchain_unstack
539 *
540 * Unstack the adj. stack it on drop
541 */
542void
543adj_nbr_midchain_unstack (adj_index_t adj_index)
544{
545 ip_adjacency_t *adj;
546
547 ASSERT(ADJ_INDEX_INVALID != adj_index);
548
549 adj = adj_get(adj_index);
550
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
561/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100562 * adj_nbr_midchain_stack
563 */
564void
565adj_nbr_midchain_stack (adj_index_t adj_index,
566 const dpo_id_t *next)
567{
568 ip_adjacency_t *adj;
569
570 ASSERT(ADJ_INDEX_INVALID != adj_index);
571
572 adj = adj_get(adj_index);
573
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800574 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
575 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100576
Neale Rannsfa5d1982017-02-20 14:19:51 -0800577 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
Neale Ranns5e575b12016-10-03 09:40:25 +0100578 &adj->sub_type.midchain.next_dpo,
579 next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100580}
581
582u8*
583format_adj_midchain (u8* s, va_list *ap)
584{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400585 index_t index = va_arg(*ap, index_t);
586 u32 indent = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100587 ip_adjacency_t * adj = adj_get(index);
588
Neale Ranns924d03a2016-10-19 08:25:46 +0100589 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100590 s = format (s, " via %U ",
591 format_ip46_address, &adj->sub_type.nbr.next_hop);
592 s = format (s, " %U",
Neale Ranns5e575b12016-10-03 09:40:25 +0100593 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400594 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100595 s = format (s, "\n%Ustacked-on:\n%U%U",
Neale Ranns43161a82017-08-12 02:12:00 -0700596 format_white_space, indent,
597 format_white_space, indent+2,
598 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100599
600 return (s);
601}
602
603static void
604adj_dpo_lock (dpo_id_t *dpo)
605{
606 adj_lock(dpo->dpoi_index);
607}
608static void
609adj_dpo_unlock (dpo_id_t *dpo)
610{
611 adj_unlock(dpo->dpoi_index);
612}
613
614const static dpo_vft_t adj_midchain_dpo_vft = {
615 .dv_lock = adj_dpo_lock,
616 .dv_unlock = adj_dpo_unlock,
617 .dv_format = format_adj_midchain,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700618 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100619};
620
621/**
622 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
623 * object.
624 *
625 * this means that these graph nodes are ones from which a midchain is the
626 * parent object in the DPO-graph.
627 */
628const static char* const midchain_ip4_nodes[] =
629{
630 "ip4-midchain",
631 NULL,
632};
633const static char* const midchain_ip6_nodes[] =
634{
635 "ip6-midchain",
636 NULL,
637};
638const static char* const midchain_mpls_nodes[] =
639{
640 "mpls-midchain",
641 NULL,
642};
Neale Ranns5e575b12016-10-03 09:40:25 +0100643const static char* const midchain_ethernet_nodes[] =
644{
645 "adj-l2-midchain",
646 NULL,
647};
Florin Corasb69111e2017-02-13 23:55:27 -0800648const static char* const midchain_nsh_nodes[] =
649{
650 "adj-nsh-midchain",
651 NULL,
652};
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100653
654const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
655{
656 [DPO_PROTO_IP4] = midchain_ip4_nodes,
657 [DPO_PROTO_IP6] = midchain_ip6_nodes,
658 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +0100659 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
Florin Corasb69111e2017-02-13 23:55:27 -0800660 [DPO_PROTO_NSH] = midchain_nsh_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100661};
662
663void
664adj_midchain_module_init (void)
665{
666 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
667}