1#
2#  Copyright (c) 2014 Spectra Logic Corporation
3#  All rights reserved.
4#
5#  Redistribution and use in source and binary forms, with or without
6#  modification, are permitted provided that the following conditions
7#  are met:
8#  1. Redistributions of source code must retain the above copyright
9#     notice, this list of conditions, and the following disclaimer,
10#     without modification.
11#  2. Redistributions in binary form must reproduce at minimum a disclaimer
12#     substantially similar to the "NO WARRANTY" disclaimer below
13#     ("Disclaimer") and any redistribution must be conditioned upon
14#     including a substantially similar Disclaimer requirement for further
15#     binary redistribution.
16#
17#  NO WARRANTY
18#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22#  HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26#  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27#  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28#  POSSIBILITY OF SUCH DAMAGES.
29#
30#  Authors: Alan Somers         (Spectra Logic Corporation)
31#
32# $FreeBSD$
33
34# Outline:
35# For each cloned interface type, do three tests
36# 1) Create and destroy it
37# 2) Create, up, and destroy it
38# 3) Create, disable IPv6 auto address assignment, up, and destroy it
39
40TESTLEN=10	# seconds
41
42atf_test_case epair_stress cleanup
43epair_stress_head()
44{
45	atf_set "descr" "Simultaneously create and destroy an epair(4)"
46	atf_set "require.user" "root"
47}
48epair_stress_body()
49{
50	if [ "$(atf_config_get ci false)" = "true" ]; then
51		atf_skip "https://bugs.freebsd.org/246443"
52	fi
53	do_stress "epair"
54}
55epair_stress_cleanup()
56{
57	cleanup_ifaces
58}
59
60atf_test_case epair_up_stress cleanup
61epair_up_stress_head()
62{
63	atf_set "descr" "Simultaneously up and detroy an epair(4)"
64	atf_set "require.user" "root"
65}
66epair_up_stress_body()
67{
68	do_up_stress "epair" "" ""
69}
70epair_up_stress_cleanup()
71{
72	cleanup_ifaces
73}
74
75atf_test_case epair_ipv6_up_stress cleanup
76epair_ipv6_up_stress_head()
77{
78	atf_set "descr" "Simultaneously up and destroy an epair(4) with IPv6"
79	atf_set "require.user" "root"
80}
81epair_ipv6_up_stress_body()
82{
83	atf_skip "Quickly panics: page fault in in6_unlink_ifa (PR 225438)"
84	do_up_stress "epair" "6" ""
85}
86epair_ipv6_up_stress_cleanup()
87{
88	cleanup_ifaces
89}
90
91atf_test_case faith_stress cleanup
92faith_stress_head()
93{
94	atf_set "descr" "Simultaneously create and destroy a faith(4)"
95	atf_set "require.user" "root"
96}
97faith_stress_body()
98{
99	do_stress "faith"
100}
101faith_stress_cleanup()
102{
103	cleanup_ifaces
104}
105
106atf_test_case faith_up_stress cleanup
107faith_up_stress_head()
108{
109	atf_set "descr" "Simultaneously up and destroy a faith(4)"
110	atf_set "require.user" "root"
111}
112faith_up_stress_body()
113{
114	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
115	do_up_stress "faith" "" ""
116}
117faith_up_stress_cleanup()
118{
119	cleanup_ifaces
120}
121
122atf_test_case faith_ipv6_up_stress cleanup
123faith_ipv6_up_stress_head()
124{
125	atf_set "descr" "Simultaneously up and destroy a faith(4) with IPv6"
126	atf_set "require.user" "root"
127}
128faith_ipv6_up_stress_body()
129{
130	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
131	do_up_stress "faith" "6" ""
132}
133faith_ipv6_up_stress_cleanup()
134{
135	cleanup_ifaces
136}
137
138atf_test_case gif_stress cleanup
139gif_stress_head()
140{
141	atf_set "descr" "Simultaneously create and destroy a gif(4)"
142	atf_set "require.user" "root"
143}
144gif_stress_body()
145{
146	do_stress "gif"
147}
148gif_stress_cleanup()
149{
150	cleanup_ifaces
151}
152
153atf_test_case gif_up_stress cleanup
154gif_up_stress_head()
155{
156	atf_set "descr" "Simultaneously up and destroy a gif(4)"
157	atf_set "require.user" "root"
158}
159gif_up_stress_body()
160{
161	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
162	do_up_stress "gif" "" "p2p"
163}
164gif_up_stress_cleanup()
165{
166	cleanup_ifaces
167}
168
169atf_test_case gif_ipv6_up_stress cleanup
170gif_ipv6_up_stress_head()
171{
172	atf_set "descr" "Simultaneously up and destroy a gif(4) with IPv6"
173	atf_set "require.user" "root"
174}
175gif_ipv6_up_stress_body()
176{
177	atf_skip "Quickly panics: rt_tables_get_rnh_ptr: fam out of bounds."
178	do_up_stress "gif" "6" "p2p"
179}
180gif_ipv6_up_stress_cleanup()
181{
182	cleanup_ifaces
183}
184
185atf_test_case lo_stress cleanup
186lo_stress_head()
187{
188	atf_set "descr" "Simultaneously create and destroy an lo(4)"
189	atf_set "require.user" "root"
190}
191lo_stress_body()
192{
193	do_stress "lo"
194}
195lo_stress_cleanup()
196{
197	cleanup_ifaces
198}
199
200atf_test_case lo_up_stress cleanup
201lo_up_stress_head()
202{
203	atf_set "descr" "Simultaneously up and destroy an lo(4)"
204	atf_set "require.user" "root"
205}
206lo_up_stress_body()
207{
208	atf_skip "Quickly panics: GPF in rtsock_routemsg"
209	do_up_stress "lo" "" ""
210}
211lo_up_stress_cleanup()
212{
213	cleanup_ifaces
214}
215
216atf_test_case lo_ipv6_up_stress cleanup
217lo_ipv6_up_stress_head()
218{
219	atf_set "descr" "Simultaneously up and destroy an lo(4) with IPv6"
220	atf_set "require.user" "root"
221}
222lo_ipv6_up_stress_body()
223{
224	atf_skip "Quickly panics: page fault in rtsock_addrmsg"
225	do_up_stress "lo" "6" ""
226}
227lo_ipv6_up_stress_cleanup()
228{
229	cleanup_ifaces
230}
231
232atf_test_case tap_stress cleanup
233tap_stress_head()
234{
235	atf_set "descr" "Simultaneously create and destroy a tap(4)"
236	atf_set "require.user" "root"
237}
238tap_stress_body()
239{
240	do_stress "tap"
241}
242tap_stress_cleanup()
243{
244	cleanup_ifaces
245}
246
247atf_test_case tap_up_stress cleanup
248tap_up_stress_head()
249{
250	atf_set "descr" "Simultaneously up and destroy a tap(4)"
251	atf_set "require.user" "root"
252}
253tap_up_stress_body()
254{
255	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
256	do_up_stress "tap" "" ""
257}
258tap_up_stress_cleanup()
259{
260	cleanup_ifaces
261}
262
263atf_test_case tap_ipv6_up_stress cleanup
264tap_ipv6_up_stress_head()
265{
266	atf_set "descr" "Simultaneously up and destroy a tap(4) with IPv6"
267	atf_set "require.user" "root"
268}
269tap_ipv6_up_stress_body()
270{
271	atf_skip "Quickly panics: if_delmulti_locked: inconsistent ifp 0xfffff80150e44000"
272	do_up_stress "tap" "6" ""
273}
274tap_ipv6_up_stress_cleanup()
275{
276	cleanup_ifaces
277}
278
279atf_test_case tun_stress cleanup
280tun_stress_head()
281{
282	atf_set "descr" "Simultaneously create and destroy a tun(4)"
283	atf_set "require.user" "root"
284}
285tun_stress_body()
286{
287	do_stress "tun"
288}
289tun_stress_cleanup()
290{
291	cleanup_ifaces
292}
293
294atf_test_case tun_up_stress cleanup
295tun_up_stress_head()
296{
297	atf_set "descr" "Simultaneously up and destroy a tun(4)"
298	atf_set "require.user" "root"
299}
300tun_up_stress_body()
301{
302	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
303	do_up_stress "tun" "" "p2p"
304}
305tun_up_stress_cleanup()
306{
307	cleanup_ifaces
308}
309
310atf_test_case tun_ipv6_up_stress cleanup
311tun_ipv6_up_stress_head()
312{
313	atf_set "descr" "Simultaneously up and destroy a tun(4) with IPv6"
314	atf_set "require.user" "root"
315}
316tun_ipv6_up_stress_body()
317{
318	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
319	do_up_stress "tun" "6" "p2p"
320}
321tun_ipv6_up_stress_cleanup()
322{
323	cleanup_ifaces
324}
325
326atf_test_case vlan_stress cleanup
327vlan_stress_head()
328{
329	atf_set "descr" "Simultaneously create and destroy a vlan(4)"
330	atf_set "require.user" "root"
331}
332vlan_stress_body()
333{
334	do_stress "vlan"
335}
336vlan_stress_cleanup()
337{
338	cleanup_ifaces
339}
340
341atf_test_case vlan_up_stress cleanup
342vlan_up_stress_head()
343{
344	atf_set "descr" "Simultaneously up and destroy a vlan(4)"
345	atf_set "require.user" "root"
346}
347vlan_up_stress_body()
348{
349	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
350	do_up_stress "vlan" "" ""
351}
352vlan_up_stress_cleanup()
353{
354	cleanup_ifaces
355}
356
357atf_test_case vlan_ipv6_up_stress cleanup
358vlan_ipv6_up_stress_head()
359{
360	atf_set "descr" "Simultaneously up and destroy a vlan(4) with IPv6"
361	atf_set "require.user" "root"
362}
363vlan_ipv6_up_stress_body()
364{
365	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
366	do_up_stress "vlan" "6" ""
367}
368vlan_ipv6_up_stress_cleanup()
369{
370	cleanup_ifaces
371}
372
373atf_test_case vmnet_stress cleanup
374vmnet_stress_head()
375{
376	atf_set "descr" "Simultaneously create and destroy a vmnet(4)"
377	atf_set "require.user" "root"
378}
379vmnet_stress_body()
380{
381	do_stress "vmnet"
382}
383vmnet_stress_cleanup()
384{
385	cleanup_ifaces
386}
387
388atf_test_case vmnet_up_stress cleanup
389vmnet_up_stress_head()
390{
391	atf_set "descr" "Simultaneously up and destroy a vmnet(4)"
392	atf_set "require.user" "root"
393}
394vmnet_up_stress_body()
395{
396	do_up_stress "vmnet" "" ""
397}
398vmnet_up_stress_cleanup()
399{
400	cleanup_ifaces
401}
402
403atf_test_case vmnet_ipv6_up_stress cleanup
404vmnet_ipv6_up_stress_head()
405{
406	atf_set "descr" "Simultaneously up and destroy a vmnet(4) with IPv6"
407	atf_set "require.user" "root"
408}
409vmnet_ipv6_up_stress_body()
410{
411	atf_skip "Quickly panics: if_freemulti: protospec not NULL"
412	do_up_stress "vmnet" "6" ""
413}
414vmnet_ipv6_up_stress_cleanup()
415{
416	cleanup_ifaces
417}
418
419atf_init_test_cases()
420{
421	atf_add_test_case epair_ipv6_up_stress
422	atf_add_test_case epair_stress
423	atf_add_test_case epair_up_stress
424	atf_add_test_case faith_ipv6_up_stress
425	atf_add_test_case faith_stress
426	atf_add_test_case faith_up_stress
427	atf_add_test_case gif_ipv6_up_stress
428	atf_add_test_case gif_stress
429	atf_add_test_case gif_up_stress
430	# Don't test lagg; it has its own test program
431	atf_add_test_case lo_ipv6_up_stress
432	atf_add_test_case lo_stress
433	atf_add_test_case lo_up_stress
434	atf_add_test_case tap_ipv6_up_stress
435	atf_add_test_case tap_stress
436	atf_add_test_case tap_up_stress
437	atf_add_test_case tun_ipv6_up_stress
438	atf_add_test_case tun_stress
439	atf_add_test_case tun_up_stress
440	atf_add_test_case vlan_ipv6_up_stress
441	atf_add_test_case vlan_stress
442	atf_add_test_case vlan_up_stress
443	atf_add_test_case vmnet_ipv6_up_stress
444	atf_add_test_case vmnet_stress
445	atf_add_test_case vmnet_up_stress
446}
447
448do_stress()
449{
450	local IFACE CREATOR_PID DESTROYER_PID
451
452	IFACE=`get_iface $1`
453
454	# First thread: create the interface
455	while true; do
456		ifconfig ${IFACE%a} create 2>/dev/null && \
457			echo -n . >> creator_count.txt
458	done &
459	CREATOR_PID=$!
460
461	# Second thread: destroy the lagg
462	while true; do
463		ifconfig $IFACE destroy 2>/dev/null && \
464			echo -n . >> destroyer_count.txt
465	done &
466	DESTROYER_PID=$!
467
468	sleep ${TESTLEN}
469	kill $CREATOR_PID
470	kill $DESTROYER_PID
471	echo "Created ${IFACE%a} `stat -f %z creator_count.txt` times."
472	echo "Destroyed it `stat -f %z destroyer_count.txt` times."
473}
474
475# Implement the up stress tests
476# Parameters
477# $1	Interface class, etc "lo" or "tap"
478# $2	"6" to enable IPv6 auto address assignment, anything else otherwise
479# $3	p2p for point to point interfaces, anything else for normal interfaces
480do_up_stress()
481{
482	local ADDR DSTADDR MASK MEAN_SLEEP_SECONDS MAX_SLEEP_USECS \
483	    IFACE IPV6 P2P SRCDIR LOOP_PID ipv6_cmd up_cmd
484
485	# Configure the interface to use an RFC5737 nonrouteable addresses
486	ADDR="192.0.2.2"
487	DSTADDR="192.0.2.3"
488	MASK="24"
489	# ifconfig takes about 10ms to run.  To increase race coverage,
490	# randomly delay the two commands relative to each other by 5ms either
491	# way.
492	MEAN_SLEEP_SECONDS=.005
493	MAX_SLEEP_USECS=10000
494
495	IFACE=`get_iface $1`
496	IPV6=$2
497	P2P=$3
498
499	SRCDIR=$( atf_get_srcdir )
500	if [ "$IPV6" = 6 ]; then
501		ipv6_cmd="true"
502	else
503		ipv6_cmd="ifconfig $IFACE inet6 ifdisabled"
504	fi
505	if [ "$P2P" = "p2p" ]; then
506		up_cmd="ifconfig $IFACE up ${ADDR}/${MASK} ${DSTADDR}"
507	else
508		up_cmd="ifconfig $IFACE up ${ADDR}/${MASK}"
509	fi
510	while true; do
511		eval "$ipv6_cmd"
512		{ sleep ${MEAN_SLEEP_SECONDS} && \
513			eval "$up_cmd" 2> /dev/null &&
514			echo -n . >> up_count.txt ; } &
515		{ ${SRCDIR}/randsleep ${MAX_SLEEP_USECS} && \
516			ifconfig $IFACE destroy &&
517			echo -n . >> destroy_count.txt ; } &
518		wait
519		ifconfig ${IFACE%a} create
520	done &
521	LOOP_PID=$!
522
523	sleep ${TESTLEN}
524	kill $LOOP_PID
525	echo "Upped ${IFACE} `stat -f %z up_count.txt` times."
526	echo "Destroyed it `stat -f %z destroy_count.txt` times."
527}
528
529# Creates a new cloned interface, registers it for cleanup, and echoes it
530# params: $1	Interface class name (tap, gif, etc)
531get_iface()
532{
533	local CLASS DEV N
534
535	CLASS=$1
536	N=0
537	while ! ifconfig ${CLASS}${N} create > /dev/null 2>&1; do
538		if [ "$N" -ge 8 ]; then
539			atf_skip "Could not create a ${CLASS} interface"
540		else
541			N=$(($N + 1))
542		fi
543	done
544	if [ ${CLASS} = "epair" ]; then
545		DEV=${CLASS}${N}a
546	else
547		DEV=${CLASS}${N}
548	fi
549	# Record the device so we can clean it up later
550	echo ${DEV} >> "devices_to_cleanup"
551	echo ${DEV}
552}
553
554
555cleanup_ifaces()
556{
557	local DEV
558
559	for DEV in `cat "devices_to_cleanup"`; do
560		ifconfig ${DEV} destroy
561	done
562	true
563}
564