zoukankan      html  css  js  c++  java
  • 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——编辑脚本保存——改为可执行——清内存——开发者模式内打开

     1 # !/usr/bin/python 
     2 from mininet.net import Mininet
     3 from mininet.node import Node
     4 from mininet.link import Link
     5 from mininet.log import setLogLevel, info
     6 from mininet.util import quietRun
     7 # 调用Mininet相关模块
     8  
     9 from time import sleep
    10 # 调用sleep模块 
    11  
    12 def scratchNet( cname='controller', cargs='-v ptcp:' ):
    13     "Create network from scratch using Open vSwitch."
    14  
    15     # 创建网络节点其中有两个主机,一个交换机,一个控制器
    16     info( "*** Creating nodes
    " )
    17     controller = Node( 'c0', inNamespace=False )
    18     switch0 = Node( 's0', inNamespace=False )
    19     h0 = Node( 'h0' )
    20     h1 = Node( 'h1' )
    21  
    22     # 创建网络连接,分别是h0-s0和h1-s0
    23     info( "*** Creating links
    " )
    24     Link( h0, switch0 )
    25     Link( h1, switch0 )
    26  
    27     # 设置主机的IP地址,然后再诊断的同时打印出string化的h0h1
    28     info( "*** Configuring hosts
    " )
    29     h0.setIP( '192.168.123.1/24' )
    30     h1.setIP( '192.168.123.2/24' )
    31     info( str( h0 ) + '
    ' )
    32     info( str( h1 ) + '
    ' )
    33  
    34     # 开启控制器和s0的链路
    35     info( "*** Starting network using Open vSwitch
    " )
    36     controller.cmd( cname + ' ' + cargs + '&' )
    37     switch0.cmd( 'ovs-vsctl del-br dp0' )
    38     switch0.cmd( 'ovs-vsctl add-br dp0' )
    39  
    40     # 在交换机的信息库里面依次打出信息
    41     for intf in switch0.intfs.values():
    42         print intf
    43         print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
    44     switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' )
    45     switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' )
    46     switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' )
    47  
    48     # 等待连接的小程序
    49     info( '*** Waiting for switch to connect to controller' )
    50     while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
    51         sleep( 1 )
    52         info( '.' )
    53     info( '
    ' )
    54  
    55     # 两个互ping6次
    56     info( "*** Running test
    " )
    57     h0.cmdPrint( 'ping -c6 ' + h1.IP() )
    58     h1.cmdPrint( 'ping -c6 ' + h0.IP() )
    59  
    60     # 关闭这个网络
    61     info( "*** Stopping network
    " )
    62     controller.cmd( 'kill %' + cname )
    63     switch0.cmd( 'ovs-vsctl del-br dp0' )
    64     switch0.deleteIntfs()
    65     info( '
    ' )
    66  
    67 # 主函数部分,设置打印等级,还有调用以上的一些函数
    68 if __name__ == '__main__':
    69     setLogLevel( 'info' )
    70     info( '*** Scratch network demo (kernel datapath)
    ' )
    71     Mininet.init()
    72     scratchNet()

     

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

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

    1.  1 #!/usr/bin/python  
      2
      from mininet.net import Mininet 3 from mininet.node import Node 4 from mininet.link import Link 5 from mininet.log import setLogLevel, info 6 from mininet.util import quietRun 7 8 from time import sleep 9 10 def scratchNet( cname='controller', cargs='-v ptcp:' ): 11 "Create network from scratch using Open vSwitch." 12 13 info( "*** Creating nodes " ) 14 controller = Node( 'c0', inNamespace=False ) 15 switch0 = Node( 's0', inNamespace=False ) 16 switch1 = Node( 's1', inNamespace=False ) 17 h0 = Node( 'h0' ) 18 h1 = Node( 'h1' ) 19 20 info( "*** Creating links " ) 21 Link( h0, switch0 ) 22 Link( h1, switch1 ) 23 Link( switch0, switch1 ) 24 25 info( "*** Configuring hosts " ) 26 h0.setIP( '192.168.123.1/24' ) 27 h1.setIP( '192.168.123.2/24' ) 28 info( str( h0 ) + ' ' ) 29 info( str( h1 ) + ' ' ) 30 31 info( "*** Starting network using Open vSwitch " ) 32 controller.cmd( cname + ' ' + cargs + '&' ) 33 switch0.cmd( 'ovs-vsctl del-br dp0' ) 34 switch0.cmd( 'ovs-vsctl add-br dp0' ) 35 switch1.cmd( 'ovs-vsctl del-br dp1' ) 36 switch1.cmd( 'ovs-vsctl add-br dp1' ) 37 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 switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' ) 48 switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' ) 49 switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' ) 50 switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' ) 51 switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=1 actions=output:2"' ) 52 switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=2 actions=output:1"' ) 53 54 55 info( '*** Waiting for switch to connect to controller' ) 56 while 'is_connected' not in quietRun( 'ovs-vsctl show' ): 57 sleep( 1 ) 58 info( '.' ) 59 info( ' ' ) 60 print switch0.cmd( 'ovs-ofctl show dp0' ) 61 print switch1.cmd( 'ovs-ofctl show dp1' ) 62 63 64 info( "*** Running test " ) 65 h0.cmdPrint( 'ping -c3 ' + h1.IP() ) 66 h1.cmdPrint( 'ping -c3 ' + h0.IP() ) 67 68 info( "*** Stopping network " ) 69 controller.cmd( 'kill %' + cname ) 70 switch0.cmd( 'ovs-vsctl del-br dp0' ) 71 switch0.deleteIntfs() 72 switch1.cmd( 'ovs-vsctl del-br dp1' ) 73 switch1.deleteIntfs() 74 info( ' ' ) 75 76 if __name__ == '__main__': 77 setLogLevel( 'info' ) 78 info( '*** Scratch network demo (kernel datapath) ' ) 79 Mininet.init() 80 scratchNet()

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

     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 from mininet.util import quietRun
     7  
     8 from time import sleep
     9  
    10 def scratchNet( cname='controller', cargs='-v ptcp:' ):
    11     "Create network from scratch using Open vSwitch."
    12  
    13     info( "*** Creating nodes
    " )
    14     controller = Node( 'c0', inNamespace=False )
    15     switch0 = Node( 's0', inNamespace=False )
    16     switch1 = Node( 's1', inNamespace=False )
    17     h0 = Node( 'h0' )
    18     h1 = Node( 'h1' )
    19  
    20     info( "*** Creating links
    " )
    21  
    22     linkopts0=dict(bw=10)
    23     linkopts1=dict(bw=10, delay='5ms', loss=10)
    24     TCLink( h0, switch0,**linkopts0)
    25     TCLink( h1, switch1,**linkopts0)
    26     TCLink( switch0, switch1,**linkopts1)
    27  
    28  
    29     info( "*** Configuring hosts
    " )
    30     h0.setIP( '192.168.123.1/24' )
    31     h1.setIP( '192.168.123.2/24' )
    32     info( str( h0 ) + '
    ' )
    33     info( str( h1 ) + '
    ' )
    34  
    35     info( "*** Starting network using Open vSwitch
    " )
    36     controller.cmd( cname + ' ' + cargs + '&' )
    37     switch0.cmd( 'ovs-vsctl del-br dp0' )
    38     switch0.cmd( 'ovs-vsctl add-br dp0' )
    39     switch1.cmd( 'ovs-vsctl del-br dp1' )
    40     switch1.cmd( 'ovs-vsctl add-br dp1' )
    41  
    42  
    43     for intf in switch0.intfs.values():
    44         print intf
    45         print switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf )
    46  
    47     for intf in switch1.intfs.values():
    48         print intf
    49         print switch1.cmd( 'ovs-vsctl add-port dp1 %s' % intf )
    50  
    51     switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:10.0.0.8:6633' )
    52     switch1.cmd( 'ovs-vsctl set-controller dp1 tcp: 10.0.0.8:6633' )
    53     switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=1 actions=output:2"' )
    54     switch0.cmd( 'ovs-ofctl add-flow dp0 "in_port=2 actions=output:1"' )
    55     switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=1 actions=output:2"' )
    56     switch1.cmd( 'ovs-ofctl add-flow dp1 "in_port=2 actions=output:1"' )
    57  
    58  
    59     info( '*** Waiting for switch to connect to controller' )
    60     while 'is_connected' not in quietRun( 'ovs-vsctl show' ):
    61         sleep( 1 )
    62         info( '.' )
    63     info( '
    ' )
    64     print switch0.cmd( 'ovs-ofctl show dp0' )
    65     print switch1.cmd( 'ovs-ofctl show dp1' )
    66  
    67  
    68     info( "*** Running test
    " )
    69     h0.cmdPrint( 'ping -c3 ' + h1.IP() )
    70     h1.cmdPrint( 'ping -c3 ' + h0.IP() )
    71  
    72     info( "*** Stopping network
    " )
    73     controller.cmd( 'kill %' + cname )
    74     switch0.cmd( 'ovs-vsctl del-br dp0' )
    75     switch0.deleteIntfs()
    76     switch1.cmd( 'ovs-vsctl del-br dp1' )
    77     switch1.deleteIntfs()
    78     info( '
    ' )
    79  
    80 if __name__ == '__main__':
    81     setLogLevel( 'info' )
    82     info( '*** Scratch network demo (kernel datapath)
    ' )
    83     Mininet.init()
    84     scratchNet()

     

    总结:

    1.对ovs加深理解

    2.能够完全看懂脚本

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

  • 相关阅读:
    Coursera课程笔记----计算导论与C语言基础----Week 7
    Coursera课程笔记----计算导论与C语言基础----Week 6
    Coursera课程笔记----计算导论与C语言基础----Week 5
    Coursera课程笔记----P4E.Capstone----Week 6&7
    Coursera课程笔记----P4E.Capstone----Week 4&5
    Coursera课程笔记----P4E.Capstone----Week 2&3
    图解 Java 垃圾回收机制,写得非常好!
    别在 Java 代码里乱打日志了,这才是正确的打日志姿势!
    聊一聊Java 泛型中的通配符 T,E,K,V,?
    Java开发最常犯的10个错误,打死都不要犯!
  • 原文地址:https://www.cnblogs.com/a1164520408/p/9629231.html
Copyright © 2011-2022 走看看