L2 over LISP and GRE (VPP-457)
[vpp.git] / vnet / vnet / interface.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  * interface.h: VNET interfaces/sub-interfaces
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_vnet_interface_h
41 #define included_vnet_interface_h
42
43 #include <vnet/unix/pcap.h>
44
45 struct vnet_main_t;
46 struct vnet_hw_interface_t;
47 struct vnet_sw_interface_t;
48
49 /* Interface up/down callback. */
50 typedef clib_error_t *(vnet_interface_function_t)
51   (struct vnet_main_t * vnm, u32 if_index, u32 flags);
52
53 /* Sub-interface add/del callback. */
54 typedef clib_error_t *(vnet_subif_add_del_function_t)
55   (struct vnet_main_t * vnm, u32 if_index,
56    struct vnet_sw_interface_t * template, int is_add);
57
58 /* Interface set mac address callback. */
59 typedef clib_error_t *(vnet_interface_set_mac_address_function_t)
60   (struct vnet_hw_interface_t * hi, char *address);
61
62 typedef struct _vnet_interface_function_list_elt
63 {
64   struct _vnet_interface_function_list_elt *next_interface_function;
65   clib_error_t *(*fp) (struct vnet_main_t * vnm, u32 if_index, u32 flags);
66 } _vnet_interface_function_list_elt_t;
67
68 #define _VNET_INTERFACE_FUNCTION_DECL(f,tag)                            \
69                                                                         \
70 static void __vnet_interface_function_init_##tag##_##f (void)           \
71     __attribute__((__constructor__)) ;                                  \
72                                                                         \
73 static void __vnet_interface_function_init_##tag##_##f (void)           \
74 {                                                                       \
75  vnet_main_t * vnm = vnet_get_main();                                   \
76  static _vnet_interface_function_list_elt_t init_function;              \
77  init_function.next_interface_function = vnm->tag##_functions;          \
78  vnm->tag##_functions = &init_function;                                 \
79  init_function.fp = (void *) &f;                                        \
80 }
81
82 #define VNET_HW_INTERFACE_ADD_DEL_FUNCTION(f)                   \
83   _VNET_INTERFACE_FUNCTION_DECL(f,hw_interface_add_del)
84 #define VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(f)              \
85   _VNET_INTERFACE_FUNCTION_DECL(f,hw_interface_link_up_down)
86 #define VNET_SW_INTERFACE_ADD_DEL_FUNCTION(f)                   \
87   _VNET_INTERFACE_FUNCTION_DECL(f,sw_interface_add_del)
88 #define VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(f)             \
89   _VNET_INTERFACE_FUNCTION_DECL(f,sw_interface_admin_up_down)
90
91 /* A class of hardware interface devices. */
92 typedef struct _vnet_device_class
93 {
94   /* Index into main vector. */
95   u32 index;
96
97   /* Device name (e.g. "FOOBAR 1234a"). */
98   char *name;
99
100   /* Function to call when hardware interface is added/deleted. */
101   vnet_interface_function_t *interface_add_del_function;
102
103   /* Function to bring device administratively up/down. */
104   vnet_interface_function_t *admin_up_down_function;
105
106   /* Function to call when sub-interface is added/deleted */
107   vnet_subif_add_del_function_t *subif_add_del_function;
108
109   /* Redistribute flag changes/existence of this interface class. */
110   u32 redistribute;
111
112   /* Transmit function. */
113   vlib_node_function_t *tx_function;
114
115   /* Error strings indexed by error code for this node. */
116   char **tx_function_error_strings;
117
118   /* Number of error codes used by this node. */
119   u32 tx_function_n_errors;
120
121   /* Renumber device name [only!] support, a control-plane kludge */
122   int (*name_renumber) (struct vnet_hw_interface_t * hi,
123                         u32 new_dev_instance);
124
125   /* Format device instance as name. */
126   format_function_t *format_device_name;
127
128   /* Parse function for device name. */
129   unformat_function_t *unformat_device_name;
130
131   /* Format device verbosely for this class. */
132   format_function_t *format_device;
133
134   /* Trace buffer format for TX function. */
135   format_function_t *format_tx_trace;
136
137   /* Function to clear hardware counters for device. */
138   void (*clear_counters) (u32 dev_class_instance);
139
140     uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
141                                            u32 hw_if_index,
142                                            u32 hw_class_index);
143
144   /* Called when hardware class of an interface changes. */
145   void (*hw_class_change) (struct vnet_main_t * vnm,
146                            u32 hw_if_index, u32 new_hw_class_index);
147
148   /* Called to redirect traffic from a specific interface instance */
149   void (*rx_redirect_to_node) (struct vnet_main_t * vnm,
150                                u32 hw_if_index, u32 node_index);
151
152   /* Link-list of all device classes set up by constructors created below */
153   struct _vnet_device_class *next_class_registration;
154
155   /* Do not splice vnet_interface_output_node into TX path */
156   u8 no_flatten_output_chains;
157
158   /* Function to set mac address. */
159   vnet_interface_set_mac_address_function_t *mac_addr_change_function;
160 } vnet_device_class_t;
161
162 #define VNET_DEVICE_CLASS(x,...)                                        \
163   __VA_ARGS__ vnet_device_class_t x;                                    \
164 static void __vnet_add_device_class_registration_##x (void)             \
165     __attribute__((__constructor__)) ;                                  \
166 static void __vnet_add_device_class_registration_##x (void)             \
167 {                                                                       \
168     vnet_main_t * vnm = vnet_get_main();                                \
169     x.next_class_registration = vnm->device_class_registrations;        \
170     vnm->device_class_registrations = &x;                               \
171 }                                                                       \
172 __VA_ARGS__ vnet_device_class_t x
173
174 #define VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt)           \
175   uword                                                                 \
176   __attribute__ ((flatten))                                             \
177   __attribute__ ((target (tgt)))                                        \
178   CLIB_CPU_OPTIMIZED                                                    \
179   fn ## _ ## arch ( vlib_main_t * vm,                                   \
180                    vlib_node_runtime_t * node,                          \
181                    vlib_frame_t * frame)                                \
182   { return fn (vm, node, frame); }
183
184 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn)                     \
185   foreach_march_variant(VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE, fn)
186
187 #if CLIB_DEBUG > 0
188 #define VLIB_MULTIARCH_CLONE_AND_SELECT_FN(fn,...)
189 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
190 #else
191 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)                      \
192   VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn)                           \
193   CLIB_MULTIARCH_SELECT_FN(fn, static inline)                           \
194   static void __attribute__((__constructor__))                          \
195   __vlib_device_tx_function_multiarch_select_##dev (void)               \
196   { dev.tx_function = fn ## _multiarch_select(); }
197 #endif
198
199
200 /* Layer-2 (e.g. Ethernet) interface class. */
201 typedef struct _vnet_hw_interface_class
202 {
203   /* Index into main vector. */
204   u32 index;
205
206   /* Class name (e.g. "Ethernet"). */
207   char *name;
208
209   /* Function to call when hardware interface is added/deleted. */
210   vnet_interface_function_t *interface_add_del_function;
211
212   /* Function to bring interface administratively up/down. */
213   vnet_interface_function_t *admin_up_down_function;
214
215   /* Function to call when link state changes. */
216   vnet_interface_function_t *link_up_down_function;
217
218   /* Format function to display interface name. */
219   format_function_t *format_interface_name;
220
221   /* Format function to display interface address. */
222   format_function_t *format_address;
223
224   /* Format packet header for this interface class. */
225   format_function_t *format_header;
226
227   /* Format device verbosely for this class. */
228   format_function_t *format_device;
229
230   /* Parser for hardware (e.g. ethernet) address. */
231   unformat_function_t *unformat_hw_address;
232
233   /* Parser for packet header for e.g. rewrite string. */
234   unformat_function_t *unformat_header;
235
236   /* Forms adjacency for given l3 packet type and destination address.
237      Returns number of bytes in adjacency. */
238     uword (*set_rewrite) (struct vnet_main_t * vnm,
239                           u32 sw_if_index,
240                           u32 l3_packet_type,
241                           void *dst_address,
242                           void *rewrite, uword max_rewrite_bytes);
243
244     uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
245                                            u32 hw_if_index,
246                                            u32 hw_class_index);
247
248   /* Called when hw interface class is changed and old hardware instance
249      may want to be deleted. */
250   void (*hw_class_change) (struct vnet_main_t * vnm, u32 hw_if_index,
251                            u32 old_class_index, u32 new_class_index);
252
253   /* List of hw interface classes, built by constructors */
254   struct _vnet_hw_interface_class *next_class_registration;
255
256 } vnet_hw_interface_class_t;
257
258 #define VNET_HW_INTERFACE_CLASS(x,...)                                  \
259   __VA_ARGS__ vnet_hw_interface_class_t x;                              \
260 static void __vnet_add_hw_interface_class_registration_##x (void)       \
261     __attribute__((__constructor__)) ;                                  \
262 static void __vnet_add_hw_interface_class_registration_##x (void)       \
263 {                                                                       \
264     vnet_main_t * vnm = vnet_get_main();                                \
265     x.next_class_registration = vnm->hw_interface_class_registrations;  \
266     vnm->hw_interface_class_registrations = &x;                         \
267 }                                                                       \
268 __VA_ARGS__ vnet_hw_interface_class_t x
269
270 /* Hardware-interface.  This corresponds to a physical wire
271    that packets flow over. */
272 typedef struct vnet_hw_interface_t
273 {
274   /* Interface name. */
275   u8 *name;
276
277   u32 flags;
278   /* Hardware link state is up. */
279 #define VNET_HW_INTERFACE_FLAG_LINK_UP          (1 << 0)
280   /* Hardware duplex state */
281 #define VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT     1
282 #define VNET_HW_INTERFACE_FLAG_HALF_DUPLEX      (1 << 1)
283 #define VNET_HW_INTERFACE_FLAG_FULL_DUPLEX      (1 << 2)
284 #define VNET_HW_INTERFACE_FLAG_DUPLEX_MASK      \
285   (VNET_HW_INTERFACE_FLAG_HALF_DUPLEX |         \
286    VNET_HW_INTERFACE_FLAG_FULL_DUPLEX)
287
288   /* Hardware link speed */
289 #define VNET_HW_INTERFACE_FLAG_SPEED_SHIFT      3
290 #define VNET_HW_INTERFACE_FLAG_SPEED_10M        (1 << 3)
291 #define VNET_HW_INTERFACE_FLAG_SPEED_100M       (1 << 4)
292 #define VNET_HW_INTERFACE_FLAG_SPEED_1G         (1 << 5)
293 #define VNET_HW_INTERFACE_FLAG_SPEED_10G        (1 << 6)
294 #define VNET_HW_INTERFACE_FLAG_SPEED_40G        (1 << 7)
295 #define VNET_HW_INTERFACE_FLAG_SPEED_100G       (1 << 8)
296 #define VNET_HW_INTERFACE_FLAG_SPEED_MASK       \
297   (VNET_HW_INTERFACE_FLAG_SPEED_10M |           \
298    VNET_HW_INTERFACE_FLAG_SPEED_100M |          \
299    VNET_HW_INTERFACE_FLAG_SPEED_1G |            \
300    VNET_HW_INTERFACE_FLAG_SPEED_10G |           \
301    VNET_HW_INTERFACE_FLAG_SPEED_40G |           \
302    VNET_HW_INTERFACE_FLAG_SPEED_100G)
303
304   /* l2output node flags */
305 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_SHIFT   9
306 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED  (1 << 9)
307
308   /* Hardware address as vector.  Zero (e.g. zero-length vector) if no
309      address for this class (e.g. PPP). */
310   u8 *hw_address;
311
312   /* Interface is up as far as software is concerned. */
313   /* NAME.{output,tx} nodes for this interface. */
314   u32 output_node_index, tx_node_index;
315
316   /* (dev_class, dev_instance) uniquely identifies hw interface. */
317   u32 dev_class_index;
318   u32 dev_instance;
319
320   /* (hw_class, hw_instance) uniquely identifies hw interface. */
321   u32 hw_class_index;
322   u32 hw_instance;
323
324   /* Hardware index for this hardware interface. */
325   u32 hw_if_index;
326
327   /* Software index for this hardware interface. */
328   u32 sw_if_index;
329
330   /* Maximum transmit rate for this interface in bits/sec. */
331   f64 max_rate_bits_per_sec;
332
333   /* Smallest packet size supported by this interface. */
334   u32 min_supported_packet_bytes;
335
336   /* Largest packet size supported by this interface. */
337   u32 max_supported_packet_bytes;
338
339   /* Smallest packet size for this interface. */
340   u32 min_packet_bytes;
341
342   /* Largest packet size for this interface. */
343   u32 max_packet_bytes;
344
345   /* Number of extra bytes that go on the wire.
346      Packet length on wire
347      = max (length + per_packet_overhead_bytes, min_packet_bytes). */
348   u32 per_packet_overhead_bytes;
349
350   /* Receive and transmit layer 3 packet size limits (MRU/MTU). */
351   u32 max_l3_packet_bytes[VLIB_N_RX_TX];
352
353   /* Hash table mapping sub interface id to sw_if_index. */
354   uword *sub_interface_sw_if_index_by_id;
355
356   /* Count of number of L2 subinterfaces */
357   u32 l2_if_count;
358
359   /* Bonded interface info -
360      0       - not a bonded interface nor a slave
361      ~0      - slave to a bonded interface
362      others  - A bonded interface with a pointer to bitmap for all slaves */
363   uword *bond_info;
364 #define VNET_HW_INTERFACE_BOND_INFO_NONE ((uword *) 0)
365 #define VNET_HW_INTERFACE_BOND_INFO_SLAVE ((uword *) ~0)
366
367 } vnet_hw_interface_t;
368
369 extern vnet_device_class_t vnet_local_interface_device_class;
370
371 typedef enum
372 {
373   /* A hw interface. */
374   VNET_SW_INTERFACE_TYPE_HARDWARE,
375
376   /* A sub-interface. */
377   VNET_SW_INTERFACE_TYPE_SUB,
378 } vnet_sw_interface_type_t;
379
380 typedef struct
381 {
382   /*
383    * Subinterface ID. A number 0-N to uniquely identify
384    * this subinterface under the main (parent?) interface
385    */
386   u32 id;
387
388   /* Classification data. Used to associate packet header with subinterface. */
389   struct
390   {
391     u16 outer_vlan_id;
392     u16 inner_vlan_id;
393     union
394     {
395       u16 raw_flags;
396       struct
397       {
398         u16 no_tags:1;
399         u16 one_tag:1;
400         u16 two_tags:1;
401         u16 dot1ad:1;           /* 0 = dot1q, 1=dot1ad */
402         u16 exact_match:1;
403         u16 default_sub:1;
404         u16 outer_vlan_id_any:1;
405         u16 inner_vlan_id_any:1;
406       } flags;
407     };
408   } eth;
409 } vnet_sub_interface_t;
410
411 /* Software-interface.  This corresponds to a Ethernet VLAN, ATM vc, a
412    tunnel, etc.  Configuration (e.g. IP address) gets attached to
413    software interface. */
414 typedef struct
415 {
416   vnet_sw_interface_type_t type:16;
417
418   u16 flags;
419   /* Interface is "up" meaning adminstratively up.
420      Up in the sense of link state being up is maintained by hardware interface. */
421 #define VNET_SW_INTERFACE_FLAG_ADMIN_UP (1 << 0)
422
423   /* Interface is disabled for forwarding: punt all traffic to slow-path. */
424 #define VNET_SW_INTERFACE_FLAG_PUNT (1 << 1)
425
426 #define VNET_SW_INTERFACE_FLAG_PROXY_ARP (1 << 2)
427
428 #define VNET_SW_INTERFACE_FLAG_UNNUMBERED (1 << 3)
429
430 #define VNET_SW_INTERFACE_FLAG_BOND_SLAVE (1 << 4)
431
432   /* Index for this interface. */
433   u32 sw_if_index;
434
435   /* Software interface index of super-interface;
436      equal to sw_if_index if this interface is not a
437      sub-interface. */
438   u32 sup_sw_if_index;
439
440   /* this swif is unnumbered, use addresses on unnumbered_sw_if_index... */
441   u32 unnumbered_sw_if_index;
442
443   u32 link_speed;
444
445   u32 output_feature_bitmap;
446
447   union
448   {
449     /* VNET_SW_INTERFACE_TYPE_HARDWARE. */
450     u32 hw_if_index;
451
452     /* VNET_SW_INTERFACE_TYPE_SUB. */
453     vnet_sub_interface_t sub;
454   };
455 } vnet_sw_interface_t;
456
457 typedef enum
458 {
459   /* Simple counters. */
460   VNET_INTERFACE_COUNTER_DROP = 0,
461   VNET_INTERFACE_COUNTER_PUNT = 1,
462   VNET_INTERFACE_COUNTER_IP4 = 2,
463   VNET_INTERFACE_COUNTER_IP6 = 3,
464   VNET_INTERFACE_COUNTER_RX_NO_BUF = 4,
465   VNET_INTERFACE_COUNTER_RX_MISS = 5,
466   VNET_INTERFACE_COUNTER_RX_ERROR = 6,
467   VNET_INTERFACE_COUNTER_TX_ERROR = 7,
468   VNET_INTERFACE_COUNTER_MPLS = 8,
469   VNET_N_SIMPLE_INTERFACE_COUNTER = 9,
470   /* Combined counters. */
471   VNET_INTERFACE_COUNTER_RX = 0,
472   VNET_INTERFACE_COUNTER_TX = 1,
473   VNET_N_COMBINED_INTERFACE_COUNTER = 2,
474 } vnet_interface_counter_type_t;
475
476 typedef struct
477 {
478   u32 output_node_index;
479   u32 tx_node_index;
480 } vnet_hw_interface_nodes_t;
481
482 typedef struct
483 {
484   /* Hardware interfaces. */
485   vnet_hw_interface_t *hw_interfaces;
486
487   /* Hash table mapping HW interface name to index. */
488   uword *hw_interface_by_name;
489
490   /* Vectors if hardware interface classes and device classes. */
491   vnet_hw_interface_class_t *hw_interface_classes;
492   vnet_device_class_t *device_classes;
493
494   /* Hash table mapping name to hw interface/device class. */
495   uword *hw_interface_class_by_name;
496   uword *device_class_by_name;
497
498   /* Software interfaces. */
499   vnet_sw_interface_t *sw_interfaces;
500
501   /* Hash table mapping sub intfc sw_if_index by sup sw_if_index and sub id */
502   uword *sw_if_index_by_sup_and_sub;
503
504   /* Software interface counters both simple and combined
505      packet and byte counters. */
506   volatile u32 *sw_if_counter_lock;
507   vlib_simple_counter_main_t *sw_if_counters;
508   vlib_combined_counter_main_t *combined_sw_if_counters;
509
510   vnet_hw_interface_nodes_t *deleted_hw_interface_nodes;
511
512   /* pcap drop tracing */
513   int drop_pcap_enable;
514   pcap_main_t pcap_main;
515   u8 *pcap_filename;
516   u32 pcap_sw_if_index;
517   u32 pcap_pkts_to_capture;
518   uword *pcap_drop_filter_hash;
519
520 } vnet_interface_main_t;
521
522 static inline void
523 vnet_interface_counter_lock (vnet_interface_main_t * im)
524 {
525   if (im->sw_if_counter_lock)
526     while (__sync_lock_test_and_set (im->sw_if_counter_lock, 1))
527       /* zzzz */ ;
528 }
529
530 static inline void
531 vnet_interface_counter_unlock (vnet_interface_main_t * im)
532 {
533   if (im->sw_if_counter_lock)
534     *im->sw_if_counter_lock = 0;
535 }
536
537 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add);
538
539 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance);
540
541
542 /*
543  *  Output features
544  */
545
546 #define foreach_intf_output_feat \
547  _(IPSEC, "ipsec-output")
548
549 /* Feature bitmap positions */
550 typedef enum
551 {
552 #define _(sym,str) INTF_OUTPUT_FEAT_##sym,
553   foreach_intf_output_feat
554 #undef _
555     INTF_OUTPUT_N_FEAT,
556 } intf_output_feat_t;
557
558 /* flag that we are done with feature path */
559 #define INTF_OUTPUT_FEAT_DONE INTF_OUTPUT_N_FEAT
560
561 int vnet_interface_add_del_feature (struct vnet_main_t *vnm, vlib_main_t * vm,
562                                     u32 sw_if_index,
563                                     intf_output_feat_t feature, int is_add);
564
565 #endif /* included_vnet_interface_h */
566
567 /*
568  * fd.io coding-style-patch-verification: ON
569  *
570  * Local Variables:
571  * eval: (c-set-style "gnu")
572  * End:
573  */