File: lib.sh

package info (click to toggle)
smcroute 2.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,292 kB
  • sloc: sh: 6,087; ansic: 5,399; perl: 184; makefile: 138
file content (415 lines) | stat: -rwxr-xr-x 8,637 bytes parent folder | download
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
#!/bin/sh

# Test name, used everywhere as /tmp/$NM/foo
NM=$(basename "$0" .sh)

# Print heading for test phases
print()
{
    printf "\e[7m>> %-80s\e[0m\n" "$1"
}

SKIP()
{
    print "TEST: SKIP"
    [ $# -gt 0 ] && echo "$*"
    exit 77
}

FAIL()
{
    print "TEST: FAIL"
    [ $# -gt 0 ] && echo "$*"
    exit 99
}

CHECK()
{
    [ "$@" ] || FAIL "$*"
}

OK()
{
    print "TEST: OK"
    [ $# -gt 0 ] && echo "$*"
    exit 0
}

check_dep()
{
    if [ -n "$2" ]; then
	if ! $@; then
	    SKIP "$* is not supported on this system."
	fi
    elif ! command -v "$1"; then
	SKIP "Cannot find $1, skipping test."
    fi
}

show_mroute()
{
    # Show active routes (and counters)
    cat /proc/net/ip_mr_cache
    cat /proc/net/ip6_mr_cache
    echo "-----------------------------------------------------------------------------------"
    ip mroute
    ip -6 mroute
    echo "-----------------------------------------------------------------------------------"
    ../src/smcroutectl -pd -u "/tmp/$NM/sock"
}

emitter()
{
    print "Starting emitter(s) on $1 ..."
    for i in $(seq 1 $2); do
	ping -I $1 -W 1 -t 3 225.1.2.$i >/dev/null &
	echo $! >> "/tmp/$NM/PIDs"
    done
}

collect()
{
    print "Starting collector on $1 ..."
    tshark -w "/tmp/$NM/pcap" -lni "$@" 2>/dev/null &
    echo $! >> "/tmp/$NM/PIDs"
    sleep 2
}

# Set up a basic bridge topology, two VETH pairs with one end in the
# bridge and the other free.  Each pair is also in a separate VLAN.
#
# No IP address assignment is done in topo files, only topology setup.
#
# Topology:          ¦
#             vlan1  ¦  vlan2
#                  \ ¦ /
#       a1 -------- br0 --------- a2
#                    ¦
#       VLAN 1       ¦        VLAN 2
#
# Note: in addition to VLAN filtering, the bridge has both IGMP and MLD
#       snooping disabled, because the main purpose of these tests is to
#       verify the IPv4 and IPv6 routing functionality of SMCRoute.
#       Future tests may include verifying join/leave of groups (TODO)
topo_bridge()
{
    cat << EOF > "$NM-topo.ip"
link add br0 type bridge vlan_filtering 1 mcast_snooping 0
link add a1 type veth peer b1
link add a2 type veth peer b2
link set b1 master br0
link set b2 master br0

link set a1 up
link set b1 up
link set a2 up
link set b2 up
link set br0 up

link add link br0 vlan1 type vlan id 1
link add link br0 vlan2 type vlan id 2

link set vlan1 up
link set vlan2 up
EOF

    # Move b2 to VLAN 2
    # Set br0 as tagged member of both VLANs
    cat <<EOF > "$NM-bridge.ip"
vlan add vid 2 dev b2 pvid untagged
vlan del vid 1 dev b2

vlan add vid 1 dev br0 self
vlan add vid 2 dev br0 self
EOF

    ip     -force -batch "$NM-topo.ip"
    bridge -force -batch "$NM-bridge.ip"

    rm -f "$NM-topo.ip" "$NM-bridge.ip"
}


# Set up a basic dummy interface topology,
#
# No IP address assignment is done in topo files, only topology setup.
topo_basic()
{
    cat << EOF > "$NM-topo.ip"
link add a1 type dummy
link set a1 up
link set a1 multicast on

link add a2 type dummy
link set a2 up
link set a2 multicast on
EOF

    ip -force -batch "$NM-topo.ip"
    rm -f "$NM-topo.ip"

    return 2
}

# Same as basic topology, but with more inbound/outbound interfaces.
#
# No IP address assignment is done in topo files, only topology setup.
topo_plus()
{
    cat << EOF > "$NM-topo.ip"
link add a1 type dummy
link set a1 up
link set a1 multicast on

link add a2 type dummy
link set a2 up
link set a2 multicast on

link add a3 type dummy
link set a3 up
link set a3 multicast on

link add a4 type dummy
link set a4 up
link set a4 multicast on

link add b1 type dummy
link set b1 up
link set b1 multicast on

link add b2 type dummy
link set b2 up
link set b2 multicast on

link add b3 type dummy
link set b3 up
link set b3 multicast on

link add b4 type dummy
link set b4 up
link set b4 multicast on
EOF

    ip -force -batch "$NM-topo.ip"
    rm -f "$NM-topo.ip"
}

# Set up VLAN interfaces on top of dummy interfaces
# shellcheck disable=SC2048
topo_basic_vlan()
{
    num=$1
    shift

    i=1
    while [ $i -le "$num" ]; do
	iface=a$i
	i=$((i + 1))

	for vid in $*; do
	    ip link add "$iface.$vid" link $iface type vlan id "$vid"
	    ip link set "$iface.$vid" up
	    ip link set "$iface.$vid" multicast on
	done
    done
}

topo_isolated()
{
    left="$1"
    right="$2"
    lif=$(basename "$left")
    rif=$(basename "$right")

    touch "$left" "$right"
    PID=$$

    echo "$left"   > "/tmp/$NM/mounts"
    echo "$right" >> "/tmp/$NM/mounts"

    unshare --net="$left" -- ip link set lo up
    nsenter --net="$left" -- ip link add eth0 type veth peer "$lif"
    nsenter --net="$left" -- ip link set "$lif" netns $PID
    nsenter --net="$left" -- ip link set eth0 up
    ip link set "$lif" up

    unshare --net="$right" -- ip link set lo up
    nsenter --net="$right" -- ip link add eth0 type veth peer "$rif"
    nsenter --net="$right" -- ip link set "$rif" netns $PID
    nsenter --net="$right" -- ip link set eth0 up
    ip link set "$rif" up
}

# Same as bridge topology, but with the VETH endpoints constructed
# by the isolated topology.  We just rename the main namespace's
# bridge ports to match.
topo_isolated_bridge()
{
    left="$1"
    right="$2"
    lif=$(basename "$left")
    rif=$(basename "$right")

    topo_isolated "$@"

    echo "Creating br0, adding $lif and $rif as bridge ports"
    ip link add br0 type bridge vlan_filtering 1 mcast_snooping 0
    ip link set "$lif" master br0
    ip link set "$rif" master br0

    ip link set br0 up

    ip link add link br0 vlan1 type vlan id 1
    ip link add link br0 vlan2 type vlan id 2

    ip link set vlan1 up
    ip link set vlan2 up

    bridge vlan add vid 2 dev "$rif" pvid untagged
    bridge vlan del vid 1 dev "$rif"

    bridge vlan add vid 1 dev br0 self
    bridge vlan add vid 2 dev br0 self
}

# Variant of a variant ... this is the Multi Domain topology.  It has
# two isolated network namespaces and a shared segment connecting them.
# The intention is to emulate network setups where the same base subnet
# is reused in many places.  So that when interconnecting them a string
# of 1:1 NAT operations need to performed.  These type of setups are
# more common than one might think; factories, train cars, ... tanks.
#
# Note, the bridge is only used to connect the ends of the VETH pairs.
topo_multi()
{
    left="$1"
    right="$2"
    lif=$(basename "$left")
    rif=$(basename "$right")

    topo_isolated "$@"

    nsenter --net="$left" -- ip link add eth1 type dummy
    nsenter --net="$left" -- ip link set eth1 up
    nsenter --net="$left" -- ip link set eth1 multicast on

    nsenter --net="$right" -- ip link add eth1 type dummy
    nsenter --net="$right" -- ip link set eth1 up
    nsenter --net="$right" -- ip link set eth1 multicast on

    echo "Creating br0, adding $lif and $rif as bridge ports"
    ip link add br0 type bridge
    ip link set "$lif" master br0
    ip link set "$rif" master br0

    ip link set br0 up
}

topo_teardown()
{
    if [ -z "$NM" ]; then
	echo "NM variable unset, skippnig teardown"
	exit 1
    fi

    if [ -f "/tmp/$NM/pid" ]; then
	PID=$(cat "/tmp/$NM/pid")
	kill -9 "$PID"
    fi

    # shellcheck disable=SC2162
    if [ -f "/tmp/$NM/mounts" ]; then
        while read ln; do umount "$ln"; rm "$ln"; done < "/tmp/$NM/mounts"
    fi

    # shellcheck disable=SC2162
    if [ -f "/tmp/$NM/PIDs" ]; then
	while read ln; do kill "$ln" 2>/dev/null; done < "/tmp/$NM/PIDs"
    fi

    ip link del br0  2>/dev/null
    ip link del a1   2>/dev/null
    ip link del a2   2>/dev/null
    ip link del b1   2>/dev/null
    ip link del b2   2>/dev/null

    rm -rf "/tmp/$NM"
}

signal()
{
    echo
    if [ "$1" != "EXIT" ]; then
	print "Got signal, cleaning up"
    fi
    topo_teardown
}

# props to https://stackoverflow.com/a/2183063/1708249
trapit()
{
    func="$1" ; shift
    for sig ; do
        trap "$func $sig" "$sig"
    done
}

topo()
{
    if [ $# -lt 1 ]; then
	print "Too few arguments to topo()"
	exit 1
    fi
    t=$1
    shift

    case "$t" in
	bridge)
	    topo_bridge
	    ;;

	basic)
	    topo_basic
	    num=$?
	    case "$1" in
		vlan)
		    shift
		    topo_basic_vlan $num "$@"
		    ;;
	    esac
	    ;;

	isolated)
	    case "$1" in
		bridge)
		    shift
		    topo_isolated_bridge "$@"
		    ;;
		*)
	    	    topo_isolated "$@"
		    ;;
	    esac
	    ;;

	multi)
	    topo_multi "$@"
	    ;;

	plus)
	    topo_plus
	    ;;

	teardown)
	    topo_teardown
	    ;;
	*)
	    print "No such topology: $t"
	    exit 1
	    ;;
    esac
}

# Runs once when including lib.sh
mkdir "/tmp/$NM"
touch "/tmp/$NM/PIDs"
trapit signal INT TERM QUIT EXIT