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.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
43 static int compare_interface_names (void *a1, void *a2)
48 return vnet_hw_interface_compare (vnet_get_main(), *hi1, *hi2);
52 show_or_clear_hw_interfaces (vlib_main_t * vm,
53 unformat_input_t * input,
54 vlib_cli_command_t * cmd)
56 clib_error_t * error = 0;
57 vnet_main_t * vnm = vnet_get_main();
58 vnet_interface_main_t * im = &vnm->interface_main;
59 vnet_hw_interface_t * hi;
60 u32 hw_if_index, * hw_if_indices = 0;
61 int i, verbose = 1, is_show;
63 is_show = strstr (cmd->path, "show") != 0;
64 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
66 /* See if user wants to show a specific interface. */
67 if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
69 vec_add1 (hw_if_indices, hw_if_index);
70 /* Implies verbose. */
73 /* See if user wants to show an interface with a specific hw_if_index. */
74 else if (unformat (input, "%u", &hw_if_index))
76 vec_add1 (hw_if_indices, hw_if_index);
77 /* Implies verbose. */
81 else if (unformat (input, "verbose"))
84 else if (unformat (input, "detail"))
87 else if (unformat (input, "brief"))
92 error = clib_error_return (0, "unknown input `%U'",
93 format_unformat_error, input);
98 /* Gather interfaces. */
99 if (vec_len (hw_if_indices) == 0)
100 pool_foreach (hi, im->hw_interfaces,
101 vec_add1 (hw_if_indices, hi - im->hw_interfaces));
106 vec_sort_with_function (hw_if_indices, compare_interface_names);
108 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
109 for (i = 0; i < vec_len (hw_if_indices); i++)
111 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
112 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, hi, verbose);
117 for (i = 0; i < vec_len (hw_if_indices); i++)
119 vnet_device_class_t * dc;
121 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
122 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
124 if (dc->clear_counters)
125 dc->clear_counters (hi->dev_instance);
130 vec_free (hw_if_indices);
134 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
135 .path = "show hardware-interfaces",
136 .short_help = "show hardware-interfaces [verbose|brief] [<if-name1> <if-name2> ...]",
137 .function = show_or_clear_hw_interfaces,
140 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
141 .path = "clear hardware-interfaces",
142 .short_help = "Clear hardware interfaces statistics",
143 .function = show_or_clear_hw_interfaces,
146 static int sw_interface_name_compare (void *a1, void *a2)
148 vnet_sw_interface_t *si1 = a1;
149 vnet_sw_interface_t *si2 = a2;
151 return vnet_sw_interface_compare (vnet_get_main(),
152 si1->sw_if_index, si2->sw_if_index);
155 static clib_error_t *
156 show_sw_interfaces (vlib_main_t * vm,
157 unformat_input_t * input,
158 vlib_cli_command_t * cmd)
160 clib_error_t * error = 0;
161 vnet_main_t * vnm = vnet_get_main();
162 vnet_interface_main_t * im = &vnm->interface_main;
163 vnet_sw_interface_t * si, * sorted_sis = 0;
164 u8 show_addresses = 0;
166 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
170 /* See if user wants to show specific interface */
171 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
173 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
174 vec_add1 (sorted_sis, si[0]);
177 else if (unformat (input, "address") || unformat (input, "addr"))
182 error = clib_error_return (0, "unknown input `%U'",
183 format_unformat_error, input);
189 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
191 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
193 /* Gather interfaces. */
194 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
195 _vec_len (sorted_sis) = 0;
196 pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
199 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
204 vec_foreach (si, sorted_sis)
206 l2input_main_t * l2m = &l2input_main;
207 ip4_main_t * im4 = &ip4_main;
208 ip6_main_t * im6 = &ip6_main;
209 ip_lookup_main_t * lm4 = &im4->lookup_main;
210 ip_lookup_main_t * lm6 = &im6->lookup_main;
211 ip_interface_address_t * ia = 0;
214 u32 fib_index4 = 0, fib_index6 = 0;
217 l2_input_config_t * config;
219 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
220 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
223 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
224 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
227 fib4 = vec_elt_at_index (im4->fibs, fib_index4);
228 fib6 = vec_elt_at_index (im6->fibs, fib_index6);
230 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
232 (vm, "%U (%s): \n unnumbered, use %U",
233 format_vnet_sw_if_index_name,
234 vnm, si->sw_if_index,
235 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
236 format_vnet_sw_if_index_name,
237 vnm, si->unnumbered_sw_if_index);
241 vlib_cli_output (vm, "%U (%s):",
242 format_vnet_sw_if_index_name,
243 vnm, si->sw_if_index,
244 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
248 /* Display any L2 addressing info */
249 vec_validate(l2m->configs, si->sw_if_index);
250 config = vec_elt_at_index(l2m->configs, si->sw_if_index);
253 u32 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
254 vlib_cli_output (vm, " l2 bridge bd_id %d%s%d", bd_id,
255 config->bvi ? " bvi shg " : " shg ", config->shg);
257 else if (config->xconnect)
259 vlib_cli_output (vm, " l2 xconnect %U",
260 format_vnet_sw_if_index_name,
261 vnm, config->output_sw_if_index);
264 /* Display any IP4 addressing info */
265 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
266 1 /* honor unnumbered */,
268 r4 = ip_interface_address_get_address (lm4, ia);
271 vlib_cli_output (vm, " %U/%d table %d",
272 format_ip4_address, r4,
278 vlib_cli_output (vm, " %U/%d",
279 format_ip4_address, r4,
284 /* Display any IP6 addressing info */
285 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
286 1 /* honor unnumbered */,
288 r6 = ip_interface_address_get_address (lm6, ia);
291 vlib_cli_output (vm, " %U/%d table %d",
292 format_ip6_address, r6,
298 vlib_cli_output (vm, " %U/%d",
299 format_ip6_address, r6,
307 vec_foreach (si, sorted_sis)
309 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
314 vec_free (sorted_sis);
318 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
319 .path = "show interfaces",
320 .short_help = "show interfaces [address|addr] [<if-name1> <if-name2> ...]",
321 .function = show_sw_interfaces,
324 /* Root of all interface commands. */
325 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
327 .short_help = "Interface commands",
330 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
331 .path = "set interface",
332 .short_help = "Interface commands",
335 static clib_error_t *
336 clear_interface_counters (vlib_main_t * vm,
337 unformat_input_t * input,
338 vlib_cli_command_t * cmd)
340 vnet_main_t * vnm = vnet_get_main();
341 vnet_interface_main_t * im = &vnm->interface_main;
342 vlib_simple_counter_main_t * sm;
343 vlib_combined_counter_main_t * cm;
344 static vnet_main_t ** my_vnet_mains;
345 int i, j, n_counters;
347 vec_reset_length (my_vnet_mains);
349 for (i = 0; i < vec_len (vnet_mains); i++)
352 vec_add1 (my_vnet_mains, vnet_mains[i]);
355 if (vec_len (vnet_mains) == 0)
356 vec_add1 (my_vnet_mains, vnm);
358 n_counters = vec_len (im->combined_sw_if_counters);
360 for (j = 0; j < n_counters; j++)
362 for (i = 0; i < vec_len(my_vnet_mains); i++)
364 im = &my_vnet_mains[i]->interface_main;
365 cm = im->combined_sw_if_counters + j;
366 vlib_clear_combined_counters (cm);
370 n_counters = vec_len (im->sw_if_counters);
372 for (j = 0; j < n_counters; j++)
374 for (i = 0; i < vec_len(my_vnet_mains); i++)
376 im = &my_vnet_mains[i]->interface_main;
377 sm = im->sw_if_counters + j;
378 vlib_clear_simple_counters (sm);
385 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
386 .path = "clear interfaces",
387 .short_help = "Clear interfaces statistics",
388 .function = clear_interface_counters,
391 // The following subinterface syntax is supported. The first two are for
392 // backwards compatability:
395 // - a subinterface with the name <intf-name>.<id>. The subinterface
396 // is a single dot1q vlan with vlan id <id> and exact-match semantics.
398 // <intf-name> <min_id>-<max_id>
399 // - a set of the above subinterfaces, repeating for each id
400 // in the range <min_id> to <max_id>
402 // In the following, exact-match semantics (i.e. the number of vlan tags on the
403 // packet must match the number of tags in the configuration) are used only if
404 // the keyword exact-match is present. Non-exact match is the default.
406 // <intf-name> <id> dot1q <outer_id> [exact-match]
407 // - a subinterface with the name <intf-name>.<id>. The subinterface
408 // is a single dot1q vlan with vlan id <outer_id>.
410 // <intf-name> <id> dot1q any [exact-match]
411 // - a subinterface with the name <intf-name>.<id>. The subinterface
412 // is a single dot1q vlan with any vlan id.
414 // <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
415 // - a subinterface with the name <intf-name>.<id>. The subinterface
416 // is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
419 // <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
420 // - a subinterface with the name <intf-name>.<id>. The subinterface
421 // is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
423 // <intf-name> <id> dot1q any inner-dot1q any [exact-match]
425 // - a subinterface with the name <intf-name>.<id>. The subinterface
426 // is a double dot1q vlan with any outer vlan id and any inner vlan id.
428 // For each of the above CLI, there is a duplicate that uses the keyword
429 // "dot1ad" in place of the first "dot1q". These interfaces use ethertype
430 // 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
431 // tagged packets the inner ethertype is always 0x8100. Also note that
432 // the dot1q and dot1ad naming spaces are independent, so it is legal to
433 // have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
435 // <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
436 // - a subinterface with the name <intf-name>.<id>. The subinterface
437 // is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
440 // <intf-name> <id> untagged
441 // - a subinterface with the name <intf-name>.<id>. The subinterface
442 // has no vlan tags. Only one can be specified per interface.
444 // <intf-name> <id> default
445 // - a subinterface with the name <intf-name>.<id>. This is associated
446 // with a packet that did not match any other configured subinterface
447 // on this interface. Only one can be specified per interface.
450 static clib_error_t *
451 parse_vlan_sub_interfaces (unformat_input_t * input,
452 vnet_sw_interface_t * template)
454 clib_error_t * error = 0;
455 u32 inner_vlan, outer_vlan;
457 if (unformat (input, "any inner-dot1q any")) {
458 template->sub.eth.flags.two_tags = 1;
459 template->sub.eth.flags.outer_vlan_id_any = 1;
460 template->sub.eth.flags.inner_vlan_id_any = 1;
461 } else if (unformat (input, "any")) {
462 template->sub.eth.flags.one_tag = 1;
463 template->sub.eth.flags.outer_vlan_id_any = 1;
464 } else if (unformat (input, "%d inner-dot1q any", &outer_vlan)) {
465 template->sub.eth.flags.two_tags = 1;
466 template->sub.eth.flags.inner_vlan_id_any = 1;
467 template->sub.eth.outer_vlan_id = outer_vlan;
468 } else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan)) {
469 template->sub.eth.flags.two_tags = 1;
470 template->sub.eth.outer_vlan_id = outer_vlan;
471 template->sub.eth.inner_vlan_id = inner_vlan;
472 } else if (unformat (input, "%d", &outer_vlan)) {
473 template->sub.eth.flags.one_tag = 1;
474 template->sub.eth.outer_vlan_id = outer_vlan;
476 error = clib_error_return (0, "expected dot1q config, got `%U'",
477 format_unformat_error, input);
481 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
482 if (unformat (input, "exact-match")) {
483 template->sub.eth.flags.exact_match = 1;
491 static clib_error_t *
492 create_sub_interfaces (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 vnet_main_t * vnm = vnet_get_main();
497 clib_error_t * error = 0;
498 u32 hw_if_index, sw_if_index;
499 vnet_hw_interface_t * hi;
500 u32 id, id_min, id_max;
501 vnet_sw_interface_t template;
504 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
506 error = clib_error_return (0, "unknown interface `%U'",
507 format_unformat_error, input);
511 memset (&template, 0, sizeof (template));
512 template.sub.eth.raw_flags = 0;
514 if (unformat (input, "%d default", &id_min)) {
516 template.sub.eth.flags.default_sub = 1;
517 } else if (unformat (input, "%d untagged", &id_min)) {
519 template.sub.eth.flags.no_tags = 1;
520 template.sub.eth.flags.exact_match = 1;
521 } else if (unformat (input, "%d dot1q", &id_min)) {
522 // parse dot1q config
524 error = parse_vlan_sub_interfaces(input, &template);
525 if (error) goto done;
526 } else if (unformat (input, "%d dot1ad", &id_min)) {
527 // parse dot1ad config
529 template.sub.eth.flags.dot1ad = 1;
530 error = parse_vlan_sub_interfaces(input, &template);
531 if (error) goto done;
532 } else if (unformat (input, "%d-%d", &id_min, &id_max)) {
533 template.sub.eth.flags.one_tag = 1;
534 template.sub.eth.outer_vlan_id = id_min;
535 template.sub.eth.flags.exact_match = 1;
538 } else if (unformat (input, "%d", &id_min)) {
540 template.sub.eth.flags.one_tag = 1;
541 template.sub.eth.outer_vlan_id = id_min;
542 template.sub.eth.flags.exact_match = 1;
545 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
546 format_unformat_error, input);
551 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
552 error = clib_error_return (0, "unexpected text `%U'",
553 format_unformat_error, input);
558 hi = vnet_get_hw_interface (vnm, hw_if_index);
559 for (id = id_min; id <= id_max; id++)
562 vnet_interface_main_t * im = &vnm->interface_main;
563 u64 sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) |
567 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
571 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
572 hi->sw_if_index, id);
576 kp = clib_mem_alloc (sizeof (*kp));
577 *kp = sup_and_sub_key;
579 template.type = VNET_SW_INTERFACE_TYPE_SUB;
580 template.sup_sw_if_index = hi->sw_if_index;
581 template.sub.id = id;
582 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
583 if (error) goto done;
584 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
585 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
595 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
596 .path = "create sub-interface",
597 .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
598 .function = create_sub_interfaces,
601 static clib_error_t *
602 set_state (vlib_main_t * vm,
603 unformat_input_t * input,
604 vlib_cli_command_t * cmd)
606 vnet_main_t * vnm = vnet_get_main();
607 clib_error_t * error;
608 u32 sw_if_index, flags;
611 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
613 error = clib_error_return (0, "unknown interface `%U'",
614 format_unformat_error, input);
618 if (! unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
620 error = clib_error_return (0, "unknown flags `%U'",
621 format_unformat_error, input);
625 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
633 VLIB_CLI_COMMAND (set_state_command, static) = {
634 .path = "set interface state",
635 .short_help = "Set interface state",
636 .function = set_state,
639 static clib_error_t *
640 set_unnumbered (vlib_main_t * vm,
641 unformat_input_t * input,
642 vlib_cli_command_t * cmd)
644 vnet_main_t * vnm = vnet_get_main();
645 u32 unnumbered_sw_if_index;
646 u32 inherit_from_sw_if_index;
647 vnet_sw_interface_t * si;
651 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
654 if (unformat (input, "%U use %U",
655 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
656 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
658 else if (unformat (input, "del %U",
659 unformat_vnet_sw_interface,
660 vnm, &unnumbered_sw_if_index))
664 if (is_set || is_del)
667 return clib_error_return
668 (0, "parse error '%U'", format_unformat_error, input);
672 si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
674 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
675 si->unnumbered_sw_if_index = (u32)~0;
677 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
678 si->unnumbered_sw_if_index = inherit_from_sw_if_index;
684 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
685 .path = "set interface unnumbered",
686 .short_help = "set interface unnumbered [<intfc> use <intfc>][del <intfc>]",
687 .function = set_unnumbered,
692 static clib_error_t *
693 set_hw_class (vlib_main_t * vm,
694 unformat_input_t * input,
695 vlib_cli_command_t * cmd)
697 vnet_main_t * vnm = vnet_get_main();
698 vnet_interface_main_t * im = &vnm->interface_main;
699 clib_error_t * error;
700 u32 hw_if_index, hw_class_index;
703 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
705 error = clib_error_return (0, "unknown hardware interface `%U'",
706 format_unformat_error, input);
710 if (! unformat_user (input, unformat_hash_string,
711 im->hw_interface_class_by_name, &hw_class_index))
713 error = clib_error_return (0, "unknown hardware class `%U'",
714 format_unformat_error, input);
718 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
726 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
727 .path = "set interface hw-class",
728 .short_help = "Set interface hardware class",
729 .function = set_hw_class,
732 static clib_error_t * vnet_interface_cli_init (vlib_main_t * vm)
735 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
737 static clib_error_t *
738 renumber_interface_command_fn (vlib_main_t * vm,
739 unformat_input_t * input,
740 vlib_cli_command_t * cmd)
743 u32 new_dev_instance;
744 vnet_main_t * vnm = vnet_get_main();
747 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
748 return clib_error_return (0, "unknown hardware interface `%U'",
749 format_unformat_error, input);
751 if (! unformat (input, "%d", &new_dev_instance))
752 return clib_error_return (0, "new dev instance missing");
754 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
762 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
771 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
772 .path = "renumber interface",
773 .short_help = "renumber interface <if-name> <new-dev-instance>",
774 .function = renumber_interface_command_fn,