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 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;
64 #define _VNET_INTERFACE_FUNCTION_DECL(f,tag) \
66 static void __vnet_interface_function_init_##tag##_##f (void) \
67 __attribute__((__constructor__)) ; \
69 static void __vnet_interface_function_init_##tag##_##f (void) \
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; \
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)
87 /* A class of hardware interface devices. */
88 typedef struct _vnet_device_class
90 /* Index into main vector. */
93 /* Device name (e.g. "FOOBAR 1234a"). */
96 /* Function to call when hardware interface is added/deleted. */
97 vnet_interface_function_t *interface_add_del_function;
99 /* Function to bring device administratively up/down. */
100 vnet_interface_function_t *admin_up_down_function;
102 /* Function to call when sub-interface is added/deleted */
103 vnet_subif_add_del_function_t *subif_add_del_function;
105 /* Redistribute flag changes/existence of this interface class. */
108 /* Transmit function. */
109 vlib_node_function_t *tx_function;
111 /* Error strings indexed by error code for this node. */
112 char **tx_function_error_strings;
114 /* Number of error codes used by this node. */
115 u32 tx_function_n_errors;
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);
121 /* Format device instance as name. */
122 format_function_t *format_device_name;
124 /* Parse function for device name. */
125 unformat_function_t *unformat_device_name;
127 /* Format device verbosely for this class. */
128 format_function_t *format_device;
130 /* Trace buffer format for TX function. */
131 format_function_t *format_tx_trace;
133 /* Function to clear hardware counters for device. */
134 void (*clear_counters) (u32 dev_class_instance);
136 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
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);
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);
148 /* Link-list of all device classes set up by constructors created below */
149 struct _vnet_device_class *next_class_registration;
151 /* Do not splice vnet_interface_output_node into TX path */
152 u8 no_flatten_output_chains;
154 } vnet_device_class_t;
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) \
162 vnet_main_t * vnm = vnet_get_main(); \
163 x.next_class_registration = vnm->device_class_registrations; \
164 vnm->device_class_registrations = &x; \
166 __VA_ARGS__ vnet_device_class_t x
168 #define VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
170 __attribute__ ((flatten)) \
171 __attribute__ ((target (tgt))) \
173 fn ## _ ## arch ( vlib_main_t * vm, \
174 vlib_node_runtime_t * node, \
175 vlib_frame_t * frame) \
176 { return fn (vm, node, frame); }
178 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn) \
179 foreach_march_variant(VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE, fn)
182 #define VLIB_MULTIARCH_CLONE_AND_SELECT_FN(fn,...)
183 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
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(); }
194 /* Layer-2 (e.g. Ethernet) interface class. */
195 typedef struct _vnet_hw_interface_class
197 /* Index into main vector. */
200 /* Class name (e.g. "Ethernet"). */
203 /* Function to call when hardware interface is added/deleted. */
204 vnet_interface_function_t *interface_add_del_function;
206 /* Function to bring interface administratively up/down. */
207 vnet_interface_function_t *admin_up_down_function;
209 /* Function to call when link state changes. */
210 vnet_interface_function_t *link_up_down_function;
212 /* Format function to display interface name. */
213 format_function_t *format_interface_name;
215 /* Format function to display interface address. */
216 format_function_t *format_address;
218 /* Format packet header for this interface class. */
219 format_function_t *format_header;
221 /* Format device verbosely for this class. */
222 format_function_t *format_device;
224 /* Parser for hardware (e.g. ethernet) address. */
225 unformat_function_t *unformat_hw_address;
227 /* Parser for packet header for e.g. rewrite string. */
228 unformat_function_t *unformat_header;
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,
236 void *rewrite, uword max_rewrite_bytes);
238 uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
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);
247 /* List of hw interface classes, built by constructors */
248 struct _vnet_hw_interface_class *next_class_registration;
250 } vnet_hw_interface_class_t;
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) \
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; \
262 __VA_ARGS__ vnet_hw_interface_class_t x
264 /* Hardware-interface. This corresponds to a physical wire
265 that packets flow over. */
266 typedef struct vnet_hw_interface_t
268 /* Interface name. */
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)
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)
298 /* l2output node flags */
299 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_SHIFT 9
300 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED (1 << 9)
302 /* Hardware address as vector. Zero (e.g. zero-length vector) if no
303 address for this class (e.g. PPP). */
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;
310 /* (dev_class, dev_instance) uniquely identifies hw interface. */
314 /* (hw_class, hw_instance) uniquely identifies hw interface. */
318 /* Hardware index for this hardware interface. */
321 /* Software index for this hardware interface. */
324 /* Maximum transmit rate for this interface in bits/sec. */
325 f64 max_rate_bits_per_sec;
327 /* Smallest packet size supported by this interface. */
328 u32 min_supported_packet_bytes;
330 /* Largest packet size supported by this interface. */
331 u32 max_supported_packet_bytes;
333 /* Smallest packet size for this interface. */
334 u32 min_packet_bytes;
336 /* Largest packet size for this interface. */
337 u32 max_packet_bytes;
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;
344 /* Receive and transmit layer 3 packet size limits (MRU/MTU). */
345 u32 max_l3_packet_bytes[VLIB_N_RX_TX];
347 /* Hash table mapping sub interface id to sw_if_index. */
348 uword *sub_interface_sw_if_index_by_id;
350 /* Count of number of L2 subinterfaces */
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 */
358 #define VNET_HW_INTERFACE_BOND_INFO_NONE ((uword *) 0)
359 #define VNET_HW_INTERFACE_BOND_INFO_SLAVE ((uword *) ~0)
361 } vnet_hw_interface_t;
363 extern vnet_device_class_t vnet_local_interface_device_class;
367 /* A hw interface. */
368 VNET_SW_INTERFACE_TYPE_HARDWARE,
370 /* A sub-interface. */
371 VNET_SW_INTERFACE_TYPE_SUB,
372 } vnet_sw_interface_type_t;
377 * Subinterface ID. A number 0-N to uniquely identify
378 * this subinterface under the main (parent?) interface
382 /* Classification data. Used to associate packet header with subinterface. */
395 u16 dot1ad:1; /* 0 = dot1q, 1=dot1ad */
398 u16 outer_vlan_id_any:1;
399 u16 inner_vlan_id_any:1;
403 } vnet_sub_interface_t;
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. */
410 vnet_sw_interface_type_t type:16;
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)
417 /* Interface is disabled for forwarding: punt all traffic to slow-path. */
418 #define VNET_SW_INTERFACE_FLAG_PUNT (1 << 1)
420 #define VNET_SW_INTERFACE_FLAG_PROXY_ARP (1 << 2)
422 #define VNET_SW_INTERFACE_FLAG_UNNUMBERED (1 << 3)
424 #define VNET_SW_INTERFACE_FLAG_BOND_SLAVE (1 << 4)
426 /* Index for this interface. */
429 /* Software interface index of super-interface;
430 equal to sw_if_index if this interface is not a
434 /* this swif is unnumbered, use addresses on unnumbered_sw_if_index... */
435 u32 unnumbered_sw_if_index;
439 u32 output_feature_bitmap;
443 /* VNET_SW_INTERFACE_TYPE_HARDWARE. */
446 /* VNET_SW_INTERFACE_TYPE_SUB. */
447 vnet_sub_interface_t sub;
449 } vnet_sw_interface_t;
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_N_SIMPLE_INTERFACE_COUNTER = 8,
463 /* Combined counters. */
464 VNET_INTERFACE_COUNTER_RX = 0,
465 VNET_INTERFACE_COUNTER_TX = 1,
466 VNET_N_COMBINED_INTERFACE_COUNTER = 2,
467 } vnet_interface_counter_type_t;
471 u32 output_node_index;
473 } vnet_hw_interface_nodes_t;
477 /* Hardware interfaces. */
478 vnet_hw_interface_t *hw_interfaces;
480 /* Hash table mapping HW interface name to index. */
481 uword *hw_interface_by_name;
483 /* Vectors if hardware interface classes and device classes. */
484 vnet_hw_interface_class_t *hw_interface_classes;
485 vnet_device_class_t *device_classes;
487 /* Hash table mapping name to hw interface/device class. */
488 uword *hw_interface_class_by_name;
489 uword *device_class_by_name;
491 /* Software interfaces. */
492 vnet_sw_interface_t *sw_interfaces;
494 /* Hash table mapping sub intfc sw_if_index by sup sw_if_index and sub id */
495 uword *sw_if_index_by_sup_and_sub;
497 /* Software interface counters both simple and combined
498 packet and byte counters. */
499 volatile u32 *sw_if_counter_lock;
500 vlib_simple_counter_main_t *sw_if_counters;
501 vlib_combined_counter_main_t *combined_sw_if_counters;
503 vnet_hw_interface_nodes_t *deleted_hw_interface_nodes;
505 /* pcap drop tracing */
506 int drop_pcap_enable;
507 pcap_main_t pcap_main;
509 u32 pcap_sw_if_index;
510 u32 pcap_pkts_to_capture;
511 uword *pcap_drop_filter_hash;
513 } vnet_interface_main_t;
516 vnet_interface_counter_lock (vnet_interface_main_t * im)
518 if (im->sw_if_counter_lock)
519 while (__sync_lock_test_and_set (im->sw_if_counter_lock, 1))
524 vnet_interface_counter_unlock (vnet_interface_main_t * im)
526 if (im->sw_if_counter_lock)
527 *im->sw_if_counter_lock = 0;
530 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add);
532 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance);
539 #define foreach_intf_output_feat \
540 _(IPSEC, "ipsec-output")
542 /* Feature bitmap positions */
545 #define _(sym,str) INTF_OUTPUT_FEAT_##sym,
546 foreach_intf_output_feat
549 } intf_output_feat_t;
551 /* flag that we are done with feature path */
552 #define INTF_OUTPUT_FEAT_DONE INTF_OUTPUT_N_FEAT
554 int vnet_interface_add_del_feature (struct vnet_main_t *vnm, vlib_main_t * vm,
556 intf_output_feat_t feature, int is_add);
558 #endif /* included_vnet_interface_h */
561 * fd.io coding-style-patch-verification: ON
564 * eval: (c-set-style "gnu")