Statistics
| Branch: | Tag: | Revision:

mininet / mininet / clean.py @ b1ec912d

History | View | Annotate | Download (3.23 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
                         CalledProcessError )
15
import time
16

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

    
20

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

    
26
def killprocs( pattern ):
27
    "Reliably terminate processes matching a pattern (including args)"
28
    sh( 'pkill -9 -f %s' % pattern )
29
    # Make sure they are gone
30
    while True:
31
        try:
32
            pids = co( [ 'pgrep', '-f', pattern ] )
33
        except CalledProcessError:
34
            pids = ''
35
        if pids:
36
            sh( 'pkill -9 -f %s' % pattern )
37
            time.sleep( .5 )
38
        else:
39
            break
40

    
41
def cleanup():
42
    """Clean up junk which might be left over from old runs;
43
       do fast stuff before slow dp and link removal!"""
44

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

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

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

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

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

    
72
    info( "***  Removing OVS datapaths" )
73
    dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
74
    if dps:
75
        sh( "ovs-vsctl " + " -- ".join( "--if-exists del-br " + dp
76
                                       for dp in dps if dp ) )
77
    # And in case the above didn't work...
78
    dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
79
    for dp in dps:
80
        sh( 'ovs-vsctl del-br ' + dp )
81

    
82
    info( "*** Removing all links of the pattern foo-ethX\n" )
83
    links = sh( "ip link show | "
84
                "egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'" ).splitlines()
85
    for link in links:
86
        if link:
87
            sh( "ip link del " + link )
88

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

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

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