Thomas Vachuska | 88dbd49 | 2014-10-29 13:04:27 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | ''' file: optical.py ''' |
| 4 | |
| 5 | from mininet.topo import Topo |
| 6 | from mininet.node import RemoteController |
| 7 | from mininet.net import Mininet |
| 8 | from mininet.cli import CLI |
| 9 | from mininet.log import setLogLevel, info |
| 10 | from mininet.link import Intf, Link |
| 11 | from mininet.util import irange |
| 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 | |
| 25 | class OpticalTopo( Topo ): |
| 26 | |
| 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 ): |
| 33 | |
| 34 | # Add hosts and switches |
| 35 | hosts = [] |
| 36 | switches = [] |
| 37 | for i in irange( 1, n ): |
| 38 | h = self.addHost( 'h%d' % i ) |
| 39 | s = self.addSwitch( 's%d' % i, dpid="0000ffffffff%04d" % i ) |
| 40 | self.addLink( h, s ) |
| 41 | hosts.append( h ) |
| 42 | switches.append( s ) |
| 43 | |
| 44 | # Add optical tap interfaces |
| 45 | tapNum = tapStart |
| 46 | for sw in switches: |
| 47 | self.addIntf( sw, 'tap%d' % tapNum ) |
| 48 | tapNum += 1 |
| 49 | |
| 50 | # if you use, sudo mn --custom custom/optical.py, then register the topo: |
| 51 | #sudo mn --custom optical.py --topo optical,5 |
| 52 | topos = { 'optical': OpticalTopo } |
| 53 | |
| 54 | def installStaticFlows( net ): |
| 55 | for sw in net.switches: |
| 56 | info( 'Adding flows to %s...' % sw.name ) |
| 57 | sw.dpctl( 'add-flow', 'in_port=1,actions=output=2' ) |
| 58 | sw.dpctl( 'add-flow', 'in_port=2,actions=output=1' ) |
| 59 | info( sw.dpctl( 'dump-flows' ) ) |
| 60 | |
| 61 | def run( n ): |
| 62 | topo = OpticalTopo( n ) |
| 63 | net = Mininet( topo=topo, controller=RemoteController, autoSetMacs=True ) |
| 64 | net.start() |
| 65 | #installStaticFlows( net ) |
| 66 | CLI( net ) |
| 67 | net.stop() |
| 68 | |
| 69 | # if the script is run directly (sudo custom/optical.py): |
| 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 |
| 78 | setLogLevel( 'info' ) |
| 79 | run( n ) |