blob: 73a3d07a5f0bcc60e05cdf890360eed77aecc171 [file] [log] [blame]
Nick Karanatsiosed645df2014-02-20 23:22:29 -08001#!/usr/bin/python
2"""Custom topology example
3
4Two directly connected switches plus a host for each switch:
5
6 host --- switch --- switch --- host
7
8Adding the 'topos' dict with a key/value pair to generate our newly defined
9topology enables one to pass in '--topo=mytopo' from the command line.
10"""
11
12import sys, getopt
13from mininet.net import Mininet
14from mininet.node import Controller, RemoteController
15from mininet.log import setLogLevel, info, error, warn, debug
16from mininet.topo import Topo
17from mininet.link import Link, TCLink
18from mininet.util import dumpNodeConnections
19
20class MyController( Controller ):
21 def __init__( self, name, ip='127.0.0.1', port=6633, **kwargs):
22 """Init.
23 name: name to give controller
24 ip: the IP address where the remote controller is
25 listening
26 port: the port where the remote controller is listening"""
27 Controller.__init__( self, name, ip=ip, port=port, **kwargs )
28
29 def start( self ):
30 "Overridden to do nothing."
31 return
32
33 def stop( self ):
34 "Overridden to do nothing."
35 return
36
37 def checkListening( self ):
38 "Warn if remote controller is not accessible"
39 listening = self.cmd( "echo A | telnet -e A %s %d" %
40 ( self.ip, self.port ) )
41 if 'Unable' in listening:
42 warn( "Unable to contact the remote controller"
43 " at %s:%d\n" % ( self.ip, self.port ) )
44
45
46class MyTopo( Topo ):
47 "Simple topology example."
48
49 def __init__( self, max_switches ):
50 "Create custom topo."
51
52 # Initialize topology
53 Topo.__init__( self )
54
55 installed_switches = {}
56 # add switches first
57 for sw in range( max_switches ):
58 sw_str = "sw" + `sw + 1`
59 msw = self.addSwitch( sw_str )
60 installed_switches[sw_str] = msw
61
62 # create links between switches
63 for sw in range( max_switches ):
64 next_sw = sw + 1
65 for link in range( next_sw, max_switches ):
66 link_from = "sw" + `next_sw`
67 link_to = "sw" + `link + 1`
68 print "link_from ", link_from, " link to ", link_to
69 self.addLink( link_from, link_to )
70
71 # finally add links to hosts
72 for sw in range( max_switches ):
73 sw_str = "sw" + `sw + 1`
74 host_str = "h" + `sw + 1`
75 mhost = self.addHost( host_str )
76 msw = installed_switches[sw_str]
77 self.addLink( msw, mhost)
78
79def main(argv):
80 max_switches = ""
81 try:
82 opts,args = getopt.getopt(argv, "hs:", ["help", "switches="])
83 except getopt.GetoptError:
84 print "Usage mesh_topology.py [options]"
85 print "-s, --switches number of switches to set"
86 sys.exit(2)
87 for opt, arg in opts:
88 if opt == '-h':
89 print "Usage mesh_topology.py [options]"
90 print "-s, --switches number of switches to set"
91 sys.exit()
92 elif opt in ("-s", "--switches"):
93 max_switches = arg
94
95 switches = 4
96 if max_switches != "":
97 switches = int(max_switches)
Nick Karanatsiosed645df2014-02-20 23:22:29 -080098 net = Mininet(topo=MyTopo(switches), controller=MyController, link=TCLink)
99 print dumpNodeConnections(net.switches)
100
101 for sw in range(switches):
102 next_sw = sw + 1
103 mhost = "h" + `next_sw`
104 host = net.get( mhost )
105 if next_sw > 255:
106 divisor = next_sw / 256
107 remainder = next_sw % 256
108 host.setMAC('00:00:%02x:%02x:%02x:%02x' % (192, 168, divisor, remainder))
109 print "Host", host.name, "has IP address", host.IP(), "and MAC address", host.MAC()
110 else:
111 host.setMAC('00:00:%02x:%02x:%02x:%02x' % (192, 168, 0, next_sw))
112 print "Host", host.name, "has IP address", host.IP(), "and MAC address", host.MAC()
113
114 net.start()
115
116if __name__ == "__main__":
117 main(sys.argv[1:])