-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathqeneth
executable file
·477 lines (378 loc) · 9.44 KB
/
qeneth
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/sh
set -e
qn_root=$(dirname $(readlink -f "$0"))
QN_TMPL_PATH=${QENETH_PATH:-.:$qn_root/templates}
qn_tmpl_path="$(echo $QN_TMPL_PATH | sed -e 's|:| |g')"
die()
{
printf "ERROR: %s\n" "$@" >&2
exit 1
}
warn()
{
printf "Warn: %s\n" "$@" >&2
}
info()
{
printf "Info: %s\n" "$@" >&2
}
print()
{
printf "\e[37;44m>>> %s\e[0m\n" "$@" >&2
}
selftest()
{
gvpr -V >/dev/null 2>&1 || die "gvpr(1) not found, is graphviz installed?"
mustache -v >/dev/null 2>&1 || die "mustache(1) not found, is ruby-mustache installed?"
if [ "$1" = "-g" ]; then
[ -f topology.dot.in ] && return
die "No topology.dot.in to generate topology.dot from"
else
[ -f topology.dot ] && return
die "No topology.dot, please run $0 generate first"
fi
}
example()
{
print "Creating example/ directory ..."
mkdir example || exit 1
cd example/
print "Fetching Infix x86-64 Classic ..."
wget -q --show-progress --progress=bar:force:noscroll https://github.com/kernelkit/infix/releases/download/latest/infix-x86_64-classic.tar.gz >/dev/null
print "Extracting Infix x86-64 Classic image from tarball ..."
tar xf infix-x86_64-classic.tar.gz infix-x86_64-classic/infix-x86_64-classic.img
mv infix-x86_64-classic/infix-x86_64-classic.img .
rmdir infix-x86_64-classic/
rm infix-x86_64-classic.tar.gz
print "Creating topology.dot.in ..."
cat <<-EOF >topology.dot.in
graph "example" {
node [shape=record];
qn_template="infix-x86_64-classic";
qn_append="quiet";
server [label="server | { <eth0> eth0 | <eth1> eth1 }"];
client1 [label="client1 | { <eth0> eth0 }"];
client2 [label="client2 | { <eth0> eth0 }"];
server:eth0 -- client1:eth0;
server:eth1 -- client2:eth0;
}
EOF
selftest -g
print "Generating ..."
generate
print "Done, enter the example directory and continue manually from there."
}
nodeattr()
{
gvpr -a "$1 $2" '
N [$.name == ARGV[0] && hasAttr($, ARGV[1])] {
print(aget($, ARGV[1]));
exit(0);
}
END {
exit(1);
}' topology.dot
}
gentopology()
{
info "Generating topology"
gvpr -o topology.dot '
BEGIN {
int ctrl = 10000;
int data = 20000;
void sdflt(obj_t obj, string attr, string val) {
if (hasAttr(obj, attr) && aget(obj, attr))
return;
if (hasAttr(obj.root, attr) && aget(obj.root, attr)) {
aset(obj, attr, aget(obj.root, attr));
return;
}
aset(obj, attr, val);
}
void idflt(obj_t obj, string attr, int val) {
if (hasAttr(obj, attr) && aget(obj, attr))
return;
aset(obj, attr, val);
}
}
N {
edge_t e;
if ($.name == "host")
return;
sdflt($, "qn_template", "node");
idflt($, "qn_console", ctrl + 0);
idflt($, "qn_monitor", ctrl + 1);
idflt($, "qn_kgdb", ctrl + 2);
idflt($, "qn_ugdb", ctrl + 3);
ctrl += 10;
for (e = fstedge($); e; e = nxtedge(e, $)) {
if (e.head.name == "host") {
sdflt(e, "qn_tap", e.headport);
} else if (e.tail.name == "host") {
sdflt(e, "qn_tap", e.tailport);
} else {
idflt(e, "qn_headport", data++);
idflt(e, "qn_tailport", data++);
}
}
}
END_G {
$O = $;
}
' topology.dot.in
}
genyaml()
{
info "Generating node YAML"
# Write all global and per-node attributes
gvpr '
N [$.qn_template] {
int yaml;
yaml = openF(sprintf("%s.yaml", $.name), "w+");
if (yaml < 0)
exit(1);
printf(yaml, "graph: %s\n", $G.name);
/* Output global attributes (unless the node overrides them) */
for (attr = fstAttr($G, "G"); attr; attr = nxtAttr($G, "G", attr)) {
if (hasAttr($, attr) && aget($, attr))
continue;
printf(yaml, "%s: \"%s\"\n", attr, aget($G, attr));
}
printf(yaml, "name: \"%s\"\n", $.name);
/* Output node attributes */
for (attr = fstAttr($G, "N"); attr; attr = nxtAttr($G, "N", attr)) {
if (!(hasAttr($, attr) && aget($, attr)))
continue;
printf(yaml, "%s: \"%s\"\n", attr, aget($, attr));
}
printf(yaml, "links:\n");
closeF(yaml);
}' topology.dot
# Version sort port names in the generated YAML. Naming ports
# eth0, eth1 etc. will then result in a 1:1 mapping between
# topology and Linux interface names.
gvpr '
N [$.qn_template] {
edge_t e;
if (hasAttr($, "qn_basemac"))
base_mac = $.qn_basemac;
else if ($G.qn_oui)
base_mac = sprintf("%s:00:00:00", $G.qn_oui);
else
base_mac = "02:00:00:00:00:00";
for (e = fstedge($); e; e = nxtedge(e, $)) {
printf("%s %s", $.name, base_mac);
if (e.qn_headport) {
if (e.head == $)
printf(" qn_name %s qn_sport %d qn_dport %d", e.headport, e.qn_headport, e.qn_tailport);
else
printf(" qn_name %s qn_sport %d qn_dport %d", e.tailport, e.qn_tailport, e.qn_headport);
} else {
if (e.head == $)
printf(" qn_name %s ", e.headport);
else
printf(" qn_name %s ", e.tailport);
}
if (hasAttr(e, "qn_headmac") && e.qn_headmac && e.head == $)
printf(" qn_mac: %s ", e.qn_headmac);
if (hasAttr(e, "qn_tailmac") && e.qn_tailmac && e.tail == $)
printf(" qn_mac: %s ", e.qn_tailmac);
for (attr = fstAttr($G, "E"); attr; attr = nxtAttr($G, "E", attr)) {
if (!(hasAttr(e, attr) && aget(e, attr)))
continue;
printf(" %s %s", attr, aget(e, attr));
}
printf(" %s.yaml\n", $.name);
}
}
' topology.dot | sort -V | awk 'BEGIN { nid = 0; } {
if (!links[$1])
nid++;
split($2, o, ":");
base = o[1] ":" o[2] ":" o[3] ":" o[4];
printf(" - {") >>$NF;
seen_mac = "";
for (attr = 3; attr < NF; attr += 2) {
val = attr + 1;
if ($attr == "qn_mac")
seen_mac = "true";
printf("%s %s: \"%s\"", attr > 3 ? "," : "", $attr, $val) >>$NF;
}
if (seen_mac != "true")
printf(", qn_mac: \"%s:%2.2x:%2.2x\"", base, nid, links[$1] + 1) >>$NF;
printf(" }\n") >>$NF;
links[$1]++;
}'
}
genexec()
{
info "Generating executables"
set -- $(gvpr 'N [$.qn_template] {
print($.name + " " + $.qn_template);
}' topology.dot)
while [ $# -ge 2 ]; do
for dir in $qn_tmpl_path ""; do
[ ! "$dir" ] && die "Unable to find a template matching \"$2\" for $1"
tmpl=$dir/$2.mustache
[ -f $tmpl ] && break;
done
local yaml=$(realpath $1.yaml)
local exe=$(dirname $yaml)/$1
(cd $(dirname $tmpl) && mustache $yaml $(basename $tmpl) >$exe)
chmod +x $1
shift 2
done
}
generate()
{
gentopology
genyaml
genexec
}
start()
{
if [ -f $1.pid ]; then
if kill -0 $(cat $1.pid) 2>/dev/null; then
info "$1 is already running, skipping"
return
fi
warn "$1 terminated unexpectedly, relaunching"
fi
info "Launching $1"
setsid ./$1 >>$1.log 2>&1 </dev/null &
echo $! >$1.pid
}
stop()
{
if [ ! -f $1.pid ]; then
info "$1 is not running, skipping"
return
fi
pid=$(cat $1.pid)
if ! kill -0 $pid 2>/dev/null; then
info "$1 terminated unexpectedly, cleaning up pidfile"
rm $1.pid
return
fi
info "Stopping $1"
kill $pid
wait $pid || true
rm $1.pid
}
startstop()
{
cmd=$1
shift
if [ "$1" ]; then
nodes="$@"
else
nodes=$(gvpr 'N [$.qn_console] { print($.name); }' topology.dot)
fi
for node in $nodes; do
$cmd $node
done
}
dostatus()
{
printf "\e[7m%-8s %8s %5s %5s\e[0m\n" NODE PID CNSOL MONTR
gvpr '
N [$.qn_console] {
string pidfile, pid = "stopped";
int fd;
pidfile = $.name + ".pid";
fd = openF(pidfile, "r");
if (fd >= 0) {
pid = sub(readL(fd), "\n");
closeF(fd);
if (system(sprintf("kill -0 %s >/dev/null 2>&1", pid)))
pid = "lost";
}
printf("%-8s %8s %5d %5d\n", $.name, pid,
aget($, "qn_console"), aget($, "qn_monitor"));
}
' topology.dot
}
doconsole()
{
port=$(nodeattr $1 qn_console || die "No console for $1")
telnet localhost $port
}
domonitor()
{
port=$(nodeattr $1 qn_monitor || die "No monitor for $1")
telnet localhost $port
}
dolink()
{
port=$(nodeattr $1 qn_monitor || die "No monitor for $1")
echo "set_link $2 $3" | nc -N localhost $port >/dev/null
}
doweb()
{
exec $qn_root/web/app.py $1
}
usage()
{
cat <<EOF
usage: ${0} <command> [<args>]
${0} generate
Generate all files required to start the topology.
${0} status
Show status information.
${0} start [<node>...]
Launch the given nodes. If no nodes are specified, all nodes in
the topology are launched.
${0} stop [<node>...]
Terminate the given nodes. If no nodes are specified, all nodes
are stopped.
${0} restart [<node>...]
Stop and start the given nodes. If no nodes are specified, all
nodes are restarted.
${0} console <node>
Start a telnet session to the specified node's console.
To detach: use telnet Ctrl-] for a prompt, and 'quit' to exit.
${0} monitor <node>
Start a telnet session to the specified node's monitor.
${0} link <node> <port> on|off
Set the "physical" link state of the specified port.
${0} web [<port>]
Launch interactive web interface on the specified port, or 8080
by default.
${0} help
Display this message.
EOF
}
if [ $# -lt 1 ]; then
usage && exit 1
fi
cmd=${1}
shift
case ${cmd} in
example)
example
;;
generate)
selftest -g
generate
;;
status|console|monitor|link|web)
selftest
do${cmd} "$@"
;;
start|stop)
selftest
startstop ${cmd} "$@"
;;
restart)
selftest
startstop stop "$@"
# Avoid "address already in use" issues
sleep 0.5
startstop start "$@"
;;
"help")
usage && exit 0
;;
*)
usage && exit 1
esac