blob: b2410b82c055c8f19a5ba813d037de1f22c7d27e [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>
Damjan Marionb8dd9812023-11-03 13:47:05 +000057#include <vnet/dev/dev.h>
58#include <vnet/dev/dev_funcs.h>
59
Dave Barachba868bb2016-08-08 09:51:21 -040060static int
61compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Dave Barachba868bb2016-08-08 09:51:21 -040063 u32 *hi1 = a1;
64 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Dave Barachba868bb2016-08-08 09:51:21 -040066 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070067}
68
69static clib_error_t *
70show_or_clear_hw_interfaces (vlib_main_t * vm,
71 unformat_input_t * input,
Benoît Ganne17814d72020-07-24 09:57:11 +020072 vlib_cli_command_t * cmd, int is_show)
Ed Warnickecb9cada2015-12-08 15:45:58 -070073{
Dave Barachba868bb2016-08-08 09:51:21 -040074 clib_error_t *error = 0;
Vratko Polak34149772022-03-31 12:32:10 +020075 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachba868bb2016-08-08 09:51:21 -040076 vnet_main_t *vnm = vnet_get_main ();
77 vnet_interface_main_t *im = &vnm->interface_main;
78 vnet_hw_interface_t *hi;
79 u32 hw_if_index, *hw_if_indices = 0;
Benoît Ganne17814d72020-07-24 09:57:11 +020080 int i, verbose = -1, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Vratko Polak34149772022-03-31 12:32:10 +020082 if (!unformat_user (input, unformat_line_input, line_input))
Mohammed Hawari83caa622022-08-08 10:31:11 +020083 goto skip_unformat;
Vratko Polak34149772022-03-31 12:32:10 +020084
85 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 {
87 /* See if user wants to show a specific interface. */
Vratko Polak34149772022-03-31 12:32:10 +020088 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
89 &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040090 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040091
Sean Hope679ea792016-02-22 15:12:01 -050092 /* See if user wants to show an interface with a specific hw_if_index. */
Vratko Polak34149772022-03-31 12:32:10 +020093 else if (unformat (line_input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040094 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Vratko Polak34149772022-03-31 12:32:10 +020096 else if (unformat (line_input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040097 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Vratko Polak34149772022-03-31 12:32:10 +020099 else if (unformat (line_input, "detail"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 verbose = 2;
101
Vratko Polak34149772022-03-31 12:32:10 +0200102 else if (unformat (line_input, "brief"))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 verbose = 0;
104
Vratko Polak34149772022-03-31 12:32:10 +0200105 else if (unformat (line_input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -0400106 {
107 show_bond = 1;
108 if (verbose < 0)
109 verbose = 0; /* default to brief for link bonding */
110 }
John Lobcebbb92016-04-05 15:47:43 -0400111
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 else
113 {
114 error = clib_error_return (0, "unknown input `%U'",
Vratko Polak34149772022-03-31 12:32:10 +0200115 format_unformat_error, line_input);
116 unformat_free (line_input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 goto done;
118 }
119 }
Dave Barachba868bb2016-08-08 09:51:21 -0400120
Vratko Polak34149772022-03-31 12:32:10 +0200121 unformat_free (line_input);
122
Mohammed Hawari83caa622022-08-08 10:31:11 +0200123skip_unformat:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 /* Gather interfaces. */
125 if (vec_len (hw_if_indices) == 0)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100126 pool_foreach (hi, im->hw_interfaces)
127 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
Dave Barachba868bb2016-08-08 09:51:21 -0400129 if (verbose < 0)
130 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400131
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132 if (is_show)
133 {
134 /* Sort by name. */
135 vec_sort_with_function (hw_if_indices, compare_interface_names);
136
137 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
138 for (i = 0; i < vec_len (hw_if_indices); i++)
139 {
140 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400141 if (show_bond == 0) /* show all interfaces */
142 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
143 hi, verbose);
144 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400145 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400146 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400147 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400148 vnet_hw_interface_t *shi;
149 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400150 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400151
152 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100153 clib_bitmap_foreach (hw_idx, hi->bond_info)
154 {
Dave Barachba868bb2016-08-08 09:51:21 -0400155 shi = vnet_get_hw_interface(vnm, hw_idx);
156 vlib_cli_output (vm, "%U\n",
157 format_vnet_hw_interface, vnm, shi, verbose);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100158 }
Dave Barachba868bb2016-08-08 09:51:21 -0400159 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400160 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 }
162 }
163 else
164 {
165 for (i = 0; i < vec_len (hw_if_indices); i++)
166 {
Dave Barachba868bb2016-08-08 09:51:21 -0400167 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
169 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
170 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400171
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 if (dc->clear_counters)
173 dc->clear_counters (hi->dev_instance);
174 }
175 }
176
Dave Barachba868bb2016-08-08 09:51:21 -0400177done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178 vec_free (hw_if_indices);
179 return error;
180}
181
Benoît Ganne17814d72020-07-24 09:57:11 +0200182static clib_error_t *
183show_hw_interfaces (vlib_main_t * vm,
184 unformat_input_t * input, vlib_cli_command_t * cmd)
185{
186 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
187}
188
189static clib_error_t *
190clear_hw_interfaces (vlib_main_t * vm,
191 unformat_input_t * input, vlib_cli_command_t * cmd)
192{
193 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
194}
195
196
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700197/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400198 * Display more detailed information about all or a list of given interfaces.
199 * The verboseness of the output can be controlled by the following optional
200 * parameters:
201 * - brief: Only show name, index and state (default for bonded interfaces).
202 * - verbose: Also display additional attributes (default for all other interfaces).
203 * - detail: Also display all remaining attributes and extended statistics.
204 *
205 * To limit the output of the command to bonded interfaces and their slave
206 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700207 *
208 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400209 * Example of how to display default data for all interfaces:
210 * @cliexstart{show hardware-interfaces}
211 * Name Idx Link Hardware
212 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
213 * Ethernet address ec:f4:bb:c0:bc:fc
214 * Intel e1000
215 * carrier up full duplex speed 1000 mtu 9216
216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
217 * cpu socket 0
218 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
219 * Ethernet address ec:f4:bb:c0:bc:fd
220 * Intel e1000
221 * carrier up full duplex speed 1000 mtu 9216
222 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
223 * cpu socket 0
224 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
225 * Ethernet address 02:fe:a5:a9:8b:8e
226 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
227 * Ethernet address 02:fe:c0:4e:3b:b0
228 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
229 * Ethernet address 02:fe:1f:73:92:81
230 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
231 * Ethernet address 02:fe:f2:25:c4:68
232 * local0 0 down local0
233 * local
234 * @cliexend
235 * Example of how to display '<em>verbose</em>' data for an interface by name and
236 * software index (where 2 is the software index):
237 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
238 * Name Idx Link Hardware
239 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
240 * Ethernet address ec:f4:bb:c0:bc:fc
241 * Intel e1000
242 * carrier up full duplex speed 1000 mtu 9216
243 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
244 * cpu socket 0
245 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
246 * Ethernet address ec:f4:bb:c0:bc:fd
247 * Intel e1000
248 * carrier up full duplex speed 1000 mtu 9216
249 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
250 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700251 * @cliexend
252 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400253/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
255 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400256 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
257 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200258 .function = show_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259};
Dave Barachba868bb2016-08-08 09:51:21 -0400260/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Billy McFalle9bac692017-08-11 14:05:11 -0400262
263/*?
264 * Clear the extended statistics for all or a list of given interfaces
265 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
266 *
267 * @cliexpar
268 * Example of how to clear the extended statistics for all interfaces:
269 * @cliexcmd{clear hardware-interfaces}
270 * Example of how to clear the extended statistics for an interface by
271 * name and software index (where 2 is the software index):
272 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
273 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400274/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
276 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400277 .short_help = "clear hardware-interfaces "
278 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200279 .function = clear_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280};
Dave Barachba868bb2016-08-08 09:51:21 -0400281/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barachba868bb2016-08-08 09:51:21 -0400283static int
284sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285{
286 vnet_sw_interface_t *si1 = a1;
287 vnet_sw_interface_t *si2 = a2;
288
Dave Barachba868bb2016-08-08 09:51:21 -0400289 return vnet_sw_interface_compare (vnet_get_main (),
290 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291}
292
293static clib_error_t *
294show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400295 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296{
Dave Barachba868bb2016-08-08 09:51:21 -0400297 clib_error_t *error = 0;
298 vnet_main_t *vnm = vnet_get_main ();
Dave Barach525c9d02018-05-26 10:48:55 -0400299 unformat_input_t _linput, *linput = &_linput;
Dave Barachba868bb2016-08-08 09:51:21 -0400300 vnet_interface_main_t *im = &vnm->interface_main;
301 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200302 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200304 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500305 u8 show_tag = 0;
Jon Loeliger9485d992019-11-08 15:05:23 -0600306 u8 show_vtr = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400307 int verbose = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Dave Barach525c9d02018-05-26 10:48:55 -0400309 /*
310 * Get a line of input. Won't work if the user typed
311 * "show interface" and nothing more.
312 */
313 if (unformat_user (input, unformat_line_input, linput))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 {
Dave Barach525c9d02018-05-26 10:48:55 -0400315 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 {
Dave Barach525c9d02018-05-26 10:48:55 -0400317 /* See if user wants to show specific interface */
318 if (unformat
319 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
320 {
321 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
322 vec_add1 (sorted_sis, si[0]);
323 }
324 else if (unformat (linput, "address") || unformat (linput, "addr"))
325 show_addresses = 1;
326 else if (unformat (linput, "features") || unformat (linput, "feat"))
327 show_features = 1;
328 else if (unformat (linput, "tag"))
329 show_tag = 1;
Jon Loeliger9485d992019-11-08 15:05:23 -0600330 else if (unformat (linput, "vtr"))
331 show_vtr = 1;
Dave Barach525c9d02018-05-26 10:48:55 -0400332 else if (unformat (linput, "verbose"))
333 verbose = 1;
Neale Rannse619ada2021-09-22 10:49:43 +0000334 else if (unformat (linput, "%d", &sw_if_index))
335 {
336 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
337 {
338 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
339 vec_add1 (sorted_sis, si[0]);
340 }
341 else
342 {
343 vec_free (sorted_sis);
344 error = clib_error_return (0, "unknown interface index `%d'",
345 sw_if_index);
346 goto done;
347 }
348 }
Dave Barach525c9d02018-05-26 10:48:55 -0400349 else
350 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400351 vec_free (sorted_sis);
Dave Barach525c9d02018-05-26 10:48:55 -0400352 error = clib_error_return (0, "unknown input `%U'",
353 format_unformat_error, linput);
354 goto done;
355 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356 }
Dave Barach525c9d02018-05-26 10:48:55 -0400357 unformat_free (linput);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 }
Jon Loeliger9485d992019-11-08 15:05:23 -0600359 if (show_features || show_tag || show_vtr)
Damjan Marion22311502016-10-28 20:30:15 +0200360 {
361 if (sw_if_index == ~(u32) 0)
Dave Barach8fdde3c2019-05-17 10:46:40 -0400362 {
363 vec_free (sorted_sis);
364 return clib_error_return (0, "Interface not specified...");
365 }
Dave Barach7be864a2016-11-28 11:41:35 -0500366 }
Damjan Marion22311502016-10-28 20:30:15 +0200367
Dave Barach7be864a2016-11-28 11:41:35 -0500368 if (show_features)
369 {
Dave Barach525c9d02018-05-26 10:48:55 -0400370 vnet_interface_features_show (vm, sw_if_index, verbose);
Neale Ranns47a3d992020-09-29 15:38:51 +0000371 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
Eyal Bari942402b2017-07-26 11:57:04 +0300372
373 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
374 vlib_cli_output (vm, "\nl2-output:");
375 if (l2_output->out_vtr_flag)
376 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
377 vlib_cli_output (vm, "%U", format_l2_output_features,
Neale Ranns5d9df1d2018-11-09 08:19:27 -0800378 l2_output->feature_bitmap, 1);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400379 vec_free (sorted_sis);
Damjan Marion22311502016-10-28 20:30:15 +0200380 return 0;
381 }
Dave Barach7be864a2016-11-28 11:41:35 -0500382 if (show_tag)
383 {
384 u8 *tag;
385 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
386 vlib_cli_output (vm, "%U: %s",
387 format_vnet_sw_if_index_name, vnm, sw_if_index,
388 tag ? (char *) tag : "(none)");
Dave Barach8fdde3c2019-05-17 10:46:40 -0400389 vec_free (sorted_sis);
Dave Barach7be864a2016-11-28 11:41:35 -0500390 return 0;
391 }
Damjan Marion22311502016-10-28 20:30:15 +0200392
Jon Loeliger9485d992019-11-08 15:05:23 -0600393 /*
394 * Show vlan tag rewrite data for one interface.
395 */
396 if (show_vtr)
397 {
398 u32 vtr_op = L2_VTR_DISABLED;
399 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
400
401 if (l2vtr_get (vm, vnm, sw_if_index,
402 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
403 {
404 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
405 format_vnet_sw_if_index_name, vnm, sw_if_index);
406 return 0;
407 }
408 vlib_cli_output (vm, "%U: VTR %0U",
409 format_vnet_sw_if_index_name, vnm, sw_if_index,
410 format_vtr, vtr_op, push_dot1q, tag1, tag2);
411 return 0;
412 }
413
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400415 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416
Dave Barachba868bb2016-08-08 09:51:21 -0400417 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418 {
419 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400420 sorted_sis =
421 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +0200422 vec_set_len (sorted_sis, 0);
Dave Barach525c9d02018-05-26 10:48:55 -0400423 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100424 pool_foreach (si, im->sw_interfaces)
425 {
Dave Barach525c9d02018-05-26 10:48:55 -0400426 int visible = vnet_swif_is_api_visible (si);
427 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100428 vec_add1 (sorted_sis, si[0]);
429 }
Ole Troand7231612018-06-07 10:17:57 +0200430 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 /* Sort by name. */
432 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
433 }
434
435 if (show_addresses)
436 {
437 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400438 {
Dave Barachba868bb2016-08-08 09:51:21 -0400439 ip4_main_t *im4 = &ip4_main;
440 ip6_main_t *im6 = &ip6_main;
441 ip_lookup_main_t *lm4 = &im4->lookup_main;
442 ip_lookup_main_t *lm6 = &im6->lookup_main;
443 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400444 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Dave Barachba868bb2016-08-08 09:51:21 -0400446 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
447 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
448 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Dave Barachba868bb2016-08-08 09:51:21 -0400450 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
451 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
452 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453
John Lo4478d8e2018-01-12 17:15:25 -0500454 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
455 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Dave Barachba868bb2016-08-08 09:51:21 -0400457 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
458 vlib_cli_output
459 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500460 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400461 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
462 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400463 else
John Lo4478d8e2018-01-12 17:15:25 -0500464 vlib_cli_output
465 (vm, "%U (%s):",
466 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
467 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468
Eyal Bari942402b2017-07-26 11:57:04 +0300469 /* Display any L2 info */
Neale Ranns47a3d992020-09-29 15:38:51 +0000470 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400471
John Lo4478d8e2018-01-12 17:15:25 -0500472 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400473 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500474 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
475 1 /* honor unnumbered */,
476 ({
477 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
Neale Rannsd6953332021-08-10 07:39:18 +0000478 if (fib4->hash.table_id)
479 vlib_cli_output (
480 vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address,
481 r4, ia->address_length, fib4->hash.table_id,
482 ip4_fib_index_from_table_id (fib4->hash.table_id));
John Lo4478d8e2018-01-12 17:15:25 -0500483 else
484 vlib_cli_output (vm, " L3 %U/%d",
485 format_ip4_address, r4, ia->address_length);
486 }));
487 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
John Lo4478d8e2018-01-12 17:15:25 -0500489 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400490 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500491 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
492 1 /* honor unnumbered */,
493 ({
494 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
495 if (fib6->table_id)
496 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
497 format_ip6_address, r6, ia->address_length,
498 fib6->table_id,
499 ip6_fib_index_from_table_id (fib6->table_id));
500 else
501 vlib_cli_output (vm, " L3 %U/%d",
502 format_ip6_address, r6, ia->address_length);
503 }));
504 /* *INDENT-ON* */
Ole Troand7231612018-06-07 10:17:57 +0200505 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 }
Ole Troand7231612018-06-07 10:17:57 +0200507 else
508 {
509 vec_foreach (si, sorted_sis)
510 {
511 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
512 }
513 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Dave Barachba868bb2016-08-08 09:51:21 -0400515done:
Ole Troand7231612018-06-07 10:17:57 +0200516 vec_free (sorted_sis);
517 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518}
519
Dave Barachba868bb2016-08-08 09:51:21 -0400520/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400522 .path = "show interface",
Mohsin Kazmi88d94142023-01-26 15:14:17 +0000523 .short_help = "show interface [address|addr|features|feat|vtr|tag] "
524 "[<interface> [<interface> [..]]] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 .function = show_sw_interfaces,
Steven Luongc5fa2b82019-04-25 11:19:49 -0700526 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527};
Dave Barachba868bb2016-08-08 09:51:21 -0400528/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400531/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
533 .path = "interface",
534 .short_help = "Interface commands",
535};
Dave Barachba868bb2016-08-08 09:51:21 -0400536/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537
Dave Barachba868bb2016-08-08 09:51:21 -0400538/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
540 .path = "set interface",
541 .short_help = "Interface commands",
542};
Dave Barachba868bb2016-08-08 09:51:21 -0400543/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
545static clib_error_t *
546clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400547 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
Dave Barachba868bb2016-08-08 09:51:21 -0400549 vnet_main_t *vnm = vnet_get_main ();
550 vnet_interface_main_t *im = &vnm->interface_main;
551 vlib_simple_counter_main_t *sm;
552 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400553 int j, n_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
555 n_counters = vec_len (im->combined_sw_if_counters);
556
557 for (j = 0; j < n_counters; j++)
558 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400559 im = &vnm->interface_main;
560 cm = im->combined_sw_if_counters + j;
561 vlib_clear_combined_counters (cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 }
563
564 n_counters = vec_len (im->sw_if_counters);
565
566 for (j = 0; j < n_counters; j++)
567 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400568 im = &vnm->interface_main;
569 sm = im->sw_if_counters + j;
570 vlib_clear_simple_counters (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 }
572
573 return 0;
574}
575
Billy McFalle9bac692017-08-11 14:05:11 -0400576/*?
577 * Clear the statistics for all interfaces (statistics associated with the
578 * '<em>show interface</em>' command).
579 *
580 * @cliexpar
581 * Example of how to clear the statistics for all interfaces:
582 * @cliexcmd{clear interfaces}
583 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400584/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
586 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400587 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588 .function = clear_interface_counters,
589};
Dave Barachba868bb2016-08-08 09:51:21 -0400590/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Chris Luke16bcf7d2016-09-01 14:31:46 -0400592/**
593 * Parse subinterface names.
594 *
Dave Barachba868bb2016-08-08 09:51:21 -0400595 * The following subinterface syntax is supported. The first two are for
596 * backwards compatability:
597 *
598 * <intf-name> <id>
599 * - a subinterface with the name <intf-name>.<id>. The subinterface
600 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
601 *
602 * <intf-name> <min_id>-<max_id>
603 * - a set of the above subinterfaces, repeating for each id
604 * in the range <min_id> to <max_id>
605 *
606 * In the following, exact-match semantics (i.e. the number of vlan tags on the
607 * packet must match the number of tags in the configuration) are used only if
608 * the keyword exact-match is present. Non-exact match is the default.
609 *
610 * <intf-name> <id> dot1q <outer_id> [exact-match]
611 * - a subinterface with the name <intf-name>.<id>. The subinterface
612 * is a single dot1q vlan with vlan id <outer_id>.
613 *
614 * <intf-name> <id> dot1q any [exact-match]
615 * - a subinterface with the name <intf-name>.<id>. The subinterface
616 * is a single dot1q vlan with any vlan id.
617 *
618 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
619 * - a subinterface with the name <intf-name>.<id>. The subinterface
620 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
621 * <inner_id>.
622 *
623 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
624 * - a subinterface with the name <intf-name>.<id>. The subinterface
625 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
626 *
627 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
628 *
629 * - a subinterface with the name <intf-name>.<id>. The subinterface
630 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
631 *
632 * For each of the above CLI, there is a duplicate that uses the keyword
633 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
634 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
635 * tagged packets the inner ethertype is always 0x8100. Also note that
636 * the dot1q and dot1ad naming spaces are independent, so it is legal to
637 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
638 *
639 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
640 * - a subinterface with the name <intf-name>.<id>. The subinterface
641 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
642 * id <inner_id>.
643 *
644 * <intf-name> <id> untagged
645 * - a subinterface with the name <intf-name>.<id>. The subinterface
646 * has no vlan tags. Only one can be specified per interface.
647 *
648 * <intf-name> <id> default
649 * - a subinterface with the name <intf-name>.<id>. This is associated
650 * with a packet that did not match any other configured subinterface
651 * on this interface. Only one can be specified per interface.
652 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
654static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400655parse_vlan_sub_interfaces (unformat_input_t * input,
656 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
Dave Barachba868bb2016-08-08 09:51:21 -0400658 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 u32 inner_vlan, outer_vlan;
660
Dave Barachba868bb2016-08-08 09:51:21 -0400661 if (unformat (input, "any inner-dot1q any"))
662 {
663 template->sub.eth.flags.two_tags = 1;
664 template->sub.eth.flags.outer_vlan_id_any = 1;
665 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666 }
Dave Barachba868bb2016-08-08 09:51:21 -0400667 else if (unformat (input, "any"))
668 {
669 template->sub.eth.flags.one_tag = 1;
670 template->sub.eth.flags.outer_vlan_id_any = 1;
671 }
672 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
673 {
674 template->sub.eth.flags.two_tags = 1;
675 template->sub.eth.flags.inner_vlan_id_any = 1;
676 template->sub.eth.outer_vlan_id = outer_vlan;
677 }
678 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
679 {
680 template->sub.eth.flags.two_tags = 1;
681 template->sub.eth.outer_vlan_id = outer_vlan;
682 template->sub.eth.inner_vlan_id = inner_vlan;
683 }
684 else if (unformat (input, "%d", &outer_vlan))
685 {
686 template->sub.eth.flags.one_tag = 1;
687 template->sub.eth.outer_vlan_id = outer_vlan;
688 }
689 else
690 {
691 error = clib_error_return (0, "expected dot1q config, got `%U'",
692 format_unformat_error, input);
693 goto done;
694 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695
Dave Barachba868bb2016-08-08 09:51:21 -0400696 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
697 {
698 if (unformat (input, "exact-match"))
699 {
700 template->sub.eth.flags.exact_match = 1;
701 }
702 }
703
704done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 return error;
706}
707
708static clib_error_t *
709create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400710 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711{
Dave Barachba868bb2016-08-08 09:51:21 -0400712 vnet_main_t *vnm = vnet_get_main ();
713 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400715 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716 u32 id, id_min, id_max;
717 vnet_sw_interface_t template;
718
719 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400720 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 {
722 error = clib_error_return (0, "unknown interface `%U'",
723 format_unformat_error, input);
724 goto done;
725 }
726
Dave Barachb7b92992018-10-17 10:38:51 -0400727 clib_memset (&template, 0, sizeof (template));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700728 template.sub.eth.raw_flags = 0;
729
Dave Barachba868bb2016-08-08 09:51:21 -0400730 if (unformat (input, "%d default", &id_min))
731 {
732 id_max = id_min;
733 template.sub.eth.flags.default_sub = 1;
734 }
735 else if (unformat (input, "%d untagged", &id_min))
736 {
737 id_max = id_min;
738 template.sub.eth.flags.no_tags = 1;
739 template.sub.eth.flags.exact_match = 1;
740 }
741 else if (unformat (input, "%d dot1q", &id_min))
742 {
743 /* parse dot1q config */
744 id_max = id_min;
745 error = parse_vlan_sub_interfaces (input, &template);
746 if (error)
747 goto done;
748 }
749 else if (unformat (input, "%d dot1ad", &id_min))
750 {
751 /* parse dot1ad config */
752 id_max = id_min;
753 template.sub.eth.flags.dot1ad = 1;
754 error = parse_vlan_sub_interfaces (input, &template);
755 if (error)
756 goto done;
757 }
758 else if (unformat (input, "%d-%d", &id_min, &id_max))
759 {
760 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400761 template.sub.eth.flags.exact_match = 1;
762 if (id_min > id_max)
763 goto id_error;
764 }
765 else if (unformat (input, "%d", &id_min))
766 {
767 id_max = id_min;
768 template.sub.eth.flags.one_tag = 1;
769 template.sub.eth.outer_vlan_id = id_min;
770 template.sub.eth.flags.exact_match = 1;
771 }
772 else
773 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700774 id_error:
775 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
776 format_unformat_error, input);
777 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400778 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
780 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400781
Dave Barachba868bb2016-08-08 09:51:21 -0400782 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
783 {
784 error =
785 clib_error_return (0,
786 "not allowed as %v belong to a BondEthernet interface",
787 hi->name);
788 goto done;
789 }
John Lobcebbb92016-04-05 15:47:43 -0400790
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791 for (id = id_min; id <= id_max; id++)
792 {
Dave Barachba868bb2016-08-08 09:51:21 -0400793 uword *p;
794 vnet_interface_main_t *im = &vnm->interface_main;
795 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
796 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797
798 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
799 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400800 {
801 if (CLIB_DEBUG > 0)
802 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
803 hi->sw_if_index, id);
804 continue;
805 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200808 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809 template.sup_sw_if_index = hi->sw_if_index;
810 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300811 if (id_min < id_max)
812 template.sub.eth.outer_vlan_id = id;
813
Ed Warnickecb9cada2015-12-08 15:45:58 -0700814 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400815 if (error)
816 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400817
Jon Loeligerb22e1f02019-12-19 09:03:52 -0600818 kp = clib_mem_alloc (sizeof (*kp));
819 *kp = sup_and_sub_key;
820
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
822 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400823 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
824 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 }
826
Dave Barachba868bb2016-08-08 09:51:21 -0400827done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828 return error;
829}
830
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700831/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400832 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
833 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
834 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
835 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
836 * but this is not recommended.
837 *
838 * This command has several variations:
839 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
840 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
841 *
842 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
843 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
844 * match any other subinterfaces should be sent to this subinterface.
845 *
846 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
847 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
848 * to this subinterface.
849 *
850 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
851 * subinterfaces to handle a range of VLAN IDs.
852 *
853 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700854 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
Billy McFalle9bac692017-08-11 14:05:11 -0400855 * VLAN ID different from the '<em>subId</em>'.
856 *
857 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
858 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700859 * the inner VLAN ID.
Billy McFalle9bac692017-08-11 14:05:11 -0400860 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700861 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
Billy McFalle9bac692017-08-11 14:05:11 -0400862 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
863 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
864 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
865 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
866 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
867 * entered, then the default behavior is exact-match.
868 *
869 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700870 *
871 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400872 * @parblock
873 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
874 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700875 *
Billy McFalle9bac692017-08-11 14:05:11 -0400876 * The previous example is shorthand and is equivalent to:
877 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
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 * Example of how to create a subinterface number that is different from the VLAN ID:
881 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700882 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700883 *
Billy McFalle9bac692017-08-11 14:05:11 -0400884 * Examples of how to create q-in-q and q-in-any subinterfaces:
885 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
886 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700887 *
Billy McFalle9bac692017-08-11 14:05:11 -0400888 * Examples of how to create dot1ad interfaces:
889 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
890 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700891 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700892 *
Billy McFalle9bac692017-08-11 14:05:11 -0400893 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
894 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
895 * is non-exact match:
896 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700897 *
Billy McFalle9bac692017-08-11 14:05:11 -0400898 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
899 * is specified and only one VLAN is configured, but packet contains two VLANs:
900 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700901 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700902 *
Billy McFalle9bac692017-08-11 14:05:11 -0400903 * Example of how to created a subinterface to process untagged packets:
904 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
905 *
906 * Example of how to created a subinterface to process any packet with a VLAN ID that
907 * does not match any other subinterface:
908 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
909 *
910 * When subinterfaces are created, they are in the down state. Example of how to
911 * enable a newly created subinterface:
912 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
913 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700914 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400915/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700917 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400918 .short_help = "create sub-interfaces <interface> "
919 "{<subId> [default|untagged]} | "
920 "{<subId>-<subId>} | "
921 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 .function = create_sub_interfaces,
923};
Dave Barachba868bb2016-08-08 09:51:21 -0400924/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925
926static clib_error_t *
927set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400928 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929{
Dave Barachba868bb2016-08-08 09:51:21 -0400930 vnet_main_t *vnm = vnet_get_main ();
931 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700932 u32 sw_if_index, flags;
933
934 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400935 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 {
937 error = clib_error_return (0, "unknown interface `%U'",
938 format_unformat_error, input);
939 goto done;
940 }
941
Dave Barachba868bb2016-08-08 09:51:21 -0400942 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943 {
944 error = clib_error_return (0, "unknown flags `%U'",
945 format_unformat_error, input);
946 goto done;
947 }
948
949 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
950 if (error)
951 goto done;
952
Dave Barachba868bb2016-08-08 09:51:21 -0400953done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 return error;
955}
956
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700957/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400958 * This command is used to change the admin state (up/down) of an interface.
959 *
960 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
961 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
962 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
963 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700964 *
965 * @cliexpar
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +0200966 * Example of how to configure the admin state of an interface to
967 '<em>up</em>':
Billy McFalle9bac692017-08-11 14:05:11 -0400968 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +0200969 * Example of how to configure the admin state of an interface to
970 '<em>down</em>':
Billy McFalle9bac692017-08-11 14:05:11 -0400971 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700972 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400973/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974VLIB_CLI_COMMAND (set_state_command, static) = {
975 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400976 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700977 .function = set_state,
978};
Dave Barachba868bb2016-08-08 09:51:21 -0400979/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980
981static clib_error_t *
982set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400983 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700984{
Dave Barachba868bb2016-08-08 09:51:21 -0400985 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700986 u32 unnumbered_sw_if_index = ~0;
987 u32 inherit_from_sw_if_index = ~0;
988 int enable = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700989
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700990 if (unformat (input, "%U use %U",
991 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
992 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700993 enable = 1;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700994 else if (unformat (input, "del %U",
995 unformat_vnet_sw_interface, vnm,
996 &unnumbered_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700997 enable = 0;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700998 else
999 return clib_error_return (0, "parse error '%U'",
1000 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001
Neale Ranns2ae2bc52018-03-16 03:22:39 -07001002 if (~0 == unnumbered_sw_if_index)
1003 return clib_error_return (0, "Specify the unnumbered interface");
1004 if (enable && ~0 == inherit_from_sw_if_index)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -07001005 return clib_error_return (0, "When enabling unnumbered specify the"
Neale Ranns2ae2bc52018-03-16 03:22:39 -07001006 " IP enabled interface that it uses");
Neale Ranns898273f2017-03-18 02:57:38 -07001007
Stanislav Zaikin328b5da2021-07-15 16:27:29 +02001008 int rv = vnet_sw_interface_update_unnumbered (
1009 unnumbered_sw_if_index, inherit_from_sw_if_index, enable);
1010
1011 switch (rv)
1012 {
1013 case 0:
1014 break;
1015
1016 case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
1017 return clib_error_return (
1018 0,
1019 "When enabling unnumbered both interfaces must be in the same tables");
1020
1021 default:
1022 return clib_error_return (
1023 0, "vnet_sw_interface_update_unnumbered returned %d", rv);
1024 }
Neale Ranns898273f2017-03-18 02:57:38 -07001025
Neale Ranns2ae2bc52018-03-16 03:22:39 -07001026 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027}
1028
Dave Barachba868bb2016-08-08 09:51:21 -04001029/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
1031 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -04001032 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 .function = set_unnumbered,
1034};
Dave Barachba868bb2016-08-08 09:51:21 -04001035/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
1037
1038
1039static clib_error_t *
1040set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001041 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042{
Dave Barachba868bb2016-08-08 09:51:21 -04001043 vnet_main_t *vnm = vnet_get_main ();
1044 vnet_interface_main_t *im = &vnm->interface_main;
1045 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 u32 hw_if_index, hw_class_index;
1047
1048 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001049 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050 {
1051 error = clib_error_return (0, "unknown hardware interface `%U'",
1052 format_unformat_error, input);
1053 goto done;
1054 }
1055
Dave Barachba868bb2016-08-08 09:51:21 -04001056 if (!unformat_user (input, unformat_hash_string,
1057 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 {
1059 error = clib_error_return (0, "unknown hardware class `%U'",
1060 format_unformat_error, input);
1061 goto done;
1062 }
1063
1064 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1065 if (error)
1066 goto done;
1067
Dave Barachba868bb2016-08-08 09:51:21 -04001068done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069 return error;
1070}
1071
Dave Barachba868bb2016-08-08 09:51:21 -04001072/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1074 .path = "set interface hw-class",
1075 .short_help = "Set interface hardware class",
1076 .function = set_hw_class,
1077};
Dave Barachba868bb2016-08-08 09:51:21 -04001078/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079
Dave Barachba868bb2016-08-08 09:51:21 -04001080static clib_error_t *
1081vnet_interface_cli_init (vlib_main_t * vm)
1082{
1083 return 0;
1084}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001085
1086VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1087
Dave Barachba868bb2016-08-08 09:51:21 -04001088static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001089renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001090 unformat_input_t * input,
1091 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092{
1093 u32 hw_if_index;
1094 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001095 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096 int rv;
1097
Dave Barachba868bb2016-08-08 09:51:21 -04001098 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001100 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101
Dave Barachba868bb2016-08-08 09:51:21 -04001102 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 return clib_error_return (0, "new dev instance missing");
1104
1105 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1106
1107 switch (rv)
1108 {
1109 case 0:
1110 break;
1111
1112 default:
1113 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001114 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001115
1116 }
1117
1118 return 0;
1119}
1120
1121
Dave Barachba868bb2016-08-08 09:51:21 -04001122/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001123VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1124 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001125 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126 .function = renumber_interface_command_fn,
1127};
Dave Barachba868bb2016-08-08 09:51:21 -04001128/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129
Damjan Marion8358ff92016-04-15 14:26:00 +02001130static clib_error_t *
1131promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001132 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001133{
Dave Barachba868bb2016-08-08 09:51:21 -04001134 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001135 u32 hw_if_index;
1136 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001137 ethernet_main_t *em = &ethernet_main;
1138 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001139
1140 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001141 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001142 ;
1143 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001144 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001145 flags = 0;
1146 else
1147 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001148 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001149
1150 eif = ethernet_get_interface (em, hw_if_index);
1151 if (!eif)
1152 return clib_error_return (0, "not supported");
1153
1154 ethernet_set_flags (vnm, hw_if_index, flags);
1155 return 0;
1156}
1157
Dave Barachba868bb2016-08-08 09:51:21 -04001158/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001159VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1160 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001161 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001162 .function = promiscuous_cmd,
1163};
Dave Barachba868bb2016-08-08 09:51:21 -04001164/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001165
1166static clib_error_t *
1167mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1168{
Dave Barachba868bb2016-08-08 09:51:21 -04001169 vnet_main_t *vnm = vnet_get_main ();
Ole Troand7231612018-06-07 10:17:57 +02001170 u32 hw_if_index, sw_if_index, mtu;
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001171 ethernet_main_t *em = &ethernet_main;
Ole Troand7231612018-06-07 10:17:57 +02001172 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
Damjan Marion81bb6fc2022-01-16 22:47:55 +01001173 clib_error_t *err;
Damjan Marion8358ff92016-04-15 14:26:00 +02001174
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001175 if (unformat (input, "%d %U", &mtu,
1176 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001177 {
Ole Troand7231612018-06-07 10:17:57 +02001178 /*
1179 * Change physical MTU on interface. Only supported for Ethernet
1180 * interfaces
1181 */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001182 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1183
1184 if (!eif)
1185 return clib_error_return (0, "not supported");
1186
Damjan Marion81bb6fc2022-01-16 22:47:55 +01001187 err = vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
1188 if (err)
1189 return err;
Ole Troand7231612018-06-07 10:17:57 +02001190 goto done;
Damjan Marion8358ff92016-04-15 14:26:00 +02001191 }
Ole Troand7231612018-06-07 10:17:57 +02001192 else if (unformat (input, "packet %d %U", &mtu,
1193 unformat_vnet_sw_interface, vnm, &sw_if_index))
1194 /* Set default packet MTU (including L3 header */
1195 mtus[VNET_MTU_L3] = mtu;
1196 else if (unformat (input, "ip4 %d %U", &mtu,
1197 unformat_vnet_sw_interface, vnm, &sw_if_index))
1198 mtus[VNET_MTU_IP4] = mtu;
1199 else if (unformat (input, "ip6 %d %U", &mtu,
1200 unformat_vnet_sw_interface, vnm, &sw_if_index))
1201 mtus[VNET_MTU_IP6] = mtu;
1202 else if (unformat (input, "mpls %d %U", &mtu,
1203 unformat_vnet_sw_interface, vnm, &sw_if_index))
1204 mtus[VNET_MTU_MPLS] = mtu;
Damjan Marion8358ff92016-04-15 14:26:00 +02001205 else
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001206 return clib_error_return (0, "unknown input `%U'",
1207 format_unformat_error, input);
Ole Troand7231612018-06-07 10:17:57 +02001208
1209 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1210
1211done:
Damjan Marion8358ff92016-04-15 14:26:00 +02001212 return 0;
1213}
1214
Dave Barachba868bb2016-08-08 09:51:21 -04001215/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001216VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1217 .path = "set interface mtu",
Ole Troand7231612018-06-07 10:17:57 +02001218 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001219 .function = mtu_cmd,
1220};
Dave Barachba868bb2016-08-08 09:51:21 -04001221/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001222
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001223static clib_error_t *
Matthew Smithe0792fd2019-07-12 11:48:24 -05001224show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1225 vlib_cli_command_t * cmd)
1226{
1227 vnet_main_t *vnm = vnet_get_main ();
1228 vnet_interface_main_t *im = &vnm->interface_main;
1229 ethernet_main_t *em = &ethernet_main;
1230 u32 sw_if_index = ~0;
1231 vnet_sw_interface_t *si, *sorted_sis = 0;
1232
1233 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1234 {
1235 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1236 vec_add1 (sorted_sis, si[0]);
1237 }
1238
1239 /* if an interface name was not passed, get all interfaces */
1240 if (vec_len (sorted_sis) == 0)
1241 {
1242 sorted_sis =
1243 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Damjan Marion8bea5892022-04-04 22:40:45 +02001244 vec_set_len (sorted_sis, 0);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001245 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001246 pool_foreach (si, im->sw_interfaces)
1247 {
Matthew Smithe0792fd2019-07-12 11:48:24 -05001248 int visible = vnet_swif_is_api_visible (si);
1249 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +01001250 vec_add1 (sorted_sis, si[0]);
1251 }
Matthew Smithe0792fd2019-07-12 11:48:24 -05001252 /* *INDENT-ON* */
1253 /* Sort by name. */
1254 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1255 }
1256
1257 vec_foreach (si, sorted_sis)
1258 {
1259 vnet_sw_interface_t *sup_si;
1260 ethernet_interface_t *ei;
1261
1262 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1263 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1264
1265 vlib_cli_output (vm, "%U (%s):",
1266 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1267 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1268 "up" : "dn");
1269
1270 if (ei && ei->secondary_addrs)
1271 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001272 ethernet_interface_address_t *sec_addr;
Matthew Smithe0792fd2019-07-12 11:48:24 -05001273
1274 vec_foreach (sec_addr, ei->secondary_addrs)
1275 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001276 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001277 }
1278 }
1279 }
1280
1281 vec_free (sorted_sis);
1282 return 0;
1283}
1284
1285/*?
1286 * This command is used to display interface secondary mac addresses.
1287 *
1288 * @cliexpar
1289 * Example of how to display interface secondary mac addresses:
1290 * @cliexstart{show interface secondary-mac-address}
1291 * @cliexend
1292?*/
1293/* *INDENT-OFF* */
1294VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1295 .path = "show interface secondary-mac-address",
1296 .short_help = "show interface secondary-mac-address [<interface>]",
1297 .function = show_interface_sec_mac_addr_fn,
1298};
1299/* *INDENT-ON* */
1300
1301static clib_error_t *
1302interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1303 vlib_cli_command_t * cmd)
1304{
1305 vnet_main_t *vnm = vnet_get_main ();
1306 vnet_sw_interface_t *si = NULL;
1307 clib_error_t *error = 0;
1308 u32 sw_if_index = ~0;
1309 u8 mac[6] = { 0 };
1310 u8 is_add, is_del;
1311
1312 is_add = is_del = 0;
1313
1314 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1315 {
1316 error = clib_error_return (0, "unknown interface `%U'",
1317 format_unformat_error, input);
1318 goto done;
1319 }
1320 if (!unformat_user (input, unformat_ethernet_address, mac))
1321 {
1322 error = clib_error_return (0, "expected mac address `%U'",
1323 format_unformat_error, input);
1324 goto done;
1325 }
1326
1327 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1328 {
1329 if (unformat (input, "add"))
1330 is_add = 1;
1331 else if (unformat (input, "del"))
1332 is_del = 1;
1333 else
1334 break;
1335 }
1336
1337 if (is_add == is_del)
1338 {
1339 error = clib_error_return (0, "must choose one of add or del");
1340 goto done;
1341 }
1342
1343 si = vnet_get_sw_interface (vnm, sw_if_index);
1344 error =
1345 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1346
1347done:
1348 return error;
1349}
1350
1351/*?
1352 * The '<em>set interface secondary-mac-address </em>' command allows adding
1353 * or deleting extra MAC addresses on a given interface without changing the
1354 * default MAC address. This could allow packets sent to these MAC addresses
1355 * to be received without setting the interface to promiscuous mode.
1356 * Not all interfaces support this operation. The ones that do are mostly
1357 * hardware NICs, though virtio does also.
1358 *
1359 * @cliexpar
1360 * @parblock
1361 * Example of how to add a secondary MAC Address on an interface:
1362 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1363 * Example of how to delete a secondary MAC address from an interface:
1364 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1365 * @endparblock
1366?*/
1367/* *INDENT-OFF* */
1368VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1369 .path = "set interface secondary-mac-address",
1370 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1371 .function = interface_add_del_mac_address,
1372};
1373/* *INDENT-ON* */
1374
1375static clib_error_t *
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001376set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1377 vlib_cli_command_t * cmd)
1378{
1379 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001380 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001381 clib_error_t *error = 0;
1382 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001383 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001384
1385 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1386 {
1387 error = clib_error_return (0, "unknown interface `%U'",
1388 format_unformat_error, input);
1389 goto done;
1390 }
John Lo62fcc0a2017-10-31 14:31:10 -04001391 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001392 {
1393 error = clib_error_return (0, "expected mac address `%U'",
1394 format_unformat_error, input);
1395 goto done;
1396 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001397 si = vnet_get_sw_interface (vnm, sw_if_index);
1398 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001399done:
1400 return error;
1401}
1402
1403/*?
1404 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1405 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1406 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1407 *
1408 * @cliexpar
1409 * @parblock
1410 * Example of how to change MAC Address of interface:
1411 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1412 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1413 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1414 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1415 * @endparblock
1416?*/
1417/* *INDENT-OFF* */
1418VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1419 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001420 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001421 .function = set_interface_mac_address,
1422};
1423/* *INDENT-ON* */
1424
Dave Barach7be864a2016-11-28 11:41:35 -05001425static clib_error_t *
1426set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1427{
1428 vnet_main_t *vnm = vnet_get_main ();
1429 u32 sw_if_index = ~0;
1430 u8 *tag = 0;
1431
1432 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1433 vnm, &sw_if_index, &tag))
1434 return clib_error_return (0, "unknown input `%U'",
1435 format_unformat_error, input);
1436
1437 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1438
1439 return 0;
1440}
1441
1442/* *INDENT-OFF* */
1443VLIB_CLI_COMMAND (set_tag_command, static) = {
1444 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001445 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001446 .function = set_tag,
1447};
1448/* *INDENT-ON* */
1449
1450static clib_error_t *
1451clear_tag (vlib_main_t * vm, unformat_input_t * input,
1452 vlib_cli_command_t * cmd)
1453{
1454 vnet_main_t *vnm = vnet_get_main ();
1455 u32 sw_if_index = ~0;
1456
1457 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1458 return clib_error_return (0, "unknown input `%U'",
1459 format_unformat_error, input);
1460
1461 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1462
1463 return 0;
1464}
1465
1466/* *INDENT-OFF* */
1467VLIB_CLI_COMMAND (clear_tag_command, static) = {
1468 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001469 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001470 .function = clear_tag,
1471};
1472/* *INDENT-ON* */
1473
Damjan Marion44036902017-04-28 12:29:15 +02001474static clib_error_t *
Neale Ranns1855b8e2018-07-11 10:31:26 -07001475set_ip_directed_broadcast (vlib_main_t * vm,
1476 unformat_input_t * input, vlib_cli_command_t * cmd)
1477{
1478 vnet_main_t *vnm = vnet_get_main ();
1479 u32 sw_if_index = ~0;
1480 u8 enable = 0;
1481
1482 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1483 else if (unformat (input, "enable"))
1484 enable = 1;
1485 else if (unformat (input, "disable"))
1486 enable = 0;
1487 else
1488 return clib_error_return (0, "unknown input: `%U'",
1489 format_unformat_error, input);
1490
1491 if (~0 == sw_if_index)
1492 return clib_error_return (0, "specify an interface: `%U'",
1493 format_unformat_error, input);
1494
1495 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1496
1497 return 0;
1498}
1499
1500/*?
1501 * This command is used to enable/disable IP directed broadcast
1502 * If directed broadcast is enabled a packet sent to the interface's
1503 * subnet broadcast address will be sent L2 broadcast on the interface,
1504 * otherwise it is dropped.
1505 ?*/
1506/* *INDENT-OFF* */
1507VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1508 .path = "set interface ip directed-broadcast",
1509 .short_help = "set interface enable <interface> <enable|disable>",
1510 .function = set_ip_directed_broadcast,
1511};
1512/* *INDENT-ON* */
1513
Stevenad8015b2017-10-29 22:10:46 -07001514clib_error_t *
1515set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1516 u8 queue_id_valid, u32 queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001517 vnet_hw_if_rx_mode mode)
Stevenad8015b2017-10-29 22:10:46 -07001518{
1519 clib_error_t *error = 0;
1520 vnet_hw_interface_t *hw;
Damjan Marion94100532020-11-06 23:25:57 +01001521 u32 *queue_indices = 0;
Damjan Marionb8dd9812023-11-03 13:47:05 +00001522 vnet_dev_port_t *port;
1523
1524 port = vnet_dev_get_port_from_hw_if_index (hw_if_index);
1525
1526 if (port)
1527 {
1528 vlib_main_t *vm = vlib_get_main ();
1529 vnet_dev_rv_t rv;
1530
1531 vnet_dev_port_cfg_change_req_t req = {
1532 .type = mode == VNET_HW_IF_RX_MODE_POLLING ?
1533 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE :
1534 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE,
1535 .queue_id = queue_id_valid ? queue_id : 0,
1536 .all_queues = queue_id_valid ? 0 : 1,
1537 };
1538
1539 if ((rv = vnet_dev_port_cfg_change_req_validate (vm, port, &req)))
1540 return vnet_dev_port_err (
1541 vm, port, rv, "rx queue interupt mode enable/disable not supported");
1542
1543 if ((rv = vnet_dev_process_port_cfg_change_req (vm, port, &req)))
1544 return vnet_dev_port_err (
1545 vm, port, rv,
1546 "device failed to enable/disable queue interrupt mode");
1547 return 0;
1548 }
Stevenad8015b2017-10-29 22:10:46 -07001549
1550 hw = vnet_get_hw_interface (vnm, hw_if_index);
1551
Damjan Marion94100532020-11-06 23:25:57 +01001552 if (queue_id_valid)
1553 {
1554 u32 queue_index;
1555 queue_index =
1556 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1557 if (queue_index == ~0)
1558 return clib_error_return (0, "unknown queue %u on interface %s",
1559 queue_id, hw->name);
1560 vec_add1 (queue_indices, queue_index);
Stevenad8015b2017-10-29 22:10:46 -07001561 }
1562 else
Damjan Marion94100532020-11-06 23:25:57 +01001563 queue_indices = hw->rx_queue_indices;
Stevenad8015b2017-10-29 22:10:46 -07001564
Damjan Marion94100532020-11-06 23:25:57 +01001565 for (int i = 0; i < vec_len (queue_indices); i++)
1566 {
1567 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1568 if (rv)
Wayne Morrison389aae52022-08-05 09:47:24 -04001569 {
1570 error = clib_error_return (
1571 0, "unable to set rx-mode on interface %v queue-id %u.\n",
1572 hw->name, queue_id);
1573 goto done;
1574 }
Damjan Marion94100532020-11-06 23:25:57 +01001575 }
1576
1577done:
1578 if (queue_indices != hw->rx_queue_indices)
1579 vec_free (queue_indices);
1580 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1581 return error;
Stevenad8015b2017-10-29 22:10:46 -07001582}
1583
Stevene3a395c2017-05-09 16:19:50 -07001584static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001585set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1586 vlib_cli_command_t * cmd)
1587{
1588 clib_error_t *error = 0;
1589 unformat_input_t _line_input, *line_input = &_line_input;
1590 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001591 u32 hw_if_index = (u32) ~ 0;
1592 u32 queue_id = (u32) ~ 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001593 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001594 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001595
Damjan Marion44036902017-04-28 12:29:15 +02001596 if (!unformat_user (input, unformat_line_input, line_input))
1597 return 0;
1598
1599 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1600 {
1601 if (unformat
1602 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1603 ;
1604 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001605 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001606 else if (unformat (line_input, "polling"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001607 mode = VNET_HW_IF_RX_MODE_POLLING;
Damjan Marion44036902017-04-28 12:29:15 +02001608 else if (unformat (line_input, "interrupt"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001609 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
Damjan Marion44036902017-04-28 12:29:15 +02001610 else if (unformat (line_input, "adaptive"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001611 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
Damjan Marion44036902017-04-28 12:29:15 +02001612 else
1613 {
1614 error = clib_error_return (0, "parse error: '%U'",
1615 format_unformat_error, line_input);
1616 unformat_free (line_input);
1617 return error;
1618 }
1619 }
1620
1621 unformat_free (line_input);
1622
1623 if (hw_if_index == (u32) ~ 0)
1624 return clib_error_return (0, "please specify valid interface name");
1625
Damjan Marioneabd4242020-10-07 20:59:07 +02001626 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Damjan Marion44036902017-04-28 12:29:15 +02001627 return clib_error_return (0, "please specify valid rx-mode");
1628
Stevenad8015b2017-10-29 22:10:46 -07001629 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1630 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001631
Stevene3a395c2017-05-09 16:19:50 -07001632 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001633}
1634
1635/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001636 * This command is used to assign the RX packet processing mode (polling,
1637 * interrupt, adaptive) of the a given interface, and optionally a
1638 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1639 * is applied to all queues of the interface. Not all interfaces support
1640 * all modes. To display the current rx-mode use the command
1641 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001642 *
1643 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001644 * Example of how to assign rx-mode to all queues on an interface:
1645 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1646 * Example of how to assign rx-mode to one queue of an interface:
1647 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1648 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001649 * @cliexstart{show interface rx-placement}
1650 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001651 * node dpdk-input:
1652 * GigabitEthernet7/0/0 queue 0 (polling)
1653 * node vhost-user-input:
1654 * VirtualEthernet0/0/12 queue 0 (interrupt)
1655 * VirtualEthernet0/0/12 queue 2 (polling)
1656 * VirtualEthernet0/0/13 queue 0 (polling)
1657 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001658 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001659 * node dpdk-input:
1660 * GigabitEthernet7/0/1 queue 0 (polling)
1661 * node vhost-user-input:
1662 * VirtualEthernet0/0/12 queue 1 (polling)
1663 * VirtualEthernet0/0/12 queue 3 (polling)
1664 * VirtualEthernet0/0/13 queue 1 (polling)
1665 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001666 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001667?*/
1668/* *INDENT-OFF* */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001669VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
Damjan Marion44036902017-04-28 12:29:15 +02001670 .path = "set interface rx-mode",
1671 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1672 .function = set_interface_rx_mode,
1673};
1674/* *INDENT-ON* */
1675
1676static clib_error_t *
1677show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1678 vlib_cli_command_t * cmd)
1679{
1680 u8 *s = 0;
1681 vnet_main_t *vnm = vnet_get_main ();
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001682 vnet_hw_if_rx_queue_t **all_queues = 0;
1683 vnet_hw_if_rx_queue_t **qptr;
1684 vnet_hw_if_rx_queue_t *q;
Nathan Skrzypczak455779f2021-02-15 14:48:33 +01001685 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001686 vec_add1 (all_queues, q);
1687 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1688 u32 prev_node = ~0;
Damjan Marion44036902017-04-28 12:29:15 +02001689
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001690 vec_foreach (qptr, all_queues)
1691 {
1692 u32 current_thread = qptr[0]->thread_index;
1693 u32 hw_if_index = qptr[0]->hw_if_index;
1694 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1695 u32 current_node = hw_if->input_node_index;
1696 if (current_node != prev_node)
1697 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1698 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1699 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1700 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1701 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1702 current_thread != qptr[1]->thread_index)
1703 {
1704 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1705 vlib_worker_threads[current_thread].name, s);
1706 vec_reset_length (s);
1707 }
1708 prev_node = current_node;
1709 }
Damjan Marion44036902017-04-28 12:29:15 +02001710 vec_free (s);
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001711 vec_free (all_queues);
Damjan Marion44036902017-04-28 12:29:15 +02001712 return 0;
1713}
1714
Billy McFalle9bac692017-08-11 14:05:11 -04001715/*?
1716 * This command is used to display the interface and queue worker
1717 * thread placement.
1718 *
1719 * @cliexpar
1720 * Example of how to display the interface placement:
1721 * @cliexstart{show interface rx-placement}
1722 * Thread 1 (vpp_wk_0):
1723 * node dpdk-input:
1724 * GigabitEthernet7/0/0 queue 0 (polling)
1725 * node vhost-user-input:
1726 * VirtualEthernet0/0/12 queue 0 (polling)
1727 * VirtualEthernet0/0/12 queue 2 (polling)
1728 * VirtualEthernet0/0/13 queue 0 (polling)
1729 * VirtualEthernet0/0/13 queue 2 (polling)
1730 * Thread 2 (vpp_wk_1):
1731 * node dpdk-input:
1732 * GigabitEthernet7/0/1 queue 0 (polling)
1733 * node vhost-user-input:
1734 * VirtualEthernet0/0/12 queue 1 (polling)
1735 * VirtualEthernet0/0/12 queue 3 (polling)
1736 * VirtualEthernet0/0/13 queue 1 (polling)
1737 * VirtualEthernet0/0/13 queue 3 (polling)
1738 * @cliexend
1739?*/
Damjan Marion44036902017-04-28 12:29:15 +02001740/* *INDENT-OFF* */
1741VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1742 .path = "show interface rx-placement",
1743 .short_help = "show interface rx-placement",
1744 .function = show_interface_rx_placement_fn,
1745};
1746/* *INDENT-ON* */
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001747clib_error_t *
1748set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1749 u32 thread_index, u8 is_main)
Damjan Marion44036902017-04-28 12:29:15 +02001750{
Damjan Marion44036902017-04-28 12:29:15 +02001751 vnet_main_t *vnm = vnet_get_main ();
1752 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion94100532020-11-06 23:25:57 +01001753 vnet_hw_interface_t *hw;
1754 u32 queue_index;
Damjan Marion44036902017-04-28 12:29:15 +02001755
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001756 if (is_main)
1757 thread_index = 0;
1758 else
1759 thread_index += vdm->first_worker_thread_index;
Damjan Marion44036902017-04-28 12:29:15 +02001760
1761 if (thread_index > vdm->last_worker_thread_index)
1762 return clib_error_return (0,
1763 "please specify valid worker thread or main");
1764
Damjan Marion94100532020-11-06 23:25:57 +01001765 hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001766
Damjan Marion94100532020-11-06 23:25:57 +01001767 queue_index =
1768 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1769 if (queue_index == ~0)
1770 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1771 hw->name);
1772 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1773 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1774 return 0;
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001775}
1776
1777static clib_error_t *
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001778set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1779 vlib_cli_command_t *cmd)
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001780{
1781 clib_error_t *error = 0;
1782 unformat_input_t _line_input, *line_input = &_line_input;
1783 vnet_main_t *vnm = vnet_get_main ();
1784 u32 hw_if_index = (u32) ~ 0;
1785 u32 queue_id = (u32) 0;
1786 u32 thread_index = (u32) ~ 0;
1787 u8 is_main = 0;
1788
1789 if (!unformat_user (input, unformat_line_input, line_input))
1790 return 0;
1791
1792 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1793 {
1794 if (unformat
1795 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1796 ;
1797 else if (unformat (line_input, "queue %d", &queue_id))
1798 ;
1799 else if (unformat (line_input, "main", &thread_index))
1800 is_main = 1;
1801 else if (unformat (line_input, "worker %d", &thread_index))
1802 ;
1803 else
1804 {
1805 error = clib_error_return (0, "parse error: '%U'",
1806 format_unformat_error, line_input);
1807 unformat_free (line_input);
1808 return error;
1809 }
1810 }
1811
1812 unformat_free (line_input);
1813
1814 if (hw_if_index == (u32) ~ 0)
1815 return clib_error_return (0, "please specify valid interface name");
1816
1817 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1818 is_main);
1819
1820 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001821}
1822
1823/*?
1824 * This command is used to assign a given interface, and optionally a
1825 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001826 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1827 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001828 *
1829 * @cliexpar
1830 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001831 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001832 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001833 * node dpdk-input:
1834 * GigabitEthernet7/0/0 queue 0 (polling)
1835 * node vhost-user-input:
1836 * VirtualEthernet0/0/12 queue 0 (polling)
1837 * VirtualEthernet0/0/12 queue 2 (polling)
1838 * VirtualEthernet0/0/13 queue 0 (polling)
1839 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001840 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001841 * node dpdk-input:
1842 * GigabitEthernet7/0/1 queue 0 (polling)
1843 * node vhost-user-input:
1844 * VirtualEthernet0/0/12 queue 1 (polling)
1845 * VirtualEthernet0/0/12 queue 3 (polling)
1846 * VirtualEthernet0/0/13 queue 1 (polling)
1847 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001848 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001849 * Example of how to assign a interface and queue to a worker thread:
1850 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1851 * Example of how to display the interface placement:
1852 * @cliexstart{show interface rx-placement}
1853 * Thread 1 (vpp_wk_0):
1854 * node dpdk-input:
1855 * GigabitEthernet7/0/0 queue 0 (polling)
1856 * node vhost-user-input:
1857 * VirtualEthernet0/0/12 queue 0 (polling)
1858 * VirtualEthernet0/0/12 queue 1 (polling)
1859 * VirtualEthernet0/0/12 queue 2 (polling)
1860 * VirtualEthernet0/0/13 queue 0 (polling)
1861 * VirtualEthernet0/0/13 queue 2 (polling)
1862 * Thread 2 (vpp_wk_1):
1863 * node dpdk-input:
1864 * GigabitEthernet7/0/1 queue 0 (polling)
1865 * node vhost-user-input:
1866 * VirtualEthernet0/0/12 queue 3 (polling)
1867 * VirtualEthernet0/0/13 queue 1 (polling)
1868 * VirtualEthernet0/0/13 queue 3 (polling)
1869 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001870?*/
1871/* *INDENT-OFF* */
1872VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1873 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001874 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001875 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001876 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001877 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001878};
Damjan Marion44036902017-04-28 12:29:15 +02001879/* *INDENT-ON* */
Dave Barach5ecd5a52019-02-25 15:27:28 -05001880
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001881int
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001882set_hw_interface_tx_queue (u32 hw_if_index, u32 queue_id, uword *bitmap)
1883{
1884 vnet_main_t *vnm = vnet_get_main ();
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001885 vlib_thread_main_t *vtm = vlib_get_thread_main ();
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001886 vnet_hw_if_tx_queue_t *txq;
1887 u32 queue_index;
1888 u32 thread_index;
1889
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001890 /* highest set bit in bitmap should not exceed last worker thread index */
Steven Luong8e3f1092021-06-17 08:22:50 -07001891 thread_index = clib_bitmap_last_set (bitmap);
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001892 if ((thread_index != ~0) && (thread_index >= vtm->n_vlib_mains))
1893 return VNET_API_ERROR_INVALID_VALUE;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001894
1895 queue_index =
1896 vnet_hw_if_get_tx_queue_index_by_id (vnm, hw_if_index, queue_id);
1897 if (queue_index == ~0)
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001898 return VNET_API_ERROR_INVALID_QUEUE;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001899
1900 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
1901
1902 // free the existing bitmap
1903 if (clib_bitmap_count_set_bits (txq->threads))
1904 {
1905 txq->shared_queue = 0;
1906 clib_bitmap_free (txq->threads);
1907 }
1908
1909 clib_bitmap_foreach (thread_index, bitmap)
1910 vnet_hw_if_tx_queue_assign_thread (vnm, queue_index, thread_index);
1911
1912 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1913 return 0;
1914}
1915
1916static clib_error_t *
1917set_interface_tx_queue (vlib_main_t *vm, unformat_input_t *input,
1918 vlib_cli_command_t *cmd)
1919{
1920 clib_error_t *error = 0;
1921 unformat_input_t _line_input, *line_input = &_line_input;
1922 vnet_main_t *vnm = vnet_get_main ();
1923 u32 hw_if_index = (u32) ~0;
1924 u32 queue_id = (u32) 0;
1925 uword *bitmap = 0;
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001926 int rv = 0;
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001927
1928 if (!unformat_user (input, unformat_line_input, line_input))
1929 return 0;
1930
1931 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1932 {
1933 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
1934 &hw_if_index))
1935 ;
1936 else if (unformat (line_input, "queue %d", &queue_id))
1937 ;
1938 else if (unformat (line_input, "threads %U", unformat_bitmap_list,
1939 &bitmap))
1940 ;
1941 else
1942 {
1943 error = clib_error_return (0, "parse error: '%U'",
1944 format_unformat_error, line_input);
1945 unformat_free (line_input);
1946 return error;
1947 }
1948 }
1949
1950 unformat_free (line_input);
1951
1952 if (hw_if_index == (u32) ~0)
1953 {
1954 error = clib_error_return (0, "please specify valid interface name");
1955 goto error;
1956 }
1957
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00001958 rv = set_hw_interface_tx_queue (hw_if_index, queue_id, bitmap);
1959
1960 switch (rv)
1961 {
1962 case VNET_API_ERROR_INVALID_VALUE:
1963 error = clib_error_return (
1964 0, "please specify valid thread(s) - last thread index %u",
1965 clib_bitmap_last_set (bitmap));
1966 break;
1967 case VNET_API_ERROR_INVALID_QUEUE:
1968 error = clib_error_return (
1969 0, "unknown queue %u on interface %s", queue_id,
1970 vnet_get_hw_interface (vnet_get_main (), hw_if_index)->name);
1971 break;
1972 default:
1973 break;
1974 }
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001975
1976error:
1977 clib_bitmap_free (bitmap);
1978 return (error);
1979}
1980
1981VLIB_CLI_COMMAND (cmd_set_if_tx_queue, static) = {
1982 .path = "set interface tx-queue",
1983 .short_help = "set interface tx-queue <interface> queue <n> "
Steven Luong8e3f1092021-06-17 08:22:50 -07001984 "[threads <list>]",
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001985 .function = set_interface_tx_queue,
1986 .is_mp_safe = 1,
1987};
1988
1989clib_error_t *
Chenmin Sunc4665092020-07-06 08:20:39 +08001990set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1991 clib_bitmap_t * bitmap)
1992{
1993 vnet_main_t *vnm = vnet_get_main ();
1994 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1995
1996 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1997}
1998
1999static clib_error_t *
2000set_interface_rss_queues_fn (vlib_main_t * vm,
2001 unformat_input_t * input,
2002 vlib_cli_command_t * cmd)
2003{
2004 clib_error_t *error = 0;
2005 unformat_input_t _line_input, *line_input = &_line_input;
2006 vnet_main_t *vnm = vnet_get_main ();
2007 u32 hw_if_index = (u32) ~ 0;
2008 clib_bitmap_t *bitmap = NULL;
2009
2010 if (!unformat_user (input, unformat_line_input, line_input))
2011 return 0;
2012
2013 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2014 {
2015 if (unformat
2016 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2017 ;
2018 else
2019 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
2020 ;
2021 else
2022 {
2023 error = clib_error_return (0, "parse error: '%U'",
2024 format_unformat_error, line_input);
2025 unformat_free (line_input);
2026 goto done;
2027 }
2028 }
2029
2030 unformat_free (line_input);
2031
2032 if (hw_if_index == (u32) ~ 0)
2033 {
2034 error = clib_error_return (0, "please specify valid interface name");
2035 goto done;
2036 }
2037
2038 if (bitmap == NULL)
2039 {
2040 error = clib_error_return (0, "please specify the valid rss queues");
2041 goto done;
2042 }
2043
2044 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
2045
2046done:
2047 if (bitmap)
2048 clib_bitmap_free (bitmap);
2049
2050 return (error);
2051}
2052
2053/*?
2054 * This command is used to set the rss queues of a given interface
2055 * Not all the interfaces support this operation.
2056 * To display the current rss queues, use the command
2057 * '<em>show hardware-interfaces</em>'.
2058 *
2059 * @cliexpar
2060 * Example of how to set the rss queues to 0,2-5,7 of an interface:
2061 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
2062 * @cliexend
2063?*/
2064/* *INDENT-OFF* */
2065VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
2066 .path = "set interface rss queues",
2067 .short_help = "set interface rss queues <interface> <list <queue-list>>",
2068 .function = set_interface_rss_queues_fn,
2069};
2070/* *INDENT-ON* */
2071
Dave Barach33909772019-09-23 10:27:27 -04002072static u8 *
2073format_vnet_pcap (u8 * s, va_list * args)
2074{
2075 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2076 int type = va_arg (*args, int);
2077 int printed = 0;
2078
2079 if (type == 0)
2080 {
2081 if (pp->pcap_rx_enable)
2082 {
2083 s = format (s, "rx");
2084 printed = 1;
2085 }
2086 if (pp->pcap_tx_enable)
2087 {
2088 if (printed)
2089 s = format (s, " and ");
2090 s = format (s, "tx");
2091 printed = 1;
2092 }
2093 if (pp->pcap_drop_enable)
2094 {
2095 if (printed)
2096 s = format (s, " and ");
2097 s = format (s, "drop");
2098 printed = 1;
2099 }
2100 return s;
2101 }
2102 s = format (s, "unknown type %d!", type);
2103 return s;
2104}
2105
2106
Dave Barachb97641c2019-09-09 16:38:17 -04002107int
2108vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2109{
2110 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8fb5add2021-03-04 18:41:59 +01002111 vnet_main_t *vnm = vnet_get_main ();
2112 vnet_pcap_t *pp = &vnm->pcap;
Dave Barachb97641c2019-09-09 16:38:17 -04002113 pcap_main_t *pm = &pp->pcap_main;
Dave Barachf5667c32019-09-25 11:27:46 -04002114 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachb97641c2019-09-09 16:38:17 -04002115
2116 if (a->status)
2117 {
Dave Barach33909772019-09-23 10:27:27 -04002118 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002119 {
2120 vlib_cli_output
Dave Barach33909772019-09-23 10:27:27 -04002121 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2122 format_vnet_pcap, pp, 0 /* print type */ ,
Dave Barachb97641c2019-09-09 16:38:17 -04002123 pm->n_packets_captured, pm->n_packets_to_capture);
2124 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2125 }
2126 else
Dave Barach33909772019-09-23 10:27:27 -04002127 vlib_cli_output (vm, "pcap dispatch capture disabled");
2128
Dave Barachb97641c2019-09-09 16:38:17 -04002129 return 0;
2130 }
2131
2132 /* Consistency checks */
2133
2134 /* Enable w/ capture already enabled not allowed */
Dave Barach33909772019-09-23 10:27:27 -04002135 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2136 && (a->rx_enable + a->tx_enable + a->drop_enable))
Dave Barachb97641c2019-09-09 16:38:17 -04002137 return VNET_API_ERROR_INVALID_VALUE;
2138
2139 /* Disable capture with capture already disabled, not interesting */
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01002140 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
2141 0) &&
2142 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
Dave Barachb97641c2019-09-09 16:38:17 -04002143 return VNET_API_ERROR_VALUE_EXIST;
2144
2145 /* Change number of packets to capture while capturing */
Dave Barach33909772019-09-23 10:27:27 -04002146 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2147 && (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002148 && (pm->n_packets_to_capture != a->packets_to_capture))
2149 return VNET_API_ERROR_INVALID_VALUE_2;
2150
Dave Barach33909772019-09-23 10:27:27 -04002151 /* Classify filter specified, but no classify filter configured */
Dave Barachf5667c32019-09-25 11:27:46 -04002152 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
Benoît Ganne5943e362021-02-26 14:46:58 +01002153 (!cm->classify_table_index_by_sw_if_index ||
2154 cm->classify_table_index_by_sw_if_index[0] == ~0))
Dave Barach9137e542019-09-13 17:47:50 -04002155 return VNET_API_ERROR_NO_SUCH_LABEL;
2156
Dave Barach33909772019-09-23 10:27:27 -04002157 if (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002158 {
Dave Barach586462f2020-08-05 16:12:35 -04002159 void *save_pcap_data;
2160
Dave Barach33909772019-09-23 10:27:27 -04002161 /* Sanity check max bytes per pkt */
2162 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2163 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2164
Dave Barachb97641c2019-09-09 16:38:17 -04002165 /* Clean up from previous run, if any */
Dave Barach586462f2020-08-05 16:12:35 -04002166 vec_reset_length (pm->pcap_data);
2167
2168 /* Throw away the data buffer? */
2169 if (a->free_data)
2170 vec_free (pm->pcap_data);
2171
2172 save_pcap_data = pm->pcap_data;
2173
Dave Barachb97641c2019-09-09 16:38:17 -04002174 memset (pm, 0, sizeof (*pm));
2175
Dave Barach586462f2020-08-05 16:12:35 -04002176 pm->pcap_data = save_pcap_data;
2177
Dave Barach11fb09e2020-08-06 12:10:09 -04002178 vec_validate_aligned (vnet_trace_placeholder, 2048,
2179 CLIB_CACHE_LINE_BYTES);
Dave Barachb97641c2019-09-09 16:38:17 -04002180 if (pm->lock == 0)
2181 clib_spinlock_init (&(pm->lock));
2182
2183 if (a->filename == 0)
Dave Barach33909772019-09-23 10:27:27 -04002184 {
2185 u8 *stem = 0;
2186
2187 if (a->rx_enable)
2188 stem = format (stem, "rx");
2189 if (a->tx_enable)
2190 stem = format (stem, "tx");
2191 if (a->drop_enable)
2192 stem = format (stem, "drop");
Benoît Ganne7d4a9972020-07-17 11:49:56 +02002193 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
Dave Barach33909772019-09-23 10:27:27 -04002194 vec_free (stem);
2195 }
Dave Barachb97641c2019-09-09 16:38:17 -04002196
2197 pm->file_name = (char *) a->filename;
2198 pm->n_packets_captured = 0;
2199 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
Dave Barach586462f2020-08-05 16:12:35 -04002200 /* Preallocate the data vector? */
2201 if (a->preallocate_data)
2202 {
2203 vec_validate
2204 (pm->pcap_data, a->packets_to_capture
2205 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2206 vec_reset_length (pm->pcap_data);
2207 }
Dave Barachb97641c2019-09-09 16:38:17 -04002208 pm->n_packets_to_capture = a->packets_to_capture;
2209 pp->pcap_sw_if_index = a->sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002210 if (a->filter)
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002211 pp->filter_classify_table_index =
2212 cm->classify_table_index_by_sw_if_index[0];
Dave Barach9137e542019-09-13 17:47:50 -04002213 else
2214 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002215 pp->pcap_error_index = a->drop_err;
Dave Barach33909772019-09-23 10:27:27 -04002216 pp->pcap_rx_enable = a->rx_enable;
2217 pp->pcap_tx_enable = a->tx_enable;
2218 pp->pcap_drop_enable = a->drop_enable;
2219 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
Dave Barachb97641c2019-09-09 16:38:17 -04002220 }
2221 else
2222 {
Dave Barach33909772019-09-23 10:27:27 -04002223 pp->pcap_rx_enable = 0;
2224 pp->pcap_tx_enable = 0;
2225 pp->pcap_drop_enable = 0;
Dave Barachf5667c32019-09-25 11:27:46 -04002226 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002227 pp->pcap_error_index = ~0;
Dave Barachb97641c2019-09-09 16:38:17 -04002228 if (pm->n_packets_captured)
2229 {
2230 clib_error_t *error;
2231 pm->n_packets_to_capture = pm->n_packets_captured;
2232 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2233 pm->n_packets_captured, pm->file_name);
2234 error = pcap_write (pm);
Andrew Yourtchenko4da15062019-09-11 14:14:43 +00002235 if (pm->flags & PCAP_MAIN_INIT_DONE)
Dave Barachb97641c2019-09-09 16:38:17 -04002236 pcap_close (pm);
2237 /* Report I/O errors... */
2238 if (error)
2239 {
2240 clib_error_report (error);
2241 return VNET_API_ERROR_SYSCALL_ERROR_1;
2242 }
Dave Barach586462f2020-08-05 16:12:35 -04002243 vec_free (pm->file_name);
2244 if (a->free_data)
2245 vec_free (pm->pcap_data);
Dave Barachb97641c2019-09-09 16:38:17 -04002246 return 0;
2247 }
2248 else
2249 return VNET_API_ERROR_NO_SUCH_ENTRY;
2250 }
2251
2252 return 0;
2253}
2254
2255static clib_error_t *
Dave Barach33909772019-09-23 10:27:27 -04002256pcap_trace_command_fn (vlib_main_t * vm,
2257 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002258{
Dave Barach5ecd5a52019-02-25 15:27:28 -05002259 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb97641c2019-09-09 16:38:17 -04002260 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002261 vnet_main_t *vnm = vnet_get_main ();
Dave Barachb97641c2019-09-09 16:38:17 -04002262 u8 *filename = 0;
2263 u32 max = 1000;
Dave Barach33909772019-09-23 10:27:27 -04002264 u32 max_bytes_per_pkt = 512;
Dave Barachb97641c2019-09-09 16:38:17 -04002265 int rv;
Dave Barach33909772019-09-23 10:27:27 -04002266 int rx_enable = 0;
2267 int tx_enable = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002268 int preallocate_data = 0;
Dave Barach33909772019-09-23 10:27:27 -04002269 int drop_enable = 0;
Dave Barachb97641c2019-09-09 16:38:17 -04002270 int status = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002271 int filter = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002272 int free_data = 0;
Dave Barach4330c462020-06-10 17:07:32 -04002273 u32 sw_if_index = 0; /* default: any interface */
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002274 vlib_error_t drop_err = ~0; /* default: any error */
Dave Barach5ecd5a52019-02-25 15:27:28 -05002275
2276 /* Get a line of input. */
2277 if (!unformat_user (input, unformat_line_input, line_input))
2278 return 0;
2279
2280 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2281 {
Dave Barach33909772019-09-23 10:27:27 -04002282 if (unformat (line_input, "rx"))
2283 rx_enable = 1;
2284 else if (unformat (line_input, "tx"))
2285 tx_enable = 1;
2286 else if (unformat (line_input, "drop"))
2287 drop_enable = 1;
2288 else if (unformat (line_input, "off"))
2289 rx_enable = tx_enable = drop_enable = 0;
2290 else if (unformat (line_input, "max-bytes-per-pkt %u",
2291 &max_bytes_per_pkt))
Dave Barachb97641c2019-09-09 16:38:17 -04002292 ;
2293 else if (unformat (line_input, "max %d", &max))
2294 ;
2295 else if (unformat (line_input, "packets-to-capture %d", &max))
2296 ;
2297 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2298 &filename))
2299 ;
2300 else if (unformat (line_input, "status %=", &status, 1))
2301 ;
2302 else if (unformat (line_input, "intfc %U",
2303 unformat_vnet_sw_interface, vnm, &sw_if_index))
2304 ;
Dave Barach586462f2020-08-05 16:12:35 -04002305 else if (unformat (line_input, "interface %U",
2306 unformat_vnet_sw_interface, vnm, &sw_if_index))
2307 ;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002308 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2309 &drop_err))
2310 ;
Dave Barach586462f2020-08-05 16:12:35 -04002311 else if (unformat (line_input, "preallocate-data %=",
2312 &preallocate_data, 1))
2313 ;
2314 else if (unformat (line_input, "free-data %=", &free_data, 1))
2315 ;
2316 else if (unformat (line_input, "intfc any")
2317 || unformat (line_input, "interface any"))
Dave Barachb97641c2019-09-09 16:38:17 -04002318 sw_if_index = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002319 else if (unformat (line_input, "filter"))
2320 filter = 1;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002321 else
2322 {
Dave Barachb97641c2019-09-09 16:38:17 -04002323 return clib_error_return (0, "unknown input `%U'",
2324 format_unformat_error, line_input);
Dave Barach5ecd5a52019-02-25 15:27:28 -05002325 }
2326 }
Dave Barachb97641c2019-09-09 16:38:17 -04002327
Dave Barach5ecd5a52019-02-25 15:27:28 -05002328 unformat_free (line_input);
2329
Dave Barachb97641c2019-09-09 16:38:17 -04002330 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2331 a->filename = filename;
Dave Barach33909772019-09-23 10:27:27 -04002332 a->rx_enable = rx_enable;
2333 a->tx_enable = tx_enable;
Dave Barach586462f2020-08-05 16:12:35 -04002334 a->preallocate_data = preallocate_data;
2335 a->free_data = free_data;
Dave Barach33909772019-09-23 10:27:27 -04002336 a->drop_enable = drop_enable;
Dave Barachb97641c2019-09-09 16:38:17 -04002337 a->status = status;
2338 a->packets_to_capture = max;
Dave Barachb97641c2019-09-09 16:38:17 -04002339 a->sw_if_index = sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002340 a->filter = filter;
Dave Barach33909772019-09-23 10:27:27 -04002341 a->max_bytes_per_pkt = max_bytes_per_pkt;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002342 a->drop_err = drop_err;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002343
Dave Barachb97641c2019-09-09 16:38:17 -04002344 rv = vnet_pcap_dispatch_trace_configure (a);
2345
2346 switch (rv)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002347 {
Dave Barachb97641c2019-09-09 16:38:17 -04002348 case 0:
2349 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002350
Dave Barachb97641c2019-09-09 16:38:17 -04002351 case VNET_API_ERROR_INVALID_VALUE:
2352 return clib_error_return (0, "dispatch trace already enabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002353
Dave Barachb97641c2019-09-09 16:38:17 -04002354 case VNET_API_ERROR_VALUE_EXIST:
2355 return clib_error_return (0, "dispatch trace already disabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002356
Dave Barachb97641c2019-09-09 16:38:17 -04002357 case VNET_API_ERROR_INVALID_VALUE_2:
2358 return clib_error_return
2359 (0, "can't change number of records to capture while tracing...");
2360
2361 case VNET_API_ERROR_SYSCALL_ERROR_1:
2362 return clib_error_return (0, "I/O writing trace capture...");
2363
2364 case VNET_API_ERROR_NO_SUCH_ENTRY:
2365 return clib_error_return (0, "No packets captured...");
2366
Dave Barach33909772019-09-23 10:27:27 -04002367 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2368 return clib_error_return (0,
2369 "Max bytes per pkt must be > 32, < 9000...");
2370
Dave Barach9137e542019-09-13 17:47:50 -04002371 case VNET_API_ERROR_NO_SUCH_LABEL:
2372 return clib_error_return
2373 (0, "No classify filter configured, see 'classify filter...'");
2374
Dave Barachb97641c2019-09-09 16:38:17 -04002375 default:
2376 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2377 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002378 }
Dave Barachb97641c2019-09-09 16:38:17 -04002379 return 0;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002380}
2381
Dave Barach5ecd5a52019-02-25 15:27:28 -05002382/*?
2383 * This command is used to start or stop a packet capture, or show
Dave Barach33909772019-09-23 10:27:27 -04002384 * the status of packet capture.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002385 *
2386 * This command has the following optional parameters:
2387 *
Dave Barach33909772019-09-23 10:27:27 -04002388 *
2389 * - <b>rx</b> - Capture received packets
2390 *
2391 * - <b>tx</b> - Capture transmitted packets
2392 *
2393 * - <b>drop</b> - Capture dropped packets
2394 *
2395 * - <b>off</b> - Stop capturing packets, write results to the specified file
Dave Barach5ecd5a52019-02-25 15:27:28 -05002396 *
2397 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2398 * of packets have been received, buffer is flushed to file. Once another
2399 * '<em>nn</em>' number of packets have been received, buffer is flushed
2400 * to file, overwriting previous write. If not entered, value defaults
2401 * to 100. Can only be updated if packet capture is off.
2402 *
Dave Barach33909772019-09-23 10:27:27 -04002403 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2404 * for each packet. Must be >= 32, <= 9000.
2405 *
Dave Barach586462f2020-08-05 16:12:35 -04002406 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2407 * vector expansion delays during pcap capture
2408 *
2409 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2410 * to retain the data buffer so this option is seldom used.
2411 *
Dave Barach33909772019-09-23 10:27:27 -04002412 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002413 * or use '<em>any</em>' to run packet capture on all interfaces.
2414 * '<em>any</em>' is the default if not provided. Settings from a previous
2415 * packet capture are preserved, so '<em>any</em>' can be used to reset
2416 * the interface setting.
2417 *
Nobuhiro MIKId346f392023-02-28 18:30:09 +09002418 * - <b>filter</b> - Use the pcap trace rx / tx / drop filter, which
Dave Barachf5667c32019-09-25 11:27:46 -04002419 * must be configured. Use <b>classify filter pcap...</b> to configure the
2420 * filter. The filter will only be executed if the per-interface or
2421 * any-interface tests fail.
2422 *
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002423 * - <b>error <node>.<error></b> - filter packets based on a specific error.
Maxime Peimddc16cf2023-01-13 08:04:55 +00002424 * For example: error {ip4-udp-lookup}.{no_listener}
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002425 *
Dave Barach5ecd5a52019-02-25 15:27:28 -05002426 * - <b>file <name></b> - Used to specify the output filename. The file will
2427 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2428 * supported. Directory should not be entered. If file already exists, file
Dave Barach33909772019-09-23 10:27:27 -04002429 * will be overwritten. If no filename is provided, the file will be
2430 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2431 * Can only be updated if packet capture is off.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002432 *
2433 * - <b>status</b> - Displays the current status and configured attributes
2434 * associated with a packet capture. If packet capture is in progress,
2435 * '<em>status</em>' also will return the number of packets currently in
2436 * the local buffer. All additional attributes entered on command line
2437 * with '<em>status</em>' will be ignored and not applied.
2438 *
2439 * @cliexpar
2440 * Example of how to display the status of a tx packet capture when off:
Dave Barach33909772019-09-23 10:27:27 -04002441 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002442 * max is 100, for any interface to file /tmp/vpe.pcap
2443 * pcap tx capture is off...
2444 * @cliexend
2445 * Example of how to start a tx packet capture:
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002446 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
Nathan Skrzypczak2c77ae42021-09-29 15:36:51 +02002447 * vppTest.pcap}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002448 * @cliexend
2449 * Example of how to display the status of a tx packet capture in progress:
Dave Barach33909772019-09-23 10:27:27 -04002450 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002451 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2452 * pcap tx capture is on: 20 of 35 pkts...
2453 * @cliexend
2454 * Example of how to stop a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002455 * @cliexstart{pcap trace off}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002456 * captured 21 pkts...
2457 * saved to /tmp/vppTest.pcap...
2458 * @cliexend
2459?*/
2460/* *INDENT-OFF* */
2461
2462VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
Dave Barach33909772019-09-23 10:27:27 -04002463 .path = "pcap trace",
Dave Barach5ecd5a52019-02-25 15:27:28 -05002464 .short_help =
Dave Barach586462f2020-08-05 16:12:35 -04002465 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2466 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2467 " [preallocate-data][free-data]",
Dave Barach33909772019-09-23 10:27:27 -04002468 .function = pcap_trace_command_fn,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002469};
2470/* *INDENT-ON* */
2471
Steven Luongf49734d2021-07-26 13:38:05 -07002472static clib_error_t *
Maxime Peim3f407552023-07-03 17:45:51 +02002473set_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2474 vlib_cli_command_t *cmd)
2475{
2476 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2477 unformat_input_t _line_input, *line_input = &_line_input;
2478 vlib_is_packet_traced_fn_t *res = 0;
2479 clib_error_t *error = 0;
2480
2481 if (!unformat_user (input, unformat_line_input, line_input))
2482 return 0;
2483
2484 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
2485 {
2486 if (unformat (line_input, "%U", unformat_vlib_trace_filter_function,
2487 &res))
2488 ;
2489 else
2490 {
2491 error = clib_error_create (
2492 "expected valid trace filter function, got `%U'",
2493 format_unformat_error, line_input);
2494 goto done;
2495 }
2496 }
2497 pp->current_filter_function = res;
2498
2499done:
2500 unformat_free (line_input);
2501
2502 return error;
2503}
2504
2505VLIB_CLI_COMMAND (set_pcap_filter_function_cli, static) = {
2506 .path = "set pcap filter function",
2507 .short_help = "set pcap filter function <func_name>",
2508 .function = set_pcap_filter_function,
2509};
2510
2511static clib_error_t *
2512show_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2513 vlib_cli_command_t *cmd)
2514{
2515 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2516 vlib_trace_filter_main_t *tfm = &vlib_trace_filter_main;
2517 vlib_is_packet_traced_fn_t *current_trace_filter_fn =
2518 pp->current_filter_function;
2519 vlib_trace_filter_function_registration_t *reg =
2520 tfm->trace_filter_registration;
2521
2522 while (reg)
2523 {
2524 vlib_cli_output (vm, "%sname:%s description: %s priority: %u",
2525 reg->function == current_trace_filter_fn ? "(*) " : "",
2526 reg->name, reg->description, reg->priority);
2527 reg = reg->next;
2528 }
2529 return 0;
2530}
2531
2532VLIB_CLI_COMMAND (show_pcap_filter_function_cli, static) = {
2533 .path = "show pcap filter function",
2534 .short_help = "show pcap filter function",
2535 .function = show_pcap_filter_function,
2536};
2537
2538static clib_error_t *
Steven Luongf49734d2021-07-26 13:38:05 -07002539set_interface_name (vlib_main_t *vm, unformat_input_t *input,
2540 vlib_cli_command_t *cmd)
2541{
2542 clib_error_t *error = 0;
2543 unformat_input_t _line_input, *line_input = &_line_input;
2544 vnet_main_t *vnm = vnet_get_main ();
2545 u32 hw_if_index = ~0;
2546 char *name = 0;
2547
2548 if (!unformat_user (input, unformat_line_input, line_input))
2549 return 0;
2550
2551 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2552 {
2553 if (unformat (line_input, "%U %s", unformat_vnet_hw_interface, vnm,
2554 &hw_if_index, &name))
2555 ;
2556 else
2557 {
2558 error = clib_error_return (0, "parse error: '%U'",
2559 format_unformat_error, line_input);
2560 unformat_free (line_input);
2561 vec_free (name);
2562 return error;
2563 }
2564 }
2565
2566 unformat_free (line_input);
2567
2568 if (hw_if_index == (u32) ~0 || name == 0)
2569 {
2570 vec_free (name);
2571 error = clib_error_return (0, "please specify valid interface name");
2572 return error;
2573 }
2574
2575 error = vnet_rename_interface (vnm, hw_if_index, name);
2576 vec_free (name);
2577
2578 return (error);
2579}
2580
2581VLIB_CLI_COMMAND (cmd_set_if_name, static) = {
2582 .path = "set interface name",
2583 .short_help = "set interface name <interface-name> <new-interface-name>",
2584 .function = set_interface_name,
2585 .is_mp_safe = 1,
2586};
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00002587
2588static clib_error_t *
2589set_interface_tx_hash_cmd (vlib_main_t *vm, unformat_input_t *input,
2590 vlib_cli_command_t *cmd)
2591{
2592 clib_error_t *error = 0;
2593 unformat_input_t _line_input, *line_input = &_line_input;
2594 vnet_main_t *vnm = vnet_get_main ();
2595 vnet_hw_interface_t *hi;
2596 u8 *hash_name = 0;
2597 u32 hw_if_index = (u32) ~0;
2598 vnet_hash_fn_t hf;
2599 vnet_hash_fn_type_t ftype;
2600
2601 if (!unformat_user (input, unformat_line_input, line_input))
2602 return 0;
2603
2604 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2605 {
2606 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2607 &hw_if_index))
2608 ;
2609 else if (unformat (line_input, "hash-name %s", &hash_name))
2610 ;
2611 else
2612 {
2613 error = clib_error_return (0, "parse error: '%U'",
2614 format_unformat_error, line_input);
2615 unformat_free (line_input);
2616 return error;
2617 }
2618 }
2619
2620 unformat_free (line_input);
2621
2622 if (hw_if_index == (u32) ~0)
2623 {
2624 error = clib_error_return (0, "please specify valid interface name");
2625 goto error;
2626 }
2627
Dave Barach9e0f9e22022-01-18 08:52:47 -05002628 if (hash_name == 0)
2629 {
2630 error = clib_error_return (0, "hash-name is required");
2631 goto error;
2632 }
2633
Mohsin Kazmi0d05c0d2021-11-09 17:44:10 +00002634 hi = vnet_get_hw_interface (vnm, hw_if_index);
2635 ftype =
2636 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2637 hf = vnet_hash_function_from_name ((const char *) hash_name, ftype);
2638
2639 if (!hf)
2640 {
2641 error = clib_error_return (0, "please specify valid hash name");
2642 goto error;
2643 }
2644
2645 hi->hf = hf;
2646error:
2647 vec_free (hash_name);
2648 return (error);
2649}
2650
2651VLIB_CLI_COMMAND (cmd_set_if_tx_hash, static) = {
2652 .path = "set interface tx-hash",
2653 .short_help = "set interface tx-hash <interface> hash-name <hash-name>",
2654 .function = set_interface_tx_hash_cmd,
2655};
2656
2657static clib_error_t *
2658show_tx_hash (vlib_main_t *vm, unformat_input_t *input,
2659 vlib_cli_command_t *cmd)
2660{
2661 clib_error_t *error = 0;
2662 unformat_input_t _line_input, *line_input = &_line_input;
2663 vnet_main_t *vnm = vnet_get_main ();
2664 vnet_hw_interface_t *hi;
2665 vnet_hash_function_registration_t *hash;
2666 u32 hw_if_index = (u32) ~0;
2667 vnet_hash_fn_type_t ftype;
2668
2669 if (!unformat_user (input, unformat_line_input, line_input))
2670 return 0;
2671
2672 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2673 {
2674 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2675 &hw_if_index))
2676 ;
2677 else
2678 {
2679 error = clib_error_return (0, "parse error: '%U'",
2680 format_unformat_error, line_input);
2681 unformat_free (line_input);
2682 goto error;
2683 }
2684 }
2685
2686 unformat_free (line_input);
2687
2688 if (hw_if_index == (u32) ~0)
2689 {
2690 error = clib_error_return (0, "please specify valid interface name");
2691 goto error;
2692 }
2693
2694 hi = vnet_get_hw_interface (vnm, hw_if_index);
2695 ftype =
2696 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2697
2698 if (hi->hf)
2699 {
2700 hash = vnet_hash_function_from_func (hi->hf, ftype);
2701 if (hash)
2702 vlib_cli_output (vm, "%U", format_vnet_hash, hash);
2703 else
2704 vlib_cli_output (vm, "no matching hash function found");
2705 }
2706 else
2707 vlib_cli_output (vm, "no hashing function set");
2708
2709error:
2710 return (error);
2711}
2712
2713VLIB_CLI_COMMAND (cmd_show_tx_hash, static) = {
2714 .path = "show interface tx-hash",
2715 .short_help = "show interface tx-hash [interface]",
2716 .function = show_tx_hash,
2717};
2718
Dave Barachba868bb2016-08-08 09:51:21 -04002719/*
2720 * fd.io coding-style-patch-verification: ON
2721 *
2722 * Local Variables:
2723 * eval: (c-set-style "gnu")
2724 * End:
2725 */