$OpenBSD: patch-mininet_net_py,v 1.2 2017/09/09 21:18:30 akoshibe Exp $

Index: mininet/net.py
--- mininet/net.py.orig
+++ mininet/net.py
@@ -96,13 +96,27 @@ from time import sleep
 from itertools import chain, groupby
 from math import ceil
 
+plat = os.uname()[ 0 ]
+if plat == 'FreeBSD':
+    from mininet.freebsd.node import Node
+    from mininet.freebsd.intf import Intf
+    from mininet.freebsd.util import fixLimits, numCores
+elif plat == 'Linux':
+    from mininet.linux.node import Node
+    from mininet.linux.intf import Intf
+    from mininet.linux.util import fixLimits, numCores
+else:
+    from mininet.openbsd.node import Node
+    from mininet.openbsd.intf import Intf
+    from mininet.openbsd.util import fixLimits, numCores
+
 from mininet.cli import CLI
 from mininet.log import info, error, debug, output, warn
-from mininet.node import ( Node, Host, OVSKernelSwitch, DefaultController,
-                           Controller )
+from mininet.node import ( Host, KernelSwitch, DefaultController,
+                           Controller, RctlHost )
 from mininet.nodelib import NAT
-from mininet.link import Link, Intf
-from mininet.util import ( quietRun, fixLimits, numCores, ensureRoot,
+from mininet.link import Link
+from mininet.util import ( quietRun, ensureRoot,
                            macColonHex, ipStr, ipParse, netParse, ipAdd,
                            waitListening )
 from mininet.term import cleanUpScreens, makeTerms
@@ -113,7 +127,7 @@ VERSION = "2.3.0d1"
 class Mininet( object ):
     "Network emulation with hosts spawned in network namespaces."
 
-    def __init__( self, topo=None, switch=OVSKernelSwitch, host=Host,
+    def __init__( self, topo=None, switch=KernelSwitch, host=Host,
                   controller=DefaultController, link=Link, intf=Intf,
                   build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8',
                   inNamespace=False,
@@ -807,7 +821,7 @@ class Mininet( object ):
         client, server = hosts
         output( '*** Iperf: testing', l4Type, 'bandwidth between',
                 client, 'and', server, '\n' )
-        server.cmd( 'killall -9 iperf' )
+        server.cmd( 'pkill -9 iperf' )
         iperfArgs = 'iperf -p %d ' % port
         bwArgs = ''
         if l4Type == 'UDP':
@@ -817,7 +831,7 @@ class Mininet( object ):
             raise Exception( 'Unexpected l4 type: %s' % l4Type )
         if fmt:
             iperfArgs += '-f %s ' % fmt
-        server.sendCmd( iperfArgs + '-s' )
+        server.cmd( iperfArgs + '-s &' )
         if l4Type == 'TCP':
             if not waitListening( client, server.IP(), port ):
                 raise Exception( 'Could not connect to iperf on port %d'
@@ -827,7 +841,7 @@ class Mininet( object ):
         debug( 'Client output: %s\n' % cliout )
         servout = ''
         # We want the last *b/sec from the iperf server output
-        # for TCP, there are two of them because of waitListening
+        # for TCP, there are two fo them because of waitListening
         count = 2 if l4Type == 'TCP' else 1
         while len( re.findall( '/sec', servout ) ) < count:
             servout += server.monitor( timeoutms=5000 )
@@ -849,7 +863,7 @@ class Mininet( object ):
         pct = cpu * 100
         info( '*** Testing CPU %.0f%% bandwidth limit\n' % pct )
         hosts = self.hosts
-        cores = int( quietRun( 'nproc' ) )
+        cores = numCores()
         # number of processes to run a while loop on per host
         num_procs = int( ceil( cores * cpu ) )
         pids = {}
@@ -863,17 +877,26 @@ class Mininet( object ):
         # get the initial cpu time for each host
         for host in hosts:
             outputs[ host ] = []
-            with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
-                       host, 'r' ) as f:
-                time[ host ] = float( f.read() )
+            if isinstance( host, RctlHost ):
+                time[ host ] = host.getCPUTime( pids[ host ] )
+            else:
+                with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
+                           host, 'r' ) as f:
+                    time[ host ] = float( f.read() )
         for _ in range( duration ):
             sleep( 1 )
             for host in hosts:
-                with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
-                           host, 'r' ) as f:
-                    readTime = float( f.read() )
-                outputs[ host ].append( ( ( readTime - time[ host ] )
-                                        / 1000000000 ) / cores * 100 )
+                if isinstance( host, RctlHost ):
+                    readTime = host.getCPUTime( pids[ host ] )
+                    # is procstat output per-core?
+                    outputs[ host ].append( ( readTime - time[ host ] )
+                                            / cores * 100 )
+                else:
+                    with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
+                               host, 'r' ) as f:
+                        readTime = float( f.read() )
+                    outputs[ host ].append( ( ( readTime - time[ host ] )
+                                            / 1000000000 ) / cores * 100 )
                 time[ host ] = readTime
         for h, pids in pids.items():
             for pid in pids:
@@ -893,9 +916,9 @@ class Mininet( object ):
            dst: node name
            status: string {up, down}"""
         if src not in self.nameToNode:
-            error( 'src not in network: %s\n' % src )
+            error( 'src node not in network: %s\n' % src )
         elif dst not in self.nameToNode:
-            error( 'dst not in network: %s\n' % dst )
+            error( 'dst node not in network: %s\n' % dst )
         else:
             src = self.nameToNode[ src ]
             dst = self.nameToNode[ dst ]
