net_common.sh revision 1.26 1 # $NetBSD: net_common.sh,v 1.26 2018/02/01 05:22:01 ozaki-r Exp $
2 #
3 # Copyright (c) 2016 Internet Initiative Japan Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 #
29 # Common utility functions for tests/net
30 #
31
32 HIJACKING="env LD_PRELOAD=/usr/lib/librumphijack.so \
33 RUMPHIJACK=path=/rump,socket=all:nolocal,sysctl=yes"
34 ONEDAYISH="(23h5[0-9]m|1d0h0m)[0-9]+s ?"
35
36 extract_new_packets()
37 {
38 local bus=$1
39 local old=./.__old
40
41 if [ ! -f $old ]; then
42 old=/dev/null
43 fi
44
45 shmif_dumpbus -p - $bus 2>/dev/null |
46 tcpdump -n -e -r - 2>/dev/null > ./.__new
47 diff -u $old ./.__new | grep '^+' | cut -d '+' -f 2 > ./.__diff
48 mv -f ./.__new ./.__old
49 cat ./.__diff
50 }
51
52 check_route()
53 {
54 local target=$1
55 local gw=$2
56 local flags=${3:-\.\+}
57 local ifname=${4:-\.\+}
58
59 target=$(echo $target | sed 's/\./\\./g')
60 if [ "$gw" = "" ]; then
61 gw=".+"
62 else
63 gw=$(echo $gw | sed 's/\./\\./g')
64 fi
65
66 atf_check -s exit:0 -e ignore \
67 -o match:"^$target +$gw +$flags +- +- +.+ +$ifname" \
68 rump.netstat -rn
69 }
70
71 check_route_flags()
72 {
73
74 check_route "$1" "" "$2" ""
75 }
76
77 check_route_gw()
78 {
79
80 check_route "$1" "$2" "" ""
81 }
82
83 check_route_no_entry()
84 {
85 local target=$(echo "$1" | sed 's/\./\\./g')
86
87 atf_check -s exit:0 -e ignore -o not-match:"^$target" rump.netstat -rn
88 }
89
90 get_linklocal_addr()
91 {
92
93 RUMP_SERVER=${1} rump.ifconfig ${2} inet6 |
94 awk "/fe80/ {sub(/%$2/, \"\"); sub(/\\/[0-9]*/, \"\"); print \$2;}"
95
96 return 0
97 }
98
99 get_macaddr()
100 {
101
102 RUMP_SERVER=${1} rump.ifconfig ${2} | awk '/address/ {print $2;}'
103 }
104
105 HTTPD_PID=./.__httpd.pid
106 start_httpd()
107 {
108 local sock=$1
109 local ip=$2
110 local backup=$RUMP_SERVER
111
112 export RUMP_SERVER=$sock
113
114 # start httpd in daemon mode
115 atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
116 /usr/libexec/httpd -P $HTTPD_PID -i $ip -b -s $(pwd)
117
118 export RUMP_SERVER=$backup
119
120 sleep 3
121 }
122
123 stop_httpd()
124 {
125
126 if [ -f $HTTPD_PID ]; then
127 kill -9 $(cat $HTTPD_PID)
128 rm -f $HTTPD_PID
129 sleep 1
130 fi
131 }
132
133 NC_PID=./.__nc.pid
134 start_nc_server()
135 {
136 local sock=$1
137 local port=$2
138 local outfile=$3
139 local proto=${4:-ipv4}
140 local backup=$RUMP_SERVER
141 local opts=
142
143 export RUMP_SERVER=$sock
144
145 if [ $proto = ipv4 ]; then
146 opts="-l -4"
147 else
148 opts="-l -6"
149 fi
150
151 env LD_PRELOAD=/usr/lib/librumphijack.so nc $opts $port > $outfile &
152 echo $! > $NC_PID
153
154 if [ $proto = ipv4 ]; then
155 $DEBUG && rump.netstat -a -f inet
156 else
157 $DEBUG && rump.netstat -a -f inet6
158 fi
159
160 export RUMP_SERVER=$backup
161
162 sleep 1
163 }
164
165 stop_nc_server()
166 {
167
168 if [ -f $NC_PID ]; then
169 kill -9 $(cat $NC_PID)
170 rm -f $NC_PID
171 sleep 1
172 fi
173 }
174
175 BASIC_LIBS="-lrumpnet -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpdev"
176 FS_LIBS="$BASIC_LIBS -lrumpvfs -lrumpfs_ffs"
177 CRYPTO_LIBS="$BASIC_LIBS -lrumpvfs -lrumpdev_opencrypto \
178 -lrumpkern_z -lrumpkern_crypto"
179 NPF_LIBS="$BASIC_LIBS -lrumpvfs -lrumpdev_bpf -lrumpnet_npf"
180
181 # We cannot keep variables between test phases, so need to store in files
182 _rump_server_socks=./.__socks
183 _rump_server_ifaces=./.__ifaces
184 _rump_server_buses=./.__buses
185
186 DEBUG_SYSCTL_ENTRIES="net.inet.arp.debug net.inet6.icmp6.nd6_debug \
187 net.inet.ipsec.debug"
188
189 IPSEC_KEY_DEBUG=${IPSEC_KEY_DEBUG:-false}
190
191 _rump_server_start_common()
192 {
193 local sock=$1
194 local backup=$RUMP_SERVER
195
196 shift 1
197
198 atf_check -s exit:0 rump_server "$@" "$sock"
199
200 if $DEBUG; then
201 # Enable debugging features in the kernel
202 export RUMP_SERVER=$sock
203 for ent in $DEBUG_SYSCTL_ENTRIES; do
204 if rump.sysctl -q $ent; then
205 atf_check -s exit:0 rump.sysctl -q -w $ent=1
206 fi
207 done
208 export RUMP_SERVER=$backup
209 fi
210 if $IPSEC_KEY_DEBUG; then
211 # Enable debugging features in the kernel
212 export RUMP_SERVER=$sock
213 if rump.sysctl -q net.key.debug; then
214 atf_check -s exit:0 \
215 rump.sysctl -q -w net.key.debug=0xffff
216 fi
217 export RUMP_SERVER=$backup
218 fi
219
220 echo $sock >> $_rump_server_socks
221 $DEBUG && cat $_rump_server_socks
222 }
223
224 rump_server_start()
225 {
226 local sock=$1
227 local lib=
228 local libs="$BASIC_LIBS"
229
230 shift 1
231
232 for lib
233 do
234 libs="$libs -lrumpnet_$lib"
235 done
236
237 _rump_server_start_common $sock $libs
238
239 return 0
240 }
241
242 rump_server_fs_start()
243 {
244 local sock=$1
245 local lib=
246 local libs="$FS_LIBS"
247
248 shift 1
249
250 for lib
251 do
252 libs="$libs -lrumpnet_$lib"
253 done
254
255 _rump_server_start_common $sock $libs
256
257 return 0
258 }
259
260 rump_server_crypto_start()
261 {
262 local sock=$1
263 local lib=
264 local libs="$CRYPTO_LIBS"
265
266 shift 1
267
268 for lib
269 do
270 libs="$libs -lrumpnet_$lib"
271 done
272
273 _rump_server_start_common $sock $libs
274
275 return 0
276 }
277
278 rump_server_npf_start()
279 {
280 local sock=$1
281 local lib=
282 local libs="$NPF_LIBS"
283
284 shift 1
285
286 for lib
287 do
288 libs="$libs -lrumpnet_$lib"
289 done
290
291 _rump_server_start_common $sock $libs
292
293 return 0
294 }
295
296 rump_server_add_iface()
297 {
298 local sock=$1
299 local ifname=$2
300 local bus=$3
301 local backup=$RUMP_SERVER
302
303 export RUMP_SERVER=$sock
304 atf_check -s exit:0 rump.ifconfig $ifname create
305 atf_check -s exit:0 rump.ifconfig $ifname linkstr $bus
306 export RUMP_SERVER=$backup
307
308 echo $sock $ifname >> $_rump_server_ifaces
309 $DEBUG && cat $_rump_server_ifaces
310
311 echo $bus >> $_rump_server_buses
312 cat $_rump_server_buses |sort -u >./.__tmp
313 mv -f ./.__tmp $_rump_server_buses
314 $DEBUG && cat $_rump_server_buses
315
316 return 0
317 }
318
319 rump_server_destroy_ifaces()
320 {
321 local backup=$RUMP_SERVER
322
323 $DEBUG && cat $_rump_server_ifaces
324
325 # Try to dump states before destroying interfaces
326 for sock in $(cat $_rump_server_socks); do
327 export RUMP_SERVER=$sock
328 atf_check -s exit:0 -o ignore rump.ifconfig
329 atf_check -s exit:0 -o ignore rump.netstat -nr
330 # XXX still need hijacking
331 atf_check -s exit:0 -o ignore $HIJACKING rump.netstat -nai
332 atf_check -s exit:0 -o ignore rump.arp -na
333 atf_check -s exit:0 -o ignore rump.ndp -na
334 atf_check -s exit:0 -o ignore $HIJACKING ifmcstat
335 done
336
337 # XXX using pipe doesn't work. See PR bin/51667
338 #cat $_rump_server_ifaces | while read sock ifname; do
339 while read sock ifname; do
340 export RUMP_SERVER=$sock
341 if rump.ifconfig -l |grep -q $ifname; then
342 atf_check -s exit:0 rump.ifconfig $ifname destroy
343 fi
344 atf_check -s exit:0 -o ignore rump.ifconfig
345 done < $_rump_server_ifaces
346 export RUMP_SERVER=$backup
347
348 return 0
349 }
350
351 rump_server_halt_servers()
352 {
353 local backup=$RUMP_SERVER
354
355 $DEBUG && cat $_rump_server_socks
356 for sock in $(cat $_rump_server_socks); do
357 env RUMP_SERVER=$sock rump.halt
358 done
359 export RUMP_SERVER=$backup
360
361 return 0
362 }
363
364 rump_server_dump_servers()
365 {
366 local backup=$RUMP_SERVER
367
368 $DEBUG && cat $_rump_server_socks
369 for sock in $(cat $_rump_server_socks); do
370 echo "### Dumping $sock"
371 export RUMP_SERVER=$sock
372 rump.ifconfig -av
373 rump.netstat -nr
374 # XXX still need hijacking
375 $HIJACKING rump.netstat -nai
376 rump.arp -na
377 rump.ndp -na
378 $HIJACKING ifmcstat
379 $HIJACKING dmesg
380 done
381 export RUMP_SERVER=$backup
382
383 if [ -f rump_server.core ]; then
384 gdb -ex bt /usr/bin/rump_server rump_server.core
385 strings rump_server.core |grep panic
386 fi
387 return 0
388 }
389
390 rump_server_dump_buses()
391 {
392
393 if [ ! -f $_rump_server_buses ]; then
394 return 0
395 fi
396
397 $DEBUG && cat $_rump_server_buses
398 for bus in $(cat $_rump_server_buses); do
399 echo "### Dumping $bus"
400 shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
401 done
402 return 0
403 }
404
405 cleanup()
406 {
407
408 rump_server_halt_servers
409 }
410
411 dump()
412 {
413
414 rump_server_dump_servers
415 rump_server_dump_buses
416 }
417
418 skip_if_qemu()
419 {
420 if sysctl machdep.cpu_brand 2>/dev/null | grep QEMU >/dev/null 2>&1
421 then
422 atf_skip "unreliable under qemu, skip until PR kern/43997 fixed"
423 fi
424 }
425
426 test_create_destroy_common()
427 {
428 local sock=$1
429 local ifname=$2
430 local test_address=${3:-false}
431 local ipv4="10.0.0.1/24"
432 local ipv6="fc00::1"
433
434 export RUMP_SERVER=$sock
435
436 atf_check -s exit:0 rump.ifconfig $ifname create
437 atf_check -s exit:0 rump.ifconfig $ifname destroy
438
439 atf_check -s exit:0 rump.ifconfig $ifname create
440 atf_check -s exit:0 rump.ifconfig $ifname up
441 atf_check -s exit:0 rump.ifconfig $ifname down
442 atf_check -s exit:0 rump.ifconfig $ifname destroy
443
444 # Destroy while UP
445 atf_check -s exit:0 rump.ifconfig $ifname create
446 atf_check -s exit:0 rump.ifconfig $ifname up
447 atf_check -s exit:0 rump.ifconfig $ifname destroy
448
449 if ! $test_address; then
450 return
451 fi
452
453 # With an IPv4 address
454 atf_check -s exit:0 rump.ifconfig $ifname create
455 atf_check -s exit:0 rump.ifconfig $ifname inet $ipv4
456 atf_check -s exit:0 rump.ifconfig $ifname up
457 atf_check -s exit:0 rump.ifconfig $ifname destroy
458
459 # With an IPv6 address
460 atf_check -s exit:0 rump.ifconfig $ifname create
461 atf_check -s exit:0 rump.ifconfig $ifname inet6 $ipv6
462 atf_check -s exit:0 rump.ifconfig $ifname up
463 atf_check -s exit:0 rump.ifconfig $ifname destroy
464
465 unset RUMP_SERVER
466 }
467