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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * ethernet_interface.c: ethernet interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vnet/vnet.h>
41 #include <vnet/ip/ip.h>
42 #include <vnet/pg/pg.h>
43 #include <vnet/ethernet/ethernet.h>
44 //#include <vnet/ethernet/arp.h>
45 #include <vnet/l2/l2_input.h>
46 #include <vnet/l2/l2_bd.h>
47 #include <vnet/adj/adj.h>
48 #include <vnet/adj/adj_mcast.h>
49 #include <vnet/ip-neighbor/ip_neighbor.h>
53 * @brief Loopback Interfaces.
55 * This file contains code to manage loopback interfaces.
59 ethernet_ip4_mcast_dst_addr (void)
61 const static u8 ethernet_mcast_dst_mac[] = {
62 0x1, 0x0, 0x5e, 0x0, 0x0, 0x0,
65 return (ethernet_mcast_dst_mac);
69 ethernet_ip6_mcast_dst_addr (void)
71 const static u8 ethernet_mcast_dst_mac[] = {
72 0x33, 0x33, 0x00, 0x0, 0x0, 0x0,
75 return (ethernet_mcast_dst_mac);
79 * @brief build a rewrite string to use for sending packets of type 'link_type'
83 ethernet_build_rewrite (vnet_main_t * vnm,
85 vnet_link_t link_type, const void *dst_address)
87 vnet_sw_interface_t *sub_sw = vnet_get_sw_interface (vnm, sw_if_index);
88 vnet_sw_interface_t *sup_sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
89 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
90 ethernet_main_t *em = ðernet_main;
91 ethernet_interface_t *ei;
94 uword n_bytes = sizeof (h[0]);
98 if ((sub_sw->type == VNET_SW_INTERFACE_TYPE_P2P) ||
99 (sub_sw->type == VNET_SW_INTERFACE_TYPE_PIPE))
101 if (sub_sw != sup_sw)
103 if (sub_sw->sub.eth.flags.one_tag)
105 n_bytes += sizeof (ethernet_vlan_header_t);
107 else if (sub_sw->sub.eth.flags.two_tags)
109 n_bytes += 2 * (sizeof (ethernet_vlan_header_t));
111 else if (PREDICT_FALSE (is_p2p))
113 n_bytes = sizeof (ethernet_header_t);
115 if (PREDICT_FALSE (!is_p2p))
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))
128 n_bytes = sizeof (ethernet_header_t);
134 #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
144 vec_validate (rewrite, n_bytes - 1);
145 h = (ethernet_header_t *) rewrite;
146 ei = pool_elt_at_index (em->interfaces, hw->hw_instance);
147 clib_memcpy (h->src_address, &ei->address, sizeof (h->src_address));
150 clib_memcpy (h->dst_address, sub_sw->p2p.client_mac,
151 sizeof (h->dst_address));
156 clib_memcpy (h->dst_address, dst_address, sizeof (h->dst_address));
158 clib_memset (h->dst_address, ~0, sizeof (h->dst_address)); /* broadcast */
161 if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.one_tag)
163 ethernet_vlan_header_t *outer = (void *) (h + 1);
165 h->type = sub_sw->sub.eth.flags.dot1ad ?
166 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
167 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
168 outer->priority_cfi_and_id =
169 clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
170 outer->type = clib_host_to_net_u16 (type);
173 else if (PREDICT_FALSE (!is_p2p) && sub_sw->sub.eth.flags.two_tags)
175 ethernet_vlan_header_t *outer = (void *) (h + 1);
176 ethernet_vlan_header_t *inner = (void *) (outer + 1);
178 h->type = sub_sw->sub.eth.flags.dot1ad ?
179 clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD) :
180 clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
181 outer->priority_cfi_and_id =
182 clib_host_to_net_u16 (sub_sw->sub.eth.outer_vlan_id);
183 outer->type = clib_host_to_net_u16 (ETHERNET_TYPE_VLAN);
184 inner->priority_cfi_and_id =
185 clib_host_to_net_u16 (sub_sw->sub.eth.inner_vlan_id);
186 inner->type = clib_host_to_net_u16 (type);
191 h->type = clib_host_to_net_u16 (type);
198 ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai)
200 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
202 if ((si->type == VNET_SW_INTERFACE_TYPE_P2P) ||
203 (si->type == VNET_SW_INTERFACE_TYPE_PIPE))
205 default_update_adjacency (vnm, sw_if_index, ai);
213 switch (adj->lookup_next_index)
215 case IP_LOOKUP_NEXT_GLEAN:
216 adj_glean_update_rewrite (ai);
218 case IP_LOOKUP_NEXT_ARP:
219 case IP_LOOKUP_NEXT_REWRITE:
220 ip_neighbor_update (vnm, ai);
222 case IP_LOOKUP_NEXT_BCAST:
223 adj_nbr_update_rewrite (ai,
224 ADJ_NBR_REWRITE_FLAG_COMPLETE,
225 ethernet_build_rewrite
227 adj->rewrite_header.sw_if_index,
229 VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST));
231 case IP_LOOKUP_NEXT_MCAST:
234 * Construct a partial rewrite from the known ethernet mcast dest MAC
239 rewrite = ethernet_build_rewrite
243 (adj->ia_nh_proto == FIB_PROTOCOL_IP6 ?
244 ethernet_ip6_mcast_dst_addr () :
245 ethernet_ip4_mcast_dst_addr ()));
248 * Complete the remaining fields of the adj's rewrite to direct the
249 * complete of the rewrite at switch time by copying in the IP
250 * dst address's bytes.
251 * Ofset is 2 bytes into the destintation address.
253 offset = vec_len (rewrite) - 2;
254 adj_mcast_update_rewrite (ai, rewrite, offset);
258 case IP_LOOKUP_NEXT_DROP:
259 case IP_LOOKUP_NEXT_PUNT:
260 case IP_LOOKUP_NEXT_LOCAL:
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:
272 ethernet_interface_address_copy (ethernet_interface_address_t * dst,
275 clib_memcpy (&dst->mac, (u8 *) mac, sizeof (dst->mac));
277 * ethernet dataplane loads mac as u64, makes sure the last 2 bytes are 0
278 * for comparison purpose
284 ethernet_set_mac (vnet_hw_interface_t * hi, ethernet_interface_t * ei,
285 const u8 * mac_address)
287 vec_validate (hi->hw_address, sizeof (mac_address_t) - 1);
288 clib_memcpy (hi->hw_address, mac_address, sizeof (mac_address_t));
289 ethernet_interface_address_copy (&ei->address, mac_address);
292 static clib_error_t *
293 ethernet_mac_change (vnet_hw_interface_t * hi,
294 const u8 * old_address, const u8 * mac_address)
296 ethernet_interface_t *ei;
300 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
302 ethernet_set_mac (hi, ei, mac_address);
305 ethernet_address_change_ctx_t *cb;
306 vec_foreach (cb, em->address_change_callbacks)
307 cb->function (em, hi->sw_if_index, cb->function_opaque);
314 VNET_HW_INTERFACE_CLASS (ethernet_hw_interface_class) = {
316 .tx_hash_fn_type = VNET_HASH_FN_TYPE_ETHERNET,
317 .format_address = format_ethernet_address,
318 .format_header = format_ethernet_header_with_length,
319 .unformat_hw_address = unformat_ethernet_address,
320 .unformat_header = unformat_ethernet_header,
321 .build_rewrite = ethernet_build_rewrite,
322 .update_adjacency = ethernet_update_adjacency,
323 .mac_addr_change_function = ethernet_mac_change,
328 unformat_ethernet_interface (unformat_input_t * input, va_list * args)
330 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
331 u32 *result = va_arg (*args, u32 *);
333 ethernet_main_t *em = ðernet_main;
334 ethernet_interface_t *eif;
336 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
339 eif = ethernet_get_interface (em, hw_if_index);
342 *result = hw_if_index;
349 vnet_eth_register_interface (vnet_main_t *vnm,
350 vnet_eth_interface_registration_t *r)
352 ethernet_main_t *em = ðernet_main;
353 ethernet_interface_t *ei;
354 vnet_hw_interface_t *hi;
357 pool_get (em->interfaces, ei);
358 clib_memcpy (&ei->cb, &r->cb, sizeof (vnet_eth_if_callbacks_t));
360 hw_if_index = vnet_register_interface (
361 vnm, r->dev_class_index, r->dev_instance,
362 ethernet_hw_interface_class.index, ei - em->interfaces);
364 hi = vnet_get_hw_interface (vnm, hw_if_index);
366 ethernet_setup_node (vnm->vlib_main, hi->output_node_index);
368 hi->min_packet_bytes = hi->min_supported_packet_bytes =
369 ETHERNET_MIN_PACKET_BYTES;
370 hi->max_packet_bytes = hi->max_supported_packet_bytes =
371 ETHERNET_MAX_PACKET_BYTES;
373 /* Default ethernet MTU, 9000 unless set by ethernet_config see below */
374 vnet_sw_interface_set_mtu (vnm, hi->sw_if_index, em->default_mtu);
376 ethernet_set_mac (hi, ei, r->address);
381 ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index)
383 ethernet_main_t *em = ðernet_main;
384 ethernet_interface_t *ei;
385 vnet_hw_interface_t *hi;
386 main_intf_t *main_intf;
387 vlan_table_t *vlan_table;
390 hi = vnet_get_hw_interface (vnm, hw_if_index);
391 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
393 /* Delete vlan mapping table for dot1q and dot1ad. */
394 main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
395 if (main_intf->dot1q_vlans)
397 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
398 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
400 if (vlan_table->vlans[idx].qinqs)
402 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
403 vlan_table->vlans[idx].qinqs = 0;
406 pool_put_index (em->vlan_pool, main_intf->dot1q_vlans);
407 main_intf->dot1q_vlans = 0;
409 if (main_intf->dot1ad_vlans)
411 vlan_table = vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
412 for (idx = 0; idx < ETHERNET_N_VLAN; idx++)
414 if (vlan_table->vlans[idx].qinqs)
416 pool_put_index (em->qinq_pool, vlan_table->vlans[idx].qinqs);
417 vlan_table->vlans[idx].qinqs = 0;
420 pool_put_index (em->vlan_pool, main_intf->dot1ad_vlans);
421 main_intf->dot1ad_vlans = 0;
424 vnet_delete_hw_interface (vnm, hw_if_index);
425 pool_put (em->interfaces, ei);
429 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
431 ethernet_main_t *em = ðernet_main;
432 vnet_hw_interface_t *hi;
433 ethernet_interface_t *ei;
434 u32 opn_flags = flags & ETHERNET_INTERFACE_FLAGS_SET_OPN_MASK;
436 hi = vnet_get_hw_interface (vnm, hw_if_index);
438 ASSERT (hi->hw_class_index == ethernet_hw_interface_class.index);
440 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
442 /* preserve status bits and update last set operation bits */
443 ei->flags = (ei->flags & ETHERNET_INTERFACE_FLAGS_STATUS_MASK) | opn_flags;
445 if (ei->cb.flag_change)
449 case ETHERNET_INTERFACE_FLAG_DEFAULT_L3:
450 if (hi->caps & VNET_HW_IF_CAP_MAC_FILTER)
452 if (ei->cb.flag_change (vnm, hi, opn_flags) != ~0)
454 ei->flags |= ETHERNET_INTERFACE_FLAG_STATUS_L3;
457 ei->flags &= ~ETHERNET_INTERFACE_FLAG_STATUS_L3;
461 case ETHERNET_INTERFACE_FLAG_ACCEPT_ALL:
462 ei->flags &= ~ETHERNET_INTERFACE_FLAG_STATUS_L3;
464 case ETHERNET_INTERFACE_FLAG_MTU:
465 return ei->cb.flag_change (vnm, hi, opn_flags);
474 * Echo packets back to ethernet/l2-input.
477 simulated_ethernet_interface_tx (vlib_main_t * vm,
478 vlib_node_runtime_t *
479 node, vlib_frame_t * frame)
481 u32 n_left_from, *from;
484 u32 thread_index = vm->thread_index;
485 vnet_main_t *vnm = vnet_get_main ();
486 vnet_interface_main_t *im = &vnm->interface_main;
487 l2_input_config_t *config;
488 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
489 u16 nexts[VLIB_FRAME_SIZE], *next;
490 u32 new_rx_sw_if_index = ~0;
491 u32 new_tx_sw_if_index = ~0;
493 n_left_from = frame->n_vectors;
494 from = vlib_frame_vector_args (frame);
496 vlib_get_buffers (vm, from, bufs, n_left_from);
500 /* Ordinarily, this is the only config lookup. */
501 config = l2input_intf_config (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
502 next_index = (l2_input_is_bridge (config) ?
503 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
504 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
505 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
506 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
508 while (n_left_from >= 4)
510 u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
511 u32 not_all_match_config;
513 /* Prefetch next iteration. */
514 if (PREDICT_TRUE (n_left_from >= 8))
516 vlib_prefetch_buffer_header (b[4], STORE);
517 vlib_prefetch_buffer_header (b[5], STORE);
518 vlib_prefetch_buffer_header (b[6], STORE);
519 vlib_prefetch_buffer_header (b[7], STORE);
522 /* Make sure all pkts were transmitted on the same (loop) intfc */
523 sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
524 sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
525 sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
526 sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
528 not_all_match_config = (sw_if_index0 ^ sw_if_index1)
529 ^ (sw_if_index2 ^ sw_if_index3);
530 not_all_match_config += sw_if_index0 ^ new_rx_sw_if_index;
532 /* Speed path / expected case: all pkts on the same intfc */
533 if (PREDICT_TRUE (not_all_match_config == 0))
535 next[0] = next_index;
536 next[1] = next_index;
537 next[2] = next_index;
538 next[3] = next_index;
539 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
540 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
541 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
542 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
543 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
544 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
545 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
546 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
547 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
548 n_bytes += vlib_buffer_length_in_chain (vm, b[1]);
549 n_bytes += vlib_buffer_length_in_chain (vm, b[2]);
550 n_bytes += vlib_buffer_length_in_chain (vm, b[3]);
552 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
554 vnet_update_l2_len (b[0]);
555 vnet_update_l2_len (b[1]);
556 vnet_update_l2_len (b[2]);
557 vnet_update_l2_len (b[3]);
560 /* increment TX interface stat */
561 vlib_increment_combined_counter (im->combined_sw_if_counters +
562 VNET_INTERFACE_COUNTER_TX,
563 thread_index, new_rx_sw_if_index,
564 4 /* pkts */ , n_bytes);
572 * Slow path: we know that at least one of the pkts
573 * was transmitted on a different sw_if_index, so
574 * check each sw_if_index against the cached data and proceed
577 * This shouldn't happen, but code can (and does) bypass the
578 * per-interface output node, so deal with it.
580 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
581 != new_rx_sw_if_index))
583 config = l2input_intf_config
584 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
585 next_index = (l2_input_is_bridge (config) ?
586 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
587 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
588 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
589 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
591 next[0] = next_index;
592 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
593 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
594 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
595 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
596 vnet_update_l2_len (b[0]);
598 vlib_increment_combined_counter (im->combined_sw_if_counters +
599 VNET_INTERFACE_COUNTER_TX,
600 thread_index, new_rx_sw_if_index,
601 1 /* pkts */ , n_bytes);
603 if (PREDICT_FALSE (vnet_buffer (b[1])->sw_if_index[VLIB_TX]
604 != new_rx_sw_if_index))
606 config = l2input_intf_config
607 (vnet_buffer (b[1])->sw_if_index[VLIB_TX]);
608 next_index = (l2_input_is_bridge (config) ?
609 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
610 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
611 new_rx_sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_TX];
612 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
614 next[1] = next_index;
615 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
616 vnet_buffer (b[1])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
617 n_bytes = vlib_buffer_length_in_chain (vm, b[1]);
618 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
619 vnet_update_l2_len (b[1]);
621 vlib_increment_combined_counter (im->combined_sw_if_counters +
622 VNET_INTERFACE_COUNTER_TX,
623 thread_index, new_rx_sw_if_index,
624 1 /* pkts */ , n_bytes);
626 if (PREDICT_FALSE (vnet_buffer (b[2])->sw_if_index[VLIB_TX]
627 != new_rx_sw_if_index))
629 config = l2input_intf_config
630 (vnet_buffer (b[2])->sw_if_index[VLIB_TX]);
631 next_index = (l2_input_is_bridge (config) ?
632 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
633 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
634 new_rx_sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_TX];
635 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
637 next[2] = next_index;
638 vnet_buffer (b[2])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
639 vnet_buffer (b[2])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
640 n_bytes = vlib_buffer_length_in_chain (vm, b[2]);
641 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
642 vnet_update_l2_len (b[2]);
644 vlib_increment_combined_counter (im->combined_sw_if_counters +
645 VNET_INTERFACE_COUNTER_TX,
646 thread_index, new_rx_sw_if_index,
647 1 /* pkts */ , n_bytes);
649 if (PREDICT_FALSE (vnet_buffer (b[3])->sw_if_index[VLIB_TX]
650 != new_rx_sw_if_index))
652 config = l2input_intf_config
653 (vnet_buffer (b[3])->sw_if_index[VLIB_TX]);
654 next_index = (l2_input_is_bridge (config) ?
655 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
656 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
657 new_rx_sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_TX];
658 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
660 next[3] = next_index;
661 vnet_buffer (b[3])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
662 vnet_buffer (b[3])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
663 n_bytes = vlib_buffer_length_in_chain (vm, b[3]);
664 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
665 vnet_update_l2_len (b[3]);
667 vlib_increment_combined_counter (im->combined_sw_if_counters +
668 VNET_INTERFACE_COUNTER_TX,
669 thread_index, new_rx_sw_if_index,
670 1 /* pkts */ , n_bytes);
675 while (n_left_from > 0)
677 if (PREDICT_FALSE (vnet_buffer (b[0])->sw_if_index[VLIB_TX]
678 != new_rx_sw_if_index))
680 config = l2input_intf_config
681 (vnet_buffer (b[0])->sw_if_index[VLIB_TX]);
682 next_index = (l2_input_is_bridge (config) ?
683 VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT :
684 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
685 new_tx_sw_if_index = l2_input_is_bvi (config) ? L2INPUT_BVI : ~0;
686 new_rx_sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
688 next[0] = next_index;
689 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = new_rx_sw_if_index;
690 vnet_buffer (b[0])->sw_if_index[VLIB_TX] = new_tx_sw_if_index;
691 n_bytes = vlib_buffer_length_in_chain (vm, b[0]);
692 if (next_index == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT)
693 vnet_update_l2_len (b[0]);
695 vlib_increment_combined_counter (im->combined_sw_if_counters +
696 VNET_INTERFACE_COUNTER_TX,
697 thread_index, new_rx_sw_if_index,
698 1 /* pkts */ , n_bytes);
704 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
706 return frame->n_vectors;
710 format_simulated_ethernet_name (u8 * s, va_list * args)
712 u32 dev_instance = va_arg (*args, u32);
713 return format (s, "loop%d", dev_instance);
716 static clib_error_t *
717 simulated_ethernet_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
720 u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
721 VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
722 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
726 static clib_error_t *
727 simulated_ethernet_mac_change (vnet_hw_interface_t * hi,
728 const u8 * old_address, const u8 * mac_address)
730 l2input_interface_mac_change (hi->sw_if_index, old_address, mac_address);
737 VNET_DEVICE_CLASS (ethernet_simulated_device_class) = {
739 .format_device_name = format_simulated_ethernet_name,
740 .tx_function = simulated_ethernet_interface_tx,
741 .admin_up_down_function = simulated_ethernet_admin_up_down,
742 .mac_addr_change_function = simulated_ethernet_mac_change,
747 * Maintain a bitmap of allocated loopback instance numbers.
749 #define LOOPBACK_MAX_INSTANCE (16 * 1024)
752 loopback_instance_alloc (u8 is_specified, u32 want)
754 ethernet_main_t *em = ðernet_main;
757 * Check for dynamically allocaetd instance number.
763 bit = clib_bitmap_first_clear (em->bm_loopback_instances);
764 if (bit >= LOOPBACK_MAX_INSTANCE)
768 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
776 if (want >= LOOPBACK_MAX_INSTANCE)
784 if (clib_bitmap_get (em->bm_loopback_instances, want))
790 * Grant allocation request.
792 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
799 loopback_instance_free (u32 instance)
801 ethernet_main_t *em = ðernet_main;
803 if (instance >= LOOPBACK_MAX_INSTANCE)
808 if (clib_bitmap_get (em->bm_loopback_instances, instance) == 0)
813 em->bm_loopback_instances = clib_bitmap_set (em->bm_loopback_instances,
819 vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
820 u8 is_specified, u32 user_instance)
822 vnet_main_t *vnm = vnet_get_main ();
823 vlib_main_t *vm = vlib_get_main ();
827 vnet_hw_interface_t *hw_if;
830 ASSERT (sw_if_indexp);
832 *sw_if_indexp = (u32) ~ 0;
834 clib_memset (address, 0, sizeof (address));
837 * Allocate a loopback instance. Either select on dynamically
838 * or try to use the desired user_instance number.
840 instance = loopback_instance_alloc (is_specified, user_instance);
843 return VNET_API_ERROR_INVALID_REGISTRATION;
847 * Default MAC address (dead:0000:0000 + instance) is allocated
848 * if zero mac_address is configured. Otherwise, user-configurable MAC
849 * address is programmed on the loopback interface.
851 if (memcmp (address, mac_address, sizeof (address)))
852 clib_memcpy (address, mac_address, sizeof (address));
857 address[5] = instance;
860 vnet_eth_interface_registration_t eir = {};
861 eir.dev_class_index = ethernet_simulated_device_class.index;
862 eir.dev_instance = instance;
863 eir.address = address;
864 hw_if_index = vnet_eth_register_interface (vnm, &eir);
865 hw_if = vnet_get_hw_interface (vnm, hw_if_index);
866 slot = vlib_node_add_named_next_with_slot
867 (vm, hw_if->tx_node_index,
868 "ethernet-input", VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
869 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
871 slot = vlib_node_add_named_next_with_slot
872 (vm, hw_if->tx_node_index,
873 "l2-input", VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
874 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT);
877 vnet_sw_interface_t *si = vnet_get_hw_sw_interface (vnm, hw_if_index);
878 *sw_if_indexp = si->sw_if_index;
880 /* By default don't flood to loopbacks, as packets just keep
881 * coming back ... If this loopback becomes a BVI, we'll change it */
882 si->flood_class = VNET_FLOOD_CLASS_NO_FLOOD;
888 static clib_error_t *
889 create_simulated_ethernet_interfaces (vlib_main_t * vm,
890 unformat_input_t * input,
891 vlib_cli_command_t * cmd)
897 u32 user_instance = 0;
899 clib_memset (mac_address, 0, sizeof (mac_address));
901 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
903 if (unformat (input, "mac %U", unformat_ethernet_address, mac_address))
905 if (unformat (input, "instance %d", &user_instance))
911 rv = vnet_create_loopback_interface (&sw_if_index, mac_address,
912 is_specified, user_instance);
915 return clib_error_return (0, "vnet_create_loopback_interface failed");
917 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
923 * Create a loopback interface. Optionally, a MAC Address can be
924 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
927 * The following two command syntaxes are equivalent:
928 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
929 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
930 * Example of how to create a loopback interface:
931 * @cliexcmd{loopback create-interface}
934 VLIB_CLI_COMMAND (create_simulated_ethernet_interface_command, static) = {
935 .path = "loopback create-interface",
936 .short_help = "loopback create-interface [mac <mac-addr>] [instance <instance>]",
937 .function = create_simulated_ethernet_interfaces,
942 * Create a loopback interface. Optionally, a MAC Address can be
943 * provided. If not provided, de:ad:00:00:00:<loopId> will be used.
946 * The following two command syntaxes are equivalent:
947 * @cliexcmd{loopback create-interface [mac <mac-addr>] [instance <instance>]}
948 * @cliexcmd{create loopback interface [mac <mac-addr>] [instance <instance>]}
949 * Example of how to create a loopback interface:
950 * @cliexcmd{create loopback interface}
953 VLIB_CLI_COMMAND (create_loopback_interface_command, static) = {
954 .path = "create loopback interface",
955 .short_help = "create loopback interface [mac <mac-addr>] [instance <instance>]",
956 .function = create_simulated_ethernet_interfaces,
960 ethernet_interface_t *
961 ethernet_get_interface (ethernet_main_t * em, u32 hw_if_index)
963 vnet_hw_interface_t *i =
964 vnet_get_hw_interface (vnet_get_main (), hw_if_index);
965 return (i->hw_class_index ==
966 ethernet_hw_interface_class.
967 index ? pool_elt_at_index (em->interfaces, i->hw_instance) : 0);
971 ethernet_interface_add_del_address (ethernet_main_t * em,
972 u32 hw_if_index, const u8 * address,
975 ethernet_interface_t *ei = ethernet_get_interface (em, hw_if_index);
976 ethernet_interface_address_t *if_addr = 0;
979 /* return if there is not an ethernet interface for this hw interface */
983 /* determine whether the address is configured on the interface */
984 vec_foreach (if_addr, ei->secondary_addrs)
986 if (ethernet_mac_address_equal (if_addr->mac.bytes, address))
997 /* address not found yet: add it */
998 vec_add2 (ei->secondary_addrs, if_addr, 1);
999 ethernet_interface_address_copy (if_addr, address);
1001 return &if_addr->mac;
1006 vec_delete (ei->secondary_addrs, 1, if_addr - ei->secondary_addrs);
1012 vnet_delete_loopback_interface (u32 sw_if_index)
1014 vnet_main_t *vnm = vnet_get_main ();
1016 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1017 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1019 vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
1020 if (hw == 0 || hw->dev_class_index != ethernet_simulated_device_class.index)
1021 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1023 if (loopback_instance_free (hw->dev_instance) < 0)
1024 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1026 ethernet_delete_interface (vnm, hw->hw_if_index);
1032 vnet_create_sub_interface (u32 sw_if_index, u32 id,
1033 u32 flags, u16 inner_vlan_id, u16 outer_vlan_id,
1034 u32 * sub_sw_if_index)
1036 vnet_main_t *vnm = vnet_get_main ();
1037 vnet_interface_main_t *im = &vnm->interface_main;
1038 vnet_hw_interface_t *hi;
1039 u64 sup_and_sub_key = ((u64) (sw_if_index) << 32) | (u64) id;
1040 vnet_sw_interface_t template;
1044 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1046 p = hash_get_mem (im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1049 return (VNET_API_ERROR_VLAN_ALREADY_EXISTS);
1052 clib_memset (&template, 0, sizeof (template));
1053 template.type = VNET_SW_INTERFACE_TYPE_SUB;
1054 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
1055 template.sup_sw_if_index = sw_if_index;
1056 template.sub.id = id;
1057 template.sub.eth.raw_flags = flags;
1058 template.sub.eth.outer_vlan_id = outer_vlan_id;
1059 template.sub.eth.inner_vlan_id = inner_vlan_id;
1061 if (vnet_create_sw_interface (vnm, &template, sub_sw_if_index))
1062 return (VNET_API_ERROR_UNSPECIFIED);
1064 kp = clib_mem_alloc (sizeof (*kp));
1065 *kp = sup_and_sub_key;
1067 hash_set (hi->sub_interface_sw_if_index_by_id, id, *sub_sw_if_index);
1068 hash_set_mem (im->sw_if_index_by_sup_and_sub, kp, *sub_sw_if_index);
1074 vnet_delete_sub_interface (u32 sw_if_index)
1076 vnet_main_t *vnm = vnet_get_main ();
1077 vnet_sw_interface_t *si;
1080 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1081 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
1083 si = vnet_get_sw_interface (vnm, sw_if_index);
1084 if (si->type == VNET_SW_INTERFACE_TYPE_SUB ||
1085 si->type == VNET_SW_INTERFACE_TYPE_PIPE ||
1086 si->type == VNET_SW_INTERFACE_TYPE_P2P)
1088 vnet_interface_main_t *im = &vnm->interface_main;
1089 vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1090 u64 sup_and_sub_key =
1091 ((u64) (si->sup_sw_if_index) << 32) | (u64) si->sub.id;
1092 hash_unset_mem_free (&im->sw_if_index_by_sup_and_sub, &sup_and_sub_key);
1093 hash_unset (hi->sub_interface_sw_if_index_by_id, si->sub.id);
1094 vnet_delete_sw_interface (vnm, sw_if_index);
1097 rv = VNET_API_ERROR_INVALID_SUB_SW_IF_INDEX;
1102 static clib_error_t *
1103 delete_simulated_ethernet_interfaces (vlib_main_t * vm,
1104 unformat_input_t * input,
1105 vlib_cli_command_t * cmd)
1108 u32 sw_if_index = ~0;
1109 vnet_main_t *vnm = vnet_get_main ();
1111 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1113 if (unformat (input, "intfc %U",
1114 unformat_vnet_sw_interface, vnm, &sw_if_index))
1120 if (sw_if_index == ~0)
1121 return clib_error_return (0, "interface not specified");
1123 rv = vnet_delete_loopback_interface (sw_if_index);
1126 return clib_error_return (0, "vnet_delete_loopback_interface failed");
1131 static clib_error_t *
1132 delete_sub_interface (vlib_main_t * vm,
1133 unformat_input_t * input, vlib_cli_command_t * cmd)
1136 u32 sw_if_index = ~0;
1137 vnet_main_t *vnm = vnet_get_main ();
1139 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1142 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1147 if (sw_if_index == ~0)
1148 return clib_error_return (0, "interface doesn't exist");
1150 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
1151 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
1153 rv = vnet_delete_sub_interface (sw_if_index);
1155 return clib_error_return (0, "delete_subinterface_interface failed");
1160 * Delete a loopback interface.
1163 * The following two command syntaxes are equivalent:
1164 * @cliexcmd{loopback delete-interface intfc <interface>}
1165 * @cliexcmd{delete loopback interface intfc <interface>}
1166 * Example of how to delete a loopback interface:
1167 * @cliexcmd{loopback delete-interface intfc loop0}
1170 VLIB_CLI_COMMAND (delete_simulated_ethernet_interface_command, static) = {
1171 .path = "loopback delete-interface",
1172 .short_help = "loopback delete-interface intfc <interface>",
1173 .function = delete_simulated_ethernet_interfaces,
1178 * Delete a loopback interface.
1181 * The following two command syntaxes are equivalent:
1182 * @cliexcmd{loopback delete-interface intfc <interface>}
1183 * @cliexcmd{delete loopback interface intfc <interface>}
1184 * Example of how to delete a loopback interface:
1185 * @cliexcmd{delete loopback interface intfc loop0}
1188 VLIB_CLI_COMMAND (delete_loopback_interface_command, static) = {
1189 .path = "delete loopback interface",
1190 .short_help = "delete loopback interface intfc <interface>",
1191 .function = delete_simulated_ethernet_interfaces,
1196 * Delete a sub-interface.
1199 * Example of how to delete a sub-interface:
1200 * @cliexcmd{delete sub-interface GigabitEthernet0/8/0.200}
1203 VLIB_CLI_COMMAND (delete_sub_interface_command, static) = {
1204 .path = "delete sub-interface",
1205 .short_help = "delete sub-interface <interface>",
1206 .function = delete_sub_interface,
1210 /* ethernet { ... } configuration. */
1213 * @cfgcmd{default-mtu <n>}
1214 * Specify the default mtu in the range of 64-9000. The default is 9000 bytes.
1217 static clib_error_t *
1218 ethernet_config (vlib_main_t * vm, unformat_input_t * input)
1220 ethernet_main_t *em = ðernet_main;
1222 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1224 if (unformat (input, "default-mtu %u", &em->default_mtu))
1226 if (em->default_mtu < 64 || em->default_mtu > 9000)
1227 return clib_error_return (0, "default MTU must be >=64, <=9000");
1231 return clib_error_return (0, "unknown input '%U'",
1232 format_unformat_error, input);
1238 VLIB_CONFIG_FUNCTION (ethernet_config, "ethernet");
1241 * fd.io coding-style-patch-verification: ON
1244 * eval: (c-set-style "gnu")