http://laplace.in/?p=1
本段描述Intel® DPDK框架的源码组织结构。
注意: 在后续的描述中,环境变量RTE_SDK
指向解压压缩包时产生的基础目录。可以跳到构建系统提供的有用的变量 查看其他变量的描述。
DPDK提供的Makefile在$(RTE_SDK)/mk
目录中。
配置文件模板在$(RTE_SDK)/config
目录。这些模板描述了对应的生成目标的选项。配置文件中同时还包含了一些关于DPDK 的开关项,包括DEBUG选项。用户应该查看配置文件并熟悉其中的选项。配置文件同时用来生成头文件,这些头文件会被生成在 新的build文件夹内。
库文件放在$(RTE_SDK)/lib
目录中。习惯上,我们调用库的代码来提供API给应用程序。 通常,这里会产生一个静态库(.a),不过内核模块也在这个文件夹下。lib文件夹包括:
应用程序是指代码中含有main()
函数的程序。它们在$(RTE_SDK)/app
和$(RTE_SDK)/examples
目录中。
app
目录中是一些用来自动测试DPDK的简单程序。
examples
目录中则是一些显示如何使用库文件的样例程序。
注意: 实际的examples目录也许会有额外的应用程序,请查看最新的DPDK源码包。
DPDK需要一个构建系统来支持编译等活动。本节描述的是使用DPDK框架时的限制与机制。 其中有有两个用例:
下文提供了如何构建DPDK二进制文件的细节
安装完毕后,会有一个构建目录
被创建出来(译注:如下文中的i686-default-linuxapp-gcc)。每一个构建 目录都包括包含文件、库文件、应用程序:
一个构建目录对应一个包含了架构、执行环境、工具链的配置文件。相同的源码通过不同的配置文件 It is possible to have several build directories sharing the same sources with different configurations. 例如,想用默认的配置文件模板config/defconfig_x86_64-linuxapp
创建一个构建目录my_sdk_build_dir
,我们可以:
这会创建一个新目录my_sdk_build_dir
。然后我们就可以编译它了:
这等同于执行:
my_sdk_build_dir中的内容如下:
参阅根make文件帮助获得能从DPDK根目录下执行的make命令的一些细节。
本质上DPDK是一个开发套件,因此其第一要务是使用户能使用SDK创建应用程序。要想编译一个程序,用户必须 设置RTE_SDK
和RTE_TARGET
环境变量。
对于一个新的程序,用户必须创建自己的Makefile,这个Makefile可能包含一些.mk文件, 比如:${RTE_SDK}/mk/DPDK.vars.mk
和${RTE_SDK}/mk/DPDK.app.mk
。根据您选择的目标(包括架构、 机器、执行环境、工具链等,这些目标定义在Makefile中或作为一个环境变量),在编译应用程序和库文件时, 框架会选择合适的.h头文件和.a库文件。目标文件在${RTE_SDK}/arch-machine-execenv-toolchain
(译注:其中arch、machine、execenv、toolchain应该依据编译环境而改变,如arch可以为i686、x86_64, toolchain可以为gcc、icc等),它会被环境变量${RTE_BIN_SDK}
内在地引用。使用make
命令即可编译应 用程序。编译结果会保存在/path/to/my_app/build
目录。
examples
目录提供了一些示例程序
DPDK的Makefile永远依照如下的格式:
$(RTE_SDK)/mk/DPDK.vars.mk
$(RTE_SDK)/mk/DPDK.XYZ.mk
,其中XYZ可以是app、lib、extapp、extlib、 obj、gnuconfigure等,具体应该用哪一个,完全取决于你想创建的项目的类型。请参阅Makefile类型下面是一个非常简单的外部应用的Makefile示例:
用户创建的Makefile中最后包含的.mk文件决定了这个Makefile的角色。注意用同一个Makefile是不可 能既创建库文件又创建应用程序的。因此用户必须在不同的目录中创建两个独立的Makefile。任何情况下 用户都应该把rte.vars.mk
包含进来。
这些Makefile产生二进制应用。
创建.a静态库
RTE_SDK
: DPDK源码包的绝对路径。编译开发套件时框架会自动设置。当编译外部应用时,用户必须定义这个环境变量。RTE_SRCDIR
: 源码的路径。当编译开发套件时RTE_SRCDIR
等于RTE_SDK
。编译外部应用时则指向外部应用的源码目录。RTE_OUTPUT
: 输出文件的路径。通常情况下是$(RTE_SRCDIR)/build
,通过make命令的O=
选项可以重写该变量。RTE_TARGET
: 一个标识了编译目标的字符串。格式为arch-machine-execenv-toolchain
。编译SDK时是由构建系 统通过配置文件(.config)推理出来的。编译外部应用时则必须由用户在Makefile或环境变量中指定。RTE_SDK_BIN
: $(RTE_SDK)/$(RTE_TARGET)
的引用。RTE_ARCH
: 定义架构(i686, x86_64)。和CONFIG_RTE_ARCH
值相同,只是不需要双引号括起来。RTE_MACHINE
: 定义目标机器。和CONFIG_RTE_MACHINE
值相同,只是不需要双引号括起来。RTE_TOOLCHAIN
: 定义工具链(gcc、icc)。和CONFIG_RTE_TOOLCHAIN
值相同,只是不需要双引号括起来。RTE_EXEC_ENV
: 定义执行环境(如linuxapp)。和CONFIG_RTE_EXEC_ENV
值相同,只是不需要双引号括起来。RTE_KERNELDIR
: 这个变量包含了用来编译内核模块的内核源码的绝对路径。内核头文件必须和执行应用程序的目 标机器相匹配。默认情况下,这个变量设置为/lib/modules/$(shell uname -r)/build
。当编译机和目标机相同时, 这是正确的。 26.3.4 Variables that Can be Set/Overridden in a Makefile OnlyVPATH
: The path list that the build system will search for sources. By default, RTE_SRCDIR will be included in VPATH.CFLAGS
: Flags to use for C compilation. The user should use += to append data in this variable.LDFLAGS
: Flags to use for linking. The user should use += to append data in this variable.ASFLAGS
: Flags to use for assembly. The user should use += to append data in this variable.CPPFLAGS
: Flags to use to give flags to C preprocessor (only useful when assembling .S files). The user should use += to append data in this variable.LDLIBS
: In an application, the list of libraries to link with (for example, -L / path/to/libfoo -lfoo). The user should use += to append data in this variable.SRC-y
: A list of source files (.c, .S, or .o if the source is a binary) in case of application, library or object Makefiles. The sources must be available from VPATH.INSTALL-y-$(INSTPATH)
: A list of files to be installed in $(INSTPATH) . The files must be available from VPATH and will be copied in $(RTE_OUTPUT)/ $(INSTPATH) . Can be used in almost any RTE Makefile.SYMLINK-y-$(INSTPATH)
: A list of files to be installed in $(INSTPATH) . The files must be available from VPATH and will be linked (symbolically) in $(RTE_OUTPUT)/$(INSTPATH). This variable can be used in almost any Intel ®DPDK Makefile.PREBUILD
: A list of prerequisite actions to be taken before building. The user should use += to append data in this variable.POSTBUILD
: A list of actions to be taken after the main build. The user should use += to append data in this variable.PREINSTALL
: A list of prerequisite actions to be taken before installing. The user should use += to append data in this variable.POSTINSTALL
: A list of actions to be taken after installing. The user should use += to append data in this variable.PRECLEAN
: A list of prerequisite actions to be taken before cleaning. The user should use += to append data in this variable.POSTCLEAN
: A list of actions to be taken after cleaning. The user should use += to append data in this variable.DEPDIR-y
: Only used in the development kit framework to specify if the build of the current directory depends on build of another one. This is needed to support parallel builds correctly. 26.3.5 Variables that can be Set/Overridden by the User on the Command Line Only Some variables can be used to configure the build system behavior. They are documented in Development Kit Root Makefile Help and External Application/Library Makefile help.WERROR_CFLAGS
: By default, this is set to a specific value that depends on the compiler. Users are encouraged to use this variable as follows: CFLAGS += $(WERROR_CFLAGS) This avoids the use of different cases depending on the compiler (icc or gcc ). Also, this variable can be overridden from the command line, which allows bypassing of the flags for testing purposes. 26.3.6 Variables that Can be Set/Ove rridden by the User in a Makefile or Command LineCFLAGS_my_file.o
: Specific flags to add for C compilation of my_file.c.LDFLAGS_my_app
: Specific flags to add when linking my_app.NO_AUTOLIBS
: If set, the libraries provided by the framework will not be included in the LDLIBS variable automatically.EXTRA_CFLAGS
: The content of this variable is appended after CFLAGS when compiling.EXTRA_LDFLAGS
: The content of this variable is appended after LDFLAGS when linking.EXTRA_ASFLAGS
: The content of this variable is appended after ASFLAGS when assembling.EXTRA_CPPFLAGS
: The content of this variable is appended after CPPFLAGS when using a C preprocessor on assembly files.27.0 开发套件的根Makefile帮助
DPDK为目标提供根级的Makefile DPDK provides a root level Makefile with targets for configuration, building, cleaning, testing, installation and others. These targets are explained in the following sections. 27.1 配置目标 The configuration target requires the name of the target, which is specified using T=mytarget and it is mandatory. The list of available targets are in $(RTESDK)/ config (remove the defconfig prefix). Configuration targets also support the specific ation of the name of the output directory, using O=mybuilddir. This is an optional parameter, the default output directory is build. • config This will create a build directory, and generates a configuration from a template. A Makefile is also created in the new build directory. Example: make config O=mybuild T=x86_64-default-linuxapp-gcc
27.2 构建目标 Build targets support the optional specificat ion of the name of the output directory, using O=mybuilddir. The default output directory is build. • all , build or just make Build the Intel ® DPDK in the output directory previously created by a make config. Example: make O=mybuild • clean Clean all objects created using make build. Example: make clean O=mybuild • %_sub Build a subdirectory only, without managing dependencies on other directories. Example: make lib/librte_eal_sub O=mybuild • %_clean Clean a subdirectory only. Example: make lib/librte_eal_clean O=mybuild
27.3 安装目标 • install Build the Intel ® DPDK binary. Actually, this builds each supported target in a separate directory. The name of each directory is the name of the target. The name of the targets to install can be optionally specified using T=mytarget. The target name can contain wildcard characters. The list of available targets are in $(RTESDK)/config (remove the defconfig prefix). Example: make install T=x86_64- • uninstall Remove installed target directories.
27.4 测试目标 • test Launch automatic tests for a build directory specified using O=mybuilddir. It is optional, the default output directory is build. Example: make test O=mybuild • testall Launch automatic tests for all installed target directories (after a make install). The name of the targets to test can be optionally specified using T=mytarget. The target name can contain wildcard ( ) characters. The list of available targets are in $(RTESDK)/config (remove the defconfig prefix). Examples: make testall, make testall T=x86_64-
27.5 目标的文档 • doxydoc Generate the Doxygen documentation (pdf only).
27.6 目标的依赖
这个目标 This target is implicitly called by make config . Typically, there is no need for a user to call it, except if DEPDIRS-y variables have been updated in Makefiles. It will generate the file $(RTE_OUTPUT)/.depdirs. Example: make depdirs O=mybuild
This command generates a dot graph of dependencies. It can be displayed to debug circular dependency issues, or just to understand the dependencies. Example: make depgraph O=mybuild > /tmp/graph.dot && dotty /tmp/ graph.dot
27.7 目标杂项
显示帮助
27.8 其他有用的命令行变量
The following variables can be specified on the command line: • V= Enable verbose build (show full compilation command line, and some intermediate commands). • D= Enable dependency debugging. This provides some useful information about why a target is built or not. • EXTRA_CFLAGS= , EXTRA_LDFLAGS=, EXTRA_ASFLAGS=, EXTRA_CPPFLAGS= Append specific compilation, link or asm flags. • CROSS= Specify a cross toolchain header that will prefix all gcc/binutils applications. This only works when using gcc.
27.9 在构建目录make 以上描述的所有目标均在SDK的根$(RTE_SDK)
被调用。在构建目录执行相同的Makefile目标也是可行的。 举例来说:
这和下面的等价:
27.10 编译用于调试
编译包含调试信息并且优化级别为0的DPDK框架及其示例程序,需要在编译前将EXTRA_CFLAGS
变量设置为:
之后就可以像平常那样编译DPDK框架、示例程序甚至用户程序了:
本节将会记述一个开发者如何扩展DPDK,例如提供一个新库、一个新目标或者支持一个新目标。
给DPDK增加新库,过程如下:
foo()
函数。在foo.c中定义:
在foo.h中声明:
在其中添加:
将其中的librte_mempool
替换为libfoo
,rte_mempool
替换为foo
mk/DPDK.app.mk
,添加-lfoo
到LDLIBS
变量(确保其可用)。之后,当链接DPDK应用时就能自动使用这个编译选项了。测试程序用来验证所有的DPDK功能是否有效。一旦你添加了新的库,就必须在测试程序用添加新的用例。
test_foo.c
文件。它包含foo.h并且从test_foo()调用foo(),当测试通过时test_foo()应该返回0。autotest.py
是一个用来产生测试报告的脚本,它在${RTE_SDK}/doc/rst/test_report/autotests
目录下。这个脚本也应该更新。如果libfoo是一个新的测试族,${RTE_SDK}/doc/rst/test_report/test_report.rst
中 的链接也应该更新。 当编译示例程序(比如hello_world)时,RTE_SDK
和RTE_TARGET
韩晶变量必须被export。
默认情况下,二进制文件会在build文件夹下生成。
示例程序Hello World可以被复制到一个新的文件夹,以作为你开发的起始点:
示例程序hello world的默认Makefile是一个不错的起始点,它包括:
用户必须定义一些变量
用同样的方法也能编译成库文件
和编译应用唯一的不同是,用LIB变量替代APP变量,LIB变量的内容是库的名字,比如,libfoo.a。
一些变量可以用来自定义Makefile的行为。下面列出最常用的一些。请参阅构建系统提供的有用的变量 查看相关细节。
外部应用或库应该从RTE_SDK
导入指定的Makefile,这些文件位于mk文件夹:
下列变量必须定义:
${RTE_SDK}
: 指向DPDK的根目录${RTE_TARGET}
: 编译目标的引用(比如,x86_64-default-linuxapp-gcc
) 构建目标支持通过O=mybuilddir
选项指定输出文件夹,这是可选的。默认的输出目录是build。
make O=mybuild
make clean O=mybuild
以下变量可以在命令行中指定:
从其他文件夹执行Makefile也是可以的。通过指定源文件目录和输出文件目录即可实现:
1
2
3
|
export
RTE_SDK
=
/
path
/
to
/
DPDK
export
RTE_TARGET
=
x86_64
-
default
-
linuxapp
-
icc
make
-
f
/
path
/
to
/
my_app
/
Makefile
S
=
/
path
/
to
/
my
_app
O
=
/
path
/
to
/
build_dir
|