blob: 3b85483732ceee3e7af660b26d31fd380fb1aa6e [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * ethernet_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>
Pavel Kotucek15ac81c2017-06-20 14:00:26 +020043#include <vnet/ethernet/p2p_ethernet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#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") \
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070051 _ (LLC, "llc-input")
Ed Warnickecb9cada2015-12-08 15:45:58 -070052
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070053typedef enum
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#define _(s,n) ETHERNET_INPUT_NEXT_##s,
56 foreach_ethernet_input_next
57#undef _
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070058 ETHERNET_INPUT_N_NEXT,
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} ethernet_input_next_t;
60
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070061typedef struct
62{
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 u8 packet_data[32];
64} ethernet_input_trace_t;
65
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070066static u8 *
67format_ethernet_input_trace (u8 * s, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
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 *);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070071 ethernet_input_trace_t *t = va_arg (*va, ethernet_input_trace_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
73 s = format (s, "%U", format_ethernet_header, t->packet_data);
74
75 return s;
76}
77
78vlib_node_registration_t ethernet_input_node;
79
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070080typedef enum
81{
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 ETHERNET_INPUT_VARIANT_ETHERNET,
83 ETHERNET_INPUT_VARIANT_ETHERNET_TYPE,
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 ETHERNET_INPUT_VARIANT_NOT_L2,
85} ethernet_input_variant_t;
86
87
Ed Warnickecb9cada2015-12-08 15:45:58 -070088// Parse the ethernet header to extract vlan tags and innermost ethertype
89static_always_inline void
90parse_header (ethernet_input_variant_t variant,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070091 vlib_buffer_t * b0,
92 u16 * type,
93 u16 * orig_type,
94 u16 * outer_id, u16 * inner_id, u32 * match_flags)
95{
Chris Luke194ebc52016-04-25 14:26:55 -040096 u8 vlan_count;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -070098 if (variant == ETHERNET_INPUT_VARIANT_ETHERNET
99 || variant == ETHERNET_INPUT_VARIANT_NOT_L2)
100 {
101 ethernet_header_t *e0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700103 e0 = (void *) (b0->data + b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104
Damjan Marion072401e2017-07-13 18:53:27 +0200105 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
Steven35de3b32017-12-03 23:40:54 -0800106 b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700108 vlib_buffer_advance (b0, sizeof (e0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700110 *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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700117 e0 = (void *) (b0->data + b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700119 vlib_buffer_advance (b0, sizeof (e0[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700121 *type = clib_net_to_host_u16 (e0[0]);
122 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
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;
Chris Luke194ebc52016-04-25 14:26:55 -0400132 vlan_count = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133
134 // check for vlan encaps
Damjan Marionb94bdad2016-09-19 11:32:03 +0200135 if (ethernet_frame_is_tagged (*type))
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700136 {
137 ethernet_vlan_header_t *h0;
138 u16 tag;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700140 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_1_TAG;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141
142 h0 = (void *) (b0->data + b0->current_data);
143
144 tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
145
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700146 *outer_id = tag & 0xfff;
Neale Ranns30d0fd42017-05-30 07:30:04 -0700147 if (0 == *outer_id)
148 *match_flags &= ~SUBINT_CONFIG_MATCH_1_TAG;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700150 *type = clib_net_to_host_u16 (h0->type);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
152 vlib_buffer_advance (b0, sizeof (h0[0]));
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700153 vlan_count = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700155 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;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700170 if (*type == ETHERNET_TYPE_VLAN)
171 {
172 // More than double tagged packet
173 *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG;
Eyal Bari6f7ebf92017-06-13 12:09:37 +0300174
175 vlib_buffer_advance (b0, sizeof (h0[0]));
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700176 vlan_count = 3; // "unknown" number, aka, 3-or-more
177 }
178 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700180 ethernet_buffer_set_vlan_count (b0, vlan_count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181}
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.
186static_always_inline void
187identify_subint (vnet_hw_interface_t * hi,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700188 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)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
195 u32 matched;
196
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700197 matched = eth_identify_subint (hi, b0, match_flags,
198 main_intf, vlan_intf, qinq_intf,
199 new_sw_if_index, error0, is_l2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700201 if (matched)
202 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700204 // 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;
Damjan Marion072401e2017-07-13 18:53:27 +0200210 e0 = (void *) (b0->data + vnet_buffer (b0)->l2_hdr_offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700212 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224}
225
226static_always_inline void
227determine_next_node (ethernet_main_t * em,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700228 ethernet_input_variant_t variant,
229 u32 is_l20,
230 u32 type0, vlib_buffer_t * b0, u8 * error0, u8 * next0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
Damjan Marionfaf9a5a2018-01-15 17:57:00 +0100232 u32 eth_start = vnet_buffer (b0)->l2_hdr_offset;
233 vnet_buffer (b0)->l2.l2_len = b0->current_data - eth_start;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700234 if (PREDICT_FALSE (*error0 != ETHERNET_ERROR_NONE))
235 {
236 // some error occurred
237 *next0 = ETHERNET_INPUT_NEXT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238 }
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700239 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
Eyal Bari6f7ebf92017-06-13 12:09:37 +0300243 ASSERT (vnet_buffer (b0)->l2.l2_len ==
244 ethernet_buffer_header_size (b0));
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700245 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 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -0800257 else if (type0 == ETHERNET_TYPE_MPLS)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700258 {
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.
Damjan Marion607de1a2016-08-16 22:53:54 +0200279 // If variant is variant_ethernet then we came from LLC processing. Don't
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700280 // 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286}
287
Eyal Bari9a1ae1a2017-06-13 08:42:35 +0300288static_always_inline int
289ethernet_frame_is_any_tagged (u16 type0, u16 type1)
290{
291#if __SSE4_2__
Damjan Marion547ecf62018-02-06 19:01:28 +0100292 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,
Eyal Bari9a1ae1a2017-06-13 08:42:35 +0300296 /* duplicate for type1 */
Damjan Marion547ecf62018-02-06 19:01:28 +0100297 (u16) ETHERNET_TYPE_VLAN,
298 (u16) ETHERNET_TYPE_DOT1AD,
299 (u16) ETHERNET_TYPE_VLAN_9100,
300 (u16)
Eyal Bari9a1ae1a2017-06-13 08:42:35 +0300301 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
Christophe Fontaine38cfe982017-07-10 15:21:10 +0200308 return ethernet_frame_is_tagged (type0) || ethernet_frame_is_tagged (type1);
Eyal Bari9a1ae1a2017-06-13 08:42:35 +0300309#endif
310}
311
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312static_always_inline uword
313ethernet_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{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700318 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
Damjan Marion586afd72017-04-05 19:18:20 +0200323 u32 thread_index = vlib_get_thread_index ();
Dave Barachcfba1e22016-11-16 10:23:50 -0500324 u32 cached_sw_if_index = ~0;
325 u32 cached_is_l2 = 0; /* shut up gcc */
John Lo1904c472017-03-10 17:15:22 -0500326 vnet_hw_interface_t *hi = NULL; /* used for main interface only */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327
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;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700356 vlib_buffer_t *b0, *b1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357 u8 next0, next1, error0, error1;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700358 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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500368 ethernet_header_t *e0, *e1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700369
370 /* Prefetch next iteration. */
371 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700372 vlib_buffer_t *b2, *b3;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500397 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
John Locc532852016-12-14 15:42:45 -0500402 /* Speed-path for the untagged case */
Dave Barachcfba1e22016-11-16 10:23:50 -0500403 if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
Eyal Bari9a1ae1a2017-06-13 08:42:35 +0300404 && !ethernet_frame_is_any_tagged (type0, type1)))
Dave Barachcfba1e22016-11-16 10:23:50 -0500405 {
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
John Lo1904c472017-03-10 17:15:22 -0500418 /* Now sw_if_index0 == sw_if_index1 */
Dave Barachcfba1e22016-11-16 10:23:50 -0500419 if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
420 {
421 cached_sw_if_index = sw_if_index0;
John Lo1904c472017-03-10 17:15:22 -0500422 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
423 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
Dave Barachcfba1e22016-11-16 10:23:50 -0500424 subint0 = &intf0->untagged_subint;
425 cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
426 }
John Lo7714b302016-12-20 16:59:02 -0500427
Damjan Marion072401e2017-07-13 18:53:27 +0200428 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
429 vnet_buffer (b1)->l2_hdr_offset = b1->current_data;
Steven35de3b32017-12-03 23:40:54 -0800430 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;
John Lo7714b302016-12-20 16:59:02 -0500438
Dave Barachcfba1e22016-11-16 10:23:50 -0500439 if (PREDICT_TRUE (is_l20 != 0))
440 {
441 next0 = em->l2_next;
442 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
Dave Barachcfba1e22016-11-16 10:23:50 -0500443 next1 = em->l2_next;
444 vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t);
Dave Barachcfba1e22016-11-16 10:23:50 -0500445 }
John Locc532852016-12-14 15:42:45 -0500446 else
447 {
John Lo1904c472017-03-10 17:15:22 -0500448 if (!ethernet_address_cast (e0->dst_address) &&
Hongjun Ni9e3e3612017-04-26 18:40:55 +0800449 (hi->hw_address != 0) &&
John Lo1904c472017-03-10 17:15:22 -0500450 !eth_mac_equal ((u8 *) e0, hi->hw_address))
451 error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
452 if (!ethernet_address_cast (e1->dst_address) &&
Hongjun Ni9e3e3612017-04-26 18:40:55 +0800453 (hi->hw_address != 0) &&
John Lo1904c472017-03-10 17:15:22 -0500454 !eth_mac_equal ((u8 *) e1, hi->hw_address))
455 error1 = ETHERNET_ERROR_L3_MAC_MISMATCH;
John Locc532852016-12-14 15:42:45 -0500456 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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500464 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
John Locc532852016-12-14 15:42:45 -0500466 /* Slow-path for the tagged case */
467 slowpath:
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700468 parse_header (variant,
469 b0,
470 &type0,
471 &orig_type0, &outer_id0, &inner_id0, &match_flags0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700473 parse_header (variant,
474 b1,
475 &type1,
476 &orig_type1, &outer_id1, &inner_id1, &match_flags1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700478 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];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700481 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700490 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498
499 identify_subint (hi0,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700500 b0,
501 match_flags0,
502 main_intf0,
503 vlan_intf0,
504 qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
506 identify_subint (hi1,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700507 b1,
508 match_flags1,
509 main_intf1,
510 vlan_intf1,
511 qinq_intf1, &new_sw_if_index1, &error1, &is_l21);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700513 // 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700521 // 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 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700528 len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
Damjan Marion072401e2017-07-13 18:53:27 +0200529 - vnet_buffer (b0)->l2_hdr_offset;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700530 len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data
Damjan Marion072401e2017-07-13 18:53:27 +0200531 - vnet_buffer (b1)->l2_hdr_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700533 stats_n_packets += 2;
534 stats_n_bytes += len0 + len1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700536 if (PREDICT_FALSE
537 (!(new_sw_if_index0 == stats_sw_if_index
538 && new_sw_if_index1 == stats_sw_if_index)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 {
540 stats_n_packets -= 2;
541 stats_n_bytes -= len0 + len1;
542
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700543 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,
Damjan Marion586afd72017-04-05 19:18:20 +0200549 thread_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700550 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,
Damjan Marion586afd72017-04-05 19:18:20 +0200558 thread_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700559 new_sw_if_index1, 1,
560 len1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561
562 if (new_sw_if_index0 == new_sw_if_index1)
563 {
564 if (stats_n_packets > 0)
565 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700566 vlib_increment_combined_counter
567 (vnm->interface_main.combined_sw_if_counters
568 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200569 thread_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700570 stats_sw_if_index,
571 stats_n_packets, stats_n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 stats_n_packets = stats_n_bytes = 0;
573 }
574 stats_sw_if_index = new_sw_if_index0;
575 }
576 }
577 }
578
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700579 if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
580 is_l20 = is_l21 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700582 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);
Steven35de3b32017-12-03 23:40:54 -0800586 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;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700592
John Lo1904c472017-03-10 17:15:22 -0500593 ship_it01:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594 b0->error = error_node->errors[error0];
595 b1->error = error_node->errors[error1];
596
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700597 // 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601 }
602
603 while (n_left_from > 0 && n_left_to_next > 0)
604 {
605 u32 bi0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700606 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607 u8 error0, next0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700608 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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500616 ethernet_header_t *e0;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700617 u32 is_l20;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700619 // Prefetch next iteration
620 if (n_left_from > 1)
621 {
622 vlib_buffer_t *p2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700624 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];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500639 e0 = vlib_buffer_get_current (b0);
640 type0 = clib_net_to_host_u16 (e0->type);
641
John Locc532852016-12-14 15:42:45 -0500642 /* Speed-path for the untagged case */
Dave Barachcfba1e22016-11-16 10:23:50 -0500643 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;
John Lo1904c472017-03-10 17:15:22 -0500656 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
657 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
Dave Barachcfba1e22016-11-16 10:23:50 -0500658 subint0 = &intf0->untagged_subint;
659 cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
660 }
John Lo7714b302016-12-20 16:59:02 -0500661
Damjan Marion072401e2017-07-13 18:53:27 +0200662 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
Steven35de3b32017-12-03 23:40:54 -0800663 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;
John Lo7714b302016-12-20 16:59:02 -0500667
Dave Barachcfba1e22016-11-16 10:23:50 -0500668 if (PREDICT_TRUE (is_l20 != 0))
669 {
670 next0 = em->l2_next;
671 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
Dave Barachcfba1e22016-11-16 10:23:50 -0500672 }
John Locc532852016-12-14 15:42:45 -0500673 else
674 {
John Lo1904c472017-03-10 17:15:22 -0500675 if (!ethernet_address_cast (e0->dst_address) &&
Hongjun Ni9e3e3612017-04-26 18:40:55 +0800676 (hi->hw_address != 0) &&
John Lo1904c472017-03-10 17:15:22 -0500677 !eth_mac_equal ((u8 *) e0, hi->hw_address))
678 error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
John Locc532852016-12-14 15:42:45 -0500679 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;
Dave Barachcfba1e22016-11-16 10:23:50 -0500684 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685
John Locc532852016-12-14 15:42:45 -0500686 /* Slow-path for the tagged case */
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700687 parse_header (variant,
688 b0,
689 &type0,
690 &orig_type0, &outer_id0, &inner_id0, &match_flags0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700691
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700692 old_sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700694 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
703 identify_subint (hi0,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700704 b0,
705 match_flags0,
706 main_intf0,
707 vlan_intf0,
708 qinq_intf0, &new_sw_if_index0, &error0, &is_l20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700710 // 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700715 // 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 //
Damjan Marion607de1a2016-08-16 22:53:54 +0200720 // Interface level counters include packets received on the main
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700721 // interface and all subinterfaces. Subinterface level counters
722 // include only those packets received on that subinterface
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 // Increment stats if the subint is valid and it is not the main intf
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700724 if ((new_sw_if_index0 != ~0)
725 && (new_sw_if_index0 != old_sw_if_index0))
726 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700727
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700728 len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
Damjan Marion072401e2017-07-13 18:53:27 +0200729 - vnet_buffer (b0)->l2_hdr_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700731 stats_n_packets += 1;
732 stats_n_bytes += len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700733
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700734 // Batch stat increments from the same subinterface so counters
Damjan Marion607de1a2016-08-16 22:53:54 +0200735 // don't need to be incremented for every packet.
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700736 if (PREDICT_FALSE (new_sw_if_index0 != stats_sw_if_index))
737 {
738 stats_n_packets -= 1;
739 stats_n_bytes -= len0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700741 if (new_sw_if_index0 != ~0)
742 vlib_increment_combined_counter
743 (vnm->interface_main.combined_sw_if_counters
744 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200745 thread_index, new_sw_if_index0, 1, len0);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700746 if (stats_n_packets > 0)
747 {
748 vlib_increment_combined_counter
749 (vnm->interface_main.combined_sw_if_counters
750 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200751 thread_index,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700752 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700757 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700759 if (variant == ETHERNET_INPUT_VARIANT_NOT_L2)
760 is_l20 = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700761
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700762 determine_next_node (em, variant, is_l20, type0, b0, &error0,
763 &next0);
Steven35de3b32017-12-03 23:40:54 -0800764 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767
John Lo1904c472017-03-10 17:15:22 -0500768 ship_it0:
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700769 b0->error = error_node->errors[error0];
770
771 // verify speculative enqueue
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772 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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700780 // Increment any remaining batched stats
781 if (stats_n_packets > 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700783 vlib_increment_combined_counter
784 (vnm->interface_main.combined_sw_if_counters
785 + VNET_INTERFACE_COUNTER_RX,
Damjan Marion586afd72017-04-05 19:18:20 +0200786 thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 node->runtime_data[0] = stats_sw_if_index;
788 }
789
790 return from_frame->n_vectors;
791}
792
793static uword
794ethernet_input (vlib_main_t * vm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700795 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}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700800
801static uword
802ethernet_input_type (vlib_main_t * vm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700803 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}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700808
809static uword
810ethernet_input_not_l2 (vlib_main_t * vm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700811 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}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700816
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.
822static subint_config_t *
823ethernet_sw_interface_get_config (vnet_main_t * vnm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700824 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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700835 hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
836
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700837 if (!hi || (hi->hw_class_index != ethernet_hw_interface_class.index))
838 {
839 *unsupported = 0;
840 goto done; // non-ethernet interface
841 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700842
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
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200850 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)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700865 {
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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700991 }
992
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700993done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994 return subint;
995}
996
997clib_error_t *
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -0700998ethernet_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001000 subint_config_t *subint;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001 u32 dummy_flags;
1002 u32 dummy_unsup;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001003 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001004
1005 // Find the config for this subinterface
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001006 subint =
1007 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1008 &dummy_unsup);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001010 if (subint == 0)
1011 {
1012 // not implemented yet or not ethernet
1013 goto done;
1014 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
1016 subint->sw_if_index =
1017 ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? sw_if_index : ~0);
1018
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001019done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020 return error;
1021}
1022
1023VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (ethernet_sw_interface_up_down);
1024
1025
1026// Set the L2/L3 mode for the subinterface
1027void
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001028ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index, u32 l2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029{
1030 subint_config_t *subint;
1031 u32 dummy_flags;
1032 u32 dummy_unsup;
1033 int is_port;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001034 vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001035
1036 is_port = !(sw->type == VNET_SW_INTERFACE_TYPE_SUB);
1037
1038 // Find the config for this subinterface
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001039 subint =
1040 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1041 &dummy_unsup);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001043 if (subint == 0)
1044 {
1045 // unimplemented or not ethernet
1046 goto done;
1047 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001052 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001068
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001069done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070 return;
1071}
1072
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001073/*
1074 * Set the L2/L3 mode for the subinterface regardless of port
1075 */
1076void
1077ethernet_sw_interface_set_l2_mode_noport (vnet_main_t * vnm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001078 u32 sw_if_index, u32 l2)
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001079{
1080 subint_config_t *subint;
1081 u32 dummy_flags;
1082 u32 dummy_unsup;
1083
1084 /* Find the config for this subinterface */
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001085 subint =
1086 ethernet_sw_interface_get_config (vnm, sw_if_index, &dummy_flags,
1087 &dummy_unsup);
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001088
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001089 if (subint == 0)
1090 {
1091 /* unimplemented or not ethernet */
1092 goto done;
1093 }
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001094
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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001101 if (l2)
1102 {
1103 subint->flags |= SUBINT_CONFIG_L2;
1104 }
1105 else
1106 {
1107 subint->flags &= ~SUBINT_CONFIG_L2;
1108 }
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001109
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001110done:
Christian Dechamplain (cdechamp)07aecbb2016-04-05 10:40:38 -04001111 return;
1112}
Ed Warnickecb9cada2015-12-08 15:45:58 -07001113
1114static clib_error_t *
1115ethernet_sw_interface_add_del (vnet_main_t * vnm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001116 u32 sw_if_index, u32 is_create)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001118 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 subint_config_t *subint;
1120 u32 match_flags;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001121 u32 unsupported = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122
1123 // Find the config for this subinterface
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001124 subint =
1125 ethernet_sw_interface_get_config (vnm, sw_if_index, &match_flags,
1126 &unsupported);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001128 if (subint == 0)
1129 {
1130 // not implemented yet or not ethernet
1131 if (unsupported)
1132 {
Damjan Marion607de1a2016-08-16 22:53:54 +02001133 // this is the NYI case
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001134 error = clib_error_return (0, "not implemented yet");
1135 }
1136 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001139 if (!is_create)
1140 {
1141 subint->flags = 0;
1142 return error;
1143 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
1145 // Initialize the subint
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001146 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 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001158done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 return error;
1160}
1161
1162VNET_SW_INTERFACE_ADD_DEL_FUNCTION (ethernet_sw_interface_add_del);
1163
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001164static char *ethernet_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165#define ethernet_error(n,c,s) s,
1166#include "error.def"
1167#undef ethernet_error
1168};
1169
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001170/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171VLIB_REGISTER_NODE (ethernet_input_node) = {
1172 .function = ethernet_input,
1173 .name = "ethernet-input",
1174 /* Takes a vector of packets. */
1175 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001176 .n_errors = ETHERNET_N_ERROR,
1177 .error_strings = ethernet_error_strings,
Ed Warnickecb9cada2015-12-08 15:45:58 -07001178 .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 },
Ed Warnickecb9cada2015-12-08 15:45:58 -07001184 .format_buffer = format_ethernet_header_with_length,
1185 .format_trace = format_ethernet_input_trace,
1186 .unformat_buffer = unformat_ethernet_header,
1187};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001188/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001190/* *INDENT-OFF* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001191VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_node, ethernet_input)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001192/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001193
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001194/* *INDENT-OFF* */
1195VLIB_REGISTER_NODE (ethernet_input_type_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001196 .function = ethernet_input_type,
1197 .name = "ethernet-input-type",
1198 /* Takes a vector of packets. */
1199 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001200 .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};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001207/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001208
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001209/* *INDENT-OFF* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001210VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_type_node, ethernet_input_type)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001211/* *INDENT-ON* */
Damjan Marion1c80e832016-05-11 23:07:18 +02001212
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001213/* *INDENT-OFF* */
1214VLIB_REGISTER_NODE (ethernet_input_not_l2_node, static) = {
Ed Warnickecb9cada2015-12-08 15:45:58 -07001215 .function = ethernet_input_not_l2,
1216 .name = "ethernet-input-not-l2",
1217 /* Takes a vector of packets. */
1218 .vector_size = sizeof (u32),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001219 .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};
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001226/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227
Damjan Marion1c80e832016-05-11 23:07:18 +02001228
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001229/* *INDENT-OFF* */
1230VLIB_NODE_FUNCTION_MULTIARCH (ethernet_input_not_l2_node,
1231 ethernet_input_not_l2)
1232/* *INDENT-ON* */
1233
1234
1235void
1236ethernet_set_rx_redirect (vnet_main_t * vnm,
1237 vnet_hw_interface_t * hi, u32 enable)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001238{
1239 // Insure all packets go to ethernet-input (i.e. untagged ipv4 packets
1240 // don't go directly to ip4-input)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001241 vnet_hw_interface_rx_redirect_to_node
1242 (vnm, hi->hw_if_index, enable ? ethernet_input_node.index : ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001243}
1244
1245
1246/*
1247 * Initialization and registration for the next_by_ethernet structure
1248 */
1249
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001250clib_error_t *
1251next_by_ethertype_init (next_by_ethertype_t * l3_next)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001252{
1253 l3_next->input_next_by_type = sparse_vec_new
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001254 ( /* elt bytes */ sizeof (l3_next->input_next_by_type[0]),
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255 /* bits in index */ BITS (((ethernet_header_t *) 0)->type));
1256
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001257 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
Damjan Marion607de1a2016-08-16 22:53:54 +02001266 /*
1267 * Make sure we don't wipe out an ethernet registration by mistake
Dave Barach1f49ed62016-02-24 11:29:06 -05001268 * Can happen if init function ordering constraints are missing.
1269 */
1270 if (CLIB_DEBUG > 0)
1271 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001272 ethernet_main_t *em = &ethernet_main;
1273 ASSERT (em->next_by_ethertype_register_called == 0);
Dave Barach1f49ed62016-02-24 11:29:06 -05001274 }
1275
Ed Warnickecb9cada2015-12-08 15:45:58 -07001276 return 0;
1277}
1278
1279// Add an ethertype -> next index mapping to the structure
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001280clib_error_t *
1281next_by_ethertype_register (next_by_ethertype_t * l3_next,
1282 u32 ethertype, u32 next_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283{
1284 u32 i;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001285 u16 *n;
1286 ethernet_main_t *em = &ethernet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001287
Dave Barach1f49ed62016-02-24 11:29:06 -05001288 if (CLIB_DEBUG > 0)
1289 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001290 ethernet_main_t *em = &ethernet_main;
Dave Barach1f49ed62016-02-24 11:29:06 -05001291 em->next_by_ethertype_register_called = 1;
1292 }
1293
Ed Warnickecb9cada2015-12-08 15:45:58 -07001294 /* 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++)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001302 l3_next->
1303 sparse_index_by_input_next_index[l3_next->input_next_by_type[i]] = i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304
1305 // do not allow the cached next index's to be updated if L3
1306 // redirect is enabled, as it will have overwritten them
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001307 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 }
Neale Ranns0f26c5a2017-03-01 15:12:11 -08001318 else if (ethertype == ETHERNET_TYPE_MPLS)
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001319 {
1320 l3_next->input_next_mpls = next_index;
1321 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001322 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001323 return 0;
1324}
1325
1326
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001327static clib_error_t *
1328ethernet_input_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001329{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001330 ethernet_main_t *em = &ethernet_main;
1331 __attribute__ ((unused)) vlan_table_t *invalid_vlan_table;
1332 __attribute__ ((unused)) qinq_table_t *invalid_qinq_table;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001333
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);
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001339
Ed Warnickecb9cada2015-12-08 15:45:58 -07001340 // Initialize pools and vector for vlan parsing
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001341 vec_validate (em->main_intfs, 10); // 10 main interfaces
1342 pool_alloc (em->vlan_pool, 10);
1343 pool_alloc (em->qinq_pool, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001344
1345 // The first vlan pool will always be reserved for an invalid table
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001346 pool_get (em->vlan_pool, invalid_vlan_table); // first id = 0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001347 // The first qinq pool will always be reserved for an invalid table
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001348 pool_get (em->qinq_pool, invalid_qinq_table); // first id = 0
Ed Warnickecb9cada2015-12-08 15:45:58 -07001349
1350 return 0;
1351}
1352
1353VLIB_INIT_FUNCTION (ethernet_input_init);
1354
1355void
1356ethernet_register_input_type (vlib_main_t * vm,
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001357 ethernet_type_t type, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001358{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001359 ethernet_main_t *em = &ethernet_main;
1360 ethernet_type_info_t *ti;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001361 u32 i;
1362
1363 {
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001364 clib_error_t *error = vlib_call_init_function (vm, ethernet_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001365 if (error)
1366 clib_error_report (error);
1367 }
1368
1369 ti = ethernet_get_type_info (em, type);
1370 ti->node_index = node_index;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001371 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);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001374 ASSERT (i == ti->next_index);
1375
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001376 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001377 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
1386void
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001387ethernet_register_l2_input (vlib_main_t * vm, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001388{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001389 ethernet_main_t *em = &ethernet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390 u32 i;
1391
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001392 em->l2_next =
1393 vlib_node_add_next (vm, ethernet_input_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001394
Damjan Marion607de1a2016-08-16 22:53:54 +02001395 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -07001396 * 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
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001402 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001403 ASSERT (i == em->l2_next);
1404}
1405
1406// Register a next node for L3 redirect, and enable L3 redirect
1407void
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001408ethernet_register_l3_redirect (vlib_main_t * vm, u32 node_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001409{
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001410 ethernet_main_t *em = &ethernet_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001411 u32 i;
1412
1413 em->redirect_l3 = 1;
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001414 em->redirect_l3_next = vlib_node_add_next (vm,
1415 ethernet_input_node.index,
1416 node_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001417 /*
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);
jerryianff82ed62016-12-05 17:13:00 +08001430
1431 i = vlib_node_add_next (vm, ethernet_input_not_l2_node.index, node_index);
1432
1433 ASSERT (i == em->redirect_l3_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001434}
Keith Burns (alagalah)e70dcc82016-08-15 18:33:19 -07001435
1436/*
1437 * fd.io coding-style-patch-verification: ON
1438 *
1439 * Local Variables:
1440 * eval: (c-set-style "gnu")
1441 * End:
1442 */