DPDK skeleton basicfwd 源码阅读

源码部分

/* 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
#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. */

/*
 * 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;
    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; // 用于获取以太网设备的信息
    struct rte_eth_txconf txconf;

    if (!rte_eth_dev_is_valid_port(port)) // 检查设备的port_id是否已连接
        return -1;

    rte_eth_dev_info_get(port, &dev_info); // 查询以太网设备的信息
    // 第一个参数指示以太网设备的网口标识符
    // 第二个参数指向要填充信息的类型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;

    /* Configure the Ethernet device. */ 
    // 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; 

    // 检查Rx和Tx描述符的数量是否满足以太网设备信息中的描述符限制,否则将它们调整为边界。
    retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
    if (retval != 0) // 返回值0:如果成功。
        return retval;

    /* Allocate and set up 1 RX queue per Ethernet port. */

    
    /*  rte_eth_rx_queue_setup 为以太网设备分配和设置接收队列。 
    
    六个参数
        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. 指向要用于接收队列的配置数据的指针。如果是NULL,则使用默认配置。
        6. 指向内存池mempool的指针,从中分配mbuf去操作队列。
    */ 
    
    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 为以太网设备分配和设置传输队列。 
    
    五个参数
        1. port id
        2. 发送队列的索引。要在[0, tx_queue - 1] 的范围内(rte_eth_dev_configure中的)
        3. 为发送环分配的接收描述符数。(环的大小)
        4. socket id
        5. 传输队列的配置数据的指针。
    */

    /* 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;
    }

    /* Start the Ethernet port. */
    // 启动设备

    // 设备启动步骤是最后一步,包括设置已配置的offload功能以及启动设备的发送和接收单元。成功时,可以调用以太网API导出的所有基本功能(链接状态,接收/发送等)。
    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;
}

/*
 * 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. 使用该描述符发送包,之后释放空间
            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]);
            }
        }
    }
}

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

    /* Creates a new mempool in memory to hold the mbufs. */

    // dpdk用mbuf保存packet,mempool用于操作mbuf

    /* 创建并初始化mbuf池,是 rte_mempool_create 这个函数的封装。
        五个参数:
        1. mbuf的名字 "MBUF_POOL"
        2. mbuf中的元素个数。每个端口给了8191个
        3. 每个核心的缓存大小,如果该参数为0 则可以禁用缓存。本程序中是250
        4. 每个mbuf中的数据缓冲区大小
        5. 应分配内存的套接字标识符。
        成功时指向新分配的mempool的指针。
    */
    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) // 只需要使用一个逻辑核
        printf("\nWARNING: Too many lcores enabled. Only 1 used.\n");

    /* Call lcore_main on the master core only. */
    // 仅仅一个主线程调用,也就是一个线程处理一对网口之间的forward而已。
    lcore_main();

    return 0;
}

参考 API 文档条目:

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

mbuf、ethdev、lcore

猜你喜欢

转载自www.cnblogs.com/ZCplayground/p/9318291.html