net_common.sh revision 1.18 1 # $NetBSD: net_common.sh,v 1.18 2017/06/02 01:18:51 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" \
88 rump.netstat -rn
89 }
90
91 get_linklocal_addr()
92 {
93
94 export RUMP_SERVER=${1}
95 rump.ifconfig ${2} inet6 |
96 awk "/fe80/ {sub(/%$2/, \"\"); sub(/\\/[0-9]*/, \"\"); print \$2;}"
97 unset RUMP_SERVER
98
99 return 0
100 }
101
102 get_macaddr()
103 {
104
105 env RUMP_SERVER=${1} \
106 rump.ifconfig ${2} |awk '/address/ {print $2;}'
107 }
108
109 HTTPD_PID=./.__httpd.pid
110 start_httpd()
111 {
112 local sock=$1
113 local ip=$2
114 local backup=$RUMP_SERVER
115
116 export RUMP_SERVER=$sock
117
118 # start httpd in daemon mode
119 atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
120 /usr/libexec/httpd -P $HTTPD_PID -i $ip -b -s $(pwd)
121
122 export RUMP_SERVER=$backup
123
124 sleep 3
125 }
126
127 stop_httpd()
128 {
129
130 if [ -f $HTTPD_PID ]; then
131 kill -9 $(cat $HTTPD_PID)
132 rm -f $HTTPD_PID
133 sleep 1
134 fi
135 }
136
137 NC_PID=./.__nc.pid
138 start_nc_server()
139 {
140 local sock=$1
141 local port=$2
142 local outfile=$3
143 local proto=${4:-ipv4}
144 local backup=$RUMP_SERVER
145 local pid= opts=
146
147 export RUMP_SERVER=$sock
148
149 if [ $proto = ipv4 ]; then
150 opts="-l -4"
151 else
152 opts="-l -6"
153 fi
154
155 env LD_PRELOAD=/usr/lib/librumphijack.so \
156 nc $opts $port > $outfile &
157 pid=$!
158 echo $pid > $NC_PID
159
160 $DEBUG && rump.netstat -a -f inet
161
162 export RUMP_SERVER=$backup
163
164 sleep 1
165 }
166
167 stop_nc_server()
168 {
169
170 if [ -f $NC_PID ]; then
171 kill -9 $(cat $NC_PID)
172 rm -f $NC_PID
173 sleep 1
174 fi
175 }
176
177 BASIC_LIBS="-lrumpnet -lrumpnet_net -lrumpnet_netinet \
178 -lrumpnet_shmif -lrumpdev"
179 FS_LIBS="$BASIC_LIBS -lrumpvfs -lrumpfs_ffs"
180 CRYPTO_LIBS="$BASIC_LIBS -lrumpvfs -lrumpdev_opencrypto \
181 -lrumpkern_z -lrumpkern_crypto"
182
183 # We cannot keep variables between test phases, so need to store in files
184 _rump_server_socks=./.__socks
185 _rump_server_ifaces=./.__ifaces
186 _rump_server_buses=./.__buses
187
188 DEBUG_SYSCTL_ENTRIES="net.inet.arp.debug net.inet6.icmp6.nd6_debug \
189 net.inet.ipsec.debug"
190
191 IPSEC_KEY_DEBUG=${IPSEC_KEY_DEBUG:-false}
192
193 _rump_server_start_common()
194 {
195 local sock=$1
196 local libs=
197 local backup=$RUMP_SERVER
198
199 shift 1
200 libs="$*"
201
202 atf_check -s exit:0 rump_server $libs $sock
203
204 if $DEBUG; then
205 # Enable debugging features in the kernel
206 export RUMP_SERVER=$sock
207 for ent in $DEBUG_SYSCTL_ENTRIES; do
208 if rump.sysctl -q $ent; then
209 atf_check -s exit:0 rump.sysctl -q -w $ent=1
210 fi
211 done
212 export RUMP_SERVER=$backup
213 fi
214 if $IPSEC_KEY_DEBUG; then
215 # Enable debugging features in the kernel
216 export RUMP_SERVER=$sock
217 if rump.sysctl -q net.key.debug; then
218 atf_check -s exit:0 \
219 rump.sysctl -q -w net.key.debug=0xffff
220 fi
221 export RUMP_SERVER=$backup
222 fi
223
224 echo $sock >> $_rump_server_socks
225 $DEBUG && cat $_rump_server_socks
226 }
227
228 rump_server_start()
229 {
230 local sock=$1
231 local _libs=
232 local libs="$BASIC_LIBS"
233
234 shift 1
235 _libs="$*"
236
237 for lib in $_libs; do
238 libs="$libs -lrumpnet_$lib"
239 done
240
241 _rump_server_start_common $sock $libs
242
243 return 0
244 }
245
246 rump_server_fs_start()
247 {
248 local sock=$1
249 local _libs=
250 local libs="$FS_LIBS"
251
252 shift 1
253 _libs="$*"
254
255 for lib in $_libs; do
256 libs="$libs -lrumpnet_$lib"
257 done
258
259 _rump_server_start_common $sock $libs
260
261 return 0
262 }
263
264 rump_server_crypto_start()
265 {
266 local sock=$1
267 local _libs=
268 local libs="$CRYPTO_LIBS"
269
270 shift 1
271 _libs="$*"
272
273 for lib in $_libs; do
274 libs="$libs -lrumpnet_$lib"
275 done
276
277 _rump_server_start_common $sock $libs
278
279 return 0
280 }
281
282 rump_server_add_iface()
283 {
284 local sock=$1
285 local ifname=$2
286 local bus=$3
287 local backup=$RUMP_SERVER
288
289 export RUMP_SERVER=$sock
290 atf_check -s exit:0 rump.ifconfig $ifname create
291 atf_check -s exit:0 rump.ifconfig $ifname linkstr $bus
292 export RUMP_SERVER=$backup
293
294 echo $sock $ifname >> $_rump_server_ifaces
295 $DEBUG && cat $_rump_server_ifaces
296
297 echo $bus >> $_rump_server_buses
298 cat $_rump_server_buses |sort -u >./.__tmp
299 mv -f ./.__tmp $_rump_server_buses
300 $DEBUG && cat $_rump_server_buses
301
302 return 0
303 }
304
305 rump_server_destroy_ifaces()
306 {
307 local backup=$RUMP_SERVER
308
309 $DEBUG && cat $_rump_server_ifaces
310
311 # Try to dump states before destroying interfaces
312 for sock in $(cat $_rump_server_socks); do
313 export RUMP_SERVER=$sock
314 atf_check -s exit:0 -o ignore rump.ifconfig
315 atf_check -s exit:0 -o ignore rump.netstat -nr
316 # XXX still need hijacking
317 atf_check -s exit:0 -o ignore $HIJACKING rump.netstat -i -a
318 atf_check -s exit:0 -o ignore rump.arp -na
319 atf_check -s exit:0 -o ignore rump.ndp -na
320 atf_check -s exit:0 -o ignore $HIJACKING ifmcstat
321 done
322
323 # XXX using pipe doesn't work. See PR bin/51667
324 #cat $_rump_server_ifaces | while read sock ifname; do
325 while read sock ifname; do
326 export RUMP_SERVER=$sock
327 if rump.ifconfig -l |grep -q $ifname; then
328 atf_check -s exit:0 rump.ifconfig $ifname destroy
329 fi
330 atf_check -s exit:0 -o ignore rump.ifconfig
331 done < $_rump_server_ifaces
332 export RUMP_SERVER=$backup
333
334 return 0
335 }
336
337 rump_server_halt_servers()
338 {
339 local backup=$RUMP_SERVER
340
341 $DEBUG && cat $_rump_server_socks
342 for sock in $(cat $_rump_server_socks); do
343 env RUMP_SERVER=$sock rump.halt
344 done
345 export RUMP_SERVER=$backup
346
347 return 0
348 }
349
350 rump_server_dump_servers()
351 {
352 local backup=$RUMP_SERVER
353
354 $DEBUG && cat $_rump_server_socks
355 for sock in $(cat $_rump_server_socks); do
356 echo "### Dumping $sock"
357 export RUMP_SERVER=$sock
358 rump.ifconfig
359 rump.netstat -nr
360 # XXX still need hijacking
361 $HIJACKING rump.netstat -i -a
362 rump.arp -na
363 rump.ndp -na
364 $HIJACKING ifmcstat
365 $HIJACKING dmesg
366 done
367 export RUMP_SERVER=$backup
368
369 if [ -f rump_server.core ]; then
370 gdb -ex bt /usr/bin/rump_server rump_server.core
371 strings rump_server.core |grep panic
372 fi
373 return 0
374 }
375
376 rump_server_dump_buses()
377 {
378
379 if [ ! -f $_rump_server_buses ]; then
380 return 0
381 fi
382
383 $DEBUG && cat $_rump_server_buses
384 for bus in $(cat $_rump_server_buses); do
385 echo "### Dumping $bus"
386 shmif_dumpbus -p - $bus 2>/dev/null| tcpdump -n -e -r -
387 done
388 return 0
389 }
390
391 cleanup()
392 {
393
394 rump_server_halt_servers
395 }
396
397 dump()
398 {
399
400 rump_server_dump_servers
401 rump_server_dump_buses
402 }
403