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