DPDK helloworld 解析

1. 学习目的

        计划通过学习 DPDK 官方提供的 demo, 对 DPDK API 的使用有一些了解,helloworld 程序是其中最简单的程序,还是实际上手学习能更快一些。

2. 编译 helloworld 源码

环境变量设置:

  • export RTE_SDK=/home//dpdk/dpdk-stable-19.08.2/
  • export RTE_TARGET=x86_64-native-linux-gcc

源码路径:./dpdk 源码/examples/helloworld/

编译方法:在上面路径下执行 make

目标文件路径:./dpdk 源码/examples/helloworld/build/helloworld

./helloworld/helloword 
------------------------------------------------------------------------
EAL: Detected 8 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: Probing VFIO support...
EAL: VFIO support initialized
hello from core 1
hello from core 2
hello from core 3
hello from core 4
hello from core 5
hello from core 6
hello from core 7
hello from core 0

3. DPDK API 学习

通过上面结果可以看出,hello 被输出了 8 次,通过查看源码发现,每个 cpu 都注册了一个 lcore_hello 函数,

也就表明当前运行程序的设备 CPU 是 8 核心的。

源码核心 API 有两个: rte_eal_init() 、rte_eal_remote_launch()

3.1. rte_eal_init()

#include <rte_eal.h>
int rte_eal_init(int argc, char *argv[]);
//作用:用于初始化DPDK的环境。

3.2. rte_eal_remote_launch()

#include <rte_launch.h>
int rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned int slave_id);
//作用:DPDK中用于在远程核心上启动指定函数的函数。
//参数:
//f   : 函数指针
//arg : 函数的参数
//slave_id: 远程核心的编号

4. 源代码

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

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

static int lcore_hello(__attribute__((unused)) void *arg)
{
        unsigned lcore_id;
        lcore_id = rte_lcore_id();
        printf("hello from core %u\n", lcore_id);
        return 0;
}

int main(int argc, char **argv)
{
        int ret;
        unsigned lcore_id;

        // EAL 环境初始化
        ret = rte_eal_init(argc, argv);
        if (ret < 0)
                rte_panic("Cannot init EAL\n");

        /* 把lcore_hello函数注册给每一个cpu core */
        /* RTE_LCORE_FOREACH_SLAVE 这个宏会变量所以的核心*/
        RTE_LCORE_FOREACH_SLAVE(lcore_id) {
                rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
        }

        /* 主线程也调用一次lcore_hello */
        lcore_hello(NULL);

        rte_eal_mp_wait_lcore();
        return 0;
}
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值