Masayoshi Kobayashi | 80493be | 2013-08-01 17:33:44 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | Start up a Simple topology |
| 5 | """ |
| 6 | from mininet.net import Mininet |
| 7 | from mininet.node import Controller, RemoteController |
| 8 | from mininet.log import setLogLevel, info, error, warn, debug |
| 9 | from mininet.cli import CLI |
| 10 | from mininet.topo import Topo |
| 11 | from mininet.util import quietRun |
| 12 | from mininet.moduledeps import pathCheck |
| 13 | from mininet.link import Link, TCLink |
| 14 | |
| 15 | from sys import exit |
| 16 | import os.path |
| 17 | from subprocess import Popen, STDOUT, PIPE |
| 18 | |
| 19 | import sys |
| 20 | |
| 21 | NR_NODES=10 #; Switches per rigonal networks |
| 22 | |
| 23 | class MyController( Controller ): |
| 24 | def __init__( self, name, ip='127.0.0.1', port=6633, **kwargs): |
| 25 | """Init. |
| 26 | name: name to give controller |
| 27 | ip: the IP address where the remote controller is |
| 28 | listening |
| 29 | port: the port where the remote controller is listening""" |
| 30 | Controller.__init__( self, name, ip=ip, port=port, **kwargs ) |
| 31 | |
| 32 | def start( self ): |
| 33 | "Overridden to do nothing." |
| 34 | return |
| 35 | |
| 36 | def stop( self ): |
| 37 | "Overridden to do nothing." |
| 38 | return |
| 39 | |
| 40 | def checkListening( self ): |
| 41 | "Warn if remote controller is not accessible" |
| 42 | listening = self.cmd( "echo A | telnet -e A %s %d" % |
| 43 | ( self.ip, self.port ) ) |
| 44 | if 'Unable' in listening: |
| 45 | warn( "Unable to contact the remote controller" |
| 46 | " at %s:%d\n" % ( self.ip, self.port ) ) |
| 47 | |
| 48 | class SDNTopo( Topo ): |
| 49 | "SDN Topology" |
| 50 | |
| 51 | def __init__( self, *args, **kwargs ): |
| 52 | Topo.__init__( self, *args, **kwargs ) |
| 53 | sw1 = self.addSwitch('sw1', dpid='0000000000000101') |
| 54 | sw2 = self.addSwitch('sw2', dpid='0000000000000102') |
| 55 | sw3 = self.addSwitch('sw3', dpid='0000000000000103') |
| 56 | sw4 = self.addSwitch('sw4', dpid='0000000000000104') |
| 57 | sw5 = self.addSwitch('sw5', dpid='0000000000000105') |
| 58 | sw6 = self.addSwitch('sw6', dpid='0000000000000106') |
| 59 | |
| 60 | self.addLink( sw1, sw2 ) |
| 61 | self.addLink( sw1, sw6 ) |
| 62 | self.addLink( sw2, sw3 ) |
| 63 | self.addLink( sw3, sw4 ) |
| 64 | self.addLink( sw3, sw6 ) |
| 65 | self.addLink( sw4, sw5 ) |
| 66 | self.addLink( sw5, sw6 ) |
| 67 | self.addLink( sw4, sw6 ) |
| 68 | |
| 69 | switches=[] |
| 70 | for n in range(2, 9): |
| 71 | NWID=n |
| 72 | switch = [] |
| 73 | host = [] |
| 74 | for i in range (NR_NODES): |
| 75 | name_suffix = '%02d' % NWID + "." + '%02d' % (int(i)+1) |
| 76 | dpid_suffix = '%02x' % NWID + '%02x' % (int(i)+1) |
| 77 | dpid = '0000' + '0000' + '0000' + dpid_suffix |
| 78 | sw = self.addSwitch('sw'+name_suffix, dpid=dpid) |
| 79 | switch.append(sw) |
| 80 | |
| 81 | for i in range (NR_NODES): |
| 82 | host.append(self.addHost( 'host%d.%d' % (n,int(i)+1) )) |
| 83 | |
| 84 | for i in range (NR_NODES): |
| 85 | self.addLink(host[i], switch[i]) |
| 86 | |
| 87 | for i in range (1, NR_NODES): |
| 88 | self.addLink(switch[0], switch[i]) |
| 89 | |
| 90 | switches.append(switch) |
| 91 | |
| 92 | self.addLink(switches[8-2][0],sw1) |
| 93 | self.addLink(switches[2-2][0],sw2) |
| 94 | self.addLink(switches[3-2][0],sw3) |
| 95 | self.addLink(switches[4-2][0],sw4) |
| 96 | self.addLink(switches[5-2][0],sw4) |
| 97 | self.addLink(switches[6-2][0],sw5) |
| 98 | self.addLink(switches[7-2][0],sw6) |
| 99 | |
| 100 | |
| 101 | def startsshd( host ): |
| 102 | "Start sshd on host" |
| 103 | info( '*** Starting sshd\n' ) |
| 104 | name, intf, ip = host.name, host.defaultIntf(), host.IP() |
| 105 | banner = '/tmp/%s.banner' % name |
| 106 | host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) ) |
| 107 | host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' ) |
| 108 | info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' ) |
| 109 | |
| 110 | def startsshds ( hosts ): |
| 111 | for h in hosts: |
| 112 | startsshd( h ) |
| 113 | |
| 114 | def stopsshd( ): |
| 115 | "Stop *all* sshd processes with a custom banner" |
| 116 | info( '*** Shutting down stale sshd/Banner processes ', |
| 117 | quietRun( "pkill -9 -f Banner" ), '\n' ) |
| 118 | |
| 119 | def sdnnet(opt): |
| 120 | # os.system('/home/ubuntu/openflow/controller/controller ptcp: &') |
| 121 | # os.system('/home/ubuntu/openflow/controller/controller ptcp:7000 &') |
| 122 | |
| 123 | topo = SDNTopo() |
| 124 | info( '*** Creating network\n' ) |
| 125 | # net = Mininet( topo=topo, controller=RemoteController ) |
| 126 | net = Mininet( topo=topo, controller=MyController, link=TCLink) |
| 127 | # dc = DebugController('c3', ip='127.0.0.1', port=7000) |
| 128 | # net.addController(dc) |
| 129 | # net.addController(controller=RemoteController) |
| 130 | # host1, host2, host3, host4, host5, host6 = net.get( 'host1', 'host2', 'host3', 'host4', 'host5', 'host6') |
| 131 | |
| 132 | ## Adding 2nd, 3rd and 4th interface to host1 connected to sw1 (for another BGP peering) |
| 133 | sw1 = net.get('sw1') |
| 134 | sw2 = net.get('sw2') |
| 135 | sw3 = net.get('sw3') |
| 136 | sw4 = net.get('sw4') |
| 137 | sw5 = net.get('sw5') |
| 138 | sw6 = net.get('sw6') |
| 139 | |
| 140 | hosts = [] |
| 141 | for n in range(2, 9): |
| 142 | host = [] |
| 143 | for i in range (NR_NODES): |
| 144 | host.append(net.get( 'host%d.%d' % (n, int(i)+1) )) |
| 145 | hosts.append(host) |
| 146 | |
| 147 | net.start() |
| 148 | |
| 149 | for n in range(2, 9): |
| 150 | for i in range (NR_NODES): |
| 151 | hosts[n-2][i].defaultIntf().setIP('192.168.%d.%d/16' % (n,(int(i)+1))) |
| 152 | hosts[n-2][i].defaultIntf().setMAC('00:00:%02x:%02x:%02x:%02x' % (192,168,n,(int(i)+1))) |
| 153 | |
| 154 | for i in range (NR_NODES): |
| 155 | for k in range (2,9): |
| 156 | for h in range (NR_NODES): |
| 157 | hosts[n-2][i].setARP('192.168.%d.%d' % (k, (int(h)+1)), '00:00:%02x:%02x:%02x:%02x' % (192,168,k,(int(h)+1))) |
| 158 | |
| 159 | stopsshd () |
| 160 | for n in range(2, 9): |
| 161 | startsshds ( hosts[n-2] ) |
| 162 | |
| 163 | if opt=="cli": |
| 164 | CLI(net) |
| 165 | stopsshd() |
| 166 | net.stop() |
| 167 | |
| 168 | if __name__ == '__main__': |
| 169 | setLogLevel( 'info' ) |
| 170 | if len(sys.argv) == 1: |
| 171 | sdnnet("cli") |
| 172 | elif len(sys.argv) == 2 and sys.argv[1] == "-n": |
| 173 | sdnnet("nocli") |
| 174 | else: |
| 175 | print "%s [-n]" % sys.argv[0] |