dpdk_18_05 應用筆記: ip_pipeline 例程
查詢網卡的 pci 地址
使用 lspci
查詢網卡的 pci 地址。該地址由於後續腳本設置來指定網卡。
lspci | grep Eth
> 01:00.0 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
> 01:00.1 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
> 01:00.2 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
> 01:00.3 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
ip_pipeline 的編譯
情況1. 編譯的時候沒有網卡的驅動。(使用,後續都是以這個爲例子)
EXTRA_CFLAGS="-O0 -g3" make
情況1. 編譯的時候加上網卡的驅動。(暫不使用)
I350 使用的是 librte_pmd_e1000.so
的驅動。
EXTRA_CFLAGS="-O0 -g3 -lrte_pmd_e1000" make
ip_pipeline/l2fwd 例程
網絡拓撲
在 l2fwd 的腳本 l2fwd.cli
中,是配置了 LINK0 和 LINK1 相互轉發,LINK2 和 LINK3 相互轉發的。
; port in 0: LINK0.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK0 rxq 0
; port in 1: LINK1.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK1 rxq 0
; port in 2: LINK2.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK2 rxq 0
; port in 3: LINK3.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK3 rxq 0
; port out 0: LINK0.txq0
pipeline PIPELINE0 port out bsz 32 link LINK0 txq 0
; port out 1: LINK1.txq0
pipeline PIPELINE0 port out bsz 32 link LINK1 txq 0
; port out 2: LINK2.txq0
pipeline PIPELINE0 port out bsz 32 link LINK2 txq 0
; port out 3: LINK3.txq0
pipeline PIPELINE0 port out bsz 32 link LINK3 txq 0
; create table
pipeline PIPELINE0 table match stub
pipeline PIPELINE0 table match stub
pipeline PIPELINE0 table match stub
pipeline PIPELINE0 table match stub
; link up port in and table
pipeline PIPELINE0 port in 0 table 0
pipeline PIPELINE0 port in 1 table 1
pipeline PIPELINE0 port in 2 table 2
pipeline PIPELINE0 port in 3 table 3
thread 1 pipeline PIPELINE0 enable
; set the rule of forwarding in table
pipeline PIPELINE0 table 0 rule add match default action fwd port 1
pipeline PIPELINE0 table 1 rule add match default action fwd port 0
pipeline PIPELINE0 table 2 rule add match default action fwd port 3
pipeline PIPELINE0 table 3 rule add match default action fwd port 2
; end of file
這裏的測試,使用了 LINK0 和 LINK1 作爲測試網口,分別連接到 pc_0, pc_1。
最後的網絡拓撲如下(注意: LINK2 和 LINK3,沒有畫出來):
+--------------------------+
| pc_0 | # ip: 192.168.111.1/24
+--------------------------+
| ^
| port_in_0: | port_out_0:
v LINK0.rxq0 | LINK0.txq0
+--------------------------+
| v ^ |
|+------+ +------+|
||table0| |table1|| # pipeline0
|+------+ +------+|
| v ^ |
+--------------------------+
| port_out_1: ^ port_in_1:
| LINK1.txq0 | LINK1.rxq0
v |
+--------------------------+
| pc_1 | # ip: 192.168.111.2/24
+--------------------------+
腳本設置
根據 lspci 的輸出。設置好腳本中網卡的 pci 地址。
修改文件:
l2fwd.cli
修改爲:
link LINK0 dev 01:00.0 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK1 dev 01:00.1 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK2 dev 01:00.2 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK3 dev 01:00.3 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
命令行設置
ip_pipeline/l2fwd 的例子,命令行設置如下:
# -d: driver of nic
# -w: whitelist of nic
# -c: logic core for packet processing
# -s: script file
./build/ip_pipeline \
-d librte_pmd_e1000.so \
-w 01:00.0 -w 01:00.1 -w 01:00.2 -w 01:00.3 \
-c 0xf \
-- \
-s examples/l2fwd.cli
測試步驟
- pc_0 和 pc_1 配置相同段的 ip。
- 測試 pc_0 可以 ping 通 pc_1。
- 測試 pc_1 可以 ping 通 pc_0。
ip_pipeline/kni 例程
網絡拓撲
在 kni 的腳本 kni.cli
中,配置了 物理網口 和 KNI網口 間的轉發規則。
配置如下:
; port in 0: LINK0.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK0 rxq 0
; port in 1: KNI1
pipeline PIPELINE0 port in bsz 32 kni KNI1
; port in 2: LINK1.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK1 rxq 0
; port in 3: KNI0
pipeline PIPELINE0 port in bsz 32 kni KNI0
; port out 0: KNI0
pipeline PIPELINE0 port out bsz 32 kni KNI0
; port out 1: LINK1.txq0
pipeline PIPELINE0 port out bsz 32 link LINK1 txq 0
; port out 2: KNI1
pipeline PIPELINE0 port out bsz 32 kni KNI1
; port out 3: LINK0.txq0
pipeline PIPELINE0 port out bsz 32 link LINK0 txq 0
; create table
pipeline PIPELINE0 table match stub action AP0
pipeline PIPELINE0 table match stub action AP0
pipeline PIPELINE0 table match stub action AP0
pipeline PIPELINE0 table match stub action AP0
; link up port in and table
pipeline PIPELINE0 port in 0 table 0
pipeline PIPELINE0 port in 1 table 1
pipeline PIPELINE0 port in 2 table 2
pipeline PIPELINE0 port in 3 table 3
thread 1 pipeline PIPELINE0 enable
; set the rule of forwarding in table
pipeline PIPELINE0 table 0 rule add match default action fwd port 0
pipeline PIPELINE0 table 1 rule add match default action fwd port 1
pipeline PIPELINE0 table 2 rule add match default action fwd port 2
pipeline PIPELINE0 table 3 rule add match default action fwd port 3
; end of file
這裏的測試,使用了 LINK0 和 LINK1 作爲測試網口,分別連接到 pc_0, pc_1。
最後整體的網絡拓撲如下:
+--------------------------+
| pc_0 | # ip: 192.168.111.1/24
+--------------------------+
| ^
| port_in_0: | port_out_3:
v LINK0.rxq0 | LINK0.txq0
+--------------------------+
| v ^ |
|+------+ +------+|
||table0| |table0|| # pipeline0
|+------+ +------+|
| v ^ |
+--------------------------+
| port_out_0: ^ port_in_3:
| KNI0 | KNI0
v |
+--------------------------+ # 內核配置的橋 br0
| v ^ | # br0 添加 KNI0
+--------------------------+ # br0 添加 KNI1
| ^
| port_in_1: | port_out_2:
v KNI1 | KNI1
+--------------------------+
| v ^ |
|+------+ +------+|
||table1| |table2|| # pipeline0
|+------+ +------+|
| v ^ |
+--------------------------+
| port_out_1: ^ port_in_2:
| LINK1.txq0 | LINK1.rxq0
v |
+--------------------------+
| pc_1 | # ip: 192.168.111.2/24
+--------------------------+
腳本設置
根據 lspci 的輸出。來設置好腳本中網卡的 pci 地址。
修改文件:
kni.cli
修改爲:
link LINK0 dev 01:00.0 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK1 dev 01:00.1 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
命令行設置
ip_pipeline/kni 的例子,命令行設置如下:
命令行:
# -d: driver of nic
# -w: whitelist of nic
# -c: logic core for packet processing
# -s: script file
./build/ip_pipeline \
-d librte_pmd_e1000.so \
-w 01:00.0 -w 01:00.1 \
-c 0xf \
-- \
-s examples/kni.cli
### 設置橋口
pipeline 只是負責將 物理網口和 KNI網口 連接起來,令到報文可以流入到內核。
所以還需要創建 橋口 br0,將 KNI0 和 KNI1 橋接起來。
# 創建 橋口
brctl addbr br0
# 向橋中添加 kni 網口
brctl addif br0 KNI0
brctl addif br0 KNI1
# 啓用 橋口 和 kni 網口
ifconfig br0 up
ifconfig KNI0 up
ifconfig KNI1 up
測試步驟
- pc_0 和 pc_1 配置相同段的 ip。
- 測試 pc_0 可以 ping 通 pc_1。
- 測試 pc_1 可以 ping 通 pc_0。
ip_pipeline/route 例程
網絡拓撲
在 route 的腳本 route.cli
中,需要按照實際情況進行修改 路由 轉發規則。
配置如下:
; port in 0: LINK0.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK0 rxq 0
; port in 1: LINK1.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK1 rxq 0
; port in 2: LINK2.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK2 rxq 0
; port in 3: LINK3.rxq0
pipeline PIPELINE0 port in bsz 32 link LINK3 rxq 0
; port out 0: LINK0.rxq0
pipeline PIPELINE0 port out bsz 32 link LINK0 txq 0
; port out 1: LINK1.rxq0
pipeline PIPELINE0 port out bsz 32 link LINK1 txq 0
; port out 2: LINK2.rxq0
pipeline PIPELINE0 port out bsz 32 link LINK2 txq 0
; port out 3: LINK3.rxq0
pipeline PIPELINE0 port out bsz 32 link LINK3 txq 0
; port out 4: sink (blackhole)
pipeline PIPELINE0 port out bsz 32 sink
; create table
pipeline PIPELINE0 table match lpm ipv4 offset 286 size 4K action AP0
; link up port in and table
pipeline PIPELINE0 port in 0 table 0
pipeline PIPELINE0 port in 1 table 1
pipeline PIPELINE0 port in 2 table 2
pipeline PIPELINE0 port in 3 table 3
thread 1 pipeline PIPELINE0 enable
; set the rule of forwarding in table
; default is forward to port_out_4 (sink, blackhole)
pipeline PIPELINE0 table 0 rule add match default action fwd port 4
; # 這裏需要按照實際情況進行修改
; match lpm ipv4 192.168.111.0 24
; # if destination ip in subnet 192.168.111.0/24
; action fwd port 0
; # then forward packet to port_out_0
; encap ether 00:1f:16:08:a7:57 00:01:02:03:04:05
; # modify the ether header with dmac=00:1f:16:08:a7:57 (pc_0's mac)
; # and smac=00:01:02:03:04:05 (LINK0's mac)
pipeline PIPELINE0 table 0 rule add match lpm ipv4 192.168.111.0 24 action fwd port 0 encap ether 00:1f:16:08:a7:57 00:01:02:03:04:05
; # 這裏需要按照實際情況進行修改
; match lpm ipv4 192.168.222.0 24
; # if destination ip in subnet 192.168.222.0/24
; action fwd port 0
; # then forward packet to port_out_0
; encap ether b8:27:eb:af:96:9b 10:11:12:13:14:15
; # modify the ether header with dmac=b8:27:eb:af:96:9b (pc_1's mac)
; # and smac=10:11:12:13:14:15 (LINK1's mac)
pipeline PIPELINE0 table 0 rule add match lpm ipv4 192.168.222.0 24 action fwd port 1 encap ether b8:27:eb:af:96:9b 10:11:12:13:14:15
; end of file
這裏的測試,使用了 LINK0 和 LINK1 作爲測試網口,分別連接到 pc_0, pc_1。
最後整體的網絡拓撲如下(注意: LINK2 和 LINK3,沒有畫出來):
+--------------------------+ # port: eth0
| pc_0 | # mac: 00:1f:16:08:a7:57
+--------------------------+ # ip: 192.168.111.1/24
| ^
| port_in_0: | port_out_0: # port: LINK0
v LINK0.rxq0 | LINK0.txq0 # mac: 00:01:02:03:04:05
+--------------------------+
| v ^ |
|+------------------------+|
|| table0 || # pipeline0
|+------------------------+|
| v ^ |
+--------------------------+
| port_out_1: ^ port_in_1: # port: LINK1
| LINK1.txq0 | LINK1.rxq0 # mac: 10:11:12:13:14:15
v |
+--------------------------+ # port: eth0
| pc_1 | # mac: b8:27:eb:af:96:9b
+--------------------------+ # ip: 192.168.222.1/24
腳本設置
根據 lspci 的輸出。來設置好腳本中網卡的 pci 地址。
修改文件:
route.cli
修改爲:
link LINK0 dev 01:00.0 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK1 dev 01:00.1 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK2 dev 01:00.2 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
link LINK3 dev 01:00.3 rxq 1 128 MEMPOOL0 txq 1 512 promiscuous on
命令行設置
router 命令行參數
ip_pipeline/route 的例子,命令行設置如下:
# -d: driver of nic
# -w: whitelist of nic
# -c: logic core for packet processing
# -s: script file
./build/ip_pipeline \
-d librte_pmd_e1000.so \
-w 01:00.0 -w 01:00.1 -w 01:00.2 -w 01:00.3 \
-c 0xf \
-- \
-s examples/route.cli
pc_0 命令行參數
# add ip address
ip a a 192.168.111.1/24 dev eth0
# add a PERMANENT neighbour
ip n a 192.168.222.1 dev eth0 lladdr 00:01:02:03:04:05
# set default port out
ip r a default dev eth0
# testing
ping 192.168.222.1
pc_1 命令行參數
# add ip address
ip a a 192.168.222.1/24 dev eth0
# add a PERMANENT neighbour
ip n a 192.168.111.1 dev eth0 lladdr 10:11:12:13:14:15
# set default port out
ip r a default dev eth0
# testing
ping 192.168.111.1
測試步驟
- 測試 pc_0 可以 ping 通 pc_1。
- 測試 pc_1 可以 ping 通 pc_0。