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