Statistics
| Branch: | Tag: | Revision:

mininet / bin / mn_clean.py @ 723d068c

History | View | Annotate | Download (1.78 KB)

1
#!/usr/bin/env python
2
"""Mininet Cleanup
3

4
@author Bob Lantz (rlantz@cs.stanford.edu)
5

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

    
14
from subprocess import Popen, PIPE
15

    
16
from mininet.xterm import cleanUpScreens
17

    
18

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

    
24

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

    
29
    print "*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes"
30
    zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core '
31
    zombies += 'udpbwtest'
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
    sh('killall -9 ' + zombies + ' 2> /dev/null')
36

    
37
    print "*** Removing junk from /tmp"
38
    sh('rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log')
39

    
40
    print "*** Removing old screen sessions"
41
    cleanUpScreens()
42

    
43
    print "*** Removing excess kernel datapaths"
44
    dps = sh("ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'").split('\n')
45
    for dp in dps:
46
        if dp != '':
47
            sh('dpctl deldp ' + dp)
48

    
49
    print "*** Removing all links of the pattern foo-ethX"
50
    links = sh("ip link show | egrep -o '(\w+-eth\w+)'").split('\n')
51
    for link in links:
52
        if link != '':
53
            sh("ip link del " + link)
54

    
55
    print "*** Cleanup complete."
56

    
57
if __name__ == "__main__":
58
    cleanup()