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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * interface.h: VNET interfaces/sub-interfaces
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #ifndef included_vnet_interface_h
41 #define included_vnet_interface_h
43 #include <vnet/unix/pcap.h>
44 #include <vnet/l3_types.h>
47 struct vnet_hw_interface_t;
48 struct vnet_sw_interface_t;
49 struct ip46_address_t;
51 /* Interface up/down callback. */
52 typedef clib_error_t *(vnet_interface_function_t)
53 (struct vnet_main_t * vnm, u32 if_index, u32 flags);
55 /* Sub-interface add/del callback. */
56 typedef clib_error_t *(vnet_subif_add_del_function_t)
57 (struct vnet_main_t * vnm, u32 if_index,
58 struct vnet_sw_interface_t * template, int is_add);
60 /* Interface set mac address callback. */
61 typedef clib_error_t *(vnet_interface_set_mac_address_function_t)
62 (struct vnet_hw_interface_t * hi, char *address);
64 typedef enum vnet_interface_function_priority_t_
66 VNET_ITF_FUNC_PRIORITY_LOW,
67 VNET_ITF_FUNC_PRIORITY_HIGH,
68 } vnet_interface_function_priority_t;
69 #define VNET_ITF_FUNC_N_PRIO ((vnet_interface_function_priority_t)VNET_ITF_FUNC_PRIORITY_HIGH+1)
71 typedef struct _vnet_interface_function_list_elt
73 struct _vnet_interface_function_list_elt *next_interface_function;
74 clib_error_t *(*fp) (struct vnet_main_t * vnm, u32 if_index, u32 flags);
75 } _vnet_interface_function_list_elt_t;
77 #define _VNET_INTERFACE_FUNCTION_DECL(f,tag) \
79 static void __vnet_interface_function_init_##tag##_##f (void) \
80 __attribute__((__constructor__)) ; \
82 static void __vnet_interface_function_init_##tag##_##f (void) \
84 vnet_main_t * vnm = vnet_get_main(); \
85 static _vnet_interface_function_list_elt_t init_function; \
86 init_function.next_interface_function = \
87 vnm->tag##_functions[VNET_ITF_FUNC_PRIORITY_LOW]; \
88 vnm->tag##_functions[VNET_ITF_FUNC_PRIORITY_LOW] = &init_function; \
89 init_function.fp = (void *) &f; \
92 #define _VNET_INTERFACE_FUNCTION_DECL_PRIO(f,tag,p) \
94 static void __vnet_interface_function_init_##tag##_##f (void) \
95 __attribute__((__constructor__)) ; \
97 static void __vnet_interface_function_init_##tag##_##f (void) \
99 vnet_main_t * vnm = vnet_get_main(); \
100 static _vnet_interface_function_list_elt_t init_function; \
101 init_function.next_interface_function = vnm->tag##_functions[p]; \
102 vnm->tag##_functions[p] = &init_function; \
103 init_function.fp = (void *) &f; \
106 #define VNET_HW_INTERFACE_ADD_DEL_FUNCTION(f) \
107 _VNET_INTERFACE_FUNCTION_DECL(f,hw_interface_add_del)
108 #define VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION(f) \
109 _VNET_INTERFACE_FUNCTION_DECL(f,hw_interface_link_up_down)
110 #define VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION_PRIO(f,p) \
111 _VNET_INTERFACE_FUNCTION_DECL_PRIO(f,hw_interface_link_up_down,p)
112 #define VNET_SW_INTERFACE_ADD_DEL_FUNCTION(f) \
113 _VNET_INTERFACE_FUNCTION_DECL(f,sw_interface_add_del)
114 #define VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(f) \
115 _VNET_INTERFACE_FUNCTION_DECL(f,sw_interface_admin_up_down)
116 #define VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION_PRIO(f,p) \
117 _VNET_INTERFACE_FUNCTION_DECL_PRIO(f,sw_interface_admin_up_down, p)
119 /* A class of hardware interface devices. */
120 typedef struct _vnet_device_class
122 /* Index into main vector. */
125 /* Device name (e.g. "FOOBAR 1234a"). */
128 /* Function to call when hardware interface is added/deleted. */
129 vnet_interface_function_t *interface_add_del_function;
131 /* Function to bring device administratively up/down. */
132 vnet_interface_function_t *admin_up_down_function;
134 /* Function to call when sub-interface is added/deleted */
135 vnet_subif_add_del_function_t *subif_add_del_function;
137 /* Redistribute flag changes/existence of this interface class. */
140 /* Transmit function. */
141 vlib_node_function_t *tx_function;
143 /* Error strings indexed by error code for this node. */
144 char **tx_function_error_strings;
146 /* Number of error codes used by this node. */
147 u32 tx_function_n_errors;
149 /* Renumber device name [only!] support, a control-plane kludge */
150 int (*name_renumber) (struct vnet_hw_interface_t * hi,
151 u32 new_dev_instance);
153 /* Format device instance as name. */
154 format_function_t *format_device_name;
156 /* Parse function for device name. */
157 unformat_function_t *unformat_device_name;
159 /* Format device verbosely for this class. */
160 format_function_t *format_device;
162 /* Trace buffer format for TX function. */
163 format_function_t *format_tx_trace;
165 /* Function to clear hardware counters for device. */
166 void (*clear_counters) (u32 dev_class_instance);
168 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
172 /* Called when hardware class of an interface changes. */
173 void (*hw_class_change) (struct vnet_main_t * vnm,
174 u32 hw_if_index, u32 new_hw_class_index);
176 /* Called to redirect traffic from a specific interface instance */
177 void (*rx_redirect_to_node) (struct vnet_main_t * vnm,
178 u32 hw_if_index, u32 node_index);
180 /* Link-list of all device classes set up by constructors created below */
181 struct _vnet_device_class *next_class_registration;
183 /* Splice vnet_interface_output_node into TX path */
184 u8 flatten_output_chains;
186 /* Function to set mac address. */
187 vnet_interface_set_mac_address_function_t *mac_addr_change_function;
188 } vnet_device_class_t;
190 #define VNET_DEVICE_CLASS(x,...) \
191 __VA_ARGS__ vnet_device_class_t x; \
192 static void __vnet_add_device_class_registration_##x (void) \
193 __attribute__((__constructor__)) ; \
194 static void __vnet_add_device_class_registration_##x (void) \
196 vnet_main_t * vnm = vnet_get_main(); \
197 x.next_class_registration = vnm->device_class_registrations; \
198 vnm->device_class_registrations = &x; \
200 __VA_ARGS__ vnet_device_class_t x
202 #define VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
204 __attribute__ ((flatten)) \
205 __attribute__ ((target (tgt))) \
207 fn ## _ ## arch ( vlib_main_t * vm, \
208 vlib_node_runtime_t * node, \
209 vlib_frame_t * frame) \
210 { return fn (vm, node, frame); }
212 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn) \
213 foreach_march_variant(VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE, fn)
216 #define VLIB_MULTIARCH_CLONE_AND_SELECT_FN(fn,...)
217 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
219 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn) \
220 VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn) \
221 CLIB_MULTIARCH_SELECT_FN(fn, static inline) \
222 static void __attribute__((__constructor__)) \
223 __vlib_device_tx_function_multiarch_select_##dev (void) \
224 { dev.tx_function = fn ## _multiarch_select(); }
228 * Link Type: A description of the protocol of packets on the link.
229 * On an ethernet link this maps directly into the ethertype. On a GRE tunnel
230 * it maps to the GRE-proto, etc for other lnk types.
232 typedef enum vnet_link_t_
243 } __attribute__ ((packed)) vnet_link_t;
245 #define VNET_LINKS { \
246 [VNET_LINK_ETHERNET] = "ethernet", \
247 [VNET_LINK_IP4] = "ipv4", \
248 [VNET_LINK_IP6] = "ipv6", \
249 [VNET_LINK_MPLS] = "mpls", \
250 [VNET_LINK_ARP] = "arp", \
254 * @brief Number of link types. Not part of the enum so it does not have to be included in
257 #define VNET_LINK_NUM (VNET_LINK_ARP+1)
260 * @brief Convert a link to to an Ethertype
262 extern vnet_l3_packet_type_t vnet_link_to_l3_proto (vnet_link_t link);
265 * @brief Attributes assignable to a HW interface Class.
267 typedef enum vnet_hw_interface_class_flags_t_
270 * @brief a point 2 point interface
272 VNET_HW_INTERFACE_CLASS_FLAG_P2P = (1 << 0),
273 } vnet_hw_interface_class_flags_t;
275 /* Layer-2 (e.g. Ethernet) interface class. */
276 typedef struct _vnet_hw_interface_class
278 /* Index into main vector. */
281 /* Class name (e.g. "Ethernet"). */
285 vnet_hw_interface_class_flags_t flags;
287 /* Function to call when hardware interface is added/deleted. */
288 vnet_interface_function_t *interface_add_del_function;
290 /* Function to bring interface administratively up/down. */
291 vnet_interface_function_t *admin_up_down_function;
293 /* Function to call when link state changes. */
294 vnet_interface_function_t *link_up_down_function;
296 /* Function to call when link MAC changes. */
297 vnet_interface_set_mac_address_function_t *mac_addr_change_function;
299 /* Format function to display interface name. */
300 format_function_t *format_interface_name;
302 /* Format function to display interface address. */
303 format_function_t *format_address;
305 /* Format packet header for this interface class. */
306 format_function_t *format_header;
308 /* Format device verbosely for this class. */
309 format_function_t *format_device;
311 /* Parser for hardware (e.g. ethernet) address. */
312 unformat_function_t *unformat_hw_address;
314 /* Parser for packet header for e.g. rewrite string. */
315 unformat_function_t *unformat_header;
317 /* Builds a rewrite string for the interface to the destination
318 * for the payload/link type. */
319 u8 *(*build_rewrite) (struct vnet_main_t * vnm,
321 vnet_link_t link_type, const void *dst_hw_address);
323 /* Update an adjacecny added by FIB (as opposed to via the
324 * neighbour resolution protocol). */
325 void (*update_adjacency) (struct vnet_main_t * vnm,
326 u32 sw_if_index, u32 adj_index);
328 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
332 /* Called when hw interface class is changed and old hardware instance
333 may want to be deleted. */
334 void (*hw_class_change) (struct vnet_main_t * vnm, u32 hw_if_index,
335 u32 old_class_index, u32 new_class_index);
337 /* List of hw interface classes, built by constructors */
338 struct _vnet_hw_interface_class *next_class_registration;
340 } vnet_hw_interface_class_t;
343 * @brief Return a complete, zero-length (aka dummy) rewrite
345 extern u8 *default_build_rewrite (struct vnet_main_t *vnm,
347 vnet_link_t link_type,
348 const void *dst_hw_address);
351 * @brief Default adjacency update function
353 extern void default_update_adjacency (struct vnet_main_t *vnm,
354 u32 sw_if_index, u32 adj_index);
356 #define VNET_HW_INTERFACE_CLASS(x,...) \
357 __VA_ARGS__ vnet_hw_interface_class_t x; \
358 static void __vnet_add_hw_interface_class_registration_##x (void) \
359 __attribute__((__constructor__)) ; \
360 static void __vnet_add_hw_interface_class_registration_##x (void) \
362 vnet_main_t * vnm = vnet_get_main(); \
363 x.next_class_registration = vnm->hw_interface_class_registrations; \
364 vnm->hw_interface_class_registrations = &x; \
366 __VA_ARGS__ vnet_hw_interface_class_t x
368 /* Hardware-interface. This corresponds to a physical wire
369 that packets flow over. */
370 typedef struct vnet_hw_interface_t
372 /* Interface name. */
376 /* Hardware link state is up. */
377 #define VNET_HW_INTERFACE_FLAG_LINK_UP (1 << 0)
378 /* Hardware duplex state */
379 #define VNET_HW_INTERFACE_FLAG_DUPLEX_SHIFT 1
380 #define VNET_HW_INTERFACE_FLAG_HALF_DUPLEX (1 << 1)
381 #define VNET_HW_INTERFACE_FLAG_FULL_DUPLEX (1 << 2)
382 #define VNET_HW_INTERFACE_FLAG_DUPLEX_MASK \
383 (VNET_HW_INTERFACE_FLAG_HALF_DUPLEX | \
384 VNET_HW_INTERFACE_FLAG_FULL_DUPLEX)
386 /* Hardware link speed */
387 #define VNET_HW_INTERFACE_FLAG_SPEED_SHIFT 3
388 #define VNET_HW_INTERFACE_FLAG_SPEED_10M (1 << 3)
389 #define VNET_HW_INTERFACE_FLAG_SPEED_100M (1 << 4)
390 #define VNET_HW_INTERFACE_FLAG_SPEED_1G (1 << 5)
391 #define VNET_HW_INTERFACE_FLAG_SPEED_10G (1 << 6)
392 #define VNET_HW_INTERFACE_FLAG_SPEED_40G (1 << 7)
393 #define VNET_HW_INTERFACE_FLAG_SPEED_100G (1 << 8)
394 #define VNET_HW_INTERFACE_FLAG_SPEED_MASK \
395 (VNET_HW_INTERFACE_FLAG_SPEED_10M | \
396 VNET_HW_INTERFACE_FLAG_SPEED_100M | \
397 VNET_HW_INTERFACE_FLAG_SPEED_1G | \
398 VNET_HW_INTERFACE_FLAG_SPEED_10G | \
399 VNET_HW_INTERFACE_FLAG_SPEED_40G | \
400 VNET_HW_INTERFACE_FLAG_SPEED_100G)
402 /* l2output node flags */
403 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_SHIFT 9
404 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED (1 << 9)
406 /* Hardware address as vector. Zero (e.g. zero-length vector) if no
407 address for this class (e.g. PPP). */
410 /* Interface is up as far as software is concerned. */
411 /* NAME.{output,tx} nodes for this interface. */
412 u32 output_node_index, tx_node_index;
414 /* (dev_class, dev_instance) uniquely identifies hw interface. */
418 /* (hw_class, hw_instance) uniquely identifies hw interface. */
422 /* Hardware index for this hardware interface. */
425 /* Software index for this hardware interface. */
428 /* Maximum transmit rate for this interface in bits/sec. */
429 f64 max_rate_bits_per_sec;
431 /* Smallest packet size supported by this interface. */
432 u32 min_supported_packet_bytes;
434 /* Largest packet size supported by this interface. */
435 u32 max_supported_packet_bytes;
437 /* Smallest packet size for this interface. */
438 u32 min_packet_bytes;
440 /* Largest packet size for this interface. */
441 u32 max_packet_bytes;
443 /* Number of extra bytes that go on the wire.
444 Packet length on wire
445 = max (length + per_packet_overhead_bytes, min_packet_bytes). */
446 u32 per_packet_overhead_bytes;
448 /* Receive and transmit layer 3 packet size limits (MRU/MTU). */
449 u32 max_l3_packet_bytes[VLIB_N_RX_TX];
451 /* Hash table mapping sub interface id to sw_if_index. */
452 uword *sub_interface_sw_if_index_by_id;
454 /* Count of number of L2 subinterfaces */
457 /* Bonded interface info -
458 0 - not a bonded interface nor a slave
459 ~0 - slave to a bonded interface
460 others - A bonded interface with a pointer to bitmap for all slaves */
462 #define VNET_HW_INTERFACE_BOND_INFO_NONE ((uword *) 0)
463 #define VNET_HW_INTERFACE_BOND_INFO_SLAVE ((uword *) ~0)
465 } vnet_hw_interface_t;
467 extern vnet_device_class_t vnet_local_interface_device_class;
471 /* A hw interface. */
472 VNET_SW_INTERFACE_TYPE_HARDWARE,
474 /* A sub-interface. */
475 VNET_SW_INTERFACE_TYPE_SUB,
476 } vnet_sw_interface_type_t;
481 * Subinterface ID. A number 0-N to uniquely identify
482 * this subinterface under the main (parent?) interface
486 /* Classification data. Used to associate packet header with subinterface. */
499 u16 dot1ad:1; /* 0 = dot1q, 1=dot1ad */
502 u16 outer_vlan_id_any:1;
503 u16 inner_vlan_id_any:1;
507 } vnet_sub_interface_t;
512 VNET_FLOOD_CLASS_NORMAL,
513 VNET_FLOOD_CLASS_TUNNEL_MASTER,
514 /* Does not flood when tunnel master is in the same L2 BD */
515 VNET_FLOOD_CLASS_TUNNEL_NORMAL
516 } vnet_flood_class_t;
518 /* Software-interface. This corresponds to a Ethernet VLAN, ATM vc, a
519 tunnel, etc. Configuration (e.g. IP address) gets attached to
520 software interface. */
523 vnet_sw_interface_type_t type:16;
526 /* Interface is "up" meaning adminstratively up.
527 Up in the sense of link state being up is maintained by hardware interface. */
528 #define VNET_SW_INTERFACE_FLAG_ADMIN_UP (1 << 0)
530 /* Interface is disabled for forwarding: punt all traffic to slow-path. */
531 #define VNET_SW_INTERFACE_FLAG_PUNT (1 << 1)
533 #define VNET_SW_INTERFACE_FLAG_PROXY_ARP (1 << 2)
535 #define VNET_SW_INTERFACE_FLAG_UNNUMBERED (1 << 3)
537 #define VNET_SW_INTERFACE_FLAG_BOND_SLAVE (1 << 4)
539 /* Index for this interface. */
542 /* Software interface index of super-interface;
543 equal to sw_if_index if this interface is not a
547 /* this swif is unnumbered, use addresses on unnumbered_sw_if_index... */
548 u32 unnumbered_sw_if_index;
554 /* VNET_SW_INTERFACE_TYPE_HARDWARE. */
557 /* VNET_SW_INTERFACE_TYPE_SUB. */
558 vnet_sub_interface_t sub;
561 vnet_flood_class_t flood_class;
562 } vnet_sw_interface_t;
566 /* Simple counters. */
567 VNET_INTERFACE_COUNTER_DROP = 0,
568 VNET_INTERFACE_COUNTER_PUNT = 1,
569 VNET_INTERFACE_COUNTER_IP4 = 2,
570 VNET_INTERFACE_COUNTER_IP6 = 3,
571 VNET_INTERFACE_COUNTER_RX_NO_BUF = 4,
572 VNET_INTERFACE_COUNTER_RX_MISS = 5,
573 VNET_INTERFACE_COUNTER_RX_ERROR = 6,
574 VNET_INTERFACE_COUNTER_TX_ERROR = 7,
575 VNET_INTERFACE_COUNTER_MPLS = 8,
576 VNET_N_SIMPLE_INTERFACE_COUNTER = 9,
577 /* Combined counters. */
578 VNET_INTERFACE_COUNTER_RX = 0,
579 VNET_INTERFACE_COUNTER_TX = 1,
580 VNET_N_COMBINED_INTERFACE_COUNTER = 2,
581 } vnet_interface_counter_type_t;
585 u32 output_node_index;
587 } vnet_hw_interface_nodes_t;
591 /* Hardware interfaces. */
592 vnet_hw_interface_t *hw_interfaces;
594 /* Hash table mapping HW interface name to index. */
595 uword *hw_interface_by_name;
597 /* Vectors if hardware interface classes and device classes. */
598 vnet_hw_interface_class_t *hw_interface_classes;
599 vnet_device_class_t *device_classes;
601 /* Hash table mapping name to hw interface/device class. */
602 uword *hw_interface_class_by_name;
603 uword *device_class_by_name;
605 /* Software interfaces. */
606 vnet_sw_interface_t *sw_interfaces;
608 /* Hash table mapping sub intfc sw_if_index by sup sw_if_index and sub id */
609 uword *sw_if_index_by_sup_and_sub;
611 /* Software interface counters both simple and combined
612 packet and byte counters. */
613 volatile u32 *sw_if_counter_lock;
614 vlib_simple_counter_main_t *sw_if_counters;
615 vlib_combined_counter_main_t *combined_sw_if_counters;
617 vnet_hw_interface_nodes_t *deleted_hw_interface_nodes;
619 /* pcap drop tracing */
620 int drop_pcap_enable;
621 pcap_main_t pcap_main;
623 u32 pcap_sw_if_index;
624 u32 pcap_pkts_to_capture;
625 uword *pcap_drop_filter_hash;
627 /* feature_arc_index */
628 u8 output_feature_arc_index;
629 } vnet_interface_main_t;
632 vnet_interface_counter_lock (vnet_interface_main_t * im)
634 if (im->sw_if_counter_lock)
635 while (__sync_lock_test_and_set (im->sw_if_counter_lock, 1))
640 vnet_interface_counter_unlock (vnet_interface_main_t * im)
642 if (im->sw_if_counter_lock)
643 *im->sw_if_counter_lock = 0;
646 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add);
648 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance);
650 #endif /* included_vnet_interface_h */
653 * fd.io coding-style-patch-verification: ON
656 * eval: (c-set-style "gnu")