2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Regression test to do some very basic AIO exercising on several types of
31 * file descriptors. Currently, the tests consist of initializing a fixed
32 * size buffer with pseudo-random data, writing it to one fd using AIO, then
33 * reading it from a second descriptor using AIO. For some targets, the same
34 * fd is used for write and read (i.e., file, md device), but for others the
35 * operation is performed on a peer (pty, socket, fifo, etc). A timeout is
36 * initiated to detect undo blocking. This test does not attempt to exercise
37 * error cases or more subtle asynchronous behavior, just make sure that the
38 * basic operations work on some basic object types.
41 #include <sys/param.h>
42 #include <sys/module.h>
43 #include <sys/socket.h>
45 #include <sys/mdioctl.h>
62 #include "freebsd_test_suite/macros.h"
65 #define PATH_TEMPLATE "aio.XXXXXXXXXX"
68 * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
69 * it sizes ac_buffer in the aio_context structure. It is also the default
70 * size for file I/O. For other types, we use smaller blocks or we risk
71 * blocking (and we run in a single process/thread so that would be bad).
73 #define GLOBAL_MAX 16384
75 #define BUFFER_MAX GLOBAL_MAX
77 int ac_read_fd, ac_write_fd;
79 char ac_buffer[GLOBAL_MAX];
82 void (*ac_cleanup)(void *arg);
86 static int aio_timedout;
89 * Each test run specifies a timeout in seconds. Use the somewhat obsoleted
90 * signal(3) and alarm(3) APIs to set this up.
93 aio_timeout_signal(int sig __unused)
100 aio_timeout_start(int seconds)
104 ATF_REQUIRE_MSG(signal(SIGALRM, aio_timeout_signal) != SIG_ERR,
105 "failed to set SIGALRM handler: %s", strerror(errno));
110 aio_timeout_stop(void)
113 ATF_REQUIRE_MSG(signal(SIGALRM, NULL) != SIG_ERR,
114 "failed to reset SIGALRM handler to default: %s", strerror(errno));
119 * Fill a buffer given a seed that can be fed into srandom() to initialize
120 * the PRNG in a repeatable manner.
123 aio_fill_buffer(char *buffer, int len, long seed)
129 for (i = 0; i < len; i++) {
130 ch = random() & 0xff;
136 * Test that a buffer matches a given seed. See aio_fill_buffer(). Return
137 * (1) on a match, (0) on a mismatch.
140 aio_test_buffer(char *buffer, int len, long seed)
146 for (i = 0; i < len; i++) {
147 ch = random() & 0xff;
155 * Initialize a testing context given the file descriptors provided by the
159 aio_context_init(struct aio_context *ac, int read_fd,
160 int write_fd, int buflen, int seconds, void (*cleanup)(void *),
164 ATF_REQUIRE_MSG(buflen <= BUFFER_MAX,
165 "aio_context_init: buffer too large (%d > %d)",
167 bzero(ac, sizeof(*ac));
168 ac->ac_read_fd = read_fd;
169 ac->ac_write_fd = write_fd;
170 ac->ac_buflen = buflen;
172 ac->ac_seed = random();
173 aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
174 ATF_REQUIRE_MSG(aio_test_buffer(ac->ac_buffer, buflen,
175 ac->ac_seed) != 0, "aio_test_buffer: internal error");
176 ac->ac_seconds = seconds;
177 ac->ac_cleanup = cleanup;
178 ac->ac_cleanup_arg = cleanup_arg;
182 * Each tester can register a callback to clean up in the event the test
183 * fails. Preserve the value of errno so that subsequent calls to errx()
187 aio_cleanup(struct aio_context *ac)
191 if (ac->ac_cleanup == NULL)
194 (ac->ac_cleanup)(ac->ac_cleanup_arg);
199 * Perform a simple write test of our initialized data buffer to the provided
203 aio_write_test(struct aio_context *ac)
205 struct aiocb aio, *aiop;
208 ATF_REQUIRE_KERNEL_MODULE("aio");
210 bzero(&aio, sizeof(aio));
211 aio.aio_buf = ac->ac_buffer;
212 aio.aio_nbytes = ac->ac_buflen;
213 aio.aio_fildes = ac->ac_write_fd;
216 aio_timeout_start(ac->ac_seconds);
218 if (aio_write(&aio) < 0) {
219 if (errno == EINTR) {
222 atf_tc_fail("aio_write timed out");
226 atf_tc_fail("aio_write failed: %s", strerror(errno));
229 len = aio_waitcomplete(&aiop, NULL);
231 if (errno == EINTR) {
234 atf_tc_fail("aio_waitcomplete timed out");
238 atf_tc_fail("aio_waitcomplete failed: %s", strerror(errno));
243 if (len != ac->ac_buflen) {
245 atf_tc_fail("aio_waitcomplete short write (%jd)",
251 * Perform a simple read test of our initialized data buffer from the
252 * provided file descriptor.
255 aio_read_test(struct aio_context *ac)
257 struct aiocb aio, *aiop;
260 ATF_REQUIRE_KERNEL_MODULE("aio");
262 bzero(ac->ac_buffer, ac->ac_buflen);
263 bzero(&aio, sizeof(aio));
264 aio.aio_buf = ac->ac_buffer;
265 aio.aio_nbytes = ac->ac_buflen;
266 aio.aio_fildes = ac->ac_read_fd;
269 aio_timeout_start(ac->ac_seconds);
271 if (aio_read(&aio) < 0) {
272 if (errno == EINTR) {
275 atf_tc_fail("aio_write timed out");
279 atf_tc_fail("aio_read failed: %s", strerror(errno));
282 len = aio_waitcomplete(&aiop, NULL);
284 if (errno == EINTR) {
287 atf_tc_fail("aio_waitcomplete timed out");
291 atf_tc_fail("aio_waitcomplete failed: %s", strerror(errno));
296 if (len != ac->ac_buflen) {
298 atf_tc_fail("aio_waitcomplete short read (%jd)",
302 if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0) {
304 atf_tc_fail("buffer mismatched");
309 * Series of type-specific tests for AIO. For now, we just make sure we can
310 * issue a write and then a read to each type. We assume that once a write
311 * is issued, a read can follow.
315 * Test with a classic file. Assumes we can create a moderate size temporary
318 struct aio_file_arg {
324 aio_file_cleanup(void *arg)
326 struct aio_file_arg *afa;
330 unlink(afa->afa_pathname);
333 #define FILE_LEN GLOBAL_MAX
334 #define FILE_TIMEOUT 30
335 ATF_TC_WITHOUT_HEAD(aio_file_test);
336 ATF_TC_BODY(aio_file_test, tc)
338 char pathname[PATH_MAX];
339 struct aio_file_arg arg;
340 struct aio_context ac;
343 ATF_REQUIRE_KERNEL_MODULE("aio");
344 ATF_REQUIRE_UNSAFE_AIO();
346 strcpy(pathname, PATH_TEMPLATE);
347 fd = mkstemp(pathname);
348 ATF_REQUIRE_MSG(fd != -1, "mkstemp failed: %s", strerror(errno));
351 arg.afa_pathname = pathname;
353 aio_context_init(&ac, fd, fd, FILE_LEN,
354 FILE_TIMEOUT, aio_file_cleanup, &arg);
358 aio_file_cleanup(&arg);
361 struct aio_fifo_arg {
368 aio_fifo_cleanup(void *arg)
370 struct aio_fifo_arg *afa;
373 if (afa->afa_read_fd != -1)
374 close(afa->afa_read_fd);
375 if (afa->afa_write_fd != -1)
376 close(afa->afa_write_fd);
377 unlink(afa->afa_pathname);
381 #define FIFO_TIMEOUT 30
382 ATF_TC_WITHOUT_HEAD(aio_fifo_test);
383 ATF_TC_BODY(aio_fifo_test, tc)
385 int error, read_fd = -1, write_fd = -1;
386 struct aio_fifo_arg arg;
387 char pathname[PATH_MAX];
388 struct aio_context ac;
390 ATF_REQUIRE_KERNEL_MODULE("aio");
391 ATF_REQUIRE_UNSAFE_AIO();
394 * In theory, mkstemp() can return a name that is then collided with.
395 * Because this is a regression test, we treat that as a test failure
396 * rather than retrying.
398 strcpy(pathname, PATH_TEMPLATE);
399 ATF_REQUIRE_MSG(mkstemp(pathname) != -1,
400 "mkstemp failed: %s", strerror(errno));
401 ATF_REQUIRE_MSG(unlink(pathname) == 0,
402 "unlink failed: %s", strerror(errno));
403 ATF_REQUIRE_MSG(mkfifo(pathname, 0600) != -1,
404 "mkfifo failed: %s", strerror(errno));
405 arg.afa_pathname = pathname;
406 arg.afa_read_fd = -1;
407 arg.afa_write_fd = -1;
409 read_fd = open(pathname, O_RDONLY | O_NONBLOCK);
412 aio_fifo_cleanup(&arg);
414 atf_tc_fail("read_fd open failed: %s",
417 arg.afa_read_fd = read_fd;
419 write_fd = open(pathname, O_WRONLY);
420 if (write_fd == -1) {
422 aio_fifo_cleanup(&arg);
424 atf_tc_fail("write_fd open failed: %s",
427 arg.afa_write_fd = write_fd;
429 aio_context_init(&ac, read_fd, write_fd, FIFO_LEN,
430 FIFO_TIMEOUT, aio_fifo_cleanup, &arg);
434 aio_fifo_cleanup(&arg);
437 struct aio_unix_socketpair_arg {
442 aio_unix_socketpair_cleanup(void *arg)
444 struct aio_unix_socketpair_arg *asa;
447 close(asa->asa_sockets[0]);
448 close(asa->asa_sockets[1]);
451 #define UNIX_SOCKETPAIR_LEN 256
452 #define UNIX_SOCKETPAIR_TIMEOUT 30
453 ATF_TC_WITHOUT_HEAD(aio_unix_socketpair_test);
454 ATF_TC_BODY(aio_unix_socketpair_test, tc)
456 struct aio_unix_socketpair_arg arg;
457 struct aio_context ac;
460 ATF_REQUIRE_KERNEL_MODULE("aio");
462 ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
463 "socketpair failed: %s", strerror(errno));
465 arg.asa_sockets[0] = sockets[0];
466 arg.asa_sockets[1] = sockets[1];
467 aio_context_init(&ac, sockets[0],
468 sockets[1], UNIX_SOCKETPAIR_LEN, UNIX_SOCKETPAIR_TIMEOUT,
469 aio_unix_socketpair_cleanup, &arg);
473 aio_unix_socketpair_cleanup(&arg);
482 aio_pty_cleanup(void *arg)
484 struct aio_pty_arg *apa;
487 close(apa->apa_read_fd);
488 close(apa->apa_write_fd);
492 #define PTY_TIMEOUT 30
493 ATF_TC_WITHOUT_HEAD(aio_pty_test);
494 ATF_TC_BODY(aio_pty_test, tc)
496 struct aio_pty_arg arg;
497 struct aio_context ac;
498 int read_fd, write_fd;
502 ATF_REQUIRE_KERNEL_MODULE("aio");
503 ATF_REQUIRE_UNSAFE_AIO();
505 ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
506 "openpty failed: %s", strerror(errno));
508 arg.apa_read_fd = read_fd;
509 arg.apa_write_fd = write_fd;
511 if (tcgetattr(write_fd, &ts) < 0) {
513 aio_pty_cleanup(&arg);
515 atf_tc_fail("tcgetattr failed: %s", strerror(errno));
518 if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
520 aio_pty_cleanup(&arg);
522 atf_tc_fail("tcsetattr failed: %s", strerror(errno));
524 aio_context_init(&ac, read_fd, write_fd, PTY_LEN,
525 PTY_TIMEOUT, aio_pty_cleanup, &arg);
530 aio_pty_cleanup(&arg);
534 aio_pipe_cleanup(void *arg)
543 #define PIPE_TIMEOUT 30
544 ATF_TC_WITHOUT_HEAD(aio_pipe_test);
545 ATF_TC_BODY(aio_pipe_test, tc)
547 struct aio_context ac;
550 ATF_REQUIRE_KERNEL_MODULE("aio");
551 ATF_REQUIRE_UNSAFE_AIO();
553 ATF_REQUIRE_MSG(pipe(pipes) != -1,
554 "pipe failed: %s", strerror(errno));
556 aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN,
557 PIPE_TIMEOUT, aio_pipe_cleanup, pipes);
561 aio_pipe_cleanup(pipes);
571 aio_md_cleanup(void *arg)
573 struct aio_md_arg *ama;
574 struct md_ioctl mdio;
579 if (ama->ama_fd != -1)
582 if (ama->ama_unit != -1) {
583 bzero(&mdio, sizeof(mdio));
584 mdio.md_version = MDIOVERSION;
585 mdio.md_unit = ama->ama_unit;
586 if (ioctl(ama->ama_mdctl_fd, MDIOCDETACH, &mdio) == -1) {
588 close(ama->ama_mdctl_fd);
590 atf_tc_fail("ioctl MDIOCDETACH failed: %s",
595 close(ama->ama_mdctl_fd);
598 #define MD_LEN GLOBAL_MAX
599 #define MD_TIMEOUT 30
601 ATF_TC_HEAD(aio_md_test, tc)
604 atf_tc_set_md_var(tc, "require.user", "root");
606 ATF_TC_BODY(aio_md_test, tc)
608 int error, fd, mdctl_fd, unit;
609 char pathname[PATH_MAX];
610 struct aio_md_arg arg;
611 struct aio_context ac;
612 struct md_ioctl mdio;
614 ATF_REQUIRE_KERNEL_MODULE("aio");
616 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
617 ATF_REQUIRE_MSG(mdctl_fd != -1,
618 "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
620 bzero(&mdio, sizeof(mdio));
621 mdio.md_version = MDIOVERSION;
622 mdio.md_type = MD_MALLOC;
623 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
624 mdio.md_mediasize = GLOBAL_MAX;
625 mdio.md_sectorsize = 512;
627 arg.ama_mdctl_fd = mdctl_fd;
630 if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
632 aio_md_cleanup(&arg);
634 atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
637 arg.ama_unit = unit = mdio.md_unit;
638 snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
639 fd = open(pathname, O_RDWR);
640 ATF_REQUIRE_MSG(fd != -1,
641 "opening %s failed: %s", pathname, strerror(errno));
644 aio_context_init(&ac, fd, fd, MD_LEN, MD_TIMEOUT,
645 aio_md_cleanup, &arg);
649 aio_md_cleanup(&arg);
652 ATF_TC_WITHOUT_HEAD(aio_large_read_test);
653 ATF_TC_BODY(aio_large_read_test, tc)
655 char pathname[PATH_MAX];
656 struct aiocb cb, *cbp;
664 ATF_REQUIRE_KERNEL_MODULE("aio");
665 ATF_REQUIRE_UNSAFE_AIO();
668 len = sizeof(clamped);
669 if (sysctlbyname("debug.iosize_max_clamp", &clamped, &len, NULL, 0) ==
671 atf_libc_error(errno, "Failed to read debug.iosize_max_clamp");
674 /* Determine the maximum supported read(2) size. */
681 strcpy(pathname, PATH_TEMPLATE);
682 fd = mkstemp(pathname);
683 ATF_REQUIRE_MSG(fd != -1, "mkstemp failed: %s", strerror(errno));
687 memset(&cb, 0, sizeof(cb));
691 if (aio_read(&cb) == -1)
692 atf_tc_fail("aio_read() of maximum read size failed: %s",
695 nread = aio_waitcomplete(&cbp, NULL);
697 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
699 atf_tc_fail("aio_read() from empty file returned data: %zd",
702 memset(&cb, 0, sizeof(cb));
703 cb.aio_nbytes = len + 1;
706 if (aio_read(&cb) == -1) {
709 atf_tc_fail("aio_read() of too large read size failed: %s",
713 nread = aio_waitcomplete(&cbp, NULL);
717 atf_tc_fail("aio_waitcomplete() failed: %s", strerror(errno));
719 atf_tc_fail("aio_read() of too large read size returned: %zd", nread);
726 * This tests for a bug where arriving socket data can wakeup multiple
727 * AIO read requests resulting in an uncancellable request.
729 ATF_TC_WITHOUT_HEAD(aio_socket_two_reads);
730 ATF_TC_BODY(aio_socket_two_reads, tc)
741 ATF_REQUIRE_KERNEL_MODULE("aio");
742 #if __FreeBSD_version < 1100101
743 aft_tc_skip("kernel version %d is too old (%d required)",
744 __FreeBSD_version, 1100101);
747 ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, s) != -1);
749 /* Queue two read requests. */
750 memset(&ioreq, 0, sizeof(ioreq));
751 for (i = 0; i < nitems(ioreq); i++) {
752 ioreq[i].iocb.aio_nbytes = sizeof(ioreq[i].buffer);
753 ioreq[i].iocb.aio_fildes = s[0];
754 ioreq[i].iocb.aio_buf = ioreq[i].buffer;
755 ATF_REQUIRE(aio_read(&ioreq[i].iocb) == 0);
758 /* Send a single byte. This should complete one request. */
760 ATF_REQUIRE(write(s[1], &c, sizeof(c)) == 1);
762 ATF_REQUIRE(aio_waitcomplete(&iocb, NULL) == 1);
764 /* Determine which request completed and verify the data was read. */
765 if (iocb == &ioreq[0].iocb)
769 ATF_REQUIRE(ioreq[i].buffer[0] == c);
774 * Try to cancel the other request. On broken systems this
775 * will fail and the process will hang on exit.
777 ATF_REQUIRE(aio_error(&ioreq[i].iocb) == EINPROGRESS);
778 ATF_REQUIRE(aio_cancel(s[0], &ioreq[i].iocb) == AIO_CANCELED);
787 ATF_TP_ADD_TC(tp, aio_file_test);
788 ATF_TP_ADD_TC(tp, aio_fifo_test);
789 ATF_TP_ADD_TC(tp, aio_unix_socketpair_test);
790 ATF_TP_ADD_TC(tp, aio_pty_test);
791 ATF_TP_ADD_TC(tp, aio_pipe_test);
792 ATF_TP_ADD_TC(tp, aio_md_test);
793 ATF_TP_ADD_TC(tp, aio_large_read_test);
794 ATF_TP_ADD_TC(tp, aio_socket_two_reads);
796 return (atf_no_error());