forked from intrig-unicamp/mininet-wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.py
More file actions
163 lines (133 loc) · 5.24 KB
/
Copy pathclean.py
File metadata and controls
163 lines (133 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Mininet Cleanup
author: Bob Lantz (rlantz@cs.stanford.edu)
Unfortunately, Mininet and OpenFlow (and the Linux kernel)
don't always clean up properly after themselves. Until they do
(or until cleanup functionality is integrated into the Python
code), this script may be used to get rid of unwanted garbage.
It may also get rid of 'false positives', but hopefully
nothing irreplaceable!
"""
from subprocess import (Popen, PIPE, check_output as co,
CalledProcessError)
import time
import subprocess
import os
import glob
from mininet.log import info
from mininet.term import cleanUpScreens
def sh(cmd):
"Print a command and send it to the shell"
info(cmd + '\n')
return Popen([ '/bin/sh', '-c', cmd ], stdout=PIPE).communicate()[ 0 ]
def killprocs(pattern):
"Reliably terminate processes matching a pattern (including args)"
sh('pkill -9 -f %s' % pattern)
# Make sure they are gone
while True:
try:
pids = co([ 'pgrep', '-f', pattern ])
except CalledProcessError:
pids = ''
if pids:
sh('pkill -9 -f %s' % pattern)
time.sleep(.5)
else:
break
class Cleanup(object):
"Wrapper for cleanup()"
callbacks = []
@classmethod
def cleanup(cls):
"""Clean up junk which might be left over from old runs;
do fast stuff before slow dp and link removal!"""
info("*** Removing excess controllers/ofprotocols/ofdatapaths/"
"pings/noxes\n")
zombies = 'controller ofprotocol ofdatapath ping nox_core lt-nox_core '
zombies += 'ovs-openflowd ovs-controller udpbwtest mnexec ivs ryu-manager'
# Note: real zombie processes can't actually be killed, since they
# are already (un)dead. Then again,
# you can't connect to them either, so they're mostly harmless.
# Send SIGTERM first to give processes a chance to shutdown cleanly.
sh('killall ' + zombies + ' 2> /dev/null')
time.sleep(1)
sh('killall -9 ' + zombies + ' 2> /dev/null')
# And kill off sudo mnexec
sh('pkill -9 -f "sudo mnexec"')
info("*** Removing junk from /tmp\n")
sh('rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log')
info("*** Removing old X11 tunnels\n")
cleanUpScreens()
info("*** Removing excess kernel datapaths\n")
dps = sh("ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'"
).splitlines()
for dp in dps:
if dp:
sh('dpctl deldp ' + dp)
info("*** Removing WiFi module and Configurations\n")
try:
(subprocess.check_output("lsmod | grep mac80211_hwsim",
shell=True))
os.system('rmmod mac80211_hwsim')
except:
pass
try:
(subprocess.check_output("lsmod | grep ifb",
shell=True))
os.system('rmmod ifb')
except:
pass
killprocs('hostapd')
if glob.glob("*.apconf"):
os.system('rm *.apconf')
if glob.glob("*.staconf"):
os.system('rm *.staconf')
if glob.glob("*wifiDirect.conf"):
os.system('rm *wifiDirect.conf')
if glob.glob("*.nodeParams"):
os.system('rm *.nodeParams')
try:
os.system('pkill -f \'wpa_supplicant -B -Dnl80211\'')
except:
pass
info("*** Removing OVS datapaths\n")
dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
if dps:
sh("ovs-vsctl " + " -- ".join("--if-exists del-br " + dp
for dp in dps if dp))
# And in case the above didn't work...
dps = sh("ovs-vsctl --timeout=1 list-br").strip().splitlines()
for dp in dps:
sh('ovs-vsctl del-br ' + dp)
info("*** Removing all links of the pattern foo-ethX\n")
links = sh("ip link show | "
"egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'"
).splitlines()
# Delete blocks of links
n = 1000 # chunk size
for i in range(0, len(links), n):
cmd = ';'.join('ip link del %s' % link
for link in links[ i : i + n ])
sh('( %s ) 2> /dev/null' % cmd)
if 'tap9' in sh('ip link show'):
info("*** Removing tap9 - assuming it's from cluster edition\n")
sh('ip link del tap9')
info("*** Killing stale mininet node processes\n")
killprocs('mininet:')
info("*** Shutting down stale tunnels\n")
killprocs('Tunnel=Ethernet')
killprocs('.ssh/mn')
sh('rm -f ~/.ssh/mn/*')
info("*** Killing wmediumd\n")
sh('pkill wmediumd')
# Call any additional cleanup code if necessary
for callback in cls.callbacks:
callback()
info("*** Cleanup complete.\n")
@classmethod
def addCleanupCallback(cls, callback):
"Add cleanup callback"
if callback not in cls.callbacks:
cls.callbacks.append(callback)
cleanup = Cleanup.cleanup
addCleanupCallback = Cleanup.addCleanupCallback