configurable per-dispatch-cycle sleep
[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_common.h>
26 #include <rte_dev.h>
27 #include <rte_memory.h>
28 #include <rte_eal.h>
29 #include <rte_per_lcore.h>
30 #include <rte_cycles.h>
31 #include <rte_lcore.h>
32 #include <rte_per_lcore.h>
33 #include <rte_interrupts.h>
34 #include <rte_pci.h>
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_ring.h>
38 #include <rte_mempool.h>
39 #include <rte_mbuf.h>
40 #include <rte_version.h>
41 #include <rte_eth_bond.h>
42 #include <rte_sched.h>
43 #include <rte_net.h>
44 #include <rte_bus_pci.h>
45 #include <rte_flow.h>
46
47 #include <vnet/unix/pcap.h>
48 #include <vnet/devices/devices.h>
49
50 #if CLIB_DEBUG > 0
51 #define always_inline static inline
52 #else
53 #define always_inline static inline __attribute__ ((__always_inline__))
54 #endif
55
56 #include <vlib/pci/pci.h>
57 #include <vnet/flow/flow.h>
58
59 #define NB_MBUF   (16<<10)
60
61 extern vnet_device_class_t dpdk_device_class;
62 extern vlib_node_registration_t dpdk_input_node;
63
64 #define foreach_dpdk_pmd          \
65   _ ("net_thunderx", THUNDERX)    \
66   _ ("net_e1000_em", E1000EM)     \
67   _ ("net_e1000_igb", IGB)        \
68   _ ("net_e1000_igb_vf", IGBVF)   \
69   _ ("net_ixgbe", IXGBE)          \
70   _ ("net_ixgbe_vf", IXGBEVF)     \
71   _ ("net_i40e", I40E)            \
72   _ ("net_i40e_vf", I40EVF)       \
73   _ ("net_virtio", VIRTIO)        \
74   _ ("net_enic", ENIC)            \
75   _ ("net_vmxnet3", VMXNET3)      \
76   _ ("AF_PACKET PMD", AF_PACKET)  \
77   _ ("net_bonding", BOND)         \
78   _ ("net_fm10k", FM10K)          \
79   _ ("net_cxgbe", CXGBE)          \
80   _ ("net_mlx4", MLX4)            \
81   _ ("net_mlx5", MLX5)            \
82   _ ("net_dpaa2", DPAA2)          \
83   _ ("net_virtio_user", VIRTIO_USER) \
84   _ ("net_vhost", VHOST_ETHER)    \
85   _ ("net_ena", ENA)              \
86   _ ("net_failsafe", FAILSAFE)    \
87   _ ("net_liovf", LIOVF_ETHER)
88
89 typedef enum
90 {
91   VNET_DPDK_PMD_NONE,
92 #define _(s,f) VNET_DPDK_PMD_##f,
93   foreach_dpdk_pmd
94 #undef _
95     VNET_DPDK_PMD_UNKNOWN,      /* must be last */
96 } dpdk_pmd_t;
97
98 typedef enum
99 {
100   VNET_DPDK_PORT_TYPE_ETH_1G,
101   VNET_DPDK_PORT_TYPE_ETH_2_5G,
102   VNET_DPDK_PORT_TYPE_ETH_5G,
103   VNET_DPDK_PORT_TYPE_ETH_10G,
104   VNET_DPDK_PORT_TYPE_ETH_20G,
105   VNET_DPDK_PORT_TYPE_ETH_25G,
106   VNET_DPDK_PORT_TYPE_ETH_40G,
107   VNET_DPDK_PORT_TYPE_ETH_50G,
108   VNET_DPDK_PORT_TYPE_ETH_56G,
109   VNET_DPDK_PORT_TYPE_ETH_100G,
110   VNET_DPDK_PORT_TYPE_ETH_BOND,
111   VNET_DPDK_PORT_TYPE_ETH_SWITCH,
112   VNET_DPDK_PORT_TYPE_AF_PACKET,
113   VNET_DPDK_PORT_TYPE_ETH_VF,
114   VNET_DPDK_PORT_TYPE_VIRTIO_USER,
115   VNET_DPDK_PORT_TYPE_VHOST_ETHER,
116   VNET_DPDK_PORT_TYPE_FAILSAFE,
117   VNET_DPDK_PORT_TYPE_UNKNOWN,
118 } dpdk_port_type_t;
119
120 typedef uint16_t dpdk_portid_t;
121
122 typedef struct
123 {
124   /* Required for vec_validate_aligned */
125   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
126
127   struct rte_ring *swq;
128
129   u64 hqos_field0_slabmask;
130   u32 hqos_field0_slabpos;
131   u32 hqos_field0_slabshr;
132   u64 hqos_field1_slabmask;
133   u32 hqos_field1_slabpos;
134   u32 hqos_field1_slabshr;
135   u64 hqos_field2_slabmask;
136   u32 hqos_field2_slabpos;
137   u32 hqos_field2_slabshr;
138   u32 hqos_tc_table[64];
139 } dpdk_device_hqos_per_worker_thread_t;
140
141 typedef struct
142 {
143   /* Required for vec_validate_aligned */
144   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
145   struct rte_ring **swq;
146   struct rte_mbuf **pkts_enq;
147   struct rte_mbuf **pkts_deq;
148   struct rte_sched_port *hqos;
149   u32 hqos_burst_enq;
150   u32 hqos_burst_deq;
151   u32 pkts_enq_len;
152   u32 swq_pos;
153   u32 flush_count;
154 } dpdk_device_hqos_per_hqos_thread_t;
155
156 #define foreach_dpdk_device_flags \
157   _( 0, ADMIN_UP, "admin-up") \
158   _( 1, PROMISC, "promisc") \
159   _( 2, PMD, "pmd") \
160   _( 3, PMD_INIT_FAIL, "pmd-init-fail") \
161   _( 4, MAYBE_MULTISEG, "maybe-multiseg") \
162   _( 5, HAVE_SUBIF, "subif") \
163   _( 6, HQOS, "hqos") \
164   _( 7, BOND_SLAVE, "bond-slave") \
165   _( 8, BOND_SLAVE_UP, "bond-slave-up") \
166   _( 9, TX_OFFLOAD, "tx-offload") \
167   _(10, INTEL_PHDR_CKSUM, "intel-phdr-cksum") \
168   _(11, RX_FLOW_OFFLOAD, "rx-flow-offload")
169
170 enum
171 {
172 #define _(a, b, c) DPDK_DEVICE_FLAG_##b = (1 << a),
173   foreach_dpdk_device_flags
174 #undef _
175 };
176
177 typedef struct
178 {
179   u32 flow_index;
180   u32 mark;
181   struct rte_flow *handle;
182 } dpdk_flow_entry_t;
183
184 typedef struct
185 {
186   u32 flow_id;
187   u16 next_index;
188   i16 buffer_advance;
189 } dpdk_flow_lookup_entry_t;
190
191 typedef struct
192 {
193   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
194   volatile u32 **lockp;
195
196   /* Instance ID to access internal device array. */
197   dpdk_portid_t device_index;
198
199   /* DPDK device port number */
200   dpdk_portid_t port_id;
201
202   u32 hw_if_index;
203   u32 sw_if_index;
204
205   /* next node index if we decide to steal the rx graph arc */
206   u32 per_interface_next_index;
207
208   dpdk_pmd_t pmd:8;
209   i8 cpu_socket;
210
211   u16 flags;
212
213   u16 nb_tx_desc;
214     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
215
216   u8 *interface_name_suffix;
217
218   /* number of sub-interfaces */
219   u16 num_subifs;
220
221   /* PMD related */
222   u16 tx_q_used;
223   u16 rx_q_used;
224   u16 nb_rx_desc;
225   u16 *cpu_socket_id_by_queue;
226   u8 *buffer_pool_for_queue;
227   struct rte_eth_conf port_conf;
228   struct rte_eth_txconf tx_conf;
229
230   /* flow related */
231   u32 supported_flow_actions;
232   dpdk_flow_entry_t *flow_entries;      /* pool */
233   dpdk_flow_lookup_entry_t *flow_lookup_entries;        /* pool */
234   u32 *parked_lookup_indexes;   /* vector */
235   u32 parked_loop_count;
236   struct rte_flow_error last_flow_error;
237
238   /* HQoS related */
239   dpdk_device_hqos_per_worker_thread_t *hqos_wt;
240   dpdk_device_hqos_per_hqos_thread_t *hqos_ht;
241
242   /* af_packet or BondEthernet instance number */
243   u16 af_packet_instance_num;
244   u16 bond_instance_num;
245
246   /* Bonded interface port# of a slave -
247      only valid if DPDK_DEVICE_FLAG_BOND_SLAVE bit is set */
248   dpdk_portid_t bond_port;
249
250   struct rte_eth_link link;
251   f64 time_last_link_update;
252
253   struct rte_eth_stats stats;
254   struct rte_eth_stats last_stats;
255   struct rte_eth_stats last_cleared_stats;
256   struct rte_eth_xstat *xstats;
257   struct rte_eth_xstat *last_cleared_xstats;
258   f64 time_last_stats_update;
259   dpdk_port_type_t port_type;
260
261   /* mac address */
262   u8 *default_mac_address;
263
264   /* error string */
265   clib_error_t *errors;
266 } dpdk_device_t;
267
268 #define DPDK_STATS_POLL_INTERVAL      (10.0)
269 #define DPDK_MIN_STATS_POLL_INTERVAL  (0.001)   /* 1msec */
270
271 #define DPDK_LINK_POLL_INTERVAL       (3.0)
272 #define DPDK_MIN_LINK_POLL_INTERVAL   (0.001)   /* 1msec */
273
274 typedef struct
275 {
276   u32 device;
277   u16 queue_id;
278 } dpdk_device_and_queue_t;
279
280 #ifndef DPDK_HQOS_DBG_BYPASS
281 #define DPDK_HQOS_DBG_BYPASS 0
282 #endif
283
284 #ifndef HQOS_FLUSH_COUNT_THRESHOLD
285 #define HQOS_FLUSH_COUNT_THRESHOLD              100000
286 #endif
287
288 typedef struct dpdk_device_config_hqos_t
289 {
290   u32 hqos_thread;
291   u32 hqos_thread_valid;
292
293   u32 swq_size;
294   u32 burst_enq;
295   u32 burst_deq;
296
297   u32 pktfield0_slabpos;
298   u32 pktfield1_slabpos;
299   u32 pktfield2_slabpos;
300   u64 pktfield0_slabmask;
301   u64 pktfield1_slabmask;
302   u64 pktfield2_slabmask;
303   u32 tc_table[64];
304
305   struct rte_sched_port_params port;
306   struct rte_sched_subport_params *subport;
307   struct rte_sched_pipe_params *pipe;
308   uint32_t *pipe_map;
309 } dpdk_device_config_hqos_t;
310
311 int dpdk_hqos_validate_mask (u64 mask, u32 n);
312 void dpdk_device_config_hqos_pipe_profile_default (dpdk_device_config_hqos_t *
313                                                    hqos, u32 pipe_profile_id);
314 void dpdk_device_config_hqos_default (dpdk_device_config_hqos_t * hqos);
315 clib_error_t *dpdk_port_setup_hqos (dpdk_device_t * xd,
316                                     dpdk_device_config_hqos_t * hqos);
317 void dpdk_hqos_metadata_set (dpdk_device_hqos_per_worker_thread_t * hqos,
318                              struct rte_mbuf **pkts, u32 n_pkts);
319
320 #define foreach_dpdk_device_config_item \
321   _ (num_rx_queues) \
322   _ (num_tx_queues) \
323   _ (num_rx_desc) \
324   _ (num_tx_desc) \
325   _ (rss_fn)
326
327 typedef struct
328 {
329   vlib_pci_addr_t pci_addr;
330   u8 is_blacklisted;
331   u8 vlan_strip_offload;
332 #define DPDK_DEVICE_VLAN_STRIP_DEFAULT 0
333 #define DPDK_DEVICE_VLAN_STRIP_OFF 1
334 #define DPDK_DEVICE_VLAN_STRIP_ON  2
335
336 #define _(x) uword x;
337     foreach_dpdk_device_config_item
338 #undef _
339     clib_bitmap_t * workers;
340   u32 hqos_enabled;
341   dpdk_device_config_hqos_t hqos;
342 } dpdk_device_config_t;
343
344 typedef struct
345 {
346
347   /* Config stuff */
348   u8 **eal_init_args;
349   u8 *eal_init_args_str;
350   u8 *uio_driver_name;
351   u8 no_multi_seg;
352   u8 enable_tcp_udp_checksum;
353   u8 no_tx_checksum_offload;
354
355   /* Required config parameters */
356   u8 coremask_set_manually;
357   u8 nchannels_set_manually;
358   u32 coremask;
359   u32 nchannels;
360   u32 num_mbufs;
361
362   /*
363    * format interface names ala xxxEthernet%d/%d/%d instead of
364    * xxxEthernet%x/%x/%x.
365    */
366   u8 interface_name_format_decimal;
367
368   /* per-device config */
369   dpdk_device_config_t default_devconf;
370   dpdk_device_config_t *dev_confs;
371   uword *device_config_index_by_pci_addr;
372
373 } dpdk_config_main_t;
374
375 extern dpdk_config_main_t dpdk_config_main;
376
377 #define DPDK_RX_BURST_SZ VLIB_FRAME_SIZE
378
379 typedef struct
380 {
381   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
382   struct rte_mbuf *mbufs[DPDK_RX_BURST_SZ];
383   u32 buffers[DPDK_RX_BURST_SZ];
384   u16 next[DPDK_RX_BURST_SZ];
385   u16 etype[DPDK_RX_BURST_SZ];
386   u8 flags[DPDK_RX_BURST_SZ];
387   vlib_buffer_t buffer_template;
388 } dpdk_per_thread_data_t;
389
390 typedef struct
391 {
392   int pcap_enable;
393   pcap_main_t pcap_main;
394   u8 *pcap_filename;
395   u32 pcap_sw_if_index;
396   u32 pcap_pkts_to_capture;
397 } dpdk_pcap_t;
398
399 typedef struct
400 {
401
402   /* Devices */
403   dpdk_device_t *devices;
404   dpdk_device_and_queue_t **devices_by_hqos_cpu;
405   dpdk_per_thread_data_t *per_thread_data;
406
407   /* per-thread recycle lists */
408   u32 **recycle;
409
410   /* buffer flags template, configurable to enable/disable tcp / udp cksum */
411   u32 buffer_flags_template;
412
413   /* pcap tracing */
414   dpdk_pcap_t pcap[VLIB_N_RX_TX];
415
416   int pcap_enable;
417   pcap_main_t pcap_main;
418   u8 *pcap_filename;
419   u32 pcap_sw_if_index;
420   u32 pcap_pkts_to_capture;
421
422   /*
423    * flag indicating that a posted admin up/down
424    * (via post_sw_interface_set_flags) is in progress
425    */
426   u8 admin_up_down_in_progress;
427
428   /* which cpus are running I/O TX */
429   int hqos_cpu_first_index;
430   int hqos_cpu_count;
431
432   /* control interval of dpdk link state and stat polling */
433   f64 link_state_poll_interval;
434   f64 stat_poll_interval;
435
436   /* convenience */
437   vlib_main_t *vlib_main;
438   vnet_main_t *vnet_main;
439   dpdk_config_main_t *conf;
440
441   /* mempool */
442   struct rte_mempool **pktmbuf_pools;
443
444   /* API message ID base */
445   u16 msg_id_base;
446
447   /* logging */
448   vlib_log_class_t log_default;
449 } dpdk_main_t;
450
451 extern dpdk_main_t dpdk_main;
452
453 typedef struct
454 {
455   u32 buffer_index;
456   u16 device_index;
457   u8 queue_index;
458   struct rte_mbuf mb;
459   /* Copy of VLIB buffer; packet data stored in pre_data. */
460   vlib_buffer_t buffer;
461   u8 data[256];                 /* First 256 data bytes, used for hexdump */
462 } dpdk_tx_trace_t;
463
464 typedef struct
465 {
466   u32 buffer_index;
467   u16 device_index;
468   u16 queue_index;
469   struct rte_mbuf mb;
470   vlib_buffer_t buffer;         /* Copy of VLIB buffer; pkt data stored in pre_data. */
471   u8 data[256];                 /* First 256 data bytes, used for hexdump */
472 } dpdk_rx_trace_t;
473
474 void dpdk_device_setup (dpdk_device_t * xd);
475 void dpdk_device_start (dpdk_device_t * xd);
476 void dpdk_device_stop (dpdk_device_t * xd);
477
478 int dpdk_port_state_callback (dpdk_portid_t port_id,
479                               enum rte_eth_event_type type,
480                               void *param, void *ret_param);
481
482 #define foreach_dpdk_error                                              \
483   _(NONE, "no error")                                                   \
484   _(RX_PACKET_ERROR, "Rx packet errors")                                \
485   _(RX_BAD_FCS, "Rx bad fcs")                                           \
486   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
487   _(RX_ALLOC_FAIL, "rx buf alloc from free list failed")                \
488   _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem")              \
489   _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error")
490
491 typedef enum
492 {
493 #define _(f,s) DPDK_ERROR_##f,
494   foreach_dpdk_error
495 #undef _
496     DPDK_N_ERROR,
497 } dpdk_error_t;
498
499 #define dpdk_log_err(...) \
500   vlib_log(VLIB_LOG_LEVEL_ERR, dpdk_main.log_default, __VA_ARGS__)
501 #define dpdk_log_warn(...) \
502   vlib_log(VLIB_LOG_LEVEL_WARNING, dpdk_main.log_default, __VA_ARGS__)
503 #define dpdk_log_notice(...) \
504   vlib_log(VLIB_LOG_LEVEL_NOTICE, dpdk_main.log_default, __VA_ARGS__)
505 #define dpdk_log_info(...) \
506   vlib_log(VLIB_LOG_LEVEL_INFO, dpdk_main.log_default, __VA_ARGS__)
507
508 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
509
510 format_function_t format_dpdk_device_name;
511 format_function_t format_dpdk_device;
512 format_function_t format_dpdk_device_errors;
513 format_function_t format_dpdk_tx_trace;
514 format_function_t format_dpdk_rx_trace;
515 format_function_t format_dpdk_rte_mbuf;
516 format_function_t format_dpdk_rx_rte_mbuf;
517 format_function_t format_dpdk_flow;
518 unformat_function_t unformat_dpdk_log_level;
519 vnet_flow_dev_ops_function_t dpdk_flow_ops_fn;
520
521 clib_error_t *unformat_rss_fn (unformat_input_t * input, uword * rss_fn);
522 clib_error_t *unformat_hqos (unformat_input_t * input,
523                              dpdk_device_config_hqos_t * hqos);
524
525 uword
526 admin_up_down_process (vlib_main_t * vm,
527                        vlib_node_runtime_t * rt, vlib_frame_t * f);
528
529 clib_error_t *dpdk_pool_create (vlib_main_t * vm, u8 * pool_name,
530                                 u32 elt_size, u32 num_elts,
531                                 u32 pool_priv_size, u16 cache_size, u8 numa,
532                                 struct rte_mempool **_mp,
533                                 vlib_physmem_region_index_t * pri);
534
535 clib_error_t *dpdk_buffer_pool_create (vlib_main_t * vm, unsigned num_mbufs,
536                                        unsigned socket_id);
537
538 #if CLI_DEBUG
539 int dpdk_buffer_validate_trajectory_all (u32 * uninitialized);
540 void dpdk_buffer_poison_trajectory_all (void);
541 #endif
542
543 #endif /* __included_dpdk_h__ */
544
545 /*
546  * fd.io coding-style-patch-verification: ON
547  *
548  * Local Variables:
549  * eval: (c-set-style "gnu")
550  * End:
551  */