blob: 0fc96f1f7e07023ca433f9ccce31ca0905b8930e [file] [log] [blame]
Masayoshi Kobayashi7dd03eb2013-03-13 00:07:27 +00001#!/usr/bin/python
2
3NWID=1
Masayoshi Kobayashic2acf782013-03-13 22:06:29 +00004NR_NODES=20
Masayoshi Kobayashi7dd03eb2013-03-13 00:07:27 +00005#Controllers=[{"ip":'127.0.0.1', "port":6633}, {"ip":'10.0.1.28', "port":6633}]
Masayoshi Kobayashic2acf782013-03-13 22:06:29 +00006Controllers=[{"ip":'10.0.1.28', "port":6633}]
Masayoshi Kobayashi7dd03eb2013-03-13 00:07:27 +00007
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
Masayoshi Kobayashic2acf782013-03-13 22:06:29 +0000123 sw=net.get('sw01.00')
124 print "center sw", sw
125 sw.attach('tapa0')
126
Masayoshi Kobayashi7dd03eb2013-03-13 00:07:27 +0000127 for i in range (NR_NODES):
128 host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,i))
129
130 root = []
131 for i in range (NR_NODES):
132 root.append(net.get( 'root%d' % i ))
133
134 for i in range (NR_NODES):
135 host[i].intf('host%d-eth1' % i).setIP('1.1.%d.1/24' % i)
136 root[i].intf('root%d-eth0' % i).setIP('1.1.%d.2/24' % i)
137
138 stopsshd ()
139 startsshds ( host )
140
141 if opt=="cli":
142 CLI(net)
143 stopsshd()
144 net.stop()
145
146if __name__ == '__main__':
147 setLogLevel( 'info' )
148 if len(sys.argv) == 1:
149 sdnnet("cli")
150 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
151 sdnnet("nocli")
152 else:
153 print "%s [-n]" % sys.argv[0]