blob: 8475225b525b12fd6ae3200d92b50986546fb2a3 [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>
Dave Barachba868bb2016-08-08 09:51:21 -040056static int
57compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070058{
Dave Barachba868bb2016-08-08 09:51:21 -040059 u32 *hi1 = a1;
60 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
Dave Barachba868bb2016-08-08 09:51:21 -040062 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063}
64
65static clib_error_t *
66show_or_clear_hw_interfaces (vlib_main_t * vm,
67 unformat_input_t * input,
Benoît Ganne17814d72020-07-24 09:57:11 +020068 vlib_cli_command_t * cmd, int is_show)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barachba868bb2016-08-08 09:51:21 -040070 clib_error_t *error = 0;
71 vnet_main_t *vnm = vnet_get_main ();
72 vnet_interface_main_t *im = &vnm->interface_main;
73 vnet_hw_interface_t *hi;
74 u32 hw_if_index, *hw_if_indices = 0;
Benoît Ganne17814d72020-07-24 09:57:11 +020075 int i, verbose = -1, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
78 {
79 /* See if user wants to show a specific interface. */
Dave Barachba868bb2016-08-08 09:51:21 -040080 if (unformat
81 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
82 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040083
Sean Hope679ea792016-02-22 15:12:01 -050084 /* See if user wants to show an interface with a specific hw_if_index. */
85 else if (unformat (input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040086 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
88 else if (unformat (input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040089 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
91 else if (unformat (input, "detail"))
92 verbose = 2;
93
94 else if (unformat (input, "brief"))
95 verbose = 0;
96
John Lobcebbb92016-04-05 15:47:43 -040097 else if (unformat (input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -040098 {
99 show_bond = 1;
100 if (verbose < 0)
101 verbose = 0; /* default to brief for link bonding */
102 }
John Lobcebbb92016-04-05 15:47:43 -0400103
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104 else
105 {
106 error = clib_error_return (0, "unknown input `%U'",
107 format_unformat_error, input);
108 goto done;
109 }
110 }
Dave Barachba868bb2016-08-08 09:51:21 -0400111
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 /* Gather interfaces. */
113 if (vec_len (hw_if_indices) == 0)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100114 pool_foreach (hi, im->hw_interfaces)
115 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barachba868bb2016-08-08 09:51:21 -0400117 if (verbose < 0)
118 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400119
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 if (is_show)
121 {
122 /* Sort by name. */
123 vec_sort_with_function (hw_if_indices, compare_interface_names);
124
125 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
126 for (i = 0; i < vec_len (hw_if_indices); i++)
127 {
128 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400129 if (show_bond == 0) /* show all interfaces */
130 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
131 hi, verbose);
132 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400133 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400134 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400135 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400136 vnet_hw_interface_t *shi;
137 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400138 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400139
140 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100141 clib_bitmap_foreach (hw_idx, hi->bond_info)
142 {
Dave Barachba868bb2016-08-08 09:51:21 -0400143 shi = vnet_get_hw_interface(vnm, hw_idx);
144 vlib_cli_output (vm, "%U\n",
145 format_vnet_hw_interface, vnm, shi, verbose);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100146 }
Dave Barachba868bb2016-08-08 09:51:21 -0400147 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400148 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 }
150 }
151 else
152 {
153 for (i = 0; i < vec_len (hw_if_indices); i++)
154 {
Dave Barachba868bb2016-08-08 09:51:21 -0400155 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
157 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
158 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400159
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 if (dc->clear_counters)
161 dc->clear_counters (hi->dev_instance);
162 }
163 }
164
Dave Barachba868bb2016-08-08 09:51:21 -0400165done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 vec_free (hw_if_indices);
167 return error;
168}
169
Benoît Ganne17814d72020-07-24 09:57:11 +0200170static clib_error_t *
171show_hw_interfaces (vlib_main_t * vm,
172 unformat_input_t * input, vlib_cli_command_t * cmd)
173{
174 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
175}
176
177static clib_error_t *
178clear_hw_interfaces (vlib_main_t * vm,
179 unformat_input_t * input, vlib_cli_command_t * cmd)
180{
181 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
182}
183
184
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700185/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400186 * Display more detailed information about all or a list of given interfaces.
187 * The verboseness of the output can be controlled by the following optional
188 * parameters:
189 * - brief: Only show name, index and state (default for bonded interfaces).
190 * - verbose: Also display additional attributes (default for all other interfaces).
191 * - detail: Also display all remaining attributes and extended statistics.
192 *
193 * To limit the output of the command to bonded interfaces and their slave
194 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700195 *
196 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400197 * Example of how to display default data for all interfaces:
198 * @cliexstart{show hardware-interfaces}
199 * Name Idx Link Hardware
200 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
201 * Ethernet address ec:f4:bb:c0:bc:fc
202 * Intel e1000
203 * carrier up full duplex speed 1000 mtu 9216
204 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
205 * cpu socket 0
206 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
207 * Ethernet address ec:f4:bb:c0:bc:fd
208 * Intel e1000
209 * carrier up full duplex speed 1000 mtu 9216
210 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
211 * cpu socket 0
212 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
213 * Ethernet address 02:fe:a5:a9:8b:8e
214 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
215 * Ethernet address 02:fe:c0:4e:3b:b0
216 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
217 * Ethernet address 02:fe:1f:73:92:81
218 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
219 * Ethernet address 02:fe:f2:25:c4:68
220 * local0 0 down local0
221 * local
222 * @cliexend
223 * Example of how to display '<em>verbose</em>' data for an interface by name and
224 * software index (where 2 is the software index):
225 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
226 * Name Idx Link Hardware
227 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
228 * Ethernet address ec:f4:bb:c0:bc:fc
229 * Intel e1000
230 * carrier up full duplex speed 1000 mtu 9216
231 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
232 * cpu socket 0
233 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
234 * Ethernet address ec:f4:bb:c0:bc:fd
235 * Intel e1000
236 * carrier up full duplex speed 1000 mtu 9216
237 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
238 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700239 * @cliexend
240 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400241/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
243 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400244 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
245 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200246 .function = show_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247};
Dave Barachba868bb2016-08-08 09:51:21 -0400248/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Billy McFalle9bac692017-08-11 14:05:11 -0400250
251/*?
252 * Clear the extended statistics for all or a list of given interfaces
253 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
254 *
255 * @cliexpar
256 * Example of how to clear the extended statistics for all interfaces:
257 * @cliexcmd{clear hardware-interfaces}
258 * Example of how to clear the extended statistics for an interface by
259 * name and software index (where 2 is the software index):
260 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
261 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400262/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
264 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400265 .short_help = "clear hardware-interfaces "
266 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200267 .function = clear_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268};
Dave Barachba868bb2016-08-08 09:51:21 -0400269/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
Dave Barachba868bb2016-08-08 09:51:21 -0400271static int
272sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273{
274 vnet_sw_interface_t *si1 = a1;
275 vnet_sw_interface_t *si2 = a2;
276
Dave Barachba868bb2016-08-08 09:51:21 -0400277 return vnet_sw_interface_compare (vnet_get_main (),
278 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279}
280
281static clib_error_t *
282show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400283 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284{
Dave Barachba868bb2016-08-08 09:51:21 -0400285 clib_error_t *error = 0;
286 vnet_main_t *vnm = vnet_get_main ();
Dave Barach525c9d02018-05-26 10:48:55 -0400287 unformat_input_t _linput, *linput = &_linput;
Dave Barachba868bb2016-08-08 09:51:21 -0400288 vnet_interface_main_t *im = &vnm->interface_main;
289 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200290 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200292 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500293 u8 show_tag = 0;
Jon Loeliger9485d992019-11-08 15:05:23 -0600294 u8 show_vtr = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400295 int verbose = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296
Dave Barach525c9d02018-05-26 10:48:55 -0400297 /*
298 * Get a line of input. Won't work if the user typed
299 * "show interface" and nothing more.
300 */
301 if (unformat_user (input, unformat_line_input, linput))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 {
Dave Barach525c9d02018-05-26 10:48:55 -0400303 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 {
Dave Barach525c9d02018-05-26 10:48:55 -0400305 /* See if user wants to show specific interface */
306 if (unformat
307 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
308 {
309 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
310 vec_add1 (sorted_sis, si[0]);
311 }
312 else if (unformat (linput, "address") || unformat (linput, "addr"))
313 show_addresses = 1;
314 else if (unformat (linput, "features") || unformat (linput, "feat"))
315 show_features = 1;
316 else if (unformat (linput, "tag"))
317 show_tag = 1;
Jon Loeliger9485d992019-11-08 15:05:23 -0600318 else if (unformat (linput, "vtr"))
319 show_vtr = 1;
Dave Barach525c9d02018-05-26 10:48:55 -0400320 else if (unformat (linput, "verbose"))
321 verbose = 1;
322 else
323 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400324 vec_free (sorted_sis);
Dave Barach525c9d02018-05-26 10:48:55 -0400325 error = clib_error_return (0, "unknown input `%U'",
326 format_unformat_error, linput);
327 goto done;
328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 }
Dave Barach525c9d02018-05-26 10:48:55 -0400330 unformat_free (linput);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 }
Jon Loeliger9485d992019-11-08 15:05:23 -0600332 if (show_features || show_tag || show_vtr)
Damjan Marion22311502016-10-28 20:30:15 +0200333 {
334 if (sw_if_index == ~(u32) 0)
Dave Barach8fdde3c2019-05-17 10:46:40 -0400335 {
336 vec_free (sorted_sis);
337 return clib_error_return (0, "Interface not specified...");
338 }
Dave Barach7be864a2016-11-28 11:41:35 -0500339 }
Damjan Marion22311502016-10-28 20:30:15 +0200340
Dave Barach7be864a2016-11-28 11:41:35 -0500341 if (show_features)
342 {
Dave Barach525c9d02018-05-26 10:48:55 -0400343 vnet_interface_features_show (vm, sw_if_index, verbose);
Neale Ranns47a3d992020-09-29 15:38:51 +0000344 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
Eyal Bari942402b2017-07-26 11:57:04 +0300345
346 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
347 vlib_cli_output (vm, "\nl2-output:");
348 if (l2_output->out_vtr_flag)
349 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
350 vlib_cli_output (vm, "%U", format_l2_output_features,
Neale Ranns5d9df1d2018-11-09 08:19:27 -0800351 l2_output->feature_bitmap, 1);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400352 vec_free (sorted_sis);
Damjan Marion22311502016-10-28 20:30:15 +0200353 return 0;
354 }
Dave Barach7be864a2016-11-28 11:41:35 -0500355 if (show_tag)
356 {
357 u8 *tag;
358 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
359 vlib_cli_output (vm, "%U: %s",
360 format_vnet_sw_if_index_name, vnm, sw_if_index,
361 tag ? (char *) tag : "(none)");
Dave Barach8fdde3c2019-05-17 10:46:40 -0400362 vec_free (sorted_sis);
Dave Barach7be864a2016-11-28 11:41:35 -0500363 return 0;
364 }
Damjan Marion22311502016-10-28 20:30:15 +0200365
Jon Loeliger9485d992019-11-08 15:05:23 -0600366 /*
367 * Show vlan tag rewrite data for one interface.
368 */
369 if (show_vtr)
370 {
371 u32 vtr_op = L2_VTR_DISABLED;
372 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
373
374 if (l2vtr_get (vm, vnm, sw_if_index,
375 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
376 {
377 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
378 format_vnet_sw_if_index_name, vnm, sw_if_index);
379 return 0;
380 }
381 vlib_cli_output (vm, "%U: VTR %0U",
382 format_vnet_sw_if_index_name, vnm, sw_if_index,
383 format_vtr, vtr_op, push_dot1q, tag1, tag2);
384 return 0;
385 }
386
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400388 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389
Dave Barachba868bb2016-08-08 09:51:21 -0400390 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 {
392 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400393 sorted_sis =
394 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 _vec_len (sorted_sis) = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400396 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100397 pool_foreach (si, im->sw_interfaces)
398 {
Dave Barach525c9d02018-05-26 10:48:55 -0400399 int visible = vnet_swif_is_api_visible (si);
400 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100401 vec_add1 (sorted_sis, si[0]);
402 }
Ole Troand7231612018-06-07 10:17:57 +0200403 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404 /* Sort by name. */
405 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
406 }
407
408 if (show_addresses)
409 {
410 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400411 {
Dave Barachba868bb2016-08-08 09:51:21 -0400412 ip4_main_t *im4 = &ip4_main;
413 ip6_main_t *im6 = &ip6_main;
414 ip_lookup_main_t *lm4 = &im4->lookup_main;
415 ip_lookup_main_t *lm6 = &im6->lookup_main;
416 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400417 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Dave Barachba868bb2016-08-08 09:51:21 -0400419 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
420 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
421 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barachba868bb2016-08-08 09:51:21 -0400423 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
424 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
425 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
John Lo4478d8e2018-01-12 17:15:25 -0500427 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
428 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Dave Barachba868bb2016-08-08 09:51:21 -0400430 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
431 vlib_cli_output
432 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500433 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400434 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
435 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400436 else
John Lo4478d8e2018-01-12 17:15:25 -0500437 vlib_cli_output
438 (vm, "%U (%s):",
439 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
440 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Eyal Bari942402b2017-07-26 11:57:04 +0300442 /* Display any L2 info */
Neale Ranns47a3d992020-09-29 15:38:51 +0000443 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400444
John Lo4478d8e2018-01-12 17:15:25 -0500445 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400446 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500447 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
448 1 /* honor unnumbered */,
449 ({
450 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
Neale Rannsd6953332021-08-10 07:39:18 +0000451 if (fib4->hash.table_id)
452 vlib_cli_output (
453 vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address,
454 r4, ia->address_length, fib4->hash.table_id,
455 ip4_fib_index_from_table_id (fib4->hash.table_id));
John Lo4478d8e2018-01-12 17:15:25 -0500456 else
457 vlib_cli_output (vm, " L3 %U/%d",
458 format_ip4_address, r4, ia->address_length);
459 }));
460 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
John Lo4478d8e2018-01-12 17:15:25 -0500462 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400463 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500464 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
465 1 /* honor unnumbered */,
466 ({
467 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
468 if (fib6->table_id)
469 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
470 format_ip6_address, r6, ia->address_length,
471 fib6->table_id,
472 ip6_fib_index_from_table_id (fib6->table_id));
473 else
474 vlib_cli_output (vm, " L3 %U/%d",
475 format_ip6_address, r6, ia->address_length);
476 }));
477 /* *INDENT-ON* */
Ole Troand7231612018-06-07 10:17:57 +0200478 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 }
Ole Troand7231612018-06-07 10:17:57 +0200480 else
481 {
482 vec_foreach (si, sorted_sis)
483 {
484 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
485 }
486 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487
Dave Barachba868bb2016-08-08 09:51:21 -0400488done:
Ole Troand7231612018-06-07 10:17:57 +0200489 vec_free (sorted_sis);
490 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491}
492
Dave Barachba868bb2016-08-08 09:51:21 -0400493/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400495 .path = "show interface",
Jon Loeliger9485d992019-11-08 15:05:23 -0600496 .short_help = "show interface [address|addr|features|feat|vtr] [<interface> [<interface> [..]]] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497 .function = show_sw_interfaces,
Steven Luongc5fa2b82019-04-25 11:19:49 -0700498 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499};
Dave Barachba868bb2016-08-08 09:51:21 -0400500/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501
502/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400503/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
505 .path = "interface",
506 .short_help = "Interface commands",
507};
Dave Barachba868bb2016-08-08 09:51:21 -0400508/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
Dave Barachba868bb2016-08-08 09:51:21 -0400510/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
512 .path = "set interface",
513 .short_help = "Interface commands",
514};
Dave Barachba868bb2016-08-08 09:51:21 -0400515/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
517static clib_error_t *
518clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400519 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520{
Dave Barachba868bb2016-08-08 09:51:21 -0400521 vnet_main_t *vnm = vnet_get_main ();
522 vnet_interface_main_t *im = &vnm->interface_main;
523 vlib_simple_counter_main_t *sm;
524 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400525 int j, n_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
527 n_counters = vec_len (im->combined_sw_if_counters);
528
529 for (j = 0; j < n_counters; j++)
530 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400531 im = &vnm->interface_main;
532 cm = im->combined_sw_if_counters + j;
533 vlib_clear_combined_counters (cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 }
535
536 n_counters = vec_len (im->sw_if_counters);
537
538 for (j = 0; j < n_counters; j++)
539 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400540 im = &vnm->interface_main;
541 sm = im->sw_if_counters + j;
542 vlib_clear_simple_counters (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 }
544
545 return 0;
546}
547
Billy McFalle9bac692017-08-11 14:05:11 -0400548/*?
549 * Clear the statistics for all interfaces (statistics associated with the
550 * '<em>show interface</em>' command).
551 *
552 * @cliexpar
553 * Example of how to clear the statistics for all interfaces:
554 * @cliexcmd{clear interfaces}
555 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400556/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
558 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400559 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 .function = clear_interface_counters,
561};
Dave Barachba868bb2016-08-08 09:51:21 -0400562/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563
Chris Luke16bcf7d2016-09-01 14:31:46 -0400564/**
565 * Parse subinterface names.
566 *
Dave Barachba868bb2016-08-08 09:51:21 -0400567 * The following subinterface syntax is supported. The first two are for
568 * backwards compatability:
569 *
570 * <intf-name> <id>
571 * - a subinterface with the name <intf-name>.<id>. The subinterface
572 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
573 *
574 * <intf-name> <min_id>-<max_id>
575 * - a set of the above subinterfaces, repeating for each id
576 * in the range <min_id> to <max_id>
577 *
578 * In the following, exact-match semantics (i.e. the number of vlan tags on the
579 * packet must match the number of tags in the configuration) are used only if
580 * the keyword exact-match is present. Non-exact match is the default.
581 *
582 * <intf-name> <id> dot1q <outer_id> [exact-match]
583 * - a subinterface with the name <intf-name>.<id>. The subinterface
584 * is a single dot1q vlan with vlan id <outer_id>.
585 *
586 * <intf-name> <id> dot1q any [exact-match]
587 * - a subinterface with the name <intf-name>.<id>. The subinterface
588 * is a single dot1q vlan with any vlan id.
589 *
590 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
591 * - a subinterface with the name <intf-name>.<id>. The subinterface
592 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
593 * <inner_id>.
594 *
595 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
596 * - a subinterface with the name <intf-name>.<id>. The subinterface
597 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
598 *
599 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
600 *
601 * - a subinterface with the name <intf-name>.<id>. The subinterface
602 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
603 *
604 * For each of the above CLI, there is a duplicate that uses the keyword
605 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
606 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
607 * tagged packets the inner ethertype is always 0x8100. Also note that
608 * the dot1q and dot1ad naming spaces are independent, so it is legal to
609 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
610 *
611 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
612 * - a subinterface with the name <intf-name>.<id>. The subinterface
613 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
614 * id <inner_id>.
615 *
616 * <intf-name> <id> untagged
617 * - a subinterface with the name <intf-name>.<id>. The subinterface
618 * has no vlan tags. Only one can be specified per interface.
619 *
620 * <intf-name> <id> default
621 * - a subinterface with the name <intf-name>.<id>. This is associated
622 * with a packet that did not match any other configured subinterface
623 * on this interface. Only one can be specified per interface.
624 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625
626static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400627parse_vlan_sub_interfaces (unformat_input_t * input,
628 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629{
Dave Barachba868bb2016-08-08 09:51:21 -0400630 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 u32 inner_vlan, outer_vlan;
632
Dave Barachba868bb2016-08-08 09:51:21 -0400633 if (unformat (input, "any inner-dot1q any"))
634 {
635 template->sub.eth.flags.two_tags = 1;
636 template->sub.eth.flags.outer_vlan_id_any = 1;
637 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 }
Dave Barachba868bb2016-08-08 09:51:21 -0400639 else if (unformat (input, "any"))
640 {
641 template->sub.eth.flags.one_tag = 1;
642 template->sub.eth.flags.outer_vlan_id_any = 1;
643 }
644 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
645 {
646 template->sub.eth.flags.two_tags = 1;
647 template->sub.eth.flags.inner_vlan_id_any = 1;
648 template->sub.eth.outer_vlan_id = outer_vlan;
649 }
650 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
651 {
652 template->sub.eth.flags.two_tags = 1;
653 template->sub.eth.outer_vlan_id = outer_vlan;
654 template->sub.eth.inner_vlan_id = inner_vlan;
655 }
656 else if (unformat (input, "%d", &outer_vlan))
657 {
658 template->sub.eth.flags.one_tag = 1;
659 template->sub.eth.outer_vlan_id = outer_vlan;
660 }
661 else
662 {
663 error = clib_error_return (0, "expected dot1q config, got `%U'",
664 format_unformat_error, input);
665 goto done;
666 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667
Dave Barachba868bb2016-08-08 09:51:21 -0400668 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
669 {
670 if (unformat (input, "exact-match"))
671 {
672 template->sub.eth.flags.exact_match = 1;
673 }
674 }
675
676done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 return error;
678}
679
680static clib_error_t *
681create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400682 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700683{
Dave Barachba868bb2016-08-08 09:51:21 -0400684 vnet_main_t *vnm = vnet_get_main ();
685 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400687 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 u32 id, id_min, id_max;
689 vnet_sw_interface_t template;
690
691 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400692 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693 {
694 error = clib_error_return (0, "unknown interface `%U'",
695 format_unformat_error, input);
696 goto done;
697 }
698
Dave Barachb7b92992018-10-17 10:38:51 -0400699 clib_memset (&template, 0, sizeof (template));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 template.sub.eth.raw_flags = 0;
701
Dave Barachba868bb2016-08-08 09:51:21 -0400702 if (unformat (input, "%d default", &id_min))
703 {
704 id_max = id_min;
705 template.sub.eth.flags.default_sub = 1;
706 }
707 else if (unformat (input, "%d untagged", &id_min))
708 {
709 id_max = id_min;
710 template.sub.eth.flags.no_tags = 1;
711 template.sub.eth.flags.exact_match = 1;
712 }
713 else if (unformat (input, "%d dot1q", &id_min))
714 {
715 /* parse dot1q config */
716 id_max = id_min;
717 error = parse_vlan_sub_interfaces (input, &template);
718 if (error)
719 goto done;
720 }
721 else if (unformat (input, "%d dot1ad", &id_min))
722 {
723 /* parse dot1ad config */
724 id_max = id_min;
725 template.sub.eth.flags.dot1ad = 1;
726 error = parse_vlan_sub_interfaces (input, &template);
727 if (error)
728 goto done;
729 }
730 else if (unformat (input, "%d-%d", &id_min, &id_max))
731 {
732 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400733 template.sub.eth.flags.exact_match = 1;
734 if (id_min > id_max)
735 goto id_error;
736 }
737 else if (unformat (input, "%d", &id_min))
738 {
739 id_max = id_min;
740 template.sub.eth.flags.one_tag = 1;
741 template.sub.eth.outer_vlan_id = id_min;
742 template.sub.eth.flags.exact_match = 1;
743 }
744 else
745 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746 id_error:
747 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
748 format_unformat_error, input);
749 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400750 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700751
752 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400753
Dave Barachba868bb2016-08-08 09:51:21 -0400754 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
755 {
756 error =
757 clib_error_return (0,
758 "not allowed as %v belong to a BondEthernet interface",
759 hi->name);
760 goto done;
761 }
John Lobcebbb92016-04-05 15:47:43 -0400762
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 for (id = id_min; id <= id_max; id++)
764 {
Dave Barachba868bb2016-08-08 09:51:21 -0400765 uword *p;
766 vnet_interface_main_t *im = &vnm->interface_main;
767 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
768 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769
770 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
771 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400772 {
773 if (CLIB_DEBUG > 0)
774 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
775 hi->sw_if_index, id);
776 continue;
777 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200780 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 template.sup_sw_if_index = hi->sw_if_index;
782 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300783 if (id_min < id_max)
784 template.sub.eth.outer_vlan_id = id;
785
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400787 if (error)
788 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400789
Jon Loeligerb22e1f02019-12-19 09:03:52 -0600790 kp = clib_mem_alloc (sizeof (*kp));
791 *kp = sup_and_sub_key;
792
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
794 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400795 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
796 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700797 }
798
Dave Barachba868bb2016-08-08 09:51:21 -0400799done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 return error;
801}
802
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700803/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400804 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
805 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
806 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
807 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
808 * but this is not recommended.
809 *
810 * This command has several variations:
811 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
812 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
813 *
814 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
815 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
816 * match any other subinterfaces should be sent to this subinterface.
817 *
818 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
819 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
820 * to this subinterface.
821 *
822 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
823 * subinterfaces to handle a range of VLAN IDs.
824 *
825 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700826 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
Billy McFalle9bac692017-08-11 14:05:11 -0400827 * VLAN ID different from the '<em>subId</em>'.
828 *
829 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
830 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700831 * the inner VLAN ID.
Billy McFalle9bac692017-08-11 14:05:11 -0400832 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700833 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
Billy McFalle9bac692017-08-11 14:05:11 -0400834 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
835 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
836 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
837 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
838 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
839 * entered, then the default behavior is exact-match.
840 *
841 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700842 *
843 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400844 * @parblock
845 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
846 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700847 *
Billy McFalle9bac692017-08-11 14:05:11 -0400848 * The previous example is shorthand and is equivalent to:
849 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700850 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700851 *
Billy McFalle9bac692017-08-11 14:05:11 -0400852 * Example of how to create a subinterface number that is different from the VLAN ID:
853 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700854 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700855 *
Billy McFalle9bac692017-08-11 14:05:11 -0400856 * Examples of how to create q-in-q and q-in-any subinterfaces:
857 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
858 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700859 *
Billy McFalle9bac692017-08-11 14:05:11 -0400860 * Examples of how to create dot1ad interfaces:
861 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
862 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700863 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700864 *
Billy McFalle9bac692017-08-11 14:05:11 -0400865 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
866 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
867 * is non-exact match:
868 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700869 *
Billy McFalle9bac692017-08-11 14:05:11 -0400870 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
871 * is specified and only one VLAN is configured, but packet contains two VLANs:
872 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700873 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700874 *
Billy McFalle9bac692017-08-11 14:05:11 -0400875 * Example of how to created a subinterface to process untagged packets:
876 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
877 *
878 * Example of how to created a subinterface to process any packet with a VLAN ID that
879 * does not match any other subinterface:
880 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
881 *
882 * When subinterfaces are created, they are in the down state. Example of how to
883 * enable a newly created subinterface:
884 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
885 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700886 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400887/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700889 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400890 .short_help = "create sub-interfaces <interface> "
891 "{<subId> [default|untagged]} | "
892 "{<subId>-<subId>} | "
893 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 .function = create_sub_interfaces,
895};
Dave Barachba868bb2016-08-08 09:51:21 -0400896/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
898static clib_error_t *
899set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400900 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901{
Dave Barachba868bb2016-08-08 09:51:21 -0400902 vnet_main_t *vnm = vnet_get_main ();
903 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 u32 sw_if_index, flags;
905
906 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400907 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 {
909 error = clib_error_return (0, "unknown interface `%U'",
910 format_unformat_error, input);
911 goto done;
912 }
913
Dave Barachba868bb2016-08-08 09:51:21 -0400914 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700915 {
916 error = clib_error_return (0, "unknown flags `%U'",
917 format_unformat_error, input);
918 goto done;
919 }
920
921 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
922 if (error)
923 goto done;
924
Dave Barachba868bb2016-08-08 09:51:21 -0400925done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700926 return error;
927}
928
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700929
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700930/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400931 * This command is used to change the admin state (up/down) of an interface.
932 *
933 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
934 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
935 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
936 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700937 *
938 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400939 * Example of how to configure the admin state of an interface to '<em>up</em?':
940 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
941 * Example of how to configure the admin state of an interface to '<em>down</em?':
942 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700943 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400944/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945VLIB_CLI_COMMAND (set_state_command, static) = {
946 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400947 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700948 .function = set_state,
949};
Dave Barachba868bb2016-08-08 09:51:21 -0400950/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
952static clib_error_t *
953set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400954 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700955{
Dave Barachba868bb2016-08-08 09:51:21 -0400956 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700957 u32 unnumbered_sw_if_index = ~0;
958 u32 inherit_from_sw_if_index = ~0;
959 int enable = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700961 if (unformat (input, "%U use %U",
962 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
963 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700964 enable = 1;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700965 else if (unformat (input, "del %U",
966 unformat_vnet_sw_interface, vnm,
967 &unnumbered_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700968 enable = 0;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700969 else
970 return clib_error_return (0, "parse error '%U'",
971 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700973 if (~0 == unnumbered_sw_if_index)
974 return clib_error_return (0, "Specify the unnumbered interface");
975 if (enable && ~0 == inherit_from_sw_if_index)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700976 return clib_error_return (0, "When enabling unnumbered specify the"
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700977 " IP enabled interface that it uses");
Neale Ranns898273f2017-03-18 02:57:38 -0700978
Stanislav Zaikin328b5da2021-07-15 16:27:29 +0200979 int rv = vnet_sw_interface_update_unnumbered (
980 unnumbered_sw_if_index, inherit_from_sw_if_index, enable);
981
982 switch (rv)
983 {
984 case 0:
985 break;
986
987 case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
988 return clib_error_return (
989 0,
990 "When enabling unnumbered both interfaces must be in the same tables");
991
992 default:
993 return clib_error_return (
994 0, "vnet_sw_interface_update_unnumbered returned %d", rv);
995 }
Neale Ranns898273f2017-03-18 02:57:38 -0700996
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700997 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998}
999
Dave Barachba868bb2016-08-08 09:51:21 -04001000/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
1002 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -04001003 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 .function = set_unnumbered,
1005};
Dave Barachba868bb2016-08-08 09:51:21 -04001006/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007
1008
1009
1010static clib_error_t *
1011set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001012 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013{
Dave Barachba868bb2016-08-08 09:51:21 -04001014 vnet_main_t *vnm = vnet_get_main ();
1015 vnet_interface_main_t *im = &vnm->interface_main;
1016 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 u32 hw_if_index, hw_class_index;
1018
1019 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001020 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021 {
1022 error = clib_error_return (0, "unknown hardware interface `%U'",
1023 format_unformat_error, input);
1024 goto done;
1025 }
1026
Dave Barachba868bb2016-08-08 09:51:21 -04001027 if (!unformat_user (input, unformat_hash_string,
1028 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 {
1030 error = clib_error_return (0, "unknown hardware class `%U'",
1031 format_unformat_error, input);
1032 goto done;
1033 }
1034
1035 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1036 if (error)
1037 goto done;
1038
Dave Barachba868bb2016-08-08 09:51:21 -04001039done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040 return error;
1041}
1042
Dave Barachba868bb2016-08-08 09:51:21 -04001043/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1045 .path = "set interface hw-class",
1046 .short_help = "Set interface hardware class",
1047 .function = set_hw_class,
1048};
Dave Barachba868bb2016-08-08 09:51:21 -04001049/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Dave Barachba868bb2016-08-08 09:51:21 -04001051static clib_error_t *
1052vnet_interface_cli_init (vlib_main_t * vm)
1053{
1054 return 0;
1055}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
1057VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1058
Dave Barachba868bb2016-08-08 09:51:21 -04001059static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001061 unformat_input_t * input,
1062 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001063{
1064 u32 hw_if_index;
1065 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001066 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067 int rv;
1068
Dave Barachba868bb2016-08-08 09:51:21 -04001069 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001071 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072
Dave Barachba868bb2016-08-08 09:51:21 -04001073 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074 return clib_error_return (0, "new dev instance missing");
1075
1076 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1077
1078 switch (rv)
1079 {
1080 case 0:
1081 break;
1082
1083 default:
1084 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001085 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001086
1087 }
1088
1089 return 0;
1090}
1091
1092
Dave Barachba868bb2016-08-08 09:51:21 -04001093/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1095 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001096 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 .function = renumber_interface_command_fn,
1098};
Dave Barachba868bb2016-08-08 09:51:21 -04001099/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100
Damjan Marion8358ff92016-04-15 14:26:00 +02001101static clib_error_t *
1102promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001103 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001104{
Dave Barachba868bb2016-08-08 09:51:21 -04001105 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001106 u32 hw_if_index;
1107 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001108 ethernet_main_t *em = &ethernet_main;
1109 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001110
1111 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001112 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001113 ;
1114 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001115 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001116 flags = 0;
1117 else
1118 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001119 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001120
1121 eif = ethernet_get_interface (em, hw_if_index);
1122 if (!eif)
1123 return clib_error_return (0, "not supported");
1124
1125 ethernet_set_flags (vnm, hw_if_index, flags);
1126 return 0;
1127}
1128
Dave Barachba868bb2016-08-08 09:51:21 -04001129/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001130VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1131 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001132 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001133 .function = promiscuous_cmd,
1134};
Dave Barachba868bb2016-08-08 09:51:21 -04001135/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001136
1137static clib_error_t *
1138mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1139{
Dave Barachba868bb2016-08-08 09:51:21 -04001140 vnet_main_t *vnm = vnet_get_main ();
Ole Troand7231612018-06-07 10:17:57 +02001141 u32 hw_if_index, sw_if_index, mtu;
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001142 ethernet_main_t *em = &ethernet_main;
Ole Troand7231612018-06-07 10:17:57 +02001143 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
Damjan Marion8358ff92016-04-15 14:26:00 +02001144
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001145 if (unformat (input, "%d %U", &mtu,
1146 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001147 {
Ole Troand7231612018-06-07 10:17:57 +02001148 /*
1149 * Change physical MTU on interface. Only supported for Ethernet
1150 * interfaces
1151 */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001152 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1153 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1154
1155 if (!eif)
1156 return clib_error_return (0, "not supported");
1157
1158 if (mtu < hi->min_supported_packet_bytes)
1159 return clib_error_return (0, "Invalid mtu (%d): "
1160 "must be >= min pkt bytes (%d)", mtu,
1161 hi->min_supported_packet_bytes);
1162
1163 if (mtu > hi->max_supported_packet_bytes)
1164 return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1165 hi->max_supported_packet_bytes);
1166
1167 vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
Ole Troand7231612018-06-07 10:17:57 +02001168 goto done;
Damjan Marion8358ff92016-04-15 14:26:00 +02001169 }
Ole Troand7231612018-06-07 10:17:57 +02001170 else if (unformat (input, "packet %d %U", &mtu,
1171 unformat_vnet_sw_interface, vnm, &sw_if_index))
1172 /* Set default packet MTU (including L3 header */
1173 mtus[VNET_MTU_L3] = mtu;
1174 else if (unformat (input, "ip4 %d %U", &mtu,
1175 unformat_vnet_sw_interface, vnm, &sw_if_index))
1176 mtus[VNET_MTU_IP4] = mtu;
1177 else if (unformat (input, "ip6 %d %U", &mtu,
1178 unformat_vnet_sw_interface, vnm, &sw_if_index))
1179 mtus[VNET_MTU_IP6] = mtu;
1180 else if (unformat (input, "mpls %d %U", &mtu,
1181 unformat_vnet_sw_interface, vnm, &sw_if_index))
1182 mtus[VNET_MTU_MPLS] = mtu;
Damjan Marion8358ff92016-04-15 14:26:00 +02001183 else
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001184 return clib_error_return (0, "unknown input `%U'",
1185 format_unformat_error, input);
Ole Troand7231612018-06-07 10:17:57 +02001186
1187 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1188
1189done:
Damjan Marion8358ff92016-04-15 14:26:00 +02001190 return 0;
1191}
1192
Dave Barachba868bb2016-08-08 09:51:21 -04001193/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001194VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1195 .path = "set interface mtu",
Ole Troand7231612018-06-07 10:17:57 +02001196 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001197 .function = mtu_cmd,
1198};
Dave Barachba868bb2016-08-08 09:51:21 -04001199/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001200
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001201static clib_error_t *
Matthew Smithe0792fd2019-07-12 11:48:24 -05001202show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1203 vlib_cli_command_t * cmd)
1204{
1205 vnet_main_t *vnm = vnet_get_main ();
1206 vnet_interface_main_t *im = &vnm->interface_main;
1207 ethernet_main_t *em = &ethernet_main;
1208 u32 sw_if_index = ~0;
1209 vnet_sw_interface_t *si, *sorted_sis = 0;
1210
1211 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1212 {
1213 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1214 vec_add1 (sorted_sis, si[0]);
1215 }
1216
1217 /* if an interface name was not passed, get all interfaces */
1218 if (vec_len (sorted_sis) == 0)
1219 {
1220 sorted_sis =
1221 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1222 _vec_len (sorted_sis) = 0;
1223 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001224 pool_foreach (si, im->sw_interfaces)
1225 {
Matthew Smithe0792fd2019-07-12 11:48:24 -05001226 int visible = vnet_swif_is_api_visible (si);
1227 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +01001228 vec_add1 (sorted_sis, si[0]);
1229 }
Matthew Smithe0792fd2019-07-12 11:48:24 -05001230 /* *INDENT-ON* */
1231 /* Sort by name. */
1232 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1233 }
1234
1235 vec_foreach (si, sorted_sis)
1236 {
1237 vnet_sw_interface_t *sup_si;
1238 ethernet_interface_t *ei;
1239
1240 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1241 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1242
1243 vlib_cli_output (vm, "%U (%s):",
1244 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1245 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1246 "up" : "dn");
1247
1248 if (ei && ei->secondary_addrs)
1249 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001250 ethernet_interface_address_t *sec_addr;
Matthew Smithe0792fd2019-07-12 11:48:24 -05001251
1252 vec_foreach (sec_addr, ei->secondary_addrs)
1253 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001254 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001255 }
1256 }
1257 }
1258
1259 vec_free (sorted_sis);
1260 return 0;
1261}
1262
1263/*?
1264 * This command is used to display interface secondary mac addresses.
1265 *
1266 * @cliexpar
1267 * Example of how to display interface secondary mac addresses:
1268 * @cliexstart{show interface secondary-mac-address}
1269 * @cliexend
1270?*/
1271/* *INDENT-OFF* */
1272VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1273 .path = "show interface secondary-mac-address",
1274 .short_help = "show interface secondary-mac-address [<interface>]",
1275 .function = show_interface_sec_mac_addr_fn,
1276};
1277/* *INDENT-ON* */
1278
1279static clib_error_t *
1280interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1281 vlib_cli_command_t * cmd)
1282{
1283 vnet_main_t *vnm = vnet_get_main ();
1284 vnet_sw_interface_t *si = NULL;
1285 clib_error_t *error = 0;
1286 u32 sw_if_index = ~0;
1287 u8 mac[6] = { 0 };
1288 u8 is_add, is_del;
1289
1290 is_add = is_del = 0;
1291
1292 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1293 {
1294 error = clib_error_return (0, "unknown interface `%U'",
1295 format_unformat_error, input);
1296 goto done;
1297 }
1298 if (!unformat_user (input, unformat_ethernet_address, mac))
1299 {
1300 error = clib_error_return (0, "expected mac address `%U'",
1301 format_unformat_error, input);
1302 goto done;
1303 }
1304
1305 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1306 {
1307 if (unformat (input, "add"))
1308 is_add = 1;
1309 else if (unformat (input, "del"))
1310 is_del = 1;
1311 else
1312 break;
1313 }
1314
1315 if (is_add == is_del)
1316 {
1317 error = clib_error_return (0, "must choose one of add or del");
1318 goto done;
1319 }
1320
1321 si = vnet_get_sw_interface (vnm, sw_if_index);
1322 error =
1323 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1324
1325done:
1326 return error;
1327}
1328
1329/*?
1330 * The '<em>set interface secondary-mac-address </em>' command allows adding
1331 * or deleting extra MAC addresses on a given interface without changing the
1332 * default MAC address. This could allow packets sent to these MAC addresses
1333 * to be received without setting the interface to promiscuous mode.
1334 * Not all interfaces support this operation. The ones that do are mostly
1335 * hardware NICs, though virtio does also.
1336 *
1337 * @cliexpar
1338 * @parblock
1339 * Example of how to add a secondary MAC Address on an interface:
1340 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1341 * Example of how to delete a secondary MAC address from an interface:
1342 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1343 * @endparblock
1344?*/
1345/* *INDENT-OFF* */
1346VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1347 .path = "set interface secondary-mac-address",
1348 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1349 .function = interface_add_del_mac_address,
1350};
1351/* *INDENT-ON* */
1352
1353static clib_error_t *
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001354set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1355 vlib_cli_command_t * cmd)
1356{
1357 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001358 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001359 clib_error_t *error = 0;
1360 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001361 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001362
1363 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1364 {
1365 error = clib_error_return (0, "unknown interface `%U'",
1366 format_unformat_error, input);
1367 goto done;
1368 }
John Lo62fcc0a2017-10-31 14:31:10 -04001369 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001370 {
1371 error = clib_error_return (0, "expected mac address `%U'",
1372 format_unformat_error, input);
1373 goto done;
1374 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001375 si = vnet_get_sw_interface (vnm, sw_if_index);
1376 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001377done:
1378 return error;
1379}
1380
1381/*?
1382 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1383 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1384 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1385 *
1386 * @cliexpar
1387 * @parblock
1388 * Example of how to change MAC Address of interface:
1389 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1390 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1391 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1392 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1393 * @endparblock
1394?*/
1395/* *INDENT-OFF* */
1396VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1397 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001398 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001399 .function = set_interface_mac_address,
1400};
1401/* *INDENT-ON* */
1402
Dave Barach7be864a2016-11-28 11:41:35 -05001403static clib_error_t *
1404set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1405{
1406 vnet_main_t *vnm = vnet_get_main ();
1407 u32 sw_if_index = ~0;
1408 u8 *tag = 0;
1409
1410 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1411 vnm, &sw_if_index, &tag))
1412 return clib_error_return (0, "unknown input `%U'",
1413 format_unformat_error, input);
1414
1415 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1416
1417 return 0;
1418}
1419
1420/* *INDENT-OFF* */
1421VLIB_CLI_COMMAND (set_tag_command, static) = {
1422 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001423 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001424 .function = set_tag,
1425};
1426/* *INDENT-ON* */
1427
1428static clib_error_t *
1429clear_tag (vlib_main_t * vm, unformat_input_t * input,
1430 vlib_cli_command_t * cmd)
1431{
1432 vnet_main_t *vnm = vnet_get_main ();
1433 u32 sw_if_index = ~0;
1434
1435 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1436 return clib_error_return (0, "unknown input `%U'",
1437 format_unformat_error, input);
1438
1439 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1440
1441 return 0;
1442}
1443
1444/* *INDENT-OFF* */
1445VLIB_CLI_COMMAND (clear_tag_command, static) = {
1446 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001447 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001448 .function = clear_tag,
1449};
1450/* *INDENT-ON* */
1451
Damjan Marion44036902017-04-28 12:29:15 +02001452static clib_error_t *
Neale Ranns1855b8e2018-07-11 10:31:26 -07001453set_ip_directed_broadcast (vlib_main_t * vm,
1454 unformat_input_t * input, vlib_cli_command_t * cmd)
1455{
1456 vnet_main_t *vnm = vnet_get_main ();
1457 u32 sw_if_index = ~0;
1458 u8 enable = 0;
1459
1460 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1461 else if (unformat (input, "enable"))
1462 enable = 1;
1463 else if (unformat (input, "disable"))
1464 enable = 0;
1465 else
1466 return clib_error_return (0, "unknown input: `%U'",
1467 format_unformat_error, input);
1468
1469 if (~0 == sw_if_index)
1470 return clib_error_return (0, "specify an interface: `%U'",
1471 format_unformat_error, input);
1472
1473 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1474
1475 return 0;
1476}
1477
1478/*?
1479 * This command is used to enable/disable IP directed broadcast
1480 * If directed broadcast is enabled a packet sent to the interface's
1481 * subnet broadcast address will be sent L2 broadcast on the interface,
1482 * otherwise it is dropped.
1483 ?*/
1484/* *INDENT-OFF* */
1485VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1486 .path = "set interface ip directed-broadcast",
1487 .short_help = "set interface enable <interface> <enable|disable>",
1488 .function = set_ip_directed_broadcast,
1489};
1490/* *INDENT-ON* */
1491
Stevenad8015b2017-10-29 22:10:46 -07001492clib_error_t *
1493set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1494 u8 queue_id_valid, u32 queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001495 vnet_hw_if_rx_mode mode)
Stevenad8015b2017-10-29 22:10:46 -07001496{
1497 clib_error_t *error = 0;
1498 vnet_hw_interface_t *hw;
Damjan Marion94100532020-11-06 23:25:57 +01001499 u32 *queue_indices = 0;
Stevenad8015b2017-10-29 22:10:46 -07001500
1501 hw = vnet_get_hw_interface (vnm, hw_if_index);
1502
Damjan Marion94100532020-11-06 23:25:57 +01001503 if (queue_id_valid)
1504 {
1505 u32 queue_index;
1506 queue_index =
1507 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1508 if (queue_index == ~0)
1509 return clib_error_return (0, "unknown queue %u on interface %s",
1510 queue_id, hw->name);
1511 vec_add1 (queue_indices, queue_index);
Stevenad8015b2017-10-29 22:10:46 -07001512 }
1513 else
Damjan Marion94100532020-11-06 23:25:57 +01001514 queue_indices = hw->rx_queue_indices;
Stevenad8015b2017-10-29 22:10:46 -07001515
Damjan Marion94100532020-11-06 23:25:57 +01001516 for (int i = 0; i < vec_len (queue_indices); i++)
1517 {
1518 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1519 if (rv)
1520 goto done;
1521 }
1522
1523done:
1524 if (queue_indices != hw->rx_queue_indices)
1525 vec_free (queue_indices);
1526 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1527 return error;
Stevenad8015b2017-10-29 22:10:46 -07001528}
1529
Stevene3a395c2017-05-09 16:19:50 -07001530static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001531set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1532 vlib_cli_command_t * cmd)
1533{
1534 clib_error_t *error = 0;
1535 unformat_input_t _line_input, *line_input = &_line_input;
1536 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001537 u32 hw_if_index = (u32) ~ 0;
1538 u32 queue_id = (u32) ~ 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001539 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001540 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001541
Damjan Marion44036902017-04-28 12:29:15 +02001542 if (!unformat_user (input, unformat_line_input, line_input))
1543 return 0;
1544
1545 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1546 {
1547 if (unformat
1548 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1549 ;
1550 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001551 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001552 else if (unformat (line_input, "polling"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001553 mode = VNET_HW_IF_RX_MODE_POLLING;
Damjan Marion44036902017-04-28 12:29:15 +02001554 else if (unformat (line_input, "interrupt"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001555 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
Damjan Marion44036902017-04-28 12:29:15 +02001556 else if (unformat (line_input, "adaptive"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001557 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
Damjan Marion44036902017-04-28 12:29:15 +02001558 else
1559 {
1560 error = clib_error_return (0, "parse error: '%U'",
1561 format_unformat_error, line_input);
1562 unformat_free (line_input);
1563 return error;
1564 }
1565 }
1566
1567 unformat_free (line_input);
1568
1569 if (hw_if_index == (u32) ~ 0)
1570 return clib_error_return (0, "please specify valid interface name");
1571
Damjan Marioneabd4242020-10-07 20:59:07 +02001572 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Damjan Marion44036902017-04-28 12:29:15 +02001573 return clib_error_return (0, "please specify valid rx-mode");
1574
Stevenad8015b2017-10-29 22:10:46 -07001575 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1576 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001577
Stevene3a395c2017-05-09 16:19:50 -07001578 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001579}
1580
1581/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001582 * This command is used to assign the RX packet processing mode (polling,
1583 * interrupt, adaptive) of the a given interface, and optionally a
1584 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1585 * is applied to all queues of the interface. Not all interfaces support
1586 * all modes. To display the current rx-mode use the command
1587 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001588 *
1589 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001590 * Example of how to assign rx-mode to all queues on an interface:
1591 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1592 * Example of how to assign rx-mode to one queue of an interface:
1593 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1594 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001595 * @cliexstart{show interface rx-placement}
1596 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001597 * node dpdk-input:
1598 * GigabitEthernet7/0/0 queue 0 (polling)
1599 * node vhost-user-input:
1600 * VirtualEthernet0/0/12 queue 0 (interrupt)
1601 * VirtualEthernet0/0/12 queue 2 (polling)
1602 * VirtualEthernet0/0/13 queue 0 (polling)
1603 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001604 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001605 * node dpdk-input:
1606 * GigabitEthernet7/0/1 queue 0 (polling)
1607 * node vhost-user-input:
1608 * VirtualEthernet0/0/12 queue 1 (polling)
1609 * VirtualEthernet0/0/12 queue 3 (polling)
1610 * VirtualEthernet0/0/13 queue 1 (polling)
1611 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001612 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001613?*/
1614/* *INDENT-OFF* */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001615VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
Damjan Marion44036902017-04-28 12:29:15 +02001616 .path = "set interface rx-mode",
1617 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1618 .function = set_interface_rx_mode,
1619};
1620/* *INDENT-ON* */
1621
1622static clib_error_t *
1623show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1624 vlib_cli_command_t * cmd)
1625{
1626 u8 *s = 0;
1627 vnet_main_t *vnm = vnet_get_main ();
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001628 vnet_hw_if_rx_queue_t **all_queues = 0;
1629 vnet_hw_if_rx_queue_t **qptr;
1630 vnet_hw_if_rx_queue_t *q;
Nathan Skrzypczak455779f2021-02-15 14:48:33 +01001631 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001632 vec_add1 (all_queues, q);
1633 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1634 u32 prev_node = ~0;
Damjan Marion44036902017-04-28 12:29:15 +02001635
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001636 vec_foreach (qptr, all_queues)
1637 {
1638 u32 current_thread = qptr[0]->thread_index;
1639 u32 hw_if_index = qptr[0]->hw_if_index;
1640 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1641 u32 current_node = hw_if->input_node_index;
1642 if (current_node != prev_node)
1643 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1644 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1645 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1646 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1647 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1648 current_thread != qptr[1]->thread_index)
1649 {
1650 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1651 vlib_worker_threads[current_thread].name, s);
1652 vec_reset_length (s);
1653 }
1654 prev_node = current_node;
1655 }
Damjan Marion44036902017-04-28 12:29:15 +02001656 vec_free (s);
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001657 vec_free (all_queues);
Damjan Marion44036902017-04-28 12:29:15 +02001658 return 0;
1659}
1660
Billy McFalle9bac692017-08-11 14:05:11 -04001661/*?
1662 * This command is used to display the interface and queue worker
1663 * thread placement.
1664 *
1665 * @cliexpar
1666 * Example of how to display the interface placement:
1667 * @cliexstart{show interface rx-placement}
1668 * Thread 1 (vpp_wk_0):
1669 * node dpdk-input:
1670 * GigabitEthernet7/0/0 queue 0 (polling)
1671 * node vhost-user-input:
1672 * VirtualEthernet0/0/12 queue 0 (polling)
1673 * VirtualEthernet0/0/12 queue 2 (polling)
1674 * VirtualEthernet0/0/13 queue 0 (polling)
1675 * VirtualEthernet0/0/13 queue 2 (polling)
1676 * Thread 2 (vpp_wk_1):
1677 * node dpdk-input:
1678 * GigabitEthernet7/0/1 queue 0 (polling)
1679 * node vhost-user-input:
1680 * VirtualEthernet0/0/12 queue 1 (polling)
1681 * VirtualEthernet0/0/12 queue 3 (polling)
1682 * VirtualEthernet0/0/13 queue 1 (polling)
1683 * VirtualEthernet0/0/13 queue 3 (polling)
1684 * @cliexend
1685?*/
Damjan Marion44036902017-04-28 12:29:15 +02001686/* *INDENT-OFF* */
1687VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1688 .path = "show interface rx-placement",
1689 .short_help = "show interface rx-placement",
1690 .function = show_interface_rx_placement_fn,
1691};
1692/* *INDENT-ON* */
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001693clib_error_t *
1694set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1695 u32 thread_index, u8 is_main)
Damjan Marion44036902017-04-28 12:29:15 +02001696{
Damjan Marion44036902017-04-28 12:29:15 +02001697 vnet_main_t *vnm = vnet_get_main ();
1698 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion94100532020-11-06 23:25:57 +01001699 vnet_hw_interface_t *hw;
1700 u32 queue_index;
Damjan Marion44036902017-04-28 12:29:15 +02001701
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001702 if (is_main)
1703 thread_index = 0;
1704 else
1705 thread_index += vdm->first_worker_thread_index;
Damjan Marion44036902017-04-28 12:29:15 +02001706
1707 if (thread_index > vdm->last_worker_thread_index)
1708 return clib_error_return (0,
1709 "please specify valid worker thread or main");
1710
Damjan Marion94100532020-11-06 23:25:57 +01001711 hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001712
Damjan Marion94100532020-11-06 23:25:57 +01001713 queue_index =
1714 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1715 if (queue_index == ~0)
1716 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1717 hw->name);
1718 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1719 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1720 return 0;
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001721}
1722
1723static clib_error_t *
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001724set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1725 vlib_cli_command_t *cmd)
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001726{
1727 clib_error_t *error = 0;
1728 unformat_input_t _line_input, *line_input = &_line_input;
1729 vnet_main_t *vnm = vnet_get_main ();
1730 u32 hw_if_index = (u32) ~ 0;
1731 u32 queue_id = (u32) 0;
1732 u32 thread_index = (u32) ~ 0;
1733 u8 is_main = 0;
1734
1735 if (!unformat_user (input, unformat_line_input, line_input))
1736 return 0;
1737
1738 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1739 {
1740 if (unformat
1741 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1742 ;
1743 else if (unformat (line_input, "queue %d", &queue_id))
1744 ;
1745 else if (unformat (line_input, "main", &thread_index))
1746 is_main = 1;
1747 else if (unformat (line_input, "worker %d", &thread_index))
1748 ;
1749 else
1750 {
1751 error = clib_error_return (0, "parse error: '%U'",
1752 format_unformat_error, line_input);
1753 unformat_free (line_input);
1754 return error;
1755 }
1756 }
1757
1758 unformat_free (line_input);
1759
1760 if (hw_if_index == (u32) ~ 0)
1761 return clib_error_return (0, "please specify valid interface name");
1762
1763 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1764 is_main);
1765
1766 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001767}
1768
1769/*?
1770 * This command is used to assign a given interface, and optionally a
1771 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001772 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1773 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001774 *
1775 * @cliexpar
1776 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001777 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001778 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001779 * node dpdk-input:
1780 * GigabitEthernet7/0/0 queue 0 (polling)
1781 * node vhost-user-input:
1782 * VirtualEthernet0/0/12 queue 0 (polling)
1783 * VirtualEthernet0/0/12 queue 2 (polling)
1784 * VirtualEthernet0/0/13 queue 0 (polling)
1785 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001786 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001787 * node dpdk-input:
1788 * GigabitEthernet7/0/1 queue 0 (polling)
1789 * node vhost-user-input:
1790 * VirtualEthernet0/0/12 queue 1 (polling)
1791 * VirtualEthernet0/0/12 queue 3 (polling)
1792 * VirtualEthernet0/0/13 queue 1 (polling)
1793 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001794 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001795 * Example of how to assign a interface and queue to a worker thread:
1796 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1797 * Example of how to display the interface placement:
1798 * @cliexstart{show interface rx-placement}
1799 * Thread 1 (vpp_wk_0):
1800 * node dpdk-input:
1801 * GigabitEthernet7/0/0 queue 0 (polling)
1802 * node vhost-user-input:
1803 * VirtualEthernet0/0/12 queue 0 (polling)
1804 * VirtualEthernet0/0/12 queue 1 (polling)
1805 * VirtualEthernet0/0/12 queue 2 (polling)
1806 * VirtualEthernet0/0/13 queue 0 (polling)
1807 * VirtualEthernet0/0/13 queue 2 (polling)
1808 * Thread 2 (vpp_wk_1):
1809 * node dpdk-input:
1810 * GigabitEthernet7/0/1 queue 0 (polling)
1811 * node vhost-user-input:
1812 * VirtualEthernet0/0/12 queue 3 (polling)
1813 * VirtualEthernet0/0/13 queue 1 (polling)
1814 * VirtualEthernet0/0/13 queue 3 (polling)
1815 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001816?*/
1817/* *INDENT-OFF* */
1818VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1819 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001820 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001821 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001822 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001823 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001824};
Damjan Marion44036902017-04-28 12:29:15 +02001825/* *INDENT-ON* */
Dave Barach5ecd5a52019-02-25 15:27:28 -05001826
Chenmin Sunc4665092020-07-06 08:20:39 +08001827clib_error_t *
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001828set_hw_interface_tx_queue (u32 hw_if_index, u32 queue_id, uword *bitmap)
1829{
1830 vnet_main_t *vnm = vnet_get_main ();
1831 vnet_device_main_t *vdm = &vnet_device_main;
1832 vnet_hw_interface_t *hw;
1833 vnet_hw_if_tx_queue_t *txq;
1834 u32 queue_index;
1835 u32 thread_index;
1836
1837 hw = vnet_get_hw_interface (vnm, hw_if_index);
1838
1839 /* highest set bit in bitmap should not exceed last worker thread index */
Steven Luong8e3f1092021-06-17 08:22:50 -07001840 thread_index = clib_bitmap_last_set (bitmap);
1841 if ((thread_index != ~0) && (thread_index > vdm->last_worker_thread_index))
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001842 return clib_error_return (0, "please specify valid thread(s)");
1843
1844 queue_index =
1845 vnet_hw_if_get_tx_queue_index_by_id (vnm, hw_if_index, queue_id);
1846 if (queue_index == ~0)
1847 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1848 hw->name);
1849
1850 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
1851
1852 // free the existing bitmap
1853 if (clib_bitmap_count_set_bits (txq->threads))
1854 {
1855 txq->shared_queue = 0;
1856 clib_bitmap_free (txq->threads);
1857 }
1858
1859 clib_bitmap_foreach (thread_index, bitmap)
1860 vnet_hw_if_tx_queue_assign_thread (vnm, queue_index, thread_index);
1861
1862 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1863 return 0;
1864}
1865
1866static clib_error_t *
1867set_interface_tx_queue (vlib_main_t *vm, unformat_input_t *input,
1868 vlib_cli_command_t *cmd)
1869{
1870 clib_error_t *error = 0;
1871 unformat_input_t _line_input, *line_input = &_line_input;
1872 vnet_main_t *vnm = vnet_get_main ();
1873 u32 hw_if_index = (u32) ~0;
1874 u32 queue_id = (u32) 0;
1875 uword *bitmap = 0;
1876
1877 if (!unformat_user (input, unformat_line_input, line_input))
1878 return 0;
1879
1880 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1881 {
1882 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
1883 &hw_if_index))
1884 ;
1885 else if (unformat (line_input, "queue %d", &queue_id))
1886 ;
1887 else if (unformat (line_input, "threads %U", unformat_bitmap_list,
1888 &bitmap))
1889 ;
1890 else
1891 {
1892 error = clib_error_return (0, "parse error: '%U'",
1893 format_unformat_error, line_input);
1894 unformat_free (line_input);
1895 return error;
1896 }
1897 }
1898
1899 unformat_free (line_input);
1900
1901 if (hw_if_index == (u32) ~0)
1902 {
1903 error = clib_error_return (0, "please specify valid interface name");
1904 goto error;
1905 }
1906
1907 error = set_hw_interface_tx_queue (hw_if_index, queue_id, bitmap);
1908
1909error:
1910 clib_bitmap_free (bitmap);
1911 return (error);
1912}
1913
1914VLIB_CLI_COMMAND (cmd_set_if_tx_queue, static) = {
1915 .path = "set interface tx-queue",
1916 .short_help = "set interface tx-queue <interface> queue <n> "
Steven Luong8e3f1092021-06-17 08:22:50 -07001917 "[threads <list>]",
Mohsin Kazmi005605f2021-05-24 18:33:50 +02001918 .function = set_interface_tx_queue,
1919 .is_mp_safe = 1,
1920};
1921
1922clib_error_t *
Chenmin Sunc4665092020-07-06 08:20:39 +08001923set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1924 clib_bitmap_t * bitmap)
1925{
1926 vnet_main_t *vnm = vnet_get_main ();
1927 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1928
1929 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1930}
1931
1932static clib_error_t *
1933set_interface_rss_queues_fn (vlib_main_t * vm,
1934 unformat_input_t * input,
1935 vlib_cli_command_t * cmd)
1936{
1937 clib_error_t *error = 0;
1938 unformat_input_t _line_input, *line_input = &_line_input;
1939 vnet_main_t *vnm = vnet_get_main ();
1940 u32 hw_if_index = (u32) ~ 0;
1941 clib_bitmap_t *bitmap = NULL;
1942
1943 if (!unformat_user (input, unformat_line_input, line_input))
1944 return 0;
1945
1946 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1947 {
1948 if (unformat
1949 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1950 ;
1951 else
1952 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
1953 ;
1954 else
1955 {
1956 error = clib_error_return (0, "parse error: '%U'",
1957 format_unformat_error, line_input);
1958 unformat_free (line_input);
1959 goto done;
1960 }
1961 }
1962
1963 unformat_free (line_input);
1964
1965 if (hw_if_index == (u32) ~ 0)
1966 {
1967 error = clib_error_return (0, "please specify valid interface name");
1968 goto done;
1969 }
1970
1971 if (bitmap == NULL)
1972 {
1973 error = clib_error_return (0, "please specify the valid rss queues");
1974 goto done;
1975 }
1976
1977 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
1978
1979done:
1980 if (bitmap)
1981 clib_bitmap_free (bitmap);
1982
1983 return (error);
1984}
1985
1986/*?
1987 * This command is used to set the rss queues of a given interface
1988 * Not all the interfaces support this operation.
1989 * To display the current rss queues, use the command
1990 * '<em>show hardware-interfaces</em>'.
1991 *
1992 * @cliexpar
1993 * Example of how to set the rss queues to 0,2-5,7 of an interface:
1994 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
1995 * @cliexend
1996?*/
1997/* *INDENT-OFF* */
1998VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
1999 .path = "set interface rss queues",
2000 .short_help = "set interface rss queues <interface> <list <queue-list>>",
2001 .function = set_interface_rss_queues_fn,
2002};
2003/* *INDENT-ON* */
2004
Dave Barach33909772019-09-23 10:27:27 -04002005static u8 *
2006format_vnet_pcap (u8 * s, va_list * args)
2007{
2008 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2009 int type = va_arg (*args, int);
2010 int printed = 0;
2011
2012 if (type == 0)
2013 {
2014 if (pp->pcap_rx_enable)
2015 {
2016 s = format (s, "rx");
2017 printed = 1;
2018 }
2019 if (pp->pcap_tx_enable)
2020 {
2021 if (printed)
2022 s = format (s, " and ");
2023 s = format (s, "tx");
2024 printed = 1;
2025 }
2026 if (pp->pcap_drop_enable)
2027 {
2028 if (printed)
2029 s = format (s, " and ");
2030 s = format (s, "drop");
2031 printed = 1;
2032 }
2033 return s;
2034 }
2035 s = format (s, "unknown type %d!", type);
2036 return s;
2037}
2038
2039
Dave Barachb97641c2019-09-09 16:38:17 -04002040int
2041vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2042{
2043 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8fb5add2021-03-04 18:41:59 +01002044 vnet_main_t *vnm = vnet_get_main ();
2045 vnet_pcap_t *pp = &vnm->pcap;
Dave Barachb97641c2019-09-09 16:38:17 -04002046 pcap_main_t *pm = &pp->pcap_main;
Dave Barachf5667c32019-09-25 11:27:46 -04002047 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachb97641c2019-09-09 16:38:17 -04002048
2049 if (a->status)
2050 {
Dave Barach33909772019-09-23 10:27:27 -04002051 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002052 {
2053 vlib_cli_output
Dave Barach33909772019-09-23 10:27:27 -04002054 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2055 format_vnet_pcap, pp, 0 /* print type */ ,
Dave Barachb97641c2019-09-09 16:38:17 -04002056 pm->n_packets_captured, pm->n_packets_to_capture);
2057 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2058 }
2059 else
Dave Barach33909772019-09-23 10:27:27 -04002060 vlib_cli_output (vm, "pcap dispatch capture disabled");
2061
Dave Barachb97641c2019-09-09 16:38:17 -04002062 return 0;
2063 }
2064
2065 /* Consistency checks */
2066
2067 /* Enable w/ capture already enabled not allowed */
Dave Barach33909772019-09-23 10:27:27 -04002068 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2069 && (a->rx_enable + a->tx_enable + a->drop_enable))
Dave Barachb97641c2019-09-09 16:38:17 -04002070 return VNET_API_ERROR_INVALID_VALUE;
2071
2072 /* Disable capture with capture already disabled, not interesting */
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01002073 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
2074 0) &&
2075 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
Dave Barachb97641c2019-09-09 16:38:17 -04002076 return VNET_API_ERROR_VALUE_EXIST;
2077
2078 /* Change number of packets to capture while capturing */
Dave Barach33909772019-09-23 10:27:27 -04002079 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2080 && (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002081 && (pm->n_packets_to_capture != a->packets_to_capture))
2082 return VNET_API_ERROR_INVALID_VALUE_2;
2083
Dave Barach33909772019-09-23 10:27:27 -04002084 /* Classify filter specified, but no classify filter configured */
Dave Barachf5667c32019-09-25 11:27:46 -04002085 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
Benoît Ganne5943e362021-02-26 14:46:58 +01002086 (!cm->classify_table_index_by_sw_if_index ||
2087 cm->classify_table_index_by_sw_if_index[0] == ~0))
Dave Barach9137e542019-09-13 17:47:50 -04002088 return VNET_API_ERROR_NO_SUCH_LABEL;
2089
Dave Barach33909772019-09-23 10:27:27 -04002090 if (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002091 {
Dave Barach586462f2020-08-05 16:12:35 -04002092 void *save_pcap_data;
2093
Dave Barach33909772019-09-23 10:27:27 -04002094 /* Sanity check max bytes per pkt */
2095 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2096 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2097
Dave Barachb97641c2019-09-09 16:38:17 -04002098 /* Clean up from previous run, if any */
Dave Barach586462f2020-08-05 16:12:35 -04002099 vec_reset_length (pm->pcap_data);
2100
2101 /* Throw away the data buffer? */
2102 if (a->free_data)
2103 vec_free (pm->pcap_data);
2104
2105 save_pcap_data = pm->pcap_data;
2106
Dave Barachb97641c2019-09-09 16:38:17 -04002107 memset (pm, 0, sizeof (*pm));
2108
Dave Barach586462f2020-08-05 16:12:35 -04002109 pm->pcap_data = save_pcap_data;
2110
Dave Barach11fb09e2020-08-06 12:10:09 -04002111 vec_validate_aligned (vnet_trace_placeholder, 2048,
2112 CLIB_CACHE_LINE_BYTES);
Dave Barachb97641c2019-09-09 16:38:17 -04002113 if (pm->lock == 0)
2114 clib_spinlock_init (&(pm->lock));
2115
2116 if (a->filename == 0)
Dave Barach33909772019-09-23 10:27:27 -04002117 {
2118 u8 *stem = 0;
2119
2120 if (a->rx_enable)
2121 stem = format (stem, "rx");
2122 if (a->tx_enable)
2123 stem = format (stem, "tx");
2124 if (a->drop_enable)
2125 stem = format (stem, "drop");
Benoît Ganne7d4a9972020-07-17 11:49:56 +02002126 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
Dave Barach33909772019-09-23 10:27:27 -04002127 vec_free (stem);
2128 }
Dave Barachb97641c2019-09-09 16:38:17 -04002129
2130 pm->file_name = (char *) a->filename;
2131 pm->n_packets_captured = 0;
2132 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
Dave Barach586462f2020-08-05 16:12:35 -04002133 /* Preallocate the data vector? */
2134 if (a->preallocate_data)
2135 {
2136 vec_validate
2137 (pm->pcap_data, a->packets_to_capture
2138 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2139 vec_reset_length (pm->pcap_data);
2140 }
Dave Barachb97641c2019-09-09 16:38:17 -04002141 pm->n_packets_to_capture = a->packets_to_capture;
2142 pp->pcap_sw_if_index = a->sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002143 if (a->filter)
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002144 pp->filter_classify_table_index =
2145 cm->classify_table_index_by_sw_if_index[0];
Dave Barach9137e542019-09-13 17:47:50 -04002146 else
2147 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002148 pp->pcap_error_index = a->drop_err;
Dave Barach33909772019-09-23 10:27:27 -04002149 pp->pcap_rx_enable = a->rx_enable;
2150 pp->pcap_tx_enable = a->tx_enable;
2151 pp->pcap_drop_enable = a->drop_enable;
2152 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
Dave Barachb97641c2019-09-09 16:38:17 -04002153 }
2154 else
2155 {
Dave Barach33909772019-09-23 10:27:27 -04002156 pp->pcap_rx_enable = 0;
2157 pp->pcap_tx_enable = 0;
2158 pp->pcap_drop_enable = 0;
Dave Barachf5667c32019-09-25 11:27:46 -04002159 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002160 pp->pcap_error_index = ~0;
Dave Barachb97641c2019-09-09 16:38:17 -04002161 if (pm->n_packets_captured)
2162 {
2163 clib_error_t *error;
2164 pm->n_packets_to_capture = pm->n_packets_captured;
2165 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2166 pm->n_packets_captured, pm->file_name);
2167 error = pcap_write (pm);
Andrew Yourtchenko4da15062019-09-11 14:14:43 +00002168 if (pm->flags & PCAP_MAIN_INIT_DONE)
Dave Barachb97641c2019-09-09 16:38:17 -04002169 pcap_close (pm);
2170 /* Report I/O errors... */
2171 if (error)
2172 {
2173 clib_error_report (error);
2174 return VNET_API_ERROR_SYSCALL_ERROR_1;
2175 }
Dave Barach586462f2020-08-05 16:12:35 -04002176 vec_free (pm->file_name);
2177 if (a->free_data)
2178 vec_free (pm->pcap_data);
Dave Barachb97641c2019-09-09 16:38:17 -04002179 return 0;
2180 }
2181 else
2182 return VNET_API_ERROR_NO_SUCH_ENTRY;
2183 }
2184
2185 return 0;
2186}
2187
2188static clib_error_t *
Dave Barach33909772019-09-23 10:27:27 -04002189pcap_trace_command_fn (vlib_main_t * vm,
2190 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002191{
Dave Barach5ecd5a52019-02-25 15:27:28 -05002192 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb97641c2019-09-09 16:38:17 -04002193 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002194 vnet_main_t *vnm = vnet_get_main ();
Dave Barachb97641c2019-09-09 16:38:17 -04002195 u8 *filename = 0;
2196 u32 max = 1000;
Dave Barach33909772019-09-23 10:27:27 -04002197 u32 max_bytes_per_pkt = 512;
Dave Barachb97641c2019-09-09 16:38:17 -04002198 int rv;
Dave Barach33909772019-09-23 10:27:27 -04002199 int rx_enable = 0;
2200 int tx_enable = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002201 int preallocate_data = 0;
Dave Barach33909772019-09-23 10:27:27 -04002202 int drop_enable = 0;
Dave Barachb97641c2019-09-09 16:38:17 -04002203 int status = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002204 int filter = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002205 int free_data = 0;
Dave Barach4330c462020-06-10 17:07:32 -04002206 u32 sw_if_index = 0; /* default: any interface */
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002207 vlib_error_t drop_err = ~0; /* default: any error */
Dave Barach5ecd5a52019-02-25 15:27:28 -05002208
2209 /* Get a line of input. */
2210 if (!unformat_user (input, unformat_line_input, line_input))
2211 return 0;
2212
2213 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2214 {
Dave Barach33909772019-09-23 10:27:27 -04002215 if (unformat (line_input, "rx"))
2216 rx_enable = 1;
2217 else if (unformat (line_input, "tx"))
2218 tx_enable = 1;
2219 else if (unformat (line_input, "drop"))
2220 drop_enable = 1;
2221 else if (unformat (line_input, "off"))
2222 rx_enable = tx_enable = drop_enable = 0;
2223 else if (unformat (line_input, "max-bytes-per-pkt %u",
2224 &max_bytes_per_pkt))
Dave Barachb97641c2019-09-09 16:38:17 -04002225 ;
2226 else if (unformat (line_input, "max %d", &max))
2227 ;
2228 else if (unformat (line_input, "packets-to-capture %d", &max))
2229 ;
2230 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2231 &filename))
2232 ;
2233 else if (unformat (line_input, "status %=", &status, 1))
2234 ;
2235 else if (unformat (line_input, "intfc %U",
2236 unformat_vnet_sw_interface, vnm, &sw_if_index))
2237 ;
Dave Barach586462f2020-08-05 16:12:35 -04002238 else if (unformat (line_input, "interface %U",
2239 unformat_vnet_sw_interface, vnm, &sw_if_index))
2240 ;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002241 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2242 &drop_err))
2243 ;
Dave Barach586462f2020-08-05 16:12:35 -04002244 else if (unformat (line_input, "preallocate-data %=",
2245 &preallocate_data, 1))
2246 ;
2247 else if (unformat (line_input, "free-data %=", &free_data, 1))
2248 ;
2249 else if (unformat (line_input, "intfc any")
2250 || unformat (line_input, "interface any"))
Dave Barachb97641c2019-09-09 16:38:17 -04002251 sw_if_index = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002252 else if (unformat (line_input, "filter"))
2253 filter = 1;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002254 else
2255 {
Dave Barachb97641c2019-09-09 16:38:17 -04002256 return clib_error_return (0, "unknown input `%U'",
2257 format_unformat_error, line_input);
Dave Barach5ecd5a52019-02-25 15:27:28 -05002258 }
2259 }
Dave Barachb97641c2019-09-09 16:38:17 -04002260
Dave Barach5ecd5a52019-02-25 15:27:28 -05002261 unformat_free (line_input);
2262
Dave Barachb97641c2019-09-09 16:38:17 -04002263 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2264 a->filename = filename;
Dave Barach33909772019-09-23 10:27:27 -04002265 a->rx_enable = rx_enable;
2266 a->tx_enable = tx_enable;
Dave Barach586462f2020-08-05 16:12:35 -04002267 a->preallocate_data = preallocate_data;
2268 a->free_data = free_data;
Dave Barach33909772019-09-23 10:27:27 -04002269 a->drop_enable = drop_enable;
Dave Barachb97641c2019-09-09 16:38:17 -04002270 a->status = status;
2271 a->packets_to_capture = max;
Dave Barachb97641c2019-09-09 16:38:17 -04002272 a->sw_if_index = sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002273 a->filter = filter;
Dave Barach33909772019-09-23 10:27:27 -04002274 a->max_bytes_per_pkt = max_bytes_per_pkt;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002275 a->drop_err = drop_err;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002276
Dave Barachb97641c2019-09-09 16:38:17 -04002277 rv = vnet_pcap_dispatch_trace_configure (a);
2278
2279 switch (rv)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002280 {
Dave Barachb97641c2019-09-09 16:38:17 -04002281 case 0:
2282 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002283
Dave Barachb97641c2019-09-09 16:38:17 -04002284 case VNET_API_ERROR_INVALID_VALUE:
2285 return clib_error_return (0, "dispatch trace already enabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002286
Dave Barachb97641c2019-09-09 16:38:17 -04002287 case VNET_API_ERROR_VALUE_EXIST:
2288 return clib_error_return (0, "dispatch trace already disabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002289
Dave Barachb97641c2019-09-09 16:38:17 -04002290 case VNET_API_ERROR_INVALID_VALUE_2:
2291 return clib_error_return
2292 (0, "can't change number of records to capture while tracing...");
2293
2294 case VNET_API_ERROR_SYSCALL_ERROR_1:
2295 return clib_error_return (0, "I/O writing trace capture...");
2296
2297 case VNET_API_ERROR_NO_SUCH_ENTRY:
2298 return clib_error_return (0, "No packets captured...");
2299
Dave Barach33909772019-09-23 10:27:27 -04002300 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2301 return clib_error_return (0,
2302 "Max bytes per pkt must be > 32, < 9000...");
2303
Dave Barach9137e542019-09-13 17:47:50 -04002304 case VNET_API_ERROR_NO_SUCH_LABEL:
2305 return clib_error_return
2306 (0, "No classify filter configured, see 'classify filter...'");
2307
Dave Barachb97641c2019-09-09 16:38:17 -04002308 default:
2309 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2310 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002311 }
Dave Barachb97641c2019-09-09 16:38:17 -04002312 return 0;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002313}
2314
Dave Barach5ecd5a52019-02-25 15:27:28 -05002315/*?
2316 * This command is used to start or stop a packet capture, or show
Dave Barach33909772019-09-23 10:27:27 -04002317 * the status of packet capture.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002318 *
2319 * This command has the following optional parameters:
2320 *
Dave Barach33909772019-09-23 10:27:27 -04002321 *
2322 * - <b>rx</b> - Capture received packets
2323 *
2324 * - <b>tx</b> - Capture transmitted packets
2325 *
2326 * - <b>drop</b> - Capture dropped packets
2327 *
2328 * - <b>off</b> - Stop capturing packets, write results to the specified file
Dave Barach5ecd5a52019-02-25 15:27:28 -05002329 *
2330 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2331 * of packets have been received, buffer is flushed to file. Once another
2332 * '<em>nn</em>' number of packets have been received, buffer is flushed
2333 * to file, overwriting previous write. If not entered, value defaults
2334 * to 100. Can only be updated if packet capture is off.
2335 *
Dave Barach33909772019-09-23 10:27:27 -04002336 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2337 * for each packet. Must be >= 32, <= 9000.
2338 *
Dave Barach586462f2020-08-05 16:12:35 -04002339 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2340 * vector expansion delays during pcap capture
2341 *
2342 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2343 * to retain the data buffer so this option is seldom used.
2344 *
Dave Barach33909772019-09-23 10:27:27 -04002345 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002346 * or use '<em>any</em>' to run packet capture on all interfaces.
2347 * '<em>any</em>' is the default if not provided. Settings from a previous
2348 * packet capture are preserved, so '<em>any</em>' can be used to reset
2349 * the interface setting.
2350 *
Dave Barachf5667c32019-09-25 11:27:46 -04002351 * - <b>filter</b> - Use the pcap rx / tx / drop trace filter, which
2352 * must be configured. Use <b>classify filter pcap...</b> to configure the
2353 * filter. The filter will only be executed if the per-interface or
2354 * any-interface tests fail.
2355 *
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002356 * - <b>error <node>.<error></b> - filter packets based on a specific error.
2357 * For example: error {ip4-udp-lookup}.{No listener for dst port}
2358 *
Dave Barach5ecd5a52019-02-25 15:27:28 -05002359 * - <b>file <name></b> - Used to specify the output filename. The file will
2360 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2361 * supported. Directory should not be entered. If file already exists, file
Dave Barach33909772019-09-23 10:27:27 -04002362 * will be overwritten. If no filename is provided, the file will be
2363 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2364 * Can only be updated if packet capture is off.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002365 *
2366 * - <b>status</b> - Displays the current status and configured attributes
2367 * associated with a packet capture. If packet capture is in progress,
2368 * '<em>status</em>' also will return the number of packets currently in
2369 * the local buffer. All additional attributes entered on command line
2370 * with '<em>status</em>' will be ignored and not applied.
2371 *
2372 * @cliexpar
2373 * Example of how to display the status of a tx packet capture when off:
Dave Barach33909772019-09-23 10:27:27 -04002374 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002375 * max is 100, for any interface to file /tmp/vpe.pcap
2376 * pcap tx capture is off...
2377 * @cliexend
2378 * Example of how to start a tx packet capture:
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002379 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
2380vppTest.pcap}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002381 * @cliexend
2382 * Example of how to display the status of a tx packet capture in progress:
Dave Barach33909772019-09-23 10:27:27 -04002383 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002384 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2385 * pcap tx capture is on: 20 of 35 pkts...
2386 * @cliexend
2387 * Example of how to stop a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002388 * @cliexstart{pcap trace off}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002389 * captured 21 pkts...
2390 * saved to /tmp/vppTest.pcap...
2391 * @cliexend
2392?*/
2393/* *INDENT-OFF* */
2394
2395VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
Dave Barach33909772019-09-23 10:27:27 -04002396 .path = "pcap trace",
Dave Barach5ecd5a52019-02-25 15:27:28 -05002397 .short_help =
Dave Barach586462f2020-08-05 16:12:35 -04002398 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2399 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2400 " [preallocate-data][free-data]",
Dave Barach33909772019-09-23 10:27:27 -04002401 .function = pcap_trace_command_fn,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002402};
2403/* *INDENT-ON* */
2404
Steven Luongf49734d2021-07-26 13:38:05 -07002405static clib_error_t *
2406set_interface_name (vlib_main_t *vm, unformat_input_t *input,
2407 vlib_cli_command_t *cmd)
2408{
2409 clib_error_t *error = 0;
2410 unformat_input_t _line_input, *line_input = &_line_input;
2411 vnet_main_t *vnm = vnet_get_main ();
2412 u32 hw_if_index = ~0;
2413 char *name = 0;
2414
2415 if (!unformat_user (input, unformat_line_input, line_input))
2416 return 0;
2417
2418 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2419 {
2420 if (unformat (line_input, "%U %s", unformat_vnet_hw_interface, vnm,
2421 &hw_if_index, &name))
2422 ;
2423 else
2424 {
2425 error = clib_error_return (0, "parse error: '%U'",
2426 format_unformat_error, line_input);
2427 unformat_free (line_input);
2428 vec_free (name);
2429 return error;
2430 }
2431 }
2432
2433 unformat_free (line_input);
2434
2435 if (hw_if_index == (u32) ~0 || name == 0)
2436 {
2437 vec_free (name);
2438 error = clib_error_return (0, "please specify valid interface name");
2439 return error;
2440 }
2441
2442 error = vnet_rename_interface (vnm, hw_if_index, name);
2443 vec_free (name);
2444
2445 return (error);
2446}
2447
2448VLIB_CLI_COMMAND (cmd_set_if_name, static) = {
2449 .path = "set interface name",
2450 .short_help = "set interface name <interface-name> <new-interface-name>",
2451 .function = set_interface_name,
2452 .is_mp_safe = 1,
2453};
Dave Barachba868bb2016-08-08 09:51:21 -04002454/*
2455 * fd.io coding-style-patch-verification: ON
2456 *
2457 * Local Variables:
2458 * eval: (c-set-style "gnu")
2459 * End:
2460 */