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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * interface_cli.c: interface CLI
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
41 * @brief Interface CLI.
43 * Source code for several CLI interface commands.
46 #include <vnet/vnet.h>
47 #include <vnet/ip/ip.h>
48 #include <vppinfra/bitmap.h>
49 #include <vnet/fib/ip4_fib.h>
50 #include <vnet/fib/ip6_fib.h>
51 #include <vnet/l2/l2_output.h>
52 #include <vnet/l2/l2_input.h>
53 #include <vnet/classify/vnet_classify.h>
54 #include <vnet/interface/rx_queue_funcs.h>
55 #include <vnet/interface/tx_queue_funcs.h>
56 #include <vnet/hash/hash.h>
57 #include <vnet/dev/dev.h>
58 #include <vnet/dev/dev_funcs.h>
61 compare_interface_names (void *a1, void *a2)
66 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
70 show_or_clear_hw_interfaces (vlib_main_t * vm,
71 unformat_input_t * input,
72 vlib_cli_command_t * cmd, int is_show)
74 clib_error_t *error = 0;
75 unformat_input_t _line_input, *line_input = &_line_input;
76 vnet_main_t *vnm = vnet_get_main ();
77 vnet_interface_main_t *im = &vnm->interface_main;
78 vnet_hw_interface_t *hi;
79 u32 hw_if_index, *hw_if_indices = 0;
80 int i, verbose = -1, show_bond = 0;
82 if (!unformat_user (input, unformat_line_input, line_input))
85 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
87 /* See if user wants to show a specific interface. */
88 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
90 vec_add1 (hw_if_indices, hw_if_index);
92 /* See if user wants to show an interface with a specific hw_if_index. */
93 else if (unformat (line_input, "%u", &hw_if_index))
94 vec_add1 (hw_if_indices, hw_if_index);
96 else if (unformat (line_input, "verbose"))
97 verbose = 1; /* this is also the default */
99 else if (unformat (line_input, "detail"))
102 else if (unformat (line_input, "brief"))
105 else if (unformat (line_input, "bond"))
109 verbose = 0; /* default to brief for link bonding */
114 error = clib_error_return (0, "unknown input `%U'",
115 format_unformat_error, line_input);
116 unformat_free (line_input);
121 unformat_free (line_input);
124 /* Gather interfaces. */
125 if (vec_len (hw_if_indices) == 0)
126 pool_foreach (hi, im->hw_interfaces)
127 vec_add1 (hw_if_indices, hi - im->hw_interfaces);
130 verbose = 1; /* default to verbose (except bond) */
135 vec_sort_with_function (hw_if_indices, compare_interface_names);
137 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
138 for (i = 0; i < vec_len (hw_if_indices); i++)
140 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
141 if (show_bond == 0) /* show all interfaces */
142 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
144 else if ((hi->bond_info) &&
145 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
146 { /* show only bonded interface and all its slave interfaces */
148 vnet_hw_interface_t *shi;
149 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
152 clib_bitmap_foreach (hw_idx, hi->bond_info)
154 shi = vnet_get_hw_interface(vnm, hw_idx);
155 vlib_cli_output (vm, "%U\n",
156 format_vnet_hw_interface, vnm, shi, verbose);
163 for (i = 0; i < vec_len (hw_if_indices); i++)
165 vnet_device_class_t *dc;
167 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
168 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
170 if (dc->clear_counters)
171 dc->clear_counters (hi->dev_instance);
176 vec_free (hw_if_indices);
180 static clib_error_t *
181 show_hw_interfaces (vlib_main_t * vm,
182 unformat_input_t * input, vlib_cli_command_t * cmd)
184 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
187 static clib_error_t *
188 clear_hw_interfaces (vlib_main_t * vm,
189 unformat_input_t * input, vlib_cli_command_t * cmd)
191 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
196 * Display more detailed information about all or a list of given interfaces.
197 * The verboseness of the output can be controlled by the following optional
199 * - brief: Only show name, index and state (default for bonded interfaces).
200 * - verbose: Also display additional attributes (default for all other interfaces).
201 * - detail: Also display all remaining attributes and extended statistics.
203 * To limit the output of the command to bonded interfaces and their slave
204 * interfaces, use the '<em>bond</em>' optional parameter.
207 * Example of how to display default data for all interfaces:
208 * @cliexstart{show hardware-interfaces}
209 * Name Idx Link Hardware
210 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
211 * Ethernet address ec:f4:bb:c0:bc:fc
213 * carrier up full duplex speed 1000 mtu 9216
214 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
216 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
217 * Ethernet address ec:f4:bb:c0:bc:fd
219 * carrier up full duplex speed 1000 mtu 9216
220 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
222 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
223 * Ethernet address 02:fe:a5:a9:8b:8e
224 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
225 * Ethernet address 02:fe:c0:4e:3b:b0
226 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
227 * Ethernet address 02:fe:1f:73:92:81
228 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
229 * Ethernet address 02:fe:f2:25:c4:68
230 * local0 0 down local0
233 * Example of how to display '<em>verbose</em>' data for an interface by name and
234 * software index (where 2 is the software index):
235 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
236 * Name Idx Link Hardware
237 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
238 * Ethernet address ec:f4:bb:c0:bc:fc
240 * carrier up full duplex speed 1000 mtu 9216
241 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
243 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
244 * Ethernet address ec:f4:bb:c0:bc:fd
246 * carrier up full duplex speed 1000 mtu 9216
247 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
251 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
252 .path = "show hardware-interfaces",
253 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
254 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
255 .function = show_hw_interfaces,
260 * Clear the extended statistics for all or a list of given interfaces
261 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
264 * Example of how to clear the extended statistics for all interfaces:
265 * @cliexcmd{clear hardware-interfaces}
266 * Example of how to clear the extended statistics for an interface by
267 * name and software index (where 2 is the software index):
268 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
270 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
271 .path = "clear hardware-interfaces",
272 .short_help = "clear hardware-interfaces "
273 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
274 .function = clear_hw_interfaces,
278 sw_interface_name_compare (void *a1, void *a2)
280 vnet_sw_interface_t *si1 = a1;
281 vnet_sw_interface_t *si2 = a2;
283 return vnet_sw_interface_compare (vnet_get_main (),
284 si1->sw_if_index, si2->sw_if_index);
287 static clib_error_t *
288 show_sw_interfaces (vlib_main_t * vm,
289 unformat_input_t * input, vlib_cli_command_t * cmd)
291 clib_error_t *error = 0;
292 vnet_main_t *vnm = vnet_get_main ();
293 unformat_input_t _linput, *linput = &_linput;
294 vnet_interface_main_t *im = &vnm->interface_main;
295 vnet_sw_interface_t *si, *sorted_sis = 0;
296 u32 sw_if_index = ~(u32) 0;
297 u8 show_addresses = 0;
298 u8 show_features = 0;
304 * Get a line of input. Won't work if the user typed
305 * "show interface" and nothing more.
307 if (unformat_user (input, unformat_line_input, linput))
309 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
311 /* See if user wants to show specific interface */
313 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
315 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
316 vec_add1 (sorted_sis, si[0]);
318 else if (unformat (linput, "address") || unformat (linput, "addr"))
320 else if (unformat (linput, "features") || unformat (linput, "feat"))
322 else if (unformat (linput, "tag"))
324 else if (unformat (linput, "vtr"))
326 else if (unformat (linput, "verbose"))
328 else if (unformat (linput, "%d", &sw_if_index))
330 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
332 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
333 vec_add1 (sorted_sis, si[0]);
337 vec_free (sorted_sis);
338 error = clib_error_return (0, "unknown interface index `%d'",
345 vec_free (sorted_sis);
346 error = clib_error_return (0, "unknown input `%U'",
347 format_unformat_error, linput);
351 unformat_free (linput);
353 if (show_features || show_tag || show_vtr)
355 if (sw_if_index == ~(u32) 0)
357 vec_free (sorted_sis);
358 return clib_error_return (0, "Interface not specified...");
364 vnet_interface_features_show (vm, sw_if_index, verbose);
365 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
367 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
368 vlib_cli_output (vm, "\nl2-output:");
369 if (l2_output->out_vtr_flag)
370 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
371 vlib_cli_output (vm, "%U", format_l2_output_features,
372 l2_output->feature_bitmap, 1);
373 vec_free (sorted_sis);
379 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
380 vlib_cli_output (vm, "%U: %s",
381 format_vnet_sw_if_index_name, vnm, sw_if_index,
382 tag ? (char *) tag : "(none)");
383 vec_free (sorted_sis);
388 * Show vlan tag rewrite data for one interface.
392 u32 vtr_op = L2_VTR_DISABLED;
393 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
395 if (l2vtr_get (vm, vnm, sw_if_index,
396 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
398 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
399 format_vnet_sw_if_index_name, vnm, sw_if_index);
402 vlib_cli_output (vm, "%U: VTR %0U",
403 format_vnet_sw_if_index_name, vnm, sw_if_index,
404 format_vtr, vtr_op, push_dot1q, tag1, tag2);
409 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
411 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
413 /* Gather interfaces. */
415 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
416 vec_set_len (sorted_sis, 0);
417 pool_foreach (si, im->sw_interfaces)
419 int visible = vnet_swif_is_api_visible (si);
421 vec_add1 (sorted_sis, si[0]);
424 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
429 vec_foreach (si, sorted_sis)
431 ip4_main_t *im4 = &ip4_main;
432 ip6_main_t *im6 = &ip6_main;
433 ip_lookup_main_t *lm4 = &im4->lookup_main;
434 ip_lookup_main_t *lm6 = &im6->lookup_main;
435 ip_interface_address_t *ia = 0;
436 u32 fib_index4 = 0, fib_index6 = 0;
438 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
439 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
442 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
443 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
446 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
447 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
449 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
451 (vm, "%U (%s): \n unnumbered, use %U",
452 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
453 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
454 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
458 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
459 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
461 /* Display any L2 info */
462 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
464 /* Display any IP4 addressing info */
465 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
466 1 /* honor unnumbered */,
468 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
469 if (fib4->hash.table_id)
471 vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address,
472 r4, ia->address_length, fib4->hash.table_id,
473 ip4_fib_index_from_table_id (fib4->hash.table_id));
475 vlib_cli_output (vm, " L3 %U/%d",
476 format_ip4_address, r4, ia->address_length);
479 /* Display any IP6 addressing info */
480 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
481 1 /* honor unnumbered */,
483 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
485 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
486 format_ip6_address, r6, ia->address_length,
488 ip6_fib_index_from_table_id (fib6->table_id));
490 vlib_cli_output (vm, " L3 %U/%d",
491 format_ip6_address, r6, ia->address_length);
497 vec_foreach (si, sorted_sis)
499 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
504 vec_free (sorted_sis);
508 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
509 .path = "show interface",
510 .short_help = "show interface [address|addr|features|feat|vtr|tag] "
511 "[<interface> [<interface> [..]]] [verbose]",
512 .function = show_sw_interfaces,
516 /* Root of all interface commands. */
517 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
519 .short_help = "Interface commands",
522 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
523 .path = "set interface",
524 .short_help = "Interface commands",
527 static clib_error_t *
528 clear_interface_counters (vlib_main_t * vm,
529 unformat_input_t * input, vlib_cli_command_t * cmd)
531 vnet_main_t *vnm = vnet_get_main ();
532 vnet_interface_main_t *im = &vnm->interface_main;
533 vlib_simple_counter_main_t *sm;
534 vlib_combined_counter_main_t *cm;
537 n_counters = vec_len (im->combined_sw_if_counters);
539 for (j = 0; j < n_counters; j++)
541 im = &vnm->interface_main;
542 cm = im->combined_sw_if_counters + j;
543 vlib_clear_combined_counters (cm);
546 n_counters = vec_len (im->sw_if_counters);
548 for (j = 0; j < n_counters; j++)
550 im = &vnm->interface_main;
551 sm = im->sw_if_counters + j;
552 vlib_clear_simple_counters (sm);
559 * Clear the statistics for all interfaces (statistics associated with the
560 * '<em>show interface</em>' command).
563 * Example of how to clear the statistics for all interfaces:
564 * @cliexcmd{clear interfaces}
566 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
567 .path = "clear interfaces",
568 .short_help = "clear interfaces",
569 .function = clear_interface_counters,
573 * Parse subinterface names.
575 * The following subinterface syntax is supported. The first two are for
576 * backwards compatability:
579 * - a subinterface with the name <intf-name>.<id>. The subinterface
580 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
582 * <intf-name> <min_id>-<max_id>
583 * - a set of the above subinterfaces, repeating for each id
584 * in the range <min_id> to <max_id>
586 * In the following, exact-match semantics (i.e. the number of vlan tags on the
587 * packet must match the number of tags in the configuration) are used only if
588 * the keyword exact-match is present. Non-exact match is the default.
590 * <intf-name> <id> dot1q <outer_id> [exact-match]
591 * - a subinterface with the name <intf-name>.<id>. The subinterface
592 * is a single dot1q vlan with vlan id <outer_id>.
594 * <intf-name> <id> dot1q any [exact-match]
595 * - a subinterface with the name <intf-name>.<id>. The subinterface
596 * is a single dot1q vlan with any vlan id.
598 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
599 * - a subinterface with the name <intf-name>.<id>. The subinterface
600 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
603 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
604 * - a subinterface with the name <intf-name>.<id>. The subinterface
605 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
607 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
609 * - a subinterface with the name <intf-name>.<id>. The subinterface
610 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
612 * For each of the above CLI, there is a duplicate that uses the keyword
613 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
614 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
615 * tagged packets the inner ethertype is always 0x8100. Also note that
616 * the dot1q and dot1ad naming spaces are independent, so it is legal to
617 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
619 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
620 * - a subinterface with the name <intf-name>.<id>. The subinterface
621 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
624 * <intf-name> <id> untagged
625 * - a subinterface with the name <intf-name>.<id>. The subinterface
626 * has no vlan tags. Only one can be specified per interface.
628 * <intf-name> <id> default
629 * - a subinterface with the name <intf-name>.<id>. This is associated
630 * with a packet that did not match any other configured subinterface
631 * on this interface. Only one can be specified per interface.
634 static clib_error_t *
635 parse_vlan_sub_interfaces (unformat_input_t * input,
636 vnet_sw_interface_t * template)
638 clib_error_t *error = 0;
639 u32 inner_vlan, outer_vlan;
641 if (unformat (input, "any inner-dot1q any"))
643 template->sub.eth.flags.two_tags = 1;
644 template->sub.eth.flags.outer_vlan_id_any = 1;
645 template->sub.eth.flags.inner_vlan_id_any = 1;
647 else if (unformat (input, "any"))
649 template->sub.eth.flags.one_tag = 1;
650 template->sub.eth.flags.outer_vlan_id_any = 1;
652 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
654 template->sub.eth.flags.two_tags = 1;
655 template->sub.eth.flags.inner_vlan_id_any = 1;
656 template->sub.eth.outer_vlan_id = outer_vlan;
658 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
660 template->sub.eth.flags.two_tags = 1;
661 template->sub.eth.outer_vlan_id = outer_vlan;
662 template->sub.eth.inner_vlan_id = inner_vlan;
664 else if (unformat (input, "%d", &outer_vlan))
666 template->sub.eth.flags.one_tag = 1;
667 template->sub.eth.outer_vlan_id = outer_vlan;
671 error = clib_error_return (0, "expected dot1q config, got `%U'",
672 format_unformat_error, input);
676 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
678 if (unformat (input, "exact-match"))
680 template->sub.eth.flags.exact_match = 1;
688 static clib_error_t *
689 create_sub_interfaces (vlib_main_t * vm,
690 unformat_input_t * input, vlib_cli_command_t * cmd)
692 vnet_main_t *vnm = vnet_get_main ();
693 clib_error_t *error = 0;
694 u32 hw_if_index, sw_if_index;
695 vnet_hw_interface_t *hi;
696 u32 id, id_min, id_max;
697 vnet_sw_interface_t template;
700 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
702 error = clib_error_return (0, "unknown interface `%U'",
703 format_unformat_error, input);
707 clib_memset (&template, 0, sizeof (template));
708 template.sub.eth.raw_flags = 0;
710 if (unformat (input, "%d default", &id_min))
713 template.sub.eth.flags.default_sub = 1;
715 else if (unformat (input, "%d untagged", &id_min))
718 template.sub.eth.flags.no_tags = 1;
719 template.sub.eth.flags.exact_match = 1;
721 else if (unformat (input, "%d dot1q", &id_min))
723 /* parse dot1q config */
725 error = parse_vlan_sub_interfaces (input, &template);
729 else if (unformat (input, "%d dot1ad", &id_min))
731 /* parse dot1ad config */
733 template.sub.eth.flags.dot1ad = 1;
734 error = parse_vlan_sub_interfaces (input, &template);
738 else if (unformat (input, "%d-%d", &id_min, &id_max))
740 template.sub.eth.flags.one_tag = 1;
741 template.sub.eth.flags.exact_match = 1;
745 else if (unformat (input, "%d", &id_min))
748 template.sub.eth.flags.one_tag = 1;
749 template.sub.eth.outer_vlan_id = id_min;
750 template.sub.eth.flags.exact_match = 1;
755 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
756 format_unformat_error, input);
760 hi = vnet_get_hw_interface (vnm, hw_if_index);
762 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
765 clib_error_return (0,
766 "not allowed as %v belong to a BondEthernet interface",
771 for (id = id_min; id <= id_max; id++)
774 vnet_interface_main_t *im = &vnm->interface_main;
775 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
778 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
782 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
783 hi->sw_if_index, id);
787 template.type = VNET_SW_INTERFACE_TYPE_SUB;
788 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
789 template.sup_sw_if_index = hi->sw_if_index;
790 template.sub.id = id;
792 template.sub.eth.outer_vlan_id = id;
794 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
798 kp = clib_mem_alloc (sizeof (*kp));
799 *kp = sup_and_sub_key;
801 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
802 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
803 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
804 vnet_get_main (), sw_if_index);
812 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
813 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
814 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
815 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
816 * but this is not recommended.
818 * This command has several variations:
819 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
820 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
822 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
823 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
824 * match any other subinterfaces should be sent to this subinterface.
826 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
827 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
828 * to this subinterface.
830 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
831 * subinterfaces to handle a range of VLAN IDs.
833 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
834 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
835 * VLAN ID different from the '<em>subId</em>'.
837 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
838 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
841 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
842 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
843 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
844 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
845 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
846 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
847 * entered, then the default behavior is exact-match.
849 * Use the '<em>show interface</em>' command to display all subinterfaces.
853 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
854 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
856 * The previous example is shorthand and is equivalent to:
857 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
860 * Example of how to create a subinterface number that is different from the VLAN ID:
861 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
864 * Examples of how to create q-in-q and q-in-any subinterfaces:
865 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
866 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
868 * Examples of how to create dot1ad interfaces:
869 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
870 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
873 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
874 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
875 * is non-exact match:
876 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
878 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
879 * is specified and only one VLAN is configured, but packet contains two VLANs:
880 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
883 * Example of how to created a subinterface to process untagged packets:
884 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
886 * Example of how to created a subinterface to process any packet with a VLAN ID that
887 * does not match any other subinterface:
888 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
890 * When subinterfaces are created, they are in the down state. Example of how to
891 * enable a newly created subinterface:
892 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
895 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
896 .path = "create sub-interfaces",
897 .short_help = "create sub-interfaces <interface> "
898 "{<subId> [default|untagged]} | "
899 "{<subId>-<subId>} | "
900 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
901 .function = create_sub_interfaces,
904 static clib_error_t *
905 set_state (vlib_main_t * vm,
906 unformat_input_t * input, vlib_cli_command_t * cmd)
908 vnet_main_t *vnm = vnet_get_main ();
910 u32 sw_if_index, flags;
913 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
915 error = clib_error_return (0, "unknown interface `%U'",
916 format_unformat_error, input);
920 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
922 error = clib_error_return (0, "unknown flags `%U'",
923 format_unformat_error, input);
927 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
936 * This command is used to change the admin state (up/down) of an interface.
938 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
939 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
940 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
941 * '<em>punt</em>' flag (interface is still down).
944 * Example of how to configure the admin state of an interface to
946 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
947 * Example of how to configure the admin state of an interface to
949 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
951 VLIB_CLI_COMMAND (set_state_command, static) = {
952 .path = "set interface state",
953 .short_help = "set interface state <interface> [up|down|punt|enable]",
954 .function = set_state,
957 static clib_error_t *
958 set_unnumbered (vlib_main_t * vm,
959 unformat_input_t * input, vlib_cli_command_t * cmd)
961 vnet_main_t *vnm = vnet_get_main ();
962 u32 unnumbered_sw_if_index = ~0;
963 u32 inherit_from_sw_if_index = ~0;
966 if (unformat (input, "%U use %U",
967 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
968 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
970 else if (unformat (input, "del %U",
971 unformat_vnet_sw_interface, vnm,
972 &unnumbered_sw_if_index))
975 return clib_error_return (0, "parse error '%U'",
976 format_unformat_error, input);
978 if (~0 == unnumbered_sw_if_index)
979 return clib_error_return (0, "Specify the unnumbered interface");
980 if (enable && ~0 == inherit_from_sw_if_index)
981 return clib_error_return (0, "When enabling unnumbered specify the"
982 " IP enabled interface that it uses");
984 int rv = vnet_sw_interface_update_unnumbered (
985 unnumbered_sw_if_index, inherit_from_sw_if_index, enable);
992 case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
993 return clib_error_return (
995 "When enabling unnumbered both interfaces must be in the same tables");
998 return clib_error_return (
999 0, "vnet_sw_interface_update_unnumbered returned %d", rv);
1005 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
1006 .path = "set interface unnumbered",
1007 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
1008 .function = set_unnumbered,
1013 static clib_error_t *
1014 set_hw_class (vlib_main_t * vm,
1015 unformat_input_t * input, vlib_cli_command_t * cmd)
1017 vnet_main_t *vnm = vnet_get_main ();
1018 vnet_interface_main_t *im = &vnm->interface_main;
1019 clib_error_t *error;
1020 u32 hw_if_index, hw_class_index;
1023 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1025 error = clib_error_return (0, "unknown hardware interface `%U'",
1026 format_unformat_error, input);
1030 if (!unformat_user (input, unformat_hash_string,
1031 im->hw_interface_class_by_name, &hw_class_index))
1033 error = clib_error_return (0, "unknown hardware class `%U'",
1034 format_unformat_error, input);
1038 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1046 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1047 .path = "set interface hw-class",
1048 .short_help = "Set interface hardware class",
1049 .function = set_hw_class,
1052 static clib_error_t *
1053 vnet_interface_cli_init (vlib_main_t * vm)
1058 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1060 static clib_error_t *
1061 renumber_interface_command_fn (vlib_main_t * vm,
1062 unformat_input_t * input,
1063 vlib_cli_command_t * cmd)
1066 u32 new_dev_instance;
1067 vnet_main_t *vnm = vnet_get_main ();
1070 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1071 return clib_error_return (0, "unknown hardware interface `%U'",
1072 format_unformat_error, input);
1074 if (!unformat (input, "%d", &new_dev_instance))
1075 return clib_error_return (0, "new dev instance missing");
1077 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1085 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
1094 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1095 .path = "renumber interface",
1096 .short_help = "renumber interface <interface> <new-dev-instance>",
1097 .function = renumber_interface_command_fn,
1100 static clib_error_t *
1101 promiscuous_cmd (vlib_main_t * vm,
1102 unformat_input_t * input, vlib_cli_command_t * cmd)
1104 vnet_main_t *vnm = vnet_get_main ();
1106 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
1107 ethernet_main_t *em = ðernet_main;
1108 ethernet_interface_t *eif;
1110 if (unformat (input, "on %U",
1111 unformat_vnet_hw_interface, vnm, &hw_if_index))
1113 else if (unformat (input, "off %U",
1114 unformat_ethernet_interface, vnm, &hw_if_index))
1117 return clib_error_return (0, "unknown input `%U'",
1118 format_unformat_error, input);
1120 eif = ethernet_get_interface (em, hw_if_index);
1122 return clib_error_return (0, "not supported");
1124 ethernet_set_flags (vnm, hw_if_index, flags);
1128 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1129 .path = "set interface promiscuous",
1130 .short_help = "set interface promiscuous [on|off] <interface>",
1131 .function = promiscuous_cmd,
1134 static clib_error_t *
1135 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1137 vnet_main_t *vnm = vnet_get_main ();
1138 u32 hw_if_index, sw_if_index, mtu;
1139 ethernet_main_t *em = ðernet_main;
1140 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
1143 if (unformat (input, "%d %U", &mtu,
1144 unformat_vnet_hw_interface, vnm, &hw_if_index))
1147 * Change physical MTU on interface. Only supported for Ethernet
1150 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1153 return clib_error_return (0, "not supported");
1155 err = vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
1160 else if (unformat (input, "packet %d %U", &mtu,
1161 unformat_vnet_sw_interface, vnm, &sw_if_index))
1162 /* Set default packet MTU (including L3 header */
1163 mtus[VNET_MTU_L3] = mtu;
1164 else if (unformat (input, "ip4 %d %U", &mtu,
1165 unformat_vnet_sw_interface, vnm, &sw_if_index))
1166 mtus[VNET_MTU_IP4] = mtu;
1167 else if (unformat (input, "ip6 %d %U", &mtu,
1168 unformat_vnet_sw_interface, vnm, &sw_if_index))
1169 mtus[VNET_MTU_IP6] = mtu;
1170 else if (unformat (input, "mpls %d %U", &mtu,
1171 unformat_vnet_sw_interface, vnm, &sw_if_index))
1172 mtus[VNET_MTU_MPLS] = mtu;
1174 return clib_error_return (0, "unknown input `%U'",
1175 format_unformat_error, input);
1177 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1183 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1184 .path = "set interface mtu",
1185 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
1186 .function = mtu_cmd,
1189 static clib_error_t *
1190 show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1191 vlib_cli_command_t * cmd)
1193 vnet_main_t *vnm = vnet_get_main ();
1194 vnet_interface_main_t *im = &vnm->interface_main;
1195 ethernet_main_t *em = ðernet_main;
1196 u32 sw_if_index = ~0;
1197 vnet_sw_interface_t *si, *sorted_sis = 0;
1199 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1201 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1202 vec_add1 (sorted_sis, si[0]);
1205 /* if an interface name was not passed, get all interfaces */
1206 if (vec_len (sorted_sis) == 0)
1209 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1210 vec_set_len (sorted_sis, 0);
1211 pool_foreach (si, im->sw_interfaces)
1213 int visible = vnet_swif_is_api_visible (si);
1215 vec_add1 (sorted_sis, si[0]);
1218 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1221 vec_foreach (si, sorted_sis)
1223 vnet_sw_interface_t *sup_si;
1224 ethernet_interface_t *ei;
1226 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1227 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1229 vlib_cli_output (vm, "%U (%s):",
1230 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1231 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1234 if (ei && ei->secondary_addrs)
1236 ethernet_interface_address_t *sec_addr;
1238 vec_foreach (sec_addr, ei->secondary_addrs)
1240 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
1245 vec_free (sorted_sis);
1250 * This command is used to display interface secondary mac addresses.
1253 * Example of how to display interface secondary mac addresses:
1254 * @cliexstart{show interface secondary-mac-address}
1257 VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1258 .path = "show interface secondary-mac-address",
1259 .short_help = "show interface secondary-mac-address [<interface>]",
1260 .function = show_interface_sec_mac_addr_fn,
1263 static clib_error_t *
1264 interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1265 vlib_cli_command_t * cmd)
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;
1274 is_add = is_del = 0;
1276 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1278 error = clib_error_return (0, "unknown interface `%U'",
1279 format_unformat_error, input);
1282 if (!unformat_user (input, unformat_ethernet_address, mac))
1284 error = clib_error_return (0, "expected mac address `%U'",
1285 format_unformat_error, input);
1289 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1291 if (unformat (input, "add"))
1293 else if (unformat (input, "del"))
1299 if (is_add == is_del)
1301 error = clib_error_return (0, "must choose one of add or del");
1305 si = vnet_get_sw_interface (vnm, sw_if_index);
1307 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
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.
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}
1329 VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1330 .path = "set interface secondary-mac-address",
1331 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1332 .function = interface_add_del_mac_address,
1335 static clib_error_t *
1336 set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1337 vlib_cli_command_t * cmd)
1339 vnet_main_t *vnm = vnet_get_main ();
1340 vnet_sw_interface_t *si = NULL;
1341 clib_error_t *error = 0;
1342 u32 sw_if_index = ~0;
1345 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1347 error = clib_error_return (0, "unknown interface `%U'",
1348 format_unformat_error, input);
1351 if (!unformat_user (input, unformat_ethernet_address, mac))
1353 error = clib_error_return (0, "expected mac address `%U'",
1354 format_unformat_error, input);
1357 si = vnet_get_sw_interface (vnm, sw_if_index);
1358 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
1364 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1365 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1366 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1370 * Example of how to change MAC Address of interface:
1371 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1372 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1373 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1374 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1377 VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1378 .path = "set interface mac address",
1379 .short_help = "set interface mac address <interface> <mac-address>",
1380 .function = set_interface_mac_address,
1383 static clib_error_t *
1384 set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1386 vnet_main_t *vnm = vnet_get_main ();
1387 u32 sw_if_index = ~0;
1390 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1391 vnm, &sw_if_index, &tag))
1392 return clib_error_return (0, "unknown input `%U'",
1393 format_unformat_error, input);
1395 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1400 VLIB_CLI_COMMAND (set_tag_command, static) = {
1401 .path = "set interface tag",
1402 .short_help = "set interface tag <interface> <tag>",
1403 .function = set_tag,
1406 static clib_error_t *
1407 clear_tag (vlib_main_t * vm, unformat_input_t * input,
1408 vlib_cli_command_t * cmd)
1410 vnet_main_t *vnm = vnet_get_main ();
1411 u32 sw_if_index = ~0;
1413 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1414 return clib_error_return (0, "unknown input `%U'",
1415 format_unformat_error, input);
1417 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1422 VLIB_CLI_COMMAND (clear_tag_command, static) = {
1423 .path = "clear interface tag",
1424 .short_help = "clear interface tag <interface>",
1425 .function = clear_tag,
1428 static clib_error_t *
1429 set_ip_directed_broadcast (vlib_main_t * vm,
1430 unformat_input_t * input, vlib_cli_command_t * cmd)
1432 vnet_main_t *vnm = vnet_get_main ();
1433 u32 sw_if_index = ~0;
1436 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1437 else if (unformat (input, "enable"))
1439 else if (unformat (input, "disable"))
1442 return clib_error_return (0, "unknown input: `%U'",
1443 format_unformat_error, input);
1445 if (~0 == sw_if_index)
1446 return clib_error_return (0, "specify an interface: `%U'",
1447 format_unformat_error, input);
1449 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1455 * This command is used to enable/disable IP directed broadcast
1456 * If directed broadcast is enabled a packet sent to the interface's
1457 * subnet broadcast address will be sent L2 broadcast on the interface,
1458 * otherwise it is dropped.
1460 VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1461 .path = "set interface ip directed-broadcast",
1462 .short_help = "set interface enable <interface> <enable|disable>",
1463 .function = set_ip_directed_broadcast,
1467 set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1468 u8 queue_id_valid, u32 queue_id,
1469 vnet_hw_if_rx_mode mode)
1471 clib_error_t *error = 0;
1472 vnet_hw_interface_t *hw;
1473 u32 *queue_indices = 0;
1474 vnet_dev_port_t *port;
1476 port = vnet_dev_get_port_from_hw_if_index (hw_if_index);
1480 vlib_main_t *vm = vlib_get_main ();
1483 vnet_dev_port_cfg_change_req_t req = {
1484 .type = mode == VNET_HW_IF_RX_MODE_POLLING ?
1485 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE :
1486 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE,
1487 .queue_id = queue_id_valid ? queue_id : 0,
1488 .all_queues = queue_id_valid ? 0 : 1,
1491 if ((rv = vnet_dev_port_cfg_change_req_validate (vm, port, &req)))
1492 return vnet_dev_port_err (
1493 vm, port, rv, "rx queue interupt mode enable/disable not supported");
1495 if ((rv = vnet_dev_process_port_cfg_change_req (vm, port, &req)))
1496 return vnet_dev_port_err (
1498 "device failed to enable/disable queue interrupt mode");
1502 hw = vnet_get_hw_interface (vnm, hw_if_index);
1508 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1509 if (queue_index == ~0)
1510 return clib_error_return (0, "unknown queue %u on interface %s",
1511 queue_id, hw->name);
1512 vec_add1 (queue_indices, queue_index);
1515 queue_indices = hw->rx_queue_indices;
1517 for (int i = 0; i < vec_len (queue_indices); i++)
1519 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1522 error = clib_error_return (
1523 0, "unable to set rx-mode on interface %v queue-id %u.\n",
1524 hw->name, queue_id);
1530 if (queue_indices != hw->rx_queue_indices)
1531 vec_free (queue_indices);
1532 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1536 static clib_error_t *
1537 set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1538 vlib_cli_command_t * cmd)
1540 clib_error_t *error = 0;
1541 unformat_input_t _line_input, *line_input = &_line_input;
1542 vnet_main_t *vnm = vnet_get_main ();
1543 u32 hw_if_index = (u32) ~ 0;
1544 u32 queue_id = (u32) ~ 0;
1545 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
1546 u8 queue_id_valid = 0;
1548 if (!unformat_user (input, unformat_line_input, line_input))
1551 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1554 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1556 else if (unformat (line_input, "queue %d", &queue_id))
1558 else if (unformat (line_input, "polling"))
1559 mode = VNET_HW_IF_RX_MODE_POLLING;
1560 else if (unformat (line_input, "interrupt"))
1561 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
1562 else if (unformat (line_input, "adaptive"))
1563 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
1566 error = clib_error_return (0, "parse error: '%U'",
1567 format_unformat_error, line_input);
1568 unformat_free (line_input);
1573 unformat_free (line_input);
1575 if (hw_if_index == (u32) ~ 0)
1576 return clib_error_return (0, "please specify valid interface name");
1578 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
1579 return clib_error_return (0, "please specify valid rx-mode");
1581 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1588 * This command is used to assign the RX packet processing mode (polling,
1589 * interrupt, adaptive) of the a given interface, and optionally a
1590 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1591 * is applied to all queues of the interface. Not all interfaces support
1592 * all modes. To display the current rx-mode use the command
1593 * '<em>show interface rx-placement</em>'.
1596 * Example of how to assign rx-mode to all queues on an interface:
1597 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1598 * Example of how to assign rx-mode to one queue of an interface:
1599 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1600 * Example of how to display the rx-mode of all interfaces:
1601 * @cliexstart{show interface rx-placement}
1602 * Thread 1 (vpp_wk_0):
1604 * GigabitEthernet7/0/0 queue 0 (polling)
1605 * node vhost-user-input:
1606 * VirtualEthernet0/0/12 queue 0 (interrupt)
1607 * VirtualEthernet0/0/12 queue 2 (polling)
1608 * VirtualEthernet0/0/13 queue 0 (polling)
1609 * VirtualEthernet0/0/13 queue 2 (polling)
1610 * Thread 2 (vpp_wk_1):
1612 * GigabitEthernet7/0/1 queue 0 (polling)
1613 * node vhost-user-input:
1614 * VirtualEthernet0/0/12 queue 1 (polling)
1615 * VirtualEthernet0/0/12 queue 3 (polling)
1616 * VirtualEthernet0/0/13 queue 1 (polling)
1617 * VirtualEthernet0/0/13 queue 3 (polling)
1620 VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1621 .path = "set interface rx-mode",
1622 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1623 .function = set_interface_rx_mode,
1626 static clib_error_t *
1627 show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1628 vlib_cli_command_t * cmd)
1631 vnet_main_t *vnm = vnet_get_main ();
1632 vnet_hw_if_rx_queue_t **all_queues = 0;
1633 vnet_hw_if_rx_queue_t **qptr;
1634 vnet_hw_if_rx_queue_t *q;
1635 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
1636 vec_add1 (all_queues, q);
1637 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1640 vec_foreach (qptr, all_queues)
1642 u32 current_thread = qptr[0]->thread_index;
1643 u32 hw_if_index = qptr[0]->hw_if_index;
1644 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1645 u32 current_node = hw_if->input_node_index;
1646 if (current_node != prev_node)
1647 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1648 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1649 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1650 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1651 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1652 current_thread != qptr[1]->thread_index)
1654 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1655 vlib_worker_threads[current_thread].name, s);
1656 vec_reset_length (s);
1658 prev_node = current_node;
1661 vec_free (all_queues);
1666 * This command is used to display the interface and queue worker
1670 * Example of how to display the interface placement:
1671 * @cliexstart{show interface rx-placement}
1672 * Thread 1 (vpp_wk_0):
1674 * GigabitEthernet7/0/0 queue 0 (polling)
1675 * node vhost-user-input:
1676 * VirtualEthernet0/0/12 queue 0 (polling)
1677 * VirtualEthernet0/0/12 queue 2 (polling)
1678 * VirtualEthernet0/0/13 queue 0 (polling)
1679 * VirtualEthernet0/0/13 queue 2 (polling)
1680 * Thread 2 (vpp_wk_1):
1682 * GigabitEthernet7/0/1 queue 0 (polling)
1683 * node vhost-user-input:
1684 * VirtualEthernet0/0/12 queue 1 (polling)
1685 * VirtualEthernet0/0/12 queue 3 (polling)
1686 * VirtualEthernet0/0/13 queue 1 (polling)
1687 * VirtualEthernet0/0/13 queue 3 (polling)
1690 VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1691 .path = "show interface rx-placement",
1692 .short_help = "show interface rx-placement",
1693 .function = show_interface_rx_placement_fn,
1696 set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1697 u32 thread_index, u8 is_main)
1699 vnet_main_t *vnm = vnet_get_main ();
1700 vnet_device_main_t *vdm = &vnet_device_main;
1701 vnet_hw_interface_t *hw;
1707 thread_index += vdm->first_worker_thread_index;
1709 if (thread_index > vdm->last_worker_thread_index)
1710 return clib_error_return (0,
1711 "please specify valid worker thread or main");
1713 hw = vnet_get_hw_interface (vnm, hw_if_index);
1716 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1717 if (queue_index == ~0)
1718 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1720 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1721 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1725 static clib_error_t *
1726 set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1727 vlib_cli_command_t *cmd)
1729 clib_error_t *error = 0;
1730 unformat_input_t _line_input, *line_input = &_line_input;
1731 vnet_main_t *vnm = vnet_get_main ();
1732 u32 hw_if_index = (u32) ~ 0;
1733 u32 queue_id = (u32) 0;
1734 u32 thread_index = (u32) ~ 0;
1737 if (!unformat_user (input, unformat_line_input, line_input))
1740 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1743 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1745 else if (unformat (line_input, "queue %d", &queue_id))
1747 else if (unformat (line_input, "main", &thread_index))
1749 else if (unformat (line_input, "worker %d", &thread_index))
1753 error = clib_error_return (0, "parse error: '%U'",
1754 format_unformat_error, line_input);
1755 unformat_free (line_input);
1760 unformat_free (line_input);
1762 if (hw_if_index == (u32) ~ 0)
1763 return clib_error_return (0, "please specify valid interface name");
1765 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1772 * This command is used to assign a given interface, and optionally a
1773 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1774 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1775 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
1778 * Example of how to display the interface placement:
1779 * @cliexstart{show interface rx-placement}
1780 * Thread 1 (vpp_wk_0):
1782 * GigabitEthernet7/0/0 queue 0 (polling)
1783 * node vhost-user-input:
1784 * VirtualEthernet0/0/12 queue 0 (polling)
1785 * VirtualEthernet0/0/12 queue 2 (polling)
1786 * VirtualEthernet0/0/13 queue 0 (polling)
1787 * VirtualEthernet0/0/13 queue 2 (polling)
1788 * Thread 2 (vpp_wk_1):
1790 * GigabitEthernet7/0/1 queue 0 (polling)
1791 * node vhost-user-input:
1792 * VirtualEthernet0/0/12 queue 1 (polling)
1793 * VirtualEthernet0/0/12 queue 3 (polling)
1794 * VirtualEthernet0/0/13 queue 1 (polling)
1795 * VirtualEthernet0/0/13 queue 3 (polling)
1797 * Example of how to assign a interface and queue to a worker thread:
1798 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1799 * Example of how to display the interface placement:
1800 * @cliexstart{show interface rx-placement}
1801 * Thread 1 (vpp_wk_0):
1803 * GigabitEthernet7/0/0 queue 0 (polling)
1804 * node vhost-user-input:
1805 * VirtualEthernet0/0/12 queue 0 (polling)
1806 * VirtualEthernet0/0/12 queue 1 (polling)
1807 * VirtualEthernet0/0/12 queue 2 (polling)
1808 * VirtualEthernet0/0/13 queue 0 (polling)
1809 * VirtualEthernet0/0/13 queue 2 (polling)
1810 * Thread 2 (vpp_wk_1):
1812 * GigabitEthernet7/0/1 queue 0 (polling)
1813 * node vhost-user-input:
1814 * VirtualEthernet0/0/12 queue 3 (polling)
1815 * VirtualEthernet0/0/13 queue 1 (polling)
1816 * VirtualEthernet0/0/13 queue 3 (polling)
1819 VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1820 .path = "set interface rx-placement",
1821 .short_help = "set interface rx-placement <interface> [queue <n>] "
1822 "[worker <n> | main]",
1823 .function = set_interface_rx_placement,
1828 set_hw_interface_tx_queue (u32 hw_if_index, u32 queue_id, uword *bitmap)
1830 vnet_main_t *vnm = vnet_get_main ();
1831 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1832 vnet_hw_if_tx_queue_t *txq;
1836 /* highest set bit in bitmap should not exceed last worker thread index */
1837 thread_index = clib_bitmap_last_set (bitmap);
1838 if ((thread_index != ~0) && (thread_index >= vtm->n_vlib_mains))
1839 return VNET_API_ERROR_INVALID_VALUE;
1842 vnet_hw_if_get_tx_queue_index_by_id (vnm, hw_if_index, queue_id);
1843 if (queue_index == ~0)
1844 return VNET_API_ERROR_INVALID_QUEUE;
1846 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
1848 // free the existing bitmap
1849 if (clib_bitmap_count_set_bits (txq->threads))
1851 txq->shared_queue = 0;
1852 clib_bitmap_free (txq->threads);
1855 clib_bitmap_foreach (thread_index, bitmap)
1856 vnet_hw_if_tx_queue_assign_thread (vnm, queue_index, thread_index);
1858 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1862 static clib_error_t *
1863 set_interface_tx_queue (vlib_main_t *vm, unformat_input_t *input,
1864 vlib_cli_command_t *cmd)
1866 clib_error_t *error = 0;
1867 unformat_input_t _line_input, *line_input = &_line_input;
1868 vnet_main_t *vnm = vnet_get_main ();
1869 u32 hw_if_index = (u32) ~0;
1870 u32 queue_id = (u32) 0;
1874 if (!unformat_user (input, unformat_line_input, line_input))
1877 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1879 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
1882 else if (unformat (line_input, "queue %d", &queue_id))
1884 else if (unformat (line_input, "threads %U", unformat_bitmap_list,
1889 error = clib_error_return (0, "parse error: '%U'",
1890 format_unformat_error, line_input);
1891 unformat_free (line_input);
1896 unformat_free (line_input);
1898 if (hw_if_index == (u32) ~0)
1900 error = clib_error_return (0, "please specify valid interface name");
1904 rv = set_hw_interface_tx_queue (hw_if_index, queue_id, bitmap);
1908 case VNET_API_ERROR_INVALID_VALUE:
1909 error = clib_error_return (
1910 0, "please specify valid thread(s) - last thread index %u",
1911 clib_bitmap_last_set (bitmap));
1913 case VNET_API_ERROR_INVALID_QUEUE:
1914 error = clib_error_return (
1915 0, "unknown queue %u on interface %s", queue_id,
1916 vnet_get_hw_interface (vnet_get_main (), hw_if_index)->name);
1923 clib_bitmap_free (bitmap);
1927 VLIB_CLI_COMMAND (cmd_set_if_tx_queue, static) = {
1928 .path = "set interface tx-queue",
1929 .short_help = "set interface tx-queue <interface> queue <n> "
1931 .function = set_interface_tx_queue,
1936 set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1937 clib_bitmap_t * bitmap)
1939 vnet_main_t *vnm = vnet_get_main ();
1940 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1942 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1945 static clib_error_t *
1946 set_interface_rss_queues_fn (vlib_main_t * vm,
1947 unformat_input_t * input,
1948 vlib_cli_command_t * cmd)
1950 clib_error_t *error = 0;
1951 unformat_input_t _line_input, *line_input = &_line_input;
1952 vnet_main_t *vnm = vnet_get_main ();
1953 u32 hw_if_index = (u32) ~ 0;
1954 clib_bitmap_t *bitmap = NULL;
1956 if (!unformat_user (input, unformat_line_input, line_input))
1959 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1962 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1965 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
1969 error = clib_error_return (0, "parse error: '%U'",
1970 format_unformat_error, line_input);
1971 unformat_free (line_input);
1976 unformat_free (line_input);
1978 if (hw_if_index == (u32) ~ 0)
1980 error = clib_error_return (0, "please specify valid interface name");
1986 error = clib_error_return (0, "please specify the valid rss queues");
1990 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
1994 clib_bitmap_free (bitmap);
2000 * This command is used to set the rss queues of a given interface
2001 * Not all the interfaces support this operation.
2002 * To display the current rss queues, use the command
2003 * '<em>show hardware-interfaces</em>'.
2006 * Example of how to set the rss queues to 0,2-5,7 of an interface:
2007 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
2010 VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
2011 .path = "set interface rss queues",
2012 .short_help = "set interface rss queues <interface> <list <queue-list>>",
2013 .function = set_interface_rss_queues_fn,
2017 format_vnet_pcap (u8 * s, va_list * args)
2019 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2020 int type = va_arg (*args, int);
2025 if (pp->pcap_rx_enable)
2027 s = format (s, "rx");
2030 if (pp->pcap_tx_enable)
2033 s = format (s, " and ");
2034 s = format (s, "tx");
2037 if (pp->pcap_drop_enable)
2040 s = format (s, " and ");
2041 s = format (s, "drop");
2046 s = format (s, "unknown type %d!", type);
2052 vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2054 vlib_main_t *vm = vlib_get_main ();
2055 vnet_main_t *vnm = vnet_get_main ();
2056 vnet_pcap_t *pp = &vnm->pcap;
2057 pcap_main_t *pm = &pp->pcap_main;
2058 vnet_classify_main_t *cm = &vnet_classify_main;
2062 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
2065 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2066 format_vnet_pcap, pp, 0 /* print type */ ,
2067 pm->n_packets_captured, pm->n_packets_to_capture);
2068 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2071 vlib_cli_output (vm, "pcap dispatch capture disabled");
2076 /* Consistency checks */
2078 /* Enable w/ capture already enabled not allowed */
2079 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2080 && (a->rx_enable + a->tx_enable + a->drop_enable))
2081 return VNET_API_ERROR_INVALID_VALUE;
2083 /* Disable capture with capture already disabled, not interesting */
2084 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
2086 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
2087 return VNET_API_ERROR_VALUE_EXIST;
2089 /* Change number of packets to capture while capturing */
2090 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2091 && (a->rx_enable + a->tx_enable + a->drop_enable)
2092 && (pm->n_packets_to_capture != a->packets_to_capture))
2093 return VNET_API_ERROR_INVALID_VALUE_2;
2095 /* Classify filter specified, but no classify filter configured */
2096 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
2097 (!cm->classify_table_index_by_sw_if_index ||
2098 cm->classify_table_index_by_sw_if_index[0] == ~0))
2099 return VNET_API_ERROR_NO_SUCH_LABEL;
2101 if (a->rx_enable + a->tx_enable + a->drop_enable)
2103 void *save_pcap_data;
2105 /* Sanity check max bytes per pkt */
2106 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2107 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2109 /* Clean up from previous run, if any */
2110 vec_reset_length (pm->pcap_data);
2112 /* Throw away the data buffer? */
2114 vec_free (pm->pcap_data);
2116 save_pcap_data = pm->pcap_data;
2118 memset (pm, 0, sizeof (*pm));
2120 pm->pcap_data = save_pcap_data;
2122 vec_validate_aligned (vnet_trace_placeholder, 2048,
2123 CLIB_CACHE_LINE_BYTES);
2125 clib_spinlock_init (&(pm->lock));
2127 if (a->filename == 0)
2132 stem = format (stem, "rx");
2134 stem = format (stem, "tx");
2136 stem = format (stem, "drop");
2137 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
2141 pm->file_name = (char *) a->filename;
2142 pm->n_packets_captured = 0;
2143 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
2144 /* Preallocate the data vector? */
2145 if (a->preallocate_data)
2148 (pm->pcap_data, a->packets_to_capture
2149 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2150 vec_reset_length (pm->pcap_data);
2152 pm->n_packets_to_capture = a->packets_to_capture;
2153 pp->pcap_sw_if_index = a->sw_if_index;
2155 pp->filter_classify_table_index =
2156 cm->classify_table_index_by_sw_if_index[0];
2158 pp->filter_classify_table_index = ~0;
2159 pp->pcap_error_index = a->drop_err;
2160 pp->pcap_rx_enable = a->rx_enable;
2161 pp->pcap_tx_enable = a->tx_enable;
2162 pp->pcap_drop_enable = a->drop_enable;
2163 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
2167 pp->pcap_rx_enable = 0;
2168 pp->pcap_tx_enable = 0;
2169 pp->pcap_drop_enable = 0;
2170 pp->filter_classify_table_index = ~0;
2171 pp->pcap_error_index = ~0;
2172 if (pm->n_packets_captured)
2174 clib_error_t *error;
2175 pm->n_packets_to_capture = pm->n_packets_captured;
2176 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2177 pm->n_packets_captured, pm->file_name);
2178 error = pcap_write (pm);
2179 if (pm->flags & PCAP_MAIN_INIT_DONE)
2181 /* Report I/O errors... */
2184 clib_error_report (error);
2185 return VNET_API_ERROR_SYSCALL_ERROR_1;
2187 vec_free (pm->file_name);
2189 vec_free (pm->pcap_data);
2193 return VNET_API_ERROR_NO_SUCH_ENTRY;
2199 static clib_error_t *
2200 pcap_trace_command_fn (vlib_main_t * vm,
2201 unformat_input_t * input, vlib_cli_command_t * cmd)
2203 unformat_input_t _line_input, *line_input = &_line_input;
2204 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
2205 vnet_main_t *vnm = vnet_get_main ();
2208 u32 max_bytes_per_pkt = 512;
2212 int preallocate_data = 0;
2213 int drop_enable = 0;
2217 u32 sw_if_index = 0; /* default: any interface */
2218 vlib_error_t drop_err = ~0; /* default: any error */
2220 /* Get a line of input. */
2221 if (!unformat_user (input, unformat_line_input, line_input))
2224 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2226 if (unformat (line_input, "rx"))
2228 else if (unformat (line_input, "tx"))
2230 else if (unformat (line_input, "drop"))
2232 else if (unformat (line_input, "off"))
2233 rx_enable = tx_enable = drop_enable = 0;
2234 else if (unformat (line_input, "max-bytes-per-pkt %u",
2235 &max_bytes_per_pkt))
2237 else if (unformat (line_input, "max %d", &max))
2239 else if (unformat (line_input, "packets-to-capture %d", &max))
2241 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2244 else if (unformat (line_input, "status %=", &status, 1))
2246 else if (unformat (line_input, "intfc %U",
2247 unformat_vnet_sw_interface, vnm, &sw_if_index))
2249 else if (unformat (line_input, "interface %U",
2250 unformat_vnet_sw_interface, vnm, &sw_if_index))
2252 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2255 else if (unformat (line_input, "preallocate-data %=",
2256 &preallocate_data, 1))
2258 else if (unformat (line_input, "free-data %=", &free_data, 1))
2260 else if (unformat (line_input, "intfc any")
2261 || unformat (line_input, "interface any"))
2263 else if (unformat (line_input, "filter"))
2267 return clib_error_return (0, "unknown input `%U'",
2268 format_unformat_error, line_input);
2272 unformat_free (line_input);
2274 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2275 a->filename = filename;
2276 a->rx_enable = rx_enable;
2277 a->tx_enable = tx_enable;
2278 a->preallocate_data = preallocate_data;
2279 a->free_data = free_data;
2280 a->drop_enable = drop_enable;
2282 a->packets_to_capture = max;
2283 a->sw_if_index = sw_if_index;
2285 a->max_bytes_per_pkt = max_bytes_per_pkt;
2286 a->drop_err = drop_err;
2288 rv = vnet_pcap_dispatch_trace_configure (a);
2295 case VNET_API_ERROR_INVALID_VALUE:
2296 return clib_error_return (0, "dispatch trace already enabled...");
2298 case VNET_API_ERROR_VALUE_EXIST:
2299 return clib_error_return (0, "dispatch trace already disabled...");
2301 case VNET_API_ERROR_INVALID_VALUE_2:
2302 return clib_error_return
2303 (0, "can't change number of records to capture while tracing...");
2305 case VNET_API_ERROR_SYSCALL_ERROR_1:
2306 return clib_error_return (0, "I/O writing trace capture...");
2308 case VNET_API_ERROR_NO_SUCH_ENTRY:
2309 return clib_error_return (0, "No packets captured...");
2311 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2312 return clib_error_return (0,
2313 "Max bytes per pkt must be > 32, < 9000...");
2315 case VNET_API_ERROR_NO_SUCH_LABEL:
2316 return clib_error_return
2317 (0, "No classify filter configured, see 'classify filter...'");
2320 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2327 * This command is used to start or stop a packet capture, or show
2328 * the status of packet capture.
2330 * This command has the following optional parameters:
2333 * - <b>rx</b> - Capture received packets
2335 * - <b>tx</b> - Capture transmitted packets
2337 * - <b>drop</b> - Capture dropped packets
2339 * - <b>off</b> - Stop capturing packets, write results to the specified file
2341 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2342 * of packets have been received, buffer is flushed to file. Once another
2343 * '<em>nn</em>' number of packets have been received, buffer is flushed
2344 * to file, overwriting previous write. If not entered, value defaults
2345 * to 100. Can only be updated if packet capture is off.
2347 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2348 * for each packet. Must be >= 32, <= 9000.
2350 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2351 * vector expansion delays during pcap capture
2353 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2354 * to retain the data buffer so this option is seldom used.
2356 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
2357 * or use '<em>any</em>' to run packet capture on all interfaces.
2358 * '<em>any</em>' is the default if not provided. Settings from a previous
2359 * packet capture are preserved, so '<em>any</em>' can be used to reset
2360 * the interface setting.
2362 * - <b>filter</b> - Use the pcap trace rx / tx / drop filter, which
2363 * must be configured. Use <b>classify filter pcap...</b> to configure the
2364 * filter. The filter will only be executed if the per-interface or
2365 * any-interface tests fail.
2367 * - <b>error <node>.<error></b> - filter packets based on a specific error.
2368 * For example: error {ip4-udp-lookup}.{no_listener}
2370 * - <b>file <name></b> - Used to specify the output filename. The file will
2371 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2372 * supported. Directory should not be entered. If file already exists, file
2373 * will be overwritten. If no filename is provided, the file will be
2374 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2375 * Can only be updated if packet capture is off.
2377 * - <b>status</b> - Displays the current status and configured attributes
2378 * associated with a packet capture. If packet capture is in progress,
2379 * '<em>status</em>' also will return the number of packets currently in
2380 * the local buffer. All additional attributes entered on command line
2381 * with '<em>status</em>' will be ignored and not applied.
2384 * Example of how to display the status of a tx packet capture when off:
2385 * @cliexstart{pcap trace status}
2386 * max is 100, for any interface to file /tmp/vpe.pcap
2387 * pcap tx capture is off...
2389 * Example of how to start a tx packet capture:
2390 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
2393 * Example of how to display the status of a tx packet capture in progress:
2394 * @cliexstart{pcap trace status}
2395 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2396 * pcap tx capture is on: 20 of 35 pkts...
2398 * Example of how to stop a tx packet capture:
2399 * @cliexstart{pcap trace off}
2400 * captured 21 pkts...
2401 * saved to /tmp/vppTest.pcap...
2405 VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
2406 .path = "pcap trace",
2408 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2409 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2410 " [preallocate-data][free-data]",
2411 .function = pcap_trace_command_fn,
2414 static clib_error_t *
2415 set_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2416 vlib_cli_command_t *cmd)
2418 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2419 unformat_input_t _line_input, *line_input = &_line_input;
2420 vlib_is_packet_traced_fn_t *res = 0;
2421 clib_error_t *error = 0;
2423 if (!unformat_user (input, unformat_line_input, line_input))
2426 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
2428 if (unformat (line_input, "%U", unformat_vlib_trace_filter_function,
2433 error = clib_error_create (
2434 "expected valid trace filter function, got `%U'",
2435 format_unformat_error, line_input);
2439 pp->current_filter_function = res;
2442 unformat_free (line_input);
2447 VLIB_CLI_COMMAND (set_pcap_filter_function_cli, static) = {
2448 .path = "set pcap filter function",
2449 .short_help = "set pcap filter function <func_name>",
2450 .function = set_pcap_filter_function,
2453 static clib_error_t *
2454 show_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2455 vlib_cli_command_t *cmd)
2457 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2458 vlib_trace_filter_main_t *tfm = &vlib_trace_filter_main;
2459 vlib_is_packet_traced_fn_t *current_trace_filter_fn =
2460 pp->current_filter_function;
2461 vlib_trace_filter_function_registration_t *reg =
2462 tfm->trace_filter_registration;
2466 vlib_cli_output (vm, "%sname:%s description: %s priority: %u",
2467 reg->function == current_trace_filter_fn ? "(*) " : "",
2468 reg->name, reg->description, reg->priority);
2474 VLIB_CLI_COMMAND (show_pcap_filter_function_cli, static) = {
2475 .path = "show pcap filter function",
2476 .short_help = "show pcap filter function",
2477 .function = show_pcap_filter_function,
2480 static clib_error_t *
2481 set_interface_name (vlib_main_t *vm, unformat_input_t *input,
2482 vlib_cli_command_t *cmd)
2484 clib_error_t *error = 0;
2485 unformat_input_t _line_input, *line_input = &_line_input;
2486 vnet_main_t *vnm = vnet_get_main ();
2487 u32 hw_if_index = ~0;
2490 if (!unformat_user (input, unformat_line_input, line_input))
2493 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2495 if (unformat (line_input, "%U %s", unformat_vnet_hw_interface, vnm,
2496 &hw_if_index, &name))
2500 error = clib_error_return (0, "parse error: '%U'",
2501 format_unformat_error, line_input);
2502 unformat_free (line_input);
2508 unformat_free (line_input);
2510 if (hw_if_index == (u32) ~0 || name == 0)
2513 error = clib_error_return (0, "please specify valid interface name");
2517 error = vnet_rename_interface (vnm, hw_if_index, name);
2523 VLIB_CLI_COMMAND (cmd_set_if_name, static) = {
2524 .path = "set interface name",
2525 .short_help = "set interface name <interface-name> <new-interface-name>",
2526 .function = set_interface_name,
2530 static clib_error_t *
2531 set_interface_tx_hash_cmd (vlib_main_t *vm, unformat_input_t *input,
2532 vlib_cli_command_t *cmd)
2534 clib_error_t *error = 0;
2535 unformat_input_t _line_input, *line_input = &_line_input;
2536 vnet_main_t *vnm = vnet_get_main ();
2537 vnet_hw_interface_t *hi;
2539 u32 hw_if_index = (u32) ~0;
2541 vnet_hash_fn_type_t ftype;
2543 if (!unformat_user (input, unformat_line_input, line_input))
2546 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2548 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2551 else if (unformat (line_input, "hash-name %s", &hash_name))
2555 error = clib_error_return (0, "parse error: '%U'",
2556 format_unformat_error, line_input);
2557 unformat_free (line_input);
2562 unformat_free (line_input);
2564 if (hw_if_index == (u32) ~0)
2566 error = clib_error_return (0, "please specify valid interface name");
2572 error = clib_error_return (0, "hash-name is required");
2576 hi = vnet_get_hw_interface (vnm, hw_if_index);
2578 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2579 hf = vnet_hash_function_from_name ((const char *) hash_name, ftype);
2583 error = clib_error_return (0, "please specify valid hash name");
2589 vec_free (hash_name);
2593 VLIB_CLI_COMMAND (cmd_set_if_tx_hash, static) = {
2594 .path = "set interface tx-hash",
2595 .short_help = "set interface tx-hash <interface> hash-name <hash-name>",
2596 .function = set_interface_tx_hash_cmd,
2599 static clib_error_t *
2600 show_tx_hash (vlib_main_t *vm, unformat_input_t *input,
2601 vlib_cli_command_t *cmd)
2603 clib_error_t *error = 0;
2604 unformat_input_t _line_input, *line_input = &_line_input;
2605 vnet_main_t *vnm = vnet_get_main ();
2606 vnet_hw_interface_t *hi;
2607 vnet_hash_function_registration_t *hash;
2608 u32 hw_if_index = (u32) ~0;
2609 vnet_hash_fn_type_t ftype;
2611 if (!unformat_user (input, unformat_line_input, line_input))
2614 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2616 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2621 error = clib_error_return (0, "parse error: '%U'",
2622 format_unformat_error, line_input);
2623 unformat_free (line_input);
2628 unformat_free (line_input);
2630 if (hw_if_index == (u32) ~0)
2632 error = clib_error_return (0, "please specify valid interface name");
2636 hi = vnet_get_hw_interface (vnm, hw_if_index);
2638 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2642 hash = vnet_hash_function_from_func (hi->hf, ftype);
2644 vlib_cli_output (vm, "%U", format_vnet_hash, hash);
2646 vlib_cli_output (vm, "no matching hash function found");
2649 vlib_cli_output (vm, "no hashing function set");
2655 VLIB_CLI_COMMAND (cmd_show_tx_hash, static) = {
2656 .path = "show interface tx-hash",
2657 .short_help = "show interface tx-hash [interface]",
2658 .function = show_tx_hash,
2662 * fd.io coding-style-patch-verification: ON
2665 * eval: (c-set-style "gnu")