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,
153 clib_bitmap_foreach (hw_idx, hi->bond_info)
155 shi = vnet_get_hw_interface(vnm, hw_idx);
156 vlib_cli_output (vm, "%U\n",
157 format_vnet_hw_interface, vnm, shi, verbose);
165 for (i = 0; i < vec_len (hw_if_indices); i++)
167 vnet_device_class_t *dc;
169 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
170 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
172 if (dc->clear_counters)
173 dc->clear_counters (hi->dev_instance);
178 vec_free (hw_if_indices);
182 static clib_error_t *
183 show_hw_interfaces (vlib_main_t * vm,
184 unformat_input_t * input, vlib_cli_command_t * cmd)
186 return show_or_clear_hw_interfaces (vm, input, cmd, 1 /* is_show */ );
189 static clib_error_t *
190 clear_hw_interfaces (vlib_main_t * vm,
191 unformat_input_t * input, vlib_cli_command_t * cmd)
193 return show_or_clear_hw_interfaces (vm, input, cmd, 0 /* is_show */ );
198 * Display more detailed information about all or a list of given interfaces.
199 * The verboseness of the output can be controlled by the following optional
201 * - brief: Only show name, index and state (default for bonded interfaces).
202 * - verbose: Also display additional attributes (default for all other interfaces).
203 * - detail: Also display all remaining attributes and extended statistics.
205 * To limit the output of the command to bonded interfaces and their slave
206 * interfaces, use the '<em>bond</em>' optional parameter.
209 * Example of how to display default data for all interfaces:
210 * @cliexstart{show hardware-interfaces}
211 * Name Idx Link Hardware
212 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
213 * Ethernet address ec:f4:bb:c0:bc:fc
215 * carrier up full duplex speed 1000 mtu 9216
216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
218 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1
219 * Ethernet address ec:f4:bb:c0:bc:fd
221 * carrier up full duplex speed 1000 mtu 9216
222 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
224 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0
225 * Ethernet address 02:fe:a5:a9:8b:8e
226 * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1
227 * Ethernet address 02:fe:c0:4e:3b:b0
228 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2
229 * Ethernet address 02:fe:1f:73:92:81
230 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3
231 * Ethernet address 02:fe:f2:25:c4:68
232 * local0 0 down local0
235 * Example of how to display '<em>verbose</em>' data for an interface by name and
236 * software index (where 2 is the software index):
237 * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose}
238 * Name Idx Link Hardware
239 * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0
240 * Ethernet address ec:f4:bb:c0:bc:fc
242 * carrier up full duplex speed 1000 mtu 9216
243 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
245 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1
246 * Ethernet address ec:f4:bb:c0:bc:fd
248 * carrier up full duplex speed 1000 mtu 9216
249 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024
254 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
255 .path = "show hardware-interfaces",
256 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] "
257 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
258 .function = show_hw_interfaces,
264 * Clear the extended statistics for all or a list of given interfaces
265 * (statistics associated with the '<em>show hardware-interfaces</em>' command).
268 * Example of how to clear the extended statistics for all interfaces:
269 * @cliexcmd{clear hardware-interfaces}
270 * Example of how to clear the extended statistics for an interface by
271 * name and software index (where 2 is the software index):
272 * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2}
275 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
276 .path = "clear hardware-interfaces",
277 .short_help = "clear hardware-interfaces "
278 "[<interface> [<interface> [..]]] [<sw_idx> [<sw_idx> [..]]]",
279 .function = clear_hw_interfaces,
284 sw_interface_name_compare (void *a1, void *a2)
286 vnet_sw_interface_t *si1 = a1;
287 vnet_sw_interface_t *si2 = a2;
289 return vnet_sw_interface_compare (vnet_get_main (),
290 si1->sw_if_index, si2->sw_if_index);
293 static clib_error_t *
294 show_sw_interfaces (vlib_main_t * vm,
295 unformat_input_t * input, vlib_cli_command_t * cmd)
297 clib_error_t *error = 0;
298 vnet_main_t *vnm = vnet_get_main ();
299 unformat_input_t _linput, *linput = &_linput;
300 vnet_interface_main_t *im = &vnm->interface_main;
301 vnet_sw_interface_t *si, *sorted_sis = 0;
302 u32 sw_if_index = ~(u32) 0;
303 u8 show_addresses = 0;
304 u8 show_features = 0;
310 * Get a line of input. Won't work if the user typed
311 * "show interface" and nothing more.
313 if (unformat_user (input, unformat_line_input, linput))
315 while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT)
317 /* See if user wants to show specific interface */
319 (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
321 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
322 vec_add1 (sorted_sis, si[0]);
324 else if (unformat (linput, "address") || unformat (linput, "addr"))
326 else if (unformat (linput, "features") || unformat (linput, "feat"))
328 else if (unformat (linput, "tag"))
330 else if (unformat (linput, "vtr"))
332 else if (unformat (linput, "verbose"))
334 else if (unformat (linput, "%d", &sw_if_index))
336 if (!pool_is_free_index (im->sw_interfaces, sw_if_index))
338 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
339 vec_add1 (sorted_sis, si[0]);
343 vec_free (sorted_sis);
344 error = clib_error_return (0, "unknown interface index `%d'",
351 vec_free (sorted_sis);
352 error = clib_error_return (0, "unknown input `%U'",
353 format_unformat_error, linput);
357 unformat_free (linput);
359 if (show_features || show_tag || show_vtr)
361 if (sw_if_index == ~(u32) 0)
363 vec_free (sorted_sis);
364 return clib_error_return (0, "Interface not specified...");
370 vnet_interface_features_show (vm, sw_if_index, verbose);
371 vlib_cli_output (vm, "%U", format_l2_input_features, sw_if_index, 1);
373 l2_output_config_t *l2_output = l2output_intf_config (sw_if_index);
374 vlib_cli_output (vm, "\nl2-output:");
375 if (l2_output->out_vtr_flag)
376 vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--");
377 vlib_cli_output (vm, "%U", format_l2_output_features,
378 l2_output->feature_bitmap, 1);
379 vec_free (sorted_sis);
385 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
386 vlib_cli_output (vm, "%U: %s",
387 format_vnet_sw_if_index_name, vnm, sw_if_index,
388 tag ? (char *) tag : "(none)");
389 vec_free (sorted_sis);
394 * Show vlan tag rewrite data for one interface.
398 u32 vtr_op = L2_VTR_DISABLED;
399 u32 push_dot1q = 0, tag1 = 0, tag2 = 0;
401 if (l2vtr_get (vm, vnm, sw_if_index,
402 &vtr_op, &push_dot1q, &tag1, &tag2) != 0)
404 vlib_cli_output (vm, "%U: Problem getting vlan tag-rewrite data",
405 format_vnet_sw_if_index_name, vnm, sw_if_index);
408 vlib_cli_output (vm, "%U: VTR %0U",
409 format_vnet_sw_if_index_name, vnm, sw_if_index,
410 format_vtr, vtr_op, push_dot1q, tag1, tag2);
415 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
417 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
419 /* Gather interfaces. */
421 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
422 vec_set_len (sorted_sis, 0);
424 pool_foreach (si, im->sw_interfaces)
426 int visible = vnet_swif_is_api_visible (si);
428 vec_add1 (sorted_sis, si[0]);
432 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
437 vec_foreach (si, sorted_sis)
439 ip4_main_t *im4 = &ip4_main;
440 ip6_main_t *im6 = &ip6_main;
441 ip_lookup_main_t *lm4 = &im4->lookup_main;
442 ip_lookup_main_t *lm6 = &im6->lookup_main;
443 ip_interface_address_t *ia = 0;
444 u32 fib_index4 = 0, fib_index6 = 0;
446 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
447 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
450 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
451 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
454 ip4_fib_t *fib4 = ip4_fib_get (fib_index4);
455 ip6_fib_t *fib6 = ip6_fib_get (fib_index6);
457 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
459 (vm, "%U (%s): \n unnumbered, use %U",
460 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
461 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
462 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
466 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
467 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn");
469 /* Display any L2 info */
470 vlib_cli_output (vm, "%U", format_l2_input, si->sw_if_index);
473 /* Display any IP4 addressing info */
474 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
475 1 /* honor unnumbered */,
477 ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia);
478 if (fib4->hash.table_id)
480 vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address,
481 r4, ia->address_length, fib4->hash.table_id,
482 ip4_fib_index_from_table_id (fib4->hash.table_id));
484 vlib_cli_output (vm, " L3 %U/%d",
485 format_ip4_address, r4, ia->address_length);
490 /* Display any IP6 addressing info */
491 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
492 1 /* honor unnumbered */,
494 ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia);
496 vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d",
497 format_ip6_address, r6, ia->address_length,
499 ip6_fib_index_from_table_id (fib6->table_id));
501 vlib_cli_output (vm, " L3 %U/%d",
502 format_ip6_address, r6, ia->address_length);
509 vec_foreach (si, sorted_sis)
511 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
516 vec_free (sorted_sis);
521 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
522 .path = "show interface",
523 .short_help = "show interface [address|addr|features|feat|vtr|tag] "
524 "[<interface> [<interface> [..]]] [verbose]",
525 .function = show_sw_interfaces,
530 /* Root of all interface commands. */
532 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
534 .short_help = "Interface commands",
539 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
540 .path = "set interface",
541 .short_help = "Interface commands",
545 static clib_error_t *
546 clear_interface_counters (vlib_main_t * vm,
547 unformat_input_t * input, vlib_cli_command_t * cmd)
549 vnet_main_t *vnm = vnet_get_main ();
550 vnet_interface_main_t *im = &vnm->interface_main;
551 vlib_simple_counter_main_t *sm;
552 vlib_combined_counter_main_t *cm;
555 n_counters = vec_len (im->combined_sw_if_counters);
557 for (j = 0; j < n_counters; j++)
559 im = &vnm->interface_main;
560 cm = im->combined_sw_if_counters + j;
561 vlib_clear_combined_counters (cm);
564 n_counters = vec_len (im->sw_if_counters);
566 for (j = 0; j < n_counters; j++)
568 im = &vnm->interface_main;
569 sm = im->sw_if_counters + j;
570 vlib_clear_simple_counters (sm);
577 * Clear the statistics for all interfaces (statistics associated with the
578 * '<em>show interface</em>' command).
581 * Example of how to clear the statistics for all interfaces:
582 * @cliexcmd{clear interfaces}
585 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
586 .path = "clear interfaces",
587 .short_help = "clear interfaces",
588 .function = clear_interface_counters,
593 * Parse subinterface names.
595 * The following subinterface syntax is supported. The first two are for
596 * backwards compatability:
599 * - a subinterface with the name <intf-name>.<id>. The subinterface
600 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
602 * <intf-name> <min_id>-<max_id>
603 * - a set of the above subinterfaces, repeating for each id
604 * in the range <min_id> to <max_id>
606 * In the following, exact-match semantics (i.e. the number of vlan tags on the
607 * packet must match the number of tags in the configuration) are used only if
608 * the keyword exact-match is present. Non-exact match is the default.
610 * <intf-name> <id> dot1q <outer_id> [exact-match]
611 * - a subinterface with the name <intf-name>.<id>. The subinterface
612 * is a single dot1q vlan with vlan id <outer_id>.
614 * <intf-name> <id> dot1q any [exact-match]
615 * - a subinterface with the name <intf-name>.<id>. The subinterface
616 * is a single dot1q vlan with any vlan id.
618 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
619 * - a subinterface with the name <intf-name>.<id>. The subinterface
620 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
623 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
624 * - a subinterface with the name <intf-name>.<id>. The subinterface
625 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
627 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
629 * - a subinterface with the name <intf-name>.<id>. The subinterface
630 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
632 * For each of the above CLI, there is a duplicate that uses the keyword
633 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
634 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
635 * tagged packets the inner ethertype is always 0x8100. Also note that
636 * the dot1q and dot1ad naming spaces are independent, so it is legal to
637 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
639 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
640 * - a subinterface with the name <intf-name>.<id>. The subinterface
641 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
644 * <intf-name> <id> untagged
645 * - a subinterface with the name <intf-name>.<id>. The subinterface
646 * has no vlan tags. Only one can be specified per interface.
648 * <intf-name> <id> default
649 * - a subinterface with the name <intf-name>.<id>. This is associated
650 * with a packet that did not match any other configured subinterface
651 * on this interface. Only one can be specified per interface.
654 static clib_error_t *
655 parse_vlan_sub_interfaces (unformat_input_t * input,
656 vnet_sw_interface_t * template)
658 clib_error_t *error = 0;
659 u32 inner_vlan, outer_vlan;
661 if (unformat (input, "any inner-dot1q any"))
663 template->sub.eth.flags.two_tags = 1;
664 template->sub.eth.flags.outer_vlan_id_any = 1;
665 template->sub.eth.flags.inner_vlan_id_any = 1;
667 else if (unformat (input, "any"))
669 template->sub.eth.flags.one_tag = 1;
670 template->sub.eth.flags.outer_vlan_id_any = 1;
672 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
674 template->sub.eth.flags.two_tags = 1;
675 template->sub.eth.flags.inner_vlan_id_any = 1;
676 template->sub.eth.outer_vlan_id = outer_vlan;
678 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
680 template->sub.eth.flags.two_tags = 1;
681 template->sub.eth.outer_vlan_id = outer_vlan;
682 template->sub.eth.inner_vlan_id = inner_vlan;
684 else if (unformat (input, "%d", &outer_vlan))
686 template->sub.eth.flags.one_tag = 1;
687 template->sub.eth.outer_vlan_id = outer_vlan;
691 error = clib_error_return (0, "expected dot1q config, got `%U'",
692 format_unformat_error, input);
696 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
698 if (unformat (input, "exact-match"))
700 template->sub.eth.flags.exact_match = 1;
708 static clib_error_t *
709 create_sub_interfaces (vlib_main_t * vm,
710 unformat_input_t * input, vlib_cli_command_t * cmd)
712 vnet_main_t *vnm = vnet_get_main ();
713 clib_error_t *error = 0;
714 u32 hw_if_index, sw_if_index;
715 vnet_hw_interface_t *hi;
716 u32 id, id_min, id_max;
717 vnet_sw_interface_t template;
720 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
722 error = clib_error_return (0, "unknown interface `%U'",
723 format_unformat_error, input);
727 clib_memset (&template, 0, sizeof (template));
728 template.sub.eth.raw_flags = 0;
730 if (unformat (input, "%d default", &id_min))
733 template.sub.eth.flags.default_sub = 1;
735 else if (unformat (input, "%d untagged", &id_min))
738 template.sub.eth.flags.no_tags = 1;
739 template.sub.eth.flags.exact_match = 1;
741 else if (unformat (input, "%d dot1q", &id_min))
743 /* parse dot1q config */
745 error = parse_vlan_sub_interfaces (input, &template);
749 else if (unformat (input, "%d dot1ad", &id_min))
751 /* parse dot1ad config */
753 template.sub.eth.flags.dot1ad = 1;
754 error = parse_vlan_sub_interfaces (input, &template);
758 else if (unformat (input, "%d-%d", &id_min, &id_max))
760 template.sub.eth.flags.one_tag = 1;
761 template.sub.eth.flags.exact_match = 1;
765 else if (unformat (input, "%d", &id_min))
768 template.sub.eth.flags.one_tag = 1;
769 template.sub.eth.outer_vlan_id = id_min;
770 template.sub.eth.flags.exact_match = 1;
775 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
776 format_unformat_error, input);
780 hi = vnet_get_hw_interface (vnm, hw_if_index);
782 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
785 clib_error_return (0,
786 "not allowed as %v belong to a BondEthernet interface",
791 for (id = id_min; id <= id_max; id++)
794 vnet_interface_main_t *im = &vnm->interface_main;
795 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
798 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
802 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
803 hi->sw_if_index, id);
807 template.type = VNET_SW_INTERFACE_TYPE_SUB;
808 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
809 template.sup_sw_if_index = hi->sw_if_index;
810 template.sub.id = id;
812 template.sub.eth.outer_vlan_id = id;
814 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
818 kp = clib_mem_alloc (sizeof (*kp));
819 *kp = sup_and_sub_key;
821 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
822 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
823 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
824 vnet_get_main (), sw_if_index);
832 * This command is used to add VLAN IDs to interfaces, also known as subinterfaces.
833 * The primary input to this command is the '<em>interface</em>' and '<em>subId</em>'
834 * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is
835 * assumed to be the '<em>subId</em>'. The VLAN ID and '<em>subId</em>' can be different,
836 * but this is not recommended.
838 * This command has several variations:
839 * - <b>create sub-interfaces <interface> <subId></b> - Create a subinterface to
840 * process packets with a given 802.1q VLAN ID (same value as the '<em>subId</em>').
842 * - <b>create sub-interfaces <interface> <subId> default</b> - Adding the
843 * '<em>default</em>' parameter indicates that packets with VLAN IDs that do not
844 * match any other subinterfaces should be sent to this subinterface.
846 * - <b>create sub-interfaces <interface> <subId> untagged</b> - Adding the
847 * '<em>untagged</em>' parameter indicates that packets no VLAN IDs should be sent
848 * to this subinterface.
850 * - <b>create sub-interfaces <interface> <subId>-<subId></b> - Create a range of
851 * subinterfaces to handle a range of VLAN IDs.
853 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any [exact-match]</b> -
854 * Use this command to specify the outer VLAN ID, to either be explicit or to make the
855 * VLAN ID different from the '<em>subId</em>'.
857 * - <b>create sub-interfaces <interface> <subId> dot1q|dot1ad <vlanId>|any inner-dot1q
858 * <vlanId>|any [exact-match]</b> - Use this command to specify the outer VLAN ID and
861 * When '<em>dot1q</em>' or '<em>dot1ad</em>' is explicitly entered, subinterfaces
862 * can be configured as either exact-match or non-exact match. Non-exact match is the CLI
863 * default. If '<em>exact-match</em>' is specified, packets must have the same number of
864 * VLAN tags as the configuration. For non-exact-match, packets must at least that number
865 * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are
866 * typically configured as non-exact-match. If '<em>dot1q</em>' or '<em>dot1ad</em>' is NOT
867 * entered, then the default behavior is exact-match.
869 * Use the '<em>show interface</em>' command to display all subinterfaces.
873 * Example of how to create a VLAN subinterface 11 to process packets on 802.1q VLAN ID 11:
874 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11}
876 * The previous example is shorthand and is equivalent to:
877 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 11 exact-match}
880 * Example of how to create a subinterface number that is different from the VLAN ID:
881 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100}
884 * Examples of how to create q-in-q and q-in-any subinterfaces:
885 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200}
886 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any}
888 * Examples of how to create dot1ad interfaces:
889 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 11 dot1ad 11}
890 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 12 dot1ad 100 inner-dot1q 200}
893 * Examples of '<em>exact-match</em>' versus non-exact match. A packet with
894 * outer VLAN 100 and inner VLAN 200 would match this interface, because the default
895 * is non-exact match:
896 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100}
898 * However, the same packet would NOT match this interface because '<em>exact-match</em>'
899 * is specified and only one VLAN is configured, but packet contains two VLANs:
900 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 dot1q 100 exact-match}
903 * Example of how to created a subinterface to process untagged packets:
904 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 5 untagged}
906 * Example of how to created a subinterface to process any packet with a VLAN ID that
907 * does not match any other subinterface:
908 * @cliexcmd{create sub-interfaces GigabitEthernet2/0/0 7 default}
910 * When subinterfaces are created, they are in the down state. Example of how to
911 * enable a newly created subinterface:
912 * @cliexcmd{set interface GigabitEthernet2/0/0.7 up}
916 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
917 .path = "create sub-interfaces",
918 .short_help = "create sub-interfaces <interface> "
919 "{<subId> [default|untagged]} | "
920 "{<subId>-<subId>} | "
921 "{<subId> dot1q|dot1ad <vlanId>|any [inner-dot1q <vlanId>|any] [exact-match]}",
922 .function = create_sub_interfaces,
926 static clib_error_t *
927 set_state (vlib_main_t * vm,
928 unformat_input_t * input, vlib_cli_command_t * cmd)
930 vnet_main_t *vnm = vnet_get_main ();
932 u32 sw_if_index, flags;
935 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
937 error = clib_error_return (0, "unknown interface `%U'",
938 format_unformat_error, input);
942 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
944 error = clib_error_return (0, "unknown flags `%U'",
945 format_unformat_error, input);
949 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
958 * This command is used to change the admin state (up/down) of an interface.
960 * If an interface is down, the optional '<em>punt</em>' flag can also be set.
961 * The '<em>punt</em>' flag implies the interface is disabled for forwarding
962 * but punt all traffic to slow-path. Use the '<em>enable</em>' flag to clear
963 * '<em>punt</em>' flag (interface is still down).
966 * Example of how to configure the admin state of an interface to
968 * @cliexcmd{set interface state GigabitEthernet2/0/0 up}
969 * Example of how to configure the admin state of an interface to
971 * @cliexcmd{set interface state GigabitEthernet2/0/0 down}
974 VLIB_CLI_COMMAND (set_state_command, static) = {
975 .path = "set interface state",
976 .short_help = "set interface state <interface> [up|down|punt|enable]",
977 .function = set_state,
981 static clib_error_t *
982 set_unnumbered (vlib_main_t * vm,
983 unformat_input_t * input, vlib_cli_command_t * cmd)
985 vnet_main_t *vnm = vnet_get_main ();
986 u32 unnumbered_sw_if_index = ~0;
987 u32 inherit_from_sw_if_index = ~0;
990 if (unformat (input, "%U use %U",
991 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
992 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
994 else if (unformat (input, "del %U",
995 unformat_vnet_sw_interface, vnm,
996 &unnumbered_sw_if_index))
999 return clib_error_return (0, "parse error '%U'",
1000 format_unformat_error, input);
1002 if (~0 == unnumbered_sw_if_index)
1003 return clib_error_return (0, "Specify the unnumbered interface");
1004 if (enable && ~0 == inherit_from_sw_if_index)
1005 return clib_error_return (0, "When enabling unnumbered specify the"
1006 " IP enabled interface that it uses");
1008 int rv = vnet_sw_interface_update_unnumbered (
1009 unnumbered_sw_if_index, inherit_from_sw_if_index, enable);
1016 case VNET_API_ERROR_UNEXPECTED_INTF_STATE:
1017 return clib_error_return (
1019 "When enabling unnumbered both interfaces must be in the same tables");
1022 return clib_error_return (
1023 0, "vnet_sw_interface_update_unnumbered returned %d", rv);
1030 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
1031 .path = "set interface unnumbered",
1032 .short_help = "set interface unnumbered [<interface> use <interface> | del <interface>]",
1033 .function = set_unnumbered,
1039 static clib_error_t *
1040 set_hw_class (vlib_main_t * vm,
1041 unformat_input_t * input, vlib_cli_command_t * cmd)
1043 vnet_main_t *vnm = vnet_get_main ();
1044 vnet_interface_main_t *im = &vnm->interface_main;
1045 clib_error_t *error;
1046 u32 hw_if_index, hw_class_index;
1049 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1051 error = clib_error_return (0, "unknown hardware interface `%U'",
1052 format_unformat_error, input);
1056 if (!unformat_user (input, unformat_hash_string,
1057 im->hw_interface_class_by_name, &hw_class_index))
1059 error = clib_error_return (0, "unknown hardware class `%U'",
1060 format_unformat_error, input);
1064 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
1073 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
1074 .path = "set interface hw-class",
1075 .short_help = "Set interface hardware class",
1076 .function = set_hw_class,
1080 static clib_error_t *
1081 vnet_interface_cli_init (vlib_main_t * vm)
1086 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
1088 static clib_error_t *
1089 renumber_interface_command_fn (vlib_main_t * vm,
1090 unformat_input_t * input,
1091 vlib_cli_command_t * cmd)
1094 u32 new_dev_instance;
1095 vnet_main_t *vnm = vnet_get_main ();
1098 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
1099 return clib_error_return (0, "unknown hardware interface `%U'",
1100 format_unformat_error, input);
1102 if (!unformat (input, "%d", &new_dev_instance))
1103 return clib_error_return (0, "new dev instance missing");
1105 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
1113 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
1123 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
1124 .path = "renumber interface",
1125 .short_help = "renumber interface <interface> <new-dev-instance>",
1126 .function = renumber_interface_command_fn,
1130 static clib_error_t *
1131 promiscuous_cmd (vlib_main_t * vm,
1132 unformat_input_t * input, vlib_cli_command_t * cmd)
1134 vnet_main_t *vnm = vnet_get_main ();
1136 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
1137 ethernet_main_t *em = ðernet_main;
1138 ethernet_interface_t *eif;
1140 if (unformat (input, "on %U",
1141 unformat_vnet_hw_interface, vnm, &hw_if_index))
1143 else if (unformat (input, "off %U",
1144 unformat_ethernet_interface, vnm, &hw_if_index))
1147 return clib_error_return (0, "unknown input `%U'",
1148 format_unformat_error, input);
1150 eif = ethernet_get_interface (em, hw_if_index);
1152 return clib_error_return (0, "not supported");
1154 ethernet_set_flags (vnm, hw_if_index, flags);
1159 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1160 .path = "set interface promiscuous",
1161 .short_help = "set interface promiscuous [on|off] <interface>",
1162 .function = promiscuous_cmd,
1166 static clib_error_t *
1167 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1169 vnet_main_t *vnm = vnet_get_main ();
1170 u32 hw_if_index, sw_if_index, mtu;
1171 ethernet_main_t *em = ðernet_main;
1172 u32 mtus[VNET_N_MTU] = { 0, 0, 0, 0 };
1175 if (unformat (input, "%d %U", &mtu,
1176 unformat_vnet_hw_interface, vnm, &hw_if_index))
1179 * Change physical MTU on interface. Only supported for Ethernet
1182 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1185 return clib_error_return (0, "not supported");
1187 err = vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
1192 else if (unformat (input, "packet %d %U", &mtu,
1193 unformat_vnet_sw_interface, vnm, &sw_if_index))
1194 /* Set default packet MTU (including L3 header */
1195 mtus[VNET_MTU_L3] = mtu;
1196 else if (unformat (input, "ip4 %d %U", &mtu,
1197 unformat_vnet_sw_interface, vnm, &sw_if_index))
1198 mtus[VNET_MTU_IP4] = mtu;
1199 else if (unformat (input, "ip6 %d %U", &mtu,
1200 unformat_vnet_sw_interface, vnm, &sw_if_index))
1201 mtus[VNET_MTU_IP6] = mtu;
1202 else if (unformat (input, "mpls %d %U", &mtu,
1203 unformat_vnet_sw_interface, vnm, &sw_if_index))
1204 mtus[VNET_MTU_MPLS] = mtu;
1206 return clib_error_return (0, "unknown input `%U'",
1207 format_unformat_error, input);
1209 vnet_sw_interface_set_protocol_mtu (vnm, sw_if_index, mtus);
1216 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1217 .path = "set interface mtu",
1218 .short_help = "set interface mtu [packet|ip4|ip6|mpls] <value> <interface>",
1219 .function = mtu_cmd,
1223 static clib_error_t *
1224 show_interface_sec_mac_addr_fn (vlib_main_t * vm, unformat_input_t * input,
1225 vlib_cli_command_t * cmd)
1227 vnet_main_t *vnm = vnet_get_main ();
1228 vnet_interface_main_t *im = &vnm->interface_main;
1229 ethernet_main_t *em = ðernet_main;
1230 u32 sw_if_index = ~0;
1231 vnet_sw_interface_t *si, *sorted_sis = 0;
1233 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1235 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1236 vec_add1 (sorted_sis, si[0]);
1239 /* if an interface name was not passed, get all interfaces */
1240 if (vec_len (sorted_sis) == 0)
1243 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1244 vec_set_len (sorted_sis, 0);
1246 pool_foreach (si, im->sw_interfaces)
1248 int visible = vnet_swif_is_api_visible (si);
1250 vec_add1 (sorted_sis, si[0]);
1254 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
1257 vec_foreach (si, sorted_sis)
1259 vnet_sw_interface_t *sup_si;
1260 ethernet_interface_t *ei;
1262 sup_si = vnet_get_sup_sw_interface (vnm, si->sw_if_index);
1263 ei = ethernet_get_interface (em, sup_si->hw_if_index);
1265 vlib_cli_output (vm, "%U (%s):",
1266 format_vnet_sw_if_index_name, vnm, si->sw_if_index,
1267 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
1270 if (ei && ei->secondary_addrs)
1272 ethernet_interface_address_t *sec_addr;
1274 vec_foreach (sec_addr, ei->secondary_addrs)
1276 vlib_cli_output (vm, " %U", format_mac_address_t, &sec_addr->mac);
1281 vec_free (sorted_sis);
1286 * This command is used to display interface secondary mac addresses.
1289 * Example of how to display interface secondary mac addresses:
1290 * @cliexstart{show interface secondary-mac-address}
1294 VLIB_CLI_COMMAND (show_interface_sec_mac_addr, static) = {
1295 .path = "show interface secondary-mac-address",
1296 .short_help = "show interface secondary-mac-address [<interface>]",
1297 .function = show_interface_sec_mac_addr_fn,
1301 static clib_error_t *
1302 interface_add_del_mac_address (vlib_main_t * vm, unformat_input_t * input,
1303 vlib_cli_command_t * cmd)
1305 vnet_main_t *vnm = vnet_get_main ();
1306 vnet_sw_interface_t *si = NULL;
1307 clib_error_t *error = 0;
1308 u32 sw_if_index = ~0;
1312 is_add = is_del = 0;
1314 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1316 error = clib_error_return (0, "unknown interface `%U'",
1317 format_unformat_error, input);
1320 if (!unformat_user (input, unformat_ethernet_address, mac))
1322 error = clib_error_return (0, "expected mac address `%U'",
1323 format_unformat_error, input);
1327 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1329 if (unformat (input, "add"))
1331 else if (unformat (input, "del"))
1337 if (is_add == is_del)
1339 error = clib_error_return (0, "must choose one of add or del");
1343 si = vnet_get_sw_interface (vnm, sw_if_index);
1345 vnet_hw_interface_add_del_mac_address (vnm, si->hw_if_index, mac, is_add);
1352 * The '<em>set interface secondary-mac-address </em>' command allows adding
1353 * or deleting extra MAC addresses on a given interface without changing the
1354 * default MAC address. This could allow packets sent to these MAC addresses
1355 * to be received without setting the interface to promiscuous mode.
1356 * Not all interfaces support this operation. The ones that do are mostly
1357 * hardware NICs, though virtio does also.
1361 * Example of how to add a secondary MAC Address on an interface:
1362 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 add}
1363 * Example of how to delete a secondary MAC address from an interface:
1364 * @cliexcmd{set interface secondary-mac-address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01 del}
1368 VLIB_CLI_COMMAND (interface_add_del_mac_address_cmd, static) = {
1369 .path = "set interface secondary-mac-address",
1370 .short_help = "set interface secondary-mac-address <interface> <mac-address> [(add|del)]",
1371 .function = interface_add_del_mac_address,
1375 static clib_error_t *
1376 set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1377 vlib_cli_command_t * cmd)
1379 vnet_main_t *vnm = vnet_get_main ();
1380 vnet_sw_interface_t *si = NULL;
1381 clib_error_t *error = 0;
1382 u32 sw_if_index = ~0;
1385 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1387 error = clib_error_return (0, "unknown interface `%U'",
1388 format_unformat_error, input);
1391 if (!unformat_user (input, unformat_ethernet_address, mac))
1393 error = clib_error_return (0, "expected mac address `%U'",
1394 format_unformat_error, input);
1397 si = vnet_get_sw_interface (vnm, sw_if_index);
1398 error = vnet_hw_interface_change_mac_address (vnm, si->hw_if_index, mac);
1404 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1405 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1406 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1410 * Example of how to change MAC Address of interface:
1411 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1412 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1413 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1414 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1418 VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1419 .path = "set interface mac address",
1420 .short_help = "set interface mac address <interface> <mac-address>",
1421 .function = set_interface_mac_address,
1425 static clib_error_t *
1426 set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1428 vnet_main_t *vnm = vnet_get_main ();
1429 u32 sw_if_index = ~0;
1432 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1433 vnm, &sw_if_index, &tag))
1434 return clib_error_return (0, "unknown input `%U'",
1435 format_unformat_error, input);
1437 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1443 VLIB_CLI_COMMAND (set_tag_command, static) = {
1444 .path = "set interface tag",
1445 .short_help = "set interface tag <interface> <tag>",
1446 .function = set_tag,
1450 static clib_error_t *
1451 clear_tag (vlib_main_t * vm, unformat_input_t * input,
1452 vlib_cli_command_t * cmd)
1454 vnet_main_t *vnm = vnet_get_main ();
1455 u32 sw_if_index = ~0;
1457 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1458 return clib_error_return (0, "unknown input `%U'",
1459 format_unformat_error, input);
1461 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1467 VLIB_CLI_COMMAND (clear_tag_command, static) = {
1468 .path = "clear interface tag",
1469 .short_help = "clear interface tag <interface>",
1470 .function = clear_tag,
1474 static clib_error_t *
1475 set_ip_directed_broadcast (vlib_main_t * vm,
1476 unformat_input_t * input, vlib_cli_command_t * cmd)
1478 vnet_main_t *vnm = vnet_get_main ();
1479 u32 sw_if_index = ~0;
1482 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index));
1483 else if (unformat (input, "enable"))
1485 else if (unformat (input, "disable"))
1488 return clib_error_return (0, "unknown input: `%U'",
1489 format_unformat_error, input);
1491 if (~0 == sw_if_index)
1492 return clib_error_return (0, "specify an interface: `%U'",
1493 format_unformat_error, input);
1495 vnet_sw_interface_ip_directed_broadcast (vnm, sw_if_index, enable);
1501 * This command is used to enable/disable IP directed broadcast
1502 * If directed broadcast is enabled a packet sent to the interface's
1503 * subnet broadcast address will be sent L2 broadcast on the interface,
1504 * otherwise it is dropped.
1507 VLIB_CLI_COMMAND (set_ip_directed_broadcast_command, static) = {
1508 .path = "set interface ip directed-broadcast",
1509 .short_help = "set interface enable <interface> <enable|disable>",
1510 .function = set_ip_directed_broadcast,
1515 set_hw_interface_change_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1516 u8 queue_id_valid, u32 queue_id,
1517 vnet_hw_if_rx_mode mode)
1519 clib_error_t *error = 0;
1520 vnet_hw_interface_t *hw;
1521 u32 *queue_indices = 0;
1522 vnet_dev_port_t *port;
1524 port = vnet_dev_get_port_from_hw_if_index (hw_if_index);
1528 vlib_main_t *vm = vlib_get_main ();
1531 vnet_dev_port_cfg_change_req_t req = {
1532 .type = mode == VNET_HW_IF_RX_MODE_POLLING ?
1533 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_DISABLE :
1534 VNET_DEV_PORT_CFG_RXQ_INTR_MODE_ENABLE,
1535 .queue_id = queue_id_valid ? queue_id : 0,
1536 .all_queues = queue_id_valid ? 0 : 1,
1539 if ((rv = vnet_dev_port_cfg_change_req_validate (vm, port, &req)))
1540 return vnet_dev_port_err (
1541 vm, port, rv, "rx queue interupt mode enable/disable not supported");
1543 if ((rv = vnet_dev_process_port_cfg_change_req (vm, port, &req)))
1544 return vnet_dev_port_err (
1546 "device failed to enable/disable queue interrupt mode");
1550 hw = vnet_get_hw_interface (vnm, hw_if_index);
1556 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1557 if (queue_index == ~0)
1558 return clib_error_return (0, "unknown queue %u on interface %s",
1559 queue_id, hw->name);
1560 vec_add1 (queue_indices, queue_index);
1563 queue_indices = hw->rx_queue_indices;
1565 for (int i = 0; i < vec_len (queue_indices); i++)
1567 int rv = vnet_hw_if_set_rx_queue_mode (vnm, queue_indices[i], mode);
1570 error = clib_error_return (
1571 0, "unable to set rx-mode on interface %v queue-id %u.\n",
1572 hw->name, queue_id);
1578 if (queue_indices != hw->rx_queue_indices)
1579 vec_free (queue_indices);
1580 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1584 static clib_error_t *
1585 set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1586 vlib_cli_command_t * cmd)
1588 clib_error_t *error = 0;
1589 unformat_input_t _line_input, *line_input = &_line_input;
1590 vnet_main_t *vnm = vnet_get_main ();
1591 u32 hw_if_index = (u32) ~ 0;
1592 u32 queue_id = (u32) ~ 0;
1593 vnet_hw_if_rx_mode mode = VNET_HW_IF_RX_MODE_UNKNOWN;
1594 u8 queue_id_valid = 0;
1596 if (!unformat_user (input, unformat_line_input, line_input))
1599 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1602 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1604 else if (unformat (line_input, "queue %d", &queue_id))
1606 else if (unformat (line_input, "polling"))
1607 mode = VNET_HW_IF_RX_MODE_POLLING;
1608 else if (unformat (line_input, "interrupt"))
1609 mode = VNET_HW_IF_RX_MODE_INTERRUPT;
1610 else if (unformat (line_input, "adaptive"))
1611 mode = VNET_HW_IF_RX_MODE_ADAPTIVE;
1614 error = clib_error_return (0, "parse error: '%U'",
1615 format_unformat_error, line_input);
1616 unformat_free (line_input);
1621 unformat_free (line_input);
1623 if (hw_if_index == (u32) ~ 0)
1624 return clib_error_return (0, "please specify valid interface name");
1626 if (mode == VNET_HW_IF_RX_MODE_UNKNOWN)
1627 return clib_error_return (0, "please specify valid rx-mode");
1629 error = set_hw_interface_change_rx_mode (vnm, hw_if_index, queue_id_valid,
1636 * This command is used to assign the RX packet processing mode (polling,
1637 * interrupt, adaptive) of the a given interface, and optionally a
1638 * given queue. If the '<em>queue</em>' is not provided, the '<em>mode</em>'
1639 * is applied to all queues of the interface. Not all interfaces support
1640 * all modes. To display the current rx-mode use the command
1641 * '<em>show interface rx-placement</em>'.
1644 * Example of how to assign rx-mode to all queues on an interface:
1645 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 polling}
1646 * Example of how to assign rx-mode to one queue of an interface:
1647 * @cliexcmd{set interface rx-mode VirtualEthernet0/0/12 queue 0 interrupt}
1648 * Example of how to display the rx-mode of all interfaces:
1649 * @cliexstart{show interface rx-placement}
1650 * Thread 1 (vpp_wk_0):
1652 * GigabitEthernet7/0/0 queue 0 (polling)
1653 * node vhost-user-input:
1654 * VirtualEthernet0/0/12 queue 0 (interrupt)
1655 * VirtualEthernet0/0/12 queue 2 (polling)
1656 * VirtualEthernet0/0/13 queue 0 (polling)
1657 * VirtualEthernet0/0/13 queue 2 (polling)
1658 * Thread 2 (vpp_wk_1):
1660 * GigabitEthernet7/0/1 queue 0 (polling)
1661 * node vhost-user-input:
1662 * VirtualEthernet0/0/12 queue 1 (polling)
1663 * VirtualEthernet0/0/12 queue 3 (polling)
1664 * VirtualEthernet0/0/13 queue 1 (polling)
1665 * VirtualEthernet0/0/13 queue 3 (polling)
1669 VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1670 .path = "set interface rx-mode",
1671 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1672 .function = set_interface_rx_mode,
1676 static clib_error_t *
1677 show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1678 vlib_cli_command_t * cmd)
1681 vnet_main_t *vnm = vnet_get_main ();
1682 vnet_hw_if_rx_queue_t **all_queues = 0;
1683 vnet_hw_if_rx_queue_t **qptr;
1684 vnet_hw_if_rx_queue_t *q;
1685 pool_foreach (q, vnm->interface_main.hw_if_rx_queues)
1686 vec_add1 (all_queues, q);
1687 vec_sort_with_function (all_queues, vnet_hw_if_rxq_cmp_cli_api);
1690 vec_foreach (qptr, all_queues)
1692 u32 current_thread = qptr[0]->thread_index;
1693 u32 hw_if_index = qptr[0]->hw_if_index;
1694 vnet_hw_interface_t *hw_if = vnet_get_hw_interface (vnm, hw_if_index);
1695 u32 current_node = hw_if->input_node_index;
1696 if (current_node != prev_node)
1697 s = format (s, " node %U:\n", format_vlib_node_name, vm, current_node);
1698 s = format (s, " %U queue %u (%U)\n", format_vnet_sw_if_index_name,
1699 vnm, hw_if->sw_if_index, qptr[0]->queue_id,
1700 format_vnet_hw_if_rx_mode, qptr[0]->mode);
1701 if (qptr == all_queues + vec_len (all_queues) - 1 ||
1702 current_thread != qptr[1]->thread_index)
1704 vlib_cli_output (vm, "Thread %u (%s):\n%v", current_thread,
1705 vlib_worker_threads[current_thread].name, s);
1706 vec_reset_length (s);
1708 prev_node = current_node;
1711 vec_free (all_queues);
1716 * This command is used to display the interface and queue worker
1720 * Example of how to display the interface placement:
1721 * @cliexstart{show interface rx-placement}
1722 * Thread 1 (vpp_wk_0):
1724 * GigabitEthernet7/0/0 queue 0 (polling)
1725 * node vhost-user-input:
1726 * VirtualEthernet0/0/12 queue 0 (polling)
1727 * VirtualEthernet0/0/12 queue 2 (polling)
1728 * VirtualEthernet0/0/13 queue 0 (polling)
1729 * VirtualEthernet0/0/13 queue 2 (polling)
1730 * Thread 2 (vpp_wk_1):
1732 * GigabitEthernet7/0/1 queue 0 (polling)
1733 * node vhost-user-input:
1734 * VirtualEthernet0/0/12 queue 1 (polling)
1735 * VirtualEthernet0/0/12 queue 3 (polling)
1736 * VirtualEthernet0/0/13 queue 1 (polling)
1737 * VirtualEthernet0/0/13 queue 3 (polling)
1741 VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1742 .path = "show interface rx-placement",
1743 .short_help = "show interface rx-placement",
1744 .function = show_interface_rx_placement_fn,
1748 set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
1749 u32 thread_index, u8 is_main)
1751 vnet_main_t *vnm = vnet_get_main ();
1752 vnet_device_main_t *vdm = &vnet_device_main;
1753 vnet_hw_interface_t *hw;
1759 thread_index += vdm->first_worker_thread_index;
1761 if (thread_index > vdm->last_worker_thread_index)
1762 return clib_error_return (0,
1763 "please specify valid worker thread or main");
1765 hw = vnet_get_hw_interface (vnm, hw_if_index);
1768 vnet_hw_if_get_rx_queue_index_by_id (vnm, hw_if_index, queue_id);
1769 if (queue_index == ~0)
1770 return clib_error_return (0, "unknown queue %u on interface %s", queue_id,
1772 vnet_hw_if_set_rx_queue_thread_index (vnm, queue_index, thread_index);
1773 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1777 static clib_error_t *
1778 set_interface_rx_placement (vlib_main_t *vm, unformat_input_t *input,
1779 vlib_cli_command_t *cmd)
1781 clib_error_t *error = 0;
1782 unformat_input_t _line_input, *line_input = &_line_input;
1783 vnet_main_t *vnm = vnet_get_main ();
1784 u32 hw_if_index = (u32) ~ 0;
1785 u32 queue_id = (u32) 0;
1786 u32 thread_index = (u32) ~ 0;
1789 if (!unformat_user (input, unformat_line_input, line_input))
1792 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1795 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1797 else if (unformat (line_input, "queue %d", &queue_id))
1799 else if (unformat (line_input, "main", &thread_index))
1801 else if (unformat (line_input, "worker %d", &thread_index))
1805 error = clib_error_return (0, "parse error: '%U'",
1806 format_unformat_error, line_input);
1807 unformat_free (line_input);
1812 unformat_free (line_input);
1814 if (hw_if_index == (u32) ~ 0)
1815 return clib_error_return (0, "please specify valid interface name");
1817 error = set_hw_interface_rx_placement (hw_if_index, queue_id, thread_index,
1824 * This command is used to assign a given interface, and optionally a
1825 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1826 * it defaults to 0. The '<em>worker</em>' parameter is zero based and the index
1827 * in the thread name, for example, 0 in the thread name '<em>vpp_wk_0</em>'.
1830 * Example of how to display the interface placement:
1831 * @cliexstart{show interface rx-placement}
1832 * Thread 1 (vpp_wk_0):
1834 * GigabitEthernet7/0/0 queue 0 (polling)
1835 * node vhost-user-input:
1836 * VirtualEthernet0/0/12 queue 0 (polling)
1837 * VirtualEthernet0/0/12 queue 2 (polling)
1838 * VirtualEthernet0/0/13 queue 0 (polling)
1839 * VirtualEthernet0/0/13 queue 2 (polling)
1840 * Thread 2 (vpp_wk_1):
1842 * GigabitEthernet7/0/1 queue 0 (polling)
1843 * node vhost-user-input:
1844 * VirtualEthernet0/0/12 queue 1 (polling)
1845 * VirtualEthernet0/0/12 queue 3 (polling)
1846 * VirtualEthernet0/0/13 queue 1 (polling)
1847 * VirtualEthernet0/0/13 queue 3 (polling)
1849 * Example of how to assign a interface and queue to a worker thread:
1850 * @cliexcmd{set interface rx-placement VirtualEthernet0/0/12 queue 1 worker 0}
1851 * Example of how to display the interface placement:
1852 * @cliexstart{show interface rx-placement}
1853 * Thread 1 (vpp_wk_0):
1855 * GigabitEthernet7/0/0 queue 0 (polling)
1856 * node vhost-user-input:
1857 * VirtualEthernet0/0/12 queue 0 (polling)
1858 * VirtualEthernet0/0/12 queue 1 (polling)
1859 * VirtualEthernet0/0/12 queue 2 (polling)
1860 * VirtualEthernet0/0/13 queue 0 (polling)
1861 * VirtualEthernet0/0/13 queue 2 (polling)
1862 * Thread 2 (vpp_wk_1):
1864 * GigabitEthernet7/0/1 queue 0 (polling)
1865 * node vhost-user-input:
1866 * VirtualEthernet0/0/12 queue 3 (polling)
1867 * VirtualEthernet0/0/13 queue 1 (polling)
1868 * VirtualEthernet0/0/13 queue 3 (polling)
1872 VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1873 .path = "set interface rx-placement",
1874 .short_help = "set interface rx-placement <interface> [queue <n>] "
1875 "[worker <n> | main]",
1876 .function = set_interface_rx_placement,
1882 set_hw_interface_tx_queue (u32 hw_if_index, u32 queue_id, uword *bitmap)
1884 vnet_main_t *vnm = vnet_get_main ();
1885 vlib_thread_main_t *vtm = vlib_get_thread_main ();
1886 vnet_hw_if_tx_queue_t *txq;
1890 /* highest set bit in bitmap should not exceed last worker thread index */
1891 thread_index = clib_bitmap_last_set (bitmap);
1892 if ((thread_index != ~0) && (thread_index >= vtm->n_vlib_mains))
1893 return VNET_API_ERROR_INVALID_VALUE;
1896 vnet_hw_if_get_tx_queue_index_by_id (vnm, hw_if_index, queue_id);
1897 if (queue_index == ~0)
1898 return VNET_API_ERROR_INVALID_QUEUE;
1900 txq = vnet_hw_if_get_tx_queue (vnm, queue_index);
1902 // free the existing bitmap
1903 if (clib_bitmap_count_set_bits (txq->threads))
1905 txq->shared_queue = 0;
1906 clib_bitmap_free (txq->threads);
1909 clib_bitmap_foreach (thread_index, bitmap)
1910 vnet_hw_if_tx_queue_assign_thread (vnm, queue_index, thread_index);
1912 vnet_hw_if_update_runtime_data (vnm, hw_if_index);
1916 static clib_error_t *
1917 set_interface_tx_queue (vlib_main_t *vm, unformat_input_t *input,
1918 vlib_cli_command_t *cmd)
1920 clib_error_t *error = 0;
1921 unformat_input_t _line_input, *line_input = &_line_input;
1922 vnet_main_t *vnm = vnet_get_main ();
1923 u32 hw_if_index = (u32) ~0;
1924 u32 queue_id = (u32) 0;
1928 if (!unformat_user (input, unformat_line_input, line_input))
1931 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1933 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
1936 else if (unformat (line_input, "queue %d", &queue_id))
1938 else if (unformat (line_input, "threads %U", unformat_bitmap_list,
1943 error = clib_error_return (0, "parse error: '%U'",
1944 format_unformat_error, line_input);
1945 unformat_free (line_input);
1950 unformat_free (line_input);
1952 if (hw_if_index == (u32) ~0)
1954 error = clib_error_return (0, "please specify valid interface name");
1958 rv = set_hw_interface_tx_queue (hw_if_index, queue_id, bitmap);
1962 case VNET_API_ERROR_INVALID_VALUE:
1963 error = clib_error_return (
1964 0, "please specify valid thread(s) - last thread index %u",
1965 clib_bitmap_last_set (bitmap));
1967 case VNET_API_ERROR_INVALID_QUEUE:
1968 error = clib_error_return (
1969 0, "unknown queue %u on interface %s", queue_id,
1970 vnet_get_hw_interface (vnet_get_main (), hw_if_index)->name);
1977 clib_bitmap_free (bitmap);
1981 VLIB_CLI_COMMAND (cmd_set_if_tx_queue, static) = {
1982 .path = "set interface tx-queue",
1983 .short_help = "set interface tx-queue <interface> queue <n> "
1985 .function = set_interface_tx_queue,
1990 set_interface_rss_queues (vlib_main_t * vm, u32 hw_if_index,
1991 clib_bitmap_t * bitmap)
1993 vnet_main_t *vnm = vnet_get_main ();
1994 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1996 return vnet_hw_interface_set_rss_queues (vnm, hi, bitmap);
1999 static clib_error_t *
2000 set_interface_rss_queues_fn (vlib_main_t * vm,
2001 unformat_input_t * input,
2002 vlib_cli_command_t * cmd)
2004 clib_error_t *error = 0;
2005 unformat_input_t _line_input, *line_input = &_line_input;
2006 vnet_main_t *vnm = vnet_get_main ();
2007 u32 hw_if_index = (u32) ~ 0;
2008 clib_bitmap_t *bitmap = NULL;
2010 if (!unformat_user (input, unformat_line_input, line_input))
2013 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2016 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
2019 if (unformat (line_input, "list %U", unformat_bitmap_list, &bitmap))
2023 error = clib_error_return (0, "parse error: '%U'",
2024 format_unformat_error, line_input);
2025 unformat_free (line_input);
2030 unformat_free (line_input);
2032 if (hw_if_index == (u32) ~ 0)
2034 error = clib_error_return (0, "please specify valid interface name");
2040 error = clib_error_return (0, "please specify the valid rss queues");
2044 error = set_interface_rss_queues (vm, hw_if_index, bitmap);
2048 clib_bitmap_free (bitmap);
2054 * This command is used to set the rss queues of a given interface
2055 * Not all the interfaces support this operation.
2056 * To display the current rss queues, use the command
2057 * '<em>show hardware-interfaces</em>'.
2060 * Example of how to set the rss queues to 0,2-5,7 of an interface:
2061 * @cliexstart{set interface rss queues VirtualFunctionEthernet18/1/0 list 0,2-5,7}
2065 VLIB_CLI_COMMAND (cmd_set_interface_rss_queues,static) = {
2066 .path = "set interface rss queues",
2067 .short_help = "set interface rss queues <interface> <list <queue-list>>",
2068 .function = set_interface_rss_queues_fn,
2073 format_vnet_pcap (u8 * s, va_list * args)
2075 vnet_pcap_t *pp = va_arg (*args, vnet_pcap_t *);
2076 int type = va_arg (*args, int);
2081 if (pp->pcap_rx_enable)
2083 s = format (s, "rx");
2086 if (pp->pcap_tx_enable)
2089 s = format (s, " and ");
2090 s = format (s, "tx");
2093 if (pp->pcap_drop_enable)
2096 s = format (s, " and ");
2097 s = format (s, "drop");
2102 s = format (s, "unknown type %d!", type);
2108 vnet_pcap_dispatch_trace_configure (vnet_pcap_dispatch_trace_args_t * a)
2110 vlib_main_t *vm = vlib_get_main ();
2111 vnet_main_t *vnm = vnet_get_main ();
2112 vnet_pcap_t *pp = &vnm->pcap;
2113 pcap_main_t *pm = &pp->pcap_main;
2114 vnet_classify_main_t *cm = &vnet_classify_main;
2118 if (pp->pcap_rx_enable || pp->pcap_tx_enable || pp->pcap_drop_enable)
2121 (vm, "pcap %U dispatch capture enabled: %d of %d pkts...",
2122 format_vnet_pcap, pp, 0 /* print type */ ,
2123 pm->n_packets_captured, pm->n_packets_to_capture);
2124 vlib_cli_output (vm, "capture to file %s", pm->file_name);
2127 vlib_cli_output (vm, "pcap dispatch capture disabled");
2132 /* Consistency checks */
2134 /* Enable w/ capture already enabled not allowed */
2135 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2136 && (a->rx_enable + a->tx_enable + a->drop_enable))
2137 return VNET_API_ERROR_INVALID_VALUE;
2139 /* Disable capture with capture already disabled, not interesting */
2140 if (((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable) ==
2142 ((a->rx_enable + a->tx_enable + a->drop_enable == 0)))
2143 return VNET_API_ERROR_VALUE_EXIST;
2145 /* Change number of packets to capture while capturing */
2146 if ((pp->pcap_rx_enable + pp->pcap_tx_enable + pp->pcap_drop_enable)
2147 && (a->rx_enable + a->tx_enable + a->drop_enable)
2148 && (pm->n_packets_to_capture != a->packets_to_capture))
2149 return VNET_API_ERROR_INVALID_VALUE_2;
2151 /* Classify filter specified, but no classify filter configured */
2152 if ((a->rx_enable + a->tx_enable + a->drop_enable) && a->filter &&
2153 (!cm->classify_table_index_by_sw_if_index ||
2154 cm->classify_table_index_by_sw_if_index[0] == ~0))
2155 return VNET_API_ERROR_NO_SUCH_LABEL;
2157 if (a->rx_enable + a->tx_enable + a->drop_enable)
2159 void *save_pcap_data;
2161 /* Sanity check max bytes per pkt */
2162 if (a->max_bytes_per_pkt < 32 || a->max_bytes_per_pkt > 9000)
2163 return VNET_API_ERROR_INVALID_MEMORY_SIZE;
2165 /* Clean up from previous run, if any */
2166 vec_reset_length (pm->pcap_data);
2168 /* Throw away the data buffer? */
2170 vec_free (pm->pcap_data);
2172 save_pcap_data = pm->pcap_data;
2174 memset (pm, 0, sizeof (*pm));
2176 pm->pcap_data = save_pcap_data;
2178 vec_validate_aligned (vnet_trace_placeholder, 2048,
2179 CLIB_CACHE_LINE_BYTES);
2181 clib_spinlock_init (&(pm->lock));
2183 if (a->filename == 0)
2188 stem = format (stem, "rx");
2190 stem = format (stem, "tx");
2192 stem = format (stem, "drop");
2193 a->filename = format (0, "/tmp/%v.pcap%c", stem, 0);
2197 pm->file_name = (char *) a->filename;
2198 pm->n_packets_captured = 0;
2199 pm->packet_type = PCAP_PACKET_TYPE_ethernet;
2200 /* Preallocate the data vector? */
2201 if (a->preallocate_data)
2204 (pm->pcap_data, a->packets_to_capture
2205 * ((sizeof (pcap_packet_header_t) + a->max_bytes_per_pkt)));
2206 vec_reset_length (pm->pcap_data);
2208 pm->n_packets_to_capture = a->packets_to_capture;
2209 pp->pcap_sw_if_index = a->sw_if_index;
2211 pp->filter_classify_table_index =
2212 cm->classify_table_index_by_sw_if_index[0];
2214 pp->filter_classify_table_index = ~0;
2215 pp->pcap_error_index = a->drop_err;
2216 pp->pcap_rx_enable = a->rx_enable;
2217 pp->pcap_tx_enable = a->tx_enable;
2218 pp->pcap_drop_enable = a->drop_enable;
2219 pp->max_bytes_per_pkt = a->max_bytes_per_pkt;
2223 pp->pcap_rx_enable = 0;
2224 pp->pcap_tx_enable = 0;
2225 pp->pcap_drop_enable = 0;
2226 pp->filter_classify_table_index = ~0;
2227 pp->pcap_error_index = ~0;
2228 if (pm->n_packets_captured)
2230 clib_error_t *error;
2231 pm->n_packets_to_capture = pm->n_packets_captured;
2232 vlib_cli_output (vm, "Write %d packets to %s, and stop capture...",
2233 pm->n_packets_captured, pm->file_name);
2234 error = pcap_write (pm);
2235 if (pm->flags & PCAP_MAIN_INIT_DONE)
2237 /* Report I/O errors... */
2240 clib_error_report (error);
2241 return VNET_API_ERROR_SYSCALL_ERROR_1;
2243 vec_free (pm->file_name);
2245 vec_free (pm->pcap_data);
2249 return VNET_API_ERROR_NO_SUCH_ENTRY;
2255 static clib_error_t *
2256 pcap_trace_command_fn (vlib_main_t * vm,
2257 unformat_input_t * input, vlib_cli_command_t * cmd)
2259 unformat_input_t _line_input, *line_input = &_line_input;
2260 vnet_pcap_dispatch_trace_args_t _a, *a = &_a;
2261 vnet_main_t *vnm = vnet_get_main ();
2264 u32 max_bytes_per_pkt = 512;
2268 int preallocate_data = 0;
2269 int drop_enable = 0;
2273 u32 sw_if_index = 0; /* default: any interface */
2274 vlib_error_t drop_err = ~0; /* default: any error */
2276 /* Get a line of input. */
2277 if (!unformat_user (input, unformat_line_input, line_input))
2280 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2282 if (unformat (line_input, "rx"))
2284 else if (unformat (line_input, "tx"))
2286 else if (unformat (line_input, "drop"))
2288 else if (unformat (line_input, "off"))
2289 rx_enable = tx_enable = drop_enable = 0;
2290 else if (unformat (line_input, "max-bytes-per-pkt %u",
2291 &max_bytes_per_pkt))
2293 else if (unformat (line_input, "max %d", &max))
2295 else if (unformat (line_input, "packets-to-capture %d", &max))
2297 else if (unformat (line_input, "file %U", unformat_vlib_tmpfile,
2300 else if (unformat (line_input, "status %=", &status, 1))
2302 else if (unformat (line_input, "intfc %U",
2303 unformat_vnet_sw_interface, vnm, &sw_if_index))
2305 else if (unformat (line_input, "interface %U",
2306 unformat_vnet_sw_interface, vnm, &sw_if_index))
2308 else if (unformat (line_input, "error %U", unformat_vlib_error, vm,
2311 else if (unformat (line_input, "preallocate-data %=",
2312 &preallocate_data, 1))
2314 else if (unformat (line_input, "free-data %=", &free_data, 1))
2316 else if (unformat (line_input, "intfc any")
2317 || unformat (line_input, "interface any"))
2319 else if (unformat (line_input, "filter"))
2323 return clib_error_return (0, "unknown input `%U'",
2324 format_unformat_error, line_input);
2328 unformat_free (line_input);
2330 /* no need for memset (a, 0, sizeof (*a)), set all fields here. */
2331 a->filename = filename;
2332 a->rx_enable = rx_enable;
2333 a->tx_enable = tx_enable;
2334 a->preallocate_data = preallocate_data;
2335 a->free_data = free_data;
2336 a->drop_enable = drop_enable;
2338 a->packets_to_capture = max;
2339 a->sw_if_index = sw_if_index;
2341 a->max_bytes_per_pkt = max_bytes_per_pkt;
2342 a->drop_err = drop_err;
2344 rv = vnet_pcap_dispatch_trace_configure (a);
2351 case VNET_API_ERROR_INVALID_VALUE:
2352 return clib_error_return (0, "dispatch trace already enabled...");
2354 case VNET_API_ERROR_VALUE_EXIST:
2355 return clib_error_return (0, "dispatch trace already disabled...");
2357 case VNET_API_ERROR_INVALID_VALUE_2:
2358 return clib_error_return
2359 (0, "can't change number of records to capture while tracing...");
2361 case VNET_API_ERROR_SYSCALL_ERROR_1:
2362 return clib_error_return (0, "I/O writing trace capture...");
2364 case VNET_API_ERROR_NO_SUCH_ENTRY:
2365 return clib_error_return (0, "No packets captured...");
2367 case VNET_API_ERROR_INVALID_MEMORY_SIZE:
2368 return clib_error_return (0,
2369 "Max bytes per pkt must be > 32, < 9000...");
2371 case VNET_API_ERROR_NO_SUCH_LABEL:
2372 return clib_error_return
2373 (0, "No classify filter configured, see 'classify filter...'");
2376 vlib_cli_output (vm, "WARNING: trace configure returned %d", rv);
2383 * This command is used to start or stop a packet capture, or show
2384 * the status of packet capture.
2386 * This command has the following optional parameters:
2389 * - <b>rx</b> - Capture received packets
2391 * - <b>tx</b> - Capture transmitted packets
2393 * - <b>drop</b> - Capture dropped packets
2395 * - <b>off</b> - Stop capturing packets, write results to the specified file
2397 * - <b>max <nn></b> - Depth of local buffer. Once '<em>nn</em>' number
2398 * of packets have been received, buffer is flushed to file. Once another
2399 * '<em>nn</em>' number of packets have been received, buffer is flushed
2400 * to file, overwriting previous write. If not entered, value defaults
2401 * to 100. Can only be updated if packet capture is off.
2403 * - <b>max-bytes-per-pkt <nnnn></b> - Maximum number of bytes to capture
2404 * for each packet. Must be >= 32, <= 9000.
2406 * - <b>preallocate-data</b> - Preallocate the data buffer, to avoid
2407 * vector expansion delays during pcap capture
2409 * - <b>free-data</b> - Free the data buffer. Ordinarily it's a feature
2410 * to retain the data buffer so this option is seldom used.
2412 * - <b>intfc <interface-name>|any</b> - Used to specify a given interface,
2413 * or use '<em>any</em>' to run packet capture on all interfaces.
2414 * '<em>any</em>' is the default if not provided. Settings from a previous
2415 * packet capture are preserved, so '<em>any</em>' can be used to reset
2416 * the interface setting.
2418 * - <b>filter</b> - Use the pcap trace rx / tx / drop filter, which
2419 * must be configured. Use <b>classify filter pcap...</b> to configure the
2420 * filter. The filter will only be executed if the per-interface or
2421 * any-interface tests fail.
2423 * - <b>error <node>.<error></b> - filter packets based on a specific error.
2424 * For example: error {ip4-udp-lookup}.{no_listener}
2426 * - <b>file <name></b> - Used to specify the output filename. The file will
2427 * be placed in the '<em>/tmp</em>' directory, so only the filename is
2428 * supported. Directory should not be entered. If file already exists, file
2429 * will be overwritten. If no filename is provided, the file will be
2430 * named "/tmp/rx.pcap", "/tmp/tx.pcap", "/tmp/rxandtx.pcap", etc.
2431 * Can only be updated if packet capture is off.
2433 * - <b>status</b> - Displays the current status and configured attributes
2434 * associated with a packet capture. If packet capture is in progress,
2435 * '<em>status</em>' also will return the number of packets currently in
2436 * the local buffer. All additional attributes entered on command line
2437 * with '<em>status</em>' will be ignored and not applied.
2440 * Example of how to display the status of a tx packet capture when off:
2441 * @cliexstart{pcap trace status}
2442 * max is 100, for any interface to file /tmp/vpe.pcap
2443 * pcap tx capture is off...
2445 * Example of how to start a tx packet capture:
2446 * @cliexstart{pcap trace tx max 35 intfc GigabitEthernet0/8/0 file
2449 * Example of how to display the status of a tx packet capture in progress:
2450 * @cliexstart{pcap trace status}
2451 * max is 35, for interface GigabitEthernet0/8/0 to file /tmp/vppTest.pcap
2452 * pcap tx capture is on: 20 of 35 pkts...
2454 * Example of how to stop a tx packet capture:
2455 * @cliexstart{pcap trace off}
2456 * captured 21 pkts...
2457 * saved to /tmp/vppTest.pcap...
2462 VLIB_CLI_COMMAND (pcap_tx_trace_command, static) = {
2463 .path = "pcap trace",
2465 "pcap trace [rx] [tx] [drop] [off] [max <nn>] [intfc <interface>|any]\n"
2466 " [file <name>] [status] [max-bytes-per-pkt <nnnn>][filter]\n"
2467 " [preallocate-data][free-data]",
2468 .function = pcap_trace_command_fn,
2472 static clib_error_t *
2473 set_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2474 vlib_cli_command_t *cmd)
2476 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2477 unformat_input_t _line_input, *line_input = &_line_input;
2478 vlib_is_packet_traced_fn_t *res = 0;
2479 clib_error_t *error = 0;
2481 if (!unformat_user (input, unformat_line_input, line_input))
2484 while (unformat_check_input (line_input) != (uword) UNFORMAT_END_OF_INPUT)
2486 if (unformat (line_input, "%U", unformat_vlib_trace_filter_function,
2491 error = clib_error_create (
2492 "expected valid trace filter function, got `%U'",
2493 format_unformat_error, line_input);
2497 pp->current_filter_function = res;
2500 unformat_free (line_input);
2505 VLIB_CLI_COMMAND (set_pcap_filter_function_cli, static) = {
2506 .path = "set pcap filter function",
2507 .short_help = "set pcap filter function <func_name>",
2508 .function = set_pcap_filter_function,
2511 static clib_error_t *
2512 show_pcap_filter_function (vlib_main_t *vm, unformat_input_t *input,
2513 vlib_cli_command_t *cmd)
2515 vnet_pcap_t *pp = &vnet_get_main ()->pcap;
2516 vlib_trace_filter_main_t *tfm = &vlib_trace_filter_main;
2517 vlib_is_packet_traced_fn_t *current_trace_filter_fn =
2518 pp->current_filter_function;
2519 vlib_trace_filter_function_registration_t *reg =
2520 tfm->trace_filter_registration;
2524 vlib_cli_output (vm, "%sname:%s description: %s priority: %u",
2525 reg->function == current_trace_filter_fn ? "(*) " : "",
2526 reg->name, reg->description, reg->priority);
2532 VLIB_CLI_COMMAND (show_pcap_filter_function_cli, static) = {
2533 .path = "show pcap filter function",
2534 .short_help = "show pcap filter function",
2535 .function = show_pcap_filter_function,
2538 static clib_error_t *
2539 set_interface_name (vlib_main_t *vm, unformat_input_t *input,
2540 vlib_cli_command_t *cmd)
2542 clib_error_t *error = 0;
2543 unformat_input_t _line_input, *line_input = &_line_input;
2544 vnet_main_t *vnm = vnet_get_main ();
2545 u32 hw_if_index = ~0;
2548 if (!unformat_user (input, unformat_line_input, line_input))
2551 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2553 if (unformat (line_input, "%U %s", unformat_vnet_hw_interface, vnm,
2554 &hw_if_index, &name))
2558 error = clib_error_return (0, "parse error: '%U'",
2559 format_unformat_error, line_input);
2560 unformat_free (line_input);
2566 unformat_free (line_input);
2568 if (hw_if_index == (u32) ~0 || name == 0)
2571 error = clib_error_return (0, "please specify valid interface name");
2575 error = vnet_rename_interface (vnm, hw_if_index, name);
2581 VLIB_CLI_COMMAND (cmd_set_if_name, static) = {
2582 .path = "set interface name",
2583 .short_help = "set interface name <interface-name> <new-interface-name>",
2584 .function = set_interface_name,
2588 static clib_error_t *
2589 set_interface_tx_hash_cmd (vlib_main_t *vm, unformat_input_t *input,
2590 vlib_cli_command_t *cmd)
2592 clib_error_t *error = 0;
2593 unformat_input_t _line_input, *line_input = &_line_input;
2594 vnet_main_t *vnm = vnet_get_main ();
2595 vnet_hw_interface_t *hi;
2597 u32 hw_if_index = (u32) ~0;
2599 vnet_hash_fn_type_t ftype;
2601 if (!unformat_user (input, unformat_line_input, line_input))
2604 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2606 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2609 else if (unformat (line_input, "hash-name %s", &hash_name))
2613 error = clib_error_return (0, "parse error: '%U'",
2614 format_unformat_error, line_input);
2615 unformat_free (line_input);
2620 unformat_free (line_input);
2622 if (hw_if_index == (u32) ~0)
2624 error = clib_error_return (0, "please specify valid interface name");
2630 error = clib_error_return (0, "hash-name is required");
2634 hi = vnet_get_hw_interface (vnm, hw_if_index);
2636 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2637 hf = vnet_hash_function_from_name ((const char *) hash_name, ftype);
2641 error = clib_error_return (0, "please specify valid hash name");
2647 vec_free (hash_name);
2651 VLIB_CLI_COMMAND (cmd_set_if_tx_hash, static) = {
2652 .path = "set interface tx-hash",
2653 .short_help = "set interface tx-hash <interface> hash-name <hash-name>",
2654 .function = set_interface_tx_hash_cmd,
2657 static clib_error_t *
2658 show_tx_hash (vlib_main_t *vm, unformat_input_t *input,
2659 vlib_cli_command_t *cmd)
2661 clib_error_t *error = 0;
2662 unformat_input_t _line_input, *line_input = &_line_input;
2663 vnet_main_t *vnm = vnet_get_main ();
2664 vnet_hw_interface_t *hi;
2665 vnet_hash_function_registration_t *hash;
2666 u32 hw_if_index = (u32) ~0;
2667 vnet_hash_fn_type_t ftype;
2669 if (!unformat_user (input, unformat_line_input, line_input))
2672 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2674 if (unformat (line_input, "%U", unformat_vnet_hw_interface, vnm,
2679 error = clib_error_return (0, "parse error: '%U'",
2680 format_unformat_error, line_input);
2681 unformat_free (line_input);
2686 unformat_free (line_input);
2688 if (hw_if_index == (u32) ~0)
2690 error = clib_error_return (0, "please specify valid interface name");
2694 hi = vnet_get_hw_interface (vnm, hw_if_index);
2696 vnet_get_hw_interface_class (vnm, hi->hw_class_index)->tx_hash_fn_type;
2700 hash = vnet_hash_function_from_func (hi->hf, ftype);
2702 vlib_cli_output (vm, "%U", format_vnet_hash, hash);
2704 vlib_cli_output (vm, "no matching hash function found");
2707 vlib_cli_output (vm, "no hashing function set");
2713 VLIB_CLI_COMMAND (cmd_show_tx_hash, static) = {
2714 .path = "show interface tx-hash",
2715 .short_help = "show interface tx-hash [interface]",
2716 .function = show_tx_hash,
2720 * fd.io coding-style-patch-verification: ON
2723 * eval: (c-set-style "gnu")