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