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 | |
Masayoshi Kobayashi | 0e78045 | 2014-01-23 15:43:34 -0800 | [diff] [blame] | 24 | import argparse |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 = [] |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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): |
Pavlin Radoslavov | faae927 | 2013-11-26 18:51:27 -0800 | [diff] [blame] | 68 | host.append(self.addHost( 'host%d.%d' % (NWID, int(i)+1) )) |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 | |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 | |
Masayoshi Kobayashi | a3639cc | 2013-04-02 16:04:19 +0000 | [diff] [blame] | 89 | def startiperf( host ): |
Masayoshi Kobayashi | 05db3ce | 2013-04-06 00:50:37 +0000 | [diff] [blame] | 90 | host.cmd( '/usr/bin/iperf', '-s &' ) |
Masayoshi Kobayashi | a3639cc | 2013-04-02 16:04:19 +0000 | [diff] [blame] | 91 | |
| 92 | def startiperfs ( hosts ): |
| 93 | for h in hosts: |
| 94 | startiperf( h ) |
| 95 | |
| 96 | def stopiperf( ): |
| 97 | quietRun( "pkill -9 iperf" ) |
| 98 | |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 | |
Masayoshi Kobayashi | 0e78045 | 2014-01-23 15:43:34 -0800 | [diff] [blame] | 104 | def sdnnet(nocli, noarp): |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 |
Masayoshi Kobayashi | cee9f35 | 2014-01-23 01:13:35 -0800 | [diff] [blame] | 116 | #net.build() |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 117 | |
| 118 | host = [] |
| 119 | for i in range (NR_NODES): |
Pavlin Radoslavov | faae927 | 2013-11-26 18:51:27 -0800 | [diff] [blame] | 120 | host.append(net.get( 'host%d.%d' % (NWID, (int(i)+1)) )) |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 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 | |
| 128 | for i in range (NR_NODES): |
| 129 | host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,(int(i)+1))) |
| 130 | host[i].defaultIntf().setMAC('00:00:%02x:%02x:%02x:%02x' % (192,168,NWID,(int(i)+1))) |
| 131 | |
Masayoshi Kobayashi | 0e78045 | 2014-01-23 15:43:34 -0800 | [diff] [blame] | 132 | |
| 133 | if noarp == False: |
| 134 | for i in range (NR_NODES): |
| 135 | for n in range (2,9): |
| 136 | for h in range (25): |
| 137 | host[i].setARP('192.168.%d.%d' % (n, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(h)+1))) |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 138 | |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 139 | stopsshd () |
Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 140 | # stopiperf () |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 141 | startsshds ( host ) |
Masayoshi Kobayashi | d41701f | 2013-04-08 04:31:18 +0000 | [diff] [blame] | 142 | # startiperfs ( host ) |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 143 | |
Masayoshi Kobayashi | 0e78045 | 2014-01-23 15:43:34 -0800 | [diff] [blame] | 144 | if nocli == False: |
Masayoshi Kobayashi | f358ff5 | 2013-03-22 00:31:59 +0000 | [diff] [blame] | 145 | CLI(net) |
| 146 | stopsshd() |
| 147 | net.stop() |
| 148 | |
| 149 | if __name__ == '__main__': |
| 150 | setLogLevel( 'info' ) |
Masayoshi Kobayashi | 0e78045 | 2014-01-23 15:43:34 -0800 | [diff] [blame] | 151 | parser = argparse.ArgumentParser(description='mininet script') |
| 152 | parser.add_argument('-x', dest='noarp', action='store_true', |
| 153 | help='do not crete staric arp entries') |
| 154 | parser.add_argument('-n', dest='nocli', action='store_true', |
| 155 | help='do not run cli') |
| 156 | args = parser.parse_args() |
| 157 | sdnnet(args.nocli, args.noarp) |