blob: c1843a1e559d159aeb7f059c8a1d3ba7cad2e6b1 [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 = []
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
81def 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
90def startsshds ( hosts ):
91 for h in hosts:
92 startsshd( h )
93
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000094def startiperf( host ):
Masayoshi Kobayashi05db3ce2013-04-06 00:50:37 +000095 host.cmd( '/usr/bin/iperf', '-s &' )
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000096
97def startiperfs ( hosts ):
98 for h in hosts:
99 startiperf( h )
100
101def stopiperf( ):
102 quietRun( "pkill -9 iperf" )
103
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000104def 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
109def 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 Kobayashi77485e12013-04-02 09:47:55 +0000138 for n in range (2,9):
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +0000139 for h in range (25):
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000140 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 Kobayashia3639cc2013-04-02 16:04:19 +0000151 stopiperf ()
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000152 startsshds ( host )
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +0000153 startiperfs ( host )
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000154
155 if opt=="cli":
156 CLI(net)
157 stopsshd()
158 net.stop()
159
160if __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]