blob: cc40a5ac8571e7ed0e9cab6cea8f433f9b38f49e [file] [log] [blame]
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +00001#!/usr/bin/python
2NWID=__NWID__
3NR_NODES=__NRSW__
4Controllers=[{"ip":"127.0.0.1", "port":6633}]
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
Masayoshi Kobayashi0e780452014-01-23 15:43:34 -080024import argparse
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000025
26class MyController( Controller ):
27 def __init__( self, name, ip='127.0.0.1', 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 switch = []
58 host = []
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000059
60 for i in range (NR_NODES):
61 name_suffix = '%02d' % NWID + "." + '%02d' % (int(i)+1)
62 dpid_suffix = '%02x' % NWID + '%02x' % (int(i)+1)
63 dpid = '0000' + '0000' + '0000' + dpid_suffix
64 sw = self.addSwitch('sw'+name_suffix, dpid=dpid)
65 switch.append(sw)
66
67 for i in range (NR_NODES):
Pavlin Radoslavovfaae9272013-11-26 18:51:27 -080068 host.append(self.addHost( 'host%d.%d' % (NWID, int(i)+1) ))
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000069
70 for i in range (NR_NODES):
71 self.addLink(host[i], switch[i])
72
73 for i in range (1, NR_NODES):
74 self.addLink(switch[0], switch[i])
75
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000076def startsshd( host ):
77 "Start sshd on host"
78 info( '*** Starting sshd\n' )
79 name, intf, ip = host.name, host.defaultIntf(), host.IP()
80 banner = '/tmp/%s.banner' % name
81 host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) )
82 host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' )
83 info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' )
84
85def startsshds ( hosts ):
86 for h in hosts:
87 startsshd( h )
88
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000089def startiperf( host ):
Masayoshi Kobayashi05db3ce2013-04-06 00:50:37 +000090 host.cmd( '/usr/bin/iperf', '-s &' )
Masayoshi Kobayashia3639cc2013-04-02 16:04:19 +000091
92def startiperfs ( hosts ):
93 for h in hosts:
94 startiperf( h )
95
96def stopiperf( ):
97 quietRun( "pkill -9 iperf" )
98
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +000099def stopsshd( ):
100 "Stop *all* sshd processes with a custom banner"
101 info( '*** Shutting down stale sshd/Banner processes ',
102 quietRun( "pkill -9 -f Banner" ), '\n' )
103
Masayoshi Kobayashi0e780452014-01-23 15:43:34 -0800104def sdnnet(nocli, noarp):
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000105 topo = SDNTopo()
106 info( '*** Creating network\n' )
107 net = Mininet( topo=topo, controller=MyController, link=TCLink)
108 #net = Mininet( topo=topo, link=TCLink, build=False)
109 #controllers=[]
110 #for c in Controllers:
111 # rc = RemoteController('c%d' % Controllers.index(c), ip=c['ip'],port=c['port'])
112 # print "controller ip %s port %s" % (c['ip'], c['port'])
113 # controllers.append(rc)
114
115 #net.controllers=controllers
Masayoshi Kobayashicee9f352014-01-23 01:13:35 -0800116 #net.build()
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000117
118 host = []
119 for i in range (NR_NODES):
Pavlin Radoslavovfaae9272013-11-26 18:51:27 -0800120 host.append(net.get( 'host%d.%d' % (NWID, (int(i)+1)) ))
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000121
122 net.start()
123
124 sw=net.get('sw%02x.%02x' % (NWID,1))
125 print "center sw", sw
126 sw.attach('tap%02x_1' % NWID)
127
128 for i in range (NR_NODES):
129 host[i].defaultIntf().setIP('192.168.%d.%d/16' % (NWID,(int(i)+1)))
130 host[i].defaultIntf().setMAC('00:00:%02x:%02x:%02x:%02x' % (192,168,NWID,(int(i)+1)))
131
Masayoshi Kobayashi0e780452014-01-23 15:43:34 -0800132
133 if noarp == False:
134 for i in range (NR_NODES):
135 for n in range (2,9):
136 for h in range (25):
137 host[i].setARP('192.168.%d.%d' % (n, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(h)+1)))
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000138
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000139 stopsshd ()
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000140# stopiperf ()
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000141 startsshds ( host )
Masayoshi Kobayashid41701f2013-04-08 04:31:18 +0000142# startiperfs ( host )
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000143
Masayoshi Kobayashi0e780452014-01-23 15:43:34 -0800144 if nocli == False:
Masayoshi Kobayashif358ff52013-03-22 00:31:59 +0000145 CLI(net)
146 stopsshd()
147 net.stop()
148
149if __name__ == '__main__':
150 setLogLevel( 'info' )
Masayoshi Kobayashi0e780452014-01-23 15:43:34 -0800151 parser = argparse.ArgumentParser(description='mininet script')
152 parser.add_argument('-x', dest='noarp', action='store_true',
153 help='do not crete staric arp entries')
154 parser.add_argument('-n', dest='nocli', action='store_true',
155 help='do not run cli')
156 args = parser.parse_args()
157 sdnnet(args.nocli, args.noarp)