Masayoshi Kobayashi | 36b7bec | 2013-02-27 09:16:56 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | NWID="a" |
| 4 | Controllers=[{"ip":'127.0.0.1', "port":6633}, {"ip":'10.0.1.28', "port":6633}] |
| 5 | #Controllers=[{"ip":'127.0.0.1', "port":6633}] |
| 6 | |
| 7 | """ |
| 8 | Start up a Simple topology |
| 9 | """ |
| 10 | from mininet.net import Mininet |
| 11 | from mininet.node import Controller, RemoteController |
| 12 | from mininet.log import setLogLevel, info, error, warn, debug |
| 13 | from mininet.cli import CLI |
| 14 | from mininet.topo import Topo |
| 15 | from mininet.util import quietRun |
| 16 | from mininet.moduledeps import pathCheck |
| 17 | from mininet.link import Link, TCLink |
| 18 | |
| 19 | from sys import exit |
| 20 | import os.path |
| 21 | from subprocess import Popen, STDOUT, PIPE |
| 22 | |
| 23 | import sys |
| 24 | |
| 25 | #import argparse |
| 26 | |
| 27 | class MyController( Controller ): |
| 28 | def __init__( self, name, ip='127.0.0.1', port=6633, **kwargs): |
| 29 | """Init. |
| 30 | name: name to give controller |
| 31 | ip: the IP address where the remote controller is |
| 32 | listening |
| 33 | port: the port where the remote controller is listening""" |
| 34 | Controller.__init__( self, name, ip=ip, port=port, **kwargs ) |
| 35 | |
| 36 | def start( self ): |
| 37 | "Overridden to do nothing." |
| 38 | return |
| 39 | |
| 40 | def stop( self ): |
| 41 | "Overridden to do nothing." |
| 42 | return |
| 43 | |
| 44 | def checkListening( self ): |
| 45 | "Warn if remote controller is not accessible" |
| 46 | listening = self.cmd( "echo A | telnet -e A %s %d" % |
| 47 | ( self.ip, self.port ) ) |
| 48 | if 'Unable' in listening: |
| 49 | warn( "Unable to contact the remote controller" |
| 50 | " at %s:%d\n" % ( self.ip, self.port ) ) |
| 51 | |
| 52 | class SDNTopo( Topo ): |
| 53 | "SDN Topology" |
| 54 | |
| 55 | def __init__( self, *args, **kwargs ): |
| 56 | Topo.__init__( self, *args, **kwargs ) |
| 57 | |
| 58 | dist_sw = [] |
| 59 | aggr_sw = [] |
| 60 | core_sw = [] |
| 61 | host = [] |
| 62 | root = [] |
| 63 | |
| 64 | for i in range (4): |
| 65 | name_suffix = '%s' % NWID + '0d' + '%02d' % i |
| 66 | dpid = '0000' + '0000' + '000' + name_suffix |
| 67 | sw = self.addSwitch('sw'+name_suffix, dpid=dpid) |
| 68 | dist_sw.append(sw) |
| 69 | |
| 70 | for i in range (4): |
| 71 | name_suffix = '%s' % NWID + '0a' + '%02d' % i |
| 72 | dpid = '0000' + '0000' + '000' + name_suffix |
| 73 | sw = self.addSwitch('sw'+name_suffix, dpid=dpid) |
| 74 | aggr_sw.append(sw) |
| 75 | |
| 76 | for i in range (1): |
| 77 | name_suffix = '%s' % NWID + '0c' + '%02d' % i |
| 78 | dpid = '0000' + '0000' + '000' + name_suffix |
| 79 | sw = self.addSwitch('sw' + name_suffix, dpid=dpid) |
| 80 | core_sw.append(sw) |
| 81 | |
| 82 | for i in range (8): |
| 83 | host.append(self.addHost( 'host%d' % i )) |
| 84 | |
| 85 | for i in range (8): |
| 86 | root.append(self.addHost( 'root%d' % i, inNamespace=False )) |
| 87 | |
| 88 | for i in range (8): |
| 89 | self.addLink(host[i], dist_sw[int(i/2)]) |
| 90 | |
| 91 | for i in range (4): |
| 92 | for j in range (int(i/2) * 2, (int(i/2) * 2 + 2)): |
| 93 | print 'add link btween dist_sw[%d] -> aggr_sw[%d]' % (i, j) |
| 94 | self.addLink(dist_sw[i], aggr_sw[j]) |
| 95 | |
| 96 | for i in range (4): |
| 97 | for j in range (1): |
| 98 | print 'add link btween aggr_sw[%d] -> core_sw[%d]' % (i, j) |
| 99 | self.addLink(aggr_sw[i], core_sw[j]) |
| 100 | |
| 101 | for i in range (8): |
| 102 | self.addLink(root[i], host[i]) |
| 103 | |
| 104 | def startsshd( host ): |
| 105 | "Start sshd on host" |
| 106 | info( '*** Starting sshd\n' ) |
| 107 | name, intf, ip = host.name, host.defaultIntf(), host.IP() |
| 108 | banner = '/tmp/%s.banner' % name |
| 109 | host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) ) |
| 110 | host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' ) |
| 111 | info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' ) |
| 112 | |
| 113 | def startsshds ( hosts ): |
| 114 | for h in hosts: |
| 115 | startsshd( h ) |
| 116 | |
| 117 | def stopsshd( ): |
| 118 | "Stop *all* sshd processes with a custom banner" |
| 119 | info( '*** Shutting down stale sshd/Banner processes ', |
| 120 | quietRun( "pkill -9 -f Banner" ), '\n' ) |
| 121 | |
| 122 | def sdnnet(opt): |
| 123 | topo = SDNTopo() |
| 124 | info( '*** Creating network\n' ) |
| 125 | #net = Mininet( topo=topo, controller=MyController, link=TCLink) |
| 126 | net = Mininet( topo=topo, link=TCLink, build=False) |
| 127 | controllers=[] |
| 128 | for c in Controllers: |
| 129 | rc = RemoteController('c%d' % Controllers.index(c), ip=c['ip'],port=c['port']) |
| 130 | print "controller ip %s port %s" % (c['ip'], c['port']) |
| 131 | controllers.append(rc) |
| 132 | |
| 133 | net.controllers=controllers |
| 134 | net.build() |
| 135 | |
| 136 | host = [] |
| 137 | for i in range (8): |
| 138 | host.append(net.get( 'host%d' % i )) |
| 139 | |
| 140 | net.start() |
| 141 | |
| 142 | core_sw = [] |
| 143 | for i in range (1): |
| 144 | name_suffix = '%s' % NWID + '0c' + '%02d' % i |
| 145 | net.get('sw' + name_suffix).attach('tap%s0' % NWID) |
| 146 | |
| 147 | for i in range (8): |
| 148 | host[i].defaultIntf().setIP('192.168.10.10%d/24' % i) |
| 149 | |
| 150 | root = [] |
| 151 | for i in range (8): |
| 152 | root.append(net.get( 'root%d' % i )) |
| 153 | |
| 154 | for i in range (8): |
| 155 | host[i].intf('host%d-eth1' % i).setIP('1.1.%d.1/24' % i) |
| 156 | root[i].intf('root%d-eth0' % i).setIP('1.1.%d.2/24' % i) |
| 157 | |
| 158 | stopsshd () |
| 159 | startsshds ( host ) |
| 160 | |
| 161 | if opt=="cli": |
| 162 | CLI(net) |
| 163 | stopsshd() |
| 164 | net.stop() |
| 165 | |
| 166 | if __name__ == '__main__': |
| 167 | setLogLevel( 'info' ) |
| 168 | if len(sys.argv) == 1: |
| 169 | sdnnet("cli") |
| 170 | elif len(sys.argv) == 2 and sys.argv[1] == "-n": |
| 171 | sdnnet("nocli") |
| 172 | else: |
| 173 | print "%s [-n]" % sys.argv[0] |