Statistics
| Branch: | Tag: | Revision:

mininet / mininet / cleanup @ 51270ce4

History | View | Annotate | Download (1.98 KB)

1
#!/usr/bin/python
2

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

    
12
from subprocess import Popen, PIPE
13
import re
14

    
15
from mininet import quietRun
16

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

    
23
def cleanUpScreens():
24
   "Remove moldy old screen sessions."      
25
   r = r'(\d+.[hsc]\d+)'
26
   output = sh( 'screen -ls' ).split( '\n' )
27
   for line in output:
28
      m = re.search( r, line )
29
      if m is not None:
30
         quietRun( 'screen -S ' + m.group( 1 ) + ' -X kill' )
31
 
32
def cleanup():
33
   """Clean up junk which might be left over from old runs;
34
      do fast stuff before slow dp and link removal!"""
35
      
36
   print "*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes"
37
   zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core '
38
   zombies += 'udpbwtest'
39
   # Note: real zombie processes can't actually be killed, since they 
40
   # are already (un)dead. Then again,
41
   # you can't connect to them either, so they're mostly harmless.
42
   sh( 'killall -9 ' + zombies + ' 2> /dev/null' )
43

    
44
   print "*** Removing junk from /tmp"
45
   sh( 'rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log' )
46

    
47
   print "*** Removing old screen sessions"
48
   cleanUpScreens()
49

    
50
   print "*** Removing excess kernel datapaths"
51
   dps = sh( "ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'" ).split( '\n')
52
   for dp in dps: 
53
      if dp != '': sh( 'dpctl deldp ' + dp )
54
      
55
   print "*** Removing all links of the pattern foo-ethX"
56
   links = sh( "ip link show | egrep -o '(\w+-eth\w+)'" ).split( '\n' )
57
   for link in links: 
58
      if link != '': sh( "ip link del " + link )
59

    
60
   print "*** Cleanup complete."
61

    
62
if __name__ == "__main__":
63
   cleanup()