Ubuntu | f6ce96c | 2013-02-07 01:45:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | NWID="a" |
| 4 | |
| 5 | """ |
| 6 | Start up a Simple topology |
| 7 | """ |
| 8 | from mininet.net import Mininet |
| 9 | from mininet.node import Controller, RemoteController |
| 10 | from mininet.log import setLogLevel, info, error, warn, debug |
| 11 | from mininet.cli import CLI |
| 12 | from mininet.topo import Topo |
| 13 | from mininet.util import quietRun |
| 14 | from mininet.moduledeps import pathCheck |
| 15 | from mininet.link import Link, TCLink |
| 16 | |
| 17 | from sys import exit |
| 18 | import os.path |
| 19 | from subprocess import Popen, STDOUT, PIPE |
| 20 | |
| 21 | import sys |
| 22 | |
| 23 | |
| 24 | #import argparse |
| 25 | |
| 26 | class MyController( Controller ): |
| 27 | def __init__( self, name, ip='10.0.1.217', 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 | |
| 51 | class SDNTopo( Topo ): |
| 52 | "SDN Topology" |
| 53 | |
| 54 | def __init__( self, *args, **kwargs ): |
| 55 | Topo.__init__( self, *args, **kwargs ) |
| 56 | sw1 = self.addSwitch('sw%s1' % NWID, dpid='00000000000000%s1' % NWID) |
| 57 | sw2 = self.addSwitch('sw%s2' % NWID, dpid='00000000000000%s2' % NWID) |
| 58 | sw3 = self.addSwitch('sw%s3' % NWID, dpid='00000000000000%s3' % NWID) |
| 59 | sw4 = self.addSwitch('sw%s4' % NWID, dpid='00000000000000%s4' % NWID) |
| 60 | sw5 = self.addSwitch('sw%s5' % NWID, dpid='00000000000000%s5' % NWID) |
| 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 | |
| 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 | |
| 74 | |
| 75 | self.addLink( host1, sw1 ) |
| 76 | self.addLink( host2, sw2 ) |
| 77 | self.addLink( host3, sw3 ) |
| 78 | self.addLink( host4, sw4 ) |
| 79 | self.addLink( host5, sw5 ) |
| 80 | |
| 81 | |
| 82 | self.addLink( sw1, sw3 ) |
| 83 | self.addLink( sw2, sw3 ) |
| 84 | self.addLink( sw4, sw3 ) |
| 85 | self.addLink( sw5, sw3 ) |
| 86 | |
| 87 | self.addLink( root1, host1 ) |
| 88 | self.addLink( root2, host2 ) |
| 89 | self.addLink( root3, host3 ) |
| 90 | self.addLink( root4, host4 ) |
| 91 | self.addLink( root5, host5 ) |
| 92 | |
| 93 | # self.addLink( sw1, sw2 ) |
| 94 | |
| 95 | def startsshd( host ): |
| 96 | "Start sshd on host" |
| 97 | info( '*** Starting sshd\n' ) |
| 98 | name, intf, ip = host.name, host.defaultIntf(), host.IP() |
| 99 | banner = '/tmp/%s.banner' % name |
| 100 | host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) ) |
| 101 | host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' ) |
| 102 | info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' ) |
| 103 | |
| 104 | def startsshds ( hosts ): |
| 105 | for h in hosts: |
| 106 | startsshd( h ) |
| 107 | |
| 108 | def stopsshd( ): |
| 109 | "Stop *all* sshd processes with a custom banner" |
| 110 | info( '*** Shutting down stale sshd/Banner processes ', |
| 111 | quietRun( "pkill -9 -f Banner" ), '\n' ) |
| 112 | |
| 113 | def sdnnet(opt): |
| 114 | # os.system('/home/ubuntu/openflow/controller/controller ptcp: &') |
| 115 | # os.system('/home/ubuntu/openflow/controller/controller ptcp:7000 &') |
| 116 | |
| 117 | topo = SDNTopo() |
| 118 | info( '*** Creating network\n' ) |
| 119 | # net = Mininet( topo=topo, controller=RemoteController ) |
| 120 | net = Mininet( topo=topo, controller=MyController, link=TCLink) |
| 121 | # dc = DebugController('c3', ip='127.0.0.1', port=7000) |
| 122 | # net.addController(dc) |
| 123 | # net.addController(controller=RemoteController) |
| 124 | |
| 125 | host1, host2, host3, host4, host5 = net.get( 'host1', 'host2', 'host3', 'host4', 'host5') |
| 126 | |
| 127 | ## Adding 2nd, 3rd and 4th interface to host1 connected to sw1 (for another BGP peering) |
| 128 | sw1 = net.get('sw%s1' % NWID) |
| 129 | sw2 = net.get('sw%s2' % NWID) |
| 130 | sw3 = net.get('sw%s3' % NWID) |
| 131 | sw4 = net.get('sw%s4' % NWID) |
| 132 | sw5 = net.get('sw%s5' % NWID) |
| 133 | |
| 134 | net.start() |
| 135 | |
| 136 | sw5.attach('tap%s0' % NWID) |
| 137 | |
| 138 | host1.defaultIntf().setIP('192.168.10.111/24') |
| 139 | host2.defaultIntf().setIP('192.168.10.112/24') |
| 140 | host3.defaultIntf().setIP('192.168.10.113/24') |
| 141 | host4.defaultIntf().setIP('192.168.10.114/24') |
| 142 | host5.defaultIntf().setIP('192.168.10.115/24') |
| 143 | |
| 144 | root1, root2, root3, root4, root5 = net.get( 'root1', 'root2', 'root3', 'root4', 'root5' ) |
| 145 | host1.intf('host1-eth1').setIP('1.1.1.1/24') |
| 146 | root1.intf('root1-eth0').setIP('1.1.1.2/24') |
| 147 | |
| 148 | host2.intf('host2-eth1') .setIP('1.1.2.1/24') |
| 149 | root2.intf('root2-eth0').setIP('1.1.2.2/24') |
| 150 | |
| 151 | host3.intf('host3-eth1') .setIP('1.1.3.1/24') |
| 152 | root3.intf('root3-eth0').setIP('1.1.3.2/24') |
| 153 | |
| 154 | host4.intf('host4-eth1') .setIP('1.1.4.1/24') |
| 155 | root4.intf('root4-eth0').setIP('1.1.4.2/24') |
| 156 | |
| 157 | host5.intf('host5-eth1') .setIP('1.1.5.1/24') |
| 158 | root5.intf('root5-eth0').setIP('1.1.5.2/24') |
| 159 | |
| 160 | hosts = [ host1, host2, host3, host4, host5 ] |
| 161 | stopsshd () |
| 162 | startsshds ( hosts ) |
| 163 | |
| 164 | if opt=="cli": |
| 165 | CLI(net) |
| 166 | stopsshd() |
| 167 | net.stop() |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | setLogLevel( 'info' ) |
| 171 | if len(sys.argv) == 1: |
| 172 | sdnnet("cli") |
| 173 | elif len(sys.argv) == 2 and sys.argv[1] == "-n": |
| 174 | sdnnet("nocli") |
| 175 | else: |
| 176 | print "%s [-n]" % sys.argv[0] |