dpdk: retire support for dpdk 16.07
[vpp.git] / src / vnet / devices / dpdk / 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 #include <rte_config.h>
22
23 #include <rte_common.h>
24 #include <rte_dev.h>
25 #include <rte_log.h>
26 #include <rte_memory.h>
27 #include <rte_memzone.h>
28 #include <rte_tailq.h>
29 #include <rte_eal.h>
30 #include <rte_per_lcore.h>
31 #include <rte_launch.h>
32 #include <rte_atomic.h>
33 #include <rte_cycles.h>
34 #include <rte_prefetch.h>
35 #include <rte_lcore.h>
36 #include <rte_per_lcore.h>
37 #include <rte_branch_prediction.h>
38 #include <rte_interrupts.h>
39 #include <rte_pci.h>
40 #include <rte_random.h>
41 #include <rte_debug.h>
42 #include <rte_ether.h>
43 #include <rte_ethdev.h>
44 #include <rte_ring.h>
45 #include <rte_mempool.h>
46 #include <rte_mbuf.h>
47 #include <rte_virtio_net.h>
48 #include <rte_version.h>
49 #include <rte_eth_bond.h>
50 #include <rte_sched.h>
51
52 #include <vnet/unix/pcap.h>
53 #include <vnet/devices/devices.h>
54
55 #if CLIB_DEBUG > 0
56 #define always_inline static inline
57 #else
58 #define always_inline static inline __attribute__ ((__always_inline__))
59 #endif
60
61 #include <vlib/pci/pci.h>
62
63 #define NB_MBUF   (16<<10)
64
65 extern vnet_device_class_t dpdk_device_class;
66 extern vlib_node_registration_t dpdk_input_node;
67 extern vlib_node_registration_t handoff_dispatch_node;
68
69 #define foreach_dpdk_pmd          \
70   _ ("net_thunderx", THUNDERX)    \
71   _ ("net_e1000_em", E1000EM)     \
72   _ ("net_e1000_igb", IGB)        \
73   _ ("net_e1000_igb_vf", IGBVF)   \
74   _ ("net_ixgbe", IXGBE)          \
75   _ ("net_ixgbe_vf", IXGBEVF)     \
76   _ ("net_i40e", I40E)            \
77   _ ("net_i40e_vf", I40EVF)       \
78   _ ("net_virtio", VIRTIO)        \
79   _ ("net_enic", ENIC)            \
80   _ ("net_vmxnet3", VMXNET3)      \
81   _ ("AF_PACKET PMD", AF_PACKET)  \
82   _ ("rte_bond_pmd", BOND)        \
83   _ ("net_fm10k", FM10K)          \
84   _ ("net_cxgbe", CXGBE)          \
85   _ ("net_mlx5", MLX5)            \
86   _ ("net_dpaa2", DPAA2)
87
88 typedef enum
89 {
90   VNET_DPDK_PMD_NONE,
91 #define _(s,f) VNET_DPDK_PMD_##f,
92   foreach_dpdk_pmd
93 #undef _
94     VNET_DPDK_PMD_UNKNOWN,      /* must be last */
95 } dpdk_pmd_t;
96
97 typedef enum
98 {
99   VNET_DPDK_PORT_TYPE_ETH_1G,
100   VNET_DPDK_PORT_TYPE_ETH_10G,
101   VNET_DPDK_PORT_TYPE_ETH_40G,
102   VNET_DPDK_PORT_TYPE_ETH_100G,
103   VNET_DPDK_PORT_TYPE_ETH_BOND,
104   VNET_DPDK_PORT_TYPE_ETH_SWITCH,
105   VNET_DPDK_PORT_TYPE_AF_PACKET,
106   VNET_DPDK_PORT_TYPE_UNKNOWN,
107 } dpdk_port_type_t;
108
109 /*
110  * The header for the tx_vector in dpdk_device_t.
111  * Head and tail are indexes into the tx_vector and are of type
112  * u64 so they never overflow.
113  */
114 typedef struct
115 {
116   u64 tx_head;
117   u64 tx_tail;
118 } tx_ring_hdr_t;
119
120 typedef struct
121 {
122   struct rte_ring *swq;
123
124   u64 hqos_field0_slabmask;
125   u32 hqos_field0_slabpos;
126   u32 hqos_field0_slabshr;
127   u64 hqos_field1_slabmask;
128   u32 hqos_field1_slabpos;
129   u32 hqos_field1_slabshr;
130   u64 hqos_field2_slabmask;
131   u32 hqos_field2_slabpos;
132   u32 hqos_field2_slabshr;
133   u32 hqos_tc_table[64];
134 } dpdk_device_hqos_per_worker_thread_t;
135
136 typedef struct
137 {
138   struct rte_ring **swq;
139   struct rte_mbuf **pkts_enq;
140   struct rte_mbuf **pkts_deq;
141   struct rte_sched_port *hqos;
142   u32 hqos_burst_enq;
143   u32 hqos_burst_deq;
144   u32 pkts_enq_len;
145   u32 swq_pos;
146   u32 flush_count;
147 } dpdk_device_hqos_per_hqos_thread_t;
148
149 typedef struct
150 {
151   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
152   volatile u32 **lockp;
153
154   /* Instance ID */
155   u32 device_index;
156
157   u32 vlib_hw_if_index;
158   u32 vlib_sw_if_index;
159
160   /* next node index if we decide to steal the rx graph arc */
161   u32 per_interface_next_index;
162
163   /* dpdk rte_mbuf rx and tx vectors, VLIB_FRAME_SIZE */
164   struct rte_mbuf ***tx_vectors;        /* one per worker thread */
165   struct rte_mbuf ***rx_vectors;
166
167   /* vector of traced contexts, per device */
168   u32 **d_trace_buffers;
169
170   dpdk_pmd_t pmd:8;
171   i8 cpu_socket;
172
173   u16 flags;
174 #define DPDK_DEVICE_FLAG_ADMIN_UP           (1 << 0)
175 #define DPDK_DEVICE_FLAG_PROMISC            (1 << 1)
176 #define DPDK_DEVICE_FLAG_PMD                (1 << 2)
177 #define DPDK_DEVICE_FLAG_PMD_SUPPORTS_PTYPE (1 << 3)
178 #define DPDK_DEVICE_FLAG_MAYBE_MULTISEG     (1 << 4)
179 #define DPDK_DEVICE_FLAG_HAVE_SUBIF         (1 << 5)
180 #define DPDK_DEVICE_FLAG_HQOS               (1 << 6)
181
182   u16 nb_tx_desc;
183     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
184
185   u8 *interface_name_suffix;
186
187   /* number of sub-interfaces */
188   u16 num_subifs;
189
190   /* PMD related */
191   u16 tx_q_used;
192   u16 rx_q_used;
193   u16 nb_rx_desc;
194   u16 *cpu_socket_id_by_queue;
195   struct rte_eth_conf port_conf;
196   struct rte_eth_txconf tx_conf;
197
198   /* HQoS related */
199   dpdk_device_hqos_per_worker_thread_t *hqos_wt;
200   dpdk_device_hqos_per_hqos_thread_t *hqos_ht;
201
202   /* af_packet */
203   u8 af_packet_port_id;
204
205   struct rte_eth_link link;
206   f64 time_last_link_update;
207
208   struct rte_eth_stats stats;
209   struct rte_eth_stats last_stats;
210   struct rte_eth_stats last_cleared_stats;
211   struct rte_eth_xstat *xstats;
212   struct rte_eth_xstat *last_cleared_xstats;
213   f64 time_last_stats_update;
214   dpdk_port_type_t port_type;
215
216   /* mac address */
217   u8 *default_mac_address;
218 } dpdk_device_t;
219
220 #define DPDK_STATS_POLL_INTERVAL      (10.0)
221 #define DPDK_MIN_STATS_POLL_INTERVAL  (0.001)   /* 1msec */
222
223 #define DPDK_LINK_POLL_INTERVAL       (3.0)
224 #define DPDK_MIN_LINK_POLL_INTERVAL   (0.001)   /* 1msec */
225
226 typedef struct
227 {
228   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
229
230   /* total input packet counter */
231   u64 aggregate_rx_packets;
232 } dpdk_worker_t;
233
234 typedef struct
235 {
236   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
237
238   /* total input packet counter */
239   u64 aggregate_rx_packets;
240 } dpdk_hqos_thread_t;
241
242 typedef struct
243 {
244   u32 device;
245   u16 queue_id;
246 } dpdk_device_and_queue_t;
247
248 #ifndef DPDK_HQOS_DBG_BYPASS
249 #define DPDK_HQOS_DBG_BYPASS 0
250 #endif
251
252 #ifndef HQOS_FLUSH_COUNT_THRESHOLD
253 #define HQOS_FLUSH_COUNT_THRESHOLD              100000
254 #endif
255
256 typedef struct dpdk_device_config_hqos_t
257 {
258   u32 hqos_thread;
259   u32 hqos_thread_valid;
260
261   u32 swq_size;
262   u32 burst_enq;
263   u32 burst_deq;
264
265   u32 pktfield0_slabpos;
266   u32 pktfield1_slabpos;
267   u32 pktfield2_slabpos;
268   u64 pktfield0_slabmask;
269   u64 pktfield1_slabmask;
270   u64 pktfield2_slabmask;
271   u32 tc_table[64];
272
273   struct rte_sched_port_params port;
274   struct rte_sched_subport_params *subport;
275   struct rte_sched_pipe_params *pipe;
276   uint32_t *pipe_map;
277 } dpdk_device_config_hqos_t;
278
279 int dpdk_hqos_validate_mask (u64 mask, u32 n);
280 void dpdk_device_config_hqos_pipe_profile_default (dpdk_device_config_hqos_t *
281                                                    hqos, u32 pipe_profile_id);
282 void dpdk_device_config_hqos_default (dpdk_device_config_hqos_t * hqos);
283 clib_error_t *dpdk_port_setup_hqos (dpdk_device_t * xd,
284                                     dpdk_device_config_hqos_t * hqos);
285 void dpdk_hqos_metadata_set (dpdk_device_hqos_per_worker_thread_t * hqos,
286                              struct rte_mbuf **pkts, u32 n_pkts);
287
288 #define foreach_dpdk_device_config_item \
289   _ (num_rx_queues) \
290   _ (num_tx_queues) \
291   _ (num_rx_desc) \
292   _ (num_tx_desc) \
293   _ (rss_fn)
294
295 typedef struct
296 {
297   vlib_pci_addr_t pci_addr;
298   u8 is_blacklisted;
299   u8 vlan_strip_offload;
300 #define DPDK_DEVICE_VLAN_STRIP_DEFAULT 0
301 #define DPDK_DEVICE_VLAN_STRIP_OFF 1
302 #define DPDK_DEVICE_VLAN_STRIP_ON  2
303
304 #define _(x) uword x;
305     foreach_dpdk_device_config_item
306 #undef _
307     clib_bitmap_t * workers;
308   u32 hqos_enabled;
309   dpdk_device_config_hqos_t hqos;
310 } dpdk_device_config_t;
311
312 typedef struct
313 {
314
315   /* Config stuff */
316   u8 **eal_init_args;
317   u8 *eal_init_args_str;
318   u8 *uio_driver_name;
319   u8 no_multi_seg;
320   u8 enable_tcp_udp_checksum;
321   u8 cryptodev;
322
323   /* Required config parameters */
324   u8 coremask_set_manually;
325   u8 nchannels_set_manually;
326   u32 coremask;
327   u32 nchannels;
328   u32 num_mbufs;
329   u8 num_kni;                   /* while kni_init allows u32, port_id in callback fn is only u8 */
330
331   /*
332    * format interface names ala xxxEthernet%d/%d/%d instead of
333    * xxxEthernet%x/%x/%x.
334    */
335   u8 interface_name_format_decimal;
336
337   /* per-device config */
338   dpdk_device_config_t default_devconf;
339   dpdk_device_config_t *dev_confs;
340   uword *device_config_index_by_pci_addr;
341
342 } dpdk_config_main_t;
343
344 dpdk_config_main_t dpdk_config_main;
345
346 typedef struct
347 {
348
349   /* Devices */
350   dpdk_device_t *devices;
351   dpdk_device_and_queue_t **devices_by_cpu;
352   dpdk_device_and_queue_t **devices_by_hqos_cpu;
353
354   /* per-thread recycle lists */
355   u32 **recycle;
356
357   /* buffer flags template, configurable to enable/disable tcp / udp cksum */
358   u32 buffer_flags_template;
359
360   /* vlib buffer free list, must be same size as an rte_mbuf */
361   u32 vlib_buffer_free_list_index;
362
363   /* dpdk worker "threads" */
364   dpdk_worker_t *workers;
365
366   /* dpdk HQoS "threads" */
367   dpdk_hqos_thread_t *hqos_threads;
368
369   /* Ethernet input node index */
370   u32 ethernet_input_node_index;
371
372   /* pcap tracing [only works if (CLIB_DEBUG > 0)] */
373   int tx_pcap_enable;
374   pcap_main_t pcap_main;
375   u8 *pcap_filename;
376   u32 pcap_sw_if_index;
377   u32 pcap_pkts_to_capture;
378
379   /* hashes */
380   uword *dpdk_device_by_kni_port_id;
381   uword *vu_sw_if_index_by_listener_fd;
382   uword *vu_sw_if_index_by_sock_fd;
383   u32 *vu_inactive_interfaces_device_index;
384
385   /*
386    * flag indicating that a posted admin up/down
387    * (via post_sw_interface_set_flags) is in progress
388    */
389   u8 admin_up_down_in_progress;
390
391   u8 use_rss;
392
393   /* which cpus are running dpdk-input */
394   int input_cpu_first_index;
395   int input_cpu_count;
396
397   /* which cpus are running I/O TX */
398   int hqos_cpu_first_index;
399   int hqos_cpu_count;
400
401   /* control interval of dpdk link state and stat polling */
402   f64 link_state_poll_interval;
403   f64 stat_poll_interval;
404
405   /* Sleep for this many MS after each device poll */
406   u32 poll_sleep;
407
408   /* convenience */
409   vlib_main_t *vlib_main;
410   vnet_main_t *vnet_main;
411   dpdk_config_main_t *conf;
412
413   /* mempool */
414   struct rte_mempool **pktmbuf_pools;
415 } dpdk_main_t;
416
417 dpdk_main_t dpdk_main;
418
419 typedef struct
420 {
421   u32 buffer_index;
422   u16 device_index;
423   u8 queue_index;
424   struct rte_mbuf mb;
425   /* Copy of VLIB buffer; packet data stored in pre_data. */
426   vlib_buffer_t buffer;
427 } dpdk_tx_dma_trace_t;
428
429 typedef struct
430 {
431   u32 buffer_index;
432   u16 device_index;
433   u16 queue_index;
434   struct rte_mbuf mb;
435   vlib_buffer_t buffer;         /* Copy of VLIB buffer; pkt data stored in pre_data. */
436   u8 data[256];                 /* First 256 data bytes, used for hexdump */
437 } dpdk_rx_dma_trace_t;
438
439 void vnet_buffer_needs_dpdk_mb (vlib_buffer_t * b);
440
441 clib_error_t *dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address);
442
443 clib_error_t *dpdk_set_mc_filter (vnet_hw_interface_t * hi,
444                                   struct ether_addr mc_addr_vec[], int naddr);
445
446 void dpdk_thread_input (dpdk_main_t * dm, dpdk_device_t * xd);
447
448 clib_error_t *dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd);
449
450 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance);
451
452 struct rte_mbuf *dpdk_replicate_packet_mb (vlib_buffer_t * b);
453 struct rte_mbuf *dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b);
454
455 #define foreach_dpdk_error                                              \
456   _(NONE, "no error")                                                   \
457   _(RX_PACKET_ERROR, "Rx packet errors")                                \
458   _(RX_BAD_FCS, "Rx bad fcs")                                           \
459   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
460   _(RX_ALLOC_FAIL, "rx buf alloc from free list failed")                \
461   _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem")              \
462   _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error")
463
464 typedef enum
465 {
466 #define _(f,s) DPDK_ERROR_##f,
467   foreach_dpdk_error
468 #undef _
469     DPDK_N_ERROR,
470 } dpdk_error_t;
471
472 int dpdk_set_stat_poll_interval (f64 interval);
473 int dpdk_set_link_state_poll_interval (f64 interval);
474 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
475 void dpdk_device_lock_init (dpdk_device_t * xd);
476 void dpdk_device_lock_free (dpdk_device_t * xd);
477
478 static inline u64
479 vnet_get_aggregate_rx_packets (void)
480 {
481   dpdk_main_t *dm = &dpdk_main;
482   u64 sum = 0;
483   dpdk_worker_t *dw;
484
485   vec_foreach (dw, dm->workers) sum += dw->aggregate_rx_packets;
486
487   return sum;
488 }
489
490 void dpdk_rx_trace (dpdk_main_t * dm,
491                     vlib_node_runtime_t * node,
492                     dpdk_device_t * xd,
493                     u16 queue_id, u32 * buffers, uword n_buffers);
494
495 #define EFD_OPERATION_LESS_THAN          0
496 #define EFD_OPERATION_GREATER_OR_EQUAL   1
497
498 format_function_t format_dpdk_device_name;
499 format_function_t format_dpdk_device;
500 format_function_t format_dpdk_tx_dma_trace;
501 format_function_t format_dpdk_rx_dma_trace;
502 format_function_t format_dpdk_rte_mbuf;
503 format_function_t format_dpdk_rx_rte_mbuf;
504 unformat_function_t unformat_socket_mem;
505 clib_error_t *unformat_rss_fn (unformat_input_t * input, uword * rss_fn);
506 clib_error_t *unformat_hqos (unformat_input_t * input,
507                              dpdk_device_config_hqos_t * hqos);
508
509 uword
510 admin_up_down_process (vlib_main_t * vm,
511                        vlib_node_runtime_t * rt, vlib_frame_t * f);
512
513 #endif /* __included_dpdk_h__ */
514
515 /*
516  * fd.io coding-style-patch-verification: ON
517  *
518  * Local Variables:
519  * eval: (c-set-style "gnu")
520  * End:
521  */