Statistics
| Branch: | Tag: | Revision:

mininet / mininet / clean.py @ 3c3344e1

History | View | Annotate | Download (2.89 KB)

1
"""
2
Mininet Cleanup
3
author: Bob Lantz (rlantz@cs.stanford.edu)
4

5
Unfortunately, Mininet and OpenFlow (and the Linux kernel)
6
don't always clean up properly after themselves. Until they do
7
(or until cleanup functionality is integrated into the Python
8
code), this script may be used to get rid of unwanted garbage.
9
It may also get rid of 'false positives', but hopefully
10
nothing irreplaceable!
11
"""
12

    
13
from subprocess import Popen, PIPE, check_output as co
14
import time
15

    
16
from mininet.log import info
17
from mininet.term import cleanUpScreens
18

    
19
def sh( cmd ):
20
    "Print a command and send it to the shell"
21
    info( cmd + '\n' )
22
    return Popen( [ '/bin/sh', '-c', cmd ], stdout=PIPE ).communicate()[ 0 ]
23

    
24
def cleanup():
25
    """Clean up junk which might be left over from old runs;
26
       do fast stuff before slow dp and link removal!"""
27

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

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

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

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

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

    
55
    info( "***  Removing OVS datapaths" )
56
    dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
57
    if dps:
58
        sh( "ovs-vsctl " + " -- ".join( "--if-exists del-br " + dp
59
                                       for dp in dps if dp ) )
60
    # And in case the above didn't work...
61
    dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
62
    for dp in dps:
63
        sh( 'ovs-vsctl del-br ' + dp )
64

    
65
    info( "*** Removing all links of the pattern foo-ethX\n" )
66
    links = sh( "ip link show | "
67
                "egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'" ).splitlines()
68
    for link in links:
69
        if link:
70
            sh( "ip link del " + link )
71

    
72
    info( "*** Killing stale mininet node processes\n" )
73
    sh( 'pkill -9 -f mininet:' )
74
    # Make sure they are gone
75
    while True:
76
        try:
77
            pids = co( 'pgrep -f mininet:'.split() )
78
        except:
79
            pids = ''
80
        if pids:
81
            sh( 'pkill -f 9 mininet:' )
82
            sleep( .5 )
83
        else:
84
            break
85

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