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