blob: c8901773e9352369835f243083c61c9440ffd129 [file] [log] [blame]
Flavio Castrocc38a542016-03-03 13:15:46 -08001#!/usr/bin/python
2
3import os
4from optparse import OptionParser
5
6from mininet.net import Mininet
7from mininet.topo import Topo
8from mininet.node import RemoteController, UserSwitch, Host
9from mininet.link import TCLink
10from mininet.log import setLogLevel
11from mininet.cli import CLI
12
Flavio Castrocc38a542016-03-03 13:15:46 -080013
Flavio Castro5608a392016-06-22 17:02:35 -070014# Parse command line options and dump results
15def parseOptions( ):
16 """Parse command line options"""
17 parser = OptionParser( )
18 parser.add_option( '--spine', dest='spine', type='int', default=2,
19 help='number of spine switches, default=2' )
20 parser.add_option( '--leaf', dest='leaf', type='int', default=2,
21 help='number of leaf switches, default=2' )
22 parser.add_option( '--fanout', dest='fanout', type='int', default=2,
23 help='number of hosts per leaf switch, default=2' )
24 parser.add_option( '--onos', dest='onos', type='int', default=0,
25 help='number of ONOS Instances, default=0, 0 means localhost, 1 will use OC1 and so on' )
26
27 (options, args) = parser.parse_args( )
Flavio Castrocc38a542016-03-03 13:15:46 -080028 return options, args
29
Flavio Castrocc38a542016-03-03 13:15:46 -080030
Flavio Castro5608a392016-06-22 17:02:35 -070031opts, args = parseOptions( )
32
33
34class LeafAndSpine( Topo ):
35 def __init__( self, spine=2, leaf=2, fanout=2, **opts ):
Flavio Castrocc38a542016-03-03 13:15:46 -080036 "Create Leaf and Spine Topo."
37
Flavio Castro5608a392016-06-22 17:02:35 -070038 Topo.__init__( self, **opts )
Flavio Castrocc38a542016-03-03 13:15:46 -080039
40 # Add spine switches
Flavio Castro5608a392016-06-22 17:02:35 -070041 spines = { }
42 for s in range( spine ):
43 spines[ s ] = self.addSwitch( 'spine10%s' % (s + 1),
44 dpid="00000000010%s" % (s + 1) )
Flavio Castrocc38a542016-03-03 13:15:46 -080045 # Set link speeds to 100Mb/s
Flavio Castro5608a392016-06-22 17:02:35 -070046 linkopts = dict( bw=100 )
Flavio Castrocc38a542016-03-03 13:15:46 -080047
48 # Add Leaf switches
Flavio Castro5608a392016-06-22 17:02:35 -070049 for ls in range( leaf ):
50 leafSwitch = self.addSwitch( 'leaf%s' % (ls + 1),
51 dpid="00000000000%s" % (1 + ls) )
Flavio Castrocc38a542016-03-03 13:15:46 -080052 # Connect leaf to all spines
Flavio Castro5608a392016-06-22 17:02:35 -070053 for s in range( spine ):
54 switch = spines[ s ]
55 self.addLink( leafSwitch, switch, **linkopts )
Flavio Castrocc38a542016-03-03 13:15:46 -080056 # Add hosts under a leaf, fanout hosts per leaf switch
Flavio Castro5608a392016-06-22 17:02:35 -070057 for f in range( fanout ):
58 host = self.addHost( 'h%s' % (ls * fanout + f + 1),
59 cls=IpHost,
60 ip='10.0.%s.%s/24' % ((ls + 1), (f + 1)),
61 gateway='10.0.%s.254' % (ls + 1) )
62 self.addLink( host, leafSwitch, **linkopts )
Flavio Castrocc38a542016-03-03 13:15:46 -080063
Flavio Castro5608a392016-06-22 17:02:35 -070064
65class IpHost( Host ):
66 def __init__( self, name, gateway, *args, **kwargs ):
67 super( IpHost, self ).__init__( name, *args, **kwargs )
Flavio Castrocc38a542016-03-03 13:15:46 -080068 self.gateway = gateway
69
Flavio Castro5608a392016-06-22 17:02:35 -070070 def config( self, **kwargs ):
71 Host.config( self, **kwargs )
72 mtu = "ifconfig " + self.name + "-eth0 mtu 1490"
73 self.cmd( mtu )
74 self.cmd( 'ip route add default via %s' % self.gateway )
Flavio Castrocc38a542016-03-03 13:15:46 -080075
Flavio Castro5608a392016-06-22 17:02:35 -070076
77def config( opts ):
Flavio Castrocc38a542016-03-03 13:15:46 -080078 spine = opts.spine
79 leaf = opts.leaf
Flavio Castro5608a392016-06-22 17:02:35 -070080 fanout = opts.fanout
81 controllers = [ os.environ[ 'OC%s' % i ] for i in
82 range( 1, opts.onos + 1 ) ] if (opts.onos) else [
83 '127.0.0.1' ]
84 topo = LeafAndSpine( spine=spine, leaf=leaf, fanout=fanout )
85 net = Mininet( topo=topo, link=TCLink, build=False,
86 switch=UserSwitch,
87 controller=None,
88 autoSetMacs=True )
Flavio Castrocc38a542016-03-03 13:15:46 -080089 i = 0
90 for ip in controllers:
Flavio Castro5608a392016-06-22 17:02:35 -070091 net.addController( "c%s" % (i), controller=RemoteController, ip=ip )
Flavio Castrocc38a542016-03-03 13:15:46 -080092 i += 1;
Flavio Castro5608a392016-06-22 17:02:35 -070093 net.build( )
94 net.start( )
95 CLI( net )
96 net.stop( )
97
Flavio Castrocc38a542016-03-03 13:15:46 -080098
99if __name__ == '__main__':
Flavio Castro5608a392016-06-22 17:02:35 -0700100 setLogLevel( 'info' )
101 config( opts )
102 os.system( 'sudo mn -c' )