DPDK skeleton basicfwd 源碼閱讀


學習這個例子用於理解單純的 dpdk 轉發過程,L2 和 L3 的轉發是基於此:在rte_eth_rx_burst()收包后進行解包,提取 mac、ip 等信息然后在轉發到輸出網卡。

如果要寫出自己的程序(例如發特定的包,做特定的流程),這個例子還是很有學習的必要。多看幾遍,直到完全弄懂里面的流程和重要的API。

代碼部分

main函數

/*
 * The main function, which does initialization and calls the per-lcore
 * functions.
 */
int
main(int argc, char *argv[])
{
	struct rte_mempool *mbuf_pool; // 指向內存池結構的指針
	unsigned nb_ports; // 網口個數
	uint16_t portid; // 網口號

	/* Initialize the Environment Abstraction Layer (EAL). */
	int ret = rte_eal_init(argc, argv); 
	if (ret < 0)
		rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
	
	// ret是init函數的返回值,是命令行中被解析成功的參數個數
	argc -= ret;
	argv += ret; // 這兩個操作有點摸不着頭腦。。

	/* Check that there is an even number of ports to send/receive on. */

	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");

	// dpdk用mbuf保存packet,mempool用於操作mbuf
	/* rte_pktmbuf_pool_create() 創建並初始化mbuf池,是 rte_mempool_create 這個函數的封裝。
		五個參數:
		1. mbuf的名字 "MBUF_POOL"
		2. mbuf中的元素個數。每個端口給了8191個
		3. 每個核心的緩存大小,如果該參數為0 則可以禁用緩存。本程序中是250
		4. 每個mbuf中的數據緩沖區大小
		5. 應分配內存的套接字標識符。
		返回值:分配成功時返回指向新分配的mempool的指針。

		mempool的指針會傳給 port_init 函數,用於 setup rx queue
	*/

	/* 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()); //rte_socket_id()返回正在運行的lcore所對應的物理socket。socket的文檔在 lcore中

	if (mbuf_pool == NULL) // 若mempool分配失敗
		rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");

	/* Initialize all ports. 在每個網口上初始化 */
	RTE_ETH_FOREACH_DEV(portid) // 必須使用RTE_ETH_FOREACH_DEV()宏來訪問這些設備以處理非連續范圍的設備。

		if (port_init(portid, mbuf_pool) != 0) 
			rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n",
					portid);

	if (rte_lcore_count() > 1) // basicfwd只需要使用一個邏輯核
		printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");

	/* Call lcore_main on the master core only. */
	// 僅僅一個主線程調用
	// 這個程序純粹地把一個網口收到的包從另一個網口轉發出去,就像是一個repeater,中間沒有其他任何處理。
	lcore_main();

	return 0;
}

端口初始化port_init(portid, mbuf_pool)

/*
 * 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(uint16_t port, struct rte_mempool *mbuf_pool)
{
	struct rte_eth_conf port_conf = port_conf_default; // 這個rte_eth_conf結構體在配置網卡時要用到
	const uint16_t rx_rings = 1, tx_rings = 1; // 每個網口有多少rx和tx隊列,這里都為1
	uint16_t nb_rxd = RX_RING_SIZE; // 接收環大小
	uint16_t nb_txd = TX_RING_SIZE; // 發送環大小
	int retval;
	uint16_t q;
	struct rte_eth_dev_info dev_info; // 用於獲取以太網設備的信息,setup queue 時用到
	struct rte_eth_txconf txconf; // setup tx queue 時用到

	if (!rte_eth_dev_is_valid_port(port)) // 檢查設備的port_id是否已連接
		return -1;

	rte_eth_dev_info_get(port, &dev_info); /* 查詢以太網設備的信息,參數port指示以太網設備的網口標識符,第二個參數指向要填充信息的類型rte_eth_dev_info的結構的指針。*/
										   
	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
		port_conf.txmode.offloads |=
			DEV_TX_OFFLOAD_MBUF_FAST_FREE;

	// rte_eth_dev_configure() 配置網卡
	/* 四個參數
		1. port id
		2. 要給該網卡配置多少個收包隊列 這里是一個
		3. 要給該網卡配置多少個發包隊列 也是一個
		4. 結構體指針類型 rte_eth_conf * 
	*/
	retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
	if (retval != 0)  // 返回值0:成功,設備已配置。
		return retval; 

	// rte_eth_dev_adjust_nb_rx_tx_desc() 檢查Rx和Tx描述符的數量是否滿足以太網設備信息中的描述符限制,否則將它們調整為邊界。
	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
	if (retval != 0) // 返回值0:如果成功。
		return retval;

	/*  rte_eth_rx_queue_setup() 顧名思義的函數名
	配置rx隊列需要六個參數
		1. port id
		2. 接收隊列的索引。要在[0, rx_queue - 1] 的范圍內(先前rte_eth_dev_configure中配置的)
		3. 為接收環分配的接收描述符數。(環的大小)
		4. socket id。 如果是 NUMA 架構 就使用 rte_eth_dev_socket_id(port)獲取port所對應的以太網設備所連接上的socket的id;若不是NUMA,該值可以是宏SOCKET_ID_ANY
		5. 指向rx queue的配置數據的指針。如果是NULL,則使用默認配置。
		6. 指向內存池mempool的指針,從中分配mbuf去操作隊列。
	*/ 

	/* 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, nb_rxd,
				rte_eth_dev_socket_id(port), NULL, mbuf_pool);
		if (retval < 0)
			return retval;
	}

	txconf = dev_info.default_txconf;
	txconf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
	txconf.offloads = port_conf.txmode.offloads; // 這是一些配置數據的,內容先略過不看了

	/* rte_eth_tx_queue_setup()
	配置tx隊列需要五個參數(不需要mempool)
		1. port id
		2. 發送隊列的索引。要在[0, tx_queue - 1] 的范圍內(先前rte_eth_dev_configure中配置的)
		3. 為發送環分配的接收描述符數。(自定義環的大小)
		4. socket id
		5. 指向tx queue的配置數據的指針,結構體是rte_eth_txconf。
	*/

	/* 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), &txconf);
		if (retval < 0)
			return retval;
	}

	// 啟動設備
	// 設備啟動步驟是最后一步,包括設置已配置的offload功能以及啟動設備的發送和接收單元。成功時,可以調用以太網API導出的所有基本功能(鏈接狀態,接收/發送等)。

	/* 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);
	// #define PRIx8 "hhx" 
	// 十六進制數形式輸出整數 一個h表示short,即short int ,兩個h表示short short,即 char。%hhx用於輸出char
	printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
			   " %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
			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;
}

lcore_main

/*
 * 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)
{
	uint16_t port;

	/*
	 * Check that the port is on the same NUMA node as the polling thread
	 * for best performance.
	 */
	// 當有NUMA結構時,檢查網口是否在同一個NUMA node節點上,只有在一個NUMA node上時線程輪詢的效率最好
	RTE_ETH_FOREACH_DEV(port)
		if (rte_eth_dev_socket_id(port) > 0 &&
				rte_eth_dev_socket_id(port) !=
						(int)rte_socket_id())
						// 若以太網口所在的NUMA socket號與當前線程所在的 socket 號不同,報warming
			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.
		 */

		/*
			一個端口收到包,就立刻轉發到另一個端口
			0 和 1 
			2 和 3
			……
		*/

		RTE_ETH_FOREACH_DEV(port) {

			/* Get burst of RX packets, from first port of pair. */
			struct rte_mbuf *bufs[BURST_SIZE];// mbuf的結構體 收到的包存在這里,也是要發出去的包

			/* 收包函數:rte_eth_rx_burst 從以太網設備的接收隊列中檢索一連串(burst收發包機制)輸入數據包。檢索到的數據包存儲在rte_mbuf結構中。
			參數四個
				1. port id (收到哪個網口)
				2. 隊列索引 (的哪一條隊列),范圍要在[0, rx_queue - 1] 的范圍內(rte_eth_dev_configure中的)
				3. 指向 rte_mbuf 結構的 指針數組 的地址。要夠容納第四個參數所表示的數目的指針。(把收到的包存在哪里?)
				4. 要檢索的最大數據包數
			
			rte_eth_rx_burst()是一個循環函數,從RX隊列中收包達到設定的最大數量為止。

			收包操作:
			1. 根據NIC的RX描述符信息,初始化rte_mbuf數據結構。
			2. 將rte_mbuf(也就是數據包)存儲到第三個參數所指示的數組的下一個條目。
			3. 從mempool分配新的的rte_mbuf
			*/

			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. */
		
			/* 發包函數:rte_eth_tx_burst 在由port id指示的以太網設備的傳輸隊列(由索引指示)發送一連串輸出數據包。
			參數四個:
				1. port id(從哪個網口)
				2. 隊列索引(的哪條隊列發出),范圍要在[0, tx_queue - 1] 的范圍內(rte_eth_dev_configure中的)
				3. 指向包含要發送的數據包的 rte_mbuf 結構的 指針數組 的地址。(要發送的包的內容在哪里)
				4. 要發送的數據包的最大數量。

			返回值是發送的包的數量。

			發包操作:
			1. 選擇發包隊列中下一個可用的描述符
			2. 使用該描述符發送包,之后釋放對應的mempool空間
			3. 再根據 *rte_mbuf 初始化發送描述符
			*/
			const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0, 
					bufs, nb_rx); // port 異或 1 --> 0就和1是一對,2就和3是一對。
					// 0 收到包就從 1 轉發, 3 收到包 就從 2 口轉發。

			/* Free any unsent packets. */
			// 用unlikely宏代表這種情況不太可能出現?
			if (unlikely(nb_tx < nb_rx)) {
				uint16_t buf;
				for (buf = nb_tx; buf < nb_rx; buf++)
					rte_pktmbuf_free(bufs[buf]);
			}
		}
	}
}

頭文件、宏、struct

/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2015 Intel Corporation
 */

#include <stdint.h>
#include <inttypes.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>

#define RX_RING_SIZE 1024   // 接收環大小
#define TX_RING_SIZE 1024   // 發送環大小

#define NUM_MBUFS 8191      // mbuf中的元素個數,推薦數量是2的冪次-1
#define MBUF_CACHE_SIZE 250 //
#define BURST_SIZE 32       // Burst收發包模式的一次完成多個數據包的收發 深入淺出P122

// struct里的點:非順序初始化 https://blog.csdn.net/comwise/article/details/9087279

static const struct rte_eth_conf port_conf_default = { // 配置端口時使用的默認配置
	.rxmode = {
		.max_rx_pkt_len = ETHER_MAX_LEN,
		.ignore_offload_bitfield = 1,
	},
};

/* basicfwd.c: Basic DPDK skeleton forwarding example. */

總結

basicfw 由三個部分組成,它們之間的關系如下:

  1. main函數中先進行eal初始化以及分配mempool,然后轉到端口初始化。
  2. port_init 中配置網卡,設置隊列參數,開啟設備。
  3. 回到main函數中,主線程開始轉發。

main 函數

main函數的內容:

  1. 初始化 eal
  2. 以太網端口數需要為偶數(在這個程序中一個口接收,往另一個口轉發)
  3. 分配mbuf rte_pktmbuf_pool_create()
  4. 初始化所有端口 轉入 port_init(portid, mbuf_pool)
  5. 調用主線程開始basicfwd。

端口初始化

配置端口的工作:

  1. 檢查設備的port_id是否已連接 rte_eth_dev_is_valid_port(port))
  2. 得到以太網設備的信息 rte_eth_dev_info_get(port, &dev_info);
  3. 配置網卡rte_eth_dev_configure() 主要要配置的內容是要給每個網卡配置多少個收發(Rx、Tx)隊列。檢查是否符合要求。
  4. 配置完網卡后,要setup隊列
    rte_eth_rx_queue_setup 為以太網設備分配和設置接收隊列。要為Rx隊列設置指向 mempool的指針。
    rte_eth_tx_queue_setup 為以太網設備分配和設置傳輸隊列。可以設置傳輸隊列的長度,傳輸的模式等。
  5. 啟動設備rte_eth_dev_start(port) 是最后一步
  6. rte_eth_promiscuous_enable(port) 設置網卡為混雜模式

主線程

主線程是一個死循環,只能強行中斷來結束。
兩個端口為一組,一個端口收到包,就立刻轉發到另一個端口。例如 0 口收到包立刻從1 口轉發出去,3 口收到包立刻從 2 口轉發出去,等等。

  1. 在主線程里需要聲明MBUF的結構體 struct rte_mbuf *bufs[BURST_SIZE];
  2. 收包函數:rte_eth_rx_burst 從指定的以太網設備的接收隊列利用burst收發包機制接受一批數據包。檢索到的數據包存儲在rte_mbuf結構中。mbuf就是dpdk中用於存儲網絡數據包的緩沖區,mbuf是使用mempool來分配和回收內存的。mempool已經在端口初始化的函數中設置給了接收隊列。
  3. 發包函數:rte_eth_tx_burst 從指定的以太網設備的發送隊列發送一連串輸出數據包。在代碼中,發包函數參數中的mbuf數組就是收包函數收來的mbuf數組,也就是實現了原封不動的轉發。
  4. 釋放沒有發的包

執行情況

需要綁定網卡,且只需要指定一個線程即可。

root@ubuntu:/home/chang/dpdk/examples/skeleton/build# ./basicfwd -l 1 -n 4
EAL: Detected 8 lcore(s)
EAL: No free hugepages reported in hugepages-1048576kB
EAL: Multi-process socket /var/run/.rte_unix
EAL: Probing VFIO support...
EAL: PCI device 0000:02:01.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:02.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:03.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:04.0 on NUMA socket -1
EAL:   Invalid NUMA socket, default to 0
EAL:   probe driver: 8086:100f net_e1000_em
Port 0 MAC: 00 0c 29 f7 4d 25
Port 1 MAC: 00 0c 29 f7 4d 2f

Core 1 forwarding packets. [Ctrl+C to quit]
^C

參考 API 文檔條目:

(進入http://doc.dpdk.org/api/index.html 后搜索下列鏈接)

mbuf、ethdev、lcore

sample guide

http://doc.dpdk.org/guides/sample_app_ug/skeleton.html

參考鏈接

https://blog.csdn.net/pangyemeng/article/details/78226434


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM