Masayoshi Kobayashi | 80493be | 2013-08-01 17:33:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | import sys |
| 3 | |
| 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 | |
| 60 | for i in range (NR_NODES): |
| 61 | name_suffix = '%02d' % NWID + "." + '%02d' % (int(i)+1) |
| 62 | dpid_suffix = '%02x' % NWID + '%02x' % (int(i)+1) |
| 63 | dpid = '0000' + '0000' + '0000' + dpid_suffix |
| 64 | sw = self.addSwitch('sw'+name_suffix, dpid=dpid) |
| 65 | switch.append(sw) |
| 66 | |
| 67 | for i in range (NR_NODES): |
| 68 | host.append(self.addHost( 'host%d.%d' % (NWID,int(i)+1) )) |
| 69 | |
| 70 | for i in range (NR_NODES): |
| 71 | self.addLink(host[i], switch[i]) |
| 72 | |
| 73 | for i in range (1, NR_NODES): |
| 74 | self.addLink(switch[0], switch[i]) |
| 75 | |
| 76 | def startsshd( host ): |
| 77 | "Start sshd on host" |
| 78 | info( '*** Starting sshd\n' ) |
| 79 | name, intf, ip = host.name, host.defaultIntf(), host.IP() |
| 80 | banner = '/tmp/%s.banner' % name |
| 81 | host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) ) |
| 82 | host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' ) |
| 83 | info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' ) |
| 84 | |
| 85 | def startsshds ( hosts ): |
| 86 | for h in hosts: |
| 87 | startsshd( h ) |
| 88 | |
| 89 | def startiperf( host ): |
| 90 | host.cmd( '/usr/bin/iperf', '-s &' ) |
| 91 | |
| 92 | def startiperfs ( hosts ): |
| 93 | for h in hosts: |
| 94 | startiperf( h ) |
| 95 | |
| 96 | def stopiperf( ): |
| 97 | quietRun( "pkill -9 iperf" ) |
| 98 | |
| 99 | def stopsshd( ): |
| 100 | "Stop *all* sshd processes with a custom banner" |
| 101 | info( '*** Shutting down stale sshd/Banner processes ', |
| 102 | quietRun( "pkill -9 -f Banner" ), '\n' ) |
| 103 | |
| 104 | def sdnnet(opt): |
| 105 | topo = SDNTopo() |
| 106 | info( '*** Creating network\n' ) |
| 107 | net = Mininet( topo=topo, controller=MyController, link=TCLink) |
| 108 | #net = Mininet( topo=topo, link=TCLink, build=False) |
| 109 | #controllers=[] |
| 110 | #for c in Controllers: |
| 111 | # rc = RemoteController('c%d' % Controllers.index(c), ip=c['ip'],port=c['port']) |
| 112 | # print "controller ip %s port %s" % (c['ip'], c['port']) |
| 113 | # controllers.append(rc) |
| 114 | |
| 115 | #net.controllers=controllers |
| 116 | net.build() |
| 117 | |
| 118 | host = [] |
| 119 | for i in range (NR_NODES): |
| 120 | host.append(net.get( 'host%d.%d' % (NWID,int(i)+1) )) |
| 121 | |
| 122 | net.start() |
| 123 | |
| 124 | sw=net.get('sw%02x.%02x' % (NWID,1)) |
| 125 | print "center sw", sw |
| 126 | sw.attach('tap%02x_1' % NWID) |
| 127 | sw.attach('tap%02x_2' % NWID) |
| 128 | |
| 129 | for i in range (NR_NODES): |
| 130 | host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,(int(i)+1))) |
| 131 | host[i].defaultIntf().setMAC('00:00:%02x:%02x:%02x:%02x' % (192,168,NWID,(int(i)+1))) |
| 132 | |
| 133 | for i in range (NR_NODES): |
| 134 | for n in range (2,9): |
| 135 | for h in range (NR_NODES): |
| 136 | host[i].setARP('192.168.%d.%d' % (n, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(h)+1))) |
| 137 | |
| 138 | stopsshd () |
| 139 | # stopiperf () |
| 140 | startsshds ( host ) |
| 141 | # startiperfs ( host ) |
| 142 | |
| 143 | if opt=="cli": |
| 144 | CLI(net) |
| 145 | stopsshd() |
| 146 | net.stop() |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | setLogLevel( 'info' ) |
| 150 | if len(sys.argv) == 3: |
| 151 | NWID=int(sys.argv[1]) |
| 152 | NR_NODES=int(sys.argv[2]) |
| 153 | sdnnet("nocli") |
| 154 | else: |
| 155 | print "%s <NWID> <NR_NODES>" % sys.argv[0] |
| 156 | sys.exit(1) |