DPDK简单配置启动示例

一、示例操作

1.使用 rte_eal_init 初始化 DPDK 的环境抽象层 (EAL)。
2.使用 rte_eth_dev_count_avail 获取当前系统中可用的以太网设备数量。
3.使用 rte_eth_dev_configure 配置第一个可用的以太网设备 (port_id = 0) 的接收和发送队列。
4.使用 rte_pktmbuf_pool_create 创建一个内存池 mbuf_pool,用于存储数据包缓冲区 (mbufs)。
5.使用 rte_eth_tx_queue_setup 配置第一个以太网设备的发送队列。
6.使用 rte_eth_rx_queue_setup 配置第一个以太网设备的接收队列。
7.使用 rte_eth_dev_start 启动第一个以太网设备。
8.使用 rte_pktmbuf_alloc 分配一组数据包缓冲区,并使用 rte_eth_tx_burst 函数一次性发送这些数据包。
9.使用rte_mempool_free释放分配的数据包缓冲区并停止并关闭以太网设备。

二、代码示例


#include <rte_eal.h>
#include <rte_memory.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include <errno.h>
#include <string.h>
#define NUM_MBUFS 4096
#define PORT_ID 0

static const struct rte_eth_conf port_conf_default = {
	.rxmode = {.max_lro_pkt_size = RTE_ETHER_MAX_LEN }
};
int main(int argc,char *argv[])
{
    int err = rte_eal_init(argc,argv);  //init EAL environment
    printf("%s\n",strerror(err));
    struct rte_mempool *mbuf_pool=NULL;
    mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool",NUM_MBUFS,0,0,RTE_MBUF_DEFAULT_BUF_SIZE,rte_socket_id());    //create and initializes a packet mbuf pool
    if (mbuf_pool==NULL)
    {
        rte_exit(EXIT_FAILURE,"create mempool failed");
    }
    printf("%ld\n",sizeof(mbuf_pool));
    uint16_t usable_port = rte_eth_dev_count_avail();
    printf("the number of usable port is:%d\n",usable_port);
    struct rte_eth_dev_info eth_info;
    //Retrieve the contextual information of an Ethernet device. 
    err = rte_eth_dev_info_get(0,&eth_info); 
   
    printf("rte_eth_dev_info_get:%s\n",strerror(err));
    printf("driver name is:%s,if_index is:%d,min_rx_buf_size is:%d,max_rx_pktlen is:%d,max_mac_addrs is:%d,max_rx_queues is:%d\
    max_tx_queues is:%d\n",eth_info.driver_name,eth_info.if_index,eth_info.min_rx_bufsize,eth_info.max_rx_pktlen,eth_info.max_mac_addrs,eth_info.max_rx_queues\
    ,eth_info.max_tx_queues);

    struct rte_eth_conf port_conf = port_conf_default;
    //Configure an Ethernet device.
    err = rte_eth_dev_configure(PORT_ID,1,0,&port_conf);
    printf("rte_eth_dev_configure:%s\n",strerror(err));

    //Allocate and set up a receive queue for an Ethernet device.
    struct rte_eth_rxconf rte_rx_conf=eth_info.default_rxconf;
    err=rte_eth_rx_queue_setup(PORT_ID,0,eth_info.min_rx_bufsize,rte_eth_dev_socket_id(PORT_ID),NULL,mbuf_pool);
    printf("rte_eth_rx_queue_setup:%s\n",strerror(err));

    //Start an Ethernet device.
    err=rte_eth_dev_start(PORT_ID);
    printf("rte_eth_dev_start:%s\n",strerror(err));

    //Free a mempool Unlink the mempool from global list, free the memory chunks, and all memory referenced by the mempool. The objects must not be used by other cores as they will be freed.
    rte_mempool_free(mbuf_pool);
    printf("rte_mempool_free\n");
    return 0;

}

三、DPDK代码解析

1.结构体

rte_eth_dev_info

该结构体包含多个字段,描述以太网设备的各个方面

  • driver_name: 驱动程序名称。
  • if_index: 设备的接口索引。
  • min_rx_bufsize: 最小接收缓冲区大小。
  • max_rx_pktlen: 最大接收数据包长度。
  • max_mac_addrs: 最大MAC地址数。
  • max_vfs: 最大虚拟功能数量。
  • max_rx_queues: 最大接收队列数。
  • max_tx_queues: 最大发送队列数。
  • rx_offload_capa:支持的接收卸载能力。
  • tx_offload_capa: 支持的发送卸载能力。
rte_eth_conf

该结构体包含多个字段,用于描述以太网设备的配置信息。以下是一些重要字段:

  • rxmode: 接收队列的配置。
    – mq_mode: 多队列模式。
    – max_rx_pkt_len: 最大接收数据包长度。
    – split_hdr_size: 拆分的头部大小。
    – offloads: 接收卸载配置。

  • txmode: 发送队列的配置。
    – mq_mode:多队列模式。
    – offloads: 发送卸载配置。

  • rx_adv_conf: 高级接收队列配置。
    — rss_conf: 接收侧缩放配置。

  • tx_adv_conf: 高级发送队列配置。

  • fdir_conf: 流量定向配置。

  • intr_conf: 中断配置。

rte_eth_rxconf

该结构体包含多个字段,用于描述接收队列的配置信息。以下是一些重要字段:

  • List item
    – rx_thresh: 接收队列的阈值配置。
    – opthresh: 预取阈值。
    – ohthresh: 主机阈值。
    – owthresh: 写入阈值。
  • rx_free_thresh: 描述符的自由阈值。 rx_drop_en: 启用接收时丢弃。 rx_deferred_start:
    启用延迟启动。

2. 函数

//1.rte_eal_init(int argc,char **argv);
int rte_eal_init(int argc, char **argv); //Initialize the Environment Abstraction Layer (EAL). 
//2.rte_pktmbuf_pool_create();
struct rte_mempool *rte_pktmbuf_pool_create(const char *name, unsigned n, unsigned cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id);
//用于创建内存池的函数,该内存池用于存储数据包缓冲区

参数解释:

  • name: 内存池的名称,必须是唯一的。
  • n: 内存池中数据包缓冲区 (mbufs) 的数量。
  • cache_size:每个核心的缓存大小,建议为 32 的倍数,以提高性能。
  • priv_size: 每个 mbuf 的私有数据区域大小。
  • data_room_size: 每个 mbuf 的数据区域大小(以字节为单位)。
  • socket_id: 内存池所在的 NUMA 节点。

返回值
成功时返回指向创建的内存池的指针,失败时返回 NULL。

//3.rte_eth_dev_count_avail
uint16_t rte_eth_dev_count_avail();
//用于获取系统中可用以太网设备的数量。这个函数在初始化网络设备和配置端口时非常有用。

返回值
返回当前系统中可用的以太网设备数量

//4.rte_eth_dev_info_get
int rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info);
//提供有关特定以太网设备的详细信息,例如其驱动程序、支持的功能、队列数量等。

参数解释:

  • port_id: 设备的端口编号。
  • dev_info: 指向 rte_eth_dev_info 结构体的指针,用于存储获取到的设备信息。

返回值
0成功,否则失败

//5.rte_eth_dev_configure
int rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_queue, uint16_t nb_tx_queue, const struct rte_eth_conf *eth_conf);
//用于配置以太网设备的函数。该函数在初始化以太网设备时非常重要,因为它定义了设备的接收和发送队列数量以及其他相关的配置信息。

参数解释

  • port_id: 要配置的以太网设备的端口编号。
  • nb_rx_queue: 要配置的接收队列数量。
  • nb_tx_queue: 要配置的发送队列数量。
  • eth_conf: 指向 rte_eth_conf 结构体的指针,用于存储设备的配置信息。

返回值
成功时返回 0,失败时返回负值。

//6.rte_eth_rx_queue_setup
int rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id, uint16_t nb_rx_desc, unsigned int socket_id, const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool);
//DPDK 中用于设置接收队列的函数。该函数用于为指定的以太网设备配置接收队列,包括队列的内存池和其他参数

参数解释

  • port_id: 要配置的以太网设备的端口编号。
  • rx_queue_id: 要配置的接收队列的队列编号。
  • nb_rx_desc:接收队列的描述符数量。
  • socket_id: 内存分配的 NUMA 节点编号。
  • rx_conf: 指向 rte_eth_rxconf结构体的指针,用于存储接收队列的配置信息。如果为 NULL,则使用默认配置。
  • mb_pool: 指向 rte_mempool 结构体的指针,用于接收队列的 mbuf 内存池。

返回值
成功时返回 0,失败时返回负值。

//7.rte_eth_tx_queue_setup
int rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id, uint16_t nb_tx_desc, unsigned int socket_id, const struct rte_eth_txconf *tx_conf)
//是 DPDK 中用于设置接收队列的函数

参数解释

  • port_id: 要配置的以太网设备的端口编号。
  • rx_queue_id: 要配置的发送队列的队列编号。
  • nb_rx_desc:发送队列的描述符数量。
  • socket_id: 内存分配的 NUMA 节点编号。
  • tx_conf: 指向 rte_eth_txconf结构体的指针,用于存储接收队列的配置信息。如果为 NULL,则使用默认配置。

返回值
成功时返回 0,失败时返回负值。

 //8.rte_eth_dev_start 
 int rte_eth_dev_start(uint16_t port_id)
 //用于启动以太网设备的函数。该函数会使能设备的接收和发送功能,并允许数据包流通过设备。通常在配置完设备的接收和发送队列后调用该函数

参数解释

  • port_id: 要启动的以太网设备的端口编号。

返回值
成功时返回 0,失败时返回负值。

  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值