Open vSwitch系列实验(一):Open vSwitch使用案例扩展实验
一、实验目的
- 通过python脚本调用OpenvSwitch命令;
- 学习Mininet基于python脚本创建拓扑的实现;
- 进一步深度使用“ovs-vsctl”命令直接控制Open vSwitch。
二、实验原理
在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为。在本实验中,使用Mininet基于python的脚本,调用“ovs-vsctl”命令直接控制Open vSwitch。使用默认的交换机泛洪规则,设置更高的优先级规则进行预先定义IP报文的转发。在多个交换机中通过设置不同TOS值的数据包将通过不同的方式到达目的地址,验证主机间的连通性及到达目的的时间。
三、实验任务
使用默认的交换机泛洪规则,设置更高的优先级规则进行预先定义IP报文的转发,不同TOS值的数据包将通过不同的方式到达目的地址。
TOS包括共8位,包括3 bit的优先权字段(取值可以从000-111所有值),4 bit的TOS子字段和1 bit未用位但必须置0。
3bit的8个优先级的定义如下:
111--Network Control(网络控制);
110--Internetwork Control(网间控制);
101--Critic(关键);
100--Flash Override(疾速);
011--Flash(闪速);
010--Immediate(快速);
001--Priority(优先);
000--Routine(普通)。
优先级6和7一般保留给网络控制数据使用,如路由。
优先级5推荐给语音数据使用。
优先级4由视频会议和视频流使用。
优先级3给语音控制数据使用。
优先级1和2给数据业务使用。
优先级0为默认标记值。
1.ovsSingleBr
1 #!/usr/bin/python 2 #调用mininet模块 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 #mynet函数 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 #print all the intf of the values 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.ovsMultiBr
这里通过设置不通的tos值和和不通的优先级来改变数据包的转发方式,其中在代码中可以看出priority的值,然后tos的值用的是16进制来表示,TOS表示服务类型,一共8bits,比如0x30:00110000,然后有相应的对照,http://www.mamicode.com/info-detail-542445.html
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()
总结:
1.泛洪规则是根据优先级以及tos值而定
2.tos值设置越大,时间使用越少