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 #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)
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)
109 #define _(s,f) VNET_DPDK_PMD_##f,
112 VNET_DPDK_PMD_UNKNOWN, /* must be last */
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,
132 #define DPDK_EFD_MAX_DISCARD_RATE 10
139 u32 consec_full_frames_cnt;
144 u32 total_packet_cnt;
147 typedef void (*dpdk_flowcontrol_callback_t) (vlib_main_t * vm,
148 u32 hw_if_index, u32 n_packets);
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.
163 struct rte_ring *swq;
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;
179 struct rte_ring **swq;
180 struct rte_mbuf **pkts_enq;
181 struct rte_mbuf **pkts_deq;
182 struct rte_sched_port *hqos;
188 } dpdk_device_hqos_per_hqos_thread_t;
192 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
193 volatile u32 **lockp;
198 u32 vlib_hw_if_index;
199 u32 vlib_sw_if_index;
201 /* next node index if we decide to steal the rx graph arc */
202 u32 per_interface_next_index;
204 /* dpdk rte_mbuf rx and tx vectors, VLIB_FRAME_SIZE */
205 struct rte_mbuf ***tx_vectors; /* one per worker thread */
206 struct rte_mbuf ***rx_vectors;
208 /* vector of traced contexts, per device */
209 u32 *d_trace_buffers;
215 #define DPDK_DEVICE_FLAG_ADMIN_UP (1 << 0)
216 #define DPDK_DEVICE_FLAG_PROMISC (1 << 1)
217 #define DPDK_DEVICE_FLAG_PMD (1 << 2)
219 #define DPDK_DEVICE_FLAG_HAVE_SUBIF (1 << 5)
220 #define DPDK_DEVICE_FLAG_HQOS (1 << 6)
223 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
225 u8 *interface_name_suffix;
227 /* number of sub-interfaces */
234 u16 *cpu_socket_id_by_queue;
235 struct rte_eth_conf port_conf;
236 struct rte_eth_txconf tx_conf;
239 dpdk_device_hqos_per_worker_thread_t *hqos_wt;
240 dpdk_device_hqos_per_hqos_thread_t *hqos_ht;
243 u8 af_packet_port_id;
245 struct rte_eth_link link;
246 f64 time_last_link_update;
248 struct rte_eth_stats stats;
249 struct rte_eth_stats last_stats;
250 struct rte_eth_stats last_cleared_stats;
251 struct rte_eth_xstat *xstats;
252 struct rte_eth_xstat *last_cleared_xstats;
253 f64 time_last_stats_update;
254 dpdk_port_type_t port_type;
256 dpdk_efd_agent_t efd_agent;
259 #define DPDK_STATS_POLL_INTERVAL (10.0)
260 #define DPDK_MIN_STATS_POLL_INTERVAL (0.001) /* 1msec */
262 #define DPDK_LINK_POLL_INTERVAL (3.0)
263 #define DPDK_MIN_LINK_POLL_INTERVAL (0.001) /* 1msec */
267 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
269 /* total input packet counter */
270 u64 aggregate_rx_packets;
275 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
277 /* total input packet counter */
278 u64 aggregate_rx_packets;
279 } dpdk_hqos_thread_t;
285 } dpdk_device_and_queue_t;
287 /* Early-Fast-Discard (EFD) */
288 #define DPDK_EFD_DISABLED 0
289 #define DPDK_EFD_DISCARD_ENABLED (1 << 0)
290 #define DPDK_EFD_MONITOR_ENABLED (1 << 1)
291 #define DPDK_EFD_DROPALL_ENABLED (1 << 2)
293 #define DPDK_EFD_DEFAULT_DEVICE_QUEUE_HI_THRESH_PCT 90
294 #define DPDK_EFD_DEFAULT_CONSEC_FULL_FRAMES_HI_THRESH 6
296 typedef struct dpdk_efd_t
300 u16 consec_full_frames_hi_thresh;
304 #ifndef DPDK_HQOS_DBG_BYPASS
305 #define DPDK_HQOS_DBG_BYPASS 0
308 #ifndef HQOS_FLUSH_COUNT_THRESHOLD
309 #define HQOS_FLUSH_COUNT_THRESHOLD 100000
312 typedef struct dpdk_device_config_hqos_t
315 u32 hqos_thread_valid;
321 u32 pktfield0_slabpos;
322 u32 pktfield1_slabpos;
323 u32 pktfield2_slabpos;
324 u64 pktfield0_slabmask;
325 u64 pktfield1_slabmask;
326 u64 pktfield2_slabmask;
329 struct rte_sched_port_params port;
330 struct rte_sched_subport_params *subport;
331 struct rte_sched_pipe_params *pipe;
333 } dpdk_device_config_hqos_t;
335 int dpdk_hqos_validate_mask (u64 mask, u32 n);
336 void dpdk_device_config_hqos_pipe_profile_default (dpdk_device_config_hqos_t *
337 hqos, u32 pipe_profile_id);
338 void dpdk_device_config_hqos_default (dpdk_device_config_hqos_t * hqos);
339 clib_error_t *dpdk_port_setup_hqos (dpdk_device_t * xd,
340 dpdk_device_config_hqos_t * hqos);
341 void dpdk_hqos_metadata_set (dpdk_device_hqos_per_worker_thread_t * hqos,
342 struct rte_mbuf **pkts, u32 n_pkts);
344 #define foreach_dpdk_device_config_item \
353 vlib_pci_addr_t pci_addr;
355 u8 vlan_strip_offload;
356 #define DPDK_DEVICE_VLAN_STRIP_DEFAULT 0
357 #define DPDK_DEVICE_VLAN_STRIP_OFF 1
358 #define DPDK_DEVICE_VLAN_STRIP_ON 2
360 #define _(x) uword x;
361 foreach_dpdk_device_config_item
363 clib_bitmap_t * workers;
365 dpdk_device_config_hqos_t hqos;
366 } dpdk_device_config_t;
373 u8 *eal_init_args_str;
376 u8 enable_tcp_udp_checksum;
378 /* Required config parameters */
379 u8 coremask_set_manually;
380 u8 nchannels_set_manually;
384 u8 num_kni; /* while kni_init allows u32, port_id in callback fn is only u8 */
387 * format interface names ala xxxEthernet%d/%d/%d instead of
388 * xxxEthernet%x/%x/%x.
390 u8 interface_name_format_decimal;
392 /* per-device config */
393 dpdk_device_config_t default_devconf;
394 dpdk_device_config_t *dev_confs;
395 uword *device_config_index_by_pci_addr;
397 } dpdk_config_main_t;
399 dpdk_config_main_t dpdk_config_main;
405 dpdk_device_t *devices;
406 dpdk_device_and_queue_t **devices_by_cpu;
407 dpdk_device_and_queue_t **devices_by_hqos_cpu;
409 /* per-thread recycle lists */
412 /* buffer flags template, configurable to enable/disable tcp / udp cksum */
413 u32 buffer_flags_template;
415 /* flow control callback. If 0 then flow control is disabled */
416 dpdk_flowcontrol_callback_t flowcontrol_callback;
418 /* vlib buffer free list, must be same size as an rte_mbuf */
419 u32 vlib_buffer_free_list_index;
421 /* dpdk worker "threads" */
422 dpdk_worker_t *workers;
424 /* dpdk HQoS "threads" */
425 dpdk_hqos_thread_t *hqos_threads;
427 /* Ethernet input node index */
428 u32 ethernet_input_node_index;
430 /* pcap tracing [only works if (CLIB_DEBUG > 0)] */
432 pcap_main_t pcap_main;
434 u32 pcap_sw_if_index;
435 u32 pcap_pkts_to_capture;
438 uword *dpdk_device_by_kni_port_id;
439 uword *vu_sw_if_index_by_listener_fd;
440 uword *vu_sw_if_index_by_sock_fd;
441 u32 *vu_inactive_interfaces_device_index;
443 /* efd (early-fast-discard) settings */
447 * flag indicating that a posted admin up/down
448 * (via post_sw_interface_set_flags) is in progress
450 u8 admin_up_down_in_progress;
454 /* which cpus are running dpdk-input */
455 int input_cpu_first_index;
458 /* which cpus are running I/O TX */
459 int hqos_cpu_first_index;
462 /* control interval of dpdk link state and stat polling */
463 f64 link_state_poll_interval;
464 f64 stat_poll_interval;
466 /* Sleep for this many MS after each device poll */
470 vlib_main_t *vlib_main;
471 vnet_main_t *vnet_main;
472 dpdk_config_main_t *conf;
475 dpdk_main_t dpdk_main;
479 DPDK_RX_NEXT_IP4_INPUT,
480 DPDK_RX_NEXT_IP6_INPUT,
481 DPDK_RX_NEXT_MPLS_INPUT,
482 DPDK_RX_NEXT_ETHERNET_INPUT,
493 /* Copy of VLIB buffer; packet data stored in pre_data. */
494 vlib_buffer_t buffer;
495 } dpdk_tx_dma_trace_t;
503 vlib_buffer_t buffer; /* Copy of VLIB buffer; pkt data stored in pre_data. */
504 u8 data[256]; /* First 256 data bytes, used for hexdump */
505 } dpdk_rx_dma_trace_t;
507 void vnet_buffer_needs_dpdk_mb (vlib_buffer_t * b);
509 void dpdk_set_next_node (dpdk_rx_next_t, char *);
511 clib_error_t *dpdk_set_mac_address (vnet_hw_interface_t * hi, char *address);
513 clib_error_t *dpdk_set_mc_filter (vnet_hw_interface_t * hi,
514 struct ether_addr mc_addr_vec[], int naddr);
516 void dpdk_thread_input (dpdk_main_t * dm, dpdk_device_t * xd);
518 clib_error_t *dpdk_port_setup (dpdk_main_t * dm, dpdk_device_t * xd);
520 void dpdk_set_flowcontrol_callback (vlib_main_t * vm,
521 dpdk_flowcontrol_callback_t callback);
523 u32 dpdk_interface_tx_vector (vlib_main_t * vm, u32 dev_instance);
525 void set_efd_bitmap (u8 * bitmap, u32 value, u32 op);
527 struct rte_mbuf *dpdk_replicate_packet_mb (vlib_buffer_t * b);
528 struct rte_mbuf *dpdk_zerocopy_replicate_packet_mb (vlib_buffer_t * b);
530 #define foreach_dpdk_error \
531 _(NONE, "no error") \
532 _(RX_PACKET_ERROR, "Rx packet errors") \
533 _(RX_BAD_FCS, "Rx bad fcs") \
534 _(L4_CHECKSUM_ERROR, "Rx L4 checksum errors") \
535 _(IP_CHECKSUM_ERROR, "Rx ip checksum errors") \
536 _(RX_ALLOC_FAIL, "rx buf alloc from free list failed") \
537 _(RX_ALLOC_NO_PHYSMEM, "rx buf alloc failed no physmem") \
538 _(RX_ALLOC_DROP_PKTS, "rx packets dropped due to alloc error") \
539 _(IPV4_EFD_DROP_PKTS, "IPV4 Early Fast Discard rx drops") \
540 _(IPV6_EFD_DROP_PKTS, "IPV6 Early Fast Discard rx drops") \
541 _(MPLS_EFD_DROP_PKTS, "MPLS Early Fast Discard rx drops") \
542 _(VLAN_EFD_DROP_PKTS, "VLAN Early Fast Discard rx drops")
546 #define _(f,s) DPDK_ERROR_##f,
553 * Increment EFD drop counter
555 static_always_inline void
556 increment_efd_drop_counter (vlib_main_t * vm, u32 counter_index, u32 count)
560 my_n = vlib_get_node (vm, dpdk_input_node.index);
561 vm->error_main.counters[my_n->error_heap_index + counter_index] += count;
564 int dpdk_set_stat_poll_interval (f64 interval);
565 int dpdk_set_link_state_poll_interval (f64 interval);
566 void dpdk_update_link_state (dpdk_device_t * xd, f64 now);
567 void dpdk_device_lock_init (dpdk_device_t * xd);
568 void dpdk_device_lock_free (dpdk_device_t * xd);
569 void dpdk_efd_update_counters (dpdk_device_t * xd, u32 n_buffers,
571 u32 is_efd_discardable (vlib_thread_main_t * tm, vlib_buffer_t * b0,
572 struct rte_mbuf *mb);
575 vnet_get_aggregate_rx_packets (void)
577 dpdk_main_t *dm = &dpdk_main;
581 vec_foreach (dw, dm->workers) sum += dw->aggregate_rx_packets;
586 void dpdk_rx_trace (dpdk_main_t * dm,
587 vlib_node_runtime_t * node,
589 u16 queue_id, u32 * buffers, uword n_buffers);
591 #define EFD_OPERATION_LESS_THAN 0
592 #define EFD_OPERATION_GREATER_OR_EQUAL 1
594 void efd_config (u32 enabled,
595 u32 ip_prec, u32 ip_op,
596 u32 mpls_exp, u32 mpls_op, u32 vlan_cos, u32 vlan_op);
598 void post_sw_interface_set_flags (vlib_main_t * vm, u32 sw_if_index,
601 u32 dpdk_get_admin_up_down_in_progress (void);
603 u32 dpdk_num_mbufs (void);
605 dpdk_pmd_t dpdk_get_pmd_type (vnet_hw_interface_t * hi);
607 i8 dpdk_get_cpu_socket (vnet_hw_interface_t * hi);
609 void *dpdk_input_multiarch_select ();
610 void *dpdk_input_rss_multiarch_select ();
611 void *dpdk_input_efd_multiarch_select ();
613 clib_error_t *dpdk_get_hw_interface_stats (u32 hw_if_index,
614 struct rte_eth_stats *dest);
616 format_function_t format_dpdk_device_name;
617 format_function_t format_dpdk_device;
618 format_function_t format_dpdk_tx_dma_trace;
619 format_function_t format_dpdk_rx_dma_trace;
620 format_function_t format_dpdk_rte_mbuf;
621 format_function_t format_dpdk_rx_rte_mbuf;
622 unformat_function_t unformat_socket_mem;
623 clib_error_t *unformat_rss_fn (unformat_input_t * input, uword * rss_fn);
624 clib_error_t *unformat_hqos (unformat_input_t * input,
625 dpdk_device_config_hqos_t * hqos);
628 admin_up_down_process (vlib_main_t * vm,
629 vlib_node_runtime_t * rt, vlib_frame_t * f);
631 #endif /* __included_dpdk_h__ */
634 * fd.io coding-style-patch-verification: ON
637 * eval: (c-set-style "gnu")