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.
45 #include <vnet/vnet.h>
46 #include <vnet/ip/ip.h>
47 #include <vppinfra/bitmap.h>
48 #include <vnet/fib/ip4_fib.h>
49 #include <vnet/fib/ip6_fib.h>
52 compare_interface_names (void *a1, void *a2)
57 return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2);
61 show_or_clear_hw_interfaces (vlib_main_t * vm,
62 unformat_input_t * input,
63 vlib_cli_command_t * cmd)
65 clib_error_t *error = 0;
66 vnet_main_t *vnm = vnet_get_main ();
67 vnet_interface_main_t *im = &vnm->interface_main;
68 vnet_hw_interface_t *hi;
69 u32 hw_if_index, *hw_if_indices = 0;
70 int i, verbose = -1, is_show, show_bond = 0;
72 is_show = strstr (cmd->path, "show") != 0;
73 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
75 /* See if user wants to show a specific interface. */
77 (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
78 vec_add1 (hw_if_indices, hw_if_index);
80 /* See if user wants to show an interface with a specific hw_if_index. */
81 else if (unformat (input, "%u", &hw_if_index))
82 vec_add1 (hw_if_indices, hw_if_index);
84 else if (unformat (input, "verbose"))
85 verbose = 1; /* this is also the default */
87 else if (unformat (input, "detail"))
90 else if (unformat (input, "brief"))
93 else if (unformat (input, "bond"))
97 verbose = 0; /* default to brief for link bonding */
102 error = clib_error_return (0, "unknown input `%U'",
103 format_unformat_error, input);
108 /* Gather interfaces. */
109 if (vec_len (hw_if_indices) == 0)
110 pool_foreach (hi, im->hw_interfaces,
111 vec_add1 (hw_if_indices, hi - im->hw_interfaces));
114 verbose = 1; /* default to verbose (except bond) */
119 vec_sort_with_function (hw_if_indices, compare_interface_names);
121 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
122 for (i = 0; i < vec_len (hw_if_indices); i++)
124 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
125 if (show_bond == 0) /* show all interfaces */
126 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
128 else if ((hi->bond_info) &&
129 (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE))
130 { /* show only bonded interface and all its slave interfaces */
132 vnet_hw_interface_t *shi;
133 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm,
137 clib_bitmap_foreach (hw_idx, hi->bond_info,
139 shi = vnet_get_hw_interface(vnm, hw_idx);
140 vlib_cli_output (vm, "%U\n",
141 format_vnet_hw_interface, vnm, shi, verbose);
149 for (i = 0; i < vec_len (hw_if_indices); i++)
151 vnet_device_class_t *dc;
153 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
154 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
156 if (dc->clear_counters)
157 dc->clear_counters (hi->dev_instance);
162 vec_free (hw_if_indices);
168 * Displays various information about the state of the current terminal
172 * @cliexstart{show hardware}
174 * GigabitEthernet2/0/0 up GigabitEthernet2/0/0
175 * Ethernet address 00:50:56:b7:7c:83
176 * Intel 82545em_copper
177 * link up, media 1000T full-duplex, master,
178 * 0 unprocessed, 384 total buffers on rx queue 0 ring
179 * 237 buffers in driver rx cache
180 * rx total packets 1816
181 * rx total bytes 181084
182 * rx good packets 1816
183 * rx good bytes 181084
184 * rx 65 127 byte packets 1586
185 * rx 256 511 byte packets 230
186 * tx total packets 346
187 * tx total bytes 90224
188 * tx good packets 346
189 * tx good bytes 88840
190 * tx 64 byte packets 1
191 * tx 65 127 byte packets 115
192 * tx 256 511 byte packets 230
195 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
196 .path = "show hardware-interfaces",
197 .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] [<if-name1> <if-name2> ...]",
198 .function = show_or_clear_hw_interfaces,
203 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
204 .path = "clear hardware-interfaces",
205 .short_help = "Clear hardware interfaces statistics",
206 .function = show_or_clear_hw_interfaces,
211 sw_interface_name_compare (void *a1, void *a2)
213 vnet_sw_interface_t *si1 = a1;
214 vnet_sw_interface_t *si2 = a2;
216 return vnet_sw_interface_compare (vnet_get_main (),
217 si1->sw_if_index, si2->sw_if_index);
220 static clib_error_t *
221 show_sw_interfaces (vlib_main_t * vm,
222 unformat_input_t * input, vlib_cli_command_t * cmd)
224 clib_error_t *error = 0;
225 vnet_main_t *vnm = vnet_get_main ();
226 vnet_interface_main_t *im = &vnm->interface_main;
227 vnet_sw_interface_t *si, *sorted_sis = 0;
228 u32 sw_if_index = ~(u32) 0;
229 u8 show_addresses = 0;
230 u8 show_features = 0;
233 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
235 /* See if user wants to show specific interface */
237 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
239 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
240 vec_add1 (sorted_sis, si[0]);
242 else if (unformat (input, "address") || unformat (input, "addr"))
244 else if (unformat (input, "features") || unformat (input, "feat"))
246 else if (unformat (input, "tag"))
250 error = clib_error_return (0, "unknown input `%U'",
251 format_unformat_error, input);
256 if (show_features || show_tag)
258 if (sw_if_index == ~(u32) 0)
259 return clib_error_return (0, "Interface not specified...");
264 vnet_interface_features_show (vm, sw_if_index);
270 tag = vnet_get_sw_interface_tag (vnm, sw_if_index);
271 vlib_cli_output (vm, "%U: %s",
272 format_vnet_sw_if_index_name, vnm, sw_if_index,
273 tag ? (char *) tag : "(none)");
278 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
280 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
282 /* Gather interfaces. */
284 vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
285 _vec_len (sorted_sis) = 0;
286 pool_foreach (si, im->sw_interfaces, (
288 if (vnet_swif_is_api_visible
289 (si)) vec_add1 (sorted_sis,
294 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
299 vec_foreach (si, sorted_sis)
301 l2input_main_t *l2m = &l2input_main;
302 ip4_main_t *im4 = &ip4_main;
303 ip6_main_t *im6 = &ip6_main;
304 ip_lookup_main_t *lm4 = &im4->lookup_main;
305 ip_lookup_main_t *lm6 = &im6->lookup_main;
306 ip_interface_address_t *ia = 0;
309 u32 fib_index4 = 0, fib_index6 = 0;
312 l2_input_config_t *config;
314 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
315 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
318 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
319 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
322 fib4 = ip4_fib_get (fib_index4);
323 fib6 = ip6_fib_get (fib_index6);
325 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
327 (vm, "%U (%s): \n unnumbered, use %U",
328 format_vnet_sw_if_index_name,
329 vnm, si->sw_if_index,
330 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
331 format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index);
335 vlib_cli_output (vm, "%U (%s):",
336 format_vnet_sw_if_index_name,
337 vnm, si->sw_if_index,
338 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
342 /* Display any L2 addressing info */
343 vec_validate (l2m->configs, si->sw_if_index);
344 config = vec_elt_at_index (l2m->configs, si->sw_if_index);
347 u32 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
348 vlib_cli_output (vm, " l2 bridge bd_id %d%s%d", bd_id,
349 config->bvi ? " bvi shg " : " shg ",
352 else if (config->xconnect)
354 vlib_cli_output (vm, " l2 xconnect %U",
355 format_vnet_sw_if_index_name,
356 vnm, config->output_sw_if_index);
359 /* Display any IP4 addressing info */
361 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
362 1 /* honor unnumbered */,
364 r4 = ip_interface_address_get_address (lm4, ia);
367 vlib_cli_output (vm, " %U/%d table %d",
368 format_ip4_address, r4,
374 vlib_cli_output (vm, " %U/%d",
375 format_ip4_address, r4,
381 /* Display any IP6 addressing info */
383 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
384 1 /* honor unnumbered */,
386 r6 = ip_interface_address_get_address (lm6, ia);
389 vlib_cli_output (vm, " %U/%d table %d",
390 format_ip6_address, r6,
396 vlib_cli_output (vm, " %U/%d",
397 format_ip6_address, r6,
406 vec_foreach (si, sorted_sis)
408 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
413 vec_free (sorted_sis);
418 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
419 .path = "show interface",
420 .short_help = "show interface [address|addr|features|feat] [<if-name1> <if-name2> ...]",
421 .function = show_sw_interfaces,
425 /* Root of all interface commands. */
427 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
429 .short_help = "Interface commands",
434 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
435 .path = "set interface",
436 .short_help = "Interface commands",
440 static clib_error_t *
441 clear_interface_counters (vlib_main_t * vm,
442 unformat_input_t * input, vlib_cli_command_t * cmd)
444 vnet_main_t *vnm = vnet_get_main ();
445 vnet_interface_main_t *im = &vnm->interface_main;
446 vlib_simple_counter_main_t *sm;
447 vlib_combined_counter_main_t *cm;
448 static vnet_main_t **my_vnet_mains;
449 int i, j, n_counters;
451 vec_reset_length (my_vnet_mains);
453 for (i = 0; i < vec_len (vnet_mains); i++)
456 vec_add1 (my_vnet_mains, vnet_mains[i]);
459 if (vec_len (vnet_mains) == 0)
460 vec_add1 (my_vnet_mains, vnm);
462 n_counters = vec_len (im->combined_sw_if_counters);
464 for (j = 0; j < n_counters; j++)
466 for (i = 0; i < vec_len (my_vnet_mains); i++)
468 im = &my_vnet_mains[i]->interface_main;
469 cm = im->combined_sw_if_counters + j;
470 vlib_clear_combined_counters (cm);
474 n_counters = vec_len (im->sw_if_counters);
476 for (j = 0; j < n_counters; j++)
478 for (i = 0; i < vec_len (my_vnet_mains); i++)
480 im = &my_vnet_mains[i]->interface_main;
481 sm = im->sw_if_counters + j;
482 vlib_clear_simple_counters (sm);
490 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
491 .path = "clear interfaces",
492 .short_help = "Clear interfaces statistics",
493 .function = clear_interface_counters,
498 * Parse subinterface names.
500 * The following subinterface syntax is supported. The first two are for
501 * backwards compatability:
504 * - a subinterface with the name <intf-name>.<id>. The subinterface
505 * is a single dot1q vlan with vlan id <id> and exact-match semantics.
507 * <intf-name> <min_id>-<max_id>
508 * - a set of the above subinterfaces, repeating for each id
509 * in the range <min_id> to <max_id>
511 * In the following, exact-match semantics (i.e. the number of vlan tags on the
512 * packet must match the number of tags in the configuration) are used only if
513 * the keyword exact-match is present. Non-exact match is the default.
515 * <intf-name> <id> dot1q <outer_id> [exact-match]
516 * - a subinterface with the name <intf-name>.<id>. The subinterface
517 * is a single dot1q vlan with vlan id <outer_id>.
519 * <intf-name> <id> dot1q any [exact-match]
520 * - a subinterface with the name <intf-name>.<id>. The subinterface
521 * is a single dot1q vlan with any vlan id.
523 * <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
524 * - a subinterface with the name <intf-name>.<id>. The subinterface
525 * is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
528 * <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
529 * - a subinterface with the name <intf-name>.<id>. The subinterface
530 * is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
532 * <intf-name> <id> dot1q any inner-dot1q any [exact-match]
534 * - a subinterface with the name <intf-name>.<id>. The subinterface
535 * is a double dot1q vlan with any outer vlan id and any inner vlan id.
537 * For each of the above CLI, there is a duplicate that uses the keyword
538 * "dot1ad" in place of the first "dot1q". These interfaces use ethertype
539 * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
540 * tagged packets the inner ethertype is always 0x8100. Also note that
541 * the dot1q and dot1ad naming spaces are independent, so it is legal to
542 * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
544 * <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
545 * - a subinterface with the name <intf-name>.<id>. The subinterface
546 * is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
549 * <intf-name> <id> untagged
550 * - a subinterface with the name <intf-name>.<id>. The subinterface
551 * has no vlan tags. Only one can be specified per interface.
553 * <intf-name> <id> default
554 * - a subinterface with the name <intf-name>.<id>. This is associated
555 * with a packet that did not match any other configured subinterface
556 * on this interface. Only one can be specified per interface.
559 static clib_error_t *
560 parse_vlan_sub_interfaces (unformat_input_t * input,
561 vnet_sw_interface_t * template)
563 clib_error_t *error = 0;
564 u32 inner_vlan, outer_vlan;
566 if (unformat (input, "any inner-dot1q any"))
568 template->sub.eth.flags.two_tags = 1;
569 template->sub.eth.flags.outer_vlan_id_any = 1;
570 template->sub.eth.flags.inner_vlan_id_any = 1;
572 else if (unformat (input, "any"))
574 template->sub.eth.flags.one_tag = 1;
575 template->sub.eth.flags.outer_vlan_id_any = 1;
577 else if (unformat (input, "%d inner-dot1q any", &outer_vlan))
579 template->sub.eth.flags.two_tags = 1;
580 template->sub.eth.flags.inner_vlan_id_any = 1;
581 template->sub.eth.outer_vlan_id = outer_vlan;
583 else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan))
585 template->sub.eth.flags.two_tags = 1;
586 template->sub.eth.outer_vlan_id = outer_vlan;
587 template->sub.eth.inner_vlan_id = inner_vlan;
589 else if (unformat (input, "%d", &outer_vlan))
591 template->sub.eth.flags.one_tag = 1;
592 template->sub.eth.outer_vlan_id = outer_vlan;
596 error = clib_error_return (0, "expected dot1q config, got `%U'",
597 format_unformat_error, input);
601 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
603 if (unformat (input, "exact-match"))
605 template->sub.eth.flags.exact_match = 1;
613 static clib_error_t *
614 create_sub_interfaces (vlib_main_t * vm,
615 unformat_input_t * input, vlib_cli_command_t * cmd)
617 vnet_main_t *vnm = vnet_get_main ();
618 clib_error_t *error = 0;
619 u32 hw_if_index, sw_if_index;
620 vnet_hw_interface_t *hi;
621 u32 id, id_min, id_max;
622 vnet_sw_interface_t template;
625 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
627 error = clib_error_return (0, "unknown interface `%U'",
628 format_unformat_error, input);
632 memset (&template, 0, sizeof (template));
633 template.sub.eth.raw_flags = 0;
635 if (unformat (input, "%d default", &id_min))
638 template.sub.eth.flags.default_sub = 1;
640 else if (unformat (input, "%d untagged", &id_min))
643 template.sub.eth.flags.no_tags = 1;
644 template.sub.eth.flags.exact_match = 1;
646 else if (unformat (input, "%d dot1q", &id_min))
648 /* parse dot1q config */
650 error = parse_vlan_sub_interfaces (input, &template);
654 else if (unformat (input, "%d dot1ad", &id_min))
656 /* parse dot1ad config */
658 template.sub.eth.flags.dot1ad = 1;
659 error = parse_vlan_sub_interfaces (input, &template);
663 else if (unformat (input, "%d-%d", &id_min, &id_max))
665 template.sub.eth.flags.one_tag = 1;
666 template.sub.eth.flags.exact_match = 1;
670 else if (unformat (input, "%d", &id_min))
673 template.sub.eth.flags.one_tag = 1;
674 template.sub.eth.outer_vlan_id = id_min;
675 template.sub.eth.flags.exact_match = 1;
680 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
681 format_unformat_error, input);
685 hi = vnet_get_hw_interface (vnm, hw_if_index);
687 if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE)
690 clib_error_return (0,
691 "not allowed as %v belong to a BondEthernet interface",
696 for (id = id_min; id <= id_max; id++)
699 vnet_interface_main_t *im = &vnm->interface_main;
700 u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id;
703 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
707 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
708 hi->sw_if_index, id);
712 kp = clib_mem_alloc (sizeof (*kp));
713 *kp = sup_and_sub_key;
715 template.type = VNET_SW_INTERFACE_TYPE_SUB;
716 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
717 template.sup_sw_if_index = hi->sw_if_index;
718 template.sub.id = id;
720 template.sub.eth.outer_vlan_id = id;
722 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
726 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
727 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
728 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
729 vnet_get_main (), sw_if_index);
738 * Create vlan subinterfaces
741 * @cliexstart{create sub-interfaces}
743 * To create a vlan subinterface 11 to process packets on 802.1q VLAN id 11, use:
745 * vpp# create sub GigabitEthernet2/0/0 11
747 * This shorthand is equivalent to:
748 * vpp# create sub GigabitEthernet2/0/0 11 dot1q 11 exact-match
750 * You can specify a subinterface number that is different from the vlan id:
751 * vpp# create sub GigabitEthernet2/0/0 11 dot1q 100
753 * You can create qinq and q-in-any interfaces:
754 * vpp# create sub GigabitEthernet2/0/0 11 dot1q 100 inner-dot1q 200
755 * vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q any
757 * You can also create dot1ad interfaces:
758 * vpp# create sub GigabitEthernet2/0/0 11 dot1ad 11
759 * vpp# create sub GigabitEthernet2/0/0 12 dot1q 100 inner-dot1q 200
761 * Subinterfaces can be configured as either exact-match or non-exact match.
762 * Non-exact match is the CLI default. If exact-match is specified,
763 * packets must have the same number of vlan tags as the configuration.
764 * For non-exact-match, packets must at least that number of tags.
765 * L3 (routed) interfaces must be configured as exact-match.
766 * L2 interfaces are typically configured as non-exact-match.
768 * For example, a packet with outer vlan 100 and inner 200 would match this interface:
769 * vpp# create sub GigabitEthernet2/0/0 5 dot1q 100
771 * but would not match this interface:
772 * vpp# create sub GigabitEthernet2/0/0 5 dot1q 100 exact-match
774 * There are two special subinterfaces that can be configured. Subinterface untagged has no vlan tags:
775 * vpp# create sub GigabitEthernet2/0/0 5 untagged
777 * The subinterface default matches any packet that does not match any other subinterface:
778 * vpp# create sub GigabitEthernet2/0/0 7 default
781 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
782 .path = "create sub-interfaces",
783 .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
784 .function = create_sub_interfaces,
788 static clib_error_t *
789 set_state (vlib_main_t * vm,
790 unformat_input_t * input, vlib_cli_command_t * cmd)
792 vnet_main_t *vnm = vnet_get_main ();
794 u32 sw_if_index, flags;
797 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
799 error = clib_error_return (0, "unknown interface `%U'",
800 format_unformat_error, input);
804 if (!unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
806 error = clib_error_return (0, "unknown flags `%U'",
807 format_unformat_error, input);
811 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
822 * Interface admin up/down
825 * @cliexstart{set interface state}
826 * vpp# set interface state GigabitEthernet2/0/0 up
827 * vpp# set interface state GigabitEthernet2/0/0 down
830 VLIB_CLI_COMMAND (set_state_command, static) = {
831 .path = "set interface state",
832 .short_help = "set interface state <if-name> [up|down|punt|enable]",
833 .function = set_state,
837 static clib_error_t *
838 set_unnumbered (vlib_main_t * vm,
839 unformat_input_t * input, vlib_cli_command_t * cmd)
841 vnet_main_t *vnm = vnet_get_main ();
842 u32 unnumbered_sw_if_index;
843 u32 inherit_from_sw_if_index;
844 vnet_sw_interface_t *si;
849 if (unformat (input, "%U use %U",
850 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
851 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
853 else if (unformat (input, "del %U",
854 unformat_vnet_sw_interface, vnm,
855 &unnumbered_sw_if_index))
858 return clib_error_return (0, "parse error '%U'",
859 format_unformat_error, input);
861 si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
862 was_unnum = (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED);
866 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
867 si->unnumbered_sw_if_index = (u32) ~ 0;
869 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
870 [unnumbered_sw_if_index] = ~0;
871 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
872 [unnumbered_sw_if_index] = ~0;
876 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
877 si->unnumbered_sw_if_index = inherit_from_sw_if_index;
879 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
880 [unnumbered_sw_if_index] =
881 ip4_main.lookup_main.if_address_pool_index_by_sw_if_index
882 [inherit_from_sw_if_index];
883 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
884 [unnumbered_sw_if_index] =
885 ip6_main.lookup_main.if_address_pool_index_by_sw_if_index
886 [inherit_from_sw_if_index];
889 if (was_unnum != (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
891 ip4_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
892 ip6_sw_interface_enable_disable (unnumbered_sw_if_index, !is_del);
899 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
900 .path = "set interface unnumbered",
901 .short_help = "set interface unnumbered [<intfc> use <intfc> | del <intfc>]",
902 .function = set_unnumbered,
908 static clib_error_t *
909 set_hw_class (vlib_main_t * vm,
910 unformat_input_t * input, vlib_cli_command_t * cmd)
912 vnet_main_t *vnm = vnet_get_main ();
913 vnet_interface_main_t *im = &vnm->interface_main;
915 u32 hw_if_index, hw_class_index;
918 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
920 error = clib_error_return (0, "unknown hardware interface `%U'",
921 format_unformat_error, input);
925 if (!unformat_user (input, unformat_hash_string,
926 im->hw_interface_class_by_name, &hw_class_index))
928 error = clib_error_return (0, "unknown hardware class `%U'",
929 format_unformat_error, input);
933 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
942 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
943 .path = "set interface hw-class",
944 .short_help = "Set interface hardware class",
945 .function = set_hw_class,
949 static clib_error_t *
950 vnet_interface_cli_init (vlib_main_t * vm)
955 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
957 static clib_error_t *
958 renumber_interface_command_fn (vlib_main_t * vm,
959 unformat_input_t * input,
960 vlib_cli_command_t * cmd)
963 u32 new_dev_instance;
964 vnet_main_t *vnm = vnet_get_main ();
967 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
968 return clib_error_return (0, "unknown hardware interface `%U'",
969 format_unformat_error, input);
971 if (!unformat (input, "%d", &new_dev_instance))
972 return clib_error_return (0, "new dev instance missing");
974 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
982 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
992 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
993 .path = "renumber interface",
994 .short_help = "renumber interface <if-name> <new-dev-instance>",
995 .function = renumber_interface_command_fn,
999 static clib_error_t *
1000 promiscuous_cmd (vlib_main_t * vm,
1001 unformat_input_t * input, vlib_cli_command_t * cmd)
1003 vnet_main_t *vnm = vnet_get_main ();
1005 u32 flags = ETHERNET_INTERFACE_FLAG_ACCEPT_ALL;
1006 ethernet_main_t *em = ðernet_main;
1007 ethernet_interface_t *eif;
1009 if (unformat (input, "on %U",
1010 unformat_vnet_hw_interface, vnm, &hw_if_index))
1012 else if (unformat (input, "off %U",
1013 unformat_ethernet_interface, vnm, &hw_if_index))
1016 return clib_error_return (0, "unknown input `%U'",
1017 format_unformat_error, input);
1019 eif = ethernet_get_interface (em, hw_if_index);
1021 return clib_error_return (0, "not supported");
1023 ethernet_set_flags (vnm, hw_if_index, flags);
1028 VLIB_CLI_COMMAND (set_interface_promiscuous_cmd, static) = {
1029 .path = "set interface promiscuous",
1030 .short_help = "set interface promiscuous [on | off] <intfc>",
1031 .function = promiscuous_cmd,
1035 static clib_error_t *
1036 mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1038 vnet_main_t *vnm = vnet_get_main ();
1039 u32 hw_if_index, mtu;
1040 u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
1041 ethernet_main_t *em = ðernet_main;
1043 if (unformat (input, "%d %U", &mtu,
1044 unformat_vnet_hw_interface, vnm, &hw_if_index))
1046 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
1047 ethernet_interface_t *eif = ethernet_get_interface (em, hw_if_index);
1050 return clib_error_return (0, "not supported");
1052 if (mtu < hi->min_supported_packet_bytes)
1053 return clib_error_return (0, "Invalid mtu (%d): "
1054 "must be >= min pkt bytes (%d)", mtu,
1055 hi->min_supported_packet_bytes);
1057 if (mtu > hi->max_supported_packet_bytes)
1058 return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
1059 hi->max_supported_packet_bytes);
1061 if (hi->max_packet_bytes != mtu)
1063 hi->max_packet_bytes = mtu;
1064 ethernet_set_flags (vnm, hw_if_index, flags);
1068 return clib_error_return (0, "unknown input `%U'",
1069 format_unformat_error, input);
1074 VLIB_CLI_COMMAND (set_interface_mtu_cmd, static) = {
1075 .path = "set interface mtu",
1076 .short_help = "set interface mtu <value> <intfc>",
1077 .function = mtu_cmd,
1081 static clib_error_t *
1082 set_interface_mac_address (vlib_main_t * vm, unformat_input_t * input,
1083 vlib_cli_command_t * cmd)
1085 vnet_main_t *vnm = vnet_get_main ();
1086 clib_error_t *error = 0;
1087 u32 sw_if_index = ~0;
1090 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
1092 error = clib_error_return (0, "unknown interface `%U'",
1093 format_unformat_error, input);
1096 if (!unformat_user (input, unformat_ethernet_address, &mac))
1098 error = clib_error_return (0, "expected mac address `%U'",
1099 format_unformat_error, input);
1102 error = vnet_hw_interface_change_mac_address (vnm, sw_if_index, mac);
1108 * The '<em>set interface mac address </em>' command allows to set MAC address of given interface.
1109 * In case of NIC interfaces the one has to support MAC address change. A side effect of MAC address
1110 * change are changes of MAC addresses in FIB tables (ipv4 and ipv6).
1114 * Example of how to change MAC Address of interface:
1115 * @cliexcmd{set interface mac address GigabitEthernet0/8/0 aa:bb:cc:dd:ee:01}
1116 * @cliexcmd{set interface mac address host-vpp0 aa:bb:cc:dd:ee:02}
1117 * @cliexcmd{set interface mac address tap-0 aa:bb:cc:dd:ee:03}
1118 * @cliexcmd{set interface mac address pg0 aa:bb:cc:dd:ee:04}
1122 VLIB_CLI_COMMAND (set_interface_mac_address_cmd, static) = {
1123 .path = "set interface mac address",
1124 .short_help = "set interface mac address <intfc> <mac-address>",
1125 .function = set_interface_mac_address,
1129 static clib_error_t *
1130 set_tag (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1132 vnet_main_t *vnm = vnet_get_main ();
1133 u32 sw_if_index = ~0;
1136 if (!unformat (input, "%U %s", unformat_vnet_sw_interface,
1137 vnm, &sw_if_index, &tag))
1138 return clib_error_return (0, "unknown input `%U'",
1139 format_unformat_error, input);
1141 vnet_set_sw_interface_tag (vnm, tag, sw_if_index);
1147 VLIB_CLI_COMMAND (set_tag_command, static) = {
1148 .path = "set interface tag",
1149 .short_help = "set interface tag <intfc> <tag>",
1150 .function = set_tag,
1154 static clib_error_t *
1155 clear_tag (vlib_main_t * vm, unformat_input_t * input,
1156 vlib_cli_command_t * cmd)
1158 vnet_main_t *vnm = vnet_get_main ();
1159 u32 sw_if_index = ~0;
1161 if (!unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1162 return clib_error_return (0, "unknown input `%U'",
1163 format_unformat_error, input);
1165 vnet_clear_sw_interface_tag (vnm, sw_if_index);
1171 VLIB_CLI_COMMAND (clear_tag_command, static) = {
1172 .path = "clear interface tag",
1173 .short_help = "clear interface tag <intfc>",
1174 .function = clear_tag,
1178 static clib_error_t *
1179 set_hw_interface_rx_mode (vnet_main_t * vnm, u32 hw_if_index,
1180 u32 queue_id, vnet_hw_interface_rx_mode mode)
1182 vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
1183 vnet_device_class_t *dev_class =
1184 vnet_get_device_class (vnm, hw->dev_class_index);
1185 clib_error_t *error;
1186 vnet_hw_interface_rx_mode old_mode;
1189 if (mode == VNET_HW_INTERFACE_RX_MODE_DEFAULT)
1190 mode = hw->default_rx_mode;
1192 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &old_mode);
1196 if (old_mode == mode)
1197 return 0; /* same rx-mode, no change */
1199 case VNET_API_ERROR_INVALID_INTERFACE:
1200 return clib_error_return (0, "invalid interface");
1202 return clib_error_return (0, "unknown error");
1205 if (dev_class->rx_mode_change_function)
1207 error = dev_class->rx_mode_change_function (vnm, hw_if_index, queue_id,
1213 rv = vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1218 case VNET_API_ERROR_UNSUPPORTED:
1219 return clib_error_return (0, "unsupported");
1220 case VNET_API_ERROR_INVALID_INTERFACE:
1221 return clib_error_return (0, "invalid interface");
1223 return clib_error_return (0, "unknown error");
1229 static clib_error_t *
1230 set_interface_rx_mode (vlib_main_t * vm, unformat_input_t * input,
1231 vlib_cli_command_t * cmd)
1233 clib_error_t *error = 0;
1234 unformat_input_t _line_input, *line_input = &_line_input;
1235 vnet_main_t *vnm = vnet_get_main ();
1236 vnet_hw_interface_t *hw;
1237 u32 hw_if_index = (u32) ~ 0;
1238 u32 queue_id = (u32) ~ 0;
1239 vnet_hw_interface_rx_mode mode = VNET_HW_INTERFACE_RX_MODE_UNKNOWN;
1242 if (!unformat_user (input, unformat_line_input, line_input))
1245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1248 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1250 else if (unformat (line_input, "queue %d", &queue_id))
1252 else if (unformat (line_input, "polling"))
1253 mode = VNET_HW_INTERFACE_RX_MODE_POLLING;
1254 else if (unformat (line_input, "interrupt"))
1255 mode = VNET_HW_INTERFACE_RX_MODE_INTERRUPT;
1256 else if (unformat (line_input, "adaptive"))
1257 mode = VNET_HW_INTERFACE_RX_MODE_ADAPTIVE;
1260 error = clib_error_return (0, "parse error: '%U'",
1261 format_unformat_error, line_input);
1262 unformat_free (line_input);
1267 unformat_free (line_input);
1269 if (hw_if_index == (u32) ~ 0)
1270 return clib_error_return (0, "please specify valid interface name");
1272 if (mode == VNET_HW_INTERFACE_RX_MODE_UNKNOWN)
1273 return clib_error_return (0, "please specify valid rx-mode");
1275 hw = vnet_get_hw_interface (vnm, hw_if_index);
1279 for (i = 0; i < vec_len (hw->dq_runtime_index_by_queue); i++)
1281 error = set_hw_interface_rx_mode (vnm, hw_if_index, i, mode);
1285 hw->default_rx_mode = mode;
1288 error = set_hw_interface_rx_mode (vnm, hw_if_index, queue_id, mode);
1294 * This command is used to assign a given interface, and optionally a
1295 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1299 * Example of how to display the interface placement:
1300 * @cliexstart{show interface rx-placement}
1301 * Thread 1 (vpp_wk_0):
1302 * GigabitEthernet0/8/0 queue 0
1303 * GigabitEthernet0/9/0 queue 0
1304 * Thread 2 (vpp_wk_1):
1305 * GigabitEthernet0/8/0 queue 1
1306 * GigabitEthernet0/9/0 queue 1
1308 * Example of how to assign a interface and queue to a thread:
1309 * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
1312 VLIB_CLI_COMMAND (cmd_set_if_rx_mode,static) = {
1313 .path = "set interface rx-mode",
1314 .short_help = "set interface rx-mode <interface> [queue <n>] [polling | interrupt | adaptive]",
1315 .function = set_interface_rx_mode,
1319 static clib_error_t *
1320 show_interface_rx_placement_fn (vlib_main_t * vm, unformat_input_t * input,
1321 vlib_cli_command_t * cmd)
1324 vnet_main_t *vnm = vnet_get_main ();
1325 vnet_device_input_runtime_t *rt;
1326 vnet_device_and_queue_t *dq;
1327 vlib_node_t *pn = vlib_get_node_by_name (vm, (u8 *) "device-input");
1332 foreach_vlib_main (({
1333 clib_bitmap_foreach (si, pn->sibling_bitmap,
1335 rt = vlib_node_get_runtime_data (this_vlib_main, si);
1337 if (vec_len (rt->devices_and_queues))
1338 s = format (s, " node %U:\n", format_vlib_node_name, vm, si);
1340 vec_foreach (dq, rt->devices_and_queues)
1342 vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm,
1344 s = format (s, " %U queue %u (%U)\n",
1345 format_vnet_sw_if_index_name, vnm, hi->sw_if_index,
1347 format_vnet_hw_interface_rx_mode, dq->mode);
1350 if (vec_len (s) > 0)
1352 vlib_cli_output(vm, "Thread %u (%v):\n%v", index,
1353 vlib_worker_threads[index].name, s);
1354 vec_reset_length (s);
1365 VLIB_CLI_COMMAND (show_interface_rx_placement, static) = {
1366 .path = "show interface rx-placement",
1367 .short_help = "show interface rx-placement",
1368 .function = show_interface_rx_placement_fn,
1372 static clib_error_t *
1373 set_interface_rx_placement (vlib_main_t * vm, unformat_input_t * input,
1374 vlib_cli_command_t * cmd)
1376 clib_error_t *error = 0;
1377 unformat_input_t _line_input, *line_input = &_line_input;
1378 vnet_main_t *vnm = vnet_get_main ();
1379 vnet_device_main_t *vdm = &vnet_device_main;
1380 vnet_hw_interface_rx_mode mode;
1381 u32 hw_if_index = (u32) ~ 0;
1382 u32 queue_id = (u32) 0;
1383 u32 thread_index = (u32) ~ 0;
1386 if (!unformat_user (input, unformat_line_input, line_input))
1389 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1392 (line_input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
1394 else if (unformat (line_input, "queue %d", &queue_id))
1396 else if (unformat (line_input, "main", &thread_index))
1398 else if (unformat (line_input, "worker %d", &thread_index))
1399 thread_index += vdm->first_worker_thread_index;
1402 error = clib_error_return (0, "parse error: '%U'",
1403 format_unformat_error, line_input);
1404 unformat_free (line_input);
1409 unformat_free (line_input);
1411 if (hw_if_index == (u32) ~ 0)
1412 return clib_error_return (0, "please specify valid interface name");
1414 if (thread_index > vdm->last_worker_thread_index)
1415 return clib_error_return (0,
1416 "please specify valid worker thread or main");
1418 rv = vnet_hw_interface_get_rx_mode (vnm, hw_if_index, queue_id, &mode);
1421 return clib_error_return (0, "not found");
1423 rv = vnet_hw_interface_unassign_rx_thread (vnm, hw_if_index, queue_id);
1426 return clib_error_return (0, "not found");
1428 vnet_hw_interface_assign_rx_thread (vnm, hw_if_index, queue_id,
1430 vnet_hw_interface_set_rx_mode (vnm, hw_if_index, queue_id, mode);
1436 * This command is used to assign a given interface, and optionally a
1437 * given queue, to a different thread. If the '<em>queue</em>' is not provided,
1441 * Example of how to display the interface placement:
1442 * @cliexstart{show interface placement}
1443 * Thread 1 (vpp_wk_0):
1444 * GigabitEthernet0/8/0 queue 0
1445 * GigabitEthernet0/9/0 queue 0
1446 * Thread 2 (vpp_wk_1):
1447 * GigabitEthernet0/8/0 queue 1
1448 * GigabitEthernet0/9/0 queue 1
1450 * Example of how to assign a interface and queue to a thread:
1451 * @cliexcmd{set interface placement GigabitEthernet0/8/0 queue 1 thread 1}
1454 VLIB_CLI_COMMAND (cmd_set_if_rx_placement,static) = {
1455 .path = "set interface rx-placement",
1456 .short_help = "set interface rx-placement <hw-interface> [queue <n>] "
1457 "[worker <n> | main]",
1458 .function = set_interface_rx_placement,
1464 * fd.io coding-style-patch-verification: ON
1467 * eval: (c-set-style "gnu")