/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * interface_cli.c: interface CLI * * Copyright (c) 2008 Eliot Dresselhaus * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @file * @brief Interface CLI. * * Source code for several CLI interface commands. * */ #include #include #include #include #include #include #include static int compare_interface_names (void *a1, void *a2) { u32 *hi1 = a1; u32 *hi2 = a2; return vnet_hw_interface_compare (vnet_get_main (), *hi1, *hi2); } static clib_error_t * show_or_clear_hw_interfaces (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { clib_error_t *error = 0; vnet_main_t *vnm = vnet_get_main (); vnet_interface_main_t *im = &vnm->interface_main; vnet_hw_interface_t *hi; u32 hw_if_index, *hw_if_indices = 0; int i, verbose = -1, is_show, show_bond = 0; is_show = strstr (cmd->path, "show") != 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { /* See if user wants to show a specific interface. */ if (unformat (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index)) vec_add1 (hw_if_indices, hw_if_index); /* See if user wants to show an interface with a specific hw_if_index. */ else if (unformat (input, "%u", &hw_if_index)) vec_add1 (hw_if_indices, hw_if_index); else if (unformat (input, "verbose")) verbose = 1; /* this is also the default */ else if (unformat (input, "detail")) verbose = 2; else if (unformat (input, "brief")) verbose = 0; else if (unformat (input, "bond")) { show_bond = 1; if (verbose < 0) verbose = 0; /* default to brief for link bonding */ } else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, input); goto done; } } /* Gather interfaces. */ if (vec_len (hw_if_indices) == 0) pool_foreach (hi, im->hw_interfaces, vec_add1 (hw_if_indices, hi - im->hw_interfaces)); if (verbose < 0) verbose = 1; /* default to verbose (except bond) */ if (is_show) { /* Sort by name. */ vec_sort_with_function (hw_if_indices, compare_interface_names); vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, 0, verbose); for (i = 0; i < vec_len (hw_if_indices); i++) { hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); if (show_bond == 0) /* show all interfaces */ vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, hi, verbose); else if ((hi->bond_info) && (hi->bond_info != VNET_HW_INTERFACE_BOND_INFO_SLAVE)) { /* show only bonded interface and all its slave interfaces */ int hw_idx; vnet_hw_interface_t *shi; vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, hi, verbose); /* *INDENT-OFF* */ clib_bitmap_foreach (hw_idx, hi->bond_info, ({ shi = vnet_get_hw_interface(vnm, hw_idx); vlib_cli_output (vm, "%U\n", format_vnet_hw_interface, vnm, shi, verbose); })); /* *INDENT-ON* */ } } } else { for (i = 0; i < vec_len (hw_if_indices); i++) { vnet_device_class_t *dc; hi = vnet_get_hw_interface (vnm, hw_if_indices[i]); dc = vec_elt_at_index (im->device_classes, hi->dev_class_index); if (dc->clear_counters) dc->clear_counters (hi->dev_instance); } } done: vec_free (hw_if_indices); return error; } /*? * Display more detailed information about all or a list of given interfaces. * The verboseness of the output can be controlled by the following optional * parameters: * - brief: Only show name, index and state (default for bonded interfaces). * - verbose: Also display additional attributes (default for all other interfaces). * - detail: Also display all remaining attributes and extended statistics. * * To limit the output of the command to bonded interfaces and their slave * interfaces, use the 'bond' optional parameter. * * @cliexpar * Example of how to display default data for all interfaces: * @cliexstart{show hardware-interfaces} * Name Idx Link Hardware * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0 * Ethernet address ec:f4:bb:c0:bc:fc * Intel e1000 * carrier up full duplex speed 1000 mtu 9216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024 * cpu socket 0 * GigabitEthernet7/0/1 2 up GigabitEthernet7/0/1 * Ethernet address ec:f4:bb:c0:bc:fd * Intel e1000 * carrier up full duplex speed 1000 mtu 9216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024 * cpu socket 0 * VirtualEthernet0/0/0 3 up VirtualEthernet0/0/0 * Ethernet address 02:fe:a5:a9:8b:8e * VirtualEthernet0/0/1 4 up VirtualEthernet0/0/1 * Ethernet address 02:fe:c0:4e:3b:b0 * VirtualEthernet0/0/2 5 up VirtualEthernet0/0/2 * Ethernet address 02:fe:1f:73:92:81 * VirtualEthernet0/0/3 6 up VirtualEthernet0/0/3 * Ethernet address 02:fe:f2:25:c4:68 * local0 0 down local0 * local * @cliexend * Example of how to display 'verbose' data for an interface by name and * software index (where 2 is the software index): * @cliexstart{show hardware-interfaces GigabitEthernet7/0/0 2 verbose} * Name Idx Link Hardware * GigabitEthernet7/0/0 1 up GigabitEthernet7/0/0 * Ethernet address ec:f4:bb:c0:bc:fc * Intel e1000 * carrier up full duplex speed 1000 mtu 9216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024 * cpu socket 0 * GigabitEthernet7/0/1 2 down GigabitEthernet7/0/1 * Ethernet address ec:f4:bb:c0:bc:fd * Intel e1000 * carrier up full duplex speed 1000 mtu 9216 * rx queues 1, rx desc 1024, tx queues 3, tx desc 1024 * cpu socket 0 * @cliexend ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_hw_interfaces_command, static) = { .path = "show hardware-interfaces", .short_help = "show hardware-interfaces [brief|verbose|detail] [bond] " "[ [ [..]]] [ [ [..]]]", .function = show_or_clear_hw_interfaces, }; /* *INDENT-ON* */ /*? * Clear the extended statistics for all or a list of given interfaces * (statistics associated with the 'show hardware-interfaces' command). * * @cliexpar * Example of how to clear the extended statistics for all interfaces: * @cliexcmd{clear hardware-interfaces} * Example of how to clear the extended statistics for an interface by * name and software index (where 2 is the software index): * @cliexcmd{clear hardware-interfaces GigabitEthernet7/0/0 2} ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (clear_hw_interface_counters_command, static) = { .path = "clear hardware-interfaces", .short_help = "clear hardware-interfaces " "[ [ [..]]] [ [ [..]]]", .function = show_or_clear_hw_interfaces, }; /* *INDENT-ON* */ static int sw_interface_name_compare (void *a1, void *a2) { vnet_sw_interface_t *si1 = a1; vnet_sw_interface_t *si2 = a2; return vnet_sw_interface_compare (vnet_get_main (), si1->sw_if_index, si2->sw_if_index); } static clib_error_t * show_sw_interfaces (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { clib_error_t *error = 0; vnet_main_t *vnm = vnet_get_main (); unformat_input_t _linput, *linput = &_linput; vnet_interface_main_t *im = &vnm->interface_main; vnet_sw_interface_t *si, *sorted_sis = 0; u32 sw_if_index = ~(u32) 0; u8 show_addresses = 0; u8 show_features = 0; u8 show_tag = 0; int verbose = 0; /* * Get a line of input. Won't work if the user typed * "show interface" and nothing more. */ if (unformat_user (input, unformat_line_input, linput)) { while (unformat_check_input (linput) != UNFORMAT_END_OF_INPUT) { /* See if user wants to show specific interface */ if (unformat (linput, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) { si = pool_elt_at_index (im->sw_interfaces, sw_if_index); vec_add1 (sorted_sis, si[0]); } else if (unformat (linput, "address") || unformat (linput, "addr")) show_addresses = 1; else if (unformat (linput, "features") || unformat (linput, "feat")) show_features = 1; else if (unformat (linput, "tag")) show_tag = 1; else if (unformat (linput, "verbose")) verbose = 1; else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, linput); goto done; } } unformat_free (linput); } if (show_features || show_tag) { if (sw_if_index == ~(u32) 0) return clib_error_return (0, "Interface not specified..."); } if (show_features) { vnet_interface_features_show (vm, sw_if_index, verbose); l2_input_config_t *l2_input = l2input_intf_config (sw_if_index); u32 fb = l2_input->feature_bitmap; /* intf input features are masked by bridge domain */ if (l2_input->bridge) fb &= l2input_bd_config (l2_input->bd_index)->feature_bitmap; vlib_cli_output (vm, "\nl2-input:\n%U", format_l2_input_features, fb); l2_output_config_t *l2_output = l2output_intf_config (sw_if_index); vlib_cli_output (vm, "\nl2-output:"); if (l2_output->out_vtr_flag) vlib_cli_output (vm, "%10s (%s)", "VTR", "--internal--"); vlib_cli_output (vm, "%U", format_l2_output_features, l2_output->feature_bitmap); return 0; } if (show_tag) { u8 *tag; tag = vnet_get_sw_interface_tag (vnm, sw_if_index); vlib_cli_output (vm, "%U: %s", format_vnet_sw_if_index_name, vnm, sw_if_index, tag ? (char *) tag : "(none)"); return 0; } if (!show_addresses) vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, 0); if (vec_len (sorted_sis) == 0) /* Get all interfaces */ { /* Gather interfaces. */ sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces)); _vec_len (sorted_sis) = 0; /* *INDENT-OFF* */ pool_foreach (si, im->sw_interfaces, ({ int visible = vnet_swif_is_api_visible (si); if (visible) vec_add1 (sorted_sis, si[0]);} )); /* *INDENT-ON* */ /* Sort by name. */ vec_sort_with_function (sorted_sis, sw_interface_name_compare); } if (show_addresses) { vec_foreach (si, sorted_sis) { ip4_main_t *im4 = &ip4_main; ip6_main_t *im6 = &ip6_main; ip_lookup_main_t *lm4 = &im4->lookup_main; ip_lookup_main_t *lm6 = &im6->lookup_main; ip_interface_address_t *ia = 0; u32 fib_index4 = 0, fib_index6 = 0; if (vec_len (im4->fib_index_by_sw_if_index) > si->sw_if_index) fib_index4 = vec_elt (im4->fib_index_by_sw_if_index, si->sw_if_index); if (vec_len (im6->fib_index_by_sw_if_index) > si->sw_if_index) fib_index6 = vec_elt (im6->fib_index_by_sw_if_index, si->sw_if_index); ip4_fib_t *fib4 = ip4_fib_get (fib_index4); ip6_fib_t *fib6 = ip6_fib_get (fib_index6); if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED) vlib_cli_output (vm, "%U (%s): \n unnumbered, use %U", format_vnet_sw_if_index_name, vnm, si->sw_if_index, (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn", format_vnet_sw_if_index_name, vnm, si->unnumbered_sw_if_index); else vlib_cli_output (vm, "%U (%s):", format_vnet_sw_if_index_name, vnm, si->sw_if_index, (si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? "up" : "dn"); /* Display any L2 info */ l2_input_config_t *l2_input = l2input_intf_config (si->sw_if_index); if (l2_input->bridge) { bd_main_t *bdm = &bd_main; u32 bd_id = l2input_main.bd_configs[l2_input->bd_index].bd_id; vlib_cli_output (vm, " L2 bridge bd-id %d idx %d shg %d %s", bd_id, bd_find_index (bdm, bd_id), l2_input->shg, l2_input->bvi ? "bvi" : " "); } else if (l2_input->xconnect) vlib_cli_output (vm, " L2 xconnect %U", format_vnet_sw_if_index_name, vnm, l2_input->output_sw_if_index); /* *INDENT-OFF* */ /* Display any IP4 addressing info */ foreach_ip_interface_address (lm4, ia, si->sw_if_index, 1 /* honor unnumbered */, ({ ip4_address_t *r4 = ip_interface_address_get_address (lm4, ia); if (fib4->table_id) vlib_cli_output (vm, " L3 %U/%d ip4 table-id %d fib-idx %d", format_ip4_address, r4, ia->address_length, fib4->table_id, ip4_fib_index_from_table_id (fib4->table_id)); else vlib_cli_output (vm, " L3 %U/%d", format_ip4_address, r4, ia->address_length); })); /* *INDENT-ON* */ /* *INDENT-OFF* */ /* Display any IP6 addressing info */ foreach_ip_interface_address (lm6, ia, si->sw_if_index, 1 /* honor unnumbered */, ({ ip6_address_t *r6 = ip_interface_address_get_address (lm6, ia); if (fib6->table_id) vlib_cli_output (vm, " L3 %U/%d ip6 table-id %d fib-idx %d", format_ip6_address, r6, ia->address_length, fib6->table_id, ip6_fib_index_from_table_id (fib6->table_id)); else vlib_cli_output (vm, " L3 %U/%d", format_ip6_address, r6, ia->address_length); })); /* *INDENT-ON* */ } } else { vec_foreach (si, sorted_sis) { vlib_cli_output (vm, "%U\n", format_vnet_sw_interface, vnm, si); } } done: vec_free (sorted_sis); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_sw_interfaces_command, static) = { .path = "show interface", .short_help = "show interface [address|addr|features|feat] [ [ [..]]] [verbose]", .function = show_sw_interfaces, }; /* *INDENT-ON* */ /* Root of all interface commands. */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vnet_cli_interface_command, static) = { .path = "interface", .short_help = "Interface commands", }; /* *INDENT-ON* */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (vnet_cli_set_interface_command, static) = { .path = "set interface", .short_help = "Interface commands", }; /* *INDENT-ON* */ static clib_error_t * clear_interface_counters (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { vnet_main_t *vnm = vnet_get_main (); vnet_interface_main_t *im = &vnm->interface_main; vlib_simple_counter_main_t *sm; vlib_combined_counter_main_t *cm; static vnet_main_t **my_vnet_mains; int i, j, n_counters; vec_reset_length (my_vnet_mains); for (i = 0; i < vec_len (vnet_mains); i++) { if (vnet_mains[i]) vec_add1 (my_vnet_mains, vnet_mains[i]); } if (vec_len (vnet_mains) == 0) vec_add1 (my_vnet_mains, vnm); n_counters = vec_len (im->combined_sw_if_counters); for (j = 0; j < n_counters; j++) { for (i = 0; i < vec_len (my_vnet_mains); i++) { im = &my_vnet_mains[i]->interface_main; cm = im->combined_sw_if_counters + j; vlib_clear_combined_counters (cm); } } n_counters = vec_len (im->sw_if_counters); for (j = 0; j < n_counters; j++) { for (i = 0; i < vec_len (my_vnet_mains); i++) { im = &my_vnet_mains[i]->interface_main; sm = im->sw_if_counters + j; vlib_clear_simple_counters (sm); } } return 0; } /*? * Clear the statistics for all interfaces (statistics associated with the * 'show interface' command). * * @cliexpar * Example of how to clear the statistics for all interfaces: * @cliexcmd{clear interfaces} ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (clear_interface_counters_command, static) = { .path = "clear interfaces", .short_help = "clear interfaces", .function = clear_interface_counters, }; /* *INDENT-ON* */ /** * Parse subinterface names. * * The following subinterface syntax is supported. The first two are for * backwards compatability: * * * - a subinterface with the name .. The subinterface * is a single dot1q vlan with vlan id and exact-match semantics. * * - * - a set of the above subinterfaces, repeating for each id * in the range to * * In the following, exact-match semantics (i.e. the number of vlan tags on the * packet must match the number of tags in the configuration) are used only if * the keyword exact-match is present. Non-exact match is the default. * * dot1q [exact-match] * - a subinterface with the name .. The subinterface * is a single dot1q vlan with vlan id . * * dot1q any [exact-match] * - a subinterface with the name .. The subinterface * is a single dot1q vlan with any vlan id. * * dot1q inner-dot1q [exact-match] * - a subinterface with the name .. The subinterface * is a double dot1q vlan with outer vlan id and inner vlan id * . * * dot1q inner-dot1q any [exact-match] * - a subinterface with the name .. The subinterface * is a double dot1q vlan with outer vlan id and any inner vlan id. * * dot1q any inner-dot1q any [exact-match] * * - a subinterface with the name .. The subinterface * is a double dot1q vlan with any outer vlan id and any inner vlan id. * * For each of the above CLI, there is a duplicate that uses the keyword * "dot1ad" in place of the first "dot1q". These interfaces use ethertype * 0x88ad in place of 0x8100 for the outer ethertype. Note that for double- * tagged packets the inner ethertype is always 0x8100. Also note that * the dot1q and dot1ad naming spaces are independent, so it is legal to * have both "Gig3/0/0.1 dot1q 100" and "Gig3/0/0.2 dot1ad 100". For example: * * dot1ad inner-dot1q [exact-match] * - a subinterface with the name .. The subinterface * is a double dot1ad vlan with outer vlan id and inner vlan * id . * * untagged * - a subinterface with the name .. The subinterface * has no vlan tags. Only one can be specified per interface. * * default * - a subinterface with the name .. This is associated * with a packet that did not match any other configured subinterface * on this interface. Only one can be specified per interface. */ static clib_error_t * parse_vlan_sub_interfaces (unformat_input_t * input, vnet_sw_interface_t * template) { clib_error_t *error = 0; u32 inner_vlan, outer_vlan; if (unformat (input, "any inner-dot1q any")) { template->sub.eth.flags.two_tags = 1; template->sub.eth.flags.outer_vlan_id_any = 1; template->sub.eth.flags.inner_vlan_id_any = 1; } else if (unformat (input, "any")) { template->sub.eth.flags.one_tag = 1; template->sub.eth.flags.outer_vlan_id_any = 1; } else if (unformat (input, "%d inner-dot1q any", &outer_vlan)) { template->sub.eth.flags.two_tags = 1; template->sub.eth.flags.inner_vlan_id_any = 1; template->sub.eth.outer_vlan_id = outer_vlan; } else if (unformat (input, "%d inner-dot1q %d", &outer_vlan, &inner_vlan)) { template->sub.eth.flags.two_tags = 1; template->sub.eth.outer_vlan_id = outer_vlan; template->sub.eth.inner_vlan_id = inner_vlan; } else if (unformat (input, "%d", &outer_vlan)) { template->sub.eth.flags.one_tag = 1; template->sub.eth.outer_vlan_id = outer_vlan; } else { error = clib_error_return (0, "expected dot1q config, got `%U'", format_unformat_error, input); goto done; } if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "exact-match")) { template->sub.eth.flags.exact_match = 1; } } done: return error; } static clib_error_t * create_sub_interfaces (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { vnet_main_t *vnm = vnet_get_main (); clib_error_t *error = 0; u32 hw_if_index, sw_if_index; vnet_hw_interface_t *hi; u32 id, id_min, id_max; vnet_sw_interface_t template; hw_if_index = ~0; if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index)) { error = clib_error_return (0, "unknown interface `%U'", format_unformat_error, input); goto done; } memset (&template, 0, sizeof (template)); template.sub.eth.raw_flags = 0; if (unformat (input, "%d default", &id_min)) { id_max = id_min; template.sub.eth.flags.default_sub = 1; } else if (unformat (input, "%d untagged", &id_min)) { id_max = id_min; template.sub.eth.flags.no_tags = 1; template.sub.eth.flags.exact_match = 1; } else if (unformat (input, "%d dot1q", &id_min)) { /* parse dot1q config */ id_max = id_min; error = parse_vlan_sub_interfaces (input, &template); if (error) goto done; } else if (unformat (input, "%d dot1ad", &id_min)) { /* parse dot1ad config */ id_max = id_min; template.sub.eth.flags.dot1ad = 1; error = parse_vlan_sub_interfaces (input, &template); if (error) goto done; } else if (unformat (input, "%d-%d", &id_min, &id_max)) { template.sub.eth.flags.one_tag = 1; template.sub.eth.flags.exact_match = 1; if (id_min > id_max) goto id_error; } else if (unformat (input, "%d", &id_min)) { id_max = id_min; template.sub.eth.flags.one_tag = 1; template.sub.eth.outer_vlan_id = id_min; template.sub.eth.flags.exact_match = 1; } else { id_error: error = clib_error_return (0, "expected ID or ID MIN-MAX, got `%U'", format_unformat_error, input); goto done; } hi = vnet_get_hw_interface (vnm, hw_if_index); if (hi->bond_info == VNET_HW_INTERFACE_BOND_INFO_SLAVE) { error = clib_error_return (0, "not allowed as %v belong to a BondEthernet interface", hi->name); goto done; } for (id = id_min; id <= id_max; id++) { uword *p; vnet_interface_main_t *im = &vnm->interface_main; u64 sup_and_sub_key = ((u64) (hi->sw_if_index) << 32) | (u64) id; u64 *kp; p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key); if (p) { if (CLIB_DEBUG > 0) clib_warning ("sup sw_if_index %d, sub id %d already exists\n", hi->sw_if_index, id); continue; } kp = clib_mem_alloc (sizeof (*kp)); *kp = sup_and_sub_key; template.type = VNET_SW_INTERFACE_TYPE_SUB; template.flood_class = VNET_FLOOD_CLASS_NORMAL; template.sup_sw_if_index = hi->sw_if_index; template.sub.id = id; if (id_min < id_max) template.sub.eth.outer_vlan_id = id; error = vnet_create_sw_interface (vnm, &template, &sw_if_index); if (error) goto done; hash_set (hi->sub_interface_sw_if_index_by_id, id, sw_if_index); hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, sw_if_index); vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), sw_if_index); } done: return error; } /*? * This command is used to add VLAN IDs to interfaces, also known as subinterfaces. * The primary input to this command is the 'interface' and 'subId' * (subinterface Id) parameters. If no additional VLAN ID is provide, the VLAN ID is * assumed to be the 'subId'. The VLAN ID and 'subId' can be different, * but this is not recommended. * * This command has several variations: * - create sub-interfaces - Create a subinterface to * process packets with a given 802.1q VLAN ID (same value as the 'subId'). * * - create sub-interfaces default - Adding the * 'default' parameter indicates that packets with VLAN IDs that do not * match any other subinterfaces should be sent to this subinterface. * * - create sub-interfaces untagged - Adding the * 'untagged' parameter indicates that packets no VLAN IDs should be sent * to this subinterface. * * - create sub-interfaces - - Create a range of * subinterfaces to handle a range of VLAN IDs. * * - create sub-interfaces dot1q|dot1ad |any [exact-match] - * Use this command to specify the outer VLAN ID, to either be explicited or to make the * VLAN ID different from the 'subId'. * * - create sub-interfaces dot1q|dot1ad |any inner-dot1q * |any [exact-match] - Use this command to specify the outer VLAN ID and * the innner VLAN ID. * * When 'dot1q' or 'dot1ad' is explictly entered, subinterfaces * can be configured as either exact-match or non-exact match. Non-exact match is the CLI * default. If 'exact-match' is specified, packets must have the same number of * VLAN tags as the configuration. For non-exact-match, packets must at least that number * of tags. L3 (routed) interfaces must be configured as exact-match. L2 interfaces are * typically configured as non-exact-match. If 'dot1q' or 'dot1ad' is NOT * entered, then the default behavior is exact-match. * * Use the 'show interface' command to display all subinterfaces. * * @cliexp
#!/usr/bin/env python
""" Classifier-based L2 ACL Test Case HLD:
"""

import unittest
import random
import binascii
import socket


from scapy.packet import Raw
from scapy.data import ETH_P_IP
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP, UDP, ICMP
from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest
from scapy.layers.inet6 import IPv6ExtHdrFragment
from framework import VppTestCase, VppTestRunner
from util import Host, ppp


class TestClassifyAcl(VppTestCase):
    """ Classifier-based L2 input and output ACL Test Case """

    # traffic types
    IP = 0
    ICMP = 1

    # IP version
    IPRANDOM = -1
    IPV4 = 0
    IPV6 = 1

    # rule types
    DENY = 0
    PERMIT = 1

    # supported protocols
    proto = [[6, 17], [1, 58]]
    proto_map = {1: 'ICMP', 58: 'ICMPv6EchoRequest', 6: 'TCP', 17: 'UDP'}
    ICMPv4 = 0
    ICMPv6 = 1
    TCP = 0
    UDP = 1
    PROTO_ALL = 0

    # port ranges
    PORTS_ALL = -1
    PORTS_RANGE = 0
    PORTS_RANGE_2 = 1
    udp_sport_from = 10
    udp_sport_to = udp_sport_from + 5
    udp_dport_from = 20000
    udp_dport_to = udp_dport_from + 5000
    tcp_sport_from = 30
    tcp_sport_to = tcp_sport_from + 5
    tcp_dport_from = 40000
    tcp_dport_to = tcp_dport_from + 5000

    udp_sport_from_2 = 90
    udp_sport_to_2 = udp_sport_from_2 + 5
    udp_dport_from_2 = 30000
    udp_dport_to_2 = udp_dport_from_2 + 5000
    tcp_sport_from_2 = 130
    tcp_sport_to_2 = tcp_sport_from_2 + 5
    tcp_dport_from_2 = 20000
    tcp_dport_to_2 = tcp_dport_from_2 + 5000

    icmp4_type = 8  # echo request
    icmp4_code = 3
    icmp6_type = 128  # echo request
    icmp6_code = 3

    icmp4_type_2 = 8
    icmp4_code_from_2 = 5
    icmp4_code_to_2 = 20
    icmp6_type_2 = 128
    icmp6_code_from_2 = 8
    icmp6_code_to_2 = 42

    # Test variables
    bd_id = 1

    @classmethod
    def setUpClass(cls):
        """
        Perform standard class setup (defined by class method setUpClass in
        class VppTestCase) before running the test case, set test case related
        variables and configure VPP.
        """
        super(TestClassifyAcl, cls).setUpClass()

        try:
            # Create 2 pg interfaces
            cls.create_pg_interfaces(range(2))

            # Packet flows mapping pg0 -> pg1, pg2 etc.
            cls.flows = dict()
            cls.flows[cls.pg0] = [cls.pg1]

            # Packet sizes
            cls.pg_if_packet_sizes = [64, 512, 1518, 9018]

            # Create BD with MAC learning and unknown unicast flooding disabled
            # and put interfaces to this BD
            cls.vapi.bridge_domain_add_del(bd_id=cls.bd_id, uu_flood=1,
                                           learn=1)
            for pg_if in cls.pg_interfaces:
                cls.vapi.sw_interface_set_l2_bridge(pg_if.sw_if_index,
                                                    bd_id=cls.bd_id)

            # Set up all interfaces
            for i in cls.pg_interfaces:
                i.admin_up()

            # Mapping between packet-generator index and lists of test hosts
            cls.hosts_by_pg_idx = dict()
            for pg_if in cls.pg_interfaces:
                cls.hosts_by_pg_idx[pg_if.sw_if_index] = []

            # Create list of deleted hosts
            cls.deleted_hosts_by_pg_idx = dict()
            for pg_if in cls.pg_interfaces:
                cls.deleted_hosts_by_pg_idx[pg_if.sw_if_index] = []

            # warm-up the mac address tables
            # self.warmup_test()

            # Holder of the active classify table key
            cls.acl_active_table = ''

        except Exception:
            super(TestClassifyAcl, cls).tearDownClass()
            raise

    def setUp(self):
        super(TestClassifyAcl, self).setUp()

        self.acl_tbl_idx = {}
        self.reset_packet_infos()

    def tearDown(self):
        """
        Show various debug prints after each test.
        """
        if not self.vpp_dead:
            self.logger.info(self.vapi.ppcli("show inacl type l2"))
            self.logger.info(self.vapi.ppcli("show outacl type l2"))
            self.logger.info(self.vapi.ppcli("show classify tables verbose"))
            self.logger.info(self.vapi.