DPDK二层转发示例源码阅读

Posted 宁静淡泊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DPDK二层转发示例源码阅读相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <netinet/in.h>
#include <setjmp.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>

#include <rte_common.h>
#include <rte_log.h>
#include <rte_malloc.h>
#include <rte_memory.h>
#include <rte_memcpy.h>
#include <rte_memzone.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_launch.h>
#include <rte_atomic.h>
#include <rte_cycles.h>
#include <rte_prefetch.h>
#include <rte_lcore.h>
#include <rte_per_lcore.h>
#include <rte_branch_prediction.h>
#include <rte_interrupts.h>
#include <rte_pci.h>
#include <rte_random.h>
#include <rte_debug.h>
#include <rte_ether.h>
#include <rte_ethdev.h>
#include <rte_mempool.h>
#include <rte_mbuf.h>

static volatile bool force_quit;

/* MAC updating enabled by default */
static int mac_updating = 1;

#define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1

#define NB_MBUF   8192

#define MAX_PKT_BURST 32
#define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
#define MEMPOOL_CACHE_SIZE 256

/*
 * Configurable number of RX/TX ring descriptors
 */
#define RTE_TEST_RX_DESC_DEFAULT 128
#define RTE_TEST_TX_DESC_DEFAULT 512
static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;

/* ethernet addresses of ports */
//端口的以太网地址
static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];

/* mask of enabled ports */
//启用端口的mask
static uint32_t l2fwd_enabled_port_mask = 0;

/* list of enabled ports */
//启用端口列表
static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];

static unsigned int l2fwd_rx_queue_per_lcore = 1;

#define MAX_RX_QUEUE_PER_LCORE 16
#define MAX_TX_QUEUE_PER_PORT 16
struct lcore_queue_conf {
    unsigned n_rx_port;
    unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
} __rte_cache_aligned;
struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];

static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];

static const struct rte_eth_conf port_conf = {
    .rxmode = {
        .split_hdr_size = 0,
        .header_split   = 0, /**< Header Split disabled */  //报头分离
        .hw_ip_checksum = 0, /**< IP checksum offload disabled */ //IP校验和卸载
        .hw_vlan_filter = 0, /**< VLAN filtering disabled */  //vlan过滤
        .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */ //巨星帧的支持
        .hw_strip_crc   = 0, /**< CRC stripped by hardware */  //使用硬件清除CRC
    },
    .txmode = {
        .mq_mode = ETH_MQ_TX_NONE,
    },
};

struct rte_mempool * l2fwd_pktmbuf_pool = NULL;

/* Per-port statistics struct */
struct l2fwd_port_statistics {
    uint64_t tx;
    uint64_t rx;
    uint64_t dropped;
} __rte_cache_aligned;
struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];

#define MAX_TIMER_PERIOD 86400 /* 1 day max */
/* A tsc-based timer responsible for triggering statistics printout */
static uint64_t timer_period = 10; /* default period is 10 seconds */

/* Print out statistics on packets dropped */
//打印的属性跳过。。
static void
print_stats(void)
{
    uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
    unsigned portid;

    total_packets_dropped = 0;
    total_packets_tx = 0;
    total_packets_rx = 0;

    const char clr[] = { 27, [, 2, J, \0 };
    const char topLeft[] = { 27, [, 1, ;, 1, H,\0 };

        /* Clear screen and move to top left */
    printf("%s%s", clr, topLeft);

    printf("\nPort statistics ====================================");

    for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
        /* skip disabled ports */
        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
            continue;
        printf("\nStatistics for port %u ------------------------------"
               "\nPackets sent: %24"PRIu64
               "\nPackets received: %20"PRIu64
               "\nPackets dropped: %21"PRIu64,
               portid,
               port_statistics[portid].tx,
               port_statistics[portid].rx,
               port_statistics[portid].dropped);

        total_packets_dropped += port_statistics[portid].dropped;
        total_packets_tx += port_statistics[portid].tx;
        total_packets_rx += port_statistics[portid].rx;
    }
    printf("\nAggregate statistics ==============================="
           "\nTotal packets sent: %18"PRIu64
           "\nTotal packets received: %14"PRIu64
           "\nTotal packets dropped: %15"PRIu64,
           total_packets_tx,
           total_packets_rx,
           total_packets_dropped);
    printf("\n====================================================\n");
}

static void
l2fwd_mac_updating(struct rte_mbuf *m, unsigned dest_portid)
{
    struct ether_hdr *eth;
    void *tmp;

    eth = rte_pktmbuf_mtod(m, struct ether_hdr *);

    /* 02:00:00:00:00:xx */
    tmp = &eth->d_addr.addr_bytes[0];
    *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);

    /* src addr */
    ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
}

//处理收到的数据包
static void
l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
{
    unsigned dst_port;
    int sent;
    struct rte_eth_dev_tx_buffer *buffer;

    //获取目的端口ID
    dst_port = l2fwd_dst_ports[portid];

    //更新MAC地址
    if (mac_updating)
        l2fwd_mac_updating(m, dst_port);

    //获取目的端口的 tx 缓存
    buffer = tx_buffer[dst_port];
    //发送数据包,到目的端口的tx缓存
    sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
    
    if (sent)
        port_statistics[dst_port].tx += sent;  //如果发包成功,发包计数+1
}

/* main processing loop */
static void
l2fwd_main_loop(void)
{
    struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
    struct rte_mbuf *m;
    int sent;
    unsigned lcore_id;
    uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
    unsigned i, j, portid, nb_rx;
    struct lcore_queue_conf *qconf;
    const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
            BURST_TX_DRAIN_US;
    struct rte_eth_dev_tx_buffer *buffer;

    prev_tsc = 0;
    timer_tsc = 0;

    //获取当前核心的ID
    lcore_id = rte_lcore_id();
    //获取当前核心的配置
    qconf = &lcore_queue_conf[lcore_id];

    //如果 rx_port的个数为0,则 记录日志。(其实是出错了)
    if (qconf->n_rx_port == 0) {
        RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
        return;
    }

    //记录日志,
    RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);

    //遍历所有的port
    for (i = 0; i < qconf->n_rx_port; i++) {
        //获取到portID
        portid = qconf->rx_port_list[i];
        //记录日志
        RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
            portid);

    }

    //如果没超时(最开始设置的那个二层转发的运行时间)
    while (!force_quit) {

        //获取时间戳
        cur_tsc = rte_rdtsc();

        /*
         * TX burst queue drain
         */
         //对比时间戳
        diff_tsc = cur_tsc - prev_tsc;
        if (unlikely(diff_tsc > drain_tsc)) {

            for (i = 0; i < qconf->n_rx_port; i++) {
                //获得portid和buffer
                portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
                buffer = tx_buffer[portid];
                
                //把buffer中的数据发送到portid对应的port
                sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
                if (sent)
                    port_statistics[portid].tx += sent;  //如果发包成功,发包计数+1

            }

            /* if timer is enabled */
            //如果计时器开启
            if (timer_period > 0) {

                /* advance the timer */
                //调整计时器
                timer_tsc += diff_tsc;

                /* if timer has reached its timeout */
                //如果计时器超时
                if (unlikely(timer_tsc >= timer_period)) {

                    /* do this only on master core */
                    //主线程打印一些属性,仅有主线程会执行print_stats
                    if (lcore_id == rte_get_master_lcore()) {
                        print_stats();   //打印属性,
                        /* reset the timer */
                        timer_tsc = 0;  //设置计时器为0
                    }
                }
            }

            prev_tsc = cur_tsc;
        }

        /*
         * Read packet from RX queues   //收包模块
         */
        for (i = 0; i < qconf->n_rx_port; i++) {
            //获取portID
            portid = qconf->rx_port_list[i];
            //从portID对应的Port收到nb_rx个包
            nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
                         pkts_burst, MAX_PKT_BURST);

            port_statistics[portid].rx += nb_rx;   //收包计数+=nb_rx

            for (j = 0; j < nb_rx; j++) {  //遍历收到的包
                m = pkts_burst[j];
                rte_prefetch0(rte_pktmbuf_mtod(m, void *));    //预取到pktmbuf
                l2fwd_simple_forward(m, portid);   //调用函数,处理收到的包
            }
        }
    }
}

//__attribute__((unused))表示可能没有这个参数,编译器不会报错
static int
l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
{
    l2fwd_main_loop(); //二层转发主要循环
    return 0;
}

/* display usage */
static void
l2fwd_usage(const char *prgname)
{
    printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
           "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
           "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
           "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n"
           "  --[no-]mac-updating: Enable or disable MAC addresses updating (enabled by default)\n"
           "      When enabled:\n"
           "       - The source MAC address is replaced by the TX port MAC address\n"
           "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n",
           prgname);
}

static int
l2fwd_parse_portmask(const char *portmask)
{
    char *end = NULL;
    unsigned long pm;

    /* parse hexadecimal string */
    //解析十六进制字符串,将十六进制字符串转换为unsigned long
    pm = strtoul(portmask, &end, 16);
    if ((portmask[0] == \0) || (end == NULL) || (*end != \0))
        return -1;

    if (pm == 0)
        return -1;

    return pm;
}

static unsigned int
l2fwd_parse_nqueue(const char *q_arg)
{
    char *end = NULL;
    unsigned long n;

    /* parse hexadecimal string */
    n = strtoul(q_arg, &end, 10);
    if ((q_arg[0] == \0) || (end == NULL) || (*end != \0))
        return 0;
    if (n == 0)
        return 0;
    if (n >= MAX_RX_QUEUE_PER_LCORE)
        return 0;

    return n;
}

static int
l2fwd_parse_timer_period(const char *q_arg)
{
    char *end = NULL;
    int n;

    /* parse number string */
    n = strtol(q_arg, &end, 10);
    if ((q_arg[0] == \0) || (end == NULL) || (*end != \0))
        return -1;
    if (n >= MAX_TIMER_PERIOD)
        return -1;

    return n;
}

//解析命令行参数
/* Parse the argument given in the command line of the application */
static int
l2fwd_parse_args(int argc, char **argv)
{
    int opt, ret, timer_secs;
    char **argvopt;
    int option_index;
    char *prgname = argv[0];
    static struct option lgopts[] = {
        { "mac-updating", no_argument, &mac_updating, 1},
        { "no-mac-updating", no_argument, &mac_updating, 0},
        {NULL, 0, 0, 0}
    };

    argvopt = argv;     //复制argv指针

    //解析命令行,getopt_long()可以解析命令行参数
    while ((opt = getopt_long(argc, argvopt, "p:q:T:",
                  lgopts, &option_index)) != EOF) {

        //命令行有三个参数, p:portmask,n:nqueue,T:timer period 
        switch (opt) {
        /* portmask */
        //port的个数,以十六进制表示的,如0x0f表示15
        case p:
            //l2fwd_parse_portmask()函数为自定义函数
            //把十六进制的mask字符串转换成unsigned long。
            //端口使能情况
            l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
            if (l2fwd_enabled_port_mask == 0) {  //mask为0表示出错
                printf("invalid portmask\n");
                l2fwd_usage(prgname);   //打印用户选项,类似于 --help
                return -1;    //参数传递错误,会返回-1,其结果是退出程序
            }
            break;

        /* nqueue */
        //队列的个数,同样也是用十六进制字符串表示的。
        case q:
            //每个核心配置几个 rx 队列。
            l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
            if (l2fwd_rx_queue_per_lcore == 0) {   //lcore 0表示出错
                printf("invalid queue number\n");
                l2fwd_usage(prgname);
                return -1;   //返回-1,其结果是退出程序。
            }
            break;

        /* timer period */  //定时器周期
        
        case T:
            //配置定时器周期,单位为秒
            //即l2fwd运行多少秒,使用十六进制表示的。
            timer_secs = l2fwd_parse_timer_period(optarg);
            if (timer_secs < 0) {
                printf("invalid timer period\n");
                l2fwd_usage(prgname);
                return -1;
            }
            //timer_period表示二层转发测试时间,默认为10秒,可通过-T来调节时间
            timer_period = timer_secs;
            break;

        /* long options */
        //--打头的选项不处理
        case 0:
            break;

        default:
            //参数传递错误,打印--help
            l2fwd_usage(prgname);
            return -1;
        }
    }

    if (optind >= 0)
        argv[optind-1] = prgname;

    ret = optind-1;
    optind = 0; /* reset getopt lib */
    return ret;
}

/* Check the link status of all ports in up to 9s, and print them finally */
static void
check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
{
#define CHECK_INTERVAL 100 /* 100ms */
#define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
    uint8_t portid, count, all_ports_up, print_flag = 0;
    struct rte_eth_link link;

    printf("\nChecking link status");
    fflush(stdout);
    for (count = 0; count <= MAX_CHECK_TIME; count++) {
        if (force_quit)
            return;
        all_ports_up = 1;
        for (portid = 0; portid < port_num; portid++) {
            if (force_quit)
                return;
            if ((port_mask & (1 << portid)) == 0)
                continue;
            memset(&link, 0, sizeof(link));
            rte_eth_link_get_nowait(portid, &link);
            /* print link status if flag set */
            if (print_flag == 1) {
                if (link.link_status)
                    printf("Port %d Link Up - speed %u "
                        "Mbps - %s\n", (uint8_t)portid,
                        (unsigned)link.link_speed,
                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
                    ("full-duplex") : ("half-duplex\n"));
                else
                    printf("Port %d Link Down\n",
                        (uint8_t)portid);
                continue;
            }
            /* clear all_ports_up flag if any link down */
            if (link.link_status == ETH_LINK_DOWN) {
                all_ports_up = 0;
                break;
            }
        }
        /* after finally printing all link status, get out */
        if (print_flag == 1)
            break;

        if (all_ports_up == 0) {
            printf(".");
            fflush(stdout);
            rte_delay_ms(CHECK_INTERVAL);
        }

        /* set the print_flag if all ports up or timeout */
        if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
            print_flag = 1;
            printf("done\n");
        }
    }
}

static void
signal_handler(int signum)
{
    if (signum == SIGINT || signum == SIGTERM) {
        printf("\n\nSignal %d received, preparing to exit...\n",
                signum);
        force_quit = true;    //强制退出按钮为真
    }
}

int
main(int argc, char **argv)
{
    /*
    //程序前面自定义的结构
    // __rte_cache_aligned 是一个宏,表示内存对其
    struct lcore_queue_conf {
        unsigned n_rx_port;        //rx_port个数
        unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];    //rx_port列表
    } __rte_cache_aligned;
    struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
    //一个计算机,包含多个核心,一个核心处理多个rx_port
    */
    struct lcore_queue_conf *qconf;    
    
    
    struct rte_eth_dev_info dev_info;    //设备信息
    int ret;                            //返回值
    uint8_t nb_ports;                    //总port个数
    uint8_t nb_ports_available;            //可用port个数
    uint8_t portid, last_port;            //当前portid,前一个portid。
    unsigned lcore_id, rx_lcore_id;        //核心id,rx_lcore_id
    unsigned nb_ports_in_mask = 0;        //??

    /* init EAL */
    //初始化环境
    ret = rte_eal_init(argc, argv);
    if (ret < 0)
        rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
    argc -= ret;    //??未知,貌似没用,不明所以
    argv += ret;    //??未知,貌似没用,不明所以

    force_quit = false;                    //强制推出按钮为假
    //当捕获到SIGINT或SIGTERM信号时,强制推出按钮为真,详见signal_handler
    //signal_handler为自定义函数
    signal(SIGINT, signal_handler);        //信号捕获处理1
    signal(SIGTERM, signal_handler);    //信号捕获处理2

    /* parse application arguments (after the EAL ones) */
    //解析传参(必须在eal_init之后),此为自定义函数,详见函数定义
    ret = l2fwd_parse_args(argc, argv);
    if (ret < 0)
        rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");

    printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");

    /* convert to number of cycles */
    //获取定时器的时钟周期
    timer_period *= rte_get_timer_hz();

    /* create the mbuf pool */
    //创建缓存池,用于存储数据包。
    l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF,
        MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
        rte_socket_id());
    if (l2fwd_pktmbuf_pool == NULL)
        rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");

    //获取Eth口的个数
    nb_ports = rte_eth_dev_count();
    if (nb_ports == 0)
        rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");

    /* reset l2fwd_dst_ports */
    //复位(清0)目的端口,此代码块作用是初始化l2fwd_dst_ports
    //个人认为写的比较low
    for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
        l2fwd_dst_ports[portid] = 0;
    last_port = 0;

    /* 
     * Each logical core is assigned a dedicated TX queue on each port.
     */
    //设置每个 port 接收的数据包要 转发 到的目的port。
    for (portid = 0; portid < nb_ports; portid++) {
        /* skip ports that are not enabled */
        //跳过没使能的端口
        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
            continue;

        //每两个port为一对,互相作二层转发
        //单数发到单数+1,双数发到双数-1
        if (nb_ports_in_mask % 2) {
            l2fwd_dst_ports[portid] = last_port;
            l2fwd_dst_ports[last_port] = portid;
        }
        else
            last_port = portid;  //对last_port赋值。

        nb_ports_in_mask++;   //对标记 ++

        rte_eth_dev_info_get(portid, &dev_info);   //获取对应portid的dev信息
    }
    //如果剩下一个网口,则该网口自己转发给自己
    if (nb_ports_in_mask % 2) {   
        printf("Notice: odd number of ports in portmask.\n");
        l2fwd_dst_ports[last_port] = last_port;
    }

    rx_lcore_id = 0;   
    qconf = NULL;      

    //初始化每个核心的队列配置
    /* Initialize the port/queue configuration of each logical core */
    //遍历所有port
    for (portid = 0; portid < nb_ports; portid++) {
        /* skip ports that are not enabled */
        //跳过没使能的端口
        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
            continue;

        /* get the lcore_id for this port */
        //port分配一个 lcore_id。
        //rte_lcore_is_enabled(rx_lcore_id) 测试core是否使能
        //lcore_queue_conf是一个自定义的结构,包含port个数和portid
        //意思是一个核心,对多个port的rx队列
        //在这里,一个核心只能匹配一个port
        while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
               lcore_queue_conf[rx_lcore_id].n_rx_port ==
               l2fwd_rx_queue_per_lcore) {
            rx_lcore_id++;
            if (rx_lcore_id >= RTE_MAX_LCORE)
                rte_exit(EXIT_FAILURE, "Not enough cores\n");
        }

        //赋值qconf,qconf最开始为NULL
        if (qconf != &lcore_queue_conf[rx_lcore_id])
            /* Assigned a new logical core in the loop above. */
            qconf = &lcore_queue_conf[rx_lcore_id];

        //当前lcore添加一个port
        //当前lcore管理的rx_port总数+1
        qconf->rx_port_list[qconf->n_rx_port] = portid;
        qconf->n_rx_port++;
        printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
    }

    //使能的port总数
    nb_ports_available = nb_ports;

    /* Initialise each port */
    //获取可用的port个数
    for (portid = 0; portid < nb_ports; portid++) {
        /* skip ports that are not enabled */
        //不可用的会剪掉。
        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
            printf("Skipping disabled port %u\n", (unsigned) portid);
            nb_ports_available--;
            continue;
        }
        /* init port */
        //初始化port
        printf("Initializing port %u... ", (unsigned) portid);
        fflush(stdout);
        //配置port,其中port_conf是一个结构,包含关于port的配置
        //vlan分离,crc硬件校验,认为这两个有用。
        ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
                  ret, (unsigned) portid);
    
        //获取port的mac地址
        //l2fwd_ports_eth_addr[] 的 类型为一个结构,是全局数组
        rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);

        /* init one RX queue */
        //初始化RX队列
        fflush(stdout);
        //rx队列会绑定到当前port上,l2fwd_pktmbuf_pool为内存池
        ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
                         rte_eth_dev_socket_id(portid),
                         NULL,
                         l2fwd_pktmbuf_pool);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
                  ret, (unsigned) portid);

        /* init one TX queue on each port */
        //初始化tx队列
        fflush(stdout);
        //tx队列会绑定到port上
        ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
                rte_eth_dev_socket_id(portid),
                NULL);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
                ret, (unsigned) portid);

        /* Initialize TX buffers */
        //malloc tx缓存,tx_buffer为指针数组,每个port对应一个tx缓存
        tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
                RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
                rte_eth_dev_socket_id(portid));
        if (tx_buffer[portid] == NULL)
            rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
                    (unsigned) portid);
        
        //初始化tx缓存
        rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);

        //tx缓存满了的时候,设置回调函数
        ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
                rte_eth_tx_buffer_count_callback,
                &port_statistics[portid].dropped);
        if (ret < 0)
                rte_exit(EXIT_FAILURE, "Cannot set error callback for "
                        "tx buffer on port %u\n", (unsigned) portid);

        /* Start device */
        //启动设备
        ret = rte_eth_dev_start(portid);
        if (ret < 0)
            rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
                  ret, (unsigned) portid);

        printf("done: \n");

        //设置网卡为混杂模式
        rte_eth_promiscuous_enable(portid);

        //打印port的MAC地址
        printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
                (unsigned) portid,
                l2fwd_ports_eth_addr[portid].addr_bytes[0],
                l2fwd_ports_eth_addr[portid].addr_bytes[1],
                l2fwd_ports_eth_addr[portid].addr_bytes[2],
                l2fwd_ports_eth_addr[portid].addr_bytes[3],
                l2fwd_ports_eth_addr[portid].addr_bytes[4],
                l2fwd_ports_eth_addr[portid].addr_bytes[5]);

        /* initialize port stats */
        //初始化port属性
        memset(&port_statistics, 0, sizeof(port_statistics));
    }

    //如果可以使能的网卡为0个,则报错。
    if (!nb_ports_available) {
        rte_exit(EXIT_FAILURE,
            "All available ports are disabled. Please set portmask.\n");
    }

    //检查所有port的link属性
    check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);

    ret = 0;
    /* launch per-lcore init on every lcore */
    //启动所有的lcore,回调l2fwd_launch_one_lcore函数,参数为NULL
    rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER);
    //遍历所有lcore_id
    RTE_LCORE_FOREACH_SLAVE(lcore_id) {
        //等所有线程结束
        if (rte_eal_wait_lcore(lcore_id) < 0) {
            ret = -1;
            break;
        }
    }

    //停止并且关闭所有的port
    for (portid = 0; portid < nb_ports; portid++) {
        if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
            continue;
        printf("Closing port %d...", portid);
        rte_eth_dev_stop(portid);  
        rte_eth_dev_close(portid);
        printf(" Done\n");
    }
    printf("Bye...\n");

    return ret;
}

 

以上是关于DPDK二层转发示例源码阅读的主要内容,如果未能解决你的问题,请参考以下文章

dpdk实例flow_classify

dpdk实例flow_classify

dpdk实例flow_classify

dpdk20.11.1学习-2.skeleton

dpdk20.11.1学习-2.skeleton

dpdk20.11.1学习-2.skeleton