DPDK-20.11.1版本在Centos8上安装和测试
一 安装前提条件
uname -a
Kernel version >= 2.6.34
ldd --version
glibc >=2.7
我在虚拟机安装的是centos8.3 版本,里面的glibc只到2.18,需要升级。
“说明:这个条件有点不靠谱,特别是要求glibc>=2.7我用的8.3版本的centos安装的, centos默认的glibc版本是2.28,还是可以安装,害的我升级glibc把系统都搞挂了,重装的.
二 编译安装DPDK
2.1 编译工具安装meson和ninja
pip3 install meson ninja
2.2 numa库安装
yum install -y numactl numactl-devel
2.3 gcc工具套件
dnf install -y gcc-toolset-9
2.4 编译DPDK
tar xJf dpdk-20.11.1.tar.xz
cd dpdk-stable-20.11.1/
#指定同时编译例子 (https://blog.csdn.net/force_eagle/article/details/110270131)
#更复杂的配置见上面链接
meson -Dexamples=all build
# 编译安装
cd build
ninja install
很棒,顺利的安装好了,如下:
Installing /root/soft/dpdk-stable-20.11.1/usertools/dpdk-devbind.py to /usr/local/bin
Installing /root/soft/dpdk-stable-20.11.1/usertools/dpdk-pmdinfo.py to /usr/local/bin
Installing /root/soft/dpdk-stable-20.11.1/usertools/dpdk-telemetry.py to /usr/local/bin
Installing /root/soft/dpdk-stable-20.11.1/usertools/dpdk-hugepages.py to /usr/local/bin
Installing /root/soft/dpdk-stable-20.11.1/build/rte_build_config.h to /usr/local/include
Installing /root/soft/dpdk-stable-20.11.1/build/meson-private/libdpdk-libs.pc to /usr/local/lib64/pkgconfig
Installing /root/soft/dpdk-stable-20.11.1/build/meson-private/libdpdk.pc to /usr/local/lib64/pkgconfig
Running custom install script '/bin/sh /root/soft/dpdk-stable-20.11.1/config/../buildtools/symlink-drivers-solibs.sh lib64 dpdk/pmds-21.0'
运行测试,到build下面的examples目录,发现例子已经编译好了,运行下:
./dpdk-helloworld
EAL: Detected 4 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: No free hugepages reported in hugepages-2048kB
EAL: No available hugepages reported in hugepages-2048kB
EAL: No available hugepages reported in hugepages-1048576kB
EAL: FATAL: Cannot get hugepage information.
EAL: Cannot get hugepage information.
PANIC in main():
Cannot init EAL
5: [./dpdk-helloworld(_start+0x2e) [0x8aa86e]]
4: [/lib64/libc.so.6(__libc_start_main+0xf3) [0x7f78f76367b3]]
3: [./dpdk-helloworld() [0x4c2fd6]]
2: [./dpdk-helloworld(__rte_panic+0xba) [0x4d7710]]
1: [./dpdk-helloworld(rte_dump_stack+0x1b) [0xa1864b]]
已放弃 (核心已转储)
预料之中,运行失败,是因为没有设置大页内存.
三 配置大页内存
3.1 配置大页内存
大页内存,需要:libhugetlbfs库的支持,如果设置2MB的据说不需要,我还是安装真实环境来操作:
yum install libhugetlbfs
安装好后,设置大页内存:
vim /etc/default/grub
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024 "
添加的内容:
transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024
大页内存每页大小为2M,一共设置1024页面,即2GB,根据自己主机内存情况进行设置,如果内存多的,CPU又支持1GB的大页内存,可以每页设置1GB的大页内存.
// 重新生成 grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
3.2 验证大页内存
重启后通过如下命令查看是否分配成功:
[root@localhost ~]# cat /proc/meminfo |grep -i HugePages
AnonHugePages: 0 kB
ShmemHugePages: 0 kB
HugePages_Total: 1024
HugePages_Free: 1024
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
配置了1024个大页内存页面,每个大小为2048KB即2MB,一共大页内存大小为2GB.
3.3 大页内存挂载
大页内存除了指定动态库,一般要挂载到特定目录使用,命令如下:
[root@localhost ~]# mkdir -p /mnt/huge
[root@localhost ~]# mount -t hugetlbfs nodev /mnt/huge
设置系统启动后自动挂载:
vim /etc/fstab
#添加以下内容
#如果是1GB 则nodev /mnt/huge hugetlbfs pagesize=1GB 0 0
nodev /mnt/huge hugetlbfs defaults 0 0
#重启系统
reboot
四 运行测试程序
运行测测程序,结果如下,验证如下:
[root@localhost examples]# ./dpdk-helloworld
EAL: Detected 4 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'PA'
EAL: No available hugepages reported in hugepages-1048576kB
EAL: Probing VFIO support...
EAL: Invalid NUMA socket, default to 0
EAL: No legacy callbacks, legacy socket not created
hello from core 1
hello from core 2
hello from core 3
hello from core 0
注意到了这一句:EAL:
No available hugepages reported in hugepages-1048576kB
通过:cat /proc/meminfo
查看大页内存根本没有使用,为什么还是分配不了那? 网上查了下,透明大页会先从大页内存里面分配,没有了才使用4KB的页面, 查看大页内存的占用进程:
grep -e AnonHugePages /proc/*/smaps | awk '{ if($2>4) print $0} ' | awk -F "/" '{print $0; system("ps -fp " $3)} '
这个问题在我的系统中不存在,透明大页默认是关闭的:
cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
后面查了下,是报的1GB的大页内存无法分配,我这边根本没分配1GB的大页内存所以忽略.
五 加载驱动
5.1 VFIO驱动
真正用起来,还需要加载dpdk的驱动,大体上有两种驱动类型:VFIO和UIO,区别如下: 网上抄一段:
“
VFIO是一个可以安全地把设备I/O、中断、DMA等暴露到用户空间(userspace),从而可以在用户空间完成设备驱动的框架。用户空间直接设备访问,虚拟机设备分配可以获得更高的IO性能。 依赖于IOMMU. vfio-pci. 相比于UIO,VFIO更为强健和安全 来自:https://www.cnblogs.com/vancasola/p/9378970.html
5.1.1 BIOS设置
要使用VFIO还是挺繁琐的,需要在BIOS设置,虚拟机可以在关机后,做如下改动:重启主机后,通过命令查看:
dmesg | grep -e DMAR -e IOMMU
[ 0.001000] DMAR-IR: IOAPIC id 1 under DRHD base 0xfec10000 IOMMU 0
看到有IOMMU,且不是IOMMU Not found,说明设置成功.
5.2 内核设置
内核需要配置支持Intel® VT-x、VT-d,内核通过如下命令查看:
$ cat /proc/cmdline | grep iommu=pt
$ cat /proc/cmdline | grep intel_iommu=on
没发现则需要添加:
vim /etc/default/grub
#在下面行中添加:iommu=pt intel_iommu=on
GRUB_CMDLINE_LINUX=
现在变成了:
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet iommu=pt intel_iommu=on transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024 "
# 更新配置和重启
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
验证下:
[root@localhost ~]# cat /proc/cmdline | grep intel_iommu=on
BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-240.el8.x86_64 root=/dev/mapper/cl-root ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet iommu=pt intel_iommu=on transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024
[root@localhost ~]# cat /proc/cmdline | grep iommu=pt
BOOT_IMAGE=(hd0,msdos1)/vmlinuz-4.18.0-240.el8.x86_64 root=/dev/mapper/cl-root ro crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet iommu=pt intel_iommu=on transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024
5.3 加载VFIO驱动
-
加载驱动 modprobe vfio-pci enable_sriov=1
-
更换驱动
./dpdk-devbind.py --status
Network devices using kernel driver
===================================
0000:02:01.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' if=ens33 drv=e1000 unused=vfio-pci *Active*
0000:02:05.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' if=ens37 drv=e1000 unused=vfio-pci
[root@localhost usertools]# ./dpdk-devbind.py --bind=vfio-pci 02:05.0
[root@localhost usertools]# ./dpdk-devbind.py --status
Network devices using DPDK-compatible driver
============================================
0000:02:05.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' drv=vfio-pci unused=e1000
Network devices using kernel driver
===================================
0000:02:01.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' if=ens33 drv=e1000 unused=vfio-pci *Active*
-
权限修改 增加执行权限,后面是让非root用户也可以使用.
chmod +x /dev/vfio
chmod 0666 /dev/vfio/*
按照官方文档,需要执行:
[root@localhost usertools]# sudo sh echo 2> /sys/bus/pci/devices/0000\:02\:05.0/sriov_numvfs
-bash: /sys/bus/pci/devices/0000:02:05.0/sriov_numvfs: 权限不够
测试:
[root@localhost examples]# ./dpdk-helloworld -l 1-3 -n 3
EAL: Detected 4 lcore(s)
EAL: Detected 1 NUMA nodes
EAL: Detected static linkage of DPDK
EAL: Multi-process socket /var/run/dpdk/rte/mp_socket
EAL: Selected IOVA mode 'VA'
EAL: No available hugepages reported in hugepages-1048576kB
EAL: Probing VFIO support...
EAL: VFIO support initialized
EAL: Invalid NUMA socket, default to 0
EAL: Invalid NUMA socket, default to 0
EAL: 0000:02:05.0 VFIO group is not viable! Not all devices in IOMMU group bound to VFIO or unbound
EAL: Requested device 0000:02:05.0 cannot be used
EAL: Invalid NUMA socket, default to 0
EAL: 0000:02:06.0 VFIO group is not viable! Not all devices in IOMMU group bound to VFIO or unbound
EAL: Requested device 0000:02:06.0 cannot be used
EAL: No legacy callbacks, legacy socket not created
hello from core 2
hello from core 3
这一句:0000:02:06.0 VFIO group is not viable! Not all devices in IOMMU group bound to VFIO or unbound
看上去是VFIO的组不可用,有一个网卡没有加入到这个组. 重新绑定后,无法连网,但是还是一样的问题.
多方折腾,还是报这个错误,放弃这种驱动了:(.
5.2 UIO
这是DPDK支持的另一种驱动模式:
modprobe uio_pci_generic
直接绑定会失败:
[root@localhost usertools]# ./dpdk-devbind.py --bind=uio_pci_generic 0000:02:06.0
Error: bind failed for 0000:02:06.0 - Cannot bind to driver uio_pci_generic
UIO驱动下需要关闭IOMMU,重新更改下启动项:
vim /etc/default/grub
#删除IOMMU配置
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/cl-swap rd.lvm.lv=cl/root rd.lvm.lv=cl/swap rhgb quiet transparent_hugepage=never default_hugepagesz=2M hugepagesz=2M hugepages=1024 "
grub2-mkconfig -o /boot/grub2/grub.cfg
reboot
绑定仍然失败:
[root@localhost usertools]# ./dpdk-devbind.py --bind=uio_pci_generic 0000:02:05.0
Error: bind failed for 0000:02:05.0 - Cannot bind to driver uio_pci_generic
看下文档"如果启用了UEFI安全启动,则Linux内核可能会禁止在系统上使用UIO" 经过查看,查看不到文件说明是BIOS启动.
ll /sys/firmware/efi
排除这个故障后,继续看下dpdk的安装文档, 上面介绍可以通过 dpdk-kmods来安装dpdk自带的驱动:
#安装依赖库
yum install elfutils-libelf-devel kernel-devel-$(uname -r)
#下载
git clone https://github.com/atsgen/dpdk-kmod.git
# 安装
cd dpdk-kmod/scripts
sh install.sh
#加载驱动
modprobe igb_uio
modprobe uio
继续绑定下网卡:
[root@localhost usertools]# ./dpdk-devbind.py --bind=igb_uio 0000:02:05.0
[root@localhost usertools]# ./dpdk-devbind.py --status
Network devices using DPDK-compatible driver
============================================
0000:02:05.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' drv=igb_uio unused=e1000
Network devices using kernel driver
===================================
0000:02:01.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' if=ens33 drv=e1000 unused=igb_uio *Active*
终于绑定uio驱动类型成功. dpdk-l2fwd 也可以正常运行
./dpdk-l2fwd -c 0x3 -n 4 -- -q 1 -p 0x3
表示,分配给2个逻辑内核,每个内核分别有1个收发队列,而一共分配了2个端口。注意这里面需要两个网卡,队列相互转发.
Lcore 0: RX port 0 TX port 1
Lcore 1: RX port 1 TX port 0
示意图如下,即为二层转发,-q 为一个内核可以处理几个队列,-p后面的0x3 对应两个网卡的掩码.
“BIOS与UEFI有什么区别?两者都可初始化计算机,任务是加载操作系统。BIOS通过读取硬盘上的第一个扇区(主引导记录(MBR))并执行它来启动。相比之下,UEFI通过从硬盘上的分区(称为EFI系统分区(ESP))加载EFI程序文件(使用.efi文件扩展名)来启动。
参考
[DPDK安装参考1](https://blog.csdn.net/Q93068/article/details/110581617)
[DPDK安装参考2](https://www.cnblogs.com/vancasola/p/9378970.html)
[DPDK安装参考3](http://blog.hexzone.cn/code/linux/2020-12/236/)
大页内存:[大页内存](https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt)