blob: 6dadbb8e8d3f9d2ff515740d1654f4077abbadca [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * interface_cli.c: interface CLI
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
Chris Luke16bcf7d2016-09-01 14:31:46 -040039/**
40 * @file
Billy McFalle9bac692017-08-11 14:05:11 -040041 * @brief Interface CLI.
42 *
43 * Source code for several CLI interface commands.
44 *
Chris Luke16bcf7d2016-09-01 14:31:46 -040045 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046#include <vnet/vnet.h>
47#include <vnet/ip/ip.h>
John Lobcebbb92016-04-05 15:47:43 -040048#include <vppinfra/bitmap.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010049#include <vnet/fib/ip4_fib.h>
50#include <vnet/fib/ip6_fib.h>
Eyal Bari942402b2017-07-26 11:57:04 +030051#include <vnet/l2/l2_output.h>
52#include <vnet/l2/l2_input.h>
Neale Rannsba4a5bf2020-01-09 06:43:14 +000053#include <vnet/classify/vnet_classify.h>
Damjan Marion94100532020-11-06 23:25:57 +010054#include <vnet/interface/rx_queue_funcs.h>
Dave Barachba868bb2016-08-08 09:51:21 -040055static int
56compare_interface_names (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barachba868bb2016-08-08 09:51:21 -040058 u32 *hi1 = a1;
59 u32 *hi2 = a2;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Dave Barachba868bb2016-08-08 09:51:21 -040061 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
Ed Warnickecb9cada2015-12-08 15:45:58 -070062}
63
64static clib_error_t *
65show_or_clear_hw_interfaces (vlib_main_t * vm,
66 unformat_input_t * input,
Benoît Ganne17814d72020-07-24 09:57:11 +020067 vlib_cli_command_t * cmd, int is_show)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Dave Barachba868bb2016-08-08 09:51:21 -040069 clib_error_t *error = 0;
70 vnet_main_t *vnm = vnet_get_main ();
71 vnet_interface_main_t *im = &vnm->interface_main;
72 vnet_hw_interface_t *hi;
73 u32 hw_if_index, *hw_if_indices = 0;
Benoît Ganne17814d72020-07-24 09:57:11 +020074 int i, verbose = -1, show_bond = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075
Ed Warnickecb9cada2015-12-08 15:45:58 -070076 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
77 {
78 /* See if user wants to show a specific interface. */
Dave Barachba868bb2016-08-08 09:51:21 -040079 if (unformat
80 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
81 vec_add1 (hw_if_indices, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -040082
Sean Hope679ea792016-02-22 15:12:01 -050083 /* See if user wants to show an interface with a specific hw_if_index. */
84 else if (unformat (input, "%u", &hw_if_index))
Dave Barachba868bb2016-08-08 09:51:21 -040085 vec_add1 (hw_if_indices, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87 else if (unformat (input, "verbose"))
Dave Barachba868bb2016-08-08 09:51:21 -040088 verbose = 1; /* this is also the default */
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
90 else if (unformat (input, "detail"))
91 verbose = 2;
92
93 else if (unformat (input, "brief"))
94 verbose = 0;
95
John Lobcebbb92016-04-05 15:47:43 -040096 else if (unformat (input, "bond"))
Dave Barachba868bb2016-08-08 09:51:21 -040097 {
98 show_bond = 1;
99 if (verbose < 0)
100 verbose = 0; /* default to brief for link bonding */
101 }
John Lobcebbb92016-04-05 15:47:43 -0400102
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 else
104 {
105 error = clib_error_return (0, "unknown input `%U'",
106 format_unformat_error, input);
107 goto done;
108 }
109 }
Dave Barachba868bb2016-08-08 09:51:21 -0400110
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 /* Gather interfaces. */
112 if (vec_len (hw_if_indices) == 0)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100113 pool_foreach (hi, im->hw_interfaces)
114 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
Dave Barachba868bb2016-08-08 09:51:21 -0400116 if (verbose < 0)
117 verbose = 1; /* default to verbose (except bond) */
John Lobcebbb92016-04-05 15:47:43 -0400118
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 if (is_show)
120 {
121 /* Sort by name. */
122 vec_sort_with_function (hw_if_indices, compare_interface_names);
123
124 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
125 for (i = 0; i < vec_len (hw_if_indices); i++)
126 {
127 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
Dave Barachba868bb2016-08-08 09:51:21 -0400128 if (show_bond == 0) /* show all interfaces */
129 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
130 hi, verbose);
131 else if ((hi->bond_info) &&
John Lobcebbb92016-04-05 15:47:43 -0400132 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
Dave Barachba868bb2016-08-08 09:51:21 -0400133 { /* show only bonded interface and all its slave interfaces */
John Lobcebbb92016-04-05 15:47:43 -0400134 int hw_idx;
Dave Barachba868bb2016-08-08 09:51:21 -0400135 vnet_hw_interface_t *shi;
136 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
John Lobcebbb92016-04-05 15:47:43 -0400137 hi, verbose);
Dave Barachba868bb2016-08-08 09:51:21 -0400138
139 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100140 clib_bitmap_foreach (hw_idx, hi->bond_info)
141 {
Dave Barachba868bb2016-08-08 09:51:21 -0400142 shi = vnet_get_hw_interface(vnm, hw_idx);
143 vlib_cli_output (vm, "%U\n",
144 format_vnet_hw_interface, vnm, shi, verbose);
Damjan Marionf0ca1e82020-12-13 23:26:56 +0100145 }
Dave Barachba868bb2016-08-08 09:51:21 -0400146 /* *INDENT-ON* */
John Lobcebbb92016-04-05 15:47:43 -0400147 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 }
149 }
150 else
151 {
152 for (i = 0; i < vec_len (hw_if_indices); i++)
153 {
Dave Barachba868bb2016-08-08 09:51:21 -0400154 vnet_device_class_t *dc;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
156 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
157 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400158
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 if (dc->clear_counters)
160 dc->clear_counters (hi->dev_instance);
161 }
162 }
163
Dave Barachba868bb2016-08-08 09:51:21 -0400164done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 vec_free (hw_if_indices);
166 return error;
167}
168
Benoît Ganne17814d72020-07-24 09:57:11 +0200169static clib_error_t *
170show_hw_interfaces (vlib_main_t * vm,
171 unformat_input_t * input, vlib_cli_command_t * cmd)
172{
173 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
174}
175
176static clib_error_t *
177clear_hw_interfaces (vlib_main_t * vm,
178 unformat_input_t * input, vlib_cli_command_t * cmd)
179{
180 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
181}
182
183
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700184/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400185 * Display more detailed information about all or a list of given interfaces.
186 * The verboseness of the output can be controlled by the following optional
187 * parameters:
188 * - brief: Only show name, index and state (default for bonded interfaces).
189 * - verbose: Also display additional attributes (default for all other interfaces).
190 * - detail: Also display all remaining attributes and extended statistics.
191 *
192 * To limit the output of the command to bonded interfaces and their slave
193 * interfaces, use the '<em>bond</em>' optional parameter.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700194 *
195 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400196 * Example of how to display default data for all interfaces:
197 * @cliexstart{show hardware-interfaces}
198 * Name Idx Link Hardware
199 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
200 * Ethernet address ec:f4:bb:c0:bc:fc
201 * Intel e1000
202 * carrier up full duplex speed 1000 mtu 9216
203 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
204 * cpu socket 0
205 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
206 * Ethernet address ec:f4:bb:c0:bc:fd
207 * Intel e1000
208 * carrier up full duplex speed 1000 mtu 9216
209 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
210 * cpu socket 0
211 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
212 * Ethernet address 02:fe:a5:a9:8b:8e
213 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
214 * Ethernet address 02:fe:c0:4e:3b:b0
215 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
216 * Ethernet address 02:fe:1f:73:92:81
217 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
218 * Ethernet address 02:fe:f2:25:c4:68
219 * local0 0 down local0
220 * local
221 * @cliexend
222 * Example of how to display '<em>verbose</em>' data for an interface by name and
223 * software index (where 2 is the software index):
224 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
225 * Name Idx Link Hardware
226 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
227 * Ethernet address ec:f4:bb:c0:bc:fc
228 * Intel e1000
229 * carrier up full duplex speed 1000 mtu 9216
230 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
231 * cpu socket 0
232 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
233 * Ethernet address ec:f4:bb:c0:bc:fd
234 * Intel e1000
235 * carrier up full duplex speed 1000 mtu 9216
236 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
237 * cpu socket 0
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700238 * @cliexend
239 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400240/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
242 .path = "show hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400243 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
244 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200245 .function = show_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246};
Dave Barachba868bb2016-08-08 09:51:21 -0400247/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Billy McFalle9bac692017-08-11 14:05:11 -0400249
250/*?
251 * Clear the extended statistics for all or a list of given interfaces
252 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
253 *
254 * @cliexpar
255 * Example of how to clear the extended statistics for all interfaces:
256 * @cliexcmd{clear hardware-interfaces}
257 * Example of how to clear the extended statistics for an interface by
258 * name and software index (where 2 is the software index):
259 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
260 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400261/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
263 .path = "clear hardware-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400264 .short_help = "clear hardware-interfaces "
265 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
Benoît Ganne17814d72020-07-24 09:57:11 +0200266 .function = clear_hw_interfaces,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267};
Dave Barachba868bb2016-08-08 09:51:21 -0400268/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
Dave Barachba868bb2016-08-08 09:51:21 -0400270static int
271sw_interface_name_compare (void *a1, void *a2)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272{
273 vnet_sw_interface_t *si1 = a1;
274 vnet_sw_interface_t *si2 = a2;
275
Dave Barachba868bb2016-08-08 09:51:21 -0400276 return vnet_sw_interface_compare (vnet_get_main (),
277 si1->sw_if_index, si2->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278}
279
280static clib_error_t *
281show_sw_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400282 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283{
Dave Barachba868bb2016-08-08 09:51:21 -0400284 clib_error_t *error = 0;
285 vnet_main_t *vnm = vnet_get_main ();
Dave Barach525c9d02018-05-26 10:48:55 -0400286 unformat_input_t _linput, *linput = &_linput;
Dave Barachba868bb2016-08-08 09:51:21 -0400287 vnet_interface_main_t *im = &vnm->interface_main;
288 vnet_sw_interface_t *si, *sorted_sis = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200289 u32 sw_if_index = ~(u32) 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 u8 show_addresses = 0;
Damjan Marion22311502016-10-28 20:30:15 +0200291 u8 show_features = 0;
Dave Barach7be864a2016-11-28 11:41:35 -0500292 u8 show_tag = 0;
Jon Loeliger9485d992019-11-08 15:05:23 -0600293 u8 show_vtr = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400294 int verbose = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barach525c9d02018-05-26 10:48:55 -0400296 /*
297 * Get a line of input. Won't work if the user typed
298 * "show interface" and nothing more.
299 */
300 if (unformat_user (input, unformat_line_input, linput))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 {
Dave Barach525c9d02018-05-26 10:48:55 -0400302 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 {
Dave Barach525c9d02018-05-26 10:48:55 -0400304 /* See if user wants to show specific interface */
305 if (unformat
306 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
307 {
308 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
309 vec_add1 (sorted_sis, si[0]);
310 }
311 else if (unformat (linput, "address") || unformat (linput, "addr"))
312 show_addresses = 1;
313 else if (unformat (linput, "features") || unformat (linput, "feat"))
314 show_features = 1;
315 else if (unformat (linput, "tag"))
316 show_tag = 1;
Jon Loeliger9485d992019-11-08 15:05:23 -0600317 else if (unformat (linput, "vtr"))
318 show_vtr = 1;
Dave Barach525c9d02018-05-26 10:48:55 -0400319 else if (unformat (linput, "verbose"))
320 verbose = 1;
321 else
322 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400323 vec_free (sorted_sis);
Dave Barach525c9d02018-05-26 10:48:55 -0400324 error = clib_error_return (0, "unknown input `%U'",
325 format_unformat_error, linput);
326 goto done;
327 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 }
Dave Barach525c9d02018-05-26 10:48:55 -0400329 unformat_free (linput);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 }
Jon Loeliger9485d992019-11-08 15:05:23 -0600331 if (show_features || show_tag || show_vtr)
Damjan Marion22311502016-10-28 20:30:15 +0200332 {
333 if (sw_if_index == ~(u32) 0)
Dave Barach8fdde3c2019-05-17 10:46:40 -0400334 {
335 vec_free (sorted_sis);
336 return clib_error_return (0, "Interface not specified...");
337 }
Dave Barach7be864a2016-11-28 11:41:35 -0500338 }
Damjan Marion22311502016-10-28 20:30:15 +0200339
Dave Barach7be864a2016-11-28 11:41:35 -0500340 if (show_features)
341 {
Dave Barach525c9d02018-05-26 10:48:55 -0400342 vnet_interface_features_show (vm, sw_if_index, verbose);
Neale Ranns47a3d992020-09-29 15:38:51 +0000343 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
Eyal Bari942402b2017-07-26 11:57:04 +0300344
345 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
346 vlib_cli_output (vm, "\nl2-output:");
347 if (l2_output->out_vtr_flag)
348 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
349 vlib_cli_output (vm, "%U", format_l2_output_features,
Neale Ranns5d9df1d2018-11-09 08:19:27 -0800350 l2_output->feature_bitmap, 1);
Dave Barach8fdde3c2019-05-17 10:46:40 -0400351 vec_free (sorted_sis);
Damjan Marion22311502016-10-28 20:30:15 +0200352 return 0;
353 }
Dave Barach7be864a2016-11-28 11:41:35 -0500354 if (show_tag)
355 {
356 u8 *tag;
357 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
358 vlib_cli_output (vm, "%U: %s",
359 format_vnet_sw_if_index_name, vnm, sw_if_index,
360 tag ? (char *) tag : "(none)");
Dave Barach8fdde3c2019-05-17 10:46:40 -0400361 vec_free (sorted_sis);
Dave Barach7be864a2016-11-28 11:41:35 -0500362 return 0;
363 }
Damjan Marion22311502016-10-28 20:30:15 +0200364
Jon Loeliger9485d992019-11-08 15:05:23 -0600365 /*
366 * Show vlan tag rewrite data for one interface.
367 */
368 if (show_vtr)
369 {
370 u32 vtr_op = L2_VTR_DISABLED;
371 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
372
373 if (l2vtr_get (vm, vnm, sw_if_index,
374 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
375 {
376 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
377 format_vnet_sw_if_index_name, vnm, sw_if_index);
378 return 0;
379 }
380 vlib_cli_output (vm, "%U: VTR %0U",
381 format_vnet_sw_if_index_name, vnm, sw_if_index,
382 format_vtr, vtr_op, push_dot1q, tag1, tag2);
383 return 0;
384 }
385
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 if (!show_addresses)
Dave Barachba868bb2016-08-08 09:51:21 -0400387 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barachba868bb2016-08-08 09:51:21 -0400389 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390 {
391 /* Gather interfaces. */
Dave Barachba868bb2016-08-08 09:51:21 -0400392 sorted_sis =
393 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 _vec_len (sorted_sis) = 0;
Dave Barach525c9d02018-05-26 10:48:55 -0400395 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +0100396 pool_foreach (si, im->sw_interfaces)
397 {
Dave Barach525c9d02018-05-26 10:48:55 -0400398 int visible = vnet_swif_is_api_visible (si);
399 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +0100400 vec_add1 (sorted_sis, si[0]);
401 }
Ole Troand7231612018-06-07 10:17:57 +0200402 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 /* Sort by name. */
404 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
405 }
406
407 if (show_addresses)
408 {
409 vec_foreach (si, sorted_sis)
Dave Barachba868bb2016-08-08 09:51:21 -0400410 {
Dave Barachba868bb2016-08-08 09:51:21 -0400411 ip4_main_t *im4 = &ip4_main;
412 ip6_main_t *im6 = &ip6_main;
413 ip_lookup_main_t *lm4 = &im4->lookup_main;
414 ip_lookup_main_t *lm6 = &im6->lookup_main;
415 ip_interface_address_t *ia = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400416 u32 fib_index4 = 0, fib_index6 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
Dave Barachba868bb2016-08-08 09:51:21 -0400418 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
419 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
420 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Dave Barachba868bb2016-08-08 09:51:21 -0400422 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
423 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
424 si->sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
John Lo4478d8e2018-01-12 17:15:25 -0500426 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
427 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Dave Barachba868bb2016-08-08 09:51:21 -0400429 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
430 vlib_cli_output
431 (vm, "%U (%s): \n unnumbered, use %U",
John Lo4478d8e2018-01-12 17:15:25 -0500432 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
Dave Barachba868bb2016-08-08 09:51:21 -0400433 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
434 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400435 else
John Lo4478d8e2018-01-12 17:15:25 -0500436 vlib_cli_output
437 (vm, "%U (%s):",
438 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
439 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440
Eyal Bari942402b2017-07-26 11:57:04 +0300441 /* Display any L2 info */
Neale Ranns47a3d992020-09-29 15:38:51 +0000442 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400443
John Lo4478d8e2018-01-12 17:15:25 -0500444 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400445 /* Display any IP4 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500446 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
447 1 /* honor unnumbered */,
448 ({
449 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
450 if (fib4->table_id)
451 vlib_cli_output (vm, " L3 %U/%d ip4 table-id %d fib-idx %d",
452 format_ip4_address, r4, ia->address_length,
453 fib4->table_id,
454 ip4_fib_index_from_table_id (fib4->table_id));
455 else
456 vlib_cli_output (vm, " L3 %U/%d",
457 format_ip4_address, r4, ia->address_length);
458 }));
459 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
John Lo4478d8e2018-01-12 17:15:25 -0500461 /* *INDENT-OFF* */
Dave Barachba868bb2016-08-08 09:51:21 -0400462 /* Display any IP6 addressing info */
John Lo4478d8e2018-01-12 17:15:25 -0500463 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
464 1 /* honor unnumbered */,
465 ({
466 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
467 if (fib6->table_id)
468 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
469 format_ip6_address, r6, ia->address_length,
470 fib6->table_id,
471 ip6_fib_index_from_table_id (fib6->table_id));
472 else
473 vlib_cli_output (vm, " L3 %U/%d",
474 format_ip6_address, r6, ia->address_length);
475 }));
476 /* *INDENT-ON* */
Ole Troand7231612018-06-07 10:17:57 +0200477 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 }
Ole Troand7231612018-06-07 10:17:57 +0200479 else
480 {
481 vec_foreach (si, sorted_sis)
482 {
483 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
484 }
485 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
Dave Barachba868bb2016-08-08 09:51:21 -0400487done:
Ole Troand7231612018-06-07 10:17:57 +0200488 vec_free (sorted_sis);
489 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490}
491
Dave Barachba868bb2016-08-08 09:51:21 -0400492/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
Dave Barach13ad1f02017-03-26 19:36:18 -0400494 .path = "show interface",
Jon Loeliger9485d992019-11-08 15:05:23 -0600495 .short_help = "show interface [address|addr|features|feat|vtr] [<interface> [<interface> [..]]] [verbose]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 .function = show_sw_interfaces,
Steven Luongc5fa2b82019-04-25 11:19:49 -0700497 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498};
Dave Barachba868bb2016-08-08 09:51:21 -0400499/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
501/* Root of all interface commands. */
Dave Barachba868bb2016-08-08 09:51:21 -0400502/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
504 .path = "interface",
505 .short_help = "Interface commands",
506};
Dave Barachba868bb2016-08-08 09:51:21 -0400507/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Dave Barachba868bb2016-08-08 09:51:21 -0400509/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
511 .path = "set interface",
512 .short_help = "Interface commands",
513};
Dave Barachba868bb2016-08-08 09:51:21 -0400514/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
516static clib_error_t *
517clear_interface_counters (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400518 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519{
Dave Barachba868bb2016-08-08 09:51:21 -0400520 vnet_main_t *vnm = vnet_get_main ();
521 vnet_interface_main_t *im = &vnm->interface_main;
522 vlib_simple_counter_main_t *sm;
523 vlib_combined_counter_main_t *cm;
Dave Barach8fdde3c2019-05-17 10:46:40 -0400524 int j, n_counters;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525
526 n_counters = vec_len (im->combined_sw_if_counters);
527
528 for (j = 0; j < n_counters; j++)
529 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400530 im = &vnm->interface_main;
531 cm = im->combined_sw_if_counters + j;
532 vlib_clear_combined_counters (cm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 }
534
535 n_counters = vec_len (im->sw_if_counters);
536
537 for (j = 0; j < n_counters; j++)
538 {
Dave Barach8fdde3c2019-05-17 10:46:40 -0400539 im = &vnm->interface_main;
540 sm = im->sw_if_counters + j;
541 vlib_clear_simple_counters (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 }
543
544 return 0;
545}
546
Billy McFalle9bac692017-08-11 14:05:11 -0400547/*?
548 * Clear the statistics for all interfaces (statistics associated with the
549 * '<em>show interface</em>' command).
550 *
551 * @cliexpar
552 * Example of how to clear the statistics for all interfaces:
553 * @cliexcmd{clear interfaces}
554 ?*/
Dave Barachba868bb2016-08-08 09:51:21 -0400555/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
557 .path = "clear interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400558 .short_help = "clear interfaces",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 .function = clear_interface_counters,
560};
Dave Barachba868bb2016-08-08 09:51:21 -0400561/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Chris Luke16bcf7d2016-09-01 14:31:46 -0400563/**
564 * Parse subinterface names.
565 *
Dave Barachba868bb2016-08-08 09:51:21 -0400566 * The following subinterface syntax is supported. The first two are for
567 * backwards compatability:
568 *
569 * <intf-name> <id>
570 * - a subinterface with the name <intf-name>.<id>. The subinterface
571 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
572 *
573 * <intf-name> <min_id>-<max_id>
574 * - a set of the above subinterfaces, repeating for each id
575 * in the range <min_id> to <max_id>
576 *
577 * In the following, exact-match semantics (i.e. the number of vlan tags on the
578 * packet must match the number of tags in the configuration) are used only if
579 * the keyword exact-match is present. Non-exact match is the default.
580 *
581 * <intf-name> <id> dot1q <outer_id> [exact-match]
582 * - a subinterface with the name <intf-name>.<id>. The subinterface
583 * is a single dot1q vlan with vlan id <outer_id>.
584 *
585 * <intf-name> <id> dot1q any [exact-match]
586 * - a subinterface with the name <intf-name>.<id>. The subinterface
587 * is a single dot1q vlan with any vlan id.
588 *
589 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
590 * - a subinterface with the name <intf-name>.<id>. The subinterface
591 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
592 * <inner_id>.
593 *
594 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
595 * - a subinterface with the name <intf-name>.<id>. The subinterface
596 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
597 *
598 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
599 *
600 * - a subinterface with the name <intf-name>.<id>. The subinterface
601 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
602 *
603 * For each of the above CLI, there is a duplicate that uses the keyword
604 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
605 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
606 * tagged packets the inner ethertype is always 0x8100. Also note that
607 * the dot1q and dot1ad naming spaces are independent, so it is legal to
608 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
609 *
610 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
611 * - a subinterface with the name <intf-name>.<id>. The subinterface
612 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
613 * id <inner_id>.
614 *
615 * <intf-name> <id> untagged
616 * - a subinterface with the name <intf-name>.<id>. The subinterface
617 * has no vlan tags. Only one can be specified per interface.
618 *
619 * <intf-name> <id> default
620 * - a subinterface with the name <intf-name>.<id>. This is associated
621 * with a packet that did not match any other configured subinterface
622 * on this interface. Only one can be specified per interface.
623 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700624
625static clib_error_t *
Dave Barachba868bb2016-08-08 09:51:21 -0400626parse_vlan_sub_interfaces (unformat_input_t * input,
627 vnet_sw_interface_t * template)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700628{
Dave Barachba868bb2016-08-08 09:51:21 -0400629 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 u32 inner_vlan, outer_vlan;
631
Dave Barachba868bb2016-08-08 09:51:21 -0400632 if (unformat (input, "any inner-dot1q any"))
633 {
634 template->sub.eth.flags.two_tags = 1;
635 template->sub.eth.flags.outer_vlan_id_any = 1;
636 template->sub.eth.flags.inner_vlan_id_any = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637 }
Dave Barachba868bb2016-08-08 09:51:21 -0400638 else if (unformat (input, "any"))
639 {
640 template->sub.eth.flags.one_tag = 1;
641 template->sub.eth.flags.outer_vlan_id_any = 1;
642 }
643 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
644 {
645 template->sub.eth.flags.two_tags = 1;
646 template->sub.eth.flags.inner_vlan_id_any = 1;
647 template->sub.eth.outer_vlan_id = outer_vlan;
648 }
649 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
650 {
651 template->sub.eth.flags.two_tags = 1;
652 template->sub.eth.outer_vlan_id = outer_vlan;
653 template->sub.eth.inner_vlan_id = inner_vlan;
654 }
655 else if (unformat (input, "%d", &outer_vlan))
656 {
657 template->sub.eth.flags.one_tag = 1;
658 template->sub.eth.outer_vlan_id = outer_vlan;
659 }
660 else
661 {
662 error = clib_error_return (0, "expected dot1q config, got `%U'",
663 format_unformat_error, input);
664 goto done;
665 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666
Dave Barachba868bb2016-08-08 09:51:21 -0400667 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
668 {
669 if (unformat (input, "exact-match"))
670 {
671 template->sub.eth.flags.exact_match = 1;
672 }
673 }
674
675done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676 return error;
677}
678
679static clib_error_t *
680create_sub_interfaces (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400681 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682{
Dave Barachba868bb2016-08-08 09:51:21 -0400683 vnet_main_t *vnm = vnet_get_main ();
684 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 u32 hw_if_index, sw_if_index;
Dave Barachba868bb2016-08-08 09:51:21 -0400686 vnet_hw_interface_t *hi;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 u32 id, id_min, id_max;
688 vnet_sw_interface_t template;
689
690 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400691 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 {
693 error = clib_error_return (0, "unknown interface `%U'",
694 format_unformat_error, input);
695 goto done;
696 }
697
Dave Barachb7b92992018-10-17 10:38:51 -0400698 clib_memset (&template, 0, sizeof (template));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 template.sub.eth.raw_flags = 0;
700
Dave Barachba868bb2016-08-08 09:51:21 -0400701 if (unformat (input, "%d default", &id_min))
702 {
703 id_max = id_min;
704 template.sub.eth.flags.default_sub = 1;
705 }
706 else if (unformat (input, "%d untagged", &id_min))
707 {
708 id_max = id_min;
709 template.sub.eth.flags.no_tags = 1;
710 template.sub.eth.flags.exact_match = 1;
711 }
712 else if (unformat (input, "%d dot1q", &id_min))
713 {
714 /* parse dot1q config */
715 id_max = id_min;
716 error = parse_vlan_sub_interfaces (input, &template);
717 if (error)
718 goto done;
719 }
720 else if (unformat (input, "%d dot1ad", &id_min))
721 {
722 /* parse dot1ad config */
723 id_max = id_min;
724 template.sub.eth.flags.dot1ad = 1;
725 error = parse_vlan_sub_interfaces (input, &template);
726 if (error)
727 goto done;
728 }
729 else if (unformat (input, "%d-%d", &id_min, &id_max))
730 {
731 template.sub.eth.flags.one_tag = 1;
Dave Barachba868bb2016-08-08 09:51:21 -0400732 template.sub.eth.flags.exact_match = 1;
733 if (id_min > id_max)
734 goto id_error;
735 }
736 else if (unformat (input, "%d", &id_min))
737 {
738 id_max = id_min;
739 template.sub.eth.flags.one_tag = 1;
740 template.sub.eth.outer_vlan_id = id_min;
741 template.sub.eth.flags.exact_match = 1;
742 }
743 else
744 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 id_error:
746 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
747 format_unformat_error, input);
748 goto done;
Dave Barachba868bb2016-08-08 09:51:21 -0400749 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
751 hi = vnet_get_hw_interface (vnm, hw_if_index);
John Lobcebbb92016-04-05 15:47:43 -0400752
Dave Barachba868bb2016-08-08 09:51:21 -0400753 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
754 {
755 error =
756 clib_error_return (0,
757 "not allowed as %v belong to a BondEthernet interface",
758 hi->name);
759 goto done;
760 }
John Lobcebbb92016-04-05 15:47:43 -0400761
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 for (id = id_min; id <= id_max; id++)
763 {
Dave Barachba868bb2016-08-08 09:51:21 -0400764 uword *p;
765 vnet_interface_main_t *im = &vnm->interface_main;
766 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
767 u64 *kp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700768
769 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
770 if (p)
Dave Barachba868bb2016-08-08 09:51:21 -0400771 {
772 if (CLIB_DEBUG > 0)
773 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
774 hi->sw_if_index, id);
775 continue;
776 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777
Ed Warnickecb9cada2015-12-08 15:45:58 -0700778 template.type = VNET_SW_INTERFACE_TYPE_SUB;
Eyal Baric5b13602016-11-24 19:42:43 +0200779 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 template.sup_sw_if_index = hi->sw_if_index;
781 template.sub.id = id;
Eyal Baria4509cf2016-09-26 09:24:09 +0300782 if (id_min < id_max)
783 template.sub.eth.outer_vlan_id = id;
784
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400786 if (error)
787 goto done;
Dave Barach16ad6ae2016-07-28 17:55:30 -0400788
Jon Loeligerb22e1f02019-12-19 09:03:52 -0600789 kp = clib_mem_alloc (sizeof (*kp));
790 *kp = sup_and_sub_key;
791
Ed Warnickecb9cada2015-12-08 15:45:58 -0700792 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
793 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
Dave Barachba868bb2016-08-08 09:51:21 -0400794 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
795 vnet_get_main (), sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 }
797
Dave Barachba868bb2016-08-08 09:51:21 -0400798done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 return error;
800}
801
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700802/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400803 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
804 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
805 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
806 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
807 * but this is not recommended.
808 *
809 * This command has several variations:
810 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
811 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
812 *
813 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
814 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
815 * match any other subinterfaces should be sent to this subinterface.
816 *
817 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
818 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
819 * to this subinterface.
820 *
821 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
822 * subinterfaces to handle a range of VLAN IDs.
823 *
824 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700825 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
Billy McFalle9bac692017-08-11 14:05:11 -0400826 * VLAN ID different from the '<em>subId</em>'.
827 *
828 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
829 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700830 * the inner VLAN ID.
Billy McFalle9bac692017-08-11 14:05:11 -0400831 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700832 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
Billy McFalle9bac692017-08-11 14:05:11 -0400833 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
834 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
835 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
836 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
837 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
838 * entered, then the default behavior is exact-match.
839 *
840 * Use the '<em>show interface</em>' command to display all subinterfaces.
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700841 *
842 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400843 * @parblock
844 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
845 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700846 *
Billy McFalle9bac692017-08-11 14:05:11 -0400847 * The previous example is shorthand and is equivalent to:
848 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700849 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700850 *
Billy McFalle9bac692017-08-11 14:05:11 -0400851 * Example of how to create a subinterface number that is different from the VLAN ID:
852 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700853 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700854 *
Billy McFalle9bac692017-08-11 14:05:11 -0400855 * Examples of how to create q-in-q and q-in-any subinterfaces:
856 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
857 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700858 *
Billy McFalle9bac692017-08-11 14:05:11 -0400859 * Examples of how to create dot1ad interfaces:
860 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
861 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700862 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700863 *
Billy McFalle9bac692017-08-11 14:05:11 -0400864 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
865 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
866 * is non-exact match:
867 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700868 *
Billy McFalle9bac692017-08-11 14:05:11 -0400869 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
870 * is specified and only one VLAN is configured, but packet contains two VLANs:
871 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700872 *
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700873 *
Billy McFalle9bac692017-08-11 14:05:11 -0400874 * Example of how to created a subinterface to process untagged packets:
875 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
876 *
877 * Example of how to created a subinterface to process any packet with a VLAN ID that
878 * does not match any other subinterface:
879 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
880 *
881 * When subinterfaces are created, they are in the down state. Example of how to
882 * enable a newly created subinterface:
883 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
884 * @endparblock
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700885 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400886/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700888 .path = "create sub-interfaces",
Billy McFalle9bac692017-08-11 14:05:11 -0400889 .short_help = "create sub-interfaces <interface> "
890 "{<subId> [default|untagged]} | "
891 "{<subId>-<subId>} | "
892 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 .function = create_sub_interfaces,
894};
Dave Barachba868bb2016-08-08 09:51:21 -0400895/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896
897static clib_error_t *
898set_state (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400899 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900{
Dave Barachba868bb2016-08-08 09:51:21 -0400901 vnet_main_t *vnm = vnet_get_main ();
902 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 u32 sw_if_index, flags;
904
905 sw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -0400906 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 {
908 error = clib_error_return (0, "unknown interface `%U'",
909 format_unformat_error, input);
910 goto done;
911 }
912
Dave Barachba868bb2016-08-08 09:51:21 -0400913 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 {
915 error = clib_error_return (0, "unknown flags `%U'",
916 format_unformat_error, input);
917 goto done;
918 }
919
920 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
921 if (error)
922 goto done;
923
Dave Barachba868bb2016-08-08 09:51:21 -0400924done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 return error;
926}
927
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700928
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700929/*?
Billy McFalle9bac692017-08-11 14:05:11 -0400930 * This command is used to change the admin state (up/down) of an interface.
931 *
932 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
933 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
934 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
935 * '<em>punt</em>' flag (interface is still down).
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700936 *
937 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -0400938 * Example of how to configure the admin state of an interface to '<em>up</em?':
939 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
940 * Example of how to configure the admin state of an interface to '<em>down</em?':
941 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
Keith Burns (alagalah)6ef7bb92016-09-10 14:55:04 -0700942 ?*/
Billy McFalle9bac692017-08-11 14:05:11 -0400943/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944VLIB_CLI_COMMAND (set_state_command, static) = {
945 .path = "set interface state",
Billy McFalle9bac692017-08-11 14:05:11 -0400946 .short_help = "set interface state <interface> [up|down|punt|enable]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 .function = set_state,
948};
Dave Barachba868bb2016-08-08 09:51:21 -0400949/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
951static clib_error_t *
952set_unnumbered (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400953 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954{
Dave Barachba868bb2016-08-08 09:51:21 -0400955 vnet_main_t *vnm = vnet_get_main ();
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700956 u32 unnumbered_sw_if_index = ~0;
957 u32 inherit_from_sw_if_index = ~0;
958 int enable = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700960 if (unformat (input, "%U use %U",
961 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
962 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700963 enable = 1;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700964 else if (unformat (input, "del %U",
965 unformat_vnet_sw_interface, vnm,
966 &unnumbered_sw_if_index))
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700967 enable = 0;
Igor Mikhailov (imichail)ab3e42b2016-09-25 15:11:53 -0700968 else
969 return clib_error_return (0, "parse error '%U'",
970 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700972 if (~0 == unnumbered_sw_if_index)
973 return clib_error_return (0, "Specify the unnumbered interface");
974 if (enable && ~0 == inherit_from_sw_if_index)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700975 return clib_error_return (0, "When enabling unnumbered specify the"
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700976 " IP enabled interface that it uses");
Neale Ranns898273f2017-03-18 02:57:38 -0700977
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700978 vnet_sw_interface_update_unnumbered (unnumbered_sw_if_index,
979 inherit_from_sw_if_index, enable);
Neale Ranns898273f2017-03-18 02:57:38 -0700980
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700981 return (NULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982}
983
Dave Barachba868bb2016-08-08 09:51:21 -0400984/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700985VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
986 .path = "set interface unnumbered",
Billy McFalle9bac692017-08-11 14:05:11 -0400987 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988 .function = set_unnumbered,
989};
Dave Barachba868bb2016-08-08 09:51:21 -0400990/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991
992
993
994static clib_error_t *
995set_hw_class (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -0400996 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997{
Dave Barachba868bb2016-08-08 09:51:21 -0400998 vnet_main_t *vnm = vnet_get_main ();
999 vnet_interface_main_t *im = &vnm->interface_main;
1000 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001 u32 hw_if_index, hw_class_index;
1002
1003 hw_if_index = ~0;
Dave Barachba868bb2016-08-08 09:51:21 -04001004 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005 {
1006 error = clib_error_return (0, "unknown hardware interface `%U'",
1007 format_unformat_error, input);
1008 goto done;
1009 }
1010
Dave Barachba868bb2016-08-08 09:51:21 -04001011 if (!unformat_user (input, unformat_hash_string,
1012 im->hw_interface_class_by_name, &hw_class_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013 {
1014 error = clib_error_return (0, "unknown hardware class `%U'",
1015 format_unformat_error, input);
1016 goto done;
1017 }
1018
1019 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1020 if (error)
1021 goto done;
1022
Dave Barachba868bb2016-08-08 09:51:21 -04001023done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 return error;
1025}
1026
Dave Barachba868bb2016-08-08 09:51:21 -04001027/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1029 .path = "set interface hw-class",
1030 .short_help = "Set interface hardware class",
1031 .function = set_hw_class,
1032};
Dave Barachba868bb2016-08-08 09:51:21 -04001033/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034
Dave Barachba868bb2016-08-08 09:51:21 -04001035static clib_error_t *
1036vnet_interface_cli_init (vlib_main_t * vm)
1037{
1038 return 0;
1039}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
1041VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1042
Dave Barachba868bb2016-08-08 09:51:21 -04001043static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044renumber_interface_command_fn (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001045 unformat_input_t * input,
1046 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001047{
1048 u32 hw_if_index;
1049 u32 new_dev_instance;
Dave Barachba868bb2016-08-08 09:51:21 -04001050 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051 int rv;
1052
Dave Barachba868bb2016-08-08 09:51:21 -04001053 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054 return clib_error_return (0, "unknown hardware interface `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001055 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056
Dave Barachba868bb2016-08-08 09:51:21 -04001057 if (!unformat (input, "%d", &new_dev_instance))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058 return clib_error_return (0, "new dev instance missing");
1059
1060 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1061
1062 switch (rv)
1063 {
1064 case 0:
1065 break;
1066
1067 default:
1068 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
Dave Barachba868bb2016-08-08 09:51:21 -04001069 rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070
1071 }
1072
1073 return 0;
1074}
1075
1076
Dave Barachba868bb2016-08-08 09:51:21 -04001077/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001078VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1079 .path = "renumber interface",
Billy McFalle9bac692017-08-11 14:05:11 -04001080 .short_help = "renumber interface <interface> <new-dev-instance>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081 .function = renumber_interface_command_fn,
1082};
Dave Barachba868bb2016-08-08 09:51:21 -04001083/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084
Damjan Marion8358ff92016-04-15 14:26:00 +02001085static clib_error_t *
1086promiscuous_cmd (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -04001087 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion8358ff92016-04-15 14:26:00 +02001088{
Dave Barachba868bb2016-08-08 09:51:21 -04001089 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion8358ff92016-04-15 14:26:00 +02001090 u32 hw_if_index;
1091 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
Dave Barachba868bb2016-08-08 09:51:21 -04001092 ethernet_main_t *em = &ethernet_main;
1093 ethernet_interface_t *eif;
Damjan Marion8358ff92016-04-15 14:26:00 +02001094
1095 if (unformat (input, "on %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001096 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001097 ;
1098 else if (unformat (input, "off %U",
Dave Barachba868bb2016-08-08 09:51:21 -04001099 unformat_ethernet_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001100 flags = 0;
1101 else
1102 return clib_error_return (0, "unknown input `%U'",
Dave Barachba868bb2016-08-08 09:51:21 -04001103 format_unformat_error, input);
Damjan Marion8358ff92016-04-15 14:26:00 +02001104
1105 eif = ethernet_get_interface (em, hw_if_index);
1106 if (!eif)
1107 return clib_error_return (0, "not supported");
1108
1109 ethernet_set_flags (vnm, hw_if_index, flags);
1110 return 0;
1111}
1112
Dave Barachba868bb2016-08-08 09:51:21 -04001113/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001114VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1115 .path = "set interface promiscuous",
Billy McFalle9bac692017-08-11 14:05:11 -04001116 .short_help = "set interface promiscuous [on|off] <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001117 .function = promiscuous_cmd,
1118};
Dave Barachba868bb2016-08-08 09:51:21 -04001119/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001120
1121static clib_error_t *
1122mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1123{
Dave Barachba868bb2016-08-08 09:51:21 -04001124 vnet_main_t *vnm = vnet_get_main ();
Ole Troand7231612018-06-07 10:17:57 +02001125 u32 hw_if_index, sw_if_index, mtu;
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001126 ethernet_main_t *em = &ethernet_main;
Ole Troand7231612018-06-07 10:17:57 +02001127 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
Damjan Marion8358ff92016-04-15 14:26:00 +02001128
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001129 if (unformat (input, "%d %U", &mtu,
1130 unformat_vnet_hw_interface, vnm, &hw_if_index))
Damjan Marion8358ff92016-04-15 14:26:00 +02001131 {
Ole Troand7231612018-06-07 10:17:57 +02001132 /*
1133 * Change physical MTU on interface. Only supported for Ethernet
1134 * interfaces
1135 */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001136 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1137 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1138
1139 if (!eif)
1140 return clib_error_return (0, "not supported");
1141
1142 if (mtu < hi->min_supported_packet_bytes)
1143 return clib_error_return (0, "Invalid mtu (%d): "
1144 "must be >= min pkt bytes (%d)", mtu,
1145 hi->min_supported_packet_bytes);
1146
1147 if (mtu > hi->max_supported_packet_bytes)
1148 return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1149 hi->max_supported_packet_bytes);
1150
1151 vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
Ole Troand7231612018-06-07 10:17:57 +02001152 goto done;
Damjan Marion8358ff92016-04-15 14:26:00 +02001153 }
Ole Troand7231612018-06-07 10:17:57 +02001154 else if (unformat (input, "packet %d %U", &mtu,
1155 unformat_vnet_sw_interface, vnm, &sw_if_index))
1156 /* Set default packet MTU (including L3 header */
1157 mtus[VNET_MTU_L3] = mtu;
1158 else if (unformat (input, "ip4 %d %U", &mtu,
1159 unformat_vnet_sw_interface, vnm, &sw_if_index))
1160 mtus[VNET_MTU_IP4] = mtu;
1161 else if (unformat (input, "ip6 %d %U", &mtu,
1162 unformat_vnet_sw_interface, vnm, &sw_if_index))
1163 mtus[VNET_MTU_IP6] = mtu;
1164 else if (unformat (input, "mpls %d %U", &mtu,
1165 unformat_vnet_sw_interface, vnm, &sw_if_index))
1166 mtus[VNET_MTU_MPLS] = mtu;
Damjan Marion8358ff92016-04-15 14:26:00 +02001167 else
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001168 return clib_error_return (0, "unknown input `%U'",
1169 format_unformat_error, input);
Ole Troand7231612018-06-07 10:17:57 +02001170
1171 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1172
1173done:
Damjan Marion8358ff92016-04-15 14:26:00 +02001174 return 0;
1175}
1176
Dave Barachba868bb2016-08-08 09:51:21 -04001177/* *INDENT-OFF* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001178VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1179 .path = "set interface mtu",
Ole Troand7231612018-06-07 10:17:57 +02001180 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
Damjan Marion8358ff92016-04-15 14:26:00 +02001181 .function = mtu_cmd,
1182};
Dave Barachba868bb2016-08-08 09:51:21 -04001183/* *INDENT-ON* */
Damjan Marion8358ff92016-04-15 14:26:00 +02001184
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001185static clib_error_t *
Matthew Smithe0792fd2019-07-12 11:48:24 -05001186show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1187 vlib_cli_command_t * cmd)
1188{
1189 vnet_main_t *vnm = vnet_get_main ();
1190 vnet_interface_main_t *im = &vnm->interface_main;
1191 ethernet_main_t *em = &ethernet_main;
1192 u32 sw_if_index = ~0;
1193 vnet_sw_interface_t *si, *sorted_sis = 0;
1194
1195 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1196 {
1197 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1198 vec_add1 (sorted_sis, si[0]);
1199 }
1200
1201 /* if an interface name was not passed, get all interfaces */
1202 if (vec_len (sorted_sis) == 0)
1203 {
1204 sorted_sis =
1205 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1206 _vec_len (sorted_sis) = 0;
1207 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001208 pool_foreach (si, im->sw_interfaces)
1209 {
Matthew Smithe0792fd2019-07-12 11:48:24 -05001210 int visible = vnet_swif_is_api_visible (si);
1211 if (visible)
Damjan Marionb2c31b62020-12-13 21:47:40 +01001212 vec_add1 (sorted_sis, si[0]);
1213 }
Matthew Smithe0792fd2019-07-12 11:48:24 -05001214 /* *INDENT-ON* */
1215 /* Sort by name. */
1216 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1217 }
1218
1219 vec_foreach (si, sorted_sis)
1220 {
1221 vnet_sw_interface_t *sup_si;
1222 ethernet_interface_t *ei;
1223
1224 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1225 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1226
1227 vlib_cli_output (vm, "%U (%s):",
1228 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1229 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1230 "up" : "dn");
1231
1232 if (ei && ei->secondary_addrs)
1233 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001234 ethernet_interface_address_t *sec_addr;
Matthew Smithe0792fd2019-07-12 11:48:24 -05001235
1236 vec_foreach (sec_addr, ei->secondary_addrs)
1237 {
Benoît Ganneb44c77d2020-10-20 16:24:17 +02001238 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
Matthew Smithe0792fd2019-07-12 11:48:24 -05001239 }
1240 }
1241 }
1242
1243 vec_free (sorted_sis);
1244 return 0;
1245}
1246
1247/*?
1248 * This command is used to display interface secondary mac addresses.
1249 *
1250 * @cliexpar
1251 * Example of how to display interface secondary mac addresses:
1252 * @cliexstart{show interface secondary-mac-address}
1253 * @cliexend
1254?*/
1255/* *INDENT-OFF* */
1256VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1257 .path = "show interface secondary-mac-address",
1258 .short_help = "show interface secondary-mac-address [<interface>]",
1259 .function = show_interface_sec_mac_addr_fn,
1260};
1261/* *INDENT-ON* */
1262
1263static clib_error_t *
1264interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1265 vlib_cli_command_t * cmd)
1266{
1267 vnet_main_t *vnm = vnet_get_main ();
1268 vnet_sw_interface_t *si = NULL;
1269 clib_error_t *error = 0;
1270 u32 sw_if_index = ~0;
1271 u8 mac[6] = { 0 };
1272 u8 is_add, is_del;
1273
1274 is_add = is_del = 0;
1275
1276 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1277 {
1278 error = clib_error_return (0, "unknown interface `%U'",
1279 format_unformat_error, input);
1280 goto done;
1281 }
1282 if (!unformat_user (input, unformat_ethernet_address, mac))
1283 {
1284 error = clib_error_return (0, "expected mac address `%U'",
1285 format_unformat_error, input);
1286 goto done;
1287 }
1288
1289 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1290 {
1291 if (unformat (input, "add"))
1292 is_add = 1;
1293 else if (unformat (input, "del"))
1294 is_del = 1;
1295 else
1296 break;
1297 }
1298
1299 if (is_add == is_del)
1300 {
1301 error = clib_error_return (0, "must choose one of add or del");
1302 goto done;
1303 }
1304
1305 si = vnet_get_sw_interface (vnm, sw_if_index);
1306 error =
1307 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1308
1309done:
1310 return error;
1311}
1312
1313/*?
1314 * The '<em>set interface secondary-mac-address </em>' command allows adding
1315 * or deleting extra MAC addresses on a given interface without changing the
1316 * default MAC address. This could allow packets sent to these MAC addresses
1317 * to be received without setting the interface to promiscuous mode.
1318 * Not all interfaces support this operation. The ones that do are mostly
1319 * hardware NICs, though virtio does also.
1320 *
1321 * @cliexpar
1322 * @parblock
1323 * Example of how to add a secondary MAC Address on an interface:
1324 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1325 * Example of how to delete a secondary MAC address from an interface:
1326 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1327 * @endparblock
1328?*/
1329/* *INDENT-OFF* */
1330VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1331 .path = "set interface secondary-mac-address",
1332 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1333 .function = interface_add_del_mac_address,
1334};
1335/* *INDENT-ON* */
1336
1337static clib_error_t *
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001338set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1339 vlib_cli_command_t * cmd)
1340{
1341 vnet_main_t *vnm = vnet_get_main ();
Neale Rannsd867a7c2017-10-04 02:29:07 -07001342 vnet_sw_interface_t *si = NULL;
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001343 clib_error_t *error = 0;
1344 u32 sw_if_index = ~0;
John Lo62fcc0a2017-10-31 14:31:10 -04001345 u8 mac[6] = { 0 };
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001346
1347 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1348 {
1349 error = clib_error_return (0, "unknown interface `%U'",
1350 format_unformat_error, input);
1351 goto done;
1352 }
John Lo62fcc0a2017-10-31 14:31:10 -04001353 if (!unformat_user (input, unformat_ethernet_address, mac))
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001354 {
1355 error = clib_error_return (0, "expected mac address `%U'",
1356 format_unformat_error, input);
1357 goto done;
1358 }
Neale Rannsd867a7c2017-10-04 02:29:07 -07001359 si = vnet_get_sw_interface (vnm, sw_if_index);
1360 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001361done:
1362 return error;
1363}
1364
1365/*?
1366 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1367 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1368 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1369 *
1370 * @cliexpar
1371 * @parblock
1372 * Example of how to change MAC Address of interface:
1373 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1374 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1375 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1376 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1377 * @endparblock
1378?*/
1379/* *INDENT-OFF* */
1380VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1381 .path = "set interface mac address",
Billy McFalle9bac692017-08-11 14:05:11 -04001382 .short_help = "set interface mac address <interface> <mac-address>",
Pavel Kotucekc631f2d2016-09-26 10:40:02 +02001383 .function = set_interface_mac_address,
1384};
1385/* *INDENT-ON* */
1386
Dave Barach7be864a2016-11-28 11:41:35 -05001387static clib_error_t *
1388set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1389{
1390 vnet_main_t *vnm = vnet_get_main ();
1391 u32 sw_if_index = ~0;
1392 u8 *tag = 0;
1393
1394 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1395 vnm, &sw_if_index, &tag))
1396 return clib_error_return (0, "unknown input `%U'",
1397 format_unformat_error, input);
1398
1399 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1400
1401 return 0;
1402}
1403
1404/* *INDENT-OFF* */
1405VLIB_CLI_COMMAND (set_tag_command, static) = {
1406 .path = "set interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001407 .short_help = "set interface tag <interface> <tag>",
Dave Barach7be864a2016-11-28 11:41:35 -05001408 .function = set_tag,
1409};
1410/* *INDENT-ON* */
1411
1412static clib_error_t *
1413clear_tag (vlib_main_t * vm, unformat_input_t * input,
1414 vlib_cli_command_t * cmd)
1415{
1416 vnet_main_t *vnm = vnet_get_main ();
1417 u32 sw_if_index = ~0;
1418
1419 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1420 return clib_error_return (0, "unknown input `%U'",
1421 format_unformat_error, input);
1422
1423 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1424
1425 return 0;
1426}
1427
1428/* *INDENT-OFF* */
1429VLIB_CLI_COMMAND (clear_tag_command, static) = {
1430 .path = "clear interface tag",
Billy McFalle9bac692017-08-11 14:05:11 -04001431 .short_help = "clear interface tag <interface>",
Dave Barach7be864a2016-11-28 11:41:35 -05001432 .function = clear_tag,
1433};
1434/* *INDENT-ON* */
1435
Damjan Marion44036902017-04-28 12:29:15 +02001436static clib_error_t *
Neale Ranns1855b8e2018-07-11 10:31:26 -07001437set_ip_directed_broadcast (vlib_main_t * vm,
1438 unformat_input_t * input, vlib_cli_command_t * cmd)
1439{
1440 vnet_main_t *vnm = vnet_get_main ();
1441 u32 sw_if_index = ~0;
1442 u8 enable = 0;
1443
1444 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1445 else if (unformat (input, "enable"))
1446 enable = 1;
1447 else if (unformat (input, "disable"))
1448 enable = 0;
1449 else
1450 return clib_error_return (0, "unknown input: `%U'",
1451 format_unformat_error, input);
1452
1453 if (~0 == sw_if_index)
1454 return clib_error_return (0, "specify an interface: `%U'",
1455 format_unformat_error, input);
1456
1457 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1458
1459 return 0;
1460}
1461
1462/*?
1463 * This command is used to enable/disable IP directed broadcast
1464 * If directed broadcast is enabled a packet sent to the interface's
1465 * subnet broadcast address will be sent L2 broadcast on the interface,
1466 * otherwise it is dropped.
1467 ?*/
1468/* *INDENT-OFF* */
1469VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1470 .path = "set interface ip directed-broadcast",
1471 .short_help = "set interface enable <interface> <enable|disable>",
1472 .function = set_ip_directed_broadcast,
1473};
1474/* *INDENT-ON* */
1475
Stevenad8015b2017-10-29 22:10:46 -07001476clib_error_t *
1477set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1478 u8 queue_id_valid, u32 queue_id,
Damjan Marioneabd4242020-10-07 20:59:07 +02001479 vnet_hw_if_rx_mode mode)
Stevenad8015b2017-10-29 22:10:46 -07001480{
1481 clib_error_t *error = 0;
1482 vnet_hw_interface_t *hw;
Damjan Marion94100532020-11-06 23:25:57 +01001483 u32 *queue_indices = 0;
Stevenad8015b2017-10-29 22:10:46 -07001484
1485 hw = vnet_get_hw_interface (vnm, hw_if_index);
1486
Damjan Marion94100532020-11-06 23:25:57 +01001487 if (queue_id_valid)
1488 {
1489 u32 queue_index;
1490 queue_index =
1491 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1492 if (queue_index == ~0)
1493 return clib_error_return (0, "unknown queue %u on interface %s",
1494 queue_id, hw->name);
1495 vec_add1 (queue_indices, queue_index);
Stevenad8015b2017-10-29 22:10:46 -07001496 }
1497 else
Damjan Marion94100532020-11-06 23:25:57 +01001498 queue_indices = hw->rx_queue_indices;
Stevenad8015b2017-10-29 22:10:46 -07001499
Damjan Marion94100532020-11-06 23:25:57 +01001500 for (int i = 0; i < vec_len (queue_indices); i++)
1501 {
1502 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1503 if (rv)
1504 goto done;
1505 }
1506
1507done:
1508 if (queue_indices != hw->rx_queue_indices)
1509 vec_free (queue_indices);
1510 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1511 return error;
Stevenad8015b2017-10-29 22:10:46 -07001512}
1513
Stevene3a395c2017-05-09 16:19:50 -07001514static clib_error_t *
Damjan Marion44036902017-04-28 12:29:15 +02001515set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1516 vlib_cli_command_t * cmd)
1517{
1518 clib_error_t *error = 0;
1519 unformat_input_t _line_input, *line_input = &_line_input;
1520 vnet_main_t *vnm = vnet_get_main ();
Damjan Marion44036902017-04-28 12:29:15 +02001521 u32 hw_if_index = (u32) ~ 0;
1522 u32 queue_id = (u32) ~ 0;
Damjan Marioneabd4242020-10-07 20:59:07 +02001523 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
Stevenad8015b2017-10-29 22:10:46 -07001524 u8 queue_id_valid = 0;
Dave Barach7be864a2016-11-28 11:41:35 -05001525
Damjan Marion44036902017-04-28 12:29:15 +02001526 if (!unformat_user (input, unformat_line_input, line_input))
1527 return 0;
1528
1529 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1530 {
1531 if (unformat
1532 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1533 ;
1534 else if (unformat (line_input, "queue %d", &queue_id))
Stevenad8015b2017-10-29 22:10:46 -07001535 queue_id_valid = 1;
Damjan Marion44036902017-04-28 12:29:15 +02001536 else if (unformat (line_input, "polling"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001537 mode = VNET_HW_IF_RX_MODE_POLLING;
Damjan Marion44036902017-04-28 12:29:15 +02001538 else if (unformat (line_input, "interrupt"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001539 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
Damjan Marion44036902017-04-28 12:29:15 +02001540 else if (unformat (line_input, "adaptive"))
Damjan Marioneabd4242020-10-07 20:59:07 +02001541 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
Damjan Marion44036902017-04-28 12:29:15 +02001542 else
1543 {
1544 error = clib_error_return (0, "parse error: '%U'",
1545 format_unformat_error, line_input);
1546 unformat_free (line_input);
1547 return error;
1548 }
1549 }
1550
1551 unformat_free (line_input);
1552
1553 if (hw_if_index == (u32) ~ 0)
1554 return clib_error_return (0, "please specify valid interface name");
1555
Damjan Marioneabd4242020-10-07 20:59:07 +02001556 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
Damjan Marion44036902017-04-28 12:29:15 +02001557 return clib_error_return (0, "please specify valid rx-mode");
1558
Stevenad8015b2017-10-29 22:10:46 -07001559 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1560 queue_id, mode);
Damjan Marion44036902017-04-28 12:29:15 +02001561
Stevene3a395c2017-05-09 16:19:50 -07001562 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001563}
1564
1565/*?
Billy McFalle9bac692017-08-11 14:05:11 -04001566 * This command is used to assign the RX packet processing mode (polling,
1567 * interrupt, adaptive) of the a given interface, and optionally a
1568 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1569 * is applied to all queues of the interface. Not all interfaces support
1570 * all modes. To display the current rx-mode use the command
1571 * '<em>show interface rx-placement</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001572 *
1573 * @cliexpar
Billy McFalle9bac692017-08-11 14:05:11 -04001574 * Example of how to assign rx-mode to all queues on an interface:
1575 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1576 * Example of how to assign rx-mode to one queue of an interface:
1577 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1578 * Example of how to display the rx-mode of all interfaces:
Damjan Marion44036902017-04-28 12:29:15 +02001579 * @cliexstart{show interface rx-placement}
1580 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001581 * node dpdk-input:
1582 * GigabitEthernet7/0/0 queue 0 (polling)
1583 * node vhost-user-input:
1584 * VirtualEthernet0/0/12 queue 0 (interrupt)
1585 * VirtualEthernet0/0/12 queue 2 (polling)
1586 * VirtualEthernet0/0/13 queue 0 (polling)
1587 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001588 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001589 * node dpdk-input:
1590 * GigabitEthernet7/0/1 queue 0 (polling)
1591 * node vhost-user-input:
1592 * VirtualEthernet0/0/12 queue 1 (polling)
1593 * VirtualEthernet0/0/12 queue 3 (polling)
1594 * VirtualEthernet0/0/13 queue 1 (polling)
1595 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001596 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001597?*/
1598/* *INDENT-OFF* */
Damjan Marionfe7d4a22018-04-13 19:43:39 +02001599VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
Damjan Marion44036902017-04-28 12:29:15 +02001600 .path = "set interface rx-mode",
1601 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1602 .function = set_interface_rx_mode,
1603};
1604/* *INDENT-ON* */
1605
1606static clib_error_t *
1607show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1608 vlib_cli_command_t * cmd)
1609{
1610 u8 *s = 0;
1611 vnet_main_t *vnm = vnet_get_main ();
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001612 vnet_hw_if_rx_queue_t **all_queues = 0;
1613 vnet_hw_if_rx_queue_t **qptr;
1614 vnet_hw_if_rx_queue_t *q;
Nathan Skrzypczak455779f2021-02-15 14:48:33 +01001615 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001616 vec_add1 (all_queues, q);
1617 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1618 u32 prev_node = ~0;
Damjan Marion44036902017-04-28 12:29:15 +02001619
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001620 vec_foreach (qptr, all_queues)
1621 {
1622 u32 current_thread = qptr[0]->thread_index;
1623 u32 hw_if_index = qptr[0]->hw_if_index;
1624 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1625 u32 current_node = hw_if->input_node_index;
1626 if (current_node != prev_node)
1627 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1628 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1629 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1630 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1631 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1632 current_thread != qptr[1]->thread_index)
1633 {
1634 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1635 vlib_worker_threads[current_thread].name, s);
1636 vec_reset_length (s);
1637 }
1638 prev_node = current_node;
1639 }
Damjan Marion44036902017-04-28 12:29:15 +02001640 vec_free (s);
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001641 vec_free (all_queues);
Damjan Marion44036902017-04-28 12:29:15 +02001642 return 0;
1643}
1644
Billy McFalle9bac692017-08-11 14:05:11 -04001645/*?
1646 * This command is used to display the interface and queue worker
1647 * thread placement.
1648 *
1649 * @cliexpar
1650 * Example of how to display the interface placement:
1651 * @cliexstart{show interface rx-placement}
1652 * Thread 1 (vpp_wk_0):
1653 * node dpdk-input:
1654 * GigabitEthernet7/0/0 queue 0 (polling)
1655 * node vhost-user-input:
1656 * VirtualEthernet0/0/12 queue 0 (polling)
1657 * VirtualEthernet0/0/12 queue 2 (polling)
1658 * VirtualEthernet0/0/13 queue 0 (polling)
1659 * VirtualEthernet0/0/13 queue 2 (polling)
1660 * Thread 2 (vpp_wk_1):
1661 * node dpdk-input:
1662 * GigabitEthernet7/0/1 queue 0 (polling)
1663 * node vhost-user-input:
1664 * VirtualEthernet0/0/12 queue 1 (polling)
1665 * VirtualEthernet0/0/12 queue 3 (polling)
1666 * VirtualEthernet0/0/13 queue 1 (polling)
1667 * VirtualEthernet0/0/13 queue 3 (polling)
1668 * @cliexend
1669?*/
Damjan Marion44036902017-04-28 12:29:15 +02001670/* *INDENT-OFF* */
1671VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1672 .path = "show interface rx-placement",
1673 .short_help = "show interface rx-placement",
1674 .function = show_interface_rx_placement_fn,
1675};
1676/* *INDENT-ON* */
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001677clib_error_t *
1678set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1679 u32 thread_index, u8 is_main)
Damjan Marion44036902017-04-28 12:29:15 +02001680{
Damjan Marion44036902017-04-28 12:29:15 +02001681 vnet_main_t *vnm = vnet_get_main ();
1682 vnet_device_main_t *vdm = &vnet_device_main;
Damjan Marion94100532020-11-06 23:25:57 +01001683 vnet_hw_interface_t *hw;
1684 u32 queue_index;
Damjan Marion44036902017-04-28 12:29:15 +02001685
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001686 if (is_main)
1687 thread_index = 0;
1688 else
1689 thread_index += vdm->first_worker_thread_index;
Damjan Marion44036902017-04-28 12:29:15 +02001690
1691 if (thread_index > vdm->last_worker_thread_index)
1692 return clib_error_return (0,
1693 "please specify valid worker thread or main");
1694
Damjan Marion94100532020-11-06 23:25:57 +01001695 hw = vnet_get_hw_interface (vnm, hw_if_index);
Damjan Marion44036902017-04-28 12:29:15 +02001696
Damjan Marion94100532020-11-06 23:25:57 +01001697 queue_index =
1698 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1699 if (queue_index == ~0)
1700 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1701 hw->name);
1702 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1703 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1704 return 0;
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001705}
1706
1707static clib_error_t *
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001708set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1709 vlib_cli_command_t *cmd)
Mohsin Kazmi54f7c512018-08-23 18:28:11 +02001710{
1711 clib_error_t *error = 0;
1712 unformat_input_t _line_input, *line_input = &_line_input;
1713 vnet_main_t *vnm = vnet_get_main ();
1714 u32 hw_if_index = (u32) ~ 0;
1715 u32 queue_id = (u32) 0;
1716 u32 thread_index = (u32) ~ 0;
1717 u8 is_main = 0;
1718
1719 if (!unformat_user (input, unformat_line_input, line_input))
1720 return 0;
1721
1722 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1723 {
1724 if (unformat
1725 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1726 ;
1727 else if (unformat (line_input, "queue %d", &queue_id))
1728 ;
1729 else if (unformat (line_input, "main", &thread_index))
1730 is_main = 1;
1731 else if (unformat (line_input, "worker %d", &thread_index))
1732 ;
1733 else
1734 {
1735 error = clib_error_return (0, "parse error: '%U'",
1736 format_unformat_error, line_input);
1737 unformat_free (line_input);
1738 return error;
1739 }
1740 }
1741
1742 unformat_free (line_input);
1743
1744 if (hw_if_index == (u32) ~ 0)
1745 return clib_error_return (0, "please specify valid interface name");
1746
1747 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1748 is_main);
1749
1750 return (error);
Damjan Marion44036902017-04-28 12:29:15 +02001751}
1752
1753/*?
1754 * This command is used to assign a given interface, and optionally a
1755 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
Billy McFalle9bac692017-08-11 14:05:11 -04001756 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1757 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
Damjan Marion44036902017-04-28 12:29:15 +02001758 *
1759 * @cliexpar
1760 * Example of how to display the interface placement:
Billy McFalle9bac692017-08-11 14:05:11 -04001761 * @cliexstart{show interface rx-placement}
Damjan Marion44036902017-04-28 12:29:15 +02001762 * Thread 1 (vpp_wk_0):
Billy McFalle9bac692017-08-11 14:05:11 -04001763 * node dpdk-input:
1764 * GigabitEthernet7/0/0 queue 0 (polling)
1765 * node vhost-user-input:
1766 * VirtualEthernet0/0/12 queue 0 (polling)
1767 * VirtualEthernet0/0/12 queue 2 (polling)
1768 * VirtualEthernet0/0/13 queue 0 (polling)
1769 * VirtualEthernet0/0/13 queue 2 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001770 * Thread 2 (vpp_wk_1):
Billy McFalle9bac692017-08-11 14:05:11 -04001771 * node dpdk-input:
1772 * GigabitEthernet7/0/1 queue 0 (polling)
1773 * node vhost-user-input:
1774 * VirtualEthernet0/0/12 queue 1 (polling)
1775 * VirtualEthernet0/0/12 queue 3 (polling)
1776 * VirtualEthernet0/0/13 queue 1 (polling)
1777 * VirtualEthernet0/0/13 queue 3 (polling)
Damjan Marion44036902017-04-28 12:29:15 +02001778 * @cliexend
Billy McFalle9bac692017-08-11 14:05:11 -04001779 * Example of how to assign a interface and queue to a worker thread:
1780 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1781 * Example of how to display the interface placement:
1782 * @cliexstart{show interface rx-placement}
1783 * Thread 1 (vpp_wk_0):
1784 * node dpdk-input:
1785 * GigabitEthernet7/0/0 queue 0 (polling)
1786 * node vhost-user-input:
1787 * VirtualEthernet0/0/12 queue 0 (polling)
1788 * VirtualEthernet0/0/12 queue 1 (polling)
1789 * VirtualEthernet0/0/12 queue 2 (polling)
1790 * VirtualEthernet0/0/13 queue 0 (polling)
1791 * VirtualEthernet0/0/13 queue 2 (polling)
1792 * Thread 2 (vpp_wk_1):
1793 * node dpdk-input:
1794 * GigabitEthernet7/0/1 queue 0 (polling)
1795 * node vhost-user-input:
1796 * VirtualEthernet0/0/12 queue 3 (polling)
1797 * VirtualEthernet0/0/13 queue 1 (polling)
1798 * VirtualEthernet0/0/13 queue 3 (polling)
1799 * @cliexend
Damjan Marion44036902017-04-28 12:29:15 +02001800?*/
1801/* *INDENT-OFF* */
1802VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1803 .path = "set interface rx-placement",
Billy McFalle9bac692017-08-11 14:05:11 -04001804 .short_help = "set interface rx-placement <interface> [queue <n>] "
Damjan Marion6f9ac652017-06-15 19:01:31 +02001805 "[worker <n> | main]",
Damjan Marion44036902017-04-28 12:29:15 +02001806 .function = set_interface_rx_placement,
Damjan Marion6f9ac652017-06-15 19:01:31 +02001807 .is_mp_safe = 1,
Damjan Marion44036902017-04-28 12:29:15 +02001808};
Damjan Marion44036902017-04-28 12:29:15 +02001809/* *INDENT-ON* */
Dave Barach5ecd5a52019-02-25 15:27:28 -05001810
Chenmin Sunc4665092020-07-06 08:20:39 +08001811clib_error_t *
1812set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1813 clib_bitmap_t * bitmap)
1814{
1815 vnet_main_t *vnm = vnet_get_main ();
1816 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1817
1818 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1819}
1820
1821static clib_error_t *
1822set_interface_rss_queues_fn (vlib_main_t * vm,
1823 unformat_input_t * input,
1824 vlib_cli_command_t * cmd)
1825{
1826 clib_error_t *error = 0;
1827 unformat_input_t _line_input, *line_input = &_line_input;
1828 vnet_main_t *vnm = vnet_get_main ();
1829 u32 hw_if_index = (u32) ~ 0;
1830 clib_bitmap_t *bitmap = NULL;
1831
1832 if (!unformat_user (input, unformat_line_input, line_input))
1833 return 0;
1834
1835 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1836 {
1837 if (unformat
1838 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1839 ;
1840 else
1841 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
1842 ;
1843 else
1844 {
1845 error = clib_error_return (0, "parse error: '%U'",
1846 format_unformat_error, line_input);
1847 unformat_free (line_input);
1848 goto done;
1849 }
1850 }
1851
1852 unformat_free (line_input);
1853
1854 if (hw_if_index == (u32) ~ 0)
1855 {
1856 error = clib_error_return (0, "please specify valid interface name");
1857 goto done;
1858 }
1859
1860 if (bitmap == NULL)
1861 {
1862 error = clib_error_return (0, "please specify the valid rss queues");
1863 goto done;
1864 }
1865
1866 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
1867
1868done:
1869 if (bitmap)
1870 clib_bitmap_free (bitmap);
1871
1872 return (error);
1873}
1874
1875/*?
1876 * This command is used to set the rss queues of a given interface
1877 * Not all the interfaces support this operation.
1878 * To display the current rss queues, use the command
1879 * '<em>show hardware-interfaces</em>'.
1880 *
1881 * @cliexpar
1882 * Example of how to set the rss queues to 0,2-5,7 of an interface:
1883 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
1884 * @cliexend
1885?*/
1886/* *INDENT-OFF* */
1887VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
1888 .path = "set interface rss queues",
1889 .short_help = "set interface rss queues <interface> <list <queue-list>>",
1890 .function = set_interface_rss_queues_fn,
1891};
1892/* *INDENT-ON* */
1893
Dave Barach33909772019-09-23 10:27:27 -04001894static u8 *
1895format_vnet_pcap (u8 * s, va_list * args)
1896{
1897 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
1898 int type = va_arg (*args, int);
1899 int printed = 0;
1900
1901 if (type == 0)
1902 {
1903 if (pp->pcap_rx_enable)
1904 {
1905 s = format (s, "rx");
1906 printed = 1;
1907 }
1908 if (pp->pcap_tx_enable)
1909 {
1910 if (printed)
1911 s = format (s, " and ");
1912 s = format (s, "tx");
1913 printed = 1;
1914 }
1915 if (pp->pcap_drop_enable)
1916 {
1917 if (printed)
1918 s = format (s, " and ");
1919 s = format (s, "drop");
1920 printed = 1;
1921 }
1922 return s;
1923 }
1924 s = format (s, "unknown type %d!", type);
1925 return s;
1926}
1927
1928
Dave Barachb97641c2019-09-09 16:38:17 -04001929int
1930vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
1931{
1932 vlib_main_t *vm = vlib_get_main ();
Damjan Marion8fb5add2021-03-04 18:41:59 +01001933 vnet_main_t *vnm = vnet_get_main ();
1934 vnet_pcap_t *pp = &vnm->pcap;
Dave Barachb97641c2019-09-09 16:38:17 -04001935 pcap_main_t *pm = &pp->pcap_main;
Dave Barachf5667c32019-09-25 11:27:46 -04001936 vnet_classify_main_t *cm = &vnet_classify_main;
Dave Barachb97641c2019-09-09 16:38:17 -04001937
1938 if (a->status)
1939 {
Dave Barach33909772019-09-23 10:27:27 -04001940 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04001941 {
1942 vlib_cli_output
Dave Barach33909772019-09-23 10:27:27 -04001943 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
1944 format_vnet_pcap, pp, 0 /* print type */ ,
Dave Barachb97641c2019-09-09 16:38:17 -04001945 pm->n_packets_captured, pm->n_packets_to_capture);
1946 vlib_cli_output (vm, "capture to file %s", pm->file_name);
1947 }
1948 else
Dave Barach33909772019-09-23 10:27:27 -04001949 vlib_cli_output (vm, "pcap dispatch capture disabled");
1950
Dave Barachb97641c2019-09-09 16:38:17 -04001951 return 0;
1952 }
1953
1954 /* Consistency checks */
1955
1956 /* Enable w/ capture already enabled not allowed */
Dave Barach33909772019-09-23 10:27:27 -04001957 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
1958 && (a->rx_enable + a->tx_enable + a->drop_enable))
Dave Barachb97641c2019-09-09 16:38:17 -04001959 return VNET_API_ERROR_INVALID_VALUE;
1960
1961 /* Disable capture with capture already disabled, not interesting */
Mohammed Hawari9fecbe12020-12-11 19:36:37 +01001962 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
1963 0) &&
1964 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
Dave Barachb97641c2019-09-09 16:38:17 -04001965 return VNET_API_ERROR_VALUE_EXIST;
1966
1967 /* Change number of packets to capture while capturing */
Dave Barach33909772019-09-23 10:27:27 -04001968 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
1969 && (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04001970 && (pm->n_packets_to_capture != a->packets_to_capture))
1971 return VNET_API_ERROR_INVALID_VALUE_2;
1972
Dave Barach33909772019-09-23 10:27:27 -04001973 /* Classify filter specified, but no classify filter configured */
Dave Barachf5667c32019-09-25 11:27:46 -04001974 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
Benoît Ganne5943e362021-02-26 14:46:58 +01001975 (!cm->classify_table_index_by_sw_if_index ||
1976 cm->classify_table_index_by_sw_if_index[0] == ~0))
Dave Barach9137e542019-09-13 17:47:50 -04001977 return VNET_API_ERROR_NO_SUCH_LABEL;
1978
Dave Barach33909772019-09-23 10:27:27 -04001979 if (a->rx_enable + a->tx_enable + a->drop_enable)
Dave Barachb97641c2019-09-09 16:38:17 -04001980 {
Dave Barach586462f2020-08-05 16:12:35 -04001981 void *save_pcap_data;
1982
Dave Barach33909772019-09-23 10:27:27 -04001983 /* Sanity check max bytes per pkt */
1984 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
1985 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
1986
Dave Barachb97641c2019-09-09 16:38:17 -04001987 /* Clean up from previous run, if any */
Dave Barach586462f2020-08-05 16:12:35 -04001988 vec_reset_length (pm->pcap_data);
1989
1990 /* Throw away the data buffer? */
1991 if (a->free_data)
1992 vec_free (pm->pcap_data);
1993
1994 save_pcap_data = pm->pcap_data;
1995
Dave Barachb97641c2019-09-09 16:38:17 -04001996 memset (pm, 0, sizeof (*pm));
1997
Dave Barach586462f2020-08-05 16:12:35 -04001998 pm->pcap_data = save_pcap_data;
1999
Dave Barach11fb09e2020-08-06 12:10:09 -04002000 vec_validate_aligned (vnet_trace_placeholder, 2048,
2001 CLIB_CACHE_LINE_BYTES);
Dave Barachb97641c2019-09-09 16:38:17 -04002002 if (pm->lock == 0)
2003 clib_spinlock_init (&(pm->lock));
2004
2005 if (a->filename == 0)
Dave Barach33909772019-09-23 10:27:27 -04002006 {
2007 u8 *stem = 0;
2008
2009 if (a->rx_enable)
2010 stem = format (stem, "rx");
2011 if (a->tx_enable)
2012 stem = format (stem, "tx");
2013 if (a->drop_enable)
2014 stem = format (stem, "drop");
Benoît Ganne7d4a9972020-07-17 11:49:56 +02002015 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
Dave Barach33909772019-09-23 10:27:27 -04002016 vec_free (stem);
2017 }
Dave Barachb97641c2019-09-09 16:38:17 -04002018
2019 pm->file_name = (char *) a->filename;
2020 pm->n_packets_captured = 0;
2021 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
Dave Barach586462f2020-08-05 16:12:35 -04002022 /* Preallocate the data vector? */
2023 if (a->preallocate_data)
2024 {
2025 vec_validate
2026 (pm->pcap_data, a->packets_to_capture
2027 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2028 vec_reset_length (pm->pcap_data);
2029 }
Dave Barachb97641c2019-09-09 16:38:17 -04002030 pm->n_packets_to_capture = a->packets_to_capture;
2031 pp->pcap_sw_if_index = a->sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002032 if (a->filter)
Jon Loeliger5c1e48c2020-10-15 14:41:36 -04002033 pp->filter_classify_table_index =
2034 cm->classify_table_index_by_sw_if_index[0];
Dave Barach9137e542019-09-13 17:47:50 -04002035 else
2036 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002037 pp->pcap_error_index = a->drop_err;
Dave Barach33909772019-09-23 10:27:27 -04002038 pp->pcap_rx_enable = a->rx_enable;
2039 pp->pcap_tx_enable = a->tx_enable;
2040 pp->pcap_drop_enable = a->drop_enable;
2041 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
Dave Barachb97641c2019-09-09 16:38:17 -04002042 }
2043 else
2044 {
Dave Barach33909772019-09-23 10:27:27 -04002045 pp->pcap_rx_enable = 0;
2046 pp->pcap_tx_enable = 0;
2047 pp->pcap_drop_enable = 0;
Dave Barachf5667c32019-09-25 11:27:46 -04002048 pp->filter_classify_table_index = ~0;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002049 pp->pcap_error_index = ~0;
Dave Barachb97641c2019-09-09 16:38:17 -04002050 if (pm->n_packets_captured)
2051 {
2052 clib_error_t *error;
2053 pm->n_packets_to_capture = pm->n_packets_captured;
2054 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2055 pm->n_packets_captured, pm->file_name);
2056 error = pcap_write (pm);
Andrew Yourtchenko4da15062019-09-11 14:14:43 +00002057 if (pm->flags & PCAP_MAIN_INIT_DONE)
Dave Barachb97641c2019-09-09 16:38:17 -04002058 pcap_close (pm);
2059 /* Report I/O errors... */
2060 if (error)
2061 {
2062 clib_error_report (error);
2063 return VNET_API_ERROR_SYSCALL_ERROR_1;
2064 }
Dave Barach586462f2020-08-05 16:12:35 -04002065 vec_free (pm->file_name);
2066 if (a->free_data)
2067 vec_free (pm->pcap_data);
Dave Barachb97641c2019-09-09 16:38:17 -04002068 return 0;
2069 }
2070 else
2071 return VNET_API_ERROR_NO_SUCH_ENTRY;
2072 }
2073
2074 return 0;
2075}
2076
2077static clib_error_t *
Dave Barach33909772019-09-23 10:27:27 -04002078pcap_trace_command_fn (vlib_main_t * vm,
2079 unformat_input_t * input, vlib_cli_command_t * cmd)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002080{
Dave Barach5ecd5a52019-02-25 15:27:28 -05002081 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb97641c2019-09-09 16:38:17 -04002082 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002083 vnet_main_t *vnm = vnet_get_main ();
Dave Barachb97641c2019-09-09 16:38:17 -04002084 u8 *filename = 0;
2085 u32 max = 1000;
Dave Barach33909772019-09-23 10:27:27 -04002086 u32 max_bytes_per_pkt = 512;
Dave Barachb97641c2019-09-09 16:38:17 -04002087 int rv;
Dave Barach33909772019-09-23 10:27:27 -04002088 int rx_enable = 0;
2089 int tx_enable = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002090 int preallocate_data = 0;
Dave Barach33909772019-09-23 10:27:27 -04002091 int drop_enable = 0;
Dave Barachb97641c2019-09-09 16:38:17 -04002092 int status = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002093 int filter = 0;
Dave Barach586462f2020-08-05 16:12:35 -04002094 int free_data = 0;
Dave Barach4330c462020-06-10 17:07:32 -04002095 u32 sw_if_index = 0; /* default: any interface */
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002096 vlib_error_t drop_err = ~0; /* default: any error */
Dave Barach5ecd5a52019-02-25 15:27:28 -05002097
2098 /* Get a line of input. */
2099 if (!unformat_user (input, unformat_line_input, line_input))
2100 return 0;
2101
2102 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2103 {
Dave Barach33909772019-09-23 10:27:27 -04002104 if (unformat (line_input, "rx"))
2105 rx_enable = 1;
2106 else if (unformat (line_input, "tx"))
2107 tx_enable = 1;
2108 else if (unformat (line_input, "drop"))
2109 drop_enable = 1;
2110 else if (unformat (line_input, "off"))
2111 rx_enable = tx_enable = drop_enable = 0;
2112 else if (unformat (line_input, "max-bytes-per-pkt %u",
2113 &max_bytes_per_pkt))
Dave Barachb97641c2019-09-09 16:38:17 -04002114 ;
2115 else if (unformat (line_input, "max %d", &max))
2116 ;
2117 else if (unformat (line_input, "packets-to-capture %d", &max))
2118 ;
2119 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2120 &filename))
2121 ;
2122 else if (unformat (line_input, "status %=", &status, 1))
2123 ;
2124 else if (unformat (line_input, "intfc %U",
2125 unformat_vnet_sw_interface, vnm, &sw_if_index))
2126 ;
Dave Barach586462f2020-08-05 16:12:35 -04002127 else if (unformat (line_input, "interface %U",
2128 unformat_vnet_sw_interface, vnm, &sw_if_index))
2129 ;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002130 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2131 &drop_err))
2132 ;
Dave Barach586462f2020-08-05 16:12:35 -04002133 else if (unformat (line_input, "preallocate-data %=",
2134 &preallocate_data, 1))
2135 ;
2136 else if (unformat (line_input, "free-data %=", &free_data, 1))
2137 ;
2138 else if (unformat (line_input, "intfc any")
2139 || unformat (line_input, "interface any"))
Dave Barachb97641c2019-09-09 16:38:17 -04002140 sw_if_index = 0;
Dave Barach9137e542019-09-13 17:47:50 -04002141 else if (unformat (line_input, "filter"))
2142 filter = 1;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002143 else
2144 {
Dave Barachb97641c2019-09-09 16:38:17 -04002145 return clib_error_return (0, "unknown input `%U'",
2146 format_unformat_error, line_input);
Dave Barach5ecd5a52019-02-25 15:27:28 -05002147 }
2148 }
Dave Barachb97641c2019-09-09 16:38:17 -04002149
Dave Barach5ecd5a52019-02-25 15:27:28 -05002150 unformat_free (line_input);
2151
Dave Barachb97641c2019-09-09 16:38:17 -04002152 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2153 a->filename = filename;
Dave Barach33909772019-09-23 10:27:27 -04002154 a->rx_enable = rx_enable;
2155 a->tx_enable = tx_enable;
Dave Barach586462f2020-08-05 16:12:35 -04002156 a->preallocate_data = preallocate_data;
2157 a->free_data = free_data;
Dave Barach33909772019-09-23 10:27:27 -04002158 a->drop_enable = drop_enable;
Dave Barachb97641c2019-09-09 16:38:17 -04002159 a->status = status;
2160 a->packets_to_capture = max;
Dave Barachb97641c2019-09-09 16:38:17 -04002161 a->sw_if_index = sw_if_index;
Dave Barach9137e542019-09-13 17:47:50 -04002162 a->filter = filter;
Dave Barach33909772019-09-23 10:27:27 -04002163 a->max_bytes_per_pkt = max_bytes_per_pkt;
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002164 a->drop_err = drop_err;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002165
Dave Barachb97641c2019-09-09 16:38:17 -04002166 rv = vnet_pcap_dispatch_trace_configure (a);
2167
2168 switch (rv)
Dave Barach5ecd5a52019-02-25 15:27:28 -05002169 {
Dave Barachb97641c2019-09-09 16:38:17 -04002170 case 0:
2171 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002172
Dave Barachb97641c2019-09-09 16:38:17 -04002173 case VNET_API_ERROR_INVALID_VALUE:
2174 return clib_error_return (0, "dispatch trace already enabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002175
Dave Barachb97641c2019-09-09 16:38:17 -04002176 case VNET_API_ERROR_VALUE_EXIST:
2177 return clib_error_return (0, "dispatch trace already disabled...");
Dave Barach5ecd5a52019-02-25 15:27:28 -05002178
Dave Barachb97641c2019-09-09 16:38:17 -04002179 case VNET_API_ERROR_INVALID_VALUE_2:
2180 return clib_error_return
2181 (0, "can't change number of records to capture while tracing...");
2182
2183 case VNET_API_ERROR_SYSCALL_ERROR_1:
2184 return clib_error_return (0, "I/O writing trace capture...");
2185
2186 case VNET_API_ERROR_NO_SUCH_ENTRY:
2187 return clib_error_return (0, "No packets captured...");
2188
Dave Barach33909772019-09-23 10:27:27 -04002189 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2190 return clib_error_return (0,
2191 "Max bytes per pkt must be > 32, < 9000...");
2192
Dave Barach9137e542019-09-13 17:47:50 -04002193 case VNET_API_ERROR_NO_SUCH_LABEL:
2194 return clib_error_return
2195 (0, "No classify filter configured, see 'classify filter...'");
2196
Dave Barachb97641c2019-09-09 16:38:17 -04002197 default:
2198 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2199 break;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002200 }
Dave Barachb97641c2019-09-09 16:38:17 -04002201 return 0;
Dave Barach5ecd5a52019-02-25 15:27:28 -05002202}
2203
Dave Barach5ecd5a52019-02-25 15:27:28 -05002204/*?
2205 * This command is used to start or stop a packet capture, or show
Dave Barach33909772019-09-23 10:27:27 -04002206 * the status of packet capture.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002207 *
2208 * This command has the following optional parameters:
2209 *
Dave Barach33909772019-09-23 10:27:27 -04002210 *
2211 * - <b>rx</b> - Capture received packets
2212 *
2213 * - <b>tx</b> - Capture transmitted packets
2214 *
2215 * - <b>drop</b> - Capture dropped packets
2216 *
2217 * - <b>off</b> - Stop capturing packets, write results to the specified file
Dave Barach5ecd5a52019-02-25 15:27:28 -05002218 *
2219 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2220 * of packets have been received, buffer is flushed to file. Once another
2221 * '<em>nn</em>' number of packets have been received, buffer is flushed
2222 * to file, overwriting previous write. If not entered, value defaults
2223 * to 100. Can only be updated if packet capture is off.
2224 *
Dave Barach33909772019-09-23 10:27:27 -04002225 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2226 * for each packet. Must be >= 32, <= 9000.
2227 *
Dave Barach586462f2020-08-05 16:12:35 -04002228 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2229 * vector expansion delays during pcap capture
2230 *
2231 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2232 * to retain the data buffer so this option is seldom used.
2233 *
Dave Barach33909772019-09-23 10:27:27 -04002234 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002235 * or use '<em>any</em>' to run packet capture on all interfaces.
2236 * '<em>any</em>' is the default if not provided. Settings from a previous
2237 * packet capture are preserved, so '<em>any</em>' can be used to reset
2238 * the interface setting.
2239 *
Dave Barachf5667c32019-09-25 11:27:46 -04002240 * - <b>filter</b> - Use the pcap rx / tx / drop trace filter, which
2241 * must be configured. Use <b>classify filter pcap...</b> to configure the
2242 * filter. The filter will only be executed if the per-interface or
2243 * any-interface tests fail.
2244 *
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002245 * - <b>error <node>.<error></b> - filter packets based on a specific error.
2246 * For example: error {ip4-udp-lookup}.{No listener for dst port}
2247 *
Dave Barach5ecd5a52019-02-25 15:27:28 -05002248 * - <b>file <name></b> - Used to specify the output filename. The file will
2249 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2250 * supported. Directory should not be entered. If file already exists, file
Dave Barach33909772019-09-23 10:27:27 -04002251 * will be overwritten. If no filename is provided, the file will be
2252 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2253 * Can only be updated if packet capture is off.
Dave Barach5ecd5a52019-02-25 15:27:28 -05002254 *
2255 * - <b>status</b> - Displays the current status and configured attributes
2256 * associated with a packet capture. If packet capture is in progress,
2257 * '<em>status</em>' also will return the number of packets currently in
2258 * the local buffer. All additional attributes entered on command line
2259 * with '<em>status</em>' will be ignored and not applied.
2260 *
2261 * @cliexpar
2262 * Example of how to display the status of a tx packet capture when off:
Dave Barach33909772019-09-23 10:27:27 -04002263 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002264 * max is 100, for any interface to file /tmp/vpe.pcap
2265 * pcap tx capture is off...
2266 * @cliexend
2267 * Example of how to start a tx packet capture:
Benoît Ganne5cfe4522021-03-03 17:37:25 +01002268 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
2269vppTest.pcap}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002270 * @cliexend
2271 * Example of how to display the status of a tx packet capture in progress:
Dave Barach33909772019-09-23 10:27:27 -04002272 * @cliexstart{pcap trace status}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002273 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2274 * pcap tx capture is on: 20 of 35 pkts...
2275 * @cliexend
2276 * Example of how to stop a tx packet capture:
Dave Barach33909772019-09-23 10:27:27 -04002277 * @cliexstart{pcap trace off}
Dave Barach5ecd5a52019-02-25 15:27:28 -05002278 * captured 21 pkts...
2279 * saved to /tmp/vppTest.pcap...
2280 * @cliexend
2281?*/
2282/* *INDENT-OFF* */
2283
2284VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
Dave Barach33909772019-09-23 10:27:27 -04002285 .path = "pcap trace",
Dave Barach5ecd5a52019-02-25 15:27:28 -05002286 .short_help =
Dave Barach586462f2020-08-05 16:12:35 -04002287 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2288 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2289 " [preallocate-data][free-data]",
Dave Barach33909772019-09-23 10:27:27 -04002290 .function = pcap_trace_command_fn,
Dave Barach5ecd5a52019-02-25 15:27:28 -05002291};
2292/* *INDENT-ON* */
2293
Dave Barachba868bb2016-08-08 09:51:21 -04002294/*
2295 * fd.io coding-style-patch-verification: ON
2296 *
2297 * Local Variables:
2298 * eval: (c-set-style "gnu")
2299 * End:
2300 */