ethernet: add sanity checks to p2p_ethernet_add/del
[vpp.git] / src / vnet / ethernet / ethernet.h
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.h: types/functions for ethernet.
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 #ifndef included_ethernet_h
41 #define included_ethernet_h
42
43 #include <vnet/vnet.h>
44 #include <vnet/ethernet/packet.h>
45 #include <vnet/ethernet/mac_address.h>
46 #include <vnet/pg/pg.h>
47 #include <vnet/feature/feature.h>
48
49 /* ethernet-input frame flags and scalar data */
50
51 /* all packets in frame share same sw_if_index */
52 #define ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX (1 << 0)
53
54 /* all ip4 packets in frame have correct ip4 checksum */
55 #define ETH_INPUT_FRAME_F_IP4_CKSUM_OK (1 << 1)
56
57 typedef struct
58 {
59   u32 sw_if_index;
60   u32 hw_if_index;
61 } ethernet_input_frame_t;
62
63 #ifdef CLIB_HAVE_VEC128
64 static const u16x8 tagged_ethertypes = {
65   (u16) ETHERNET_TYPE_VLAN,
66   (u16) ETHERNET_TYPE_DOT1AD,
67   (u16) ETHERNET_TYPE_VLAN_9100,
68   (u16) ETHERNET_TYPE_VLAN_9200,
69   /* duplicate last one to fill register */
70   (u16) ETHERNET_TYPE_VLAN_9200,
71   (u16) ETHERNET_TYPE_VLAN_9200,
72   (u16) ETHERNET_TYPE_VLAN_9200,
73   (u16) ETHERNET_TYPE_VLAN_9200
74 };
75 #endif
76
77 static_always_inline int
78 ethernet_frame_is_tagged (u16 type)
79 {
80 #ifdef CLIB_HAVE_VEC128
81   return !u16x8_is_all_zero (tagged_ethertypes == u16x8_splat (type));
82 #else
83   if ((type == ETHERNET_TYPE_VLAN) ||
84       (type == ETHERNET_TYPE_DOT1AD) ||
85       (type == ETHERNET_TYPE_VLAN_9100) || (type == ETHERNET_TYPE_VLAN_9200))
86     return 1;
87 #endif
88   return 0;
89 }
90
91 static_always_inline int
92 ethernet_frame_is_any_tagged_x2 (u16 type0, u16 type1)
93 {
94 #ifdef CLIB_HAVE_VEC128
95   u16x8 r0 = (tagged_ethertypes == u16x8_splat (type0));
96   u16x8 r1 = (tagged_ethertypes == u16x8_splat (type1));
97   return !u16x8_is_all_zero (r0 | r1);
98 #else
99   return ethernet_frame_is_tagged (type0) || ethernet_frame_is_tagged (type1);
100 #endif
101 }
102
103 static_always_inline int
104 ethernet_frame_is_any_tagged_x4 (u16 type0, u16 type1, u16 type2, u16 type3)
105 {
106 #ifdef CLIB_HAVE_VEC128
107   u16x8 r0 = (tagged_ethertypes == u16x8_splat (type0));
108   u16x8 r1 = (tagged_ethertypes == u16x8_splat (type1));
109   u16x8 r2 = (tagged_ethertypes == u16x8_splat (type2));
110   u16x8 r3 = (tagged_ethertypes == u16x8_splat (type3));
111   return !u16x8_is_all_zero (r0 | r1 | r2 | r3);
112 #else
113   return ethernet_frame_is_tagged (type0) || ethernet_frame_is_tagged (type1)
114     || ethernet_frame_is_tagged (type2) || ethernet_frame_is_tagged (type3);
115 #endif
116 }
117
118 /* Max. sized ethernet/vlan header for parsing. */
119 typedef struct
120 {
121   ethernet_header_t ethernet;
122
123   /* Allow up to 2 stacked vlan headers. */
124   ethernet_vlan_header_t vlan[2];
125 } ethernet_max_header_t;
126
127 struct vnet_hw_interface_t;
128 /* Ethernet flag change callback. */
129 typedef u32 (ethernet_flag_change_function_t)
130   (vnet_main_t * vnm, struct vnet_hw_interface_t * hi, u32 flags);
131
132 #define ETHERNET_MIN_PACKET_BYTES  64
133 #define ETHERNET_MAX_PACKET_BYTES  9216
134
135 /* Ethernet interface instance. */
136 typedef struct ethernet_interface
137 {
138   u32 flags;
139
140   /* Accept all packets (promiscuous mode). */
141 #define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL (1 << 0)
142 #define ETHERNET_INTERFACE_FLAG_CONFIG_PROMISC(flags) \
143   (((flags) & ~ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) == 0)
144
145   /* Change MTU on interface from hw interface structure */
146 #define ETHERNET_INTERFACE_FLAG_MTU (1 << 1)
147 #define ETHERNET_INTERFACE_FLAG_CONFIG_MTU(flags) \
148   ((flags) & ETHERNET_INTERFACE_FLAG_MTU)
149
150   /* Callback, e.g. to turn on/off promiscuous mode */
151   ethernet_flag_change_function_t *flag_change;
152
153   u32 driver_instance;
154
155   /* Ethernet (MAC) address for this interface. */
156   u8 address[6];
157
158   /* Secondary MAC addresses for this interface */
159   mac_address_t *secondary_addrs;
160 } ethernet_interface_t;
161
162 extern vnet_hw_interface_class_t ethernet_hw_interface_class;
163
164 typedef struct
165 {
166   /* Name (a c string). */
167   char *name;
168
169   /* Ethernet type in host byte order. */
170   ethernet_type_t type;
171
172   /* Node which handles this type. */
173   u32 node_index;
174
175   /* Next index for this type. */
176   u32 next_index;
177 } ethernet_type_info_t;
178
179 typedef enum
180 {
181 #define ethernet_error(n,c,s) ETHERNET_ERROR_##n,
182 #include <vnet/ethernet/error.def>
183 #undef ethernet_error
184   ETHERNET_N_ERROR,
185 } ethernet_error_t;
186
187
188 // Structs used when parsing packet to find sw_if_index
189
190 typedef struct
191 {
192   u32 sw_if_index;
193   u32 flags;
194   // config entry is-valid flag
195   // exact match flags (valid if packet has 0/1/2/3 tags)
196   // L2 vs L3 forwarding mode
197 #define SUBINT_CONFIG_MATCH_0_TAG (1<<0)
198 #define SUBINT_CONFIG_MATCH_1_TAG (1<<1)
199 #define SUBINT_CONFIG_MATCH_2_TAG (1<<2)
200 #define SUBINT_CONFIG_MATCH_3_TAG (1<<3)
201 #define SUBINT_CONFIG_VALID       (1<<4)
202 #define SUBINT_CONFIG_L2          (1<<5)
203 #define SUBINT_CONFIG_P2P         (1<<6)
204
205 } subint_config_t;
206
207 always_inline u32
208 eth_create_valid_subint_match_flags (u32 num_tags)
209 {
210   return SUBINT_CONFIG_VALID | (1 << num_tags);
211 }
212
213
214 typedef struct
215 {
216   subint_config_t untagged_subint;
217   subint_config_t default_subint;
218   u16 dot1q_vlans;              // pool id for vlan table
219   u16 dot1ad_vlans;             // pool id for vlan table
220 } main_intf_t;
221
222 typedef struct
223 {
224   subint_config_t single_tag_subint;
225   subint_config_t inner_any_subint;
226   u32 qinqs;                    // pool id for qinq table
227 } vlan_intf_t;
228
229 typedef struct
230 {
231   vlan_intf_t vlans[ETHERNET_N_VLAN];
232 } vlan_table_t;
233
234 typedef struct
235 {
236   subint_config_t subint;
237 } qinq_intf_t;
238
239 typedef struct
240 {
241   qinq_intf_t vlans[ETHERNET_N_VLAN];
242 } qinq_table_t;
243
244 // Structure mapping to a next index based on ethertype.
245 // Common ethertypes are stored explicitly, others are
246 // stored in a sparse table.
247 typedef struct
248 {
249   /* Sparse vector mapping ethernet type in network byte order
250      to next index. */
251   u16 *input_next_by_type;
252   u32 *sparse_index_by_input_next_index;
253
254   /* cached next indexes for common ethertypes */
255   u32 input_next_ip4;
256   u32 input_next_ip6;
257   u32 input_next_mpls;
258 } next_by_ethertype_t;
259
260 struct ethernet_main_t_;
261
262 typedef void (ethernet_address_change_function_t)
263   (struct ethernet_main_t_ * im, u32 sw_if_index, uword opaque);
264
265 typedef struct
266 {
267   ethernet_address_change_function_t *function;
268   uword function_opaque;
269 } ethernet_address_change_ctx_t;
270
271 typedef struct ethernet_main_t_
272 {
273   vlib_main_t *vlib_main;
274
275   /* next node index for the L3 input node of each ethertype */
276   next_by_ethertype_t l3_next;
277
278   /* next node index for L2 interfaces */
279   u32 l2_next;
280
281   /* flag and next node index for L3 redirect */
282   u32 redirect_l3;
283   u32 redirect_l3_next;
284
285   /* Pool of ethernet interface instances. */
286   ethernet_interface_t *interfaces;
287
288   ethernet_type_info_t *type_infos;
289
290   /* Hash tables mapping name/type to type info index. */
291   uword *type_info_by_name, *type_info_by_type;
292
293   // The root of the vlan parsing tables. A vector with one element
294   // for each main interface, indexed by hw_if_index.
295   main_intf_t *main_intfs;
296
297   // Pool of vlan tables
298   vlan_table_t *vlan_pool;
299
300   // Pool of qinq tables;
301   qinq_table_t *qinq_pool;
302
303   /* Set to one to use AB.CD.EF instead of A:B:C:D:E:F as ethernet format. */
304   int format_ethernet_address_16bit;
305
306   /* debug: make sure we don't wipe out an ethernet registration by mistake */
307   u8 next_by_ethertype_register_called;
308
309   /* Feature arc index */
310   u8 output_feature_arc_index;
311
312   /* Allocated loopback instances */
313   uword *bm_loopback_instances;
314
315   /** Functions to call when interface hw address changes. */
316   ethernet_address_change_ctx_t *address_change_callbacks;
317
318   /** Default interface MTU */
319   u32 default_mtu;
320
321 } ethernet_main_t;
322
323 extern ethernet_main_t ethernet_main;
324
325 always_inline ethernet_type_info_t *
326 ethernet_get_type_info (ethernet_main_t * em, ethernet_type_t type)
327 {
328   uword *p = hash_get (em->type_info_by_type, type);
329   return p ? vec_elt_at_index (em->type_infos, p[0]) : 0;
330 }
331
332 ethernet_interface_t *ethernet_get_interface (ethernet_main_t * em,
333                                               u32 hw_if_index);
334 mac_address_t *ethernet_interface_add_del_address (ethernet_main_t * em,
335                                                    u32 hw_if_index,
336                                                    const u8 * address,
337                                                    u8 is_add);
338
339 clib_error_t *ethernet_register_interface (vnet_main_t * vnm,
340                                            u32 dev_class_index,
341                                            u32 dev_instance,
342                                            const u8 * address,
343                                            u32 * hw_if_index_return,
344                                            ethernet_flag_change_function_t
345                                            flag_change);
346
347 void ethernet_delete_interface (vnet_main_t * vnm, u32 hw_if_index);
348
349 /* Register given node index to take input for given ethernet type. */
350 void
351 ethernet_register_input_type (vlib_main_t * vm,
352                               ethernet_type_t type, u32 node_index);
353
354 /* Register given node index to take input for packet from L2 interfaces. */
355 void ethernet_register_l2_input (vlib_main_t * vm, u32 node_index);
356
357 /* Register given node index to take redirected L3 traffic, and enable L3 redirect */
358 void ethernet_register_l3_redirect (vlib_main_t * vm, u32 node_index);
359
360 /* Formats ethernet address X:X:X:X:X:X */
361 u8 *format_mac_address (u8 * s, va_list * args);
362 u8 *format_ethernet_address (u8 * s, va_list * args);
363 u8 *format_ethernet_type (u8 * s, va_list * args);
364 u8 *format_ethernet_vlan_tci (u8 * s, va_list * va);
365 u8 *format_ethernet_header (u8 * s, va_list * args);
366 u8 *format_ethernet_header_with_length (u8 * s, va_list * args);
367
368 /* Parse ethernet address in either X:X:X:X:X:X unix or X.X.X cisco format. */
369 uword unformat_ethernet_address (unformat_input_t * input, va_list * args);
370 uword unformat_mac_address (unformat_input_t * input, va_list * args);
371
372 /* Parse ethernet type as 0xXXXX or type name from ethernet/types.def.
373    In either host or network byte order. */
374 uword
375 unformat_ethernet_type_host_byte_order (unformat_input_t * input,
376                                         va_list * args);
377 uword
378 unformat_ethernet_type_net_byte_order (unformat_input_t * input,
379                                        va_list * args);
380
381 /* Parse ethernet header. */
382 uword unformat_ethernet_header (unformat_input_t * input, va_list * args);
383
384 /* Parse ethernet interface name; return hw_if_index. */
385 uword unformat_ethernet_interface (unformat_input_t * input, va_list * args);
386
387 uword unformat_pg_ethernet_header (unformat_input_t * input, va_list * args);
388
389 always_inline void
390 ethernet_setup_node (vlib_main_t * vm, u32 node_index)
391 {
392   vlib_node_t *n = vlib_get_node (vm, node_index);
393   pg_node_t *pn = pg_get_node (node_index);
394
395   n->format_buffer = format_ethernet_header_with_length;
396   n->unformat_buffer = unformat_ethernet_header;
397   pn->unformat_edit = unformat_pg_ethernet_header;
398 }
399
400 always_inline ethernet_header_t *
401 ethernet_buffer_get_header (vlib_buffer_t * b)
402 {
403   return (void *) (b->data + vnet_buffer (b)->l2_hdr_offset);
404 }
405
406 /** Returns the number of VLAN headers in the current Ethernet frame in the
407  * buffer. Returns 0, 1, 2 for the known header count. The value 3 indicates
408  * the number of headers is not known.
409  */
410 #define ethernet_buffer_get_vlan_count(b) ( \
411     ((b)->flags & VNET_BUFFER_FLAGS_VLAN_BITS) >> VNET_BUFFER_F_LOG2_VLAN_1_DEEP \
412 )
413
414 /** Sets the number of VLAN headers in the current Ethernet frame in the
415  * buffer. Values 0, 1, 2 indicate  the header count. The value 3 indicates
416  * the number of headers is not known.
417  */
418 #define ethernet_buffer_set_vlan_count(b, v) ( \
419     (b)->flags = ((b)->flags & ~VNET_BUFFER_FLAGS_VLAN_BITS) | \
420         (((v) << VNET_BUFFER_F_LOG2_VLAN_1_DEEP) & VNET_BUFFER_FLAGS_VLAN_BITS) \
421 )
422
423 /** Adjusts the vlan count by the delta in 'v' */
424 #define ethernet_buffer_adjust_vlan_count(b, v) ( \
425   ethernet_buffer_set_vlan_count(b,  \
426       (word)ethernet_buffer_get_vlan_count(b) + (word)(v)) \
427 )
428
429 /** Adjusts the vlan count by the header size byte delta in 'v' */
430 #define ethernet_buffer_adjust_vlan_count_by_bytes(b, v) ( \
431     (b)->flags = ((b)->flags & ~VNET_BUFFER_FLAGS_VLAN_BITS) | (( \
432         ((b)->flags & VNET_BUFFER_FLAGS_VLAN_BITS) + \
433         ((v) << (VNET_BUFFER_F_LOG2_VLAN_1_DEEP - 2)) \
434     ) & VNET_BUFFER_FLAGS_VLAN_BITS) \
435 )
436
437 /**
438  * Determine the size of the Ethernet headers of the current frame in
439  * the buffer. This uses the VLAN depth flags that are set by
440  * ethernet-input. Because these flags are stored in the vlib_buffer_t
441  * "flags" field this count is valid regardless of the node so long as it's
442  * checked downstream of ethernet-input; That is, the value is not stored in
443  * the opaque space.
444  */
445 #define ethernet_buffer_header_size(b) ( \
446         ethernet_buffer_get_vlan_count((b)) * sizeof(ethernet_vlan_header_t) + \
447         sizeof(ethernet_header_t) \
448 )
449
450 ethernet_main_t *ethernet_get_main (vlib_main_t * vm);
451 u32 ethernet_set_flags (vnet_main_t * vnm, u32 hw_if_index, u32 flags);
452 void ethernet_sw_interface_set_l2_mode (vnet_main_t * vnm, u32 sw_if_index,
453                                         u32 l2);
454 void ethernet_sw_interface_set_l2_mode_noport (vnet_main_t * vnm,
455                                                u32 sw_if_index, u32 l2);
456 void ethernet_set_rx_redirect (vnet_main_t * vnm, vnet_hw_interface_t * hi,
457                                u32 enable);
458
459 clib_error_t *next_by_ethertype_init (next_by_ethertype_t * l3_next);
460 clib_error_t *next_by_ethertype_register (next_by_ethertype_t * l3_next,
461                                           u32 ethertype, u32 next_index);
462
463 int vnet_create_loopback_interface (u32 * sw_if_indexp, u8 * mac_address,
464                                     u8 is_specified, u32 user_instance);
465 int vnet_delete_loopback_interface (u32 sw_if_index);
466 int vnet_create_sub_interface (u32 sw_if_index, u32 id,
467                                u32 flags, u16 inner_vlan_id,
468                                u16 outer_vlan_id, u32 * sub_sw_if_index);
469 int vnet_delete_sub_interface (u32 sw_if_index);
470
471 // Perform ethernet subinterface classification table lookups given
472 // the ports's sw_if_index and fields extracted from the ethernet header.
473 // The resulting tables are used by identify_subint().
474 always_inline void
475 eth_vlan_table_lookups (ethernet_main_t * em,
476                         vnet_main_t * vnm,
477                         u32 port_sw_if_index0,
478                         u16 first_ethertype,
479                         u16 outer_id,
480                         u16 inner_id,
481                         vnet_hw_interface_t ** hi,
482                         main_intf_t ** main_intf,
483                         vlan_intf_t ** vlan_intf, qinq_intf_t ** qinq_intf)
484 {
485   vlan_table_t *vlan_table;
486   qinq_table_t *qinq_table;
487   u32 vlan_table_id;
488
489   // Read the main, vlan, and qinq interface table entries
490   // TODO: Consider if/how to prefetch tables. Also consider
491   // single-entry cache to skip table lookups and identify_subint()
492   // processing.
493   *hi = vnet_get_sup_hw_interface (vnm, port_sw_if_index0);
494   *main_intf = vec_elt_at_index (em->main_intfs, (*hi)->hw_if_index);
495
496   // Always read the vlan and qinq tables, even if there are not that
497   // many tags on the packet. This makes the lookups and comparisons
498   // easier (and less branchy).
499   vlan_table_id = (first_ethertype == ETHERNET_TYPE_DOT1AD) ?
500     (*main_intf)->dot1ad_vlans : (*main_intf)->dot1q_vlans;
501   vlan_table = vec_elt_at_index (em->vlan_pool, vlan_table_id);
502   *vlan_intf = &vlan_table->vlans[outer_id];
503
504   qinq_table = vec_elt_at_index (em->qinq_pool, (*vlan_intf)->qinqs);
505   *qinq_intf = &qinq_table->vlans[inner_id];
506 }
507
508
509 // Determine the subinterface for this packet, given the result of the
510 // vlan table lookups and vlan header parsing. Check the most specific
511 // matches first.
512 // Returns 1 if a matching subinterface was found, otherwise returns 0.
513 always_inline u32
514 eth_identify_subint (vnet_hw_interface_t * hi,
515                      u32 match_flags,
516                      main_intf_t * main_intf,
517                      vlan_intf_t * vlan_intf,
518                      qinq_intf_t * qinq_intf,
519                      u32 * new_sw_if_index, u8 * error0, u32 * is_l2)
520 {
521   subint_config_t *subint;
522
523   // Each comparison is checking both the valid flag and the number of tags
524   // (incorporating exact-match/non-exact-match).
525
526   // check for specific double tag
527   subint = &qinq_intf->subint;
528   if ((subint->flags & match_flags) == match_flags)
529     goto matched;
530
531   // check for specific outer and 'any' inner
532   subint = &vlan_intf->inner_any_subint;
533   if ((subint->flags & match_flags) == match_flags)
534     goto matched;
535
536   // check for specific single tag
537   subint = &vlan_intf->single_tag_subint;
538   if ((subint->flags & match_flags) == match_flags)
539     goto matched;
540
541   // check for default interface
542   subint = &main_intf->default_subint;
543   if ((subint->flags & match_flags) == match_flags)
544     goto matched;
545
546   // check for untagged interface
547   subint = &main_intf->untagged_subint;
548   if ((subint->flags & match_flags) == match_flags)
549     goto matched;
550
551   // No matching subinterface
552   *new_sw_if_index = ~0;
553   *error0 = ETHERNET_ERROR_UNKNOWN_VLAN;
554   *is_l2 = 0;
555   return 0;
556
557 matched:
558   *new_sw_if_index = subint->sw_if_index;
559   *is_l2 = subint->flags & SUBINT_CONFIG_L2;
560   return 1;
561 }
562
563 always_inline ethernet_main_t *
564 vnet_get_ethernet_main (void)
565 {
566   return &ethernet_main;
567 }
568
569 void ethernet_update_adjacency (vnet_main_t * vnm, u32 sw_if_index, u32 ai);
570 u8 *ethernet_build_rewrite (vnet_main_t * vnm,
571                             u32 sw_if_index,
572                             vnet_link_t link_type, const void *dst_address);
573 void ethernet_input_init (vlib_main_t * vm, ethernet_main_t * em);
574
575 extern vlib_node_registration_t ethernet_input_node;
576
577 #endif /* included_ethernet_h */
578
579 /*
580  * fd.io coding-style-patch-verification: ON
581  *
582  * Local Variables:
583  * eval: (c-set-style "gnu")
584  * End:
585  */