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