前面我们已经了解了如何使用DPDK创建线程并绑定核心,以及如何申请内存池并创建 RX/TX 队列。
接下来我们将了解DPDK的核心内容之一:以轮询的方式从网卡中收取报文。
下面直接给出一个实例,此实例使用核心1及核心2创建了两个线程用于报文处理,其中在核心1上运行的线程接收网卡0的消息,而在核心2上运行的线程接收网卡1的消息。在这里,从网卡上获取报文的方式为轮询,具体的接口为 rte_eth_rx_burst,而所谓的报文处理,就是简单地打印报文消息。
#include <arpa/inet.h>
#include <rte_eal.h>
#include <rte_mbuf.h>
#include <rte_ethdev.h>
#include<rte_errno.h>
#define NB_SOCKETS 10
#define MEMPOOL_CACHE_SIZE 250
static unsigned nb_mbuf = 512;
static int numa_on = 0;
static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
static int
init_mem(unsigned nb_mbuf){
unsigned lcore_id;
int socketid;
char mbuf_pool_name[64];
for(lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++){
if (rte_lcore_is_enabled(lcore_id) == 0){
continue;
}
if(numa_on){
socketid = rte_lcore_to_socket_id(lcore_id);
}
e