zoukankan      html  css  js  c++  java
  • mininet 多径仿真双路由双网卡

    网络拓扑:

    仿真结果:

    python代码:

    #!/usr/bin/python
    
    from mininet.net import Mininet
    from mininet.node import Controller, RemoteController, OVSController
    from mininet.node import CPULimitedHost, Host, Node
    from mininet.node import OVSKernelSwitch, UserSwitch
    from mininet.node import IVSSwitch
    from mininet.cli import CLI
    from mininet.log import setLogLevel, info
    from mininet.link import TCLink, Intf
    from subprocess import call
    
    def myNetwork():
    
        net = Mininet( topo=None,
                       build=False,
                       ipBase='10.0.0.0/8')
    
        info( '*** Adding controller
    ' )
        info( '*** Add switches
    ')
        r1 = net.addHost('r1', cls=Node, ip='0.0.0.0')
        r1.cmd('sysctl -w net.ipv4.ip_forward=1')
        r2 = net.addHost('r2', cls=Node, ip='0.0.0.0')
        r2.cmd('sysctl -w net.ipv4.ip_forward=1')
    
        info( '*** Add hosts
    ')
        h1 = net.addHost('h1', cls=Host, ip='10.0.0.1', defaultRoute=None)
        h2 = net.addHost('h2', cls=Host, ip='10.0.0.2', defaultRoute=None)
    
        info( '*** Add links
    ')
        net.addLink(h1, r1)
        net.addLink(r1, h2)
        net.addLink(h1, r2)
        net.addLink(r2, h2)
    
        info( '*** Starting network
    ')
        net.build()
        info( '*** Starting controllers
    ')
        for controller in net.controllers:
            controller.start()
    
        info( '*** Starting switches
    ')
    
        info( '*** Post configure switches and hosts
    ')
        h1.cmd('ifconfig h1-eth0 10.0.0.1/24')
        h1.cmd('ifconfig h1-eth1 192.168.0.1/24')
    
    
        r1.cmd('ifconfig r1-eth0 10.0.0.2/24')
        r1.cmd('ifconfig r1-eth1 10.10.10.2/24')
    
        r2.cmd('ifconfig r2-eth0 192.168.0.2/24')
        r2.cmd('ifconfig r2-eth1 192.168.10.2/24')
    
    
        h2.cmd('ifconfig h2-eth0 10.10.10.1/24')
        h2.cmd('ifconfig h2-eth1 192.168.10.1/24')
    
    
        #h1.cmd('ip route add 10.0.0.1/24 dev h1-eth0 table 520')
        #h1.cmd('ip route add default via 10.0.0.2 dev h1-eth0 table 520')
    
        #h1.cmd('ip route add 192.168.10.1/24 dev h1-eth1 table 521')
        #h1.cmd('ip route add default via 192.168.0.2 dev h1-eth1 table 521')
    
        #h2.cmd('ip route add 10.10.10.2/24 dev h2-eth0 table 522')
        #h2.cmd('ip route add default via 10.10.10.2 dev h2-eth0 table 522')
    
        #h2.cmd('ip route add 192.168.10.2/24 dev h2-eth1 table 523')
        #h2.cmd('ip route add default via 192.168.10.2 dev h2-eth1 table 523')
        h1.cmd("ip route flush all proto static scope global")
        
        h1.cmd('ip route add 10.0.0.0/24 via 10.0.0.2 dev h1-eth0 table 520')
        h1.cmd('ip route add default via 10.0.0.2 dev h1-eth0 table 520')
    
        h1.cmd('ip route add 192.168.0.0/24 via 192.168.0.2 dev h1-eth1 table 521')
        h1.cmd('ip route add default via 192.168.0.2 dev h1-eth1 table 521')
    
        h1.cmd('ip route add 10.10.10.0/24 via 10.0.0.2 dev h1-eth0')
        h1.cmd('ip route add 192.168.10.0/24 via 192.168.0.2 dev h1-eth1')
    
        h1.cmd('ip rule add from 10.0.0.1 table 520')
        h1.cmd('ip rule add from 192.168.0.1 table 521')
        # h1.cmd("route add default gw 10.0.0.2")
        
    
    
        h2.cmd('ip route add 10.10.10.0/24 via 10.10.10.2 dev h2-eth0 table 522')
        h2.cmd('ip route add default via 10.10.10.2 dev h2-eth0 table 522')
    
        h2.cmd('ip route add 192.168.10.0/24 via 192.168.10.2 dev h2-eth1 table 523')
        h2.cmd('ip route add default via 192.168.10.2 dev h2-eth1 table 523')
    
        h2.cmd('ip route add 10.0.0.0/24 via 10.10.10.2 dev h2-eth0')
        h2.cmd('ip route add 192.168.0.0/24 via 192.168.10.2 dev h2-eth1')
    
        h1.cmd('ip rule add from 10.10.10.1 table 522')
        h1.cmd('ip rule add from 192.168.10.2 table 523')
        # h2.cmd("route add default gw 10.10.10.2")
    
        CLI(net)
        net.stop()
    
    if __name__ == '__main__':
        setLogLevel( 'info' )
        myNetwork()
  • 相关阅读:
    VC++使用socket进行TCP、UDP通信实例总结
    [Android Pro] 调用系统相机和图库,裁剪图片
    [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
    [Android 新特性] 谷歌发布Android Studio开发工具1.0正式版(组图) 2014-12-09 09:35:40
    [Android 新特性] 有史来最大改变 Android 5.0十大新特性
    [Android Pro] service中显示一个dialog 或者通过windowmanage显示view
    [Android Pro] 通过Android trace文件分析死锁ANR
    [Android Memory] Android 的 StrictMode
    [Android Memory] Android性能测试小工具Emmagee
    [Android Memory] Android内存管理、监测剖析
  • 原文地址:https://www.cnblogs.com/iuyy/p/14030755.html
Copyright © 2011-2022 走看看