dpdk: correct waiting times
[vpp.git] / src / plugins / dpdk / device / dpdk.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 #ifndef __included_dpdk_h__
16 #define __included_dpdk_h__
17
18 /* $$$$ We should rename always_inline -> clib_always_inline */
19 #undef always_inline
20
21 #define ALLOW_EXPERIMENTAL_API
22
23 #include <rte_config.h>
24
25 #include <rte_eal.h>
26 #include <rte_bus_pci.h>
27 #include <rte_bus_vmbus.h>
28 #include <rte_ethdev.h>
29 #include <rte_version.h>
30 #include <rte_net.h>
31 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
32 #include <rte_bus.h>
33 #include <rte_pci.h>
34 #include <ctype.h>
35
36 #include <bus_driver.h>
37 #include <bus_pci_driver.h>
38 #include <bus_vmbus_driver.h>
39 #endif
40
41 #include <vnet/devices/devices.h>
42
43 #if CLIB_DEBUG > 0
44 #define always_inline static inline
45 #else
46 #define always_inline static inline __attribute__ ((__always_inline__))
47 #endif
48
49 #include <vlib/pci/pci.h>
50 #include <vlib/vmbus/vmbus.h>
51 #include <vnet/flow/flow.h>
52
53 extern vnet_device_class_t dpdk_device_class;
54 extern vlib_node_registration_t dpdk_input_node;
55 extern vlib_node_registration_t admin_up_down_process_node;
56
57 typedef uint16_t dpdk_portid_t;
58
59 #define foreach_dpdk_device_flags                                             \
60   _ (0, ADMIN_UP, "admin-up")                                                 \
61   _ (1, PROMISC, "promisc")                                                   \
62   _ (3, PMD_INIT_FAIL, "pmd-init-fail")                                       \
63   _ (4, MAYBE_MULTISEG, "maybe-multiseg")                                     \
64   _ (5, HAVE_SUBIF, "subif")                                                  \
65   _ (9, TX_OFFLOAD, "tx-offload")                                             \
66   _ (10, INTEL_PHDR_CKSUM, "intel-phdr-cksum")                                \
67   _ (11, RX_FLOW_OFFLOAD, "rx-flow-offload")                                  \
68   _ (12, RX_IP4_CKSUM, "rx-ip4-cksum")                                        \
69   _ (13, INT_SUPPORTED, "int-supported")                                      \
70   _ (14, INT_UNMASKABLE, "int-unmaskable")
71
72 typedef enum
73 {
74 #define _(a, b, c) DPDK_DEVICE_FLAG_##b = (1 << a),
75   foreach_dpdk_device_flags
76 #undef _
77 } dpdk_device_flag_t;
78
79 typedef struct
80 {
81   u32 flow_index;
82   u32 mark;
83   struct rte_flow *handle;
84 } dpdk_flow_entry_t;
85
86 typedef struct
87 {
88   u32 flow_id;
89   u16 next_index;
90   i16 buffer_advance;
91 } dpdk_flow_lookup_entry_t;
92
93 typedef struct
94 {
95   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
96   u8 buffer_pool_index;
97   u32 queue_index;
98   int efd;
99   uword clib_file_index;
100 } dpdk_rx_queue_t;
101
102 typedef struct
103 {
104   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
105   clib_spinlock_t lock;
106   u32 queue_index;
107 } dpdk_tx_queue_t;
108
109 typedef struct
110 {
111   const char *name;
112   const char *desc;
113 } dpdk_driver_name_t;
114
115 typedef struct
116 {
117   dpdk_driver_name_t *drivers;
118   const char *interface_name_prefix;
119   u16 n_rx_desc;
120   u16 n_tx_desc;
121   u32 supported_flow_actions;
122   u32 enable_lsc_int : 1;
123   u32 enable_rxq_int : 1;
124   u32 disable_rx_scatter : 1;
125   u32 program_vlans : 1;
126   u32 mq_mode_none : 1;
127   u32 interface_number_from_port_id : 1;
128   u32 use_intel_phdr_cksum : 1;
129   u32 int_unmaskable : 1;
130 } dpdk_driver_t;
131
132 dpdk_driver_t *dpdk_driver_find (const char *name, const char **desc);
133
134 typedef union
135 {
136   struct
137   {
138     u16 disable_multi_seg : 1;
139     u16 enable_lro : 1;
140     u16 enable_tso : 1;
141     u16 enable_tcp_udp_checksum : 1;
142     u16 enable_outer_checksum_offload : 1;
143     u16 enable_lsc_int : 1;
144     u16 enable_rxq_int : 1;
145     u16 disable_tx_checksum_offload : 1;
146     u16 disable_rss : 1;
147     u16 disable_rx_scatter : 1;
148     u16 n_rx_queues;
149     u16 n_tx_queues;
150     u16 n_rx_desc;
151     u16 n_tx_desc;
152     u32 max_lro_pkt_size;
153     u64 rss_hf;
154   };
155   u64 as_u64[3];
156 } dpdk_port_conf_t;
157
158 STATIC_ASSERT_SIZEOF (dpdk_port_conf_t, 24);
159
160 typedef struct
161 {
162   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
163
164   dpdk_rx_queue_t *rx_queues;
165   dpdk_tx_queue_t *tx_queues;
166
167   /* Instance ID to access internal device array. */
168   u32 device_index;
169
170   u32 hw_if_index;
171   u32 sw_if_index;
172   u32 buffer_flags;
173
174   /* next node index if we decide to steal the rx graph arc */
175   u32 per_interface_next_index;
176
177   u16 flags;
178
179   /* DPDK device port number */
180   dpdk_portid_t port_id;
181   i8 cpu_socket;
182
183   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
184
185   u64 enabled_tx_off;
186   u64 enabled_rx_off;
187   dpdk_driver_t *driver;
188   u8 *name;
189   const char *if_desc;
190
191   /* number of sub-interfaces */
192   u16 num_subifs;
193
194   /* flow related */
195   u32 supported_flow_actions;
196   dpdk_flow_entry_t *flow_entries;      /* pool */
197   dpdk_flow_lookup_entry_t *flow_lookup_entries;        /* pool */
198   u32 *parked_lookup_indexes;   /* vector */
199   u32 parked_loop_count;
200   struct rte_flow_error last_flow_error;
201
202   struct rte_eth_link link;
203   f64 time_last_link_update;
204
205   struct rte_eth_stats stats;
206   struct rte_eth_stats last_stats;
207   struct rte_eth_xstat *xstats;
208   f64 time_last_stats_update;
209
210   /* mac address */
211   u8 *default_mac_address;
212
213   /* maximum supported max frame size */
214   u32 max_supported_frame_size;
215
216   /* due to lack of API to get ethernet max_frame_size we store information
217    * deducted from device info */
218   u8 driver_frame_overhead;
219
220   /* error string */
221   clib_error_t *errors;
222   dpdk_port_conf_t conf;
223 } dpdk_device_t;
224
225 #define DPDK_MIN_POLL_INTERVAL (0.001) /* 1msec */
226
227 #define DPDK_STATS_POLL_INTERVAL      (10.0)
228 #define DPDK_MIN_STATS_POLL_INTERVAL  DPDK_MIN_POLL_INTERVAL
229
230 #define DPDK_LINK_POLL_INTERVAL       (3.0)
231 #define DPDK_MIN_LINK_POLL_INTERVAL   DPDK_MIN_POLL_INTERVAL
232
233 #define foreach_dpdk_device_config_item                                       \
234   _ (num_rx_queues)                                                           \
235   _ (num_tx_queues)                                                           \
236   _ (num_rx_desc)                                                             \
237   _ (num_tx_desc)                                                             \
238   _ (max_lro_pkt_size)                                                        \
239   _ (rss_fn)
240
241 typedef enum
242 {
243   VNET_DEV_ADDR_PCI,
244   VNET_DEV_ADDR_VMBUS,
245   VNET_DEV_ADDR_ANY,
246 } dpdk_device_addr_type_t;
247
248 typedef struct
249 {
250   union
251   {
252     vlib_pci_addr_t pci_addr;
253     vlib_vmbus_addr_t vmbus_addr;
254   };
255   dpdk_device_addr_type_t dev_addr_type;
256   u8 *name;
257   u8 *tag;
258   u8 is_blacklisted;
259
260 #define _(x) uword x;
261     foreach_dpdk_device_config_item
262 #undef _
263     clib_bitmap_t * workers;
264   u8 tso;
265   u8 *devargs;
266   clib_bitmap_t *rss_queues;
267
268 #define DPDK_DEVICE_TSO_DEFAULT 0
269 #define DPDK_DEVICE_TSO_OFF 1
270 #define DPDK_DEVICE_TSO_ON  2
271 } dpdk_device_config_t;
272
273 typedef struct
274 {
275
276   /* Config stuff */
277   u8 **eal_init_args;
278   u8 *eal_init_args_str;
279   u8 *uio_driver_name;
280   u8 uio_bind_force;
281   u8 enable_telemetry;
282   u16 max_simd_bitwidth;
283
284 #define DPDK_MAX_SIMD_BITWIDTH_DEFAULT 0
285 #define DPDK_MAX_SIMD_BITWIDTH_256     256
286 #define DPDK_MAX_SIMD_BITWIDTH_512     512
287
288   /*
289    * format interface names ala xxxEthernet%d/%d/%d instead of
290    * xxxEthernet%x/%x/%x.
291    */
292   u8 interface_name_format_decimal;
293
294   /* per-device config */
295   dpdk_device_config_t default_devconf;
296   dpdk_device_config_t *dev_confs;
297   uword *device_config_index_by_pci_addr;
298   mhash_t device_config_index_by_vmbus_addr;
299
300   /* devices blacklist by pci vendor_id, device_id */
301   u32 *blacklist_by_pci_vendor_and_device;
302   /* devices blacklist by VMBUS address */
303   vlib_vmbus_addr_t *blacklist_by_vmbus_addr;
304
305 } dpdk_config_main_t;
306
307 extern dpdk_config_main_t dpdk_config_main;
308
309 #define DPDK_RX_BURST_SZ VLIB_FRAME_SIZE
310
311 typedef struct
312 {
313   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
314   struct rte_mbuf *mbufs[DPDK_RX_BURST_SZ];
315   u32 buffers[DPDK_RX_BURST_SZ];
316   u16 next[DPDK_RX_BURST_SZ];
317   u16 etype[DPDK_RX_BURST_SZ];
318   u32 flags[DPDK_RX_BURST_SZ];
319   vlib_buffer_t buffer_template;
320 } dpdk_per_thread_data_t;
321
322 typedef struct
323 {
324   /* Devices */
325   dpdk_device_t *devices;
326   dpdk_per_thread_data_t *per_thread_data;
327
328   /*
329    * flag indicating that a posted admin up/down
330    * (via post_sw_interface_set_flags) is in progress
331    */
332   u8 admin_up_down_in_progress;
333
334   /* control interval of dpdk link state and stat polling */
335   f64 link_state_poll_interval;
336   f64 stat_poll_interval;
337
338   dpdk_config_main_t *conf;
339   dpdk_port_conf_t default_port_conf;
340
341   /* API message ID base */
342   u16 msg_id_base;
343
344   /* logging */
345   vlib_log_class_t log_default;
346   vlib_log_class_t log_cryptodev;
347 } dpdk_main_t;
348
349 extern dpdk_main_t dpdk_main;
350
351 typedef struct
352 {
353   u32 buffer_index;
354   u16 device_index;
355   u8 queue_index;
356   struct rte_mbuf mb;
357   u8 data[256];                 /* First 256 data bytes, used for hexdump */
358   /* Copy of VLIB buffer; packet data stored in pre_data. */
359   vlib_buffer_t buffer;
360 } dpdk_tx_trace_t;
361
362 typedef struct
363 {
364   u32 buffer_index;
365   u16 device_index;
366   u16 queue_index;
367   struct rte_mbuf mb;
368   u8 data[256];                 /* First 256 data bytes, used for hexdump */
369   vlib_buffer_t buffer;         /* Copy of VLIB buffer; pkt data stored in pre_data. */
370 } dpdk_rx_trace_t;
371
372 void dpdk_device_setup (dpdk_device_t * xd);
373 void dpdk_device_start (dpdk_device_t * xd);
374 void dpdk_device_stop (dpdk_device_t * xd);
375 int dpdk_port_state_callback (dpdk_portid_t port_id,
376                               enum rte_eth_event_type type,
377                               void *param, void *ret_param);
378
379 #define foreach_dpdk_error                                              \
380   _(NONE, "no error")                                                   \
381   _(RX_PACKET_ERROR, "Rx packet errors")                                \
382   _(RX_BAD_FCS, "Rx bad fcs")                                           \
383   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
384   _(RX_ALLOC_FAIL, "rx buf alloc from free list failed")                \
385   _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem")              \
386   _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error")
387
388 typedef enum
389 {
390 #define _(f,s) DPDK_ERROR_##f,
391   foreach_dpdk_error
392 #undef _
393     DPDK_N_ERROR,
394 } dpdk_error_t;
395
396 #define dpdk_log_err(...) \
397   vlib_log(VLIB_LOG_LEVEL_ERR, dpdk_main.log_default, __VA_ARGS__)
398 #define dpdk_log_warn(...) \
399   vlib_log(VLIB_LOG_LEVEL_WARNING, dpdk_main.log_default, __VA_ARGS__)
400 #define dpdk_log_notice(...) \
401   vlib_log(VLIB_LOG_LEVEL_NOTICE, dpdk_main.log_default, __VA_ARGS__)
402 #define dpdk_log_info(...) \
403   vlib_log(VLIB_LOG_LEVEL_INFO, dpdk_main.log_default, __VA_ARGS__)
404 #define dpdk_log_debug(...)                                                   \
405   vlib_log (VLIB_LOG_LEVEL_DEBUG, dpdk_main.log_default, __VA_ARGS__)
406
407 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
408
409 #define foreach_dpdk_rss_hf                                                   \
410   _ (0, RTE_ETH_RSS_FRAG_IPV4, "ipv4-frag")                                   \
411   _ (1, RTE_ETH_RSS_NONFRAG_IPV4_TCP, "ipv4-tcp")                             \
412   _ (2, RTE_ETH_RSS_NONFRAG_IPV4_UDP, "ipv4-udp")                             \
413   _ (3, RTE_ETH_RSS_NONFRAG_IPV4_SCTP, "ipv4-sctp")                           \
414   _ (4, RTE_ETH_RSS_NONFRAG_IPV4_OTHER, "ipv4-other")                         \
415   _ (5, RTE_ETH_RSS_IPV4, "ipv4")                                             \
416   _ (6, RTE_ETH_RSS_IPV6_TCP_EX, "ipv6-tcp-ex")                               \
417   _ (7, RTE_ETH_RSS_IPV6_UDP_EX, "ipv6-udp-ex")                               \
418   _ (8, RTE_ETH_RSS_FRAG_IPV6, "ipv6-frag")                                   \
419   _ (9, RTE_ETH_RSS_NONFRAG_IPV6_TCP, "ipv6-tcp")                             \
420   _ (10, RTE_ETH_RSS_NONFRAG_IPV6_UDP, "ipv6-udp")                            \
421   _ (11, RTE_ETH_RSS_NONFRAG_IPV6_SCTP, "ipv6-sctp")                          \
422   _ (12, RTE_ETH_RSS_NONFRAG_IPV6_OTHER, "ipv6-other")                        \
423   _ (13, RTE_ETH_RSS_IPV6_EX, "ipv6-ex")                                      \
424   _ (14, RTE_ETH_RSS_IPV6, "ipv6")                                            \
425   _ (15, RTE_ETH_RSS_L2_PAYLOAD, "l2-payload")                                \
426   _ (16, RTE_ETH_RSS_PORT, "port")                                            \
427   _ (17, RTE_ETH_RSS_VXLAN, "vxlan")                                          \
428   _ (18, RTE_ETH_RSS_GENEVE, "geneve")                                        \
429   _ (19, RTE_ETH_RSS_NVGRE, "nvgre")                                          \
430   _ (20, RTE_ETH_RSS_GTPU, "gtpu")                                            \
431   _ (21, RTE_ETH_RSS_ESP, "esp")                                              \
432   _ (22, RTE_ETH_RSS_L2TPV3, "l2tpv3")                                        \
433   _ (60, RTE_ETH_RSS_L4_DST_ONLY, "l4-dst-only")                              \
434   _ (61, RTE_ETH_RSS_L4_SRC_ONLY, "l4-src-only")                              \
435   _ (62, RTE_ETH_RSS_L3_DST_ONLY, "l3-dst-only")                              \
436   _ (63, RTE_ETH_RSS_L3_SRC_ONLY, "l3-src-only")
437
438 format_function_t format_dpdk_device_name;
439 format_function_t format_dpdk_device;
440 format_function_t format_dpdk_device_errors;
441 format_function_t format_dpdk_tx_trace;
442 format_function_t format_dpdk_rx_trace;
443 format_function_t format_dpdk_rte_mbuf;
444 format_function_t format_dpdk_rx_rte_mbuf;
445 format_function_t format_dpdk_flow;
446 format_function_t format_dpdk_rss_hf_name;
447 format_function_t format_dpdk_rx_offload_caps;
448 format_function_t format_dpdk_tx_offload_caps;
449 format_function_t format_dpdk_burst_fn;
450 format_function_t format_dpdk_rte_device;
451 vnet_flow_dev_ops_function_t dpdk_flow_ops_fn;
452
453 clib_error_t *unformat_rss_fn (unformat_input_t * input, uword * rss_fn);
454
455 struct rte_pci_device *dpdk_get_pci_device (const struct rte_eth_dev_info
456                                             *info);
457 struct rte_vmbus_device *
458 dpdk_get_vmbus_device (const struct rte_eth_dev_info *info);
459 void dpdk_cli_reference (void);
460
461 #if CLI_DEBUG
462 int dpdk_buffer_validate_trajectory_all (u32 * uninitialized);
463 void dpdk_buffer_poison_trajectory_all (void);
464 #endif
465
466 #endif /* __included_dpdk_h__ */
467
468 /*
469  * fd.io coding-style-patch-verification: ON
470  *
471  * Local Variables:
472  * eval: (c-set-style "gnu")
473  * End:
474  */