dpdk环境搭建+创建dpdk项目,并连接dpdk库

主要参照文章:https://www.yuque.com/zzqcn/opensource/ik2tg2

新的编译方式

DPDK 较新版本已经支持 meson+ninja 的编译方式, 而在 20.11 应该就要抛弃上述老的编译方式. 其中我个人理解,  meson 相当于 CMake, ninja 相当于 make.

依赖项

有些依赖项是和编译方式没有关系的, 比如对 make, gcc, Python, NUMA库等的依赖, 详见:https://doc.dpdk.org/guides/linux_gsg/sys_reqs.html#compilation-of-the-dpdk

如果使用新编译方式, 需要依赖:

meson 0.47.1+
ninja 1.7+

通过系统包管理器也可以安装 meson/ninja, 但一般版本很老. 比如 Ubuntu 16.04 用 apt 安装的话 meson 版本是 0.29.0, 远不能满足需求. 这时可以使用 pip3 来安装:

$ sudo pip3 install meson
$ sudo pip3 install ninja

编译DPDK

与 CMake 类似, 调用 meson 跟一个编译输出目录, 这目录可以放在源码目录之外, 避免污染. 本文中我把它设置为源码上一级目录的 dpdk_build/ 目录.

dpdk源码下载再地址:https://core.dpdk.org/download/,可以下载最新版使用

进入 DPDK 源码主目录, 运行:

$ meson <options> ../dpdk_build

输出:

zzq@ubuntu16:~/dev/dpdk (main)
$meson ../dpdk_build
The Meson build system
Version: 0.55.1
Source dir: /home/zzq/dev/dpdk
Build dir: /home/zzq/dev/dpdk_build
Build type: native build
Program cat found: YES
Project name: DPDK
Project version: 20.11.0-rc0
C compiler for the host machine: cc (gcc 5.4.0 "cc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609")
C linker for the host machine: cc ld.bfd 2.26.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program pkg-config found: YES
Program gen-pmdinfo-cfile.sh found: YES
Program list-dir-globs.py found: YES
...
Build targets in project: 929

Found ninja-1.10.0.git.kitware.jobserver-1 at /home/zzq/.local/bin/ninja

这里可以在 <options> 中设置编译选项, 也可以在运行此命令后在 ../dpdk_build 中再进行设置. 详见下文: 编译选项的设置. 设置好编译选项后, 在 ../dpdk_build 目录开始编译:

$ cd ../dpdk_build/
$ meson configure -Dbuildtype=debug -Dexamples=l3fwd-graph
$ ninja

成功编译后编译输出目录内容如下:

zzq@ubuntu16:~/dev/dpdk_build 
$ ls
app/                   config/    lib/            meson-uninstalled/
build.ninja            doc/       meson-info/     rte_build_config.h
buildtools/            drivers/   meson-logs/     usertools/
compile_commands.json  examples/  meson-private/

编译成功后, 可以进行安装, 以便后续使用, 此时需要 root 权限.

$ sudo ninja install
$ sudo ldconfig

在centos8下,并不会链接成功,可以手动将dpdk的lib目录链接到ldconfig下

linux 下有两种添加加载库路径的方式:

1.修改环境变量:
   #export LD_LIBRARY_PATH=path_name

2.修改配置文件
  修改  /etc/ld.so.conf 的内容在最后添加库加载的新的路径即可.
  最后执行: #ldconfig 使配置生效.

目前使用的是第二种方案:
在目录/etc/ld.so.conf.d/下新建文件:dpdk-ling.conf
文件内容:
[root@localhost build]# cat /etc/ld.so.conf.d/dpdk-ling.conf 
/usr/local/src/dpdk/dpdk-20.11/build/lib
[root@localhost build]# 

设置库查找路径

ldconfig

默认会安装在 /usr/local/ 目录, 其中库文件会在 /usr/local/lib/x86_64-linux-gnu/ 下面, 执行 ldconfig 是为了让 ld.so 更新 cache, 这样依赖到它的应用程序运行时就可以找到它了.

在某些系统上, 如 Fedora/Redhat, /usr/local 并不在默认路径中, 这需要在运行 ldconfig 之前把库路径添加到 /etc/ld.so.conf.d/ 中的某个文件里 (比如, dpdk.conf)

可以通过 ldconfig -p 来检查安装后的 DPDK 库是不是已经在 cache 里了:

执行将上述dpdk的lib目录写入到ldconf配置中,可以用以下确认:

$ ldconfig -p | grep librte | wc -l   
286
$ ldconfig -p | grep librte_graph
        librte_graph.so.21 (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/librte_graph.so.21
        librte_graph.so (libc6,x86-64) => /usr/local/lib/x86_64-linux-gnu/librte_graph.so

 

 

 

pkg-config

后续如果用 meson 编译 DPDK 程序, 默认会使用 pkg-config 来寻找 DPDK 库, 而 DPDK 安装后会把对应的 libdpdk.pc 安装在某个目录, 如 /usr/local/lib/x86_64-linux-gnu/pkgconfig/libdpdk.pc, 我们需要确保这个路径 pkg-config 可以找到.

$pkg-config --variable pc_path pkg-config
/usr/local/lib/x86_64-linux-gnu/pkgconfig:
/usr/local/lib/pkgconfig:
/usr/local/share/pkgconfig:
/usr/lib/x86_64-linux-gnu/pkgconfig:
/usr/lib/pkgconfig:
/usr/share/pkgconfig

 

而在 CentOS 上, libdpdk.pc 的路径是 /usr/local/lib64/pkgconfig/libdpdk.pc, 而 pkg-config 的默认搜索路径是:

$pkg-config --variable pc_path pkg-config
/usr/lib64/pkgconfig:
/usr/share/pkgconfig

可以看到 libdpdk.pc 路径并不在查找路径中, 所以需要添加, 方法之一:

$export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig

最终, 可通过以下命令检查 pkg-config 设置是否正确, 如果正确会显示版本号:

$pkg-config --modversion libdpdk
20.11.0-rc5

 

失败则会显示以下类似信息:

$pkg-config --modversion libdpdk          
Package libdpdk was not found in the pkg-config search path.
Perhaps you should add the directory containing `libdpdk.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libdpdk' found

当在 meson 中使用此路径时, 可直接通过选项设置, 如:

$meson --pkg-config-path=/usr/local/lib64/pkgconfig <BUILD> 

设置编译选项

在新的编译系统里, 不是通过修改 config/ 下面的文件来修改配置, 而是通过指定编译选项. 编译选项有两种指定方式:

  • meson <options> ../dpdk_build 中的 options 里指定
  • 执行完 meson ../dpdk_build 之后, 在生成的目录 ../dpdk_build 里执行 meson configure <options> , 在 options 里指定

两种设置方式的语法都是 -D<option> , 比如:

  • 修改编译版本 -Dbuildtype=debug 
  • 修改最大 lcore 数定义 -Dmax_lcores=256 

 

其中第一种方式还支持 --<option> 语法, 如

meson --buildtype=debug ../dpdk_build 

 

DPDK 内置了很多程序示例(examples), 默认不会编译, 如果想要编译某个示例, 比如 l3fwd 和 ip_reassembly, 语法是: -Dexamples=l3fwd,ip_reassembly , 多个示例名用逗号分开. 也可以通过 -Dexamples=all 来编译所有当前编译选项所支持的示例程序.

编译DPDK应用程序

有两种方式. 下文通过把 DPDK 自带的示例 examples/helloworld 移到一个外部目录进行编译来说明.

当动态链接某些版本的 DPDK 库时, 可能会出现运行时找不到网口的错误, 但明明已经绑定了网卡. 这是因为在外部编译的 DPDK 程序没有链接相关的 PMD 驱动库, 问题类似于: https://www.yuque.com/zzqcn/opensource/wdr49w
不依赖 PMD 驱动的 DPDK 程序不受此影响, 因为默认链接会链接基本的 DPDK 动态库.
要解决此问题, 不论是用 Makefile 还是 meson, 都需要设法链接要用到的 PMD 驱动, 如 -lrte_net_ixgbe (早期库名是 -lrte_pmd_ixgbe) -lrte_mempool_ring

 

 

使用Makefile

DPDK 编译系统使用 pkg-config 来方便 Makefile 的编写. DPDK 安装后把 libdpdk.pc 文件放在(Ubuntu)  /usr/local/lib/x86_64-linux-gnu/pkgconfig/libdpdk.pc.

 

通过 pkg-config 以及 libdpdk.pc 的帮助, 不必在 Makefile 文件里费心思写 CFLAGS, LDFLAGS 等东西, 而是让 pkg-config 自动生成. DPDK 应用程序的 Makefile 一般都类似下面这样:

PKGCONF = pkg-config

CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
LDFLAGS += $(shell $(PKGCONF) --libs libdpdk)

$(APP): $(SRCS-y) Makefile
        $(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS)

 

可以看到 CFLAGS, LDFLAGS 是推断出来的. 像我这样把 helloword 示例代码移到另一个外部目录编译的话, 相当于编译自己写的一个外部 DPDK 程序, 直接 make 就可以了:

zzq@ubuntu16:~/dev/helloworld 
$ make
cc -O3 -include rte_config.h -march=native -I/usr/local/include main.c -o build/helloworld-shared  -L/usr/local/lib/x86_64-linux-gnu -Wl,--as-needed -lrte_node -lrte_graph -lrte_bpf -lrte_flow_classify -lrte_pipeline -lrte_table -lrte_port -lrte_fib -lrte_ipsec -lrte_vhost -lrte_stack -lrte_security -lrte_sched -lrte_reorder -lrte_rib -lrte_regexdev -lrte_rawdev -lrte_pdump -lrte_power -lrte_member -lrte_lpm -lrte_latencystats -lrte_kni -lrte_jobstats -lrte_ip_frag -lrte_gso -lrte_gro -lrte_eventdev -lrte_efd -lrte_distributor -lrte_cryptodev -lrte_compressdev -lrte_cfgfile -lrte_bitratestats -lrte_bbdev -lrte_acl -lrte_timer -lrte_hash -lrte_metrics -lrte_cmdline -lrte_pci -lrte_ethdev -lrte_meter -lrte_net -lrte_mbuf -lrte_mempool -lrte_rcu -lrte_ring -lrte_eal -lrte_telemetry -lrte_kvargs
ln -sf helloworld-shared build/helloworld

编译完成会在 build/ 下生成可执行文件.

如果动态链接 DPDK 库出现找不到网卡问题, 请在链接选项中添加类似以下项:

-L/usr/local/lib64 -lrte_net_ixgbe -lrte_mempool_ring 

使用meson

helloword 示例代码目录中已经有一个 meson.build 文件, 但这个文件是用于编译整个 DPDK 源码时编译 helloworld 用的, 而不是用于在外部单独编译它. 所以我们需要创建一个新的 meson 配置文件.

project('helloworld', 'c')

dpdk = dependency('libdpdk')
allow_experimental_apis = true
sources = files(
    'main.c'
)
executable('helloworld', sources, dependencies: dpdk)

 

语法我目前也不清楚, 但对于简单的示例来说够用了. // TODO

 

写好之后, 其余编译步骤就和编译 DPDK 一样了:

$ cd ~/dev/helloword/
$ meson build
$ cd build/
$ meson configure
$ ninja

编译好之后会在 build/ 下生成可执行文件.

 

如果动态链接 DPDK 库出现找不到网卡问题, 请参考以下写法:

project('l2fwd', 'c')

dpdk_dep = declare_dependency(
    dependencies: dependency('libdpdk'),
    link_args: [
        '-Wl,--no-as-needed',
        '-L/usr/local/lib64',
        '-lrte_net_vmxnet3',
        '-lrte_net_ixgbe',
        '-lrte_net_i40e',
        '-lrte_mempool_ring'
    ],
)

sources = files(
    'main.c'
)

executable('l2fwd',sources,
    dependencies: dpdk_dep
)

meson使用以及自建dpdk项目,可以参阅:

https://blog.csdn.net/doujiangbear/article/details/112797023

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值