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_node.c: ethernet packet processing
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 <vlib/vlib.h>
41 #include <vnet/pg/pg.h>
42 #include <vnet/ethernet/ethernet.h>
43 #include <vppinfra/sparse_vec.h>
44 #include <vnet/l2/l2_bvi.h>
47 #define foreach_ethernet_input_next \
48 _ (PUNT, "error-punt") \
49 _ (DROP, "error-drop") \
54 #define _(s,n) ETHERNET_INPUT_NEXT_##s,
55 foreach_ethernet_input_next
57 ETHERNET_INPUT_N_NEXT,
58 } ethernet_input_next_t;
63 } ethernet_input_trace_t;
66 format_ethernet_input_trace (u8 * s, va_list * va)
68 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
69 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
70 ethernet_input_trace_t *t = va_arg (*va, ethernet_input_trace_t *);
72 s = format (s, "%U", format_ethernet_header, t->packet_data);
77 vlib_node_registration_t ethernet_input_node;
81 ETHERNET_INPUT_VARIANT_ETHERNET,
82 ETHERNET_INPUT_VARIANT_ETHERNET_TYPE,
83 ETHERNET_INPUT_VARIANT_NOT_L2,
84 } ethernet_input_variant_t;
87 // Parse the ethernet header to extract vlan tags and innermost ethertype
88 static_always_inline void
89 parse_header (ethernet_input_variant_t variant,
93 u16 * outer_id, u16 * inner_id, u32 * match_flags)
97 if (variant == ETHERNET_INPUT_VARIANT_ETHERNET
98 || variant == ETHERNET_INPUT_VARIANT_NOT_L2)
100 ethernet_header_t *e0;
102 e0 = (void *) (b0->data + b0->current_data);
104 vnet_buffer (b0)->ethernet.start_of_ethernet_header = b0->current_data;
106 vlib_buffer_advance (b0, sizeof (e0[0]));
108 *type = clib_net_to_host_u16 (e0->type);
110 else if (variant == ETHERNET_INPUT_VARIANT_ETHERNET_TYPE)
112 // here when prior node was LLC/SNAP processing
115 e0 = (void *) (b0->data + b0->current_data);
117 vlib_buffer_advance (b0, sizeof (e0[0]));
119 *type = clib_net_to_host_u16 (e0[0]);
122 // save for distinguishing between dot1q and dot1ad later
125 // default the tags to 0 (used if there is no corresponding tag)
129 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_0_TAG;
132 // check for vlan encaps
133 if (ethernet_frame_is_tagged (*type))
135 ethernet_vlan_header_t *h0;
138 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_1_TAG;
140 h0 = (void *) (b0->data + b0->current_data);
142 tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
144 *outer_id = tag & 0xfff;
146 *match_flags &= ~SUBINT_CONFIG_MATCH_1_TAG;
148 *type = clib_net_to_host_u16 (h0->type);
150 vlib_buffer_advance (b0, sizeof (h0[0]));
153 if (*type == ETHERNET_TYPE_VLAN)
155 // Double tagged packet
156 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_2_TAG;
158 h0 = (void *) (b0->data + b0->current_data);
160 tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
162 *inner_id = tag & 0xfff;
164 *type = clib_net_to_host_u16 (h0->type);
166 vlib_buffer_advance (b0, sizeof (h0[0]));
169 if (*type == ETHERNET_TYPE_VLAN)
171 // More than double tagged packet
172 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG;
173 vlan_count = 3; // "unknown" number, aka, 3-or-more
177 ethernet_buffer_set_vlan_count (b0, vlan_count);
180 // Determine the subinterface for this packet, given the result of the
181 // vlan table lookups and vlan header parsing. Check the most specific
183 static_always_inline void
184 identify_subint (vnet_hw_interface_t * hi,
187 main_intf_t * main_intf,
188 vlan_intf_t * vlan_intf,
189 qinq_intf_t * qinq_intf,
190 u32 * new_sw_if_index, u8 * error0, u32 * is_l2)
194 matched = eth_identify_subint (hi, b0, match_flags,
195 main_intf, vlan_intf, qinq_intf,
196 new_sw_if_index, error0, is_l2);
201 // Perform L3 my-mac filter
202 // A unicast packet arriving on an L3 interface must have a dmac matching the interface mac.
203 // This is required for promiscuous mode, else we will forward packets we aren't supposed to.
206 ethernet_header_t *e0;
209 vnet_buffer (b0)->ethernet.start_of_ethernet_header);
211 if (!(ethernet_address_cast (e0->dst_address)))
213 if (!eth_mac_equal ((u8 *) e0, hi->hw_address))
215 *error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
220 // Check for down subinterface
221 *error0 = (*new_sw_if_index) != ~0 ? (*error0) : ETHERNET_ERROR_DOWN;
225 static_always_inline void
226 determine_next_node (ethernet_main_t * em,
227 ethernet_input_variant_t variant,
229 u32 type0, vlib_buffer_t * b0, u8 * error0, u8 * next0)
231 if (PREDICT_FALSE (*error0 != ETHERNET_ERROR_NONE))
233 // some error occurred
234 *next0 = ETHERNET_INPUT_NEXT_DROP;
238 *next0 = em->l2_next;
239 // record the L2 len and reset the buffer so the L2 header is preserved
240 u32 eth_start = vnet_buffer (b0)->ethernet.start_of_ethernet_header;
241 vnet_buffer (b0)->l2.l2_len = b0->current_data - eth_start;
242 vlib_buffer_advance (b0, -ethernet_buffer_header_size (b0));
244 // check for common IP/MPLS ethertypes
246 else if (type0 == ETHERNET_TYPE_IP4)
248 *next0 = em->l3_next.input_next_ip4;
250 else if (type0 == ETHERNET_TYPE_IP6)
252 *next0 = em->l3_next.input_next_ip6;
254 else if (type0 == ETHERNET_TYPE_MPLS)
256 *next0 = em->l3_next.input_next_mpls;
259 else if (em->redirect_l3)
261 // L3 Redirect is on, the cached common next nodes will be
262 // pointing to the redirect node, catch the uncommon types here
263 *next0 = em->redirect_l3_next;
267 // uncommon ethertype, check table
269 i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0);
270 *next0 = vec_elt (em->l3_next.input_next_by_type, i0);
273 SPARSE_VEC_INVALID_INDEX ? ETHERNET_ERROR_UNKNOWN_TYPE : *error0;
275 // The table is not populated with LLC values, so check that now.
276 // If variant is variant_ethernet then we came from LLC processing. Don't
277 // go back there; drop instead using by keeping the drop/bad table result.
278 if ((type0 < 0x600) && (variant == ETHERNET_INPUT_VARIANT_ETHERNET))
280 *next0 = ETHERNET_INPUT_NEXT_LLC;
285 static_always_inline uword
286 ethernet_input_inline (vlib_main_t * vm,
287 vlib_node_runtime_t * node,
288 vlib_frame_t * from_frame,
289 ethernet_input_variant_t variant)
291 vnet_main_t *vnm = vnet_get_main ();
292 ethernet_main_t *em = ðernet_main;
293 vlib_node_runtime_t *error_node;
294 u32 n_left_from, next_index, *from, *to_next;
295 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
296 u32 thread_index = vlib_get_thread_index ();
297 u32 cached_sw_if_index = ~0;
298 u32 cached_is_l2 = 0; /* shut up gcc */
299 vnet_hw_interface_t *hi = NULL; /* used for main interface only */
301 if (variant != ETHERNET_INPUT_VARIANT_ETHERNET)
302 error_node = vlib_node_get_runtime (vm, ethernet_input_node.index);
306 from = vlib_frame_vector_args (from_frame);
307 n_left_from = from_frame->n_vectors;
309 if (node->flags & VLIB_NODE_FLAG_TRACE)
310 vlib_trace_frame_buffers_only (vm, node,
314 sizeof (ethernet_input_trace_t));
316 next_index = node->cached_next_index;
317 stats_sw_if_index = node->runtime_data[0];
318 stats_n_packets = stats_n_bytes = 0;
320 while (n_left_from > 0)
324 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
326 while (n_left_from >= 4 && n_left_to_next >= 2)
329 vlib_buffer_t *b0, *b1;
330 u8 next0, next1, error0, error1;
331 u16 type0, orig_type0, type1, orig_type1;
332 u16 outer_id0, inner_id0, outer_id1, inner_id1;
333 u32 match_flags0, match_flags1;
334 u32 old_sw_if_index0, new_sw_if_index0, len0, old_sw_if_index1,
335 new_sw_if_index1, len1;
336 vnet_hw_interface_t *hi0, *hi1;
337 main_intf_t *main_intf0, *main_intf1;
338 vlan_intf_t *vlan_intf0, *vlan_intf1;
339 qinq_intf_t *qinq_intf0, *qinq_intf1;
341 ethernet_header_t *e0, *e1;
343 /* Prefetch next iteration. */
345 vlib_buffer_t *b2, *b3;
347 b2 = vlib_get_buffer (vm, from[2]);
348 b3 = vlib_get_buffer (vm, from[3]);
350 vlib_prefetch_buffer_header (b2, STORE);
351 vlib_prefetch_buffer_header (b3, STORE);
353 CLIB_PREFETCH (b2->data, sizeof (ethernet_header_t), LOAD);
354 CLIB_PREFETCH (b3->data, sizeof (ethernet_header_t), LOAD);
366 b0 = vlib_get_buffer (vm, bi0);
367 b1 = vlib_get_buffer (vm, bi1);
369 error0 = error1 = ETHERNET_ERROR_NONE;
370 e0 = vlib_buffer_get_current (b0);
371 type0 = clib_net_to_host_u16 (e0->type);
372 e1 = vlib_buffer_get_current (b1);
373 type1 = clib_net_to_host_u16 (e1->type);
375 /* Speed-path for the untagged case */
376 if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
377 && !ethernet_frame_is_tagged (type0)
378 && !ethernet_frame_is_tagged (type1)))
381 subint_config_t *subint0;
382 u32 sw_if_index0, sw_if_index1;
384 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
385 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
386 is_l20 = cached_is_l2;
388 /* This is probably wholly unnecessary */
389 if (PREDICT_FALSE (sw_if_index0 != sw_if_index1))
392 /* Now sw_if_index0 == sw_if_index1 */
393 if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
395 cached_sw_if_index = sw_if_index0;
396 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
397 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
398 subint0 = &intf0->untagged_subint;
399 cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
402 vnet_buffer (b0)->ethernet.start_of_ethernet_header =
404 vnet_buffer (b1)->ethernet.start_of_ethernet_header =
407 if (PREDICT_TRUE (is_l20 != 0))
410 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
412 vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t);
416 if (!ethernet_address_cast (e0->dst_address) &&
417 (hi->hw_address != 0) &&
418 !eth_mac_equal ((u8 *) e0, hi->hw_address))
419 error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
420 if (!ethernet_address_cast (e1->dst_address) &&
421 (hi->hw_address != 0) &&
422 !eth_mac_equal ((u8 *) e1, hi->hw_address))
423 error1 = ETHERNET_ERROR_L3_MAC_MISMATCH;
424 determine_next_node (em, variant, 0, type0, b0,
426 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
427 determine_next_node (em, variant, 0, type1, b1,
429 vlib_buffer_advance (b1, sizeof (ethernet_header_t));
434 /* Slow-path for the tagged case */
436 parse_header (variant,
439 &orig_type0, &outer_id0, &inner_id0, &match_flags0);
441 parse_header (variant,
444 &orig_type1, &outer_id1, &inner_id1, &match_flags1);
446 old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
447 old_sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
449 eth_vlan_table_lookups (em,
456 &main_intf0, &vlan_intf0, &qinq_intf0);
458 eth_vlan_table_lookups (em,
465 &main_intf1, &vlan_intf1, &qinq_intf1);
467 identify_subint (hi0,
472 qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
474 identify_subint (hi1,
479 qinq_intf1, &new_sw_if_index1, &error1, &is_l21);
481 // Save RX sw_if_index for later nodes
482 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
484 ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0;
485 vnet_buffer (b1)->sw_if_index[VLIB_RX] =
487 ETHERNET_ERROR_NONE ? old_sw_if_index1 : new_sw_if_index1;
489 // Check if there is a stat to take (valid and non-main sw_if_index for pkt 0 or pkt 1)
490 if (((new_sw_if_index0 != ~0)
491 && (new_sw_if_index0 != old_sw_if_index0))
492 || ((new_sw_if_index1 != ~0)
493 && (new_sw_if_index1 != old_sw_if_index1)))
496 len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
497 - vnet_buffer (b0)->ethernet.start_of_ethernet_header;
498 len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data
499 - vnet_buffer (b1)->ethernet.start_of_ethernet_header;
501 stats_n_packets += 2;
502 stats_n_bytes += len0 + len1;
505 (!(new_sw_if_index0 == stats_sw_if_index
506 && new_sw_if_index1 == stats_sw_if_index)))
508 stats_n_packets -= 2;
509 stats_n_bytes -= len0 + len1;
511 if (new_sw_if_index0 != old_sw_if_index0
512 && new_sw_if_index0 != ~0)
513 vlib_increment_combined_counter (vnm->
514 interface_main.combined_sw_if_counters
516 VNET_INTERFACE_COUNTER_RX,
520 if (new_sw_if_index1 != old_sw_if_index1
521 && new_sw_if_index1 != ~0)
522 vlib_increment_combined_counter (vnm->
523 interface_main.combined_sw_if_counters
525 VNET_INTERFACE_COUNTER_RX,
530 if (new_sw_if_index0 == new_sw_if_index1)
532 if (stats_n_packets > 0)
534 vlib_increment_combined_counter
535 (vnm->interface_main.combined_sw_if_counters
536 + VNET_INTERFACE_COUNTER_RX,
539 stats_n_packets, stats_n_bytes);
540 stats_n_packets = stats_n_bytes = 0;
542 stats_sw_if_index = new_sw_if_index0;
547 if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
550 determine_next_node (em, variant, is_l20, type0, b0, &error0,
552 determine_next_node (em, variant, is_l21, type1, b1, &error1,
556 b0->error = error_node->errors[error0];
557 b1->error = error_node->errors[error1];
559 // verify speculative enqueue
560 vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
561 n_left_to_next, bi0, bi1, next0,
565 while (n_left_from > 0 && n_left_to_next > 0)
570 u16 type0, orig_type0;
571 u16 outer_id0, inner_id0;
573 u32 old_sw_if_index0, new_sw_if_index0, len0;
574 vnet_hw_interface_t *hi0;
575 main_intf_t *main_intf0;
576 vlan_intf_t *vlan_intf0;
577 qinq_intf_t *qinq_intf0;
578 ethernet_header_t *e0;
581 // Prefetch next iteration
586 p2 = vlib_get_buffer (vm, from[1]);
587 vlib_prefetch_buffer_header (p2, STORE);
588 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
598 b0 = vlib_get_buffer (vm, bi0);
600 error0 = ETHERNET_ERROR_NONE;
601 e0 = vlib_buffer_get_current (b0);
602 type0 = clib_net_to_host_u16 (e0->type);
604 /* Speed-path for the untagged case */
605 if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
606 && !ethernet_frame_is_tagged (type0)))
609 subint_config_t *subint0;
612 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
613 is_l20 = cached_is_l2;
615 if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
617 cached_sw_if_index = sw_if_index0;
618 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
619 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
620 subint0 = &intf0->untagged_subint;
621 cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
624 vnet_buffer (b0)->ethernet.start_of_ethernet_header =
627 if (PREDICT_TRUE (is_l20 != 0))
630 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
634 if (!ethernet_address_cast (e0->dst_address) &&
635 (hi->hw_address != 0) &&
636 !eth_mac_equal ((u8 *) e0, hi->hw_address))
637 error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
638 determine_next_node (em, variant, 0, type0, b0,
640 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
645 /* Slow-path for the tagged case */
646 parse_header (variant,
649 &orig_type0, &outer_id0, &inner_id0, &match_flags0);
651 old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
653 eth_vlan_table_lookups (em,
660 &main_intf0, &vlan_intf0, &qinq_intf0);
662 identify_subint (hi0,
667 qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
669 // Save RX sw_if_index for later nodes
670 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
672 ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0;
674 // Increment subinterface stats
675 // Note that interface-level counters have already been incremented
676 // prior to calling this function. Thus only subinterface counters
677 // are incremented here.
679 // Interface level counters include packets received on the main
680 // interface and all subinterfaces. Subinterface level counters
681 // include only those packets received on that subinterface
682 // Increment stats if the subint is valid and it is not the main intf
683 if ((new_sw_if_index0 != ~0)
684 && (new_sw_if_index0 != old_sw_if_index0))
687 len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
688 - vnet_buffer (b0)->ethernet.start_of_ethernet_header;
690 stats_n_packets += 1;
691 stats_n_bytes += len0;
693 // Batch stat increments from the same subinterface so counters
694 // don't need to be incremented for every packet.
695 if (PREDICT_FALSE (new_sw_if_index0 != stats_sw_if_index))
697 stats_n_packets -= 1;
698 stats_n_bytes -= len0;
700 if (new_sw_if_index0 != ~0)
701 vlib_increment_combined_counter
702 (vnm->interface_main.combined_sw_if_counters
703 + VNET_INTERFACE_COUNTER_RX,
704 thread_index, new_sw_if_index0, 1, len0);
705 if (stats_n_packets > 0)
707 vlib_increment_combined_counter
708 (vnm->interface_main.combined_sw_if_counters
709 + VNET_INTERFACE_COUNTER_RX,
711 stats_sw_if_index, stats_n_packets, stats_n_bytes);
712 stats_n_packets = stats_n_bytes = 0;
714 stats_sw_if_index = new_sw_if_index0;
718 if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
721 determine_next_node (em, variant, is_l20, type0, b0, &error0,
725 b0->error = error_node->errors[error0];
727 // verify speculative enqueue
728 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
729 to_next, n_left_to_next,
733 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
736 // Increment any remaining batched stats
737 if (stats_n_packets > 0)
739 vlib_increment_combined_counter
740 (vnm->interface_main.combined_sw_if_counters
741 + VNET_INTERFACE_COUNTER_RX,
742 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
743 node->runtime_data[0] = stats_sw_if_index;
746 return from_frame->n_vectors;
750 ethernet_input (vlib_main_t * vm,
751 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
753 return ethernet_input_inline (vm, node, from_frame,
754 ETHERNET_INPUT_VARIANT_ETHERNET);
758 ethernet_input_type (vlib_main_t * vm,
759 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
761 return ethernet_input_inline (vm, node, from_frame,
762 ETHERNET_INPUT_VARIANT_ETHERNET_TYPE);
766 ethernet_input_not_l2 (vlib_main_t * vm,
767 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
769 return ethernet_input_inline (vm, node, from_frame,
770 ETHERNET_INPUT_VARIANT_NOT_L2);
774 // Return the subinterface config struct for the given sw_if_index
775 // Also return via parameter the appropriate match flags for the
776 // configured number of tags.
777 // On error (unsupported or not ethernet) return 0.
778 static subint_config_t *
779 ethernet_sw_interface_get_config (vnet_main_t * vnm,
781 u32 * flags, u32 * unsupported)
783 ethernet_main_t *em = ðernet_main;
784 vnet_hw_interface_t *hi;
785 vnet_sw_interface_t *si;
786 main_intf_t *main_intf;
787 vlan_table_t *vlan_table;
788 qinq_table_t *qinq_table;
789 subint_config_t *subint = 0;
791 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
793 if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
796 goto done; // non-ethernet interface
799 // ensure there's an entry for the main intf (shouldn't really be necessary)
800 vec_validate (em->main_intfs, hi->hw_if_index);
801 main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
803 // Locate the subint for the given ethernet config
804 si = vnet_get_sw_interface (vnm, sw_if_index);
806 if (si->sub.eth.flags.default_sub)
808 subint = &main_intf->default_subint;
809 *flags = SUBINT_CONFIG_MATCH_0_TAG |
810 SUBINT_CONFIG_MATCH_1_TAG |
811 SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG;
813 else if ((si->sub.eth.flags.no_tags) || (si->sub.eth.raw_flags == 0))
815 // if no flags are set then this is a main interface
816 // so treat as untagged
817 subint = &main_intf->untagged_subint;
818 *flags = SUBINT_CONFIG_MATCH_0_TAG;
823 // first get the vlan table
824 if (si->sub.eth.flags.dot1ad)
826 if (main_intf->dot1ad_vlans == 0)
828 // Allocate a vlan table from the pool
829 pool_get (em->vlan_pool, vlan_table);
830 main_intf->dot1ad_vlans = vlan_table - em->vlan_pool;
834 // Get ptr to existing vlan table
836 vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
841 if (main_intf->dot1q_vlans == 0)
843 // Allocate a vlan table from the pool
844 pool_get (em->vlan_pool, vlan_table);
845 main_intf->dot1q_vlans = vlan_table - em->vlan_pool;
849 // Get ptr to existing vlan table
851 vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
855 if (si->sub.eth.flags.one_tag)
857 *flags = si->sub.eth.flags.exact_match ?
858 SUBINT_CONFIG_MATCH_1_TAG :
859 (SUBINT_CONFIG_MATCH_1_TAG |
860 SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG);
862 if (si->sub.eth.flags.outer_vlan_id_any)
864 // not implemented yet
870 // a single vlan, a common case
872 &vlan_table->vlans[si->sub.eth.
873 outer_vlan_id].single_tag_subint;
880 *flags = si->sub.eth.flags.exact_match ?
881 SUBINT_CONFIG_MATCH_2_TAG :
882 (SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG);
884 if (si->sub.eth.flags.outer_vlan_id_any
885 && si->sub.eth.flags.inner_vlan_id_any)
887 // not implemented yet
892 if (si->sub.eth.flags.inner_vlan_id_any)
894 // a specific outer and "any" inner
895 // don't need a qinq table for this
897 &vlan_table->vlans[si->sub.eth.
898 outer_vlan_id].inner_any_subint;
899 if (si->sub.eth.flags.exact_match)
901 *flags = SUBINT_CONFIG_MATCH_2_TAG;
905 *flags = SUBINT_CONFIG_MATCH_2_TAG |
906 SUBINT_CONFIG_MATCH_3_TAG;
911 // a specific outer + specifc innner vlan id, a common case
913 // get the qinq table
914 if (vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs == 0)
916 // Allocate a qinq table from the pool
917 pool_get (em->qinq_pool, qinq_table);
918 vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs =
919 qinq_table - em->qinq_pool;
923 // Get ptr to existing qinq table
925 vec_elt_at_index (em->qinq_pool,
926 vlan_table->vlans[si->sub.
930 subint = &qinq_table->vlans[si->sub.eth.inner_vlan_id].subint;
940 ethernet_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
942 subint_config_t *subint;
945 clib_error_t *error = 0;
947 // Find the config for this subinterface
949 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
954 // not implemented yet or not ethernet
958 subint->sw_if_index =
959 ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? sw_if_index : ~0);
965 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_sw_interface_up_down);
968 // Set the L2/L3 mode for the subinterface
970 ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index, u32 l2)
972 subint_config_t *subint;
976 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
978 is_port = !(sw->type == VNET_SW_INTERFACE_TYPE_SUB);
980 // Find the config for this subinterface
982 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
987 // unimplemented or not ethernet
991 // Double check that the config we found is for our interface (or the interface is down)
992 ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0));
996 subint->flags |= SUBINT_CONFIG_L2;
999 SUBINT_CONFIG_MATCH_0_TAG | SUBINT_CONFIG_MATCH_1_TAG
1000 | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG;
1004 subint->flags &= ~SUBINT_CONFIG_L2;
1007 ~(SUBINT_CONFIG_MATCH_1_TAG | SUBINT_CONFIG_MATCH_2_TAG
1008 | SUBINT_CONFIG_MATCH_3_TAG);
1016 * Set the L2/L3 mode for the subinterface regardless of port
1019 ethernet_sw_interface_set_l2_mode_noport (vnet_main_t * vnm,
1020 u32 sw_if_index, u32 l2)
1022 subint_config_t *subint;
1026 /* Find the config for this subinterface */
1028 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1033 /* unimplemented or not ethernet */
1038 * Double check that the config we found is for our interface (or the
1039 * interface is down)
1041 ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0));
1045 subint->flags |= SUBINT_CONFIG_L2;
1049 subint->flags &= ~SUBINT_CONFIG_L2;
1056 static clib_error_t *
1057 ethernet_sw_interface_add_del (vnet_main_t * vnm,
1058 u32 sw_if_index, u32 is_create)
1060 clib_error_t *error = 0;
1061 subint_config_t *subint;
1063 u32 unsupported = 0;
1065 // Find the config for this subinterface
1067 ethernet_sw_interface_get_config (vnm, sw_if_index, &match_flags,
1072 // not implemented yet or not ethernet
1075 // this is the NYI case
1076 error = clib_error_return (0, "not implemented yet");
1087 // Initialize the subint
1088 if (subint->flags & SUBINT_CONFIG_VALID)
1090 // Error vlan already in use
1091 error = clib_error_return (0, "vlan is already in use");
1095 // Note that config is L3 by defaulty
1096 subint->flags = SUBINT_CONFIG_VALID | match_flags;
1097 subint->sw_if_index = ~0; // because interfaces are initially down
1104 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ethernet_sw_interface_add_del);
1106 static char *ethernet_error_strings[] = {
1107 #define ethernet_error(n,c,s) s,
1108 #include "error.def"
1109 #undef ethernet_error
1113 VLIB_REGISTER_NODE (ethernet_input_node) = {
1114 .function = ethernet_input,
1115 .name = "ethernet-input",
1116 /* Takes a vector of packets. */
1117 .vector_size = sizeof (u32),
1118 .n_errors = ETHERNET_N_ERROR,
1119 .error_strings = ethernet_error_strings,
1120 .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1122 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1123 foreach_ethernet_input_next
1126 .format_buffer = format_ethernet_header_with_length,
1127 .format_trace = format_ethernet_input_trace,
1128 .unformat_buffer = unformat_ethernet_header,
1133 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_node, ethernet_input)
1137 VLIB_REGISTER_NODE (ethernet_input_type_node, static) = {
1138 .function = ethernet_input_type,
1139 .name = "ethernet-input-type",
1140 /* Takes a vector of packets. */
1141 .vector_size = sizeof (u32),
1142 .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1144 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1145 foreach_ethernet_input_next
1152 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_type_node, ethernet_input_type)
1156 VLIB_REGISTER_NODE (ethernet_input_not_l2_node, static) = {
1157 .function = ethernet_input_not_l2,
1158 .name = "ethernet-input-not-l2",
1159 /* Takes a vector of packets. */
1160 .vector_size = sizeof (u32),
1161 .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1163 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1164 foreach_ethernet_input_next
1172 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_not_l2_node,
1173 ethernet_input_not_l2)
1178 ethernet_set_rx_redirect (vnet_main_t * vnm,
1179 vnet_hw_interface_t * hi, u32 enable)
1181 // Insure all packets go to ethernet-input (i.e. untagged ipv4 packets
1182 // don't go directly to ip4-input)
1183 vnet_hw_interface_rx_redirect_to_node
1184 (vnm, hi->hw_if_index, enable ? ethernet_input_node.index : ~0);
1189 * Initialization and registration for the next_by_ethernet structure
1193 next_by_ethertype_init (next_by_ethertype_t * l3_next)
1195 l3_next->input_next_by_type = sparse_vec_new
1196 ( /* elt bytes */ sizeof (l3_next->input_next_by_type[0]),
1197 /* bits in index */ BITS (((ethernet_header_t *) 0)->type));
1199 vec_validate (l3_next->sparse_index_by_input_next_index,
1200 ETHERNET_INPUT_NEXT_DROP);
1201 vec_validate (l3_next->sparse_index_by_input_next_index,
1202 ETHERNET_INPUT_NEXT_PUNT);
1203 l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_DROP] =
1204 SPARSE_VEC_INVALID_INDEX;
1205 l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_PUNT] =
1206 SPARSE_VEC_INVALID_INDEX;
1209 * Make sure we don't wipe out an ethernet registration by mistake
1210 * Can happen if init function ordering constraints are missing.
1214 ethernet_main_t *em = ðernet_main;
1215 ASSERT (em->next_by_ethertype_register_called == 0);
1221 // Add an ethertype -> next index mapping to the structure
1223 next_by_ethertype_register (next_by_ethertype_t * l3_next,
1224 u32 ethertype, u32 next_index)
1228 ethernet_main_t *em = ðernet_main;
1232 ethernet_main_t *em = ðernet_main;
1233 em->next_by_ethertype_register_called = 1;
1236 /* Setup ethernet type -> next index sparse vector mapping. */
1237 n = sparse_vec_validate (l3_next->input_next_by_type, ethertype);
1240 /* Rebuild next index -> sparse index inverse mapping when sparse vector
1242 vec_validate (l3_next->sparse_index_by_input_next_index, next_index);
1243 for (i = 1; i < vec_len (l3_next->input_next_by_type); i++)
1245 sparse_index_by_input_next_index[l3_next->input_next_by_type[i]] = i;
1247 // do not allow the cached next index's to be updated if L3
1248 // redirect is enabled, as it will have overwritten them
1249 if (!em->redirect_l3)
1251 // Cache common ethertypes directly
1252 if (ethertype == ETHERNET_TYPE_IP4)
1254 l3_next->input_next_ip4 = next_index;
1256 else if (ethertype == ETHERNET_TYPE_IP6)
1258 l3_next->input_next_ip6 = next_index;
1260 else if (ethertype == ETHERNET_TYPE_MPLS)
1262 l3_next->input_next_mpls = next_index;
1269 static clib_error_t *
1270 ethernet_input_init (vlib_main_t * vm)
1272 ethernet_main_t *em = ðernet_main;
1273 __attribute__ ((unused)) vlan_table_t *invalid_vlan_table;
1274 __attribute__ ((unused)) qinq_table_t *invalid_qinq_table;
1276 ethernet_setup_node (vm, ethernet_input_node.index);
1277 ethernet_setup_node (vm, ethernet_input_type_node.index);
1278 ethernet_setup_node (vm, ethernet_input_not_l2_node.index);
1280 next_by_ethertype_init (&em->l3_next);
1282 // Initialize pools and vector for vlan parsing
1283 vec_validate (em->main_intfs, 10); // 10 main interfaces
1284 pool_alloc (em->vlan_pool, 10);
1285 pool_alloc (em->qinq_pool, 1);
1287 // The first vlan pool will always be reserved for an invalid table
1288 pool_get (em->vlan_pool, invalid_vlan_table); // first id = 0
1289 // The first qinq pool will always be reserved for an invalid table
1290 pool_get (em->qinq_pool, invalid_qinq_table); // first id = 0
1295 VLIB_INIT_FUNCTION (ethernet_input_init);
1298 ethernet_register_input_type (vlib_main_t * vm,
1299 ethernet_type_t type, u32 node_index)
1301 ethernet_main_t *em = ðernet_main;
1302 ethernet_type_info_t *ti;
1306 clib_error_t *error = vlib_call_init_function (vm, ethernet_init);
1308 clib_error_report (error);
1311 ti = ethernet_get_type_info (em, type);
1312 ti->node_index = node_index;
1313 ti->next_index = vlib_node_add_next (vm,
1314 ethernet_input_node.index, node_index);
1315 i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1316 ASSERT (i == ti->next_index);
1318 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1319 ASSERT (i == ti->next_index);
1321 // Add the L3 node for this ethertype to the next nodes structure
1322 next_by_ethertype_register (&em->l3_next, type, ti->next_index);
1324 // Call the registration functions for other nodes that want a mapping
1325 l2bvi_register_input_type (vm, type, node_index);
1329 ethernet_register_l2_input (vlib_main_t * vm, u32 node_index)
1331 ethernet_main_t *em = ðernet_main;
1335 vlib_node_add_next (vm, ethernet_input_node.index, node_index);
1338 * Even if we never use these arcs, we have to align the next indices...
1340 i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1342 ASSERT (i == em->l2_next);
1344 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1345 ASSERT (i == em->l2_next);
1348 // Register a next node for L3 redirect, and enable L3 redirect
1350 ethernet_register_l3_redirect (vlib_main_t * vm, u32 node_index)
1352 ethernet_main_t *em = ðernet_main;
1355 em->redirect_l3 = 1;
1356 em->redirect_l3_next = vlib_node_add_next (vm,
1357 ethernet_input_node.index,
1360 * Change the cached next nodes to the redirect node
1362 em->l3_next.input_next_ip4 = em->redirect_l3_next;
1363 em->l3_next.input_next_ip6 = em->redirect_l3_next;
1364 em->l3_next.input_next_mpls = em->redirect_l3_next;
1367 * Even if we never use these arcs, we have to align the next indices...
1369 i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1371 ASSERT (i == em->redirect_l3_next);
1373 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1375 ASSERT (i == em->redirect_l3_next);
1379 * fd.io coding-style-patch-verification: ON
1382 * eval: (c-set-style "gnu")