4 * IO engine that reads/writes to/from sockets.
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
25 struct sockaddr_in addr;
28 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
30 struct netio_data *nd = td->io_ops->data;
33 * Make sure we don't see spurious reads to a receiver, and vice versa
35 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
36 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
37 td_verror(td, EINVAL, "bad direction");
44 #ifdef FIO_HAVE_SPLICE
45 static int splice_io_u(int fdin, int fdout, unsigned int len)
50 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
68 * Receive bytes from a socket and fill them into the internal pipe
70 static int splice_in(struct thread_data *td, struct io_u *io_u)
72 struct netio_data *nd = td->io_ops->data;
74 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
78 * Transmit 'len' bytes from the internal pipe
80 static int splice_out(struct thread_data *td, struct io_u *io_u,
83 struct netio_data *nd = td->io_ops->data;
85 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
88 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
91 .iov_base = io_u->xfer_buf,
97 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
116 * vmsplice() pipe to io_u buffer
118 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
121 struct netio_data *nd = td->io_ops->data;
123 return vmsplice_io_u(io_u, nd->pipes[0], len);
127 * vmsplice() io_u to pipe
129 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
131 struct netio_data *nd = td->io_ops->data;
133 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
137 * splice receive - transfer socket data into a pipe using splice, then map
138 * that pipe data into the io_u using vmsplice.
140 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
144 ret = splice_in(td, io_u);
146 return vmsplice_io_u_out(td, io_u, ret);
152 * splice transmit - map data from the io_u into a pipe by using vmsplice,
153 * then transfer that pipe to a socket using splice.
155 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
159 ret = vmsplice_io_u_in(td, io_u);
161 return splice_out(td, io_u, ret);
166 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
172 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
179 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
184 * if we are going to write more, set MSG_MORE
187 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
191 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
194 static int fio_netio_recv(struct io_u *io_u)
196 int flags = MSG_WAITALL;
198 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
201 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
203 struct netio_data *nd = td->io_ops->data;
206 fio_ro_check(td, io_u);
208 if (io_u->ddir == DDIR_WRITE) {
210 ret = fio_netio_splice_out(td, io_u);
212 ret = fio_netio_send(td, io_u);
213 } else if (io_u->ddir == DDIR_READ) {
215 ret = fio_netio_splice_in(td, io_u);
217 ret = fio_netio_recv(io_u);
219 ret = 0; /* must be a SYNC */
221 if (ret != (int) io_u->xfer_buflen) {
223 io_u->resid = io_u->xfer_buflen - ret;
225 return FIO_Q_COMPLETED;
231 td_verror(td, io_u->error, "xfer");
233 return FIO_Q_COMPLETED;
236 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
238 struct netio_data *nd = td->io_ops->data;
240 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
242 td_verror(td, errno, "socket");
246 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
247 td_verror(td, errno, "connect");
254 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
256 struct netio_data *nd = td->io_ops->data;
257 socklen_t socklen = sizeof(nd->addr);
261 log_info("fio: waiting for connection\n");
264 * Accept loop. poll for incoming events, accept them. Repeat until we
265 * have all connections.
267 while (!td->terminate) {
268 pfd.fd = nd->listenfd;
271 ret = poll(&pfd, 1, -1);
276 td_verror(td, errno, "poll");
282 * should be impossible
284 if (!(pfd.revents & POLLIN))
287 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
289 td_verror(td, errno, "accept");
298 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
301 return fio_netio_accept(td, f);
303 return fio_netio_connect(td, f);
306 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
309 struct netio_data *nd = td->io_ops->data;
311 nd->addr.sin_family = AF_INET;
312 nd->addr.sin_port = htons(port);
314 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
315 struct hostent *hent;
317 hent = gethostbyname(host);
319 td_verror(td, errno, "gethostbyname");
323 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
329 static int fio_netio_setup_listen(struct thread_data *td, short port)
331 struct netio_data *nd = td->io_ops->data;
334 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
336 td_verror(td, errno, "socket");
341 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
342 td_verror(td, errno, "setsockopt");
346 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
347 td_verror(td, errno, "setsockopt");
352 nd->addr.sin_family = AF_INET;
353 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
354 nd->addr.sin_port = htons(port);
356 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
357 td_verror(td, errno, "bind");
360 if (listen(fd, 1) < 0) {
361 td_verror(td, errno, "listen");
369 static int fio_netio_init(struct thread_data *td)
371 struct netio_data *nd = td->io_ops->data;
373 char host[64], buf[128];
378 log_err("fio: network connections must be read OR write\n");
382 log_err("fio: network IO can't be random\n");
386 strcpy(buf, td->o.filename);
388 sep = strchr(buf, '/');
398 port = strtol(sep, NULL, 10);
399 if (!port || port > 65535)
404 ret = fio_netio_setup_listen(td, port);
407 ret = fio_netio_setup_connect(td, host, port);
412 log_err("fio: bad network host/port: %s\n", td->o.filename);
416 static void fio_netio_cleanup(struct thread_data *td)
418 struct netio_data *nd = td->io_ops->data;
421 if (nd->listenfd != -1)
423 if (nd->pipes[0] != -1)
425 if (nd->pipes[1] != -1)
432 static int fio_netio_setup(struct thread_data *td)
434 struct netio_data *nd;
436 if (!td->io_ops->data) {
437 nd = malloc(sizeof(*nd));;
439 memset(nd, 0, sizeof(*nd));
441 nd->pipes[0] = nd->pipes[1] = -1;
442 td->io_ops->data = nd;
448 #ifdef FIO_HAVE_SPLICE
449 static int fio_netio_setup_splice(struct thread_data *td)
451 struct netio_data *nd;
455 nd = td->io_ops->data;
457 if (pipe(nd->pipes) < 0)
467 static struct ioengine_ops ioengine_splice = {
469 .version = FIO_IOOPS_VERSION,
470 .prep = fio_netio_prep,
471 .queue = fio_netio_queue,
472 .setup = fio_netio_setup_splice,
473 .init = fio_netio_init,
474 .cleanup = fio_netio_cleanup,
475 .open_file = fio_netio_open_file,
476 .close_file = generic_close_file,
477 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
482 static struct ioengine_ops ioengine_rw = {
484 .version = FIO_IOOPS_VERSION,
485 .prep = fio_netio_prep,
486 .queue = fio_netio_queue,
487 .setup = fio_netio_setup,
488 .init = fio_netio_init,
489 .cleanup = fio_netio_cleanup,
490 .open_file = fio_netio_open_file,
491 .close_file = generic_close_file,
492 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
496 static void fio_init fio_netio_register(void)
498 register_ioengine(&ioengine_rw);
499 #ifdef FIO_HAVE_SPLICE
500 register_ioengine(&ioengine_splice);
504 static void fio_exit fio_netio_unregister(void)
506 unregister_ioengine(&ioengine_rw);
507 #ifdef FIO_HAVE_SPLICE
508 unregister_ioengine(&ioengine_splice);