blob: 39e5cfb3cf89553e00efb081bec83baa45e8b32b [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Rannscbe25aa2019-09-30 10:53:31 +000044//#include <vnet/ethernet/arp.h>
John Lo405e41b2016-04-23 15:14:12 -040045#include <vnet/l2/l2_input.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070046#include <vnet/l2/l2_bd.h>
Neale Rannsb80c5362016-10-08 13:03:40 +010047#include <vnet/adj/adj.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000048#include <vnet/adj/adj_mcast.h>
49#include <vnet/ip-neighbor/ip_neighbor.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010050
Neale Ranns5e575b12016-10-03 09:40:25 +010051/**
52 * @file
53 * @brief Loopback Interfaces.
54 *
55 * This file contains code to manage loopback interfaces.
56 */
57
Neale Rannscbe25aa2019-09-30 10:53:31 +000058static const u8 *
Neale Ranns32e1c012016-11-22 17:07:28 +000059ethernet_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 Rannscbe25aa2019-09-30 10:53:31 +000068static const u8 *
Neale Ranns32e1c012016-11-22 17:07:28 +000069ethernet_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 Rannsb80c5362016-10-08 13:03:40 +010078/**
79 * @brief build a rewrite string to use for sending packets of type 'link_type'
80 * to 'dst_address'
81 */
82u8 *
83ethernet_build_rewrite (vnet_main_t * vnm,
84 u32 sw_if_index,
85 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070087 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 = &ethernet_main;
91 ethernet_interface_t *ei;
Neale Rannsb80c5362016-10-08 13:03:40 +010092 ethernet_header_t *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 ethernet_type_t type;
94 uword n_bytes = sizeof (h[0]);
Neale Rannsb80c5362016-10-08 13:03:40 +010095 u8 *rewrite = NULL;
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020096 u8 is_p2p = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Neale Ranns17ff3c12018-07-04 10:24:24 -070098 if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) ||
99 (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200100 is_p2p = 1;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700101 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 Kotucek15ac81c2017-06-20 14:00:26 +0200111 else if (PREDICT_FALSE (is_p2p))
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700112 {
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200113 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)e70dcc82016-08-15 18:33:19 -0700129 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Neale Rannsb80c5362016-10-08 13:03:40 +0100132 switch (link_type)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700133 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100134#define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700135 _(IP4, IP4);
136 _(IP6, IP6);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800137 _(MPLS, MPLS);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700138 _(ARP, ARP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139#undef _
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700140 default:
Neale Rannsb80c5362016-10-08 13:03:40 +0100141 return NULL;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700142 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Neale Rannsb80c5362016-10-08 13:03:40 +0100144 vec_validate (rewrite, n_bytes - 1);
145 h = (ethernet_header_t *) rewrite;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100147 clib_memcpy (h->src_address, ei->address, sizeof (h->src_address));
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200148 if (is_p2p)
149 {
150 clib_memcpy (h->dst_address, sub_sw->p2p.client_mac,
151 sizeof (h->dst_address));
152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153 else
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200154 {
155 if (dst_address)
156 clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
157 else
Dave Barachb7b92992018-10-17 10:38:51 -0400158 clib_memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200159 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200161 if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700162 {
163 ethernet_vlan_header_t *outer = (void *) (h + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700165 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 Warnickecb9cada2015-12-08 15:45:58 -0700171
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700172 }
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200173 else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700174 {
175 ethernet_vlan_header_t *outer = (void *) (h + 1);
176 ethernet_vlan_header_t *inner = (void *) (outer + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700178 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 Warnickecb9cada2015-12-08 15:45:58 -0700187
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700188 }
189 else
190 {
191 h->type = clib_host_to_net_u16 (type);
192 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193
Neale Rannsb80c5362016-10-08 13:03:40 +0100194 return (rewrite);
195}
196
197void
198ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
199{
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200200 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
Neale Rannscbe25aa2019-09-30 10:53:31 +0000201
Neale Ranns17ff3c12018-07-04 10:24:24 -0700202 if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) ||
203 (si->type == VNET_SW_INTERFACE_TYPE_PIPE))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200204 {
205 default_update_adjacency (vnm, sw_if_index, ai);
206 }
Neale Rannsb80c5362016-10-08 13:03:40 +0100207 else
208 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000209 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:
219 ip_neighbor_update (vnm, ai);
220 break;
221 case IP_LOOKUP_NEXT_BCAST:
222 adj_nbr_update_rewrite (ai,
223 ADJ_NBR_REWRITE_FLAG_COMPLETE,
224 ethernet_build_rewrite
225 (vnm,
226 adj->rewrite_header.sw_if_index,
227 adj->ia_link,
228 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
229 break;
230 case IP_LOOKUP_NEXT_MCAST:
231 {
232 /*
233 * Construct a partial rewrite from the known ethernet mcast dest MAC
234 */
235 u8 *rewrite;
236 u8 offset;
237
238 rewrite = ethernet_build_rewrite
239 (vnm,
240 sw_if_index,
241 adj->ia_link,
242 (adj->ia_nh_proto == FIB_PROTOCOL_IP6 ?
243 ethernet_ip6_mcast_dst_addr () :
244 ethernet_ip4_mcast_dst_addr ()));
245
246 /*
247 * Complete the remaining fields of the adj's rewrite to direct the
248 * complete of the rewrite at switch time by copying in the IP
249 * dst address's bytes.
250 * Ofset is 2 bytes into the destintation address.
251 */
252 offset = vec_len (rewrite) - 2;
253 adj_mcast_update_rewrite (ai, rewrite, offset);
254
255 break;
256 }
257 case IP_LOOKUP_NEXT_DROP:
258 case IP_LOOKUP_NEXT_PUNT:
259 case IP_LOOKUP_NEXT_LOCAL:
260 case IP_LOOKUP_NEXT_REWRITE:
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 Rannsb80c5362016-10-08 13:03:40 +0100268 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269}
270
Neale Ranns3be6b282016-12-20 14:24:01 +0000271static clib_error_t *
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700272ethernet_mac_change (vnet_hw_interface_t * hi,
273 const u8 * old_address, const u8 * mac_address)
Neale Ranns3be6b282016-12-20 14:24:01 +0000274{
275 ethernet_interface_t *ei;
276 ethernet_main_t *em;
277
278 em = &ethernet_main;
279 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
280
281 vec_validate (hi->hw_address,
282 STRUCT_SIZE_OF (ethernet_header_t, src_address) - 1);
283 clib_memcpy (hi->hw_address, mac_address, vec_len (hi->hw_address));
284
285 clib_memcpy (ei->address, (u8 *) mac_address, sizeof (ei->address));
Neale Rannscbe25aa2019-09-30 10:53:31 +0000286
287 {
288 ethernet_address_change_ctx_t *cb;
289 vec_foreach (cb, em->address_change_callbacks)
290 cb->function (em, hi->sw_if_index, cb->function_opaque);
291 }
Neale Ranns3be6b282016-12-20 14:24:01 +0000292
293 return (NULL);
294}
295
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700296/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
298 .name = "Ethernet",
299 .format_address = format_ethernet_address,
300 .format_header = format_ethernet_header_with_length,
301 .unformat_hw_address = unformat_ethernet_address,
302 .unformat_header = unformat_ethernet_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100303 .build_rewrite = ethernet_build_rewrite,
304 .update_adjacency = ethernet_update_adjacency,
Neale Ranns3be6b282016-12-20 14:24:01 +0000305 .mac_addr_change_function = ethernet_mac_change,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700307/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700309uword
310unformat_ethernet_interface (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700312 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
313 u32 *result = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 u32 hw_if_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700315 ethernet_main_t *em = &ethernet_main;
316 ethernet_interface_t *eif;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700318 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 return 0;
320
321 eif = ethernet_get_interface (em, hw_if_index);
322 if (eif)
323 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700324 *result = hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 return 1;
326 }
327 return 0;
328}
329
330clib_error_t *
331ethernet_register_interface (vnet_main_t * vnm,
332 u32 dev_class_index,
333 u32 dev_instance,
Neale Ranns192b13f2019-03-15 02:16:20 -0700334 const u8 * address,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700335 u32 * hw_if_index_return,
336 ethernet_flag_change_function_t flag_change)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700338 ethernet_main_t *em = &ethernet_main;
339 ethernet_interface_t *ei;
340 vnet_hw_interface_t *hi;
341 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 u32 hw_if_index;
343
344 pool_get (em->interfaces, ei);
345 ei->flag_change = flag_change;
346
347 hw_if_index = vnet_register_interface
348 (vnm,
349 dev_class_index, dev_instance,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700350 ethernet_hw_interface_class.index, ei - em->interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 *hw_if_index_return = hw_if_index;
352
353 hi = vnet_get_hw_interface (vnm, hw_if_index);
354
355 ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
356
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700357 hi->min_packet_bytes = hi->min_supported_packet_bytes =
358 ETHERNET_MIN_PACKET_BYTES;
359 hi->max_packet_bytes = hi->max_supported_packet_bytes =
360 ETHERNET_MAX_PACKET_BYTES;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700361
362 /* Standard default ethernet MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200363 vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200364
Damjan Marionf1213b82016-03-13 02:22:06 +0100365 clib_memcpy (ei->address, address, sizeof (ei->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366 vec_add (hi->hw_address, address, sizeof (ei->address));
Benoît Ganne9fb6d402019-04-15 15:28:21 +0200367 CLIB_MEM_UNPOISON (hi->hw_address, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
369 if (error)
370 {
371 pool_put (em->interfaces, ei);
372 return error;
373 }
374 return error;
375}
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700376
Ed Warnickecb9cada2015-12-08 15:45:58 -0700377void
378ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
379{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700380 ethernet_main_t *em = &ethernet_main;
381 ethernet_interface_t *ei;
382 vnet_hw_interface_t *hi;
383 main_intf_t *main_intf;
384 vlan_table_t *vlan_table;
385 u32 idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
387 hi = vnet_get_hw_interface (vnm, hw_if_index);
388 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
389
390 /* Delete vlan mapping table for dot1q and dot1ad. */
391 main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700392 if (main_intf->dot1q_vlans)
393 {
394 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
395 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
396 {
397 if (vlan_table->vlans[idx].qinqs)
398 {
399 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
John Lod63abff2018-10-04 16:22:21 -0400400 vlan_table->vlans[idx].qinqs = 0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700401 }
402 }
403 pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
Steve Shin8a3e5752018-02-20 11:38:34 -0800404 main_intf->dot1q_vlans = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700406 if (main_intf->dot1ad_vlans)
407 {
408 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
409 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
410 {
411 if (vlan_table->vlans[idx].qinqs)
412 {
413 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
John Lod63abff2018-10-04 16:22:21 -0400414 vlan_table->vlans[idx].qinqs = 0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700415 }
416 }
417 pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
Steve Shin8a3e5752018-02-20 11:38:34 -0800418 main_intf->dot1ad_vlans = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700420
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421 vnet_delete_hw_interface (vnm, hw_if_index);
422 pool_put (em->interfaces, ei);
423}
424
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700425u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
427{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700428 ethernet_main_t *em = &ethernet_main;
429 vnet_hw_interface_t *hi;
430 ethernet_interface_t *ei;
431
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432 hi = vnet_get_hw_interface (vnm, hw_if_index);
433
434 ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
435
436 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
Damjan Marion650223c2018-11-14 16:55:53 +0100437 ei->flags = flags;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 if (ei->flag_change)
439 return ei->flag_change (vnm, hi, flags);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700440 return (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441}
442
Dave Barach635ec3b2018-07-13 20:12:45 -0400443/**
444 * Echo packets back to ethernet/l2-input.
Dave Barach635ec3b2018-07-13 20:12:45 -0400445 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446static uword
447simulated_ethernet_interface_tx (vlib_main_t * vm,
Dave Barach635ec3b2018-07-13 20:12:45 -0400448 vlib_node_runtime_t *
449 node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450{
Dave Barach78451a62018-07-21 09:25:00 -0400451 u32 n_left_from, *from;
452 u32 next_index = 0;
453 u32 n_bytes;
Damjan Marion586afd72017-04-05 19:18:20 +0200454 u32 thread_index = vm->thread_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700455 vnet_main_t *vnm = vnet_get_main ();
456 vnet_interface_main_t *im = &vnm->interface_main;
Dave Barach635ec3b2018-07-13 20:12:45 -0400457 l2_input_config_t *config;
Dave Barach78451a62018-07-21 09:25:00 -0400458 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
459 u16 nexts[VLIB_FRAME_SIZE], *next;
460 u32 new_rx_sw_if_index = ~0;
461 u32 new_tx_sw_if_index = ~0;
John Lo405e41b2016-04-23 15:14:12 -0400462
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463 n_left_from = frame->n_vectors;
Damjan Mariona3d59862018-11-10 10:23:00 +0100464 from = vlib_frame_vector_args (frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Dave Barach78451a62018-07-21 09:25:00 -0400466 vlib_get_buffers (vm, from, bufs, n_left_from);
467 b = bufs;
468 next = nexts;
Dave Barach635ec3b2018-07-13 20:12:45 -0400469
Dave Barach78451a62018-07-21 09:25:00 -0400470 /* Ordinarily, this is the only config lookup. */
471 config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
472 next_index =
Dave Barach635ec3b2018-07-13 20:12:45 -0400473 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
474 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
Dave Barach78451a62018-07-21 09:25:00 -0400475 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
476 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Dave Barach635ec3b2018-07-13 20:12:45 -0400477
Dave Barach78451a62018-07-21 09:25:00 -0400478 while (n_left_from >= 4)
479 {
480 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
481 u32 not_all_match_config;
Dave Barach635ec3b2018-07-13 20:12:45 -0400482
Dave Barach78451a62018-07-21 09:25:00 -0400483 /* Prefetch next iteration. */
484 if (PREDICT_TRUE (n_left_from >= 8))
485 {
486 vlib_prefetch_buffer_header (b[4], STORE);
487 vlib_prefetch_buffer_header (b[5], STORE);
488 vlib_prefetch_buffer_header (b[6], STORE);
489 vlib_prefetch_buffer_header (b[7], STORE);
490 }
Dave Barach635ec3b2018-07-13 20:12:45 -0400491
Dave Barach78451a62018-07-21 09:25:00 -0400492 /* Make sure all pkts were transmitted on the same (loop) intfc */
493 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
494 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
495 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
496 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
Dave Barach635ec3b2018-07-13 20:12:45 -0400497
Dave Barach78451a62018-07-21 09:25:00 -0400498 not_all_match_config = (sw_if_index0 ^ sw_if_index1)
499 ^ (sw_if_index2 ^ sw_if_index3);
500 not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index;
501
502 /* Speed path / expected case: all pkts on the same intfc */
503 if (PREDICT_TRUE (not_all_match_config == 0))
504 {
505 next[0] = next_index;
506 next[1] = next_index;
507 next[2] = next_index;
508 next[3] = next_index;
509 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
510 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
511 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
512 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
513 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
514 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
515 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
516 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
517 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
518 n_bytes += vlib_buffer_length_in_chain (vm, b[1]);
519 n_bytes += vlib_buffer_length_in_chain (vm, b[2]);
520 n_bytes += vlib_buffer_length_in_chain (vm, b[3]);
521
522 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
523 {
524 vnet_update_l2_len (b[0]);
525 vnet_update_l2_len (b[1]);
526 vnet_update_l2_len (b[2]);
527 vnet_update_l2_len (b[3]);
528 }
529
530 /* increment TX interface stat */
531 vlib_increment_combined_counter (im->combined_sw_if_counters +
532 VNET_INTERFACE_COUNTER_TX,
533 thread_index, new_rx_sw_if_index,
534 4 /* pkts */ , n_bytes);
535 b += 4;
536 next += 4;
537 n_left_from -= 4;
538 continue;
539 }
540
541 /*
542 * Slow path: we know that at least one of the pkts
543 * was transmitted on a different sw_if_index, so
544 * check each sw_if_index against the cached data and proceed
545 * accordingly.
546 *
547 * This shouldn't happen, but code can (and does) bypass the
548 * per-interface output node, so deal with it.
549 */
550 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
551 != new_rx_sw_if_index))
552 {
553 config = l2input_intf_config
554 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
555 next_index =
556 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
557 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
558 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
559 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
560 }
561 next[0] = next_index;
562 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
563 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
564 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
565 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
566 vnet_update_l2_len (b[0]);
567
568 vlib_increment_combined_counter (im->combined_sw_if_counters +
569 VNET_INTERFACE_COUNTER_TX,
570 thread_index, new_rx_sw_if_index,
571 1 /* pkts */ , n_bytes);
572
573 if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX]
574 != new_rx_sw_if_index))
575 {
576 config = l2input_intf_config
577 (vnet_buffer (b[1])->sw_if_index[VLIB_TX]);
578 next_index =
579 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
580 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
581 new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
582 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
583 }
584 next[1] = next_index;
585 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
586 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
587 n_bytes = vlib_buffer_length_in_chain (vm, b[1]);
588 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
589 vnet_update_l2_len (b[1]);
590
591 vlib_increment_combined_counter (im->combined_sw_if_counters +
592 VNET_INTERFACE_COUNTER_TX,
593 thread_index, new_rx_sw_if_index,
594 1 /* pkts */ , n_bytes);
595
596 if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX]
597 != new_rx_sw_if_index))
598 {
599 config = l2input_intf_config
600 (vnet_buffer (b[2])->sw_if_index[VLIB_TX]);
601 next_index =
602 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
603 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
604 new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
605 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
606 }
607 next[2] = next_index;
608 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
609 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
610 n_bytes = vlib_buffer_length_in_chain (vm, b[2]);
611 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
612 vnet_update_l2_len (b[2]);
613
614 vlib_increment_combined_counter (im->combined_sw_if_counters +
615 VNET_INTERFACE_COUNTER_TX,
616 thread_index, new_rx_sw_if_index,
617 1 /* pkts */ , n_bytes);
618
619 if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX]
620 != new_rx_sw_if_index))
621 {
622 config = l2input_intf_config
623 (vnet_buffer (b[3])->sw_if_index[VLIB_TX]);
624 next_index =
625 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
626 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
627 new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
628 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
629 }
630 next[3] = next_index;
631 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
632 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
633 n_bytes = vlib_buffer_length_in_chain (vm, b[3]);
634 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
635 vnet_update_l2_len (b[3]);
636
637 vlib_increment_combined_counter (im->combined_sw_if_counters +
638 VNET_INTERFACE_COUNTER_TX,
639 thread_index, new_rx_sw_if_index,
640 1 /* pkts */ , n_bytes);
641 b += 4;
642 next += 4;
643 n_left_from -= 4;
644 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700645 while (n_left_from > 0)
646 {
Dave Barach78451a62018-07-21 09:25:00 -0400647 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
648 != new_rx_sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 {
Dave Barach78451a62018-07-21 09:25:00 -0400650 config = l2input_intf_config
651 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
652 next_index =
653 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
654 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
655 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
656 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657 }
Dave Barach78451a62018-07-21 09:25:00 -0400658 next[0] = next_index;
659 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
660 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
661 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
662 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
663 vnet_update_l2_len (b[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664
Dave Barach78451a62018-07-21 09:25:00 -0400665 vlib_increment_combined_counter (im->combined_sw_if_counters +
666 VNET_INTERFACE_COUNTER_TX,
667 thread_index, new_rx_sw_if_index,
668 1 /* pkts */ , n_bytes);
669 b += 1;
670 next += 1;
671 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672 }
673
Dave Barach78451a62018-07-21 09:25:00 -0400674 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Dave Barach635ec3b2018-07-13 20:12:45 -0400675
676 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677}
678
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700679static u8 *
680format_simulated_ethernet_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681{
682 u32 dev_instance = va_arg (*args, u32);
683 return format (s, "loop%d", dev_instance);
684}
685
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000686static clib_error_t *
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700687simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
688 u32 flags)
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000689{
690 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700691 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000692 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
693 return 0;
694}
695
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700696static clib_error_t *
697simulated_ethernet_mac_change (vnet_hw_interface_t * hi,
698 const u8 * old_address, const u8 * mac_address)
699{
700 l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address);
701
702 return (NULL);
703}
704
705
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700706/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
708 .name = "Loopback",
709 .format_device_name = format_simulated_ethernet_name,
710 .tx_function = simulated_ethernet_interface_tx,
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000711 .admin_up_down_function = simulated_ethernet_admin_up_down,
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700712 .mac_addr_change_function = simulated_ethernet_mac_change,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700713};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700714/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600716/*
717 * Maintain a bitmap of allocated loopback instance numbers.
718 */
719#define LOOPBACK_MAX_INSTANCE (16 * 1024)
720
721static u32
722loopback_instance_alloc (u8 is_specified, u32 want)
723{
724 ethernet_main_t *em = &ethernet_main;
725
726 /*
727 * Check for dynamically allocaetd instance number.
728 */
729 if (!is_specified)
730 {
731 u32 bit;
732
733 bit = clib_bitmap_first_clear (em->bm_loopback_instances);
734 if (bit >= LOOPBACK_MAX_INSTANCE)
735 {
736 return ~0;
737 }
738 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
739 bit, 1);
740 return bit;
741 }
742
743 /*
744 * In range?
745 */
746 if (want >= LOOPBACK_MAX_INSTANCE)
747 {
748 return ~0;
749 }
750
751 /*
752 * Already in use?
753 */
754 if (clib_bitmap_get (em->bm_loopback_instances, want))
755 {
756 return ~0;
757 }
758
759 /*
760 * Grant allocation request.
761 */
762 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
763 want, 1);
764
765 return want;
766}
767
768static int
769loopback_instance_free (u32 instance)
770{
771 ethernet_main_t *em = &ethernet_main;
772
773 if (instance >= LOOPBACK_MAX_INSTANCE)
774 {
775 return -1;
776 }
777
778 if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0)
779 {
780 return -1;
781 }
782
783 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
784 instance, 0);
785 return 0;
786}
787
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700788int
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600789vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
790 u8 is_specified, u32 user_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700791{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700792 vnet_main_t *vnm = vnet_get_main ();
793 vlib_main_t *vm = vlib_get_main ();
794 clib_error_t *error;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600795 u32 instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796 u8 address[6];
797 u32 hw_if_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700798 vnet_hw_interface_t *hw_if;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 u32 slot;
800 int rv = 0;
801
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700802 ASSERT (sw_if_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700803
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700804 *sw_if_indexp = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Dave Barachb7b92992018-10-17 10:38:51 -0400806 clib_memset (address, 0, sizeof (address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807
808 /*
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600809 * Allocate a loopback instance. Either select on dynamically
810 * or try to use the desired user_instance number.
811 */
812 instance = loopback_instance_alloc (is_specified, user_instance);
813 if (instance == ~0)
814 {
815 return VNET_API_ERROR_INVALID_REGISTRATION;
816 }
817
818 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819 * Default MAC address (dead:0000:0000 + instance) is allocated
820 * if zero mac_address is configured. Otherwise, user-configurable MAC
821 * address is programmed on the loopback interface.
822 */
823 if (memcmp (address, mac_address, sizeof (address)))
Damjan Marionf1213b82016-03-13 02:22:06 +0100824 clib_memcpy (address, mac_address, sizeof (address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 else
826 {
827 address[0] = 0xde;
828 address[1] = 0xad;
829 address[5] = instance;
830 }
831
832 error = ethernet_register_interface
833 (vnm,
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600834 ethernet_simulated_device_class.index, instance, address, &hw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835 /* flag change */ 0);
836
837 if (error)
838 {
839 rv = VNET_API_ERROR_INVALID_REGISTRATION;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700840 clib_error_report (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700841 return rv;
842 }
843
844 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
845 slot = vlib_node_add_named_next_with_slot
846 (vm, hw_if->tx_node_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700847 "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
849
Dave Barach635ec3b2018-07-13 20:12:45 -0400850 slot = vlib_node_add_named_next_with_slot
851 (vm, hw_if->tx_node_index,
852 "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
853 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
854
Ed Warnickecb9cada2015-12-08 15:45:58 -0700855 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700856 vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700857 *sw_if_indexp = si->sw_if_index;
Neale Ranns87dad112018-04-09 01:53:01 -0700858
859 /* By default don't flood to loopbacks, as packets just keep
860 * coming back ... If this loopback becomes a BVI, we'll change it */
861 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862 }
863
864 return 0;
865}
866
867static clib_error_t *
868create_simulated_ethernet_interfaces (vlib_main_t * vm,
869 unformat_input_t * input,
870 vlib_cli_command_t * cmd)
871{
872 int rv;
873 u32 sw_if_index;
874 u8 mac_address[6];
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600875 u8 is_specified = 0;
876 u32 user_instance = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700877
Dave Barachb7b92992018-10-17 10:38:51 -0400878 clib_memset (mac_address, 0, sizeof (mac_address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879
880 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
881 {
882 if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700883 ;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600884 if (unformat (input, "instance %d", &user_instance))
885 is_specified = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 else
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700887 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888 }
889
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600890 rv = vnet_create_loopback_interface (&sw_if_index, mac_address,
891 is_specified, user_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
893 if (rv)
894 return clib_error_return (0, "vnet_create_loopback_interface failed");
895
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700896 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
897 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 return 0;
899}
900
Billy McFall2d085d92016-09-13 21:47:55 -0400901/*?
902 * Create a loopback interface. Optionally, a MAC Address can be
903 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
904 *
905 * @cliexpar
906 * The following two command syntaxes are equivalent:
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600907 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
908 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
Billy McFall2d085d92016-09-13 21:47:55 -0400909 * Example of how to create a loopback interface:
910 * @cliexcmd{loopback create-interface}
911?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700912/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700913VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
914 .path = "loopback create-interface",
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600915 .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 .function = create_simulated_ethernet_interfaces,
917};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700918/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700919
Billy McFall2d085d92016-09-13 21:47:55 -0400920/*?
921 * Create a loopback interface. Optionally, a MAC Address can be
922 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
923 *
924 * @cliexpar
925 * The following two command syntaxes are equivalent:
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600926 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
927 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
Billy McFall2d085d92016-09-13 21:47:55 -0400928 * Example of how to create a loopback interface:
929 * @cliexcmd{create loopback interface}
930?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700931/* *INDENT-OFF* */
Alpesh Patel370a24e2016-04-08 07:27:37 -0700932VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
933 .path = "create loopback interface",
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600934 .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]",
Alpesh Patel370a24e2016-04-08 07:27:37 -0700935 .function = create_simulated_ethernet_interfaces,
936};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700937/* *INDENT-ON* */
Alpesh Patel370a24e2016-04-08 07:27:37 -0700938
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939ethernet_interface_t *
940ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
941{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700942 vnet_hw_interface_t *i =
943 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
944 return (i->hw_class_index ==
945 ethernet_hw_interface_class.
946 index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947}
948
Matthew G Smithd459bf32019-09-04 15:01:04 -0500949mac_address_t *
950ethernet_interface_add_del_address (ethernet_main_t * em,
951 u32 hw_if_index, const u8 * address,
952 u8 is_add)
953{
954 ethernet_interface_t *ei = ethernet_get_interface (em, hw_if_index);
955 mac_address_t *if_addr = 0;
956
957 /* return if there is not an ethernet interface for this hw interface */
958 if (!ei)
959 return 0;
960
961 /* determine whether the address is configured on the interface */
962 vec_foreach (if_addr, ei->secondary_addrs)
963 {
964 if (!ethernet_mac_address_equal (if_addr->bytes, address))
965 continue;
966
967 break;
968 }
969
970 if (if_addr && vec_is_member (ei->secondary_addrs, if_addr))
971 {
972 /* delete found address */
973 if (!is_add)
974 {
975 vec_delete (ei->secondary_addrs, 1, if_addr - ei->secondary_addrs);
976 if_addr = 0;
977 }
978 /* address already found, so nothing needs to be done if adding */
979 }
980 else
981 {
982 /* if_addr could be 0 or past the end of the vector. reset to 0 */
983 if_addr = 0;
984
985 /* add new address */
986 if (is_add)
987 {
988 vec_add2 (ei->secondary_addrs, if_addr, 1);
989 clib_memcpy (&if_addr->bytes, address, sizeof (if_addr->bytes));
990 }
991 }
992
993 return if_addr;
994}
995
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700996int
997vnet_delete_loopback_interface (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700999 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001001 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1003
Eyal Barib52c0342018-07-16 19:18:56 +03001004 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1005 if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index)
1006 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Jon Loeligerc83c3b72017-02-23 13:57:35 -06001007
Eyal Barib52c0342018-07-16 19:18:56 +03001008 if (loopback_instance_free (hw->dev_instance) < 0)
1009 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Jon Loeligerc83c3b72017-02-23 13:57:35 -06001010
Eyal Barib52c0342018-07-16 19:18:56 +03001011 ethernet_delete_interface (vnm, hw->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012
1013 return 0;
1014}
1015
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001016int
Neale Ranns669f4e32019-10-28 07:56:15 -07001017vnet_create_sub_interface (u32 sw_if_index, u32 id,
1018 u32 flags, u16 inner_vlan_id, u16 outer_vlan_id,
1019 u32 * sub_sw_if_index)
1020{
1021 vnet_main_t *vnm = vnet_get_main ();
1022 vnet_interface_main_t *im = &vnm->interface_main;
1023 vnet_hw_interface_t *hi;
1024 u64 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) id;
1025 vnet_sw_interface_t template;
1026 uword *p;
1027 u64 *kp;
1028
1029 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1030
1031 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1032 if (p)
1033 {
1034 return (VNET_API_ERROR_VLAN_ALREADY_EXISTS);
1035 }
1036
1037 clib_memset (&template, 0, sizeof (template));
1038 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1039 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1040 template.sup_sw_if_index = sw_if_index;
1041 template.sub.id = id;
1042 template.sub.eth.raw_flags = flags;
1043 template.sub.eth.outer_vlan_id = outer_vlan_id;
1044 template.sub.eth.inner_vlan_id = inner_vlan_id;
1045
1046 if (vnet_create_sw_interface (vnm, &template, sub_sw_if_index))
1047 return (VNET_API_ERROR_UNSPECIFIED);
1048
1049 kp = clib_mem_alloc (sizeof (*kp));
1050 *kp = sup_and_sub_key;
1051
1052 hash_set (hi->sub_interface_sw_if_index_by_id, id, *sub_sw_if_index);
1053 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, *sub_sw_if_index);
1054
1055 return (0);
1056}
1057
1058int
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001059vnet_delete_sub_interface (u32 sw_if_index)
1060{
1061 vnet_main_t *vnm = vnet_get_main ();
Steve Shina36f08c2018-03-07 13:29:29 -08001062 vnet_sw_interface_t *si;
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001063 int rv = 0;
1064
1065 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1066 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1067
Steve Shina36f08c2018-03-07 13:29:29 -08001068 si = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek15ac81c2017-06-20 14:00:26 +02001069 if (si->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -07001070 si->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek15ac81c2017-06-20 14:00:26 +02001071 si->type == VNET_SW_INTERFACE_TYPE_P2P)
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001072 {
John Lo5957a142018-01-18 12:44:50 -05001073 vnet_interface_main_t *im = &vnm->interface_main;
1074 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001075 u64 sup_and_sub_key =
1076 ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
John Lo5957a142018-01-18 12:44:50 -05001077 hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1078 hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id);
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001079 vnet_delete_sw_interface (vnm, sw_if_index);
1080 }
1081 else
John Lo5957a142018-01-18 12:44:50 -05001082 rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
1083
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001084 return rv;
1085}
1086
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087static clib_error_t *
1088delete_simulated_ethernet_interfaces (vlib_main_t * vm,
1089 unformat_input_t * input,
1090 vlib_cli_command_t * cmd)
1091{
1092 int rv;
1093 u32 sw_if_index = ~0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001094 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095
1096 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1097 {
1098 if (unformat (input, "intfc %U",
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001099 unformat_vnet_sw_interface, vnm, &sw_if_index))
1100 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101 else
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001102 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 }
1104
1105 if (sw_if_index == ~0)
1106 return clib_error_return (0, "interface not specified");
1107
1108 rv = vnet_delete_loopback_interface (sw_if_index);
1109
1110 if (rv)
1111 return clib_error_return (0, "vnet_delete_loopback_interface failed");
1112
1113 return 0;
1114}
1115
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001116static clib_error_t *
1117delete_sub_interface (vlib_main_t * vm,
1118 unformat_input_t * input, vlib_cli_command_t * cmd)
1119{
1120 int rv = 0;
1121 u32 sw_if_index = ~0;
1122 vnet_main_t *vnm = vnet_get_main ();
1123
1124 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1125 {
1126 if (unformat
1127 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1128 ;
1129 else
1130 break;
1131 }
1132 if (sw_if_index == ~0)
1133 return clib_error_return (0, "interface doesn't exist");
1134
1135 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1136 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1137 else
1138 rv = vnet_delete_sub_interface (sw_if_index);
1139 if (rv)
1140 return clib_error_return (0, "delete_subinterface_interface failed");
1141 return 0;
1142}
1143
Billy McFall2d085d92016-09-13 21:47:55 -04001144/*?
1145 * Delete a loopback interface.
1146 *
1147 * @cliexpar
1148 * The following two command syntaxes are equivalent:
1149 * @cliexcmd{loopback delete-interface intfc <interface>}
1150 * @cliexcmd{delete loopback interface intfc <interface>}
1151 * Example of how to delete a loopback interface:
1152 * @cliexcmd{loopback delete-interface intfc loop0}
1153?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001154/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001155VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
1156 .path = "loopback delete-interface",
Billy McFall2d085d92016-09-13 21:47:55 -04001157 .short_help = "loopback delete-interface intfc <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001158 .function = delete_simulated_ethernet_interfaces,
1159};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001160/* *INDENT-ON* */
Alpesh S. Patel633951c2016-04-12 09:58:56 -07001161
Billy McFall2d085d92016-09-13 21:47:55 -04001162/*?
1163 * Delete a loopback interface.
1164 *
1165 * @cliexpar
1166 * The following two command syntaxes are equivalent:
1167 * @cliexcmd{loopback delete-interface intfc <interface>}
1168 * @cliexcmd{delete loopback interface intfc <interface>}
1169 * Example of how to delete a loopback interface:
1170 * @cliexcmd{delete loopback interface intfc loop0}
1171?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001172/* *INDENT-OFF* */
Alpesh S. Patel633951c2016-04-12 09:58:56 -07001173VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
1174 .path = "delete loopback interface",
1175 .short_help = "delete loopback interface intfc <interface>",
1176 .function = delete_simulated_ethernet_interfaces,
1177};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001178/* *INDENT-ON* */
1179
Billy McFall2d085d92016-09-13 21:47:55 -04001180/*?
1181 * Delete a sub-interface.
1182 *
1183 * @cliexpar
1184 * Example of how to delete a sub-interface:
1185 * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
1186?*/
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001187/* *INDENT-OFF* */
1188VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
1189 .path = "delete sub-interface",
1190 .short_help = "delete sub-interface <interface>",
1191 .function = delete_sub_interface,
1192};
1193/* *INDENT-ON* */
1194
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001195/*
1196 * fd.io coding-style-patch-verification: ON
1197 *
1198 * Local Variables:
1199 * eval: (c-set-style "gnu")
1200 * End:
1201 */