blob: cd1fda9f54372526ddf0709c31941de77675cdd5 [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboed4f12dd2007-02-08 12:59:02 +01002 * Transfer data over the net.
Jens Axboeed92ac02007-02-06 14:43:52 +01003 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <errno.h>
8#include <assert.h>
9#include <netinet/in.h>
10#include <arpa/inet.h>
11#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010012#include <sys/poll.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010013
14#include "../fio.h"
15#include "../os.h"
16
Jens Axboeb5af8292007-03-08 12:43:13 +010017struct netio_data {
18 int listenfd;
19 int send_to_net;
20 char host[64];
21 struct sockaddr_in addr;
22};
Jens Axboeed92ac02007-02-06 14:43:52 +010023
24static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
25{
Jens Axboeb5af8292007-03-08 12:43:13 +010026 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010027 struct fio_file *f = io_u->file;
28
Jens Axboe7a6499d2007-02-07 09:35:29 +010029 /*
30 * Make sure we don't see spurious reads to a receiver, and vice versa
31 */
Jens Axboeb5af8292007-03-08 12:43:13 +010032 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
33 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010034 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010035 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010036 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010037
Jens Axboeed92ac02007-02-06 14:43:52 +010038 if (io_u->ddir == DDIR_SYNC)
39 return 0;
40 if (io_u->offset == f->last_completed_pos)
41 return 0;
42
Jens Axboee01547d2007-02-06 19:16:01 +010043 /*
44 * If offset is different from last end position, it's a seek.
45 * As network io is purely sequential, we don't allow seeks.
46 */
Jens Axboee1161c32007-02-22 19:36:48 +010047 td_verror(td, EINVAL, "cannot seek");
Jens Axboeed92ac02007-02-06 14:43:52 +010048 return 1;
49}
50
51static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
52{
Jens Axboeed92ac02007-02-06 14:43:52 +010053 struct fio_file *f = io_u->file;
Jens Axboed4f12dd2007-02-08 12:59:02 +010054 int ret, flags = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010055
Jens Axboe7a6499d2007-02-07 09:35:29 +010056 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010057 /*
58 * if we are going to write more, set MSG_MORE
59 */
60 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
61 td->io_size)
62 flags = MSG_MORE;
63
64 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
Jens Axboed4f12dd2007-02-08 12:59:02 +010065 } else if (io_u->ddir == DDIR_READ) {
66 flags = MSG_WAITALL;
67 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
68 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +010069 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +010070
Jens Axboecec6b552007-02-06 20:15:38 +010071 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010072 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010073 io_u->resid = io_u->xfer_buflen - ret;
74 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010075 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010076 } else
77 io_u->error = errno;
78 }
79
Jens Axboe36167d82007-02-18 05:41:31 +010080 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010081 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +010082
Jens Axboe36167d82007-02-18 05:41:31 +010083 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010084}
85
Jens Axboeb5af8292007-03-08 12:43:13 +010086static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +010087{
Jens Axboeb5af8292007-03-08 12:43:13 +010088 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010089
Jens Axboeb5af8292007-03-08 12:43:13 +010090 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
91 if (f->fd < 0) {
92 td_verror(td, errno, "socket");
93 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010094 }
95
Jens Axboeb5af8292007-03-08 12:43:13 +010096 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
97 td_verror(td, errno, "connect");
98 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010099 }
100
101 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100102}
103
Jens Axboeb5af8292007-03-08 12:43:13 +0100104static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100105{
Jens Axboeb5af8292007-03-08 12:43:13 +0100106 struct netio_data *nd = td->io_ops->data;
107 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100108 struct pollfd pfd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100109 int ret;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100110
Jens Axboeb5af8292007-03-08 12:43:13 +0100111 fprintf(f_out, "fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100112
113 /*
114 * Accept loop. poll for incoming events, accept them. Repeat until we
115 * have all connections.
116 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100117 while (!td->terminate) {
118 pfd.fd = nd->listenfd;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100119 pfd.events = POLLIN;
120
121 ret = poll(&pfd, 1, -1);
122 if (ret < 0) {
123 if (errno == EINTR)
124 continue;
125
Jens Axboee1161c32007-02-22 19:36:48 +0100126 td_verror(td, errno, "poll");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100127 break;
128 } else if (!ret)
129 continue;
130
Jens Axboe0c094422007-02-11 04:44:02 +0100131 /*
132 * should be impossible
133 */
134 if (!(pfd.revents & POLLIN))
135 continue;
136
Jens Axboeb5af8292007-03-08 12:43:13 +0100137 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
138 if (f->fd < 0) {
139 td_verror(td, errno, "accept");
140 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100141 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100142 break;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100143 }
144
145 return 0;
146}
147
Jens Axboeb5af8292007-03-08 12:43:13 +0100148
149static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100150{
Jens Axboeb5af8292007-03-08 12:43:13 +0100151 if (td_read(td))
152 return fio_netio_accept(td, f);
153 else
154 return fio_netio_connect(td, f);
155}
156
157static int fio_netio_setup_connect(struct thread_data *td, const char *host,
158 unsigned short port)
159{
160 struct netio_data *nd = td->io_ops->data;
161
162 nd->addr.sin_family = AF_INET;
163 nd->addr.sin_port = htons(port);
164
165 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
166 struct hostent *hent;
167
168 hent = gethostbyname(host);
169 if (!hent) {
170 td_verror(td, errno, "gethostbyname");
171 return 1;
172 }
173
174 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
175 }
176
177 return 0;
178}
179
180static int fio_netio_setup_listen(struct thread_data *td, short port)
181{
182 struct netio_data *nd = td->io_ops->data;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100183 int fd, opt;
Jens Axboeed92ac02007-02-06 14:43:52 +0100184
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100185 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboeed92ac02007-02-06 14:43:52 +0100186 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100187 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100188 return 1;
189 }
190
191 opt = 1;
192 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100193 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100194 return 1;
195 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100196#ifdef SO_REUSEPORT
197 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100198 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100199 return 1;
200 }
201#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100202
Jens Axboeb5af8292007-03-08 12:43:13 +0100203 nd->addr.sin_family = AF_INET;
204 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
205 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100206
Jens Axboeb5af8292007-03-08 12:43:13 +0100207 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100208 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100209 return 1;
210 }
211 if (listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100212 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100213 return 1;
214 }
215
Jens Axboeb5af8292007-03-08 12:43:13 +0100216 nd->listenfd = fd;
217 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100218}
219
Jens Axboe9bec88e2007-03-02 08:55:48 +0100220static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100221{
Jens Axboeb5af8292007-03-08 12:43:13 +0100222 struct netio_data *nd = td->io_ops->data;
Jens Axboee01547d2007-02-06 19:16:01 +0100223 unsigned short port;
Jens Axboe2fc26982007-02-06 19:27:27 +0100224 struct fio_file *f;
Jens Axboeb5af8292007-03-08 12:43:13 +0100225 char host[64], buf[128];
Jens Axboeaf52b342007-03-13 10:07:47 +0100226 unsigned int i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100227 char *sep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100228 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100229
Jens Axboe7a6499d2007-02-07 09:35:29 +0100230 if (!td->total_file_size) {
231 log_err("fio: need size= set\n");
232 return 1;
233 }
234
Jens Axboe413dd452007-02-23 09:26:09 +0100235 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100236 log_err("fio: network connections must be read OR write\n");
237 return 1;
238 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100239
240 strcpy(buf, td->filename);
241
Jens Axboe9f9214f2007-03-13 14:02:16 +0100242 sep = strchr(buf, '/');
Jens Axboeed92ac02007-02-06 14:43:52 +0100243 if (!sep) {
Jens Axboe9f9214f2007-03-13 14:02:16 +0100244 log_err("fio: bad network host/port <<%s>>\n", td->filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100245 return 1;
246 }
247
248 *sep = '\0';
249 sep++;
250 strcpy(host, buf);
Jens Axboee01547d2007-02-06 19:16:01 +0100251 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100252
Jens Axboe413dd452007-02-23 09:26:09 +0100253 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100254 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100255 ret = fio_netio_setup_listen(td, port);
256 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100257 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100258 ret = fio_netio_setup_connect(td, host, port);
259 }
260
Jens Axboe2fc26982007-02-06 19:27:27 +0100261 if (ret)
262 return ret;
263
264 td->io_size = td->total_file_size;
265 td->total_io_size = td->io_size;
266
267 for_each_file(td, f, i) {
268 f->file_size = td->total_file_size / td->nr_files;
269 f->real_file_size = f->file_size;
Jens Axboeed92ac02007-02-06 14:43:52 +0100270 }
271
Jens Axboe2fc26982007-02-06 19:27:27 +0100272 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100273}
274
Jens Axboeb5af8292007-03-08 12:43:13 +0100275static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100276{
Jens Axboeb5af8292007-03-08 12:43:13 +0100277 struct netio_data *nd = td->io_ops->data;
278
279 if (nd) {
280 free(nd);
281 td->io_ops->data = NULL;
282 }
283}
284
285static int fio_netio_setup(struct thread_data *td)
286{
287 struct netio_data *nd = malloc(sizeof(*nd));
288
289 memset(nd, 0, sizeof(*nd));
290 nd->listenfd = -1;
291 td->io_ops->data = nd;
Jens Axboe9bec88e2007-03-02 08:55:48 +0100292 return 0;
293}
294
Jens Axboeed92ac02007-02-06 14:43:52 +0100295static struct ioengine_ops ioengine = {
296 .name = "net",
297 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100298 .prep = fio_netio_prep,
299 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100300 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100301 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100302 .cleanup = fio_netio_cleanup,
303 .open_file = fio_netio_open_file,
304 .close_file = generic_close_file,
305 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100306};
307
308static void fio_init fio_netio_register(void)
309{
310 register_ioengine(&ioengine);
311}
312
313static void fio_exit fio_netio_unregister(void)
314{
315 unregister_ioengine(&ioengine);
316}