实验原理
在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为。在本实验中,使用Mininet基于python的脚本,调用“ovs-vsctl”命令直接控制Open vSwitch。
实验内容
一台交换机两台主机,实现从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口
mininet脚本代码如下:
1 from mininet.net import Mininet 2 from mininet.node import Node 3 from mininet.link import Link 4 from mininet.log import setLogLevel, info 5 from mininet.util import quietRun 6 7 from time import sleep 8 9 def scratchNet( cname=‘controller‘, cargs=‘-v ptcp:‘ ): 10 "Create network from scratch using Open vSwitch." 11 12 info( "*** Creating nodes\n" ) 13 controller = Node( ‘c0‘, inNamespace=False ) 14 switch0 = Node( ‘s0‘, inNamespace=False ) 15 h0 = Node( ‘h0‘ ) 16 h1 = Node( ‘h1‘ ) 17 18 info( "*** Creating links\n" ) 19 Link( h0, switch0 ) 20 Link( h1, switch0 ) 21 22 info( "*** Configuring hosts\n" ) 23 h0.setIP( ‘192.168.123.1/24‘ ) 24 h1.setIP( ‘192.168.123.2/24‘ ) 25 info( str( h0 ) + ‘\n‘ ) 26 info( str( h1 ) + ‘\n‘ ) 27 28 info( "*** Starting network using Open vSwitch\n" ) 29 controller.cmd( cname + ‘ ‘ + cargs + ‘&‘ ) 30 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 31 switch0.cmd( ‘ovs-vsctl add-br dp0‘ ) 32 33 for intf in switch0.intfs.values(): 34 print (intf) 35 print (switch0.cmd( ‘ovs-vsctl add-port dp0 %s‘ % intf )) 36 switch0.cmd( ‘ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633‘ ) 37 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"‘ ) 38 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"‘ ) 39 40 info( ‘*** Waiting for switch to connect to controller‘ ) 41 while ‘is_connected‘ not in quietRun( ‘ovs-vsctl show‘ ): 42 sleep( 1 ) 43 info( ‘.‘ ) 44 info( ‘\n‘ ) 45 46 info( "*** Running test\n" ) 47 h0.cmdPrint( ‘ping -c6 ‘ + h1.IP() ) 48 h1.cmdPrint( ‘ping -c6 ‘ + h0.IP() ) 49 50 info( "*** Stopping network\n" ) 51 controller.cmd( ‘kill %‘ + cname ) 52 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 53 switch0.deleteIntfs() 54 info( ‘\n‘ ) 55 56 if __name__ == ‘__main__‘: 57 setLogLevel( ‘info‘ ) 58 info( ‘*** Scratch network demo (kernel datapath)\n‘ ) 59 Mininet.init() 60 scratchNet()
其实,源码中的也可以不用连接controller,直接就可以使用“ovs-vsctl”命令控制控制器。也就是代码中的 switch0.cmd( ‘ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633‘ )其实不是那么必要。
两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口要求同脚本1
mininet代码如下:
1 from mininet.net import Mininet 2 from mininet.node import Node 3 from mininet.link import Link 4 from mininet.log import setLogLevel, info 5 from mininet.util import quietRun 6 7 from time import sleep 8 9 def scratchNet( cname=‘controller‘, cargs=‘-v ptcp:‘ ): 10 "Create network from scratch using Open vSwitch." 11 12 info( "*** Creating nodes\n" ) 13 controller = Node( ‘c0‘, inNamespace=False ) 14 switch0 = Node( ‘s0‘, inNamespace=False ) 15 switch1 = Node( ‘s1‘, inNamespace=False ) 16 h0 = Node( ‘h0‘ ) 17 h1 = Node( ‘h1‘ ) 18 19 info( "*** Creating links\n" ) 20 Link( h0, switch0 ) 21 Link( h1, switch1 ) 22 Link( switch0, switch1 ) 23 24 info( "*** Configuring hosts\n" ) 25 h0.setIP( ‘192.168.123.1/24‘ ) 26 h1.setIP( ‘192.168.123.2/24‘ ) 27 info( str( h0 ) + ‘\n‘ ) 28 info( str( h1 ) + ‘\n‘ ) 29 30 info( "*** Starting network using Open vSwitch\n" ) 31 controller.cmd( cname + ‘ ‘ + cargs + ‘&‘ ) 32 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 33 switch0.cmd( ‘ovs-vsctl add-br dp0‘ ) 34 switch1.cmd( ‘ovs-vsctl del-br dp1‘ ) 35 switch1.cmd( ‘ovs-vsctl add-br dp1‘ ) 36 37 38 for intf in switch0.intfs.values(): 39 print (intf) 40 print (switch0.cmd( ‘ovs-vsctl add-port dp0 %s‘ % intf )) 41 42 for intf in switch1.intfs.values(): 43 print (intf) 44 print (switch1.cmd( ‘ovs-vsctl add-port dp1 %s‘ % intf )) 45 46 switch0.cmd( ‘ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633‘ ) 47 switch1.cmd( ‘ovs-vsctl set-controller dp1 tcp:127.0.0.1:6633‘ ) 48 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"‘ ) 49 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"‘ ) 50 switch1.cmd( ‘ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"‘ ) 51 switch1.cmd( ‘ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"‘ ) 52 53 54 info( ‘*** Waiting for switch to connect to controller‘ ) 55 while ‘is_connected‘ not in quietRun( ‘ovs-vsctl show‘ ): 56 sleep( 1 ) 57 info( ‘.‘ ) 58 info( ‘\n‘ ) 59 print (switch0.cmd( ‘ovs-ofctl show dp0‘ )) 60 print (switch1.cmd( ‘ovs-ofctl show dp1‘ )) 61 62 63 info( "*** Running test\n" ) 64 h0.cmdPrint( ‘ping -c3 ‘ + h1.IP() ) 65 h1.cmdPrint( ‘ping -c3 ‘ + h0.IP() ) 66 67 info( "*** Stopping network\n" ) 68 controller.cmd( ‘kill %‘ + cname ) 69 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 70 switch0.deleteIntfs() 71 switch1.cmd( ‘ovs-vsctl del-br dp1‘ ) 72 switch1.deleteIntfs() 73 info( ‘\n‘ ) 74 75 if __name__ == ‘__main__‘: 76 setLogLevel( ‘info‘ ) 77 info( ‘*** Scratch network demo (kernel datapath)\n‘ ) 78 Mininet.init() 79 scratchNet()
在脚本2的基础上设置带宽、延迟以及丢包率
mininet代码如下:
1 from mininet.net import Mininet 2 from mininet.node import Node 3 from mininet.link import TCLink 4 from mininet.log import setLogLevel, info 5 from mininet.util import quietRun 6 7 from time import sleep 8 9 def scratchNet( cname=‘controller‘, cargs=‘-v ptcp:‘ ): 10 "Create network from scratch using Open vSwitch." 11 12 info( "*** Creating nodes\n" ) 13 controller = Node( ‘c0‘, inNamespace=False ) 14 switch0 = Node( ‘s0‘, inNamespace=False ) 15 switch1 = Node( ‘s1‘, inNamespace=False ) 16 h0 = Node( ‘h0‘ ) 17 h1 = Node( ‘h1‘ ) 18 19 info( "*** Creating links\n" ) 20 21 linkopts0=dict(bw=10) 22 linkopts1=dict(bw=10, delay=‘5ms‘, loss=10) 23 TCLink( h0, switch0,**linkopts0) 24 TCLink( h1, switch1,**linkopts0) 25 TCLink( switch0, switch1,**linkopts1) 26 27 28 info( "*** Configuring hosts\n" ) 29 h0.setIP( ‘192.168.123.1/24‘ ) 30 h1.setIP( ‘192.168.123.2/24‘ ) 31 info( str( h0 ) + ‘\n‘ ) 32 info( str( h1 ) + ‘\n‘ ) 33 34 info( "*** Starting network using Open vSwitch\n" ) 35 controller.cmd( cname + ‘ ‘ + cargs + ‘&‘ ) 36 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 37 switch0.cmd( ‘ovs-vsctl add-br dp0‘ ) 38 switch1.cmd( ‘ovs-vsctl del-br dp1‘ ) 39 switch1.cmd( ‘ovs-vsctl add-br dp1‘ ) 40 41 42 for intf in switch0.intfs.values(): 43 print (intf) 44 print (switch0.cmd( ‘ovs-vsctl add-port dp0 %s‘ % intf )) 45 46 for intf in switch1.intfs.values(): 47 print (intf) 48 print (switch1.cmd( ‘ovs-vsctl add-port dp1 %s‘ % intf )) 49 50 switch0.cmd( ‘ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633‘ ) 51 switch1.cmd( ‘ovs-vsctl set-controller dp1 tcp:127.0.0.1:6633‘ ) 52 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"‘ ) 53 switch0.cmd( ‘ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"‘ ) 54 switch1.cmd( ‘ovs-ofctl add-flow dp1 \"in_port=1 actions=output:2\"‘ ) 55 switch1.cmd( ‘ovs-ofctl add-flow dp1 \"in_port=2 actions=output:1\"‘ ) 56 57 58 info( ‘*** Waiting for switch to connect to controller‘ ) 59 while ‘is_connected‘ not in quietRun( ‘ovs-vsctl show‘ ): 60 sleep( 1 ) 61 info( ‘.‘ ) 62 info( ‘\n‘ ) 63 print (switch0.cmd( ‘ovs-ofctl show dp0‘ )) 64 print (switch1.cmd( ‘ovs-ofctl show dp1‘ )) 65 66 67 info( "*** Running test\n" ) 68 h0.cmdPrint( ‘ping -c3 ‘ + h1.IP() ) 69 h1.cmdPrint( ‘ping -c3 ‘ + h0.IP() ) 70 71 info( "*** Stopping network\n" ) 72 controller.cmd( ‘kill %‘ + cname ) 73 switch0.cmd( ‘ovs-vsctl del-br dp0‘ ) 74 switch0.deleteIntfs() 75 switch1.cmd( ‘ovs-vsctl del-br dp1‘ ) 76 switch1.deleteIntfs() 77 info( ‘\n‘ ) 78 79 if __name__ == ‘__main__‘: 80 setLogLevel( ‘info‘ ) 81 info( ‘*** Scratch network demo (kernel datapath)\n‘ ) 82 Mininet.init() 83 scratchNet()
原文地址:https://www.cnblogs.com/pullself/p/10225343.html
时间: 2024-10-16 04:15:08