建立ovs接口連接兩個namespace組成二層網絡
環境搭建拓撲
br0 +--------------------------------------+ +--+ +--+ +---+ | tap1 tap2| +---+ | +--+ +--+ | | | | | | +--------------------------------------+ | | | | | | | | | +------------------+ +-------------------+ | tap1 | | tap2 | |192.168.1.102/24 | | 192.168.1.102/24 | | | | | | | | | | | | | | namespace ns1 | | namespace ns1 | | | | | +------------------+ +-------------------+
實現腳本:
ip netns add ns1 ip netns add ns2 ovs-vsctl add-br br0 ovs-vsctl add-port br0 tap1 -- set Interface tap1 type=internal ip link set tap1 netns ns1 ip netns exec ns1 ip link set dev tap1 up ovs-vsctl add-port br0 tap2 -- set Interface tap2 type=internal ip link set tap2 netns ns2 ip netns exec ns2 ip link set dev tap2 up ip netns exec ns1 ip addr add 192.168.1.102/24 dev tap1 ip netns exec ns2 ip addr add 192.168.1.101/24 dev tap2 ip netns exec ns1 ip link set lo up ip netns exec ns2 ip link set lo up ip netns exec ns1 ping -c 4 192.168.1.102 ip netns exec ns1 ping -c 4 192.168.1.101
建立vlan二層網絡
環境搭建拓撲
br0 trunk vlan tag 10,11 br1 +------------------------+ +------------------------+ | | tag10 tag10 | | | trunk_br0 +-----------------------+trunk_br1 | | +-----------------------+ | | | tag11 tag11 | | |tap1 tap2 | | tap3 | +------------------------+ +------------------------+ |tag 10 tag11| tag10| | | | | | | 192.168.1.101/24 | | 192.168.1.102/24 | 192.168.1.103/24 +-------+ +-------+ +-------+ | tap1 | | tap2 | |tap3 | | | | | | | | | | | | | | | | | | | | | | | | | +-------+ +-------+ +-------+ ns1 ns2 ns3
環境實現腳本
ip netns add ns1 ip netns add ns2 ovs-vsctl add-br br0 ovs-vsctl add-port br0 tap1 -- set Interface tap1 type=internal ovs-vsctl set Port tap1 tag=10 ip link set tap1 netns ns1 ip netns exec ns1 ip link set dev tap1 up ovs-vsctl add-port br0 tap2 -- set Interface tap2 type=internal ovs-vsctl set Port tap2 tag=11 ip link set tap2 netns ns2 ip netns exec ns2 ip link set dev tap2 up ip netns exec ns1 ip addr add 192.168.1.101/24 dev tap1 ip netns exec ns2 ip addr add 192.168.1.102/24 dev tap2 ip netns exec ns1 ip link set lo up ip netns exec ns2 ip link set lo up ovs-vsctl add-br br1 ovs-vsctl add-port br1 tap3 -- set Interface tap3 type=internal ovs-vsctl add-port br0 trunk_br0 trunks=10,11 -- set Interface trunk_br0 type=patch options:peer=trunk_br1 ovs-vsctl add-port br1 trunk_br1 trunks=10,11 -- set Interface trunk_br1 type=patch options:peer=trunk_br0 ip netns add ns3 ip link set tap3 netns ns3 ip netns exec ns3 ip addr add 192.168.1.103/24 dev tap3 ip netns exec ns3 ip link set dev tap3 up ovs-vsctl set Port tap3 tag=10 ip netns exec ns3 ping -c 4 192.168.1.101 ip netns exec ns3 ping -c 4 192.168.1.102
說明:
-
br0和br1兩個交換機之間連接使用的是patch口,在創建時候需要指明peer(對端口)選項
-
ovs-vsctl add-port br0 trunk_br0 trunks=10,11 -- set Interface trunk_br0 type=patch options:peer=trunk_br1 ovs-vsctl add-port br1 trunk_br1 trunks=10,11 -- set Interface trunk_br1 type=patch options:peer=trunk_br0
br0和br1兩個交換機之間連接在trunk口附加上tag10和tag11
-
結論
ns3:tap3:vlan10 能ping通ns1:tap1:vlan10 因為ns3和ns1屬於同一個vlan;同時無法ping通ns2
ovs vlan報文轉發原理探究
環境搭建拓撲
first_ns second_ns third_ns +-----------+ +-----------+ +-----------+ | | | | | | | | | | | | | | | | | | | first_br | second_br | third_br| +-----------+ +-----------+ +-----------+ 10.0.0.4/24 10.0.0.5/24 | 10.0.0.6/24 | | | | | | |tag 10 | 無 tag | trunk 11,12 +------------------------------------------+ | first_br second_br third_br | | | br0 | | | +------------------------------------------+ | tag 10 | | | | |10.0.0.1/24 +------------+ | | | | | | | | +------------+ ns1
搭建網絡腳本
ovs-vsctl add-br br0 ovs-vsctl add-port br0 first_br -- set Interface first_br type=internal ovs-vsctl set Port first_br tag=10 ip netns add first ip link set first_br netns first ip netns exec first ip addr add 10.0.0.4/24 dev first_br ip netns exec first ip link set dev first_br up ip netns add ns1 ovs-vsctl add-port br0 tap1 -- set Interface tap1 type=internal ovs-vsctl set Port tap1 tag=10 ip link set tap1 netns ns1 ip netns exec ns1 ip link set lo up ip netns exec ns1 ip link set dev tap1 up ip netns exec ns1 ip addr add 10.0.0.1/24 dev tap1 ovs-vsctl add-port br0 second_br -- set Interface second_br type=internal ip netns add second ip link set second_br netns second ip netns exec second ip addr add 10.0.0.5/24 dev second_br ip netns exec second ip link set dev second_br up ovs-vsctl add-port br0 third_br trunks=11,12 -- set Interface third_br type=internal ip netns add third ip link set third_br netns third ip netns exec third ip addr add 10.0.0.6/24 dev third_br ip netns exec third ip link set dev third_br up
實驗過程:
進入netns1,一直ping 10.0.0.4,在netns first、second、third分別抓包
實驗記錄
- first抓取報文
-
root@controller-VirtualBox:~# ip netns exec first tcpdump -n -e -i first_br arp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on first_br, link-type EN10MB (Ethernet), capture size 262144 bytes 15:47:54.636790 9a:03:f1:61:48:9d > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Request who-has 10.0.0.4 tell 10.0.0.1, length 28 15:47:54.636808 4e:cc:d6:5a:53:f4 > 9a:03:f1:61:48:9d, ethertype ARP (0x0806), length 42: Reply 10.0.0.4 is-at 4e:cc:d6:5a:53:f4, length 28
-
抓到arp廣播包
second抓取報文
root@controller-VirtualBox:~# ip netns exec second tcpdump -n -e -i second_br arp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on second_br, link-type EN10MB (Ethernet), capture size 262144 bytes 15:49:40.345271 9a:03:f1:61:48:9d > ff:ff:ff:ff:ff:ff, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP, Request who-has 10.0.0.4 tell 10.0.0.1, length 28
抓到arp廣播包
third抓取報文
root@controller-VirtualBox:~# ip netns exec third tcpdump -n -e -i third_br arp tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on third_br, link-type EN10MB (Ethernet), capture size 262144 bytes
沒有抓到arp廣播
結論
- trunk port
(1)這個port不配置tag,配置trunks,如果trunks為空,則所有的VLAN都trunk,也就意味着對於所有的VLAN的包,本身帶什么VLAN ID,就是攜帶者什么VLAN ID,
(2)如果沒有設置VLAN,就屬於VLAN 0,全部允許通過。
(3)如果trunks不為空,則僅僅帶着這些VLAN ID的包通過。 - access port
(1)這個port配置tag,從這個port進來的包會被打上這個tag,
(2)從其他的trunk port中進來的本身就帶有VLAN ID的包,如果VLAN ID等於tag,則會從這個port發出,
(3)從其他的access port上來的包,如果tag相同,也會被forward到這個port。
(4)從access port發出的包不帶VLAN ID。
(5)如果一個本身帶VLAN ID的包到達access port,即便VLAN ID等於tag,也會被拋棄。
openvswitch概念補充
幾個重要的概念
- Bridge: Bridge 代表一個以太網交換機(Switch),一個主機中可以創建一個或者多個 Bridge 設備。
- Port: 端口與物理交換機的端口概念類似,每個 Port 都隸屬於一個 Bridge。
- Interface: 連接到 Port 的網絡接口設備。在通常情況下,Port 和 Interface 是一對一的關系, 只有在配置 Port 為 bond 模式后,Port 和 Interface 是一對多的關系。
- Controller: OpenFlow 控制器。OVS 可以同時接受一個或者多個 OpenFlow 控制器的管理。
- datapath: 在 OVS 中,datapath 負責執行數據交換,也就是把從接收端口收到的數據包在流表中進行匹配,並執行匹配到的動作。
- Flow table: 每個 datapath 都和一個“flow table”關聯,當 datapath 接收到數據之后, OVS 會在 flow table 中查找可以匹配的 flow,執行對應的操作, 例如轉發數據到另外的端口。