FD.io VPP  v16.09
Vector Packet Processing
interface.h
Go to the documentation of this file.
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. */
51  (struct vnet_main_t * vnm, u32 if_index, u32 flags);
52 
53 /* Sub-interface add/del callback. */
55  (struct vnet_main_t * vnm, u32 if_index,
56  struct vnet_sw_interface_t * template, int is_add);
57 
58 typedef struct _vnet_interface_function_list_elt
59 {
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 {
90  /* Index into main vector. */
91  u32 index;
92 
93  /* Device name (e.g. "FOOBAR 1234a"). */
94  char *name;
95 
96  /* Function to call when hardware interface is added/deleted. */
97  vnet_interface_function_t *interface_add_del_function;
98 
99  /* Function to bring device administratively up/down. */
101 
102  /* Function to call when sub-interface is added/deleted */
103  vnet_subif_add_del_function_t *subif_add_del_function;
104 
105  /* Redistribute flag changes/existence of this interface class. */
106  u32 redistribute;
107 
108  /* Transmit function. */
109  vlib_node_function_t *tx_function;
110 
111  /* Error strings indexed by error code for this node. */
112  char **tx_function_error_strings;
113 
114  /* Number of error codes used by this node. */
115  u32 tx_function_n_errors;
116 
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);
120 
121  /* Format device instance as name. */
122  format_function_t *format_device_name;
123 
124  /* Parse function for device name. */
125  unformat_function_t *unformat_device_name;
126 
127  /* Format device verbosely for this class. */
128  format_function_t *format_device;
129 
130  /* Trace buffer format for TX function. */
131  format_function_t *format_tx_trace;
132 
133  /* Function to clear hardware counters for device. */
134  void (*clear_counters) (u32 dev_class_instance);
135 
136  uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
137  u32 hw_if_index,
138  u32 hw_class_index);
139 
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);
143 
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);
147 
148  /* Link-list of all device classes set up by constructors created below */
149  struct _vnet_device_class *next_class_registration;
150 
151  /* Do not splice vnet_interface_output_node into TX path */
152  u8 no_flatten_output_chains;
153 
155 
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) \
161 { \
162  vnet_main_t * vnm = vnet_get_main(); \
163  x.next_class_registration = vnm->device_class_registrations; \
164  vnm->device_class_registrations = &x; \
165 } \
166 __VA_ARGS__ vnet_device_class_t x
167 
168 #define VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE(arch, fn, tgt) \
169  uword \
170  __attribute__ ((flatten)) \
171  __attribute__ ((target (tgt))) \
172  CLIB_CPU_OPTIMIZED \
173  fn ## _ ## arch ( vlib_main_t * vm, \
174  vlib_node_runtime_t * node, \
175  vlib_frame_t * frame) \
176  { return fn (vm, node, frame); }
177 
178 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH_CLONE(fn) \
179  foreach_march_variant(VLIB_DEVICE_TX_FUNCTION_CLONE_TEMPLATE, fn)
180 
181 #if CLIB_DEBUG > 0
182 #define VLIB_MULTIARCH_CLONE_AND_SELECT_FN(fn,...)
183 #define VLIB_DEVICE_TX_FUNCTION_MULTIARCH(dev, fn)
184 #else
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(); }
191 #endif
192 
193 
194 /* Layer-2 (e.g. Ethernet) interface class. */
195 typedef struct _vnet_hw_interface_class
196 {
197  /* Index into main vector. */
198  u32 index;
199 
200  /* Class name (e.g. "Ethernet"). */
201  char *name;
202 
203  /* Function to call when hardware interface is added/deleted. */
204  vnet_interface_function_t *interface_add_del_function;
205 
206  /* Function to bring interface administratively up/down. */
208 
209  /* Function to call when link state changes. */
211 
212  /* Format function to display interface name. */
213  format_function_t *format_interface_name;
214 
215  /* Format function to display interface address. */
216  format_function_t *format_address;
217 
218  /* Format packet header for this interface class. */
219  format_function_t *format_header;
220 
221  /* Format device verbosely for this class. */
222  format_function_t *format_device;
223 
224  /* Parser for hardware (e.g. ethernet) address. */
225  unformat_function_t *unformat_hw_address;
226 
227  /* Parser for packet header for e.g. rewrite string. */
228  unformat_function_t *unformat_header;
229 
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,
233  u32 sw_if_index,
234  u32 l3_packet_type,
235  void *dst_address,
236  void *rewrite, uword max_rewrite_bytes);
237 
238  uword (*is_valid_class_for_interface) (struct vnet_main_t * vnm,
239  u32 hw_if_index,
240  u32 hw_class_index);
241 
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);
246 
247  /* List of hw interface classes, built by constructors */
248  struct _vnet_hw_interface_class *next_class_registration;
249 
251 
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) \
257 { \
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; \
261 } \
262 __VA_ARGS__ vnet_hw_interface_class_t x
263 
264 /* Hardware-interface. This corresponds to a physical wire
265  that packets flow over. */
266 typedef struct vnet_hw_interface_t
267 {
268  /* Interface name. */
270 
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)
281 
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)
297 
298  /* l2output node flags */
299 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_SHIFT 9
300 #define VNET_HW_INTERFACE_FLAG_L2OUTPUT_MAPPED (1 << 9)
301 
302  /* Hardware address as vector. Zero (e.g. zero-length vector) if no
303  address for this class (e.g. PPP). */
305 
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;
309 
310  /* (dev_class, dev_instance) uniquely identifies hw interface. */
313 
314  /* (hw_class, hw_instance) uniquely identifies hw interface. */
317 
318  /* Hardware index for this hardware interface. */
320 
321  /* Software index for this hardware interface. */
323 
324  /* Maximum transmit rate for this interface in bits/sec. */
326 
327  /* Smallest packet size supported by this interface. */
329 
330  /* Largest packet size supported by this interface. */
332 
333  /* Smallest packet size for this interface. */
335 
336  /* Largest packet size for this interface. */
338 
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). */
343 
344  /* Receive and transmit layer 3 packet size limits (MRU/MTU). */
345  u32 max_l3_packet_bytes[VLIB_N_RX_TX];
346 
347  /* Hash table mapping sub interface id to sw_if_index. */
349 
350  /* Count of number of L2 subinterfaces */
352 
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)
360 
362 
364 
365 typedef enum
366 {
367  /* A hw interface. */
369 
370  /* A sub-interface. */
373 
374 typedef struct
375 {
376  /*
377  * Subinterface ID. A number 0-N to uniquely identify
378  * this subinterface under the main (parent?) interface
379  */
381 
382  /* Classification data. Used to associate packet header with subinterface. */
383  struct
384  {
387  union
388  {
390  struct
391  {
392  u16 no_tags:1;
393  u16 one_tag:1;
394  u16 two_tags:1;
395  u16 dot1ad:1; /* 0 = dot1q, 1=dot1ad */
396  u16 exact_match:1;
397  u16 default_sub:1;
398  u16 outer_vlan_id_any:1;
399  u16 inner_vlan_id_any:1;
400  } flags;
401  };
402  } eth;
404 
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. */
408 typedef struct
409 {
410  vnet_sw_interface_type_t type:16;
411 
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)
416 
417  /* Interface is disabled for forwarding: punt all traffic to slow-path. */
418 #define VNET_SW_INTERFACE_FLAG_PUNT (1 << 1)
419 
420 #define VNET_SW_INTERFACE_FLAG_PROXY_ARP (1 << 2)
421 
422 #define VNET_SW_INTERFACE_FLAG_UNNUMBERED (1 << 3)
423 
424 #define VNET_SW_INTERFACE_FLAG_BOND_SLAVE (1 << 4)
425 
426  /* Index for this interface. */
428 
429  /* Software interface index of super-interface;
430  equal to sw_if_index if this interface is not a
431  sub-interface. */
433 
434  /* this swif is unnumbered, use addresses on unnumbered_sw_if_index... */
436 
438 
440 
441  union
442  {
443  /* VNET_SW_INTERFACE_TYPE_HARDWARE. */
445 
446  /* VNET_SW_INTERFACE_TYPE_SUB. */
448  };
450 
451 typedef enum
452 {
453  /* Simple counters. */
463  /* Combined counters. */
468 
469 typedef struct
470 {
474 
475 typedef struct
476 {
477  /* Hardware interfaces. */
479 
480  /* Hash table mapping HW interface name to index. */
482 
483  /* Vectors if hardware interface classes and device classes. */
486 
487  /* Hash table mapping name to hw interface/device class. */
490 
491  /* Software interfaces. */
493 
494  /* Hash table mapping sub intfc sw_if_index by sup sw_if_index and sub id */
496 
497  /* Software interface counters both simple and combined
498  packet and byte counters. */
502 
504 
505  /* pcap drop tracing */
512 
514 
515 static inline void
517 {
518  if (im->sw_if_counter_lock)
519  while (__sync_lock_test_and_set (im->sw_if_counter_lock, 1))
520  /* zzzz */ ;
521 }
522 
523 static inline void
525 {
526  if (im->sw_if_counter_lock)
527  *im->sw_if_counter_lock = 0;
528 }
529 
530 void vnet_pcap_drop_trace_filter_add_del (u32 error_index, int is_add);
531 
532 int vnet_interface_name_renumber (u32 sw_if_index, u32 new_show_dev_instance);
533 
534 
535 /*
536  * Output features
537  */
538 
539 #define foreach_intf_output_feat \
540  _(IPSEC, "ipsec-output")
541 
542 /* Feature bitmap positions */
543 typedef enum
544 {
545 #define _(sym,str) INTF_OUTPUT_FEAT_##sym,
547 #undef _
550 
551 /* flag that we are done with feature path */
552 #define INTF_OUTPUT_FEAT_DONE INTF_OUTPUT_N_FEAT
553 
555  u32 sw_if_index,
556  intf_output_feat_t feature, int is_add);
557 
558 #endif /* included_vnet_interface_h */
559 
560 /*
561  * fd.io coding-style-patch-verification: ON
562  *
563  * Local Variables:
564  * eval: (c-set-style "gnu")
565  * End:
566  */
vmrglw vmrglh hi
uword( vlib_node_function_t)(struct vlib_main_t *vm, struct vlib_node_runtime_t *node, struct vlib_frame_t *frame)
Definition: node.h:54
pcap_main_t pcap_main
Definition: interface.h:507
uword( unformat_function_t)(unformat_input_t *input, va_list *args)
Definition: format.h:231
volatile u32 * sw_if_counter_lock
Definition: interface.h:499
vnet_hw_interface_nodes_t * deleted_hw_interface_nodes
Definition: interface.h:503
int vnet_interface_add_del_feature(struct vnet_main_t *vnm, vlib_main_t *vm, u32 sw_if_index, intf_output_feat_t feature, int is_add)
Definition: interface.c:1180
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
PCAP utility definitions.
intf_output_feat_t
Definition: interface.h:543
uword * sub_interface_sw_if_index_by_id
Definition: interface.h:348
struct _vnet_device_class vnet_device_class_t
uword * pcap_drop_filter_hash
Definition: interface.h:511
PCAP main state data structure.
Definition: pcap.h:122
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:501
clib_error_t *( vnet_interface_function_t)(struct vnet_main_t *vnm, u32 if_index, u32 flags)
Definition: interface.h:51
A collection of simple counters.
Definition: counter.h:59
void vnet_pcap_drop_trace_filter_add_del(u32 error_index, int is_add)
u32 max_supported_packet_bytes
Definition: interface.h:331
uword * hw_interface_by_name
Definition: interface.h:481
vnet_hw_interface_t * hw_interfaces
Definition: interface.h:478
vnet_sub_interface_t sub
Definition: interface.h:447
uword * sw_if_index_by_sup_and_sub
Definition: interface.h:495
static clib_error_t * link_up_down_function(vnet_main_t *vm, u32 hw_if_index, u32 flags)
Definition: api.c:562
vlib_simple_counter_main_t * sw_if_counters
Definition: interface.h:500
vnet_device_class_t vnet_local_interface_device_class
uword * hw_interface_class_by_name
Definition: interface.h:488
static clib_error_t * admin_up_down_function(vnet_main_t *vm, u32 hw_if_index, u32 flags)
Definition: api.c:575
static void vnet_interface_counter_unlock(vnet_interface_main_t *im)
Definition: interface.h:524
vnet_interface_counter_type_t
Definition: interface.h:451
static void vnet_interface_counter_lock(vnet_interface_main_t *im)
Definition: interface.h:516
unsigned int u32
Definition: types.h:88
clib_error_t *( vnet_subif_add_del_function_t)(struct vnet_main_t *vnm, u32 if_index, struct vnet_sw_interface_t *template, int is_add)
Definition: interface.h:55
u8 *( format_function_t)(u8 *s, va_list *args)
Definition: format.h:48
uword * device_class_by_name
Definition: interface.h:489
u64 uword
Definition: types.h:112
struct vnet_hw_interface_t vnet_hw_interface_t
unsigned short u16
Definition: types.h:57
struct _vnet_hw_interface_class vnet_hw_interface_class_t
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:492
vnet_hw_interface_class_t * hw_interface_classes
Definition: interface.h:484
A collection of combined counters.
Definition: counter.h:212
u32 min_supported_packet_bytes
Definition: interface.h:328
#define foreach_intf_output_feat
Definition: interface.h:539
vnet_device_class_t * device_classes
Definition: interface.h:485
u32 per_packet_overhead_bytes
Definition: interface.h:342
u32 flags
Definition: vhost-user.h:76
int vnet_interface_name_renumber(u32 sw_if_index, u32 new_show_dev_instance)
Definition: interface.c:1151
vnet_sw_interface_type_t
Definition: interface.h:365