blob: 52e3184dad2b6a538f5ccfcc234acfb605cf5a74 [file] [log] [blame]
Masayoshi Kobayashie76395e2013-03-15 08:05:11 +00001#!/usr/bin/python
2
3NWID=3
4NR_NODES=20
5#Controllers=[{"ip":'127.0.0.1', "port":6633}, {"ip":'10.0.1.28', "port":6633}]
6Controllers=[{"ip":'10.0.1.223', "port":6633}]
7
8"""
9Start up a Simple topology
10"""
11from mininet.net import Mininet
12from mininet.node import Controller, RemoteController
13from mininet.log import setLogLevel, info, error, warn, debug
14from mininet.cli import CLI
15from mininet.topo import Topo
16from mininet.util import quietRun
17from mininet.moduledeps import pathCheck
18from mininet.link import Link, TCLink
19
20from sys import exit
21import os.path
22from subprocess import Popen, STDOUT, PIPE
23
24import sys
25
26#import argparse
27
28class MyController( Controller ):
29 def __init__( self, name, ip='127.0.0.1', port=6633, **kwargs):
30 """Init.
31 name: name to give controller
32 ip: the IP address where the remote controller is
33 listening
34 port: the port where the remote controller is listening"""
35 Controller.__init__( self, name, ip=ip, port=port, **kwargs )
36
37 def start( self ):
38 "Overridden to do nothing."
39 return
40
41 def stop( self ):
42 "Overridden to do nothing."
43 return
44
45 def checkListening( self ):
46 "Warn if remote controller is not accessible"
47 listening = self.cmd( "echo A | telnet -e A %s %d" %
48 ( self.ip, self.port ) )
49 if 'Unable' in listening:
50 warn( "Unable to contact the remote controller"
51 " at %s:%d\n" % ( self.ip, self.port ) )
52
53class SDNTopo( Topo ):
54 "SDN Topology"
55
56 def __init__( self, *args, **kwargs ):
57 Topo.__init__( self, *args, **kwargs )
58
59 switch = []
60 host = []
61 root = []
62
63 for i in range (NR_NODES):
64 name_suffix = '%02d' % NWID + "." + '%02d' % i
65 dpid_suffix = '%02x' % NWID + '%02x' % i
66 dpid = '0000' + '0000' + '0000' + dpid_suffix
67 sw = self.addSwitch('sw'+name_suffix, dpid=dpid)
68 switch.append(sw)
69
70 for i in range (NR_NODES):
71 host.append(self.addHost( 'host%d' % i ))
72
73 for i in range (NR_NODES):
74 root.append(self.addHost( 'root%d' % i, inNamespace=False ))
75
76 for i in range (NR_NODES):
77 self.addLink(host[i], switch[i])
78
79 for i in range (1, NR_NODES):
80 self.addLink(switch[0], switch[i])
81
82 for i in range (NR_NODES):
83 self.addLink(root[i], host[i])
84
85def startsshd( host ):
86 "Start sshd on host"
87 info( '*** Starting sshd\n' )
88 name, intf, ip = host.name, host.defaultIntf(), host.IP()
89 banner = '/tmp/%s.banner' % name
90 host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) )
91 host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' )
92 info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' )
93
94def startsshds ( hosts ):
95 for h in hosts:
96 startsshd( h )
97
98def stopsshd( ):
99 "Stop *all* sshd processes with a custom banner"
100 info( '*** Shutting down stale sshd/Banner processes ',
101 quietRun( "pkill -9 -f Banner" ), '\n' )
102
103def sdnnet(opt):
104 topo = SDNTopo()
105 info( '*** Creating network\n' )
106 #net = Mininet( topo=topo, controller=MyController, link=TCLink)
107 net = Mininet( topo=topo, link=TCLink, build=False)
108 controllers=[]
109 for c in Controllers:
110 rc = RemoteController('c%d' % Controllers.index(c), ip=c['ip'],port=c['port'])
111 print "controller ip %s port %s" % (c['ip'], c['port'])
112 controllers.append(rc)
113
114 net.controllers=controllers
115 net.build()
116
117 host = []
118 for i in range (NR_NODES):
119 host.append(net.get( 'host%d' % i ))
120
121 net.start()
122
123 sw=net.get('sw03.00')
124 print "center sw", sw
125 sw.attach('tapc0')
126
127 for i in range (NR_NODES):
128 host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,i))
129 host[i].defaultIntf().setMAC('00:00:00:00:%02x:%02x' % (NWID,i))
130
131 for i in range (NR_NODES):
132 for n in range (3):
133 for h in range (10):
134 host[i].setARP('192.168.%d.%d' % (n, h), '00:00:00:00:%02x:%02x' % (n,h))
135
136
137 root = []
138 for i in range (NR_NODES):
139 root.append(net.get( 'root%d' % i ))
140
141 for i in range (NR_NODES):
142 host[i].intf('host%d-eth1' % i).setIP('1.1.%d.1/24' % i)
143 root[i].intf('root%d-eth0' % i).setIP('1.1.%d.2/24' % i)
144
145 stopsshd ()
146 startsshds ( host )
147
148 if opt=="cli":
149 CLI(net)
150 stopsshd()
151 net.stop()
152
153if __name__ == '__main__':
154 setLogLevel( 'info' )
155 if len(sys.argv) == 1:
156 sdnnet("cli")
157 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
158 sdnnet("nocli")
159 else:
160 print "%s [-n]" % sys.argv[0]