Simplified and refactored examples/dynamicnet.py
Extended LinearTopo to support mulitple hosts per switch
This commit is contained in:
Regular → Executable
+15
-102
@@ -1,4 +1,4 @@
|
|||||||
# -*- coding: utf-8 -*-
|
#!/usr/bin/python
|
||||||
|
|
||||||
"""
|
"""
|
||||||
This script builds a network using mininet for using with
|
This script builds a network using mininet for using with
|
||||||
@@ -17,118 +17,28 @@ it make links between each switch.
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from os import environ
|
from optparse import OptionParser
|
||||||
from sys import argv
|
|
||||||
from sys import exit
|
|
||||||
from getopt import getopt
|
|
||||||
from getopt import GetoptError
|
|
||||||
|
|
||||||
from mininet.topo import Topo
|
from mininet.topo import LinearTopo
|
||||||
from mininet.log import setLogLevel
|
from mininet.log import setLogLevel
|
||||||
from mininet.net import Mininet
|
from mininet.net import Mininet
|
||||||
from mininet.cli import CLI
|
from mininet.cli import CLI
|
||||||
from mininet.node import RemoteController
|
from mininet.node import RemoteController
|
||||||
|
|
||||||
|
def main():
|
||||||
class MyTopo(Topo):
|
|
||||||
""" Topology Class Used in all classes to add network components """
|
|
||||||
def __init__(self, **opts):
|
|
||||||
super(MyTopo, self).__init__(**opts)
|
|
||||||
|
|
||||||
|
|
||||||
class Switches():
|
|
||||||
""" Class to control switches addition """
|
|
||||||
|
|
||||||
def __init__(self, num, topo):
|
|
||||||
|
|
||||||
for i in xrange(num):
|
|
||||||
topo.addSwitch('s%s' % str(i))
|
|
||||||
|
|
||||||
|
|
||||||
class Hosts():
|
|
||||||
""" Class to control host addition """
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self._seq = 0
|
|
||||||
|
|
||||||
def add(self, num, switch, topo):
|
|
||||||
""" Adds hosts and its links """
|
|
||||||
|
|
||||||
for i in xrange(num):
|
|
||||||
host = topo.addHost("h%s" % str(self._seq))
|
|
||||||
topo.addLink(host, switch)
|
|
||||||
self._seq += 1
|
|
||||||
|
|
||||||
|
|
||||||
class Links():
|
|
||||||
""" Create links between two switches """
|
|
||||||
|
|
||||||
def __init__(self, topo, switch0, switch1):
|
|
||||||
|
|
||||||
topo.addLink(switch0, switch1)
|
|
||||||
|
|
||||||
|
|
||||||
class ArgParser(object):
|
|
||||||
""" Command line arguments parser class """
|
|
||||||
|
|
||||||
# possible command line options
|
|
||||||
OPTS = "s:h:"
|
|
||||||
|
|
||||||
def __init__(self, argv):
|
|
||||||
""" Tries to get options from command line """
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.opts, self.args = getopt(argv, ArgParser.OPTS, ["help"])
|
|
||||||
except GetoptError:
|
|
||||||
self.usage()
|
|
||||||
|
|
||||||
self.switches = 2 # default value for the number of switches
|
|
||||||
self.hosts = 5 # default value for the number of hosts
|
|
||||||
self.parse()
|
|
||||||
|
|
||||||
def parse(self):
|
|
||||||
""" Parses the arguments and set values to the script execution """
|
|
||||||
|
|
||||||
for opt, args in self.opts:
|
|
||||||
|
|
||||||
if opt == "--help":
|
|
||||||
self.usage()
|
|
||||||
elif opt == "-s":
|
|
||||||
self.switches = int(args)
|
|
||||||
elif opt == "-h":
|
|
||||||
self.hosts = int(args)
|
|
||||||
|
|
||||||
def usage(self):
|
|
||||||
""" Prints how to usage the program """
|
|
||||||
|
|
||||||
print "python dinamicnet.py -s <Number-of-Switches>" \
|
|
||||||
" -h <hosts-per-switch>"
|
|
||||||
exit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
|
|
||||||
# Defines the log level
|
# Defines the log level
|
||||||
setLogLevel('info')
|
setLogLevel('info')
|
||||||
|
|
||||||
# parses command line arguments
|
# parses command line arguments
|
||||||
parser = ArgParser(argv[1:])
|
parser = OptionParser()
|
||||||
|
parser.add_option('-H', dest='hosts', default=5,
|
||||||
|
help='Number of hosts per switch')
|
||||||
|
parser.add_option('-S', dest='switches', default=2,
|
||||||
|
help='Number of switches')
|
||||||
|
(options, args) = parser.parse_args()
|
||||||
|
|
||||||
# Build network topology
|
# Build network topology (see mininet/topo.py)
|
||||||
topo = MyTopo()
|
topo = LinearTopo(int(options.switches), int(options.hosts))
|
||||||
|
|
||||||
# Adds Switches to network
|
|
||||||
Switches(parser.switches, topo)
|
|
||||||
|
|
||||||
# Adds Hosts to network
|
|
||||||
hosts = Hosts()
|
|
||||||
for i, switch in enumerate(topo.switches()):
|
|
||||||
hosts.add(parser.hosts, switch, topo)
|
|
||||||
|
|
||||||
try: # Add links between switches
|
|
||||||
Links(topo, switch, topo.switches()[i + 1])
|
|
||||||
except IndexError:
|
|
||||||
break
|
|
||||||
|
|
||||||
# Creates the Network using a remote controller
|
# Creates the Network using a remote controller
|
||||||
net = Mininet(topo,
|
net = Mininet(topo,
|
||||||
@@ -140,3 +50,6 @@ if __name__ == "__main__":
|
|||||||
CLI(net)
|
CLI(net)
|
||||||
# Stop the network
|
# Stop the network
|
||||||
net.stop()
|
net.stop()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
+11
-4
@@ -236,23 +236,30 @@ class SingleSwitchReversedTopo(Topo):
|
|||||||
port1=0, port2=(k - h + 1))
|
port1=0, port2=(k - h + 1))
|
||||||
|
|
||||||
class LinearTopo(Topo):
|
class LinearTopo(Topo):
|
||||||
"Linear topology of k switches, with one host per switch."
|
"Linear topology of k switches, with n hosts per switch."
|
||||||
|
|
||||||
def __init__(self, k=2, **opts):
|
def __init__(self, k=2, n=1, **opts):
|
||||||
"""Init.
|
"""Init.
|
||||||
k: number of switches (and hosts)
|
k: number of switches
|
||||||
|
n: number of hosts per switch
|
||||||
hconf: host configuration options
|
hconf: host configuration options
|
||||||
lconf: link configuration options"""
|
lconf: link configuration options"""
|
||||||
|
|
||||||
super(LinearTopo, self).__init__(**opts)
|
super(LinearTopo, self).__init__(**opts)
|
||||||
|
|
||||||
self.k = k
|
self.k = k
|
||||||
|
self.n = n
|
||||||
|
|
||||||
lastSwitch = None
|
lastSwitch = None
|
||||||
for i in irange(1, k):
|
for i in irange(1, k):
|
||||||
host = self.addHost('h%s' % i)
|
# Add switch
|
||||||
switch = self.addSwitch('s%s' % i)
|
switch = self.addSwitch('s%s' % i)
|
||||||
|
# Add hosts to switch
|
||||||
|
for j in irange(1, n):
|
||||||
|
hostNum = (i-1)*n + j
|
||||||
|
host = self.addHost('h%s' % hostNum)
|
||||||
self.addLink(host, switch)
|
self.addLink(host, switch)
|
||||||
|
# Connect switch to previous
|
||||||
if lastSwitch:
|
if lastSwitch:
|
||||||
self.addLink(switch, lastSwitch)
|
self.addLink(switch, lastSwitch)
|
||||||
lastSwitch = switch
|
lastSwitch = switch
|
||||||
|
|||||||
Reference in New Issue
Block a user