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. */
74 else if (unformat (input, "verbose"))
77 else if (unformat (input, "detail"))
80 else if (unformat (input, "brief"))
85 error = clib_error_return (0, "unknown input `%U'",
86 format_unformat_error, input);
91 /* Gather interfaces. */
92 if (vec_len (hw_if_indices) == 0)
93 pool_foreach (hi, im->hw_interfaces,
94 vec_add1 (hw_if_indices, hi - im->hw_interfaces));
99 vec_sort_with_function (hw_if_indices, compare_interface_names);
101 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose);
102 for (i = 0; i < vec_len (hw_if_indices); i++)
104 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
105 vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, hi, verbose);
110 for (i = 0; i < vec_len (hw_if_indices); i++)
112 vnet_device_class_t * dc;
114 hi = vnet_get_hw_interface (vnm, hw_if_indices[i]);
115 dc = vec_elt_at_index (im->device_classes, hi->dev_class_index);
117 if (dc->clear_counters)
118 dc->clear_counters (hi->dev_instance);
123 vec_free (hw_if_indices);
127 VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = {
128 .path = "show hardware-interfaces",
129 .short_help = "show hardware-interfaces [verbose|brief] [<if-name1> <if-name2> ...]",
130 .function = show_or_clear_hw_interfaces,
133 VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = {
134 .path = "clear hardware-interfaces",
135 .short_help = "Clear hardware interfaces statistics",
136 .function = show_or_clear_hw_interfaces,
139 static int sw_interface_name_compare (void *a1, void *a2)
141 vnet_sw_interface_t *si1 = a1;
142 vnet_sw_interface_t *si2 = a2;
144 return vnet_sw_interface_compare (vnet_get_main(),
145 si1->sw_if_index, si2->sw_if_index);
148 static clib_error_t *
149 show_sw_interfaces (vlib_main_t * vm,
150 unformat_input_t * input,
151 vlib_cli_command_t * cmd)
153 clib_error_t * error = 0;
154 vnet_main_t * vnm = vnet_get_main();
155 vnet_interface_main_t * im = &vnm->interface_main;
156 vnet_sw_interface_t * si, * sorted_sis = 0;
157 u8 show_addresses = 0;
159 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
163 /* See if user wants to show specific interface */
164 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
166 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
167 vec_add1 (sorted_sis, si[0]);
170 else if (unformat (input, "address") || unformat (input, "addr"))
175 error = clib_error_return (0, "unknown input `%U'",
176 format_unformat_error, input);
182 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0);
184 if (vec_len (sorted_sis) == 0) /* Get all interfaces */
186 /* Gather interfaces. */
187 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
188 _vec_len (sorted_sis) = 0;
189 pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sorted_sis, si[0]); }));
192 vec_sort_with_function (sorted_sis, sw_interface_name_compare);
197 vec_foreach (si, sorted_sis)
199 l2input_main_t * l2m = &l2input_main;
200 ip4_main_t * im4 = &ip4_main;
201 ip6_main_t * im6 = &ip6_main;
202 ip_lookup_main_t * lm4 = &im4->lookup_main;
203 ip_lookup_main_t * lm6 = &im6->lookup_main;
204 ip_interface_address_t * ia = 0;
207 u32 fib_index4 = 0, fib_index6 = 0;
210 l2_input_config_t * config;
212 if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index)
213 fib_index4 = vec_elt (im4->fib_index_by_sw_if_index,
216 if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index)
217 fib_index6 = vec_elt (im6->fib_index_by_sw_if_index,
220 fib4 = vec_elt_at_index (im4->fibs, fib_index4);
221 fib6 = vec_elt_at_index (im6->fibs, fib_index6);
223 if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
225 (vm, "%U (%s): \n unnumbered, use %U",
226 format_vnet_sw_if_index_name,
227 vnm, si->sw_if_index,
228 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn",
229 format_vnet_sw_if_index_name,
230 vnm, si->unnumbered_sw_if_index);
234 vlib_cli_output (vm, "%U (%s):",
235 format_vnet_sw_if_index_name,
236 vnm, si->sw_if_index,
237 (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
241 /* Display any L2 addressing info */
242 vec_validate(l2m->configs, si->sw_if_index);
243 config = vec_elt_at_index(l2m->configs, si->sw_if_index);
246 u32 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
247 vlib_cli_output (vm, " l2 bridge bd_id %d%s%d", bd_id,
248 config->bvi ? " bvi shg " : " shg ", config->shg);
250 else if (config->xconnect)
252 vlib_cli_output (vm, " l2 xconnect %U",
253 format_vnet_sw_if_index_name,
254 vnm, config->output_sw_if_index);
257 /* Display any IP4 addressing info */
258 foreach_ip_interface_address (lm4, ia, si->sw_if_index,
259 1 /* honor unnumbered */,
261 r4 = ip_interface_address_get_address (lm4, ia);
264 vlib_cli_output (vm, " %U/%d table %d",
265 format_ip4_address, r4,
271 vlib_cli_output (vm, " %U/%d",
272 format_ip4_address, r4,
277 /* Display any IP6 addressing info */
278 foreach_ip_interface_address (lm6, ia, si->sw_if_index,
279 1 /* honor unnumbered */,
281 r6 = ip_interface_address_get_address (lm6, ia);
284 vlib_cli_output (vm, " %U/%d table %d",
285 format_ip6_address, r6,
291 vlib_cli_output (vm, " %U/%d",
292 format_ip6_address, r6,
300 vec_foreach (si, sorted_sis)
302 vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si);
307 vec_free (sorted_sis);
311 VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = {
312 .path = "show interfaces",
313 .short_help = "show interfaces [address|addr] [<if-name1> <if-name2> ...]",
314 .function = show_sw_interfaces,
317 /* Root of all interface commands. */
318 VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = {
320 .short_help = "Interface commands",
323 VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = {
324 .path = "set interface",
325 .short_help = "Interface commands",
328 static clib_error_t *
329 clear_interface_counters (vlib_main_t * vm,
330 unformat_input_t * input,
331 vlib_cli_command_t * cmd)
333 vnet_main_t * vnm = vnet_get_main();
334 vnet_interface_main_t * im = &vnm->interface_main;
335 vlib_simple_counter_main_t * sm;
336 vlib_combined_counter_main_t * cm;
337 static vnet_main_t ** my_vnet_mains;
338 int i, j, n_counters;
340 vec_reset_length (my_vnet_mains);
342 for (i = 0; i < vec_len (vnet_mains); i++)
345 vec_add1 (my_vnet_mains, vnet_mains[i]);
348 if (vec_len (vnet_mains) == 0)
349 vec_add1 (my_vnet_mains, vnm);
351 n_counters = vec_len (im->combined_sw_if_counters);
353 for (j = 0; j < n_counters; j++)
355 for (i = 0; i < vec_len(my_vnet_mains); i++)
357 im = &my_vnet_mains[i]->interface_main;
358 cm = im->combined_sw_if_counters + j;
359 vlib_clear_combined_counters (cm);
363 n_counters = vec_len (im->sw_if_counters);
365 for (j = 0; j < n_counters; j++)
367 for (i = 0; i < vec_len(my_vnet_mains); i++)
369 im = &my_vnet_mains[i]->interface_main;
370 sm = im->sw_if_counters + j;
371 vlib_clear_simple_counters (sm);
378 VLIB_CLI_COMMAND (clear_interface_counters_command, static) = {
379 .path = "clear interfaces",
380 .short_help = "Clear interfaces statistics",
381 .function = clear_interface_counters,
384 // The following subinterface syntax is supported. The first two are for
385 // backwards compatability:
388 // - a subinterface with the name <intf-name>.<id>. The subinterface
389 // is a single dot1q vlan with vlan id <id> and exact-match semantics.
391 // <intf-name> <min_id>-<max_id>
392 // - a set of the above subinterfaces, repeating for each id
393 // in the range <min_id> to <max_id>
395 // In the following, exact-match semantics (i.e. the number of vlan tags on the
396 // packet must match the number of tags in the configuration) are used only if
397 // the keyword exact-match is present. Non-exact match is the default.
399 // <intf-name> <id> dot1q <outer_id> [exact-match]
400 // - a subinterface with the name <intf-name>.<id>. The subinterface
401 // is a single dot1q vlan with vlan id <outer_id>.
403 // <intf-name> <id> dot1q any [exact-match]
404 // - a subinterface with the name <intf-name>.<id>. The subinterface
405 // is a single dot1q vlan with any vlan id.
407 // <intf-name> <id> dot1q <outer_id> inner-dot1q <inner_id> [exact-match]
408 // - a subinterface with the name <intf-name>.<id>. The subinterface
409 // is a double dot1q vlan with outer vlan id <outer_id> and inner vlan id
412 // <intf-name> <id> dot1q <outer_id> inner-dot1q any [exact-match]
413 // - a subinterface with the name <intf-name>.<id>. The subinterface
414 // is a double dot1q vlan with outer vlan id <id> and any inner vlan id.
416 // <intf-name> <id> dot1q any inner-dot1q any [exact-match]
418 // - a subinterface with the name <intf-name>.<id>. The subinterface
419 // is a double dot1q vlan with any outer vlan id and any inner vlan id.
421 // For each of the above CLI, there is a duplicate that uses the keyword
422 // "dot1ad" in place of the first "dot1q". These interfaces use ethertype
423 // 0x88ad in place of 0x8100 for the outer ethertype. Note that for double-
424 // tagged packets the inner ethertype is always 0x8100. Also note that
425 // the dot1q and dot1ad naming spaces are independent, so it is legal to
426 // have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example:
428 // <intf-name> <id> dot1ad <outer_id> inner-dot1q <inner_id> [exact-match]
429 // - a subinterface with the name <intf-name>.<id>. The subinterface
430 // is a double dot1ad vlan with outer vlan id <outer_id> and inner vlan
433 // <intf-name> <id> untagged
434 // - a subinterface with the name <intf-name>.<id>. The subinterface
435 // has no vlan tags. Only one can be specified per interface.
437 // <intf-name> <id> default
438 // - a subinterface with the name <intf-name>.<id>. This is associated
439 // with a packet that did not match any other configured subinterface
440 // on this interface. Only one can be specified per interface.
443 static clib_error_t *
444 parse_vlan_sub_interfaces (unformat_input_t * input,
445 vnet_sw_interface_t * template)
447 clib_error_t * error = 0;
448 u32 inner_vlan, outer_vlan;
450 if (unformat (input, "any inner-dot1q any")) {
451 template->sub.eth.flags.two_tags = 1;
452 template->sub.eth.flags.outer_vlan_id_any = 1;
453 template->sub.eth.flags.inner_vlan_id_any = 1;
454 } else if (unformat (input, "any")) {
455 template->sub.eth.flags.one_tag = 1;
456 template->sub.eth.flags.outer_vlan_id_any = 1;
457 } else if (unformat (input, "%d inner-dot1q any", &outer_vlan)) {
458 template->sub.eth.flags.two_tags = 1;
459 template->sub.eth.flags.inner_vlan_id_any = 1;
460 template->sub.eth.outer_vlan_id = outer_vlan;
461 } else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan)) {
462 template->sub.eth.flags.two_tags = 1;
463 template->sub.eth.outer_vlan_id = outer_vlan;
464 template->sub.eth.inner_vlan_id = inner_vlan;
465 } else if (unformat (input, "%d", &outer_vlan)) {
466 template->sub.eth.flags.one_tag = 1;
467 template->sub.eth.outer_vlan_id = outer_vlan;
469 error = clib_error_return (0, "expected dot1q config, got `%U'",
470 format_unformat_error, input);
474 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
475 if (unformat (input, "exact-match")) {
476 template->sub.eth.flags.exact_match = 1;
484 static clib_error_t *
485 create_sub_interfaces (vlib_main_t * vm,
486 unformat_input_t * input,
487 vlib_cli_command_t * cmd)
489 vnet_main_t * vnm = vnet_get_main();
490 clib_error_t * error = 0;
491 u32 hw_if_index, sw_if_index;
492 vnet_hw_interface_t * hi;
493 u32 id, id_min, id_max;
494 vnet_sw_interface_t template;
497 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
499 error = clib_error_return (0, "unknown interface `%U'",
500 format_unformat_error, input);
504 memset (&template, 0, sizeof (template));
505 template.sub.eth.raw_flags = 0;
507 if (unformat (input, "%d default", &id_min)) {
509 template.sub.eth.flags.default_sub = 1;
510 } else if (unformat (input, "%d untagged", &id_min)) {
512 template.sub.eth.flags.no_tags = 1;
513 template.sub.eth.flags.exact_match = 1;
514 } else if (unformat (input, "%d dot1q", &id_min)) {
515 // parse dot1q config
517 error = parse_vlan_sub_interfaces(input, &template);
518 if (error) goto done;
519 } else if (unformat (input, "%d dot1ad", &id_min)) {
520 // parse dot1ad config
522 template.sub.eth.flags.dot1ad = 1;
523 error = parse_vlan_sub_interfaces(input, &template);
524 if (error) goto done;
525 } else if (unformat (input, "%d-%d", &id_min, &id_max)) {
526 template.sub.eth.flags.one_tag = 1;
527 template.sub.eth.outer_vlan_id = id_min;
528 template.sub.eth.flags.exact_match = 1;
531 } else if (unformat (input, "%d", &id_min)) {
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 error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'",
539 format_unformat_error, input);
544 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
545 error = clib_error_return (0, "unexpected text `%U'",
546 format_unformat_error, input);
551 hi = vnet_get_hw_interface (vnm, hw_if_index);
552 for (id = id_min; id <= id_max; id++)
555 vnet_interface_main_t * im = &vnm->interface_main;
556 u64 sup_and_sub_key = ((u64)(hi->sw_if_index) << 32) |
560 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
564 clib_warning ("sup sw_if_index %d, sub id %d already exists\n",
565 hi->sw_if_index, id);
569 kp = clib_mem_alloc (sizeof (*kp));
570 *kp = sup_and_sub_key;
572 template.type = VNET_SW_INTERFACE_TYPE_SUB;
573 template.sup_sw_if_index = hi->sw_if_index;
574 template.sub.id = id;
575 error = vnet_create_sw_interface (vnm, &template, &sw_if_index);
576 if (error) goto done;
577 hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index);
578 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index);
588 VLIB_CLI_COMMAND (create_sub_interfaces_command, static) = {
589 .path = "create sub-interface",
590 .short_help = "create sub-interfaces <nn>[-<nn>] [dot1q|dot1ad|default|untagged]",
591 .function = create_sub_interfaces,
594 static clib_error_t *
595 set_state (vlib_main_t * vm,
596 unformat_input_t * input,
597 vlib_cli_command_t * cmd)
599 vnet_main_t * vnm = vnet_get_main();
600 clib_error_t * error;
601 u32 sw_if_index, flags;
604 if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
606 error = clib_error_return (0, "unknown interface `%U'",
607 format_unformat_error, input);
611 if (! unformat (input, "%U", unformat_vnet_sw_interface_flags, &flags))
613 error = clib_error_return (0, "unknown flags `%U'",
614 format_unformat_error, input);
618 error = vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
626 VLIB_CLI_COMMAND (set_state_command, static) = {
627 .path = "set interface state",
628 .short_help = "Set interface state",
629 .function = set_state,
632 static clib_error_t *
633 set_unnumbered (vlib_main_t * vm,
634 unformat_input_t * input,
635 vlib_cli_command_t * cmd)
637 vnet_main_t * vnm = vnet_get_main();
638 u32 unnumbered_sw_if_index;
639 u32 inherit_from_sw_if_index;
640 vnet_sw_interface_t * si;
644 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
647 if (unformat (input, "%U use %U",
648 unformat_vnet_sw_interface, vnm, &unnumbered_sw_if_index,
649 unformat_vnet_sw_interface, vnm, &inherit_from_sw_if_index))
651 else if (unformat (input, "del %U",
652 unformat_vnet_sw_interface,
653 vnm, &unnumbered_sw_if_index))
657 if (is_set || is_del)
660 return clib_error_return
661 (0, "parse error '%U'", format_unformat_error, input);
665 si = vnet_get_sw_interface (vnm, unnumbered_sw_if_index);
667 si->flags &= ~(VNET_SW_INTERFACE_FLAG_UNNUMBERED);
668 si->unnumbered_sw_if_index = (u32)~0;
670 si->flags |= VNET_SW_INTERFACE_FLAG_UNNUMBERED;
671 si->unnumbered_sw_if_index = inherit_from_sw_if_index;
677 VLIB_CLI_COMMAND (set_unnumbered_command, static) = {
678 .path = "set interface unnumbered",
679 .short_help = "set interface unnumbered [<intfc> use <intfc>][del <intfc>]",
680 .function = set_unnumbered,
685 static clib_error_t *
686 set_hw_class (vlib_main_t * vm,
687 unformat_input_t * input,
688 vlib_cli_command_t * cmd)
690 vnet_main_t * vnm = vnet_get_main();
691 vnet_interface_main_t * im = &vnm->interface_main;
692 clib_error_t * error;
693 u32 hw_if_index, hw_class_index;
696 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
698 error = clib_error_return (0, "unknown hardware interface `%U'",
699 format_unformat_error, input);
703 if (! unformat_user (input, unformat_hash_string,
704 im->hw_interface_class_by_name, &hw_class_index))
706 error = clib_error_return (0, "unknown hardware class `%U'",
707 format_unformat_error, input);
711 error = vnet_hw_interface_set_class (vnm, hw_if_index, hw_class_index);
719 VLIB_CLI_COMMAND (set_hw_class_command, static) = {
720 .path = "set interface hw-class",
721 .short_help = "Set interface hardware class",
722 .function = set_hw_class,
725 static clib_error_t * vnet_interface_cli_init (vlib_main_t * vm)
728 VLIB_INIT_FUNCTION (vnet_interface_cli_init);
730 static clib_error_t *
731 renumber_interface_command_fn (vlib_main_t * vm,
732 unformat_input_t * input,
733 vlib_cli_command_t * cmd)
736 u32 new_dev_instance;
737 vnet_main_t * vnm = vnet_get_main();
740 if (! unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
741 return clib_error_return (0, "unknown hardware interface `%U'",
742 format_unformat_error, input);
744 if (! unformat (input, "%d", &new_dev_instance))
745 return clib_error_return (0, "new dev instance missing");
747 rv = vnet_interface_name_renumber (hw_if_index, new_dev_instance);
755 return clib_error_return (0, "vnet_interface_name_renumber returned %d",
764 VLIB_CLI_COMMAND (renumber_interface_command, static) = {
765 .path = "renumber interface",
766 .short_help = "renumber interface <if-name> <new-dev-instance>",
767 .function = renumber_interface_command_fn,