blob: 710a9d9e8c31d58e355359c01ce4aa2076067496 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_fwd.c : layer 2 forwarding using l2fib
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 <vnet/l2/l2_input.h>
25#include <vnet/l2/l2_bvi.h>
26#include <vnet/l2/l2_fwd.h>
27#include <vnet/l2/l2_fib.h>
28
29#include <vppinfra/error.h>
30#include <vppinfra/hash.h>
31#include <vppinfra/sparse_vec.h>
32
33
Billy McFall22aa3e92016-09-09 08:46:40 -040034/**
35 * @file
36 * @brief Ethernet Forwarding.
37 *
38 * Code in this file handles forwarding Layer 2 packets. This file calls
39 * the FIB lookup, packet learning and the packet flooding as necessary.
40 * Packet is then sent to the next graph node.
41 */
42
Dave Barach97d8dc22016-08-15 15:31:15 -040043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
Dave Barach97d8dc22016-08-15 15:31:15 -040046 /* Hash table */
47 BVT (clib_bihash) * mac_table;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Dave Barach97d8dc22016-08-15 15:31:15 -040049 /* next node index for the L3 input node of each ethertype */
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 next_by_ethertype_t l3_next;
51
52 /* convenience variables */
Dave Barach97d8dc22016-08-15 15:31:15 -040053 vlib_main_t *vlib_main;
54 vnet_main_t *vnet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -070055} l2fwd_main_t;
56
Dave Barach97d8dc22016-08-15 15:31:15 -040057typedef struct
58{
59 /* per-pkt trace data */
Ed Warnickecb9cada2015-12-08 15:45:58 -070060 u8 src[6];
61 u8 dst[6];
62 u32 sw_if_index;
63 u16 bd_index;
64} l2fwd_trace_t;
65
66/* packet trace format function */
Dave Barach97d8dc22016-08-15 15:31:15 -040067static u8 *
68format_l2fwd_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
70 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
71 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach97d8dc22016-08-15 15:31:15 -040072 l2fwd_trace_t *t = va_arg (*args, l2fwd_trace_t *);
73
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 s = format (s, "l2-fwd: sw_if_index %d dst %U src %U bd_index %d",
75 t->sw_if_index,
Dave Barach97d8dc22016-08-15 15:31:15 -040076 format_ethernet_address, t->dst,
77 format_ethernet_address, t->src, t->bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070078 return s;
79}
80
81l2fwd_main_t l2fwd_main;
82
83static vlib_node_registration_t l2fwd_node;
84
85#define foreach_l2fwd_error \
86_(L2FWD, "L2 forward packets") \
87_(FLOOD, "L2 forward misses") \
88_(HIT, "L2 forward hits") \
John Lo7185c3b2016-06-04 00:02:37 -040089_(BVI_BAD_MAC, "BVI L3 MAC mismatch") \
Ed Warnickecb9cada2015-12-08 15:45:58 -070090_(BVI_ETHERTYPE, "BVI packet with unhandled ethertype") \
91_(FILTER_DROP, "Filter Mac Drop") \
92_(REFLECT_DROP, "Reflection Drop")
93
Dave Barach97d8dc22016-08-15 15:31:15 -040094typedef enum
95{
Ed Warnickecb9cada2015-12-08 15:45:58 -070096#define _(sym,str) L2FWD_ERROR_##sym,
97 foreach_l2fwd_error
98#undef _
Dave Barach97d8dc22016-08-15 15:31:15 -040099 L2FWD_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100} l2fwd_error_t;
101
Dave Barach97d8dc22016-08-15 15:31:15 -0400102static char *l2fwd_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103#define _(sym,string) string,
104 foreach_l2fwd_error
105#undef _
106};
107
Dave Barach97d8dc22016-08-15 15:31:15 -0400108typedef enum
109{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 L2FWD_NEXT_L2_OUTPUT,
111 L2FWD_NEXT_FLOOD,
112 L2FWD_NEXT_DROP,
113 L2FWD_N_NEXT,
114} l2fwd_next_t;
115
Chris Luke16bcf7d2016-09-01 14:31:46 -0400116/** Forward one packet based on the mac table lookup result. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117
118static_always_inline void
119l2fwd_process (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400120 vlib_node_runtime_t * node,
121 l2fwd_main_t * msm,
122 vlib_error_main_t * em,
123 vlib_buffer_t * b0,
124 u32 sw_if_index0, l2fib_entry_result_t * result0, u32 * next0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125{
Dave Barach97d8dc22016-08-15 15:31:15 -0400126 if (PREDICT_FALSE (result0->raw == ~0))
127 {
128 /*
129 * lookup miss, so flood
130 * TODO:replicate packet to each intf in bridge-domain
131 * For now just drop
132 */
133 if (vnet_buffer (b0)->l2.feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
134 {
135 *next0 = L2FWD_NEXT_FLOOD;
136 }
137 else
138 {
139 /* Flooding is disabled */
140 b0->error = node->errors[L2FWD_ERROR_FLOOD];
141 *next0 = L2FWD_NEXT_DROP;
142 }
143
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400145 else
146 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Dave Barach97d8dc22016-08-15 15:31:15 -0400148 /* lookup hit, forward packet */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149#ifdef COUNTERS
Dave Barach97d8dc22016-08-15 15:31:15 -0400150 em->counters[node_counter_base_index + L2FWD_ERROR_HIT] += 1;
151#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barach97d8dc22016-08-15 15:31:15 -0400153 vnet_buffer (b0)->sw_if_index[VLIB_TX] = result0->fields.sw_if_index;
154 *next0 = L2FWD_NEXT_L2_OUTPUT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Dave Barach97d8dc22016-08-15 15:31:15 -0400156 /* perform reflection check */
157 if (PREDICT_FALSE (sw_if_index0 == result0->fields.sw_if_index))
158 {
159 b0->error = node->errors[L2FWD_ERROR_REFLECT_DROP];
160 *next0 = L2FWD_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach97d8dc22016-08-15 15:31:15 -0400162 /* perform filter check */
163 }
164 else if (PREDICT_FALSE (result0->fields.filter))
165 {
166 b0->error = node->errors[L2FWD_ERROR_FILTER_DROP];
167 *next0 = L2FWD_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barach97d8dc22016-08-15 15:31:15 -0400169 /* perform BVI check */
170 }
171 else if (PREDICT_FALSE (result0->fields.bvi))
172 {
173 u32 rc;
174 rc = l2_to_bvi (vm,
175 msm->vnet_main,
176 b0,
177 vnet_buffer (b0)->sw_if_index[VLIB_TX],
178 &msm->l3_next, next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179
Dave Barach97d8dc22016-08-15 15:31:15 -0400180 if (PREDICT_FALSE (rc))
181 {
182 if (rc == TO_BVI_ERR_BAD_MAC)
183 {
184 b0->error = node->errors[L2FWD_ERROR_BVI_BAD_MAC];
185 *next0 = L2FWD_NEXT_DROP;
186 }
187 else if (rc == TO_BVI_ERR_ETHERTYPE)
188 {
189 b0->error = node->errors[L2FWD_ERROR_BVI_ETHERTYPE];
190 *next0 = L2FWD_NEXT_DROP;
191 }
192 }
193 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195}
196
197
198static uword
199l2fwd_node_fn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400200 vlib_node_runtime_t * node, vlib_frame_t * frame)
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 l2fwd_next_t next_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400204 l2fwd_main_t *msm = &l2fwd_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 vlib_node_t *n = vlib_get_node (vm, l2fwd_node.index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400206 CLIB_UNUSED (u32 node_counter_base_index) = n->error_heap_index;
207 vlib_error_main_t *em = &vm->error_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 l2fib_entry_key_t cached_key;
209 l2fib_entry_result_t cached_result;
210
Dave Barach97d8dc22016-08-15 15:31:15 -0400211 /* Clear the one-entry cache in case mac table was updated */
212 cached_key.raw = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 cached_result.raw = ~0;
214
215 from = vlib_frame_vector_args (frame);
Dave Barach97d8dc22016-08-15 15:31:15 -0400216 n_left_from = frame->n_vectors; /* number of packets to process */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 next_index = node->cached_next_index;
218
219 while (n_left_from > 0)
220 {
221 u32 n_left_to_next;
222
223 /* get space to enqueue frame to graph node "next_index" */
Dave Barach97d8dc22016-08-15 15:31:15 -0400224 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800226 while (n_left_from >= 8 && n_left_to_next >= 4)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 {
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800228 u32 bi0, bi1, bi2, bi3;
229 vlib_buffer_t *b0, *b1, *b2, *b3;
230 u32 next0, next1, next2, next3;
231 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
232 ethernet_header_t *h0, *h1, *h2, *h3;
233 l2fib_entry_key_t key0, key1, key2, key3;
234 l2fib_entry_result_t result0, result1, result2, result3;
235 u32 bucket0, bucket1, bucket2, bucket3;
Dave Barach97d8dc22016-08-15 15:31:15 -0400236
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 /* Prefetch next iteration. */
238 {
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800239 vlib_buffer_t *p4, *p5, *p6, *p7;
Dave Barach97d8dc22016-08-15 15:31:15 -0400240
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800241 p4 = vlib_get_buffer (vm, from[4]);
242 p5 = vlib_get_buffer (vm, from[5]);
243 p6 = vlib_get_buffer (vm, from[6]);
244 p7 = vlib_get_buffer (vm, from[7]);
Dave Barach97d8dc22016-08-15 15:31:15 -0400245
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800246 vlib_prefetch_buffer_header (p4, LOAD);
247 vlib_prefetch_buffer_header (p5, LOAD);
248 vlib_prefetch_buffer_header (p6, LOAD);
249 vlib_prefetch_buffer_header (p7, LOAD);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800251 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
252 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
253 CLIB_PREFETCH (p6->data, CLIB_CACHE_LINE_BYTES, STORE);
254 CLIB_PREFETCH (p7->data, CLIB_CACHE_LINE_BYTES, STORE);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 }
256
Dave Barach97d8dc22016-08-15 15:31:15 -0400257 /* speculatively enqueue b0 and b1 to the current next frame */
258 /* bi is "buffer index", b is pointer to the buffer */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 to_next[0] = bi0 = from[0];
260 to_next[1] = bi1 = from[1];
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800261 to_next[2] = bi2 = from[2];
262 to_next[3] = bi3 = from[3];
263 from += 4;
264 to_next += 4;
265 n_left_from -= 4;
266 n_left_to_next -= 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
268 b0 = vlib_get_buffer (vm, bi0);
269 b1 = vlib_get_buffer (vm, bi1);
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800270 b2 = vlib_get_buffer (vm, bi2);
271 b3 = vlib_get_buffer (vm, bi3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barach97d8dc22016-08-15 15:31:15 -0400273 /* RX interface handles */
274 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
275 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800276 sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX];
277 sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
Dave Barach97d8dc22016-08-15 15:31:15 -0400279 h0 = vlib_buffer_get_current (b0);
280 h1 = vlib_buffer_get_current (b1);
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800281 h2 = vlib_buffer_get_current (b2);
282 h3 = vlib_buffer_get_current (b3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400283
284 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
285 {
286 if (b0->flags & VLIB_BUFFER_IS_TRACED)
287 {
288 l2fwd_trace_t *t =
289 vlib_add_trace (vm, node, b0, sizeof (*t));
290 t->sw_if_index = sw_if_index0;
291 t->bd_index = vnet_buffer (b0)->l2.bd_index;
292 clib_memcpy (t->src, h0->src_address, 6);
293 clib_memcpy (t->dst, h0->dst_address, 6);
294 }
295 if (b1->flags & VLIB_BUFFER_IS_TRACED)
296 {
297 l2fwd_trace_t *t =
298 vlib_add_trace (vm, node, b1, sizeof (*t));
299 t->sw_if_index = sw_if_index1;
300 t->bd_index = vnet_buffer (b1)->l2.bd_index;
301 clib_memcpy (t->src, h1->src_address, 6);
302 clib_memcpy (t->dst, h1->dst_address, 6);
303 }
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800304 if (b2->flags & VLIB_BUFFER_IS_TRACED)
305 {
306 l2fwd_trace_t *t =
307 vlib_add_trace (vm, node, b2, sizeof (*t));
308 t->sw_if_index = sw_if_index2;
309 t->bd_index = vnet_buffer (b2)->l2.bd_index;
310 clib_memcpy (t->src, h2->src_address, 6);
311 clib_memcpy (t->dst, h2->dst_address, 6);
312 }
313 if (b3->flags & VLIB_BUFFER_IS_TRACED)
314 {
315 l2fwd_trace_t *t =
316 vlib_add_trace (vm, node, b3, sizeof (*t));
317 t->sw_if_index = sw_if_index3;
318 t->bd_index = vnet_buffer (b3)->l2.bd_index;
319 clib_memcpy (t->src, h3->src_address, 6);
320 clib_memcpy (t->dst, h3->dst_address, 6);
321 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400322 }
323
324 /* process 2 pkts */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325#ifdef COUNTERS
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800326 em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327#endif
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800328 /* *INDENT-OFF* */
329 l2fib_lookup_4 (msm->mac_table, &cached_key, &cached_result,
330 h0->dst_address, h1->dst_address,
331 h2->dst_address, h3->dst_address,
332 vnet_buffer (b0)->l2.bd_index,
333 vnet_buffer (b1)->l2.bd_index,
334 vnet_buffer (b2)->l2.bd_index,
335 vnet_buffer (b3)->l2.bd_index,
336 &key0, /* not used */
Dave Barach97d8dc22016-08-15 15:31:15 -0400337 &key1, /* not used */
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800338 &key2, /* not used */
339 &key3, /* not used */
Dave Barach97d8dc22016-08-15 15:31:15 -0400340 &bucket0, /* not used */
341 &bucket1, /* not used */
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800342 &bucket2, /* not used */
343 &bucket3, /* not used */
344 &result0,
345 &result1,
346 &result2,
347 &result3);
348 /* *INDENT-ON* */
Dave Barach97d8dc22016-08-15 15:31:15 -0400349 l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0,
350 &next0);
351 l2fwd_process (vm, node, msm, em, b1, sw_if_index1, &result1,
352 &next1);
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800353 l2fwd_process (vm, node, msm, em, b2, sw_if_index2, &result2,
354 &next2);
355 l2fwd_process (vm, node, msm, em, b3, sw_if_index3, &result3,
356 &next3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
Dave Barach97d8dc22016-08-15 15:31:15 -0400358 /* verify speculative enqueues, maybe switch current next frame */
359 /* if next0==next1==next_index then nothing special needs to be done */
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800360 vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
Dave Barach97d8dc22016-08-15 15:31:15 -0400361 to_next, n_left_to_next,
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800362 bi0, bi1, bi2, bi3,
363 next0, next1, next2, next3);
Dave Barach97d8dc22016-08-15 15:31:15 -0400364 }
365
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 while (n_left_from > 0 && n_left_to_next > 0)
367 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400368 u32 bi0;
369 vlib_buffer_t *b0;
370 u32 next0;
371 u32 sw_if_index0;
372 ethernet_header_t *h0;
373 l2fib_entry_key_t key0;
374 l2fib_entry_result_t result0;
375 u32 bucket0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Dave Barach97d8dc22016-08-15 15:31:15 -0400377 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 bi0 = from[0];
379 to_next[0] = bi0;
380 from += 1;
381 to_next += 1;
382 n_left_from -= 1;
383 n_left_to_next -= 1;
384
385 b0 = vlib_get_buffer (vm, bi0);
386
Dave Barach97d8dc22016-08-15 15:31:15 -0400387 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barach97d8dc22016-08-15 15:31:15 -0400389 h0 = vlib_buffer_get_current (b0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
392 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
393 {
394 l2fwd_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
395 t->sw_if_index = sw_if_index0;
396 t->bd_index = vnet_buffer (b0)->l2.bd_index;
397 clib_memcpy (t->src, h0->src_address, 6);
398 clib_memcpy (t->dst, h0->dst_address, 6);
399 }
400
401 /* process 1 pkt */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402#ifdef COUNTERS
Dave Barach97d8dc22016-08-15 15:31:15 -0400403 em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400405 l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result, h0->dst_address, vnet_buffer (b0)->l2.bd_index, &key0, /* not used */
406 &bucket0, /* not used */
407 &result0);
408 l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0,
409 &next0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
Dave Barach97d8dc22016-08-15 15:31:15 -0400411 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
413 to_next, n_left_to_next,
414 bi0, next0);
415 }
416
417 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
418 }
419
420 return frame->n_vectors;
421}
422
Dave Barach97d8dc22016-08-15 15:31:15 -0400423/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424VLIB_REGISTER_NODE (l2fwd_node,static) = {
425 .function = l2fwd_node_fn,
426 .name = "l2-fwd",
427 .vector_size = sizeof (u32),
428 .format_trace = format_l2fwd_trace,
429 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach97d8dc22016-08-15 15:31:15 -0400430
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 .n_errors = ARRAY_LEN(l2fwd_error_strings),
432 .error_strings = l2fwd_error_strings,
433
434 .n_next_nodes = L2FWD_N_NEXT,
435
436 /* edit / add dispositions here */
437 .next_nodes = {
438 [L2FWD_NEXT_L2_OUTPUT] = "l2-output",
439 [L2FWD_NEXT_FLOOD] = "l2-flood",
440 [L2FWD_NEXT_DROP] = "error-drop",
441 },
442};
Dave Barach97d8dc22016-08-15 15:31:15 -0400443/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
Damjan Marion1c80e832016-05-11 23:07:18 +0200445VLIB_NODE_FUNCTION_MULTIARCH (l2fwd_node, l2fwd_node_fn)
Dave Barach97d8dc22016-08-15 15:31:15 -0400446 clib_error_t *l2fwd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447{
Dave Barach97d8dc22016-08-15 15:31:15 -0400448 l2fwd_main_t *mp = &l2fwd_main;
449
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 mp->vlib_main = vm;
Dave Barach97d8dc22016-08-15 15:31:15 -0400451 mp->vnet_main = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
453 /* init the hash table ptr */
Dave Barach97d8dc22016-08-15 15:31:15 -0400454 mp->mac_table = get_mac_table ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455
Dave Barach97d8dc22016-08-15 15:31:15 -0400456 /* Initialize the next nodes for each ethertype */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 next_by_ethertype_init (&mp->l3_next);
458
459 return 0;
460}
461
462VLIB_INIT_FUNCTION (l2fwd_init);
463
464
Chris Luke16bcf7d2016-09-01 14:31:46 -0400465/** Add the L3 input node for this ethertype to the next nodes structure. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466void
467l2fwd_register_input_type (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400468 ethernet_type_t type, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469{
Dave Barach97d8dc22016-08-15 15:31:15 -0400470 l2fwd_main_t *mp = &l2fwd_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471 u32 next_index;
472
Dave Barach97d8dc22016-08-15 15:31:15 -0400473 next_index = vlib_node_add_next (vm, l2fwd_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
475 next_by_ethertype_register (&mp->l3_next, type, next_index);
476}
477
478
Dave Barach97d8dc22016-08-15 15:31:15 -0400479/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400480 * Set subinterface forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400481 * The CLI format is:
482 * set interface l2 forward <interface> [disable]
483 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400485int_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486{
Dave Barach97d8dc22016-08-15 15:31:15 -0400487 vnet_main_t *vnm = vnet_get_main ();
488 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489 u32 sw_if_index;
490 u32 enable;
491
Dave Barach97d8dc22016-08-15 15:31:15 -0400492 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 {
494 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400495 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 goto done;
497 }
498
499 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400500 if (unformat (input, "disable"))
501 {
502 enable = 0;
503 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
Dave Barach97d8dc22016-08-15 15:31:15 -0400505 /* set the interface flag */
506 if (l2input_intf_config (sw_if_index)->xconnect)
507 {
508 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_XCONNECT, enable);
509 }
510 else
511 {
512 l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_FWD, enable);
513 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Dave Barach97d8dc22016-08-15 15:31:15 -0400515done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 return error;
517}
518
Billy McFall22aa3e92016-09-09 08:46:40 -0400519/*?
520 * Layer 2 unicast forwarding can be enabled and disabled on each
521 * interface and on each bridge-domain. Use this command to
522 * manage interfaces. It is enabled by default.
523 *
524 * @cliexpar
525 * Example of how to enable fowarding:
526 * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0}
527 * Example of how to disable fowarding:
528 * @cliexcmd{set interface l2 forward GigabitEthernet0/8/0 disable}
529?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400530/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531VLIB_CLI_COMMAND (int_fwd_cli, static) = {
532 .path = "set interface l2 forward",
533 .short_help = "set interface l2 forward <interface> [disable]",
534 .function = int_fwd,
535};
Dave Barach97d8dc22016-08-15 15:31:15 -0400536/* *INDENT-ON* */
537
538/*
539 * fd.io coding-style-patch-verification: ON
540 *
541 * Local Variables:
542 * eval: (c-set-style "gnu")
543 * End:
544 */