blob: de22cef600e18de92484c64f8dc1abdfcfb2a482 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_input.c : layer 2 input 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 <vnet/ethernet/packet.h>
22#include <vnet/ip/ip_packet.h>
23#include <vnet/ip/ip4_packet.h>
24#include <vnet/ip/ip6_packet.h>
John Lo0f853332017-11-10 12:24:32 -050025#include <vnet/fib/fib_node.h>
26#include <vnet/ethernet/arp_packet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#include <vlib/cli.h>
28#include <vnet/l2/l2_input.h>
29#include <vnet/l2/l2_output.h>
30#include <vnet/l2/feat_bitmap.h>
31#include <vnet/l2/l2_bvi.h>
32#include <vnet/l2/l2_fib.h>
John Loda1f2c72017-03-24 20:11:15 -040033#include <vnet/l2/l2_bd.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070034
35#include <vppinfra/error.h>
36#include <vppinfra/hash.h>
37#include <vppinfra/cache.h>
38
Billy McFall22aa3e92016-09-09 08:46:40 -040039/**
40 * @file
41 * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3).
42 *
43 * This file contains the CLI Commands that modify the input mode of an
44 * interface. For interfaces in a Layer 2 cross-connect, all packets
45 * received on one interface will be transmitted to the other. For
46 * interfaces in a bridge-domain, packets will be forwarded to other
47 * interfaces in the same bridge-domain based on destination mac address.
48 * For interfaces in Layer 3 mode, the packets will be routed.
49 */
Pierre Pfistere389de72016-03-09 15:56:31 +000050
Dave Barach97d8dc22016-08-15 15:31:15 -040051/* Feature graph node names */
52static char *l2input_feat_names[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070053#define _(sym,name) name,
54 foreach_l2input_feat
55#undef _
56};
57
Dave Barach97d8dc22016-08-15 15:31:15 -040058char **
59l2input_get_feat_names (void)
60{
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 return l2input_feat_names;
62}
63
Eyal Bari942402b2017-07-26 11:57:04 +030064u8 *
Neale Ranns47a3d992020-09-29 15:38:51 +000065format_l2_input_feature_bitmap (u8 * s, va_list * args)
Eyal Bari942402b2017-07-26 11:57:04 +030066{
67 static char *display_names[] = {
68#define _(sym,name) #sym,
69 foreach_l2input_feat
70#undef _
71 };
72 u32 feature_bitmap = va_arg (*args, u32);
Neale Ranns5d9df1d2018-11-09 08:19:27 -080073 u32 verbose = va_arg (*args, u32);
Eyal Bari942402b2017-07-26 11:57:04 +030074
75 if (feature_bitmap == 0)
76 {
77 s = format (s, " none configured");
78 return s;
79 }
80
81 feature_bitmap &= ~L2INPUT_FEAT_DROP; /* Not a feature */
82 int i;
BenoƮt Ganne70ef0fa2019-12-16 15:51:38 +010083 for (i = L2INPUT_N_FEAT - 1; i >= 0; i--)
Neale Ranns5d9df1d2018-11-09 08:19:27 -080084 {
85 if (feature_bitmap & (1 << i))
86 {
87 if (verbose)
88 s = format (s, "%17s (%s)\n",
89 display_names[i], l2input_feat_names[i]);
90 else
91 s = format (s, "%s ", l2input_feat_names[i]);
92 }
93 }
Eyal Bari942402b2017-07-26 11:57:04 +030094 return s;
95}
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
Neale Ranns47a3d992020-09-29 15:38:51 +000097u8 *
98format_l2_input_features (u8 * s, va_list * args)
Dave Barach97d8dc22016-08-15 15:31:15 -040099{
Neale Ranns47a3d992020-09-29 15:38:51 +0000100 u32 sw_if_index = va_arg (*args, u32);
101 u32 verbose = va_arg (*args, u32);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Neale Ranns47a3d992020-09-29 15:38:51 +0000103 l2_input_config_t *l2_input = l2input_intf_config (sw_if_index);
104 u32 fb = l2_input->feature_bitmap;
Dave Barach97d8dc22016-08-15 15:31:15 -0400105
Neale Ranns47a3d992020-09-29 15:38:51 +0000106 /* intf input features are masked by bridge domain */
107 if (l2_input_is_bridge (l2_input))
108 fb &= l2_input->bd_feature_bitmap;
109
110 s =
111 format (s, "\nl2-input:\n%U", format_l2_input_feature_bitmap, fb,
112 verbose);
113
114 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115}
116
Neale Ranns47a3d992020-09-29 15:38:51 +0000117u8 *
118format_l2_input (u8 * s, va_list * args)
Dave Barach97d8dc22016-08-15 15:31:15 -0400119{
Neale Ranns47a3d992020-09-29 15:38:51 +0000120 u32 sw_if_index = va_arg (*args, u32);
121 l2_input_config_t *l2_input = l2input_intf_config (sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
Neale Ranns47a3d992020-09-29 15:38:51 +0000123 /* intf input features are masked by bridge domain */
124 if (l2_input_is_bridge (l2_input))
Dave Barach97d8dc22016-08-15 15:31:15 -0400125 {
Neale Ranns47a3d992020-09-29 15:38:51 +0000126 bd_main_t *bdm = &bd_main;
127 u32 bd_id = l2input_main.bd_configs[l2_input->bd_index].bd_id;
Eyal Bari109139e2018-03-28 15:25:28 +0300128
Neale Ranns47a3d992020-09-29 15:38:51 +0000129 s = format (s, " L2 bridge bd-id %d idx %d shg %d %s",
130 bd_id, bd_find_index (bdm, bd_id), l2_input->shg,
131 l2_input_is_bvi (l2_input) ? "bvi" : " ");
Dave Barach97d8dc22016-08-15 15:31:15 -0400132 }
Neale Ranns47a3d992020-09-29 15:38:51 +0000133 else if (l2_input_is_xconnect (l2_input))
134 s = format (s, " L2 xconnect %U",
135 format_vnet_sw_if_index_name, vnet_get_main (),
136 l2_input->output_sw_if_index);
John Lo1fc44b62018-09-13 22:10:25 +0000137
Neale Ranns47a3d992020-09-29 15:38:51 +0000138 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139}
140
Filip Tehlar44f0f712019-03-11 04:26:37 -0700141clib_error_t *
142l2input_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach97d8dc22016-08-15 15:31:15 -0400144 l2input_main_t *mp = &l2input_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Dave Barach97d8dc22016-08-15 15:31:15 -0400146 mp->vlib_main = vm;
147 mp->vnet_main = vnet_get_main ();
148
149 /* Get packets RX'd from L2 interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 ethernet_register_l2_input (vm, l2input_node.index);
151
Dave Barach97d8dc22016-08-15 15:31:15 -0400152 /* Initialize the feature next-node indexes */
153 feat_bitmap_init_next_nodes (vm,
154 l2input_node.index,
155 L2INPUT_N_FEAT,
156 l2input_get_feat_names (),
157 mp->feat_next_node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
159 return 0;
160}
161
162VLIB_INIT_FUNCTION (l2input_init);
163
164
Chris Luke16bcf7d2016-09-01 14:31:46 -0400165/** Get a pointer to the config for the given interface. */
Dave Barach97d8dc22016-08-15 15:31:15 -0400166l2_input_config_t *
167l2input_intf_config (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168{
Dave Barach97d8dc22016-08-15 15:31:15 -0400169 l2input_main_t *mp = &l2input_main;
170
171 vec_validate (mp->configs, sw_if_index);
172 return vec_elt_at_index (mp->configs, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173}
174
Chris Luke16bcf7d2016-09-01 14:31:46 -0400175/** Enable (or disable) the feature in the bitmap for the given interface. */
Dave Barach97d8dc22016-08-15 15:31:15 -0400176u32
Neale Rannsa1179582018-10-24 02:31:51 -0700177l2input_intf_bitmap_enable (u32 sw_if_index,
178 l2input_feat_masks_t feature_bitmap, u32 enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300180 l2_input_config_t *config = l2input_intf_config (sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400181
182 if (enable)
Eyal Bariafc47aa2017-04-20 14:45:17 +0300183 config->feature_bitmap |= feature_bitmap;
Dave Barach97d8dc22016-08-15 15:31:15 -0400184 else
Eyal Bariafc47aa2017-04-20 14:45:17 +0300185 config->feature_bitmap &= ~feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186
187 return config->feature_bitmap;
188}
189
Dave Barach97d8dc22016-08-15 15:31:15 -0400190u32
191l2input_set_bridge_features (u32 bd_index, u32 feat_mask, u32 feat_value)
Pierre Pfisterd6f5b962016-03-21 16:17:52 +0000192{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300193 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);;
Pierre Pfisterd6f5b962016-03-21 16:17:52 +0000194 bd_validate (bd_config);
Dave Barach97d8dc22016-08-15 15:31:15 -0400195 bd_config->feature_bitmap =
196 (bd_config->feature_bitmap & ~feat_mask) | feat_value;
Pierre Pfisterd6f5b962016-03-21 16:17:52 +0000197 return bd_config->feature_bitmap;
198}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700200void
201l2input_interface_mac_change (u32 sw_if_index,
202 const u8 * old_address, const u8 * new_address)
203{
204 /* check if the sw_if_index passed is a BVI in a BD */
205 l2_input_config_t *intf_config;
206
207 intf_config = l2input_intf_config (sw_if_index);
208
Neale Ranns47a3d992020-09-29 15:38:51 +0000209 if (l2_input_is_bridge (intf_config) && l2_input_is_bvi (intf_config))
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700210 {
211 /* delete and re-add l2fib entry for the bvi interface */
212 l2fib_del_entry (old_address, intf_config->bd_index, sw_if_index);
213 l2fib_add_entry (new_address,
214 intf_config->bd_index,
215 sw_if_index,
216 L2FIB_ENTRY_RESULT_FLAG_BVI |
217 L2FIB_ENTRY_RESULT_FLAG_STATIC);
218 }
219}
220
Neale Ranns47a3d992020-09-29 15:38:51 +0000221walk_rc_t
222l2input_recache (u32 bd_index, u32 sw_if_index)
223{
224 l2_input_config_t *input;
225 l2_bridge_domain_t *bd;
226
227 bd = bd_get (bd_index);
228 input = l2input_intf_config (sw_if_index);
229
230 input->bd_mac_age = bd->mac_age;
231 input->bd_seq_num = bd->seq_num;
232 input->bd_feature_bitmap = bd->feature_bitmap;
233
234 return (WALK_CONTINUE);
235}
236
237void
238l2_input_seq_num_inc (u32 sw_if_index)
239{
240 l2_input_config_t *input;
241
242 input = vec_elt_at_index (l2input_main.configs, sw_if_index);
243
244 input->seq_num++;
245}
246
Dave Barach97d8dc22016-08-15 15:31:15 -0400247/**
248 * Set the subinterface to run in l2 or l3 mode.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400249 * For L3 mode, just the sw_if_index is specified.
250 * For bridged mode, the bd id and bvi flag are also specified.
251 * For xconnect mode, the peer sw_if_index is also specified.
252 * Return 0 if ok, or non-0 if there was an error.
Dave Barach97d8dc22016-08-15 15:31:15 -0400253 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Dave Barach97d8dc22016-08-15 15:31:15 -0400255u32
John Lo7100b9c2017-02-28 13:10:52 -0500256set_int_l2_mode (vlib_main_t * vm, vnet_main_t * vnet_main, /* */
257 u32 mode, /* One of L2 modes or back to L3 mode */
258 u32 sw_if_index, /* sw interface index */
259 u32 bd_index, /* for bridged interface */
Neale Rannsb4743802018-09-05 09:13:57 -0700260 l2_bd_port_type_t port_type, /* port_type */
John Lo7100b9c2017-02-28 13:10:52 -0500261 u32 shg, /* the bridged interface split horizon group */
262 u32 xc_sw_if_index) /* peer interface for xconnect */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263{
Dave Barach97d8dc22016-08-15 15:31:15 -0400264 l2output_main_t *l2om = &l2output_main;
265 vnet_main_t *vnm = vnet_get_main ();
266 vnet_hw_interface_t *hi;
267 l2_output_config_t *out_config;
268 l2_input_config_t *config;
269 l2_bridge_domain_t *bd_config;
Dave Barach97d8dc22016-08-15 15:31:15 -0400270 i32 l2_if_adjust = 0;
Steven4f8863b2018-04-12 19:36:19 -0700271 vnet_device_class_t *dev_class;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
273 hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
Eyal Baric5b13602016-11-24 19:42:43 +0200274 config = l2input_intf_config (sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
Damjan Marion95147812020-09-14 12:18:44 +0200276 if (l2fib_main.mac_table_initialized == 0)
277 l2fib_table_init ();
278
Neale Ranns47a3d992020-09-29 15:38:51 +0000279 if (l2_input_is_bridge (config))
Dave Barach97d8dc22016-08-15 15:31:15 -0400280 {
281 /* Interface is already in bridge mode. Undo the existing config. */
Neale Ranns47a3d992020-09-29 15:38:51 +0000282 bd_config = bd_get (config->bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Dave Barach97d8dc22016-08-15 15:31:15 -0400284 /* remove interface from flood vector */
285 bd_remove_member (bd_config, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach97d8dc22016-08-15 15:31:15 -0400287 /* undo any BVI-related config */
288 if (bd_config->bvi_sw_if_index == sw_if_index)
289 {
Neale Ranns87dad112018-04-09 01:53:01 -0700290 vnet_sw_interface_t *si;
291
Dave Barach97d8dc22016-08-15 15:31:15 -0400292 bd_config->bvi_sw_if_index = ~0;
Neale Ranns47a3d992020-09-29 15:38:51 +0000293 config->flags &= ~L2_INPUT_FLAG_BVI;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Dave Barach97d8dc22016-08-15 15:31:15 -0400295 /* delete the l2fib entry for the bvi interface */
John Lo7dbd7262018-05-31 10:25:18 -0400296 l2fib_del_entry (hi->hw_address, config->bd_index, sw_if_index);
Pierre Pfistere389de72016-03-09 15:56:31 +0000297
Neale Ranns87dad112018-04-09 01:53:01 -0700298 /* since this is a no longer BVI interface do not to flood to it */
299 si = vnet_get_sw_interface (vnm, sw_if_index);
300 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
Dave Barach97d8dc22016-08-15 15:31:15 -0400301 }
Neale Rannsb4743802018-09-05 09:13:57 -0700302 if (bd_config->uu_fwd_sw_if_index == sw_if_index)
303 {
304 bd_config->uu_fwd_sw_if_index = ~0;
305 bd_config->feature_bitmap &= ~L2INPUT_FEAT_UU_FWD;
306 }
John Loda1f2c72017-03-24 20:11:15 -0400307
308 /* Clear MACs learned on the interface */
Eyal Bariafc47aa2017-04-20 14:45:17 +0300309 if ((config->feature_bitmap & L2INPUT_FEAT_LEARN) ||
310 (bd_config->feature_bitmap & L2INPUT_FEAT_LEARN))
John Loda1f2c72017-03-24 20:11:15 -0400311 l2fib_flush_int_mac (vm, sw_if_index);
312
Neale Ranns47a3d992020-09-29 15:38:51 +0000313 bd_input_walk (config->bd_index, l2input_recache, NULL);
Dave Barach97d8dc22016-08-15 15:31:15 -0400314 l2_if_adjust--;
315 }
Neale Ranns47a3d992020-09-29 15:38:51 +0000316 else if (l2_input_is_xconnect (config))
Dave Barach97d8dc22016-08-15 15:31:15 -0400317 {
318 l2_if_adjust--;
319 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
John Lob2fd6cb2017-07-12 19:56:45 -0400321 /* Make sure vector is big enough */
John Lobeb0b2e2017-07-22 00:21:36 -0400322 vec_validate_init_empty (l2om->output_node_index_vec, sw_if_index,
323 L2OUTPUT_NEXT_DROP);
John Lo0fc9bc12016-10-27 11:17:02 -0400324
Dave Barach97d8dc22016-08-15 15:31:15 -0400325 /* Initialize the l2-input configuration for the interface */
326 if (mode == MODE_L3)
327 {
John Lo0fc9bc12016-10-27 11:17:02 -0400328 /* Set L2 config to BD index 0 so that if any packet accidentally
329 * came in on L2 path, it will be dropped in BD 0 */
Neale Ranns47a3d992020-09-29 15:38:51 +0000330 config->flags = L2_INPUT_FLAG_NONE;
Dave Barach97d8dc22016-08-15 15:31:15 -0400331 config->shg = 0;
332 config->bd_index = 0;
333 config->feature_bitmap = L2INPUT_FEAT_DROP;
John Lo93a46912016-07-21 14:54:05 -0400334
John Lo7100b9c2017-02-28 13:10:52 -0500335 /* Clear L2 output config */
336 out_config = l2output_intf_config (sw_if_index);
Dave Barachb7b92992018-10-17 10:38:51 -0400337 clib_memset (out_config, 0, sizeof (l2_output_config_t));
John Lo7100b9c2017-02-28 13:10:52 -0500338
John Lo0fc9bc12016-10-27 11:17:02 -0400339 /* Make sure any L2-output packet to this interface now in L3 mode is
340 * dropped. This may happen if L2 FIB MAC entry is stale */
John Lobeb0b2e2017-07-22 00:21:36 -0400341 l2om->output_node_index_vec[sw_if_index] = L2OUTPUT_NEXT_BAD_INTF;
Dave Barach97d8dc22016-08-15 15:31:15 -0400342 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400343 else
344 {
John Lob2fd6cb2017-07-12 19:56:45 -0400345 /* Add or update l2-output node next-arc and output_node_index_vec table
346 * for the interface */
347 l2output_create_output_node_mapping (vm, vnet_main, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348
Dave Barach97d8dc22016-08-15 15:31:15 -0400349 if (mode == MODE_L2_BRIDGE)
350 {
Neale Rannsb4743802018-09-05 09:13:57 -0700351 u8 member_flags;
352
Dave Barach97d8dc22016-08-15 15:31:15 -0400353 /*
354 * Remove a check that the interface must be an Ethernet.
355 * Specifically so we can bridge to L3 tunnel interfaces.
356 * Here's the check:
357 * if (hi->hw_class_index != ethernet_hw_interface_class.index)
358 *
359 */
360 if (!hi)
361 return MODE_ERROR_ETH; /* non-ethernet */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362
Neale Ranns47a3d992020-09-29 15:38:51 +0000363 config->flags = L2_INPUT_FLAG_BRIDGE;
Dave Barach97d8dc22016-08-15 15:31:15 -0400364 config->bd_index = bd_index;
Neale Ranns47a3d992020-09-29 15:38:51 +0000365 l2_input_seq_num_inc (sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 /*
368 * Enable forwarding, flooding, learning and ARP termination by default
369 * (note that ARP term is disabled on BD feature bitmap by default)
370 */
Neale Rannsb4743802018-09-05 09:13:57 -0700371 config->feature_bitmap |= (L2INPUT_FEAT_FWD |
372 L2INPUT_FEAT_UU_FLOOD |
373 L2INPUT_FEAT_UU_FWD |
374 L2INPUT_FEAT_FLOOD |
375 L2INPUT_FEAT_LEARN |
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200376 L2INPUT_FEAT_ARP_UFWD |
Neale Rannsb4743802018-09-05 09:13:57 -0700377 L2INPUT_FEAT_ARP_TERM);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378
Dave Barach97d8dc22016-08-15 15:31:15 -0400379 /* Make sure last-chance drop is configured */
380 config->feature_bitmap |= L2INPUT_FEAT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381
Dave Barach97d8dc22016-08-15 15:31:15 -0400382 /* Make sure xconnect is disabled */
383 config->feature_bitmap &= ~L2INPUT_FEAT_XCONNECT;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384
Dave Barach97d8dc22016-08-15 15:31:15 -0400385 /* Set up bridge domain */
Eyal Bariafc47aa2017-04-20 14:45:17 +0300386 bd_config = l2input_bd_config (bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400387 bd_validate (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barach97d8dc22016-08-15 15:31:15 -0400389 /* TODO: think: add l2fib entry even for non-bvi interface? */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 /* Do BVI interface initializations */
Neale Rannsb4743802018-09-05 09:13:57 -0700392 if (L2_BD_PORT_TYPE_BVI == port_type)
Dave Barach97d8dc22016-08-15 15:31:15 -0400393 {
Neale Ranns87dad112018-04-09 01:53:01 -0700394 vnet_sw_interface_t *si;
395
Dave Barach97d8dc22016-08-15 15:31:15 -0400396 /* ensure BD has no bvi interface (or replace that one with this??) */
397 if (bd_config->bvi_sw_if_index != ~0)
398 {
399 return MODE_ERROR_BVI_DEF; /* bd already has a bvi interface */
400 }
401 bd_config->bvi_sw_if_index = sw_if_index;
Neale Ranns47a3d992020-09-29 15:38:51 +0000402 config->flags |= L2_INPUT_FLAG_BVI;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Dave Barach97d8dc22016-08-15 15:31:15 -0400404 /* create the l2fib entry for the bvi interface */
Neale Rannsb54d0812018-09-06 06:22:56 -0700405 l2fib_add_entry (hi->hw_address, bd_index, sw_if_index,
406 L2FIB_ENTRY_RESULT_FLAG_BVI |
407 L2FIB_ENTRY_RESULT_FLAG_STATIC);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Dave Barach97d8dc22016-08-15 15:31:15 -0400409 /* Disable learning by default. no use since l2fib entry is static. */
410 config->feature_bitmap &= ~L2INPUT_FEAT_LEARN;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411
Neale Ranns87dad112018-04-09 01:53:01 -0700412 /* since this is a BVI interface we want to flood to it */
413 si = vnet_get_sw_interface (vnm, sw_if_index);
414 si->flood_class = VNET_FLOOD_CLASS_BVI;
Neale Rannsb4743802018-09-05 09:13:57 -0700415 member_flags = L2_FLOOD_MEMBER_BVI;
416 }
417 else if (L2_BD_PORT_TYPE_UU_FWD == port_type)
418 {
419 bd_config->uu_fwd_sw_if_index = sw_if_index;
420 bd_config->feature_bitmap |= L2INPUT_FEAT_UU_FWD;
421 }
422 else
423 {
424 member_flags = L2_FLOOD_MEMBER_NORMAL;
Dave Barach97d8dc22016-08-15 15:31:15 -0400425 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Neale Rannsb4743802018-09-05 09:13:57 -0700427 if (L2_BD_PORT_TYPE_NORMAL == port_type ||
428 L2_BD_PORT_TYPE_BVI == port_type)
429 {
430 /* Add interface to bridge-domain flood vector */
431 l2_flood_member_t member = {
432 .sw_if_index = sw_if_index,
433 .flags = member_flags,
434 .shg = shg,
435 };
436 bd_add_member (bd_config, &member);
437 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 }
John Lob2fd6cb2017-07-12 19:56:45 -0400439 else if (mode == MODE_L2_XC)
Dave Barach97d8dc22016-08-15 15:31:15 -0400440 {
Neale Ranns47a3d992020-09-29 15:38:51 +0000441 config->flags = L2_INPUT_FLAG_XCONNECT;
Dave Barach97d8dc22016-08-15 15:31:15 -0400442 config->output_sw_if_index = xc_sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Dave Barach97d8dc22016-08-15 15:31:15 -0400444 /* Make sure last-chance drop is configured */
445 config->feature_bitmap |= L2INPUT_FEAT_DROP;
446
447 /* Make sure bridging features are disabled */
448 config->feature_bitmap &=
449 ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
450
451 config->feature_bitmap |= L2INPUT_FEAT_XCONNECT;
452 shg = 0; /* not used in xconnect */
453 }
John Lob2fd6cb2017-07-12 19:56:45 -0400454 else if (mode == MODE_L2_CLASSIFY)
455 {
Neale Ranns47a3d992020-09-29 15:38:51 +0000456 config->flags = L2_INPUT_FLAG_XCONNECT;
John Lob2fd6cb2017-07-12 19:56:45 -0400457 config->output_sw_if_index = xc_sw_if_index;
458
459 /* Make sure last-chance drop is configured */
460 config->feature_bitmap |=
461 L2INPUT_FEAT_DROP | L2INPUT_FEAT_INPUT_CLASSIFY;
462
463 /* Make sure bridging features are disabled */
464 config->feature_bitmap &=
465 ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
466 shg = 0; /* not used in xconnect */
John Lob2fd6cb2017-07-12 19:56:45 -0400467 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400468
Andrew Yourtchenkocc405652017-03-03 13:11:30 +0000469 /* set up split-horizon group and set output feature bit */
Dave Barach97d8dc22016-08-15 15:31:15 -0400470 config->shg = shg;
471 out_config = l2output_intf_config (sw_if_index);
472 out_config->shg = shg;
Andrew Yourtchenkocc405652017-03-03 13:11:30 +0000473 out_config->feature_bitmap |= L2OUTPUT_FEAT_OUTPUT;
Dave Barach97d8dc22016-08-15 15:31:15 -0400474
475 /*
476 * Test: remove this when non-IP features can be configured.
477 * Enable a non-IP feature to test IP feature masking
478 * config->feature_bitmap |= L2INPUT_FEAT_CTRL_PKT;
479 */
480
481 l2_if_adjust++;
Neale Ranns47a3d992020-09-29 15:38:51 +0000482
483 bd_input_walk (bd_index, l2input_recache, NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484 }
485
Dave Barach97d8dc22016-08-15 15:31:15 -0400486 /* Adjust count of L2 interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 hi->l2_if_count += l2_if_adjust;
488
Dave Barach97d8dc22016-08-15 15:31:15 -0400489 if (hi->hw_class_index == ethernet_hw_interface_class.index)
490 {
491 if ((hi->l2_if_count == 1) && (l2_if_adjust == 1))
492 {
John Lof415a3b2020-05-14 15:02:16 -0400493 /* Just added first L2 interface on this port
494 * Set promiscuous mode on the l2 interface */
Dave Barach97d8dc22016-08-15 15:31:15 -0400495 ethernet_set_flags (vnet_main, hi->hw_if_index,
496 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
Dave Barach97d8dc22016-08-15 15:31:15 -0400497 }
498 else if ((hi->l2_if_count == 0) && (l2_if_adjust == -1))
499 {
John Lof415a3b2020-05-14 15:02:16 -0400500 /* Just removed only L2 subinterface on this port
501 * Disable promiscuous mode on the l2 interface */
502 ethernet_set_flags (vnet_main, hi->hw_if_index,
503 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504
Dave Barach97d8dc22016-08-15 15:31:15 -0400505 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
Dave Barach97d8dc22016-08-15 15:31:15 -0400508 /* Set up the L2/L3 flag in the interface parsing tables */
509 ethernet_sw_interface_set_l2_mode (vnm, sw_if_index, (mode != MODE_L3));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
Steven4f8863b2018-04-12 19:36:19 -0700511 dev_class = vnet_get_device_class (vnet_main, hi->dev_class_index);
512 if (dev_class->set_l2_mode_function)
513 {
514 dev_class->set_l2_mode_function (vnet_main, hi, l2_if_adjust);
515 }
516
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 return 0;
518}
Neale Ranns47a3d992020-09-29 15:38:51 +0000519
520static clib_error_t *
521l2_input_interface_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_add)
522{
523 if (!is_add)
524 {
525 vlib_main_t *vm = vlib_get_main ();
526 l2_input_config_t *config;
527
528 if (sw_if_index < vec_len (l2input_main.configs))
529 {
530 config = vec_elt_at_index (l2input_main.configs, sw_if_index);
531 if (l2_input_is_xconnect (config))
532 set_int_l2_mode (vm, vnm, MODE_L3, config->output_sw_if_index, 0,
533 L2_BD_PORT_TYPE_NORMAL, 0, 0);
534 if (l2_input_is_xconnect (config) || l2_input_is_bridge (config))
535 set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0,
536 L2_BD_PORT_TYPE_NORMAL, 0, 0);
537 }
538 }
539
540 return (NULL);
541}
542
543VNET_SW_INTERFACE_ADD_DEL_FUNCTION (l2_input_interface_add_del);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
Dave Barach97d8dc22016-08-15 15:31:15 -0400545/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400546 * Set subinterface in bridging mode with a bridge-domain ID.
Dave Barach97d8dc22016-08-15 15:31:15 -0400547 * The CLI format is:
548 * set interface l2 bridge <interface> <bd> [bvi] [split-horizon-group]
549 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400551int_l2_bridge (vlib_main_t * vm,
552 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553{
Dave Barach97d8dc22016-08-15 15:31:15 -0400554 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsb4743802018-09-05 09:13:57 -0700555 l2_bd_port_type_t port_type;
Dave Barach97d8dc22016-08-15 15:31:15 -0400556 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 u32 bd_index, bd_id;
558 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 u32 rc;
560 u32 shg;
561
Dave Barach97d8dc22016-08-15 15:31:15 -0400562 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 {
564 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400565 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 goto done;
567 }
568
Dave Barach97d8dc22016-08-15 15:31:15 -0400569 if (!unformat (input, "%d", &bd_id))
570 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 error = clib_error_return (0, "expected bridge domain ID `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400572 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400574 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575
John Lo97934772017-05-18 22:26:47 -0400576 if (bd_id > L2_BD_ID_MAX)
577 {
578 error = clib_error_return (0, "bridge domain ID exceed 16M limit",
579 format_unformat_error, input);
580 goto done;
581 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 bd_index = bd_find_or_add_bd_index (&bd_main, bd_id);
583
Dave Barach97d8dc22016-08-15 15:31:15 -0400584 /* optional bvi */
Neale Rannsb4743802018-09-05 09:13:57 -0700585 port_type = L2_BD_PORT_TYPE_NORMAL;
586 if (unformat (input, "bvi"))
587 port_type = L2_BD_PORT_TYPE_BVI;
588 if (unformat (input, "uu-fwd"))
589 port_type = L2_BD_PORT_TYPE_UU_FWD;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 /* optional split horizon group */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 shg = 0;
593 (void) unformat (input, "%d", &shg);
594
Dave Barach97d8dc22016-08-15 15:31:15 -0400595 /* set the interface mode */
596 if ((rc =
Neale Rannsb4743802018-09-05 09:13:57 -0700597 set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE, sw_if_index, bd_index,
598 port_type, shg, 0)))
Dave Barach97d8dc22016-08-15 15:31:15 -0400599 {
600 if (rc == MODE_ERROR_ETH)
601 {
602 error = clib_error_return (0, "bridged interface must be ethernet",
603 format_unformat_error, input);
604 }
605 else if (rc == MODE_ERROR_BVI_DEF)
606 {
607 error =
608 clib_error_return (0, "bridge-domain already has a bvi interface",
609 format_unformat_error, input);
610 }
611 else
612 {
613 error = clib_error_return (0, "invalid configuration for interface",
614 format_unformat_error, input);
615 }
616 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Dave Barach97d8dc22016-08-15 15:31:15 -0400619done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620 return error;
621}
622
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700623/*?
Billy McFall22aa3e92016-09-09 08:46:40 -0400624 * Use this command put an interface into Layer 2 bridge domain. If a
625 * bridge-domain with the provided bridge-domain-id does not exist, it
626 * will be created. Interfaces in a bridge-domain forward packets to
627 * other interfaces in the same bridge-domain based on destination mac
628 * address. To remove an interface from a the Layer 2 bridge domain,
629 * put the interface in a different mode, for example Layer 3 mode.
630 *
631 * Optionally, an interface can be added to a Layer 2 bridge-domain as
632 * a Bridged Virtual Interface (bvi). Only one interface in a Layer 2
633 * bridge-domain can be a bvi.
634 *
635 * Optionally, a split-horizon group can also be specified. This defaults
636 * to 0 if not specified.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700637 *
638 * @cliexpar
Billy McFall22aa3e92016-09-09 08:46:40 -0400639 * Example of how to configure a Layer 2 bridge-domain with three
640 * interfaces (where 200 is the bridge-domain-id):
641 * @cliexcmd{set interface l2 bridge GigabitEthernet0/8/0.200 200}
642 * This interface is added a BVI interface:
643 * @cliexcmd{set interface l2 bridge GigabitEthernet0/9/0.200 200 bvi}
644 * This interface also has a split-horizon group of 1 specified:
645 * @cliexcmd{set interface l2 bridge GigabitEthernet0/a/0.200 200 1}
646 * Example of how to remove an interface from a Layer2 bridge-domain:
647 * @cliexcmd{set interface l3 GigabitEthernet0/a/0.200}
648?*/
649/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700650VLIB_CLI_COMMAND (int_l2_bridge_cli, static) = {
651 .path = "set interface l2 bridge",
Neale Rannsb4743802018-09-05 09:13:57 -0700652 .short_help = "set interface l2 bridge <interface> <bridge-domain-id> [bvi|uu-fwd] [shg]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653 .function = int_l2_bridge,
654};
Dave Barach97d8dc22016-08-15 15:31:15 -0400655/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Dave Barach97d8dc22016-08-15 15:31:15 -0400657/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400658 * Set subinterface in xconnect mode with another interface.
Dave Barach97d8dc22016-08-15 15:31:15 -0400659 * The CLI format is:
660 * set interface l2 xconnect <interface> <peer interface>
661 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662static clib_error_t *
663int_l2_xc (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400664 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
Dave Barach97d8dc22016-08-15 15:31:15 -0400666 vnet_main_t *vnm = vnet_get_main ();
667 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 u32 sw_if_index;
669 u32 xc_sw_if_index;
670
Dave Barach97d8dc22016-08-15 15:31:15 -0400671 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 {
673 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400674 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675 goto done;
676 }
677
Dave Barach97d8dc22016-08-15 15:31:15 -0400678 if (!unformat_user
679 (input, unformat_vnet_sw_interface, vnm, &xc_sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680 {
681 error = clib_error_return (0, "unknown peer interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400682 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683 goto done;
684 }
685
Dave Barach97d8dc22016-08-15 15:31:15 -0400686 /* set the interface mode */
687 if (set_int_l2_mode
Neale Rannsb4743802018-09-05 09:13:57 -0700688 (vm, vnm, MODE_L2_XC, sw_if_index, 0, L2_BD_PORT_TYPE_NORMAL,
689 0, xc_sw_if_index))
Dave Barach97d8dc22016-08-15 15:31:15 -0400690 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691 error = clib_error_return (0, "invalid configuration for interface",
Dave Barach97d8dc22016-08-15 15:31:15 -0400692 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400694 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Dave Barach97d8dc22016-08-15 15:31:15 -0400696done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697 return error;
698}
699
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700700/*?
Billy McFall22aa3e92016-09-09 08:46:40 -0400701 * Use this command put an interface into Layer 2 cross-connect mode.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700702 * Both interfaces must be in this mode for bi-directional traffic. All
Billy McFall22aa3e92016-09-09 08:46:40 -0400703 * packets received on one interface will be transmitted to the other.
704 * To remove the Layer 2 cross-connect, put the interface in a different
705 * mode, for example Layer 3 mode.
706 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700707 * @cliexpar
Billy McFall22aa3e92016-09-09 08:46:40 -0400708 * Example of how to configure a Layer2 cross-connect between two interfaces:
709 * @cliexcmd{set interface l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300}
710 * @cliexcmd{set interface l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300}
711 * Example of how to remove a Layer2 cross-connect:
712 * @cliexcmd{set interface l3 GigabitEthernet0/8/0.300}
713 * @cliexcmd{set interface l3 GigabitEthernet0/9/0.300}
714?*/
715/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716VLIB_CLI_COMMAND (int_l2_xc_cli, static) = {
717 .path = "set interface l2 xconnect",
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700718 .short_help = "set interface l2 xconnect <interface> <peer interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719 .function = int_l2_xc,
720};
Dave Barach97d8dc22016-08-15 15:31:15 -0400721/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700722
Dave Barach97d8dc22016-08-15 15:31:15 -0400723/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400724 * Set subinterface in L3 mode.
Dave Barach97d8dc22016-08-15 15:31:15 -0400725 * The CLI format is:
726 * set interface l3 <interface>
727 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400729int_l3 (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730{
Dave Barach97d8dc22016-08-15 15:31:15 -0400731 vnet_main_t *vnm = vnet_get_main ();
732 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733 u32 sw_if_index;
734
Dave Barach97d8dc22016-08-15 15:31:15 -0400735 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736 {
737 error = clib_error_return (0, "unknown interface `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400738 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 goto done;
740 }
741
Dave Barach97d8dc22016-08-15 15:31:15 -0400742 /* set the interface mode */
Neale Rannsb4743802018-09-05 09:13:57 -0700743 if (set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0,
744 L2_BD_PORT_TYPE_NORMAL, 0, 0))
Dave Barach97d8dc22016-08-15 15:31:15 -0400745 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 error = clib_error_return (0, "invalid configuration for interface",
Dave Barach97d8dc22016-08-15 15:31:15 -0400747 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
Dave Barach97d8dc22016-08-15 15:31:15 -0400751done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 return error;
753}
754
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700755/*?
Billy McFall22aa3e92016-09-09 08:46:40 -0400756 * Modify the packet processing mode of the interface to Layer 3, which
757 * implies packets will be routed. This is the default mode of an interface.
758 * Use this command to remove an interface from a Layer 2 cross-connect or a
759 * Layer 2 bridge.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700760 *
761 * @cliexpar
Billy McFall22aa3e92016-09-09 08:46:40 -0400762 * Example of how to set the mode of an interface to Layer 3:
763 * @cliexcmd{set interface l3 GigabitEthernet0/8/0.200}
764?*/
765/* *INDENT-OFF* */
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700766 VLIB_CLI_COMMAND (int_l3_cli, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 .path = "set interface l3",
Billy McFall22aa3e92016-09-09 08:46:40 -0400768 .short_help = "set interface l3 <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 .function = int_l3,
770};
Dave Barach97d8dc22016-08-15 15:31:15 -0400771/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Dave Barach97d8dc22016-08-15 15:31:15 -0400773/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400774 * Show interface mode.
Dave Barach97d8dc22016-08-15 15:31:15 -0400775 * The CLI format is:
776 * show mode [<if-name1> <if-name2> ...]
777 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400779show_int_mode (vlib_main_t * vm,
780 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781{
Dave Barach97d8dc22016-08-15 15:31:15 -0400782 vnet_main_t *vnm = vnet_get_main ();
783 clib_error_t *error = 0;
784 char *mode;
785 u8 *args;
786 vnet_interface_main_t *im = &vnm->interface_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400787
Eyal Bariafc47aa2017-04-20 14:45:17 +0300788 vnet_sw_interface_t *si, *sis = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700789 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
790 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400791 u32 sw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792
793 /* See if user wants to show specific interface */
Dave Barach97d8dc22016-08-15 15:31:15 -0400794 if (unformat
795 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400797 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798 vec_add1 (sis, si[0]);
799 }
800 else
Dave Barach97d8dc22016-08-15 15:31:15 -0400801 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 error = clib_error_return (0, "unknown input `%U'",
803 format_unformat_error, input);
804 goto done;
Dave Barach97d8dc22016-08-15 15:31:15 -0400805 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806 }
807
Dave Barach97d8dc22016-08-15 15:31:15 -0400808 if (vec_len (sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809 {
810 /* Gather interfaces. */
811 sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
812 _vec_len (sis) = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400813 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100814 pool_foreach (si, im->sw_interfaces) { vec_add1 (sis, si[0]); }
Dave Barach97d8dc22016-08-15 15:31:15 -0400815 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Dave Barach97d8dc22016-08-15 15:31:15 -0400818 vec_foreach (si, sis)
819 {
Eyal Bariafc47aa2017-04-20 14:45:17 +0300820 l2_input_config_t *config = l2input_intf_config (si->sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +0000821 if (l2_input_is_bridge (config))
Dave Barach97d8dc22016-08-15 15:31:15 -0400822 {
823 u32 bd_id;
824 mode = "l2 bridge";
825 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
826
Neale Ranns47a3d992020-09-29 15:38:51 +0000827 args = format (0, "bd_id %d%s shg %d", bd_id,
828 l2_input_is_bvi (config) ? " bvi" : "", config->shg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 }
Neale Ranns47a3d992020-09-29 15:38:51 +0000830 else if (l2_input_is_xconnect (config))
Dave Barach97d8dc22016-08-15 15:31:15 -0400831 {
832 mode = "l2 xconnect";
833 args = format (0, "%U",
834 format_vnet_sw_if_index_name,
835 vnm, config->output_sw_if_index);
836 }
837 else
838 {
839 mode = "l3";
840 args = format (0, " ");
841 }
842 vlib_cli_output (vm, "%s %U %v\n",
843 mode,
844 format_vnet_sw_if_index_name,
845 vnm, si->sw_if_index, args);
846 vec_free (args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847 }
848
849done:
850 vec_free (sis);
Dave Barach97d8dc22016-08-15 15:31:15 -0400851
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852 return error;
853}
854
Billy McFall22aa3e92016-09-09 08:46:40 -0400855/*?
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700856 * Show the packet processing mode (Layer2 cross-connect, Layer 2 bridge,
Billy McFall22aa3e92016-09-09 08:46:40 -0400857 * Layer 3 routed) of all interfaces and sub-interfaces, or limit the
858 * output to just the provided list of interfaces and sub-interfaces.
859 * The output shows the mode, the interface, and if the interface is
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700860 * a member of a bridge, the bridge-domain-id and the split horizon group (shg).
Billy McFall22aa3e92016-09-09 08:46:40 -0400861 *
862 * @cliexpar
863 * Example of displaying the mode of all interfaces:
864 * @cliexstart{show mode}
865 * l3 local0
866 * l3 GigabitEthernet0/8/0
867 * l3 GigabitEthernet0/9/0
868 * l3 GigabitEthernet0/a/0
869 * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
870 * l2 bridge GigabitEthernet0/9/0.200 bd_id 200 shg 0
871 * l2 bridge GigabitEthernet0/a/0.200 bd_id 200 shg 0
872 * l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300
873 * l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300
874 * @cliexend
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700875 * Example of displaying the mode of a selected list of interfaces:
Billy McFall22aa3e92016-09-09 08:46:40 -0400876 * @cliexstart{show mode GigabitEthernet0/8/0 GigabitEthernet0/8/0.200}
877 * l3 GigabitEthernet0/8/0
878 * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
879 * @cliexend
880?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400881/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700882VLIB_CLI_COMMAND (show_l2_mode, static) = {
883 .path = "show mode",
884 .short_help = "show mode [<if-name1> <if-name2> ...]",
885 .function = show_int_mode,
886};
Dave Barach97d8dc22016-08-15 15:31:15 -0400887/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
889#define foreach_l2_init_function \
890_(feat_bitmap_drop_init) \
891_(l2fib_init) \
Dave Barachb84a3e52016-08-30 17:01:52 -0400892_(l2_input_classify_init) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893_(l2bd_init) \
894_(l2fwd_init) \
Andrew Yourtchenko815d7d52018-02-07 11:37:02 +0100895_(l2_in_out_acl_init) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896_(l2input_init) \
897_(l2_vtr_init) \
898_(l2_invtr_init) \
899_(l2_efp_filter_init) \
900_(l2learn_init) \
901_(l2flood_init) \
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902_(l2output_init) \
903_(l2_patch_init) \
904_(l2_xcrw_init)
905
Dave Barach97d8dc22016-08-15 15:31:15 -0400906clib_error_t *
907l2_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908{
Dave Barach97d8dc22016-08-15 15:31:15 -0400909 clib_error_t *error;
910
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911#define _(a) do { \
912 if ((error = vlib_call_init_function (vm, a))) return error; } \
913while (0);
914 foreach_l2_init_function;
915#undef _
916 return 0;
917}
918
919VLIB_INIT_FUNCTION (l2_init);
Dave Barach97d8dc22016-08-15 15:31:15 -0400920
921/*
922 * fd.io coding-style-patch-verification: ON
923 *
924 * Local Variables:
925 * eval: (c-set-style "gnu")
926 * End:
927 */