From 6f09dedfadf7adac43dea5fc2f42e504f8730b80 Mon Sep 17 00:00:00 2001 From: Gustavo Pantuza Coelho Pinto Date: Thu, 18 Jul 2013 18:32:08 -0700 Subject: [PATCH 1/3] Dynamic network creation using a remote controller The script builds a network topology based on command line arguments and uses a remote controller --- examples/dynamicnet.py | 142 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 examples/dynamicnet.py diff --git a/examples/dynamicnet.py b/examples/dynamicnet.py new file mode 100644 index 0000000..5ab5a0d --- /dev/null +++ b/examples/dynamicnet.py @@ -0,0 +1,142 @@ +# -*- coding: utf-8 -*- + +""" + This script builds a network using mininet for using with +a remote controller like POX. + + The script receives from command line two arguments. The number +of Switches and the number of Hosts per Switch. Then, it will build +the network topology based on this arguments. + + First of all, it build a topology and add the Switches to the network. +After that, add the same number of Hosts for each Switch added. Lastly +it make links between each switch. + +@author: Gustavo Pantuza +@since: 18.07.2013 + +""" + +from os import environ +from sys import argv +from sys import exit +from getopt import getopt +from getopt import GetoptError + +from mininet.topo import Topo +from mininet.log import setLogLevel +from mininet.net import Mininet +from mininet.cli import CLI +from mininet.node import RemoteController + + +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 " \ + " -h " + exit() + + +if __name__ == "__main__": + + # Defines the log level + setLogLevel('info') + + # parses command line arguments + parser = ArgParser(argv[1:]) + + # Build network topology + topo = MyTopo() + + # 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 + net = Mininet(topo, + controller=lambda a: RemoteController(a, ip='127.0.0.1')) + + # Starts the network + net.start() + # Run the mininet client + CLI(net) + # Stop the network + net.stop() From bf97d21c034398844b1b4290237891ec32842917 Mon Sep 17 00:00:00 2001 From: Gustavo Pantuza Coelho Pinto Date: Thu, 18 Jul 2013 18:42:10 -0700 Subject: [PATCH 2/3] Added dynamicnet.py to README file of the examples --- examples/README | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/README b/examples/README index 841486e..23cad6e 100644 --- a/examples/README +++ b/examples/README @@ -118,3 +118,8 @@ This example attempts to create a 1024-host network, and then runs the CLI on it. It may run into scalability limits, depending on available memory and sysctl configuration (see INSTALL.) +dynamicnet.py: + +This example builds a network based on command line arguments and uses +a remote controller. It is a good option to use with POX controller and +develop Software Defined Networks. From 921123155ae568d35a42ad5f71efca0c126c1368 Mon Sep 17 00:00:00 2001 From: Brian O'Connor Date: Fri, 19 Jul 2013 15:56:06 -0700 Subject: [PATCH 3/3] Simplified and refactored examples/dynamicnet.py Extended LinearTopo to support mulitple hosts per switch --- examples/dynamicnet.py | 117 ++++++----------------------------------- mininet/topo.py | 19 ++++--- 2 files changed, 28 insertions(+), 108 deletions(-) mode change 100644 => 100755 examples/dynamicnet.py diff --git a/examples/dynamicnet.py b/examples/dynamicnet.py old mode 100644 new mode 100755 index 5ab5a0d..334ce26 --- a/examples/dynamicnet.py +++ b/examples/dynamicnet.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +#!/usr/bin/python """ 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 sys import argv -from sys import exit -from getopt import getopt -from getopt import GetoptError +from optparse import OptionParser -from mininet.topo import Topo +from mininet.topo import LinearTopo from mininet.log import setLogLevel from mininet.net import Mininet from mininet.cli import CLI from mininet.node import RemoteController - -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 " \ - " -h " - exit() - - -if __name__ == "__main__": - +def main(): # Defines the log level setLogLevel('info') # 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 - topo = MyTopo() - - # 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 + # Build network topology (see mininet/topo.py) + topo = LinearTopo(int(options.switches), int(options.hosts)) # Creates the Network using a remote controller net = Mininet(topo, @@ -140,3 +50,6 @@ if __name__ == "__main__": CLI(net) # Stop the network net.stop() + +if __name__ == "__main__": + main() diff --git a/mininet/topo.py b/mininet/topo.py index fa20e19..2619634 100644 --- a/mininet/topo.py +++ b/mininet/topo.py @@ -236,23 +236,30 @@ class SingleSwitchReversedTopo(Topo): port1=0, port2=(k - h + 1)) 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. - k: number of switches (and hosts) + k: number of switches + n: number of hosts per switch hconf: host configuration options lconf: link configuration options""" super(LinearTopo, self).__init__(**opts) self.k = k + self.n = n lastSwitch = None for i in irange(1, k): - host = self.addHost('h%s' % i) + # Add switch switch = self.addSwitch('s%s' % i) - self.addLink( host, switch) + # 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) + # Connect switch to previous if lastSwitch: - self.addLink( switch, lastSwitch) + self.addLink(switch, lastSwitch) lastSwitch = switch