blob: 500fc5d0e95b5b0baf84235c8b4e3bdeba4a414b [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>
20#include <vnet/pg/pg.h>
21#include <vnet/ethernet/ethernet.h>
22#include <vlib/cli.h>
23
24#include <vppinfra/error.h>
25#include <vppinfra/hash.h>
26#include <vnet/l2/feat_bitmap.h>
27#include <vnet/l2/l2_output.h>
28
29
Dave Barach97d8dc22016-08-15 15:31:15 -040030/* Feature graph node names */
31static char *l2output_feat_names[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070032#define _(sym,name) name,
33 foreach_l2output_feat
34#undef _
35};
36
Dave Barach97d8dc22016-08-15 15:31:15 -040037char **
38l2output_get_feat_names (void)
39{
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 return l2output_feat_names;
41}
42
Eyal Bari942402b2017-07-26 11:57:04 +030043u8 *
44format_l2_output_features (u8 * s, va_list * args)
45{
46 static char *display_names[] = {
47#define _(sym,name) #sym,
48 foreach_l2output_feat
49#undef _
50 };
51 u32 feature_bitmap = va_arg (*args, u32);
52
53 if (feature_bitmap == 0)
54 {
55 s = format (s, " none configured");
56 return s;
57 }
58
59 int i;
60 for (i = L2OUTPUT_N_FEAT - 1; i >= 0; i--)
61 if (feature_bitmap & (1 << i))
62 s = format (s, "%10s (%s)\n", display_names[i], l2output_feat_names[i]);
63 return s;
64}
65
Ed Warnickecb9cada2015-12-08 15:45:58 -070066l2output_main_t l2output_main;
67
Dave Barach97d8dc22016-08-15 15:31:15 -040068typedef struct
69{
70 /* per-pkt trace data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 u8 src[6];
72 u8 dst[6];
73 u32 sw_if_index;
Pavel Kotucek65e84572017-01-16 17:01:56 +010074 u8 raw[12]; /* raw data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070075} l2output_trace_t;
76
77/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040078static u8 *
79format_l2output_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
81 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
82 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040083 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
84
Pavel Kotucek65e84572017-01-16 17:01:56 +010085 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
86 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 t->sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -040088 format_ethernet_address, t->dst,
Pavel Kotucek65e84572017-01-16 17:01:56 +010089 format_ethernet_address, t->src,
90 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
91 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
92 t->raw[10], t->raw[11]);
93
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 return s;
95}
96
97
Dave Barach97d8dc22016-08-15 15:31:15 -040098static char *l2output_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070099#define _(sym,string) string,
100 foreach_l2output_error
101#undef _
102};
103
Dave Barach97d8dc22016-08-15 15:31:15 -0400104/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400105 * Check for split horizon violations.
106 * Return 0 if split horizon check passes, otherwise return non-zero.
Dave Barach97d8dc22016-08-15 15:31:15 -0400107 * Packets should not be transmitted out an interface with the same
Chris Luke16bcf7d2016-09-01 14:31:46 -0400108 * split-horizon group as the input interface, except if the @c shg is 0
Dave Barach97d8dc22016-08-15 15:31:15 -0400109 * in which case the check always passes.
110 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111static_always_inline u32
112split_horizon_violation (u8 shg1, u8 shg2)
113{
Dave Barach97d8dc22016-08-15 15:31:15 -0400114 if (PREDICT_TRUE (shg1 == 0))
115 {
116 return 0;
117 }
118 else
119 {
120 return shg1 == shg2;
121 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122}
123
John Lobeb0b2e2017-07-22 00:21:36 -0400124/** Determine the next L2 node based on the output feature bitmap */
125static_always_inline void
126l2_output_dispatch (vlib_buffer_t * b0, vlib_node_runtime_t * node,
127 u32 * cached_sw_if_index, u32 * cached_next_index,
128 u32 sw_if_index, u32 feature_bitmap, u32 * next0)
129{
130 /*
131 * The output feature bitmap always have at least the L2 output bit set
132 * for a normal L2 interface (or 0 if the interface is changed from L2
133 * to L3 mode). So if the feature bitmap is 0 or just have L2 output bits set,
134 * we know there is no more feature and will just output packets on interface.
135 * Otherwise, get the index of the next feature node.
136 */
137 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
138 {
139 /* Save bitmap for the next feature graph nodes */
140 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
141
142 /* Determine the next node */
143 *next0 =
144 feat_bitmap_get_next_node_index (l2output_main.l2_out_feat_next,
145 feature_bitmap);
146 }
147 else
148 {
149 /*
150 * There are no features. Send packet to TX node for sw_if_index0
151 * This is a little tricky in that the output interface next node indexes
152 * are not precomputed at init time.
153 */
154
155 if (sw_if_index == *cached_sw_if_index)
156 {
157 /* We hit in the one-entry cache. Use it. */
158 *next0 = *cached_next_index;
159 }
160 else
161 {
162 /* Look up the output TX node for the sw_if_index */
163 *next0 = vec_elt (l2output_main.output_node_index_vec, sw_if_index);
164
165 if (PREDICT_FALSE (*next0 == L2OUTPUT_NEXT_DROP))
166 b0->error = node->errors[L2OUTPUT_ERROR_MAPPING_DROP];
167
168 /* Update the one-entry cache */
169 *cached_sw_if_index = sw_if_index;
170 *cached_next_index = *next0;
171 }
172 }
173}
174
Damjan Mariondc5252b2016-11-24 19:25:01 -0800175static_always_inline void
176l2output_vtr (vlib_node_runtime_t * node, l2_output_config_t * config,
177 u32 feature_bitmap, vlib_buffer_t * b, u32 * next)
178{
179 if (PREDICT_FALSE (config->out_vtr_flag))
180 {
181 /* Perform pre-vtr EFP filter check if configured */
182 if (config->output_vtr.push_and_pop_bytes)
183 {
184 /*
185 * Perform output vlan tag rewrite and the pre-vtr EFP filter check.
186 * The EFP Filter only needs to be run if there is an output VTR
187 * configured. The flag for the post-vtr EFP Filter node is used
188 * to trigger the pre-vtr check as well.
189 */
190 u32 failed1 = (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
191 && (l2_efp_filter_process (b, &(config->input_vtr)));
192 u32 failed2 = l2_vtr_process (b, &(config->output_vtr));
193
194 if (PREDICT_FALSE (failed1 | failed2))
195 {
196 *next = L2OUTPUT_NEXT_DROP;
197 if (failed2)
198 {
199 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
200 }
201 if (failed1)
202 {
203 b->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
204 }
205 }
206 }
207 // perform the PBB rewrite
208 else if (config->output_pbb_vtr.push_and_pop_bytes)
209 {
210 u32 failed = l2_pbb_process (b, &(config->output_pbb_vtr));
211 if (PREDICT_FALSE (failed))
212 {
213 *next = L2OUTPUT_NEXT_DROP;
214 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
215 }
216 }
217 }
218}
219
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barach681abe42017-02-15 09:01:01 -0500221static_always_inline uword
222l2output_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
223 vlib_frame_t * frame, int do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224{
Dave Barach97d8dc22016-08-15 15:31:15 -0400225 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 l2output_next_t next_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400227 l2output_main_t *msm = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 u32 cached_sw_if_index;
229 u32 cached_next_index;
230
231 /* Invalidate cache */
232 cached_sw_if_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400233 cached_next_index = ~0; /* warning be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
235 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400236 n_left_from = frame->n_vectors; /* number of packets to process */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 next_index = node->cached_next_index;
238
239 while (n_left_from > 0)
240 {
241 u32 n_left_to_next;
242
243 /* get space to enqueue frame to graph node "next_index" */
Dave Barach97d8dc22016-08-15 15:31:15 -0400244 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Damjan Mariondc5252b2016-11-24 19:25:01 -0800246 while (n_left_from >= 8 && n_left_to_next >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 {
Damjan Mariondc5252b2016-11-24 19:25:01 -0800248 u32 bi0, bi1, bi2, bi3;
249 vlib_buffer_t *b0, *b1, *b2, *b3;
250 u32 next0, next1, next2, next3;
251 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
252 ethernet_header_t *h0, *h1, *h2, *h3;
253 l2_output_config_t *config0, *config1, *config2, *config3;
Dave Barach97d8dc22016-08-15 15:31:15 -0400254 u32 feature_bitmap0, feature_bitmap1;
Damjan Mariondc5252b2016-11-24 19:25:01 -0800255 u32 feature_bitmap2, feature_bitmap3;
Dave Barach97d8dc22016-08-15 15:31:15 -0400256
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257 /* Prefetch next iteration. */
258 {
Damjan Mariondc5252b2016-11-24 19:25:01 -0800259 vlib_buffer_t *p4, *p5, *p6, *p7;
Dave Barach97d8dc22016-08-15 15:31:15 -0400260
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 p4 = vlib_get_buffer (vm, from[4]);
262 p5 = vlib_get_buffer (vm, from[5]);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800263 p6 = vlib_get_buffer (vm, from[6]);
264 p7 = vlib_get_buffer (vm, from[7]);
Dave Barach97d8dc22016-08-15 15:31:15 -0400265
266 /* Prefetch the buffer header for the N+2 loop iteration */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 vlib_prefetch_buffer_header (p4, LOAD);
268 vlib_prefetch_buffer_header (p5, LOAD);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800269 vlib_prefetch_buffer_header (p6, LOAD);
270 vlib_prefetch_buffer_header (p7, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 }
272
Dave Barach97d8dc22016-08-15 15:31:15 -0400273 /* speculatively enqueue b0 and b1 to the current next frame */
274 /* bi is "buffer index", b is pointer to the buffer */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 to_next[0] = bi0 = from[0];
276 to_next[1] = bi1 = from[1];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800277 to_next[2] = bi2 = from[2];
278 to_next[3] = bi3 = from[3];
279 from += 4;
280 to_next += 4;
281 n_left_from -= 4;
282 n_left_to_next -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
284 b0 = vlib_get_buffer (vm, bi0);
285 b1 = vlib_get_buffer (vm, bi1);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800286 b2 = vlib_get_buffer (vm, bi2);
287 b3 = vlib_get_buffer (vm, bi3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach97d8dc22016-08-15 15:31:15 -0400289 /* TX interface handles */
290 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
291 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800292 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
293 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Damjan Mariondc5252b2016-11-24 19:25:01 -0800295 vlib_node_increment_counter (vm, l2output_node.index,
296 L2OUTPUT_ERROR_L2OUTPUT, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297
Dave Barach97d8dc22016-08-15 15:31:15 -0400298 /* Get config for the output interface */
299 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
300 config1 = vec_elt_at_index (msm->configs, sw_if_index1);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800301 config2 = vec_elt_at_index (msm->configs, sw_if_index2);
302 config3 = vec_elt_at_index (msm->configs, sw_if_index3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400303
304 /*
305 * Get features from the config
306 * TODO: mask out any non-applicable features
307 */
308 feature_bitmap0 = config0->feature_bitmap;
309 feature_bitmap1 = config1->feature_bitmap;
Damjan Mariondc5252b2016-11-24 19:25:01 -0800310 feature_bitmap2 = config2->feature_bitmap;
311 feature_bitmap3 = config3->feature_bitmap;
Dave Barach97d8dc22016-08-15 15:31:15 -0400312
313 /* Determine next node */
John Lobeb0b2e2017-07-22 00:21:36 -0400314 l2_output_dispatch (b0, node, &cached_sw_if_index,
315 &cached_next_index, sw_if_index0,
316 feature_bitmap0, &next0);
317 l2_output_dispatch (b1, node, &cached_sw_if_index,
318 &cached_next_index, sw_if_index1,
319 feature_bitmap1, &next1);
320 l2_output_dispatch (b2, node, &cached_sw_if_index,
321 &cached_next_index, sw_if_index2,
322 feature_bitmap2, &next2);
323 l2_output_dispatch (b3, node, &cached_sw_if_index,
324 &cached_next_index, sw_if_index3,
325 feature_bitmap3, &next3);
Pavel Kotucek95300d12016-08-26 16:11:36 +0200326
Damjan Mariondc5252b2016-11-24 19:25:01 -0800327 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
328 l2output_vtr (node, config1, feature_bitmap1, b1, &next1);
329 l2output_vtr (node, config2, feature_bitmap2, b2, &next2);
330 l2output_vtr (node, config3, feature_bitmap3, b3, &next3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400331
Pavel Kotucek65e84572017-01-16 17:01:56 +0100332 if (do_trace)
333 {
334 h0 = vlib_buffer_get_current (b0);
335 h1 = vlib_buffer_get_current (b1);
336 h2 = vlib_buffer_get_current (b2);
337 h3 = vlib_buffer_get_current (b3);
338 if (b0->flags & VLIB_BUFFER_IS_TRACED)
339 {
340 l2output_trace_t *t =
341 vlib_add_trace (vm, node, b0, sizeof (*t));
342 t->sw_if_index = sw_if_index0;
343 clib_memcpy (t->src, h0->src_address, 6);
344 clib_memcpy (t->dst, h0->dst_address, 6);
345 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
346 }
347 if (b1->flags & VLIB_BUFFER_IS_TRACED)
348 {
349 l2output_trace_t *t =
350 vlib_add_trace (vm, node, b1, sizeof (*t));
351 t->sw_if_index = sw_if_index1;
352 clib_memcpy (t->src, h1->src_address, 6);
353 clib_memcpy (t->dst, h1->dst_address, 6);
354 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
355 }
356 if (b2->flags & VLIB_BUFFER_IS_TRACED)
357 {
358 l2output_trace_t *t =
359 vlib_add_trace (vm, node, b2, sizeof (*t));
360 t->sw_if_index = sw_if_index2;
361 clib_memcpy (t->src, h2->src_address, 6);
362 clib_memcpy (t->dst, h2->dst_address, 6);
363 clib_memcpy (t->raw, &h2->type, sizeof (t->raw));
364 }
365 if (b3->flags & VLIB_BUFFER_IS_TRACED)
366 {
367 l2output_trace_t *t =
368 vlib_add_trace (vm, node, b3, sizeof (*t));
369 t->sw_if_index = sw_if_index3;
370 clib_memcpy (t->src, h3->src_address, 6);
371 clib_memcpy (t->dst, h3->dst_address, 6);
372 clib_memcpy (t->raw, &h3->type, sizeof (t->raw));
373 }
374 }
375
Dave Barach97d8dc22016-08-15 15:31:15 -0400376 /*
377 * Perform the split horizon check
378 * The check can only fail for non-zero shg's
379 */
Damjan Mariondc5252b2016-11-24 19:25:01 -0800380 if (PREDICT_FALSE (config0->shg + config1->shg +
381 config2->shg + config3->shg))
Dave Barach97d8dc22016-08-15 15:31:15 -0400382 {
383 /* one of the checks might fail, check both */
384 if (split_horizon_violation
385 (config0->shg, vnet_buffer (b0)->l2.shg))
386 {
387 next0 = L2OUTPUT_NEXT_DROP;
388 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
389 }
390 if (split_horizon_violation
391 (config1->shg, vnet_buffer (b1)->l2.shg))
392 {
393 next1 = L2OUTPUT_NEXT_DROP;
394 b1->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
395 }
Damjan Mariondc5252b2016-11-24 19:25:01 -0800396 if (split_horizon_violation
397 (config2->shg, vnet_buffer (b2)->l2.shg))
398 {
399 next2 = L2OUTPUT_NEXT_DROP;
400 b2->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
401 }
402 if (split_horizon_violation
403 (config3->shg, vnet_buffer (b3)->l2.shg))
404 {
405 next3 = L2OUTPUT_NEXT_DROP;
406 b3->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
407 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400408 }
409
410 /* verify speculative enqueues, maybe switch current next frame */
411 /* if next0==next1==next_index then nothing special needs to be done */
Damjan Mariondc5252b2016-11-24 19:25:01 -0800412 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400413 to_next, n_left_to_next,
Damjan Mariondc5252b2016-11-24 19:25:01 -0800414 bi0, bi1, bi2, bi3,
415 next0, next1, next2, next3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400416 }
417
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 while (n_left_from > 0 && n_left_to_next > 0)
419 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400420 u32 bi0;
421 vlib_buffer_t *b0;
422 u32 next0;
423 u32 sw_if_index0;
424 ethernet_header_t *h0;
425 l2_output_config_t *config0;
426 u32 feature_bitmap0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Dave Barach97d8dc22016-08-15 15:31:15 -0400428 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 bi0 = from[0];
430 to_next[0] = bi0;
431 from += 1;
432 to_next += 1;
433 n_left_from -= 1;
434 n_left_to_next -= 1;
435
436 b0 = vlib_get_buffer (vm, bi0);
437
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Damjan Mariondc5252b2016-11-24 19:25:01 -0800440 vlib_node_increment_counter (vm, l2output_node.index,
441 L2OUTPUT_ERROR_L2OUTPUT, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Dave Barach97d8dc22016-08-15 15:31:15 -0400443 /* Get config for the output interface */
444 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Dave Barach97d8dc22016-08-15 15:31:15 -0400446 /*
447 * Get features from the config
448 * TODO: mask out any non-applicable features
449 */
450 feature_bitmap0 = config0->feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
Dave Barach97d8dc22016-08-15 15:31:15 -0400452 /* Determine next node */
John Lobeb0b2e2017-07-22 00:21:36 -0400453 l2_output_dispatch (b0, node, &cached_sw_if_index,
454 &cached_next_index, sw_if_index0,
455 feature_bitmap0, &next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Damjan Mariondc5252b2016-11-24 19:25:01 -0800457 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
Pavel Kotucek65e84572017-01-16 17:01:56 +0100459 if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
460 {
461 l2output_trace_t *t =
462 vlib_add_trace (vm, node, b0, sizeof (*t));
463 t->sw_if_index = sw_if_index0;
464 h0 = vlib_buffer_get_current (b0);
465 clib_memcpy (t->src, h0->src_address, 6);
466 clib_memcpy (t->dst, h0->dst_address, 6);
467 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
468 }
469
Dave Barach97d8dc22016-08-15 15:31:15 -0400470 /* Perform the split horizon check */
471 if (PREDICT_FALSE
472 (split_horizon_violation
473 (config0->shg, vnet_buffer (b0)->l2.shg)))
474 {
475 next0 = L2OUTPUT_NEXT_DROP;
476 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
477 }
478
479 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
481 to_next, n_left_to_next,
482 bi0, next0);
483 }
484
485 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
486 }
487
488 return frame->n_vectors;
489}
490
Dave Barach681abe42017-02-15 09:01:01 -0500491static uword
492l2output_node_fn (vlib_main_t * vm,
493 vlib_node_runtime_t * node, vlib_frame_t * frame)
494{
495 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
496 return l2output_node_inline (vm, node, frame, 1 /* do_trace */ );
497 return l2output_node_inline (vm, node, frame, 0 /* do_trace */ );
498}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
Dave Barach97d8dc22016-08-15 15:31:15 -0400500/* *INDENT-OFF* */
Eyal Bari001fd402017-07-16 09:34:53 +0300501VLIB_REGISTER_NODE (l2output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 .function = l2output_node_fn,
503 .name = "l2-output",
504 .vector_size = sizeof (u32),
505 .format_trace = format_l2output_trace,
506 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400507
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 .n_errors = ARRAY_LEN(l2output_error_strings),
509 .error_strings = l2output_error_strings,
510
511 .n_next_nodes = L2OUTPUT_N_NEXT,
512
513 /* edit / add dispositions here */
514 .next_nodes = {
515 [L2OUTPUT_NEXT_DROP] = "error-drop",
John Lo0fc9bc12016-10-27 11:17:02 -0400516 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 },
518};
Eyal Bari001fd402017-07-16 09:34:53 +0300519
520VLIB_NODE_FUNCTION_MULTIARCH (l2output_node, l2output_node_fn);
Dave Barach97d8dc22016-08-15 15:31:15 -0400521/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522
John Lo3ef822e2016-06-07 09:14:07 -0400523
John Lo0fc9bc12016-10-27 11:17:02 -0400524#define foreach_l2output_bad_intf_error \
525_(DROP, "L2 output to interface not in L2 mode or deleted")
John Lo3ef822e2016-06-07 09:14:07 -0400526
John Lo0fc9bc12016-10-27 11:17:02 -0400527static char *l2output_bad_intf_error_strings[] = {
John Lo3ef822e2016-06-07 09:14:07 -0400528#define _(sym,string) string,
John Lo0fc9bc12016-10-27 11:17:02 -0400529 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400530#undef _
531};
532
Dave Barach97d8dc22016-08-15 15:31:15 -0400533typedef enum
534{
John Lo0fc9bc12016-10-27 11:17:02 -0400535#define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
536 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400537#undef _
John Lo0fc9bc12016-10-27 11:17:02 -0400538 L2OUTPUT_BAD_INTF_N_ERROR,
539} l2output_bad_intf_error_t;
John Lo3ef822e2016-06-07 09:14:07 -0400540
541
Dave Barach97d8dc22016-08-15 15:31:15 -0400542/**
John Lo0fc9bc12016-10-27 11:17:02 -0400543 * Output node for interfaces/tunnels which was in L2 mode but were changed
544 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
545 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
546 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
547 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
548 * this sw_if_index, l2-output will send packets for this sw_if_index to the
549 * l2-output-bad-intf node which just setup the proper drop reason before
550 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
551 * entries for delted tunnels won't cause possible packet or memory corrpution.
Dave Barach97d8dc22016-08-15 15:31:15 -0400552 */
John Lo0fc9bc12016-10-27 11:17:02 -0400553static vlib_node_registration_t l2output_bad_intf_node;
John Lo3ef822e2016-06-07 09:14:07 -0400554
555static uword
John Lo0fc9bc12016-10-27 11:17:02 -0400556l2output_bad_intf_node_fn (vlib_main_t * vm,
557 vlib_node_runtime_t * node, vlib_frame_t * frame)
John Lo3ef822e2016-06-07 09:14:07 -0400558{
Dave Barach97d8dc22016-08-15 15:31:15 -0400559 u32 n_left_from, *from, *to_next;
John Lo3ef822e2016-06-07 09:14:07 -0400560 l2output_next_t next_index = 0;
561
562 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400563 n_left_from = frame->n_vectors; /* number of packets to process */
John Lo3ef822e2016-06-07 09:14:07 -0400564
565 while (n_left_from > 0)
566 {
567 u32 n_left_to_next;
568
Dave Barach97d8dc22016-08-15 15:31:15 -0400569 /* get space to enqueue frame to graph node "next_index" */
570 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
John Lo3ef822e2016-06-07 09:14:07 -0400571
572 while (n_left_from >= 4 && n_left_to_next >= 2)
573 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400574 u32 bi0, bi1;
575 vlib_buffer_t *b0, *b1;
John Lo3ef822e2016-06-07 09:14:07 -0400576
Dave Barach97d8dc22016-08-15 15:31:15 -0400577 to_next[0] = bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400578 to_next[1] = bi1 = from[1];
579 from += 2;
580 to_next += 2;
581 n_left_from -= 2;
582 n_left_to_next -= 2;
583 b0 = vlib_get_buffer (vm, bi0);
584 b1 = vlib_get_buffer (vm, bi1);
John Lo0fc9bc12016-10-27 11:17:02 -0400585 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
586 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
Dave Barach97d8dc22016-08-15 15:31:15 -0400587 }
588
John Lo3ef822e2016-06-07 09:14:07 -0400589 while (n_left_from > 0 && n_left_to_next > 0)
590 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 u32 bi0;
592 vlib_buffer_t *b0;
John Lo3ef822e2016-06-07 09:14:07 -0400593
Dave Barach97d8dc22016-08-15 15:31:15 -0400594 bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400595 to_next[0] = bi0;
596 from += 1;
597 to_next += 1;
598 n_left_from -= 1;
599 n_left_to_next -= 1;
600 b0 = vlib_get_buffer (vm, bi0);
John Lo0fc9bc12016-10-27 11:17:02 -0400601 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
John Lo3ef822e2016-06-07 09:14:07 -0400602 }
603
604 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
605 }
606
607 return frame->n_vectors;
608}
609
Dave Barach97d8dc22016-08-15 15:31:15 -0400610/* *INDENT-OFF* */
John Lo0fc9bc12016-10-27 11:17:02 -0400611VLIB_REGISTER_NODE (l2output_bad_intf_node,static) = {
612 .function = l2output_bad_intf_node_fn,
613 .name = "l2-output-bad-intf",
John Lo3ef822e2016-06-07 09:14:07 -0400614 .vector_size = sizeof (u32),
615 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400616
John Lo0fc9bc12016-10-27 11:17:02 -0400617 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
618 .error_strings = l2output_bad_intf_error_strings,
John Lo3ef822e2016-06-07 09:14:07 -0400619
620 .n_next_nodes = 1,
621
622 /* edit / add dispositions here */
623 .next_nodes = {
624 [0] = "error-drop",
625 },
626};
Eyal Bari001fd402017-07-16 09:34:53 +0300627
628VLIB_NODE_FUNCTION_MULTIARCH (l2output_bad_intf_node, l2output_bad_intf_node_fn);
Dave Barach97d8dc22016-08-15 15:31:15 -0400629/* *INDENT-ON* */
John Lo3ef822e2016-06-07 09:14:07 -0400630
Eyal Bari001fd402017-07-16 09:34:53 +0300631static clib_error_t *
632l2output_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633{
Dave Barach97d8dc22016-08-15 15:31:15 -0400634 l2output_main_t *mp = &l2output_main;
635
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -0400637 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638
Dave Barach97d8dc22016-08-15 15:31:15 -0400639 /* Create the config vector */
640 vec_validate (mp->configs, 100);
641 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700642
Dave Barach97d8dc22016-08-15 15:31:15 -0400643 /* Initialize the feature next-node indexes */
644 feat_bitmap_init_next_nodes (vm,
645 l2output_node.index,
646 L2OUTPUT_N_FEAT,
647 l2output_get_feat_names (),
John Lobeb0b2e2017-07-22 00:21:36 -0400648 mp->l2_out_feat_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Dave Barach97d8dc22016-08-15 15:31:15 -0400650 /* Initialize the output node mapping table */
John Lobeb0b2e2017-07-22 00:21:36 -0400651 vec_validate_init_empty (mp->output_node_index_vec, 100,
John Lob2fd6cb2017-07-12 19:56:45 -0400652 L2OUTPUT_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
654 return 0;
655}
656
657VLIB_INIT_FUNCTION (l2output_init);
658
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Chris Luke16bcf7d2016-09-01 14:31:46 -0400660/** Create a mapping in the next node mapping table for the given sw_if_index. */
John Lob2fd6cb2017-07-12 19:56:45 -0400661void
662l2output_create_output_node_mapping (vlib_main_t * vlib_main,
663 vnet_main_t * vnet_main, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400664{
John Lob2fd6cb2017-07-12 19:56:45 -0400665 vnet_hw_interface_t *hw0 =
666 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barach97d8dc22016-08-15 15:31:15 -0400668 /* dynamically create graph node arc */
John Lob2fd6cb2017-07-12 19:56:45 -0400669 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
670 hw0->output_node_index);
John Lobeb0b2e2017-07-22 00:21:36 -0400671 l2output_main.output_node_index_vec[sw_if_index] = next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673
Dave Barach97d8dc22016-08-15 15:31:15 -0400674/* Get a pointer to the config for the given interface */
675l2_output_config_t *
676l2output_intf_config (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
Dave Barach97d8dc22016-08-15 15:31:15 -0400678 l2output_main_t *mp = &l2output_main;
679
680 vec_validate (mp->configs, sw_if_index);
681 return vec_elt_at_index (mp->configs, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682}
683
Chris Luke16bcf7d2016-09-01 14:31:46 -0400684/** Enable (or disable) the feature in the bitmap for the given interface. */
Dave Barach97d8dc22016-08-15 15:31:15 -0400685void
686l2output_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687{
Dave Barach97d8dc22016-08-15 15:31:15 -0400688 l2output_main_t *mp = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700689 l2_output_config_t *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690
Dave Barach97d8dc22016-08-15 15:31:15 -0400691 vec_validate (mp->configs, sw_if_index);
692 config = vec_elt_at_index (mp->configs, sw_if_index);
693
694 if (enable)
695 {
696 config->feature_bitmap |= feature_bitmap;
697 }
698 else
699 {
700 config->feature_bitmap &= ~feature_bitmap;
701 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702}
Dave Barach97d8dc22016-08-15 15:31:15 -0400703
704/*
705 * fd.io coding-style-patch-verification: ON
706 *
707 * Local Variables:
708 * eval: (c-set-style "gnu")
709 * End:
710 */