實(shí)驗(yàn)?zāi)康?/p>
- 能夠?qū)pen vSwitch進(jìn)行基本操作
- 能夠通過命令行終端使用OVS命令操作Open vSwitch交換機(jī),管理流表
- 能夠通過Min.NET的Python/ target=_blank class=infotextkey>Python代碼運(yùn)行OVS命令,控制網(wǎng)絡(luò)拓?fù)渲械腛pen vSwitch交換機(jī)
- 創(chuàng)建OVS交換機(jī)
- 為OVS交換機(jī)配置p0端口
- 創(chuàng)建虛擬空間ns0,將p0移入虛擬網(wǎng)絡(luò)空間ns0,并配置IP
- 為OVS交換機(jī)配置p1端口
- 創(chuàng)建虛擬空間ns1,將p0移入虛擬網(wǎng)絡(luò)空間ns1,并配置IP
- 測(cè)試聯(lián)通性
成功ping通
實(shí)驗(yàn)2 Mininet下發(fā)流表1. 自定義拓?fù)?/p>
運(yùn)行拓?fù)?/p>
2. 劃分VLAN
需求: VLAN劃分:
聯(lián)通性要求: h1-h3 h2-h4,其余不通
為s1配置流表項(xiàng):
查看下發(fā)的流表項(xiàng):
這四條命令原理: 1、2條命令:
- 首先對(duì)輸入端口進(jìn)行匹配,如果輸入端口為1(即為h1),執(zhí)行action:將h1設(shè)置為vlan0虛擬局域網(wǎng)的主機(jī),并將數(shù)據(jù)包向3號(hào)端口轉(zhuǎn)發(fā) 如果輸入端口為3(即為h3),執(zhí)行action:將h2設(shè)置為vlan1虛擬局域網(wǎng)的主機(jī),并將數(shù)據(jù)包向3號(hào)端口轉(zhuǎn)發(fā) 3、4條命令: 這兩條命令處理接受到的數(shù)據(jù)包,匹配域:Vlan標(biāo)簽 如果Vlan號(hào)屬于0,(h3屬于Vlan0),則將數(shù)據(jù)包往1號(hào)端口發(fā),也就是發(fā)給主機(jī)h1,從而實(shí)現(xiàn)h1和h3之間的通信;第四條同理,不再贅述
為s2配置流表項(xiàng): 原理與s1配置類似
- 從1號(hào)或2號(hào)端口輸入的數(shù)據(jù)包,則配上Vlan標(biāo)簽,1號(hào)端口輸入的配上Vlan0(和h1在同一虛擬局域網(wǎng)),并向3號(hào)端口轉(zhuǎn)發(fā)
- 2號(hào)端口輸入的配上Vlan1標(biāo)簽(和h2在同一局域網(wǎng)),并向3號(hào)端口轉(zhuǎn)發(fā)
- 從3號(hào)段口輸入的數(shù)據(jù)包已經(jīng)打過Vlan標(biāo)記,對(duì)Vlan標(biāo)記進(jìn)行匹配,若為0則為發(fā)送給h3的數(shù)據(jù)包,若為1則為發(fā)送給h4的數(shù)據(jù)包
符合h1和h3聯(lián)通,h2和h4聯(lián)通,其余不連通要求。
wireshark驗(yàn)證:
(二)進(jìn)階實(shí)驗(yàn)
要求:利用Python代碼實(shí)現(xiàn)VlAN劃分
- 分配ip 由于虛擬實(shí)現(xiàn)h1和h3在同一局域網(wǎng),因此分配ip如下
- code 編寫topo.py文件并運(yùn)行
#!/usr/bin/python from mininet.net import Mininet from mininet.node import Node from mininet.link import Link from mininet.log import setLogLevel, info def myNet(): "Create network from scratch using Open vSwitch." info( "*** Creating nodesn" ) switch1 = Node( 's1', inNamespace=False ) switch2 = Node( 's2', inNamespace=False ) h1 = Node( 'h1' ) h2 = Node( 'h2' ) h3 = Node( 'h3' ) h4 = Node( 'h4' ) info( "*** Creating linksn" ) Link( h1, switch1) Link( h2, switch1) Link( h3, switch2) Link( h4, switch2) Link( switch1, switch2) info( "*** Configuring hostsn" ) h1.setIP( '192.168.123.1/24' ) h2.setIP( '192.168.124.1/24' ) h3.setIP( '192.168.123.2/24' ) h4.setIP( '192.168.124.2/24' ) info( "*** Starting network using Open vSwitchn" ) switch1.cmd( 'ovs-vsctl del-br dp0' ) switch1.cmd( 'ovs-vsctl add-br dp0' ) switch2.cmd( 'ovs-vsctl del-br dp1' ) switch2.cmd( 'ovs-vsctl add-br dp1' ) for intf in switch1.intfs.values(): print (intf) print (switch1.cmd( 'ovs-vsctl add-port dp0 %s' % intf )) for intf in switch2.intfs.values(): print (intf) print (switch2.cmd( 'ovs-vsctl add-port dp1 %s' % intf )) print (switch1.cmd(r'ovs-vsctl show')) print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096->vlan_vid,output:3')) print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097->vlan_vid,output:3')) print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=0,actions=pop_vlan,output:1')) print (switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=1,actions=pop_vlan,output:2')) print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096->vlan_vid,output:3')) print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097->vlan_vid,output:3')) print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output:1')) print (switch2.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=1,actions=pop_vlan,output:2')) #switch0.cmd('tcpdump -i s0-eth0 -U -w aaa &') #h0.cmd('tcpdump -i h0-eth0 -U -w aaa &') info( "*** Running testn" ) h1.cmdPrint( 'ping -c 3 ' + h3.IP() ) h2.cmdPrint( 'ping -c 3 ' + h4.IP() ) h1.cmdPrint( 'ping -c 3 ' + h4.IP() ) h2.cmdPrint( 'ping -c 3 ' + h3.IP() ) #print switch0.cmd( 'ovs-ofctl show dp0' ) #print switch0.cmd( 'ovs-ofctl dump-tables dp0' ) #print switch0.cmd( 'ovs-ofctl dump-ports dp0' ) #print switch0.cmd( 'ovs-ofctl dump-flows dp0' ) #print switch0.cmd( 'ovs-ofctl dump-aggregate dp0' ) #print switch0.cmd( 'ovs-ofctl queue-stats dp0' ) info( "*** Stopping networkn" ) switch1.cmd( 'ovs-vsctl del-br dp0' ) switch1.deleteIntfs() switch2.cmd( 'ovs-vsctl del-br dp1' ) switch2.deleteIntfs() info( 'n' ) if __name__ == '__main__': setLogLevel( 'info' ) info( '*** Scratch network demo (kernel datapath)n' ) Mininet.init() myNet()
- 驗(yàn)證結(jié)果 驗(yàn)證1:直接在python腳本中驗(yàn)證 結(jié)果:
結(jié)果符合要求 驗(yàn)證2:通過wireshark驗(yàn)證
成功打上Vlan標(biāo)簽
- 錯(cuò)誤總結(jié)
- 忘記為兩臺(tái)交換機(jī)設(shè)置連接,導(dǎo)致ping不通,太蠢了TAT
1)此次實(shí)驗(yàn)我學(xué)習(xí)了如何使用OvS命令讓數(shù)據(jù)包打上Vlan標(biāo)簽,并學(xué)習(xí)了如何使用控制行實(shí)現(xiàn)流表下發(fā)。 2)通過進(jìn)階實(shí)驗(yàn),我學(xué)習(xí)了如何使用python文件實(shí)現(xiàn)流表下發(fā),并能在python文件中進(jìn)行測(cè)試。編寫python文件的過程中遇到不少bug,但通過不斷嘗試最終進(jìn)行了解決,提升了我的問題解決能力及創(chuàng)新能力。
附:知識(shí)總結(jié)
實(shí)驗(yàn)原理:用VLAN技術(shù)可以設(shè)計(jì)廣播域構(gòu)成,交換機(jī)收到廣播幀后,從而防止廣播風(fēng)暴
命令總結(jié)
創(chuàng)建ovs交換機(jī)過程
- 自動(dòng)創(chuàng)建類型為internal的ovs-switch000網(wǎng)橋
sudo ovs-vsctl add-port ovs-switch000 p0
(通過ovs-vsctl show解決報(bào)錯(cuò)) 2. 查詢p0網(wǎng)口相關(guān)信息
sudo ethtool -i p0
- 創(chuàng)建虛擬網(wǎng)絡(luò)空間ns0,把p0接口移入ns0,并配置IP
sudo ip netns add ns0 sudo ip link set p0 netns ns0 sudo ip netns exec ns0 ip addr add 192.168.1.100/24 dev p0 sudo ip netns exec ns0 ifconfig p0 promisc up
- 測(cè)試
sudo ip netns exec ns0 ping 192.168.1.101
- OVS命令
sudo ovs-ofctl show ovs-switch000
- OVS下發(fā)流表命令
sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096->vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097->vlan_vid,output:3 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1 sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2
查看流表項(xiàng)命令:
sudo ovs-ofctl -O OpenFlow13 dump-flows s1
原文鏈接:https://www.cnblogs.com/hhhf/p/16702352.html 原文作者:盒什么飯