-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
291 lines (250 loc) · 9.75 KB
/
util.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import sys
import agenda
from fabric import Connection, Result
from termcolor import colored
import os
###################################################################################################
# Helpers
###################################################################################################
class FakeResult(object):
def __init__(self):
self.exited = 0
self.stdout = '(dryrun)'
class ConnectionWrapper(Connection):
def __init__(self, addr, nickname, user=None, port=None, verbose=True, dry=False, interact=False):
super().__init__(
addr,
forward_agent=True,
user=user,
port=port,
)
self.addr = addr
self.nickname = nickname
self.verbose = verbose
self.dry = dry
self.interact = interact
# Start the ssh connection
super().open()
"""
Run a command on the remote machine
verbose : if true, print the command before running it, and any output it produces
(if not redirected)
if false, capture anything produced in stdout and save in result (res.stdout)
background : if true, start the process in the background via nohup.
if output is not directed to a file or pty=True, this won't work
stdin : string of filename for stdin (default /dev/stdin as expected)
stdout : ""
stderr : ""
ignore_out : shortcut to set stdout and stderr to /dev/null
wd : cd into this directory before running the given command
sudo : if true, execute this command with sudo (done AFTER changing to wd)
returns result struct
.exited = return code
.stdout = stdout string (if not redirected to a file)
.stderr = stderr string (if not redirected to a file)
"""
def run(self, cmd, *args, stdin="/dev/stdin", stdout="/dev/stdout", stderr="/dev/stderr", ignore_out=False, wd=None, sudo=False, background=False, pty=True, **kwargs):
# Prepare command string
pre = ""
if wd:
pre += "cd {} && ".format(wd)
if background:
pre += "screen -d -m "
#escape the strings
cmd = cmd.replace("\"", "\\\"")
if sudo:
pre += "sudo "
pre += "bash -c \""
if ignore_out:
stdin="/dev/null"
stdout="/dev/null"
stderr="/dev/null"
if background:
stdin="/dev/null"
full_cmd = "{pre}{cmd} > {stdout} 2> {stderr} < {stdin}".format(
pre=pre,
cmd=cmd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
)
full_cmd += "\""
# Prepare arguments for invoke/fabric
if background:
pty=False
# Print command if necessary
if self.dry or self.verbose:
print("[{}]{} {}".format(self.nickname.ljust(10), " (bg) " if background else " ", full_cmd))
# Finally actually run it
if self.interact:
input("")
if not self.dry:
return super().run(full_cmd, *args, hide=(not self.verbose), warn=True, pty=pty, **kwargs)
else:
return FakeResult()
def file_exists(self, fname):
res = self.run("ls {}".format(fname))
return res.exited == 0
def prog_exists(self, prog):
res = self.run("which {}".format(prog))
return res.exited == 0
def check_proc(self, proc_name, proc_out):
res = self.run("pgrep {}".format(proc_name))
if res.exited != 0:
fatal_warn('failed to find running process with name \"{}\" on {}'.format(proc_name, self.addr), exit=False)
res = self.run('tail {}'.format(proc_out))
if not self.verbose and res.exited == 0:
print(res.command)
print(res.stdout)
sys.exit(1)
def check_file(self, grep, where):
res = self.run("grep \"{}\" {}".format(grep, where))
if res.exited != 0:
fatal_warn("Unable to find search string (\"{}\") in process output file {}".format(
grep,
where
), exit=False)
res = self.run('tail {}'.format(where))
if not self.verbose and res.exited == 0:
print(res.command)
print(res.stdout)
sys.exit(1)
def local_path(self, path):
r = self.run(f"ls {path}")
return r.stdout.strip().replace("'", "")
def put(self, local_file, remote=None, preserve_mode=True):
if remote and remote[0] == "~":
remote = remote[2:]
if self.dry or self.verbose:
print("[{}] scp localhost:{} -> {}:{}".format(
self.addr,
local_file,
self.addr,
remote
))
if self.interact:
input("")
if not self.dry:
return super().put(local_file, remote, preserve_mode)
else:
return FakeResult()
def get(self, remote_file, local=None, preserve_mode=True):
if self.dry or self.verbose:
print("[{}] scp {}:{} -> localhost:{}".format(
self.addr,
self.addr,
remote_file,
local
))
if self.interact:
input("")
if not self.dry:
return super().get(remote_file, local=local, preserve_mode=preserve_mode)
else:
return FakeResult()
def update_sysctl(machines, config):
if 'sysctl' in config:
agenda.task("Updating sysctl")
for (name, conn) in set((m, machines[m]) for m in machines if m in ("sender", "inbox", "outbox", "receiver")):
agenda.subtask(f"{name}")
for k in config['sysctl']:
v = config['sysctl'][k]
expect(
conn.run(f"sysctl -w {k}=\"{v}\"", sudo=True),
f"Failed to set {k} on {conn.addr}"
)
def disable_tcp_offloads(config, machines):
agenda.task("Turn off TSO, GSO, and GRO")
for (name, conn) in set((m, machines[m]) for m in machines if m in ("sender", "inbox", "outbox", "receiver")):
agenda.subtask(name)
for iface in config['topology'][name]['ifaces']:
expect(
conn.run(
"ethtool -K {} tso off gso off gro off".format(
iface['dev']
),
sudo=True
),
"Failed to turn off optimizations"
)
def start_tcpprobe(config, sender):
if config['args'].verbose:
agenda.subtask("Start tcpprobe")
if not sender.file_exists("/proc/net/tcpprobe"):
fatal_warn("Could not find tcpprobe on sender. Make sure the kernel module is loaded.")
expect(
sender.run("dd if=/dev/null of=/proc/net/tcpprobe bs=256", sudo=True, background=True),
"Sender failed to clear tcpprobe buffer"
)
tcpprobe_out = os.path.join(config['iteration_dir'], 'tcpprobe.log')
expect(
sender.run(
"dd if=/proc/net/tcpprobe of={} bs=256".format(tcpprobe_out),
sudo=True,
background=True
),
"Sender failed to start tcpprobe"
)
config['iteration_outputs'].append((sender, tcpprobe_out))
return tcpprobe_out
def start_tcpdump(config, machines):
agenda.subtask("Start tcpdump")
inbox = machines['inbox']
outbox = machines['outbox']
inbox_pcap = os.path.join(config['iteration_dir'], 'inbox.pcap')
outbox_pcap = os.path.join(config['iteration_dir'], 'outbox.pcap')
inbox.run(f"tcpdump -i {config['topology']['inbox']['ifaces'][1]['dev']} -n -s128 -w {inbox_pcap} \"src portrange {config['parameters']['bg_port_start']}-{config['parameters']['bg_port_end']}\"", sudo=True, background=True)
outbox.run(f"tcpdump -i {config['topology']['outbox']['ifaces'][0]['dev']} -n -s128 -w {outbox_pcap} \"src portrange {config['parameters']['bg_port_start']}-{config['parameters']['bg_port_end']}\"", sudo=True, background=True)
config['iteration_outputs'].append((inbox, inbox_pcap))
config['iteration_outputs'].append((outbox, outbox_pcap))
return config
def kill_leftover_procs(config, machines, verbose=False):
agenda.subtask("Kill leftover experiment processes")
for (_name, conn) in set((m, machines[m]) for m in machines if m in ("sender", "inbox", "outbox", "receiver")):
proc_regex = "|".join(["inbox", "outbox", *config['ccp'].keys(), "iperf", "tcpdump", "etgClient", "etgServer", "ccp_const"])
conn.run(
"pkill -9 \"({search})\"".format(
search=proc_regex
),
sudo=True
)
res = conn.run(
"pgrep -c \"({search})\"".format(
search=proc_regex
),
sudo=True
)
if not res.exited and not config['args'].dry_run:
fatal_warn("Failed to kill all procs on {}.".format(conn.addr))
# True = some processes remain, therefore there *are* zombies, so we return false
return (not res.exited)
def expect(res, msg):
if res and res.exited:
agenda.subfailure(msg)
print("exit code: {}\ncommand: {}\nstdout: {}\nstderr: {}".format(
res.exited,
res.command,
res.stdout,
res.stderr
))
return res
def warn(msg, exit=True):
print()
for m in msg.split("\n"):
print(colored(" -> {}".format(m), 'yellow', attrs=['bold']))
print()
if exit:
sys.exit(1)
def fatal_warn(msg, exit=True):
print()
for m in msg.split("\n"):
agenda.subfailure(m)
print()
if exit:
sys.exit(1)
def fatal_error(msg, exit=True):
print()
agenda.failure(msg)
print()
if exit:
sys.exit(1)