blob: 4936cc24fbc74c9c3b538bbbb382c5214f970ba6 [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
Jens Axboeed92ac02007-02-06 14:43:52 +01006 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010015#include <sys/poll.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010016
17#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010018
Jens Axboeb5af8292007-03-08 12:43:13 +010019struct netio_data {
20 int listenfd;
21 int send_to_net;
Jens Axboe9cce02e2007-06-22 15:42:21 +020022 int use_splice;
23 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010024 char host[64];
25 struct sockaddr_in addr;
26};
Jens Axboeed92ac02007-02-06 14:43:52 +010027
28static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
29{
Jens Axboeb5af8292007-03-08 12:43:13 +010030 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010031 struct fio_file *f = io_u->file;
32
Jens Axboe7a6499d2007-02-07 09:35:29 +010033 /*
34 * Make sure we don't see spurious reads to a receiver, and vice versa
35 */
Jens Axboeb5af8292007-03-08 12:43:13 +010036 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
37 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010038 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010039 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010040 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010041
Jens Axboeed92ac02007-02-06 14:43:52 +010042 if (io_u->ddir == DDIR_SYNC)
43 return 0;
44 if (io_u->offset == f->last_completed_pos)
45 return 0;
46
Jens Axboee01547d2007-02-06 19:16:01 +010047 /*
48 * If offset is different from last end position, it's a seek.
49 * As network io is purely sequential, we don't allow seeks.
50 */
Jens Axboee1161c32007-02-22 19:36:48 +010051 td_verror(td, EINVAL, "cannot seek");
Jens Axboeed92ac02007-02-06 14:43:52 +010052 return 1;
53}
54
Jens Axboe9cce02e2007-06-22 15:42:21 +020055/*
56 * Receive bytes from a socket and fill them into the internal pipe
57 */
58static int splice_in(struct thread_data *td, struct io_u *io_u)
59{
60 struct netio_data *nd = td->io_ops->data;
61 unsigned int len = io_u->xfer_buflen;
62 struct fio_file *f = io_u->file;
63 int bytes = 0;
64
65 while (len) {
66 int ret = splice(nd->pipes[1], NULL, f->fd, NULL, len, 0);
67
68 if (ret < 0) {
69 if (!bytes)
70 bytes = ret;
71
72 break;
73 } else if (!ret)
74 break;
75
76 bytes += ret;
77 }
78
79 return bytes;
80}
81
82/*
83 * Transmit 'len' bytes from the internal pipe
84 */
85static int splice_out(struct thread_data *td, struct io_u *io_u,
86 unsigned int len)
87{
88 struct netio_data *nd = td->io_ops->data;
89 struct fio_file *f = io_u->file;
90 int bytes = 0;
91
92 while (len) {
93 int ret = splice(nd->pipes[0], NULL, f->fd, NULL, len, 0);
94
95 if (ret < 0) {
96 if (!bytes)
97 bytes = ret;
98
99 break;
100 } else if (!ret)
101 break;
102
103 bytes += ret;
104 len -= ret;
105 }
106
107 return bytes;
108}
109
110/*
111 * vmsplice() pipe to io_u buffer
112 */
113static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
114 unsigned int len)
115{
116 struct netio_data *nd = td->io_ops->data;
117 struct iovec iov = {
118 .iov_base = io_u->xfer_buf,
119 .iov_len = len,
120 };
121 int bytes = 0;
122
123 while (iov.iov_len) {
124 int ret = vmsplice(nd->pipes[0], &iov, 1, 0);
125
126 if (ret < 0) {
127 if (!bytes)
128 bytes = ret;
129 break;
130 } else if (!ret)
131 break;
132
133 iov.iov_len -= ret;
134 if (iov.iov_len)
135 iov.iov_base += ret;
136 }
137
138 return bytes;
139}
140
141/*
142 * vmsplice() io_u to pipe
143 */
144static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
145{
146 struct netio_data *nd = td->io_ops->data;
147 struct iovec iov = {
148 .iov_base = io_u->xfer_buf,
149 .iov_len = io_u->xfer_buflen,
150 };
151 unsigned int bytes = 0;
152
153 while (iov.iov_len) {
154 int ret = vmsplice(nd->pipes[1], &iov, 1, 0);
155
156 if (ret < 0)
157 return -1;
158 else if (!ret)
159 return bytes;
160
161 iov.iov_len -= ret;
162 bytes += ret;
163 if (iov.iov_len)
164 iov.iov_base += ret;
165 }
166
167 return bytes;
168}
169
170static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
171{
172 int ret;
173
174 ret = splice_in(td, io_u);
175 if (ret <= 0)
176 return ret;
177
178 return vmsplice_io_u_out(td, io_u, ret);
179}
180
181static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
182{
183 int ret;
184
185 ret = vmsplice_io_u_in(td, io_u);
186 if (ret <= 0)
187 return ret;
188
189 return splice_out(td, io_u, ret);
190}
191
192static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
193{
194 int flags = 0;
195
196 /*
197 * if we are going to write more, set MSG_MORE
198 */
199 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
200 flags = MSG_MORE;
201
202 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
203}
204
205static int fio_netio_recv(struct io_u *io_u)
206{
207 int flags = MSG_WAITALL;
208
209 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
210}
211
Jens Axboeed92ac02007-02-06 14:43:52 +0100212static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
213{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200214 struct netio_data *nd = td->io_ops->data;
215 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100216
Jens Axboe7a6499d2007-02-07 09:35:29 +0100217 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe9cce02e2007-06-22 15:42:21 +0200218 if (nd->use_splice)
219 ret = fio_netio_splice_out(td, io_u);
220 else
221 ret = fio_netio_send(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100222 } else if (io_u->ddir == DDIR_READ) {
Jens Axboe9cce02e2007-06-22 15:42:21 +0200223 if (nd->use_splice)
224 ret = fio_netio_splice_in(td, io_u);
225 else
226 ret = fio_netio_recv(io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100227 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100228 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100229
Jens Axboecec6b552007-02-06 20:15:38 +0100230 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100231 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100232 io_u->resid = io_u->xfer_buflen - ret;
233 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100234 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100235 } else
236 io_u->error = errno;
237 }
238
Jens Axboe36167d82007-02-18 05:41:31 +0100239 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100240 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100241
Jens Axboe36167d82007-02-18 05:41:31 +0100242 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100243}
244
Jens Axboeb5af8292007-03-08 12:43:13 +0100245static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100246{
Jens Axboeb5af8292007-03-08 12:43:13 +0100247 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +0100248
Jens Axboeb5af8292007-03-08 12:43:13 +0100249 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
250 if (f->fd < 0) {
251 td_verror(td, errno, "socket");
252 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100253 }
254
Jens Axboeb5af8292007-03-08 12:43:13 +0100255 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
256 td_verror(td, errno, "connect");
257 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100258 }
259
260 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100261}
262
Jens Axboeb5af8292007-03-08 12:43:13 +0100263static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100264{
Jens Axboeb5af8292007-03-08 12:43:13 +0100265 struct netio_data *nd = td->io_ops->data;
266 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100267 struct pollfd pfd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100268 int ret;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100269
Jens Axboe6d861442007-03-15 09:22:23 +0100270 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100271
272 /*
273 * Accept loop. poll for incoming events, accept them. Repeat until we
274 * have all connections.
275 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100276 while (!td->terminate) {
277 pfd.fd = nd->listenfd;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100278 pfd.events = POLLIN;
279
280 ret = poll(&pfd, 1, -1);
281 if (ret < 0) {
282 if (errno == EINTR)
283 continue;
284
Jens Axboee1161c32007-02-22 19:36:48 +0100285 td_verror(td, errno, "poll");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100286 break;
287 } else if (!ret)
288 continue;
289
Jens Axboe0c094422007-02-11 04:44:02 +0100290 /*
291 * should be impossible
292 */
293 if (!(pfd.revents & POLLIN))
294 continue;
295
Jens Axboeb5af8292007-03-08 12:43:13 +0100296 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
297 if (f->fd < 0) {
298 td_verror(td, errno, "accept");
299 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100300 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100301 break;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100302 }
303
304 return 0;
305}
306
Jens Axboeb5af8292007-03-08 12:43:13 +0100307
308static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100309{
Jens Axboeb5af8292007-03-08 12:43:13 +0100310 if (td_read(td))
311 return fio_netio_accept(td, f);
312 else
313 return fio_netio_connect(td, f);
314}
315
316static int fio_netio_setup_connect(struct thread_data *td, const char *host,
317 unsigned short port)
318{
319 struct netio_data *nd = td->io_ops->data;
320
321 nd->addr.sin_family = AF_INET;
322 nd->addr.sin_port = htons(port);
323
324 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
325 struct hostent *hent;
326
327 hent = gethostbyname(host);
328 if (!hent) {
329 td_verror(td, errno, "gethostbyname");
330 return 1;
331 }
332
333 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
334 }
335
336 return 0;
337}
338
339static int fio_netio_setup_listen(struct thread_data *td, short port)
340{
341 struct netio_data *nd = td->io_ops->data;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100342 int fd, opt;
Jens Axboeed92ac02007-02-06 14:43:52 +0100343
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100344 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboeed92ac02007-02-06 14:43:52 +0100345 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100346 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100347 return 1;
348 }
349
350 opt = 1;
351 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100352 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100353 return 1;
354 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100355#ifdef SO_REUSEPORT
356 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100357 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100358 return 1;
359 }
360#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100361
Jens Axboeb5af8292007-03-08 12:43:13 +0100362 nd->addr.sin_family = AF_INET;
363 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
364 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100365
Jens Axboeb5af8292007-03-08 12:43:13 +0100366 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100367 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100368 return 1;
369 }
370 if (listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100371 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100372 return 1;
373 }
374
Jens Axboeb5af8292007-03-08 12:43:13 +0100375 nd->listenfd = fd;
376 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100377}
378
Jens Axboe9bec88e2007-03-02 08:55:48 +0100379static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100380{
Jens Axboeb5af8292007-03-08 12:43:13 +0100381 struct netio_data *nd = td->io_ops->data;
Jens Axboee01547d2007-02-06 19:16:01 +0100382 unsigned short port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100383 char host[64], buf[128];
Jens Axboeed92ac02007-02-06 14:43:52 +0100384 char *sep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100385 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100386
Jens Axboe413dd452007-02-23 09:26:09 +0100387 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100388 log_err("fio: network connections must be read OR write\n");
389 return 1;
390 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200391 if (td_random(td)) {
392 log_err("fio: network IO can't be random\n");
393 return 1;
394 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100395
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100396 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100397
Jens Axboe9f9214f2007-03-13 14:02:16 +0100398 sep = strchr(buf, '/');
Jens Axboeed92ac02007-02-06 14:43:52 +0100399 if (!sep) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100400 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100401 return 1;
402 }
403
404 *sep = '\0';
405 sep++;
406 strcpy(host, buf);
Jens Axboee01547d2007-02-06 19:16:01 +0100407 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100408
Jens Axboe413dd452007-02-23 09:26:09 +0100409 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100410 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100411 ret = fio_netio_setup_listen(td, port);
412 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100413 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100414 ret = fio_netio_setup_connect(td, host, port);
415 }
416
Jens Axboe7bb48f82007-03-27 15:30:28 +0200417 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100418}
419
Jens Axboeb5af8292007-03-08 12:43:13 +0100420static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100421{
Jens Axboeb5af8292007-03-08 12:43:13 +0100422 struct netio_data *nd = td->io_ops->data;
423
424 if (nd) {
425 free(nd);
426 td->io_ops->data = NULL;
427 }
428}
429
430static int fio_netio_setup(struct thread_data *td)
431{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200432 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100433
Jens Axboe7bb48f82007-03-27 15:30:28 +0200434 if (!td->io_ops->data) {
435 nd = malloc(sizeof(*nd));;
436
437 memset(nd, 0, sizeof(*nd));
438 nd->listenfd = -1;
439 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200440 }
441
Jens Axboe9bec88e2007-03-02 08:55:48 +0100442 return 0;
443}
444
Jens Axboe9cce02e2007-06-22 15:42:21 +0200445static int fio_netio_setup_splice(struct thread_data *td)
446{
447 struct netio_data *nd;
448
449 fio_netio_setup(td);
450
451 nd = td->io_ops->data;
452 if (nd) {
453 if (pipe(nd->pipes) < 0)
454 return 1;
455
456 nd->use_splice = 1;
457 return 0;
458 }
459
460 return 1;
461}
462
463static struct ioengine_ops ioengine_rw = {
Jens Axboeed92ac02007-02-06 14:43:52 +0100464 .name = "net",
465 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100466 .prep = fio_netio_prep,
467 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100468 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100469 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100470 .cleanup = fio_netio_cleanup,
471 .open_file = fio_netio_open_file,
472 .close_file = generic_close_file,
473 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100474};
475
Jens Axboe9cce02e2007-06-22 15:42:21 +0200476static struct ioengine_ops ioengine_splice = {
477 .name = "netsplice",
478 .version = FIO_IOOPS_VERSION,
479 .prep = fio_netio_prep,
480 .queue = fio_netio_queue,
481 .setup = fio_netio_setup_splice,
482 .init = fio_netio_init,
483 .cleanup = fio_netio_cleanup,
484 .open_file = fio_netio_open_file,
485 .close_file = generic_close_file,
486 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
487};
488
Jens Axboeed92ac02007-02-06 14:43:52 +0100489static void fio_init fio_netio_register(void)
490{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200491 register_ioengine(&ioengine_rw);
492 register_ioengine(&ioengine_splice);
Jens Axboeed92ac02007-02-06 14:43:52 +0100493}
494
495static void fio_exit fio_netio_unregister(void)
496{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200497 unregister_ioengine(&ioengine_rw);
498 unregister_ioengine(&ioengine_splice);
Jens Axboeed92ac02007-02-06 14:43:52 +0100499}