Exemplo n.º 1
0
def killprocs(patterns, timeout=10):
    """Reliably terminate processes matching a pattern (including args)"""

    # Try clean kill
    for p in patterns:
        mnclean.sh('pkill -SIGINT -f %s' % p)

    # Make sure they are gone
    t = 0
    to_be_killed = {p: True for p in patterns}
    while any(to_be_killed.values()) or t >= timeout:
        for p in patterns:
            try:
                pids = check_output(['pgrep', '-f', p])
            except CalledProcessError:
                pids = ''
            if not pids:
                log.info(p)
                to_be_killed[p] = False

        time.sleep(.5)
        t += .5

    # Last resort
    for p in patterns:
        if to_be_killed[p]:
            log.info(p)
            mnclean.killprocs(p)
Exemplo n.º 2
0
 def start(self):
     "Start up ONOS cluster"
     killprocs('karaf.jar')
     info('*** ONOS_APPS = %s\n' % ONOS_APPS)
     self.net.start()
     for node in self.nodes():
         node.start(self.env)
     info('\n')
     self.configPortForwarding(ports=self.forward, action='A')
     self.waitStarted()
     return
Exemplo n.º 3
0
 def start( self ):
     "Start up ONOS cluster"
     killprocs( 'karaf.jar' )
     info( '*** ONOS_APPS = %s\n' % ONOS_APPS )
     self.net.start()
     for node in self.nodes():
         node.start( self.env )
     info( '\n' )
     self.configPortForwarding( ports=self.forward, action='A' )
     self.waitStarted()
     return
Exemplo n.º 4
0
    def cleanup_wifi(cls):
        """Clean up junk which might be left over from old runs;
           do fast stuff before slow dp and link removal!"""

        info("***  Removing WiFi module and Configurations\n")

        try:
            co("lsmod | grep mac80211_hwsim", shell=True)
            os.system('rmmod mac80211_hwsim')
        except:
            pass

        try:
            co("lsmod | grep ifb", shell=True)
            os.system('rmmod ifb')
        except:
            pass

        killprocs('hostapd')

        if glob.glob('*-mn-telemetry.txt'):
            os.system('rm *-mn-telemetry.txt')
        if glob.glob('*.apconf'):
            os.system('rm *.apconf')
        if glob.glob('*.staconf'):
            os.system('rm *.staconf')
        if glob.glob('*wifiDirect.conf'):
            os.system('rm *wifiDirect.conf')
        if glob.glob('*.nodeParams'):
            os.system('rm *.nodeParams')

        try:
            os.system('pkill -f \'wpa_supplicant -B -Dnl80211\'')
        except:
            pass

        try:
            os.system('pkill -f \'babeld\'')
        except:
            pass

        info("*** Killing wmediumd\n")
        sh('pkill wmediumd')

        sixlowpan.cleanup_6lowpan()
Exemplo n.º 5
0
def cleanup(level='info'):
    """Cleanup all possible junk that we may have started."""
    log.setLogLevel(level)
    # Standard mininet cleanup
    mnclean.cleanup()
    # Cleanup any leftover daemon
    patterns = []
    for d in daemons.__all__:
        obj = getattr(daemons, d)
        killp = getattr(obj, 'KILL_PATTERNS', None)
        if not killp:
            continue
        if not is_container(killp):
            killp = [killp]
        patterns.extend(killp)
    log.info('*** Cleaning up daemons:\n')
    for pattern in patterns:
        mnclean.killprocs('"%s"' % pattern)
    log.info('\n')
Exemplo n.º 6
0
def cleanup():
    """Cleanup all possible junk that we may have started."""
    log.setLogLevel('info')
    # Standard mininet cleanup
    mnclean.cleanup()
    # Cleanup any leftover daemon
    patterns = []
    for d in daemons.__all__:
        obj = getattr(daemons, d)
        killp = getattr(obj, 'KILL_PATTERNS', None)
        if not killp:
            continue
        if not is_container(killp):
            killp = [killp]
        patterns.extend(killp)
    log.info('*** Cleaning up daemons:\n')
    for pattern in patterns:
        mnclean.killprocs('"%s"' % pattern)
    log.info('\n')
Exemplo n.º 7
0
 def stop( self ):
     "Stop the controller(s), switches and hosts"
     info( '*** Stopping %i controllers\n' % len( self.controllers ) )
     for controller in self.controllers:
         info( controller.name + ' ' )
         controller.stop()
     info( '\n' )
     if self.terms:
         info( '*** Stopping %i terms\n' % len( self.terms ) )
         self.stopXterms()
     info( '*** Stopping %i links\n' % len( self.links ) )
     for link in self.links:
         info( '.' )
         link.stop()
     info( '\n' )
     info( '*** Stopping %i switches\n' % len( self.switches ) )
     stopped = {}
     for swclass, switches in groupby(
             sorted( self.switches,
                     key=lambda s: str( type( s ) ) ), type ):
         switches = tuple( switches )
         if hasattr( swclass, 'batchShutdown' ):
             success = swclass.batchShutdown( switches )
             stopped.update( { s: s for s in success } )
     for switch in self.switches:
         info( switch.name + ' ' )
         if switch not in stopped:
             switch.stop()
         switch.terminate()
     info( '\n' )
     info( '*** Stopping ofc-servers\n' )
     from mininet.clean import killprocs
     killprocs( 'ofconfig' )
     killprocs( 'ofc-server' )
     info( '*** Stopping %i hosts\n' % len( self.hosts ) )
     for host in self.hosts:
         info( host.name + ' ' )
         host.terminate()
     info( '\n*** Done\n' )
Exemplo n.º 8
0
    def cleanup_wifi(cls):
        """Clean up junk which might be left over from old runs;
           do fast stuff before slow dp and link removal!"""

        info("***  Removing WiFi module and Configurations\n")

        try:
            co("lsmod | grep mac80211_hwsim", shell=True)
            os.system('rmmod mac80211_hwsim')
        except:
            pass

        try:
            co("lsmod | grep ifb", shell=True)
            os.system('rmmod ifb')
        except:
            pass

        killprocs('hostapd')

        if glob.glob("*.apconf"):
            os.system('rm *.apconf')
        if glob.glob("*.staconf"):
            os.system('rm *.staconf')
        if glob.glob("*wifiDirect.conf"):
            os.system('rm *wifiDirect.conf')
        if glob.glob("*.nodeParams"):
            os.system('rm *.nodeParams')

        try:
            os.system('pkill -f \'wpa_supplicant -B -Dnl80211\'')
        except:
            pass

        info("*** Killing wmediumd\n")
        sh('pkill wmediumd')

        sixlowpan.cleanup_6lowpan()
Exemplo n.º 9
0
from mininet.clean import cleanup, killprocs, Cleanup
from mininet.cli import CLI
from mininet.log import info, setLogLevel
from mininet.net import Containernet
from mininet.node import Controller
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import MultiprocessFTPServer

from container.kali import Kali
from controller import PoxController

from typing import List, Tuple

# Add a cleanup command to mininet.clean to clean pox controller
Cleanup.addCleanupCallback(lambda: killprocs(PoxController.pox_comand))


class Scenario(object):
    """
    Base scenario class.
    Performs the functions to run and document a scenario.
    Should be extended by any other implemented scenarios, all public methods are extensible.
    Extension classes should have the class name `Import` as per the `start` file.
    """
    # These attributes allow for the filtering/ordering of scenarios when presenting them to students
    name = "Base Scenario"
    """Scenario name. Used in the produced documentation"""
    enabled = False
    """If the scenario should be shown to users."""
    weight = -1
Exemplo n.º 10
0
    def cleanup(cls):
        """Clean up junk which might be left over from old runs;
           do fast stuff before slow dp and link removal!"""

        Cleanup_nm_wifi.cleanup_wifi()

        info("**** Removing excess controllers/ofprotocols/ofdatapaths/"
             "pings/noxes\n")
        zombies = ('controller ofprotocol ofdatapath ping nox_core'
                   'lt-nox_core ovs-openflowd ovs-controller'
                   'ovs-testcontroller udpbwtest mnexec ivs ryu-manager')
        # Note: real zombie processes can't actually be killed, since they
        # are already (un)dead. Then again,
        # you can't connect to them either, so they're mostly harmless.
        # Send SIGTERM first to give processes a chance to shutdown cleanly.
        sh('killall ' + zombies + ' 2> /dev/null')
        time.sleep(1)
        sh('killall -9 ' + zombies + ' 2> /dev/null')

        # And kill off sudo mnexec
        sh('pkill -9 -f "sudo mnexec"')

        info("*** Removing junk from /tmp\n")
        sh('rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log')

        info("*** Removing old X11 tunnels\n")
        cleanUpScreens()

        info("*** Removing excess kernel datapaths\n")
        dps = sh("ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'").splitlines()
        for dp in dps:
            if dp:
                sh('dpctl deldp ' + dp)

        info("***  Removing OVS datapaths\n")
        dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
        if dps:
            sh("ovs-vsctl " + " -- ".join("--if-exists del-br " + dp
                                          for dp in dps if dp))
        # And in case the above didn't work...
        dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
        for dp in dps:
            sh('ovs-vsctl del-br ' + dp)

        info("*** Removing all links of the pattern foo-ethX\n")
        links = sh(
            "ip link show | "
            "egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'").splitlines()
        # Delete blocks of links
        n = 1000  # chunk size
        for i in range(0, len(links), n):
            cmd = ';'.join('ip link del %s' % link for link in links[i:i + n])
            sh('( %s ) 2> /dev/null' % cmd)

        if 'tap9' in sh('ip link show'):
            info("*** Removing tap9 - assuming it's from cluster edition\n")
            sh('ip link del tap9')

        info("*** Killing stale mininet node processes\n")
        killprocs('mininet:')

        info("*** Shutting down stale tunnels\n")
        killprocs('Tunnel=Ethernet')
        killprocs('.ssh/mn')
        sh('rm -f ~/.ssh/mn/*')

        # Call any additional cleanup code if necessary
        for callback in cls.callbacks:
            callback()

        # Containernet should also cleanup pending Docker
        cmd = "docker rm -f $( docker ps --filter 'label=com.containernet' -a -q)"
        call(cmd,
             shell=True,
             stdout=open(os.devnull, 'wb'),
             stderr=open(os.devnull, 'wb'))

        # cleanup any remaining iptables rules from external SAPs with NAT
        # we use iptc module to iterate through the loops, but due to a bug, we cannot use iptc to delete the rules
        # we rely on iptables CLI to delete the found rules
        info("***  Removing SAP NAT rules\n")
        table = iptc.Table(iptc.Table.NAT)
        chain = iptc.Chain(table, 'POSTROUTING')

        for rule in chain.rules:
            if SAP_PREFIX in str(rule.out_interface):
                src_CIDR = str(
                    ipaddress.IPv4Network(u'{}'.format(str(rule.src))))
                rule0_ = "iptables -t nat -D POSTROUTING ! -o {0} -s {1} -j MASQUERADE".\
                    format(rule.out_interface.strip('!'), src_CIDR)
                p = Popen(shlex.split(rule0_))
                p.communicate()
                info("delete NAT rule from SAP: {1} - {0} - {2}\n".format(
                    rule.out_interface, rule.in_interface, src_CIDR))

        table = iptc.Table(iptc.Table.FILTER)
        chain = iptc.Chain(table, 'FORWARD')
        for rule in chain.rules:
            src_CIDR = str(ipaddress.IPv4Network(u'{}'.format(str(rule.src))))
            if SAP_PREFIX in str(rule.out_interface):
                rule1_ = "iptables -D FORWARD -o {0} -j ACCEPT".format(
                    rule.out_interface)
                p = Popen(shlex.split(rule1_))
                p.communicate()
                info("delete FORWARD rule from SAP: {1} - {0} - {2}\n".format(
                    rule.out_interface, rule.in_interface, src_CIDR))

            if SAP_PREFIX in str(rule.in_interface):
                rule2_ = "iptables -D FORWARD -i {0} -j ACCEPT".format(
                    rule.in_interface)
                p = Popen(shlex.split(rule2_))
                p.communicate()
                info("delete FORWARD rule from SAP: {1} - {0} - {2}\n".format(
                    rule.out_interface, rule.in_interface, src_CIDR))

        info("*** Cleanup complete.\n")