【Open vSwitch】Open vSwitch使用案例扩展实验
实验参考:
实验步骤:
1. 实验任务一。
1.创建新文件ovsSingleBr.py并编辑以下内容:
1 #!/usr/bin/python 2 3 from mininet.net import Mininet 4 from mininet.node import Node 5 from mininet.link import Link 6 from mininet.log import setLogLevel, info 7 8 def myNet(): 9 "Create network from scratch using Open vSwitch." 10 11 info( "*** Creating nodes\n" ) 12 switch0 = Node( 's0', inNamespace=False ) 13 14 h0 = Node( 'h0' ) 15 h1 = Node( 'h1' ) 16 h2 = Node( 'h2' ) 17 18 info( "*** Creating links\n" ) 19 Link( h0, switch0) 20 Link( h1, switch0) 21 Link( h2, switch0) 22 23 info( "*** Configuring hosts\n" ) 24 h0.setIP( '192.168.123.1/24' ) 25 h1.setIP( '192.168.123.2/24' ) 26 h2.setIP( '192.168.123.3/24' ) 27 28 info( "*** Starting network using Open vSwitch\n" ) 29 switch0.cmd( 'ovs-vsctl del-br dp0' ) 30 switch0.cmd( 'ovs-vsctl add-br dp0' ) 31 32 for intf in switch0.intfs.values(): 33 print intf 34 print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) 35 36 # Note: controller and switch are in root namespace, and we 37 # can connect via loopback interface 38 #switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' ) 39 40 print switch0.cmd(r'ovs-vsctl show') 41 42 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=1,in_port=1,actions=flood' ) 43 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=1,in_port=2,actions=flood' ) 44 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=1,in_port=3,actions=flood' ) 45 46 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.1,actions=output:1' ) 47 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.2,actions=output:2' ) 48 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.3,actions=output:3') 49 50 #switch0.cmd('tcpdump -i s0-eth0 -U -w aaa &') 51 #h0.cmd('tcpdump -i h0-eth0 -U -w aaa &') 52 info( "*** Running test\n" ) 53 h0.cmdPrint( 'ping -c 3 ' + h1.IP() ) 54 h0.cmdPrint( 'ping -c 3 ' + h2.IP() ) 55 56 #print switch0.cmd( 'ovs-ofctl show dp0' ) 57 #print switch0.cmd( 'ovs-ofctl dump-tables dp0' ) 58 #print switch0.cmd( 'ovs-ofctl dump-ports dp0' ) 59 #print switch0.cmd( 'ovs-ofctl dump-flows dp0' ) 60 #print switch0.cmd( 'ovs-ofctl dump-aggregate dp0' ) 61 #print switch0.cmd( 'ovs-ofctl queue-stats dp0' ) 62 63 info( "*** Stopping network\n" ) 64 switch0.cmd( 'ovs-vsctl del-br dp0' ) 65 switch0.deleteIntfs() 66 info( '\n' ) 67 68 if __name__ == '__main__': 69 setLogLevel( 'info' ) 70 info( '*** Scratch network demo (kernel datapath)\n' ) 71 Mininet.init() 72 myNet()
2.改为可执行文件并将其执行:
2. 实验任务二。
1. 创建新文件ovsMultiBr.py并编辑以下内容:
1 #!/usr/bin/python 2 3 from mininet.net import Mininet 4 from mininet.node import Node 5 from mininet.link import TCLink 6 from mininet.log import setLogLevel, info 7 8 def myNet(): 9 "Create network from scratch using Open vSwitch." 10 11 info( "*** Creating nodes\n" ) 12 switch0 = Node( 's0', inNamespace=False ) 13 switch1 = Node( 's1', inNamespace=False ) 14 switch2 = Node( 's2', inNamespace=False ) 15 switch3 = Node( 's3', inNamespace=False ) 16 switch4 = Node( 's4', inNamespace=False ) 17 h0 = Node( 'h0' ) 18 h1 = Node( 'h1' ) 19 20 info( "*** Creating links\n" ) 21 linkopts0=dict(bw=100, delay='1ms', loss=0) 22 linkopts1=dict(bw=1, delay='100ms', loss=0) 23 linkopts2=dict(bw=10, delay='50ms', loss=0) 24 linkopts3=dict(bw=100, delay='1ms', loss=0) 25 TCLink( h0, switch0, **linkopts0) 26 TCLink( switch0, switch1, **linkopts0) 27 TCLink( switch0, switch2, **linkopts0) 28 TCLink( switch0, switch3, **linkopts0) 29 TCLink( switch1, switch4,**linkopts1) 30 TCLink( switch2, switch4,**linkopts2) 31 TCLink( switch3, switch4,**linkopts3) 32 TCLink( h1, switch4, **linkopts0) 33 34 info( "*** Configuring hosts\n" ) 35 h0.setIP( '192.168.123.1/24' ) 36 h1.setIP( '192.168.123.2/24' ) 37 info( str( h0 ) + '\n' ) 38 info( str( h1 ) + '\n' ) 39 40 info( "*** Starting network using Open vSwitch\n" ) 41 switch0.cmd( 'ovs-vsctl del-br dp0' ) 42 switch0.cmd( 'ovs-vsctl add-br dp0' ) 43 switch1.cmd( 'ovs-vsctl del-br dp1' ) 44 switch1.cmd( 'ovs-vsctl add-br dp1' ) 45 switch2.cmd( 'ovs-vsctl del-br dp2' ) 46 switch2.cmd( 'ovs-vsctl add-br dp2' ) 47 switch3.cmd( 'ovs-vsctl del-br dp3' ) 48 switch3.cmd( 'ovs-vsctl add-br dp3' ) 49 switch4.cmd( 'ovs-vsctl del-br dp4' ) 50 switch4.cmd( 'ovs-vsctl add-br dp4' ) 51 52 for intf in switch0.intfs.values(): 53 print intf 54 print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ) 55 56 for intf in switch1.intfs.values(): 57 print intf 58 print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf ) 59 60 for intf in switch2.intfs.values(): 61 print intf 62 print switch2.cmd( 'ovs-vsctl add-port dp2 %s' % intf ) 63 64 for intf in switch3.intfs.values(): 65 print intf 66 print switch3.cmd( 'ovs-vsctl add-port dp3 %s' % intf ) 67 68 for intf in switch4.intfs.values(): 69 print intf 70 print switch4.cmd( 'ovs-vsctl add-port dp4 %s' % intf ) 71 72 print switch1.cmd(r'ovs-ofctl add-flow dp1 idle_timeout=0,priority=1,in_port=1,actions=flood' ) 73 print switch1.cmd(r'ovs-ofctl add-flow dp1 idle_timeout=0,priority=1,in_port=1,actions=output:2' ) 74 print switch1.cmd(r'ovs-ofctl add-flow dp1 idle_timeout=0,priority=1,in_port=2,actions=output:1' ) 75 print switch2.cmd(r'ovs-ofctl add-flow dp2 idle_timeout=0,priority=1,in_port=1,actions=output:2' ) 76 print switch2.cmd(r'ovs-ofctl add-flow dp2 idle_timeout=0,priority=1,in_port=2,actions=output:1' ) 77 print switch3.cmd(r'ovs-ofctl add-flow dp3 idle_timeout=0,priority=1,in_port=1,actions=output:2' ) 78 print switch3.cmd(r'ovs-ofctl add-flow dp3 idle_timeout=0,priority=1,in_port=2,actions=output:1' ) 79 print switch4.cmd(r'ovs-ofctl add-flow dp4 idle_timeout=0,priority=1,in_port=1,actions=output:4' ) 80 print switch4.cmd(r'ovs-ofctl add-flow dp4 idle_timeout=0,priority=1,in_port=2,actions=output:4' ) 81 print switch4.cmd(r'ovs-ofctl add-flow dp4 idle_timeout=0,priority=1,in_port=3,actions=output:4' ) 82 print switch4.cmd(r'ovs-ofctl add-flow dp4 idle_timeout=0,priority=1,in_port=4,actions=output:3' ) 83 84 #print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.2,actions=output:4') 85 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.2,nw_tos=0x10,actions=output:2') 86 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.2,nw_tos=0x20,actions=output:3') 87 print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.2,nw_tos=0x30,actions=output:4') 88 #print switch0.cmd(r'ovs-ofctl add-flow dp0 idle_timeout=0,priority=10,ip,nw_dst=192.168.123.1,actions=output:1') 89 90 #switch0.cmd('tcpdump -i s0-eth0 -U -w aaa &') 91 #h0.cmd('tcpdump -i h0-eth0 -U -w aaa &') 92 info( "*** Running test\n" ) 93 h0.cmdPrint( 'ping -Q 0x10 -c 3 ' + h1.IP() ) 94 h0.cmdPrint( 'ping -Q 0x20 -c 3 ' + h1.IP() ) 95 h0.cmdPrint( 'ping -Q 0x30 -c 3 ' + h1.IP() ) 96 #h1.cmdPrint('iperf -s -p 12345 -u &') 97 #h0.cmdPrint('iperf -c ' + h1.IP() +' -u -b 10m -p 12345 -t 10 -i 1') 98 99 #print switch0.cmd( 'ovs-ofctl show dp0' ) 100 #print switch1.cmd( 'ovs-ofctl show dp1' ) 101 #print switch2.cmd( 'ovs-ofctl show dp2' ) 102 #print switch3.cmd( 'ovs-ofctl show dp3' ) 103 #print switch4.cmd( 'ovs-ofctl show dp4' ) 104 #print switch0.cmd( 'ovs-ofctl dump-tables dp0' ) 105 #print switch0.cmd( 'ovs-ofctl dump-ports dp0' ) 106 #print switch0.cmd( 'ovs-ofctl dump-flows dp0' ) 107 #print switch0.cmd( 'ovs-ofctl dump-aggregate dp0' ) 108 #print switch0.cmd( 'ovs-ofctl queue-stats dp0' ) 109 110 #print "Testing video transmission between h1 and h2" 111 #h1.cmd('./myrtg_svc -u > myrd &') 112 #h0.cmd('./mystg_svc -trace st 192.168.123.2') 113 114 info( "*** Stopping network\n" ) 115 switch0.cmd( 'ovs-vsctl del-br dp0' ) 116 switch0.deleteIntfs() 117 switch1.cmd( 'ovs-vsctl del-br dp1' ) 118 switch1.deleteIntfs() 119 switch2.cmd( 'ovs-vsctl del-br dp2' ) 120 switch2.deleteIntfs() 121 switch3.cmd( 'ovs-vsctl del-br dp3' ) 122 switch3.deleteIntfs() 123 switch4.cmd( 'ovs-vsctl del-br dp4' ) 124 switch4.deleteIntfs() 125 info( '\n' ) 126 127 if __name__ == '__main__': 128 setLogLevel( 'info' ) 129 info( '*** Scratch network demo (kernel datapath)\n' ) 130 Mininet.init() 131 myNet()
2. 改为可执行文件并将其执行:
实验结论:
此实验并未连接控制器,只通过脚本在单个/多个交换机中下发静态流表实现主机间的通信。在给多个交换机下发流表时,通过ping操作测试验证主机间的连通性,并通过-Q参数设置不同的tos值验证主机间的连通性及到达目的地址的时间,通过验证发现,tos值设置越大,时间使用越少。