blob: e5122c8551f5b579f91d0206bfbfdf19d39135f5 [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 */
Chris Luke16bcf7d2016-09-01 14:31:46 -040039/**
40 * @file
Billy McFalle9bac692017-08-11 14:05:11 -040041 * @brief Interface CLI.
42 *
43 * Source code for several CLI interface commands.
44 *
Chris Luke16bcf7d2016-09-01 14:31:46 -040045 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vnet/vnet.h>
47#include <vnet/ip/ip.h>
John Lobcebbb92016-04-05 15:47:43 -040048#include <vppinfra/bitmap.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049#include <vnet/fib/ip4_fib.h>
50#include <vnet/fib/ip6_fib.h>
Eyal Bari942402b2017-07-26 11:57:04 +030051#include <vnet/l2/l2_output.h>
52#include <vnet/l2/l2_input.h>
Neale Rannsba4a5bf2020-01-09 06:43:14 +000053#include <vnet/classify/vnet_classify.h>
Damjan Marion94100532020-11-06 23:25:57 +010054#include <vnet/interface/rx_queue_funcs.h>
Mohsin Kazmi005605f2021-05-24 18:33:50 +020055#include <vnet/interface/tx_queue_funcs.h>
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +000056#include <vnet/hash/hash.h>
Dave Barachba868bb2016-08-08 09:51:21 -040057static int
58compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
Dave Barachba868bb2016-08-08 09:51:21 -040060 u32 *hi1 = a1;
61 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
Dave Barachba868bb2016-08-08 09:51:21 -040063 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070064}
65
66static clib_error_t *
67show_or_clear_hw_interfaces (vlib_main_t * vm,
68 unformat_input_t * input,
Benoît Ganne17814d72020-07-24 09:57:11 +020069 vlib_cli_command_t * cmd, int is_show)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Dave Barachba868bb2016-08-08 09:51:21 -040071 clib_error_t *error = 0;
Vratko Polak34149772022-03-31 12:32:10 +020072 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachba868bb2016-08-08 09:51:21 -040073 vnet_main_t *vnm = vnet_get_main ();
74 vnet_interface_main_t *im = &vnm->interface_main;
75 vnet_hw_interface_t *hi;
76 u32 hw_if_index, *hw_if_indices = 0;
Benoît Ganne17814d72020-07-24 09:57:11 +020077 int i, verbose = -1, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Vratko Polak34149772022-03-31 12:32:10 +020079 if (!unformat_user (input, unformat_line_input, line_input))
Mohammed Hawari83caa622022-08-08 10:31:11 +020080 goto skip_unformat;
Vratko Polak34149772022-03-31 12:32:10 +020081
82 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -070083 {
84 /* See if user wants to show a specific interface. */
Vratko Polak34149772022-03-31 12:32:10 +020085 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
86 &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040087 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040088
Sean Hope679ea792016-02-22 15:12:01 -050089 /* See if user wants to show an interface with a specific hw_if_index. */
Vratko Polak34149772022-03-31 12:32:10 +020090 else if (unformat (line_input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040091 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
Vratko Polak34149772022-03-31 12:32:10 +020093 else if (unformat (line_input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040094 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Vratko Polak34149772022-03-31 12:32:10 +020096 else if (unformat (line_input, "detail"))
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 verbose = 2;
98
Vratko Polak34149772022-03-31 12:32:10 +020099 else if (unformat (line_input, "brief"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 verbose = 0;
101
Vratko Polak34149772022-03-31 12:32:10 +0200102 else if (unformat (line_input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -0400103 {
104 show_bond = 1;
105 if (verbose < 0)
106 verbose = 0; /* default to brief for link bonding */
107 }
John Lobcebbb92016-04-05 15:47:43 -0400108
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 else
110 {
111 error = clib_error_return (0, "unknown input `%U'",
Vratko Polak34149772022-03-31 12:32:10 +0200112 format_unformat_error, line_input);
113 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 goto done;
115 }
116 }
Dave Barachba868bb2016-08-08 09:51:21 -0400117
Vratko Polak34149772022-03-31 12:32:10 +0200118 unformat_free (line_input);
119
Mohammed Hawari83caa622022-08-08 10:31:11 +0200120skip_unformat:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 /* Gather interfaces. */
122 if (vec_len (hw_if_indices) == 0)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100123 pool_foreach (hi, im->hw_interfaces)
124 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barachba868bb2016-08-08 09:51:21 -0400126 if (verbose < 0)
127 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400128
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 if (is_show)
130 {
131 /* Sort by name. */
132 vec_sort_with_function (hw_if_indices, compare_interface_names);
133
134 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
135 for (i = 0; i < vec_len (hw_if_indices); i++)
136 {
137 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400138 if (show_bond == 0) /* show all interfaces */
139 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
140 hi, verbose);
141 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400142 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400143 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400144 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400145 vnet_hw_interface_t *shi;
146 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400147 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400148
149 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100150 clib_bitmap_foreach (hw_idx, hi->bond_info)
151 {
Dave Barachba868bb2016-08-08 09:51:21 -0400152 shi = vnet_get_hw_interface(vnm, hw_idx);
153 vlib_cli_output (vm, "%U\n",
154 format_vnet_hw_interface, vnm, shi, verbose);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100155 }
Dave Barachba868bb2016-08-08 09:51:21 -0400156 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400157 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 }
159 }
160 else
161 {
162 for (i = 0; i < vec_len (hw_if_indices); i++)
163 {
Dave Barachba868bb2016-08-08 09:51:21 -0400164 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
166 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
167 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400168
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 if (dc->clear_counters)
170 dc->clear_counters (hi->dev_instance);
171 }
172 }
173
Dave Barachba868bb2016-08-08 09:51:21 -0400174done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 vec_free (hw_if_indices);
176 return error;
177}
178
Benoît Ganne17814d72020-07-24 09:57:11 +0200179static clib_error_t *
180show_hw_interfaces (vlib_main_t * vm,
181 unformat_input_t * input, vlib_cli_command_t * cmd)
182{
183 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
184}
185
186static clib_error_t *
187clear_hw_interfaces (vlib_main_t * vm,
188 unformat_input_t * input, vlib_cli_command_t * cmd)
189{
190 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
191}
192
193
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700194/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400195 * Display more detailed information about all or a list of given interfaces.
196 * The verboseness of the output can be controlled by the following optional
197 * parameters:
198 * - brief: Only show name, index and state (default for bonded interfaces).
199 * - verbose: Also display additional attributes (default for all other interfaces).
200 * - detail: Also display all remaining attributes and extended statistics.
201 *
202 * To limit the output of the command to bonded interfaces and their slave
203 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700204 *
205 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400206 * Example of how to display default data for all interfaces:
207 * @cliexstart{show hardware-interfaces}
208 * Name Idx Link Hardware
209 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
210 * Ethernet address ec:f4:bb:c0:bc:fc
211 * Intel e1000
212 * carrier up full duplex speed 1000 mtu 9216
213 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
214 * cpu socket 0
215 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
216 * Ethernet address ec:f4:bb:c0:bc:fd
217 * Intel e1000
218 * carrier up full duplex speed 1000 mtu 9216
219 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
220 * cpu socket 0
221 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
222 * Ethernet address 02:fe:a5:a9:8b:8e
223 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
224 * Ethernet address 02:fe:c0:4e:3b:b0
225 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
226 * Ethernet address 02:fe:1f:73:92:81
227 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
228 * Ethernet address 02:fe:f2:25:c4:68
229 * local0 0 down local0
230 * local
231 * @cliexend
232 * Example of how to display '<em>verbose</em>' data for an interface by name and
233 * software index (where 2 is the software index):
234 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
235 * Name Idx Link Hardware
236 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
237 * Ethernet address ec:f4:bb:c0:bc:fc
238 * Intel e1000
239 * carrier up full duplex speed 1000 mtu 9216
240 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
241 * cpu socket 0
242 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
243 * Ethernet address ec:f4:bb:c0:bc:fd
244 * Intel e1000
245 * carrier up full duplex speed 1000 mtu 9216
246 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
247 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700248 * @cliexend
249 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400250/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
252 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400253 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
254 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200255 .function = show_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256};
Dave Barachba868bb2016-08-08 09:51:21 -0400257/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
Billy McFalle9bac692017-08-11 14:05:11 -0400259
260/*?
261 * Clear the extended statistics for all or a list of given interfaces
262 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
263 *
264 * @cliexpar
265 * Example of how to clear the extended statistics for all interfaces:
266 * @cliexcmd{clear hardware-interfaces}
267 * Example of how to clear the extended statistics for an interface by
268 * name and software index (where 2 is the software index):
269 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
270 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400271/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
273 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400274 .short_help = "clear hardware-interfaces "
275 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200276 .function = clear_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277};
Dave Barachba868bb2016-08-08 09:51:21 -0400278/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barachba868bb2016-08-08 09:51:21 -0400280static int
281sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282{
283 vnet_sw_interface_t *si1 = a1;
284 vnet_sw_interface_t *si2 = a2;
285
Dave Barachba868bb2016-08-08 09:51:21 -0400286 return vnet_sw_interface_compare (vnet_get_main (),
287 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288}
289
290static clib_error_t *
291show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400292 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293{
Dave Barachba868bb2016-08-08 09:51:21 -0400294 clib_error_t *error = 0;
295 vnet_main_t *vnm = vnet_get_main ();
Dave Barach525c9d02018-05-26 10:48:55 -0400296 unformat_input_t _linput, *linput = &_linput;
Dave Barachba868bb2016-08-08 09:51:21 -0400297 vnet_interface_main_t *im = &vnm->interface_main;
298 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200299 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200301 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500302 u8 show_tag = 0;
Jon Loeliger9485d992019-11-08 15:05:23 -0600303 u8 show_vtr = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400304 int verbose = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Dave Barach525c9d02018-05-26 10:48:55 -0400306 /*
307 * Get a line of input. Won't work if the user typed
308 * "show interface" and nothing more.
309 */
310 if (unformat_user (input, unformat_line_input, linput))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 {
Dave Barach525c9d02018-05-26 10:48:55 -0400312 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700313 {
Dave Barach525c9d02018-05-26 10:48:55 -0400314 /* See if user wants to show specific interface */
315 if (unformat
316 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
317 {
318 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
319 vec_add1 (sorted_sis, si[0]);
320 }
321 else if (unformat (linput, "address") || unformat (linput, "addr"))
322 show_addresses = 1;
323 else if (unformat (linput, "features") || unformat (linput, "feat"))
324 show_features = 1;
325 else if (unformat (linput, "tag"))
326 show_tag = 1;
Jon Loeliger9485d992019-11-08 15:05:23 -0600327 else if (unformat (linput, "vtr"))
328 show_vtr = 1;
Dave Barach525c9d02018-05-26 10:48:55 -0400329 else if (unformat (linput, "verbose"))
330 verbose = 1;
Neale Rannse619ada2021-09-22 10:49:43 +0000331 else if (unformat (linput, "%d", &sw_if_index))
332 {
333 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
334 {
335 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
336 vec_add1 (sorted_sis, si[0]);
337 }
338 else
339 {
340 vec_free (sorted_sis);
341 error = clib_error_return (0, "unknown interface index `%d'",
342 sw_if_index);
343 goto done;
344 }
345 }
Dave Barach525c9d02018-05-26 10:48:55 -0400346 else
347 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400348 vec_free (sorted_sis);
Dave Barach525c9d02018-05-26 10:48:55 -0400349 error = clib_error_return (0, "unknown input `%U'",
350 format_unformat_error, linput);
351 goto done;
352 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 }
Dave Barach525c9d02018-05-26 10:48:55 -0400354 unformat_free (linput);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 }
Jon Loeliger9485d992019-11-08 15:05:23 -0600356 if (show_features || show_tag || show_vtr)
Damjan Marion22311502016-10-28 20:30:15 +0200357 {
358 if (sw_if_index == ~(u32) 0)
Dave Barach8fdde3c2019-05-17 10:46:40 -0400359 {
360 vec_free (sorted_sis);
361 return clib_error_return (0, "Interface not specified...");
362 }
Dave Barach7be864a2016-11-28 11:41:35 -0500363 }
Damjan Marion22311502016-10-28 20:30:15 +0200364
Dave Barach7be864a2016-11-28 11:41:35 -0500365 if (show_features)
366 {
Dave Barach525c9d02018-05-26 10:48:55 -0400367 vnet_interface_features_show (vm, sw_if_index, verbose);
Neale Ranns47a3d992020-09-29 15:38:51 +0000368 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
Eyal Bari942402b2017-07-26 11:57:04 +0300369
370 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
371 vlib_cli_output (vm, "\nl2-output:");
372 if (l2_output->out_vtr_flag)
373 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
374 vlib_cli_output (vm, "%U", format_l2_output_features,
Neale Ranns5d9df1d2018-11-09 08:19:27 -0800375 l2_output->feature_bitmap, 1);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400376 vec_free (sorted_sis);
Damjan Marion22311502016-10-28 20:30:15 +0200377 return 0;
378 }
Dave Barach7be864a2016-11-28 11:41:35 -0500379 if (show_tag)
380 {
381 u8 *tag;
382 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
383 vlib_cli_output (vm, "%U: %s",
384 format_vnet_sw_if_index_name, vnm, sw_if_index,
385 tag ? (char *) tag : "(none)");
Dave Barach8fdde3c2019-05-17 10:46:40 -0400386 vec_free (sorted_sis);
Dave Barach7be864a2016-11-28 11:41:35 -0500387 return 0;
388 }
Damjan Marion22311502016-10-28 20:30:15 +0200389
Jon Loeliger9485d992019-11-08 15:05:23 -0600390 /*
391 * Show vlan tag rewrite data for one interface.
392 */
393 if (show_vtr)
394 {
395 u32 vtr_op = L2_VTR_DISABLED;
396 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
397
398 if (l2vtr_get (vm, vnm, sw_if_index,
399 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
400 {
401 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
402 format_vnet_sw_if_index_name, vnm, sw_if_index);
403 return 0;
404 }
405 vlib_cli_output (vm, "%U: VTR %0U",
406 format_vnet_sw_if_index_name, vnm, sw_if_index,
407 format_vtr, vtr_op, push_dot1q, tag1, tag2);
408 return 0;
409 }
410
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400412 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
Dave Barachba868bb2016-08-08 09:51:21 -0400414 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 {
416 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400417 sorted_sis =
418 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +0200419 vec_set_len (sorted_sis, 0);
Dave Barach525c9d02018-05-26 10:48:55 -0400420 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100421 pool_foreach (si, im->sw_interfaces)
422 {
Dave Barach525c9d02018-05-26 10:48:55 -0400423 int visible = vnet_swif_is_api_visible (si);
424 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100425 vec_add1 (sorted_sis, si[0]);
426 }
Ole Troand7231612018-06-07 10:17:57 +0200427 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 /* Sort by name. */
429 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
430 }
431
432 if (show_addresses)
433 {
434 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400435 {
Dave Barachba868bb2016-08-08 09:51:21 -0400436 ip4_main_t *im4 = &ip4_main;
437 ip6_main_t *im6 = &ip6_main;
438 ip_lookup_main_t *lm4 = &im4->lookup_main;
439 ip_lookup_main_t *lm6 = &im6->lookup_main;
440 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400441 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442
Dave Barachba868bb2016-08-08 09:51:21 -0400443 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
444 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
445 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Dave Barachba868bb2016-08-08 09:51:21 -0400447 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
448 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
449 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
John Lo4478d8e2018-01-12 17:15:25 -0500451 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
452 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
Dave Barachba868bb2016-08-08 09:51:21 -0400454 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
455 vlib_cli_output
456 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500457 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400458 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
459 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400460 else
John Lo4478d8e2018-01-12 17:15:25 -0500461 vlib_cli_output
462 (vm, "%U (%s):",
463 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
464 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Eyal Bari942402b2017-07-26 11:57:04 +0300466 /* Display any L2 info */
Neale Ranns47a3d992020-09-29 15:38:51 +0000467 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400468
John Lo4478d8e2018-01-12 17:15:25 -0500469 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400470 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500471 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
472 1 /* honor unnumbered */,
473 ({
474 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
Neale Rannsd6953332021-08-10 07:39:18 +0000475 if (fib4->hash.table_id)
476 vlib_cli_output (
477 vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address,
478 r4, ia->address_length, fib4->hash.table_id,
479 ip4_fib_index_from_table_id (fib4->hash.table_id));
John Lo4478d8e2018-01-12 17:15:25 -0500480 else
481 vlib_cli_output (vm, " L3 %U/%d",
482 format_ip4_address, r4, ia->address_length);
483 }));
484 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485
John Lo4478d8e2018-01-12 17:15:25 -0500486 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400487 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500488 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
489 1 /* honor unnumbered */,
490 ({
491 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
492 if (fib6->table_id)
493 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
494 format_ip6_address, r6, ia->address_length,
495 fib6->table_id,
496 ip6_fib_index_from_table_id (fib6->table_id));
497 else
498 vlib_cli_output (vm, " L3 %U/%d",
499 format_ip6_address, r6, ia->address_length);
500 }));
501 /* *INDENT-ON* */
Ole Troand7231612018-06-07 10:17:57 +0200502 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 }
Ole Troand7231612018-06-07 10:17:57 +0200504 else
505 {
506 vec_foreach (si, sorted_sis)
507 {
508 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
509 }
510 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Dave Barachba868bb2016-08-08 09:51:21 -0400512done:
Ole Troand7231612018-06-07 10:17:57 +0200513 vec_free (sorted_sis);
514 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515}
516
Dave Barachba868bb2016-08-08 09:51:21 -0400517/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400519 .path = "show interface",
Jon Loeliger9485d992019-11-08 15:05:23 -0600520 .short_help = "show interface [address|addr|features|feat|vtr] [<interface> [<interface> [..]]] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 .function = show_sw_interfaces,
Steven Luongc5fa2b82019-04-25 11:19:49 -0700522 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523};
Dave Barachba868bb2016-08-08 09:51:21 -0400524/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
526/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400527/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
529 .path = "interface",
530 .short_help = "Interface commands",
531};
Dave Barachba868bb2016-08-08 09:51:21 -0400532/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Dave Barachba868bb2016-08-08 09:51:21 -0400534/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
536 .path = "set interface",
537 .short_help = "Interface commands",
538};
Dave Barachba868bb2016-08-08 09:51:21 -0400539/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
541static clib_error_t *
542clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400543 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544{
Dave Barachba868bb2016-08-08 09:51:21 -0400545 vnet_main_t *vnm = vnet_get_main ();
546 vnet_interface_main_t *im = &vnm->interface_main;
547 vlib_simple_counter_main_t *sm;
548 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400549 int j, n_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551 n_counters = vec_len (im->combined_sw_if_counters);
552
553 for (j = 0; j < n_counters; j++)
554 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400555 im = &vnm->interface_main;
556 cm = im->combined_sw_if_counters + j;
557 vlib_clear_combined_counters (cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 }
559
560 n_counters = vec_len (im->sw_if_counters);
561
562 for (j = 0; j < n_counters; j++)
563 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400564 im = &vnm->interface_main;
565 sm = im->sw_if_counters + j;
566 vlib_clear_simple_counters (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567 }
568
569 return 0;
570}
571
Billy McFalle9bac692017-08-11 14:05:11 -0400572/*?
573 * Clear the statistics for all interfaces (statistics associated with the
574 * '<em>show interface</em>' command).
575 *
576 * @cliexpar
577 * Example of how to clear the statistics for all interfaces:
578 * @cliexcmd{clear interfaces}
579 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400580/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
582 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400583 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 .function = clear_interface_counters,
585};
Dave Barachba868bb2016-08-08 09:51:21 -0400586/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Chris Luke16bcf7d2016-09-01 14:31:46 -0400588/**
589 * Parse subinterface names.
590 *
Dave Barachba868bb2016-08-08 09:51:21 -0400591 * The following subinterface syntax is supported. The first two are for
592 * backwards compatability:
593 *
594 * <intf-name> <id>
595 * - a subinterface with the name <intf-name>.<id>. The subinterface
596 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
597 *
598 * <intf-name> <min_id>-<max_id>
599 * - a set of the above subinterfaces, repeating for each id
600 * in the range <min_id> to <max_id>
601 *
602 * In the following, exact-match semantics (i.e. the number of vlan tags on the
603 * packet must match the number of tags in the configuration) are used only if
604 * the keyword exact-match is present. Non-exact match is the default.
605 *
606 * <intf-name> <id> dot1q <outer_id> [exact-match]
607 * - a subinterface with the name <intf-name>.<id>. The subinterface
608 * is a single dot1q vlan with vlan id <outer_id>.
609 *
610 * <intf-name> <id> dot1q any [exact-match]
611 * - a subinterface with the name <intf-name>.<id>. The subinterface
612 * is a single dot1q vlan with any vlan id.
613 *
614 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
615 * - a subinterface with the name <intf-name>.<id>. The subinterface
616 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
617 * <inner_id>.
618 *
619 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
620 * - a subinterface with the name <intf-name>.<id>. The subinterface
621 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
622 *
623 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
624 *
625 * - a subinterface with the name <intf-name>.<id>. The subinterface
626 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
627 *
628 * For each of the above CLI, there is a duplicate that uses the keyword
629 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
630 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
631 * tagged packets the inner ethertype is always 0x8100. Also note that
632 * the dot1q and dot1ad naming spaces are independent, so it is legal to
633 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
634 *
635 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
636 * - a subinterface with the name <intf-name>.<id>. The subinterface
637 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
638 * id <inner_id>.
639 *
640 * <intf-name> <id> untagged
641 * - a subinterface with the name <intf-name>.<id>. The subinterface
642 * has no vlan tags. Only one can be specified per interface.
643 *
644 * <intf-name> <id> default
645 * - a subinterface with the name <intf-name>.<id>. This is associated
646 * with a packet that did not match any other configured subinterface
647 * on this interface. Only one can be specified per interface.
648 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
650static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400651parse_vlan_sub_interfaces (unformat_input_t * input,
652 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653{
Dave Barachba868bb2016-08-08 09:51:21 -0400654 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655 u32 inner_vlan, outer_vlan;
656
Dave Barachba868bb2016-08-08 09:51:21 -0400657 if (unformat (input, "any inner-dot1q any"))
658 {
659 template->sub.eth.flags.two_tags = 1;
660 template->sub.eth.flags.outer_vlan_id_any = 1;
661 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700662 }
Dave Barachba868bb2016-08-08 09:51:21 -0400663 else if (unformat (input, "any"))
664 {
665 template->sub.eth.flags.one_tag = 1;
666 template->sub.eth.flags.outer_vlan_id_any = 1;
667 }
668 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
669 {
670 template->sub.eth.flags.two_tags = 1;
671 template->sub.eth.flags.inner_vlan_id_any = 1;
672 template->sub.eth.outer_vlan_id = outer_vlan;
673 }
674 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
675 {
676 template->sub.eth.flags.two_tags = 1;
677 template->sub.eth.outer_vlan_id = outer_vlan;
678 template->sub.eth.inner_vlan_id = inner_vlan;
679 }
680 else if (unformat (input, "%d", &outer_vlan))
681 {
682 template->sub.eth.flags.one_tag = 1;
683 template->sub.eth.outer_vlan_id = outer_vlan;
684 }
685 else
686 {
687 error = clib_error_return (0, "expected dot1q config, got `%U'",
688 format_unformat_error, input);
689 goto done;
690 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Dave Barachba868bb2016-08-08 09:51:21 -0400692 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
693 {
694 if (unformat (input, "exact-match"))
695 {
696 template->sub.eth.flags.exact_match = 1;
697 }
698 }
699
700done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700701 return error;
702}
703
704static clib_error_t *
705create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400706 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707{
Dave Barachba868bb2016-08-08 09:51:21 -0400708 vnet_main_t *vnm = vnet_get_main ();
709 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400711 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700712 u32 id, id_min, id_max;
713 vnet_sw_interface_t template;
714
715 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400716 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717 {
718 error = clib_error_return (0, "unknown interface `%U'",
719 format_unformat_error, input);
720 goto done;
721 }
722
Dave Barachb7b92992018-10-17 10:38:51 -0400723 clib_memset (&template, 0, sizeof (template));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700724 template.sub.eth.raw_flags = 0;
725
Dave Barachba868bb2016-08-08 09:51:21 -0400726 if (unformat (input, "%d default", &id_min))
727 {
728 id_max = id_min;
729 template.sub.eth.flags.default_sub = 1;
730 }
731 else if (unformat (input, "%d untagged", &id_min))
732 {
733 id_max = id_min;
734 template.sub.eth.flags.no_tags = 1;
735 template.sub.eth.flags.exact_match = 1;
736 }
737 else if (unformat (input, "%d dot1q", &id_min))
738 {
739 /* parse dot1q config */
740 id_max = id_min;
741 error = parse_vlan_sub_interfaces (input, &template);
742 if (error)
743 goto done;
744 }
745 else if (unformat (input, "%d dot1ad", &id_min))
746 {
747 /* parse dot1ad config */
748 id_max = id_min;
749 template.sub.eth.flags.dot1ad = 1;
750 error = parse_vlan_sub_interfaces (input, &template);
751 if (error)
752 goto done;
753 }
754 else if (unformat (input, "%d-%d", &id_min, &id_max))
755 {
756 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400757 template.sub.eth.flags.exact_match = 1;
758 if (id_min > id_max)
759 goto id_error;
760 }
761 else if (unformat (input, "%d", &id_min))
762 {
763 id_max = id_min;
764 template.sub.eth.flags.one_tag = 1;
765 template.sub.eth.outer_vlan_id = id_min;
766 template.sub.eth.flags.exact_match = 1;
767 }
768 else
769 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 id_error:
771 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
772 format_unformat_error, input);
773 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400774 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775
776 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400777
Dave Barachba868bb2016-08-08 09:51:21 -0400778 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
779 {
780 error =
781 clib_error_return (0,
782 "not allowed as %v belong to a BondEthernet interface",
783 hi->name);
784 goto done;
785 }
John Lobcebbb92016-04-05 15:47:43 -0400786
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 for (id = id_min; id <= id_max; id++)
788 {
Dave Barachba868bb2016-08-08 09:51:21 -0400789 uword *p;
790 vnet_interface_main_t *im = &vnm->interface_main;
791 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
792 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793
794 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
795 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400796 {
797 if (CLIB_DEBUG > 0)
798 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
799 hi->sw_if_index, id);
800 continue;
801 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200804 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805 template.sup_sw_if_index = hi->sw_if_index;
806 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300807 if (id_min < id_max)
808 template.sub.eth.outer_vlan_id = id;
809
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400811 if (error)
812 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400813
Jon Loeligerb22e1f02019-12-19 09:03:52 -0600814 kp = clib_mem_alloc (sizeof (*kp));
815 *kp = sup_and_sub_key;
816
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
818 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400819 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
820 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 }
822
Dave Barachba868bb2016-08-08 09:51:21 -0400823done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700824 return error;
825}
826
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700827/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400828 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
829 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
830 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
831 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
832 * but this is not recommended.
833 *
834 * This command has several variations:
835 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
836 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
837 *
838 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
839 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
840 * match any other subinterfaces should be sent to this subinterface.
841 *
842 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
843 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
844 * to this subinterface.
845 *
846 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
847 * subinterfaces to handle a range of VLAN IDs.
848 *
849 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700850 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
Billy McFalle9bac692017-08-11 14:05:11 -0400851 * VLAN ID different from the '<em>subId</em>'.
852 *
853 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
854 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700855 * the inner VLAN ID.
Billy McFalle9bac692017-08-11 14:05:11 -0400856 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700857 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
Billy McFalle9bac692017-08-11 14:05:11 -0400858 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
859 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
860 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
861 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
862 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
863 * entered, then the default behavior is exact-match.
864 *
865 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700866 *
867 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400868 * @parblock
869 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
870 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700871 *
Billy McFalle9bac692017-08-11 14:05:11 -0400872 * The previous example is shorthand and is equivalent to:
873 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700874 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700875 *
Billy McFalle9bac692017-08-11 14:05:11 -0400876 * Example of how to create a subinterface number that is different from the VLAN ID:
877 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700878 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700879 *
Billy McFalle9bac692017-08-11 14:05:11 -0400880 * Examples of how to create q-in-q and q-in-any subinterfaces:
881 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
882 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700883 *
Billy McFalle9bac692017-08-11 14:05:11 -0400884 * Examples of how to create dot1ad interfaces:
885 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
886 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700887 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700888 *
Billy McFalle9bac692017-08-11 14:05:11 -0400889 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
890 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
891 * is non-exact match:
892 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700893 *
Billy McFalle9bac692017-08-11 14:05:11 -0400894 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
895 * is specified and only one VLAN is configured, but packet contains two VLANs:
896 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700897 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700898 *
Billy McFalle9bac692017-08-11 14:05:11 -0400899 * Example of how to created a subinterface to process untagged packets:
900 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
901 *
902 * Example of how to created a subinterface to process any packet with a VLAN ID that
903 * does not match any other subinterface:
904 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
905 *
906 * When subinterfaces are created, they are in the down state. Example of how to
907 * enable a newly created subinterface:
908 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
909 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700910 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400911/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700913 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400914 .short_help = "create sub-interfaces <interface> "
915 "{<subId> [default|untagged]} | "
916 "{<subId>-<subId>} | "
917 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918 .function = create_sub_interfaces,
919};
Dave Barachba868bb2016-08-08 09:51:21 -0400920/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921
922static clib_error_t *
923set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400924 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925{
Dave Barachba868bb2016-08-08 09:51:21 -0400926 vnet_main_t *vnm = vnet_get_main ();
927 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 u32 sw_if_index, flags;
929
930 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400931 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 {
933 error = clib_error_return (0, "unknown interface `%U'",
934 format_unformat_error, input);
935 goto done;
936 }
937
Dave Barachba868bb2016-08-08 09:51:21 -0400938 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 {
940 error = clib_error_return (0, "unknown flags `%U'",
941 format_unformat_error, input);
942 goto done;
943 }
944
945 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
946 if (error)
947 goto done;
948
Dave Barachba868bb2016-08-08 09:51:21 -0400949done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 return error;
951}
952
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700953/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400954 * This command is used to change the admin state (up/down) of an interface.
955 *
956 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
957 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
958 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
959 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700960 *
961 * @cliexpar
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +0200962 * Example of how to configure the admin state of an interface to
963 '<em>up</em>':
Billy McFalle9bac692017-08-11 14:05:11 -0400964 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +0200965 * Example of how to configure the admin state of an interface to
966 '<em>down</em>':
Billy McFalle9bac692017-08-11 14:05:11 -0400967 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700968 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400969/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970VLIB_CLI_COMMAND (set_state_command, static) = {
971 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400972 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 .function = set_state,
974};
Dave Barachba868bb2016-08-08 09:51:21 -0400975/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700976
977static clib_error_t *
978set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400979 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980{
Dave Barachba868bb2016-08-08 09:51:21 -0400981 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700982 u32 unnumbered_sw_if_index = ~0;
983 u32 inherit_from_sw_if_index = ~0;
984 int enable = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700986 if (unformat (input, "%U use %U",
987 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
988 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700989 enable = 1;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700990 else if (unformat (input, "del %U",
991 unformat_vnet_sw_interface, vnm,
992 &unnumbered_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700993 enable = 0;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700994 else
995 return clib_error_return (0, "parse error '%U'",
996 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700998 if (~0 == unnumbered_sw_if_index)
999 return clib_error_return (0, "Specify the unnumbered interface");
1000 if (enable && ~0 == inherit_from_sw_if_index)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001001 return clib_error_return (0, "When enabling unnumbered specify the"
Neale Ranns2ae2bc52018-03-16 03:22:39 -07001002 " IP enabled interface that it uses");
Neale Ranns898273f2017-03-18 02:57:38 -07001003
Stanislav Zaikin328b5da2021-07-15 16:27:29 +02001004 int rv = vnet_sw_interface_update_unnumbered (
1005 unnumbered_sw_if_index, inherit_from_sw_if_index, enable);
1006
1007 switch (rv)
1008 {
1009 case 0:
1010 break;
1011
1012 case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
1013 return clib_error_return (
1014 0,
1015 "When enabling unnumbered both interfaces must be in the same tables");
1016
1017 default:
1018 return clib_error_return (
1019 0, "vnet_sw_interface_update_unnumbered returned %d", rv);
1020 }
Neale Ranns898273f2017-03-18 02:57:38 -07001021
Neale Ranns2ae2bc52018-03-16 03:22:39 -07001022 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023}
1024
Dave Barachba868bb2016-08-08 09:51:21 -04001025/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
1027 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -04001028 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 .function = set_unnumbered,
1030};
Dave Barachba868bb2016-08-08 09:51:21 -04001031/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032
1033
1034
1035static clib_error_t *
1036set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001037 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001038{
Dave Barachba868bb2016-08-08 09:51:21 -04001039 vnet_main_t *vnm = vnet_get_main ();
1040 vnet_interface_main_t *im = &vnm->interface_main;
1041 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042 u32 hw_if_index, hw_class_index;
1043
1044 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001045 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 {
1047 error = clib_error_return (0, "unknown hardware interface `%U'",
1048 format_unformat_error, input);
1049 goto done;
1050 }
1051
Dave Barachba868bb2016-08-08 09:51:21 -04001052 if (!unformat_user (input, unformat_hash_string,
1053 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054 {
1055 error = clib_error_return (0, "unknown hardware class `%U'",
1056 format_unformat_error, input);
1057 goto done;
1058 }
1059
1060 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1061 if (error)
1062 goto done;
1063
Dave Barachba868bb2016-08-08 09:51:21 -04001064done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065 return error;
1066}
1067
Dave Barachba868bb2016-08-08 09:51:21 -04001068/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1070 .path = "set interface hw-class",
1071 .short_help = "Set interface hardware class",
1072 .function = set_hw_class,
1073};
Dave Barachba868bb2016-08-08 09:51:21 -04001074/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075
Dave Barachba868bb2016-08-08 09:51:21 -04001076static clib_error_t *
1077vnet_interface_cli_init (vlib_main_t * vm)
1078{
1079 return 0;
1080}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
1082VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1083
Dave Barachba868bb2016-08-08 09:51:21 -04001084static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001086 unformat_input_t * input,
1087 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088{
1089 u32 hw_if_index;
1090 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001091 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 int rv;
1093
Dave Barachba868bb2016-08-08 09:51:21 -04001094 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001096 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097
Dave Barachba868bb2016-08-08 09:51:21 -04001098 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 return clib_error_return (0, "new dev instance missing");
1100
1101 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1102
1103 switch (rv)
1104 {
1105 case 0:
1106 break;
1107
1108 default:
1109 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001110 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111
1112 }
1113
1114 return 0;
1115}
1116
1117
Dave Barachba868bb2016-08-08 09:51:21 -04001118/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1120 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001121 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122 .function = renumber_interface_command_fn,
1123};
Dave Barachba868bb2016-08-08 09:51:21 -04001124/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125
Damjan Marion8358ff92016-04-15 14:26:00 +02001126static clib_error_t *
1127promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001128 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001129{
Dave Barachba868bb2016-08-08 09:51:21 -04001130 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001131 u32 hw_if_index;
1132 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001133 ethernet_main_t *em = &ethernet_main;
1134 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001135
1136 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001137 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001138 ;
1139 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001140 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001141 flags = 0;
1142 else
1143 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001144 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001145
1146 eif = ethernet_get_interface (em, hw_if_index);
1147 if (!eif)
1148 return clib_error_return (0, "not supported");
1149
1150 ethernet_set_flags (vnm, hw_if_index, flags);
1151 return 0;
1152}
1153
Dave Barachba868bb2016-08-08 09:51:21 -04001154/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001155VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1156 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001157 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001158 .function = promiscuous_cmd,
1159};
Dave Barachba868bb2016-08-08 09:51:21 -04001160/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001161
1162static clib_error_t *
1163mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1164{
Dave Barachba868bb2016-08-08 09:51:21 -04001165 vnet_main_t *vnm = vnet_get_main ();
Ole Troand7231612018-06-07 10:17:57 +02001166 u32 hw_if_index, sw_if_index, mtu;
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001167 ethernet_main_t *em = &ethernet_main;
Ole Troand7231612018-06-07 10:17:57 +02001168 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
Damjan Marion81bb6fc2022-01-16 22:47:55 +01001169 clib_error_t *err;
Damjan Marion8358ff92016-04-15 14:26:00 +02001170
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001171 if (unformat (input, "%d %U", &mtu,
1172 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001173 {
Ole Troand7231612018-06-07 10:17:57 +02001174 /*
1175 * Change physical MTU on interface. Only supported for Ethernet
1176 * interfaces
1177 */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001178 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1179
1180 if (!eif)
1181 return clib_error_return (0, "not supported");
1182
Damjan Marion81bb6fc2022-01-16 22:47:55 +01001183 err = vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
1184 if (err)
1185 return err;
Ole Troand7231612018-06-07 10:17:57 +02001186 goto done;
Damjan Marion8358ff92016-04-15 14:26:00 +02001187 }
Ole Troand7231612018-06-07 10:17:57 +02001188 else if (unformat (input, "packet %d %U", &mtu,
1189 unformat_vnet_sw_interface, vnm, &sw_if_index))
1190 /* Set default packet MTU (including L3 header */
1191 mtus[VNET_MTU_L3] = mtu;
1192 else if (unformat (input, "ip4 %d %U", &mtu,
1193 unformat_vnet_sw_interface, vnm, &sw_if_index))
1194 mtus[VNET_MTU_IP4] = mtu;
1195 else if (unformat (input, "ip6 %d %U", &mtu,
1196 unformat_vnet_sw_interface, vnm, &sw_if_index))
1197 mtus[VNET_MTU_IP6] = mtu;
1198 else if (unformat (input, "mpls %d %U", &mtu,
1199 unformat_vnet_sw_interface, vnm, &sw_if_index))
1200 mtus[VNET_MTU_MPLS] = mtu;
Damjan Marion8358ff92016-04-15 14:26:00 +02001201 else
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001202 return clib_error_return (0, "unknown input `%U'",
1203 format_unformat_error, input);
Ole Troand7231612018-06-07 10:17:57 +02001204
1205 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1206
1207done:
Damjan Marion8358ff92016-04-15 14:26:00 +02001208 return 0;
1209}
1210
Dave Barachba868bb2016-08-08 09:51:21 -04001211/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001212VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1213 .path = "set interface mtu",
Ole Troand7231612018-06-07 10:17:57 +02001214 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001215 .function = mtu_cmd,
1216};
Dave Barachba868bb2016-08-08 09:51:21 -04001217/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001218
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001219static clib_error_t *
Matthew Smithe0792fd2019-07-12 11:48:24 -05001220show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1221 vlib_cli_command_t * cmd)
1222{
1223 vnet_main_t *vnm = vnet_get_main ();
1224 vnet_interface_main_t *im = &vnm->interface_main;
1225 ethernet_main_t *em = &ethernet_main;
1226 u32 sw_if_index = ~0;
1227 vnet_sw_interface_t *si, *sorted_sis = 0;
1228
1229 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1230 {
1231 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1232 vec_add1 (sorted_sis, si[0]);
1233 }
1234
1235 /* if an interface name was not passed, get all interfaces */
1236 if (vec_len (sorted_sis) == 0)
1237 {
1238 sorted_sis =
1239 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +02001240 vec_set_len (sorted_sis, 0);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001241 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001242 pool_foreach (si, im->sw_interfaces)
1243 {
Matthew Smithe0792fd2019-07-12 11:48:24 -05001244 int visible = vnet_swif_is_api_visible (si);
1245 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +01001246 vec_add1 (sorted_sis, si[0]);
1247 }
Matthew Smithe0792fd2019-07-12 11:48:24 -05001248 /* *INDENT-ON* */
1249 /* Sort by name. */
1250 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1251 }
1252
1253 vec_foreach (si, sorted_sis)
1254 {
1255 vnet_sw_interface_t *sup_si;
1256 ethernet_interface_t *ei;
1257
1258 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1259 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1260
1261 vlib_cli_output (vm, "%U (%s):",
1262 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1263 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1264 "up" : "dn");
1265
1266 if (ei && ei->secondary_addrs)
1267 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001268 ethernet_interface_address_t *sec_addr;
Matthew Smithe0792fd2019-07-12 11:48:24 -05001269
1270 vec_foreach (sec_addr, ei->secondary_addrs)
1271 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001272 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001273 }
1274 }
1275 }
1276
1277 vec_free (sorted_sis);
1278 return 0;
1279}
1280
1281/*?
1282 * This command is used to display interface secondary mac addresses.
1283 *
1284 * @cliexpar
1285 * Example of how to display interface secondary mac addresses:
1286 * @cliexstart{show interface secondary-mac-address}
1287 * @cliexend
1288?*/
1289/* *INDENT-OFF* */
1290VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1291 .path = "show interface secondary-mac-address",
1292 .short_help = "show interface secondary-mac-address [<interface>]",
1293 .function = show_interface_sec_mac_addr_fn,
1294};
1295/* *INDENT-ON* */
1296
1297static clib_error_t *
1298interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1299 vlib_cli_command_t * cmd)
1300{
1301 vnet_main_t *vnm = vnet_get_main ();
1302 vnet_sw_interface_t *si = NULL;
1303 clib_error_t *error = 0;
1304 u32 sw_if_index = ~0;
1305 u8 mac[6] = { 0 };
1306 u8 is_add, is_del;
1307
1308 is_add = is_del = 0;
1309
1310 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1311 {
1312 error = clib_error_return (0, "unknown interface `%U'",
1313 format_unformat_error, input);
1314 goto done;
1315 }
1316 if (!unformat_user (input, unformat_ethernet_address, mac))
1317 {
1318 error = clib_error_return (0, "expected mac address `%U'",
1319 format_unformat_error, input);
1320 goto done;
1321 }
1322
1323 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1324 {
1325 if (unformat (input, "add"))
1326 is_add = 1;
1327 else if (unformat (input, "del"))
1328 is_del = 1;
1329 else
1330 break;
1331 }
1332
1333 if (is_add == is_del)
1334 {
1335 error = clib_error_return (0, "must choose one of add or del");
1336 goto done;
1337 }
1338
1339 si = vnet_get_sw_interface (vnm, sw_if_index);
1340 error =
1341 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1342
1343done:
1344 return error;
1345}
1346
1347/*?
1348 * The '<em>set interface secondary-mac-address </em>' command allows adding
1349 * or deleting extra MAC addresses on a given interface without changing the
1350 * default MAC address. This could allow packets sent to these MAC addresses
1351 * to be received without setting the interface to promiscuous mode.
1352 * Not all interfaces support this operation. The ones that do are mostly
1353 * hardware NICs, though virtio does also.
1354 *
1355 * @cliexpar
1356 * @parblock
1357 * Example of how to add a secondary MAC Address on an interface:
1358 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1359 * Example of how to delete a secondary MAC address from an interface:
1360 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1361 * @endparblock
1362?*/
1363/* *INDENT-OFF* */
1364VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1365 .path = "set interface secondary-mac-address",
1366 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1367 .function = interface_add_del_mac_address,
1368};
1369/* *INDENT-ON* */
1370
1371static clib_error_t *
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001372set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1373 vlib_cli_command_t * cmd)
1374{
1375 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001376 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001377 clib_error_t *error = 0;
1378 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001379 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001380
1381 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1382 {
1383 error = clib_error_return (0, "unknown interface `%U'",
1384 format_unformat_error, input);
1385 goto done;
1386 }
John Lo62fcc0a2017-10-31 14:31:10 -04001387 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001388 {
1389 error = clib_error_return (0, "expected mac address `%U'",
1390 format_unformat_error, input);
1391 goto done;
1392 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001393 si = vnet_get_sw_interface (vnm, sw_if_index);
1394 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001395done:
1396 return error;
1397}
1398
1399/*?
1400 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1401 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1402 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1403 *
1404 * @cliexpar
1405 * @parblock
1406 * Example of how to change MAC Address of interface:
1407 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1408 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1409 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1410 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1411 * @endparblock
1412?*/
1413/* *INDENT-OFF* */
1414VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1415 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001416 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001417 .function = set_interface_mac_address,
1418};
1419/* *INDENT-ON* */
1420
Dave Barach7be864a2016-11-28 11:41:35 -05001421static clib_error_t *
1422set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1423{
1424 vnet_main_t *vnm = vnet_get_main ();
1425 u32 sw_if_index = ~0;
1426 u8 *tag = 0;
1427
1428 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1429 vnm, &sw_if_index, &tag))
1430 return clib_error_return (0, "unknown input `%U'",
1431 format_unformat_error, input);
1432
1433 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1434
1435 return 0;
1436}
1437
1438/* *INDENT-OFF* */
1439VLIB_CLI_COMMAND (set_tag_command, static) = {
1440 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001441 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001442 .function = set_tag,
1443};
1444/* *INDENT-ON* */
1445
1446static clib_error_t *
1447clear_tag (vlib_main_t * vm, unformat_input_t * input,
1448 vlib_cli_command_t * cmd)
1449{
1450 vnet_main_t *vnm = vnet_get_main ();
1451 u32 sw_if_index = ~0;
1452
1453 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1454 return clib_error_return (0, "unknown input `%U'",
1455 format_unformat_error, input);
1456
1457 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1458
1459 return 0;
1460}
1461
1462/* *INDENT-OFF* */
1463VLIB_CLI_COMMAND (clear_tag_command, static) = {
1464 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001465 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001466 .function = clear_tag,
1467};
1468/* *INDENT-ON* */
1469
Damjan Marion44036902017-04-28 12:29:15 +02001470static clib_error_t *
Neale Ranns1855b8e2018-07-11 10:31:26 -07001471set_ip_directed_broadcast (vlib_main_t * vm,
1472 unformat_input_t * input, vlib_cli_command_t * cmd)
1473{
1474 vnet_main_t *vnm = vnet_get_main ();
1475 u32 sw_if_index = ~0;
1476 u8 enable = 0;
1477
1478 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1479 else if (unformat (input, "enable"))
1480 enable = 1;
1481 else if (unformat (input, "disable"))
1482 enable = 0;
1483 else
1484 return clib_error_return (0, "unknown input: `%U'",
1485 format_unformat_error, input);
1486
1487 if (~0 == sw_if_index)
1488 return clib_error_return (0, "specify an interface: `%U'",
1489 format_unformat_error, input);
1490
1491 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1492
1493 return 0;
1494}
1495
1496/*?
1497 * This command is used to enable/disable IP directed broadcast
1498 * If directed broadcast is enabled a packet sent to the interface's
1499 * subnet broadcast address will be sent L2 broadcast on the interface,
1500 * otherwise it is dropped.
1501 ?*/
1502/* *INDENT-OFF* */
1503VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1504 .path = "set interface ip directed-broadcast",
1505 .short_help = "set interface enable <interface> <enable|disable>",
1506 .function = set_ip_directed_broadcast,
1507};
1508/* *INDENT-ON* */
1509
Stevenad8015b2017-10-29 22:10:46 -07001510clib_error_t *
1511set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1512 u8 queue_id_valid, u32 queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001513 vnet_hw_if_rx_mode mode)
Stevenad8015b2017-10-29 22:10:46 -07001514{
1515 clib_error_t *error = 0;
1516 vnet_hw_interface_t *hw;
Damjan Marion94100532020-11-06 23:25:57 +01001517 u32 *queue_indices = 0;
Stevenad8015b2017-10-29 22:10:46 -07001518
1519 hw = vnet_get_hw_interface (vnm, hw_if_index);
1520
Damjan Marion94100532020-11-06 23:25:57 +01001521 if (queue_id_valid)
1522 {
1523 u32 queue_index;
1524 queue_index =
1525 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1526 if (queue_index == ~0)
1527 return clib_error_return (0, "unknown queue %u on interface %s",
1528 queue_id, hw->name);
1529 vec_add1 (queue_indices, queue_index);
Stevenad8015b2017-10-29 22:10:46 -07001530 }
1531 else
Damjan Marion94100532020-11-06 23:25:57 +01001532 queue_indices = hw->rx_queue_indices;
Stevenad8015b2017-10-29 22:10:46 -07001533
Damjan Marion94100532020-11-06 23:25:57 +01001534 for (int i = 0; i < vec_len (queue_indices); i++)
1535 {
1536 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1537 if (rv)
Wayne Morrison389aae52022-08-05 09:47:24 -04001538 {
1539 error = clib_error_return (
1540 0, "unable to set rx-mode on interface %v queue-id %u.\n",
1541 hw->name, queue_id);
1542 goto done;
1543 }
Damjan Marion94100532020-11-06 23:25:57 +01001544 }
1545
1546done:
1547 if (queue_indices != hw->rx_queue_indices)
1548 vec_free (queue_indices);
1549 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1550 return error;
Stevenad8015b2017-10-29 22:10:46 -07001551}
1552
Stevene3a395c2017-05-09 16:19:50 -07001553static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001554set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1555 vlib_cli_command_t * cmd)
1556{
1557 clib_error_t *error = 0;
1558 unformat_input_t _line_input, *line_input = &_line_input;
1559 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001560 u32 hw_if_index = (u32) ~ 0;
1561 u32 queue_id = (u32) ~ 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001562 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001563 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001564
Damjan Marion44036902017-04-28 12:29:15 +02001565 if (!unformat_user (input, unformat_line_input, line_input))
1566 return 0;
1567
1568 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1569 {
1570 if (unformat
1571 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1572 ;
1573 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001574 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001575 else if (unformat (line_input, "polling"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001576 mode = VNET_HW_IF_RX_MODE_POLLING;
Damjan Marion44036902017-04-28 12:29:15 +02001577 else if (unformat (line_input, "interrupt"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001578 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
Damjan Marion44036902017-04-28 12:29:15 +02001579 else if (unformat (line_input, "adaptive"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001580 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
Damjan Marion44036902017-04-28 12:29:15 +02001581 else
1582 {
1583 error = clib_error_return (0, "parse error: '%U'",
1584 format_unformat_error, line_input);
1585 unformat_free (line_input);
1586 return error;
1587 }
1588 }
1589
1590 unformat_free (line_input);
1591
1592 if (hw_if_index == (u32) ~ 0)
1593 return clib_error_return (0, "please specify valid interface name");
1594
Damjan Marioneabd4242020-10-07 20:59:07 +02001595 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Damjan Marion44036902017-04-28 12:29:15 +02001596 return clib_error_return (0, "please specify valid rx-mode");
1597
Stevenad8015b2017-10-29 22:10:46 -07001598 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1599 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001600
Stevene3a395c2017-05-09 16:19:50 -07001601 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001602}
1603
1604/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001605 * This command is used to assign the RX packet processing mode (polling,
1606 * interrupt, adaptive) of the a given interface, and optionally a
1607 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1608 * is applied to all queues of the interface. Not all interfaces support
1609 * all modes. To display the current rx-mode use the command
1610 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001611 *
1612 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001613 * Example of how to assign rx-mode to all queues on an interface:
1614 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1615 * Example of how to assign rx-mode to one queue of an interface:
1616 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1617 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001618 * @cliexstart{show interface rx-placement}
1619 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001620 * node dpdk-input:
1621 * GigabitEthernet7/0/0 queue 0 (polling)
1622 * node vhost-user-input:
1623 * VirtualEthernet0/0/12 queue 0 (interrupt)
1624 * VirtualEthernet0/0/12 queue 2 (polling)
1625 * VirtualEthernet0/0/13 queue 0 (polling)
1626 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001627 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001628 * node dpdk-input:
1629 * GigabitEthernet7/0/1 queue 0 (polling)
1630 * node vhost-user-input:
1631 * VirtualEthernet0/0/12 queue 1 (polling)
1632 * VirtualEthernet0/0/12 queue 3 (polling)
1633 * VirtualEthernet0/0/13 queue 1 (polling)
1634 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001635 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001636?*/
1637/* *INDENT-OFF* */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001638VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
Damjan Marion44036902017-04-28 12:29:15 +02001639 .path = "set interface rx-mode",
1640 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1641 .function = set_interface_rx_mode,
1642};
1643/* *INDENT-ON* */
1644
1645static clib_error_t *
1646show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1647 vlib_cli_command_t * cmd)
1648{
1649 u8 *s = 0;
1650 vnet_main_t *vnm = vnet_get_main ();
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001651 vnet_hw_if_rx_queue_t **all_queues = 0;
1652 vnet_hw_if_rx_queue_t **qptr;
1653 vnet_hw_if_rx_queue_t *q;
Nathan Skrzypczak455779f2021-02-15 14:48:33 +01001654 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001655 vec_add1 (all_queues, q);
1656 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1657 u32 prev_node = ~0;
Damjan Marion44036902017-04-28 12:29:15 +02001658
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001659 vec_foreach (qptr, all_queues)
1660 {
1661 u32 current_thread = qptr[0]->thread_index;
1662 u32 hw_if_index = qptr[0]->hw_if_index;
1663 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1664 u32 current_node = hw_if->input_node_index;
1665 if (current_node != prev_node)
1666 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1667 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1668 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1669 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1670 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1671 current_thread != qptr[1]->thread_index)
1672 {
1673 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1674 vlib_worker_threads[current_thread].name, s);
1675 vec_reset_length (s);
1676 }
1677 prev_node = current_node;
1678 }
Damjan Marion44036902017-04-28 12:29:15 +02001679 vec_free (s);
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001680 vec_free (all_queues);
Damjan Marion44036902017-04-28 12:29:15 +02001681 return 0;
1682}
1683
Billy McFalle9bac692017-08-11 14:05:11 -04001684/*?
1685 * This command is used to display the interface and queue worker
1686 * thread placement.
1687 *
1688 * @cliexpar
1689 * Example of how to display the interface placement:
1690 * @cliexstart{show interface rx-placement}
1691 * Thread 1 (vpp_wk_0):
1692 * node dpdk-input:
1693 * GigabitEthernet7/0/0 queue 0 (polling)
1694 * node vhost-user-input:
1695 * VirtualEthernet0/0/12 queue 0 (polling)
1696 * VirtualEthernet0/0/12 queue 2 (polling)
1697 * VirtualEthernet0/0/13 queue 0 (polling)
1698 * VirtualEthernet0/0/13 queue 2 (polling)
1699 * Thread 2 (vpp_wk_1):
1700 * node dpdk-input:
1701 * GigabitEthernet7/0/1 queue 0 (polling)
1702 * node vhost-user-input:
1703 * VirtualEthernet0/0/12 queue 1 (polling)
1704 * VirtualEthernet0/0/12 queue 3 (polling)
1705 * VirtualEthernet0/0/13 queue 1 (polling)
1706 * VirtualEthernet0/0/13 queue 3 (polling)
1707 * @cliexend
1708?*/
Damjan Marion44036902017-04-28 12:29:15 +02001709/* *INDENT-OFF* */
1710VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1711 .path = "show interface rx-placement",
1712 .short_help = "show interface rx-placement",
1713 .function = show_interface_rx_placement_fn,
1714};
1715/* *INDENT-ON* */
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001716clib_error_t *
1717set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1718 u32 thread_index, u8 is_main)
Damjan Marion44036902017-04-28 12:29:15 +02001719{
Damjan Marion44036902017-04-28 12:29:15 +02001720 vnet_main_t *vnm = vnet_get_main ();
1721 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion94100532020-11-06 23:25:57 +01001722 vnet_hw_interface_t *hw;
1723 u32 queue_index;
Damjan Marion44036902017-04-28 12:29:15 +02001724
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001725 if (is_main)
1726 thread_index = 0;
1727 else
1728 thread_index += vdm->first_worker_thread_index;
Damjan Marion44036902017-04-28 12:29:15 +02001729
1730 if (thread_index > vdm->last_worker_thread_index)
1731 return clib_error_return (0,
1732 "please specify valid worker thread or main");
1733
Damjan Marion94100532020-11-06 23:25:57 +01001734 hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001735
Damjan Marion94100532020-11-06 23:25:57 +01001736 queue_index =
1737 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1738 if (queue_index == ~0)
1739 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1740 hw->name);
1741 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1742 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1743 return 0;
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001744}
1745
1746static clib_error_t *
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001747set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1748 vlib_cli_command_t *cmd)
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001749{
1750 clib_error_t *error = 0;
1751 unformat_input_t _line_input, *line_input = &_line_input;
1752 vnet_main_t *vnm = vnet_get_main ();
1753 u32 hw_if_index = (u32) ~ 0;
1754 u32 queue_id = (u32) 0;
1755 u32 thread_index = (u32) ~ 0;
1756 u8 is_main = 0;
1757
1758 if (!unformat_user (input, unformat_line_input, line_input))
1759 return 0;
1760
1761 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1762 {
1763 if (unformat
1764 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1765 ;
1766 else if (unformat (line_input, "queue %d", &queue_id))
1767 ;
1768 else if (unformat (line_input, "main", &thread_index))
1769 is_main = 1;
1770 else if (unformat (line_input, "worker %d", &thread_index))
1771 ;
1772 else
1773 {
1774 error = clib_error_return (0, "parse error: '%U'",
1775 format_unformat_error, line_input);
1776 unformat_free (line_input);
1777 return error;
1778 }
1779 }
1780
1781 unformat_free (line_input);
1782
1783 if (hw_if_index == (u32) ~ 0)
1784 return clib_error_return (0, "please specify valid interface name");
1785
1786 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1787 is_main);
1788
1789 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001790}
1791
1792/*?
1793 * This command is used to assign a given interface, and optionally a
1794 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001795 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1796 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001797 *
1798 * @cliexpar
1799 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001800 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001801 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001802 * node dpdk-input:
1803 * GigabitEthernet7/0/0 queue 0 (polling)
1804 * node vhost-user-input:
1805 * VirtualEthernet0/0/12 queue 0 (polling)
1806 * VirtualEthernet0/0/12 queue 2 (polling)
1807 * VirtualEthernet0/0/13 queue 0 (polling)
1808 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001809 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001810 * node dpdk-input:
1811 * GigabitEthernet7/0/1 queue 0 (polling)
1812 * node vhost-user-input:
1813 * VirtualEthernet0/0/12 queue 1 (polling)
1814 * VirtualEthernet0/0/12 queue 3 (polling)
1815 * VirtualEthernet0/0/13 queue 1 (polling)
1816 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001817 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001818 * Example of how to assign a interface and queue to a worker thread:
1819 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1820 * Example of how to display the interface placement:
1821 * @cliexstart{show interface rx-placement}
1822 * Thread 1 (vpp_wk_0):
1823 * node dpdk-input:
1824 * GigabitEthernet7/0/0 queue 0 (polling)
1825 * node vhost-user-input:
1826 * VirtualEthernet0/0/12 queue 0 (polling)
1827 * VirtualEthernet0/0/12 queue 1 (polling)
1828 * VirtualEthernet0/0/12 queue 2 (polling)
1829 * VirtualEthernet0/0/13 queue 0 (polling)
1830 * VirtualEthernet0/0/13 queue 2 (polling)
1831 * Thread 2 (vpp_wk_1):
1832 * node dpdk-input:
1833 * GigabitEthernet7/0/1 queue 0 (polling)
1834 * node vhost-user-input:
1835 * VirtualEthernet0/0/12 queue 3 (polling)
1836 * VirtualEthernet0/0/13 queue 1 (polling)
1837 * VirtualEthernet0/0/13 queue 3 (polling)
1838 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001839?*/
1840/* *INDENT-OFF* */
1841VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1842 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001843 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001844 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001845 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001846 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001847};
Damjan Marion44036902017-04-28 12:29:15 +02001848/* *INDENT-ON* */
Dave Barach5ecd5a52019-02-25 15:27:28 -05001849
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001850int
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001851set_hw_interface_tx_queue (u32 hw_if_index, u32 queue_id, uword *bitmap)
1852{
1853 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001854 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001855 vnet_hw_if_tx_queue_t *txq;
1856 u32 queue_index;
1857 u32 thread_index;
1858
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001859 /* highest set bit in bitmap should not exceed last worker thread index */
Steven Luong8e3f1092021-06-17 08:22:50 -07001860 thread_index = clib_bitmap_last_set (bitmap);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001861 if ((thread_index != ~0) && (thread_index >= vtm->n_vlib_mains))
1862 return VNET_API_ERROR_INVALID_VALUE;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001863
1864 queue_index =
1865 vnet_hw_if_get_tx_queue_index_by_id (vnm, hw_if_index, queue_id);
1866 if (queue_index == ~0)
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001867 return VNET_API_ERROR_INVALID_QUEUE;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001868
1869 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
1870
1871 // free the existing bitmap
1872 if (clib_bitmap_count_set_bits (txq->threads))
1873 {
1874 txq->shared_queue = 0;
1875 clib_bitmap_free (txq->threads);
1876 }
1877
1878 clib_bitmap_foreach (thread_index, bitmap)
1879 vnet_hw_if_tx_queue_assign_thread (vnm, queue_index, thread_index);
1880
1881 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1882 return 0;
1883}
1884
1885static clib_error_t *
1886set_interface_tx_queue (vlib_main_t *vm, unformat_input_t *input,
1887 vlib_cli_command_t *cmd)
1888{
1889 clib_error_t *error = 0;
1890 unformat_input_t _line_input, *line_input = &_line_input;
1891 vnet_main_t *vnm = vnet_get_main ();
1892 u32 hw_if_index = (u32) ~0;
1893 u32 queue_id = (u32) 0;
1894 uword *bitmap = 0;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001895 int rv = 0;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001896
1897 if (!unformat_user (input, unformat_line_input, line_input))
1898 return 0;
1899
1900 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1901 {
1902 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
1903 &hw_if_index))
1904 ;
1905 else if (unformat (line_input, "queue %d", &queue_id))
1906 ;
1907 else if (unformat (line_input, "threads %U", unformat_bitmap_list,
1908 &bitmap))
1909 ;
1910 else
1911 {
1912 error = clib_error_return (0, "parse error: '%U'",
1913 format_unformat_error, line_input);
1914 unformat_free (line_input);
1915 return error;
1916 }
1917 }
1918
1919 unformat_free (line_input);
1920
1921 if (hw_if_index == (u32) ~0)
1922 {
1923 error = clib_error_return (0, "please specify valid interface name");
1924 goto error;
1925 }
1926
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001927 rv = set_hw_interface_tx_queue (hw_if_index, queue_id, bitmap);
1928
1929 switch (rv)
1930 {
1931 case VNET_API_ERROR_INVALID_VALUE:
1932 error = clib_error_return (
1933 0, "please specify valid thread(s) - last thread index %u",
1934 clib_bitmap_last_set (bitmap));
1935 break;
1936 case VNET_API_ERROR_INVALID_QUEUE:
1937 error = clib_error_return (
1938 0, "unknown queue %u on interface %s", queue_id,
1939 vnet_get_hw_interface (vnet_get_main (), hw_if_index)->name);
1940 break;
1941 default:
1942 break;
1943 }
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001944
1945error:
1946 clib_bitmap_free (bitmap);
1947 return (error);
1948}
1949
1950VLIB_CLI_COMMAND (cmd_set_if_tx_queue, static) = {
1951 .path = "set interface tx-queue",
1952 .short_help = "set interface tx-queue <interface> queue <n> "
Steven Luong8e3f1092021-06-17 08:22:50 -07001953 "[threads <list>]",
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001954 .function = set_interface_tx_queue,
1955 .is_mp_safe = 1,
1956};
1957
1958clib_error_t *
Chenmin Sunc4665092020-07-06 08:20:39 +08001959set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1960 clib_bitmap_t * bitmap)
1961{
1962 vnet_main_t *vnm = vnet_get_main ();
1963 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1964
1965 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1966}
1967
1968static clib_error_t *
1969set_interface_rss_queues_fn (vlib_main_t * vm,
1970 unformat_input_t * input,
1971 vlib_cli_command_t * cmd)
1972{
1973 clib_error_t *error = 0;
1974 unformat_input_t _line_input, *line_input = &_line_input;
1975 vnet_main_t *vnm = vnet_get_main ();
1976 u32 hw_if_index = (u32) ~ 0;
1977 clib_bitmap_t *bitmap = NULL;
1978
1979 if (!unformat_user (input, unformat_line_input, line_input))
1980 return 0;
1981
1982 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1983 {
1984 if (unformat
1985 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1986 ;
1987 else
1988 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
1989 ;
1990 else
1991 {
1992 error = clib_error_return (0, "parse error: '%U'",
1993 format_unformat_error, line_input);
1994 unformat_free (line_input);
1995 goto done;
1996 }
1997 }
1998
1999 unformat_free (line_input);
2000
2001 if (hw_if_index == (u32) ~ 0)
2002 {
2003 error = clib_error_return (0, "please specify valid interface name");
2004 goto done;
2005 }
2006
2007 if (bitmap == NULL)
2008 {
2009 error = clib_error_return (0, "please specify the valid rss queues");
2010 goto done;
2011 }
2012
2013 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
2014
2015done:
2016 if (bitmap)
2017 clib_bitmap_free (bitmap);
2018
2019 return (error);
2020}
2021
2022/*?
2023 * This command is used to set the rss queues of a given interface
2024 * Not all the interfaces support this operation.
2025 * To display the current rss queues, use the command
2026 * '<em>show hardware-interfaces</em>'.
2027 *
2028 * @cliexpar
2029 * Example of how to set the rss queues to 0,2-5,7 of an interface:
2030 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
2031 * @cliexend
2032?*/
2033/* *INDENT-OFF* */
2034VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
2035 .path = "set interface rss queues",
2036 .short_help = "set interface rss queues <interface> <list <queue-list>>",
2037 .function = set_interface_rss_queues_fn,
2038};
2039/* *INDENT-ON* */
2040
Dave Barach33909772019-09-23 10:27:27 -04002041static u8 *
2042format_vnet_pcap (u8 * s, va_list * args)
2043{
2044 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2045 int type = va_arg (*args, int);
2046 int printed = 0;
2047
2048 if (type == 0)
2049 {
2050 if (pp->pcap_rx_enable)
2051 {
2052 s = format (s, "rx");
2053 printed = 1;
2054 }
2055 if (pp->pcap_tx_enable)
2056 {
2057 if (printed)
2058 s = format (s, " and ");
2059 s = format (s, "tx");
2060 printed = 1;
2061 }
2062 if (pp->pcap_drop_enable)
2063 {
2064 if (printed)
2065 s = format (s, " and ");
2066 s = format (s, "drop");
2067 printed = 1;
2068 }
2069 return s;
2070 }
2071 s = format (s, "unknown type %d!", type);
2072 return s;
2073}
2074
2075
Dave Barachb97641c2019-09-09 16:38:17 -04002076int
2077vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2078{
2079 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8fb5add2021-03-04 18:41:59 +01002080 vnet_main_t *vnm = vnet_get_main ();
2081 vnet_pcap_t *pp = &vnm->pcap;
Dave Barachb97641c2019-09-09 16:38:17 -04002082 pcap_main_t *pm = &pp->pcap_main;
Dave Barachf5667c32019-09-25 11:27:46 -04002083 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachb97641c2019-09-09 16:38:17 -04002084
2085 if (a->status)
2086 {
Dave Barach33909772019-09-23 10:27:27 -04002087 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002088 {
2089 vlib_cli_output
Dave Barach33909772019-09-23 10:27:27 -04002090 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2091 format_vnet_pcap, pp, 0 /* print type */ ,
Dave Barachb97641c2019-09-09 16:38:17 -04002092 pm->n_packets_captured, pm->n_packets_to_capture);
2093 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2094 }
2095 else
Dave Barach33909772019-09-23 10:27:27 -04002096 vlib_cli_output (vm, "pcap dispatch capture disabled");
2097
Dave Barachb97641c2019-09-09 16:38:17 -04002098 return 0;
2099 }
2100
2101 /* Consistency checks */
2102
2103 /* Enable w/ capture already enabled not allowed */
Dave Barach33909772019-09-23 10:27:27 -04002104 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2105 && (a->rx_enable + a->tx_enable + a->drop_enable))
Dave Barachb97641c2019-09-09 16:38:17 -04002106 return VNET_API_ERROR_INVALID_VALUE;
2107
2108 /* Disable capture with capture already disabled, not interesting */
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01002109 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
2110 0) &&
2111 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
Dave Barachb97641c2019-09-09 16:38:17 -04002112 return VNET_API_ERROR_VALUE_EXIST;
2113
2114 /* Change number of packets to capture while capturing */
Dave Barach33909772019-09-23 10:27:27 -04002115 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2116 && (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002117 && (pm->n_packets_to_capture != a->packets_to_capture))
2118 return VNET_API_ERROR_INVALID_VALUE_2;
2119
Dave Barach33909772019-09-23 10:27:27 -04002120 /* Classify filter specified, but no classify filter configured */
Dave Barachf5667c32019-09-25 11:27:46 -04002121 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
Benoît Ganne5943e362021-02-26 14:46:58 +01002122 (!cm->classify_table_index_by_sw_if_index ||
2123 cm->classify_table_index_by_sw_if_index[0] == ~0))
Dave Barach9137e542019-09-13 17:47:50 -04002124 return VNET_API_ERROR_NO_SUCH_LABEL;
2125
Dave Barach33909772019-09-23 10:27:27 -04002126 if (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002127 {
Dave Barach586462f2020-08-05 16:12:35 -04002128 void *save_pcap_data;
2129
Dave Barach33909772019-09-23 10:27:27 -04002130 /* Sanity check max bytes per pkt */
2131 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2132 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2133
Dave Barachb97641c2019-09-09 16:38:17 -04002134 /* Clean up from previous run, if any */
Dave Barach586462f2020-08-05 16:12:35 -04002135 vec_reset_length (pm->pcap_data);
2136
2137 /* Throw away the data buffer? */
2138 if (a->free_data)
2139 vec_free (pm->pcap_data);
2140
2141 save_pcap_data = pm->pcap_data;
2142
Dave Barachb97641c2019-09-09 16:38:17 -04002143 memset (pm, 0, sizeof (*pm));
2144
Dave Barach586462f2020-08-05 16:12:35 -04002145 pm->pcap_data = save_pcap_data;
2146
Dave Barach11fb09e2020-08-06 12:10:09 -04002147 vec_validate_aligned (vnet_trace_placeholder, 2048,
2148 CLIB_CACHE_LINE_BYTES);
Dave Barachb97641c2019-09-09 16:38:17 -04002149 if (pm->lock == 0)
2150 clib_spinlock_init (&(pm->lock));
2151
2152 if (a->filename == 0)
Dave Barach33909772019-09-23 10:27:27 -04002153 {
2154 u8 *stem = 0;
2155
2156 if (a->rx_enable)
2157 stem = format (stem, "rx");
2158 if (a->tx_enable)
2159 stem = format (stem, "tx");
2160 if (a->drop_enable)
2161 stem = format (stem, "drop");
Benoît Ganne7d4a9972020-07-17 11:49:56 +02002162 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
Dave Barach33909772019-09-23 10:27:27 -04002163 vec_free (stem);
2164 }
Dave Barachb97641c2019-09-09 16:38:17 -04002165
2166 pm->file_name = (char *) a->filename;
2167 pm->n_packets_captured = 0;
2168 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
Dave Barach586462f2020-08-05 16:12:35 -04002169 /* Preallocate the data vector? */
2170 if (a->preallocate_data)
2171 {
2172 vec_validate
2173 (pm->pcap_data, a->packets_to_capture
2174 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2175 vec_reset_length (pm->pcap_data);
2176 }
Dave Barachb97641c2019-09-09 16:38:17 -04002177 pm->n_packets_to_capture = a->packets_to_capture;
2178 pp->pcap_sw_if_index = a->sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002179 if (a->filter)
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002180 pp->filter_classify_table_index =
2181 cm->classify_table_index_by_sw_if_index[0];
Dave Barach9137e542019-09-13 17:47:50 -04002182 else
2183 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002184 pp->pcap_error_index = a->drop_err;
Dave Barach33909772019-09-23 10:27:27 -04002185 pp->pcap_rx_enable = a->rx_enable;
2186 pp->pcap_tx_enable = a->tx_enable;
2187 pp->pcap_drop_enable = a->drop_enable;
2188 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
Dave Barachb97641c2019-09-09 16:38:17 -04002189 }
2190 else
2191 {
Dave Barach33909772019-09-23 10:27:27 -04002192 pp->pcap_rx_enable = 0;
2193 pp->pcap_tx_enable = 0;
2194 pp->pcap_drop_enable = 0;
Dave Barachf5667c32019-09-25 11:27:46 -04002195 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002196 pp->pcap_error_index = ~0;
Dave Barachb97641c2019-09-09 16:38:17 -04002197 if (pm->n_packets_captured)
2198 {
2199 clib_error_t *error;
2200 pm->n_packets_to_capture = pm->n_packets_captured;
2201 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2202 pm->n_packets_captured, pm->file_name);
2203 error = pcap_write (pm);
Andrew Yourtchenko4da15062019-09-11 14:14:43 +00002204 if (pm->flags & PCAP_MAIN_INIT_DONE)
Dave Barachb97641c2019-09-09 16:38:17 -04002205 pcap_close (pm);
2206 /* Report I/O errors... */
2207 if (error)
2208 {
2209 clib_error_report (error);
2210 return VNET_API_ERROR_SYSCALL_ERROR_1;
2211 }
Dave Barach586462f2020-08-05 16:12:35 -04002212 vec_free (pm->file_name);
2213 if (a->free_data)
2214 vec_free (pm->pcap_data);
Dave Barachb97641c2019-09-09 16:38:17 -04002215 return 0;
2216 }
2217 else
2218 return VNET_API_ERROR_NO_SUCH_ENTRY;
2219 }
2220
2221 return 0;
2222}
2223
2224static clib_error_t *
Dave Barach33909772019-09-23 10:27:27 -04002225pcap_trace_command_fn (vlib_main_t * vm,
2226 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002227{
Dave Barach5ecd5a52019-02-25 15:27:28 -05002228 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb97641c2019-09-09 16:38:17 -04002229 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002230 vnet_main_t *vnm = vnet_get_main ();
Dave Barachb97641c2019-09-09 16:38:17 -04002231 u8 *filename = 0;
2232 u32 max = 1000;
Dave Barach33909772019-09-23 10:27:27 -04002233 u32 max_bytes_per_pkt = 512;
Dave Barachb97641c2019-09-09 16:38:17 -04002234 int rv;
Dave Barach33909772019-09-23 10:27:27 -04002235 int rx_enable = 0;
2236 int tx_enable = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002237 int preallocate_data = 0;
Dave Barach33909772019-09-23 10:27:27 -04002238 int drop_enable = 0;
Dave Barachb97641c2019-09-09 16:38:17 -04002239 int status = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002240 int filter = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002241 int free_data = 0;
Dave Barach4330c462020-06-10 17:07:32 -04002242 u32 sw_if_index = 0; /* default: any interface */
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002243 vlib_error_t drop_err = ~0; /* default: any error */
Dave Barach5ecd5a52019-02-25 15:27:28 -05002244
2245 /* Get a line of input. */
2246 if (!unformat_user (input, unformat_line_input, line_input))
2247 return 0;
2248
2249 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2250 {
Dave Barach33909772019-09-23 10:27:27 -04002251 if (unformat (line_input, "rx"))
2252 rx_enable = 1;
2253 else if (unformat (line_input, "tx"))
2254 tx_enable = 1;
2255 else if (unformat (line_input, "drop"))
2256 drop_enable = 1;
2257 else if (unformat (line_input, "off"))
2258 rx_enable = tx_enable = drop_enable = 0;
2259 else if (unformat (line_input, "max-bytes-per-pkt %u",
2260 &max_bytes_per_pkt))
Dave Barachb97641c2019-09-09 16:38:17 -04002261 ;
2262 else if (unformat (line_input, "max %d", &max))
2263 ;
2264 else if (unformat (line_input, "packets-to-capture %d", &max))
2265 ;
2266 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2267 &filename))
2268 ;
2269 else if (unformat (line_input, "status %=", &status, 1))
2270 ;
2271 else if (unformat (line_input, "intfc %U",
2272 unformat_vnet_sw_interface, vnm, &sw_if_index))
2273 ;
Dave Barach586462f2020-08-05 16:12:35 -04002274 else if (unformat (line_input, "interface %U",
2275 unformat_vnet_sw_interface, vnm, &sw_if_index))
2276 ;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002277 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2278 &drop_err))
2279 ;
Dave Barach586462f2020-08-05 16:12:35 -04002280 else if (unformat (line_input, "preallocate-data %=",
2281 &preallocate_data, 1))
2282 ;
2283 else if (unformat (line_input, "free-data %=", &free_data, 1))
2284 ;
2285 else if (unformat (line_input, "intfc any")
2286 || unformat (line_input, "interface any"))
Dave Barachb97641c2019-09-09 16:38:17 -04002287 sw_if_index = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002288 else if (unformat (line_input, "filter"))
2289 filter = 1;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002290 else
2291 {
Dave Barachb97641c2019-09-09 16:38:17 -04002292 return clib_error_return (0, "unknown input `%U'",
2293 format_unformat_error, line_input);
Dave Barach5ecd5a52019-02-25 15:27:28 -05002294 }
2295 }
Dave Barachb97641c2019-09-09 16:38:17 -04002296
Dave Barach5ecd5a52019-02-25 15:27:28 -05002297 unformat_free (line_input);
2298
Dave Barachb97641c2019-09-09 16:38:17 -04002299 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2300 a->filename = filename;
Dave Barach33909772019-09-23 10:27:27 -04002301 a->rx_enable = rx_enable;
2302 a->tx_enable = tx_enable;
Dave Barach586462f2020-08-05 16:12:35 -04002303 a->preallocate_data = preallocate_data;
2304 a->free_data = free_data;
Dave Barach33909772019-09-23 10:27:27 -04002305 a->drop_enable = drop_enable;
Dave Barachb97641c2019-09-09 16:38:17 -04002306 a->status = status;
2307 a->packets_to_capture = max;
Dave Barachb97641c2019-09-09 16:38:17 -04002308 a->sw_if_index = sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002309 a->filter = filter;
Dave Barach33909772019-09-23 10:27:27 -04002310 a->max_bytes_per_pkt = max_bytes_per_pkt;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002311 a->drop_err = drop_err;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002312
Dave Barachb97641c2019-09-09 16:38:17 -04002313 rv = vnet_pcap_dispatch_trace_configure (a);
2314
2315 switch (rv)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002316 {
Dave Barachb97641c2019-09-09 16:38:17 -04002317 case 0:
2318 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002319
Dave Barachb97641c2019-09-09 16:38:17 -04002320 case VNET_API_ERROR_INVALID_VALUE:
2321 return clib_error_return (0, "dispatch trace already enabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002322
Dave Barachb97641c2019-09-09 16:38:17 -04002323 case VNET_API_ERROR_VALUE_EXIST:
2324 return clib_error_return (0, "dispatch trace already disabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002325
Dave Barachb97641c2019-09-09 16:38:17 -04002326 case VNET_API_ERROR_INVALID_VALUE_2:
2327 return clib_error_return
2328 (0, "can't change number of records to capture while tracing...");
2329
2330 case VNET_API_ERROR_SYSCALL_ERROR_1:
2331 return clib_error_return (0, "I/O writing trace capture...");
2332
2333 case VNET_API_ERROR_NO_SUCH_ENTRY:
2334 return clib_error_return (0, "No packets captured...");
2335
Dave Barach33909772019-09-23 10:27:27 -04002336 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2337 return clib_error_return (0,
2338 "Max bytes per pkt must be > 32, < 9000...");
2339
Dave Barach9137e542019-09-13 17:47:50 -04002340 case VNET_API_ERROR_NO_SUCH_LABEL:
2341 return clib_error_return
2342 (0, "No classify filter configured, see 'classify filter...'");
2343
Dave Barachb97641c2019-09-09 16:38:17 -04002344 default:
2345 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2346 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002347 }
Dave Barachb97641c2019-09-09 16:38:17 -04002348 return 0;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002349}
2350
Dave Barach5ecd5a52019-02-25 15:27:28 -05002351/*?
2352 * This command is used to start or stop a packet capture, or show
Dave Barach33909772019-09-23 10:27:27 -04002353 * the status of packet capture.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002354 *
2355 * This command has the following optional parameters:
2356 *
Dave Barach33909772019-09-23 10:27:27 -04002357 *
2358 * - <b>rx</b> - Capture received packets
2359 *
2360 * - <b>tx</b> - Capture transmitted packets
2361 *
2362 * - <b>drop</b> - Capture dropped packets
2363 *
2364 * - <b>off</b> - Stop capturing packets, write results to the specified file
Dave Barach5ecd5a52019-02-25 15:27:28 -05002365 *
2366 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2367 * of packets have been received, buffer is flushed to file. Once another
2368 * '<em>nn</em>' number of packets have been received, buffer is flushed
2369 * to file, overwriting previous write. If not entered, value defaults
2370 * to 100. Can only be updated if packet capture is off.
2371 *
Dave Barach33909772019-09-23 10:27:27 -04002372 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2373 * for each packet. Must be >= 32, <= 9000.
2374 *
Dave Barach586462f2020-08-05 16:12:35 -04002375 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2376 * vector expansion delays during pcap capture
2377 *
2378 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2379 * to retain the data buffer so this option is seldom used.
2380 *
Dave Barach33909772019-09-23 10:27:27 -04002381 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002382 * or use '<em>any</em>' to run packet capture on all interfaces.
2383 * '<em>any</em>' is the default if not provided. Settings from a previous
2384 * packet capture are preserved, so '<em>any</em>' can be used to reset
2385 * the interface setting.
2386 *
Nobuhiro MIKId346f392023-02-28 18:30:09 +09002387 * - <b>filter</b> - Use the pcap trace rx / tx / drop filter, which
Dave Barachf5667c32019-09-25 11:27:46 -04002388 * must be configured. Use <b>classify filter pcap...</b> to configure the
2389 * filter. The filter will only be executed if the per-interface or
2390 * any-interface tests fail.
2391 *
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002392 * - <b>error <node>.<error></b> - filter packets based on a specific error.
Maxime Peimddc16cf2023-01-13 08:04:55 +00002393 * For example: error {ip4-udp-lookup}.{no_listener}
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002394 *
Dave Barach5ecd5a52019-02-25 15:27:28 -05002395 * - <b>file <name></b> - Used to specify the output filename. The file will
2396 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2397 * supported. Directory should not be entered. If file already exists, file
Dave Barach33909772019-09-23 10:27:27 -04002398 * will be overwritten. If no filename is provided, the file will be
2399 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2400 * Can only be updated if packet capture is off.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002401 *
2402 * - <b>status</b> - Displays the current status and configured attributes
2403 * associated with a packet capture. If packet capture is in progress,
2404 * '<em>status</em>' also will return the number of packets currently in
2405 * the local buffer. All additional attributes entered on command line
2406 * with '<em>status</em>' will be ignored and not applied.
2407 *
2408 * @cliexpar
2409 * Example of how to display the status of a tx packet capture when off:
Dave Barach33909772019-09-23 10:27:27 -04002410 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002411 * max is 100, for any interface to file /tmp/vpe.pcap
2412 * pcap tx capture is off...
2413 * @cliexend
2414 * Example of how to start a tx packet capture:
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002415 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002416 * vppTest.pcap}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002417 * @cliexend
2418 * Example of how to display the status of a tx packet capture in progress:
Dave Barach33909772019-09-23 10:27:27 -04002419 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002420 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2421 * pcap tx capture is on: 20 of 35 pkts...
2422 * @cliexend
2423 * Example of how to stop a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002424 * @cliexstart{pcap trace off}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002425 * captured 21 pkts...
2426 * saved to /tmp/vppTest.pcap...
2427 * @cliexend
2428?*/
2429/* *INDENT-OFF* */
2430
2431VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
Dave Barach33909772019-09-23 10:27:27 -04002432 .path = "pcap trace",
Dave Barach5ecd5a52019-02-25 15:27:28 -05002433 .short_help =
Dave Barach586462f2020-08-05 16:12:35 -04002434 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2435 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2436 " [preallocate-data][free-data]",
Dave Barach33909772019-09-23 10:27:27 -04002437 .function = pcap_trace_command_fn,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002438};
2439/* *INDENT-ON* */
2440
Steven Luongf49734d2021-07-26 13:38:05 -07002441static clib_error_t *
2442set_interface_name (vlib_main_t *vm, unformat_input_t *input,
2443 vlib_cli_command_t *cmd)
2444{
2445 clib_error_t *error = 0;
2446 unformat_input_t _line_input, *line_input = &_line_input;
2447 vnet_main_t *vnm = vnet_get_main ();
2448 u32 hw_if_index = ~0;
2449 char *name = 0;
2450
2451 if (!unformat_user (input, unformat_line_input, line_input))
2452 return 0;
2453
2454 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2455 {
2456 if (unformat (line_input, "%U %s", unformat_vnet_hw_interface, vnm,
2457 &hw_if_index, &name))
2458 ;
2459 else
2460 {
2461 error = clib_error_return (0, "parse error: '%U'",
2462 format_unformat_error, line_input);
2463 unformat_free (line_input);
2464 vec_free (name);
2465 return error;
2466 }
2467 }
2468
2469 unformat_free (line_input);
2470
2471 if (hw_if_index == (u32) ~0 || name == 0)
2472 {
2473 vec_free (name);
2474 error = clib_error_return (0, "please specify valid interface name");
2475 return error;
2476 }
2477
2478 error = vnet_rename_interface (vnm, hw_if_index, name);
2479 vec_free (name);
2480
2481 return (error);
2482}
2483
2484VLIB_CLI_COMMAND (cmd_set_if_name, static) = {
2485 .path = "set interface name",
2486 .short_help = "set interface name <interface-name> <new-interface-name>",
2487 .function = set_interface_name,
2488 .is_mp_safe = 1,
2489};
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00002490
2491static clib_error_t *
2492set_interface_tx_hash_cmd (vlib_main_t *vm, unformat_input_t *input,
2493 vlib_cli_command_t *cmd)
2494{
2495 clib_error_t *error = 0;
2496 unformat_input_t _line_input, *line_input = &_line_input;
2497 vnet_main_t *vnm = vnet_get_main ();
2498 vnet_hw_interface_t *hi;
2499 u8 *hash_name = 0;
2500 u32 hw_if_index = (u32) ~0;
2501 vnet_hash_fn_t hf;
2502 vnet_hash_fn_type_t ftype;
2503
2504 if (!unformat_user (input, unformat_line_input, line_input))
2505 return 0;
2506
2507 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2508 {
2509 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2510 &hw_if_index))
2511 ;
2512 else if (unformat (line_input, "hash-name %s", &hash_name))
2513 ;
2514 else
2515 {
2516 error = clib_error_return (0, "parse error: '%U'",
2517 format_unformat_error, line_input);
2518 unformat_free (line_input);
2519 return error;
2520 }
2521 }
2522
2523 unformat_free (line_input);
2524
2525 if (hw_if_index == (u32) ~0)
2526 {
2527 error = clib_error_return (0, "please specify valid interface name");
2528 goto error;
2529 }
2530
Dave Barach9e0f9e22022-01-18 08:52:47 -05002531 if (hash_name == 0)
2532 {
2533 error = clib_error_return (0, "hash-name is required");
2534 goto error;
2535 }
2536
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00002537 hi = vnet_get_hw_interface (vnm, hw_if_index);
2538 ftype =
2539 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2540 hf = vnet_hash_function_from_name ((const char *) hash_name, ftype);
2541
2542 if (!hf)
2543 {
2544 error = clib_error_return (0, "please specify valid hash name");
2545 goto error;
2546 }
2547
2548 hi->hf = hf;
2549error:
2550 vec_free (hash_name);
2551 return (error);
2552}
2553
2554VLIB_CLI_COMMAND (cmd_set_if_tx_hash, static) = {
2555 .path = "set interface tx-hash",
2556 .short_help = "set interface tx-hash <interface> hash-name <hash-name>",
2557 .function = set_interface_tx_hash_cmd,
2558};
2559
2560static clib_error_t *
2561show_tx_hash (vlib_main_t *vm, unformat_input_t *input,
2562 vlib_cli_command_t *cmd)
2563{
2564 clib_error_t *error = 0;
2565 unformat_input_t _line_input, *line_input = &_line_input;
2566 vnet_main_t *vnm = vnet_get_main ();
2567 vnet_hw_interface_t *hi;
2568 vnet_hash_function_registration_t *hash;
2569 u32 hw_if_index = (u32) ~0;
2570 vnet_hash_fn_type_t ftype;
2571
2572 if (!unformat_user (input, unformat_line_input, line_input))
2573 return 0;
2574
2575 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2576 {
2577 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2578 &hw_if_index))
2579 ;
2580 else
2581 {
2582 error = clib_error_return (0, "parse error: '%U'",
2583 format_unformat_error, line_input);
2584 unformat_free (line_input);
2585 goto error;
2586 }
2587 }
2588
2589 unformat_free (line_input);
2590
2591 if (hw_if_index == (u32) ~0)
2592 {
2593 error = clib_error_return (0, "please specify valid interface name");
2594 goto error;
2595 }
2596
2597 hi = vnet_get_hw_interface (vnm, hw_if_index);
2598 ftype =
2599 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2600
2601 if (hi->hf)
2602 {
2603 hash = vnet_hash_function_from_func (hi->hf, ftype);
2604 if (hash)
2605 vlib_cli_output (vm, "%U", format_vnet_hash, hash);
2606 else
2607 vlib_cli_output (vm, "no matching hash function found");
2608 }
2609 else
2610 vlib_cli_output (vm, "no hashing function set");
2611
2612error:
2613 return (error);
2614}
2615
2616VLIB_CLI_COMMAND (cmd_show_tx_hash, static) = {
2617 .path = "show interface tx-hash",
2618 .short_help = "show interface tx-hash [interface]",
2619 .function = show_tx_hash,
2620};
2621
Dave Barachba868bb2016-08-08 09:51:21 -04002622/*
2623 * fd.io coding-style-patch-verification: ON
2624 *
2625 * Local Variables:
2626 * eval: (c-set-style "gnu")
2627 * End:
2628 */