博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mininet实验 脚本实现控制交换机行为
阅读量:5846 次
发布时间:2019-06-18

本文共 8027 字,大约阅读时间需要 26 分钟。

实验原理

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


实验内容

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

mininet脚本代码如下:

1 from mininet.net import Mininet 2 from mininet.node import Node 3 from mininet.link import Link 4 from mininet.log import setLogLevel, info 5 from mininet.util import quietRun 6   7 from time import sleep 8   9 def scratchNet( cname='controller', cargs='-v ptcp:' ):10     "Create network from scratch using Open vSwitch."11  12     info( "*** Creating nodes\n" )13     controller = Node( 'c0', inNamespace=False )14     switch0 = Node( 's0', inNamespace=False )15     h0 = Node( 'h0' )16     h1 = Node( 'h1' )17  18     info( "*** Creating links\n" )19     Link( h0, switch0 )20     Link( h1, switch0 )21  22     info( "*** Configuring hosts\n" )23     h0.setIP( '192.168.123.1/24' )24     h1.setIP( '192.168.123.2/24' )25     info( str( h0 ) + '\n' )26     info( str( h1 ) + '\n' )27  28     info( "*** Starting network using Open vSwitch\n" )29     controller.cmd( cname + ' ' + cargs + '&' )30     switch0.cmd( 'ovs-vsctl del-br dp0' )31     switch0.cmd( 'ovs-vsctl add-br dp0' )32  33     for intf in switch0.intfs.values():34         print (intf)35         print (switch0.cmd( 'ovs-vsctl add-port dp0 %s' % intf ))36     switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' )37     switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=1 actions=output:2\"' )38     switch0.cmd( 'ovs-ofctl add-flow dp0 \"in_port=2 actions=output:1\"' )39  40     info( '*** Waiting for switch to connect to controller' )41     while 'is_connected' not in quietRun( 'ovs-vsctl show' ):42         sleep( 1 )43         info( '.' )44     info( '\n' )45  46     info( "*** Running test\n" )47     h0.cmdPrint( 'ping -c6 ' + h1.IP() )48     h1.cmdPrint( 'ping -c6 ' + h0.IP() )49  50     info( "*** Stopping network\n" )51     controller.cmd( 'kill %' + cname )52     switch0.cmd( 'ovs-vsctl del-br dp0' )53     switch0.deleteIntfs()54     info( '\n' )55  56 if __name__ == '__main__':57     setLogLevel( 'info' )58     info( '*** Scratch network demo (kernel datapath)\n' )59     Mininet.init()60     scratchNet()

其实,源码中的也可以不用连接controller,直接就可以使用“ovs-vsctl”命令控制控制器。也就是代码中的 switch0.cmd( 'ovs-vsctl set-controller dp0 tcp:127.0.0.1:6633' )其实不是那么必要。

两台交换机两台主机,主机0连接交换机0,主机1连接交换机1,交换机0连接交换机1,数据转发端口要求同脚本1

mininet代码如下:

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

在脚本2的基础上设置带宽、延迟以及丢包率

mininet代码如下:

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

转载于:https://www.cnblogs.com/pullself/p/10225343.html

你可能感兴趣的文章
https/443安装
查看>>
Web服务器压力测试工具http_load、webbench、ab、Siege使用教程
查看>>
我的友情链接
查看>>
RHEL6.3 源码安装Puppet
查看>>
RSAT for Windows10
查看>>
我的友情链接
查看>>
mybatis 和 hibernate 区别?
查看>>
关于线程耗尽导致请求超时系统假死的思考
查看>>
初级文件系统管理之一
查看>>
Mac软件下载备忘
查看>>
java 泛型初探
查看>>
Golang安装包go get使用代理
查看>>
ceph-objectstore-tool工具使用示例
查看>>
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory
查看>>
备份事务日志 (SQL Server)
查看>>
Ubuntu操作系统安装使用教程
查看>>
一个GUI对话框界面
查看>>
Linux中sort 排序
查看>>
就是一个表格
查看>>
CakePHP 2.x CookBook 中文版 第三章 入门 之 CakePHP 的结构
查看>>