Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | /* |
| 16 | * ethernet_interface.c: ethernet interfaces |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 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: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 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. |
| 38 | */ |
| 39 | |
| 40 | #include <vnet/vnet.h> |
| 41 | #include <vnet/ip/ip.h> |
| 42 | #include <vnet/pg/pg.h> |
| 43 | #include <vnet/ethernet/ethernet.h> |
John Lo | 405e41b | 2016-04-23 15:14:12 -0400 | [diff] [blame] | 44 | #include <vnet/l2/l2_input.h> |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 45 | #include <vnet/l2/l2_bd.h> |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 46 | #include <vnet/adj/adj.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 47 | |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 48 | /** |
| 49 | * @file |
| 50 | * @brief Loopback Interfaces. |
| 51 | * |
| 52 | * This file contains code to manage loopback interfaces. |
| 53 | */ |
| 54 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 55 | const u8 * |
| 56 | ethernet_ip4_mcast_dst_addr (void) |
| 57 | { |
| 58 | const static u8 ethernet_mcast_dst_mac[] = { |
| 59 | 0x1, 0x0, 0x5e, 0x0, 0x0, 0x0, |
| 60 | }; |
| 61 | |
| 62 | return (ethernet_mcast_dst_mac); |
| 63 | } |
| 64 | |
| 65 | const u8 * |
| 66 | ethernet_ip6_mcast_dst_addr (void) |
| 67 | { |
| 68 | const static u8 ethernet_mcast_dst_mac[] = { |
| 69 | 0x33, 0x33, 0x00, 0x0, 0x0, 0x0, |
| 70 | }; |
| 71 | |
| 72 | return (ethernet_mcast_dst_mac); |
| 73 | } |
| 74 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 75 | /** |
| 76 | * @brief build a rewrite string to use for sending packets of type 'link_type' |
| 77 | * to 'dst_address' |
| 78 | */ |
| 79 | u8 * |
| 80 | ethernet_build_rewrite (vnet_main_t * vnm, |
| 81 | u32 sw_if_index, |
| 82 | vnet_link_t link_type, const void *dst_address) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 84 | vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index); |
| 85 | vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index); |
| 86 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 87 | ethernet_main_t *em = ðernet_main; |
| 88 | ethernet_interface_t *ei; |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 89 | ethernet_header_t *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | ethernet_type_t type; |
| 91 | uword n_bytes = sizeof (h[0]); |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 92 | u8 *rewrite = NULL; |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 93 | u8 is_p2p = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 95 | if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) || |
| 96 | (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE)) |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 97 | is_p2p = 1; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 98 | if (sub_sw != sup_sw) |
| 99 | { |
| 100 | if (sub_sw->sub.eth.flags.one_tag) |
| 101 | { |
| 102 | n_bytes += sizeof (ethernet_vlan_header_t); |
| 103 | } |
| 104 | else if (sub_sw->sub.eth.flags.two_tags) |
| 105 | { |
| 106 | n_bytes += 2 * (sizeof (ethernet_vlan_header_t)); |
| 107 | } |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 108 | else if (PREDICT_FALSE (is_p2p)) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 109 | { |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 110 | n_bytes = sizeof (ethernet_header_t); |
| 111 | } |
| 112 | if (PREDICT_FALSE (!is_p2p)) |
| 113 | { |
| 114 | // Check for encaps that are not supported for L3 interfaces |
| 115 | if (!(sub_sw->sub.eth.flags.exact_match) || |
| 116 | (sub_sw->sub.eth.flags.default_sub) || |
| 117 | (sub_sw->sub.eth.flags.outer_vlan_id_any) || |
| 118 | (sub_sw->sub.eth.flags.inner_vlan_id_any)) |
| 119 | { |
| 120 | return 0; |
| 121 | } |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | n_bytes = sizeof (ethernet_header_t); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 126 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 129 | switch (link_type) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 130 | { |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 131 | #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 132 | _(IP4, IP4); |
| 133 | _(IP6, IP6); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 134 | _(MPLS, MPLS); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 135 | _(ARP, ARP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | #undef _ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 137 | default: |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 138 | return NULL; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 139 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 141 | vec_validate (rewrite, n_bytes - 1); |
| 142 | h = (ethernet_header_t *) rewrite; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | ei = pool_elt_at_index (em->interfaces, hw->hw_instance); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 144 | clib_memcpy (h->src_address, ei->address, sizeof (h->src_address)); |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 145 | if (is_p2p) |
| 146 | { |
| 147 | clib_memcpy (h->dst_address, sub_sw->p2p.client_mac, |
| 148 | sizeof (h->dst_address)); |
| 149 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | else |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 151 | { |
| 152 | if (dst_address) |
| 153 | clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address)); |
| 154 | else |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 155 | clib_memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */ |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 156 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 158 | if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 159 | { |
| 160 | ethernet_vlan_header_t *outer = (void *) (h + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 162 | h->type = sub_sw->sub.eth.flags.dot1ad ? |
| 163 | clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : |
| 164 | clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); |
| 165 | outer->priority_cfi_and_id = |
| 166 | clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id); |
| 167 | outer->type = clib_host_to_net_u16 (type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 169 | } |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 170 | else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 171 | { |
| 172 | ethernet_vlan_header_t *outer = (void *) (h + 1); |
| 173 | ethernet_vlan_header_t *inner = (void *) (outer + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 175 | h->type = sub_sw->sub.eth.flags.dot1ad ? |
| 176 | clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) : |
| 177 | clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); |
| 178 | outer->priority_cfi_and_id = |
| 179 | clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id); |
| 180 | outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN); |
| 181 | inner->priority_cfi_and_id = |
| 182 | clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id); |
| 183 | inner->type = clib_host_to_net_u16 (type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 184 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 185 | } |
| 186 | else |
| 187 | { |
| 188 | h->type = clib_host_to_net_u16 (type); |
| 189 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 190 | |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 191 | return (rewrite); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai) |
| 196 | { |
| 197 | ip_adjacency_t *adj; |
| 198 | |
| 199 | adj = adj_get (ai); |
| 200 | |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 201 | vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index); |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 202 | if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) || |
| 203 | (si->type == VNET_SW_INTERFACE_TYPE_PIPE)) |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 204 | { |
| 205 | default_update_adjacency (vnm, sw_if_index, ai); |
| 206 | } |
| 207 | else if (FIB_PROTOCOL_IP4 == adj->ia_nh_proto) |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 208 | { |
| 209 | arp_update_adjacency (vnm, sw_if_index, ai); |
| 210 | } |
| 211 | else if (FIB_PROTOCOL_IP6 == adj->ia_nh_proto) |
| 212 | { |
| 213 | ip6_ethernet_update_adjacency (vnm, sw_if_index, ai); |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | ASSERT (0); |
| 218 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Neale Ranns | 3be6b28 | 2016-12-20 14:24:01 +0000 | [diff] [blame] | 221 | static clib_error_t * |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 222 | ethernet_mac_change (vnet_hw_interface_t * hi, |
| 223 | const u8 * old_address, const u8 * mac_address) |
Neale Ranns | 3be6b28 | 2016-12-20 14:24:01 +0000 | [diff] [blame] | 224 | { |
| 225 | ethernet_interface_t *ei; |
| 226 | ethernet_main_t *em; |
| 227 | |
| 228 | em = ðernet_main; |
| 229 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
| 230 | |
| 231 | vec_validate (hi->hw_address, |
| 232 | STRUCT_SIZE_OF (ethernet_header_t, src_address) - 1); |
| 233 | clib_memcpy (hi->hw_address, mac_address, vec_len (hi->hw_address)); |
| 234 | |
| 235 | clib_memcpy (ei->address, (u8 *) mac_address, sizeof (ei->address)); |
| 236 | ethernet_arp_change_mac (hi->sw_if_index); |
| 237 | ethernet_ndp_change_mac (hi->sw_if_index); |
| 238 | |
| 239 | return (NULL); |
| 240 | } |
| 241 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 242 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = { |
| 244 | .name = "Ethernet", |
| 245 | .format_address = format_ethernet_address, |
| 246 | .format_header = format_ethernet_header_with_length, |
| 247 | .unformat_hw_address = unformat_ethernet_address, |
| 248 | .unformat_header = unformat_ethernet_header, |
Neale Ranns | b80c536 | 2016-10-08 13:03:40 +0100 | [diff] [blame] | 249 | .build_rewrite = ethernet_build_rewrite, |
| 250 | .update_adjacency = ethernet_update_adjacency, |
Neale Ranns | 3be6b28 | 2016-12-20 14:24:01 +0000 | [diff] [blame] | 251 | .mac_addr_change_function = ethernet_mac_change, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 253 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 255 | uword |
| 256 | unformat_ethernet_interface (unformat_input_t * input, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 258 | vnet_main_t *vnm = va_arg (*args, vnet_main_t *); |
| 259 | u32 *result = va_arg (*args, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | u32 hw_if_index; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 261 | ethernet_main_t *em = ðernet_main; |
| 262 | ethernet_interface_t *eif; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 263 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 264 | if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | |
| 267 | eif = ethernet_get_interface (em, hw_if_index); |
| 268 | if (eif) |
| 269 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 270 | *result = hw_if_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | return 1; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | clib_error_t * |
| 277 | ethernet_register_interface (vnet_main_t * vnm, |
| 278 | u32 dev_class_index, |
| 279 | u32 dev_instance, |
| 280 | u8 * address, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 281 | u32 * hw_if_index_return, |
| 282 | ethernet_flag_change_function_t flag_change) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 284 | ethernet_main_t *em = ðernet_main; |
| 285 | ethernet_interface_t *ei; |
| 286 | vnet_hw_interface_t *hi; |
| 287 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | u32 hw_if_index; |
| 289 | |
| 290 | pool_get (em->interfaces, ei); |
| 291 | ei->flag_change = flag_change; |
| 292 | |
| 293 | hw_if_index = vnet_register_interface |
| 294 | (vnm, |
| 295 | dev_class_index, dev_instance, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 296 | ethernet_hw_interface_class.index, ei - em->interfaces); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 297 | *hw_if_index_return = hw_if_index; |
| 298 | |
| 299 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 300 | |
| 301 | ethernet_setup_node (vnm->vlib_main, hi->output_node_index); |
| 302 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 303 | hi->min_packet_bytes = hi->min_supported_packet_bytes = |
| 304 | ETHERNET_MIN_PACKET_BYTES; |
| 305 | hi->max_packet_bytes = hi->max_supported_packet_bytes = |
| 306 | ETHERNET_MAX_PACKET_BYTES; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | |
| 308 | /* Standard default ethernet MTU. */ |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 309 | vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000); |
Damjan Marion | fe7d4a2 | 2018-04-13 19:43:39 +0200 | [diff] [blame] | 310 | |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 311 | clib_memcpy (ei->address, address, sizeof (ei->address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | vec_add (hi->hw_address, address, sizeof (ei->address)); |
| 313 | |
| 314 | if (error) |
| 315 | { |
| 316 | pool_put (em->interfaces, ei); |
| 317 | return error; |
| 318 | } |
| 319 | return error; |
| 320 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 321 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | void |
| 323 | ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index) |
| 324 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 325 | ethernet_main_t *em = ðernet_main; |
| 326 | ethernet_interface_t *ei; |
| 327 | vnet_hw_interface_t *hi; |
| 328 | main_intf_t *main_intf; |
| 329 | vlan_table_t *vlan_table; |
| 330 | u32 idx; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | |
| 332 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 333 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
| 334 | |
| 335 | /* Delete vlan mapping table for dot1q and dot1ad. */ |
| 336 | main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 337 | if (main_intf->dot1q_vlans) |
| 338 | { |
| 339 | vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans); |
| 340 | for (idx = 0; idx < ETHERNET_N_VLAN; idx++) |
| 341 | { |
| 342 | if (vlan_table->vlans[idx].qinqs) |
| 343 | { |
| 344 | pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs); |
John Lo | d63abff | 2018-10-04 16:22:21 -0400 | [diff] [blame] | 345 | vlan_table->vlans[idx].qinqs = 0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | pool_put_index (em->vlan_pool, main_intf->dot1q_vlans); |
Steve Shin | 8a3e575 | 2018-02-20 11:38:34 -0800 | [diff] [blame] | 349 | main_intf->dot1q_vlans = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 351 | if (main_intf->dot1ad_vlans) |
| 352 | { |
| 353 | vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans); |
| 354 | for (idx = 0; idx < ETHERNET_N_VLAN; idx++) |
| 355 | { |
| 356 | if (vlan_table->vlans[idx].qinqs) |
| 357 | { |
| 358 | pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs); |
John Lo | d63abff | 2018-10-04 16:22:21 -0400 | [diff] [blame] | 359 | vlan_table->vlans[idx].qinqs = 0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans); |
Steve Shin | 8a3e575 | 2018-02-20 11:38:34 -0800 | [diff] [blame] | 363 | main_intf->dot1ad_vlans = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 364 | } |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 365 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 366 | vnet_delete_hw_interface (vnm, hw_if_index); |
| 367 | pool_put (em->interfaces, ei); |
| 368 | } |
| 369 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 370 | u32 |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 372 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 373 | ethernet_main_t *em = ðernet_main; |
| 374 | vnet_hw_interface_t *hi; |
| 375 | ethernet_interface_t *ei; |
| 376 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 377 | hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 378 | |
| 379 | ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index); |
| 380 | |
| 381 | ei = pool_elt_at_index (em->interfaces, hi->hw_instance); |
Damjan Marion | 650223c | 2018-11-14 16:55:53 +0100 | [diff] [blame] | 382 | ei->flags = flags; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | if (ei->flag_change) |
| 384 | return ei->flag_change (vnm, hi, flags); |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 385 | return (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 388 | /** |
| 389 | * Echo packets back to ethernet/l2-input. |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 390 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 391 | static uword |
| 392 | simulated_ethernet_interface_tx (vlib_main_t * vm, |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 393 | vlib_node_runtime_t * |
| 394 | node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 395 | { |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 396 | u32 n_left_from, *from; |
| 397 | u32 next_index = 0; |
| 398 | u32 n_bytes; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 399 | u32 thread_index = vm->thread_index; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 400 | vnet_main_t *vnm = vnet_get_main (); |
| 401 | vnet_interface_main_t *im = &vnm->interface_main; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 402 | l2_input_config_t *config; |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 403 | vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b; |
| 404 | u16 nexts[VLIB_FRAME_SIZE], *next; |
| 405 | u32 new_rx_sw_if_index = ~0; |
| 406 | u32 new_tx_sw_if_index = ~0; |
John Lo | 405e41b | 2016-04-23 15:14:12 -0400 | [diff] [blame] | 407 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | n_left_from = frame->n_vectors; |
Damjan Marion | a3d5986 | 2018-11-10 10:23:00 +0100 | [diff] [blame] | 409 | from = vlib_frame_vector_args (frame); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 411 | vlib_get_buffers (vm, from, bufs, n_left_from); |
| 412 | b = bufs; |
| 413 | next = nexts; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 414 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 415 | /* Ordinarily, this is the only config lookup. */ |
| 416 | config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]); |
| 417 | next_index = |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 418 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 419 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 420 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 421 | new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 422 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 423 | while (n_left_from >= 4) |
| 424 | { |
| 425 | u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3; |
| 426 | u32 not_all_match_config; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 427 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 428 | /* Prefetch next iteration. */ |
| 429 | if (PREDICT_TRUE (n_left_from >= 8)) |
| 430 | { |
| 431 | vlib_prefetch_buffer_header (b[4], STORE); |
| 432 | vlib_prefetch_buffer_header (b[5], STORE); |
| 433 | vlib_prefetch_buffer_header (b[6], STORE); |
| 434 | vlib_prefetch_buffer_header (b[7], STORE); |
| 435 | } |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 436 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 437 | /* Make sure all pkts were transmitted on the same (loop) intfc */ |
| 438 | sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 439 | sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX]; |
| 440 | sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX]; |
| 441 | sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX]; |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 442 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 443 | not_all_match_config = (sw_if_index0 ^ sw_if_index1) |
| 444 | ^ (sw_if_index2 ^ sw_if_index3); |
| 445 | not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index; |
| 446 | |
| 447 | /* Speed path / expected case: all pkts on the same intfc */ |
| 448 | if (PREDICT_TRUE (not_all_match_config == 0)) |
| 449 | { |
| 450 | next[0] = next_index; |
| 451 | next[1] = next_index; |
| 452 | next[2] = next_index; |
| 453 | next[3] = next_index; |
| 454 | vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 455 | vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 456 | vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 457 | vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 458 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 459 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 460 | vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 461 | vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 462 | n_bytes = vlib_buffer_length_in_chain (vm, b[0]); |
| 463 | n_bytes += vlib_buffer_length_in_chain (vm, b[1]); |
| 464 | n_bytes += vlib_buffer_length_in_chain (vm, b[2]); |
| 465 | n_bytes += vlib_buffer_length_in_chain (vm, b[3]); |
| 466 | |
| 467 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 468 | { |
| 469 | vnet_update_l2_len (b[0]); |
| 470 | vnet_update_l2_len (b[1]); |
| 471 | vnet_update_l2_len (b[2]); |
| 472 | vnet_update_l2_len (b[3]); |
| 473 | } |
| 474 | |
| 475 | /* increment TX interface stat */ |
| 476 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 477 | VNET_INTERFACE_COUNTER_TX, |
| 478 | thread_index, new_rx_sw_if_index, |
| 479 | 4 /* pkts */ , n_bytes); |
| 480 | b += 4; |
| 481 | next += 4; |
| 482 | n_left_from -= 4; |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Slow path: we know that at least one of the pkts |
| 488 | * was transmitted on a different sw_if_index, so |
| 489 | * check each sw_if_index against the cached data and proceed |
| 490 | * accordingly. |
| 491 | * |
| 492 | * This shouldn't happen, but code can (and does) bypass the |
| 493 | * per-interface output node, so deal with it. |
| 494 | */ |
| 495 | if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX] |
| 496 | != new_rx_sw_if_index)) |
| 497 | { |
| 498 | config = l2input_intf_config |
| 499 | (vnet_buffer (b[0])->sw_if_index[VLIB_TX]); |
| 500 | next_index = |
| 501 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 502 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
| 503 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 504 | new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
| 505 | } |
| 506 | next[0] = next_index; |
| 507 | vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 508 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 509 | n_bytes = vlib_buffer_length_in_chain (vm, b[0]); |
| 510 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 511 | vnet_update_l2_len (b[0]); |
| 512 | |
| 513 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 514 | VNET_INTERFACE_COUNTER_TX, |
| 515 | thread_index, new_rx_sw_if_index, |
| 516 | 1 /* pkts */ , n_bytes); |
| 517 | |
| 518 | if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX] |
| 519 | != new_rx_sw_if_index)) |
| 520 | { |
| 521 | config = l2input_intf_config |
| 522 | (vnet_buffer (b[1])->sw_if_index[VLIB_TX]); |
| 523 | next_index = |
| 524 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 525 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
| 526 | new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX]; |
| 527 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 528 | } |
| 529 | next[1] = next_index; |
| 530 | vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 531 | vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 532 | n_bytes = vlib_buffer_length_in_chain (vm, b[1]); |
| 533 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 534 | vnet_update_l2_len (b[1]); |
| 535 | |
| 536 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 537 | VNET_INTERFACE_COUNTER_TX, |
| 538 | thread_index, new_rx_sw_if_index, |
| 539 | 1 /* pkts */ , n_bytes); |
| 540 | |
| 541 | if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX] |
| 542 | != new_rx_sw_if_index)) |
| 543 | { |
| 544 | config = l2input_intf_config |
| 545 | (vnet_buffer (b[2])->sw_if_index[VLIB_TX]); |
| 546 | next_index = |
| 547 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 548 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
| 549 | new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX]; |
| 550 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 551 | } |
| 552 | next[2] = next_index; |
| 553 | vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 554 | vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 555 | n_bytes = vlib_buffer_length_in_chain (vm, b[2]); |
| 556 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 557 | vnet_update_l2_len (b[2]); |
| 558 | |
| 559 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 560 | VNET_INTERFACE_COUNTER_TX, |
| 561 | thread_index, new_rx_sw_if_index, |
| 562 | 1 /* pkts */ , n_bytes); |
| 563 | |
| 564 | if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX] |
| 565 | != new_rx_sw_if_index)) |
| 566 | { |
| 567 | config = l2input_intf_config |
| 568 | (vnet_buffer (b[3])->sw_if_index[VLIB_TX]); |
| 569 | next_index = |
| 570 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 571 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
| 572 | new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX]; |
| 573 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 574 | } |
| 575 | next[3] = next_index; |
| 576 | vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 577 | vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 578 | n_bytes = vlib_buffer_length_in_chain (vm, b[3]); |
| 579 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 580 | vnet_update_l2_len (b[3]); |
| 581 | |
| 582 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 583 | VNET_INTERFACE_COUNTER_TX, |
| 584 | thread_index, new_rx_sw_if_index, |
| 585 | 1 /* pkts */ , n_bytes); |
| 586 | b += 4; |
| 587 | next += 4; |
| 588 | n_left_from -= 4; |
| 589 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 590 | while (n_left_from > 0) |
| 591 | { |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 592 | if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX] |
| 593 | != new_rx_sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | { |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 595 | config = l2input_intf_config |
| 596 | (vnet_buffer (b[0])->sw_if_index[VLIB_TX]); |
| 597 | next_index = |
| 598 | config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT : |
| 599 | VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT; |
| 600 | new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0; |
| 601 | new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | } |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 603 | next[0] = next_index; |
| 604 | vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index; |
| 605 | vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index; |
| 606 | n_bytes = vlib_buffer_length_in_chain (vm, b[0]); |
| 607 | if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT) |
| 608 | vnet_update_l2_len (b[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 610 | vlib_increment_combined_counter (im->combined_sw_if_counters + |
| 611 | VNET_INTERFACE_COUNTER_TX, |
| 612 | thread_index, new_rx_sw_if_index, |
| 613 | 1 /* pkts */ , n_bytes); |
| 614 | b += 1; |
| 615 | next += 1; |
| 616 | n_left_from -= 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Dave Barach | 78451a6 | 2018-07-21 09:25:00 -0400 | [diff] [blame] | 619 | vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors); |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 620 | |
| 621 | return frame->n_vectors; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 624 | static u8 * |
| 625 | format_simulated_ethernet_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 626 | { |
| 627 | u32 dev_instance = va_arg (*args, u32); |
| 628 | return format (s, "loop%d", dev_instance); |
| 629 | } |
| 630 | |
Pierre Pfister | f00f91a | 2016-03-09 18:22:32 +0000 | [diff] [blame] | 631 | static clib_error_t * |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 632 | simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, |
| 633 | u32 flags) |
Pierre Pfister | f00f91a | 2016-03-09 18:22:32 +0000 | [diff] [blame] | 634 | { |
| 635 | u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 636 | VNET_HW_INTERFACE_FLAG_LINK_UP : 0; |
Pierre Pfister | f00f91a | 2016-03-09 18:22:32 +0000 | [diff] [blame] | 637 | vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); |
| 638 | return 0; |
| 639 | } |
| 640 | |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 641 | static clib_error_t * |
| 642 | simulated_ethernet_mac_change (vnet_hw_interface_t * hi, |
| 643 | const u8 * old_address, const u8 * mac_address) |
| 644 | { |
| 645 | l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address); |
| 646 | |
| 647 | return (NULL); |
| 648 | } |
| 649 | |
| 650 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 651 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 652 | VNET_DEVICE_CLASS (ethernet_simulated_device_class) = { |
| 653 | .name = "Loopback", |
| 654 | .format_device_name = format_simulated_ethernet_name, |
| 655 | .tx_function = simulated_ethernet_interface_tx, |
Pierre Pfister | f00f91a | 2016-03-09 18:22:32 +0000 | [diff] [blame] | 656 | .admin_up_down_function = simulated_ethernet_admin_up_down, |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 657 | .mac_addr_change_function = simulated_ethernet_mac_change, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 658 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 659 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 660 | |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 661 | VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ethernet_simulated_device_class, |
| 662 | simulated_ethernet_interface_tx); |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * Maintain a bitmap of allocated loopback instance numbers. |
| 666 | */ |
| 667 | #define LOOPBACK_MAX_INSTANCE (16 * 1024) |
| 668 | |
| 669 | static u32 |
| 670 | loopback_instance_alloc (u8 is_specified, u32 want) |
| 671 | { |
| 672 | ethernet_main_t *em = ðernet_main; |
| 673 | |
| 674 | /* |
| 675 | * Check for dynamically allocaetd instance number. |
| 676 | */ |
| 677 | if (!is_specified) |
| 678 | { |
| 679 | u32 bit; |
| 680 | |
| 681 | bit = clib_bitmap_first_clear (em->bm_loopback_instances); |
| 682 | if (bit >= LOOPBACK_MAX_INSTANCE) |
| 683 | { |
| 684 | return ~0; |
| 685 | } |
| 686 | em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances, |
| 687 | bit, 1); |
| 688 | return bit; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * In range? |
| 693 | */ |
| 694 | if (want >= LOOPBACK_MAX_INSTANCE) |
| 695 | { |
| 696 | return ~0; |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * Already in use? |
| 701 | */ |
| 702 | if (clib_bitmap_get (em->bm_loopback_instances, want)) |
| 703 | { |
| 704 | return ~0; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Grant allocation request. |
| 709 | */ |
| 710 | em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances, |
| 711 | want, 1); |
| 712 | |
| 713 | return want; |
| 714 | } |
| 715 | |
| 716 | static int |
| 717 | loopback_instance_free (u32 instance) |
| 718 | { |
| 719 | ethernet_main_t *em = ðernet_main; |
| 720 | |
| 721 | if (instance >= LOOPBACK_MAX_INSTANCE) |
| 722 | { |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0) |
| 727 | { |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances, |
| 732 | instance, 0); |
| 733 | return 0; |
| 734 | } |
| 735 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 736 | int |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 737 | vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address, |
| 738 | u8 is_specified, u32 user_instance) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 739 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 740 | vnet_main_t *vnm = vnet_get_main (); |
| 741 | vlib_main_t *vm = vlib_get_main (); |
| 742 | clib_error_t *error; |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 743 | u32 instance; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | u8 address[6]; |
| 745 | u32 hw_if_index; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 746 | vnet_hw_interface_t *hw_if; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | u32 slot; |
| 748 | int rv = 0; |
| 749 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 750 | ASSERT (sw_if_indexp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 751 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 752 | *sw_if_indexp = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 753 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 754 | clib_memset (address, 0, sizeof (address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 755 | |
| 756 | /* |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 757 | * Allocate a loopback instance. Either select on dynamically |
| 758 | * or try to use the desired user_instance number. |
| 759 | */ |
| 760 | instance = loopback_instance_alloc (is_specified, user_instance); |
| 761 | if (instance == ~0) |
| 762 | { |
| 763 | return VNET_API_ERROR_INVALID_REGISTRATION; |
| 764 | } |
| 765 | |
| 766 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | * Default MAC address (dead:0000:0000 + instance) is allocated |
| 768 | * if zero mac_address is configured. Otherwise, user-configurable MAC |
| 769 | * address is programmed on the loopback interface. |
| 770 | */ |
| 771 | if (memcmp (address, mac_address, sizeof (address))) |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 772 | clib_memcpy (address, mac_address, sizeof (address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 773 | else |
| 774 | { |
| 775 | address[0] = 0xde; |
| 776 | address[1] = 0xad; |
| 777 | address[5] = instance; |
| 778 | } |
| 779 | |
| 780 | error = ethernet_register_interface |
| 781 | (vnm, |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 782 | ethernet_simulated_device_class.index, instance, address, &hw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 783 | /* flag change */ 0); |
| 784 | |
| 785 | if (error) |
| 786 | { |
| 787 | rv = VNET_API_ERROR_INVALID_REGISTRATION; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 788 | clib_error_report (error); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 789 | return rv; |
| 790 | } |
| 791 | |
| 792 | hw_if = vnet_get_hw_interface (vnm, hw_if_index); |
| 793 | slot = vlib_node_add_named_next_with_slot |
| 794 | (vm, hw_if->tx_node_index, |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 795 | "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 796 | ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT); |
| 797 | |
Dave Barach | 635ec3b | 2018-07-13 20:12:45 -0400 | [diff] [blame] | 798 | slot = vlib_node_add_named_next_with_slot |
| 799 | (vm, hw_if->tx_node_index, |
| 800 | "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT); |
| 801 | ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT); |
| 802 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 803 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 804 | vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 805 | *sw_if_indexp = si->sw_if_index; |
Neale Ranns | 87dad11 | 2018-04-09 01:53:01 -0700 | [diff] [blame] | 806 | |
| 807 | /* By default don't flood to loopbacks, as packets just keep |
| 808 | * coming back ... If this loopback becomes a BVI, we'll change it */ |
| 809 | si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static clib_error_t * |
| 816 | create_simulated_ethernet_interfaces (vlib_main_t * vm, |
| 817 | unformat_input_t * input, |
| 818 | vlib_cli_command_t * cmd) |
| 819 | { |
| 820 | int rv; |
| 821 | u32 sw_if_index; |
| 822 | u8 mac_address[6]; |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 823 | u8 is_specified = 0; |
| 824 | u32 user_instance = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 825 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 826 | clib_memset (mac_address, 0, sizeof (mac_address)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 827 | |
| 828 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 829 | { |
| 830 | if (unformat (input, "mac %U", unformat_ethernet_address, mac_address)) |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 831 | ; |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 832 | if (unformat (input, "instance %d", &user_instance)) |
| 833 | is_specified = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 834 | else |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 835 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 838 | rv = vnet_create_loopback_interface (&sw_if_index, mac_address, |
| 839 | is_specified, user_instance); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 840 | |
| 841 | if (rv) |
| 842 | return clib_error_return (0, "vnet_create_loopback_interface failed"); |
| 843 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 844 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), |
| 845 | sw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 849 | /*? |
| 850 | * Create a loopback interface. Optionally, a MAC Address can be |
| 851 | * provided. If not provided, de:ad:00:00:00:<loopId> will be used. |
| 852 | * |
| 853 | * @cliexpar |
| 854 | * The following two command syntaxes are equivalent: |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 855 | * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]} |
| 856 | * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]} |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 857 | * Example of how to create a loopback interface: |
| 858 | * @cliexcmd{loopback create-interface} |
| 859 | ?*/ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 860 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 861 | VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = { |
| 862 | .path = "loopback create-interface", |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 863 | .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 864 | .function = create_simulated_ethernet_interfaces, |
| 865 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 866 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 867 | |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 868 | /*? |
| 869 | * Create a loopback interface. Optionally, a MAC Address can be |
| 870 | * provided. If not provided, de:ad:00:00:00:<loopId> will be used. |
| 871 | * |
| 872 | * @cliexpar |
| 873 | * The following two command syntaxes are equivalent: |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 874 | * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]} |
| 875 | * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]} |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 876 | * Example of how to create a loopback interface: |
| 877 | * @cliexcmd{create loopback interface} |
| 878 | ?*/ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 879 | /* *INDENT-OFF* */ |
Alpesh Patel | 370a24e | 2016-04-08 07:27:37 -0700 | [diff] [blame] | 880 | VLIB_CLI_COMMAND (create_loopback_interface_command, static) = { |
| 881 | .path = "create loopback interface", |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 882 | .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]", |
Alpesh Patel | 370a24e | 2016-04-08 07:27:37 -0700 | [diff] [blame] | 883 | .function = create_simulated_ethernet_interfaces, |
| 884 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 885 | /* *INDENT-ON* */ |
Alpesh Patel | 370a24e | 2016-04-08 07:27:37 -0700 | [diff] [blame] | 886 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 887 | ethernet_interface_t * |
| 888 | ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index) |
| 889 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 890 | vnet_hw_interface_t *i = |
| 891 | vnet_get_hw_interface (vnet_get_main (), hw_if_index); |
| 892 | return (i->hw_class_index == |
| 893 | ethernet_hw_interface_class. |
| 894 | index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 897 | int |
| 898 | vnet_delete_loopback_interface (u32 sw_if_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 899 | { |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 900 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 902 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 903 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 904 | |
Eyal Bari | b52c034 | 2018-07-16 19:18:56 +0300 | [diff] [blame] | 905 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 906 | if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index) |
| 907 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 908 | |
Eyal Bari | b52c034 | 2018-07-16 19:18:56 +0300 | [diff] [blame] | 909 | if (loopback_instance_free (hw->dev_instance) < 0) |
| 910 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
Jon Loeliger | c83c3b7 | 2017-02-23 13:57:35 -0600 | [diff] [blame] | 911 | |
Eyal Bari | b52c034 | 2018-07-16 19:18:56 +0300 | [diff] [blame] | 912 | ethernet_delete_interface (vnm, hw->hw_if_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 917 | int |
| 918 | vnet_delete_sub_interface (u32 sw_if_index) |
| 919 | { |
| 920 | vnet_main_t *vnm = vnet_get_main (); |
Steve Shin | a36f08c | 2018-03-07 13:29:29 -0800 | [diff] [blame] | 921 | vnet_sw_interface_t *si; |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 922 | int rv = 0; |
| 923 | |
| 924 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
| 925 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 926 | |
Steve Shin | a36f08c | 2018-03-07 13:29:29 -0800 | [diff] [blame] | 927 | si = vnet_get_sw_interface (vnm, sw_if_index); |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 928 | if (si->type == VNET_SW_INTERFACE_TYPE_SUB || |
Neale Ranns | 17ff3c1 | 2018-07-04 10:24:24 -0700 | [diff] [blame] | 929 | si->type == VNET_SW_INTERFACE_TYPE_PIPE || |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 930 | si->type == VNET_SW_INTERFACE_TYPE_P2P) |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 931 | { |
John Lo | 5957a14 | 2018-01-18 12:44:50 -0500 | [diff] [blame] | 932 | vnet_interface_main_t *im = &vnm->interface_main; |
| 933 | vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 934 | u64 sup_and_sub_key = |
| 935 | ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id; |
John Lo | 5957a14 | 2018-01-18 12:44:50 -0500 | [diff] [blame] | 936 | hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key); |
| 937 | hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id); |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 938 | vnet_delete_sw_interface (vnm, sw_if_index); |
| 939 | } |
| 940 | else |
John Lo | 5957a14 | 2018-01-18 12:44:50 -0500 | [diff] [blame] | 941 | rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX; |
| 942 | |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 943 | return rv; |
| 944 | } |
| 945 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 946 | static clib_error_t * |
| 947 | delete_simulated_ethernet_interfaces (vlib_main_t * vm, |
| 948 | unformat_input_t * input, |
| 949 | vlib_cli_command_t * cmd) |
| 950 | { |
| 951 | int rv; |
| 952 | u32 sw_if_index = ~0; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 953 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | |
| 955 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 956 | { |
| 957 | if (unformat (input, "intfc %U", |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 958 | unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 959 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 960 | else |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 961 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (sw_if_index == ~0) |
| 965 | return clib_error_return (0, "interface not specified"); |
| 966 | |
| 967 | rv = vnet_delete_loopback_interface (sw_if_index); |
| 968 | |
| 969 | if (rv) |
| 970 | return clib_error_return (0, "vnet_delete_loopback_interface failed"); |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 975 | static clib_error_t * |
| 976 | delete_sub_interface (vlib_main_t * vm, |
| 977 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 978 | { |
| 979 | int rv = 0; |
| 980 | u32 sw_if_index = ~0; |
| 981 | vnet_main_t *vnm = vnet_get_main (); |
| 982 | |
| 983 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 984 | { |
| 985 | if (unformat |
| 986 | (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 987 | ; |
| 988 | else |
| 989 | break; |
| 990 | } |
| 991 | if (sw_if_index == ~0) |
| 992 | return clib_error_return (0, "interface doesn't exist"); |
| 993 | |
| 994 | if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)) |
| 995 | rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 996 | else |
| 997 | rv = vnet_delete_sub_interface (sw_if_index); |
| 998 | if (rv) |
| 999 | return clib_error_return (0, "delete_subinterface_interface failed"); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 1003 | /*? |
| 1004 | * Delete a loopback interface. |
| 1005 | * |
| 1006 | * @cliexpar |
| 1007 | * The following two command syntaxes are equivalent: |
| 1008 | * @cliexcmd{loopback delete-interface intfc <interface>} |
| 1009 | * @cliexcmd{delete loopback interface intfc <interface>} |
| 1010 | * Example of how to delete a loopback interface: |
| 1011 | * @cliexcmd{loopback delete-interface intfc loop0} |
| 1012 | ?*/ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1013 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1014 | VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = { |
| 1015 | .path = "loopback delete-interface", |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 1016 | .short_help = "loopback delete-interface intfc <interface>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1017 | .function = delete_simulated_ethernet_interfaces, |
| 1018 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1019 | /* *INDENT-ON* */ |
Alpesh S. Patel | 633951c | 2016-04-12 09:58:56 -0700 | [diff] [blame] | 1020 | |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 1021 | /*? |
| 1022 | * Delete a loopback interface. |
| 1023 | * |
| 1024 | * @cliexpar |
| 1025 | * The following two command syntaxes are equivalent: |
| 1026 | * @cliexcmd{loopback delete-interface intfc <interface>} |
| 1027 | * @cliexcmd{delete loopback interface intfc <interface>} |
| 1028 | * Example of how to delete a loopback interface: |
| 1029 | * @cliexcmd{delete loopback interface intfc loop0} |
| 1030 | ?*/ |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1031 | /* *INDENT-OFF* */ |
Alpesh S. Patel | 633951c | 2016-04-12 09:58:56 -0700 | [diff] [blame] | 1032 | VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = { |
| 1033 | .path = "delete loopback interface", |
| 1034 | .short_help = "delete loopback interface intfc <interface>", |
| 1035 | .function = delete_simulated_ethernet_interfaces, |
| 1036 | }; |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1037 | /* *INDENT-ON* */ |
| 1038 | |
Billy McFall | 2d085d9 | 2016-09-13 21:47:55 -0400 | [diff] [blame] | 1039 | /*? |
| 1040 | * Delete a sub-interface. |
| 1041 | * |
| 1042 | * @cliexpar |
| 1043 | * Example of how to delete a sub-interface: |
| 1044 | * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200} |
| 1045 | ?*/ |
Pavel Kotucek | d85590a | 2016-08-26 13:35:40 +0200 | [diff] [blame] | 1046 | /* *INDENT-OFF* */ |
| 1047 | VLIB_CLI_COMMAND (delete_sub_interface_command, static) = { |
| 1048 | .path = "delete sub-interface", |
| 1049 | .short_help = "delete sub-interface <interface>", |
| 1050 | .function = delete_sub_interface, |
| 1051 | }; |
| 1052 | /* *INDENT-ON* */ |
| 1053 | |
Keith Burns (alagalah) | e70dcc8 | 2016-08-15 18:33:19 -0700 | [diff] [blame] | 1054 | /* |
| 1055 | * fd.io coding-style-patch-verification: ON |
| 1056 | * |
| 1057 | * Local Variables: |
| 1058 | * eval: (c-set-style "gnu") |
| 1059 | * End: |
| 1060 | */ |