blob: e62ae77b2cc795cd8a2b1d9063f5f8b366fbbe05 [file] [log] [blame]
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +00001#!/usr/bin/python
2
3"""
4Start up a Simple topology
5"""
6from mininet.net import Mininet
7from mininet.node import Controller, RemoteController
8from mininet.log import setLogLevel, info, error, warn, debug
9from mininet.cli import CLI
10from mininet.topo import Topo
11from mininet.util import quietRun
12from mininet.moduledeps import pathCheck
13from mininet.link import Link, TCLink
14
15from sys import exit
16import os.path
17from subprocess import Popen, STDOUT, PIPE
18
19import sys
20
21
22#import argparse
23
24class MyController( Controller ):
25 def __init__( self, name, ip='127.0.0.1', port=6633, **kwargs):
26 """Init.
27 name: name to give controller
28 ip: the IP address where the remote controller is
29 listening
30 port: the port where the remote controller is listening"""
31 Controller.__init__( self, name, ip=ip, port=port, **kwargs )
32
33 def start( self ):
34 "Overridden to do nothing."
35 return
36
37 def stop( self ):
38 "Overridden to do nothing."
39 return
40
41 def checkListening( self ):
42 "Warn if remote controller is not accessible"
43 listening = self.cmd( "echo A | telnet -e A %s %d" %
44 ( self.ip, self.port ) )
45 if 'Unable' in listening:
46 warn( "Unable to contact the remote controller"
47 " at %s:%d\n" % ( self.ip, self.port ) )
48
49class SDNTopo( Topo ):
50 "SDN Topology"
51
52 def __init__( self, *args, **kwargs ):
53 Topo.__init__( self, *args, **kwargs )
54 sw1 = self.addSwitch('sw1', dpid='00:00:00:16:97:08:9a:46')
55 sw2 = self.addSwitch('sw2', dpid='00:00:00:00:ba:5e:ba:11')
56 sw3 = self.addSwitch('sw3', dpid='00:00:00:08:a2:08:f9:01')
57 sw4 = self.addSwitch('sw4', dpid='00:00:00:00:00:00:ba:12')
58 sw5 = self.addSwitch('sw5', dpid='00:00:00:00:ba:5e:ba:13')
59 sw6 = self.addSwitch('sw6', dpid='00:00:20:4e:7f:51:8a:35')
60
61 host1 = self.addHost( 'host1' )
62 host2 = self.addHost( 'host2' )
63 host3 = self.addHost( 'host3' )
64 host4 = self.addHost( 'host4' )
65 host5 = self.addHost( 'host5' )
66 host6 = self.addHost( 'host6' )
67
68 root1 = self.addHost( 'root1', inNamespace=False )
69 root2 = self.addHost( 'root2', inNamespace=False )
70 root3 = self.addHost( 'root3', inNamespace=False )
71 root4 = self.addHost( 'root4', inNamespace=False )
72 root5 = self.addHost( 'root5', inNamespace=False )
73 root6 = self.addHost( 'root6', inNamespace=False )
74
75
76 self.addLink( host1, sw1 )
77 self.addLink( host2, sw2 )
78 self.addLink( host3, sw3 )
79 self.addLink( host4, sw4 )
80 self.addLink( host5, sw5 )
81 self.addLink( host6, sw6 )
82
83
84 self.addLink( sw1, sw2 )
85 self.addLink( sw1, sw6 )
86 self.addLink( sw2, sw3 )
87 self.addLink( sw3, sw4 )
88 self.addLink( sw3, sw6 )
89 self.addLink( sw4, sw5 )
90 self.addLink( sw5, sw6 )
91
92 self.addLink( root1, host1 )
93 self.addLink( root2, host2 )
94 self.addLink( root3, host3 )
95 self.addLink( root4, host4 )
96 self.addLink( root5, host5 )
97 self.addLink( root6, host6 )
98
99def startsshd( host ):
100 "Start sshd on host"
101 info( '*** Starting sshd\n' )
102 name, intf, ip = host.name, host.defaultIntf(), host.IP()
103 banner = '/tmp/%s.banner' % name
104 host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) )
105 host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' )
106 info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' )
107
108def startsshds ( hosts ):
109 for h in hosts:
110 startsshd( h )
111
112def stopsshd( ):
113 "Stop *all* sshd processes with a custom banner"
114 info( '*** Shutting down stale sshd/Banner processes ',
115 quietRun( "pkill -9 -f Banner" ), '\n' )
116
117def sdnnet(opt):
118# os.system('/home/ubuntu/openflow/controller/controller ptcp: &')
119# os.system('/home/ubuntu/openflow/controller/controller ptcp:7000 &')
120
121 topo = SDNTopo()
122 info( '*** Creating network\n' )
123# net = Mininet( topo=topo, controller=RemoteController )
124 net = Mininet( topo=topo, controller=MyController, link=TCLink)
125# dc = DebugController('c3', ip='127.0.0.1', port=7000)
126# net.addController(dc)
127# net.addController(controller=RemoteController)
128
129 host1, host2, host3, host4, host5, host6 = net.get( 'host1', 'host2', 'host3', 'host4', 'host5', 'host6')
130
131 ## Adding 2nd, 3rd and 4th interface to host1 connected to sw1 (for another BGP peering)
132 sw1 = net.get('sw1')
133 sw2 = net.get('sw2')
134 sw3 = net.get('sw3')
135 sw4 = net.get('sw4')
136 sw5 = net.get('sw5')
137 sw6 = net.get('sw6')
138
139 net.start()
140
141 sw2.attach('tap01_2')
142 sw3.attach('tap01_3')
143 sw4.attach('tap01_4')
144 sw4.attach('tap01_5')
145 sw5.attach('tap01_6')
146 sw6.attach('tap01_7')
147 sw1.attach('tap01_8')
148
149 host1.defaultIntf().setIP('192.168.100.141/16')
150 host2.defaultIntf().setIP('192.168.100.142/16')
151 host3.defaultIntf().setIP('192.168.100.143/16')
152 host4.defaultIntf().setIP('192.168.100.144/16')
153 host5.defaultIntf().setIP('192.168.100.145/16')
154 host6.defaultIntf().setIP('192.168.100.146/16')
155
156 root1, root2, root3, root4, root5, root6 = net.get( 'root1', 'root2', 'root3', 'root4', 'root5', 'root6' )
157 host1.intf('host1-eth1').setIP('1.1.1.1/24')
158 root1.intf('root1-eth0').setIP('1.1.1.2/24')
159
160 host2.intf('host2-eth1') .setIP('1.1.2.1/24')
161 root2.intf('root2-eth0').setIP('1.1.2.2/24')
162
163 host3.intf('host3-eth1') .setIP('1.1.3.1/24')
164 root3.intf('root3-eth0').setIP('1.1.3.2/24')
165
166 host4.intf('host4-eth1') .setIP('1.1.4.1/24')
167 root4.intf('root4-eth0').setIP('1.1.4.2/24')
168
169 host5.intf('host5-eth1') .setIP('1.1.5.1/24')
170 root5.intf('root5-eth0').setIP('1.1.5.2/24')
171
172 host6.intf('host6-eth1') .setIP('1.1.6.1/24')
173 root6.intf('root6-eth0').setIP('1.1.6.2/24')
174
175 hosts = [ host1, host2, host3, host4, host5, host6 ]
176 stopsshd ()
177 startsshds ( hosts )
178
179 if opt=="cli":
180 CLI(net)
181 stopsshd()
182 net.stop()
183
184if __name__ == '__main__':
185 setLogLevel( 'info' )
186 if len(sys.argv) == 1:
187 sdnnet("cli")
188 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
189 sdnnet("nocli")
190 else:
191 print "%s [-n]" % sys.argv[0]