dpdk--helloworld源码+注释

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>

#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_launch.h>
#include <rte_eal.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_debug.h>

/**
 * 回调函数
 * __attribute__((unused))的作用
 * 表明函数或变量可能不被使用,可避免gcc编译时的警告信息
 * 此处用来修饰void *arg,表示在lcore_hell()中,arg变量可能不被使用
 */
static int
lcore_hello(__attribute__((unused)) void *arg)
{
        unsigned lcore_id;
        lcore_id = rte_lcore_id();                      //获取lcore的ID
        printf("hello from core %u\n", lcore_id);       //打印信息
        return 0;
}

int
main(int argc, char **argv)
{
        int ret;                //返回值
        unsigned lcore_id;      //逻辑核心ID

        /**
         * 运行环境初始化
         * 通过传递argc、argv来初始化DPDK运行环境
         * /lib/librte_eal/common/eal_common_options.c中有对环境变量参数的详解
         * -c 线程数掩码
         * rte_eal_init()函数中,包含如下操作:
         *      配置初始化
         *      内存初始化
         *      内存池初始化
         *      队列初始化
         *      告警初始化
         *      中断初始化
         *      PCI初始化
         *      定时器初始化
         *      检测内存本地化(NUMA)
         *      插件初始化
         *      主线程初始化
         *      轮询设备初始化
         *      建立主从线程通道
         *      将从线程设置在等待模式
         *      PCI设备的探测与初始化
         */
        ret = rte_eal_init(argc, argv);
        if (ret < 0)
                rte_panic("Cannot init EAL\n");

        /* call lcore_hello() on every slave lcore */
        /**
         * RTE_LCORE_FOREACH_SLAVE(lcore_id)
         * 遍历所有EAL指定可以使用的子lcore(logic core逻辑核)
         * rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
         * 在每个核心上,回调lcore_hello函数
         */
        RTE_LCORE_FOREACH_SLAVE(lcore_id) {
                printf("%d\n", lcore_id);
                rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
        }

        /* call it on master lcore too */
        //在主lcore上调用lcore_hello()函数
        lcore_hello(NULL);

        //等待所有执行单元结束
        rte_eal_mp_wait_lcore();

        return 0;
}

 

posted @ 2017-06-07 09:39  宁静淡泊  阅读(874)  评论(0编辑  收藏  举报