简介
DPDK包括了1G、10G、40G的PMD(Poll Mode Driver),即轮询模式驱动,运行在用户态,PMD直接访问RX和TX描述符而不需要中断,能够快速的接收、处理、发送报文。
报文处理的两种模式
- 一种是串行的,可以理解为lcore从报文接收、处理、发送一条龙全部搞完,然后再处理下一个报文。
- 一种是并行的,可以理解为lcore只处理一个业务逻辑,但同时会处理多个报文;然后通过ring过渡到下一个阶段。
run-to-completion模式
- 从端口RX描述符轮询收包。
- 然后在同样的lcore上被处理。
- 最后从端口TX描述符进行发送。
pipe-line模式
- 一个lcore轮询一个或多个端口RX描述符进行收包。
- 然后报文通过ring传送到另外的lcore上进行处理。
- 另外的lcore继续处理后从端口TX描述符进行发送。
性能优化
- CPU缓存
- 总线速度
- 网卡PCI带宽
- 批量处理
- 缓存行对齐
lcore、内存、网口队列关系
- NUMA架构下访问本地内存性能最高,即lcore和网口在同样的socket,且网口队列的mbuf内存池也是用的同样socket。
- 一个发送队列&接收队列只被一个lcore访问,即不要多核访问同样一个队列。
- 一个简单的公式
- p个端口,每个端口q个队列,总共有n个核;
- n大于等于q的情况下,0核处理0队列(每个端口的)、1核处理1队列(每个端口的),以此类推。
- 不保证最优性能,但是能保证逻辑简单且能正常使用。
设备标识
- 网口下标,即port_id
- 网口名称,即port_name
设备配置
- Allocate PCI resources
- Reset the hardware (issue a Global Reset) to a well-known default state
- Set up the PHY and the link
- Initialize statistics counters
一些硬件卸载的特性需要再端口初始化的时候使用特定的配置参数,如RSS、DCB等。
配置发送队列
- The number of descriptors of the transmit ring
- The socket identifier used to identify the appropriate DMA memory zone from which to allocate the transmit ring in NUMA architectures
- The values of the Prefetch, Host and Write-Back threshold registers of the transmit queue
- The minimum transmit packets to free threshold (tx_free_thresh).
- The minimum RS bit threshold. The minimum number of transmit descriptors to use before setting the Report Status (RS) bit in the transmit descriptor.
tx_free_thresh:传0表示使用默认值,默认值为32,即这个网口的发送队列至少发送了32个报文,网络控制器才进行检查看看是否写回了描述符,这个确保PMD在32个处理完前不需要查找完成的描述符。
tx_rs_thresh:传0表示使用默认值,默认值为32,即这个网口的发送队列至少发送了32个报文才触发RS(Report Status)位设置,注意此参数只在10G网口才生效,这样避免频繁的描述符写回节省了上游PCIe的带宽。
报文在网卡TX队列发送完成后,mbuf内存需要回收到mempool;也可以使用手动调用rte_eth_tx_done_cleanup()
的方式来实现。用于报文需要发送到多个目的接口,或者是报文生成工具的场景。
硬件卸载
读取
可以通过rte_eth_dev_info_get()
获取硬件具体支持的能力,一般包括校验和、TCP分段、VLAN插入等特性。
- 基于队列的
dev_info->[rt]x_queue_offload_capa
- 基于端口和队列的
dev_info->[rt]x_offload_capa
配置
可以通过RTE_ETH_TX_OFFLOAD_*
or RTE_ETH_RX_OFFLOAD_*
标志来设置硬件卸载特性。
- 基于队列的
rte_eth_[rt]x_queue_setup()
- 基于网口的
rte_eth_dev_configure()
扩展统计API
每个统计有三个属性:
name
: A human readable string formatted by the scheme detailed below.id
: An integer that represents only that statistic.value
: A unsigned 64-bit integer that is the value of the statistic. 可以通过 rte_eth_xstats_*() 系列函数进行读取。
命名规范
如 rx_bytes
表示接收的字节数。
- direction(方向)
- detail 1(信息1)
- detail 2(信息2)
- detail n(信息n)
- unit(单位)