版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012570105/article/details/82385690
基本转发
- 代码分析
#define RX_RING_SIZE 128
#define TX_RING_SIZE 512
#define NUM_MBUFS 8191
#define MBUF_CACHE_SIZE 250
#define BURST_SIZE 32
// max_rx_pkt_len这个选项只有当jumbo_frame被使能的时候才有效,
// 否则就按照1500的ip报文来处理
// mac帧为1514
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.
*/
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;
int retval;
uint16_t q;
if (port >= rte_eth_dev_count())
return -1;
/* Configure the Ethernet device. */
// 配置每个接口的收发队列、jumbo frame之类的
retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
if (retval != 0)
return retval;
// 设置该接口的每个收队列的配置,队列大小等
/* Allocate and set up 1 RX queue per Ethernet port. */
for (q = 0; q < rx_rings; q++) {
retval = rte_eth_rx_queue_setup(port, q, RX_RING_SIZE,
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, TX_RING_SIZE,
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);
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;
}
/*
* The lcore main. This is the main thread that does the work, reading from
* an input port and writing to an output port.
*/
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())
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 (;;) {
/*
* Receive packets on a port and forward them on the paired
* port. The mapping is 0 -> 1, 1 -> 0, 2 -> 3, 3 -> 2, etc.
*/
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,
bufs, BURST_SIZE);
if (unlikely(nb_rx == 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);
/* Free any unsent packets. */
if (unlikely(nb_tx < nb_rx)) {
uint16_t buf;
for (buf = nb_tx; buf < nb_rx; buf++)
rte_pktmbuf_free(bufs[buf]);
}
}
}
}
- 运行
./basicfwd
Port 0 MAC: 00 e0 4c 0a fe 6d
PMD: eth_em_rx_queue_setup(): sw_ring=0x7fffbab43d40 hw_ring=0x7fffbab44240 dma_addr=0x13f544240
PMD: eth_em_tx_queue_setup(): sw_ring=0x7fffbab31c00 hw_ring=0x7fffbab33d00 dma_addr=0x13f533d00
PMD: eth_em_start(): <<
Port 1 MAC: 00 e0 4c 0a fe 6e
PMD: eth_em_rx_queue_setup(): sw_ring=0x7fffbab215c0 hw_ring=0x7fffbab21ac0 dma_addr=0x13f521ac0
PMD: eth_em_tx_queue_setup(): sw_ring=0x7fffbab0f480 hw_ring=0x7fffbab11580 dma_addr=0x13f511580
PMD: eth_em_start(): <<
Port 2 MAC: 00 e0 4c 0a fe 6f
PMD: eth_em_rx_queue_setup(): sw_ring=0x7fffbaafee40 hw_ring=0x7fffbaaff340 dma_addr=0x13f4ff340
PMD: eth_em_tx_queue_setup(): sw_ring=0x7fffbaaecd00 hw_ring=0x7fffbaaeee00 dma_addr=0x13f4eee00
PMD: eth_em_start(): <<
Port 3 MAC: 00 e0 4c 0a fe 70
WARNING: Too many lcores enabled. Only 1 used.
Core 0 forwarding packets. [Ctrl+C to quit]
- 使用分析
- 这个程序可以当做虚拟线来使用
- 这个程序只支持网卡单队列的情况
- 后续可以将这个程序修改成多队列多线程