Naoki Shiota | 6e44b37 | 2013-10-14 14:33:58 -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 | |
| 22 | #import argparse |
| 23 | |
| 24 | class 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 | |
| 49 | class SDNTopo( Topo ): |
| 50 | "SDN Topology" |
| 51 | |
| 52 | def __init__( self, *args, **kwargs ): |
| 53 | Topo.__init__( self, *args, **kwargs ) |
| 54 | sw1 = self.addSwitch('sw1', dpid='0000000000000101') |
| 55 | sw2 = self.addSwitch('sw2', dpid='0000000000000102') |
| 56 | sw3 = self.addSwitch('sw3', dpid='0000000000000103') |
| 57 | sw4 = self.addSwitch('sw4', dpid='0000000000000104') |
| 58 | sw5 = self.addSwitch('sw5', dpid='0000000000000105') |
| 59 | sw6 = self.addSwitch('sw6', dpid='0000000000000106') |
| 60 | |
| 61 | host1 = self.addHost( 'host1' ) |
| 62 | host2 = self.addHost( 'host2' ) |
| 63 | host3 = self.addHost( 'host3' ) |
| 64 | host4 = self.addHost( 'host4' ) |
| 65 | host5 = self.addHost( 'host5' ) |
| 66 | host6 = self.addHost( 'host6' ) |
| 67 | |
| 68 | self.addLink( host1, sw1 ) |
| 69 | self.addLink( host2, sw2 ) |
| 70 | self.addLink( host3, sw3 ) |
| 71 | self.addLink( host4, sw4 ) |
| 72 | self.addLink( host5, sw5 ) |
| 73 | self.addLink( host6, sw6 ) |
| 74 | |
| 75 | self.addLink( sw1, sw2 ) |
| 76 | self.addLink( sw1, sw6 ) |
| 77 | self.addLink( sw2, sw3 ) |
| 78 | self.addLink( sw3, sw4 ) |
| 79 | self.addLink( sw3, sw6 ) |
| 80 | self.addLink( sw4, sw5 ) |
| 81 | self.addLink( sw5, sw6 ) |
| 82 | self.addLink( sw4, sw6 ) |
| 83 | |
| 84 | def startsshd( host ): |
| 85 | "Start sshd on host" |
| 86 | info( '*** Starting sshd\n' ) |
| 87 | name, intf, ip = host.name, host.defaultIntf(), host.IP() |
| 88 | banner = '/tmp/%s.banner' % name |
| 89 | host.cmd( 'echo "Welcome to %s at %s" > %s' % ( name, ip, banner ) ) |
| 90 | host.cmd( '/usr/sbin/sshd -o "Banner %s"' % banner, '-o "UseDNS no"' ) |
| 91 | info( '***', host.name, 'is running sshd on', intf, 'at', ip, '\n' ) |
| 92 | |
| 93 | def startsshds ( hosts ): |
| 94 | for h in hosts: |
| 95 | startsshd( h ) |
| 96 | |
| 97 | def stopsshd( ): |
| 98 | "Stop *all* sshd processes with a custom banner" |
| 99 | info( '*** Shutting down stale sshd/Banner processes ', |
| 100 | quietRun( "pkill -9 -f Banner" ), '\n' ) |
| 101 | |
| 102 | def sdnnet(opt): |
| 103 | # os.system('/home/ubuntu/openflow/controller/controller ptcp: &') |
| 104 | # os.system('/home/ubuntu/openflow/controller/controller ptcp:7000 &') |
| 105 | |
| 106 | topo = SDNTopo() |
| 107 | info( '*** Creating network\n' ) |
| 108 | # net = Mininet( topo=topo, controller=RemoteController ) |
| 109 | net = Mininet( topo=topo, controller=MyController, link=TCLink) |
| 110 | # dc = DebugController('c3', ip='127.0.0.1', port=7000) |
| 111 | # net.addController(dc) |
| 112 | # net.addController(controller=RemoteController) |
| 113 | |
| 114 | host1, host2, host3, host4, host5, host6 = net.get( 'host1', 'host2', 'host3', 'host4', 'host5', 'host6') |
| 115 | |
| 116 | ## Adding 2nd, 3rd and 4th interface to host1 connected to sw1 (for another BGP peering) |
| 117 | sw1 = net.get('sw1') |
| 118 | sw2 = net.get('sw2') |
| 119 | sw3 = net.get('sw3') |
| 120 | sw4 = net.get('sw4') |
| 121 | sw5 = net.get('sw5') |
| 122 | sw6 = net.get('sw6') |
| 123 | |
| 124 | net.start() |
| 125 | |
| 126 | sw2.attach('tap01_2') |
| 127 | sw3.attach('tap01_3') |
| 128 | sw4.attach('tap01_4') |
| 129 | sw4.attach('tap01_5') |
| 130 | sw5.attach('tap01_6') |
| 131 | sw6.attach('tap01_7') |
| 132 | sw1.attach('tap01_8') |
| 133 | |
| 134 | host1.defaultIntf().setIP('192.168.100.141/16') |
| 135 | host2.defaultIntf().setIP('192.168.100.142/16') |
| 136 | host3.defaultIntf().setIP('192.168.100.143/16') |
| 137 | host4.defaultIntf().setIP('192.168.100.144/16') |
| 138 | host5.defaultIntf().setIP('192.168.100.145/16') |
| 139 | host6.defaultIntf().setIP('192.168.100.146/16') |
| 140 | |
| 141 | hosts = [ host1, host2, host3, host4, host5, host6 ] |
| 142 | stopsshd () |
| 143 | startsshds ( hosts ) |
| 144 | |
| 145 | if opt=="cli": |
| 146 | CLI(net) |
| 147 | stopsshd() |
| 148 | net.stop() |
| 149 | |
| 150 | if __name__ == '__main__': |
| 151 | setLogLevel( 'info' ) |
| 152 | if len(sys.argv) == 1: |
| 153 | sdnnet("cli") |
| 154 | elif len(sys.argv) == 2 and sys.argv[1] == "-n": |
| 155 | sdnnet("nocli") |
| 156 | else: |
| 157 | print "%s [-n]" % sys.argv[0] |