blob: 5509e3ffa579878622fd8af16809c9384c2882fa [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * interface_cli.c: interface CLI
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Chris Luke16bcf7d2016-09-01 14:31:46 -040040/**
41 * @file
Billy McFalle9bac692017-08-11 14:05:11 -040042 * @brief Interface CLI.
43 *
44 * Source code for several CLI interface commands.
45 *
Chris Luke16bcf7d2016-09-01 14:31:46 -040046 */
47
Ed Warnickecb9cada2015-12-08 15:45:58 -070048#include <vnet/vnet.h>
49#include <vnet/ip/ip.h>
John Lobcebbb92016-04-05 15:47:43 -040050#include <vppinfra/bitmap.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010051#include <vnet/fib/ip4_fib.h>
52#include <vnet/fib/ip6_fib.h>
Eyal Bari942402b2017-07-26 11:57:04 +030053#include <vnet/l2/l2_output.h>
54#include <vnet/l2/l2_input.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070055
Dave Barachba868bb2016-08-08 09:51:21 -040056static int
57compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070058{
Dave Barachba868bb2016-08-08 09:51:21 -040059 u32 *hi1 = a1;
60 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
Dave Barachba868bb2016-08-08 09:51:21 -040062 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063}
64
65static clib_error_t *
66show_or_clear_hw_interfaces (vlib_main_t * vm,
67 unformat_input_t * input,
68 vlib_cli_command_t * cmd)
69{
Dave Barachba868bb2016-08-08 09:51:21 -040070 clib_error_t *error = 0;
71 vnet_main_t *vnm = vnet_get_main ();
72 vnet_interface_main_t *im = &vnm->interface_main;
73 vnet_hw_interface_t *hi;
74 u32 hw_if_index, *hw_if_indices = 0;
John Lobcebbb92016-04-05 15:47:43 -040075 int i, verbose = -1, is_show, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 is_show = strstr (cmd->path, "show") != 0;
78 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
79 {
80 /* See if user wants to show a specific interface. */
Dave Barachba868bb2016-08-08 09:51:21 -040081 if (unformat
82 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
83 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040084
Sean Hope679ea792016-02-22 15:12:01 -050085 /* See if user wants to show an interface with a specific hw_if_index. */
86 else if (unformat (input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040087 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
89 else if (unformat (input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040090 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
92 else if (unformat (input, "detail"))
93 verbose = 2;
94
95 else if (unformat (input, "brief"))
96 verbose = 0;
97
John Lobcebbb92016-04-05 15:47:43 -040098 else if (unformat (input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -040099 {
100 show_bond = 1;
101 if (verbose < 0)
102 verbose = 0; /* default to brief for link bonding */
103 }
John Lobcebbb92016-04-05 15:47:43 -0400104
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 else
106 {
107 error = clib_error_return (0, "unknown input `%U'",
108 format_unformat_error, input);
109 goto done;
110 }
111 }
Dave Barachba868bb2016-08-08 09:51:21 -0400112
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113 /* Gather interfaces. */
114 if (vec_len (hw_if_indices) == 0)
115 pool_foreach (hi, im->hw_interfaces,
116 vec_add1 (hw_if_indices, hi - im->hw_interfaces));
117
Dave Barachba868bb2016-08-08 09:51:21 -0400118 if (verbose < 0)
119 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400120
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 if (is_show)
122 {
123 /* Sort by name. */
124 vec_sort_with_function (hw_if_indices, compare_interface_names);
125
126 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
127 for (i = 0; i < vec_len (hw_if_indices); i++)
128 {
129 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400130 if (show_bond == 0) /* show all interfaces */
131 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
132 hi, verbose);
133 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400134 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400135 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400136 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400137 vnet_hw_interface_t *shi;
138 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400139 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400140
141 /* *INDENT-OFF* */
John Lobcebbb92016-04-05 15:47:43 -0400142 clib_bitmap_foreach (hw_idx, hi->bond_info,
Dave Barachba868bb2016-08-08 09:51:21 -0400143 ({
144 shi = vnet_get_hw_interface(vnm, hw_idx);
145 vlib_cli_output (vm, "%U\n",
146 format_vnet_hw_interface, vnm, shi, verbose);
147 }));
148 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 }
151 }
152 else
153 {
154 for (i = 0; i < vec_len (hw_if_indices); i++)
155 {
Dave Barachba868bb2016-08-08 09:51:21 -0400156 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
158 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
159 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400160
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 if (dc->clear_counters)
162 dc->clear_counters (hi->dev_instance);
163 }
164 }
165
Dave Barachba868bb2016-08-08 09:51:21 -0400166done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 vec_free (hw_if_indices);
168 return error;
169}
170
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700171/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400172 * Display more detailed information about all or a list of given interfaces.
173 * The verboseness of the output can be controlled by the following optional
174 * parameters:
175 * - brief: Only show name, index and state (default for bonded interfaces).
176 * - verbose: Also display additional attributes (default for all other interfaces).
177 * - detail: Also display all remaining attributes and extended statistics.
178 *
179 * To limit the output of the command to bonded interfaces and their slave
180 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700181 *
182 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400183 * Example of how to display default data for all interfaces:
184 * @cliexstart{show hardware-interfaces}
185 * Name Idx Link Hardware
186 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
187 * Ethernet address ec:f4:bb:c0:bc:fc
188 * Intel e1000
189 * carrier up full duplex speed 1000 mtu 9216
190 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
191 * cpu socket 0
192 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
193 * Ethernet address ec:f4:bb:c0:bc:fd
194 * Intel e1000
195 * carrier up full duplex speed 1000 mtu 9216
196 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
197 * cpu socket 0
198 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
199 * Ethernet address 02:fe:a5:a9:8b:8e
200 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
201 * Ethernet address 02:fe:c0:4e:3b:b0
202 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
203 * Ethernet address 02:fe:1f:73:92:81
204 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
205 * Ethernet address 02:fe:f2:25:c4:68
206 * local0 0 down local0
207 * local
208 * @cliexend
209 * Example of how to display '<em>verbose</em>' data for an interface by name and
210 * software index (where 2 is the software index):
211 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
212 * Name Idx Link Hardware
213 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
214 * Ethernet address ec:f4:bb:c0:bc:fc
215 * Intel e1000
216 * carrier up full duplex speed 1000 mtu 9216
217 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
218 * cpu socket 0
219 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
220 * Ethernet address ec:f4:bb:c0:bc:fd
221 * Intel e1000
222 * carrier up full duplex speed 1000 mtu 9216
223 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
224 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700225 * @cliexend
226 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400227/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
229 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400230 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
231 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232 .function = show_or_clear_hw_interfaces,
233};
Dave Barachba868bb2016-08-08 09:51:21 -0400234/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Billy McFalle9bac692017-08-11 14:05:11 -0400236
237/*?
238 * Clear the extended statistics for all or a list of given interfaces
239 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
240 *
241 * @cliexpar
242 * Example of how to clear the extended statistics for all interfaces:
243 * @cliexcmd{clear hardware-interfaces}
244 * Example of how to clear the extended statistics for an interface by
245 * name and software index (where 2 is the software index):
246 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
247 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400248/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
250 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400251 .short_help = "clear hardware-interfaces "
252 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 .function = show_or_clear_hw_interfaces,
254};
Dave Barachba868bb2016-08-08 09:51:21 -0400255/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barachba868bb2016-08-08 09:51:21 -0400257static int
258sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259{
260 vnet_sw_interface_t *si1 = a1;
261 vnet_sw_interface_t *si2 = a2;
262
Dave Barachba868bb2016-08-08 09:51:21 -0400263 return vnet_sw_interface_compare (vnet_get_main (),
264 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265}
266
267static clib_error_t *
268show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400269 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270{
Dave Barachba868bb2016-08-08 09:51:21 -0400271 clib_error_t *error = 0;
272 vnet_main_t *vnm = vnet_get_main ();
273 vnet_interface_main_t *im = &vnm->interface_main;
274 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200275 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200277 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500278 u8 show_tag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
280 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
281 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282 /* See if user wants to show specific interface */
Dave Barachba868bb2016-08-08 09:51:21 -0400283 if (unformat
284 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285 {
Dave Barachba868bb2016-08-08 09:51:21 -0400286 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 vec_add1 (sorted_sis, si[0]);
288 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700289 else if (unformat (input, "address") || unformat (input, "addr"))
Dave Barachba868bb2016-08-08 09:51:21 -0400290 show_addresses = 1;
Damjan Marion22311502016-10-28 20:30:15 +0200291 else if (unformat (input, "features") || unformat (input, "feat"))
292 show_features = 1;
Dave Barach7be864a2016-11-28 11:41:35 -0500293 else if (unformat (input, "tag"))
294 show_tag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 else
Dave Barachba868bb2016-08-08 09:51:21 -0400296 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 error = clib_error_return (0, "unknown input `%U'",
298 format_unformat_error, input);
299 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400300 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 }
302
Dave Barach7be864a2016-11-28 11:41:35 -0500303 if (show_features || show_tag)
Damjan Marion22311502016-10-28 20:30:15 +0200304 {
305 if (sw_if_index == ~(u32) 0)
306 return clib_error_return (0, "Interface not specified...");
Dave Barach7be864a2016-11-28 11:41:35 -0500307 }
Damjan Marion22311502016-10-28 20:30:15 +0200308
Dave Barach7be864a2016-11-28 11:41:35 -0500309 if (show_features)
310 {
Damjan Marion22311502016-10-28 20:30:15 +0200311 vnet_interface_features_show (vm, sw_if_index);
Eyal Bari942402b2017-07-26 11:57:04 +0300312
313 l2_input_config_t *l2_input = l2input_intf_config (sw_if_index);
314 u32 fb = l2_input->feature_bitmap;
315 /* intf input features are masked by bridge domain */
316 if (l2_input->bridge)
317 fb &= l2input_bd_config (l2_input->bd_index)->feature_bitmap;
318 vlib_cli_output (vm, "\nl2-input:\n%U", format_l2_input_features, fb);
319
320 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
321 vlib_cli_output (vm, "\nl2-output:");
322 if (l2_output->out_vtr_flag)
323 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
324 vlib_cli_output (vm, "%U", format_l2_output_features,
325 l2_output->feature_bitmap);
Damjan Marion22311502016-10-28 20:30:15 +0200326 return 0;
327 }
Dave Barach7be864a2016-11-28 11:41:35 -0500328 if (show_tag)
329 {
330 u8 *tag;
331 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
332 vlib_cli_output (vm, "%U: %s",
333 format_vnet_sw_if_index_name, vnm, sw_if_index,
334 tag ? (char *) tag : "(none)");
335 return 0;
336 }
Damjan Marion22311502016-10-28 20:30:15 +0200337
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400339 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340
Dave Barachba868bb2016-08-08 09:51:21 -0400341 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 {
343 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400344 sorted_sis =
345 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 _vec_len (sorted_sis) = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400347 pool_foreach (si, im->sw_interfaces, (
348 {
Eyal Bari942402b2017-07-26 11:57:04 +0300349 int visible =
350 vnet_swif_is_api_visible (si);
351 if (visible)
352 vec_add1 (sorted_sis, si[0]);}
Dave Barachba868bb2016-08-08 09:51:21 -0400353 ));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
355 /* Sort by name. */
356 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
357 }
358
359 if (show_addresses)
360 {
361 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400362 {
Dave Barachba868bb2016-08-08 09:51:21 -0400363 ip4_main_t *im4 = &ip4_main;
364 ip6_main_t *im6 = &ip6_main;
365 ip_lookup_main_t *lm4 = &im4->lookup_main;
366 ip_lookup_main_t *lm6 = &im6->lookup_main;
367 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400368 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
Dave Barachba868bb2016-08-08 09:51:21 -0400370 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
371 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
372 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barachba868bb2016-08-08 09:51:21 -0400374 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
375 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
376 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377
John Lo4478d8e2018-01-12 17:15:25 -0500378 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
379 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380
Dave Barachba868bb2016-08-08 09:51:21 -0400381 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
382 vlib_cli_output
383 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500384 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400385 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
386 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400387 else
John Lo4478d8e2018-01-12 17:15:25 -0500388 vlib_cli_output
389 (vm, "%U (%s):",
390 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
391 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Eyal Bari942402b2017-07-26 11:57:04 +0300393 /* Display any L2 info */
394 l2_input_config_t *l2_input = l2input_intf_config (si->sw_if_index);
395 if (l2_input->bridge)
Dave Barachba868bb2016-08-08 09:51:21 -0400396 {
John Lo4478d8e2018-01-12 17:15:25 -0500397 bd_main_t *bdm = &bd_main;
Eyal Bari942402b2017-07-26 11:57:04 +0300398 u32 bd_id = l2input_main.bd_configs[l2_input->bd_index].bd_id;
John Lo4478d8e2018-01-12 17:15:25 -0500399 vlib_cli_output (vm, " L2 bridge bd-id %d idx %d shg %d %s",
400 bd_id, bd_find_index (bdm, bd_id), l2_input->shg,
401 l2_input->bvi ? "bvi" : " ");
Dave Barachba868bb2016-08-08 09:51:21 -0400402 }
Eyal Bari942402b2017-07-26 11:57:04 +0300403 else if (l2_input->xconnect)
John Lo4478d8e2018-01-12 17:15:25 -0500404 vlib_cli_output (vm, " L2 xconnect %U",
405 format_vnet_sw_if_index_name, vnm,
406 l2_input->output_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400407
John Lo4478d8e2018-01-12 17:15:25 -0500408 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400409 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500410 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
411 1 /* honor unnumbered */,
412 ({
413 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
414 if (fib4->table_id)
415 vlib_cli_output (vm, " L3 %U/%d ip4 table-id %d fib-idx %d",
416 format_ip4_address, r4, ia->address_length,
417 fib4->table_id,
418 ip4_fib_index_from_table_id (fib4->table_id));
419 else
420 vlib_cli_output (vm, " L3 %U/%d",
421 format_ip4_address, r4, ia->address_length);
422 }));
423 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
John Lo4478d8e2018-01-12 17:15:25 -0500425 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400426 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500427 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
428 1 /* honor unnumbered */,
429 ({
430 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
431 if (fib6->table_id)
432 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
433 format_ip6_address, r6, ia->address_length,
434 fib6->table_id,
435 ip6_fib_index_from_table_id (fib6->table_id));
436 else
437 vlib_cli_output (vm, " L3 %U/%d",
438 format_ip6_address, r6, ia->address_length);
439 }));
440 /* *INDENT-ON* */
Dave Barachba868bb2016-08-08 09:51:21 -0400441 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 }
443 else
444 {
445 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400446 {
447 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
448 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449 }
450
Dave Barachba868bb2016-08-08 09:51:21 -0400451done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 vec_free (sorted_sis);
453 return error;
454}
455
Dave Barachba868bb2016-08-08 09:51:21 -0400456/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400458 .path = "show interface",
Billy McFalle9bac692017-08-11 14:05:11 -0400459 .short_help = "show interface [address|addr|features|feat] [<interface> [<interface> [..]]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 .function = show_sw_interfaces,
461};
Dave Barachba868bb2016-08-08 09:51:21 -0400462/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
464/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400465/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
467 .path = "interface",
468 .short_help = "Interface commands",
469};
Dave Barachba868bb2016-08-08 09:51:21 -0400470/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barachba868bb2016-08-08 09:51:21 -0400472/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
474 .path = "set interface",
475 .short_help = "Interface commands",
476};
Dave Barachba868bb2016-08-08 09:51:21 -0400477/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
479static clib_error_t *
480clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400481 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482{
Dave Barachba868bb2016-08-08 09:51:21 -0400483 vnet_main_t *vnm = vnet_get_main ();
484 vnet_interface_main_t *im = &vnm->interface_main;
485 vlib_simple_counter_main_t *sm;
486 vlib_combined_counter_main_t *cm;
487 static vnet_main_t **my_vnet_mains;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 int i, j, n_counters;
489
490 vec_reset_length (my_vnet_mains);
Dave Barachba868bb2016-08-08 09:51:21 -0400491
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492 for (i = 0; i < vec_len (vnet_mains); i++)
493 {
494 if (vnet_mains[i])
Dave Barachba868bb2016-08-08 09:51:21 -0400495 vec_add1 (my_vnet_mains, vnet_mains[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 }
497
498 if (vec_len (vnet_mains) == 0)
499 vec_add1 (my_vnet_mains, vnm);
500
501 n_counters = vec_len (im->combined_sw_if_counters);
502
503 for (j = 0; j < n_counters; j++)
504 {
Dave Barachba868bb2016-08-08 09:51:21 -0400505 for (i = 0; i < vec_len (my_vnet_mains); i++)
506 {
507 im = &my_vnet_mains[i]->interface_main;
508 cm = im->combined_sw_if_counters + j;
509 vlib_clear_combined_counters (cm);
510 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 }
512
513 n_counters = vec_len (im->sw_if_counters);
514
515 for (j = 0; j < n_counters; j++)
516 {
Dave Barachba868bb2016-08-08 09:51:21 -0400517 for (i = 0; i < vec_len (my_vnet_mains); i++)
518 {
519 im = &my_vnet_mains[i]->interface_main;
520 sm = im->sw_if_counters + j;
521 vlib_clear_simple_counters (sm);
522 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 }
524
525 return 0;
526}
527
Billy McFalle9bac692017-08-11 14:05:11 -0400528/*?
529 * Clear the statistics for all interfaces (statistics associated with the
530 * '<em>show interface</em>' command).
531 *
532 * @cliexpar
533 * Example of how to clear the statistics for all interfaces:
534 * @cliexcmd{clear interfaces}
535 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400536/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
538 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400539 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 .function = clear_interface_counters,
541};
Dave Barachba868bb2016-08-08 09:51:21 -0400542/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543
Chris Luke16bcf7d2016-09-01 14:31:46 -0400544/**
545 * Parse subinterface names.
546 *
Dave Barachba868bb2016-08-08 09:51:21 -0400547 * The following subinterface syntax is supported. The first two are for
548 * backwards compatability:
549 *
550 * <intf-name> <id>
551 * - a subinterface with the name <intf-name>.<id>. The subinterface
552 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
553 *
554 * <intf-name> <min_id>-<max_id>
555 * - a set of the above subinterfaces, repeating for each id
556 * in the range <min_id> to <max_id>
557 *
558 * In the following, exact-match semantics (i.e. the number of vlan tags on the
559 * packet must match the number of tags in the configuration) are used only if
560 * the keyword exact-match is present. Non-exact match is the default.
561 *
562 * <intf-name> <id> dot1q <outer_id> [exact-match]
563 * - a subinterface with the name <intf-name>.<id>. The subinterface
564 * is a single dot1q vlan with vlan id <outer_id>.
565 *
566 * <intf-name> <id> dot1q any [exact-match]
567 * - a subinterface with the name <intf-name>.<id>. The subinterface
568 * is a single dot1q vlan with any vlan id.
569 *
570 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
571 * - a subinterface with the name <intf-name>.<id>. The subinterface
572 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
573 * <inner_id>.
574 *
575 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
576 * - a subinterface with the name <intf-name>.<id>. The subinterface
577 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
578 *
579 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
580 *
581 * - a subinterface with the name <intf-name>.<id>. The subinterface
582 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
583 *
584 * For each of the above CLI, there is a duplicate that uses the keyword
585 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
586 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
587 * tagged packets the inner ethertype is always 0x8100. Also note that
588 * the dot1q and dot1ad naming spaces are independent, so it is legal to
589 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
590 *
591 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
592 * - a subinterface with the name <intf-name>.<id>. The subinterface
593 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
594 * id <inner_id>.
595 *
596 * <intf-name> <id> untagged
597 * - a subinterface with the name <intf-name>.<id>. The subinterface
598 * has no vlan tags. Only one can be specified per interface.
599 *
600 * <intf-name> <id> default
601 * - a subinterface with the name <intf-name>.<id>. This is associated
602 * with a packet that did not match any other configured subinterface
603 * on this interface. Only one can be specified per interface.
604 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605
606static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400607parse_vlan_sub_interfaces (unformat_input_t * input,
608 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609{
Dave Barachba868bb2016-08-08 09:51:21 -0400610 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611 u32 inner_vlan, outer_vlan;
612
Dave Barachba868bb2016-08-08 09:51:21 -0400613 if (unformat (input, "any inner-dot1q any"))
614 {
615 template->sub.eth.flags.two_tags = 1;
616 template->sub.eth.flags.outer_vlan_id_any = 1;
617 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 }
Dave Barachba868bb2016-08-08 09:51:21 -0400619 else if (unformat (input, "any"))
620 {
621 template->sub.eth.flags.one_tag = 1;
622 template->sub.eth.flags.outer_vlan_id_any = 1;
623 }
624 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
625 {
626 template->sub.eth.flags.two_tags = 1;
627 template->sub.eth.flags.inner_vlan_id_any = 1;
628 template->sub.eth.outer_vlan_id = outer_vlan;
629 }
630 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
631 {
632 template->sub.eth.flags.two_tags = 1;
633 template->sub.eth.outer_vlan_id = outer_vlan;
634 template->sub.eth.inner_vlan_id = inner_vlan;
635 }
636 else if (unformat (input, "%d", &outer_vlan))
637 {
638 template->sub.eth.flags.one_tag = 1;
639 template->sub.eth.outer_vlan_id = outer_vlan;
640 }
641 else
642 {
643 error = clib_error_return (0, "expected dot1q config, got `%U'",
644 format_unformat_error, input);
645 goto done;
646 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700647
Dave Barachba868bb2016-08-08 09:51:21 -0400648 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
649 {
650 if (unformat (input, "exact-match"))
651 {
652 template->sub.eth.flags.exact_match = 1;
653 }
654 }
655
656done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 return error;
658}
659
660static clib_error_t *
661create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400662 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663{
Dave Barachba868bb2016-08-08 09:51:21 -0400664 vnet_main_t *vnm = vnet_get_main ();
665 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400667 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 u32 id, id_min, id_max;
669 vnet_sw_interface_t template;
670
671 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400672 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700673 {
674 error = clib_error_return (0, "unknown interface `%U'",
675 format_unformat_error, input);
676 goto done;
677 }
678
679 memset (&template, 0, sizeof (template));
680 template.sub.eth.raw_flags = 0;
681
Dave Barachba868bb2016-08-08 09:51:21 -0400682 if (unformat (input, "%d default", &id_min))
683 {
684 id_max = id_min;
685 template.sub.eth.flags.default_sub = 1;
686 }
687 else if (unformat (input, "%d untagged", &id_min))
688 {
689 id_max = id_min;
690 template.sub.eth.flags.no_tags = 1;
691 template.sub.eth.flags.exact_match = 1;
692 }
693 else if (unformat (input, "%d dot1q", &id_min))
694 {
695 /* parse dot1q config */
696 id_max = id_min;
697 error = parse_vlan_sub_interfaces (input, &template);
698 if (error)
699 goto done;
700 }
701 else if (unformat (input, "%d dot1ad", &id_min))
702 {
703 /* parse dot1ad config */
704 id_max = id_min;
705 template.sub.eth.flags.dot1ad = 1;
706 error = parse_vlan_sub_interfaces (input, &template);
707 if (error)
708 goto done;
709 }
710 else if (unformat (input, "%d-%d", &id_min, &id_max))
711 {
712 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400713 template.sub.eth.flags.exact_match = 1;
714 if (id_min > id_max)
715 goto id_error;
716 }
717 else if (unformat (input, "%d", &id_min))
718 {
719 id_max = id_min;
720 template.sub.eth.flags.one_tag = 1;
721 template.sub.eth.outer_vlan_id = id_min;
722 template.sub.eth.flags.exact_match = 1;
723 }
724 else
725 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726 id_error:
727 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
728 format_unformat_error, input);
729 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400730 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731
732 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400733
Dave Barachba868bb2016-08-08 09:51:21 -0400734 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
735 {
736 error =
737 clib_error_return (0,
738 "not allowed as %v belong to a BondEthernet interface",
739 hi->name);
740 goto done;
741 }
John Lobcebbb92016-04-05 15:47:43 -0400742
Ed Warnickecb9cada2015-12-08 15:45:58 -0700743 for (id = id_min; id <= id_max; id++)
744 {
Dave Barachba868bb2016-08-08 09:51:21 -0400745 uword *p;
746 vnet_interface_main_t *im = &vnm->interface_main;
747 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
748 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
750 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
751 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400752 {
753 if (CLIB_DEBUG > 0)
754 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
755 hi->sw_if_index, id);
756 continue;
757 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
759 kp = clib_mem_alloc (sizeof (*kp));
760 *kp = sup_and_sub_key;
761
762 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200763 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 template.sup_sw_if_index = hi->sw_if_index;
765 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300766 if (id_min < id_max)
767 template.sub.eth.outer_vlan_id = id;
768
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400770 if (error)
771 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400772
Ed Warnickecb9cada2015-12-08 15:45:58 -0700773 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
774 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400775 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
776 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 }
778
Dave Barachba868bb2016-08-08 09:51:21 -0400779done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 return error;
781}
782
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700783/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400784 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
785 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
786 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
787 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
788 * but this is not recommended.
789 *
790 * This command has several variations:
791 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
792 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
793 *
794 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
795 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
796 * match any other subinterfaces should be sent to this subinterface.
797 *
798 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
799 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
800 * to this subinterface.
801 *
802 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
803 * subinterfaces to handle a range of VLAN IDs.
804 *
805 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
806 * Use this command to specify the outer VLAN ID, to either be explicited or to make the
807 * VLAN ID different from the '<em>subId</em>'.
808 *
809 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
810 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
811 * the innner VLAN ID.
812 *
813 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explictly entered, subinterfaces
814 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
815 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
816 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
817 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
818 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
819 * entered, then the default behavior is exact-match.
820 *
821 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700822 *
823 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400824 * @parblock
825 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
826 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700827 *
Billy McFalle9bac692017-08-11 14:05:11 -0400828 * The previous example is shorthand and is equivalent to:
829 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700830 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700831 *
Billy McFalle9bac692017-08-11 14:05:11 -0400832 * Example of how to create a subinterface number that is different from the VLAN ID:
833 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700834 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700835 *
Billy McFalle9bac692017-08-11 14:05:11 -0400836 * Examples of how to create q-in-q and q-in-any subinterfaces:
837 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
838 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700839 *
Billy McFalle9bac692017-08-11 14:05:11 -0400840 * Examples of how to create dot1ad interfaces:
841 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
842 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700843 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700844 *
Billy McFalle9bac692017-08-11 14:05:11 -0400845 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
846 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
847 * is non-exact match:
848 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700849 *
Billy McFalle9bac692017-08-11 14:05:11 -0400850 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
851 * is specified and only one VLAN is configured, but packet contains two VLANs:
852 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700853 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700854 *
Billy McFalle9bac692017-08-11 14:05:11 -0400855 * Example of how to created a subinterface to process untagged packets:
856 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
857 *
858 * Example of how to created a subinterface to process any packet with a VLAN ID that
859 * does not match any other subinterface:
860 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
861 *
862 * When subinterfaces are created, they are in the down state. Example of how to
863 * enable a newly created subinterface:
864 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
865 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700866 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400867/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700868VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700869 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400870 .short_help = "create sub-interfaces <interface> "
871 "{<subId> [default|untagged]} | "
872 "{<subId>-<subId>} | "
873 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 .function = create_sub_interfaces,
875};
Dave Barachba868bb2016-08-08 09:51:21 -0400876/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877
878static clib_error_t *
879set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400880 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881{
Dave Barachba868bb2016-08-08 09:51:21 -0400882 vnet_main_t *vnm = vnet_get_main ();
883 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 u32 sw_if_index, flags;
885
886 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400887 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 {
889 error = clib_error_return (0, "unknown interface `%U'",
890 format_unformat_error, input);
891 goto done;
892 }
893
Dave Barachba868bb2016-08-08 09:51:21 -0400894 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 {
896 error = clib_error_return (0, "unknown flags `%U'",
897 format_unformat_error, input);
898 goto done;
899 }
900
901 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
902 if (error)
903 goto done;
904
Dave Barachba868bb2016-08-08 09:51:21 -0400905done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 return error;
907}
908
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700909
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700910/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400911 * This command is used to change the admin state (up/down) of an interface.
912 *
913 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
914 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
915 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
916 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700917 *
918 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400919 * Example of how to configure the admin state of an interface to '<em>up</em?':
920 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
921 * Example of how to configure the admin state of an interface to '<em>down</em?':
922 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700923 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400924/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925VLIB_CLI_COMMAND (set_state_command, static) = {
926 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400927 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 .function = set_state,
929};
Dave Barachba868bb2016-08-08 09:51:21 -0400930/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931
932static clib_error_t *
933set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400934 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935{
Dave Barachba868bb2016-08-08 09:51:21 -0400936 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 u32 unnumbered_sw_if_index;
938 u32 inherit_from_sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400939 vnet_sw_interface_t *si;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 int is_set = 0;
941 int is_del = 0;
Neale Ranns898273f2017-03-18 02:57:38 -0700942 u32 was_unnum;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700944 if (unformat (input, "%U use %U",
945 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
946 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
947 is_set = 1;
948 else if (unformat (input, "del %U",
949 unformat_vnet_sw_interface, vnm,
950 &unnumbered_sw_if_index))
951 is_del = 1;
952 else
953 return clib_error_return (0, "parse error '%U'",
954 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955
956 si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
Neale Ranns898273f2017-03-18 02:57:38 -0700957 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
958
Dave Barachba868bb2016-08-08 09:51:21 -0400959 if (is_del)
960 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
Dave Barachba868bb2016-08-08 09:51:21 -0400962 si->unnumbered_sw_if_index = (u32) ~ 0;
Neale Ranns898273f2017-03-18 02:57:38 -0700963
Neale Ranns4b919a52017-03-11 05:55:21 -0800964 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
965 [unnumbered_sw_if_index] = ~0;
966 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
967 [unnumbered_sw_if_index] = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400968 }
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700969 else if (is_set)
Dave Barachba868bb2016-08-08 09:51:21 -0400970 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
972 si->unnumbered_sw_if_index = inherit_from_sw_if_index;
Neale Ranns898273f2017-03-18 02:57:38 -0700973
Neale Ranns4b919a52017-03-11 05:55:21 -0800974 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
975 [unnumbered_sw_if_index] =
976 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
977 [inherit_from_sw_if_index];
978 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
979 [unnumbered_sw_if_index] =
980 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
981 [inherit_from_sw_if_index];
Dave Barachba868bb2016-08-08 09:51:21 -0400982 }
Neale Ranns898273f2017-03-18 02:57:38 -0700983
984 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
985 {
986 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
987 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
988 }
Dave Barachba868bb2016-08-08 09:51:21 -0400989
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990 return 0;
991}
992
Dave Barachba868bb2016-08-08 09:51:21 -0400993/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
995 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -0400996 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 .function = set_unnumbered,
998};
Dave Barachba868bb2016-08-08 09:51:21 -0400999/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
1001
1002
1003static clib_error_t *
1004set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001005 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006{
Dave Barachba868bb2016-08-08 09:51:21 -04001007 vnet_main_t *vnm = vnet_get_main ();
1008 vnet_interface_main_t *im = &vnm->interface_main;
1009 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010 u32 hw_if_index, hw_class_index;
1011
1012 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001013 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 {
1015 error = clib_error_return (0, "unknown hardware interface `%U'",
1016 format_unformat_error, input);
1017 goto done;
1018 }
1019
Dave Barachba868bb2016-08-08 09:51:21 -04001020 if (!unformat_user (input, unformat_hash_string,
1021 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 {
1023 error = clib_error_return (0, "unknown hardware class `%U'",
1024 format_unformat_error, input);
1025 goto done;
1026 }
1027
1028 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1029 if (error)
1030 goto done;
1031
Dave Barachba868bb2016-08-08 09:51:21 -04001032done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 return error;
1034}
1035
Dave Barachba868bb2016-08-08 09:51:21 -04001036/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1038 .path = "set interface hw-class",
1039 .short_help = "Set interface hardware class",
1040 .function = set_hw_class,
1041};
Dave Barachba868bb2016-08-08 09:51:21 -04001042/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043
Dave Barachba868bb2016-08-08 09:51:21 -04001044static clib_error_t *
1045vnet_interface_cli_init (vlib_main_t * vm)
1046{
1047 return 0;
1048}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001049
1050VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1051
Dave Barachba868bb2016-08-08 09:51:21 -04001052static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001053renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001054 unformat_input_t * input,
1055 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056{
1057 u32 hw_if_index;
1058 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001059 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060 int rv;
1061
Dave Barachba868bb2016-08-08 09:51:21 -04001062 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001064 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065
Dave Barachba868bb2016-08-08 09:51:21 -04001066 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 return clib_error_return (0, "new dev instance missing");
1068
1069 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1070
1071 switch (rv)
1072 {
1073 case 0:
1074 break;
1075
1076 default:
1077 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001078 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079
1080 }
1081
1082 return 0;
1083}
1084
1085
Dave Barachba868bb2016-08-08 09:51:21 -04001086/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1088 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001089 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 .function = renumber_interface_command_fn,
1091};
Dave Barachba868bb2016-08-08 09:51:21 -04001092/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001093
Damjan Marion8358ff92016-04-15 14:26:00 +02001094static clib_error_t *
1095promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001096 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001097{
Dave Barachba868bb2016-08-08 09:51:21 -04001098 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001099 u32 hw_if_index;
1100 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001101 ethernet_main_t *em = &ethernet_main;
1102 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001103
1104 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001105 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001106 ;
1107 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001108 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001109 flags = 0;
1110 else
1111 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001112 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001113
1114 eif = ethernet_get_interface (em, hw_if_index);
1115 if (!eif)
1116 return clib_error_return (0, "not supported");
1117
1118 ethernet_set_flags (vnm, hw_if_index, flags);
1119 return 0;
1120}
1121
Dave Barachba868bb2016-08-08 09:51:21 -04001122/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001123VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1124 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001125 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001126 .function = promiscuous_cmd,
1127};
Dave Barachba868bb2016-08-08 09:51:21 -04001128/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001129
1130static clib_error_t *
1131mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1132{
Dave Barachba868bb2016-08-08 09:51:21 -04001133 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001134 u32 hw_if_index, mtu;
1135 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
Dave Barachba868bb2016-08-08 09:51:21 -04001136 ethernet_main_t *em = &ethernet_main;
Damjan Marion8358ff92016-04-15 14:26:00 +02001137
1138 if (unformat (input, "%d %U", &mtu,
Dave Barachba868bb2016-08-08 09:51:21 -04001139 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001140 {
Dave Barachba868bb2016-08-08 09:51:21 -04001141 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1142 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
Damjan Marion8358ff92016-04-15 14:26:00 +02001143
1144 if (!eif)
Dave Barachba868bb2016-08-08 09:51:21 -04001145 return clib_error_return (0, "not supported");
Damjan Marion8358ff92016-04-15 14:26:00 +02001146
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001147 if (mtu < hi->min_supported_packet_bytes)
Damjan Marion8358ff92016-04-15 14:26:00 +02001148 return clib_error_return (0, "Invalid mtu (%d): "
1149 "must be >= min pkt bytes (%d)", mtu,
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001150 hi->min_supported_packet_bytes);
Damjan Marion8358ff92016-04-15 14:26:00 +02001151
Mohsin Kazmif2ba9aa2016-04-24 18:53:42 +02001152 if (mtu > hi->max_supported_packet_bytes)
1153 return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
Dave Barachba868bb2016-08-08 09:51:21 -04001154 hi->max_supported_packet_bytes);
Damjan Marion8358ff92016-04-15 14:26:00 +02001155
1156 if (hi->max_packet_bytes != mtu)
1157 {
1158 hi->max_packet_bytes = mtu;
1159 ethernet_set_flags (vnm, hw_if_index, flags);
1160 }
1161 }
1162 else
1163 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001164 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001165 return 0;
1166}
1167
Dave Barachba868bb2016-08-08 09:51:21 -04001168/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001169VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1170 .path = "set interface mtu",
Billy McFalle9bac692017-08-11 14:05:11 -04001171 .short_help = "set interface mtu <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001172 .function = mtu_cmd,
1173};
Dave Barachba868bb2016-08-08 09:51:21 -04001174/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001175
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001176static clib_error_t *
1177set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1178 vlib_cli_command_t * cmd)
1179{
1180 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001181 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001182 clib_error_t *error = 0;
1183 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001184 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001185
1186 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1187 {
1188 error = clib_error_return (0, "unknown interface `%U'",
1189 format_unformat_error, input);
1190 goto done;
1191 }
John Lo62fcc0a2017-10-31 14:31:10 -04001192 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001193 {
1194 error = clib_error_return (0, "expected mac address `%U'",
1195 format_unformat_error, input);
1196 goto done;
1197 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001198 si = vnet_get_sw_interface (vnm, sw_if_index);
1199 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001200done:
1201 return error;
1202}
1203
1204/*?
1205 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1206 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1207 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1208 *
1209 * @cliexpar
1210 * @parblock
1211 * Example of how to change MAC Address of interface:
1212 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1213 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1214 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1215 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1216 * @endparblock
1217?*/
1218/* *INDENT-OFF* */
1219VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1220 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001221 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001222 .function = set_interface_mac_address,
1223};
1224/* *INDENT-ON* */
1225
Dave Barach7be864a2016-11-28 11:41:35 -05001226static clib_error_t *
1227set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1228{
1229 vnet_main_t *vnm = vnet_get_main ();
1230 u32 sw_if_index = ~0;
1231 u8 *tag = 0;
1232
1233 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1234 vnm, &sw_if_index, &tag))
1235 return clib_error_return (0, "unknown input `%U'",
1236 format_unformat_error, input);
1237
1238 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1239
1240 return 0;
1241}
1242
1243/* *INDENT-OFF* */
1244VLIB_CLI_COMMAND (set_tag_command, static) = {
1245 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001246 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001247 .function = set_tag,
1248};
1249/* *INDENT-ON* */
1250
1251static clib_error_t *
1252clear_tag (vlib_main_t * vm, unformat_input_t * input,
1253 vlib_cli_command_t * cmd)
1254{
1255 vnet_main_t *vnm = vnet_get_main ();
1256 u32 sw_if_index = ~0;
1257
1258 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1259 return clib_error_return (0, "unknown input `%U'",
1260 format_unformat_error, input);
1261
1262 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1263
1264 return 0;
1265}
1266
1267/* *INDENT-OFF* */
1268VLIB_CLI_COMMAND (clear_tag_command, static) = {
1269 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001270 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001271 .function = clear_tag,
1272};
1273/* *INDENT-ON* */
1274
Damjan Marion44036902017-04-28 12:29:15 +02001275static clib_error_t *
Stevene3a395c2017-05-09 16:19:50 -07001276set_hw_interface_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1277 u32 queue_id, vnet_hw_interface_rx_mode mode)
1278{
1279 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1280 vnet_device_class_t *dev_class =
1281 vnet_get_device_class (vnm, hw->dev_class_index);
1282 clib_error_t *error;
1283 vnet_hw_interface_rx_mode old_mode;
1284 int rv;
1285
Damjan Marion4e53a0d2017-06-21 14:29:44 +02001286 if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
1287 mode = hw->default_rx_mode;
1288
Stevene3a395c2017-05-09 16:19:50 -07001289 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &old_mode);
1290 switch (rv)
1291 {
1292 case 0:
1293 if (old_mode == mode)
1294 return 0; /* same rx-mode, no change */
1295 break;
1296 case VNET_API_ERROR_INVALID_INTERFACE:
1297 return clib_error_return (0, "invalid interface");
Stevenbd8a6112017-07-30 10:29:26 -07001298 case VNET_API_ERROR_INVALID_QUEUE:
1299 return clib_error_return (0, "invalid queue");
Stevene3a395c2017-05-09 16:19:50 -07001300 default:
1301 return clib_error_return (0, "unknown error");
1302 }
1303
1304 if (dev_class->rx_mode_change_function)
1305 {
1306 error = dev_class->rx_mode_change_function (vnm, hw_if_index, queue_id,
1307 mode);
1308 if (error)
1309 return (error);
1310 }
1311
1312 rv = vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1313 switch (rv)
1314 {
1315 case 0:
1316 break;
1317 case VNET_API_ERROR_UNSUPPORTED:
1318 return clib_error_return (0, "unsupported");
1319 case VNET_API_ERROR_INVALID_INTERFACE:
1320 return clib_error_return (0, "invalid interface");
Stevenbd8a6112017-07-30 10:29:26 -07001321 case VNET_API_ERROR_INVALID_QUEUE:
1322 return clib_error_return (0, "invalid queue");
Stevene3a395c2017-05-09 16:19:50 -07001323 default:
1324 return clib_error_return (0, "unknown error");
1325 }
1326
1327 return 0;
1328}
1329
Stevenad8015b2017-10-29 22:10:46 -07001330clib_error_t *
1331set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1332 u8 queue_id_valid, u32 queue_id,
1333 vnet_hw_interface_rx_mode mode)
1334{
1335 clib_error_t *error = 0;
1336 vnet_hw_interface_t *hw;
1337 int i;
1338
1339 hw = vnet_get_hw_interface (vnm, hw_if_index);
1340
1341 if (queue_id_valid == 0)
1342 {
1343 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1344 {
1345 error = set_hw_interface_rx_mode (vnm, hw_if_index, i, mode);
1346 if (error)
1347 break;
1348 }
1349 hw->default_rx_mode = mode;
1350 }
1351 else
1352 error = set_hw_interface_rx_mode (vnm, hw_if_index, queue_id, mode);
1353
1354 return (error);
1355}
1356
Stevene3a395c2017-05-09 16:19:50 -07001357static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001358set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1359 vlib_cli_command_t * cmd)
1360{
1361 clib_error_t *error = 0;
1362 unformat_input_t _line_input, *line_input = &_line_input;
1363 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001364 u32 hw_if_index = (u32) ~ 0;
1365 u32 queue_id = (u32) ~ 0;
1366 vnet_hw_interface_rx_mode mode = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001367 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001368
Damjan Marion44036902017-04-28 12:29:15 +02001369 if (!unformat_user (input, unformat_line_input, line_input))
1370 return 0;
1371
1372 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1373 {
1374 if (unformat
1375 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1376 ;
1377 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001378 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001379 else if (unformat (line_input, "polling"))
1380 mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
1381 else if (unformat (line_input, "interrupt"))
1382 mode = VNET_HW_INTERFACE_RX_MODE_INTERRUPT;
1383 else if (unformat (line_input, "adaptive"))
1384 mode = VNET_HW_INTERFACE_RX_MODE_ADAPTIVE;
1385 else
1386 {
1387 error = clib_error_return (0, "parse error: '%U'",
1388 format_unformat_error, line_input);
1389 unformat_free (line_input);
1390 return error;
1391 }
1392 }
1393
1394 unformat_free (line_input);
1395
1396 if (hw_if_index == (u32) ~ 0)
1397 return clib_error_return (0, "please specify valid interface name");
1398
1399 if (mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
1400 return clib_error_return (0, "please specify valid rx-mode");
1401
Stevenad8015b2017-10-29 22:10:46 -07001402 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1403 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001404
Stevene3a395c2017-05-09 16:19:50 -07001405 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001406}
1407
1408/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001409 * This command is used to assign the RX packet processing mode (polling,
1410 * interrupt, adaptive) of the a given interface, and optionally a
1411 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1412 * is applied to all queues of the interface. Not all interfaces support
1413 * all modes. To display the current rx-mode use the command
1414 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001415 *
1416 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001417 * Example of how to assign rx-mode to all queues on an interface:
1418 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1419 * Example of how to assign rx-mode to one queue of an interface:
1420 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1421 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001422 * @cliexstart{show interface rx-placement}
1423 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001424 * node dpdk-input:
1425 * GigabitEthernet7/0/0 queue 0 (polling)
1426 * node vhost-user-input:
1427 * VirtualEthernet0/0/12 queue 0 (interrupt)
1428 * VirtualEthernet0/0/12 queue 2 (polling)
1429 * VirtualEthernet0/0/13 queue 0 (polling)
1430 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001431 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001432 * node dpdk-input:
1433 * GigabitEthernet7/0/1 queue 0 (polling)
1434 * node vhost-user-input:
1435 * VirtualEthernet0/0/12 queue 1 (polling)
1436 * VirtualEthernet0/0/12 queue 3 (polling)
1437 * VirtualEthernet0/0/13 queue 1 (polling)
1438 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001439 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001440?*/
1441/* *INDENT-OFF* */
1442VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1443 .path = "set interface rx-mode",
1444 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1445 .function = set_interface_rx_mode,
1446};
1447/* *INDENT-ON* */
1448
1449static clib_error_t *
1450show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1451 vlib_cli_command_t * cmd)
1452{
1453 u8 *s = 0;
1454 vnet_main_t *vnm = vnet_get_main ();
1455 vnet_device_input_runtime_t *rt;
1456 vnet_device_and_queue_t *dq;
1457 vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
1458 uword si;
1459 int index = 0;
1460
1461 /* *INDENT-OFF* */
1462 foreach_vlib_main (({
1463 clib_bitmap_foreach (si, pn->sibling_bitmap,
1464 ({
1465 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1466
1467 if (vec_len (rt->devices_and_queues))
1468 s = format (s, " node %U:\n", format_vlib_node_name, vm, si);
1469
1470 vec_foreach (dq, rt->devices_and_queues)
1471 {
Steven9d6d9892017-06-30 07:15:02 -07001472 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm,
1473 dq->hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001474 s = format (s, " %U queue %u (%U)\n",
Steven9d6d9892017-06-30 07:15:02 -07001475 format_vnet_sw_if_index_name, vnm, hi->sw_if_index,
Damjan Marion44036902017-04-28 12:29:15 +02001476 dq->queue_id,
1477 format_vnet_hw_interface_rx_mode, dq->mode);
1478 }
1479 }));
1480 if (vec_len (s) > 0)
1481 {
1482 vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
1483 vlib_worker_threads[index].name, s);
1484 vec_reset_length (s);
1485 }
1486 index++;
1487 }));
1488 /* *INDENT-ON* */
1489
1490 vec_free (s);
1491 return 0;
1492}
1493
Billy McFalle9bac692017-08-11 14:05:11 -04001494/*?
1495 * This command is used to display the interface and queue worker
1496 * thread placement.
1497 *
1498 * @cliexpar
1499 * Example of how to display the interface placement:
1500 * @cliexstart{show interface rx-placement}
1501 * Thread 1 (vpp_wk_0):
1502 * node dpdk-input:
1503 * GigabitEthernet7/0/0 queue 0 (polling)
1504 * node vhost-user-input:
1505 * VirtualEthernet0/0/12 queue 0 (polling)
1506 * VirtualEthernet0/0/12 queue 2 (polling)
1507 * VirtualEthernet0/0/13 queue 0 (polling)
1508 * VirtualEthernet0/0/13 queue 2 (polling)
1509 * Thread 2 (vpp_wk_1):
1510 * node dpdk-input:
1511 * GigabitEthernet7/0/1 queue 0 (polling)
1512 * node vhost-user-input:
1513 * VirtualEthernet0/0/12 queue 1 (polling)
1514 * VirtualEthernet0/0/12 queue 3 (polling)
1515 * VirtualEthernet0/0/13 queue 1 (polling)
1516 * VirtualEthernet0/0/13 queue 3 (polling)
1517 * @cliexend
1518?*/
Damjan Marion44036902017-04-28 12:29:15 +02001519/* *INDENT-OFF* */
1520VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1521 .path = "show interface rx-placement",
1522 .short_help = "show interface rx-placement",
1523 .function = show_interface_rx_placement_fn,
1524};
1525/* *INDENT-ON* */
1526
1527static clib_error_t *
1528set_interface_rx_placement (vlib_main_t * vm, unformat_input_t * input,
1529 vlib_cli_command_t * cmd)
1530{
1531 clib_error_t *error = 0;
1532 unformat_input_t _line_input, *line_input = &_line_input;
1533 vnet_main_t *vnm = vnet_get_main ();
1534 vnet_device_main_t *vdm = &vnet_device_main;
1535 vnet_hw_interface_rx_mode mode;
1536 u32 hw_if_index = (u32) ~ 0;
1537 u32 queue_id = (u32) 0;
1538 u32 thread_index = (u32) ~ 0;
1539 int rv;
1540
1541 if (!unformat_user (input, unformat_line_input, line_input))
1542 return 0;
1543
1544 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1545 {
1546 if (unformat
1547 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1548 ;
1549 else if (unformat (line_input, "queue %d", &queue_id))
1550 ;
1551 else if (unformat (line_input, "main", &thread_index))
1552 thread_index = 0;
1553 else if (unformat (line_input, "worker %d", &thread_index))
1554 thread_index += vdm->first_worker_thread_index;
1555 else
1556 {
1557 error = clib_error_return (0, "parse error: '%U'",
1558 format_unformat_error, line_input);
1559 unformat_free (line_input);
1560 return error;
1561 }
1562 }
1563
1564 unformat_free (line_input);
1565
1566 if (hw_if_index == (u32) ~ 0)
1567 return clib_error_return (0, "please specify valid interface name");
1568
1569 if (thread_index > vdm->last_worker_thread_index)
1570 return clib_error_return (0,
1571 "please specify valid worker thread or main");
1572
1573 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &mode);
1574
1575 if (rv)
1576 return clib_error_return (0, "not found");
1577
1578 rv = vnet_hw_interface_unassign_rx_thread (vnm, hw_if_index, queue_id);
1579
1580 if (rv)
1581 return clib_error_return (0, "not found");
1582
1583 vnet_hw_interface_assign_rx_thread (vnm, hw_if_index, queue_id,
1584 thread_index);
1585 vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1586
1587 return 0;
1588}
1589
1590/*?
1591 * This command is used to assign a given interface, and optionally a
1592 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001593 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1594 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001595 *
1596 * @cliexpar
1597 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001598 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001599 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001600 * node dpdk-input:
1601 * GigabitEthernet7/0/0 queue 0 (polling)
1602 * node vhost-user-input:
1603 * VirtualEthernet0/0/12 queue 0 (polling)
1604 * VirtualEthernet0/0/12 queue 2 (polling)
1605 * VirtualEthernet0/0/13 queue 0 (polling)
1606 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001607 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001608 * node dpdk-input:
1609 * GigabitEthernet7/0/1 queue 0 (polling)
1610 * node vhost-user-input:
1611 * VirtualEthernet0/0/12 queue 1 (polling)
1612 * VirtualEthernet0/0/12 queue 3 (polling)
1613 * VirtualEthernet0/0/13 queue 1 (polling)
1614 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001615 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001616 * Example of how to assign a interface and queue to a worker thread:
1617 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1618 * Example of how to display the interface placement:
1619 * @cliexstart{show interface rx-placement}
1620 * Thread 1 (vpp_wk_0):
1621 * node dpdk-input:
1622 * GigabitEthernet7/0/0 queue 0 (polling)
1623 * node vhost-user-input:
1624 * VirtualEthernet0/0/12 queue 0 (polling)
1625 * VirtualEthernet0/0/12 queue 1 (polling)
1626 * VirtualEthernet0/0/12 queue 2 (polling)
1627 * VirtualEthernet0/0/13 queue 0 (polling)
1628 * VirtualEthernet0/0/13 queue 2 (polling)
1629 * Thread 2 (vpp_wk_1):
1630 * node dpdk-input:
1631 * GigabitEthernet7/0/1 queue 0 (polling)
1632 * node vhost-user-input:
1633 * VirtualEthernet0/0/12 queue 3 (polling)
1634 * VirtualEthernet0/0/13 queue 1 (polling)
1635 * VirtualEthernet0/0/13 queue 3 (polling)
1636 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001637?*/
1638/* *INDENT-OFF* */
1639VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1640 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001641 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001642 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001643 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001644 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001645};
Damjan Marion44036902017-04-28 12:29:15 +02001646/* *INDENT-ON* */
Dave Barachba868bb2016-08-08 09:51:21 -04001647/*
1648 * fd.io coding-style-patch-verification: ON
1649 *
1650 * Local Variables:
1651 * eval: (c-set-style "gnu")
1652 * End:
1653 */