• mininet实验 脚本实现控制交换机行为


    写在前面


    本文参考

    • 通过这个实验,我学习到了另一种下流表的方式。
    • 下流表有两种方式(我目前了解):
      • 通过controller下发。
      • 通过OvS提供的API直接向OvS交换机下流表。
    • 本实验脚本已经把相关过程写好,读一下脚本熟悉API是关键。

    实验简介


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

    本实验在基于Mininet脚本的不同拓扑环境下使用OpenDaylight控制交换机行为。

    • 任务一:一台交换机两台主机,从1端口进入的数据流转发到2端口,从2端口进入的数据流转发到1端口;
    • 任务二:两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口同任务一;
    • 任务三:与任务二类似,但是在创建link的时候,设置带宽、延迟以及丢包率。

    实验步骤


    1.搭建环境

    • mininet虚拟机
    • OpenDaylight虚拟机

    2.任务一

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

    • 在Mininet里新建文件mymininet1.py。
    • 查看ODL控制器所在主机IP。
    • 编辑myminnet1脚本内容如下:
    #!/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.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( '
    ' )
    
        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()
    
    • 修改其为可执行文件并运行:
    chmod +x mymininet1.py
    ./mymininet1.py
    

    • mn -c命令清理进程

    3.任务二

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

    • 在Mininet里新建文件mymininet2.py。
    • 查看ODL控制器所在主机IP。
    • 编辑myminnet2脚本内容如下:
    #!/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' )
        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( '
    ' )
        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()
    
    • 修改其为可执行文件并运行:
    chmod +x mymininet2.py
    ./mymininet2.py
    

    • mn -c命令清理进程

    4.任务三

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

    • 在Mininet里新建文件mymininet3.py。
    • 查看ODL控制器所在主机IP。
    • 编辑myminnet3脚本内容如下:
    #!/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' )
        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( '
    ' )
        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()
    
    • 修改其为可执行文件并运行:
    chmod +x mymininet2.py
    ./mymininet2.py
    

    • mn -c命令清理进程

    实验小结


    • 这次实验很顺利,遇见的问题都通过陈翔学长和百度解决掉了。
  • 相关阅读:
    2021-深信服-安服实习-面试反省
    cookie越权的一个案例
    图片大小相关参数可控导致ddos的一个案例
    流量代理-reGeorg脚本实现
    端口转发-lcx
    渗透各阶段目标&错误配置&正反向连接&MSF初探
    权限维持-几种常用手段复现
    解决pip安装第三方包编码错误:UnicodeDecodeError: 'ascii' codec can't decode byte....
    黑龙江网络安全技能竞赛awd后门分析复现
    实验吧-PHP大法-eregi()函数
  • 原文地址:https://www.cnblogs.com/031602523liu/p/8998344.html
Copyright © 2020-2023  润新知