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