blob: c7e4178407a0c67daa21d400259ac01ca5db0857 [file] [log] [blame]
Masayoshi Kobayashicbd5e0a2013-02-25 19:00:19 +00001#!/usr/bin/python
2
3NWID="a"
4CONTROLLER_IP='127.0.0.1'
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=CONTROLLER_IP, 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 dist_sw = []
58 aggr_sw = []
59 core_sw = []
60 host = []
61 root = []
62
63 for i in range (4):
64 name_suffix = '%s' % NWID + '0d' + '%02d' % i
65 dpid = '0000' + '0000' + '000' + name_suffix
66 sw = self.addSwitch('sw'+name_suffix, dpid=dpid)
67 dist_sw.append(sw)
68
69 for i in range (4):
70 name_suffix = '%s' % NWID + '0a' + '%02d' % i
71 dpid = '0000' + '0000' + '000' + name_suffix
72 sw = self.addSwitch('sw'+name_suffix, dpid=dpid)
73 aggr_sw.append(sw)
74
75 for i in range (2):
76 name_suffix = '%s' % NWID + '0c' + '%02d' % i
77 dpid = '0000' + '0000' + '000' + name_suffix
78 sw = self.addSwitch('sw' + name_suffix, dpid=dpid)
79 core_sw.append(sw)
80
81 for i in range (8):
82 host.append(self.addHost( 'host%d' % i ))
83
84 for i in range (8):
85 root.append(self.addHost( 'root%d' % i, inNamespace=False ))
86
87 for i in range (8):
88 self.addLink(host[i], dist_sw[int(i/2)])
89
90 for i in range (4):
91 for j in range (int(i/2) * 2, (int(i/2) * 2 + 2)):
92 print 'add link btween dist_sw[%d] -> aggr_sw[%d]' % (i, j)
93 self.addLink(dist_sw[i], aggr_sw[j])
94
95 for i in range (4):
96 for j in range (int(i/4) * 4, int(i/4) * 4 + 2):
97 print 'add link btween aggr_sw[%d] -> core_sw[%d]' % (i, j)
98 self.addLink(aggr_sw[i], core_sw[j])
99
100 for i in range (8):
101 self.addLink(root[i], host[i])
102
103def startsshd( host ):
104 "Start sshd on host"
105 info( '*** Starting sshd\n' )
106 name, intf, ip = host.name, host.defaultIntf(), host.IP()
107 banner = '/tmp/%s.banner' % name
108 host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) )
109 host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' )
110 info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' )
111
112def startsshds ( hosts ):
113 for h in hosts:
114 startsshd( h )
115
116def stopsshd( ):
117 "Stop *all* sshd processes with a custom banner"
118 info( '*** Shutting down stale sshd/Banner processes ',
119 quietRun( "pkill -9 -f Banner" ), '\n' )
120
121def sdnnet(opt):
122 topo = SDNTopo()
123 info( '*** Creating network\n' )
124 net = Mininet( topo=topo, controller=MyController, link=TCLink)
125
126 host = []
127 for i in range (8):
128 host.append(net.get( 'host%d' % i ))
129
130 net.start()
131
132 core_sw = []
133 for i in range (2):
134 name_suffix = '%s' % NWID + '0c' + '%02d' % i
135 net.get('sw' + name_suffix).attach('tap%s0' % NWID)
136
137 for i in range (8):
138 host[i].defaultIntf().setIP('192.168.10.10%d/24' % i)
139
140 root = []
141 for i in range (8):
142 root.append(net.get( 'root%d' % i ))
143
144 for i in range (8):
145 host[i].intf('host%d-eth1' % i).setIP('1.1.%d.1/24' % i)
146 root[i].intf('root%d-eth0' % i).setIP('1.1.%d.2/24' % i)
147
148 stopsshd ()
149 startsshds ( host )
150
151 if opt=="cli":
152 CLI(net)
153 stopsshd()
154 net.stop()
155
156if __name__ == '__main__':
157 setLogLevel( 'info' )
158 if len(sys.argv) == 1:
159 sdnnet("cli")
160 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
161 sdnnet("nocli")
162 else:
163 print "%s [-n]" % sys.argv[0]