Mininet系列实验(七):Mininet脚本实现控制交换机行为

1 实验目的

  • 熟悉Mininet自定义拓扑脚本的编写;
  • 掌握使用“ovs-vsctl”命令直接控制Open vSwitch。

2 实验原理

在SDN环境中,控制器可以通过对交换机下发流表操作来控制交换机的转发行为。在本实验中,使用Mininet基于python的脚本,调用“ovs-vsctl”命令直接控制Open vSwitch。

3 实验任务

本实验在基于Mininet脚本的不同拓扑环境下使用OpenDaylight控制交换机行为。任务一:一台交换机两台主机,从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口;任务二:两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口同任务一;任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。

任务一:一台交换机两台主机,实现从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口。

还是那个流程:

打开mininet文件夹——创建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
# 调用Mininet相关模块

from time import sleep
# 调用sleep模块 

def scratchNet( cname='controller', cargs='-v ptcp:' ):
    "Create network from scratch using Open vSwitch."

    # 创建网络节点其中有两个主机,一个交换机,一个控制器
    info( "*** Creating nodes\n" )
    controller = Node( 'c0', inNamespace=False )
    switch0 = Node( 's0', inNamespace=False )
    h0 = Node( 'h0' )
    h1 = Node( 'h1' )

    # 创建网络连接,分别是h0-s0和h1-s0
    info( "*** Creating links\n" )
    Link( h0, switch0 )
    Link( h1, switch0 )

    # 设置主机的IP地址,然后再诊断的同时打印出string化的h0h1
    info( "*** Configuring hosts\n" )
    h0.setIP( '192.168.123.1/24' )
    h1.setIP( '192.168.123.2/24' )
    info( str( h0 ) + '\n' )
    info( str( h1 ) + '\n' )
 
    # 开启控制器和s0的链路
    info( "*** Starting network using Open vSwitch\n" )
    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.0.8:6633' )
    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( '\n' )

    # 两个互ping6次
    info( "*** Running test\n" )
    h0.cmdPrint( 'ping -c6 ' + h1.IP() )
    h1.cmdPrint( 'ping -c6 ' + h0.IP() )

    # 关闭这个网络
    info( "*** Stopping network\n" )
    controller.cmd( 'kill %' + cname )
    switch0.cmd( 'ovs-vsctl del-br dp0' )
    switch0.deleteIntfs()
    info( '\n' )

# 主函数部分,设置打印等级,还有调用以上的一些函数
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )
    Mininet.init()
    scratchNet()

 Mininet系列实验(七):Mininet脚本实现控制交换机行为_第1张图片

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第2张图片

任务二:两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口要求同任务一。

和前者的脚本大同小异,这里就解释mymininet1.py没有的,print switch0.cmd( 'ovs-ofctl show dp0' )意思就是打出关于s0的信息

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第3张图片

#!/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\n" )
    controller = Node( 'c0', inNamespace=False )
    switch0 = Node( 's0', inNamespace=False )
    switch1 = Node( 's1', inNamespace=False )
    h0 = Node( 'h0' )
    h1 = Node( 'h1' )
 
    info( "*** Creating links\n" )
    Link( h0, switch0 )
    Link( h1, switch1 )
    Link( switch0, switch1 )
 
    info( "*** Configuring hosts\n" )
    h0.setIP( '192.168.123.1/24' )
    h1.setIP( '192.168.123.2/24' )
    info( str( h0 ) + '\n' )
    info( str( h1 ) + '\n' )
 
    info( "*** Starting network using Open vSwitch\n" )
    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' )
    switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' )
    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( '\n' )
    print switch0.cmd( 'ovs-ofctl show dp0' )
    print switch1.cmd( 'ovs-ofctl show dp1' )
 
 
    info( "*** Running test\n" )
    h0.cmdPrint( 'ping -c3 ' + h1.IP() )
    h1.cmdPrint( 'ping -c3 ' + h0.IP() )
 
    info( "*** Stopping network\n" )
    controller.cmd( 'kill %' + cname )
    switch0.cmd( 'ovs-vsctl del-br dp0' )
    switch0.deleteIntfs()
    switch1.cmd( 'ovs-vsctl del-br dp1' )
    switch1.deleteIntfs()
    info( '\n' )
 
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )
    Mininet.init()
    scratchNet()

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第4张图片

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第5张图片

任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。

#!/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\n" )
    controller = Node( 'c0', inNamespace=False )
    switch0 = Node( 's0', inNamespace=False )
    switch1 = Node( 's1', inNamespace=False )
    h0 = Node( 'h0' )
    h1 = Node( 'h1' )
 
    info( "*** Creating links\n" )
 
    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\n" )
    h0.setIP( '192.168.123.1/24' )
    h1.setIP( '192.168.123.2/24' )
    info( str( h0 ) + '\n' )
    info( str( h1 ) + '\n' )
 
    info( "*** Starting network using Open vSwitch\n" )
    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' )
    switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' )
    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( '\n' )
    print switch0.cmd( 'ovs-ofctl show dp0' )
    print switch1.cmd( 'ovs-ofctl show dp1' )
 
 
    info( "*** Running test\n" )
    h0.cmdPrint( 'ping -c3 ' + h1.IP() )
    h1.cmdPrint( 'ping -c3 ' + h0.IP() )
 
    info( "*** Stopping network\n" )
    controller.cmd( 'kill %' + cname )
    switch0.cmd( 'ovs-vsctl del-br dp0' )
    switch0.deleteIntfs()
    switch1.cmd( 'ovs-vsctl del-br dp1' )
    switch1.deleteIntfs()
    info( '\n' )
 
if __name__ == '__main__':
    setLogLevel( 'info' )
    info( '*** Scratch network demo (kernel datapath)\n' )
    Mininet.init()
    scratchNet()

 Mininet系列实验(七):Mininet脚本实现控制交换机行为_第6张图片

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第7张图片

Mininet系列实验(七):Mininet脚本实现控制交换机行为_第8张图片

总结:

1.对ovs加深理解

2.能够完全看懂脚本

3.需要更深一步了解mininet的模块的函数

你可能感兴趣的:(Level.4,SDN实验)