首页 > 技术文章 > 软件定义网络-实验 4:Open vSwitch 实验——Mininet 中使用 OVS 命令

vistor94694 2020-09-23 20:45 原文


一、实验目的
Mininet 安装之后,会连带安装 Open vSwitch,可以直接通过 Python 脚本调用
Open vSwitch 命令,从而直接控制 Open vSwitch,通过实验了解调用控制的方法

二、实验任务
在本实验中,使用 Mininet 基于 Python 的脚本,调用“ovs-vsctl”命令直接控制
Open vSwitch。使用默认的交换机泛洪规则,设置更高的优先级规则进行预先定
义 IP 报文的转发。在多个交换机中通过设置不同 TOS 值的数据包将通过不同的
方式到达目的地址,验证主机间的连通性及到达目的地址的时间。
三、实验步骤
1. 实验环境
安装了 Ubuntu 18.04.5 Desktop amd64 的虚拟机
2.实验过程

学习 ovsSingleBr.py 和 ovsMultiBr.py,在下图拓扑中实现一个 VLAN。 

实验代码:

 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 def myNet():
 7     "Create network from scratch using Open vSwitch."
 8     info( "*** Creating nodes\n" )
 9     switch0 = Node( 's0', inNamespace=False )
10     switch1 = Node( 's1', inNamespace=False )
11     
12     h0 = Node( 'h0' )
13     h1 = Node( 'h1' )
14     h2 = Node( 'h2' )
15     h3 = Node( 'h3' )
16  
17     info( "*** Creating links\n" )
18     linkopts0=dict(bw=100, delay='1ms', loss=0)
19     linkopts1=dict(bw=1, delay='100ms', loss=0)
20 
21     TCLink( h0, switch0, **linkopts0)
22     TCLink( h1, switch0, **linkopts1)
23     TCLink( h2, switch1, **linkopts0)
24     TCLink( h3, switch1, **linkopts1)
25     TCLink( switch0, switch1, **linkopts1)
26  
27     info( "*** Configuring hosts\n" )
28     h0.setIP( '192.168.123.1/24' )
29     h1.setIP( '192.168.123.2/24' )
30     h2.setIP( '192.168.123.3/24' )
31     h3.setIP( '192.168.123.4/24' )
32        
33     info( "*** Starting network using Open vSwitch\n" )
34     switch0.cmd( 'ovs-vsctl del-br dp0' )
35     switch0.cmd( 'ovs-vsctl add-br dp0' )
36     switch1.cmd( 'ovs-vsctl del-br dp1' )
37     switch1.cmd( 'ovs-vsctl add-br dp1' )
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    
48     print switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
49     print switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
50     print switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
51     print switch0.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp0 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
52 
53     print switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3')
54     print switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3')
55     print switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=0,actions=pop_vlan,output:1')
56     print switch1.cmd(r'ovs-ofctl -O OpenFlow13 add-flow dp1 priority=1,dl_vlan=1,actions=pop_vlan,output:2')
57    
58  
59     info( "*** Running test\n" )
60     h0.cmdPrint( 'ping -Q 0x90 -c 3 ' + h1.IP() )
61     h0.cmdPrint( 'ping -Q 0x90 -c 3 ' + h2.IP() )
62     h0.cmdPrint( 'ping -Q 0x90 -c 3 ' + h3.IP() )
63     h1.cmdPrint( 'ping -Q 0x90 -c 3 ' + h2.IP() )
64     h1.cmdPrint( 'ping -Q 0x90 -c 3 ' + h3.IP() )
65     h2.cmdPrint( 'ping -Q 0x90 -c 3 ' + h3.IP() )
66 
67  
68     info( "*** Stopping network\n" )
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     myNet()

上述代码将 h0 和 h2 划分在 VLAN 0 中,h1 和 h3 划分在 VLAN 1 中,由于拓扑没
有控制器,并且初始化时删除了交换机中的所有流表,因此除非下发流表,否则
主机之间网络无法连通。请尝试修改代码,利用 ovs 命令直接下发 VLAN 设置的
流表项,最终测试 h0 和 h2 互通h1 和 h3 互通其余主机均不通,结果如下图。

 

推荐阅读