一)基本要求
-
实验2:Open vSwitch虚拟交换机实践
一、实验目的
- 能够对Open vSwitch进行基本操作;
- 能够通过命令行终端使用OVS命令操作Open vSwitch交换机,管理流表;
- 能够通过Mininet的Python代码运行OVS命令,控制网络拓扑中的Open vSwitch交换机
二、实验环境
Ubuntu 20.04 Desktop amd64
三、实验要求
(一)基本要求
- ovs-vsctl基础操作实践:创建OVS交换机,以ovs-xxxxxxxxx命名,其中xxxxxxxxx为本人学号。在创建的交换机上增加端口p0和p1,设置p0的端口号为100,p1的端口号为101,类型均为internal;为了避免网络接口上的地址和本机已有网络地址冲突,需要创建虚拟网络空间(参考命令netns)ns0和ns1,分别将p0和p1移入,并分别配置p0和p1端口的ip地址为190.168.1.100、192.168.1.101,子网掩码为255.255.255.0;最后测试p0和p1的连通性。
- 使用Mininet搭建的SDN拓扑,如下图所示,要求支持OpenFlow 1.3协议,主机名、交换机名以及端口对应正确。
from mininet.topo import Topo
topos = {'mytopo': (lambda: MyTopo())}
class MyTopo(Topo):
def __init__(self):# initilaize topology
Topo.__init__(self)
# add hosts and switches
h1 = self.addHost('h1')
h2 = self.addHost('h2')
h3 = self.addHost('h3')h4 = self.addHost('h4')
s1 = self.addSwitch('s1')
s2 = self.addSwitch('s2')# add links
self.addLink(h1, s1, 1, 1)
self.addLink(h2, s1, 1, 2)
self.addLink(h3, s2, 1, 1)
self.addLink(h4, s2, 1, 2)
self.addLink(s1,s2,3,3) - 通过命令行终端输入“ovs-ofctl”命令,直接在s1和s2上添加流表,划分出所要求的VLAN。
VLAN_ID Hosts 0 h1 h3 1 h2 h4 - 主机连通性要求:
- h1 – h3互通
- h2 – h4互通
- 其余主机不通
(二)进阶要求
阅读SDNLAB实验使用Mininet,编写Python代码,生成(一)中的SDN拓扑,并在代码中直接使用OVS命令,做到可以直接运行Python程序完成和(一)相同的VLAN划分。
1 from mininet.net import Mininet 2 from mininet.node import Controller, RemoteController, OVSController 3 from mininet.node import CPULimitedHost, Host, Node 4 from mininet.node import OVSKernelSwitch, UserSwitch 5 from mininet.node import IVSSwitch 6 from mininet.cli import CLI 7 from mininet.log import setLogLevel, info 8 from mininet.link import TCLink, Intf 9 from subprocess import call 10 def myNetwork(): 11 net = Mininet( topo=None, 12 build=False, 13 ipBase='10.0.0.0/24') 14 info( '*** Adding controller\n' ) 15 c0=net.addController(name='c0', 16 controller=Controller, 17 protocol='tcp', 18 port=6633) 19 info( '*** Add switches\n') 20 s1 = net.addSwitch('s1', cls=OVSKernelSwitch) 21 s2 = net.addSwitch('s2', cls=OVSKernelSwitch) 22 info( '*** Add hosts\n') 23 h1 = net.addHost('h1', cls=Host, ip='10.0.0.1/24', defaultRoute=None) 24 h2 = net.addHost('h2', cls=Host, ip='10.0.0.2/24', defaultRoute=None) 25 h3 = net.addHost('h3', cls=Host, ip='10.0.0.3/24', defaultRoute=None) 26 h4 = net.addHost('h4', cls=Host, ip='10.0.0.4/24', defaultRoute=None) 27 info( '*** Add links\n') 28 net.addLink(h1, s1,1,1) 29 net.addLink(s1, h2,2,1) 30 net.addLink(s1, s2,3,3) 31 net.addLink(s2, h4,2,1) 32 net.addLink(s2, h3,1,1) 33 info( '*** Starting network\n') 34 net.build() 35 info( '*** Starting controllers\n') 36 for controller in net.controllers: 37 controller.start() 38 info( '*** Starting switches\n') 39 net.get('s1').start([c0]) 40 net.get('s2').start([c0]) 41 info( '*** Post configure switches and hosts\n') 42 s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3') 43 s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3') 44 s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=0,actions=pop_vlan,output:1') 45 s1.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s1 priority=1,dl_vlan=1,actions=pop_vlan,output:2') 46 s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=1,actions=push_vlan:0x8100,set_field:4096-\>vlan_vid,output:3') 47 s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,in_port=2,actions=push_vlan:0x8100,set_field:4097-\>vlan_vid,output:3') 48 s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=0,actions=pop_vlan,output:1') 49 s2.cmd('sudo ovs-ofctl -O OpenFlow13 add-flow s2 priority=1,dl_vlan=1,actions=pop_vlan,output:2') 50 CLI(net) 51 net.stop() 52 if __name__ == '__main__': 53 setLogLevel( 'info' ) 54 myNetwork()
个人总结:
- 实验这次要一(3)要运行sudo mn --custom text2.py --topo mytopo --controller=remote,ip=127.0.0.1,port=6653 --switch ovsk,protocols=OpenFlow13,输入“ovs-ofctl”命令,才能找到s1和 s2。这点理解完实验就简单了。这次实验python代码把上次的实验代码改一下就完成了,根本没必要重新写。这次利用了wireshark抓包,在运行pingall就可以看到wireshark抓包软件有内容了,可以开始截图,进阶也就是把前面内容用python实现,也不难,前面理解,进阶直接理解。老师文件上的命令,有些复制下来会报错。必须要理解复制。