diff --git a/tests/sys/kern/sendfile_helper.c b/tests/sys/kern/sendfile_helper.c index 6a3b4a1ac880..703b04fdea6c 100644 --- a/tests/sys/kern/sendfile_helper.c +++ b/tests/sys/kern/sendfile_helper.c @@ -1,156 +1,177 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2020 Netflix, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include static char buf[1024*1024]; ssize_t readlen; static volatile bool read_done = false; static int tcp_socketpair(int *sv) { struct sockaddr_in sin = { .sin_len = sizeof(struct sockaddr_in), .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK), }; int flags; int ls; ls = socket(PF_INET, SOCK_STREAM, 0); if (ls < 0) err(1, "socket ls"); if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, sizeof(int)) < 0) err(1, "SO_REUSEADDR"); if (bind(ls, (struct sockaddr *)&sin, sizeof(sin)) < 0) err(1, "bind ls"); if (getsockname(ls, (struct sockaddr *)&sin, &(socklen_t){ sizeof(sin) }) < 0) err(1, "getsockname"); if (listen(ls, 5) < 0) err(1, "listen ls"); sv[0] = socket(PF_INET, SOCK_STREAM, 0); if (sv[0] < 0) err(1, "socket cs"); flags = fcntl(sv[0], F_GETFL); flags |= O_NONBLOCK; if (fcntl(sv[0], F_SETFL, flags) == -1) err(1, "fcntl +O_NONBLOCK"); if (connect(sv[0], (void *)&sin, sizeof(sin)) != -1 || errno != EINPROGRESS) err(1, "connect cs"); sv[1] = accept(ls, NULL, 0); if (sv[1] < 0) err(1, "accept ls"); flags &= ~O_NONBLOCK; if (fcntl(sv[0], F_SETFL, flags) == -1) err(1, "fcntl -O_NONBLOCK"); close(ls); return (0); } static void * receiver(void *arg) { int s = *(int *)arg; ssize_t rv; do { rv = read(s, buf, sizeof(buf)); if (rv == -1) err(2, "read receiver"); if (rv == 0) break; readlen -= rv; } while (readlen != 0); read_done = true; return NULL; } +static void +usage(void) +{ + errx(1, "usage: %s [-u] ", getprogname()); +} + int main(int argc, char **argv) { pthread_t pt; off_t start; - int fd, ss[2], flags, error; + int ch, fd, ss[2], flags, error; + bool pf_unix = false; + + while ((ch = getopt(argc, argv, "u")) != -1) + switch (ch) { + case 'u': + pf_unix = true; + break; + default: + usage(); + } + argc -= optind; + argv += optind; - if (argc != 5) - errx(1, "usage: %s ", - getprogname()); + if (argc != 4) + usage(); - start = strtoull(argv[2], NULL, 0); - readlen = strtoull(argv[3], NULL, 0); - flags = strtoul(argv[4], NULL, 0); + start = strtoull(argv[1], NULL, 0); + readlen = strtoull(argv[2], NULL, 0); + flags = strtoul(argv[3], NULL, 0); - fd = open(argv[1], O_RDONLY); + fd = open(argv[0], O_RDONLY); if (fd < 0) err(1, "open"); - tcp_socketpair(ss); + if (pf_unix) { + if (socketpair(PF_LOCAL, SOCK_STREAM, 0, ss) != 0) + err(1, "socketpair"); + } else + tcp_socketpair(ss); error = pthread_create(&pt, NULL, receiver, &ss[1]); if (error) errc(1, error, "pthread_create"); if (sendfile(fd, ss[0], start, readlen, NULL, NULL, flags) < 0) err(3, "sendfile"); while (!read_done) usleep(1000); exit(0); } diff --git a/tests/sys/kern/sendfile_test.sh b/tests/sys/kern/sendfile_test.sh index 03d87292f3a3..7e549eec610a 100755 --- a/tests/sys/kern/sendfile_test.sh +++ b/tests/sys/kern/sendfile_test.sh @@ -1,168 +1,192 @@ # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2020 Netflix, Inc. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # # These tests exercise a few basic cases for the sendfile() syscall: # - successful operation. # - sendfile() starts an async disk read but that async I/O fails. # - sendfile() fails to read an indirect block and thus cannot # even start an async I/O. # # In all cases we request some read ahead in addition to # the data to be sent to the socket. # MD_DEVS="md.devs" MNT=mnt FILE=$MNT/file HELPER="$(atf_get_srcdir)/sendfile_helper" BSIZE=4096 atf_test_case io_success cleanup io_success_head() { atf_set "descr" "sendfile where all disk I/O succeeds" atf_set "require.user" "root" atf_set "timeout" 15 } io_success_body() { if [ "$(atf_config_get qemu false)" = "true" ]; then atf_skip "Sendfile(4) unimplemented. https://github.com/qemu-bsd-user/qemu-bsd-user/issues/25" fi alloc_md md common_body_setup $md atf_check $HELPER $FILE 0 0x10000 0x10000 } io_success_cleanup() { common_cleanup } atf_test_case io_fail_sync cleanup io_fail_sync_head() { atf_set "descr" "sendfile where we fail to start async I/O" atf_set "require.user" "root" atf_set "timeout" 15 } io_fail_sync_body() { if [ "$(atf_config_get qemu false)" = "true" ]; then atf_skip "Sendfile(4) unimplemented. https://github.com/qemu-bsd-user/qemu-bsd-user/issues/25" fi alloc_md md common_body_setup $md atf_check gnop configure -r 100 -e 5 ${md}.nop atf_check -s exit:3 -e ignore $HELPER $FILE $((12 * $BSIZE)) $BSIZE 0x10000 } io_fail_sync_cleanup() { common_cleanup } atf_test_case io_fail_async cleanup io_fail_async_head() { atf_set "descr" "sendfile where an async I/O fails" atf_set "require.user" "root" atf_set "timeout" 15 } io_fail_async_body() { if [ "$(atf_config_get qemu false)" = "true" ]; then atf_skip "Sendfile(4) unimplemented. https://github.com/qemu-bsd-user/qemu-bsd-user/issues/25" fi alloc_md md common_body_setup $md atf_check gnop configure -r 100 -e 5 ${md}.nop atf_check -s exit:2 -e ignore $HELPER $FILE 0 $BSIZE 0x10000 } io_fail_async_cleanup() { common_cleanup } +atf_test_case unix_success cleanup +unix_success_head() +{ + atf_set "descr" "sendfile via unix(4) where all disk I/O succeeds" + atf_set "require.user" "root" + atf_set "timeout" 15 +} +unix_success_body() +{ + if [ "$(atf_config_get qemu false)" = "true" ]; then + atf_skip "Sendfile(4) unimplemented. https://github.com/qemu-bsd-user/qemu-bsd-user/issues/25" + fi + + alloc_md md + common_body_setup $md + + atf_check $HELPER -u $FILE 0 0x10000 0x10000 +} +unix_success_cleanup() +{ + common_cleanup +} + atf_init_test_cases() { atf_add_test_case io_success atf_add_test_case io_fail_sync atf_add_test_case io_fail_async + atf_add_test_case unix_success } alloc_md() { local _md [ -c /dev/mdctl ] || atf_skip "no /dev/mdctl to create md devices" _md=$(mdconfig -a -t swap -s 256M) || atf_fail "mdconfig -a failed" echo ${_md} >> $MD_DEVS eval "${1}='${_md}'" } common_body_setup() { us=$1 atf_check mkdir $MNT atf_check -o ignore -e ignore newfs -b $BSIZE -U -j /dev/${us} atf_check mount /dev/${us} $MNT atf_check -e ignore dd if=/dev/zero of=$FILE bs=1m count=1 atf_check umount $MNT load_gnop atf_check gnop create /dev/${us} atf_check mount /dev/${us}.nop $MNT atf_check -o ignore ls -l $MNT/file } common_cleanup() { umount -f $MNT if [ -f "$MD_DEVS" ]; then while read test_md; do gnop destroy -f ${test_md}.nop 2>/dev/null mdconfig -d -u $test_md 2>/dev/null done < $MD_DEVS rm $MD_DEVS fi true } load_gnop() { if ! kldstat -q -m g_nop; then geom nop load || atf_skip "could not load module for geom nop" fi }