]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/net/if_clone_test.sh
contrib/tzdata: import tzdata 2022e
[FreeBSD/FreeBSD.git] / tests / sys / net / if_clone_test.sh
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
40 TESTLEN=10      # seconds
41
42 atf_test_case epair_stress cleanup
43 epair_stress_head()
44 {
45         atf_set "descr" "Simultaneously create and destroy an epair(4)"
46         atf_set "require.user" "root"
47 }
48 epair_stress_body()
49 {
50         do_stress "epair"
51 }
52 epair_stress_cleanup()
53 {
54         cleanup_ifaces
55 }
56
57 atf_test_case epair_up_stress cleanup
58 epair_up_stress_head()
59 {
60         atf_set "descr" "Simultaneously up and detroy an epair(4)"
61         atf_set "require.user" "root"
62 }
63 epair_up_stress_body()
64 {
65         do_up_stress "epair" "" ""
66 }
67 epair_up_stress_cleanup()
68 {
69         cleanup_ifaces
70 }
71
72 atf_test_case epair_destroy_race cleanup
73 epair_destroy_race_head()
74 {
75         atf_set "descr" "Race if_detach() and if_vmove()"
76         atf_set "require.user" "root"
77 }
78 epair_destroy_race_body()
79 {
80         for i in `seq 1 10`
81         do
82                 epair_a=$(ifconfig epair create)
83                 echo $epair_a >> devices_to_cleanup
84                 epair_b=${epair_a%a}b
85
86                 jail -c vnet name="epair_destroy" nopersist path=/ \
87                   host.hostname="epair_destroy" vnet.interface="$epair_b" \
88                   command=sh -c "ifconfig $epair_b 192.0.2.1/24; sleep 0.1"&
89                 pid=$!
90                 ifconfig "$epair_a" destroy
91                 wait $pid
92         done
93         true
94 }
95 epair_destroy_race_cleanup()
96 {
97         cleanup_ifaces
98 }
99
100 atf_test_case epair_ipv6_up_stress cleanup
101 epair_ipv6_up_stress_head()
102 {
103         atf_set "descr" "Simultaneously up and destroy an epair(4) with IPv6"
104         atf_set "require.user" "root"
105 }
106 epair_ipv6_up_stress_body()
107 {
108         do_up_stress "epair" "6" ""
109 }
110 epair_ipv6_up_stress_cleanup()
111 {
112         cleanup_ifaces
113 }
114
115 atf_test_case faith_stress cleanup
116 faith_stress_head()
117 {
118         atf_set "descr" "Simultaneously create and destroy a faith(4)"
119         atf_set "require.user" "root"
120 }
121 faith_stress_body()
122 {
123         do_stress "faith"
124 }
125 faith_stress_cleanup()
126 {
127         cleanup_ifaces
128 }
129
130 atf_test_case faith_up_stress cleanup
131 faith_up_stress_head()
132 {
133         atf_set "descr" "Simultaneously up and destroy a faith(4)"
134         atf_set "require.user" "root"
135 }
136 faith_up_stress_body()
137 {
138         do_up_stress "faith" "" ""
139 }
140 faith_up_stress_cleanup()
141 {
142         cleanup_ifaces
143 }
144
145 atf_test_case faith_ipv6_up_stress cleanup
146 faith_ipv6_up_stress_head()
147 {
148         atf_set "descr" "Simultaneously up and destroy a faith(4) with IPv6"
149         atf_set "require.user" "root"
150 }
151 faith_ipv6_up_stress_body()
152 {
153         do_up_stress "faith" "6" ""
154 }
155 faith_ipv6_up_stress_cleanup()
156 {
157         cleanup_ifaces
158 }
159
160 atf_test_case gif_stress cleanup
161 gif_stress_head()
162 {
163         atf_set "descr" "Simultaneously create and destroy a gif(4)"
164         atf_set "require.user" "root"
165 }
166 gif_stress_body()
167 {
168         do_stress "gif"
169 }
170 gif_stress_cleanup()
171 {
172         cleanup_ifaces
173 }
174
175 atf_test_case gif_up_stress cleanup
176 gif_up_stress_head()
177 {
178         atf_set "descr" "Simultaneously up and destroy a gif(4)"
179         atf_set "require.user" "root"
180 }
181 gif_up_stress_body()
182 {
183         do_up_stress "gif" "" "p2p"
184 }
185 gif_up_stress_cleanup()
186 {
187         cleanup_ifaces
188 }
189
190 atf_test_case gif_ipv6_up_stress cleanup
191 gif_ipv6_up_stress_head()
192 {
193         atf_set "descr" "Simultaneously up and destroy a gif(4) with IPv6"
194         atf_set "require.user" "root"
195 }
196 gif_ipv6_up_stress_body()
197 {
198         do_up_stress "gif" "6" "p2p"
199 }
200 gif_ipv6_up_stress_cleanup()
201 {
202         cleanup_ifaces
203 }
204
205 atf_test_case lo_stress cleanup
206 lo_stress_head()
207 {
208         atf_set "descr" "Simultaneously create and destroy an lo(4)"
209         atf_set "require.user" "root"
210 }
211 lo_stress_body()
212 {
213         do_stress "lo"
214 }
215 lo_stress_cleanup()
216 {
217         cleanup_ifaces
218 }
219
220 atf_test_case lo_up_stress cleanup
221 lo_up_stress_head()
222 {
223         atf_set "descr" "Simultaneously up and destroy an lo(4)"
224         atf_set "require.user" "root"
225 }
226 lo_up_stress_body()
227 {
228         do_up_stress "lo" "" ""
229 }
230 lo_up_stress_cleanup()
231 {
232         cleanup_ifaces
233 }
234
235 atf_test_case lo_ipv6_up_stress cleanup
236 lo_ipv6_up_stress_head()
237 {
238         atf_set "descr" "Simultaneously up and destroy an lo(4) with IPv6"
239         atf_set "require.user" "root"
240 }
241 lo_ipv6_up_stress_body()
242 {
243         do_up_stress "lo" "6" ""
244 }
245 lo_ipv6_up_stress_cleanup()
246 {
247         cleanup_ifaces
248 }
249
250 atf_test_case tap_stress cleanup
251 tap_stress_head()
252 {
253         atf_set "descr" "Simultaneously create and destroy a tap(4)"
254         atf_set "require.user" "root"
255 }
256 tap_stress_body()
257 {
258         do_stress "tap"
259 }
260 tap_stress_cleanup()
261 {
262         cleanup_ifaces
263 }
264
265 atf_test_case tap_up_stress cleanup
266 tap_up_stress_head()
267 {
268         atf_set "descr" "Simultaneously up and destroy a tap(4)"
269         atf_set "require.user" "root"
270 }
271 tap_up_stress_body()
272 {
273         do_up_stress "tap" "" ""
274 }
275 tap_up_stress_cleanup()
276 {
277         cleanup_ifaces
278 }
279
280 atf_test_case tap_ipv6_up_stress cleanup
281 tap_ipv6_up_stress_head()
282 {
283         atf_set "descr" "Simultaneously up and destroy a tap(4) with IPv6"
284         atf_set "require.user" "root"
285 }
286 tap_ipv6_up_stress_body()
287 {
288         atf_skip "Quickly panics: if_delmulti_locked: inconsistent ifp 0xfffff80150e44000"
289         do_up_stress "tap" "6" ""
290 }
291 tap_ipv6_up_stress_cleanup()
292 {
293         cleanup_ifaces
294 }
295
296 atf_test_case tun_stress cleanup
297 tun_stress_head()
298 {
299         atf_set "descr" "Simultaneously create and destroy a tun(4)"
300         atf_set "require.user" "root"
301 }
302 tun_stress_body()
303 {
304         do_stress "tun"
305 }
306 tun_stress_cleanup()
307 {
308         cleanup_ifaces
309 }
310
311 atf_test_case tun_up_stress cleanup
312 tun_up_stress_head()
313 {
314         atf_set "descr" "Simultaneously up and destroy a tun(4)"
315         atf_set "require.user" "root"
316 }
317 tun_up_stress_body()
318 {
319         do_up_stress "tun" "" "p2p"
320 }
321 tun_up_stress_cleanup()
322 {
323         cleanup_ifaces
324 }
325
326 atf_test_case tun_ipv6_up_stress cleanup
327 tun_ipv6_up_stress_head()
328 {
329         atf_set "descr" "Simultaneously up and destroy a tun(4) with IPv6"
330         atf_set "require.user" "root"
331 }
332 tun_ipv6_up_stress_body()
333 {
334         do_up_stress "tun" "6" "p2p"
335 }
336 tun_ipv6_up_stress_cleanup()
337 {
338         cleanup_ifaces
339 }
340
341 atf_test_case vlan_stress cleanup
342 vlan_stress_head()
343 {
344         atf_set "descr" "Simultaneously create and destroy a vlan(4)"
345         atf_set "require.user" "root"
346 }
347 vlan_stress_body()
348 {
349         do_stress "vlan"
350 }
351 vlan_stress_cleanup()
352 {
353         cleanup_ifaces
354 }
355
356 atf_test_case vlan_up_stress cleanup
357 vlan_up_stress_head()
358 {
359         atf_set "descr" "Simultaneously up and destroy a vlan(4)"
360         atf_set "require.user" "root"
361 }
362 vlan_up_stress_body()
363 {
364         do_up_stress "vlan" "" ""
365 }
366 vlan_up_stress_cleanup()
367 {
368         cleanup_ifaces
369 }
370
371 atf_test_case vlan_ipv6_up_stress cleanup
372 vlan_ipv6_up_stress_head()
373 {
374         atf_set "descr" "Simultaneously up and destroy a vlan(4) with IPv6"
375         atf_set "require.user" "root"
376 }
377 vlan_ipv6_up_stress_body()
378 {
379         do_up_stress "vlan" "6" ""
380 }
381 vlan_ipv6_up_stress_cleanup()
382 {
383         cleanup_ifaces
384 }
385
386 atf_test_case vmnet_stress cleanup
387 vmnet_stress_head()
388 {
389         atf_set "descr" "Simultaneously create and destroy a vmnet(4)"
390         atf_set "require.user" "root"
391 }
392 vmnet_stress_body()
393 {
394         do_stress "vmnet"
395 }
396 vmnet_stress_cleanup()
397 {
398         cleanup_ifaces
399 }
400
401 atf_test_case vmnet_up_stress cleanup
402 vmnet_up_stress_head()
403 {
404         atf_set "descr" "Simultaneously up and destroy a vmnet(4)"
405         atf_set "require.user" "root"
406 }
407 vmnet_up_stress_body()
408 {
409         do_up_stress "vmnet" "" ""
410 }
411 vmnet_up_stress_cleanup()
412 {
413         cleanup_ifaces
414 }
415
416 atf_test_case vmnet_ipv6_up_stress cleanup
417 vmnet_ipv6_up_stress_head()
418 {
419         atf_set "descr" "Simultaneously up and destroy a vmnet(4) with IPv6"
420         atf_set "require.user" "root"
421 }
422 vmnet_ipv6_up_stress_body()
423 {
424         do_up_stress "vmnet" "6" ""
425 }
426 vmnet_ipv6_up_stress_cleanup()
427 {
428         cleanup_ifaces
429 }
430
431 atf_init_test_cases()
432 {
433         atf_add_test_case epair_ipv6_up_stress
434         atf_add_test_case epair_stress
435         atf_add_test_case epair_up_stress
436         atf_add_test_case epair_destroy_race
437         atf_add_test_case faith_ipv6_up_stress
438         atf_add_test_case faith_stress
439         atf_add_test_case faith_up_stress
440         atf_add_test_case gif_ipv6_up_stress
441         atf_add_test_case gif_stress
442         atf_add_test_case gif_up_stress
443         # Don't test lagg; it has its own test program
444         atf_add_test_case lo_ipv6_up_stress
445         atf_add_test_case lo_stress
446         atf_add_test_case lo_up_stress
447         atf_add_test_case tap_ipv6_up_stress
448         atf_add_test_case tap_stress
449         atf_add_test_case tap_up_stress
450         atf_add_test_case tun_ipv6_up_stress
451         atf_add_test_case tun_stress
452         atf_add_test_case tun_up_stress
453         atf_add_test_case vlan_ipv6_up_stress
454         atf_add_test_case vlan_stress
455         atf_add_test_case vlan_up_stress
456         atf_add_test_case vmnet_ipv6_up_stress
457         atf_add_test_case vmnet_stress
458         atf_add_test_case vmnet_up_stress
459 }
460
461 do_stress()
462 {
463         local IFACE CREATOR_PID DESTROYER_PID
464
465         IFACE=`get_iface $1`
466
467         # First thread: create the interface
468         while true; do
469                 ifconfig ${IFACE%a} create 2>/dev/null && \
470                         echo -n . >> creator_count.txt
471         done &
472         CREATOR_PID=$!
473
474         # Second thread: destroy the lagg
475         while true; do
476                 ifconfig $IFACE destroy 2>/dev/null && \
477                         echo -n . >> destroyer_count.txt
478         done &
479         DESTROYER_PID=$!
480
481         sleep ${TESTLEN}
482         kill $CREATOR_PID
483         kill $DESTROYER_PID
484         echo "Created ${IFACE%a} `stat -f %z creator_count.txt` times."
485         echo "Destroyed it `stat -f %z destroyer_count.txt` times."
486 }
487
488 # Implement the up stress tests
489 # Parameters
490 # $1    Interface class, etc "lo" or "tap"
491 # $2    "6" to enable IPv6 auto address assignment, anything else otherwise
492 # $3    p2p for point to point interfaces, anything else for normal interfaces
493 do_up_stress()
494 {
495         local ADDR DSTADDR MASK MEAN_SLEEP_SECONDS MAX_SLEEP_USECS \
496             IFACE IPV6 P2P SRCDIR LOOP_PID ipv6_cmd up_cmd
497
498         # Configure the interface to use an RFC5737 nonrouteable addresses
499         ADDR="192.0.2.2"
500         DSTADDR="192.0.2.3"
501         MASK="24"
502         # ifconfig takes about 10ms to run.  To increase race coverage,
503         # randomly delay the two commands relative to each other by 5ms either
504         # way.
505         MEAN_SLEEP_SECONDS=.005
506         MAX_SLEEP_USECS=10000
507
508         IFACE=`get_iface $1`
509         IPV6=$2
510         P2P=$3
511
512         SRCDIR=$( atf_get_srcdir )
513         if [ "$IPV6" = 6 ]; then
514                 ipv6_cmd="true"
515         else
516                 ipv6_cmd="ifconfig $IFACE inet6 ifdisabled"
517         fi
518         if [ "$P2P" = "p2p" ]; then
519                 up_cmd="ifconfig $IFACE up ${ADDR}/${MASK} ${DSTADDR}"
520         else
521                 up_cmd="ifconfig $IFACE up ${ADDR}/${MASK}"
522         fi
523         while true; do
524                 eval "$ipv6_cmd"
525                 { sleep ${MEAN_SLEEP_SECONDS} && \
526                         eval "$up_cmd" 2> /dev/null &&
527                         echo -n . >> up_count.txt ; } &
528                 { ${SRCDIR}/randsleep ${MAX_SLEEP_USECS} && \
529                         ifconfig $IFACE destroy &&
530                         echo -n . >> destroy_count.txt ; } &
531                 wait
532                 ifconfig ${IFACE%a} create
533         done &
534         LOOP_PID=$!
535
536         sleep ${TESTLEN}
537         kill $LOOP_PID
538         echo "Upped ${IFACE} `stat -f %z up_count.txt` times."
539         echo "Destroyed it `stat -f %z destroy_count.txt` times."
540 }
541
542 # Creates a new cloned interface, registers it for cleanup, and echoes it
543 # params: $1    Interface class name (tap, gif, etc)
544 get_iface()
545 {
546         local CLASS DEV N
547
548         CLASS=$1
549         N=0
550         while ! ifconfig ${CLASS}${N} create > /dev/null 2>&1; do
551                 if [ "$N" -ge 8 ]; then
552                         atf_skip "Could not create a ${CLASS} interface"
553                 else
554                         N=$(($N + 1))
555                 fi
556         done
557         if [ ${CLASS} = "epair" ]; then
558                 DEV=${CLASS}${N}a
559         else
560                 DEV=${CLASS}${N}
561         fi
562         # Record the device so we can clean it up later
563         echo ${DEV} >> "devices_to_cleanup"
564         echo ${DEV}
565 }
566
567
568 cleanup_ifaces()
569 {
570         local DEV
571
572         for DEV in `cat "devices_to_cleanup"`; do
573                 ifconfig ${DEV} destroy
574         done
575         true
576 }