blob: b6cb8240a52bc4600c229d29b093771f8a2750f1 [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,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800463 const void *data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800464 adj_flags_t flags)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100465{
Neale Rannsfa5d1982017-02-20 14:19:51 -0800466 u32 feature_index, tx_node;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100467 ip_adjacency_t *adj;
Damjan Marion8b3191e2016-11-09 19:54:20 +0100468 u8 arc_index;
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100469
470 ASSERT(ADJ_INDEX_INVALID != adj_index);
471
472 adj = adj_get(adj_index);
Neale Rannsb80c5362016-10-08 13:03:40 +0100473
Neale Ranns5e575b12016-10-03 09:40:25 +0100474 adj->sub_type.midchain.fixup_func = fixup;
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800475 adj->sub_type.midchain.fixup_data = data;
Neale Rannsfa5d1982017-02-20 14:19:51 -0800476 adj->ia_flags |= flags;
Neale Ranns5e575b12016-10-03 09:40:25 +0100477
Damjan Marion8b3191e2016-11-09 19:54:20 +0100478 arc_index = adj_midchain_get_feature_arc_index_for_link_type (adj);
Neale Ranns43161a82017-08-12 02:12:00 -0700479 feature_index = adj_nbr_midchain_get_feature_node(adj);
Neale Rannsfa5d1982017-02-20 14:19:51 -0800480 tx_node = adj_nbr_midchain_get_tx_node(adj);
Neale Ranns5e575b12016-10-03 09:40:25 +0100481
Damjan Marion8b3191e2016-11-09 19:54:20 +0100482 vnet_feature_enable_disable_with_index (arc_index, feature_index,
483 adj->rewrite_header.sw_if_index,
484 1 /* enable */, 0, 0);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100485
Neale Rannsb80c5362016-10-08 13:03:40 +0100486 /*
487 * stack the midchain on the drop so it's ready to forward in the adj-midchain-tx.
488 * The graph arc used/created here is from the midchain-tx node to the
489 * child's registered node. This is because post adj processing the next
490 * node are any output features, then the midchain-tx. from there we
491 * need to get to the stacked child's node.
492 */
Neale Rannsfa5d1982017-02-20 14:19:51 -0800493 dpo_stack_from_node(tx_node,
Neale Ranns43161a82017-08-12 02:12:00 -0700494 &adj->sub_type.midchain.next_dpo,
495 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800496}
497
498/**
499 * adj_nbr_midchain_update_rewrite
500 *
501 * Update the adjacency's rewrite string. A NULL string implies the
502 * rewrite is reset (i.e. when ARP/ND etnry is gone).
503 * NB: the adj being updated may be handling traffic in the DP.
504 */
505void
506adj_nbr_midchain_update_rewrite (adj_index_t adj_index,
507 adj_midchain_fixup_t fixup,
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800508 const void *fixup_data,
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800509 adj_flags_t flags,
510 u8 *rewrite)
511{
512 ip_adjacency_t *adj;
513
514 ASSERT(ADJ_INDEX_INVALID != adj_index);
515
516 adj = adj_get(adj_index);
517
518 /*
519 * one time only update. since we don't support chainging the tunnel
520 * src,dst, this is all we need.
521 */
Neale Rannsc819fc62018-02-16 02:44:05 -0800522 ASSERT((adj->lookup_next_index == IP_LOOKUP_NEXT_ARP) ||
523 (adj->lookup_next_index == IP_LOOKUP_NEXT_GLEAN));
524
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800525 /*
526 * tunnels can always provide a rewrite.
527 */
528 ASSERT(NULL != rewrite);
529
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800530 adj_midchain_setup(adj_index, fixup, fixup_data, flags);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100531
Neale Rannsb80c5362016-10-08 13:03:40 +0100532 /*
533 * update the rewirte with the workers paused.
534 */
535 adj_nbr_update_rewrite_internal(adj,
536 IP_LOOKUP_NEXT_MIDCHAIN,
537 adj_get_midchain_node(adj->ia_link),
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800538 adj_nbr_midchain_get_tx_node(adj),
Neale Rannsb80c5362016-10-08 13:03:40 +0100539 rewrite);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100540}
541
542/**
Neale Ranns5e575b12016-10-03 09:40:25 +0100543 * adj_nbr_midchain_unstack
544 *
545 * Unstack the adj. stack it on drop
546 */
547void
548adj_nbr_midchain_unstack (adj_index_t adj_index)
549{
550 ip_adjacency_t *adj;
551
552 ASSERT(ADJ_INDEX_INVALID != adj_index);
553
554 adj = adj_get(adj_index);
555
556 /*
557 * stack on the drop
558 */
559 dpo_stack(DPO_ADJACENCY_MIDCHAIN,
Neale Ranns43161a82017-08-12 02:12:00 -0700560 vnet_link_to_dpo_proto(adj->ia_link),
561 &adj->sub_type.midchain.next_dpo,
562 drop_dpo_get(vnet_link_to_dpo_proto(adj->ia_link)));
Neale Ranns5e575b12016-10-03 09:40:25 +0100563 CLIB_MEMORY_BARRIER();
564}
565
566/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100567 * adj_nbr_midchain_stack
568 */
569void
570adj_nbr_midchain_stack (adj_index_t adj_index,
571 const dpo_id_t *next)
572{
573 ip_adjacency_t *adj;
574
575 ASSERT(ADJ_INDEX_INVALID != adj_index);
576
577 adj = adj_get(adj_index);
578
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800579 ASSERT((IP_LOOKUP_NEXT_MIDCHAIN == adj->lookup_next_index) ||
580 (IP_LOOKUP_NEXT_MCAST_MIDCHAIN == adj->lookup_next_index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100581
Neale Rannsfa5d1982017-02-20 14:19:51 -0800582 dpo_stack_from_node(adj_nbr_midchain_get_tx_node(adj),
Neale Ranns5e575b12016-10-03 09:40:25 +0100583 &adj->sub_type.midchain.next_dpo,
584 next);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100585}
586
587u8*
588format_adj_midchain (u8* s, va_list *ap)
589{
Billy McFallcfcf1e22016-10-14 09:51:49 -0400590 index_t index = va_arg(*ap, index_t);
591 u32 indent = va_arg(*ap, u32);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100592 ip_adjacency_t * adj = adj_get(index);
593
Neale Ranns924d03a2016-10-19 08:25:46 +0100594 s = format (s, "%U", format_vnet_link, adj->ia_link);
Neale Rannsc819fc62018-02-16 02:44:05 -0800595 s = format (s, " via %U",
596 format_ip46_address, &adj->sub_type.nbr.next_hop,
597 adj_proto_to_46(adj->ia_nh_proto));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100598 s = format (s, " %U",
Neale Ranns5e575b12016-10-03 09:40:25 +0100599 format_vnet_rewrite,
Neale Rannsb069a692017-03-15 12:34:25 -0400600 &adj->rewrite_header, sizeof (adj->rewrite_data), indent);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100601 s = format (s, "\n%Ustacked-on:\n%U%U",
Neale Ranns43161a82017-08-12 02:12:00 -0700602 format_white_space, indent,
603 format_white_space, indent+2,
604 format_dpo_id, &adj->sub_type.midchain.next_dpo, indent+2);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100605
606 return (s);
607}
608
609static void
610adj_dpo_lock (dpo_id_t *dpo)
611{
612 adj_lock(dpo->dpoi_index);
613}
614static void
615adj_dpo_unlock (dpo_id_t *dpo)
616{
617 adj_unlock(dpo->dpoi_index);
618}
619
620const static dpo_vft_t adj_midchain_dpo_vft = {
621 .dv_lock = adj_dpo_lock,
622 .dv_unlock = adj_dpo_unlock,
623 .dv_format = format_adj_midchain,
Andrew Yourtchenko5f3fcb92017-10-25 05:50:37 -0700624 .dv_get_urpf = adj_dpo_get_urpf,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100625};
626
627/**
628 * @brief The per-protocol VLIB graph nodes that are assigned to a midchain
629 * object.
630 *
631 * this means that these graph nodes are ones from which a midchain is the
632 * parent object in the DPO-graph.
633 */
634const static char* const midchain_ip4_nodes[] =
635{
636 "ip4-midchain",
637 NULL,
638};
639const static char* const midchain_ip6_nodes[] =
640{
641 "ip6-midchain",
642 NULL,
643};
644const static char* const midchain_mpls_nodes[] =
645{
646 "mpls-midchain",
647 NULL,
648};
Neale Ranns5e575b12016-10-03 09:40:25 +0100649const static char* const midchain_ethernet_nodes[] =
650{
651 "adj-l2-midchain",
652 NULL,
653};
Florin Corasb69111e2017-02-13 23:55:27 -0800654const static char* const midchain_nsh_nodes[] =
655{
656 "adj-nsh-midchain",
657 NULL,
658};
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100659
660const static char* const * const midchain_nodes[DPO_PROTO_NUM] =
661{
662 [DPO_PROTO_IP4] = midchain_ip4_nodes,
663 [DPO_PROTO_IP6] = midchain_ip6_nodes,
664 [DPO_PROTO_MPLS] = midchain_mpls_nodes,
Neale Ranns5e575b12016-10-03 09:40:25 +0100665 [DPO_PROTO_ETHERNET] = midchain_ethernet_nodes,
Florin Corasb69111e2017-02-13 23:55:27 -0800666 [DPO_PROTO_NSH] = midchain_nsh_nodes,
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100667};
668
669void
670adj_midchain_module_init (void)
671{
672 dpo_register(DPO_ADJACENCY_MIDCHAIN, &adj_midchain_dpo_vft, midchain_nodes);
673}