blob: fbee590c9445bd57180e4e02f78c2844c7c22bc4 [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
43l2output_main_t l2output_main;
44
Dave Barach97d8dc22016-08-15 15:31:15 -040045typedef struct
46{
47 /* per-pkt trace data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 u8 src[6];
49 u8 dst[6];
50 u32 sw_if_index;
Pavel Kotucek65e84572017-01-16 17:01:56 +010051 u8 raw[12]; /* raw data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070052} l2output_trace_t;
53
54/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040055static u8 *
56format_l2output_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
58 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040060 l2output_trace_t *t = va_arg (*args, l2output_trace_t *);
61
Pavel Kotucek65e84572017-01-16 17:01:56 +010062 s = format (s, "l2-output: sw_if_index %d dst %U src %U data "
63 "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 t->sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -040065 format_ethernet_address, t->dst,
Pavel Kotucek65e84572017-01-16 17:01:56 +010066 format_ethernet_address, t->src,
67 t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
68 t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
69 t->raw[10], t->raw[11]);
70
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 return s;
72}
73
74
Dave Barach97d8dc22016-08-15 15:31:15 -040075static char *l2output_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070076#define _(sym,string) string,
77 foreach_l2output_error
78#undef _
79};
80
Dave Barach97d8dc22016-08-15 15:31:15 -040081/**
Chris Luke16bcf7d2016-09-01 14:31:46 -040082 * Check for split horizon violations.
83 * Return 0 if split horizon check passes, otherwise return non-zero.
Dave Barach97d8dc22016-08-15 15:31:15 -040084 * Packets should not be transmitted out an interface with the same
Chris Luke16bcf7d2016-09-01 14:31:46 -040085 * split-horizon group as the input interface, except if the @c shg is 0
Dave Barach97d8dc22016-08-15 15:31:15 -040086 * in which case the check always passes.
87 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070088static_always_inline u32
89split_horizon_violation (u8 shg1, u8 shg2)
90{
Dave Barach97d8dc22016-08-15 15:31:15 -040091 if (PREDICT_TRUE (shg1 == 0))
92 {
93 return 0;
94 }
95 else
96 {
97 return shg1 == shg2;
98 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070099}
100
John Lobeb0b2e2017-07-22 00:21:36 -0400101/** Determine the next L2 node based on the output feature bitmap */
102static_always_inline void
103l2_output_dispatch (vlib_buffer_t * b0, vlib_node_runtime_t * node,
104 u32 * cached_sw_if_index, u32 * cached_next_index,
105 u32 sw_if_index, u32 feature_bitmap, u32 * next0)
106{
107 /*
108 * The output feature bitmap always have at least the L2 output bit set
109 * for a normal L2 interface (or 0 if the interface is changed from L2
110 * to L3 mode). So if the feature bitmap is 0 or just have L2 output bits set,
111 * we know there is no more feature and will just output packets on interface.
112 * Otherwise, get the index of the next feature node.
113 */
114 if (PREDICT_FALSE ((feature_bitmap & ~L2OUTPUT_FEAT_OUTPUT) != 0))
115 {
116 /* Save bitmap for the next feature graph nodes */
117 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
118
119 /* Determine the next node */
120 *next0 =
121 feat_bitmap_get_next_node_index (l2output_main.l2_out_feat_next,
122 feature_bitmap);
123 }
124 else
125 {
126 /*
127 * There are no features. Send packet to TX node for sw_if_index0
128 * This is a little tricky in that the output interface next node indexes
129 * are not precomputed at init time.
130 */
131
132 if (sw_if_index == *cached_sw_if_index)
133 {
134 /* We hit in the one-entry cache. Use it. */
135 *next0 = *cached_next_index;
136 }
137 else
138 {
139 /* Look up the output TX node for the sw_if_index */
140 *next0 = vec_elt (l2output_main.output_node_index_vec, sw_if_index);
141
142 if (PREDICT_FALSE (*next0 == L2OUTPUT_NEXT_DROP))
143 b0->error = node->errors[L2OUTPUT_ERROR_MAPPING_DROP];
144
145 /* Update the one-entry cache */
146 *cached_sw_if_index = sw_if_index;
147 *cached_next_index = *next0;
148 }
149 }
150}
151
Damjan Mariondc5252b2016-11-24 19:25:01 -0800152static_always_inline void
153l2output_vtr (vlib_node_runtime_t * node, l2_output_config_t * config,
154 u32 feature_bitmap, vlib_buffer_t * b, u32 * next)
155{
156 if (PREDICT_FALSE (config->out_vtr_flag))
157 {
158 /* Perform pre-vtr EFP filter check if configured */
159 if (config->output_vtr.push_and_pop_bytes)
160 {
161 /*
162 * Perform output vlan tag rewrite and the pre-vtr EFP filter check.
163 * The EFP Filter only needs to be run if there is an output VTR
164 * configured. The flag for the post-vtr EFP Filter node is used
165 * to trigger the pre-vtr check as well.
166 */
167 u32 failed1 = (feature_bitmap & L2OUTPUT_FEAT_EFP_FILTER)
168 && (l2_efp_filter_process (b, &(config->input_vtr)));
169 u32 failed2 = l2_vtr_process (b, &(config->output_vtr));
170
171 if (PREDICT_FALSE (failed1 | failed2))
172 {
173 *next = L2OUTPUT_NEXT_DROP;
174 if (failed2)
175 {
176 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
177 }
178 if (failed1)
179 {
180 b->error = node->errors[L2OUTPUT_ERROR_EFP_DROP];
181 }
182 }
183 }
184 // perform the PBB rewrite
185 else if (config->output_pbb_vtr.push_and_pop_bytes)
186 {
187 u32 failed = l2_pbb_process (b, &(config->output_pbb_vtr));
188 if (PREDICT_FALSE (failed))
189 {
190 *next = L2OUTPUT_NEXT_DROP;
191 b->error = node->errors[L2OUTPUT_ERROR_VTR_DROP];
192 }
193 }
194 }
195}
196
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197
Dave Barach681abe42017-02-15 09:01:01 -0500198static_always_inline uword
199l2output_node_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
200 vlib_frame_t * frame, int do_trace)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201{
Dave Barach97d8dc22016-08-15 15:31:15 -0400202 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203 l2output_next_t next_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400204 l2output_main_t *msm = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 u32 cached_sw_if_index;
206 u32 cached_next_index;
207
208 /* Invalidate cache */
209 cached_sw_if_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400210 cached_next_index = ~0; /* warning be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400213 n_left_from = frame->n_vectors; /* number of packets to process */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 next_index = node->cached_next_index;
215
216 while (n_left_from > 0)
217 {
218 u32 n_left_to_next;
219
220 /* get space to enqueue frame to graph node "next_index" */
Dave Barach97d8dc22016-08-15 15:31:15 -0400221 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222
Damjan Mariondc5252b2016-11-24 19:25:01 -0800223 while (n_left_from >= 8 && n_left_to_next >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224 {
Damjan Mariondc5252b2016-11-24 19:25:01 -0800225 u32 bi0, bi1, bi2, bi3;
226 vlib_buffer_t *b0, *b1, *b2, *b3;
227 u32 next0, next1, next2, next3;
228 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
229 ethernet_header_t *h0, *h1, *h2, *h3;
230 l2_output_config_t *config0, *config1, *config2, *config3;
Dave Barach97d8dc22016-08-15 15:31:15 -0400231 u32 feature_bitmap0, feature_bitmap1;
Damjan Mariondc5252b2016-11-24 19:25:01 -0800232 u32 feature_bitmap2, feature_bitmap3;
Dave Barach97d8dc22016-08-15 15:31:15 -0400233
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 /* Prefetch next iteration. */
235 {
Damjan Mariondc5252b2016-11-24 19:25:01 -0800236 vlib_buffer_t *p4, *p5, *p6, *p7;
Dave Barach97d8dc22016-08-15 15:31:15 -0400237
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 p4 = vlib_get_buffer (vm, from[4]);
239 p5 = vlib_get_buffer (vm, from[5]);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800240 p6 = vlib_get_buffer (vm, from[6]);
241 p7 = vlib_get_buffer (vm, from[7]);
Dave Barach97d8dc22016-08-15 15:31:15 -0400242
243 /* Prefetch the buffer header for the N+2 loop iteration */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 vlib_prefetch_buffer_header (p4, LOAD);
245 vlib_prefetch_buffer_header (p5, LOAD);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800246 vlib_prefetch_buffer_header (p6, LOAD);
247 vlib_prefetch_buffer_header (p7, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 }
249
Dave Barach97d8dc22016-08-15 15:31:15 -0400250 /* speculatively enqueue b0 and b1 to the current next frame */
251 /* bi is "buffer index", b is pointer to the buffer */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 to_next[0] = bi0 = from[0];
253 to_next[1] = bi1 = from[1];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800254 to_next[2] = bi2 = from[2];
255 to_next[3] = bi3 = from[3];
256 from += 4;
257 to_next += 4;
258 n_left_from -= 4;
259 n_left_to_next -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
261 b0 = vlib_get_buffer (vm, bi0);
262 b1 = vlib_get_buffer (vm, bi1);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800263 b2 = vlib_get_buffer (vm, bi2);
264 b3 = vlib_get_buffer (vm, bi3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barach97d8dc22016-08-15 15:31:15 -0400266 /* TX interface handles */
267 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
268 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_TX];
Damjan Mariondc5252b2016-11-24 19:25:01 -0800269 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_TX];
270 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Damjan Mariondc5252b2016-11-24 19:25:01 -0800272 vlib_node_increment_counter (vm, l2output_node.index,
273 L2OUTPUT_ERROR_L2OUTPUT, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274
Dave Barach97d8dc22016-08-15 15:31:15 -0400275 /* Get config for the output interface */
276 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
277 config1 = vec_elt_at_index (msm->configs, sw_if_index1);
Damjan Mariondc5252b2016-11-24 19:25:01 -0800278 config2 = vec_elt_at_index (msm->configs, sw_if_index2);
279 config3 = vec_elt_at_index (msm->configs, sw_if_index3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400280
281 /*
282 * Get features from the config
283 * TODO: mask out any non-applicable features
284 */
285 feature_bitmap0 = config0->feature_bitmap;
286 feature_bitmap1 = config1->feature_bitmap;
Damjan Mariondc5252b2016-11-24 19:25:01 -0800287 feature_bitmap2 = config2->feature_bitmap;
288 feature_bitmap3 = config3->feature_bitmap;
Dave Barach97d8dc22016-08-15 15:31:15 -0400289
290 /* Determine next node */
John Lobeb0b2e2017-07-22 00:21:36 -0400291 l2_output_dispatch (b0, node, &cached_sw_if_index,
292 &cached_next_index, sw_if_index0,
293 feature_bitmap0, &next0);
294 l2_output_dispatch (b1, node, &cached_sw_if_index,
295 &cached_next_index, sw_if_index1,
296 feature_bitmap1, &next1);
297 l2_output_dispatch (b2, node, &cached_sw_if_index,
298 &cached_next_index, sw_if_index2,
299 feature_bitmap2, &next2);
300 l2_output_dispatch (b3, node, &cached_sw_if_index,
301 &cached_next_index, sw_if_index3,
302 feature_bitmap3, &next3);
Pavel Kotucek95300d12016-08-26 16:11:36 +0200303
Damjan Mariondc5252b2016-11-24 19:25:01 -0800304 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
305 l2output_vtr (node, config1, feature_bitmap1, b1, &next1);
306 l2output_vtr (node, config2, feature_bitmap2, b2, &next2);
307 l2output_vtr (node, config3, feature_bitmap3, b3, &next3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400308
Pavel Kotucek65e84572017-01-16 17:01:56 +0100309 if (do_trace)
310 {
311 h0 = vlib_buffer_get_current (b0);
312 h1 = vlib_buffer_get_current (b1);
313 h2 = vlib_buffer_get_current (b2);
314 h3 = vlib_buffer_get_current (b3);
315 if (b0->flags & VLIB_BUFFER_IS_TRACED)
316 {
317 l2output_trace_t *t =
318 vlib_add_trace (vm, node, b0, sizeof (*t));
319 t->sw_if_index = sw_if_index0;
320 clib_memcpy (t->src, h0->src_address, 6);
321 clib_memcpy (t->dst, h0->dst_address, 6);
322 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
323 }
324 if (b1->flags & VLIB_BUFFER_IS_TRACED)
325 {
326 l2output_trace_t *t =
327 vlib_add_trace (vm, node, b1, sizeof (*t));
328 t->sw_if_index = sw_if_index1;
329 clib_memcpy (t->src, h1->src_address, 6);
330 clib_memcpy (t->dst, h1->dst_address, 6);
331 clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
332 }
333 if (b2->flags & VLIB_BUFFER_IS_TRACED)
334 {
335 l2output_trace_t *t =
336 vlib_add_trace (vm, node, b2, sizeof (*t));
337 t->sw_if_index = sw_if_index2;
338 clib_memcpy (t->src, h2->src_address, 6);
339 clib_memcpy (t->dst, h2->dst_address, 6);
340 clib_memcpy (t->raw, &h2->type, sizeof (t->raw));
341 }
342 if (b3->flags & VLIB_BUFFER_IS_TRACED)
343 {
344 l2output_trace_t *t =
345 vlib_add_trace (vm, node, b3, sizeof (*t));
346 t->sw_if_index = sw_if_index3;
347 clib_memcpy (t->src, h3->src_address, 6);
348 clib_memcpy (t->dst, h3->dst_address, 6);
349 clib_memcpy (t->raw, &h3->type, sizeof (t->raw));
350 }
351 }
352
Dave Barach97d8dc22016-08-15 15:31:15 -0400353 /*
354 * Perform the split horizon check
355 * The check can only fail for non-zero shg's
356 */
Damjan Mariondc5252b2016-11-24 19:25:01 -0800357 if (PREDICT_FALSE (config0->shg + config1->shg +
358 config2->shg + config3->shg))
Dave Barach97d8dc22016-08-15 15:31:15 -0400359 {
360 /* one of the checks might fail, check both */
361 if (split_horizon_violation
362 (config0->shg, vnet_buffer (b0)->l2.shg))
363 {
364 next0 = L2OUTPUT_NEXT_DROP;
365 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
366 }
367 if (split_horizon_violation
368 (config1->shg, vnet_buffer (b1)->l2.shg))
369 {
370 next1 = L2OUTPUT_NEXT_DROP;
371 b1->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
372 }
Damjan Mariondc5252b2016-11-24 19:25:01 -0800373 if (split_horizon_violation
374 (config2->shg, vnet_buffer (b2)->l2.shg))
375 {
376 next2 = L2OUTPUT_NEXT_DROP;
377 b2->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
378 }
379 if (split_horizon_violation
380 (config3->shg, vnet_buffer (b3)->l2.shg))
381 {
382 next3 = L2OUTPUT_NEXT_DROP;
383 b3->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
384 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400385 }
386
387 /* verify speculative enqueues, maybe switch current next frame */
388 /* if next0==next1==next_index then nothing special needs to be done */
Damjan Mariondc5252b2016-11-24 19:25:01 -0800389 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400390 to_next, n_left_to_next,
Damjan Mariondc5252b2016-11-24 19:25:01 -0800391 bi0, bi1, bi2, bi3,
392 next0, next1, next2, next3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400393 }
394
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 while (n_left_from > 0 && n_left_to_next > 0)
396 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400397 u32 bi0;
398 vlib_buffer_t *b0;
399 u32 next0;
400 u32 sw_if_index0;
401 ethernet_header_t *h0;
402 l2_output_config_t *config0;
403 u32 feature_bitmap0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barach97d8dc22016-08-15 15:31:15 -0400405 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 bi0 = from[0];
407 to_next[0] = bi0;
408 from += 1;
409 to_next += 1;
410 n_left_from -= 1;
411 n_left_to_next -= 1;
412
413 b0 = vlib_get_buffer (vm, bi0);
414
Dave Barach97d8dc22016-08-15 15:31:15 -0400415 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
Damjan Mariondc5252b2016-11-24 19:25:01 -0800417 vlib_node_increment_counter (vm, l2output_node.index,
418 L2OUTPUT_ERROR_L2OUTPUT, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Dave Barach97d8dc22016-08-15 15:31:15 -0400420 /* Get config for the output interface */
421 config0 = vec_elt_at_index (msm->configs, sw_if_index0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barach97d8dc22016-08-15 15:31:15 -0400423 /*
424 * Get features from the config
425 * TODO: mask out any non-applicable features
426 */
427 feature_bitmap0 = config0->feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Dave Barach97d8dc22016-08-15 15:31:15 -0400429 /* Determine next node */
John Lobeb0b2e2017-07-22 00:21:36 -0400430 l2_output_dispatch (b0, node, &cached_sw_if_index,
431 &cached_next_index, sw_if_index0,
432 feature_bitmap0, &next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Damjan Mariondc5252b2016-11-24 19:25:01 -0800434 l2output_vtr (node, config0, feature_bitmap0, b0, &next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Pavel Kotucek65e84572017-01-16 17:01:56 +0100436 if (do_trace && PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
437 {
438 l2output_trace_t *t =
439 vlib_add_trace (vm, node, b0, sizeof (*t));
440 t->sw_if_index = sw_if_index0;
441 h0 = vlib_buffer_get_current (b0);
442 clib_memcpy (t->src, h0->src_address, 6);
443 clib_memcpy (t->dst, h0->dst_address, 6);
444 clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
445 }
446
Dave Barach97d8dc22016-08-15 15:31:15 -0400447 /* Perform the split horizon check */
448 if (PREDICT_FALSE
449 (split_horizon_violation
450 (config0->shg, vnet_buffer (b0)->l2.shg)))
451 {
452 next0 = L2OUTPUT_NEXT_DROP;
453 b0->error = node->errors[L2OUTPUT_ERROR_SHG_DROP];
454 }
455
456 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
458 to_next, n_left_to_next,
459 bi0, next0);
460 }
461
462 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
463 }
464
465 return frame->n_vectors;
466}
467
Dave Barach681abe42017-02-15 09:01:01 -0500468static uword
469l2output_node_fn (vlib_main_t * vm,
470 vlib_node_runtime_t * node, vlib_frame_t * frame)
471{
472 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
473 return l2output_node_inline (vm, node, frame, 1 /* do_trace */ );
474 return l2output_node_inline (vm, node, frame, 0 /* do_trace */ );
475}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
Dave Barach97d8dc22016-08-15 15:31:15 -0400477/* *INDENT-OFF* */
Eyal Bari001fd402017-07-16 09:34:53 +0300478VLIB_REGISTER_NODE (l2output_node) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 .function = l2output_node_fn,
480 .name = "l2-output",
481 .vector_size = sizeof (u32),
482 .format_trace = format_l2output_trace,
483 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400484
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 .n_errors = ARRAY_LEN(l2output_error_strings),
486 .error_strings = l2output_error_strings,
487
488 .n_next_nodes = L2OUTPUT_N_NEXT,
489
490 /* edit / add dispositions here */
491 .next_nodes = {
492 [L2OUTPUT_NEXT_DROP] = "error-drop",
John Lo0fc9bc12016-10-27 11:17:02 -0400493 [L2OUTPUT_NEXT_BAD_INTF] = "l2-output-bad-intf",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494 },
495};
Eyal Bari001fd402017-07-16 09:34:53 +0300496
497VLIB_NODE_FUNCTION_MULTIARCH (l2output_node, l2output_node_fn);
Dave Barach97d8dc22016-08-15 15:31:15 -0400498/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499
John Lo3ef822e2016-06-07 09:14:07 -0400500
John Lo0fc9bc12016-10-27 11:17:02 -0400501#define foreach_l2output_bad_intf_error \
502_(DROP, "L2 output to interface not in L2 mode or deleted")
John Lo3ef822e2016-06-07 09:14:07 -0400503
John Lo0fc9bc12016-10-27 11:17:02 -0400504static char *l2output_bad_intf_error_strings[] = {
John Lo3ef822e2016-06-07 09:14:07 -0400505#define _(sym,string) string,
John Lo0fc9bc12016-10-27 11:17:02 -0400506 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400507#undef _
508};
509
Dave Barach97d8dc22016-08-15 15:31:15 -0400510typedef enum
511{
John Lo0fc9bc12016-10-27 11:17:02 -0400512#define _(sym,str) L2OUTPUT_BAD_INTF_ERROR_##sym,
513 foreach_l2output_bad_intf_error
John Lo3ef822e2016-06-07 09:14:07 -0400514#undef _
John Lo0fc9bc12016-10-27 11:17:02 -0400515 L2OUTPUT_BAD_INTF_N_ERROR,
516} l2output_bad_intf_error_t;
John Lo3ef822e2016-06-07 09:14:07 -0400517
518
Dave Barach97d8dc22016-08-15 15:31:15 -0400519/**
John Lo0fc9bc12016-10-27 11:17:02 -0400520 * Output node for interfaces/tunnels which was in L2 mode but were changed
521 * to L3 mode or possibly deleted thereafter. On changing forwarding mode
522 * of any tunnel/interface from L2 to L3, its entry in l2_output_main table
523 * next_nodes.output_node_index_vec[sw_if_index] MUST be set to the value of
524 * L2OUTPUT_NEXT_BAD_INTF. Thus, if there are stale entries in the L2FIB for
525 * this sw_if_index, l2-output will send packets for this sw_if_index to the
526 * l2-output-bad-intf node which just setup the proper drop reason before
527 * sending packets to the error-drop node to drop the packet. Then, stale L2FIB
528 * entries for delted tunnels won't cause possible packet or memory corrpution.
Dave Barach97d8dc22016-08-15 15:31:15 -0400529 */
John Lo0fc9bc12016-10-27 11:17:02 -0400530static vlib_node_registration_t l2output_bad_intf_node;
John Lo3ef822e2016-06-07 09:14:07 -0400531
532static uword
John Lo0fc9bc12016-10-27 11:17:02 -0400533l2output_bad_intf_node_fn (vlib_main_t * vm,
534 vlib_node_runtime_t * node, vlib_frame_t * frame)
John Lo3ef822e2016-06-07 09:14:07 -0400535{
Dave Barach97d8dc22016-08-15 15:31:15 -0400536 u32 n_left_from, *from, *to_next;
John Lo3ef822e2016-06-07 09:14:07 -0400537 l2output_next_t next_index = 0;
538
539 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400540 n_left_from = frame->n_vectors; /* number of packets to process */
John Lo3ef822e2016-06-07 09:14:07 -0400541
542 while (n_left_from > 0)
543 {
544 u32 n_left_to_next;
545
Dave Barach97d8dc22016-08-15 15:31:15 -0400546 /* get space to enqueue frame to graph node "next_index" */
547 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
John Lo3ef822e2016-06-07 09:14:07 -0400548
549 while (n_left_from >= 4 && n_left_to_next >= 2)
550 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400551 u32 bi0, bi1;
552 vlib_buffer_t *b0, *b1;
John Lo3ef822e2016-06-07 09:14:07 -0400553
Dave Barach97d8dc22016-08-15 15:31:15 -0400554 to_next[0] = bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400555 to_next[1] = bi1 = from[1];
556 from += 2;
557 to_next += 2;
558 n_left_from -= 2;
559 n_left_to_next -= 2;
560 b0 = vlib_get_buffer (vm, bi0);
561 b1 = vlib_get_buffer (vm, bi1);
John Lo0fc9bc12016-10-27 11:17:02 -0400562 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
563 b1->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
Dave Barach97d8dc22016-08-15 15:31:15 -0400564 }
565
John Lo3ef822e2016-06-07 09:14:07 -0400566 while (n_left_from > 0 && n_left_to_next > 0)
567 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400568 u32 bi0;
569 vlib_buffer_t *b0;
John Lo3ef822e2016-06-07 09:14:07 -0400570
Dave Barach97d8dc22016-08-15 15:31:15 -0400571 bi0 = from[0];
John Lo3ef822e2016-06-07 09:14:07 -0400572 to_next[0] = bi0;
573 from += 1;
574 to_next += 1;
575 n_left_from -= 1;
576 n_left_to_next -= 1;
577 b0 = vlib_get_buffer (vm, bi0);
John Lo0fc9bc12016-10-27 11:17:02 -0400578 b0->error = node->errors[L2OUTPUT_BAD_INTF_ERROR_DROP];
John Lo3ef822e2016-06-07 09:14:07 -0400579 }
580
581 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
582 }
583
584 return frame->n_vectors;
585}
586
Dave Barach97d8dc22016-08-15 15:31:15 -0400587/* *INDENT-OFF* */
John Lo0fc9bc12016-10-27 11:17:02 -0400588VLIB_REGISTER_NODE (l2output_bad_intf_node,static) = {
589 .function = l2output_bad_intf_node_fn,
590 .name = "l2-output-bad-intf",
John Lo3ef822e2016-06-07 09:14:07 -0400591 .vector_size = sizeof (u32),
592 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400593
John Lo0fc9bc12016-10-27 11:17:02 -0400594 .n_errors = ARRAY_LEN(l2output_bad_intf_error_strings),
595 .error_strings = l2output_bad_intf_error_strings,
John Lo3ef822e2016-06-07 09:14:07 -0400596
597 .n_next_nodes = 1,
598
599 /* edit / add dispositions here */
600 .next_nodes = {
601 [0] = "error-drop",
602 },
603};
Eyal Bari001fd402017-07-16 09:34:53 +0300604
605VLIB_NODE_FUNCTION_MULTIARCH (l2output_bad_intf_node, l2output_bad_intf_node_fn);
Dave Barach97d8dc22016-08-15 15:31:15 -0400606/* *INDENT-ON* */
John Lo3ef822e2016-06-07 09:14:07 -0400607
Eyal Bari001fd402017-07-16 09:34:53 +0300608static clib_error_t *
609l2output_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610{
Dave Barach97d8dc22016-08-15 15:31:15 -0400611 l2output_main_t *mp = &l2output_main;
612
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -0400614 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615
Dave Barach97d8dc22016-08-15 15:31:15 -0400616 /* Create the config vector */
617 vec_validate (mp->configs, 100);
618 /* Until we hook up the CLI config, just create 100 sw interface entries and zero them */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619
Dave Barach97d8dc22016-08-15 15:31:15 -0400620 /* Initialize the feature next-node indexes */
621 feat_bitmap_init_next_nodes (vm,
622 l2output_node.index,
623 L2OUTPUT_N_FEAT,
624 l2output_get_feat_names (),
John Lobeb0b2e2017-07-22 00:21:36 -0400625 mp->l2_out_feat_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
Dave Barach97d8dc22016-08-15 15:31:15 -0400627 /* Initialize the output node mapping table */
John Lobeb0b2e2017-07-22 00:21:36 -0400628 vec_validate_init_empty (mp->output_node_index_vec, 100,
John Lob2fd6cb2017-07-12 19:56:45 -0400629 L2OUTPUT_NEXT_DROP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
631 return 0;
632}
633
634VLIB_INIT_FUNCTION (l2output_init);
635
Ed Warnickecb9cada2015-12-08 15:45:58 -0700636
Chris Luke16bcf7d2016-09-01 14:31:46 -0400637/** Create a mapping in the next node mapping table for the given sw_if_index. */
John Lob2fd6cb2017-07-12 19:56:45 -0400638void
639l2output_create_output_node_mapping (vlib_main_t * vlib_main,
640 vnet_main_t * vnet_main, u32 sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400641{
John Lob2fd6cb2017-07-12 19:56:45 -0400642 vnet_hw_interface_t *hw0 =
643 vnet_get_sup_hw_interface (vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700644
Dave Barach97d8dc22016-08-15 15:31:15 -0400645 /* dynamically create graph node arc */
John Lob2fd6cb2017-07-12 19:56:45 -0400646 u32 next = vlib_node_add_next (vlib_main, l2output_node.index,
647 hw0->output_node_index);
John Lobeb0b2e2017-07-22 00:21:36 -0400648 l2output_main.output_node_index_vec[sw_if_index] = next;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650
Dave Barach97d8dc22016-08-15 15:31:15 -0400651/* Get a pointer to the config for the given interface */
652l2_output_config_t *
653l2output_intf_config (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654{
Dave Barach97d8dc22016-08-15 15:31:15 -0400655 l2output_main_t *mp = &l2output_main;
656
657 vec_validate (mp->configs, sw_if_index);
658 return vec_elt_at_index (mp->configs, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659}
660
Chris Luke16bcf7d2016-09-01 14:31:46 -0400661/** Enable (or disable) the feature in the bitmap for the given interface. */
Dave Barach97d8dc22016-08-15 15:31:15 -0400662void
663l2output_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664{
Dave Barach97d8dc22016-08-15 15:31:15 -0400665 l2output_main_t *mp = &l2output_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 l2_output_config_t *config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barach97d8dc22016-08-15 15:31:15 -0400668 vec_validate (mp->configs, sw_if_index);
669 config = vec_elt_at_index (mp->configs, sw_if_index);
670
671 if (enable)
672 {
673 config->feature_bitmap |= feature_bitmap;
674 }
675 else
676 {
677 config->feature_bitmap &= ~feature_bitmap;
678 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679}
Dave Barach97d8dc22016-08-15 15:31:15 -0400680
681/*
682 * fd.io coding-style-patch-verification: ON
683 *
684 * Local Variables:
685 * eval: (c-set-style "gnu")
686 * End:
687 */