Flavio Castro | cc38a54 | 2016-03-03 13:15:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import os |
| 4 | from optparse import OptionParser |
| 5 | |
| 6 | from mininet.net import Mininet |
| 7 | from mininet.topo import Topo |
| 8 | from mininet.node import RemoteController, UserSwitch, Host |
| 9 | from mininet.link import TCLink |
| 10 | from mininet.log import setLogLevel |
| 11 | from mininet.cli import CLI |
| 12 | |
| 13 | # Parse command line options and dump results |
| 14 | def parseOptions(): |
| 15 | """Parse command line options""" |
| 16 | parser = OptionParser() |
| 17 | parser.add_option('--spine', dest='spine', type='int', default=2, |
| 18 | help='number of spine switches, default=2') |
| 19 | parser.add_option('--leaf', dest='leaf', type='int', default=2, |
| 20 | help='number of leaf switches, default=2') |
| 21 | parser.add_option('--fanout', dest='fanout', type='int', default=2, |
| 22 | help='number of hosts per leaf switch, default=2') |
| 23 | parser.add_option('--onos', dest='onos', type='int', default=0, |
| 24 | help='number of ONOS Instances, default=0, 0 means localhost, 1 will use OC1 and so on') |
| 25 | |
| 26 | (options, args) = parser.parse_args() |
| 27 | return options, args |
| 28 | |
| 29 | opts, args = parseOptions() |
| 30 | |
| 31 | class LeafAndSpine(Topo): |
| 32 | def __init__(self, spine=2, leaf=2, fanout=2, **opts): |
| 33 | "Create Leaf and Spine Topo." |
| 34 | |
| 35 | Topo.__init__(self, **opts) |
| 36 | |
| 37 | # Add spine switches |
| 38 | spines = {} |
| 39 | for s in range(spine): |
| 40 | spines[s] = self.addSwitch('spine10%s' % (s + 1), dpid="00000000010%s" % (s + 1)) |
| 41 | # Set link speeds to 100Mb/s |
Flavio Castro | 4cd37c1 | 2016-03-28 07:39:16 -0700 | [diff] [blame] | 42 | linkopts = dict(bw=100) |
Flavio Castro | cc38a54 | 2016-03-03 13:15:46 -0800 | [diff] [blame] | 43 | |
| 44 | # Add Leaf switches |
| 45 | for ls in range(leaf): |
| 46 | leafSwitch = self.addSwitch('leaf%s' % (ls + 1), dpid="00000000000%s" % (1+ls)) |
| 47 | # Connect leaf to all spines |
| 48 | for s in range(spine): |
| 49 | switch = spines[s] |
| 50 | self.addLink(leafSwitch, switch, **linkopts) |
| 51 | # Add hosts under a leaf, fanout hosts per leaf switch |
| 52 | for f in range(fanout): |
| 53 | host = self.addHost('h%s' % (ls * fanout + f + 1), |
| 54 | cls=IpHost, |
| 55 | ip='10.0.%s.%s/24' % ((ls + 1), (f + 1)), |
| 56 | gateway='10.0.%s.254' % (ls + 1)) |
| 57 | self.addLink(host, leafSwitch, **linkopts) |
| 58 | |
| 59 | class IpHost(Host): |
| 60 | def __init__(self, name, gateway, *args, **kwargs): |
| 61 | super(IpHost, self).__init__(name, *args, **kwargs) |
| 62 | self.gateway = gateway |
| 63 | |
| 64 | def config(self, **kwargs): |
| 65 | Host.config(self, **kwargs) |
| 66 | mtu = "ifconfig "+self.name+"-eth0 mtu 1490" |
| 67 | self.cmd(mtu) |
| 68 | self.cmd('ip route add default via %s' % self.gateway) |
| 69 | |
| 70 | def config(opts): |
| 71 | spine = opts.spine |
| 72 | leaf = opts.leaf |
| 73 | fanout = opts.fanout |
| 74 | controllers= [ os.environ['OC%s' % i] for i in range(1,opts.onos+1) ] if (opts.onos) else ['127.0.0.1'] |
| 75 | topo = LeafAndSpine(spine=spine, leaf=leaf, fanout=fanout) |
| 76 | net = Mininet(topo=topo, link=TCLink, build=False, |
| 77 | switch=UserSwitch, |
| 78 | controller = None, |
| 79 | autoSetMacs = True) |
| 80 | i = 0 |
| 81 | for ip in controllers: |
| 82 | net.addController( "c%s" % (i), controller=RemoteController, ip=ip) |
| 83 | i += 1; |
| 84 | net.build() |
| 85 | net.start() |
| 86 | CLI(net) |
| 87 | net.stop() |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | setLogLevel('info') |
| 91 | config(opts) |
| 92 | os.system('sudo mn -c') |
| 93 | |