blob: 74ca868e535929790497e57f4556cf0d56572b4b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_output.c : layer 2 output packet processing
3 *
4 * Copyright (c) 2013 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/vnet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070020#include <vnet/ethernet/ethernet.h>
21#include <vlib/cli.h>
22
23#include <vppinfra/error.h>
24#include <vppinfra/hash.h>
Damjan Marion4c276f02021-11-06 13:17:31 +010025#include <vppinfra/vector/count_equal.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070026#include <vnet/l2/feat_bitmap.h>
27#include <vnet/l2/l2_output.h>
28
29
Damjan Marion812b32d2018-05-28 21:26:47 +020030#ifndef CLIB_MARCH_VARIANT
Dave Barach97d8dc22016-08-15 15:31:15 -040031/* Feature graph node names */
32static char *l2output_feat_names[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070033#define _(sym,name) name,
34 foreach_l2output_feat
35#undef _
36};
37
Dave Barach97d8dc22016-08-15 15:31:15 -040038char **
39l2output_get_feat_names (void)
40{
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 return l2output_feat_names;
42}
43
Eyal Bari942402b2017-07-26 11:57:04 +030044u8 *
45format_l2_output_features (u8 * s, va_list * args)
46{
47 static char *display_names[] = {
48#define _(sym,name) #sym,
49 foreach_l2output_feat
50#undef _
51 };
52 u32 feature_bitmap = va_arg (*args, u32);
Neale Ranns5d9df1d2018-11-09 08:19:27 -080053 u32 verbose = va_arg (*args, u32);
Eyal Bari942402b2017-07-26 11:57:04 +030054
55 if (feature_bitmap == 0)
56 {
57 s = format (s, " none configured");
58 return s;
59 }
60
61 int i;
62 for (i = L2OUTPUT_N_FEAT - 1; i >= 0; i--)
Neale Ranns5d9df1d2018-11-09 08:19:27 -080063 {
64 if (feature_bitmap & (1 << i))
65 {
66 if (verbose)
67 s =
68 format (s, "%17s (%s)\n", display_names[i],
69 l2output_feat_names[i]);
70 else
71 s = format (s, "%s ", l2output_feat_names[i]);
72 }
73 }
74
Eyal Bari942402b2017-07-26 11:57:04 +030075 return s;
76}
77
Ed Warnickecb9cada2015-12-08 15:45:58 -070078l2output_main_t l2output_main;
Damjan Marionf3b27462018-05-15 19:51:38 +020079#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Dave Barach97d8dc22016-08-15 15:31:15 -040081typedef struct
82{
83 /* per-pkt trace data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 u8 src[6];
85 u8 dst[6];
86 u32 sw_if_index;
Pavel Kotucek65e84572017-01-16 17:01:56 +010087 u8 raw[12]; /* raw data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070088} l2output_trace_t;
89
90/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040091static u8 *
92format_l2output_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070093{
94 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
95 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040096 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
97
Pavel Kotucek65e84572017-01-16 17:01:56 +010098 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
99 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 t->sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400101 format_ethernet_address, t->dst,
Pavel Kotucek65e84572017-01-16 17:01:56 +0100102 format_ethernet_address, t->src,
103 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
104 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
105 t->raw[10], t->raw[11]);
106
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 return s;
108}
109
110
Dave Barach97d8dc22016-08-15 15:31:15 -0400111static char *l2output_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112#define _(sym,string) string,
113 foreach_l2output_error
114#undef _
115};
116
Dave Barach97d8dc22016-08-15 15:31:15 -0400117/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400118 * Check for split horizon violations.
119 * Return 0 if split horizon check passes, otherwise return non-zero.
Dave Barach97d8dc22016-08-15 15:31:15 -0400120 * Packets should not be transmitted out an interface with the same
Chris Luke16bcf7d2016-09-01 14:31:46 -0400121 * split-horizon group as the input interface, except if the @c shg is 0
Dave Barach97d8dc22016-08-15 15:31:15 -0400122 * in which case the check always passes.
123 */
John Lobeb0b2e2017-07-22 00:21:36 -0400124static_always_inline void
Damjan Marionf3b27462018-05-15 19:51:38 +0200125split_horizon_violation (vlib_node_runtime_t * node, u8 shg,
126 vlib_buffer_t * b, u16 * next)
John Lobeb0b2e2017-07-22 00:21:36 -0400127{
Damjan Marionf3b27462018-05-15 19:51:38 +0200128 if (shg != vnet_buffer (b)->l2.shg)
129 return;
130 next[0] = L2OUTPUT_NEXT_DROP;
131 b->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
John Lobeb0b2e2017-07-22 00:21:36 -0400132}
133
Damjan Mariondc5252b2016-11-24 19:25:01 -0800134static_always_inline void
Damjan Marionf3b27462018-05-15 19:51:38 +0200135l2output_process_batch_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
136 l2_output_config_t * config,
137 vlib_buffer_t ** b, i16 * cdo, u16 * next,
138 u32 n_left, int l2_efp, int l2_vtr, int l2_pbb,
139 int shg_set, int update_feature_bitmap)
Damjan Mariondc5252b2016-11-24 19:25:01 -0800140{
Damjan Marionf3b27462018-05-15 19:51:38 +0200141 while (n_left >= 8)
Damjan Mariondc5252b2016-11-24 19:25:01 -0800142 {
Damjan Marionf3b27462018-05-15 19:51:38 +0200143 vlib_prefetch_buffer_header (b[4], LOAD);
144 vlib_prefetch_buffer_header (b[5], LOAD);
145 vlib_prefetch_buffer_header (b[6], LOAD);
146 vlib_prefetch_buffer_header (b[7], LOAD);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800147
Damjan Marionf3b27462018-05-15 19:51:38 +0200148 /* prefetch eth headers only if we need to touch them */
149 if (l2_vtr || l2_pbb || shg_set)
150 {
Damjan Marionaf7fb042021-07-15 11:54:41 +0200151 clib_prefetch_load (b[4]->data + cdo[4]);
152 clib_prefetch_load (b[5]->data + cdo[5]);
153 clib_prefetch_load (b[6]->data + cdo[6]);
154 clib_prefetch_load (b[7]->data + cdo[7]);
Damjan Marionf3b27462018-05-15 19:51:38 +0200155 }
156
157 if (update_feature_bitmap)
158 {
159 vnet_buffer (b[0])->l2.feature_bitmap = config->feature_bitmap;
160 vnet_buffer (b[1])->l2.feature_bitmap = config->feature_bitmap;
161 vnet_buffer (b[2])->l2.feature_bitmap = config->feature_bitmap;
162 vnet_buffer (b[3])->l2.feature_bitmap = config->feature_bitmap;
163 }
164
165 if (l2_vtr)
166 {
167 int i;
168 for (i = 0; i < 4; i++)
169 {
170 u32 failed1 = l2_efp &&
171 l2_efp_filter_process (b[i], &(config->input_vtr));
172 u32 failed2 = l2_vtr_process (b[i], &(config->output_vtr));
173 if (PREDICT_FALSE (failed1 | failed2))
174 {
175 next[i] = L2OUTPUT_NEXT_DROP;
176 if (failed2)
177 b[i]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
178 if (failed1)
179 b[i]->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
180 }
181 }
182 }
183
184 if (l2_pbb)
185 {
186 int i;
187 for (i = 0; i < 4; i++)
188 if (l2_pbb_process (b[i], &(config->output_pbb_vtr)))
189 {
190 next[i] = L2OUTPUT_NEXT_DROP;
191 b[i]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
192 }
193 }
194
195 if (shg_set)
196 {
197 split_horizon_violation (node, config->shg, b[0], next);
198 split_horizon_violation (node, config->shg, b[1], next + 1);
199 split_horizon_violation (node, config->shg, b[2], next + 2);
200 split_horizon_violation (node, config->shg, b[3], next + 3);
201 }
202 /* next */
203 n_left -= 4;
204 b += 4;
205 next += 4;
206 cdo += 4;
207 }
208
209 while (n_left)
210 {
211 if (update_feature_bitmap)
212 vnet_buffer (b[0])->l2.feature_bitmap = config->feature_bitmap;
213
214 if (l2_vtr)
215 {
216 u32 failed1 = l2_efp &&
217 l2_efp_filter_process (b[0], &(config->input_vtr));
218 u32 failed2 = l2_vtr_process (b[0], &(config->output_vtr));
Damjan Mariondc5252b2016-11-24 19:25:01 -0800219 if (PREDICT_FALSE (failed1 | failed2))
220 {
221 *next = L2OUTPUT_NEXT_DROP;
222 if (failed2)
Damjan Marionf3b27462018-05-15 19:51:38 +0200223 b[0]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800224 if (failed1)
Damjan Marionf3b27462018-05-15 19:51:38 +0200225 b[0]->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800226 }
227 }
Damjan Marionf3b27462018-05-15 19:51:38 +0200228
229 if (l2_pbb && l2_pbb_process (b[0], &(config->output_pbb_vtr)))
Damjan Mariondc5252b2016-11-24 19:25:01 -0800230 {
Damjan Marionf3b27462018-05-15 19:51:38 +0200231 next[0] = L2OUTPUT_NEXT_DROP;
232 b[0]->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800233 }
Damjan Marionf3b27462018-05-15 19:51:38 +0200234
235 if (shg_set)
236 split_horizon_violation (node, config->shg, b[0], next);
237
238 /* next */
239 n_left -= 1;
240 b += 1;
241 next += 1;
Damjan Mariondc5252b2016-11-24 19:25:01 -0800242 }
243}
244
Damjan Marionf3b27462018-05-15 19:51:38 +0200245static_always_inline void
246l2output_set_buffer_error (vlib_buffer_t ** b, u32 n_left, vlib_error_t error)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
Damjan Marionf3b27462018-05-15 19:51:38 +0200248 while (n_left >= 8)
249 {
250 vlib_prefetch_buffer_header (b[4], LOAD);
251 vlib_prefetch_buffer_header (b[5], LOAD);
252 vlib_prefetch_buffer_header (b[6], LOAD);
253 vlib_prefetch_buffer_header (b[7], LOAD);
254 b[0]->error = b[1]->error = b[2]->error = b[3]->error = error;
255 b += 4;
256 n_left -= 4;
257 }
258 while (n_left)
259 {
260 b[0]->error = error;
261 b += 1;
262 n_left -= 1;
263 }
264}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Damjan Marionf3b27462018-05-15 19:51:38 +0200266static_always_inline void
267l2output_process_batch (vlib_main_t * vm, vlib_node_runtime_t * node,
268 l2_output_config_t * config, vlib_buffer_t ** b,
269 i16 * cdo, u16 * next, u32 n_left, int l2_efp,
270 int l2_vtr, int l2_pbb)
271{
272 u32 feature_bitmap = config->feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT;
273 if (config->shg == 0 && feature_bitmap == 0)
Eyal Bariceb8d5a2018-09-02 17:08:22 +0300274 {
275 if ((l2_efp | l2_vtr | l2_pbb) == 0)
276 return;
277 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
278 l2_efp, l2_vtr, l2_pbb, 0, 0);
279 }
Damjan Marionf3b27462018-05-15 19:51:38 +0200280 else if (config->shg == 0)
281 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
282 l2_efp, l2_vtr, l2_pbb, 0, 1);
283 else if (feature_bitmap == 0)
284 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
285 l2_efp, l2_vtr, l2_pbb, 1, 0);
286 else
287 l2output_process_batch_inline (vm, node, config, b, cdo, next, n_left,
288 l2_efp, l2_vtr, l2_pbb, 1, 1);
289}
290
Damjan Marion812b32d2018-05-28 21:26:47 +0200291VLIB_NODE_FN (l2output_node) (vlib_main_t * vm,
292 vlib_node_runtime_t * node,
293 vlib_frame_t * frame)
Damjan Marionf3b27462018-05-15 19:51:38 +0200294{
295 u32 n_left, *from;
296 l2output_main_t *msm = &l2output_main;
297 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
298 u16 nexts[VLIB_FRAME_SIZE];
299 u32 sw_if_indices[VLIB_FRAME_SIZE], *sw_if_index;
300 i16 cur_data_offsets[VLIB_FRAME_SIZE], *cdo;
301 l2_output_config_t *config;
302 u32 feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303
304 from = vlib_frame_vector_args (frame);
Damjan Marionf3b27462018-05-15 19:51:38 +0200305 n_left = frame->n_vectors; /* number of packets to process */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Damjan Marionf3b27462018-05-15 19:51:38 +0200307 vlib_get_buffers (vm, from, bufs, n_left);
308 b = bufs;
309 sw_if_index = sw_if_indices;
310 cdo = cur_data_offsets;
311
312 /* extract data from buffer metadata */
313 while (n_left >= 8)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 {
Damjan Marionf3b27462018-05-15 19:51:38 +0200315 /* Prefetch the buffer header for the N+2 loop iteration */
316 vlib_prefetch_buffer_header (b[4], LOAD);
317 vlib_prefetch_buffer_header (b[5], LOAD);
318 vlib_prefetch_buffer_header (b[6], LOAD);
319 vlib_prefetch_buffer_header (b[7], LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Damjan Marionf3b27462018-05-15 19:51:38 +0200321 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
322 cdo[0] = b[0]->current_data;
323 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
324 cdo[1] = b[1]->current_data;
325 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
326 cdo[2] = b[2]->current_data;
327 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
328 cdo[3] = b[3]->current_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Damjan Marionf3b27462018-05-15 19:51:38 +0200330 /* next */
331 sw_if_index += 4;
332 n_left -= 4;
333 b += 4;
334 cdo += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 }
Damjan Marionf3b27462018-05-15 19:51:38 +0200336 while (n_left)
337 {
338 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
339 cdo[0] = b[0]->current_data;
340
341 /* next */
342 sw_if_index += 1;
343 n_left -= 1;
344 b += 1;
345 cdo += 1;
346 }
347
348 n_left = frame->n_vectors;
349 while (n_left)
350 {
351 u16 count, new_next, *next;
352 u16 off = frame->n_vectors - n_left;
353 b = bufs + off;
354
355 if (n_left >= 4)
356 {
357 vlib_prefetch_buffer_header (b[0], LOAD);
358 vlib_prefetch_buffer_header (b[1], LOAD);
359 vlib_prefetch_buffer_header (b[2], LOAD);
360 vlib_prefetch_buffer_header (b[3], LOAD);
361 }
362
363 sw_if_index = sw_if_indices + off;
364 cdo = cur_data_offsets + off;
365 next = nexts + off;
366
367 count = clib_count_equal_u32 (sw_if_index, n_left);
368 n_left -= count;
369
370 config = vec_elt_at_index (msm->configs, sw_if_index[0]);
371 feature_bitmap = config->feature_bitmap;
372 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
373 new_next = feat_bitmap_get_next_node_index
374 (l2output_main.l2_out_feat_next, feature_bitmap);
375 else
376 new_next = vec_elt (l2output_main.output_node_index_vec,
377 sw_if_index[0]);
378 clib_memset_u16 (nexts + off, new_next, count);
379
380 if (new_next == L2OUTPUT_NEXT_DROP)
381 {
382 l2output_set_buffer_error
383 (b, count, node->errors[L2OUTPUT_ERROR_MAPPING_DROP]);
384 continue;
385 }
386
387 /* VTR */
388 if (config->out_vtr_flag && config->output_vtr.push_and_pop_bytes)
389 {
390 if (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
391 l2output_process_batch (vm, node, config, b, cdo, next, count,
392 /* l2_efp */ 1,
393 /* l2_vtr */ 1,
394 /* l2_pbb */ 0);
395 else
396 l2output_process_batch (vm, node, config, b, cdo, next, count,
397 /* l2_efp */ 0,
398 /* l2_vtr */ 1,
399 /* l2_pbb */ 0);
400 }
401 else if (config->out_vtr_flag &&
402 config->output_pbb_vtr.push_and_pop_bytes)
403 l2output_process_batch (vm, node, config, b, cdo, next, count,
404 /* l2_efp */ 0,
405 /* l2_vtr */ 0,
406 /* l2_pbb */ 1);
407 else
408 l2output_process_batch (vm, node, config, b, cdo, next, count,
409 /* l2_efp */ 0,
410 /* l2_vtr */ 0,
411 /* l2_pbb */ 0);
412 }
413
414
415 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
416 {
417 n_left = frame->n_vectors; /* number of packets to process */
418 b = bufs;
419
420 while (n_left)
421 {
422 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
423 {
424 ethernet_header_t *h;
425 l2output_trace_t *t =
426 vlib_add_trace (vm, node, b[0], sizeof (*t));
427 t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
428 h = vlib_buffer_get_current (b[0]);
Dave Barach178cf492018-11-13 16:34:13 -0500429 clib_memcpy_fast (t->src, h->src_address, 6);
430 clib_memcpy_fast (t->dst, h->dst_address, 6);
431 clib_memcpy_fast (t->raw, &h->type, sizeof (t->raw));
Damjan Marionf3b27462018-05-15 19:51:38 +0200432 }
433 /* next */
434 n_left--;
435 b++;
436 }
437 }
438
439 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
440 vlib_node_increment_counter (vm, l2output_node.index,
441 L2OUTPUT_ERROR_L2OUTPUT, frame->n_vectors);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
443 return frame->n_vectors;
444}
445
Dave Barach97d8dc22016-08-15 15:31:15 -0400446/* *INDENT-OFF* */
Eyal Bari001fd402017-07-16 09:34:53 +0300447VLIB_REGISTER_NODE (l2output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 .name = "l2-output",
449 .vector_size = sizeof (u32),
450 .format_trace = format_l2output_trace,
451 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400452
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 .n_errors = ARRAY_LEN(l2output_error_strings),
454 .error_strings = l2output_error_strings,
455
456 .n_next_nodes = L2OUTPUT_N_NEXT,
457
458 /* edit / add dispositions here */
459 .next_nodes = {
460 [L2OUTPUT_NEXT_DROP] = "error-drop",
John Lo0fc9bc12016-10-27 11:17:02 -0400461 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 },
463};
Dave Barach97d8dc22016-08-15 15:31:15 -0400464/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
John Lo3ef822e2016-06-07 09:14:07 -0400466
John Lo0fc9bc12016-10-27 11:17:02 -0400467#define foreach_l2output_bad_intf_error \
468_(DROP, "L2 output to interface not in L2 mode or deleted")
John Lo3ef822e2016-06-07 09:14:07 -0400469
John Lo0fc9bc12016-10-27 11:17:02 -0400470static char *l2output_bad_intf_error_strings[] = {
John Lo3ef822e2016-06-07 09:14:07 -0400471#define _(sym,string) string,
John Lo0fc9bc12016-10-27 11:17:02 -0400472 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400473#undef _
474};
475
Dave Barach97d8dc22016-08-15 15:31:15 -0400476typedef enum
477{
John Lo0fc9bc12016-10-27 11:17:02 -0400478#define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
479 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400480#undef _
John Lo0fc9bc12016-10-27 11:17:02 -0400481 L2OUTPUT_BAD_INTF_N_ERROR,
482} l2output_bad_intf_error_t;
John Lo3ef822e2016-06-07 09:14:07 -0400483
484
Dave Barach97d8dc22016-08-15 15:31:15 -0400485/**
John Lo0fc9bc12016-10-27 11:17:02 -0400486 * Output node for interfaces/tunnels which was in L2 mode but were changed
487 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
488 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
489 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
490 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
491 * this sw_if_index, l2-output will send packets for this sw_if_index to the
492 * l2-output-bad-intf node which just setup the proper drop reason before
493 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700494 * entries for deleted tunnels won't cause possible packet or memory corruption.
Dave Barach97d8dc22016-08-15 15:31:15 -0400495 */
John Lo3ef822e2016-06-07 09:14:07 -0400496
Damjan Marion6e363512018-08-10 22:39:11 +0200497VLIB_NODE_FN (l2output_bad_intf_node) (vlib_main_t * vm,
498 vlib_node_runtime_t * node,
499 vlib_frame_t * frame)
John Lo3ef822e2016-06-07 09:14:07 -0400500{
Dave Barach97d8dc22016-08-15 15:31:15 -0400501 u32 n_left_from, *from, *to_next;
John Lo3ef822e2016-06-07 09:14:07 -0400502 l2output_next_t next_index = 0;
503
504 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400505 n_left_from = frame->n_vectors; /* number of packets to process */
John Lo3ef822e2016-06-07 09:14:07 -0400506
507 while (n_left_from > 0)
508 {
509 u32 n_left_to_next;
510
Dave Barach97d8dc22016-08-15 15:31:15 -0400511 /* get space to enqueue frame to graph node "next_index" */
512 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
John Lo3ef822e2016-06-07 09:14:07 -0400513
514 while (n_left_from >= 4 && n_left_to_next >= 2)
515 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400516 u32 bi0, bi1;
517 vlib_buffer_t *b0, *b1;
John Lo3ef822e2016-06-07 09:14:07 -0400518
Dave Barach97d8dc22016-08-15 15:31:15 -0400519 to_next[0] = bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400520 to_next[1] = bi1 = from[1];
521 from += 2;
522 to_next += 2;
523 n_left_from -= 2;
524 n_left_to_next -= 2;
525 b0 = vlib_get_buffer (vm, bi0);
526 b1 = vlib_get_buffer (vm, bi1);
John Lo0fc9bc12016-10-27 11:17:02 -0400527 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
528 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
Dave Barach97d8dc22016-08-15 15:31:15 -0400529 }
530
John Lo3ef822e2016-06-07 09:14:07 -0400531 while (n_left_from > 0 && n_left_to_next > 0)
532 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400533 u32 bi0;
534 vlib_buffer_t *b0;
John Lo3ef822e2016-06-07 09:14:07 -0400535
Dave Barach97d8dc22016-08-15 15:31:15 -0400536 bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400537 to_next[0] = bi0;
538 from += 1;
539 to_next += 1;
540 n_left_from -= 1;
541 n_left_to_next -= 1;
542 b0 = vlib_get_buffer (vm, bi0);
John Lo0fc9bc12016-10-27 11:17:02 -0400543 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
John Lo3ef822e2016-06-07 09:14:07 -0400544 }
545
546 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
547 }
548
549 return frame->n_vectors;
550}
551
Dave Barach97d8dc22016-08-15 15:31:15 -0400552/* *INDENT-OFF* */
Damjan Marion6e363512018-08-10 22:39:11 +0200553VLIB_REGISTER_NODE (l2output_bad_intf_node) = {
John Lo0fc9bc12016-10-27 11:17:02 -0400554 .name = "l2-output-bad-intf",
John Lo3ef822e2016-06-07 09:14:07 -0400555 .vector_size = sizeof (u32),
556 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400557
John Lo0fc9bc12016-10-27 11:17:02 -0400558 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
559 .error_strings = l2output_bad_intf_error_strings,
John Lo3ef822e2016-06-07 09:14:07 -0400560
561 .n_next_nodes = 1,
562
563 /* edit / add dispositions here */
564 .next_nodes = {
565 [0] = "error-drop",
566 },
567};
Dave Barach97d8dc22016-08-15 15:31:15 -0400568/* *INDENT-ON* */
John Lo3ef822e2016-06-07 09:14:07 -0400569
Eyal Bari001fd402017-07-16 09:34:53 +0300570static clib_error_t *
571l2output_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572{
Dave Barach97d8dc22016-08-15 15:31:15 -0400573 l2output_main_t *mp = &l2output_main;
574
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -0400576 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 /* Create the config vector */
579 vec_validate (mp->configs, 100);
580 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Dave Barach97d8dc22016-08-15 15:31:15 -0400582 /* Initialize the feature next-node indexes */
583 feat_bitmap_init_next_nodes (vm,
584 l2output_node.index,
585 L2OUTPUT_N_FEAT,
586 l2output_get_feat_names (),
John Lobeb0b2e2017-07-22 00:21:36 -0400587 mp->l2_out_feat_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
Dave Barach97d8dc22016-08-15 15:31:15 -0400589 /* Initialize the output node mapping table */
John Lobeb0b2e2017-07-22 00:21:36 -0400590 vec_validate_init_empty (mp->output_node_index_vec, 100,
John Lob2fd6cb2017-07-12 19:56:45 -0400591 L2OUTPUT_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
593 return 0;
594}
595
596VLIB_INIT_FUNCTION (l2output_init);
597
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598
Damjan Marion6e363512018-08-10 22:39:11 +0200599#ifndef CLIB_MARCH_VARIANT
Chris Luke16bcf7d2016-09-01 14:31:46 -0400600/** Create a mapping in the next node mapping table for the given sw_if_index. */
John Lob2fd6cb2017-07-12 19:56:45 -0400601void
602l2output_create_output_node_mapping (vlib_main_t * vlib_main,
603 vnet_main_t * vnet_main, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400604{
John Lob2fd6cb2017-07-12 19:56:45 -0400605 vnet_hw_interface_t *hw0 =
606 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
Dave Barach97d8dc22016-08-15 15:31:15 -0400608 /* dynamically create graph node arc */
John Lob2fd6cb2017-07-12 19:56:45 -0400609 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
610 hw0->output_node_index);
John Lobeb0b2e2017-07-22 00:21:36 -0400611 l2output_main.output_node_index_vec[sw_if_index] = next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Dave Barach97d8dc22016-08-15 15:31:15 -0400614/* Get a pointer to the config for the given interface */
615l2_output_config_t *
616l2output_intf_config (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617{
Dave Barach97d8dc22016-08-15 15:31:15 -0400618 l2output_main_t *mp = &l2output_main;
619
620 vec_validate (mp->configs, sw_if_index);
621 return vec_elt_at_index (mp->configs, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622}
623
Chris Luke16bcf7d2016-09-01 14:31:46 -0400624/** Enable (or disable) the feature in the bitmap for the given interface. */
Dave Barach97d8dc22016-08-15 15:31:15 -0400625void
Neale Rannsa1179582018-10-24 02:31:51 -0700626l2output_intf_bitmap_enable (u32 sw_if_index,
627 l2output_feat_masks_t feature_bitmap, u32 enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628{
Dave Barach97d8dc22016-08-15 15:31:15 -0400629 l2output_main_t *mp = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 l2_output_config_t *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631
Dave Barach97d8dc22016-08-15 15:31:15 -0400632 vec_validate (mp->configs, sw_if_index);
633 config = vec_elt_at_index (mp->configs, sw_if_index);
634
635 if (enable)
636 {
637 config->feature_bitmap |= feature_bitmap;
638 }
639 else
640 {
641 config->feature_bitmap &= ~feature_bitmap;
642 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643}
Damjan Marionf3b27462018-05-15 19:51:38 +0200644#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400645
646/*
647 * fd.io coding-style-patch-verification: ON
648 *
649 * Local Variables:
650 * eval: (c-set-style "gnu")
651 * End:
652 */