实验 4:Open vSwitch 实验——Mininet 中使用 OVS 命令
一、实验目的
Mininet 安装之后,会连带安装 Open vSwitch,可以直接通过 Python 脚本调用Open vSwitch 命令,从而直接控制 Open vSwitch,通过实验了解调用控制的方法。
二、实验任务
在本实验中,使用 Mininet 基于 Python 的脚本,调用“ovs-vsctl”命令直接控制Open vSwitch。使用默认的交换机泛洪规则,设置更高的优先级规则进行预先定义 IP 报文的转发。在多个交换机中通过设置不同 TOS 值的数据包将通过不同的方式到达目的地址,验证主机间的连通性及到达目的地址的时间。
三、实验步骤
1. 实验环境
安装了 Ubuntu 18.04.5 Desktop amd64 的虚拟机
2. 实验过程
SDNLAB 实验参考资料:https://www.sdnlab.com/15083.html
(1)创建 ovsSingleBr.py 脚本并添加内容,代码参考 SDNLAB
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922145743455-2143056471.png)
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922145642998-1131807399.png)
脚本对应的拓扑如上图所示,执行 ovsSingleBr.py,在没有控制器的情况下,在Mininet 脚本中通过调用 ovs 命令直接向 switch0 交换机下发流表,将入端口号为1/2/3 的数据包泛洪广播,并对目的地址为 192.168.123.1/2/3 的数据包分别从1/2/3 端口转发出去。之后测试 h0 ping h1,h0 ping h2,网络连通。
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922150304675-231817086.png)
(2)创建 ovsMultiBr.py 脚本并添加内容,代码参考 SDNLAB
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922145823278-1222172790.png)
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922150020721-2027128724.png)
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922150129837-562568334.png)
脚本对应的拓扑如上图所示,执行 ovsMultiBr.py,在没有控制器的情况下,在Mininet 脚本中通过调用 ovs 命令给多个交换机下发流表,通过 h0 ping h1 操作测试验证主机间的连通性,并通过-Q 参数设置不通的 tos 值查看主机间的连通性。通过验证发现,tos 值设置越大,时间使用越少。
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922150407897-325168257.png)
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922150434281-2122173404.png)
四、实验要求
1.学习 ovsSingleBr.py 和 ovsMultiBr.py,在下图拓扑中实现一个 VLAN。
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200922220721161-1843402795.png)
上述代码将 h0 和 h2 划分在 VLAN 0 中,h1 和 h3 划分在 VLAN 1 中,由于拓扑没有控制器,并且初始化时删除了交换机中的所有流表,因此除非下发流表,否则主机之间网络无法连通。请尝试修改代码,利用 ovs 命令直接下发 VLAN 设置的流表项,最终测试 h0 和 h2 互通,h1 和 h3 互通,其余主机均不通,结果如下图。OVS 实现 VLAN 参考博客:https://www.cnblogs.com/fjlinww/p/11791846.html
实现代码:
#!/usr/bin/python from mininet.net import Mininet from mininet.node import Node from mininet.link import TCLink from mininet.log import setLogLevel, info def myNet(): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) switch0 = Node( 's0', inNamespace=False ) switch1 = Node( 's1', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) h2 = Node( 'h2' ) h3 = Node( 'h3' ) info( "*** Creating links\n" ) linkopts0=dict(bw=100, delay='1ms', loss=0) linkopts1=dict(bw=1, delay='100ms', loss=0) TCLink( h0, switch0, **linkopts0) TCLink( h1, switch0, **linkopts0) TCLink( h2, switch1, **linkopts1) TCLink( h3, switch1, **linkopts1) TCLink( switch0, switch1, **linkopts0) info( "*** Configuring hosts\n" ) h0.setIP( '192.168.123.1/24' ) h1.setIP( '192.168.123.2/24' ) h2.setIP( '192.168.123.3/24' ) h3.setIP( '192.168.123.4/24' ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( str( h2 ) + '\n' ) info( str( h3 ) + '\n' ) info( "*** Starting network using Open vSwitch\n" ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.cmd( 'ovs-vsctl add-br dp0' ) switch1.cmd( 'ovs-vsctl del-br dp1' ) switch1.cmd( 'ovs-vsctl add-br dp1' ) for intf in switch0.intfs.values(): print intf print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) for intf in switch1.intfs.values(): print intf print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf ) print switch0.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 switch0.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 switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=0,actions=pop_vlan,output:1' ) print switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=1,actions=pop_vlan,output:2' ) print switch1.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 switch1.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 switch1.cmd(r'sudo ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output:1' ) print switch1.cmd(r'sudo 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 test\n" ) h0.cmdPrint( 'ping -Q 0x10 -c 3 ' + h1.IP() ) h0.cmdPrint( 'ping -Q 0x20 -c 3 ' + h2.IP() ) h0.cmdPrint( 'ping -Q 0x30 -c 3 ' + h3.IP() ) h1.cmdPrint( 'ping -Q 0x40 -c 3 ' + h2.IP() ) h1.cmdPrint( 'ping -Q 0x50 -c 3 ' + h3.IP() ) h2.cmdPrint( 'ping -Q 0x60 -c 3 ' + h3.IP() ) #h1.cmdPrint('iperf -s -p 12345 -u &') #h0.cmdPrint('iperf -c ' + h1.IP() +' -u -b 10m -p 12345 -t 10 -i 1') #print switch0.cmd( 'ovs-ofctl show dp0' ) #print switch1.cmd( 'ovs-ofctl show dp1' ) #print switch2.cmd( 'ovs-ofctl show dp2' ) #print switch3.cmd( 'ovs-ofctl show dp3' ) #print switch4.cmd( 'ovs-ofctl show dp4' ) #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' ) #print "Testing video transmission between h1 and h2" #h1.cmd('./myrtg_svc -u > myrd &') #h0.cmd('./mystg_svc -trace st 192.168.123.2') info( "*** Stopping network\n" ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.deleteIntfs() switch1.cmd( 'ovs-vsctl del-br dp1' ) switch1.deleteIntfs() # switch2.cmd( 'ovs-vsctl del-br dp2' ) # switch2.deleteIntfs() # switch3.cmd( 'ovs-vsctl del-br dp3' ) # switch3.deleteIntfs() # switch4.cmd( 'ovs-vsctl del-br dp4' ) # switch4.deleteIntfs() info( '\n' ) if __name__ == '__main__': setLogLevel( 'info' ) info( '*** Scratch network demo (kernel datapath)\n' ) Mininet.init() myNet()
命令行执行结果:
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200924111112333-1058983215.png)
![](https://img2020.cnblogs.com/blog/2143890/202009/2143890-20200924111140495-1437943597.png)