这个是最简单的使用dpdk开发套件的例程。
源码分析:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/queue.h>
//以上头开发环境glibc的相关头文件
#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>
/*以上为dpdk自己编写的一些公共库头文件,如内存池、线程、抽象环境等工具,DPDK有自己的开发风格,适应之。*/
static int
lcore_hello(__attribute__((unused)) void *arg)/* 此处有一个字节对齐操作,此处不做详细分析。*/
{
unsigned lcore_id;
lcore_id = rte_lcore_id(); //获取逻辑核编号,并输出逻辑核id,返回,线程退出。
printf("hello from core %u\n", lcore_id);
return 0;
}
int
main(int argc, char **argv)
{
int ret;
unsigned lcore_id;
/* 相关初始化工作,如命令含参数处理,自动检测环境相关条件。以及相关库平台初始化工作*/
ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_panic("Cannot init EAL\n");
/* 每个从逻辑核调用回调函数lcore_hello输出相关信息。 */
RTE_LCORE_FOREACH_SLAVE(lcore_id) {
rte_eal_remote_launch(lcore_hello, NULL, lcore_id);
}
/* 再次调用主逻辑核输出相关信息。 */
lcore_hello(NULL);
/* 等待所有从逻辑核调用返回,相当于主线程阻塞等待。*/
rte_eal_mp_wait_lcore();
return 0;
}
通用makefile
#判断相关环境变量是否设置
ifeq ($(RTE_SDK),)
$(error "Please define RTE_SDK environment variable")
endif
# 默认的平台目标
RTE_TARGET ?= x86_64-native-linuxapp-gcc
include $(RTE_SDK)/mk/rte.vars.mk
# binary name
APP = helloworld
# all source are stored in SRCS-y
SRCS-y := main.c
CFLAGS += -O3
CFLAGS += $(WERROR_FLAGS)
include $(RTE_SDK)/mk/rte.extapp.mk
可带参数运行:
./build/helloworld -c COREMASK -n NUM
解释:coremask:十六进制掩码表示分配的逻辑内核(例如1111表示f代表四个逻辑核同时运行);m:内存通道数。
也可以通过--master-lcore=1,指定lcore1为主线程。
如下输入./helloworld -c f -n 2 --master-lcore=2输出情况:
[root@localhost build]# ./helloworld -c f -n 2 --master-lcore=2
EAL: Detected 4 lcore(s)
EAL: Detected 1 NUMA nodes
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: PCI device 0000:02:01.0 on NUMA socket -1
EAL: Invalid NUMA socket, default to 0
EAL: probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:02.0 on NUMA socket -1
EAL: Invalid NUMA socket, default to 0
EAL: probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:06.0 on NUMA socket -1
EAL: Invalid NUMA socket, default to 0
EAL: probe driver: 8086:100f net_e1000_em
EAL: PCI device 0000:02:07.0 on NUMA socket -1
EAL: Invalid NUMA socket, default to 0
EAL: probe driver: 8086:100f net_e1000_em
hello from core 0
hello from core 1
hello from core 3
hello from core 2
如下是对参数带参数运行的解释:
[root@dev build]# ./helloworld --help
EAL: Detected lcore 0 as core 0 on socket 0
EAL: Detected lcore 1 as core 1 on socket 0
EAL: Support maximum 128 logical core(s) by configuration.
EAL: Detected 2 lcore(s)
Usage: ./helloworld [options]
EAL common options:
-c COREMASK 逻辑核16进制掩码
-l CORELIST 列出运行时逻辑核列表
参数格式 <c1>[-c2][,c3[-c4],...]
where c1, c2, etc are core indexes between 0 and 128
--lcores COREMAP 映射逻辑核到物理逻辑核集合中
The argument format is
'<lcores[@cpus]>[<,lcores[@cpus]>...]'
lcores and cpus list are grouped by '(' and ')'
Within the group, '-' is used for range separator,
',' is used for single number separator.
'( )' can be omitted for single element group,
'@' can be omitted if cpus and lcores have the same value
--master-lcore ID 指定主线程逻辑核id
-n CHANNELS 指定内存通道数
-m MB 指定内存分配 (类似 --socket-mem)
-r RANKS Force number of memory ranks (don't detect) 强制内存参数
-b, --pci-blacklist 将PCI网络设备列入黑名单,防止EAL环境使用这些PCI设备,参数格式为<domain:bus:devid.func>
-w, --pci-whitelist 将PCI网络设备列入白名单,仅仅用指定的PCI设备,参数格式为<[domain:]bus:devid.func>
--vdev 添加一块虚拟设备,这个参数格式为 <driver><id>[,key=val,...]
(例如: --vdev=eth_pcap0,iface=eth2).
-d LIB.so|DIR 添加驱动活驱动目录(can be used multiple times)
--vmware-tsc-map Use VMware TSC map instead of native RDTSC
--proc-type 进程的类型 (primary|secondary|auto)
--syslog 设定syslog日志
--log-level 设定默认日志级别
-v 启动时显示版本信息
-h, --help This help
EAL options for DEBUG use only: 调试
--huge-unlink 在初始化后去掉大页面文件连接
--no-huge 用 malloc 代替 hugetlbfs
--no-pci 关闭 PCI
--no-hpet 关闭 HPET
--no-shconf 不共享配置(mmap'd files)
EAL Linux options: 选项
--socket-mem 内存分配
--huge-dir 大页面挂载目录
--file-prefix 页表文件前缀
--base-virtaddr 虚拟地址基址
--create-uio-dev Create /dev/uioX (usually done by hotplug)
--vfio-intr Interrupt mode for VFIO (legacy|msi|msix)
--xen-dom0 Support running on Xen dom0 without hugetlbfs</span>