blob: d44a8a8ecb058a4562679420052968ee07956fc5 [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>
John Lo405e41b2016-04-23 15:14:12 -040044#include <vnet/l2/l2_input.h>
Neale Ranns3b81a1e2018-09-06 09:50:26 -070045#include <vnet/l2/l2_bd.h>
Neale Rannsb80c5362016-10-08 13:03:40 +010046#include <vnet/adj/adj.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010047
Neale Ranns5e575b12016-10-03 09:40:25 +010048/**
49 * @file
50 * @brief Loopback Interfaces.
51 *
52 * This file contains code to manage loopback interfaces.
53 */
54
Neale Ranns32e1c012016-11-22 17:07:28 +000055const u8 *
56ethernet_ip4_mcast_dst_addr (void)
57{
58 const static u8 ethernet_mcast_dst_mac[] = {
59 0x1, 0x0, 0x5e, 0x0, 0x0, 0x0,
60 };
61
62 return (ethernet_mcast_dst_mac);
63}
64
65const u8 *
66ethernet_ip6_mcast_dst_addr (void)
67{
68 const static u8 ethernet_mcast_dst_mac[] = {
69 0x33, 0x33, 0x00, 0x0, 0x0, 0x0,
70 };
71
72 return (ethernet_mcast_dst_mac);
73}
74
Neale Rannsb80c5362016-10-08 13:03:40 +010075/**
76 * @brief build a rewrite string to use for sending packets of type 'link_type'
77 * to 'dst_address'
78 */
79u8 *
80ethernet_build_rewrite (vnet_main_t * vnm,
81 u32 sw_if_index,
82 vnet_link_t link_type, const void *dst_address)
Ed Warnickecb9cada2015-12-08 15:45:58 -070083{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070084 vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
85 vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
86 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
87 ethernet_main_t *em = &ethernet_main;
88 ethernet_interface_t *ei;
Neale Rannsb80c5362016-10-08 13:03:40 +010089 ethernet_header_t *h;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090 ethernet_type_t type;
91 uword n_bytes = sizeof (h[0]);
Neale Rannsb80c5362016-10-08 13:03:40 +010092 u8 *rewrite = NULL;
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020093 u8 is_p2p = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
Neale Ranns17ff3c12018-07-04 10:24:24 -070095 if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) ||
96 (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE))
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020097 is_p2p = 1;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070098 if (sub_sw != sup_sw)
99 {
100 if (sub_sw->sub.eth.flags.one_tag)
101 {
102 n_bytes += sizeof (ethernet_vlan_header_t);
103 }
104 else if (sub_sw->sub.eth.flags.two_tags)
105 {
106 n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
107 }
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200108 else if (PREDICT_FALSE (is_p2p))
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700109 {
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200110 n_bytes = sizeof (ethernet_header_t);
111 }
112 if (PREDICT_FALSE (!is_p2p))
113 {
114 // Check for encaps that are not supported for L3 interfaces
115 if (!(sub_sw->sub.eth.flags.exact_match) ||
116 (sub_sw->sub.eth.flags.default_sub) ||
117 (sub_sw->sub.eth.flags.outer_vlan_id_any) ||
118 (sub_sw->sub.eth.flags.inner_vlan_id_any))
119 {
120 return 0;
121 }
122 }
123 else
124 {
125 n_bytes = sizeof (ethernet_header_t);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700126 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
Neale Rannsb80c5362016-10-08 13:03:40 +0100129 switch (link_type)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700130 {
Neale Rannsb80c5362016-10-08 13:03:40 +0100131#define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700132 _(IP4, IP4);
133 _(IP6, IP6);
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800134 _(MPLS, MPLS);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700135 _(ARP, ARP);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136#undef _
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700137 default:
Neale Rannsb80c5362016-10-08 13:03:40 +0100138 return NULL;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700139 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
Neale Rannsb80c5362016-10-08 13:03:40 +0100141 vec_validate (rewrite, n_bytes - 1);
142 h = (ethernet_header_t *) rewrite;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
Damjan Marionf1213b82016-03-13 02:22:06 +0100144 clib_memcpy (h->src_address, ei->address, sizeof (h->src_address));
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200145 if (is_p2p)
146 {
147 clib_memcpy (h->dst_address, sub_sw->p2p.client_mac,
148 sizeof (h->dst_address));
149 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 else
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200151 {
152 if (dst_address)
153 clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
154 else
155 memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */
156 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200158 if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700159 {
160 ethernet_vlan_header_t *outer = (void *) (h + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700162 h->type = sub_sw->sub.eth.flags.dot1ad ?
163 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
164 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
165 outer->priority_cfi_and_id =
166 clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
167 outer->type = clib_host_to_net_u16 (type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700169 }
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200170 else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700171 {
172 ethernet_vlan_header_t *outer = (void *) (h + 1);
173 ethernet_vlan_header_t *inner = (void *) (outer + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700175 h->type = sub_sw->sub.eth.flags.dot1ad ?
176 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
177 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
178 outer->priority_cfi_and_id =
179 clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
180 outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
181 inner->priority_cfi_and_id =
182 clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
183 inner->type = clib_host_to_net_u16 (type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700185 }
186 else
187 {
188 h->type = clib_host_to_net_u16 (type);
189 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Neale Rannsb80c5362016-10-08 13:03:40 +0100191 return (rewrite);
192}
193
194void
195ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
196{
197 ip_adjacency_t *adj;
198
199 adj = adj_get (ai);
200
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200201 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
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 }
207 else if (FIB_PROTOCOL_IP4 == adj->ia_nh_proto)
Neale Rannsb80c5362016-10-08 13:03:40 +0100208 {
209 arp_update_adjacency (vnm, sw_if_index, ai);
210 }
211 else if (FIB_PROTOCOL_IP6 == adj->ia_nh_proto)
212 {
213 ip6_ethernet_update_adjacency (vnm, sw_if_index, ai);
214 }
215 else
216 {
217 ASSERT (0);
218 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219}
220
Neale Ranns3be6b282016-12-20 14:24:01 +0000221static clib_error_t *
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700222ethernet_mac_change (vnet_hw_interface_t * hi,
223 const u8 * old_address, const u8 * mac_address)
Neale Ranns3be6b282016-12-20 14:24:01 +0000224{
225 ethernet_interface_t *ei;
226 ethernet_main_t *em;
227
228 em = &ethernet_main;
229 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
230
231 vec_validate (hi->hw_address,
232 STRUCT_SIZE_OF (ethernet_header_t, src_address) - 1);
233 clib_memcpy (hi->hw_address, mac_address, vec_len (hi->hw_address));
234
235 clib_memcpy (ei->address, (u8 *) mac_address, sizeof (ei->address));
236 ethernet_arp_change_mac (hi->sw_if_index);
237 ethernet_ndp_change_mac (hi->sw_if_index);
238
239 return (NULL);
240}
241
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700242/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
244 .name = "Ethernet",
245 .format_address = format_ethernet_address,
246 .format_header = format_ethernet_header_with_length,
247 .unformat_hw_address = unformat_ethernet_address,
248 .unformat_header = unformat_ethernet_header,
Neale Rannsb80c5362016-10-08 13:03:40 +0100249 .build_rewrite = ethernet_build_rewrite,
250 .update_adjacency = ethernet_update_adjacency,
Neale Ranns3be6b282016-12-20 14:24:01 +0000251 .mac_addr_change_function = ethernet_mac_change,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700253/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700255uword
256unformat_ethernet_interface (unformat_input_t * input, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700258 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
259 u32 *result = va_arg (*args, u32 *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 u32 hw_if_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700261 ethernet_main_t *em = &ethernet_main;
262 ethernet_interface_t *eif;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700264 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265 return 0;
266
267 eif = ethernet_get_interface (em, hw_if_index);
268 if (eif)
269 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700270 *result = hw_if_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 return 1;
272 }
273 return 0;
274}
275
276clib_error_t *
277ethernet_register_interface (vnet_main_t * vnm,
278 u32 dev_class_index,
279 u32 dev_instance,
280 u8 * address,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700281 u32 * hw_if_index_return,
282 ethernet_flag_change_function_t flag_change)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700284 ethernet_main_t *em = &ethernet_main;
285 ethernet_interface_t *ei;
286 vnet_hw_interface_t *hi;
287 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 u32 hw_if_index;
289
290 pool_get (em->interfaces, ei);
291 ei->flag_change = flag_change;
292
293 hw_if_index = vnet_register_interface
294 (vnm,
295 dev_class_index, dev_instance,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700296 ethernet_hw_interface_class.index, ei - em->interfaces);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297 *hw_if_index_return = hw_if_index;
298
299 hi = vnet_get_hw_interface (vnm, hw_if_index);
300
301 ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
302
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700303 hi->min_packet_bytes = hi->min_supported_packet_bytes =
304 ETHERNET_MIN_PACKET_BYTES;
305 hi->max_packet_bytes = hi->max_supported_packet_bytes =
306 ETHERNET_MAX_PACKET_BYTES;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307
308 /* Standard default ethernet MTU. */
Ole Troand7231612018-06-07 10:17:57 +0200309 vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, 9000);
Damjan Marionfe7d4a22018-04-13 19:43:39 +0200310
Damjan Marionf1213b82016-03-13 02:22:06 +0100311 clib_memcpy (ei->address, address, sizeof (ei->address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 vec_add (hi->hw_address, address, sizeof (ei->address));
313
314 if (error)
315 {
316 pool_put (em->interfaces, ei);
317 return error;
318 }
319 return error;
320}
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700321
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322void
323ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
324{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700325 ethernet_main_t *em = &ethernet_main;
326 ethernet_interface_t *ei;
327 vnet_hw_interface_t *hi;
328 main_intf_t *main_intf;
329 vlan_table_t *vlan_table;
330 u32 idx;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
332 hi = vnet_get_hw_interface (vnm, hw_if_index);
333 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
334
335 /* Delete vlan mapping table for dot1q and dot1ad. */
336 main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700337 if (main_intf->dot1q_vlans)
338 {
339 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
340 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
341 {
342 if (vlan_table->vlans[idx].qinqs)
343 {
344 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
345 }
346 }
347 pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
Steve Shin8a3e5752018-02-20 11:38:34 -0800348 main_intf->dot1q_vlans = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700350 if (main_intf->dot1ad_vlans)
351 {
352 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
353 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
354 {
355 if (vlan_table->vlans[idx].qinqs)
356 {
357 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
358 }
359 }
360 pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
Steve Shin8a3e5752018-02-20 11:38:34 -0800361 main_intf->dot1ad_vlans = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700363
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 vnet_delete_hw_interface (vnm, hw_if_index);
365 pool_put (em->interfaces, ei);
366}
367
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700368u32
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
370{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700371 ethernet_main_t *em = &ethernet_main;
372 vnet_hw_interface_t *hi;
373 ethernet_interface_t *ei;
374
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 hi = vnet_get_hw_interface (vnm, hw_if_index);
376
377 ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
378
379 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
380 if (ei->flag_change)
381 return ei->flag_change (vnm, hi, flags);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700382 return (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383}
384
Dave Barach635ec3b2018-07-13 20:12:45 -0400385/**
386 * Echo packets back to ethernet/l2-input.
Dave Barach635ec3b2018-07-13 20:12:45 -0400387 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388static uword
389simulated_ethernet_interface_tx (vlib_main_t * vm,
Dave Barach635ec3b2018-07-13 20:12:45 -0400390 vlib_node_runtime_t *
391 node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392{
Dave Barach78451a62018-07-21 09:25:00 -0400393 u32 n_left_from, *from;
394 u32 next_index = 0;
395 u32 n_bytes;
Damjan Marion586afd72017-04-05 19:18:20 +0200396 u32 thread_index = vm->thread_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700397 vnet_main_t *vnm = vnet_get_main ();
398 vnet_interface_main_t *im = &vnm->interface_main;
Dave Barach635ec3b2018-07-13 20:12:45 -0400399 l2_input_config_t *config;
Dave Barach78451a62018-07-21 09:25:00 -0400400 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
401 u16 nexts[VLIB_FRAME_SIZE], *next;
402 u32 new_rx_sw_if_index = ~0;
403 u32 new_tx_sw_if_index = ~0;
John Lo405e41b2016-04-23 15:14:12 -0400404
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 n_left_from = frame->n_vectors;
406 from = vlib_frame_args (frame);
407
Dave Barach78451a62018-07-21 09:25:00 -0400408 vlib_get_buffers (vm, from, bufs, n_left_from);
409 b = bufs;
410 next = nexts;
Dave Barach635ec3b2018-07-13 20:12:45 -0400411
Dave Barach78451a62018-07-21 09:25:00 -0400412 /* Ordinarily, this is the only config lookup. */
413 config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
414 next_index =
Dave Barach635ec3b2018-07-13 20:12:45 -0400415 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
416 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
Dave Barach78451a62018-07-21 09:25:00 -0400417 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
418 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Dave Barach635ec3b2018-07-13 20:12:45 -0400419
Dave Barach78451a62018-07-21 09:25:00 -0400420 while (n_left_from >= 4)
421 {
422 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
423 u32 not_all_match_config;
Dave Barach635ec3b2018-07-13 20:12:45 -0400424
Dave Barach78451a62018-07-21 09:25:00 -0400425 /* Prefetch next iteration. */
426 if (PREDICT_TRUE (n_left_from >= 8))
427 {
428 vlib_prefetch_buffer_header (b[4], STORE);
429 vlib_prefetch_buffer_header (b[5], STORE);
430 vlib_prefetch_buffer_header (b[6], STORE);
431 vlib_prefetch_buffer_header (b[7], STORE);
432 }
Dave Barach635ec3b2018-07-13 20:12:45 -0400433
Dave Barach78451a62018-07-21 09:25:00 -0400434 /* Make sure all pkts were transmitted on the same (loop) intfc */
435 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
436 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
437 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
438 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
Dave Barach635ec3b2018-07-13 20:12:45 -0400439
Dave Barach78451a62018-07-21 09:25:00 -0400440 not_all_match_config = (sw_if_index0 ^ sw_if_index1)
441 ^ (sw_if_index2 ^ sw_if_index3);
442 not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index;
443
444 /* Speed path / expected case: all pkts on the same intfc */
445 if (PREDICT_TRUE (not_all_match_config == 0))
446 {
447 next[0] = next_index;
448 next[1] = next_index;
449 next[2] = next_index;
450 next[3] = next_index;
451 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
452 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
453 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
454 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
455 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
456 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
457 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
458 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
459 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
460 n_bytes += vlib_buffer_length_in_chain (vm, b[1]);
461 n_bytes += vlib_buffer_length_in_chain (vm, b[2]);
462 n_bytes += vlib_buffer_length_in_chain (vm, b[3]);
463
464 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
465 {
466 vnet_update_l2_len (b[0]);
467 vnet_update_l2_len (b[1]);
468 vnet_update_l2_len (b[2]);
469 vnet_update_l2_len (b[3]);
470 }
471
472 /* increment TX interface stat */
473 vlib_increment_combined_counter (im->combined_sw_if_counters +
474 VNET_INTERFACE_COUNTER_TX,
475 thread_index, new_rx_sw_if_index,
476 4 /* pkts */ , n_bytes);
477 b += 4;
478 next += 4;
479 n_left_from -= 4;
480 continue;
481 }
482
483 /*
484 * Slow path: we know that at least one of the pkts
485 * was transmitted on a different sw_if_index, so
486 * check each sw_if_index against the cached data and proceed
487 * accordingly.
488 *
489 * This shouldn't happen, but code can (and does) bypass the
490 * per-interface output node, so deal with it.
491 */
492 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
493 != new_rx_sw_if_index))
494 {
495 config = l2input_intf_config
496 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
497 next_index =
498 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
499 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
500 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
501 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
502 }
503 next[0] = next_index;
504 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
505 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
506 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
507 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
508 vnet_update_l2_len (b[0]);
509
510 vlib_increment_combined_counter (im->combined_sw_if_counters +
511 VNET_INTERFACE_COUNTER_TX,
512 thread_index, new_rx_sw_if_index,
513 1 /* pkts */ , n_bytes);
514
515 if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX]
516 != new_rx_sw_if_index))
517 {
518 config = l2input_intf_config
519 (vnet_buffer (b[1])->sw_if_index[VLIB_TX]);
520 next_index =
521 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
522 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
523 new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
524 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
525 }
526 next[1] = next_index;
527 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
528 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
529 n_bytes = vlib_buffer_length_in_chain (vm, b[1]);
530 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
531 vnet_update_l2_len (b[1]);
532
533 vlib_increment_combined_counter (im->combined_sw_if_counters +
534 VNET_INTERFACE_COUNTER_TX,
535 thread_index, new_rx_sw_if_index,
536 1 /* pkts */ , n_bytes);
537
538 if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX]
539 != new_rx_sw_if_index))
540 {
541 config = l2input_intf_config
542 (vnet_buffer (b[2])->sw_if_index[VLIB_TX]);
543 next_index =
544 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
545 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
546 new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
547 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
548 }
549 next[2] = next_index;
550 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
551 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
552 n_bytes = vlib_buffer_length_in_chain (vm, b[2]);
553 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
554 vnet_update_l2_len (b[2]);
555
556 vlib_increment_combined_counter (im->combined_sw_if_counters +
557 VNET_INTERFACE_COUNTER_TX,
558 thread_index, new_rx_sw_if_index,
559 1 /* pkts */ , n_bytes);
560
561 if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX]
562 != new_rx_sw_if_index))
563 {
564 config = l2input_intf_config
565 (vnet_buffer (b[3])->sw_if_index[VLIB_TX]);
566 next_index =
567 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
568 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
569 new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
570 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
571 }
572 next[3] = next_index;
573 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
574 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
575 n_bytes = vlib_buffer_length_in_chain (vm, b[3]);
576 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
577 vnet_update_l2_len (b[3]);
578
579 vlib_increment_combined_counter (im->combined_sw_if_counters +
580 VNET_INTERFACE_COUNTER_TX,
581 thread_index, new_rx_sw_if_index,
582 1 /* pkts */ , n_bytes);
583 b += 4;
584 next += 4;
585 n_left_from -= 4;
586 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 while (n_left_from > 0)
588 {
Dave Barach78451a62018-07-21 09:25:00 -0400589 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
590 != new_rx_sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 {
Dave Barach78451a62018-07-21 09:25:00 -0400592 config = l2input_intf_config
593 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
594 next_index =
595 config->bridge ? VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
596 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT;
597 new_tx_sw_if_index = config->bvi ? L2INPUT_BVI : ~0;
598 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 }
Dave Barach78451a62018-07-21 09:25:00 -0400600 next[0] = next_index;
601 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
602 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
603 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
604 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
605 vnet_update_l2_len (b[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Dave Barach78451a62018-07-21 09:25:00 -0400607 vlib_increment_combined_counter (im->combined_sw_if_counters +
608 VNET_INTERFACE_COUNTER_TX,
609 thread_index, new_rx_sw_if_index,
610 1 /* pkts */ , n_bytes);
611 b += 1;
612 next += 1;
613 n_left_from -= 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614 }
615
Dave Barach78451a62018-07-21 09:25:00 -0400616 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
Dave Barach635ec3b2018-07-13 20:12:45 -0400617
618 return frame->n_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700619}
620
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700621static u8 *
622format_simulated_ethernet_name (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623{
624 u32 dev_instance = va_arg (*args, u32);
625 return format (s, "loop%d", dev_instance);
626}
627
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000628static clib_error_t *
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700629simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
630 u32 flags)
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000631{
632 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700633 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000634 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
635 return 0;
636}
637
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700638static clib_error_t *
639simulated_ethernet_mac_change (vnet_hw_interface_t * hi,
640 const u8 * old_address, const u8 * mac_address)
641{
642 l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address);
643
644 return (NULL);
645}
646
647
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700648/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
650 .name = "Loopback",
651 .format_device_name = format_simulated_ethernet_name,
652 .tx_function = simulated_ethernet_interface_tx,
Pierre Pfisterf00f91a2016-03-09 18:22:32 +0000653 .admin_up_down_function = simulated_ethernet_admin_up_down,
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700654 .mac_addr_change_function = simulated_ethernet_mac_change,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700655};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700656/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657
Dave Barach635ec3b2018-07-13 20:12:45 -0400658VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ethernet_simulated_device_class,
659 simulated_ethernet_interface_tx);
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600660
661/*
662 * Maintain a bitmap of allocated loopback instance numbers.
663 */
664#define LOOPBACK_MAX_INSTANCE (16 * 1024)
665
666static u32
667loopback_instance_alloc (u8 is_specified, u32 want)
668{
669 ethernet_main_t *em = &ethernet_main;
670
671 /*
672 * Check for dynamically allocaetd instance number.
673 */
674 if (!is_specified)
675 {
676 u32 bit;
677
678 bit = clib_bitmap_first_clear (em->bm_loopback_instances);
679 if (bit >= LOOPBACK_MAX_INSTANCE)
680 {
681 return ~0;
682 }
683 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
684 bit, 1);
685 return bit;
686 }
687
688 /*
689 * In range?
690 */
691 if (want >= LOOPBACK_MAX_INSTANCE)
692 {
693 return ~0;
694 }
695
696 /*
697 * Already in use?
698 */
699 if (clib_bitmap_get (em->bm_loopback_instances, want))
700 {
701 return ~0;
702 }
703
704 /*
705 * Grant allocation request.
706 */
707 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
708 want, 1);
709
710 return want;
711}
712
713static int
714loopback_instance_free (u32 instance)
715{
716 ethernet_main_t *em = &ethernet_main;
717
718 if (instance >= LOOPBACK_MAX_INSTANCE)
719 {
720 return -1;
721 }
722
723 if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0)
724 {
725 return -1;
726 }
727
728 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
729 instance, 0);
730 return 0;
731}
732
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700733int
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600734vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
735 u8 is_specified, u32 user_instance)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700737 vnet_main_t *vnm = vnet_get_main ();
738 vlib_main_t *vm = vlib_get_main ();
739 clib_error_t *error;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600740 u32 instance;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 u8 address[6];
742 u32 hw_if_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700743 vnet_hw_interface_t *hw_if;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 u32 slot;
745 int rv = 0;
746
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700747 ASSERT (sw_if_indexp);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700749 *sw_if_indexp = (u32) ~ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700750
751 memset (address, 0, sizeof (address));
752
753 /*
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600754 * Allocate a loopback instance. Either select on dynamically
755 * or try to use the desired user_instance number.
756 */
757 instance = loopback_instance_alloc (is_specified, user_instance);
758 if (instance == ~0)
759 {
760 return VNET_API_ERROR_INVALID_REGISTRATION;
761 }
762
763 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700764 * Default MAC address (dead:0000:0000 + instance) is allocated
765 * if zero mac_address is configured. Otherwise, user-configurable MAC
766 * address is programmed on the loopback interface.
767 */
768 if (memcmp (address, mac_address, sizeof (address)))
Damjan Marionf1213b82016-03-13 02:22:06 +0100769 clib_memcpy (address, mac_address, sizeof (address));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700770 else
771 {
772 address[0] = 0xde;
773 address[1] = 0xad;
774 address[5] = instance;
775 }
776
777 error = ethernet_register_interface
778 (vnm,
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600779 ethernet_simulated_device_class.index, instance, address, &hw_if_index,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780 /* flag change */ 0);
781
782 if (error)
783 {
784 rv = VNET_API_ERROR_INVALID_REGISTRATION;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700785 clib_error_report (error);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786 return rv;
787 }
788
789 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
790 slot = vlib_node_add_named_next_with_slot
791 (vm, hw_if->tx_node_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700792 "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
794
Dave Barach635ec3b2018-07-13 20:12:45 -0400795 slot = vlib_node_add_named_next_with_slot
796 (vm, hw_if->tx_node_index,
797 "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
798 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
799
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700801 vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802 *sw_if_indexp = si->sw_if_index;
Neale Ranns87dad112018-04-09 01:53:01 -0700803
804 /* By default don't flood to loopbacks, as packets just keep
805 * coming back ... If this loopback becomes a BVI, we'll change it */
806 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700807 }
808
809 return 0;
810}
811
812static clib_error_t *
813create_simulated_ethernet_interfaces (vlib_main_t * vm,
814 unformat_input_t * input,
815 vlib_cli_command_t * cmd)
816{
817 int rv;
818 u32 sw_if_index;
819 u8 mac_address[6];
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600820 u8 is_specified = 0;
821 u32 user_instance = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700822
823 memset (mac_address, 0, sizeof (mac_address));
824
825 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
826 {
827 if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700828 ;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600829 if (unformat (input, "instance %d", &user_instance))
830 is_specified = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700831 else
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700832 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833 }
834
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600835 rv = vnet_create_loopback_interface (&sw_if_index, mac_address,
836 is_specified, user_instance);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700837
838 if (rv)
839 return clib_error_return (0, "vnet_create_loopback_interface failed");
840
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700841 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
842 sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700843 return 0;
844}
845
Billy McFall2d085d92016-09-13 21:47:55 -0400846/*?
847 * Create a loopback interface. Optionally, a MAC Address can be
848 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
849 *
850 * @cliexpar
851 * The following two command syntaxes are equivalent:
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600852 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
853 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
Billy McFall2d085d92016-09-13 21:47:55 -0400854 * Example of how to create a loopback interface:
855 * @cliexcmd{loopback create-interface}
856?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700857/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
859 .path = "loopback create-interface",
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600860 .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700861 .function = create_simulated_ethernet_interfaces,
862};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700863/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Billy McFall2d085d92016-09-13 21:47:55 -0400865/*?
866 * Create a loopback interface. Optionally, a MAC Address can be
867 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
868 *
869 * @cliexpar
870 * The following two command syntaxes are equivalent:
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600871 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
872 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
Billy McFall2d085d92016-09-13 21:47:55 -0400873 * Example of how to create a loopback interface:
874 * @cliexcmd{create loopback interface}
875?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700876/* *INDENT-OFF* */
Alpesh Patel370a24e2016-04-08 07:27:37 -0700877VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
878 .path = "create loopback interface",
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600879 .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]",
Alpesh Patel370a24e2016-04-08 07:27:37 -0700880 .function = create_simulated_ethernet_interfaces,
881};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700882/* *INDENT-ON* */
Alpesh Patel370a24e2016-04-08 07:27:37 -0700883
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884ethernet_interface_t *
885ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
886{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700887 vnet_hw_interface_t *i =
888 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
889 return (i->hw_class_index ==
890 ethernet_hw_interface_class.
891 index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892}
893
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700894int
895vnet_delete_loopback_interface (u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700897 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700899 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
901
Eyal Barib52c0342018-07-16 19:18:56 +0300902 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
903 if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index)
904 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600905
Eyal Barib52c0342018-07-16 19:18:56 +0300906 if (loopback_instance_free (hw->dev_instance) < 0)
907 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Jon Loeligerc83c3b72017-02-23 13:57:35 -0600908
Eyal Barib52c0342018-07-16 19:18:56 +0300909 ethernet_delete_interface (vnm, hw->hw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910
911 return 0;
912}
913
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200914int
915vnet_delete_sub_interface (u32 sw_if_index)
916{
917 vnet_main_t *vnm = vnet_get_main ();
Steve Shina36f08c2018-03-07 13:29:29 -0800918 vnet_sw_interface_t *si;
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200919 int rv = 0;
920
921 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
922 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
923
Steve Shina36f08c2018-03-07 13:29:29 -0800924 si = vnet_get_sw_interface (vnm, sw_if_index);
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200925 if (si->type == VNET_SW_INTERFACE_TYPE_SUB ||
Neale Ranns17ff3c12018-07-04 10:24:24 -0700926 si->type == VNET_SW_INTERFACE_TYPE_PIPE ||
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200927 si->type == VNET_SW_INTERFACE_TYPE_P2P)
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200928 {
John Lo5957a142018-01-18 12:44:50 -0500929 vnet_interface_main_t *im = &vnm->interface_main;
930 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200931 u64 sup_and_sub_key =
932 ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
John Lo5957a142018-01-18 12:44:50 -0500933 hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
934 hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id);
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200935 vnet_delete_sw_interface (vnm, sw_if_index);
936 }
937 else
John Lo5957a142018-01-18 12:44:50 -0500938 rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
939
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200940 return rv;
941}
942
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943static clib_error_t *
944delete_simulated_ethernet_interfaces (vlib_main_t * vm,
945 unformat_input_t * input,
946 vlib_cli_command_t * cmd)
947{
948 int rv;
949 u32 sw_if_index = ~0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700950 vnet_main_t *vnm = vnet_get_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
952 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
953 {
954 if (unformat (input, "intfc %U",
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700955 unformat_vnet_sw_interface, vnm, &sw_if_index))
956 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957 else
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700958 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700959 }
960
961 if (sw_if_index == ~0)
962 return clib_error_return (0, "interface not specified");
963
964 rv = vnet_delete_loopback_interface (sw_if_index);
965
966 if (rv)
967 return clib_error_return (0, "vnet_delete_loopback_interface failed");
968
969 return 0;
970}
971
Pavel Kotucekd85590a2016-08-26 13:35:40 +0200972static clib_error_t *
973delete_sub_interface (vlib_main_t * vm,
974 unformat_input_t * input, vlib_cli_command_t * cmd)
975{
976 int rv = 0;
977 u32 sw_if_index = ~0;
978 vnet_main_t *vnm = vnet_get_main ();
979
980 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
981 {
982 if (unformat
983 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
984 ;
985 else
986 break;
987 }
988 if (sw_if_index == ~0)
989 return clib_error_return (0, "interface doesn't exist");
990
991 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
992 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
993 else
994 rv = vnet_delete_sub_interface (sw_if_index);
995 if (rv)
996 return clib_error_return (0, "delete_subinterface_interface failed");
997 return 0;
998}
999
Billy McFall2d085d92016-09-13 21:47:55 -04001000/*?
1001 * Delete a loopback interface.
1002 *
1003 * @cliexpar
1004 * The following two command syntaxes are equivalent:
1005 * @cliexcmd{loopback delete-interface intfc <interface>}
1006 * @cliexcmd{delete loopback interface intfc <interface>}
1007 * Example of how to delete a loopback interface:
1008 * @cliexcmd{loopback delete-interface intfc loop0}
1009?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001010/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
1012 .path = "loopback delete-interface",
Billy McFall2d085d92016-09-13 21:47:55 -04001013 .short_help = "loopback delete-interface intfc <interface>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014 .function = delete_simulated_ethernet_interfaces,
1015};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001016/* *INDENT-ON* */
Alpesh S. Patel633951c2016-04-12 09:58:56 -07001017
Billy McFall2d085d92016-09-13 21:47:55 -04001018/*?
1019 * Delete a loopback interface.
1020 *
1021 * @cliexpar
1022 * The following two command syntaxes are equivalent:
1023 * @cliexcmd{loopback delete-interface intfc <interface>}
1024 * @cliexcmd{delete loopback interface intfc <interface>}
1025 * Example of how to delete a loopback interface:
1026 * @cliexcmd{delete loopback interface intfc loop0}
1027?*/
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001028/* *INDENT-OFF* */
Alpesh S. Patel633951c2016-04-12 09:58:56 -07001029VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
1030 .path = "delete loopback interface",
1031 .short_help = "delete loopback interface intfc <interface>",
1032 .function = delete_simulated_ethernet_interfaces,
1033};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001034/* *INDENT-ON* */
1035
Billy McFall2d085d92016-09-13 21:47:55 -04001036/*?
1037 * Delete a sub-interface.
1038 *
1039 * @cliexpar
1040 * Example of how to delete a sub-interface:
1041 * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
1042?*/
Pavel Kotucekd85590a2016-08-26 13:35:40 +02001043/* *INDENT-OFF* */
1044VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
1045 .path = "delete sub-interface",
1046 .short_help = "delete sub-interface <interface>",
1047 .function = delete_sub_interface,
1048};
1049/* *INDENT-ON* */
1050
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001051/*
1052 * fd.io coding-style-patch-verification: ON
1053 *
1054 * Local Variables:
1055 * eval: (c-set-style "gnu")
1056 * End:
1057 */