-
实验内容
本实验在基于 Mininet 脚本的不同拓扑环境下使用 OpenDaylight 控制交换机行为。任务一:一台交换机两台主机,从1端口进入的数据流转发到 2 端口,从 2 端口进入的数据流转发到1端口;任务二:两台交换机两台主机,主机 0 连接交换机 0,主机 1 连接交换机 1,交换机 0 连接交换机 1,数据转发端口同任务一;任务三:与任务二类似,但是在创建 link 的时候,设置带宽、延迟以及丢包率。
-
参考
-
实验环境
虚拟机: Oracle VM VirtualBox Ubuntu16.04LTS
-
实验步骤
任务一:一台交换机两台主机,实现从 1 端口进入的数据流转发到 2 端口,从 2 端口进入的数据流转发到 1 端口。
1.在~/mininet目录里新建文件 mymininet1.py
# cd mininet
# touch mininet
# vim mymininet1.py
脚本内容如下:
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Node
from mininet.link import Link
from mininet.log import setLogLevel, info
from mininet.util import quietRun
from time import sleep
def scratchNet( cname='controller', cargs='-v ptcp:' ):
"Create network from scratch using Open vSwitch."
info( "*** Creating nodes
" )
controller = Node( 'c0', inNamespace=False )
switch0 = Node( 's0', inNamespace=False )
h0 = Node( 'h0' )
h1 = Node( 'h1' )
info( "*** Creating links
" )
Link( h0, switch0 )
Link( h1, switch0 )
info( "*** Configuring hosts
" )
h0.setIP( '192.168.123.1/24' )
h1.setIP( '192.168.123.2/24' )
info( str( h0 ) + '
' )
info( str( h1 ) + '
' )
info( "*** Starting network using Open vSwitch
" )
controller.cmd( cname + ' ' + cargs + '&' )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.cmd( 'ovs-vsctl add-br dp0' )
for intf in switch0.intfs.values():
print intf
print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.2.15:6633' )
# 这里的 IP 地址改为自己本机的,例如我的是10.0.2.15
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' )
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' )
info( '*** Waiting for switch to connect to controller' )
while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
sleep( 1 )
info( '.' )
info( '
' )
info( "*** Running test
" )
h0.cmdPrint( 'ping -c6 ' + h1.IP() )
h1.cmdPrint( 'ping -c6 ' + h0.IP() )
info( "*** Stopping network
" )
controller.cmd( 'kill %' + cname )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.deleteIntfs()
info( '
' )
if __name__ == '__main__':
setLogLevel( 'info' )
info( '*** Scratch network demo (kernel datapath)
' )
Mininet.init()
scratchNet()
2.执行该脚本
# python mymininet1.py
任务二:两台交换机两台主机,主机 0 连接交换机 0,主机 1 连接交换机 1,交换机 0 连接交换机 1,数据转发端口要求同任务一。
1.在~/mininet目录里新建文件 mymininet2.py
# cd mininet
# touch mininet
# vim mymininet2.py
脚本内容如下:
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Node
from mininet.link import Link
from mininet.log import setLogLevel, info
from mininet.util import quietRun
from time import sleep
def scratchNet( cname='controller', cargs='-v ptcp:' ):
"Create network from scratch using Open vSwitch."
info( "*** Creating nodes
" )
controller = Node( 'c0', inNamespace=False )
switch0 = Node( 's0', inNamespace=False )
switch1 = Node( 's1', inNamespace=False )
h0 = Node( 'h0' )
h1 = Node( 'h1' )
info( "*** Creating links
" )
Link( h0, switch0 )
Link( h1, switch1 )
Link( switch0, switch1 )
info( "*** Configuring hosts
" )
h0.setIP( '192.168.123.1/24' )
h1.setIP( '192.168.123.2/24' )
info( str( h0 ) + '
' )
info( str( h1 ) + '
' )
info( "*** Starting network using Open vSwitch
" )
controller.cmd( cname + ' ' + cargs + '&' )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.cmd( 'ovs-vsctl add-br dp0' )
switch1.cmd( 'ovs-vsctl del-br dp1' )
switch1.cmd( 'ovs-vsctl add-br dp1' )
for intf in switch0.intfs.values():
print intf
print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
for intf in switch1.intfs.values():
print intf
print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )
switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' )
# 这里的 IP 地址改为自己本机的,例如我的是10.0.2.15
switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' )
# 这里的 IP 地址改为自己本机的,例如我的是10.0.2.15
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' )
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' )
switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=1 actions=output:2"' )
switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=2 actions=output:1"' )
info( '*** Waiting for switch to connect to controller' )
while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
sleep( 1 )
info( '.' )
info( '
' )
print switch0.cmd( 'ovs-ofctl show dp0' )
print switch1.cmd( 'ovs-ofctl show dp1' )
info( "*** Running test
" )
h0.cmdPrint( 'ping -c3 ' + h1.IP() )
h1.cmdPrint( 'ping -c3 ' + h0.IP() )
info( "*** Stopping network
" )
controller.cmd( 'kill %' + cname )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.deleteIntfs()
switch1.cmd( 'ovs-vsctl del-br dp1' )
switch1.deleteIntfs()
info( '
' )
if __name__ == '__main__':
setLogLevel( 'info' )
info( '*** Scratch network demo (kernel datapath)
' )
Mininet.init()
scratchNet()
2.执行该脚本
# python mymininet2.py
任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。
1.在~/mininet目录里新建文件 mymininet3.py
# cd mininet
# touch mininet
# vim mymininet3.py
脚本内容如下:
#!/usr/bin/python
from mininet.net import Mininet
from mininet.node import Node
from mininet.link import TCLink
from mininet.log import setLogLevel, info
from mininet.util import quietRun
from time import sleep
def scratchNet( cname='controller', cargs='-v ptcp:' ):
"Create network from scratch using Open vSwitch."
info( "*** Creating nodes
" )
controller = Node( 'c0', inNamespace=False )
switch0 = Node( 's0', inNamespace=False )
switch1 = Node( 's1', inNamespace=False )
h0 = Node( 'h0' )
h1 = Node( 'h1' )
info( "*** Creating links
" )
linkopts0=dict(bw=10)
linkopts1=dict(bw=10, delay='5ms', loss=10)
TCLink( h0, switch0,**linkopts0)
TCLink( h1, switch1,**linkopts0)
TCLink( switch0, switch1,**linkopts1)
info( "*** Configuring hosts
" )
h0.setIP( '192.168.123.1/24' )
h1.setIP( '192.168.123.2/24' )
info( str( h0 ) + '
' )
info( str( h1 ) + '
' )
info( "*** Starting network using Open vSwitch
" )
controller.cmd( cname + ' ' + cargs + '&' )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.cmd( 'ovs-vsctl add-br dp0' )
switch1.cmd( 'ovs-vsctl del-br dp1' )
switch1.cmd( 'ovs-vsctl add-br dp1' )
for intf in switch0.intfs.values():
print intf
print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
for intf in switch1.intfs.values():
print intf
print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )
switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' )
# 这里的 IP 地址改为自己本机的,例如我的是10.0.2.15
switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' )
# 这里的 IP 地址改为自己本机的,例如我的是10.0.2.15
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' )
switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' )
switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=1 actions=output:2"' )
switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=2 actions=output:1"' )
info( '*** Waiting for switch to connect to controller' )
while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
sleep( 1 )
info( '.' )
info( '
' )
print switch0.cmd( 'ovs-ofctl show dp0' )
print switch1.cmd( 'ovs-ofctl show dp1' )
info( "*** Running test
" )
h0.cmdPrint( 'ping -c3 ' + h1.IP() )
h1.cmdPrint( 'ping -c3 ' + h0.IP() )
info( "*** Stopping network
" )
controller.cmd( 'kill %' + cname )
switch0.cmd( 'ovs-vsctl del-br dp0' )
switch0.deleteIntfs()
switch1.cmd( 'ovs-vsctl del-br dp1' )
switch1.deleteIntfs()
info( '
' )
if __name__ == '__main__':
setLogLevel( 'info' )
info( '*** Scratch network demo (kernel datapath)
' )
Mininet.init()
scratchNet()
2.执行该脚本
# python mymininet3.py
-
总结
1. 实验过程中遇到的问题
- 一开始忘记修改代码中控制器的 IP 地址导致连接不上
2. 收获
- 熟悉 Mininet 自定义拓扑脚本的编写;
- 掌握使用「ovs-vsctl」命令直接控制 Open vSwitch。