1 /*
2 * Copyright (C) 2013-2021 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *
18 * This code is a complete clean re-write of the stress tool by
19 * Colin Ian King <colin.king@canonical.com> and attempts to be
20 * backwardly compatible with the stress tool by Amos Waterland
21 * <apw@rossby.metr.ou.edu> but has more stress tests and more
22 * functionality.
23 *
24 */
25 #include "stress-ng.h"
26
27 static const stress_help_t help[] = {
28 { NULL, "sendfile N", "start N workers exercising sendfile" },
29 { NULL, "sendfile-ops N", "stop after N bogo sendfile operations" },
30 { NULL, "sendfile-size N", "size of data to be sent with sendfile" },
31 { NULL, NULL, NULL }
32 };
33
stress_set_sendfile_size(const char * opt)34 static int stress_set_sendfile_size(const char *opt)
35 {
36 uint64_t sendfile_size;
37
38 sendfile_size = stress_get_uint64_byte(opt);
39 stress_check_range_bytes("sendfile-size", sendfile_size,
40 MIN_SENDFILE_SIZE, MAX_SENDFILE_SIZE);
41 return stress_set_setting("sendfile-size", TYPE_ID_UINT64, &sendfile_size);
42 }
43
44 static const stress_opt_set_func_t opt_set_funcs[] = {
45 { OPT_sendfile_size, stress_set_sendfile_size },
46 { 0, NULL }
47 };
48
49 #if defined(HAVE_SYS_SENDFILE_H) && \
50 NEED_GLIBC(2,1,0)
51
52 /*
53 * stress_sendfile
54 * stress reading of a temp file and writing to /dev/null via sendfile
55 */
stress_sendfile(const stress_args_t * args)56 static int stress_sendfile(const stress_args_t *args)
57 {
58 char filename[PATH_MAX];
59 int i = 0, fdin, fdout, ret, bad_fd, rc = EXIT_SUCCESS;
60 size_t sz;
61 int64_t sendfile_size = DEFAULT_SENDFILE_SIZE;
62
63 if (!stress_get_setting("sendfile-size", &sendfile_size)) {
64 if (g_opt_flags & OPT_FLAGS_MAXIMIZE)
65 sendfile_size = MAX_SENDFILE_SIZE;
66 if (g_opt_flags & OPT_FLAGS_MINIMIZE)
67 sendfile_size = MIN_SENDFILE_SIZE;
68 }
69 sz = (size_t)sendfile_size;
70
71 ret = stress_temp_dir_mk_args(args);
72 if (ret < 0)
73 return exit_status(-ret);
74
75 (void)stress_temp_filename_args(args,
76 filename, sizeof(filename), stress_mwc32());
77
78 if ((fdin = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
79 rc = exit_status(errno);
80 pr_fail("%s: open %s failed, errno=%d (%s)\n",
81 args->name, filename, errno, strerror(errno));
82 goto dir_out;
83 }
84 #if defined(HAVE_POSIX_FALLOCATE)
85 ret = posix_fallocate(fdin, (off_t)0, (off_t)sz);
86 #else
87 ret = shim_fallocate(fdin, 0, (off_t)0, (off_t)sz);
88 #endif
89 if (ret < 0) {
90 rc = exit_status(errno);
91 pr_fail("%s: fallocate failed, errno=%d (%s)\n",
92 args->name, errno, strerror(errno));
93 goto dir_out;
94 }
95 (void)close(fdin);
96 if ((fdin = open(filename, O_RDONLY)) < 0) {
97 rc = exit_status(errno);
98 pr_fail("%s: open %s failed, errno=%d (%s)\n",
99 args->name, filename, errno, strerror(errno));
100 goto dir_out;
101 }
102
103 if ((fdout = open("/dev/null", O_WRONLY)) < 0) {
104 pr_fail("%s: open /dev/null failed, errno=%d (%s)\n",
105 args->name, errno, strerror(errno));
106 rc = EXIT_FAILURE;
107 goto close_in;
108 }
109
110 bad_fd = stress_get_bad_fd();
111
112 stress_set_proc_state(args->name, STRESS_STATE_RUN);
113
114 do {
115 off_t offset = 0;
116
117 if (sendfile(fdout, fdin, &offset, sz) < 0) {
118 if (errno == ENOSYS) {
119 if (args->instance == 0)
120 pr_inf_skip("%s: skipping stressor, sendfile not implemented\n",
121 args->name);
122 rc = EXIT_NOT_IMPLEMENTED;
123 goto close_out;
124 }
125 if (errno == EINTR)
126 continue;
127 pr_fail("%s: sendfile failed, errno=%d (%s)\n",
128 args->name, errno, strerror(errno));
129 rc = EXIT_FAILURE;
130 goto close_out;
131 }
132
133 /* Periodically perform some unusual sendfile calls */
134 if ((i++ & 0xff) == 0) {
135 /* Exercise with invalid destination fd */
136 offset = 0;
137 (void)sendfile(bad_fd, fdin, &offset, sz);
138
139 /* Exercise with invalid source fd */
140 offset = 0;
141 (void)sendfile(fdout, bad_fd, &offset, sz);
142
143 /* Exercise with invalid offset */
144 offset = -1;
145 (void)sendfile(fdout, fdin, &offset, sz);
146
147 /* Exercise with invalid size */
148 offset = 0;
149 (void)sendfile(fdout, fdin, &offset, (size_t)-1);
150
151 /* Exercise with zero size (should work, no-op) */
152 offset = 0;
153 (void)sendfile(fdout, fdin, &offset, 0);
154
155 /* Exercise with read-only destination (EBADF) */
156 offset = 0;
157 (void)sendfile(fdin, fdin, &offset, sz);
158
159 /* Exercise with write-only source (EBADF) */
160 offset = 0;
161 (void)sendfile(fdout, fdout, &offset, sz);
162
163 /* Exercise truncated read */
164 offset = (off_t)(sz - 1);
165 (void)sendfile(fdout, fdin, &offset, sz);
166 }
167 inc_counter(args);
168 } while (keep_stressing(args));
169
170 close_out:
171 stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
172 (void)close(fdout);
173 close_in:
174 stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
175 (void)close(fdin);
176 (void)unlink(filename);
177 dir_out:
178 stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
179 (void)stress_temp_dir_rm_args(args);
180
181 return rc;
182 }
183
184 stressor_info_t stress_sendfile_info = {
185 .stressor = stress_sendfile,
186 .class = CLASS_PIPE_IO | CLASS_OS,
187 .opt_set_funcs = opt_set_funcs,
188 .help = help
189 };
190 #else
191 stressor_info_t stress_sendfile_info = {
192 .stressor = stress_not_implemented,
193 .class = CLASS_PIPE_IO | CLASS_OS,
194 .opt_set_funcs = opt_set_funcs,
195 .help = help
196 };
197 #endif
198