1.实验目的
熟悉Mininet自定义拓扑脚本的编写;
掌握使用“ovs-vsctl”命令直接控制Open vSwitch。
2.实验原理
在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为。在本实验中,使用Mininet基于python的脚本,调用“ovs-vsctl”命令直接控制Open vSwitch。
3.实验任务
本实验在基于Mininet脚本的不同拓扑环境下使用OpenDaylight控制交换机行为。任务一:一台交换机两台主机,从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口;任务二:两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口同任务一;任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。
4.实验步骤
4.1 搭建环境
本实验需要两台虚拟机,分别安装OpenDaylight(6G)和支持OpenFlow1.3协议的Mininet。
4.2 实验操作
任务一:一台交换机两台主机,实现从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口。
1 在Mininet里新建文件mymininet1.py。
#touch mymininet1.py
2 查看OpenDaylight所在虚拟机的IP为10.0.0.8。
3 编辑文件mymininet1.py,内容如下:
#!/usr/bin/python from mininet.net import Mininet from mininet.node import Node from mininet.link import Link from mininet.log import setLogLevel, info from mininet.util import quietRun from time import sleep def scratchNet( cname='controller', cargs='-v ptcp:' ): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) switch0 = Node( 's0', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) info( "*** Creating links\n" ) Link( h0, switch0 ) Link( h1, switch0 ) info( "*** Configuring hosts\n" ) h0.setIP( '192.168.123.1/24' ) h1.setIP( '192.168.123.2/24' ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( "*** Starting network using Open vSwitch\n" ) controller.cmd( cname + ' ' + cargs + '&' ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.cmd( 'ovs-vsctl add-br dp0' ) for intf in switch0.intfs.values(): print intf print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) info( '*** Waiting for switch to connect to controller' ) while 'is_connected' not in quietRun( 'ovs-vsctl show' ): sleep( 1 ) info( '.' ) info( '\n' ) info( "*** Running test\n" ) h0.cmdPrint( 'ping -c6 ' + h1.IP() ) h1.cmdPrint( 'ping -c6 ' + h0.IP() ) info( "*** Stopping network\n" ) controller.cmd( 'kill %' + cname ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.deleteIntfs() info( '\n' ) if __name__ == '__main__': setLogLevel( 'info' ) info( '*** Scratch network demo (kernel datapath)\n' ) Mininet.init() scratchNet()
4 将文件mymininet1.py设置成可执行文件。
#chmod +x mymininet1.py
5 执行文件mymininet1.py。
#./mymininet1.py
任务二:两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口要求同任务一。
1 在Mininet里新建文件mymininet2.py。
#touch mymininet2.py
2 编辑文件mymininet2.py,内容如下:
#!/usr/bin/python from mininet.net import Mininet from mininet.node import Node from mininet.link import Link from mininet.log import setLogLevel, info from mininet.util import quietRun from time import sleep def scratchNet( cname='controller', cargs='-v ptcp:' ): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) switch0 = Node( 's0', inNamespace=False ) switch1 = Node( 's1', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) info( "*** Creating links\n" ) Link( h0, switch0 ) Link( h1, switch1 ) Link( switch0, switch1 ) info( "*** Configuring hosts\n" ) h0.setIP( '192.168.123.1/24' ) h1.setIP( '192.168.123.2/24' ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( "*** Starting network using Open vSwitch\n" ) controller.cmd( cname + ' ' + cargs + '&' ) 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 ) switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' ) switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' ) info( '*** Waiting for switch to connect to controller' ) while 'is_connected' not in quietRun( 'ovs-vsctl show' ): sleep( 1 ) info( '.' ) info( '\n' ) print switch0.cmd( 'ovs-ofctl show dp0' ) print switch1.cmd( 'ovs-ofctl show dp1' ) info( "*** Running test\n" ) h0.cmdPrint( 'ping -c3 ' + h1.IP() ) h1.cmdPrint( 'ping -c3 ' + h0.IP() ) info( "*** Stopping network\n" ) controller.cmd( 'kill %' + cname ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.deleteIntfs() switch1.cmd( 'ovs-vsctl del-br dp1' ) switch1.deleteIntfs() info( '\n' ) if __name__ == '__main__': setLogLevel( 'info' ) info( '*** Scratch network demo (kernel datapath)\n' ) Mininet.init() scratchNet()
3 将文件mymininet2.py设置成可执行文件。
#chmod +x mymininet2.py
4 执行文件mymininet2.py。
#./mymininet2.py
任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。
1 在Mininet里新建文件mymininet3.py。
#touch mymininet3.py
2 编辑文件mymininet3.py,内容如下:
#!/usr/bin/python from mininet.net import Mininet from mininet.node import Node from mininet.link import TCLink from mininet.log import setLogLevel, info from mininet.util import quietRun from time import sleep def scratchNet( cname='controller', cargs='-v ptcp:' ): "Create network from scratch using Open vSwitch." info( "*** Creating nodes\n" ) controller = Node( 'c0', inNamespace=False ) switch0 = Node( 's0', inNamespace=False ) switch1 = Node( 's1', inNamespace=False ) h0 = Node( 'h0' ) h1 = Node( 'h1' ) info( "*** Creating links\n" ) linkopts0=dict(bw=10) linkopts1=dict(bw=10, delay='5ms', loss=10) TCLink( h0, switch0,**linkopts0) TCLink( h1, switch1,**linkopts0) TCLink( switch0, switch1,**linkopts1) info( "*** Configuring hosts\n" ) h0.setIP( '192.168.123.1/24' ) h1.setIP( '192.168.123.2/24' ) info( str( h0 ) + '\n' ) info( str( h1 ) + '\n' ) info( "*** Starting network using Open vSwitch\n" ) controller.cmd( cname + ' ' + cargs + '&' ) 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 ) switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' ) switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' ) info( '*** Waiting for switch to connect to controller' ) while 'is_connected' not in quietRun( 'ovs-vsctl show' ): sleep( 1 ) info( '.' ) info( '\n' ) print switch0.cmd( 'ovs-ofctl show dp0' ) print switch1.cmd( 'ovs-ofctl show dp1' ) info( "*** Running test\n" ) h0.cmdPrint( 'ping -c3 ' + h1.IP() ) h1.cmdPrint( 'ping -c3 ' + h0.IP() ) info( "*** Stopping network\n" ) controller.cmd( 'kill %' + cname ) switch0.cmd( 'ovs-vsctl del-br dp0' ) switch0.deleteIntfs() switch1.cmd( 'ovs-vsctl del-br dp1' ) switch1.deleteIntfs() info( '\n' ) if __name__ == '__main__': setLogLevel( 'info' ) info( '*** Scratch network demo (kernel datapath)\n' ) Mininet.init() scratchNet()
3 将文件mymininet3.py设置成可执行文件。
#chmod +x mymininet3.py
4 执行文件mymininet3.py。
#./mymininet3.py
5.实验结论
在Mininet脚本中,可以通过使用“ovs-vsctl”命令直接控制Open vSwitch,实现通信。