Statistics
| Branch: | Tag: | Revision:

mininet / mininet / clean.py @ 32507498

History | View | Annotate | Download (2.26 KB)

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

5 80be5642 Bob Lantz
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 ea420ee2 Bob Lantz
"""
12
13
from subprocess import Popen, PIPE
14 5c242637 Rich Lane
import time
15 ea420ee2 Bob Lantz
16 3eb5abe6 Brandon Heller
from mininet.log import info
17 ff6145c0 Bob Lantz
from mininet.term import cleanUpScreens
18 ede34b6a Brandon Heller
19 8895862a Bob Lantz
def sh( cmd ):
20 723d068c Brandon Heller
    "Print a command and send it to the shell"
21 3eb5abe6 Brandon Heller
    info( cmd + '\n' )
22 8895862a Bob Lantz
    return Popen( [ '/bin/sh', '-c', cmd ], stdout=PIPE ).communicate()[ 0 ]
23 723d068c Brandon Heller
24 ea420ee2 Bob Lantz
def cleanup():
25 723d068c Brandon Heller
    """Clean up junk which might be left over from old runs;
26
       do fast stuff before slow dp and link removal!"""
27 82b72072 Bob Lantz
28 3eb5abe6 Brandon Heller
    info("*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes"
29
         "\n")
30 723d068c Brandon Heller
    zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core '
31 60abb344 Rich Lane
    zombies += 'ovs-openflowd ovs-controller udpbwtest mnexec ivs'
32 723d068c Brandon Heller
    # Note: real zombie processes can't actually be killed, since they
33 80be5642 Bob Lantz
    # are already (un)dead. Then again,
34 723d068c Brandon Heller
    # you can't connect to them either, so they're mostly harmless.
35 5c242637 Rich Lane
    # Send SIGTERM first to give processes a chance to shutdown cleanly.
36
    sh( 'killall ' + zombies + ' 2> /dev/null' )
37
    time.sleep(1)
38 8895862a Bob Lantz
    sh( 'killall -9 ' + zombies + ' 2> /dev/null' )
39 723d068c Brandon Heller
40 1bf1a4d5 Bob Lantz
    # And kill off sudo mnexec
41
    sh( 'pkill -9 -f "sudo mnexec"')
42
43 ce67093a Bob Lantz
    info( "*** Removing junk from /tmp\n" )
44 8895862a Bob Lantz
    sh( 'rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log' )
45 723d068c Brandon Heller
46 a0f69d98 Bob Lantz
    info( "*** Removing old X11 tunnels\n" )
47 723d068c Brandon Heller
    cleanUpScreens()
48
49 ce67093a Bob Lantz
    info( "*** Removing excess kernel datapaths\n" )
50 8895862a Bob Lantz
    dps = sh( "ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'" ).split( '\n' )
51 723d068c Brandon Heller
    for dp in dps:
52
        if dp != '':
53 8895862a Bob Lantz
            sh( 'dpctl deldp ' + dp )
54 723d068c Brandon Heller
55 a49c85a6 Bob Lantz
    info( "***  Removing OVS datapaths" )
56 94ff77f2 Rich Lane
    dps = sh("ovs-vsctl --timeout=1 list-br").split( '\n' )
57 a49c85a6 Bob Lantz
    for dp in dps:
58
        if dp:
59
            sh( 'ovs-vsctl del-br ' + dp )
60
61 ce67093a Bob Lantz
    info( "*** Removing all links of the pattern foo-ethX\n" )
62 33e39a24 Bob Lantz
    links = sh( r"ip link show | egrep -o '(\w+-eth\w+)'" ).split( '\n' )
63 723d068c Brandon Heller
    for link in links:
64
        if link != '':
65 8895862a Bob Lantz
            sh( "ip link del " + link )
66 723d068c Brandon Heller
67 ce67093a Bob Lantz
    info( "*** Cleanup complete.\n" )