blob: a1ac11cf41e030608c3252abffd8bb38929bcde4 [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
94def stopsshd( ):
95 "Stop *all* sshd processes with a custom banner"
96 info( '*** Shutting down stale sshd/Banner processes ',
97 quietRun( "pkill -9 -f Banner" ), '\n' )
98
99def sdnnet(opt):
100 topo = SDNTopo()
101 info( '*** Creating network\n' )
102 net = Mininet( topo=topo, controller=MyController, link=TCLink)
103 #net = Mininet( topo=topo, link=TCLink, build=False)
104 #controllers=[]
105 #for c in Controllers:
106 # rc = RemoteController('c%d' % Controllers.index(c), ip=c['ip'],port=c['port'])
107 # print "controller ip %s port %s" % (c['ip'], c['port'])
108 # controllers.append(rc)
109
110 #net.controllers=controllers
111 net.build()
112
113 host = []
114 for i in range (NR_NODES):
115 host.append(net.get( 'host%d' % (int(i)+1) ))
116
117 net.start()
118
119 sw=net.get('sw%02x.%02x' % (NWID,1))
120 print "center sw", sw
121 sw.attach('tap%02x_1' % NWID)
122
123 for i in range (NR_NODES):
124 host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,(int(i)+1)))
125 host[i].defaultIntf().setMAC('00:00:%02x:%02x:%02x:%02x' % (192,168,NWID,(int(i)+1)))
126
127 for i in range (NR_NODES):
128 for n in range (1,8):
129 for h in range (25):
130 host[i].setARP('192.168.%d.%d' % (n, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(h)+1)))
131
132 root = []
133 for i in range (NR_NODES):
134 root.append(net.get( 'root%d' % (int(i)+1) ))
135
136 for i in range (NR_NODES):
137 host[i].intf('host%d-eth1' % (int(i)+1)).setIP('1.1.%d.1/24' % (int(i)+1))
138 root[i].intf('root%d-eth0' % (int(i)+1)).setIP('1.1.%d.2/24' % (int(i)+1))
139
140 stopsshd ()
141 startsshds ( host )
142
143 if opt=="cli":
144 CLI(net)
145 stopsshd()
146 net.stop()
147
148if __name__ == '__main__':
149 setLogLevel( 'info' )
150 if len(sys.argv) == 1:
151 sdnnet("cli")
152 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
153 sdnnet("nocli")
154 else:
155 print "%s [-n]" % sys.argv[0]