Committed by
Gerrit Code Review
Adding background iperf to onosnet.py
Also, doing some other cleanup and improvements Change-Id: I17ee64126e5e8de986e460641994a279e18c9492
Showing
1 changed file
with
69 additions
and
15 deletions
1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
2 | 2 | ||
3 | import sys | 3 | import sys |
4 | -from threading import Thread | 4 | +import itertools |
5 | +from time import sleep | ||
5 | 6 | ||
6 | from mininet.net import Mininet | 7 | from mininet.net import Mininet |
7 | from mininet.log import setLogLevel | 8 | from mininet.log import setLogLevel |
8 | from mininet.node import RemoteController | 9 | from mininet.node import RemoteController |
9 | -from mininet.log import info, debug | 10 | +from mininet.log import info, debug, output |
10 | from mininet.util import quietRun | 11 | from mininet.util import quietRun |
11 | from mininet.link import TCLink | 12 | from mininet.link import TCLink |
12 | from mininet.cli import CLI | 13 | from mininet.cli import CLI |
... | @@ -62,19 +63,16 @@ class ONOSMininet( Mininet ): | ... | @@ -62,19 +63,16 @@ class ONOSMininet( Mininet ): |
62 | info( '\nWARNING: arping is not found, using ping instead.\n' | 63 | 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 | 'For higher performance, install arping: sudo apt-get install iputils-arping\n\n' ) |
64 | 65 | ||
65 | - threads = [ self.threadPing(s, d) for (s, d) in zip( self.hosts, self.hosts[1:] + self.hosts[0:1] ) ] | 66 | + procs = [ s.popen( 'ping -w 0.1 -W 0.1 -c1 %s > /dev/null; printf "%s "' |
66 | - for t in threads: | 67 | + % ( d.IP(), s.name ), shell=True ) |
67 | - t.join() | 68 | + for (s, d) in zip( self.hosts, self.hosts[1:] + self.hosts[0:1] ) ] |
68 | - info ( '\n' ) | 69 | + for t in procs: |
69 | - | 70 | + out, err = t.communicate() |
70 | - def threadPing( self, src, dst ): | 71 | + if err: |
71 | - "Ping from src to dst in a thread" | 72 | + info ( err ) |
72 | - def p(): | 73 | + else: |
73 | - src.cmd( 'ping -w 0.1 -W 0.1 -c1 ' + dst.IP() ) | 74 | + info ( out ) |
74 | - t = Thread( target=p ) | 75 | + info ( '\n' ) |
75 | - info ( '%s ' % src.name ) | ||
76 | - t.start() | ||
77 | - return t | ||
78 | 76 | ||
79 | def pingloop( self ): | 77 | def pingloop( self ): |
80 | "Loop forever pinging the full mesh of hosts" | 78 | "Loop forever pinging the full mesh of hosts" |
... | @@ -85,9 +83,65 @@ class ONOSMininet( Mininet ): | ... | @@ -85,9 +83,65 @@ class ONOSMininet( Mininet ): |
85 | finally: | 83 | finally: |
86 | setLogLevel( 'info' ) | 84 | setLogLevel( 'info' ) |
87 | 85 | ||
86 | + def bgIperf( self, hosts=[], seconds=10 ): | ||
87 | + #TODO check if the hosts are strings or objects | ||
88 | + # h1 = net.getNodeByName('h1') | ||
89 | + servers = [ host.popen("iperf -s") for host in hosts ] | ||
90 | + | ||
91 | + clients = [] | ||
92 | + for pair in itertools.combinations(hosts, 2): | ||
93 | + info ( '%s <--> %s\n' % ( pair[0].name, pair[1].name )) | ||
94 | + cmd = "iperf -c %s -t %s" % (pair[1].IP(), seconds) | ||
95 | + clients.append(pair[0].popen(cmd)) | ||
96 | + | ||
97 | + progress( seconds ) | ||
98 | + | ||
99 | + for c in clients: | ||
100 | + out, err = c.communicate() | ||
101 | + if err: | ||
102 | + info( err ) | ||
103 | + else: | ||
104 | + debug( out ) | ||
105 | + #TODO parse output and print summary | ||
106 | + | ||
107 | + for s in servers: | ||
108 | + s.terminate() | ||
109 | + | ||
110 | +def progress(t): | ||
111 | + while t > 0: | ||
112 | + sys.stdout.write( '.' ) | ||
113 | + t -= 1 | ||
114 | + sys.stdout.flush() | ||
115 | + sleep(1) | ||
116 | |||
117 | + | ||
88 | # Initialize ONOSMininet the first time that the class is loaded | 118 | # Initialize ONOSMininet the first time that the class is loaded |
89 | ONOSMininet.setup() | 119 | ONOSMininet.setup() |
90 | 120 | ||
121 | +def do_iperf( self, line ): | ||
122 | + args = line.split() | ||
123 | + if not args: | ||
124 | + output( 'Provide a list of hosts.\n' ) | ||
125 | + hosts = [] | ||
126 | + err = False | ||
127 | + for arg in args: | ||
128 | + if arg not in self.mn: | ||
129 | + err = True | ||
130 | + error( "node '%s' not in network\n" % arg ) | ||
131 | + else: | ||
132 | + hosts.append( self.mn[ arg ] ) | ||
133 | + if "bgIperf" in dir(self.mn) and not err: | ||
134 | + self.mn.bgIperf( hosts ) | ||
135 | + | ||
136 | +def do_gratuitousArp( self, _line ): | ||
137 | + if "gratuitousArp" in dir(self.mn): | ||
138 | + self.mn.gratuitousArp() | ||
139 | + else: | ||
140 | + output( 'Gratuitous ARP is not support.\n' ) | ||
141 | + | ||
142 | +CLI.do_bgIperf = do_iperf | ||
143 | +CLI.do_gratuitousArp = do_gratuitousArp | ||
144 | + | ||
91 | def run( topo, controllers=None, link=TCLink, autoSetMacs=True ): | 145 | def run( topo, controllers=None, link=TCLink, autoSetMacs=True ): |
92 | if not controllers and len( sys.argv ) > 1: | 146 | if not controllers and len( sys.argv ) > 1: |
93 | controllers = sys.argv[ 1: ] | 147 | controllers = sys.argv[ 1: ] | ... | ... |
-
Please register or login to post a comment