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>
46 struct vnet_hw_interface_t;
47 struct vnet_sw_interface_t;
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);
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);
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);
62 typedef struct _vnet_interface_function_list_elt
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;
68 #define _VNET_INTERFACE_FUNCTION_DECL(f,tag) \
70 static void __vnet_interface_function_init_##tag##_##f (void) \
71 __attribute__((__constructor__)) ; \
73 static void __vnet_interface_function_init_##tag##_##f (void) \
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; \
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)
91 /* A class of hardware interface devices. */
92 typedef struct _vnet_device_class
94 /* Index into main vector. */
97 /* Device name (e.g. "FOOBAR 1234a"). */
100 /* Function to call when hardware interface is added/deleted. */
101 vnet_interface_function_t *interface_add_del_function;
103 /* Function to bring device administratively up/down. */
104 vnet_interface_function_t *admin_up_down_function;
106 /* Function to call when sub-interface is added/deleted */
107 vnet_subif_add_del_function_t *subif_add_del_function;
109 /* Redistribute flag changes/existence of this interface class. */
112 /* Transmit function. */
113 vlib_node_function_t *tx_function;
115 /* Error strings indexed by error code for this node. */
116 char **tx_function_error_strings;
118 /* Number of error codes used by this node. */
119 u32 tx_function_n_errors;
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);
125 /* Format device instance as name. */
126 format_function_t *format_device_name;
128 /* Parse function for device name. */
129 unformat_function_t *unformat_device_name;
131 /* Format device verbosely for this class. */
132 format_function_t *format_device;
134 /* Trace buffer format for TX function. */
135 format_function_t *format_tx_trace;
137 /* Function to clear hardware counters for device. */
138 void (*clear_counters) (u32 dev_class_instance);
140 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
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);
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);
152 /* Link-list of all device classes set up by constructors created below */
153 struct _vnet_device_class *next_class_registration;
155 /* Do not splice vnet_interface_output_node into TX path */
156 u8 no_flatten_output_chains;
158 /* Function to set mac address. */
159 vnet_interface_set_mac_address_function_t *mac_addr_change_function;
161 } vnet_device_class_t;
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) \
169 vnet_main_t * vnm = vnet_get_main(); \
170 x.next_class_registration = vnm->device_class_registrations; \
171 vnm->device_class_registrations = &x; \
173 __VA_ARGS__ vnet_device_class_t x
175 #define VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
177 __attribute__ ((flatten)) \
178 __attribute__ ((target (tgt))) \
180 fn ## _ ## arch ( vlib_main_t * vm, \
181 vlib_node_runtime_t * node, \
182 vlib_frame_t * frame) \
183 { return fn (vm, node, frame); }
185 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn) \
186 foreach_march_variant(VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE, fn)
189 #define VLIB_MULTIARCH_CLONE_AND_SELECT_FN(fn,...)
190 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
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(); }
201 /* Layer-2 (e.g. Ethernet) interface class. */
202 typedef struct _vnet_hw_interface_class
204 /* Index into main vector. */
207 /* Class name (e.g. "Ethernet"). */
210 /* Function to call when hardware interface is added/deleted. */
211 vnet_interface_function_t *interface_add_del_function;
213 /* Function to bring interface administratively up/down. */
214 vnet_interface_function_t *admin_up_down_function;
216 /* Function to call when link state changes. */
217 vnet_interface_function_t *link_up_down_function;
219 /* Format function to display interface name. */
220 format_function_t *format_interface_name;
222 /* Format function to display interface address. */
223 format_function_t *format_address;
225 /* Format packet header for this interface class. */
226 format_function_t *format_header;
228 /* Format device verbosely for this class. */
229 format_function_t *format_device;
231 /* Parser for hardware (e.g. ethernet) address. */
232 unformat_function_t *unformat_hw_address;
234 /* Parser for packet header for e.g. rewrite string. */
235 unformat_function_t *unformat_header;
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,
243 void *rewrite, uword max_rewrite_bytes);
245 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
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);
254 /* List of hw interface classes, built by constructors */
255 struct _vnet_hw_interface_class *next_class_registration;
257 } vnet_hw_interface_class_t;
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) \
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; \
269 __VA_ARGS__ vnet_hw_interface_class_t x
271 /* Hardware-interface. This corresponds to a physical wire
272 that packets flow over. */
273 typedef struct vnet_hw_interface_t
275 /* Interface name. */
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)
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)
305 /* l2output node flags */
306 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_SHIFT 9
307 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED (1 << 9)
309 /* Hardware address as vector. Zero (e.g. zero-length vector) if no
310 address for this class (e.g. PPP). */
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;
317 /* (dev_class, dev_instance) uniquely identifies hw interface. */
321 /* (hw_class, hw_instance) uniquely identifies hw interface. */
325 /* Hardware index for this hardware interface. */
328 /* Software index for this hardware interface. */
331 /* Maximum transmit rate for this interface in bits/sec. */
332 f64 max_rate_bits_per_sec;
334 /* Smallest packet size supported by this interface. */
335 u32 min_supported_packet_bytes;
337 /* Largest packet size supported by this interface. */
338 u32 max_supported_packet_bytes;
340 /* Smallest packet size for this interface. */
341 u32 min_packet_bytes;
343 /* Largest packet size for this interface. */
344 u32 max_packet_bytes;
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;
351 /* Receive and transmit layer 3 packet size limits (MRU/MTU). */
352 u32 max_l3_packet_bytes[VLIB_N_RX_TX];
354 /* Hash table mapping sub interface id to sw_if_index. */
355 uword *sub_interface_sw_if_index_by_id;
357 /* Count of number of L2 subinterfaces */
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 */
365 #define VNET_HW_INTERFACE_BOND_INFO_NONE ((uword *) 0)
366 #define VNET_HW_INTERFACE_BOND_INFO_SLAVE ((uword *) ~0)
368 } vnet_hw_interface_t;
370 extern vnet_device_class_t vnet_local_interface_device_class;
374 /* A hw interface. */
375 VNET_SW_INTERFACE_TYPE_HARDWARE,
377 /* A sub-interface. */
378 VNET_SW_INTERFACE_TYPE_SUB,
379 } vnet_sw_interface_type_t;
384 * Subinterface ID. A number 0-N to uniquely identify
385 * this subinterface under the main (parent?) interface
389 /* Classification data. Used to associate packet header with subinterface. */
402 u16 dot1ad:1; /* 0 = dot1q, 1=dot1ad */
405 u16 outer_vlan_id_any:1;
406 u16 inner_vlan_id_any:1;
410 } vnet_sub_interface_t;
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. */
417 vnet_sw_interface_type_t type:16;
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)
424 /* Interface is disabled for forwarding: punt all traffic to slow-path. */
425 #define VNET_SW_INTERFACE_FLAG_PUNT (1 << 1)
427 #define VNET_SW_INTERFACE_FLAG_PROXY_ARP (1 << 2)
429 #define VNET_SW_INTERFACE_FLAG_UNNUMBERED (1 << 3)
431 #define VNET_SW_INTERFACE_FLAG_BOND_SLAVE (1 << 4)
433 /* Index for this interface. */
436 /* Software interface index of super-interface;
437 equal to sw_if_index if this interface is not a
441 /* this swif is unnumbered, use addresses on unnumbered_sw_if_index... */
442 u32 unnumbered_sw_if_index;
446 u32 output_feature_bitmap;
450 /* VNET_SW_INTERFACE_TYPE_HARDWARE. */
453 /* VNET_SW_INTERFACE_TYPE_SUB. */
454 vnet_sub_interface_t sub;
456 } vnet_sw_interface_t;
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;
479 u32 output_node_index;
481 } vnet_hw_interface_nodes_t;
485 /* Hardware interfaces. */
486 vnet_hw_interface_t *hw_interfaces;
488 /* Hash table mapping HW interface name to index. */
489 uword *hw_interface_by_name;
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;
495 /* Hash table mapping name to hw interface/device class. */
496 uword *hw_interface_class_by_name;
497 uword *device_class_by_name;
499 /* Software interfaces. */
500 vnet_sw_interface_t *sw_interfaces;
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;
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;
511 vnet_hw_interface_nodes_t *deleted_hw_interface_nodes;
513 /* pcap drop tracing */
514 int drop_pcap_enable;
515 pcap_main_t pcap_main;
517 u32 pcap_sw_if_index;
518 u32 pcap_pkts_to_capture;
519 uword *pcap_drop_filter_hash;
521 } vnet_interface_main_t;
524 vnet_interface_counter_lock (vnet_interface_main_t * im)
526 if (im->sw_if_counter_lock)
527 while (__sync_lock_test_and_set (im->sw_if_counter_lock, 1))
532 vnet_interface_counter_unlock (vnet_interface_main_t * im)
534 if (im->sw_if_counter_lock)
535 *im->sw_if_counter_lock = 0;
538 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add);
540 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance);
547 #define foreach_intf_output_feat \
548 _(IPSEC, "ipsec-output")
550 /* Feature bitmap positions */
553 #define _(sym,str) INTF_OUTPUT_FEAT_##sym,
554 foreach_intf_output_feat
557 } intf_output_feat_t;
559 /* flag that we are done with feature path */
560 #define INTF_OUTPUT_FEAT_DONE INTF_OUTPUT_N_FEAT
562 int vnet_interface_add_del_feature (struct vnet_main_t *vnm, vlib_main_t * vm,
564 intf_output_feat_t feature, int is_add);
566 #endif /* included_vnet_interface_h */
569 * fd.io coding-style-patch-verification: ON
572 * eval: (c-set-style "gnu")