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