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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef __included_dpdk_h__
16 #define __included_dpdk_h__
18 /* $$$$ We should rename always_inline -> clib_always_inline */
21 #include <rte_config.h>
23 #include <rte_common.h>
26 #include <rte_memory.h>
27 #include <rte_memzone.h>
28 #include <rte_tailq.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>
40 #include <rte_random.h>
41 #include <rte_debug.h>
42 #include <rte_ether.h>
43 #include <rte_ethdev.h>
45 #include <rte_mempool.h>
47 #include <rte_virtio_net.h>
48 #include <rte_version.h>
49 #include <rte_eth_bond.h>
50 #include <rte_sched.h>
52 #include <vnet/unix/pcap.h>
55 #define always_inline static inline
57 #define always_inline static inline __attribute__ ((__always_inline__))
60 #include <vlib/pci/pci.h>
62 #define NB_MBUF (16<<10)
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;
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)
89 #define _(s,f) VNET_DPDK_PMD_##f,
92 VNET_DPDK_PMD_UNKNOWN, /* must be last */
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,
112 #define DPDK_EFD_MAX_DISCARD_RATE 10
119 u32 consec_full_frames_cnt;
124 u32 total_packet_cnt;
127 typedef void (*dpdk_flowcontrol_callback_t) (vlib_main_t * vm,
128 u32 hw_if_index, u32 n_packets);
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.
143 struct rte_ring *swq;
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;
159 struct rte_ring **swq;
160 struct rte_mbuf **pkts_enq;
161 struct rte_mbuf **pkts_deq;
162 struct rte_sched_port *hqos;
167 } dpdk_device_hqos_per_hqos_thread_t;
171 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
172 volatile u32 **lockp;
177 u32 vlib_hw_if_index;
178 u32 vlib_sw_if_index;
180 /* next node index if we decide to steal the rx graph arc */
181 u32 per_interface_next_index;
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;
187 /* vector of traced contexts, per device */
188 u32 *d_trace_buffers;
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)
198 #define DPDK_DEVICE_FLAG_HAVE_SUBIF (1 << 5)
199 #define DPDK_DEVICE_FLAG_HQOS (1 << 6)
202 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
204 u8 *interface_name_suffix;
206 /* number of sub-interfaces */
213 u16 *cpu_socket_id_by_queue;
214 struct rte_eth_conf port_conf;
215 struct rte_eth_txconf tx_conf;
218 dpdk_device_hqos_per_worker_thread_t *hqos_wt;
219 dpdk_device_hqos_per_hqos_thread_t *hqos_ht;
222 u8 af_packet_port_id;
224 struct rte_eth_link link;
225 f64 time_last_link_update;
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;
235 dpdk_efd_agent_t efd_agent;
238 #define DPDK_STATS_POLL_INTERVAL (10.0)
239 #define DPDK_MIN_STATS_POLL_INTERVAL (0.001) /* 1msec */
241 #define DPDK_LINK_POLL_INTERVAL (3.0)
242 #define DPDK_MIN_LINK_POLL_INTERVAL (0.001) /* 1msec */
246 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
248 /* total input packet counter */
249 u64 aggregate_rx_packets;
254 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
256 /* total input packet counter */
257 u64 aggregate_rx_packets;
258 } dpdk_hqos_thread_t;
264 } dpdk_device_and_queue_t;
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)
272 #define DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT 90
273 #define DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH 6
275 typedef struct dpdk_efd_t
279 u16 consec_full_frames_hi_thresh;
283 #ifndef DPDK_HQOS_DBG_BYPASS
284 #define DPDK_HQOS_DBG_BYPASS 0
287 typedef struct dpdk_device_config_hqos_t
290 u32 hqos_thread_valid;
296 u32 pktfield0_slabpos;
297 u32 pktfield1_slabpos;
298 u32 pktfield2_slabpos;
299 u64 pktfield0_slabmask;
300 u64 pktfield1_slabmask;
301 u64 pktfield2_slabmask;
304 struct rte_sched_port_params port;
305 struct rte_sched_subport_params *subport;
306 struct rte_sched_pipe_params *pipe;
308 } dpdk_device_config_hqos_t;
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);
319 #define foreach_dpdk_device_config_item \
328 vlib_pci_addr_t pci_addr;
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
335 #define _(x) uword x;
336 foreach_dpdk_device_config_item
338 clib_bitmap_t * workers;
340 dpdk_device_config_hqos_t hqos;
341 } dpdk_device_config_t;
348 u8 *eal_init_args_str;
351 u8 enable_tcp_udp_checksum;
353 /* Required config parameters */
354 u8 coremask_set_manually;
355 u8 nchannels_set_manually;
359 u8 num_kni; /* while kni_init allows u32, port_id in callback fn is only u8 */
362 * format interface names ala xxxEthernet%d/%d/%d instead of
363 * xxxEthernet%x/%x/%x.
365 u8 interface_name_format_decimal;
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;
372 } dpdk_config_main_t;
374 dpdk_config_main_t dpdk_config_main;
380 dpdk_device_t *devices;
381 dpdk_device_and_queue_t **devices_by_cpu;
382 dpdk_device_and_queue_t **devices_by_hqos_cpu;
384 /* per-thread recycle lists */
387 /* buffer flags template, configurable to enable/disable tcp / udp cksum */
388 u32 buffer_flags_template;
390 /* flow control callback. If 0 then flow control is disabled */
391 dpdk_flowcontrol_callback_t flowcontrol_callback;
393 /* vlib buffer free list, must be same size as an rte_mbuf */
394 u32 vlib_buffer_free_list_index;
396 /* dpdk worker "threads" */
397 dpdk_worker_t *workers;
399 /* dpdk HQoS "threads" */
400 dpdk_hqos_thread_t *hqos_threads;
402 /* Ethernet input node index */
403 u32 ethernet_input_node_index;
405 /* pcap tracing [only works if (CLIB_DEBUG > 0)] */
407 pcap_main_t pcap_main;
409 u32 pcap_sw_if_index;
410 u32 pcap_pkts_to_capture;
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;
418 /* efd (early-fast-discard) settings */
422 * flag indicating that a posted admin up/down
423 * (via post_sw_interface_set_flags) is in progress
425 u8 admin_up_down_in_progress;
429 /* which cpus are running dpdk-input */
430 int input_cpu_first_index;
433 /* which cpus are running I/O TX */
434 int hqos_cpu_first_index;
437 /* control interval of dpdk link state and stat polling */
438 f64 link_state_poll_interval;
439 f64 stat_poll_interval;
441 /* Sleep for this many MS after each device poll */
445 vlib_main_t *vlib_main;
446 vnet_main_t *vnet_main;
447 dpdk_config_main_t *conf;
450 dpdk_main_t dpdk_main;
454 DPDK_RX_NEXT_IP4_INPUT,
455 DPDK_RX_NEXT_IP6_INPUT,
456 DPDK_RX_NEXT_MPLS_INPUT,
457 DPDK_RX_NEXT_ETHERNET_INPUT,
468 /* Copy of VLIB buffer; packet data stored in pre_data. */
469 vlib_buffer_t buffer;
470 } dpdk_tx_dma_trace_t;
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;
482 void vnet_buffer_needs_dpdk_mb (vlib_buffer_t * b);
484 void dpdk_set_next_node (dpdk_rx_next_t, char *);
486 clib_error_t *dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address);
488 clib_error_t *dpdk_set_mc_filter (vnet_hw_interface_t * hi,
489 struct ether_addr mc_addr_vec[], int naddr);
491 void dpdk_thread_input (dpdk_main_t * dm, dpdk_device_t * xd);
493 clib_error_t *dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd);
495 void dpdk_set_flowcontrol_callback (vlib_main_t * vm,
496 dpdk_flowcontrol_callback_t callback);
498 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance);
500 void set_efd_bitmap (u8 * bitmap, u32 value, u32 op);
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);
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")
521 #define _(f,s) DPDK_ERROR_##f,
528 * Increment EFD drop counter
530 static_always_inline void
531 increment_efd_drop_counter (vlib_main_t * vm, u32 counter_index, u32 count)
535 my_n = vlib_get_node (vm, dpdk_input_node.index);
536 vm->error_main.counters[my_n->error_heap_index + counter_index] += count;
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,
546 u32 is_efd_discardable (vlib_thread_main_t * tm, vlib_buffer_t * b0,
547 struct rte_mbuf *mb);
550 vnet_get_aggregate_rx_packets (void)
552 dpdk_main_t *dm = &dpdk_main;
556 vec_foreach (dw, dm->workers) sum += dw->aggregate_rx_packets;
561 void dpdk_rx_trace (dpdk_main_t * dm,
562 vlib_node_runtime_t * node,
564 u16 queue_id, u32 * buffers, uword n_buffers);
566 #define EFD_OPERATION_LESS_THAN 0
567 #define EFD_OPERATION_GREATER_OR_EQUAL 1
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);
573 void post_sw_interface_set_flags (vlib_main_t * vm, u32 sw_if_index,
576 u32 dpdk_get_admin_up_down_in_progress (void);
578 u32 dpdk_num_mbufs (void);
580 dpdk_pmd_t dpdk_get_pmd_type (vnet_hw_interface_t * hi);
582 i8 dpdk_get_cpu_socket (vnet_hw_interface_t * hi);
584 void *dpdk_input_multiarch_select ();
585 void *dpdk_input_rss_multiarch_select ();
586 void *dpdk_input_efd_multiarch_select ();
588 clib_error_t *dpdk_get_hw_interface_stats (u32 hw_if_index,
589 struct rte_eth_stats *dest);
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);
603 admin_up_down_process (vlib_main_t * vm,
604 vlib_node_runtime_t * rt, vlib_frame_t * f);
606 #endif /* __included_dpdk_h__ */
609 * fd.io coding-style-patch-verification: ON
612 * eval: (c-set-style "gnu")