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