使用 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)
EXTRA_CFLAGS="-O0 -g3" make
I350 使用的是 librte_pmd_e1000.so
的驱动。
EXTRA_CFLAGS="-O0 -g3 -lrte_pmd_e1000" make
在 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
在 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
在 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
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
# 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
# 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