(mininet)如何使用两个路由器及其各自的主机创建拓扑

时间:2017-10-05 22:00:04

标签: python router mininet

该场景由两个相互连接的路由器r1-r2组成(我在Mininet给出的例子中找到了LinuxRouter类)。将3台主机连接到r1,每台主机属于不同的子网,正确ping网。

class LinuxRouter( Node ):

    def config( self, **params ):
        super( LinuxRouter, self).config( **params )
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )

    def terminate( self ):
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
        super( LinuxRouter, self ).terminate()


class NetworkTopo( Topo ):

    def build( self, **_opts ):
        r1 = self.addNode( 'r1', cls=LinuxRouter, ip='192.168.1.1/24' )
        s1, s2, s3, s4= [ self.addSwitch( s ) for s in ( 's1', 's2', 's3','s4') ]

        self.addLink( s1, r1, intfName2='r1-eth1', params2={ 'ip' : '192.168.1.1/24' } )  
        self.addLink( s2, r1, intfName2='r1-eth2', params2={ 'ip' : '172.16.0.1/12' } )
        self.addLink( s3, r1, intfName2='r1-eth3', params2={ 'ip' : '10.0.0.1/8' } )
        self.addLink( s4, r1, intfName2='r1-eth4', params2={ 'ip' : '5.5.5.1/16' } )

        h1 = self.addHost( 'h1', ip='192.168.1.100/24', defaultRoute='via 192.168.1.1' )
        h2 = self.addHost( 'h2', ip='172.16.0.100/12', defaultRoute='via 172.16.0.1' )
        h3 = self.addHost( 'h3', ip='10.0.0.100/8', defaultRoute='via 10.0.0.1' )
        r2 = self.addNode( 'r2',cls=LinuxRouter,ip='5.5.5.100/16', defaultRoute='via 5.5.5.1' )

        for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
            self.addLink( h, s)
        self.addLink(r2,s4)

def run():
    topo = NetworkTopo()
    net = Mininet( topo=topo)
    net.start()
    CLI( net )
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    run()

如果我尝试将主机(让我们称之为h4)添加到r2,就像我对其他主机一样(添加一个开关,s4,就像r2 - s4 - h4),它无法ping 。我该如何管理?

1 个答案:

答案 0 :(得分:0)

让我们将问题简化为使用2台路由器,2台交换机和2台主机可以拥有的最简单的拓扑。

Topology 使用this tool创建的图像。

这是Mininet经过修改的linuxrouter.py,它创建了此拓扑。 (删除了默认文档字符串和注释,并添加了新的相关文档。)

这可以在以下Mininet VM版本上使用:mininet-2.2.2-170321-ubuntu-14.04.4-server

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI


class LinuxRouter(Node):
    def config(self, **params):
        super(LinuxRouter, self).config(**params)
        self.cmd('sysctl net.ipv4.ip_forward=1')

    def terminate(self):
        self.cmd('sysctl net.ipv4.ip_forward=0')
        super(LinuxRouter, self).terminate()


class NetworkTopo(Topo):
    def build(self, **_opts):
        # Add 2 routers in two different subnets
        r1 = self.addHost('r1', cls=LinuxRouter, ip='10.0.0.1/24')
        r2 = self.addHost('r2', cls=LinuxRouter, ip='10.1.0.1/24')

        # Add 2 switches
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        # Add host-switch links in the same subnet
        self.addLink(s1,
                     r1,
                     intfName2='r1-eth1',
                     params2={'ip': '10.0.0.1/24'})

        self.addLink(s2,
                     r2,
                     intfName2='r2-eth1',
                     params2={'ip': '10.1.0.1/24'})

        # Add router-router link in a new subnet for the router-router connection
        self.addLink(r1,
                     r2,
                     intfName1='r1-eth2',
                     intfName2='r2-eth2',
                     params1={'ip': '10.100.0.1/24'},
                     params2={'ip': '10.100.0.2/24'})

        # Adding hosts specifying the default route
        d1 = self.addHost(name='d1',
                          ip='10.0.0.251/24',
                          defaultRoute='via 10.0.0.1')
        d2 = self.addHost(name='d2',
                          ip='10.1.0.252/24',
                          defaultRoute='via 10.1.0.1')

        # Add host-switch links
        self.addLink(d1, s1)
        self.addLink(d2, s2)


def run():
    topo = NetworkTopo()
    net = Mininet(topo=topo)

    # Add routing for reaching networks that aren't directly connected
    info(net['r1'].cmd("ip route add 10.1.0.0/24 via 10.100.0.2 dev r1-eth2"))
    info(net['r2'].cmd("ip route add 10.0.0.0/24 via 10.100.0.1 dev r2-eth2"))

    net.start()
    CLI(net)
    net.stop()


if __name__ == '__main__':
    setLogLevel('info')
    run()

概述:您有两个网络10.0.0.0/24和10.1.0.0/24通过2个路由器使用网络10.100.0.0/24连接。