blob: ad74e4b2450cab97d85ed520e9ba312d4750db14 [file] [log] [blame]
HIGUCHI Yuta7cbc93e2013-06-25 13:59:56 -07001#!/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
55 sw5 = self.addSwitch('sw5', dpid='00000000ba5eba13')
56 sw2 = self.addSwitch('sw2', dpid='00000000ba5eba11')
57 sw3 = self.addSwitch('sw3', dpid='00000008a208f901')
58 sw4 = self.addSwitch('sw4', dpid='000000000000ba12')
59 sw6 = self.addSwitch('sw6', dpid='0000204e7f518a35')
60 sw1 = self.addSwitch('sw1', dpid='0000001697089a46')
61
62 host1 = self.addHost( 'host1' )
63 host2 = self.addHost( 'host2' )
64 host3 = self.addHost( 'host3' )
65 host4 = self.addHost( 'host4' )
66 host5 = self.addHost( 'host5' )
67 host6 = self.addHost( 'host6' )
68
69 self.addLink( host1, sw1 )
70 self.addLink( host2, sw2 )
71 self.addLink( host3, sw3 )
72 self.addLink( host4, sw4 )
73 self.addLink( host5, sw5 )
74 self.addLink( host6, sw6 )
75
76 self.addLink( sw1, sw2 )
77 self.addLink( sw1, sw6 )
78 self.addLink( sw2, sw3 )
79 self.addLink( sw3, sw4 )
80 self.addLink( sw3, sw6 )
81 self.addLink( sw4, sw5 )
82 self.addLink( sw5, sw6 )
83 self.addLink( sw4, sw6 )
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# os.system('/home/ubuntu/openflow/controller/controller ptcp: &')
105# os.system('/home/ubuntu/openflow/controller/controller ptcp:7000 &')
106
107 topo = SDNTopo()
108 info( '*** Creating network\n' )
109# net = Mininet( topo=topo, controller=RemoteController )
110 net = Mininet( topo=topo, controller=MyController, link=TCLink)
111# dc = DebugController('c3', ip='127.0.0.1', port=7000)
112# net.addController(dc)
113# net.addController(controller=RemoteController)
114
115 host1, host2, host3, host4, host5, host6 = net.get( 'host1', 'host2', 'host3', 'host4', 'host5', 'host6')
116
117 ## Adding 2nd, 3rd and 4th interface to host1 connected to sw1 (for another BGP peering)
118 sw1 = net.get('sw1')
119 sw2 = net.get('sw2')
120 sw3 = net.get('sw3')
121 sw4 = net.get('sw4')
122 sw5 = net.get('sw5')
123 sw6 = net.get('sw6')
124
125 net.start()
126
127 sw2.attach('tap01_2')
128 sw3.attach('tap01_3')
129 sw4.attach('tap01_4')
130 sw4.attach('tap01_5')
131 sw5.attach('tap01_6')
132 sw6.attach('tap01_7')
133 sw1.attach('tap01_8')
134
135 host1.defaultIntf().setIP('192.168.100.141/16')
136 host2.defaultIntf().setIP('192.168.100.142/16')
137 host3.defaultIntf().setIP('192.168.100.143/16')
138 host4.defaultIntf().setIP('192.168.100.144/16')
139 host5.defaultIntf().setIP('192.168.100.145/16')
140 host6.defaultIntf().setIP('192.168.100.146/16')
141
142 hosts = [ host1, host2, host3, host4, host5, host6 ]
143 stopsshd ()
144 startsshds ( hosts )
145
146 if opt=="cli":
147 CLI(net)
148 stopsshd()
149 net.stop()
150
151if __name__ == '__main__':
152 setLogLevel( 'info' )
153 if len(sys.argv) == 1:
154 sdnnet("cli")
155 elif len(sys.argv) == 2 and sys.argv[1] == "-n":
156 sdnnet("nocli")
157 else:
158 print "%s [-n]" % sys.argv[0]