Initial commit of vpp code.
[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_memcpy.h>
28 #include <rte_memzone.h>
29 #include <rte_tailq.h>
30 #include <rte_eal.h>
31 #include <rte_per_lcore.h>
32 #include <rte_launch.h>
33 #include <rte_atomic.h>
34 #include <rte_cycles.h>
35 #include <rte_prefetch.h>
36 #include <rte_lcore.h>
37 #include <rte_per_lcore.h>
38 #include <rte_branch_prediction.h>
39 #include <rte_interrupts.h>
40 #include <rte_pci.h>
41 #include <rte_random.h>
42 #include <rte_debug.h>
43 #include <rte_ether.h>
44 #include <rte_ethdev.h>
45 #include <rte_ring.h>
46 #include <rte_mempool.h>
47 #include <rte_mbuf.h>
48 #include <rte_kni.h>
49 #include <rte_virtio_net.h>
50 #include <rte_pci_dev_ids.h>
51 #include <rte_version.h>
52
53 #include <vnet/unix/pcap.h>
54 #include <vnet/devices/virtio/vhost-user.h>
55
56 #if CLIB_DEBUG > 0
57 #define always_inline static inline
58 #else
59 #define always_inline static inline __attribute__ ((__always_inline__))
60 #endif
61
62 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
63 #define NB_MBUF   (32<<10)
64
65 vnet_device_class_t dpdk_device_class;
66 vlib_node_registration_t dpdk_input_node;
67 vlib_node_registration_t dpdk_io_input_node;
68 vlib_node_registration_t handoff_dispatch_node;
69
70 typedef enum {
71   VNET_DPDK_DEV_ETH = 1,      /* Standard DPDK PMD driver */
72   VNET_DPDK_DEV_KNI,          /* Kernel NIC Interface */
73   VNET_DPDK_DEV_VHOST_USER,
74   VNET_DPDK_DEV_UNKNOWN,      /* must be last */
75 } dpdk_device_type_t;
76
77 #define foreach_dpdk_pmd          \
78   _ ("rte_em_pmd", E1000EM)       \
79   _ ("rte_igb_pmd", IGB)          \
80   _ ("rte_igbvf_pmd", IGBVF)      \
81   _ ("rte_ixgbe_pmd", IXGBE)      \
82   _ ("rte_ixgbevf_pmd", IXGBEVF)  \
83   _ ("rte_i40e_pmd", I40E)        \
84   _ ("rte_i40evf_pmd", I40EVF)    \
85   _ ("rte_virtio_pmd", VIRTIO)    \
86   _ ("rte_vice_pmd", VICE)        \
87   _ ("rte_enic_pmd", ENIC)        \
88   _ ("rte_vmxnet3_pmd", VMXNET3)  \
89   _ ("AF_PACKET PMD", AF_PACKET)  \
90   _ ("rte_pmd_fm10k", FM10K)
91
92 typedef enum {
93   VNET_DPDK_PMD_NONE,
94 #define _(s,f) VNET_DPDK_PMD_##f,
95   foreach_dpdk_pmd
96 #undef _
97 #ifdef NETMAP
98   VNET_DPDK_PMD_NETMAP,
99 #endif
100   VNET_DPDK_PMD_UNKNOWN, /* must be last */
101 } dpdk_pmd_t;
102
103 typedef enum {
104   VNET_DPDK_PORT_TYPE_ETH_1G,
105   VNET_DPDK_PORT_TYPE_ETH_10G,
106   VNET_DPDK_PORT_TYPE_ETH_40G,
107   VNET_DPDK_PORT_TYPE_ETH_SWITCH,
108 #ifdef NETMAP
109   VNET_DPDK_PORT_TYPE_NETMAP,
110 #endif
111   VNET_DPDK_PORT_TYPE_AF_PACKET,
112   VNET_DPDK_PORT_TYPE_UNKNOWN,
113 } dpdk_port_type_t;
114
115 typedef struct {
116   f64 deadline;
117   vlib_frame_t * frame;
118 } dpdk_frame_t;
119
120 #define DPDK_EFD_MAX_DISCARD_RATE 10
121
122 typedef struct {
123   u16 last_burst_sz;
124   u16 max_burst_sz;
125   u32 full_frames_cnt;
126   u32 consec_full_frames_cnt;
127   u32 congestion_cnt;
128   u64 last_poll_time;
129   u64 max_poll_delay;
130   u32 discard_cnt;
131   u32 total_packet_cnt;
132 } dpdk_efd_agent_t;
133
134 typedef struct {
135   int callfd;
136   int kickfd;
137   int errfd;
138   u32 callfd_idx;
139   u32 n_since_last_int;
140   f64 int_deadline;
141 } dpdk_vu_vring;
142
143 typedef struct {
144   u32 is_up;
145   u32 unix_fd;
146   u32 unix_file_index;
147   u32 client_fd;
148   char sock_filename[256];
149   int sock_errno;
150   u8 sock_is_server;
151   u8 active;
152
153   u64 feature_mask;
154   u32 num_vrings;
155   dpdk_vu_vring vrings[2];
156   u64 region_addr[VHOST_MEMORY_MAX_NREGIONS];
157   u32 region_fd[VHOST_MEMORY_MAX_NREGIONS];
158 } dpdk_vu_intf_t;
159
160 typedef void (*dpdk_flowcontrol_callback_t) (vlib_main_t *vm,
161                                              u32 hw_if_index,
162                                              u32 n_packets);
163
164 /*
165  * The header for the tx_vector in dpdk_device_t.
166  * Head and tail are indexes into the tx_vector and are of type
167  * u64 so they never overflow.
168  */
169 typedef struct {
170   u64 tx_head;
171   u64 tx_tail;
172 } tx_ring_hdr_t;
173
174 typedef struct {
175   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
176   volatile u32 *lockp;
177
178   /* Instance ID */
179   u32 device_index;
180
181   u32 vlib_hw_if_index;
182   u32 vlib_sw_if_index;
183
184   /* next node index if we decide to steal the rx graph arc */
185   u32 per_interface_next_index;
186
187   /* dpdk rte_mbuf rx and tx vectors, VLIB_FRAME_SIZE */
188   struct rte_mbuf *** tx_vectors; /* one per worker thread */
189   struct rte_mbuf *** rx_vectors;
190
191   /* vector of traced contexts, per device */
192   u32 * d_trace_buffers;
193
194   /* per-worker destination frame queue */
195   dpdk_frame_t * frames;
196
197   dpdk_device_type_t dev_type:8;
198   dpdk_pmd_t pmd:8;
199   i8 cpu_socket;
200
201   u8 admin_up;
202   u8 promisc;
203
204   CLIB_CACHE_LINE_ALIGN_MARK(cacheline1);
205
206   /* PMD related */
207   u16 tx_q_used;
208   u16 rx_q_used;
209   u16 nb_rx_desc;
210   u16 nb_tx_desc;
211   u16 * cpu_socket_id_by_queue;
212   struct rte_eth_conf port_conf;
213   struct rte_eth_txconf tx_conf;
214
215   /* KNI related */
216   struct rte_kni *kni;
217   u8 kni_port_id;
218
219   /* vhost-user related */
220   u32 vu_if_id;
221   struct virtio_net  vu_vhost_dev;
222   u32 vu_is_running;
223   dpdk_vu_intf_t *vu_intf;
224
225   /* af_packet */
226   u8 af_packet_port_id;
227
228   struct rte_eth_link link;
229   f64 time_last_link_update;
230
231   struct rte_eth_stats stats;
232   struct rte_eth_stats last_stats;
233   struct rte_eth_xstats * xstats;
234   f64 time_last_stats_update;
235   dpdk_port_type_t port_type;
236
237   dpdk_efd_agent_t efd_agent;
238 } dpdk_device_t;
239
240 #define MAX_NELTS 32
241 typedef struct {
242   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
243   u64 head;
244   u64 head_hint;
245   u64 tail;
246   u32 n_in_use;
247   u32 nelts;
248   u32 written;
249   u32 threshold;
250   i32 n_vectors[MAX_NELTS];
251 } frame_queue_trace_t;
252
253 #define DPDK_TX_RING_SIZE (4 * 1024)
254
255 #define DPDK_STATS_POLL_INTERVAL  10.0
256 #define DPDK_LINK_POLL_INTERVAL   3.0
257
258 typedef struct {
259   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
260
261   /* total input packet counter */
262   u64 aggregate_rx_packets;
263 } dpdk_worker_t;
264
265 typedef struct {
266   u32 device;
267   u16 queue_id;
268 } dpdk_device_and_queue_t;
269
270 /* Early-Fast-Discard (EFD) */
271 #define DPDK_EFD_DISABLED                       0
272 #define DPDK_EFD_DISCARD_ENABLED                (1 << 0)
273 #define DPDK_EFD_MONITOR_ENABLED                (1 << 1)
274 #define DPDK_EFD_DROPALL_ENABLED                (1 << 2)
275
276 #define DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT    90
277 #define DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH  6
278
279 typedef struct dpdk_efd_t {
280   u16 enabled;
281   u16 queue_hi_thresh;
282   u16 consec_full_frames_hi_thresh;
283   u16 pad;
284 } dpdk_efd_t;
285
286 typedef struct {
287
288   /* Devices */
289   dpdk_device_t * devices;
290   dpdk_device_and_queue_t ** devices_by_cpu;
291
292   /* per-thread recycle lists */
293   u32 ** recycle;
294
295   /* flow control callback. If 0 then flow control is disabled */
296   dpdk_flowcontrol_callback_t flowcontrol_callback;
297
298   /* vlib buffer free list, must be same size as an rte_mbuf */
299   u32 vlib_buffer_free_list_index;
300
301   /*
302    * format interface names ala xxxEthernet%d/%d/%d instead of
303    * xxxEthernet%x/%x/%x. For VIRL.
304    */
305   u8 interface_name_format_decimal;
306
307
308   /* dpdk worker "threads" */
309   dpdk_worker_t * workers;
310
311   /* Config stuff */
312   u8 ** eal_init_args;
313   u8 * eth_if_blacklist;
314   u8 * eth_if_whitelist;
315   u8 * uio_driver_name;
316   u8 no_multi_seg;
317
318   /* Required config parameters */
319   u8 coremask_set_manually;
320   u8 nchannels_set_manually;
321   u32 coremask;
322   u32 nchannels;
323   u32 num_mbufs;
324   u32 use_rss;
325   u8 num_kni; /* while kni_init allows u32, port_id in callback fn is only u8 */
326
327   /* Ethernet input node index */
328   u32 ethernet_input_node_index;
329
330   /* dpdk i/o thread initialization barrier */
331   volatile u32 io_thread_release;
332
333   /* pcap tracing [only works if (CLIB_DEBUG > 0)] */
334   int tx_pcap_enable;
335   pcap_main_t pcap_main;
336   u8 * pcap_filename;
337   u32 pcap_sw_if_index;
338   u32 pcap_pkts_to_capture;
339
340   /* virtio vhost-user switch */
341   u8 use_virtio_vhost;
342
343   /* vhost-user coalescence frames config */
344   u32 vhost_coalesce_frames;
345   f64 vhost_coalesce_time;
346
347   /* hashes */
348   uword * dpdk_device_by_kni_port_id;
349   uword * vu_sw_if_index_by_listener_fd;
350   uword * vu_sw_if_index_by_sock_fd;
351   u32 * vu_inactive_interfaces_device_index;
352
353   u32 next_vu_if_id;
354
355   /* efd (early-fast-discard) settings */
356   dpdk_efd_t efd;
357
358   /*
359    * flag indicating that a posted admin up/down
360    * (via post_sw_interface_set_flags) is in progress
361    */
362   u8 admin_up_down_in_progress;
363
364   u8 have_io_threads;
365
366   /* which cpus are running dpdk-input */
367   int input_cpu_first_index;
368   int input_cpu_count;
369
370   /* convenience */
371   vlib_main_t * vlib_main;
372   vnet_main_t * vnet_main;
373 } dpdk_main_t;
374
375 dpdk_main_t dpdk_main;
376
377 typedef enum {
378   DPDK_RX_NEXT_IP4_INPUT,
379   DPDK_RX_NEXT_IP6_INPUT,
380   DPDK_RX_NEXT_MPLS_INPUT,
381   DPDK_RX_NEXT_ETHERNET_INPUT,
382   DPDK_RX_NEXT_DROP,
383   DPDK_RX_N_NEXT,
384 } dpdk_rx_next_t;
385
386 void vnet_buffer_needs_dpdk_mb (vlib_buffer_t * b);
387
388 void dpdk_set_next_node (dpdk_rx_next_t, char *);
389
390 typedef void (*dpdk_io_thread_callback_t) (vlib_main_t *vm);
391
392 void dpdk_io_thread (vlib_worker_thread_t * w,
393                      u32 instances,
394                      u32 instance_id,
395                      char *worker_name,
396                      dpdk_io_thread_callback_t callback);
397 void dpdk_thread_input (dpdk_main_t * dm, dpdk_device_t * xd);
398
399 clib_error_t * dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd);
400
401 void dpdk_set_flowcontrol_callback (vlib_main_t *vm, 
402                                     dpdk_flowcontrol_callback_t callback);
403
404 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance);
405
406 vlib_frame_queue_elt_t * vlib_get_handoff_queue_elt (u32 vlib_worker_index);
407
408 u32 dpdk_get_handoff_node_index (void);
409
410 void set_efd_bitmap (u8 *bitmap, u32 value, u32 op);
411
412 #define foreach_dpdk_error                                              \
413   _(NONE, "no error")                                                   \
414   _(RX_PACKET_ERROR, "Rx packet errors")                                \
415   _(RX_BAD_FCS, "Rx bad fcs")                                           \
416   _(L4_CHECKSUM_ERROR, "Rx L4 checksum errors")                         \
417   _(IP_CHECKSUM_ERROR, "Rx ip checksum errors")                         \
418   _(RX_ALLOC_FAIL, "rx buf alloc from free list failed")                \
419   _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem")              \
420   _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error")        \
421   _(IPV4_EFD_DROP_PKTS, "IPV4 Early Fast Discard rx drops")             \
422   _(IPV6_EFD_DROP_PKTS, "IPV6 Early Fast Discard rx drops")             \
423   _(MPLS_EFD_DROP_PKTS, "MPLS Early Fast Discard rx drops")             \
424   _(VLAN_EFD_DROP_PKTS, "VLAN Early Fast Discard rx drops")
425
426 typedef enum {
427 #define _(f,s) DPDK_ERROR_##f,
428   foreach_dpdk_error
429 #undef _
430   DPDK_N_ERROR,
431 } dpdk_error_t;
432
433 /*
434  * Increment EFD drop counter
435  */
436 static_always_inline
437 void increment_efd_drop_counter (vlib_main_t * vm, u32 counter_index, u32 count)
438 {
439    vlib_node_t *my_n;
440
441    my_n = vlib_get_node (vm, dpdk_input_node.index);
442    vm->error_main.counters[my_n->error_heap_index+counter_index] += count;
443 }
444
445 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
446 void dpdk_efd_update_counters(dpdk_device_t *xd, u32 n_buffers, u16 enabled);
447 u32 is_efd_discardable(vlib_thread_main_t *tm,
448                        vlib_buffer_t * b0,
449                        struct rte_mbuf *mb);
450
451 /* dpdk vhost-user interrupt management */
452 u8 dpdk_vhost_user_want_interrupt (dpdk_device_t *xd, int idx);
453 void dpdk_vhost_user_send_interrupt (vlib_main_t * vm, dpdk_device_t * xd,
454                                     int idx);
455
456
457 static inline u64 vnet_get_aggregate_rx_packets (void)
458 {
459     dpdk_main_t * dm = &dpdk_main;
460     u64 sum = 0;
461     dpdk_worker_t * dw;
462
463     vec_foreach(dw, dm->workers)
464         sum += dw->aggregate_rx_packets;
465
466     return sum;
467 }
468
469 void dpdk_rx_trace (dpdk_main_t * dm,
470                     vlib_node_runtime_t * node,
471                     dpdk_device_t * xd,
472                     u16 queue_id,
473                     u32 * buffers,
474                     uword n_buffers);
475
476 #define EFD_OPERATION_LESS_THAN          0
477 #define EFD_OPERATION_GREATER_OR_EQUAL   1
478
479 void efd_config(u32 enabled,
480                 u32 ip_prec,  u32 ip_op,
481                 u32 mpls_exp, u32 mpls_op,
482                 u32 vlan_cos, u32 vlan_op);
483
484 void post_sw_interface_set_flags (vlib_main_t *vm, u32 sw_if_index, u32 flags);
485
486 typedef struct vhost_user_memory vhost_user_memory_t;
487
488 void dpdk_vhost_user_process_init (void **ctx);
489 void dpdk_vhost_user_process_cleanup (void *ctx);
490 uword dpdk_vhost_user_process_if (vlib_main_t *vm, dpdk_device_t *xd, void *ctx);
491
492 // vhost-user calls
493 int dpdk_vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
494                               const char * sock_filename,
495                               u8 is_server,
496                               u32 * sw_if_index,
497                               u64 feature_mask,
498                               u8 renumber, u32 custom_dev_instance);
499 int dpdk_vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
500                               const char * sock_filename,
501                               u8 is_server,
502                               u32 sw_if_index,
503                               u64 feature_mask,
504                               u8 renumber, u32 custom_dev_instance);
505 int dpdk_vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
506                               u32 sw_if_index);
507 int dpdk_vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
508                              vhost_user_intf_details_t **out_vuids);
509
510 u32 dpdk_get_admin_up_down_in_progress (void);
511
512 uword
513 dpdk_input_rss (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f);
514
515 #endif /* __included_dpdk_h__ */