wiki:EsperimentiTopologieMininet

Esempio di topologia pura

from mininet.topo import Topo

class MyTopo(Topo):
    def __init__(self):
        Topo.__init__(self)
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        h = []
        for i in xrange(4):
            h.append(self.addHost('h' + str(i+1)))

        self.addLink(s1, h[0])
        self.addLink(s1, h[1])
        self.addLink(s2, h[2])
        self.addLink(s2, h[3])
        self.addLink(s1, s2)


topos = { 'prova' : (lambda : MyTopo()) }

Se il file si chiama lab/mytopo.py, da eseguire con

sudo mn --custom lab/mytopo.py --topo prova

Esempio con IP e parametri del link variati

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI

class MyTopo(Topo):
    def __init__(self):
        Topo.__init__(self)
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        h = []
        for i in xrange(4):
            h.append(self.addHost('h' + str(i+1)))

        self.addLink(s1, h[0])
        self.addLink(s1, h[1])
        self.addLink(s2, h[2])
        self.addLink(s2, h[3])
        self.addLink(s1, s2, bw=10)


if __name__ == '__main__':
    net = Mininet(topo=MyTopo(), link=TCLink)
    h1, h2 = net.get('h1','h2')
    h1.setIP('192.168.1.10/24')
    h2.setIP('192.168.1.20/24')

    h3, h4 = net.get('h3','h4')
    h3.setIP('192.168.2.100/24')
    h4.setIP('192.168.2.200/24')

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

Se il file si chiama lab/mytopo.py, da eseguire con

sudo python lab/mytopo.py

Come rendere h4 raggiungibile da h1

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI

class MyTopo(Topo):
    def __init__(self):
        Topo.__init__(self)
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        h = []
        for i in xrange(4):
            h.append(self.addHost('h' + str(i+1)))

        self.addLink(s1, h[0])
        self.addLink(s1, h[1])
        self.addLink(s2, h[2])
        self.addLink(s2, h[3])
        self.addLink(s1, s2, bw=10)


if __name__ == '__main__':
    net = Mininet(topo=MyTopo(), link=TCLink)
    h1, h2 = net.get('h1','h2')
    h1.setIP('192.168.1.10/24')
    h2.setIP('192.168.1.20/24')
    
    h3, h4 = net.get('h3','h4')
    h3.setIP('192.168.2.100/24')
    h4.setIP('192.168.2.200/24')

    # essendo i due switch direttamente collegati
    # e` sufficiente aggiungere le rotte di default
    for h in net.hosts:
        h.setDefaultRoute(intf=h.intf())

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

Esempio con routing

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI

class MyTopo(Topo):
    def __init__(self):
        Topo.__init__(self)
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        h = []
        for i in xrange(4):
            h.append(self.addHost('h' + str(i+1)))

        self.addLink(s1, h[0])
        self.addLink(s1, h[1])
        self.addLink(s2, h[2])
        self.addLink(s2, h[3])

        # Gateway node
        gw = self.addHost('h5')
        self.addLink(s1, gw)
        self.addLink(s2, gw)


if __name__ == '__main__':
    net = Mininet(topo=MyTopo(), link=TCLink)
    h1, h2 = net.get('h1','h2')
    h1.setIP('192.168.1.10/24')
    h2.setIP('192.168.1.20/24')

    
    h3, h4 = net.get('h3','h4')
    h3.setIP('192.168.2.100/24')
    h4.setIP('192.168.2.200/24')

    for h in net.hosts:
        if h.name != 'h5': # gateway
            h.setDefaultRoute(intf=h.intf())

    gw = net.get('h5')
    gw.setIP('192.168.1.1/24', intf='h5-eth0')
    gw.setIP('192.168.2.1/24', intf='h5-eth1')
    h1.cmd('ip route add 192.168.2.0/24 via 192.168.1.1')
    h2.cmd('ip route add 192.168.2.0/24 via 192.168.1.1')
    h3.cmd('ip route add 192.168.1.0/24 via 192.168.2.1')
    h4.cmd('ip route add 192.168.1.0/24 via 192.168.2.1')

    gw.setHostRoute('192.168.1.0', intf='h5-eth0')
    gw.setHostRoute('192.168.2.0', intf='h5-eth1')
    gw.cmd('sysctl -w net.ipv4.ip_forward=1')
    
    net.start()
    CLI(net)
    net.stop()
Last modified 5 years ago Last modified on Oct 27, 2014, 8:31:34 PM