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