#include
#include
#include
#include
#include
#include
#include
#define RX_RING_SIZE 128 //接收环大小
#define TX_RING_SIZE 512 //发送环大小
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32
static const struct rte_eth_conf port_conf_default = {
.rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN } //用于配置以太网端口的默认结构。
};
/* basicfwd.c: Basic DPDK skeleton forwarding example. */
/*
* Initializes a given port using global settings and with the RX buffers
* coming from the mbuf_pool passed as a parameter.
*/
/*
指定网口的队列数,本列中指定的队列
在tx、rx两个方向上,设置缓冲区
*/
static inline int
port_init(uint8_t port, struct rte_mempool *mbuf_pool) //初始化网卡配置;
{
struct rte_eth_conf port_conf = port_conf_default; //网口配置=默认的网口配置
const uint16_t rx_rings = 1, tx_rings = 1; //网口tx、rx队列的个数
uint16_t nb_rxd = RX_RING_SIZE; //为接收环分配的接收描述符数量
uint16_t nb_txd = TX_RING_SIZE; //同上
int retval; //临时变量,返回值
uint16_t q;
if (port >= rte_eth_dev_count()) //port号大于可使用的以太网设备总数,则参数有错,退出;
return -1;
/* Configure the Ethernet device. */
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf); //设置网卡设备
if (retval != 0)
return retval;
retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);//??????????????????????????????????????
if (retval != 0)
return retval;
/* Allocate and set up 1 RX queue per Ethernet port. */
for (q = 0; q < rx_rings; q++) { //遍历指定网口的所有rx队列
retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
rte_eth_dev_socket_id(port), NULL, mbuf_pool); //申请并设置一个收包队列
if (retval < 0)
return retval;
}
/* Allocate and set up 1 TX queue per Ethernet port. */
for (q = 0; q < tx_rings; q++) {
retval = rte_eth_tx_queue_setup(port, q, nb_txd, //同上
rte_eth_dev_socket_id(port), NULL);
if (retval < 0)
return retval;
}
/* Start the Ethernet port. */
retval = rte_eth_dev_start(port); //启动网卡
if (retval < 0)
return retval;
/* Display the port MAC address. */
struct ether_addr addr;
rte_eth_macaddr_get(port, &addr); //获取网卡的MAC地址,并打印
printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
" %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
(unsigned)port,
addr.addr_bytes[0], addr.addr_bytes[1],
addr.addr_bytes[2], addr.addr_bytes[3],
addr.addr_bytes[4], addr.addr_bytes[5]);
/* Enable RX in promiscuous mode for the Ethernet device. */
rte_eth_promiscuous_enable(port); //开启设备的混杂模式,允许所有报文进入;
return 0;
}
//业务函数入口点
//__attribute__((noreturn))用法
//标明函数无返回值
//用来修饰lcore_main函数,标明lcore_main无返回值
*/
/*
//1、检测CPU与网卡是否匹配
//2、建议使用本地CPU就近网卡???,不理解
//3、数据接收、发送的while(1)
*/
static __attribute__((noreturn)) void
lcore_main(void)
{
const uint8_t nb_ports = rte_eth_dev_count(); //网口总数
uint8_t port; //临时变量,网口号
/*
* Check that the port is on the same NUMA node as the polling thread
* for best performance.
*/
for (port = 0; port < nb_ports; port++)
if (rte_eth_dev_socket_id(port) > 0 &&
rte_eth_dev_socket_id(port) !=
(int)rte_socket_id()) //检测网口和当当前lcore的物理socket的ID是否匹配,即这个网口是不是当前CPU的;
printf("WARNING, port %u is on remote NUMA node to "
"polling thread.\n\tPerformance will "
"not be optimal.\n", port);
printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
rte_lcore_id());
/* Run until the application is quit or killed. */
for (;;) { /*运行 直到 应用程序 推出 或 被kill*/
/*
* 从Eth接收数据包 ,并发送到 ETH上。
* 发送顺序为:0 的接收 到 1的 发送,
* 1 的接收 到 0的 发送
* 每两个端口为一对
*/
for (port = 0; port < nb_ports; port++) {
/* Get burst of RX packets, from first port of pair. */
struct rte_mbuf *bufs[BURST_SIZE];//存储数据包的指针;
const uint16_t nb_rx = rte_eth_rx_burst(port, 0, //0是队列的ID;
bufs, BURST_SIZE); //BURST_SIZE要取回的包的最大数目。
if (unlikely(nb_rx == 0))//把这个选择标记成绝少发生的分支:即if(unlikely(x))等价于if(x),但是它告诉gcc,x取0的可能性比较大。
continue;
/* Send burst of TX packets, to second port of pair. */
const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
bufs, nb_rx);
//*****注意:以上流程为:从x收到的包,发送到x^1口
//其中,0^1 = 1, 1^1 = 0
//此运算可以达到测试要求的收、发包逻辑
/* Free any unsent packets. */
//释放不发送的数据包
//1、收到nb_rx个包,转发了nb_tx个,剩余nb_rx-nb_tx个
//2、把剩余的包释放掉
if (unlikely(nb_tx < nb_rx)) {
uint16_t buf;
for (buf = nb_tx; buf < nb_rx; buf++)
rte_pktmbuf_free(bufs[buf]); //把没有发送的数据包释放掉
}
}
}
}
int main(int argc, char *argv[])
{
struct rte_mempool *mbuf_pool; //指向内存池结构的指针变量
unsigned nb_ports; //网口个数
uint8_t portid; //网口号,临时的标记变量
int ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
argc -= ret; //不知道有啥用???????????????????????????
argv += ret; //???????????????????????????????????????
nb_ports = rte_eth_dev_count();
if (nb_ports < 2 || (nb_ports & 1))
rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
/* Creates a new mempool in memory to hold the mbufs. */
mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports,
MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
if (mbuf_pool == NULL) //如果有效网口数小于2或有效网口数为奇数0,则出错。奇数不行吗,最后一个网口不用不就行了??????????????
rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
/* Initialize all ports. */
for (portid = 0; portid < nb_ports; portid++) //端口初始化;
if (port_init(portid, mbuf_pool) != 0) //只有等于0,网口初始化才成功
rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8 "\n",
portid);
if (rte_lcore_count() > 1)
printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");
/* Call lcore_main on the master core only. */
lcore_main(); //执行数据转发函数;
return 0;
}