本篇主要介紹DPDK的簡單安裝使用和一點其他相關(guān)內(nèi)容,希望對初學者有所幫助。
本文打算寫的內(nèi)容:
- 對DPDK是什么和做什么用的簡單介紹
- CentOS和Ubuntu安裝DPDK的環(huán)境及流程
- 關(guān)于一個基于DPDK的很好用的報文發(fā)送工具 —— dpdk-pktgen的簡單安裝使用教程
1. 對DPDK的介紹
來自官方項目地址
DPDK is a set of libraries and drivers for fast packet processing.
It supports many processor architectures and both FreeBSD and Linux.
是一個用來進行快速報文處理的庫和驅(qū)動集,支持多核架構(gòu)以及FreeBSD和Linux。
評論指正:官方文檔中many processor architectures應(yīng)該是多種處理器架構(gòu)的意思
用途:用來綁定和優(yōu)化物理網(wǎng)卡驅(qū)動,提高其處理性能
DPDK相關(guān)技術(shù):
(不詳寫,具體建議Google)
- 大頁內(nèi)存:用來減少TLB miss,提高效率
- IGB UIO:用戶態(tài)的網(wǎng)卡驅(qū)動,用來取代原有的驅(qū)動,其中用戶態(tài)是DPDK的核心之一(話說我至今搞不明白IGB是什么的縮寫)
- 輪詢模式:用來取代收報文進行處理時產(chǎn)生的中斷,靠中斷的話CPU效率太低,所以采用輪詢來處理
- NUMA架構(gòu):Non Uniform Memory Access Architecture,建議Google
2. DPDK安裝
2.1. 環(huán)境配置
| 名稱 | 配置 |
|---|---|
| 操作系統(tǒng) | CentOS / Ubuntu |
| CPU | 8核 |
| 內(nèi)存 | 4G |
| 網(wǎng)卡 | 最好兩張以上 |
| 大頁內(nèi)存 | 最好512MB以上 |
| DPDK版本 | 越高坑越少 |
2.2. 影響因素
上面是個建議的運行配置,下面詳細說說
- 關(guān)于系統(tǒng)版本:一般沒太大影響,DPDK似乎會被內(nèi)核驅(qū)動的版本影響,但是我運行高版本的DPDK時沒遇到類似問題
- 關(guān)于CPU: CPU核數(shù)自然是越高越好,這直接牽扯到處理能力。但是實際使用DPDK時牽扯到一個綁定CPU給DPDK線程的問題,這個具體到那里時會有所介紹,這里只簡單表示:沒必要綁定多余的CPU給DPDK線程,性能反而有可能降低。
- 內(nèi)存:沒啥好說的,越高越好,內(nèi)存夠大才能分配足夠的大頁內(nèi)存,而大頁內(nèi)存又是直接影響DPDK性能的因素之一
- 網(wǎng)卡: DPDK綁定網(wǎng)卡之后原來的網(wǎng)絡(luò)驅(qū)動就沒用了,原先的協(xié)議棧中網(wǎng)絡(luò)層及IP地址之類的全都會失效,還想連網(wǎng)建議多搞個網(wǎng)卡
-
大頁內(nèi)存:越高越好,影響性能,低了DPDK甚至沒法正常用,可能報這個 ——
Cannot Allocate Memory - DPDK版本:現(xiàn)在都19.05了,除非是基于DPDK進行開發(fā)的開發(fā)者,版本改動可能影響較大,新接觸建議選擇高版本,坑少真的能省很多很多事情……
- 關(guān)于一些其他的可能的性能影響因素: 建議開啟CPU超頻,建議關(guān)掉防火墻,關(guān)掉藍牙等用不上的功能
2.3. 安裝過程
- 安之前最好安一下DPDK一些的依賴項,比較重要的:
# CentOS:
1: sudo yum install libnuma-devel
2: sudo yum install git
3: sudo yum install gcc
4: sudo yum install net-tools
# Ubuntu:
1: sudo apt install libnuma-dev
2: sudo apt install git
3: sudo apt install gcc
4: sudo apt install net-tools
其他還缺什么的話,建議根據(jù)到時候具體的錯誤信息進行查詢
- 安裝過程
1: ifconfig
2: sudo ens33 ifconfig down
3: cd /
4: git clone https://github.com/DPDK/dpdk.git
5: cd dpdk
6: export RTE_SDK = /dpdk
7: sudo ./usertools/dpdk-setup.sh
步驟1和2: 查看網(wǎng)卡信息,然后關(guān)了回頭要綁定給DPDK的網(wǎng)卡,不關(guān)綁不上的,綁不上用不了的,會提示你還沒綁網(wǎng)卡的
步驟6: 配置環(huán)境變量,下在哪配在哪,這里順帶一提,大部分基于DPDK開發(fā)的應(yīng)用都要配兩個特別重要的環(huán)境變量:RTE_SDK和RTE_TARGET,這倆一個代表DPDK下載地址,一個代表編譯后文件的生成地址。
步驟7: 運行DPDK安裝腳本,不建議自行一個個使用命令來安裝。
運行步驟7之后:
------------------------------------------------------------------------------
RTE_SDK exported as /root/dpdk
------------------------------------------------------------------------------
----------------------------------------------------------
Step 1: Select the DPDK environment to build
----------------------------------------------------------
[1] arm64-armv8a-linuxapp-clang
[2] arm64-armv8a-linuxapp-gcc
[3] arm64-armv8a-linux-clang
[4] arm64-armv8a-linux-gcc
[5] arm64-bluefield-linuxapp-gcc
[6] arm64-bluefield-linux-gcc
[7] arm64-dpaa2-linuxapp-gcc
[8] arm64-dpaa2-linux-gcc
[9] arm64-dpaa-linuxapp-gcc
[10] arm64-dpaa-linux-gcc
[11] arm64-octeontx2-linuxapp-gcc
[12] arm64-octeontx2-linux-gcc
[13] arm64-stingray-linuxapp-gcc
[14] arm64-stingray-linux-gcc
[15] arm64-thunderx2-linuxapp-gcc
[16] arm64-thunderx2-linux-gcc
[17] arm64-thunderx-linuxapp-gcc
[18] arm64-thunderx-linux-gcc
[19] arm64-xgene1-linuxapp-gcc
[20] arm64-xgene1-linux-gcc
[21] arm-armv7a-linuxapp-gcc
[22] arm-armv7a-linux-gcc
[23] i686-native-linuxapp-gcc
[24] i686-native-linuxapp-icc
[25] i686-native-linux-gcc
[26] i686-native-linux-icc
[27] ppc_64-power8-linuxapp-gcc
[28] ppc_64-power8-linux-gcc
[29] x86_64-native-bsdapp-clang
[30] x86_64-native-bsdapp-gcc
[31] x86_64-native-freebsd-clang
[32] x86_64-native-freebsd-gcc
[33] x86_64-native-linuxapp-clang
[34] x86_64-native-linuxapp-gcc
[35] x86_64-native-linuxapp-icc
[36] x86_64-native-linux-clang
[37] x86_64-native-linux-gcc
[38] x86_64-native-linux-icc
[39] x86_x32-native-linuxapp-gcc
[40] x86_x32-native-linux-gcc
----------------------------------------------------------
Step 2: Setup linux environment
----------------------------------------------------------
[41] Insert IGB UIO module
[42] Insert VFIO module
[43] Insert KNI module
[44] Setup hugepage mappings for non-NUMA systems
[45] Setup hugepage mappings for NUMA systems
[46] Display current Ethernet/Crypto device settings
[47] Bind Ethernet/Crypto device to IGB UIO module
[48] Bind Ethernet/Crypto device to VFIO module
[49] Setup VFIO permissions
----------------------------------------------------------
Step 3: Run test application for linux environment
----------------------------------------------------------
[50] Run test application ($RTE_TARGET/app/test)
[51] Run testpmd application in interactive mode ($RTE_TARGET/app/testpmd)
----------------------------------------------------------
Step 4: Other tools
----------------------------------------------------------
[52] List hugepage info from /proc/meminfo
----------------------------------------------------------
Step 5: Uninstall and system cleanup
----------------------------------------------------------
[53] Unbind devices from IGB UIO or VFIO driver
[54] Remove IGB UIO module
[55] Remove VFIO module
[56] Remove KNI module
[57] Remove hugepage mappings
[58] Exit Script
Option:
輸入對應(yīng)的命令數(shù)字就可以執(zhí)行操作了,執(zhí)行的順序是:
-
x86_64-native-linuxapp-gcc
選擇build的平臺,系統(tǒng)和編譯器,及x86_64平臺,linux系統(tǒng),c語言編譯器 -
Insert IGB UIO module
插入IGB UIO驅(qū)動,必須在編譯后才能正常運行,而它運行后才能順利執(zhí)行后面的操作 -
Setup hugepage mappings for NUMA systems
配置大頁內(nèi)存,選之后輸入數(shù)目就行了,一般大頁內(nèi)存分兩種,2M的和1G的 -
Bind Ethernet/Crypto device to IGB UIO module
把網(wǎng)卡綁定給UIO,詳細如下:
Option: 47
Network devices using DPDK-compatible driver
============================================
0000:02:01.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' drv=igb_uio unused=e1000
0000:02:06.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' drv=igb_uio unused=e1000
Network devices using kernel driver
===================================
0000:02:07.0 '82545EM Gigabit Ethernet Controller (Copper) 100f' if=ens39 drv=e1000 unused=igb_uio
No 'Crypto' devices detected
============================
No 'Eventdev' devices detected
==============================
No 'Mempool' devices detected
=============================
No 'Compress' devices detected
==============================
Enter PCI address of device to bind to IGB UIO driver:
假設(shè)你要綁定最上面那個,輸入02:01.0就可以了,綁定之前記得在ifconfig里面把它關(guān)了
-
Run testpmd application in interactive mode ($RTE_TARGET/app/testpmd)
從括號里就能看到DPDK自帶的app編譯完了都在RTE_TARGET這個路徑下面,這個是個DPDK的測試程序,可以檢測下DPDK安成功了沒 - 關(guān)于testpmd,直接看這里吧
3. dpdk - pktgen
這是一個基于DPDK開發(fā)的報文發(fā)送工具,能指定MAC地址和包大小讓它使用起來十分靈活
但是對于這個工具我個人還有許多沒搞懂的地方,大家使用時見仁見智吧~
直接丟安裝方法,先安裝依賴:
1: wget -c http://www.lua.org/ftp/lua-5.3.5.tar.gz
2: tar zxvf lua-5.3.5.tar.gz
3: cd lua-5.3.5
4: make linux
Ubuntu
5: sudo apt-get install libreadline5
6: sudo apt-get install libreadline-gplv2-dev
CentOS
5/6: yum install readline-devel
7: sudo make install
dpdk - pktgen是用lua語言做腳本的,所以需要下載lua和相關(guān)內(nèi)容
注意步驟7哪個系統(tǒng)都是要運行的
然后安裝dpdk - pktgen
1: export RTE_SDK=/root/dpdk
2: export RTE_TARGET=x86_64-native-linuxapp-gcc
3: cd $RTE_SDK
4: make install T=x86_64-native-linuxapp-gcc
5: cd /root/pktgen-dpdk
6: make
7: ./app/x86_64-linuxapp-gcc/pktgen -l 0-2 -n 3 -- -P -m "[1].0, [2].1"
8: set 0 dst mac <mac addr>
9: set 0 dst ip <ip addr>
10: set 0 size <size between 64 - 1518>
11: start 0 # str
其中步驟1-7是安裝過程,步驟8-11是簡單使用過程。
其中步驟7很重要,涉及到初始化操作,直接影響后續(xù)使用時的性能表現(xiàn)。
步驟8表示設(shè)置MAC地址,dst表示接收端,src表示發(fā)送端
步驟9表示設(shè)置IP地址,dst同上
步驟10表示發(fā)包的包大小
步驟11表示端口0開始發(fā)包,str表示所有端口開始發(fā)包
具體可以運行時使用help命令自行查看
| 參數(shù) | 數(shù)值 |
|---|---|
| -l | 使用到的cpu核 |
| -n | 內(nèi)存通道數(shù)目 |
| -m | 內(nèi)核掩碼 |
關(guān)于以上內(nèi)容和性能表現(xiàn)之間的關(guān)系本篇并不涉及,內(nèi)核掩碼要講也可以另開一篇了,所以也不會涉及到,可以自行g(shù)oogle。
最后放一個運行時的效果圖:

