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值設置越大,時間使用越少
轉載于:https://www.cnblogs.com/a1164520408/p/9629255.html
總結
以上是生活随笔為你收集整理的Open vSwitch系列实验(一):Open vSwitch使用案例扩展实验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java高级-多线程编程
- 下一篇: 算法: 把字字符串转化为整数;