e34d4b97bdd5f7c17973e3398ddf2270b3949c81
[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
54 #if CLIB_DEBUG > 0
55 #define always_inline static inline
56 #else
57 #define always_inline static inline __attribute__ ((__always_inline__))
58 #endif
59
60 #include <vlib/pci/pci.h>
61
62 #define NB_MBUF   (16<<10)
63
64 extern vnet_device_class_t dpdk_device_class;
65 extern vlib_node_registration_t dpdk_input_node;
66 extern vlib_node_registration_t handoff_dispatch_node;
67
68 #if RTE_VERSION >= RTE_VERSION_NUM(16, 11, 0, 0)
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   _ ("net_af_packet", AF_PACKET)  \
82   _ ("net_bonding", BOND)         \
83   _ ("net_fm10k", FM10K)          \
84   _ ("net_cxgbe", CXGBE)          \
85   _ ("net_dpaa2", DPAA2)
86 #else
87 #define foreach_dpdk_pmd          \
88   _ ("rte_nicvf_pmd", THUNDERX)   \
89   _ ("rte_em_pmd", E1000EM)       \
90   _ ("rte_igb_pmd", IGB)          \
91   _ ("rte_igbvf_pmd", IGBVF)      \
92   _ ("rte_ixgbe_pmd", IXGBE)      \
93   _ ("rte_ixgbevf_pmd", IXGBEVF)  \
94   _ ("rte_i40e_pmd", I40E)        \
95   _ ("rte_i40evf_pmd", I40EVF)    \
96   _ ("rte_virtio_pmd", VIRTIO)    \
97   _ ("rte_enic_pmd", ENIC)        \
98   _ ("rte_vmxnet3_pmd", VMXNET3)  \
99   _ ("AF_PACKET PMD", AF_PACKET)  \
100   _ ("rte_bond_pmd", BOND)        \
101   _ ("rte_pmd_fm10k", FM10K)      \
102   _ ("rte_cxgbe_pmd", CXGBE)      \
103   _ ("rte_dpaa2_dpni", DPAA2)
104 #endif
105
106 typedef enum
107 {
108   VNET_DPDK_PMD_NONE,
109 #define _(s,f) VNET_DPDK_PMD_##f,
110   foreach_dpdk_pmd
111 #undef _
112     VNET_DPDK_PMD_UNKNOWN,      /* must be last */
113 } dpdk_pmd_t;
114
115 typedef enum
116 {
117   VNET_DPDK_PORT_TYPE_ETH_1G,
118   VNET_DPDK_PORT_TYPE_ETH_10G,
119   VNET_DPDK_PORT_TYPE_ETH_40G,
120   VNET_DPDK_PORT_TYPE_ETH_BOND,
121   VNET_DPDK_PORT_TYPE_ETH_SWITCH,
122   VNET_DPDK_PORT_TYPE_AF_PACKET,
123   VNET_DPDK_PORT_TYPE_UNKNOWN,
124 } dpdk_port_type_t;
125
126 typedef struct
127 {
128   f64 deadline;
129   vlib_frame_t *frame;
130 } dpdk_frame_t;
131
132 #define DPDK_EFD_MAX_DISCARD_RATE 10
133
134 typedef struct
135 {
136   u16 last_burst_sz;
137   u16 max_burst_sz;
138   u32 full_frames_cnt;
139   u32 consec_full_frames_cnt;
140   u32 congestion_cnt;
141   u64 last_poll_time;
142   u64 max_poll_delay;
143   u32 discard_cnt;
144   u32 total_packet_cnt;
145 } dpdk_efd_agent_t;
146
147 typedef void (*dpdk_flowcontrol_callback_t) (vlib_main_t * vm,
148                                              u32 hw_if_index, u32 n_packets);
149
150 /*
151  * The header for the tx_vector in dpdk_device_t.
152  * Head and tail are indexes into the tx_vector and are of type
153  * u64 so they never overflow.
154  */
155 typedef struct
156 {
157   u64 tx_head;
158   u64 tx_tail;
159 } tx_ring_hdr_t;
160
161 typedef struct
162 {
163   struct rte_ring *swq;
164
165   u64 hqos_field0_slabmask;
166   u32 hqos_field0_slabpos;
167   u32 hqos_field0_slabshr;
168   u64 hqos_field1_slabmask;
169   u32 hqos_field1_slabpos;
170   u32 hqos_field1_slabshr;
171   u64 hqos_field2_slabmask;
172   u32 hqos_field2_slabpos;
173   u32 hqos_field2_slabshr;
174   u32 hqos_tc_table[64];
175 } dpdk_device_hqos_per_worker_thread_t;
176
177 typedef struct
178 {
179   struct rte_ring **swq;
180   struct rte_mbuf **pkts_enq;
181   struct rte_mbuf **pkts_deq;
182   struct rte_sched_port *hqos;
183   u32 hqos_burst_enq;
184   u32 hqos_burst_deq;
185   u32 pkts_enq_len;
186   u32 swq_pos;
187 } dpdk_device_hqos_per_hqos_thread_t;
188
189 typedef struct
190 {
191   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
192   volatile u32 **lockp;
193
194   /* Instance ID */
195   u32 device_index;
196
197   u32 vlib_hw_if_index;
198   u32 vlib_sw_if_index;
199
200   /* next node index if we decide to steal the rx graph arc */
201   u32 per_interface_next_index;
202
203   /* dpdk rte_mbuf rx and tx vectors, VLIB_FRAME_SIZE */
204   struct rte_mbuf ***tx_vectors;        /* one per worker thread */
205   struct rte_mbuf ***rx_vectors;
206
207   /* vector of traced contexts, per device */
208   u32 *d_trace_buffers;
209
210   dpdk_pmd_t pmd:8;
211   i8 cpu_socket;
212
213   u16 flags;
214 #define DPDK_DEVICE_FLAG_ADMIN_UP       (1 << 0)
215 #define DPDK_DEVICE_FLAG_PROMISC        (1 << 1)
216 #define DPDK_DEVICE_FLAG_PMD            (1 << 2)
217
218 #define DPDK_DEVICE_FLAG_HAVE_SUBIF     (1 << 5)
219 #define DPDK_DEVICE_FLAG_HQOS           (1 << 6)
220
221   u16 nb_tx_desc;
222     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
223
224   u8 *interface_name_suffix;
225
226   /* number of sub-interfaces */
227   u16 num_subifs;
228
229   /* PMD related */
230   u16 tx_q_used;
231   u16 rx_q_used;
232   u16 nb_rx_desc;
233   u16 *cpu_socket_id_by_queue;
234   struct rte_eth_conf port_conf;
235   struct rte_eth_txconf tx_conf;
236
237   /* HQoS related */
238   dpdk_device_hqos_per_worker_thread_t *hqos_wt;
239   dpdk_device_hqos_per_hqos_thread_t *hqos_ht;
240
241   /* af_packet */
242   u8 af_packet_port_id;
243
244   struct rte_eth_link link;
245   f64 time_last_link_update;
246
247   struct rte_eth_stats stats;
248   struct rte_eth_stats last_stats;
249   struct rte_eth_stats last_cleared_stats;
250   struct rte_eth_xstat *xstats;
251   struct rte_eth_xstat *last_cleared_xstats;
252   f64 time_last_stats_update;
253   dpdk_port_type_t port_type;
254
255   dpdk_efd_agent_t efd_agent;
256 } dpdk_device_t;
257
258 #define DPDK_STATS_POLL_INTERVAL      (10.0)
259 #define DPDK_MIN_STATS_POLL_INTERVAL  (0.001)   /* 1msec */
260
261 #define DPDK_LINK_POLL_INTERVAL       (3.0)
262 #define DPDK_MIN_LINK_POLL_INTERVAL   (0.001)   /* 1msec */
263
264 typedef struct
265 {
266   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
267
268   /* total input packet counter */
269   u64 aggregate_rx_packets;
270 } dpdk_worker_t;
271
272 typedef struct
273 {
274   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
275
276   /* total input packet counter */
277   u64 aggregate_rx_packets;
278 } dpdk_hqos_thread_t;
279
280 typedef struct
281 {
282   u32 device;
283   u16 queue_id;
284 } dpdk_device_and_queue_t;
285
286 /* Early-Fast-Discard (EFD) */
287 #define DPDK_EFD_DISABLED                       0
288 #define DPDK_EFD_DISCARD_ENABLED                (1 << 0)
289 #define DPDK_EFD_MONITOR_ENABLED                (1 << 1)
290 #define DPDK_EFD_DROPALL_ENABLED                (1 << 2)
291
292 #define DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT    90
293 #define DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH  6
294
295 typedef struct dpdk_efd_t
296 {
297   u16 enabled;
298   u16 queue_hi_thresh;
299   u16 consec_full_frames_hi_thresh;
300   u16 pad;
301 } dpdk_efd_t;
302
303 #ifndef DPDK_HQOS_DBG_BYPASS
304 #define DPDK_HQOS_DBG_BYPASS 0
305 #endif
306
307 typedef struct dpdk_device_config_hqos_t
308 {
309   u32 hqos_thread;
310   u32 hqos_thread_valid;
311
312   u32 swq_size;
313   u32 burst_enq;
314   u32 burst_deq;
315
316   u32 pktfield0_slabpos;
317   u32 pktfield1_slabpos;
318   u32 pktfield2_slabpos;
319   u64 pktfield0_slabmask;
320   u64 pktfield1_slabmask;
321   u64 pktfield2_slabmask;
322   u32 tc_table[64];
323
324   struct rte_sched_port_params port;
325   struct rte_sched_subport_params *subport;
326   struct rte_sched_pipe_params *pipe;
327   uint32_t *pipe_map;
328 } dpdk_device_config_hqos_t;
329
330 int dpdk_hqos_validate_mask (u64 mask, u32 n);
331 void dpdk_device_config_hqos_pipe_profile_default (dpdk_device_config_hqos_t *
332                                                    hqos, u32 pipe_profile_id);
333 void dpdk_device_config_hqos_default (dpdk_device_config_hqos_t * hqos);
334 clib_error_t *dpdk_port_setup_hqos (dpdk_device_t * xd,
335                                     dpdk_device_config_hqos_t * hqos);
336 void dpdk_hqos_metadata_set (dpdk_device_hqos_per_worker_thread_t * hqos,
337                              struct rte_mbuf **pkts, u32 n_pkts);
338
339 #define foreach_dpdk_device_config_item \
340   _ (num_rx_queues) \
341   _ (num_tx_queues) \
342   _ (num_rx_desc) \
343   _ (num_tx_desc) \
344   _ (rss_fn)
345
346 typedef struct
347 {
348   vlib_pci_addr_t pci_addr;
349   u8 is_blacklisted;
350   u8 vlan_strip_offload;
351 #define DPDK_DEVICE_VLAN_STRIP_DEFAULT 0
352 #define DPDK_DEVICE_VLAN_STRIP_OFF 1
353 #define DPDK_DEVICE_VLAN_STRIP_ON  2
354
355 #define _(x) uword x;
356     foreach_dpdk_device_config_item
357 #undef _
358     clib_bitmap_t * workers;
359   u32 hqos_enabled;
360   dpdk_device_config_hqos_t hqos;
361 } dpdk_device_config_t;
362
363 typedef struct
364 {
365
366   /* Config stuff */
367   u8 **eal_init_args;
368   u8 *eal_init_args_str;
369   u8 *uio_driver_name;
370   u8 no_multi_seg;
371   u8 enable_tcp_udp_checksum;
372
373   /* Required config parameters */
374   u8 coremask_set_manually;
375   u8 nchannels_set_manually;
376   u32 coremask;
377   u32 nchannels;
378   u32 num_mbufs;
379   u8 num_kni;                   /* while kni_init allows u32, port_id in callback fn is only u8 */
380
381   /*
382    * format interface names ala xxxEthernet%d/%d/%d instead of
383    * xxxEthernet%x/%x/%x.
384    */
385   u8 interface_name_format_decimal;
386
387   /* per-device config */
388   dpdk_device_config_t default_devconf;
389   dpdk_device_config_t *dev_confs;
390   uword *device_config_index_by_pci_addr;
391
392 } dpdk_config_main_t;
393
394 dpdk_config_main_t dpdk_config_main;
395
396 typedef struct
397 {
398
399   /* Devices */
400   dpdk_device_t *devices;
401   dpdk_device_and_queue_t **devices_by_cpu;
402   dpdk_device_and_queue_t **devices_by_hqos_cpu;
403
404   /* per-thread recycle lists */
405   u32 **recycle;
406
407   /* buffer flags template, configurable to enable/disable tcp / udp cksum */
408   u32 buffer_flags_template;
409
410   /* flow control callback. If 0 then flow control is disabled */
411   dpdk_flowcontrol_callback_t flowcontrol_callback;
412
413   /* vlib buffer free list, must be same size as an rte_mbuf */
414   u32 vlib_buffer_free_list_index;
415
416   /* dpdk worker "threads" */
417   dpdk_worker_t *workers;
418
419   /* dpdk HQoS "threads" */
420   dpdk_hqos_thread_t *hqos_threads;
421
422   /* Ethernet input node index */
423   u32 ethernet_input_node_index;
424
425   /* pcap tracing [only works if (CLIB_DEBUG > 0)] */
426   int tx_pcap_enable;
427   pcap_main_t pcap_main;
428   u8 *pcap_filename;
429   u32 pcap_sw_if_index;
430   u32 pcap_pkts_to_capture;
431
432   /* hashes */
433   uword *dpdk_device_by_kni_port_id;
434   uword *vu_sw_if_index_by_listener_fd;
435   uword *vu_sw_if_index_by_sock_fd;
436   u32 *vu_inactive_interfaces_device_index;
437
438   /* efd (early-fast-discard) settings */
439   dpdk_efd_t efd;
440
441   /*
442    * flag indicating that a posted admin up/down
443    * (via post_sw_interface_set_flags) is in progress
444    */
445   u8 admin_up_down_in_progress;
446
447   u8 use_rss;
448
449   /* which cpus are running dpdk-input */
450   int input_cpu_first_index;
451   int input_cpu_count;
452
453   /* which cpus are running I/O TX */
454   int hqos_cpu_first_index;
455   int hqos_cpu_count;
456
457   /* control interval of dpdk link state and stat polling */
458   f64 link_state_poll_interval;
459   f64 stat_poll_interval;
460
461   /* Sleep for this many MS after each device poll */
462   u32 poll_sleep;
463
464   /* convenience */
465   vlib_main_t *vlib_main;
466   vnet_main_t *vnet_main;
467   dpdk_config_main_t *conf;
468 } dpdk_main_t;
469
470 dpdk_main_t dpdk_main;
471
472 typedef enum
473 {
474   DPDK_RX_NEXT_IP4_INPUT,
475   DPDK_RX_NEXT_IP6_INPUT,
476   DPDK_RX_NEXT_MPLS_INPUT,
477   DPDK_RX_NEXT_ETHERNET_INPUT,
478   DPDK_RX_NEXT_DROP,
479   DPDK_RX_N_NEXT,
480 } dpdk_rx_next_t;
481
482 typedef struct
483 {
484   u32 buffer_index;
485   u16 device_index;
486   u8 queue_index;
487   struct rte_mbuf mb;
488   /* Copy of VLIB buffer; packet data stored in pre_data. */
489   vlib_buffer_t buffer;
490 } dpdk_tx_dma_trace_t;
491
492 typedef struct
493 {
494   u32 buffer_index;
495   u16 device_index;
496   u16 queue_index;
497   struct rte_mbuf mb;
498   vlib_buffer_t buffer;         /* Copy of VLIB buffer; pkt data stored in pre_data. */
499   u8 data[256];                 /* First 256 data bytes, used for hexdump */
500 } dpdk_rx_dma_trace_t;
501
502 void vnet_buffer_needs_dpdk_mb (vlib_buffer_t * b);
503
504 void dpdk_set_next_node (dpdk_rx_next_t, char *);
505
506 clib_error_t *dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address);
507
508 clib_error_t *dpdk_set_mc_filter (vnet_hw_interface_t * hi,
509                                   struct ether_addr mc_addr_vec[], int naddr);
510
511 void dpdk_thread_input (dpdk_main_t * dm, dpdk_device_t * xd);
512
513 clib_error_t *dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd);
514
515 void dpdk_set_flowcontrol_callback (vlib_main_t * vm,
516                                     dpdk_flowcontrol_callback_t callback);
517
518 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance);
519
520 void set_efd_bitmap (u8 * bitmap, u32 value, u32 op);
521
522 struct rte_mbuf *dpdk_replicate_packet_mb (vlib_buffer_t * b);
523 struct rte_mbuf *dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b);
524
525 #define foreach_dpdk_error                                              \
526   _(NONE, "no error")                                                   \
527   _(RX_PACKET_ERROR, "Rx packet errors")                                \
528   _(RX_BAD_FCS, "Rx bad fcs")                                           \
529   _(L4_CHECKSUM_ERROR, "Rx L4 checksum errors")                         \
530   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
531   _(RX_ALLOC_FAIL, "rx buf alloc from free list failed")                \
532   _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem")              \
533   _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error")        \
534   _(IPV4_EFD_DROP_PKTS, "IPV4 Early Fast Discard rx drops")             \
535   _(IPV6_EFD_DROP_PKTS, "IPV6 Early Fast Discard rx drops")             \
536   _(MPLS_EFD_DROP_PKTS, "MPLS Early Fast Discard rx drops")             \
537   _(VLAN_EFD_DROP_PKTS, "VLAN Early Fast Discard rx drops")
538
539 typedef enum
540 {
541 #define _(f,s) DPDK_ERROR_##f,
542   foreach_dpdk_error
543 #undef _
544     DPDK_N_ERROR,
545 } dpdk_error_t;
546
547 /*
548  * Increment EFD drop counter
549  */
550 static_always_inline void
551 increment_efd_drop_counter (vlib_main_t * vm, u32 counter_index, u32 count)
552 {
553   vlib_node_t *my_n;
554
555   my_n = vlib_get_node (vm, dpdk_input_node.index);
556   vm->error_main.counters[my_n->error_heap_index + counter_index] += count;
557 }
558
559 int dpdk_set_stat_poll_interval (f64 interval);
560 int dpdk_set_link_state_poll_interval (f64 interval);
561 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
562 void dpdk_device_lock_init (dpdk_device_t * xd);
563 void dpdk_device_lock_free (dpdk_device_t * xd);
564 void dpdk_efd_update_counters (dpdk_device_t * xd, u32 n_buffers,
565                                u16 enabled);
566 u32 is_efd_discardable (vlib_thread_main_t * tm, vlib_buffer_t * b0,
567                         struct rte_mbuf *mb);
568
569 static inline u64
570 vnet_get_aggregate_rx_packets (void)
571 {
572   dpdk_main_t *dm = &dpdk_main;
573   u64 sum = 0;
574   dpdk_worker_t *dw;
575
576   vec_foreach (dw, dm->workers) sum += dw->aggregate_rx_packets;
577
578   return sum;
579 }
580
581 void dpdk_rx_trace (dpdk_main_t * dm,
582                     vlib_node_runtime_t * node,
583                     dpdk_device_t * xd,
584                     u16 queue_id, u32 * buffers, uword n_buffers);
585
586 #define EFD_OPERATION_LESS_THAN          0
587 #define EFD_OPERATION_GREATER_OR_EQUAL   1
588
589 void efd_config (u32 enabled,
590                  u32 ip_prec, u32 ip_op,
591                  u32 mpls_exp, u32 mpls_op, u32 vlan_cos, u32 vlan_op);
592
593 void post_sw_interface_set_flags (vlib_main_t * vm, u32 sw_if_index,
594                                   u32 flags);
595
596 u32 dpdk_get_admin_up_down_in_progress (void);
597
598 u32 dpdk_num_mbufs (void);
599
600 dpdk_pmd_t dpdk_get_pmd_type (vnet_hw_interface_t * hi);
601
602 i8 dpdk_get_cpu_socket (vnet_hw_interface_t * hi);
603
604 void *dpdk_input_multiarch_select ();
605 void *dpdk_input_rss_multiarch_select ();
606 void *dpdk_input_efd_multiarch_select ();
607
608 clib_error_t *dpdk_get_hw_interface_stats (u32 hw_if_index,
609                                            struct rte_eth_stats *dest);
610
611 format_function_t format_dpdk_device_name;
612 format_function_t format_dpdk_device;
613 format_function_t format_dpdk_tx_dma_trace;
614 format_function_t format_dpdk_rx_dma_trace;
615 format_function_t format_dpdk_rte_mbuf;
616 format_function_t format_dpdk_rx_rte_mbuf;
617 unformat_function_t unformat_socket_mem;
618 clib_error_t *unformat_rss_fn (unformat_input_t * input, uword * rss_fn);
619 clib_error_t *unformat_hqos (unformat_input_t * input,
620                              dpdk_device_config_hqos_t * hqos);
621
622 uword
623 admin_up_down_process (vlib_main_t * vm,
624                        vlib_node_runtime_t * rt, vlib_frame_t * f);
625
626 #endif /* __included_dpdk_h__ */
627
628 /*
629  * fd.io coding-style-patch-verification: ON
630  *
631  * Local Variables:
632  * eval: (c-set-style "gnu")
633  * End:
634  */