xref: /freebsd/tests/sys/kern/sendfile_test.sh (revision 9e5787d2284e187abb5b654d924394a65772e004)
1# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
2#
3# Copyright (c) 2020 Netflix, Inc.
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# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28#
29# These tests exercise a few basic cases for the sendfile() syscall:
30# - successful operation.
31# - sendfile() starts an async disk read but that async I/O fails.
32# - sendfile() fails to read an indirect block and thus cannot
33#   even start an async I/O.
34#
35# In all cases we request some read ahead in addition to
36# the data to be sent to the socket.
37#
38
39MD_DEVS="md.devs"
40MNT=/mnt
41FILE=$MNT/file
42HELPER="$(atf_get_srcdir)/sendfile_helper"
43BSIZE=4096
44
45atf_test_case io_success cleanup
46io_success_head()
47{
48	atf_set "descr" "sendfile where all disk I/O succeeds"
49	atf_set "require.user" "root"
50	atf_set "timeout" 15
51}
52io_success_body()
53{
54	md=$(alloc_md)
55	common_body_setup $md
56
57	atf_check $HELPER $FILE 0 0x10000 0x10000
58}
59io_success_cleanup()
60{
61	common_cleanup
62}
63
64atf_test_case io_fail_sync cleanup
65io_fail_sync_head()
66{
67	atf_set "descr" "sendfile where we fail to start async I/O"
68	atf_set "require.user" "root"
69	atf_set "timeout" 15
70}
71io_fail_sync_body()
72{
73	md=$(alloc_md)
74	common_body_setup $md
75
76	atf_check gnop configure -r 100 -e 5 ${md}.nop
77	atf_check -s exit:3 -e ignore $HELPER $FILE $((12 * $BSIZE)) $BSIZE 0x10000
78}
79io_fail_sync_cleanup()
80{
81	common_cleanup
82}
83
84atf_test_case io_fail_async cleanup
85io_fail_async_head()
86{
87	atf_set "descr" "sendfile where an async I/O fails"
88	atf_set "require.user" "root"
89	atf_set "timeout" 15
90}
91io_fail_async_body()
92{
93	md=$(alloc_md)
94	common_body_setup $md
95
96	atf_check gnop configure -r 100 -e 5 ${md}.nop
97	atf_check -s exit:2 -e ignore $HELPER $FILE 0 $BSIZE 0x10000
98}
99io_fail_async_cleanup()
100{
101	common_cleanup
102}
103
104
105atf_init_test_cases()
106{
107	atf_add_test_case io_success
108	atf_add_test_case io_fail_sync
109	atf_add_test_case io_fail_async
110}
111
112alloc_md()
113{
114	local md
115
116	md=$(mdconfig -a -t swap -s 256M) || atf_fail "mdconfig -a failed"
117	echo ${md} >> $MD_DEVS
118	echo ${md}
119}
120
121common_body_setup()
122{
123	us=$1
124
125	atf_check -o ignore -e ignore newfs -b $BSIZE -U -j /dev/${us}
126	atf_check mount /dev/${us} $MNT
127	atf_check -e ignore dd if=/dev/zero of=$FILE bs=1m count=1
128	atf_check umount /mnt
129
130	load_gnop
131	atf_check gnop create /dev/${us}
132	atf_check mount /dev/${us}.nop $MNT
133	atf_check -o ignore ls -l $MNT/file
134}
135
136common_cleanup()
137{
138	umount -f $MNT
139	if [ -f "$MD_DEVS" ]; then
140		while read test_md; do
141			gnop destroy -f ${test_md}.nop 2>/dev/null
142			mdconfig -d -u $test_md 2>/dev/null
143		done < $MD_DEVS
144		rm $MD_DEVS
145	fi
146
147	true
148}
149
150load_gnop()
151{
152	if ! kldstat -q -m g_nop; then
153		geom nop load || atf_skip "could not load module for geom nop"
154	fi
155}
156