Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | start_topo.py : Mininet topology builder for multiple controller. |
| 5 | |
| 6 | This script reads config file and creates a Mininet topology. |
| 7 | |
| 8 | Usage: |
| 9 | |
| 10 | $ sudo -E start_topo.py [filename] |
| 11 | |
| 12 | filename: Config filename to specify topology. |
| 13 | If omitted, "topo.`hostname`.py" or "topo.py" in current |
| 14 | directory will be used. |
| 15 | """ |
| 16 | |
| 17 | import platform |
| 18 | import time |
| 19 | import os.path |
| 20 | import getpass |
| 21 | from sys import argv |
| 22 | from time import sleep |
| 23 | |
| 24 | from mininet.node import Controller, OVSSwitch, RemoteController |
| 25 | from mininet.net import Mininet |
| 26 | from mininet.cli import CLI |
| 27 | from mininet.topo import Topo |
| 28 | from mininet.log import setLogLevel, info, warn |
| 29 | |
| 30 | usage = """ |
| 31 | Usage: Create Mininet topology. |
| 32 | $ sudo -E %s [filename] |
| 33 | filename: config file (topo.`hostname`.py by default) |
| 34 | """ % os.path.basename( argv[0] ) |
| 35 | |
| 36 | # Mininet must be run by root user |
| 37 | if ( getpass.getuser() != 'root' ): |
| 38 | print usage |
| 39 | exit( 0 ) |
| 40 | |
| 41 | argc = len( argv ) |
| 42 | if ( argc > 2 ): |
| 43 | print usage |
| 44 | exit( 0 ) |
| 45 | |
| 46 | if ( argc == 2 and ( argv[ 1 ] == "-h" or argv[ 1 ] == "--help" ) ): |
| 47 | print usage |
| 48 | exit( 0 ) |
| 49 | |
| 50 | if ( argc == 2 ): |
| 51 | topofile = argv[1] |
| 52 | else: |
| 53 | hostname = platform.node() |
| 54 | default_topo = 'topo.%s.py' % hostname |
| 55 | fallback_topo = 'topo.py' |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 56 | if os.path.exists(default_topo): |
| 57 | topofile = default_topo |
| 58 | else: |
| 59 | topofile = fallback_topo |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 60 | |
| 61 | if not os.path.exists( topofile ): |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 62 | print( 'Config file %s not found' % topofile ) |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 63 | exit( 1 ) |
| 64 | |
| 65 | execfile( topofile ) |
| 66 | conf = createTopo() |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 67 | |
| 68 | class ConfigTopo( Topo ): |
| 69 | "Topology created from config." |
| 70 | |
| 71 | def __init__( self, *args, **kwargs ): |
| 72 | "Create custom topo." |
| 73 | |
| 74 | Topo.__init__( self, *args, **kwargs ) |
| 75 | |
| 76 | # Add hosts and switches |
| 77 | nmap = {} |
| 78 | for s, prop in conf[ 'switches' ].iteritems(): |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 79 | s = s.strip() |
| 80 | dpid = None |
| 81 | if 'dpid' in prop: |
| 82 | dpid = prop[ 'dpid' ].strip() |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 83 | nmap[ s ] = self.addSwitch( s, dpid=dpid ) |
| 84 | |
| 85 | for h, prop in conf[ 'hosts' ].iteritems(): |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 86 | h = h.strip() |
| 87 | params = {} |
| 88 | if 'mac' in prop: |
| 89 | mac = prop[ 'mac' ].strip() |
| 90 | params[ 'mac' ] = mac |
| 91 | if 'ip' in prop: |
| 92 | ip = prop[ 'ip' ].strip() |
| 93 | params[ 'ip' ] = ip |
| 94 | nmap[ h ] = self.addHost( h, **params ) |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 95 | |
| 96 | # Add links |
| 97 | for l in conf[ 'links' ]: |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 98 | node1 = nmap[ l[ 'node1' ] ].strip() |
| 99 | node2 = nmap[ l[ 'node2' ] ].strip() |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 100 | self.addLink( node1, node2 ) |
| 101 | |
| 102 | class ClusterConnectedSwitch( OVSSwitch ): |
| 103 | "OVSSwitch connected to controller cluster." |
| 104 | |
| 105 | def start( self, controllers ): |
| 106 | # make sure controllers contains only a ControllerCluster |
| 107 | assert len( controllers ) == 1 |
| 108 | ccluster = controllers[ 0 ] |
| 109 | assert type( ccluster ) == ControllerCluster |
| 110 | |
| 111 | controller_list = ccluster.clist( self.name ) |
| 112 | # TODO: manage order of controllers to control mastership |
| 113 | OVSSwitch.start( self, controllers=controller_list ) |
| 114 | |
| 115 | class ControllerCluster( Controller ): |
| 116 | "Cluster of controllers. Relationship between controllers and switches is defined by config file" |
| 117 | |
| 118 | def __init__( self, name, **params ): |
| 119 | self.sw_clist_map = {} |
| 120 | self.cmap = {} |
| 121 | |
| 122 | Controller.__init__( self, name, **params ) |
| 123 | |
| 124 | for cname, addr in conf[ 'controllers' ].iteritems(): |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 125 | cname = cname.strip() |
| 126 | addr = addr.strip() |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 127 | ip, port = addr.split( ':' ) |
| 128 | self.cmap[ cname ] = RemoteController( cname, ip=ip, port=int( port ) ) |
| 129 | |
| 130 | for sw, params in conf[ 'switches' ].iteritems(): |
| 131 | clist = [] |
| 132 | for c in params[ 'controllers' ]: |
| 133 | assert not self.cmap[ c ] is None |
| 134 | clist.append( self.cmap[ c ] ) |
| 135 | self.sw_clist_map[ sw ] = clist |
| 136 | |
| 137 | def start( self ): |
| 138 | # do nothing |
| 139 | return |
| 140 | |
| 141 | def stop( self ): |
| 142 | # do nothing |
| 143 | return |
| 144 | |
| 145 | def checkListening( self ): |
| 146 | for c in self.cmap.values(): |
| 147 | c.checkListening() |
| 148 | |
| 149 | def clist( self, sw ): |
| 150 | return self.sw_clist_map[ sw ] |
| 151 | |
| 152 | if __name__ == '__main__': |
| 153 | setLogLevel( 'info' ) |
Naoki Shiota | 801875c | 2014-05-29 17:06:06 -0700 | [diff] [blame] | 154 | info( "Topology file : %s\n" % topofile ) |
Naoki Shiota | 2322a2f | 2014-05-21 19:47:40 -0700 | [diff] [blame] | 155 | net = Mininet( topo=ConfigTopo(), |
| 156 | controller=ControllerCluster, |
| 157 | switch=ClusterConnectedSwitch ) |
| 158 | net.start() |
| 159 | CLI( net ) |
| 160 | net.stop() |