Fix clang implicit conversion errors
[vpp.git] / src / vnet / ethernet / node.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * ethernet_node.c: ethernet packet processing
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 <vlib/vlib.h>
41 #include <vnet/pg/pg.h>
42 #include <vnet/ethernet/ethernet.h>
43 #include <vnet/ethernet/p2p_ethernet.h>
44 #include <vppinfra/sparse_vec.h>
45 #include <vnet/l2/l2_bvi.h>
46
47
48 #define foreach_ethernet_input_next             \
49   _ (PUNT, "error-punt")                        \
50   _ (DROP, "error-drop")                        \
51   _ (LLC, "llc-input")
52
53 typedef enum
54 {
55 #define _(s,n) ETHERNET_INPUT_NEXT_##s,
56   foreach_ethernet_input_next
57 #undef _
58     ETHERNET_INPUT_N_NEXT,
59 } ethernet_input_next_t;
60
61 typedef struct
62 {
63   u8 packet_data[32];
64 } ethernet_input_trace_t;
65
66 static u8 *
67 format_ethernet_input_trace (u8 * s, va_list * va)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
71   ethernet_input_trace_t *t = va_arg (*va, ethernet_input_trace_t *);
72
73   s = format (s, "%U", format_ethernet_header, t->packet_data);
74
75   return s;
76 }
77
78 vlib_node_registration_t ethernet_input_node;
79
80 typedef enum
81 {
82   ETHERNET_INPUT_VARIANT_ETHERNET,
83   ETHERNET_INPUT_VARIANT_ETHERNET_TYPE,
84   ETHERNET_INPUT_VARIANT_NOT_L2,
85 } ethernet_input_variant_t;
86
87
88 // Parse the ethernet header to extract vlan tags and innermost ethertype
89 static_always_inline void
90 parse_header (ethernet_input_variant_t variant,
91               vlib_buffer_t * b0,
92               u16 * type,
93               u16 * orig_type,
94               u16 * outer_id, u16 * inner_id, u32 * match_flags)
95 {
96   u8 vlan_count;
97
98   if (variant == ETHERNET_INPUT_VARIANT_ETHERNET
99       || variant == ETHERNET_INPUT_VARIANT_NOT_L2)
100     {
101       ethernet_header_t *e0;
102
103       e0 = (void *) (b0->data + b0->current_data);
104
105       vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
106       b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID;
107
108       vlib_buffer_advance (b0, sizeof (e0[0]));
109
110       *type = clib_net_to_host_u16 (e0->type);
111     }
112   else if (variant == ETHERNET_INPUT_VARIANT_ETHERNET_TYPE)
113     {
114       // here when prior node was LLC/SNAP processing
115       u16 *e0;
116
117       e0 = (void *) (b0->data + b0->current_data);
118
119       vlib_buffer_advance (b0, sizeof (e0[0]));
120
121       *type = clib_net_to_host_u16 (e0[0]);
122     }
123
124   // save for distinguishing between dot1q and dot1ad later
125   *orig_type = *type;
126
127   // default the tags to 0 (used if there is no corresponding tag)
128   *outer_id = 0;
129   *inner_id = 0;
130
131   *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_0_TAG;
132   vlan_count = 0;
133
134   // check for vlan encaps
135   if (ethernet_frame_is_tagged (*type))
136     {
137       ethernet_vlan_header_t *h0;
138       u16 tag;
139
140       *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_1_TAG;
141
142       h0 = (void *) (b0->data + b0->current_data);
143
144       tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
145
146       *outer_id = tag & 0xfff;
147       if (0 == *outer_id)
148         *match_flags &= ~SUBINT_CONFIG_MATCH_1_TAG;
149
150       *type = clib_net_to_host_u16 (h0->type);
151
152       vlib_buffer_advance (b0, sizeof (h0[0]));
153       vlan_count = 1;
154
155       if (*type == ETHERNET_TYPE_VLAN)
156         {
157           // Double tagged packet
158           *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_2_TAG;
159
160           h0 = (void *) (b0->data + b0->current_data);
161
162           tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
163
164           *inner_id = tag & 0xfff;
165
166           *type = clib_net_to_host_u16 (h0->type);
167
168           vlib_buffer_advance (b0, sizeof (h0[0]));
169           vlan_count = 2;
170           if (*type == ETHERNET_TYPE_VLAN)
171             {
172               // More than double tagged packet
173               *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG;
174
175               vlib_buffer_advance (b0, sizeof (h0[0]));
176               vlan_count = 3;   // "unknown" number, aka, 3-or-more
177             }
178         }
179     }
180   ethernet_buffer_set_vlan_count (b0, vlan_count);
181 }
182
183 // Determine the subinterface for this packet, given the result of the
184 // vlan table lookups and vlan header parsing. Check the most specific
185 // matches first.
186 static_always_inline void
187 identify_subint (vnet_hw_interface_t * hi,
188                  vlib_buffer_t * b0,
189                  u32 match_flags,
190                  main_intf_t * main_intf,
191                  vlan_intf_t * vlan_intf,
192                  qinq_intf_t * qinq_intf,
193                  u32 * new_sw_if_index, u8 * error0, u32 * is_l2)
194 {
195   u32 matched;
196
197   matched = eth_identify_subint (hi, b0, match_flags,
198                                  main_intf, vlan_intf, qinq_intf,
199                                  new_sw_if_index, error0, is_l2);
200
201   if (matched)
202     {
203
204       // Perform L3 my-mac filter
205       // A unicast packet arriving on an L3 interface must have a dmac matching the interface mac.
206       // This is required for promiscuous mode, else we will forward packets we aren't supposed to.
207       if (!(*is_l2))
208         {
209           ethernet_header_t *e0;
210           e0 = (void *) (b0->data + vnet_buffer (b0)->l2_hdr_offset);
211
212           if (!(ethernet_address_cast (e0->dst_address)))
213             {
214               if (!eth_mac_equal ((u8 *) e0, hi->hw_address))
215                 {
216                   *error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
217                 }
218             }
219         }
220
221       // Check for down subinterface
222       *error0 = (*new_sw_if_index) != ~0 ? (*error0) : ETHERNET_ERROR_DOWN;
223     }
224 }
225
226 static_always_inline void
227 determine_next_node (ethernet_main_t * em,
228                      ethernet_input_variant_t variant,
229                      u32 is_l20,
230                      u32 type0, vlib_buffer_t * b0, u8 * error0, u8 * next0)
231 {
232   u32 eth_start = vnet_buffer (b0)->l2_hdr_offset;
233   vnet_buffer (b0)->l2.l2_len = b0->current_data - eth_start;
234   if (PREDICT_FALSE (*error0 != ETHERNET_ERROR_NONE))
235     {
236       // some error occurred
237       *next0 = ETHERNET_INPUT_NEXT_DROP;
238     }
239   else if (is_l20)
240     {
241       *next0 = em->l2_next;
242       // record the L2 len and reset the buffer so the L2 header is preserved
243       ASSERT (vnet_buffer (b0)->l2.l2_len ==
244               ethernet_buffer_header_size (b0));
245       vlib_buffer_advance (b0, -ethernet_buffer_header_size (b0));
246
247       // check for common IP/MPLS ethertypes
248     }
249   else if (type0 == ETHERNET_TYPE_IP4)
250     {
251       *next0 = em->l3_next.input_next_ip4;
252     }
253   else if (type0 == ETHERNET_TYPE_IP6)
254     {
255       *next0 = em->l3_next.input_next_ip6;
256     }
257   else if (type0 == ETHERNET_TYPE_MPLS)
258     {
259       *next0 = em->l3_next.input_next_mpls;
260
261     }
262   else if (em->redirect_l3)
263     {
264       // L3 Redirect is on, the cached common next nodes will be
265       // pointing to the redirect node, catch the uncommon types here
266       *next0 = em->redirect_l3_next;
267     }
268   else
269     {
270       // uncommon ethertype, check table
271       u32 i0;
272       i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0);
273       *next0 = vec_elt (em->l3_next.input_next_by_type, i0);
274       *error0 =
275         i0 ==
276         SPARSE_VEC_INVALID_INDEX ? ETHERNET_ERROR_UNKNOWN_TYPE : *error0;
277
278       // The table is not populated with LLC values, so check that now.
279       // If variant is variant_ethernet then we came from LLC processing. Don't
280       // go back there; drop instead using by keeping the drop/bad table result.
281       if ((type0 < 0x600) && (variant == ETHERNET_INPUT_VARIANT_ETHERNET))
282         {
283           *next0 = ETHERNET_INPUT_NEXT_LLC;
284         }
285     }
286 }
287
288 static_always_inline int
289 ethernet_frame_is_any_tagged (u16 type0, u16 type1)
290 {
291 #if __SSE4_2__
292   const __m128i ethertype_mask = _mm_set_epi16 ((u16) ETHERNET_TYPE_VLAN,
293                                                 (u16) ETHERNET_TYPE_DOT1AD,
294                                                 (u16) ETHERNET_TYPE_VLAN_9100,
295                                                 (u16) ETHERNET_TYPE_VLAN_9200,
296                                                 /* duplicate for type1 */
297                                                 (u16) ETHERNET_TYPE_VLAN,
298                                                 (u16) ETHERNET_TYPE_DOT1AD,
299                                                 (u16) ETHERNET_TYPE_VLAN_9100,
300                                                 (u16)
301                                                 ETHERNET_TYPE_VLAN_9200);
302
303   __m128i r =
304     _mm_set_epi16 (type0, type0, type0, type0, type1, type1, type1, type1);
305   r = _mm_cmpeq_epi16 (ethertype_mask, r);
306   return !_mm_test_all_zeros (r, r);
307 #else
308   return ethernet_frame_is_tagged (type0) || ethernet_frame_is_tagged (type1);
309 #endif
310 }
311
312 static_always_inline uword
313 ethernet_input_inline (vlib_main_t * vm,
314                        vlib_node_runtime_t * node,
315                        vlib_frame_t * from_frame,
316                        ethernet_input_variant_t variant)
317 {
318   vnet_main_t *vnm = vnet_get_main ();
319   ethernet_main_t *em = &ethernet_main;
320   vlib_node_runtime_t *error_node;
321   u32 n_left_from, next_index, *from, *to_next;
322   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
323   u32 thread_index = vlib_get_thread_index ();
324   u32 cached_sw_if_index = ~0;
325   u32 cached_is_l2 = 0;         /* shut up gcc */
326   vnet_hw_interface_t *hi = NULL;       /* used for main interface only */
327
328   if (variant != ETHERNET_INPUT_VARIANT_ETHERNET)
329     error_node = vlib_node_get_runtime (vm, ethernet_input_node.index);
330   else
331     error_node = node;
332
333   from = vlib_frame_vector_args (from_frame);
334   n_left_from = from_frame->n_vectors;
335
336   if (node->flags & VLIB_NODE_FLAG_TRACE)
337     vlib_trace_frame_buffers_only (vm, node,
338                                    from,
339                                    n_left_from,
340                                    sizeof (from[0]),
341                                    sizeof (ethernet_input_trace_t));
342
343   next_index = node->cached_next_index;
344   stats_sw_if_index = node->runtime_data[0];
345   stats_n_packets = stats_n_bytes = 0;
346
347   while (n_left_from > 0)
348     {
349       u32 n_left_to_next;
350
351       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
352
353       while (n_left_from >= 4 && n_left_to_next >= 2)
354         {
355           u32 bi0, bi1;
356           vlib_buffer_t *b0, *b1;
357           u8 next0, next1, error0, error1;
358           u16 type0, orig_type0, type1, orig_type1;
359           u16 outer_id0, inner_id0, outer_id1, inner_id1;
360           u32 match_flags0, match_flags1;
361           u32 old_sw_if_index0, new_sw_if_index0, len0, old_sw_if_index1,
362             new_sw_if_index1, len1;
363           vnet_hw_interface_t *hi0, *hi1;
364           main_intf_t *main_intf0, *main_intf1;
365           vlan_intf_t *vlan_intf0, *vlan_intf1;
366           qinq_intf_t *qinq_intf0, *qinq_intf1;
367           u32 is_l20, is_l21;
368           ethernet_header_t *e0, *e1;
369
370           /* Prefetch next iteration. */
371           {
372             vlib_buffer_t *b2, *b3;
373
374             b2 = vlib_get_buffer (vm, from[2]);
375             b3 = vlib_get_buffer (vm, from[3]);
376
377             vlib_prefetch_buffer_header (b2, STORE);
378             vlib_prefetch_buffer_header (b3, STORE);
379
380             CLIB_PREFETCH (b2->data, sizeof (ethernet_header_t), LOAD);
381             CLIB_PREFETCH (b3->data, sizeof (ethernet_header_t), LOAD);
382           }
383
384           bi0 = from[0];
385           bi1 = from[1];
386           to_next[0] = bi0;
387           to_next[1] = bi1;
388           from += 2;
389           to_next += 2;
390           n_left_to_next -= 2;
391           n_left_from -= 2;
392
393           b0 = vlib_get_buffer (vm, bi0);
394           b1 = vlib_get_buffer (vm, bi1);
395
396           error0 = error1 = ETHERNET_ERROR_NONE;
397           e0 = vlib_buffer_get_current (b0);
398           type0 = clib_net_to_host_u16 (e0->type);
399           e1 = vlib_buffer_get_current (b1);
400           type1 = clib_net_to_host_u16 (e1->type);
401
402           /* Speed-path for the untagged case */
403           if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
404                             && !ethernet_frame_is_any_tagged (type0, type1)))
405             {
406               main_intf_t *intf0;
407               subint_config_t *subint0;
408               u32 sw_if_index0, sw_if_index1;
409
410               sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
411               sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
412               is_l20 = cached_is_l2;
413
414               /* This is probably wholly unnecessary */
415               if (PREDICT_FALSE (sw_if_index0 != sw_if_index1))
416                 goto slowpath;
417
418               /* Now sw_if_index0 == sw_if_index1  */
419               if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
420                 {
421                   cached_sw_if_index = sw_if_index0;
422                   hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
423                   intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
424                   subint0 = &intf0->untagged_subint;
425                   cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
426                 }
427
428               vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
429               vnet_buffer (b1)->l2_hdr_offset = b1->current_data;
430               vnet_buffer (b0)->l3_hdr_offset =
431                 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
432               vnet_buffer (b1)->l3_hdr_offset =
433                 vnet_buffer (b1)->l2_hdr_offset + sizeof (ethernet_header_t);
434               b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
435                 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
436               b1->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
437                 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
438
439               if (PREDICT_TRUE (is_l20 != 0))
440                 {
441                   next0 = em->l2_next;
442                   vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
443                   next1 = em->l2_next;
444                   vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t);
445                 }
446               else
447                 {
448                   if (!ethernet_address_cast (e0->dst_address) &&
449                       (hi->hw_address != 0) &&
450                       !eth_mac_equal ((u8 *) e0, hi->hw_address))
451                     error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
452                   if (!ethernet_address_cast (e1->dst_address) &&
453                       (hi->hw_address != 0) &&
454                       !eth_mac_equal ((u8 *) e1, hi->hw_address))
455                     error1 = ETHERNET_ERROR_L3_MAC_MISMATCH;
456                   determine_next_node (em, variant, 0, type0, b0,
457                                        &error0, &next0);
458                   vlib_buffer_advance (b0, sizeof (ethernet_header_t));
459                   determine_next_node (em, variant, 0, type1, b1,
460                                        &error1, &next1);
461                   vlib_buffer_advance (b1, sizeof (ethernet_header_t));
462                 }
463               goto ship_it01;
464             }
465
466           /* Slow-path for the tagged case */
467         slowpath:
468           parse_header (variant,
469                         b0,
470                         &type0,
471                         &orig_type0, &outer_id0, &inner_id0, &match_flags0);
472
473           parse_header (variant,
474                         b1,
475                         &type1,
476                         &orig_type1, &outer_id1, &inner_id1, &match_flags1);
477
478           old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
479           old_sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
480
481           eth_vlan_table_lookups (em,
482                                   vnm,
483                                   old_sw_if_index0,
484                                   orig_type0,
485                                   outer_id0,
486                                   inner_id0,
487                                   &hi0,
488                                   &main_intf0, &vlan_intf0, &qinq_intf0);
489
490           eth_vlan_table_lookups (em,
491                                   vnm,
492                                   old_sw_if_index1,
493                                   orig_type1,
494                                   outer_id1,
495                                   inner_id1,
496                                   &hi1,
497                                   &main_intf1, &vlan_intf1, &qinq_intf1);
498
499           identify_subint (hi0,
500                            b0,
501                            match_flags0,
502                            main_intf0,
503                            vlan_intf0,
504                            qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
505
506           identify_subint (hi1,
507                            b1,
508                            match_flags1,
509                            main_intf1,
510                            vlan_intf1,
511                            qinq_intf1, &new_sw_if_index1, &error1, &is_l21);
512
513           // Save RX sw_if_index for later nodes
514           vnet_buffer (b0)->sw_if_index[VLIB_RX] =
515             error0 !=
516             ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0;
517           vnet_buffer (b1)->sw_if_index[VLIB_RX] =
518             error1 !=
519             ETHERNET_ERROR_NONE ? old_sw_if_index1 : new_sw_if_index1;
520
521           // Check if there is a stat to take (valid and non-main sw_if_index for pkt 0 or pkt 1)
522           if (((new_sw_if_index0 != ~0)
523                && (new_sw_if_index0 != old_sw_if_index0))
524               || ((new_sw_if_index1 != ~0)
525                   && (new_sw_if_index1 != old_sw_if_index1)))
526             {
527
528               len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
529                 - vnet_buffer (b0)->l2_hdr_offset;
530               len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data
531                 - vnet_buffer (b1)->l2_hdr_offset;
532
533               stats_n_packets += 2;
534               stats_n_bytes += len0 + len1;
535
536               if (PREDICT_FALSE
537                   (!(new_sw_if_index0 == stats_sw_if_index
538                      && new_sw_if_index1 == stats_sw_if_index)))
539                 {
540                   stats_n_packets -= 2;
541                   stats_n_bytes -= len0 + len1;
542
543                   if (new_sw_if_index0 != old_sw_if_index0
544                       && new_sw_if_index0 != ~0)
545                     vlib_increment_combined_counter (vnm->
546                                                      interface_main.combined_sw_if_counters
547                                                      +
548                                                      VNET_INTERFACE_COUNTER_RX,
549                                                      thread_index,
550                                                      new_sw_if_index0, 1,
551                                                      len0);
552                   if (new_sw_if_index1 != old_sw_if_index1
553                       && new_sw_if_index1 != ~0)
554                     vlib_increment_combined_counter (vnm->
555                                                      interface_main.combined_sw_if_counters
556                                                      +
557                                                      VNET_INTERFACE_COUNTER_RX,
558                                                      thread_index,
559                                                      new_sw_if_index1, 1,
560                                                      len1);
561
562                   if (new_sw_if_index0 == new_sw_if_index1)
563                     {
564                       if (stats_n_packets > 0)
565                         {
566                           vlib_increment_combined_counter
567                             (vnm->interface_main.combined_sw_if_counters
568                              + VNET_INTERFACE_COUNTER_RX,
569                              thread_index,
570                              stats_sw_if_index,
571                              stats_n_packets, stats_n_bytes);
572                           stats_n_packets = stats_n_bytes = 0;
573                         }
574                       stats_sw_if_index = new_sw_if_index0;
575                     }
576                 }
577             }
578
579           if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
580             is_l20 = is_l21 = 0;
581
582           determine_next_node (em, variant, is_l20, type0, b0, &error0,
583                                &next0);
584           determine_next_node (em, variant, is_l21, type1, b1, &error1,
585                                &next1);
586           vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset +
587             vnet_buffer (b0)->l2.l2_len;
588           vnet_buffer (b1)->l3_hdr_offset = vnet_buffer (b1)->l2_hdr_offset +
589             vnet_buffer (b1)->l2.l2_len;
590           b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
591           b1->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
592
593         ship_it01:
594           b0->error = error_node->errors[error0];
595           b1->error = error_node->errors[error1];
596
597           // verify speculative enqueue
598           vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
599                                            n_left_to_next, bi0, bi1, next0,
600                                            next1);
601         }
602
603       while (n_left_from > 0 && n_left_to_next > 0)
604         {
605           u32 bi0;
606           vlib_buffer_t *b0;
607           u8 error0, next0;
608           u16 type0, orig_type0;
609           u16 outer_id0, inner_id0;
610           u32 match_flags0;
611           u32 old_sw_if_index0, new_sw_if_index0, len0;
612           vnet_hw_interface_t *hi0;
613           main_intf_t *main_intf0;
614           vlan_intf_t *vlan_intf0;
615           qinq_intf_t *qinq_intf0;
616           ethernet_header_t *e0;
617           u32 is_l20;
618
619           // Prefetch next iteration
620           if (n_left_from > 1)
621             {
622               vlib_buffer_t *p2;
623
624               p2 = vlib_get_buffer (vm, from[1]);
625               vlib_prefetch_buffer_header (p2, STORE);
626               CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
627             }
628
629           bi0 = from[0];
630           to_next[0] = bi0;
631           from += 1;
632           to_next += 1;
633           n_left_from -= 1;
634           n_left_to_next -= 1;
635
636           b0 = vlib_get_buffer (vm, bi0);
637
638           error0 = ETHERNET_ERROR_NONE;
639           e0 = vlib_buffer_get_current (b0);
640           type0 = clib_net_to_host_u16 (e0->type);
641
642           /* Speed-path for the untagged case */
643           if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
644                             && !ethernet_frame_is_tagged (type0)))
645             {
646               main_intf_t *intf0;
647               subint_config_t *subint0;
648               u32 sw_if_index0;
649
650               sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
651               is_l20 = cached_is_l2;
652
653               if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
654                 {
655                   cached_sw_if_index = sw_if_index0;
656                   hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
657                   intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
658                   subint0 = &intf0->untagged_subint;
659                   cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
660                 }
661
662               vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
663               vnet_buffer (b0)->l3_hdr_offset =
664                 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
665               b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
666                 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
667
668               if (PREDICT_TRUE (is_l20 != 0))
669                 {
670                   next0 = em->l2_next;
671                   vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
672                 }
673               else
674                 {
675                   if (!ethernet_address_cast (e0->dst_address) &&
676                       (hi->hw_address != 0) &&
677                       !eth_mac_equal ((u8 *) e0, hi->hw_address))
678                     error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
679                   determine_next_node (em, variant, 0, type0, b0,
680                                        &error0, &next0);
681                   vlib_buffer_advance (b0, sizeof (ethernet_header_t));
682                 }
683               goto ship_it0;
684             }
685
686           /* Slow-path for the tagged case */
687           parse_header (variant,
688                         b0,
689                         &type0,
690                         &orig_type0, &outer_id0, &inner_id0, &match_flags0);
691
692           old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
693
694           eth_vlan_table_lookups (em,
695                                   vnm,
696                                   old_sw_if_index0,
697                                   orig_type0,
698                                   outer_id0,
699                                   inner_id0,
700                                   &hi0,
701                                   &main_intf0, &vlan_intf0, &qinq_intf0);
702
703           identify_subint (hi0,
704                            b0,
705                            match_flags0,
706                            main_intf0,
707                            vlan_intf0,
708                            qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
709
710           // Save RX sw_if_index for later nodes
711           vnet_buffer (b0)->sw_if_index[VLIB_RX] =
712             error0 !=
713             ETHERNET_ERROR_NONE ? old_sw_if_index0 : new_sw_if_index0;
714
715           // Increment subinterface stats
716           // Note that interface-level counters have already been incremented
717           // prior to calling this function. Thus only subinterface counters
718           // are incremented here.
719           //
720           // Interface level counters include packets received on the main
721           // interface and all subinterfaces. Subinterface level counters
722           // include only those packets received on that subinterface
723           // Increment stats if the subint is valid and it is not the main intf
724           if ((new_sw_if_index0 != ~0)
725               && (new_sw_if_index0 != old_sw_if_index0))
726             {
727
728               len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
729                 - vnet_buffer (b0)->l2_hdr_offset;
730
731               stats_n_packets += 1;
732               stats_n_bytes += len0;
733
734               // Batch stat increments from the same subinterface so counters
735               // don't need to be incremented for every packet.
736               if (PREDICT_FALSE (new_sw_if_index0 != stats_sw_if_index))
737                 {
738                   stats_n_packets -= 1;
739                   stats_n_bytes -= len0;
740
741                   if (new_sw_if_index0 != ~0)
742                     vlib_increment_combined_counter
743                       (vnm->interface_main.combined_sw_if_counters
744                        + VNET_INTERFACE_COUNTER_RX,
745                        thread_index, new_sw_if_index0, 1, len0);
746                   if (stats_n_packets > 0)
747                     {
748                       vlib_increment_combined_counter
749                         (vnm->interface_main.combined_sw_if_counters
750                          + VNET_INTERFACE_COUNTER_RX,
751                          thread_index,
752                          stats_sw_if_index, stats_n_packets, stats_n_bytes);
753                       stats_n_packets = stats_n_bytes = 0;
754                     }
755                   stats_sw_if_index = new_sw_if_index0;
756                 }
757             }
758
759           if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
760             is_l20 = 0;
761
762           determine_next_node (em, variant, is_l20, type0, b0, &error0,
763                                &next0);
764           vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset +
765             vnet_buffer (b0)->l2.l2_len;
766           b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
767
768         ship_it0:
769           b0->error = error_node->errors[error0];
770
771           // verify speculative enqueue
772           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
773                                            to_next, n_left_to_next,
774                                            bi0, next0);
775         }
776
777       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
778     }
779
780   // Increment any remaining batched stats
781   if (stats_n_packets > 0)
782     {
783       vlib_increment_combined_counter
784         (vnm->interface_main.combined_sw_if_counters
785          + VNET_INTERFACE_COUNTER_RX,
786          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
787       node->runtime_data[0] = stats_sw_if_index;
788     }
789
790   return from_frame->n_vectors;
791 }
792
793 static uword
794 ethernet_input (vlib_main_t * vm,
795                 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
796 {
797   return ethernet_input_inline (vm, node, from_frame,
798                                 ETHERNET_INPUT_VARIANT_ETHERNET);
799 }
800
801 static uword
802 ethernet_input_type (vlib_main_t * vm,
803                      vlib_node_runtime_t * node, vlib_frame_t * from_frame)
804 {
805   return ethernet_input_inline (vm, node, from_frame,
806                                 ETHERNET_INPUT_VARIANT_ETHERNET_TYPE);
807 }
808
809 static uword
810 ethernet_input_not_l2 (vlib_main_t * vm,
811                        vlib_node_runtime_t * node, vlib_frame_t * from_frame)
812 {
813   return ethernet_input_inline (vm, node, from_frame,
814                                 ETHERNET_INPUT_VARIANT_NOT_L2);
815 }
816
817
818 // Return the subinterface config struct for the given sw_if_index
819 // Also return via parameter the appropriate match flags for the
820 // configured number of tags.
821 // On error (unsupported or not ethernet) return 0.
822 static subint_config_t *
823 ethernet_sw_interface_get_config (vnet_main_t * vnm,
824                                   u32 sw_if_index,
825                                   u32 * flags, u32 * unsupported)
826 {
827   ethernet_main_t *em = &ethernet_main;
828   vnet_hw_interface_t *hi;
829   vnet_sw_interface_t *si;
830   main_intf_t *main_intf;
831   vlan_table_t *vlan_table;
832   qinq_table_t *qinq_table;
833   subint_config_t *subint = 0;
834
835   hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
836
837   if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
838     {
839       *unsupported = 0;
840       goto done;                // non-ethernet interface
841     }
842
843   // ensure there's an entry for the main intf (shouldn't really be necessary)
844   vec_validate (em->main_intfs, hi->hw_if_index);
845   main_intf = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
846
847   // Locate the subint for the given ethernet config
848   si = vnet_get_sw_interface (vnm, sw_if_index);
849
850   if (si->type == VNET_SW_INTERFACE_TYPE_P2P)
851     {
852       p2p_ethernet_main_t *p2pm = &p2p_main;
853       u32 p2pe_sw_if_index =
854         p2p_ethernet_lookup (hi->hw_if_index, si->p2p.client_mac);
855       if (p2pe_sw_if_index == ~0)
856         {
857           pool_get (p2pm->p2p_subif_pool, subint);
858           si->p2p.pool_index = subint - p2pm->p2p_subif_pool;
859         }
860       else
861         subint = vec_elt_at_index (p2pm->p2p_subif_pool, si->p2p.pool_index);
862       *flags = SUBINT_CONFIG_P2P;
863     }
864   else if (si->sub.eth.flags.default_sub)
865     {
866       subint = &main_intf->default_subint;
867       *flags = SUBINT_CONFIG_MATCH_0_TAG |
868         SUBINT_CONFIG_MATCH_1_TAG |
869         SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG;
870     }
871   else if ((si->sub.eth.flags.no_tags) || (si->sub.eth.raw_flags == 0))
872     {
873       // if no flags are set then this is a main interface
874       // so treat as untagged
875       subint = &main_intf->untagged_subint;
876       *flags = SUBINT_CONFIG_MATCH_0_TAG;
877     }
878   else
879     {
880       // one or two tags
881       // first get the vlan table
882       if (si->sub.eth.flags.dot1ad)
883         {
884           if (main_intf->dot1ad_vlans == 0)
885             {
886               // Allocate a vlan table from the pool
887               pool_get (em->vlan_pool, vlan_table);
888               main_intf->dot1ad_vlans = vlan_table - em->vlan_pool;
889             }
890           else
891             {
892               // Get ptr to existing vlan table
893               vlan_table =
894                 vec_elt_at_index (em->vlan_pool, main_intf->dot1ad_vlans);
895             }
896         }
897       else
898         {                       // dot1q
899           if (main_intf->dot1q_vlans == 0)
900             {
901               // Allocate a vlan table from the pool
902               pool_get (em->vlan_pool, vlan_table);
903               main_intf->dot1q_vlans = vlan_table - em->vlan_pool;
904             }
905           else
906             {
907               // Get ptr to existing vlan table
908               vlan_table =
909                 vec_elt_at_index (em->vlan_pool, main_intf->dot1q_vlans);
910             }
911         }
912
913       if (si->sub.eth.flags.one_tag)
914         {
915           *flags = si->sub.eth.flags.exact_match ?
916             SUBINT_CONFIG_MATCH_1_TAG :
917             (SUBINT_CONFIG_MATCH_1_TAG |
918              SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG);
919
920           if (si->sub.eth.flags.outer_vlan_id_any)
921             {
922               // not implemented yet
923               *unsupported = 1;
924               goto done;
925             }
926           else
927             {
928               // a single vlan, a common case
929               subint =
930                 &vlan_table->vlans[si->sub.eth.
931                                    outer_vlan_id].single_tag_subint;
932             }
933
934         }
935       else
936         {
937           // Two tags
938           *flags = si->sub.eth.flags.exact_match ?
939             SUBINT_CONFIG_MATCH_2_TAG :
940             (SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG);
941
942           if (si->sub.eth.flags.outer_vlan_id_any
943               && si->sub.eth.flags.inner_vlan_id_any)
944             {
945               // not implemented yet
946               *unsupported = 1;
947               goto done;
948             }
949
950           if (si->sub.eth.flags.inner_vlan_id_any)
951             {
952               // a specific outer and "any" inner
953               // don't need a qinq table for this
954               subint =
955                 &vlan_table->vlans[si->sub.eth.
956                                    outer_vlan_id].inner_any_subint;
957               if (si->sub.eth.flags.exact_match)
958                 {
959                   *flags = SUBINT_CONFIG_MATCH_2_TAG;
960                 }
961               else
962                 {
963                   *flags = SUBINT_CONFIG_MATCH_2_TAG |
964                     SUBINT_CONFIG_MATCH_3_TAG;
965                 }
966             }
967           else
968             {
969               // a specific outer + specifc innner vlan id, a common case
970
971               // get the qinq table
972               if (vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs == 0)
973                 {
974                   // Allocate a qinq table from the pool
975                   pool_get (em->qinq_pool, qinq_table);
976                   vlan_table->vlans[si->sub.eth.outer_vlan_id].qinqs =
977                     qinq_table - em->qinq_pool;
978                 }
979               else
980                 {
981                   // Get ptr to existing qinq table
982                   qinq_table =
983                     vec_elt_at_index (em->qinq_pool,
984                                       vlan_table->vlans[si->sub.
985                                                         eth.outer_vlan_id].
986                                       qinqs);
987                 }
988               subint = &qinq_table->vlans[si->sub.eth.inner_vlan_id].subint;
989             }
990         }
991     }
992
993 done:
994   return subint;
995 }
996
997 clib_error_t *
998 ethernet_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
999 {
1000   subint_config_t *subint;
1001   u32 dummy_flags;
1002   u32 dummy_unsup;
1003   clib_error_t *error = 0;
1004
1005   // Find the config for this subinterface
1006   subint =
1007     ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1008                                       &dummy_unsup);
1009
1010   if (subint == 0)
1011     {
1012       // not implemented yet or not ethernet
1013       goto done;
1014     }
1015
1016   subint->sw_if_index =
1017     ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? sw_if_index : ~0);
1018
1019 done:
1020   return error;
1021 }
1022
1023 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_sw_interface_up_down);
1024
1025
1026 // Set the L2/L3 mode for the subinterface
1027 void
1028 ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index, u32 l2)
1029 {
1030   subint_config_t *subint;
1031   u32 dummy_flags;
1032   u32 dummy_unsup;
1033   int is_port;
1034   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
1035
1036   is_port = !(sw->type == VNET_SW_INTERFACE_TYPE_SUB);
1037
1038   // Find the config for this subinterface
1039   subint =
1040     ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1041                                       &dummy_unsup);
1042
1043   if (subint == 0)
1044     {
1045       // unimplemented or not ethernet
1046       goto done;
1047     }
1048
1049   // Double check that the config we found is for our interface (or the interface is down)
1050   ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0));
1051
1052   if (l2)
1053     {
1054       subint->flags |= SUBINT_CONFIG_L2;
1055       if (is_port)
1056         subint->flags |=
1057           SUBINT_CONFIG_MATCH_0_TAG | SUBINT_CONFIG_MATCH_1_TAG
1058           | SUBINT_CONFIG_MATCH_2_TAG | SUBINT_CONFIG_MATCH_3_TAG;
1059     }
1060   else
1061     {
1062       subint->flags &= ~SUBINT_CONFIG_L2;
1063       if (is_port)
1064         subint->flags &=
1065           ~(SUBINT_CONFIG_MATCH_1_TAG | SUBINT_CONFIG_MATCH_2_TAG
1066             | SUBINT_CONFIG_MATCH_3_TAG);
1067     }
1068
1069 done:
1070   return;
1071 }
1072
1073 /*
1074  * Set the L2/L3 mode for the subinterface regardless of port
1075  */
1076 void
1077 ethernet_sw_interface_set_l2_mode_noport (vnet_main_t * vnm,
1078                                           u32 sw_if_index, u32 l2)
1079 {
1080   subint_config_t *subint;
1081   u32 dummy_flags;
1082   u32 dummy_unsup;
1083
1084   /* Find the config for this subinterface */
1085   subint =
1086     ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1087                                       &dummy_unsup);
1088
1089   if (subint == 0)
1090     {
1091       /* unimplemented or not ethernet */
1092       goto done;
1093     }
1094
1095   /*
1096    * Double check that the config we found is for our interface (or the
1097    * interface is down)
1098    */
1099   ASSERT ((subint->sw_if_index == sw_if_index) | (subint->sw_if_index == ~0));
1100
1101   if (l2)
1102     {
1103       subint->flags |= SUBINT_CONFIG_L2;
1104     }
1105   else
1106     {
1107       subint->flags &= ~SUBINT_CONFIG_L2;
1108     }
1109
1110 done:
1111   return;
1112 }
1113
1114 static clib_error_t *
1115 ethernet_sw_interface_add_del (vnet_main_t * vnm,
1116                                u32 sw_if_index, u32 is_create)
1117 {
1118   clib_error_t *error = 0;
1119   subint_config_t *subint;
1120   u32 match_flags;
1121   u32 unsupported = 0;
1122
1123   // Find the config for this subinterface
1124   subint =
1125     ethernet_sw_interface_get_config (vnm, sw_if_index, &match_flags,
1126                                       &unsupported);
1127
1128   if (subint == 0)
1129     {
1130       // not implemented yet or not ethernet
1131       if (unsupported)
1132         {
1133           // this is the NYI case
1134           error = clib_error_return (0, "not implemented yet");
1135         }
1136       goto done;
1137     }
1138
1139   if (!is_create)
1140     {
1141       subint->flags = 0;
1142       return error;
1143     }
1144
1145   // Initialize the subint
1146   if (subint->flags & SUBINT_CONFIG_VALID)
1147     {
1148       // Error vlan already in use
1149       error = clib_error_return (0, "vlan is already in use");
1150     }
1151   else
1152     {
1153       // Note that config is L3 by defaulty
1154       subint->flags = SUBINT_CONFIG_VALID | match_flags;
1155       subint->sw_if_index = ~0; // because interfaces are initially down
1156     }
1157
1158 done:
1159   return error;
1160 }
1161
1162 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ethernet_sw_interface_add_del);
1163
1164 static char *ethernet_error_strings[] = {
1165 #define ethernet_error(n,c,s) s,
1166 #include "error.def"
1167 #undef ethernet_error
1168 };
1169
1170 /* *INDENT-OFF* */
1171 VLIB_REGISTER_NODE (ethernet_input_node) = {
1172   .function = ethernet_input,
1173   .name = "ethernet-input",
1174   /* Takes a vector of packets. */
1175   .vector_size = sizeof (u32),
1176   .n_errors = ETHERNET_N_ERROR,
1177   .error_strings = ethernet_error_strings,
1178   .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1179   .next_nodes = {
1180 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1181     foreach_ethernet_input_next
1182 #undef _
1183   },
1184   .format_buffer = format_ethernet_header_with_length,
1185   .format_trace = format_ethernet_input_trace,
1186   .unformat_buffer = unformat_ethernet_header,
1187 };
1188 /* *INDENT-ON* */
1189
1190 /* *INDENT-OFF* */
1191 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_node, ethernet_input)
1192 /* *INDENT-ON* */
1193
1194 /* *INDENT-OFF* */
1195 VLIB_REGISTER_NODE (ethernet_input_type_node, static) = {
1196   .function = ethernet_input_type,
1197   .name = "ethernet-input-type",
1198   /* Takes a vector of packets. */
1199   .vector_size = sizeof (u32),
1200   .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1201   .next_nodes = {
1202 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1203     foreach_ethernet_input_next
1204 #undef _
1205   },
1206 };
1207 /* *INDENT-ON* */
1208
1209 /* *INDENT-OFF* */
1210 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_type_node, ethernet_input_type)
1211 /* *INDENT-ON* */
1212
1213 /* *INDENT-OFF* */
1214 VLIB_REGISTER_NODE (ethernet_input_not_l2_node, static) = {
1215   .function = ethernet_input_not_l2,
1216   .name = "ethernet-input-not-l2",
1217   /* Takes a vector of packets. */
1218   .vector_size = sizeof (u32),
1219   .n_next_nodes = ETHERNET_INPUT_N_NEXT,
1220   .next_nodes = {
1221 #define _(s,n) [ETHERNET_INPUT_NEXT_##s] = n,
1222     foreach_ethernet_input_next
1223 #undef _
1224   },
1225 };
1226 /* *INDENT-ON* */
1227
1228
1229 /* *INDENT-OFF* */
1230 VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_not_l2_node,
1231                               ethernet_input_not_l2)
1232 /* *INDENT-ON* */
1233
1234
1235 void
1236 ethernet_set_rx_redirect (vnet_main_t * vnm,
1237                           vnet_hw_interface_t * hi, u32 enable)
1238 {
1239   // Insure all packets go to ethernet-input (i.e. untagged ipv4 packets
1240   // don't go directly to ip4-input)
1241   vnet_hw_interface_rx_redirect_to_node
1242     (vnm, hi->hw_if_index, enable ? ethernet_input_node.index : ~0);
1243 }
1244
1245
1246 /*
1247  * Initialization and registration for the next_by_ethernet structure
1248  */
1249
1250 clib_error_t *
1251 next_by_ethertype_init (next_by_ethertype_t * l3_next)
1252 {
1253   l3_next->input_next_by_type = sparse_vec_new
1254     ( /* elt bytes */ sizeof (l3_next->input_next_by_type[0]),
1255      /* bits in index */ BITS (((ethernet_header_t *) 0)->type));
1256
1257   vec_validate (l3_next->sparse_index_by_input_next_index,
1258                 ETHERNET_INPUT_NEXT_DROP);
1259   vec_validate (l3_next->sparse_index_by_input_next_index,
1260                 ETHERNET_INPUT_NEXT_PUNT);
1261   l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_DROP] =
1262     SPARSE_VEC_INVALID_INDEX;
1263   l3_next->sparse_index_by_input_next_index[ETHERNET_INPUT_NEXT_PUNT] =
1264     SPARSE_VEC_INVALID_INDEX;
1265
1266   /*
1267    * Make sure we don't wipe out an ethernet registration by mistake
1268    * Can happen if init function ordering constraints are missing.
1269    */
1270   if (CLIB_DEBUG > 0)
1271     {
1272       ethernet_main_t *em = &ethernet_main;
1273       ASSERT (em->next_by_ethertype_register_called == 0);
1274     }
1275
1276   return 0;
1277 }
1278
1279 // Add an ethertype -> next index mapping to the structure
1280 clib_error_t *
1281 next_by_ethertype_register (next_by_ethertype_t * l3_next,
1282                             u32 ethertype, u32 next_index)
1283 {
1284   u32 i;
1285   u16 *n;
1286   ethernet_main_t *em = &ethernet_main;
1287
1288   if (CLIB_DEBUG > 0)
1289     {
1290       ethernet_main_t *em = &ethernet_main;
1291       em->next_by_ethertype_register_called = 1;
1292     }
1293
1294   /* Setup ethernet type -> next index sparse vector mapping. */
1295   n = sparse_vec_validate (l3_next->input_next_by_type, ethertype);
1296   n[0] = next_index;
1297
1298   /* Rebuild next index -> sparse index inverse mapping when sparse vector
1299      is updated. */
1300   vec_validate (l3_next->sparse_index_by_input_next_index, next_index);
1301   for (i = 1; i < vec_len (l3_next->input_next_by_type); i++)
1302     l3_next->
1303       sparse_index_by_input_next_index[l3_next->input_next_by_type[i]] = i;
1304
1305   // do not allow the cached next index's to be updated if L3
1306   // redirect is enabled, as it will have overwritten them
1307   if (!em->redirect_l3)
1308     {
1309       // Cache common ethertypes directly
1310       if (ethertype == ETHERNET_TYPE_IP4)
1311         {
1312           l3_next->input_next_ip4 = next_index;
1313         }
1314       else if (ethertype == ETHERNET_TYPE_IP6)
1315         {
1316           l3_next->input_next_ip6 = next_index;
1317         }
1318       else if (ethertype == ETHERNET_TYPE_MPLS)
1319         {
1320           l3_next->input_next_mpls = next_index;
1321         }
1322     }
1323   return 0;
1324 }
1325
1326
1327 static clib_error_t *
1328 ethernet_input_init (vlib_main_t * vm)
1329 {
1330   ethernet_main_t *em = &ethernet_main;
1331   __attribute__ ((unused)) vlan_table_t *invalid_vlan_table;
1332   __attribute__ ((unused)) qinq_table_t *invalid_qinq_table;
1333
1334   ethernet_setup_node (vm, ethernet_input_node.index);
1335   ethernet_setup_node (vm, ethernet_input_type_node.index);
1336   ethernet_setup_node (vm, ethernet_input_not_l2_node.index);
1337
1338   next_by_ethertype_init (&em->l3_next);
1339
1340   // Initialize pools and vector for vlan parsing
1341   vec_validate (em->main_intfs, 10);    // 10 main interfaces
1342   pool_alloc (em->vlan_pool, 10);
1343   pool_alloc (em->qinq_pool, 1);
1344
1345   // The first vlan pool will always be reserved for an invalid table
1346   pool_get (em->vlan_pool, invalid_vlan_table); // first id = 0
1347   // The first qinq pool will always be reserved for an invalid table
1348   pool_get (em->qinq_pool, invalid_qinq_table); // first id = 0
1349
1350   return 0;
1351 }
1352
1353 VLIB_INIT_FUNCTION (ethernet_input_init);
1354
1355 void
1356 ethernet_register_input_type (vlib_main_t * vm,
1357                               ethernet_type_t type, u32 node_index)
1358 {
1359   ethernet_main_t *em = &ethernet_main;
1360   ethernet_type_info_t *ti;
1361   u32 i;
1362
1363   {
1364     clib_error_t *error = vlib_call_init_function (vm, ethernet_init);
1365     if (error)
1366       clib_error_report (error);
1367   }
1368
1369   ti = ethernet_get_type_info (em, type);
1370   ti->node_index = node_index;
1371   ti->next_index = vlib_node_add_next (vm,
1372                                        ethernet_input_node.index, node_index);
1373   i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1374   ASSERT (i == ti->next_index);
1375
1376   i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1377   ASSERT (i == ti->next_index);
1378
1379   // Add the L3 node for this ethertype to the next nodes structure
1380   next_by_ethertype_register (&em->l3_next, type, ti->next_index);
1381
1382   // Call the registration functions for other nodes that want a mapping
1383   l2bvi_register_input_type (vm, type, node_index);
1384 }
1385
1386 void
1387 ethernet_register_l2_input (vlib_main_t * vm, u32 node_index)
1388 {
1389   ethernet_main_t *em = &ethernet_main;
1390   u32 i;
1391
1392   em->l2_next =
1393     vlib_node_add_next (vm, ethernet_input_node.index, node_index);
1394
1395   /*
1396    * Even if we never use these arcs, we have to align the next indices...
1397    */
1398   i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1399
1400   ASSERT (i == em->l2_next);
1401
1402   i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1403   ASSERT (i == em->l2_next);
1404 }
1405
1406 // Register a next node for L3 redirect, and enable L3 redirect
1407 void
1408 ethernet_register_l3_redirect (vlib_main_t * vm, u32 node_index)
1409 {
1410   ethernet_main_t *em = &ethernet_main;
1411   u32 i;
1412
1413   em->redirect_l3 = 1;
1414   em->redirect_l3_next = vlib_node_add_next (vm,
1415                                              ethernet_input_node.index,
1416                                              node_index);
1417   /*
1418    * Change the cached next nodes to the redirect node
1419    */
1420   em->l3_next.input_next_ip4 = em->redirect_l3_next;
1421   em->l3_next.input_next_ip6 = em->redirect_l3_next;
1422   em->l3_next.input_next_mpls = em->redirect_l3_next;
1423
1424   /*
1425    * Even if we never use these arcs, we have to align the next indices...
1426    */
1427   i = vlib_node_add_next (vm, ethernet_input_type_node.index, node_index);
1428
1429   ASSERT (i == em->redirect_l3_next);
1430
1431   i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1432
1433   ASSERT (i == em->redirect_l3_next);
1434 }
1435
1436 /*
1437  * fd.io coding-style-patch-verification: ON
1438  *
1439  * Local Variables:
1440  * eval: (c-set-style "gnu")
1441  * End:
1442  */