Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 4 | import itertools |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 5 | import signal |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 6 | from time import sleep |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 7 | from threading import Thread |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 8 | |
| 9 | from mininet.net import Mininet |
| 10 | from mininet.log import setLogLevel |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 11 | from mininet.node import RemoteController, Node |
| 12 | from mininet.log import info, debug, output, error |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 13 | from mininet.link import TCLink |
| 14 | from mininet.cli import CLI |
| 15 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 16 | # This is the program that each host will call |
| 17 | import gratuitousArp |
| 18 | ARP_PATH = gratuitousArp.__file__.replace('.pyc', '.py') |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 19 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 20 | class ONOSMininet( Mininet ): |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 21 | |
| 22 | def __init__( self, controllers=[], gratuitousArp=True, build=True, *args, **kwargs ): |
| 23 | """Create Mininet object for ONOS. |
| 24 | controllers: List of controller IP addresses |
| 25 | gratuitousArp: Send an ARP from each host to aid controller's host discovery""" |
| 26 | # discarding provided controller (if any), |
| 27 | # using list of remote controller IPs instead |
| 28 | kwargs[ 'controller' ] = None |
| 29 | |
| 30 | # delay building for a second |
| 31 | kwargs[ 'build' ] = False |
| 32 | |
| 33 | Mininet.__init__(self, *args, **kwargs ) |
| 34 | |
| 35 | self.gratArp = gratuitousArp |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 36 | |
| 37 | info ( '*** Adding controllers\n' ) |
| 38 | ctrl_count = 0 |
| 39 | for controllerIP in controllers: |
| 40 | self.addController( 'c%d' % ctrl_count, RemoteController, ip=controllerIP ) |
| 41 | info( ' c%d (%s)\n' % ( ctrl_count, controllerIP ) ) |
| 42 | ctrl_count = ctrl_count + 1 |
| 43 | |
| 44 | if self.topo and build: |
| 45 | self.build() |
| 46 | |
| 47 | def start( self ): |
| 48 | Mininet.start( self ) |
| 49 | if self.gratArp: |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 50 | self.waitConnected( timeout=5 ) |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 51 | info ( '*** Sending a gratuitious ARP from each host\n' ) |
| 52 | self.gratuitousArp() |
| 53 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 54 | def verifyHosts( self, hosts ): |
| 55 | for i in range( len( hosts ) ): |
| 56 | if isinstance( hosts[i], str): |
| 57 | if hosts[i] in self: |
| 58 | hosts[i] = self[ hosts[i] ] |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 59 | else: |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 60 | info( '*** ERROR: %s is not a host\n' % hosts[i] ) |
| 61 | del hosts[i] |
| 62 | elif not isinstance( hosts[i], Node): |
| 63 | del hosts[i] |
| 64 | |
| 65 | def gratuitousArp( self, hosts=[] ): |
| 66 | "Send an ARP from each host to aid controller's host discovery; fallback to ping if necessary" |
| 67 | if not hosts: |
| 68 | hosts = self.hosts |
| 69 | self.verifyHosts( hosts ) |
| 70 | |
| 71 | for host in hosts: |
| 72 | info( '%s ' % host.name ) |
| 73 | info( host.cmd( ARP_PATH ) ) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 74 | info ( '\n' ) |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 75 | |
| 76 | def pingloop( self ): |
| 77 | "Loop forever pinging the full mesh of hosts" |
| 78 | setLogLevel( 'error' ) |
| 79 | try: |
| 80 | while True: |
| 81 | self.ping() |
| 82 | finally: |
| 83 | setLogLevel( 'info' ) |
| 84 | |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 85 | def bgIperf( self, hosts=[], seconds=10 ): |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 86 | self.verifyHosts( hosts ) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 87 | servers = [ host.popen("iperf -s") for host in hosts ] |
| 88 | |
| 89 | clients = [] |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 90 | for s, d in itertools.combinations(hosts, 2): |
| 91 | info ( '%s <--> %s\n' % ( s.name, d.name )) |
| 92 | cmd = 'iperf -c %s -t %s -y csv' % (d.IP(), seconds) |
| 93 | p = s.popen(cmd) |
| 94 | p.s = s.name |
| 95 | p.d = d.name |
| 96 | clients.append(p) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 97 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 98 | def handler (_signum, _frame): |
| 99 | raise BackgroundException() |
| 100 | oldSignal = signal.getsignal(signal.SIGTSTP) |
| 101 | signal.signal(signal.SIGTSTP, handler) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 102 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 103 | def finish( verbose=True ): |
| 104 | for c in clients: |
| 105 | out, err = c.communicate() |
| 106 | if verbose: |
| 107 | if err: |
| 108 | info( err ) |
| 109 | else: |
| 110 | bw = out.split( ',' )[8] |
| 111 | info( '%s <--> %s: %s\n' % ( c.s, c.d, formatBw(bw) ) ) |
| 112 | for s in servers: |
| 113 | s.terminate() |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 114 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 115 | try: |
| 116 | info ( 'Press ^Z to continue in background or ^C to abort\n') |
| 117 | progress( seconds ) |
| 118 | finish() |
| 119 | except KeyboardInterrupt: |
| 120 | for c in clients: |
| 121 | c.terminate() |
| 122 | for s in servers: |
| 123 | s.terminate() |
| 124 | except BackgroundException: |
| 125 | info( '\n*** Continuing in background...\n' ) |
| 126 | t = Thread( target=finish, args=[ False ] ) |
| 127 | t.start() |
| 128 | finally: |
| 129 | #Disable custom background signal |
| 130 | signal.signal(signal.SIGTSTP, oldSignal) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 131 | |
| 132 | def progress(t): |
| 133 | while t > 0: |
| 134 | sys.stdout.write( '.' ) |
| 135 | t -= 1 |
| 136 | sys.stdout.flush() |
| 137 | sleep(1) |
| 138 | print |
| 139 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 140 | def formatBw( bw ): |
| 141 | bw = float(bw) |
| 142 | if bw > 1000: |
| 143 | bw /= 1000 |
| 144 | if bw > 1000: |
| 145 | bw /= 1000 |
| 146 | if bw > 1000: |
| 147 | bw /= 1000 |
| 148 | return '%.2f Gbps' % bw |
| 149 | return '%.2f Mbps' % bw |
| 150 | return '%.2f Kbps' % bw |
| 151 | return '%.2f bps' % bw |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 152 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 153 | class BackgroundException( Exception ): |
| 154 | pass |
| 155 | |
| 156 | def do_bgIperf( self, line ): |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 157 | args = line.split() |
| 158 | if not args: |
| 159 | output( 'Provide a list of hosts.\n' ) |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 160 | |
| 161 | #Try to parse the '-t' argument as the number of seconds |
| 162 | seconds = 10 |
| 163 | for i, arg in enumerate(args): |
| 164 | if arg == '-t': |
| 165 | if i + 1 < len(args): |
| 166 | try: |
| 167 | seconds = int(args[i + 1]) |
| 168 | except ValueError: |
| 169 | error( 'Could not parse number of seconds: %s', args[i+1] ) |
| 170 | del(args[i+1]) |
| 171 | del args[i] |
| 172 | |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 173 | hosts = [] |
| 174 | err = False |
| 175 | for arg in args: |
| 176 | if arg not in self.mn: |
| 177 | err = True |
| 178 | error( "node '%s' not in network\n" % arg ) |
| 179 | else: |
| 180 | hosts.append( self.mn[ arg ] ) |
| 181 | if "bgIperf" in dir(self.mn) and not err: |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 182 | self.mn.bgIperf( hosts, seconds=seconds ) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 183 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 184 | def do_gratuitousArp( self, line ): |
| 185 | args = line.split() |
| 186 | if "gratuitousArp" in dir( self.mn ): |
| 187 | self.mn.gratuitousArp( args ) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 188 | else: |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 189 | output( 'Gratuitous ARP is not supported.\n' ) |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 190 | |
Brian O'Connor | e9b4dd7 | 2016-03-05 01:07:18 -0800 | [diff] [blame] | 191 | CLI.do_bgIperf = do_bgIperf |
Brian O'Connor | d6c73fb | 2016-03-04 15:24:52 -0800 | [diff] [blame] | 192 | CLI.do_gratuitousArp = do_gratuitousArp |
| 193 | |
Thomas Vachuska | 9ee4979 | 2016-03-01 16:51:14 -0800 | [diff] [blame] | 194 | def run( topo, controllers=None, link=TCLink, autoSetMacs=True ): |
| 195 | if not controllers and len( sys.argv ) > 1: |
| 196 | controllers = sys.argv[ 1: ] |
| 197 | else: |
| 198 | print 'Need to provide a topology and list of controllers' |
| 199 | exit( 1 ) |
| 200 | |
| 201 | setLogLevel( 'info' ) |
| 202 | |
| 203 | net = ONOSMininet( topo=topo, controllers=controllers, link=link, autoSetMacs=autoSetMacs ) |
| 204 | net.start() |
| 205 | CLI( net ) |
| 206 | net.stop() |