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"
64 #define PATH_TEMPLATE "aio.XXXXXXXXXX"
67 * GLOBAL_MAX sets the largest usable buffer size to be read and written, as
68 * it sizes ac_buffer in the aio_context structure. It is also the default
69 * size for file I/O. For other types, we use smaller blocks or we risk
70 * blocking (and we run in a single process/thread so that would be bad).
72 #define GLOBAL_MAX 16384
74 #define BUFFER_MAX GLOBAL_MAX
76 int ac_read_fd, ac_write_fd;
78 char ac_buffer[GLOBAL_MAX];
81 void (*ac_cleanup)(void *arg);
85 static int aio_timedout;
88 * Each test run specifies a timeout in seconds. Use the somewhat obsoleted
89 * signal(3) and alarm(3) APIs to set this up.
92 aio_timeout_signal(int sig __unused)
99 aio_timeout_start(int seconds)
103 ATF_REQUIRE_MSG(signal(SIGALRM, aio_timeout_signal) != SIG_ERR,
104 "failed to set SIGALRM handler: %s", strerror(errno));
109 aio_timeout_stop(void)
112 ATF_REQUIRE_MSG(signal(SIGALRM, NULL) != SIG_ERR,
113 "failed to reset SIGALRM handler to default: %s", strerror(errno));
118 * Fill a buffer given a seed that can be fed into srandom() to initialize
119 * the PRNG in a repeatable manner.
122 aio_fill_buffer(char *buffer, int len, long seed)
128 for (i = 0; i < len; i++) {
129 ch = random() & 0xff;
135 * Test that a buffer matches a given seed. See aio_fill_buffer(). Return
136 * (1) on a match, (0) on a mismatch.
139 aio_test_buffer(char *buffer, int len, long seed)
145 for (i = 0; i < len; i++) {
146 ch = random() & 0xff;
154 * Initialize a testing context given the file descriptors provided by the
158 aio_context_init(struct aio_context *ac, int read_fd,
159 int write_fd, int buflen, int seconds, void (*cleanup)(void *),
163 ATF_REQUIRE_MSG(buflen <= BUFFER_MAX,
164 "aio_context_init: buffer too large (%d > %d)",
166 bzero(ac, sizeof(*ac));
167 ac->ac_read_fd = read_fd;
168 ac->ac_write_fd = write_fd;
169 ac->ac_buflen = buflen;
171 ac->ac_seed = random();
172 aio_fill_buffer(ac->ac_buffer, buflen, ac->ac_seed);
173 ATF_REQUIRE_MSG(aio_test_buffer(ac->ac_buffer, buflen,
174 ac->ac_seed) != 0, "aio_test_buffer: internal error");
175 ac->ac_seconds = seconds;
176 ac->ac_cleanup = cleanup;
177 ac->ac_cleanup_arg = cleanup_arg;
181 * Each tester can register a callback to clean up in the event the test
182 * fails. Preserve the value of errno so that subsequent calls to errx()
186 aio_cleanup(struct aio_context *ac)
190 if (ac->ac_cleanup == NULL)
193 (ac->ac_cleanup)(ac->ac_cleanup_arg);
198 * Perform a simple write test of our initialized data buffer to the provided
202 aio_write_test(struct aio_context *ac)
204 struct aiocb aio, *aiop;
207 ATF_REQUIRE_KERNEL_MODULE("aio");
209 bzero(&aio, sizeof(aio));
210 aio.aio_buf = ac->ac_buffer;
211 aio.aio_nbytes = ac->ac_buflen;
212 aio.aio_fildes = ac->ac_write_fd;
215 aio_timeout_start(ac->ac_seconds);
217 if (aio_write(&aio) < 0) {
218 if (errno == EINTR) {
221 atf_tc_fail("aio_write timed out");
225 atf_tc_fail("aio_write failed: %s", strerror(errno));
228 len = aio_waitcomplete(&aiop, NULL);
230 if (errno == EINTR) {
233 atf_tc_fail("aio_waitcomplete timed out");
237 atf_tc_fail("aio_waitcomplete failed: %s", strerror(errno));
242 if (len != ac->ac_buflen) {
244 atf_tc_fail("aio_waitcomplete short write (%jd)",
250 * Perform a simple read test of our initialized data buffer from the
251 * provided file descriptor.
254 aio_read_test(struct aio_context *ac)
256 struct aiocb aio, *aiop;
259 ATF_REQUIRE_KERNEL_MODULE("aio");
261 bzero(ac->ac_buffer, ac->ac_buflen);
262 bzero(&aio, sizeof(aio));
263 aio.aio_buf = ac->ac_buffer;
264 aio.aio_nbytes = ac->ac_buflen;
265 aio.aio_fildes = ac->ac_read_fd;
268 aio_timeout_start(ac->ac_seconds);
270 if (aio_read(&aio) < 0) {
271 if (errno == EINTR) {
274 atf_tc_fail("aio_write timed out");
278 atf_tc_fail("aio_read failed: %s", strerror(errno));
281 len = aio_waitcomplete(&aiop, NULL);
283 if (errno == EINTR) {
286 atf_tc_fail("aio_waitcomplete timed out");
290 atf_tc_fail("aio_waitcomplete failed: %s", strerror(errno));
295 if (len != ac->ac_buflen) {
297 atf_tc_fail("aio_waitcomplete short read (%jd)",
301 if (aio_test_buffer(ac->ac_buffer, ac->ac_buflen, ac->ac_seed) == 0) {
303 atf_tc_fail("buffer mismatched");
308 * Series of type-specific tests for AIO. For now, we just make sure we can
309 * issue a write and then a read to each type. We assume that once a write
310 * is issued, a read can follow.
314 * Test with a classic file. Assumes we can create a moderate size temporary
317 struct aio_file_arg {
323 aio_file_cleanup(void *arg)
325 struct aio_file_arg *afa;
329 unlink(afa->afa_pathname);
332 #define FILE_LEN GLOBAL_MAX
333 #define FILE_TIMEOUT 30
334 ATF_TC_WITHOUT_HEAD(aio_file_test);
335 ATF_TC_BODY(aio_file_test, tc)
337 char pathname[PATH_MAX];
338 struct aio_file_arg arg;
339 struct aio_context ac;
342 ATF_REQUIRE_KERNEL_MODULE("aio");
344 strcpy(pathname, PATH_TEMPLATE);
345 fd = mkstemp(pathname);
346 ATF_REQUIRE_MSG(fd != -1, "mkstemp failed: %s", strerror(errno));
349 arg.afa_pathname = pathname;
351 aio_context_init(&ac, fd, fd, FILE_LEN,
352 FILE_TIMEOUT, aio_file_cleanup, &arg);
356 aio_file_cleanup(&arg);
359 struct aio_fifo_arg {
366 aio_fifo_cleanup(void *arg)
368 struct aio_fifo_arg *afa;
371 if (afa->afa_read_fd != -1)
372 close(afa->afa_read_fd);
373 if (afa->afa_write_fd != -1)
374 close(afa->afa_write_fd);
375 unlink(afa->afa_pathname);
379 #define FIFO_TIMEOUT 30
380 ATF_TC_WITHOUT_HEAD(aio_fifo_test);
381 ATF_TC_BODY(aio_fifo_test, tc)
383 int error, read_fd = -1, write_fd = -1;
384 struct aio_fifo_arg arg;
385 char pathname[PATH_MAX];
386 struct aio_context ac;
388 ATF_REQUIRE_KERNEL_MODULE("aio");
391 * In theory, mkstemp() can return a name that is then collided with.
392 * Because this is a regression test, we treat that as a test failure
393 * rather than retrying.
395 strcpy(pathname, PATH_TEMPLATE);
396 ATF_REQUIRE_MSG(mkstemp(pathname) != -1,
397 "mkstemp failed: %s", strerror(errno));
398 ATF_REQUIRE_MSG(unlink(pathname) == 0,
399 "unlink failed: %s", strerror(errno));
400 ATF_REQUIRE_MSG(mkfifo(pathname, 0600) != -1,
401 "mkfifo failed: %s", strerror(errno));
402 arg.afa_pathname = pathname;
403 arg.afa_read_fd = -1;
404 arg.afa_write_fd = -1;
406 read_fd = open(pathname, O_RDONLY | O_NONBLOCK);
409 aio_fifo_cleanup(&arg);
411 atf_tc_fail("read_fd open failed: %s",
414 arg.afa_read_fd = read_fd;
416 write_fd = open(pathname, O_WRONLY);
417 if (write_fd == -1) {
419 aio_fifo_cleanup(&arg);
421 atf_tc_fail("write_fd open failed: %s",
424 arg.afa_write_fd = write_fd;
426 aio_context_init(&ac, read_fd, write_fd, FIFO_LEN,
427 FIFO_TIMEOUT, aio_fifo_cleanup, &arg);
431 aio_fifo_cleanup(&arg);
434 struct aio_unix_socketpair_arg {
439 aio_unix_socketpair_cleanup(void *arg)
441 struct aio_unix_socketpair_arg *asa;
444 close(asa->asa_sockets[0]);
445 close(asa->asa_sockets[1]);
448 #define UNIX_SOCKETPAIR_LEN 256
449 #define UNIX_SOCKETPAIR_TIMEOUT 30
450 ATF_TC_WITHOUT_HEAD(aio_unix_socketpair_test);
451 ATF_TC_BODY(aio_unix_socketpair_test, tc)
453 struct aio_unix_socketpair_arg arg;
454 struct aio_context ac;
457 ATF_REQUIRE_KERNEL_MODULE("aio");
459 ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != -1,
460 "socketpair failed: %s", strerror(errno));
462 arg.asa_sockets[0] = sockets[0];
463 arg.asa_sockets[1] = sockets[1];
464 aio_context_init(&ac, sockets[0],
465 sockets[1], UNIX_SOCKETPAIR_LEN, UNIX_SOCKETPAIR_TIMEOUT,
466 aio_unix_socketpair_cleanup, &arg);
470 aio_unix_socketpair_cleanup(&arg);
479 aio_pty_cleanup(void *arg)
481 struct aio_pty_arg *apa;
484 close(apa->apa_read_fd);
485 close(apa->apa_write_fd);
489 #define PTY_TIMEOUT 30
490 ATF_TC_WITHOUT_HEAD(aio_pty_test);
491 ATF_TC_BODY(aio_pty_test, tc)
493 struct aio_pty_arg arg;
494 struct aio_context ac;
495 int read_fd, write_fd;
499 ATF_REQUIRE_KERNEL_MODULE("aio");
501 ATF_REQUIRE_MSG(openpty(&read_fd, &write_fd, NULL, NULL, NULL) == 0,
502 "openpty failed: %s", strerror(errno));
504 arg.apa_read_fd = read_fd;
505 arg.apa_write_fd = write_fd;
507 if (tcgetattr(write_fd, &ts) < 0) {
509 aio_pty_cleanup(&arg);
511 atf_tc_fail("tcgetattr failed: %s", strerror(errno));
514 if (tcsetattr(write_fd, TCSANOW, &ts) < 0) {
516 aio_pty_cleanup(&arg);
518 atf_tc_fail("tcsetattr failed: %s", strerror(errno));
520 aio_context_init(&ac, read_fd, write_fd, PTY_LEN,
521 PTY_TIMEOUT, aio_pty_cleanup, &arg);
526 aio_pty_cleanup(&arg);
530 aio_pipe_cleanup(void *arg)
539 #define PIPE_TIMEOUT 30
540 ATF_TC_WITHOUT_HEAD(aio_pipe_test);
541 ATF_TC_BODY(aio_pipe_test, tc)
543 struct aio_context ac;
546 ATF_REQUIRE_KERNEL_MODULE("aio");
548 ATF_REQUIRE_MSG(pipe(pipes) != -1,
549 "pipe failed: %s", strerror(errno));
551 aio_context_init(&ac, pipes[0], pipes[1], PIPE_LEN,
552 PIPE_TIMEOUT, aio_pipe_cleanup, pipes);
556 aio_pipe_cleanup(pipes);
566 aio_md_cleanup(void *arg)
568 struct aio_md_arg *ama;
569 struct md_ioctl mdio;
574 if (ama->ama_fd != -1)
577 if (ama->ama_unit != -1) {
578 bzero(&mdio, sizeof(mdio));
579 mdio.md_version = MDIOVERSION;
580 mdio.md_unit = ama->ama_unit;
581 if (ioctl(ama->ama_mdctl_fd, MDIOCDETACH, &mdio) == -1) {
583 close(ama->ama_mdctl_fd);
585 atf_tc_fail("ioctl MDIOCDETACH failed: %s",
590 close(ama->ama_mdctl_fd);
593 #define MD_LEN GLOBAL_MAX
594 #define MD_TIMEOUT 30
596 ATF_TC_HEAD(aio_md_test, tc)
599 atf_tc_set_md_var(tc, "require.user", "root");
601 ATF_TC_BODY(aio_md_test, tc)
603 int error, fd, mdctl_fd, unit;
604 char pathname[PATH_MAX];
605 struct aio_md_arg arg;
606 struct aio_context ac;
607 struct md_ioctl mdio;
609 ATF_REQUIRE_KERNEL_MODULE("aio");
611 mdctl_fd = open("/dev/" MDCTL_NAME, O_RDWR, 0);
612 ATF_REQUIRE_MSG(mdctl_fd != -1,
613 "opening /dev/%s failed: %s", MDCTL_NAME, strerror(errno));
615 bzero(&mdio, sizeof(mdio));
616 mdio.md_version = MDIOVERSION;
617 mdio.md_type = MD_MALLOC;
618 mdio.md_options = MD_AUTOUNIT | MD_COMPRESS;
619 mdio.md_mediasize = GLOBAL_MAX;
620 mdio.md_sectorsize = 512;
622 arg.ama_mdctl_fd = mdctl_fd;
625 if (ioctl(mdctl_fd, MDIOCATTACH, &mdio) < 0) {
627 aio_md_cleanup(&arg);
629 atf_tc_fail("ioctl MDIOCATTACH failed: %s", strerror(errno));
632 arg.ama_unit = unit = mdio.md_unit;
633 snprintf(pathname, PATH_MAX, "/dev/md%d", unit);
634 fd = open(pathname, O_RDWR);
635 ATF_REQUIRE_MSG(fd != -1,
636 "opening %s failed: %s", pathname, strerror(errno));
639 aio_context_init(&ac, fd, fd, MD_LEN, MD_TIMEOUT,
640 aio_md_cleanup, &arg);
644 aio_md_cleanup(&arg);
650 ATF_TP_ADD_TC(tp, aio_file_test);
651 ATF_TP_ADD_TC(tp, aio_fifo_test);
652 ATF_TP_ADD_TC(tp, aio_unix_socketpair_test);
653 ATF_TP_ADD_TC(tp, aio_pty_test);
654 ATF_TP_ADD_TC(tp, aio_pipe_test);
655 ATF_TP_ADD_TC(tp, aio_md_test);
657 return (atf_no_error());