实验参考:
Mininet脚本实现控制交换机行为
实验步骤:
1. 一台交换机两台主机,实现从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口。
1. 创建新文件mymininet1.py并编辑以下内容(注意要将控制器设置为Opendaylight安装的虚拟机,即修改第37行代码,之后同理):
1 #!/usr/bin/python 2 from mininet.net import Mininet 3 from mininet.node import Node 4 from mininet.link import Link 5 from mininet.log import setLogLevel, info 6 from mininet.util import quietRun 7 8 from time import sleep 9 10 def scratchNet( cname='controller', cargs='-v ptcp:' ): 11 "Create network from scratch using Open vSwitch." 12 13 info( "*** Creating nodes\n" ) 14 controller = Node( 'c0', inNamespace=False ) 15 switch0 = Node( 's0', inNamespace=False ) 16 h0 = Node( 'h0' ) 17 h1 = Node( 'h1' ) 18 19 info( "*** Creating links\n" ) 20 Link( h0, switch0 ) 21 Link( h1, switch0 ) 22 23 info( "*** Configuring hosts\n" ) 24 h0.setIP( '192.168.123.1/24' ) 25 h1.setIP( '192.168.123.2/24' ) 26 info( str( h0 ) + '\n' ) 27 info( str( h1 ) + '\n' ) 28 29 info( "*** Starting network using Open vSwitch\n" ) 30 controller.cmd( cname + ' ' + cargs + '&' ) 31 switch0.cmd( 'ovs-vsctl del-br dp0' ) 32 switch0.cmd( 'ovs-vsctl add-br dp0' ) 33 34 for intf in switch0.intfs.values(): 35 print intf 36 print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) 37 switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) 38 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) 39 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) 40 41 info( '*** Waiting for switch to connect to controller' ) 42 while 'is_connected' not in quietRun( 'ovs-vsctl show' ): 43 sleep( 1 ) 44 info( '.' ) 45 info( '\n' ) 46 47 info( "*** Running test\n" ) 48 h0.cmdPrint( 'ping -c6 ' + h1.IP() ) 49 h1.cmdPrint( 'ping -c6 ' + h0.IP() ) 50 51 info( "*** Stopping network\n" ) 52 controller.cmd( 'kill %' + cname ) 53 switch0.cmd( 'ovs-vsctl del-br dp0' ) 54 switch0.deleteIntfs() 55 info( '\n' ) 56 57 if __name__ == '__main__': 58 setLogLevel( 'info' ) 59 info( '*** Scratch network demo (kernel datapath)\n' ) 60 Mininet.init() 61 scratchNet()
2. 改为可执行文件并将其执行:
2. 两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口要求同1。
1. 创建新文件mymininet2.py并编辑以下内容:
1 #!/usr/bin/python 2 from mininet.net import Mininet 3 from mininet.node import Node 4 from mininet.link import Link 5 from mininet.log import setLogLevel, info 6 from mininet.util import quietRun 7 8 from time import sleep 9 10 def scratchNet( cname='controller', cargs='-v ptcp:' ): 11 "Create network from scratch using Open vSwitch." 12 13 info( "*** Creating nodes\n" ) 14 controller = Node( 'c0', inNamespace=False ) 15 switch0 = Node( 's0', inNamespace=False ) 16 switch1 = Node( 's1', inNamespace=False ) 17 h0 = Node( 'h0' ) 18 h1 = Node( 'h1' ) 19 20 info( "*** Creating links\n" ) 21 Link( h0, switch0 ) 22 Link( h1, switch1 ) 23 Link( switch0, switch1 ) 24 25 info( "*** Configuring hosts\n" ) 26 h0.setIP( '192.168.123.1/24' ) 27 h1.setIP( '192.168.123.2/24' ) 28 info( str( h0 ) + '\n' ) 29 info( str( h1 ) + '\n' ) 30 31 info( "*** Starting network using Open vSwitch\n" ) 32 controller.cmd( cname + ' ' + cargs + '&' ) 33 switch0.cmd( 'ovs-vsctl del-br dp0' ) 34 switch0.cmd( 'ovs-vsctl add-br dp0' ) 35 switch1.cmd( 'ovs-vsctl del-br dp1' ) 36 switch1.cmd( 'ovs-vsctl add-br dp1' ) 37 38 39 for intf in switch0.intfs.values(): 40 print intf 41 print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) 42 43 for intf in switch1.intfs.values(): 44 print intf 45 print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf ) 46 47 switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) 48 switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' ) 49 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) 50 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) 51 switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' ) 52 switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' ) 53 54 55 info( '*** Waiting for switch to connect to controller' ) 56 while 'is_connected' not in quietRun( 'ovs-vsctl show' ): 57 sleep( 1 ) 58 info( '.' ) 59 info( '\n' ) 60 print switch0.cmd( 'ovs-ofctl show dp0' ) 61 print switch1.cmd( 'ovs-ofctl show dp1' ) 62 63 64 info( "*** Running test\n" ) 65 h0.cmdPrint( 'ping -c3 ' + h1.IP() ) 66 h1.cmdPrint( 'ping -c3 ' + h0.IP() ) 67 68 info( "*** Stopping network\n" ) 69 controller.cmd( 'kill %' + cname ) 70 switch0.cmd( 'ovs-vsctl del-br dp0' ) 71 switch0.deleteIntfs() 72 switch1.cmd( 'ovs-vsctl del-br dp1' ) 73 switch1.deleteIntfs() 74 info( '\n' ) 75 76 if __name__ == '__main__': 77 setLogLevel( 'info' ) 78 info( '*** Scratch network demo (kernel datapath)\n' ) 79 Mininet.init() 80 scratchNet()
2. 改为可执行文件并将其执行:
3. 与2类似,但是在创建link的时候,设置带宽、延迟以及丢包率。
1. 创建新文件mymininet3.py并编辑以下内容:
1 #!/usr/bin/python 2 from mininet.net import Mininet 3 from mininet.node import Node 4 from mininet.link import TCLink 5 from mininet.log import setLogLevel, info 6 from mininet.util import quietRun 7 8 from time import sleep 9 10 def scratchNet( cname='controller', cargs='-v ptcp:' ): 11 "Create network from scratch using Open vSwitch." 12 13 info( "*** Creating nodes\n" ) 14 controller = Node( 'c0', inNamespace=False ) 15 switch0 = Node( 's0', inNamespace=False ) 16 switch1 = Node( 's1', inNamespace=False ) 17 h0 = Node( 'h0' ) 18 h1 = Node( 'h1' ) 19 20 info( "*** Creating links\n" ) 21 22 linkopts0=dict(bw=10) 23 linkopts1=dict(bw=10, delay='5ms', loss=10) 24 TCLink( h0, switch0,**linkopts0) 25 TCLink( h1, switch1,**linkopts0) 26 TCLink( switch0, switch1,**linkopts1) 27 28 29 info( "*** Configuring hosts\n" ) 30 h0.setIP( '192.168.123.1/24' ) 31 h1.setIP( '192.168.123.2/24' ) 32 info( str( h0 ) + '\n' ) 33 info( str( h1 ) + '\n' ) 34 35 info( "*** Starting network using Open vSwitch\n" ) 36 controller.cmd( cname + ' ' + cargs + '&' ) 37 switch0.cmd( 'ovs-vsctl del-br dp0' ) 38 switch0.cmd( 'ovs-vsctl add-br dp0' ) 39 switch1.cmd( 'ovs-vsctl del-br dp1' ) 40 switch1.cmd( 'ovs-vsctl add-br dp1' ) 41 42 43 for intf in switch0.intfs.values(): 44 print intf 45 print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) 46 47 for intf in switch1.intfs.values(): 48 print intf 49 print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf ) 50 51 switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) 52 switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' ) 53 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' ) 54 switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' ) 55 switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"' ) 56 switch1.cmd( 'ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"' ) 57 58 59 info( '*** Waiting for switch to connect to controller' ) 60 while 'is_connected' not in quietRun( 'ovs-vsctl show' ): 61 sleep( 1 ) 62 info( '.' ) 63 info( '\n' ) 64 print switch0.cmd( 'ovs-ofctl show dp0' ) 65 print switch1.cmd( 'ovs-ofctl show dp1' ) 66 67 68 info( "*** Running test\n" ) 69 h0.cmdPrint( 'ping -c3 ' + h1.IP() ) 70 h1.cmdPrint( 'ping -c3 ' + h0.IP() ) 71 72 info( "*** Stopping network\n" ) 73 controller.cmd( 'kill %' + cname ) 74 switch0.cmd( 'ovs-vsctl del-br dp0' ) 75 switch0.deleteIntfs() 76 switch1.cmd( 'ovs-vsctl del-br dp1' ) 77 switch1.deleteIntfs() 78 info( '\n' ) 79 80 if __name__ == '__main__': 81 setLogLevel( 'info' ) 82 info( '*** Scratch network demo (kernel datapath)\n' ) 83 Mininet.init() 84 scratchNet()
2. 改为可执行文件并将其执行:
实验结论:
在Mininet脚本中,可以通过使用“ovs-vsctl”命令直接控制Open vSwitch,实现通信。