Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
| 4 | from threading import Thread |
| 5 | |
| 6 | from mininet.net import Mininet |
| 7 | from mininet.log import setLogLevel |
| 8 | from mininet.node import RemoteController |
| 9 | from mininet.log import info, debug |
| 10 | from mininet.util import quietRun |
| 11 | from mininet.link import TCLink |
| 12 | from mininet.cli import CLI |
| 13 | |
| 14 | class ONOSMininet( Mininet ): |
| 15 | |
| 16 | @classmethod |
| 17 | def setup( cls ): |
| 18 | cls.useArping = True if quietRun( 'which arping' ) else False |
| 19 | |
| 20 | def __init__( self, controllers=[], gratuitousArp=True, build=True, *args, **kwargs ): |
| 21 | """Create Mininet object for ONOS. |
| 22 | controllers: List of controller IP addresses |
| 23 | gratuitousArp: Send an ARP from each host to aid controller's host discovery""" |
| 24 | # discarding provided controller (if any), |
| 25 | # using list of remote controller IPs instead |
| 26 | kwargs[ 'controller' ] = None |
| 27 | |
| 28 | # delay building for a second |
| 29 | kwargs[ 'build' ] = False |
| 30 | |
| 31 | Mininet.__init__(self, *args, **kwargs ) |
| 32 | |
| 33 | self.gratArp = gratuitousArp |
| 34 | self.useArping = ONOSMininet.useArping |
| 35 | |
| 36 | info ( '*** Adding controllers\n' ) |
| 37 | ctrl_count = 0 |
| 38 | for controllerIP in controllers: |
| 39 | self.addController( 'c%d' % ctrl_count, RemoteController, ip=controllerIP ) |
| 40 | info( ' c%d (%s)\n' % ( ctrl_count, controllerIP ) ) |
| 41 | ctrl_count = ctrl_count + 1 |
| 42 | |
| 43 | if self.topo and build: |
| 44 | self.build() |
| 45 | |
| 46 | def start( self ): |
| 47 | Mininet.start( self ) |
| 48 | if self.gratArp: |
| 49 | self.waitConnected() |
| 50 | info ( '*** Sending a gratuitious ARP from each host\n' ) |
| 51 | self.gratuitousArp() |
| 52 | |
| 53 | |
| 54 | def gratuitousArp( self ): |
| 55 | "Send an ARP from each host to aid controller's host discovery; fallback to ping if necessary" |
| 56 | if self.useArping: |
| 57 | for host in self.hosts: |
| 58 | info( '%s ' % host.name ) |
| 59 | debug( host.cmd( 'arping -U -c 1 ' + host.IP() ) ) |
| 60 | info ( '\n' ) |
| 61 | else: |
| 62 | info( '\nWARNING: arping is not found, using ping instead.\n' |
| 63 | 'For higher performance, install arping: sudo apt-get install iputils-arping\n\n' ) |
| 64 | |
| 65 | threads = [ self.threadPing(s, d) for (s, d) in zip( self.hosts, self.hosts[1:] + self.hosts[0:1] ) ] |
| 66 | for t in threads: |
| 67 | t.join() |
| 68 | info ( '\n' ) |
| 69 | |
| 70 | def threadPing( self, src, dst ): |
| 71 | "Ping from src to dst in a thread" |
| 72 | def p(): |
| 73 | src.cmd( 'ping -w 0.1 -W 0.1 -c1 ' + dst.IP() ) |
| 74 | t = Thread( target=p ) |
| 75 | info ( '%s ' % src.name ) |
| 76 | t.start() |
| 77 | return t |
| 78 | |
| 79 | def pingloop( self ): |
| 80 | "Loop forever pinging the full mesh of hosts" |
| 81 | setLogLevel( 'error' ) |
| 82 | try: |
| 83 | while True: |
| 84 | self.ping() |
| 85 | finally: |
| 86 | setLogLevel( 'info' ) |
| 87 | |
| 88 | # Initialize ONOSMininet the first time that the class is loaded |
| 89 | ONOSMininet.setup() |
| 90 | |
| 91 | def run( topo, controllers=None, link=TCLink, autoSetMacs=True ): |
| 92 | if not controllers and len( sys.argv ) > 1: |
| 93 | controllers = sys.argv[ 1: ] |
| 94 | else: |
| 95 | print 'Need to provide a topology and list of controllers' |
| 96 | exit( 1 ) |
| 97 | |
| 98 | setLogLevel( 'info' ) |
| 99 | |
| 100 | net = ONOSMininet( topo=topo, controllers=controllers, link=link, autoSetMacs=autoSetMacs ) |
| 101 | net.start() |
| 102 | CLI( net ) |
| 103 | net.stop() |