blob: 03e7436feadacbe9b6964600b5d90e267ad1def5 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * interface_cli.c: interface CLI
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
Chris Luke16bcf7d2016-09-01 14:31:46 -040040/**
41 * @file
Billy McFalle9bac692017-08-11 14:05:11 -040042 * @brief Interface CLI.
43 *
44 * Source code for several CLI interface commands.
45 *
Chris Luke16bcf7d2016-09-01 14:31:46 -040046 */
47
Ed Warnickecb9cada2015-12-08 15:45:58 -070048#include <vnet/vnet.h>
49#include <vnet/ip/ip.h>
John Lobcebbb92016-04-05 15:47:43 -040050#include <vppinfra/bitmap.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010051#include <vnet/fib/ip4_fib.h>
52#include <vnet/fib/ip6_fib.h>
Eyal Bari942402b2017-07-26 11:57:04 +030053#include <vnet/l2/l2_output.h>
54#include <vnet/l2/l2_input.h>
Neale Rannsba4a5bf2020-01-09 06:43:14 +000055#include <vnet/classify/vnet_classify.h>
Damjan Marion94100532020-11-06 23:25:57 +010056#include <vnet/interface/rx_queue_funcs.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
Dave Barachba868bb2016-08-08 09:51:21 -040058static int
59compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070060{
Dave Barachba868bb2016-08-08 09:51:21 -040061 u32 *hi1 = a1;
62 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barachba868bb2016-08-08 09:51:21 -040064 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070065}
66
67static clib_error_t *
68show_or_clear_hw_interfaces (vlib_main_t * vm,
69 unformat_input_t * input,
Benoît Ganne17814d72020-07-24 09:57:11 +020070 vlib_cli_command_t * cmd, int is_show)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Dave Barachba868bb2016-08-08 09:51:21 -040072 clib_error_t *error = 0;
73 vnet_main_t *vnm = vnet_get_main ();
74 vnet_interface_main_t *im = &vnm->interface_main;
75 vnet_hw_interface_t *hi;
76 u32 hw_if_index, *hw_if_indices = 0;
Benoît Ganne17814d72020-07-24 09:57:11 +020077 int i, verbose = -1, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
80 {
81 /* See if user wants to show a specific interface. */
Dave Barachba868bb2016-08-08 09:51:21 -040082 if (unformat
83 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
84 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040085
Sean Hope679ea792016-02-22 15:12:01 -050086 /* See if user wants to show an interface with a specific hw_if_index. */
87 else if (unformat (input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040088 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
90 else if (unformat (input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040091 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
93 else if (unformat (input, "detail"))
94 verbose = 2;
95
96 else if (unformat (input, "brief"))
97 verbose = 0;
98
John Lobcebbb92016-04-05 15:47:43 -040099 else if (unformat (input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -0400100 {
101 show_bond = 1;
102 if (verbose < 0)
103 verbose = 0; /* default to brief for link bonding */
104 }
John Lobcebbb92016-04-05 15:47:43 -0400105
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 else
107 {
108 error = clib_error_return (0, "unknown input `%U'",
109 format_unformat_error, input);
110 goto done;
111 }
112 }
Dave Barachba868bb2016-08-08 09:51:21 -0400113
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114 /* Gather interfaces. */
115 if (vec_len (hw_if_indices) == 0)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100116 pool_foreach (hi, im->hw_interfaces)
117 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Dave Barachba868bb2016-08-08 09:51:21 -0400119 if (verbose < 0)
120 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 if (is_show)
123 {
124 /* Sort by name. */
125 vec_sort_with_function (hw_if_indices, compare_interface_names);
126
127 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
128 for (i = 0; i < vec_len (hw_if_indices); i++)
129 {
130 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400131 if (show_bond == 0) /* show all interfaces */
132 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
133 hi, verbose);
134 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400135 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400136 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400137 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400138 vnet_hw_interface_t *shi;
139 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400140 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400141
142 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100143 clib_bitmap_foreach (hw_idx, hi->bond_info)
144 {
Dave Barachba868bb2016-08-08 09:51:21 -0400145 shi = vnet_get_hw_interface(vnm, hw_idx);
146 vlib_cli_output (vm, "%U\n",
147 format_vnet_hw_interface, vnm, shi, verbose);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100148 }
Dave Barachba868bb2016-08-08 09:51:21 -0400149 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400150 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 }
152 }
153 else
154 {
155 for (i = 0; i < vec_len (hw_if_indices); i++)
156 {
Dave Barachba868bb2016-08-08 09:51:21 -0400157 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
159 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
160 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400161
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 if (dc->clear_counters)
163 dc->clear_counters (hi->dev_instance);
164 }
165 }
166
Dave Barachba868bb2016-08-08 09:51:21 -0400167done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 vec_free (hw_if_indices);
169 return error;
170}
171
Benoît Ganne17814d72020-07-24 09:57:11 +0200172static clib_error_t *
173show_hw_interfaces (vlib_main_t * vm,
174 unformat_input_t * input, vlib_cli_command_t * cmd)
175{
176 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
177}
178
179static clib_error_t *
180clear_hw_interfaces (vlib_main_t * vm,
181 unformat_input_t * input, vlib_cli_command_t * cmd)
182{
183 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
184}
185
186
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700187/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400188 * Display more detailed information about all or a list of given interfaces.
189 * The verboseness of the output can be controlled by the following optional
190 * parameters:
191 * - brief: Only show name, index and state (default for bonded interfaces).
192 * - verbose: Also display additional attributes (default for all other interfaces).
193 * - detail: Also display all remaining attributes and extended statistics.
194 *
195 * To limit the output of the command to bonded interfaces and their slave
196 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700197 *
198 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400199 * Example of how to display default data for all interfaces:
200 * @cliexstart{show hardware-interfaces}
201 * Name Idx Link Hardware
202 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
203 * Ethernet address ec:f4:bb:c0:bc:fc
204 * Intel e1000
205 * carrier up full duplex speed 1000 mtu 9216
206 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
207 * cpu socket 0
208 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
209 * Ethernet address ec:f4:bb:c0:bc:fd
210 * Intel e1000
211 * carrier up full duplex speed 1000 mtu 9216
212 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
213 * cpu socket 0
214 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
215 * Ethernet address 02:fe:a5:a9:8b:8e
216 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
217 * Ethernet address 02:fe:c0:4e:3b:b0
218 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
219 * Ethernet address 02:fe:1f:73:92:81
220 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
221 * Ethernet address 02:fe:f2:25:c4:68
222 * local0 0 down local0
223 * local
224 * @cliexend
225 * Example of how to display '<em>verbose</em>' data for an interface by name and
226 * software index (where 2 is the software index):
227 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
228 * Name Idx Link Hardware
229 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
230 * Ethernet address ec:f4:bb:c0:bc:fc
231 * Intel e1000
232 * carrier up full duplex speed 1000 mtu 9216
233 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
234 * cpu socket 0
235 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
236 * Ethernet address ec:f4:bb:c0:bc:fd
237 * Intel e1000
238 * carrier up full duplex speed 1000 mtu 9216
239 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
240 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700241 * @cliexend
242 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400243/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
245 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400246 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
247 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200248 .function = show_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249};
Dave Barachba868bb2016-08-08 09:51:21 -0400250/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
Billy McFalle9bac692017-08-11 14:05:11 -0400252
253/*?
254 * Clear the extended statistics for all or a list of given interfaces
255 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
256 *
257 * @cliexpar
258 * Example of how to clear the extended statistics for all interfaces:
259 * @cliexcmd{clear hardware-interfaces}
260 * Example of how to clear the extended statistics for an interface by
261 * name and software index (where 2 is the software index):
262 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
263 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400264/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
266 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400267 .short_help = "clear hardware-interfaces "
268 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200269 .function = clear_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270};
Dave Barachba868bb2016-08-08 09:51:21 -0400271/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barachba868bb2016-08-08 09:51:21 -0400273static int
274sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275{
276 vnet_sw_interface_t *si1 = a1;
277 vnet_sw_interface_t *si2 = a2;
278
Dave Barachba868bb2016-08-08 09:51:21 -0400279 return vnet_sw_interface_compare (vnet_get_main (),
280 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281}
282
283static clib_error_t *
284show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400285 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286{
Dave Barachba868bb2016-08-08 09:51:21 -0400287 clib_error_t *error = 0;
288 vnet_main_t *vnm = vnet_get_main ();
Dave Barach525c9d02018-05-26 10:48:55 -0400289 unformat_input_t _linput, *linput = &_linput;
Dave Barachba868bb2016-08-08 09:51:21 -0400290 vnet_interface_main_t *im = &vnm->interface_main;
291 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200292 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200294 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500295 u8 show_tag = 0;
Jon Loeliger9485d992019-11-08 15:05:23 -0600296 u8 show_vtr = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400297 int verbose = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Dave Barach525c9d02018-05-26 10:48:55 -0400299 /*
300 * Get a line of input. Won't work if the user typed
301 * "show interface" and nothing more.
302 */
303 if (unformat_user (input, unformat_line_input, linput))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 {
Dave Barach525c9d02018-05-26 10:48:55 -0400305 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 {
Dave Barach525c9d02018-05-26 10:48:55 -0400307 /* See if user wants to show specific interface */
308 if (unformat
309 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
310 {
311 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
312 vec_add1 (sorted_sis, si[0]);
313 }
314 else if (unformat (linput, "address") || unformat (linput, "addr"))
315 show_addresses = 1;
316 else if (unformat (linput, "features") || unformat (linput, "feat"))
317 show_features = 1;
318 else if (unformat (linput, "tag"))
319 show_tag = 1;
Jon Loeliger9485d992019-11-08 15:05:23 -0600320 else if (unformat (linput, "vtr"))
321 show_vtr = 1;
Dave Barach525c9d02018-05-26 10:48:55 -0400322 else if (unformat (linput, "verbose"))
323 verbose = 1;
324 else
325 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400326 vec_free (sorted_sis);
Dave Barach525c9d02018-05-26 10:48:55 -0400327 error = clib_error_return (0, "unknown input `%U'",
328 format_unformat_error, linput);
329 goto done;
330 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 }
Dave Barach525c9d02018-05-26 10:48:55 -0400332 unformat_free (linput);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 }
Jon Loeliger9485d992019-11-08 15:05:23 -0600334 if (show_features || show_tag || show_vtr)
Damjan Marion22311502016-10-28 20:30:15 +0200335 {
336 if (sw_if_index == ~(u32) 0)
Dave Barach8fdde3c2019-05-17 10:46:40 -0400337 {
338 vec_free (sorted_sis);
339 return clib_error_return (0, "Interface not specified...");
340 }
Dave Barach7be864a2016-11-28 11:41:35 -0500341 }
Damjan Marion22311502016-10-28 20:30:15 +0200342
Dave Barach7be864a2016-11-28 11:41:35 -0500343 if (show_features)
344 {
Dave Barach525c9d02018-05-26 10:48:55 -0400345 vnet_interface_features_show (vm, sw_if_index, verbose);
Neale Ranns47a3d992020-09-29 15:38:51 +0000346 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
Eyal Bari942402b2017-07-26 11:57:04 +0300347
348 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
349 vlib_cli_output (vm, "\nl2-output:");
350 if (l2_output->out_vtr_flag)
351 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
352 vlib_cli_output (vm, "%U", format_l2_output_features,
Neale Ranns5d9df1d2018-11-09 08:19:27 -0800353 l2_output->feature_bitmap, 1);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400354 vec_free (sorted_sis);
Damjan Marion22311502016-10-28 20:30:15 +0200355 return 0;
356 }
Dave Barach7be864a2016-11-28 11:41:35 -0500357 if (show_tag)
358 {
359 u8 *tag;
360 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
361 vlib_cli_output (vm, "%U: %s",
362 format_vnet_sw_if_index_name, vnm, sw_if_index,
363 tag ? (char *) tag : "(none)");
Dave Barach8fdde3c2019-05-17 10:46:40 -0400364 vec_free (sorted_sis);
Dave Barach7be864a2016-11-28 11:41:35 -0500365 return 0;
366 }
Damjan Marion22311502016-10-28 20:30:15 +0200367
Jon Loeliger9485d992019-11-08 15:05:23 -0600368 /*
369 * Show vlan tag rewrite data for one interface.
370 */
371 if (show_vtr)
372 {
373 u32 vtr_op = L2_VTR_DISABLED;
374 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
375
376 if (l2vtr_get (vm, vnm, sw_if_index,
377 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
378 {
379 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
380 format_vnet_sw_if_index_name, vnm, sw_if_index);
381 return 0;
382 }
383 vlib_cli_output (vm, "%U: VTR %0U",
384 format_vnet_sw_if_index_name, vnm, sw_if_index,
385 format_vtr, vtr_op, push_dot1q, tag1, tag2);
386 return 0;
387 }
388
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400390 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Dave Barachba868bb2016-08-08 09:51:21 -0400392 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700393 {
394 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400395 sorted_sis =
396 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700397 _vec_len (sorted_sis) = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400398 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100399 pool_foreach (si, im->sw_interfaces)
400 {
Dave Barach525c9d02018-05-26 10:48:55 -0400401 int visible = vnet_swif_is_api_visible (si);
402 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100403 vec_add1 (sorted_sis, si[0]);
404 }
Ole Troand7231612018-06-07 10:17:57 +0200405 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406 /* Sort by name. */
407 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
408 }
409
410 if (show_addresses)
411 {
412 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400413 {
Dave Barachba868bb2016-08-08 09:51:21 -0400414 ip4_main_t *im4 = &ip4_main;
415 ip6_main_t *im6 = &ip6_main;
416 ip_lookup_main_t *lm4 = &im4->lookup_main;
417 ip_lookup_main_t *lm6 = &im6->lookup_main;
418 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400419 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420
Dave Barachba868bb2016-08-08 09:51:21 -0400421 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
422 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
423 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Dave Barachba868bb2016-08-08 09:51:21 -0400425 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
426 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
427 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
John Lo4478d8e2018-01-12 17:15:25 -0500429 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
430 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Dave Barachba868bb2016-08-08 09:51:21 -0400432 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
433 vlib_cli_output
434 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500435 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400436 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
437 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400438 else
John Lo4478d8e2018-01-12 17:15:25 -0500439 vlib_cli_output
440 (vm, "%U (%s):",
441 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
442 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Eyal Bari942402b2017-07-26 11:57:04 +0300444 /* Display any L2 info */
Neale Ranns47a3d992020-09-29 15:38:51 +0000445 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400446
John Lo4478d8e2018-01-12 17:15:25 -0500447 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400448 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500449 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
450 1 /* honor unnumbered */,
451 ({
452 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
453 if (fib4->table_id)
454 vlib_cli_output (vm, " L3 %U/%d ip4 table-id %d fib-idx %d",
455 format_ip4_address, r4, ia->address_length,
456 fib4->table_id,
457 ip4_fib_index_from_table_id (fib4->table_id));
458 else
459 vlib_cli_output (vm, " L3 %U/%d",
460 format_ip4_address, r4, ia->address_length);
461 }));
462 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
John Lo4478d8e2018-01-12 17:15:25 -0500464 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400465 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500466 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
467 1 /* honor unnumbered */,
468 ({
469 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
470 if (fib6->table_id)
471 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
472 format_ip6_address, r6, ia->address_length,
473 fib6->table_id,
474 ip6_fib_index_from_table_id (fib6->table_id));
475 else
476 vlib_cli_output (vm, " L3 %U/%d",
477 format_ip6_address, r6, ia->address_length);
478 }));
479 /* *INDENT-ON* */
Ole Troand7231612018-06-07 10:17:57 +0200480 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 }
Ole Troand7231612018-06-07 10:17:57 +0200482 else
483 {
484 vec_foreach (si, sorted_sis)
485 {
486 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
487 }
488 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
Dave Barachba868bb2016-08-08 09:51:21 -0400490done:
Ole Troand7231612018-06-07 10:17:57 +0200491 vec_free (sorted_sis);
492 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493}
494
Dave Barachba868bb2016-08-08 09:51:21 -0400495/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400497 .path = "show interface",
Jon Loeliger9485d992019-11-08 15:05:23 -0600498 .short_help = "show interface [address|addr|features|feat|vtr] [<interface> [<interface> [..]]] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 .function = show_sw_interfaces,
Steven Luongc5fa2b82019-04-25 11:19:49 -0700500 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700501};
Dave Barachba868bb2016-08-08 09:51:21 -0400502/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
504/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400505/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
507 .path = "interface",
508 .short_help = "Interface commands",
509};
Dave Barachba868bb2016-08-08 09:51:21 -0400510/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511
Dave Barachba868bb2016-08-08 09:51:21 -0400512/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
514 .path = "set interface",
515 .short_help = "Interface commands",
516};
Dave Barachba868bb2016-08-08 09:51:21 -0400517/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518
519static clib_error_t *
520clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400521 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522{
Dave Barachba868bb2016-08-08 09:51:21 -0400523 vnet_main_t *vnm = vnet_get_main ();
524 vnet_interface_main_t *im = &vnm->interface_main;
525 vlib_simple_counter_main_t *sm;
526 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400527 int j, n_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
529 n_counters = vec_len (im->combined_sw_if_counters);
530
531 for (j = 0; j < n_counters; j++)
532 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400533 im = &vnm->interface_main;
534 cm = im->combined_sw_if_counters + j;
535 vlib_clear_combined_counters (cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 }
537
538 n_counters = vec_len (im->sw_if_counters);
539
540 for (j = 0; j < n_counters; j++)
541 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400542 im = &vnm->interface_main;
543 sm = im->sw_if_counters + j;
544 vlib_clear_simple_counters (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 }
546
547 return 0;
548}
549
Billy McFalle9bac692017-08-11 14:05:11 -0400550/*?
551 * Clear the statistics for all interfaces (statistics associated with the
552 * '<em>show interface</em>' command).
553 *
554 * @cliexpar
555 * Example of how to clear the statistics for all interfaces:
556 * @cliexcmd{clear interfaces}
557 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400558/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
560 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400561 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 .function = clear_interface_counters,
563};
Dave Barachba868bb2016-08-08 09:51:21 -0400564/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Chris Luke16bcf7d2016-09-01 14:31:46 -0400566/**
567 * Parse subinterface names.
568 *
Dave Barachba868bb2016-08-08 09:51:21 -0400569 * The following subinterface syntax is supported. The first two are for
570 * backwards compatability:
571 *
572 * <intf-name> <id>
573 * - a subinterface with the name <intf-name>.<id>. The subinterface
574 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
575 *
576 * <intf-name> <min_id>-<max_id>
577 * - a set of the above subinterfaces, repeating for each id
578 * in the range <min_id> to <max_id>
579 *
580 * In the following, exact-match semantics (i.e. the number of vlan tags on the
581 * packet must match the number of tags in the configuration) are used only if
582 * the keyword exact-match is present. Non-exact match is the default.
583 *
584 * <intf-name> <id> dot1q <outer_id> [exact-match]
585 * - a subinterface with the name <intf-name>.<id>. The subinterface
586 * is a single dot1q vlan with vlan id <outer_id>.
587 *
588 * <intf-name> <id> dot1q any [exact-match]
589 * - a subinterface with the name <intf-name>.<id>. The subinterface
590 * is a single dot1q vlan with any vlan id.
591 *
592 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
593 * - a subinterface with the name <intf-name>.<id>. The subinterface
594 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
595 * <inner_id>.
596 *
597 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
598 * - a subinterface with the name <intf-name>.<id>. The subinterface
599 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
600 *
601 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
602 *
603 * - a subinterface with the name <intf-name>.<id>. The subinterface
604 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
605 *
606 * For each of the above CLI, there is a duplicate that uses the keyword
607 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
608 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
609 * tagged packets the inner ethertype is always 0x8100. Also note that
610 * the dot1q and dot1ad naming spaces are independent, so it is legal to
611 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
612 *
613 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
614 * - a subinterface with the name <intf-name>.<id>. The subinterface
615 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
616 * id <inner_id>.
617 *
618 * <intf-name> <id> untagged
619 * - a subinterface with the name <intf-name>.<id>. The subinterface
620 * has no vlan tags. Only one can be specified per interface.
621 *
622 * <intf-name> <id> default
623 * - a subinterface with the name <intf-name>.<id>. This is associated
624 * with a packet that did not match any other configured subinterface
625 * on this interface. Only one can be specified per interface.
626 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627
628static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400629parse_vlan_sub_interfaces (unformat_input_t * input,
630 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631{
Dave Barachba868bb2016-08-08 09:51:21 -0400632 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700633 u32 inner_vlan, outer_vlan;
634
Dave Barachba868bb2016-08-08 09:51:21 -0400635 if (unformat (input, "any inner-dot1q any"))
636 {
637 template->sub.eth.flags.two_tags = 1;
638 template->sub.eth.flags.outer_vlan_id_any = 1;
639 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700640 }
Dave Barachba868bb2016-08-08 09:51:21 -0400641 else if (unformat (input, "any"))
642 {
643 template->sub.eth.flags.one_tag = 1;
644 template->sub.eth.flags.outer_vlan_id_any = 1;
645 }
646 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
647 {
648 template->sub.eth.flags.two_tags = 1;
649 template->sub.eth.flags.inner_vlan_id_any = 1;
650 template->sub.eth.outer_vlan_id = outer_vlan;
651 }
652 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
653 {
654 template->sub.eth.flags.two_tags = 1;
655 template->sub.eth.outer_vlan_id = outer_vlan;
656 template->sub.eth.inner_vlan_id = inner_vlan;
657 }
658 else if (unformat (input, "%d", &outer_vlan))
659 {
660 template->sub.eth.flags.one_tag = 1;
661 template->sub.eth.outer_vlan_id = outer_vlan;
662 }
663 else
664 {
665 error = clib_error_return (0, "expected dot1q config, got `%U'",
666 format_unformat_error, input);
667 goto done;
668 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700669
Dave Barachba868bb2016-08-08 09:51:21 -0400670 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
671 {
672 if (unformat (input, "exact-match"))
673 {
674 template->sub.eth.flags.exact_match = 1;
675 }
676 }
677
678done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700679 return error;
680}
681
682static clib_error_t *
683create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400684 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685{
Dave Barachba868bb2016-08-08 09:51:21 -0400686 vnet_main_t *vnm = vnet_get_main ();
687 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700688 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400689 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700690 u32 id, id_min, id_max;
691 vnet_sw_interface_t template;
692
693 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400694 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700695 {
696 error = clib_error_return (0, "unknown interface `%U'",
697 format_unformat_error, input);
698 goto done;
699 }
700
Dave Barachb7b92992018-10-17 10:38:51 -0400701 clib_memset (&template, 0, sizeof (template));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 template.sub.eth.raw_flags = 0;
703
Dave Barachba868bb2016-08-08 09:51:21 -0400704 if (unformat (input, "%d default", &id_min))
705 {
706 id_max = id_min;
707 template.sub.eth.flags.default_sub = 1;
708 }
709 else if (unformat (input, "%d untagged", &id_min))
710 {
711 id_max = id_min;
712 template.sub.eth.flags.no_tags = 1;
713 template.sub.eth.flags.exact_match = 1;
714 }
715 else if (unformat (input, "%d dot1q", &id_min))
716 {
717 /* parse dot1q config */
718 id_max = id_min;
719 error = parse_vlan_sub_interfaces (input, &template);
720 if (error)
721 goto done;
722 }
723 else if (unformat (input, "%d dot1ad", &id_min))
724 {
725 /* parse dot1ad config */
726 id_max = id_min;
727 template.sub.eth.flags.dot1ad = 1;
728 error = parse_vlan_sub_interfaces (input, &template);
729 if (error)
730 goto done;
731 }
732 else if (unformat (input, "%d-%d", &id_min, &id_max))
733 {
734 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400735 template.sub.eth.flags.exact_match = 1;
736 if (id_min > id_max)
737 goto id_error;
738 }
739 else if (unformat (input, "%d", &id_min))
740 {
741 id_max = id_min;
742 template.sub.eth.flags.one_tag = 1;
743 template.sub.eth.outer_vlan_id = id_min;
744 template.sub.eth.flags.exact_match = 1;
745 }
746 else
747 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748 id_error:
749 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
750 format_unformat_error, input);
751 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400752 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700753
754 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400755
Dave Barachba868bb2016-08-08 09:51:21 -0400756 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
757 {
758 error =
759 clib_error_return (0,
760 "not allowed as %v belong to a BondEthernet interface",
761 hi->name);
762 goto done;
763 }
John Lobcebbb92016-04-05 15:47:43 -0400764
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765 for (id = id_min; id <= id_max; id++)
766 {
Dave Barachba868bb2016-08-08 09:51:21 -0400767 uword *p;
768 vnet_interface_main_t *im = &vnm->interface_main;
769 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
770 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700771
772 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
773 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400774 {
775 if (CLIB_DEBUG > 0)
776 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
777 hi->sw_if_index, id);
778 continue;
779 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200782 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700783 template.sup_sw_if_index = hi->sw_if_index;
784 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300785 if (id_min < id_max)
786 template.sub.eth.outer_vlan_id = id;
787
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400789 if (error)
790 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400791
Jon Loeligerb22e1f02019-12-19 09:03:52 -0600792 kp = clib_mem_alloc (sizeof (*kp));
793 *kp = sup_and_sub_key;
794
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
796 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400797 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
798 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 }
800
Dave Barachba868bb2016-08-08 09:51:21 -0400801done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 return error;
803}
804
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700805/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400806 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
807 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
808 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
809 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
810 * but this is not recommended.
811 *
812 * This command has several variations:
813 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
814 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
815 *
816 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
817 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
818 * match any other subinterfaces should be sent to this subinterface.
819 *
820 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
821 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
822 * to this subinterface.
823 *
824 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
825 * subinterfaces to handle a range of VLAN IDs.
826 *
827 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700828 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
Billy McFalle9bac692017-08-11 14:05:11 -0400829 * VLAN ID different from the '<em>subId</em>'.
830 *
831 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
832 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700833 * the inner VLAN ID.
Billy McFalle9bac692017-08-11 14:05:11 -0400834 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700835 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
Billy McFalle9bac692017-08-11 14:05:11 -0400836 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
837 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
838 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
839 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
840 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
841 * entered, then the default behavior is exact-match.
842 *
843 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700844 *
845 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400846 * @parblock
847 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
848 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700849 *
Billy McFalle9bac692017-08-11 14:05:11 -0400850 * The previous example is shorthand and is equivalent to:
851 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700852 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700853 *
Billy McFalle9bac692017-08-11 14:05:11 -0400854 * Example of how to create a subinterface number that is different from the VLAN ID:
855 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700856 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700857 *
Billy McFalle9bac692017-08-11 14:05:11 -0400858 * Examples of how to create q-in-q and q-in-any subinterfaces:
859 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
860 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700861 *
Billy McFalle9bac692017-08-11 14:05:11 -0400862 * Examples of how to create dot1ad interfaces:
863 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
864 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700865 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700866 *
Billy McFalle9bac692017-08-11 14:05:11 -0400867 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
868 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
869 * is non-exact match:
870 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700871 *
Billy McFalle9bac692017-08-11 14:05:11 -0400872 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
873 * is specified and only one VLAN is configured, but packet contains two VLANs:
874 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700875 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700876 *
Billy McFalle9bac692017-08-11 14:05:11 -0400877 * Example of how to created a subinterface to process untagged packets:
878 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
879 *
880 * Example of how to created a subinterface to process any packet with a VLAN ID that
881 * does not match any other subinterface:
882 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
883 *
884 * When subinterfaces are created, they are in the down state. Example of how to
885 * enable a newly created subinterface:
886 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
887 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700888 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400889/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700890VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700891 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400892 .short_help = "create sub-interfaces <interface> "
893 "{<subId> [default|untagged]} | "
894 "{<subId>-<subId>} | "
895 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896 .function = create_sub_interfaces,
897};
Dave Barachba868bb2016-08-08 09:51:21 -0400898/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899
900static clib_error_t *
901set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400902 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903{
Dave Barachba868bb2016-08-08 09:51:21 -0400904 vnet_main_t *vnm = vnet_get_main ();
905 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 u32 sw_if_index, flags;
907
908 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400909 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910 {
911 error = clib_error_return (0, "unknown interface `%U'",
912 format_unformat_error, input);
913 goto done;
914 }
915
Dave Barachba868bb2016-08-08 09:51:21 -0400916 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 {
918 error = clib_error_return (0, "unknown flags `%U'",
919 format_unformat_error, input);
920 goto done;
921 }
922
923 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
924 if (error)
925 goto done;
926
Dave Barachba868bb2016-08-08 09:51:21 -0400927done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 return error;
929}
930
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700931
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700932/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400933 * This command is used to change the admin state (up/down) of an interface.
934 *
935 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
936 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
937 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
938 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700939 *
940 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400941 * Example of how to configure the admin state of an interface to '<em>up</em?':
942 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
943 * Example of how to configure the admin state of an interface to '<em>down</em?':
944 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700945 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400946/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947VLIB_CLI_COMMAND (set_state_command, static) = {
948 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400949 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950 .function = set_state,
951};
Dave Barachba868bb2016-08-08 09:51:21 -0400952/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
954static clib_error_t *
955set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400956 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957{
Dave Barachba868bb2016-08-08 09:51:21 -0400958 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700959 u32 unnumbered_sw_if_index = ~0;
960 u32 inherit_from_sw_if_index = ~0;
961 int enable = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700963 if (unformat (input, "%U use %U",
964 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
965 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700966 enable = 1;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700967 else if (unformat (input, "del %U",
968 unformat_vnet_sw_interface, vnm,
969 &unnumbered_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700970 enable = 0;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700971 else
972 return clib_error_return (0, "parse error '%U'",
973 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700975 if (~0 == unnumbered_sw_if_index)
976 return clib_error_return (0, "Specify the unnumbered interface");
977 if (enable && ~0 == inherit_from_sw_if_index)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700978 return clib_error_return (0, "When enabling unnumbered specify the"
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700979 " IP enabled interface that it uses");
Neale Ranns898273f2017-03-18 02:57:38 -0700980
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700981 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
982 inherit_from_sw_if_index, enable);
Neale Ranns898273f2017-03-18 02:57:38 -0700983
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700984 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985}
986
Dave Barachba868bb2016-08-08 09:51:21 -0400987/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
989 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -0400990 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 .function = set_unnumbered,
992};
Dave Barachba868bb2016-08-08 09:51:21 -0400993/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
995
996
997static clib_error_t *
998set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400999 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000{
Dave Barachba868bb2016-08-08 09:51:21 -04001001 vnet_main_t *vnm = vnet_get_main ();
1002 vnet_interface_main_t *im = &vnm->interface_main;
1003 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004 u32 hw_if_index, hw_class_index;
1005
1006 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001007 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001008 {
1009 error = clib_error_return (0, "unknown hardware interface `%U'",
1010 format_unformat_error, input);
1011 goto done;
1012 }
1013
Dave Barachba868bb2016-08-08 09:51:21 -04001014 if (!unformat_user (input, unformat_hash_string,
1015 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 {
1017 error = clib_error_return (0, "unknown hardware class `%U'",
1018 format_unformat_error, input);
1019 goto done;
1020 }
1021
1022 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1023 if (error)
1024 goto done;
1025
Dave Barachba868bb2016-08-08 09:51:21 -04001026done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 return error;
1028}
1029
Dave Barachba868bb2016-08-08 09:51:21 -04001030/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1032 .path = "set interface hw-class",
1033 .short_help = "Set interface hardware class",
1034 .function = set_hw_class,
1035};
Dave Barachba868bb2016-08-08 09:51:21 -04001036/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037
Dave Barachba868bb2016-08-08 09:51:21 -04001038static clib_error_t *
1039vnet_interface_cli_init (vlib_main_t * vm)
1040{
1041 return 0;
1042}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043
1044VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1045
Dave Barachba868bb2016-08-08 09:51:21 -04001046static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001048 unformat_input_t * input,
1049 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050{
1051 u32 hw_if_index;
1052 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001053 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054 int rv;
1055
Dave Barachba868bb2016-08-08 09:51:21 -04001056 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001058 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001059
Dave Barachba868bb2016-08-08 09:51:21 -04001060 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061 return clib_error_return (0, "new dev instance missing");
1062
1063 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1064
1065 switch (rv)
1066 {
1067 case 0:
1068 break;
1069
1070 default:
1071 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001072 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
1074 }
1075
1076 return 0;
1077}
1078
1079
Dave Barachba868bb2016-08-08 09:51:21 -04001080/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1082 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001083 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 .function = renumber_interface_command_fn,
1085};
Dave Barachba868bb2016-08-08 09:51:21 -04001086/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
Damjan Marion8358ff92016-04-15 14:26:00 +02001088static clib_error_t *
1089promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001090 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001091{
Dave Barachba868bb2016-08-08 09:51:21 -04001092 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001093 u32 hw_if_index;
1094 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001095 ethernet_main_t *em = &ethernet_main;
1096 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001097
1098 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001099 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001100 ;
1101 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001102 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001103 flags = 0;
1104 else
1105 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001106 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001107
1108 eif = ethernet_get_interface (em, hw_if_index);
1109 if (!eif)
1110 return clib_error_return (0, "not supported");
1111
1112 ethernet_set_flags (vnm, hw_if_index, flags);
1113 return 0;
1114}
1115
Dave Barachba868bb2016-08-08 09:51:21 -04001116/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001117VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1118 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001119 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001120 .function = promiscuous_cmd,
1121};
Dave Barachba868bb2016-08-08 09:51:21 -04001122/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001123
1124static clib_error_t *
1125mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1126{
Dave Barachba868bb2016-08-08 09:51:21 -04001127 vnet_main_t *vnm = vnet_get_main ();
Ole Troand7231612018-06-07 10:17:57 +02001128 u32 hw_if_index, sw_if_index, mtu;
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001129 ethernet_main_t *em = &ethernet_main;
Ole Troand7231612018-06-07 10:17:57 +02001130 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
Damjan Marion8358ff92016-04-15 14:26:00 +02001131
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001132 if (unformat (input, "%d %U", &mtu,
1133 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001134 {
Ole Troand7231612018-06-07 10:17:57 +02001135 /*
1136 * Change physical MTU on interface. Only supported for Ethernet
1137 * interfaces
1138 */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001139 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1140 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1141
1142 if (!eif)
1143 return clib_error_return (0, "not supported");
1144
1145 if (mtu < hi->min_supported_packet_bytes)
1146 return clib_error_return (0, "Invalid mtu (%d): "
1147 "must be >= min pkt bytes (%d)", mtu,
1148 hi->min_supported_packet_bytes);
1149
1150 if (mtu > hi->max_supported_packet_bytes)
1151 return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1152 hi->max_supported_packet_bytes);
1153
1154 vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
Ole Troand7231612018-06-07 10:17:57 +02001155 goto done;
Damjan Marion8358ff92016-04-15 14:26:00 +02001156 }
Ole Troand7231612018-06-07 10:17:57 +02001157 else if (unformat (input, "packet %d %U", &mtu,
1158 unformat_vnet_sw_interface, vnm, &sw_if_index))
1159 /* Set default packet MTU (including L3 header */
1160 mtus[VNET_MTU_L3] = mtu;
1161 else if (unformat (input, "ip4 %d %U", &mtu,
1162 unformat_vnet_sw_interface, vnm, &sw_if_index))
1163 mtus[VNET_MTU_IP4] = mtu;
1164 else if (unformat (input, "ip6 %d %U", &mtu,
1165 unformat_vnet_sw_interface, vnm, &sw_if_index))
1166 mtus[VNET_MTU_IP6] = mtu;
1167 else if (unformat (input, "mpls %d %U", &mtu,
1168 unformat_vnet_sw_interface, vnm, &sw_if_index))
1169 mtus[VNET_MTU_MPLS] = mtu;
Damjan Marion8358ff92016-04-15 14:26:00 +02001170 else
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001171 return clib_error_return (0, "unknown input `%U'",
1172 format_unformat_error, input);
Ole Troand7231612018-06-07 10:17:57 +02001173
1174 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1175
1176done:
Damjan Marion8358ff92016-04-15 14:26:00 +02001177 return 0;
1178}
1179
Dave Barachba868bb2016-08-08 09:51:21 -04001180/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001181VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1182 .path = "set interface mtu",
Ole Troand7231612018-06-07 10:17:57 +02001183 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001184 .function = mtu_cmd,
1185};
Dave Barachba868bb2016-08-08 09:51:21 -04001186/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001187
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001188static clib_error_t *
Matthew Smithe0792fd2019-07-12 11:48:24 -05001189show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1190 vlib_cli_command_t * cmd)
1191{
1192 vnet_main_t *vnm = vnet_get_main ();
1193 vnet_interface_main_t *im = &vnm->interface_main;
1194 ethernet_main_t *em = &ethernet_main;
1195 u32 sw_if_index = ~0;
1196 vnet_sw_interface_t *si, *sorted_sis = 0;
1197
1198 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1199 {
1200 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1201 vec_add1 (sorted_sis, si[0]);
1202 }
1203
1204 /* if an interface name was not passed, get all interfaces */
1205 if (vec_len (sorted_sis) == 0)
1206 {
1207 sorted_sis =
1208 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1209 _vec_len (sorted_sis) = 0;
1210 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001211 pool_foreach (si, im->sw_interfaces)
1212 {
Matthew Smithe0792fd2019-07-12 11:48:24 -05001213 int visible = vnet_swif_is_api_visible (si);
1214 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +01001215 vec_add1 (sorted_sis, si[0]);
1216 }
Matthew Smithe0792fd2019-07-12 11:48:24 -05001217 /* *INDENT-ON* */
1218 /* Sort by name. */
1219 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1220 }
1221
1222 vec_foreach (si, sorted_sis)
1223 {
1224 vnet_sw_interface_t *sup_si;
1225 ethernet_interface_t *ei;
1226
1227 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1228 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1229
1230 vlib_cli_output (vm, "%U (%s):",
1231 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1232 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1233 "up" : "dn");
1234
1235 if (ei && ei->secondary_addrs)
1236 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001237 ethernet_interface_address_t *sec_addr;
Matthew Smithe0792fd2019-07-12 11:48:24 -05001238
1239 vec_foreach (sec_addr, ei->secondary_addrs)
1240 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001241 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001242 }
1243 }
1244 }
1245
1246 vec_free (sorted_sis);
1247 return 0;
1248}
1249
1250/*?
1251 * This command is used to display interface secondary mac addresses.
1252 *
1253 * @cliexpar
1254 * Example of how to display interface secondary mac addresses:
1255 * @cliexstart{show interface secondary-mac-address}
1256 * @cliexend
1257?*/
1258/* *INDENT-OFF* */
1259VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1260 .path = "show interface secondary-mac-address",
1261 .short_help = "show interface secondary-mac-address [<interface>]",
1262 .function = show_interface_sec_mac_addr_fn,
1263};
1264/* *INDENT-ON* */
1265
1266static clib_error_t *
1267interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1268 vlib_cli_command_t * cmd)
1269{
1270 vnet_main_t *vnm = vnet_get_main ();
1271 vnet_sw_interface_t *si = NULL;
1272 clib_error_t *error = 0;
1273 u32 sw_if_index = ~0;
1274 u8 mac[6] = { 0 };
1275 u8 is_add, is_del;
1276
1277 is_add = is_del = 0;
1278
1279 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1280 {
1281 error = clib_error_return (0, "unknown interface `%U'",
1282 format_unformat_error, input);
1283 goto done;
1284 }
1285 if (!unformat_user (input, unformat_ethernet_address, mac))
1286 {
1287 error = clib_error_return (0, "expected mac address `%U'",
1288 format_unformat_error, input);
1289 goto done;
1290 }
1291
1292 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1293 {
1294 if (unformat (input, "add"))
1295 is_add = 1;
1296 else if (unformat (input, "del"))
1297 is_del = 1;
1298 else
1299 break;
1300 }
1301
1302 if (is_add == is_del)
1303 {
1304 error = clib_error_return (0, "must choose one of add or del");
1305 goto done;
1306 }
1307
1308 si = vnet_get_sw_interface (vnm, sw_if_index);
1309 error =
1310 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1311
1312done:
1313 return error;
1314}
1315
1316/*?
1317 * The '<em>set interface secondary-mac-address </em>' command allows adding
1318 * or deleting extra MAC addresses on a given interface without changing the
1319 * default MAC address. This could allow packets sent to these MAC addresses
1320 * to be received without setting the interface to promiscuous mode.
1321 * Not all interfaces support this operation. The ones that do are mostly
1322 * hardware NICs, though virtio does also.
1323 *
1324 * @cliexpar
1325 * @parblock
1326 * Example of how to add a secondary MAC Address on an interface:
1327 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1328 * Example of how to delete a secondary MAC address from an interface:
1329 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1330 * @endparblock
1331?*/
1332/* *INDENT-OFF* */
1333VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1334 .path = "set interface secondary-mac-address",
1335 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1336 .function = interface_add_del_mac_address,
1337};
1338/* *INDENT-ON* */
1339
1340static clib_error_t *
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001341set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1342 vlib_cli_command_t * cmd)
1343{
1344 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001345 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001346 clib_error_t *error = 0;
1347 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001348 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001349
1350 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1351 {
1352 error = clib_error_return (0, "unknown interface `%U'",
1353 format_unformat_error, input);
1354 goto done;
1355 }
John Lo62fcc0a2017-10-31 14:31:10 -04001356 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001357 {
1358 error = clib_error_return (0, "expected mac address `%U'",
1359 format_unformat_error, input);
1360 goto done;
1361 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001362 si = vnet_get_sw_interface (vnm, sw_if_index);
1363 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001364done:
1365 return error;
1366}
1367
1368/*?
1369 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1370 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1371 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1372 *
1373 * @cliexpar
1374 * @parblock
1375 * Example of how to change MAC Address of interface:
1376 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1377 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1378 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1379 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1380 * @endparblock
1381?*/
1382/* *INDENT-OFF* */
1383VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1384 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001385 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001386 .function = set_interface_mac_address,
1387};
1388/* *INDENT-ON* */
1389
Dave Barach7be864a2016-11-28 11:41:35 -05001390static clib_error_t *
1391set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1392{
1393 vnet_main_t *vnm = vnet_get_main ();
1394 u32 sw_if_index = ~0;
1395 u8 *tag = 0;
1396
1397 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1398 vnm, &sw_if_index, &tag))
1399 return clib_error_return (0, "unknown input `%U'",
1400 format_unformat_error, input);
1401
1402 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1403
1404 return 0;
1405}
1406
1407/* *INDENT-OFF* */
1408VLIB_CLI_COMMAND (set_tag_command, static) = {
1409 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001410 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001411 .function = set_tag,
1412};
1413/* *INDENT-ON* */
1414
1415static clib_error_t *
1416clear_tag (vlib_main_t * vm, unformat_input_t * input,
1417 vlib_cli_command_t * cmd)
1418{
1419 vnet_main_t *vnm = vnet_get_main ();
1420 u32 sw_if_index = ~0;
1421
1422 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1423 return clib_error_return (0, "unknown input `%U'",
1424 format_unformat_error, input);
1425
1426 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1427
1428 return 0;
1429}
1430
1431/* *INDENT-OFF* */
1432VLIB_CLI_COMMAND (clear_tag_command, static) = {
1433 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001434 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001435 .function = clear_tag,
1436};
1437/* *INDENT-ON* */
1438
Damjan Marion44036902017-04-28 12:29:15 +02001439static clib_error_t *
Neale Ranns1855b8e2018-07-11 10:31:26 -07001440set_ip_directed_broadcast (vlib_main_t * vm,
1441 unformat_input_t * input, vlib_cli_command_t * cmd)
1442{
1443 vnet_main_t *vnm = vnet_get_main ();
1444 u32 sw_if_index = ~0;
1445 u8 enable = 0;
1446
1447 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1448 else if (unformat (input, "enable"))
1449 enable = 1;
1450 else if (unformat (input, "disable"))
1451 enable = 0;
1452 else
1453 return clib_error_return (0, "unknown input: `%U'",
1454 format_unformat_error, input);
1455
1456 if (~0 == sw_if_index)
1457 return clib_error_return (0, "specify an interface: `%U'",
1458 format_unformat_error, input);
1459
1460 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1461
1462 return 0;
1463}
1464
1465/*?
1466 * This command is used to enable/disable IP directed broadcast
1467 * If directed broadcast is enabled a packet sent to the interface's
1468 * subnet broadcast address will be sent L2 broadcast on the interface,
1469 * otherwise it is dropped.
1470 ?*/
1471/* *INDENT-OFF* */
1472VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1473 .path = "set interface ip directed-broadcast",
1474 .short_help = "set interface enable <interface> <enable|disable>",
1475 .function = set_ip_directed_broadcast,
1476};
1477/* *INDENT-ON* */
1478
1479static clib_error_t *
Stevene3a395c2017-05-09 16:19:50 -07001480set_hw_interface_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
Damjan Marioneabd4242020-10-07 20:59:07 +02001481 u32 queue_id, vnet_hw_if_rx_mode mode)
Stevene3a395c2017-05-09 16:19:50 -07001482{
1483 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1484 vnet_device_class_t *dev_class =
1485 vnet_get_device_class (vnm, hw->dev_class_index);
1486 clib_error_t *error;
Damjan Marioneabd4242020-10-07 20:59:07 +02001487 vnet_hw_if_rx_mode old_mode;
Stevene3a395c2017-05-09 16:19:50 -07001488 int rv;
1489
Damjan Marioneabd4242020-10-07 20:59:07 +02001490 if (mode == VNET_HW_IF_RX_MODE_DEFAULT)
Damjan Marion4e53a0d2017-06-21 14:29:44 +02001491 mode = hw->default_rx_mode;
1492
Stevene3a395c2017-05-09 16:19:50 -07001493 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &old_mode);
1494 switch (rv)
1495 {
1496 case 0:
1497 if (old_mode == mode)
1498 return 0; /* same rx-mode, no change */
1499 break;
1500 case VNET_API_ERROR_INVALID_INTERFACE:
1501 return clib_error_return (0, "invalid interface");
Stevenbd8a6112017-07-30 10:29:26 -07001502 case VNET_API_ERROR_INVALID_QUEUE:
1503 return clib_error_return (0, "invalid queue");
Stevene3a395c2017-05-09 16:19:50 -07001504 default:
1505 return clib_error_return (0, "unknown error");
1506 }
1507
1508 if (dev_class->rx_mode_change_function)
1509 {
1510 error = dev_class->rx_mode_change_function (vnm, hw_if_index, queue_id,
1511 mode);
1512 if (error)
1513 return (error);
1514 }
1515
1516 rv = vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1517 switch (rv)
1518 {
1519 case 0:
1520 break;
1521 case VNET_API_ERROR_UNSUPPORTED:
1522 return clib_error_return (0, "unsupported");
1523 case VNET_API_ERROR_INVALID_INTERFACE:
1524 return clib_error_return (0, "invalid interface");
Stevenbd8a6112017-07-30 10:29:26 -07001525 case VNET_API_ERROR_INVALID_QUEUE:
1526 return clib_error_return (0, "invalid queue");
Stevene3a395c2017-05-09 16:19:50 -07001527 default:
1528 return clib_error_return (0, "unknown error");
1529 }
1530
1531 return 0;
1532}
1533
Stevenad8015b2017-10-29 22:10:46 -07001534clib_error_t *
1535set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1536 u8 queue_id_valid, u32 queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001537 vnet_hw_if_rx_mode mode)
Stevenad8015b2017-10-29 22:10:46 -07001538{
1539 clib_error_t *error = 0;
1540 vnet_hw_interface_t *hw;
Damjan Marion94100532020-11-06 23:25:57 +01001541 u32 *queue_indices = 0;
Stevenad8015b2017-10-29 22:10:46 -07001542 int i;
1543
1544 hw = vnet_get_hw_interface (vnm, hw_if_index);
1545
Damjan Marion94100532020-11-06 23:25:57 +01001546 /* to be deprecated */
1547 if (vec_len (hw->rx_queue_indices) == 0)
Stevenad8015b2017-10-29 22:10:46 -07001548 {
Damjan Marion94100532020-11-06 23:25:57 +01001549 if (queue_id_valid == 0)
Stevenad8015b2017-10-29 22:10:46 -07001550 {
Damjan Marion94100532020-11-06 23:25:57 +01001551 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1552 {
1553 error = set_hw_interface_rx_mode (vnm, hw_if_index, i, mode);
1554 if (error)
1555 break;
1556 }
1557 hw->default_rx_mode = mode;
Stevenad8015b2017-10-29 22:10:46 -07001558 }
Damjan Marion94100532020-11-06 23:25:57 +01001559 else
1560 error = set_hw_interface_rx_mode (vnm, hw_if_index, queue_id, mode);
1561
1562 return (error);
1563 }
1564
1565 if (queue_id_valid)
1566 {
1567 u32 queue_index;
1568 queue_index =
1569 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1570 if (queue_index == ~0)
1571 return clib_error_return (0, "unknown queue %u on interface %s",
1572 queue_id, hw->name);
1573 vec_add1 (queue_indices, queue_index);
Stevenad8015b2017-10-29 22:10:46 -07001574 }
1575 else
Damjan Marion94100532020-11-06 23:25:57 +01001576 queue_indices = hw->rx_queue_indices;
Stevenad8015b2017-10-29 22:10:46 -07001577
Damjan Marion94100532020-11-06 23:25:57 +01001578 for (int i = 0; i < vec_len (queue_indices); i++)
1579 {
1580 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1581 if (rv)
1582 goto done;
1583 }
1584
1585done:
1586 if (queue_indices != hw->rx_queue_indices)
1587 vec_free (queue_indices);
1588 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1589 return error;
Stevenad8015b2017-10-29 22:10:46 -07001590}
1591
Stevene3a395c2017-05-09 16:19:50 -07001592static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001593set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1594 vlib_cli_command_t * cmd)
1595{
1596 clib_error_t *error = 0;
1597 unformat_input_t _line_input, *line_input = &_line_input;
1598 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001599 u32 hw_if_index = (u32) ~ 0;
1600 u32 queue_id = (u32) ~ 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001601 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001602 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001603
Damjan Marion44036902017-04-28 12:29:15 +02001604 if (!unformat_user (input, unformat_line_input, line_input))
1605 return 0;
1606
1607 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1608 {
1609 if (unformat
1610 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1611 ;
1612 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001613 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001614 else if (unformat (line_input, "polling"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001615 mode = VNET_HW_IF_RX_MODE_POLLING;
Damjan Marion44036902017-04-28 12:29:15 +02001616 else if (unformat (line_input, "interrupt"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001617 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
Damjan Marion44036902017-04-28 12:29:15 +02001618 else if (unformat (line_input, "adaptive"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001619 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
Damjan Marion44036902017-04-28 12:29:15 +02001620 else
1621 {
1622 error = clib_error_return (0, "parse error: '%U'",
1623 format_unformat_error, line_input);
1624 unformat_free (line_input);
1625 return error;
1626 }
1627 }
1628
1629 unformat_free (line_input);
1630
1631 if (hw_if_index == (u32) ~ 0)
1632 return clib_error_return (0, "please specify valid interface name");
1633
Damjan Marioneabd4242020-10-07 20:59:07 +02001634 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Damjan Marion44036902017-04-28 12:29:15 +02001635 return clib_error_return (0, "please specify valid rx-mode");
1636
Stevenad8015b2017-10-29 22:10:46 -07001637 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1638 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001639
Stevene3a395c2017-05-09 16:19:50 -07001640 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001641}
1642
1643/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001644 * This command is used to assign the RX packet processing mode (polling,
1645 * interrupt, adaptive) of the a given interface, and optionally a
1646 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1647 * is applied to all queues of the interface. Not all interfaces support
1648 * all modes. To display the current rx-mode use the command
1649 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001650 *
1651 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001652 * Example of how to assign rx-mode to all queues on an interface:
1653 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1654 * Example of how to assign rx-mode to one queue of an interface:
1655 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1656 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001657 * @cliexstart{show interface rx-placement}
1658 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001659 * node dpdk-input:
1660 * GigabitEthernet7/0/0 queue 0 (polling)
1661 * node vhost-user-input:
1662 * VirtualEthernet0/0/12 queue 0 (interrupt)
1663 * VirtualEthernet0/0/12 queue 2 (polling)
1664 * VirtualEthernet0/0/13 queue 0 (polling)
1665 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001666 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001667 * node dpdk-input:
1668 * GigabitEthernet7/0/1 queue 0 (polling)
1669 * node vhost-user-input:
1670 * VirtualEthernet0/0/12 queue 1 (polling)
1671 * VirtualEthernet0/0/12 queue 3 (polling)
1672 * VirtualEthernet0/0/13 queue 1 (polling)
1673 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001674 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001675?*/
1676/* *INDENT-OFF* */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001677VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
Damjan Marion44036902017-04-28 12:29:15 +02001678 .path = "set interface rx-mode",
1679 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1680 .function = set_interface_rx_mode,
1681};
1682/* *INDENT-ON* */
1683
1684static clib_error_t *
1685show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1686 vlib_cli_command_t * cmd)
1687{
1688 u8 *s = 0;
1689 vnet_main_t *vnm = vnet_get_main ();
1690 vnet_device_input_runtime_t *rt;
1691 vnet_device_and_queue_t *dq;
1692 vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
1693 uword si;
1694 int index = 0;
1695
1696 /* *INDENT-OFF* */
1697 foreach_vlib_main (({
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001698 clib_bitmap_foreach (si, pn->sibling_bitmap)
1699 {
Damjan Marion44036902017-04-28 12:29:15 +02001700 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1701
1702 if (vec_len (rt->devices_and_queues))
1703 s = format (s, " node %U:\n", format_vlib_node_name, vm, si);
1704
1705 vec_foreach (dq, rt->devices_and_queues)
1706 {
Steven9d6d9892017-06-30 07:15:02 -07001707 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm,
1708 dq->hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001709 s = format (s, " %U queue %u (%U)\n",
Steven9d6d9892017-06-30 07:15:02 -07001710 format_vnet_sw_if_index_name, vnm, hi->sw_if_index,
Damjan Marion44036902017-04-28 12:29:15 +02001711 dq->queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001712 format_vnet_hw_if_rx_mode, dq->mode);
Damjan Marion44036902017-04-28 12:29:15 +02001713 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01001714 }
Damjan Marion44036902017-04-28 12:29:15 +02001715 if (vec_len (s) > 0)
1716 {
Steven84f36cb2018-09-26 10:44:54 -07001717 vlib_cli_output(vm, "Thread %u (%s):\n%v", index,
Damjan Marion44036902017-04-28 12:29:15 +02001718 vlib_worker_threads[index].name, s);
1719 vec_reset_length (s);
1720 }
1721 index++;
1722 }));
1723 /* *INDENT-ON* */
1724
1725 vec_free (s);
1726 return 0;
1727}
1728
Billy McFalle9bac692017-08-11 14:05:11 -04001729/*?
1730 * This command is used to display the interface and queue worker
1731 * thread placement.
1732 *
1733 * @cliexpar
1734 * Example of how to display the interface placement:
1735 * @cliexstart{show interface rx-placement}
1736 * Thread 1 (vpp_wk_0):
1737 * node dpdk-input:
1738 * GigabitEthernet7/0/0 queue 0 (polling)
1739 * node vhost-user-input:
1740 * VirtualEthernet0/0/12 queue 0 (polling)
1741 * VirtualEthernet0/0/12 queue 2 (polling)
1742 * VirtualEthernet0/0/13 queue 0 (polling)
1743 * VirtualEthernet0/0/13 queue 2 (polling)
1744 * Thread 2 (vpp_wk_1):
1745 * node dpdk-input:
1746 * GigabitEthernet7/0/1 queue 0 (polling)
1747 * node vhost-user-input:
1748 * VirtualEthernet0/0/12 queue 1 (polling)
1749 * VirtualEthernet0/0/12 queue 3 (polling)
1750 * VirtualEthernet0/0/13 queue 1 (polling)
1751 * VirtualEthernet0/0/13 queue 3 (polling)
1752 * @cliexend
1753?*/
Damjan Marion44036902017-04-28 12:29:15 +02001754/* *INDENT-OFF* */
1755VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1756 .path = "show interface rx-placement",
1757 .short_help = "show interface rx-placement",
1758 .function = show_interface_rx_placement_fn,
1759};
1760/* *INDENT-ON* */
1761
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001762clib_error_t *
1763set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1764 u32 thread_index, u8 is_main)
Damjan Marion44036902017-04-28 12:29:15 +02001765{
Damjan Marion44036902017-04-28 12:29:15 +02001766 vnet_main_t *vnm = vnet_get_main ();
1767 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion94100532020-11-06 23:25:57 +01001768 vnet_hw_interface_t *hw;
1769 u32 queue_index;
Damjan Marion44036902017-04-28 12:29:15 +02001770 int rv;
1771
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001772 if (is_main)
1773 thread_index = 0;
1774 else
1775 thread_index += vdm->first_worker_thread_index;
Damjan Marion44036902017-04-28 12:29:15 +02001776
1777 if (thread_index > vdm->last_worker_thread_index)
1778 return clib_error_return (0,
1779 "please specify valid worker thread or main");
1780
Damjan Marion94100532020-11-06 23:25:57 +01001781 hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001782
Damjan Marion94100532020-11-06 23:25:57 +01001783 /* to be deprecated */
1784 if (vec_len (hw->rx_queue_indices) == 0)
1785 {
1786 clib_error_t *error = 0;
1787 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
1788 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &mode);
Damjan Marion44036902017-04-28 12:29:15 +02001789
Damjan Marion94100532020-11-06 23:25:57 +01001790 if (rv)
1791 return clib_error_return (0, "not found");
Damjan Marion44036902017-04-28 12:29:15 +02001792
Damjan Marion94100532020-11-06 23:25:57 +01001793 rv = vnet_hw_interface_unassign_rx_thread (vnm, hw_if_index, queue_id);
Damjan Marion44036902017-04-28 12:29:15 +02001794
Damjan Marion94100532020-11-06 23:25:57 +01001795 if (rv)
1796 return clib_error_return (0, "not found");
Damjan Marion44036902017-04-28 12:29:15 +02001797
Damjan Marion94100532020-11-06 23:25:57 +01001798 vnet_hw_interface_assign_rx_thread (vnm, hw_if_index, queue_id,
1799 thread_index);
1800 vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1801
1802 return (error);
1803 }
1804
1805 queue_index =
1806 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1807 if (queue_index == ~0)
1808 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1809 hw->name);
1810 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1811 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1812 return 0;
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001813}
1814
1815static clib_error_t *
1816set_interface_rx_placement (vlib_main_t * vm, unformat_input_t * input,
1817 vlib_cli_command_t * cmd)
1818{
1819 clib_error_t *error = 0;
1820 unformat_input_t _line_input, *line_input = &_line_input;
1821 vnet_main_t *vnm = vnet_get_main ();
1822 u32 hw_if_index = (u32) ~ 0;
1823 u32 queue_id = (u32) 0;
1824 u32 thread_index = (u32) ~ 0;
1825 u8 is_main = 0;
1826
1827 if (!unformat_user (input, unformat_line_input, line_input))
1828 return 0;
1829
1830 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1831 {
1832 if (unformat
1833 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1834 ;
1835 else if (unformat (line_input, "queue %d", &queue_id))
1836 ;
1837 else if (unformat (line_input, "main", &thread_index))
1838 is_main = 1;
1839 else if (unformat (line_input, "worker %d", &thread_index))
1840 ;
1841 else
1842 {
1843 error = clib_error_return (0, "parse error: '%U'",
1844 format_unformat_error, line_input);
1845 unformat_free (line_input);
1846 return error;
1847 }
1848 }
1849
1850 unformat_free (line_input);
1851
1852 if (hw_if_index == (u32) ~ 0)
1853 return clib_error_return (0, "please specify valid interface name");
1854
1855 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1856 is_main);
1857
1858 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001859}
1860
1861/*?
1862 * This command is used to assign a given interface, and optionally a
1863 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001864 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1865 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001866 *
1867 * @cliexpar
1868 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001869 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001870 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001871 * node dpdk-input:
1872 * GigabitEthernet7/0/0 queue 0 (polling)
1873 * node vhost-user-input:
1874 * VirtualEthernet0/0/12 queue 0 (polling)
1875 * VirtualEthernet0/0/12 queue 2 (polling)
1876 * VirtualEthernet0/0/13 queue 0 (polling)
1877 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001878 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001879 * node dpdk-input:
1880 * GigabitEthernet7/0/1 queue 0 (polling)
1881 * node vhost-user-input:
1882 * VirtualEthernet0/0/12 queue 1 (polling)
1883 * VirtualEthernet0/0/12 queue 3 (polling)
1884 * VirtualEthernet0/0/13 queue 1 (polling)
1885 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001886 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001887 * Example of how to assign a interface and queue to a worker thread:
1888 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1889 * Example of how to display the interface placement:
1890 * @cliexstart{show interface rx-placement}
1891 * Thread 1 (vpp_wk_0):
1892 * node dpdk-input:
1893 * GigabitEthernet7/0/0 queue 0 (polling)
1894 * node vhost-user-input:
1895 * VirtualEthernet0/0/12 queue 0 (polling)
1896 * VirtualEthernet0/0/12 queue 1 (polling)
1897 * VirtualEthernet0/0/12 queue 2 (polling)
1898 * VirtualEthernet0/0/13 queue 0 (polling)
1899 * VirtualEthernet0/0/13 queue 2 (polling)
1900 * Thread 2 (vpp_wk_1):
1901 * node dpdk-input:
1902 * GigabitEthernet7/0/1 queue 0 (polling)
1903 * node vhost-user-input:
1904 * VirtualEthernet0/0/12 queue 3 (polling)
1905 * VirtualEthernet0/0/13 queue 1 (polling)
1906 * VirtualEthernet0/0/13 queue 3 (polling)
1907 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001908?*/
1909/* *INDENT-OFF* */
1910VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1911 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001912 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001913 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001914 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001915 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001916};
Damjan Marion44036902017-04-28 12:29:15 +02001917/* *INDENT-ON* */
Dave Barach5ecd5a52019-02-25 15:27:28 -05001918
Chenmin Sunc4665092020-07-06 08:20:39 +08001919clib_error_t *
1920set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1921 clib_bitmap_t * bitmap)
1922{
1923 vnet_main_t *vnm = vnet_get_main ();
1924 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1925
1926 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1927}
1928
1929static clib_error_t *
1930set_interface_rss_queues_fn (vlib_main_t * vm,
1931 unformat_input_t * input,
1932 vlib_cli_command_t * cmd)
1933{
1934 clib_error_t *error = 0;
1935 unformat_input_t _line_input, *line_input = &_line_input;
1936 vnet_main_t *vnm = vnet_get_main ();
1937 u32 hw_if_index = (u32) ~ 0;
1938 clib_bitmap_t *bitmap = NULL;
1939
1940 if (!unformat_user (input, unformat_line_input, line_input))
1941 return 0;
1942
1943 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1944 {
1945 if (unformat
1946 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1947 ;
1948 else
1949 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
1950 ;
1951 else
1952 {
1953 error = clib_error_return (0, "parse error: '%U'",
1954 format_unformat_error, line_input);
1955 unformat_free (line_input);
1956 goto done;
1957 }
1958 }
1959
1960 unformat_free (line_input);
1961
1962 if (hw_if_index == (u32) ~ 0)
1963 {
1964 error = clib_error_return (0, "please specify valid interface name");
1965 goto done;
1966 }
1967
1968 if (bitmap == NULL)
1969 {
1970 error = clib_error_return (0, "please specify the valid rss queues");
1971 goto done;
1972 }
1973
1974 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
1975
1976done:
1977 if (bitmap)
1978 clib_bitmap_free (bitmap);
1979
1980 return (error);
1981}
1982
1983/*?
1984 * This command is used to set the rss queues of a given interface
1985 * Not all the interfaces support this operation.
1986 * To display the current rss queues, use the command
1987 * '<em>show hardware-interfaces</em>'.
1988 *
1989 * @cliexpar
1990 * Example of how to set the rss queues to 0,2-5,7 of an interface:
1991 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
1992 * @cliexend
1993?*/
1994/* *INDENT-OFF* */
1995VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
1996 .path = "set interface rss queues",
1997 .short_help = "set interface rss queues <interface> <list <queue-list>>",
1998 .function = set_interface_rss_queues_fn,
1999};
2000/* *INDENT-ON* */
2001
Dave Barach33909772019-09-23 10:27:27 -04002002static u8 *
2003format_vnet_pcap (u8 * s, va_list * args)
2004{
2005 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2006 int type = va_arg (*args, int);
2007 int printed = 0;
2008
2009 if (type == 0)
2010 {
2011 if (pp->pcap_rx_enable)
2012 {
2013 s = format (s, "rx");
2014 printed = 1;
2015 }
2016 if (pp->pcap_tx_enable)
2017 {
2018 if (printed)
2019 s = format (s, " and ");
2020 s = format (s, "tx");
2021 printed = 1;
2022 }
2023 if (pp->pcap_drop_enable)
2024 {
2025 if (printed)
2026 s = format (s, " and ");
2027 s = format (s, "drop");
2028 printed = 1;
2029 }
2030 return s;
2031 }
2032 s = format (s, "unknown type %d!", type);
2033 return s;
2034}
2035
2036
Dave Barachb97641c2019-09-09 16:38:17 -04002037int
2038vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2039{
2040 vlib_main_t *vm = vlib_get_main ();
Dave Barach33909772019-09-23 10:27:27 -04002041 vnet_pcap_t *pp = &vm->pcap;
Dave Barachb97641c2019-09-09 16:38:17 -04002042 pcap_main_t *pm = &pp->pcap_main;
Dave Barachf5667c32019-09-25 11:27:46 -04002043 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachb97641c2019-09-09 16:38:17 -04002044
2045 if (a->status)
2046 {
Dave Barach33909772019-09-23 10:27:27 -04002047 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002048 {
2049 vlib_cli_output
Dave Barach33909772019-09-23 10:27:27 -04002050 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2051 format_vnet_pcap, pp, 0 /* print type */ ,
Dave Barachb97641c2019-09-09 16:38:17 -04002052 pm->n_packets_captured, pm->n_packets_to_capture);
2053 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2054 }
2055 else
Dave Barach33909772019-09-23 10:27:27 -04002056 vlib_cli_output (vm, "pcap dispatch capture disabled");
2057
Dave Barachb97641c2019-09-09 16:38:17 -04002058 return 0;
2059 }
2060
2061 /* Consistency checks */
2062
2063 /* Enable w/ capture already enabled not allowed */
Dave Barach33909772019-09-23 10:27:27 -04002064 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2065 && (a->rx_enable + a->tx_enable + a->drop_enable))
Dave Barachb97641c2019-09-09 16:38:17 -04002066 return VNET_API_ERROR_INVALID_VALUE;
2067
2068 /* Disable capture with capture already disabled, not interesting */
Dave Barach33909772019-09-23 10:27:27 -04002069 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) == 0)
2070 && ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
Dave Barachb97641c2019-09-09 16:38:17 -04002071 return VNET_API_ERROR_VALUE_EXIST;
2072
2073 /* Change number of packets to capture while capturing */
Dave Barach33909772019-09-23 10:27:27 -04002074 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2075 && (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002076 && (pm->n_packets_to_capture != a->packets_to_capture))
2077 return VNET_API_ERROR_INVALID_VALUE_2;
2078
Dave Barach33909772019-09-23 10:27:27 -04002079 /* Classify filter specified, but no classify filter configured */
Dave Barachf5667c32019-09-25 11:27:46 -04002080 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002081 cm->classify_table_index_by_sw_if_index[0] == ~0)
Dave Barach9137e542019-09-13 17:47:50 -04002082 return VNET_API_ERROR_NO_SUCH_LABEL;
2083
Dave Barach33909772019-09-23 10:27:27 -04002084 if (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04002085 {
Dave Barach586462f2020-08-05 16:12:35 -04002086 void *save_pcap_data;
2087
Dave Barach33909772019-09-23 10:27:27 -04002088 /* Sanity check max bytes per pkt */
2089 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2090 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2091
Dave Barachb97641c2019-09-09 16:38:17 -04002092 /* Clean up from previous run, if any */
Dave Barach586462f2020-08-05 16:12:35 -04002093 vec_reset_length (pm->pcap_data);
2094
2095 /* Throw away the data buffer? */
2096 if (a->free_data)
2097 vec_free (pm->pcap_data);
2098
2099 save_pcap_data = pm->pcap_data;
2100
Dave Barachb97641c2019-09-09 16:38:17 -04002101 memset (pm, 0, sizeof (*pm));
2102
Dave Barach586462f2020-08-05 16:12:35 -04002103 pm->pcap_data = save_pcap_data;
2104
Dave Barach11fb09e2020-08-06 12:10:09 -04002105 vec_validate_aligned (vnet_trace_placeholder, 2048,
2106 CLIB_CACHE_LINE_BYTES);
Dave Barachb97641c2019-09-09 16:38:17 -04002107 if (pm->lock == 0)
2108 clib_spinlock_init (&(pm->lock));
2109
2110 if (a->filename == 0)
Dave Barach33909772019-09-23 10:27:27 -04002111 {
2112 u8 *stem = 0;
2113
2114 if (a->rx_enable)
2115 stem = format (stem, "rx");
2116 if (a->tx_enable)
2117 stem = format (stem, "tx");
2118 if (a->drop_enable)
2119 stem = format (stem, "drop");
Benoît Ganne7d4a9972020-07-17 11:49:56 +02002120 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
Dave Barach33909772019-09-23 10:27:27 -04002121 vec_free (stem);
2122 }
Dave Barachb97641c2019-09-09 16:38:17 -04002123
2124 pm->file_name = (char *) a->filename;
2125 pm->n_packets_captured = 0;
2126 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
Dave Barach586462f2020-08-05 16:12:35 -04002127 /* Preallocate the data vector? */
2128 if (a->preallocate_data)
2129 {
2130 vec_validate
2131 (pm->pcap_data, a->packets_to_capture
2132 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2133 vec_reset_length (pm->pcap_data);
2134 }
Dave Barachb97641c2019-09-09 16:38:17 -04002135 pm->n_packets_to_capture = a->packets_to_capture;
2136 pp->pcap_sw_if_index = a->sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002137 if (a->filter)
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002138 pp->filter_classify_table_index =
2139 cm->classify_table_index_by_sw_if_index[0];
Dave Barach9137e542019-09-13 17:47:50 -04002140 else
2141 pp->filter_classify_table_index = ~0;
Dave Barach33909772019-09-23 10:27:27 -04002142 pp->pcap_rx_enable = a->rx_enable;
2143 pp->pcap_tx_enable = a->tx_enable;
2144 pp->pcap_drop_enable = a->drop_enable;
2145 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
Dave Barachb97641c2019-09-09 16:38:17 -04002146 }
2147 else
2148 {
Dave Barach33909772019-09-23 10:27:27 -04002149 pp->pcap_rx_enable = 0;
2150 pp->pcap_tx_enable = 0;
2151 pp->pcap_drop_enable = 0;
Dave Barachf5667c32019-09-25 11:27:46 -04002152 pp->filter_classify_table_index = ~0;
Dave Barachb97641c2019-09-09 16:38:17 -04002153 if (pm->n_packets_captured)
2154 {
2155 clib_error_t *error;
2156 pm->n_packets_to_capture = pm->n_packets_captured;
2157 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2158 pm->n_packets_captured, pm->file_name);
2159 error = pcap_write (pm);
Andrew Yourtchenko4da15062019-09-11 14:14:43 +00002160 if (pm->flags & PCAP_MAIN_INIT_DONE)
Dave Barachb97641c2019-09-09 16:38:17 -04002161 pcap_close (pm);
2162 /* Report I/O errors... */
2163 if (error)
2164 {
2165 clib_error_report (error);
2166 return VNET_API_ERROR_SYSCALL_ERROR_1;
2167 }
Dave Barach586462f2020-08-05 16:12:35 -04002168 vec_free (pm->file_name);
2169 if (a->free_data)
2170 vec_free (pm->pcap_data);
Dave Barachb97641c2019-09-09 16:38:17 -04002171 return 0;
2172 }
2173 else
2174 return VNET_API_ERROR_NO_SUCH_ENTRY;
2175 }
2176
2177 return 0;
2178}
2179
2180static clib_error_t *
Dave Barach33909772019-09-23 10:27:27 -04002181pcap_trace_command_fn (vlib_main_t * vm,
2182 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002183{
Dave Barach5ecd5a52019-02-25 15:27:28 -05002184 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb97641c2019-09-09 16:38:17 -04002185 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002186 vnet_main_t *vnm = vnet_get_main ();
Dave Barachb97641c2019-09-09 16:38:17 -04002187 u8 *filename = 0;
2188 u32 max = 1000;
Dave Barach33909772019-09-23 10:27:27 -04002189 u32 max_bytes_per_pkt = 512;
Dave Barachb97641c2019-09-09 16:38:17 -04002190 int rv;
Dave Barach33909772019-09-23 10:27:27 -04002191 int rx_enable = 0;
2192 int tx_enable = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002193 int preallocate_data = 0;
Dave Barach33909772019-09-23 10:27:27 -04002194 int drop_enable = 0;
Dave Barachb97641c2019-09-09 16:38:17 -04002195 int status = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002196 int filter = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002197 int free_data = 0;
Dave Barach4330c462020-06-10 17:07:32 -04002198 u32 sw_if_index = 0; /* default: any interface */
Dave Barach5ecd5a52019-02-25 15:27:28 -05002199
2200 /* Get a line of input. */
2201 if (!unformat_user (input, unformat_line_input, line_input))
2202 return 0;
2203
2204 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2205 {
Dave Barach33909772019-09-23 10:27:27 -04002206 if (unformat (line_input, "rx"))
2207 rx_enable = 1;
2208 else if (unformat (line_input, "tx"))
2209 tx_enable = 1;
2210 else if (unformat (line_input, "drop"))
2211 drop_enable = 1;
2212 else if (unformat (line_input, "off"))
2213 rx_enable = tx_enable = drop_enable = 0;
2214 else if (unformat (line_input, "max-bytes-per-pkt %u",
2215 &max_bytes_per_pkt))
Dave Barachb97641c2019-09-09 16:38:17 -04002216 ;
2217 else if (unformat (line_input, "max %d", &max))
2218 ;
2219 else if (unformat (line_input, "packets-to-capture %d", &max))
2220 ;
2221 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2222 &filename))
2223 ;
2224 else if (unformat (line_input, "status %=", &status, 1))
2225 ;
2226 else if (unformat (line_input, "intfc %U",
2227 unformat_vnet_sw_interface, vnm, &sw_if_index))
2228 ;
Dave Barach586462f2020-08-05 16:12:35 -04002229 else if (unformat (line_input, "interface %U",
2230 unformat_vnet_sw_interface, vnm, &sw_if_index))
2231 ;
2232 else if (unformat (line_input, "preallocate-data %=",
2233 &preallocate_data, 1))
2234 ;
2235 else if (unformat (line_input, "free-data %=", &free_data, 1))
2236 ;
2237 else if (unformat (line_input, "intfc any")
2238 || unformat (line_input, "interface any"))
Dave Barachb97641c2019-09-09 16:38:17 -04002239 sw_if_index = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002240 else if (unformat (line_input, "filter"))
2241 filter = 1;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002242 else
2243 {
Dave Barachb97641c2019-09-09 16:38:17 -04002244 return clib_error_return (0, "unknown input `%U'",
2245 format_unformat_error, line_input);
Dave Barach5ecd5a52019-02-25 15:27:28 -05002246 }
2247 }
Dave Barachb97641c2019-09-09 16:38:17 -04002248
Dave Barach5ecd5a52019-02-25 15:27:28 -05002249 unformat_free (line_input);
2250
Dave Barachb97641c2019-09-09 16:38:17 -04002251 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2252 a->filename = filename;
Dave Barach33909772019-09-23 10:27:27 -04002253 a->rx_enable = rx_enable;
2254 a->tx_enable = tx_enable;
Dave Barach586462f2020-08-05 16:12:35 -04002255 a->preallocate_data = preallocate_data;
2256 a->free_data = free_data;
Dave Barach33909772019-09-23 10:27:27 -04002257 a->drop_enable = drop_enable;
Dave Barachb97641c2019-09-09 16:38:17 -04002258 a->status = status;
2259 a->packets_to_capture = max;
Dave Barachb97641c2019-09-09 16:38:17 -04002260 a->sw_if_index = sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002261 a->filter = filter;
Dave Barach33909772019-09-23 10:27:27 -04002262 a->max_bytes_per_pkt = max_bytes_per_pkt;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002263
Dave Barachb97641c2019-09-09 16:38:17 -04002264 rv = vnet_pcap_dispatch_trace_configure (a);
2265
2266 switch (rv)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002267 {
Dave Barachb97641c2019-09-09 16:38:17 -04002268 case 0:
2269 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002270
Dave Barachb97641c2019-09-09 16:38:17 -04002271 case VNET_API_ERROR_INVALID_VALUE:
2272 return clib_error_return (0, "dispatch trace already enabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002273
Dave Barachb97641c2019-09-09 16:38:17 -04002274 case VNET_API_ERROR_VALUE_EXIST:
2275 return clib_error_return (0, "dispatch trace already disabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002276
Dave Barachb97641c2019-09-09 16:38:17 -04002277 case VNET_API_ERROR_INVALID_VALUE_2:
2278 return clib_error_return
2279 (0, "can't change number of records to capture while tracing...");
2280
2281 case VNET_API_ERROR_SYSCALL_ERROR_1:
2282 return clib_error_return (0, "I/O writing trace capture...");
2283
2284 case VNET_API_ERROR_NO_SUCH_ENTRY:
2285 return clib_error_return (0, "No packets captured...");
2286
Dave Barach33909772019-09-23 10:27:27 -04002287 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2288 return clib_error_return (0,
2289 "Max bytes per pkt must be > 32, < 9000...");
2290
Dave Barach9137e542019-09-13 17:47:50 -04002291 case VNET_API_ERROR_NO_SUCH_LABEL:
2292 return clib_error_return
2293 (0, "No classify filter configured, see 'classify filter...'");
2294
Dave Barachb97641c2019-09-09 16:38:17 -04002295 default:
2296 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2297 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002298 }
Dave Barachb97641c2019-09-09 16:38:17 -04002299 return 0;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002300}
2301
Dave Barach5ecd5a52019-02-25 15:27:28 -05002302/*?
2303 * This command is used to start or stop a packet capture, or show
Dave Barach33909772019-09-23 10:27:27 -04002304 * the status of packet capture.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002305 *
2306 * This command has the following optional parameters:
2307 *
Dave Barach33909772019-09-23 10:27:27 -04002308 *
2309 * - <b>rx</b> - Capture received packets
2310 *
2311 * - <b>tx</b> - Capture transmitted packets
2312 *
2313 * - <b>drop</b> - Capture dropped packets
2314 *
2315 * - <b>off</b> - Stop capturing packets, write results to the specified file
Dave Barach5ecd5a52019-02-25 15:27:28 -05002316 *
2317 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2318 * of packets have been received, buffer is flushed to file. Once another
2319 * '<em>nn</em>' number of packets have been received, buffer is flushed
2320 * to file, overwriting previous write. If not entered, value defaults
2321 * to 100. Can only be updated if packet capture is off.
2322 *
Dave Barach33909772019-09-23 10:27:27 -04002323 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2324 * for each packet. Must be >= 32, <= 9000.
2325 *
Dave Barach586462f2020-08-05 16:12:35 -04002326 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2327 * vector expansion delays during pcap capture
2328 *
2329 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2330 * to retain the data buffer so this option is seldom used.
2331 *
Dave Barach33909772019-09-23 10:27:27 -04002332 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002333 * or use '<em>any</em>' to run packet capture on all interfaces.
2334 * '<em>any</em>' is the default if not provided. Settings from a previous
2335 * packet capture are preserved, so '<em>any</em>' can be used to reset
2336 * the interface setting.
2337 *
Dave Barachf5667c32019-09-25 11:27:46 -04002338 * - <b>filter</b> - Use the pcap rx / tx / drop trace filter, which
2339 * must be configured. Use <b>classify filter pcap...</b> to configure the
2340 * filter. The filter will only be executed if the per-interface or
2341 * any-interface tests fail.
2342 *
Dave Barach5ecd5a52019-02-25 15:27:28 -05002343 * - <b>file <name></b> - Used to specify the output filename. The file will
2344 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2345 * supported. Directory should not be entered. If file already exists, file
Dave Barach33909772019-09-23 10:27:27 -04002346 * will be overwritten. If no filename is provided, the file will be
2347 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2348 * Can only be updated if packet capture is off.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002349 *
2350 * - <b>status</b> - Displays the current status and configured attributes
2351 * associated with a packet capture. If packet capture is in progress,
2352 * '<em>status</em>' also will return the number of packets currently in
2353 * the local buffer. All additional attributes entered on command line
2354 * with '<em>status</em>' will be ignored and not applied.
2355 *
2356 * @cliexpar
2357 * Example of how to display the status of a tx packet capture when off:
Dave Barach33909772019-09-23 10:27:27 -04002358 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002359 * max is 100, for any interface to file /tmp/vpe.pcap
2360 * pcap tx capture is off...
2361 * @cliexend
2362 * Example of how to start a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002363 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file vppTest.pcap}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002364 * @cliexend
2365 * Example of how to display the status of a tx packet capture in progress:
Dave Barach33909772019-09-23 10:27:27 -04002366 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002367 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2368 * pcap tx capture is on: 20 of 35 pkts...
2369 * @cliexend
2370 * Example of how to stop a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002371 * @cliexstart{pcap trace off}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002372 * captured 21 pkts...
2373 * saved to /tmp/vppTest.pcap...
2374 * @cliexend
2375?*/
2376/* *INDENT-OFF* */
2377
2378VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
Dave Barach33909772019-09-23 10:27:27 -04002379 .path = "pcap trace",
Dave Barach5ecd5a52019-02-25 15:27:28 -05002380 .short_help =
Dave Barach586462f2020-08-05 16:12:35 -04002381 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2382 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2383 " [preallocate-data][free-data]",
Dave Barach33909772019-09-23 10:27:27 -04002384 .function = pcap_trace_command_fn,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002385};
2386/* *INDENT-ON* */
2387
Dave Barachba868bb2016-08-08 09:51:21 -04002388/*
2389 * fd.io coding-style-patch-verification: ON
2390 *
2391 * Local Variables:
2392 * eval: (c-set-style "gnu")
2393 * End:
2394 */