Reputation: 587
I want to build a mininet topology using python API. My topology is supposed to have 2 controllers, 3 switches and each switch will have several hosts connected to it (number of connected hosts is 2, 4, 1 respectively). Here is the code:
#!/usr/bin/python
import time
from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
def net():
net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
c1 = net.addController('c1', controller=RemoteController, ip="127.0.0.1", port=6653)
c2 = net.addController('c2', controller=RemoteController, ip="127.0.0.1", port=7753)
s_n = 3 #number of switches
c_n = 2 #number of controllers
hosts = []
amount = [2, 4, 1] # number of hosts that each switch has
info( "*** Creating switches\n" )
switches = [ net.addSwitch( 's%s' % s ) for s in range( 1, s_n ) ]
index1 = 0
L_in = 0
index2 = 0
info( "*** Creating hosts\n" )
for s in switches:
L_in = (L_in + 1)
if L_in <= len(amount):
index1 = (index2 + 1)
index2 = (index1 + amount[L_in]) - 1
hosts = [ net.addHost( 'h%s' % n ) for n in ( index1, index2 ) ]
for h in hosts:
net.addLink( s, h )
else:
break
# Wire up switches
last = None
for switch in switches:
if last:
net.addLink( last, switch )
last = switch
net.build()
c1.start()
c2.start()
for sw in switches[:c_n]:
sw.start( [c1] )
for sw in switches[c_n:]:
sw.start( [c2] )
net.start()
CLI( net )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
net()
But when I run the code, it is not working correctly. For example, I expect 3 switches, but 2 switches are created. I expect 7 hosts, but 4 hosts are created. The connection between hosts and switches is not correct, too. Here is the output:
*** Creating switches
*** Creating hosts
*** Configuring hosts
h1 h4 h5 h5
*** Starting controller
c1 c2
*** Starting 2 switches
s1 s2 ...
*** Starting CLI:
mininet> net
h1 h1-eth0:s1-eth1
h4 h4-eth0:s1-eth2
h5 h5-eth0:s2-eth2
h5 h5-eth0:s2-eth2
s1 lo: s1-eth1:h1-eth0 s1-eth2:h4-eth0 s1-eth3:s2-eth3
s2 lo: s2-eth1:h5-eth0 s2-eth2:h5-eth0 s2-eth3:s1-eth3
c1
c2
Upvotes: 0
Views: 2901
Reputation: 40894
A number of problems here.
range(1, n)
produces numbers from 1
no n-1
, not to n
.
You define function net
that will shadow the previous (imported?) definition of module net
. Call it make_net
or something.
Explicit loop indices like L_in
is almost always a bad idea, same as non-descriptive names as index2
.
Something like this should give you the idea:
n_switches = 3
hosts_per_switch = [2, 4, 1]
switches = [addSwitch("s%d" % n + 1) for n in range(n_switches)]
for (switch, number_of_hosts) in zip(switches, hosts_per_switch): # Pair them.
hosts = [addHost("h%d" % n + 1) for n in range(number_of_hosts)]
for host in hosts:
addLink(switch, host)
Upvotes: 1