blob: e340f38408e7a44cd9fd32542074de2de1c016e6 [file] [log] [blame]
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +00001#!/usr/bin/python
2NWID=__NWID__
3NR_NODES=__NRSW__
4Controllers=[{"ip":"127.0.0.1", "port":6633}]
5
6"""
7Start up a Simple topology
8"""
9from mininet.net import Mininet
10from mininet.node import Controller, RemoteController
11from mininet.log import setLogLevel, info, error, warn, debug
12from mininet.cli import CLI
13from mininet.topo import Topo
14from mininet.util import quietRun
15from mininet.moduledeps import pathCheck
16from mininet.link import Link, TCLink
17
18from sys import exit
19import os.path
20from subprocess import Popen, STDOUT, PIPE
21
22import sys
23
24#import argparse
25
26class 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
51class SDNTopo( Topo ):
52 "SDN Topology"
53
54 def __init__( self, *args, **kwargs ):
55 Topo.__init__( self, *args, **kwargs )
56
57 switch = []
58 host = []
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000059
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' % (int(i)+1) ))
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000069
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 Kobayashif358ff52013-03-22 00:31:59 +000076def 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
85def startsshds ( hosts ):
86 for h in hosts:
87 startsshd( h )
88
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000089def startiperf( host ):
Masayoshi Kobayashi05db3ce2013-04-06 00:50:37 +000090 host.cmd( '/usr/bin/iperf', '-s &' )
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000091
92def startiperfs ( hosts ):
93 for h in hosts:
94 startiperf( h )
95
96def stopiperf( ):
97 quietRun( "pkill -9 iperf" )
98
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000099def 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
104def 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' % (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
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
132 for i in range (NR_NODES):
Masayoshi Kobayashi77485e12013-04-02 09:47:55 +0000133 for n in range (2,9):
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +0000134 for h in range (25):
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000135 host[i].setARP('192.168.%d.%d' % (n, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(h)+1)))
136
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000137 stopsshd ()
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000138# stopiperf ()
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000139 startsshds ( host )
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000140# startiperfs ( host )
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000141
142 if opt=="cli":
143 CLI(net)
144 stopsshd()
145 net.stop()
146
147if __name__ == '__main__':
148 setLogLevel( 'info' )
149 if len(sys.argv) == 1:
150 sdnnet("cli")
151 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
152 sdnnet("nocli")
153 else:
154 print "%s [-n]" % sys.argv[0]