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