Thomas Vachuska

Updated optical.py to allow parametrized topologies.

1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 -''' file: custom/optical.py ''' 3 +''' file: optical.py '''
4 4
5 from mininet.topo import Topo 5 from mininet.topo import Topo
6 +from mininet.node import RemoteController
6 from mininet.net import Mininet 7 from mininet.net import Mininet
7 from mininet.cli import CLI 8 from mininet.cli import CLI
8 from mininet.log import setLogLevel, info 9 from mininet.log import setLogLevel, info
10 +from mininet.link import Intf, Link
9 from mininet.util import irange 11 from mininet.util import irange
10 12
13 +class NullIntf( Intf ):
14 + "A dummy interface with a blank name that doesn't do any configuration"
15 + def __init__( self, name, **params ):
16 + self.name = ''
17 +
18 +class NullLink( Link ):
19 + "A dummy link that doesn't touch either interface"
20 + def makeIntfPair( cls, intf1, intf2, addr1=None, addr2=None ):
21 + pass
22 + def delete( self ):
23 + pass
24 +
11 class OpticalTopo( Topo ): 25 class OpticalTopo( Topo ):
12 26
13 - def build( self, n=3, tapStart=3 ): 27 + def addIntf( self, switch, intfName ):
28 + "Add intf intfName to switch"
29 + self.addLink( switch, switch, cls=NullLink,
30 + intfName1=intfName, cls2=NullIntf, intfName2=intfName )
31 +
32 + def build( self, n=2, tapStart=3 ):
14 33
15 # Add hosts and switches 34 # Add hosts and switches
16 hosts = [] 35 hosts = []
17 switches = [] 36 switches = []
18 for i in irange( 1, n ): 37 for i in irange( 1, n ):
19 h = self.addHost( 'h%d' % i ) 38 h = self.addHost( 'h%d' % i )
20 - s = self.addSwitch( 's%d' % i ) 39 + s = self.addSwitch( 's%d' % i, dpid="0000ffffffff%04d" % i )
21 self.addLink( h, s ) 40 self.addLink( h, s )
22 hosts.append( h ) 41 hosts.append( h )
23 switches.append( s ) 42 switches.append( s )
...@@ -25,23 +44,23 @@ class OpticalTopo( Topo ): ...@@ -25,23 +44,23 @@ class OpticalTopo( Topo ):
25 # Add optical tap interfaces 44 # Add optical tap interfaces
26 tapNum = tapStart 45 tapNum = tapStart
27 for sw in switches: 46 for sw in switches:
28 - self.addLink( sw, sw, intfName1='%s-eth0' % sw, intfName2='tap%d' % tapNum ) 47 + self.addIntf( sw, 'tap%d' % tapNum )
29 tapNum += 1 48 tapNum += 1
30 49
31 # if you use, sudo mn --custom custom/optical.py, then register the topo: 50 # if you use, sudo mn --custom custom/optical.py, then register the topo:
32 -#sudo mn --custom optical-topo.py --topo optical,5 51 +#sudo mn --custom optical.py --topo optical,5
33 topos = { 'optical': OpticalTopo } 52 topos = { 'optical': OpticalTopo }
34 53
35 def installStaticFlows( net ): 54 def installStaticFlows( net ):
36 - for swName in [ 's1', 's2', 's3', 's4', 's5', 's6' ]: 55 + for sw in net.switches:
37 - info( 'Adding flows to %s...' % swName ) 56 + info( 'Adding flows to %s...' % sw.name )
38 - sw = net[ swName ]
39 sw.dpctl( 'add-flow', 'in_port=1,actions=output=2' ) 57 sw.dpctl( 'add-flow', 'in_port=1,actions=output=2' )
40 sw.dpctl( 'add-flow', 'in_port=2,actions=output=1' ) 58 sw.dpctl( 'add-flow', 'in_port=2,actions=output=1' )
41 info( sw.dpctl( 'dump-flows' ) ) 59 info( sw.dpctl( 'dump-flows' ) )
42 60
43 -def run(): 61 +def run( n ):
44 - net = Mininet( topo=OpticalTopo() ) 62 + topo = OpticalTopo( n )
63 + net = Mininet( topo=topo, controller=RemoteController, autoSetMacs=True )
45 net.start() 64 net.start()
46 #installStaticFlows( net ) 65 #installStaticFlows( net )
47 CLI( net ) 66 CLI( net )
...@@ -49,5 +68,12 @@ def run(): ...@@ -49,5 +68,12 @@ def run():
49 68
50 # if the script is run directly (sudo custom/optical.py): 69 # if the script is run directly (sudo custom/optical.py):
51 if __name__ == '__main__': 70 if __name__ == '__main__':
71 + import sys
72 + try:
73 + n = int( sys.argv[1] )
74 + except:
75 + print ( 'Usage: ./optical.py n # n is number of switches\n'
76 + 'Starting with default of 2 switches...\n' )
77 + n = 2
52 setLogLevel( 'info' ) 78 setLogLevel( 'info' )
53 - run() 79 + run( n )
......