4 * IO engine that reads/writes to/from sockets.
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
28 struct sockaddr_in addr;
31 struct udp_close_msg {
37 FIO_LINK_CLOSE = 0x89,
38 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
42 * Return -1 for error and 'nr events' for a positive number
45 static int poll_wait(struct thread_data *td, int fd, short events)
50 while (!td->terminate) {
53 ret = poll(&pfd, 1, -1);
58 td_verror(td, errno, "poll");
66 if (pfd.revents & events)
72 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
74 struct netio_data *nd = td->io_ops->data;
77 * Make sure we don't see spurious reads to a receiver, and vice versa
79 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
80 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
81 td_verror(td, EINVAL, "bad direction");
88 #ifdef FIO_HAVE_SPLICE
89 static int splice_io_u(int fdin, int fdout, unsigned int len)
94 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
112 * Receive bytes from a socket and fill them into the internal pipe
114 static int splice_in(struct thread_data *td, struct io_u *io_u)
116 struct netio_data *nd = td->io_ops->data;
118 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
122 * Transmit 'len' bytes from the internal pipe
124 static int splice_out(struct thread_data *td, struct io_u *io_u,
127 struct netio_data *nd = td->io_ops->data;
129 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
132 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
135 .iov_base = io_u->xfer_buf,
140 while (iov.iov_len) {
141 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
160 * vmsplice() pipe to io_u buffer
162 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
165 struct netio_data *nd = td->io_ops->data;
167 return vmsplice_io_u(io_u, nd->pipes[0], len);
171 * vmsplice() io_u to pipe
173 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
175 struct netio_data *nd = td->io_ops->data;
177 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
181 * splice receive - transfer socket data into a pipe using splice, then map
182 * that pipe data into the io_u using vmsplice.
184 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
188 ret = splice_in(td, io_u);
190 return vmsplice_io_u_out(td, io_u, ret);
196 * splice transmit - map data from the io_u into a pipe by using vmsplice,
197 * then transfer that pipe to a socket using splice.
199 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
203 ret = vmsplice_io_u_in(td, io_u);
205 return splice_out(td, io_u, ret);
210 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
216 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
223 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
225 struct netio_data *nd = td->io_ops->data;
228 flags = MSG_DONTWAIT;
232 if (nd->net_protocol == IPPROTO_UDP) {
233 struct sockaddr *to = (struct sockaddr *) &nd->addr;
235 ret = sendto(io_u->file->fd, io_u->xfer_buf,
236 io_u->xfer_buflen, flags, to,
240 * if we are going to write more, set MSG_MORE
243 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
247 ret = send(io_u->file->fd, io_u->xfer_buf,
248 io_u->xfer_buflen, flags);
253 ret = poll_wait(td, io_u->file->fd, POLLOUT);
258 flags &= ~MSG_DONTWAIT;
265 static int is_udp_close(struct io_u *io_u, int len)
267 struct udp_close_msg *msg;
269 if (len != sizeof(struct udp_close_msg))
272 msg = io_u->xfer_buf;
273 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
275 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
281 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
283 struct netio_data *nd = td->io_ops->data;
286 flags = MSG_DONTWAIT;
290 if (nd->net_protocol == IPPROTO_UDP) {
291 socklen_t len = sizeof(nd->addr);
292 struct sockaddr *from = (struct sockaddr *) &nd->addr;
294 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
295 io_u->xfer_buflen, flags, from, &len);
296 if (is_udp_close(io_u, ret)) {
301 ret = recv(io_u->file->fd, io_u->xfer_buf,
302 io_u->xfer_buflen, flags);
307 ret = poll_wait(td, io_u->file->fd, POLLIN);
311 flags &= ~MSG_DONTWAIT;
313 flags |= MSG_WAITALL;
319 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
321 struct netio_data *nd = td->io_ops->data;
324 fio_ro_check(td, io_u);
326 if (io_u->ddir == DDIR_WRITE) {
327 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
328 ret = fio_netio_send(td, io_u);
330 ret = fio_netio_splice_out(td, io_u);
331 } else if (io_u->ddir == DDIR_READ) {
332 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
333 ret = fio_netio_recv(td, io_u);
335 ret = fio_netio_splice_in(td, io_u);
337 ret = 0; /* must be a SYNC */
339 if (ret != (int) io_u->xfer_buflen) {
341 io_u->resid = io_u->xfer_buflen - ret;
343 return FIO_Q_COMPLETED;
347 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
355 td_verror(td, io_u->error, "xfer");
357 return FIO_Q_COMPLETED;
360 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
362 struct netio_data *nd = td->io_ops->data;
365 if (nd->net_protocol == IPPROTO_TCP)
370 f->fd = socket(AF_INET, type, nd->net_protocol);
372 td_verror(td, errno, "socket");
376 if (nd->net_protocol == IPPROTO_UDP)
379 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
380 td_verror(td, errno, "connect");
387 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
389 struct netio_data *nd = td->io_ops->data;
390 socklen_t socklen = sizeof(nd->addr);
392 if (nd->net_protocol == IPPROTO_UDP) {
393 f->fd = nd->listenfd;
397 log_info("fio: waiting for connection\n");
399 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
402 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
404 td_verror(td, errno, "accept");
411 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
414 return fio_netio_accept(td, f);
416 return fio_netio_connect(td, f);
419 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
421 struct netio_data *nd = td->io_ops->data;
422 struct udp_close_msg msg;
423 struct sockaddr *to = (struct sockaddr *) &nd->addr;
426 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
427 msg.cmd = htonl(FIO_LINK_CLOSE);
429 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
432 td_verror(td, errno, "sendto udp link close");
435 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
437 struct netio_data *nd = td->io_ops->data;
440 * If this is an UDP connection, notify the receiver that we are
441 * closing down the link
443 if (nd->net_protocol == IPPROTO_UDP)
444 fio_netio_udp_close(td, f);
446 return generic_close_file(td, f);
449 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
452 struct netio_data *nd = td->io_ops->data;
454 nd->addr.sin_family = AF_INET;
455 nd->addr.sin_port = htons(port);
457 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
458 struct hostent *hent;
460 hent = gethostbyname(host);
462 td_verror(td, errno, "gethostbyname");
466 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
472 static int fio_netio_setup_listen(struct thread_data *td, short port)
474 struct netio_data *nd = td->io_ops->data;
477 if (nd->net_protocol == IPPROTO_TCP)
482 fd = socket(AF_INET, type, nd->net_protocol);
484 td_verror(td, errno, "socket");
489 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
490 td_verror(td, errno, "setsockopt");
494 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
495 td_verror(td, errno, "setsockopt");
500 nd->addr.sin_family = AF_INET;
501 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
502 nd->addr.sin_port = htons(port);
504 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
505 td_verror(td, errno, "bind");
508 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
509 td_verror(td, errno, "listen");
517 static int fio_netio_init(struct thread_data *td)
519 struct netio_data *nd = td->io_ops->data;
521 char host[64], buf[128];
522 char *sep, *portp, *modep;
526 log_err("fio: network connections must be read OR write\n");
530 log_err("fio: network IO can't be random\n");
534 strcpy(buf, td->o.filename);
536 sep = strchr(buf, '/');
548 sep = strchr(portp, '/');
554 port = strtol(portp, NULL, 10);
555 if (!port || port > 65535)
559 if (!strncmp("tcp", modep, strlen(modep)) ||
560 !strncmp("TCP", modep, strlen(modep)))
561 nd->net_protocol = IPPROTO_TCP;
562 else if (!strncmp("udp", modep, strlen(modep)) ||
563 !strncmp("UDP", modep, strlen(modep)))
564 nd->net_protocol = IPPROTO_UDP;
568 nd->net_protocol = IPPROTO_TCP;
572 ret = fio_netio_setup_listen(td, port);
575 ret = fio_netio_setup_connect(td, host, port);
580 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
584 static void fio_netio_cleanup(struct thread_data *td)
586 struct netio_data *nd = td->io_ops->data;
589 if (nd->listenfd != -1)
591 if (nd->pipes[0] != -1)
593 if (nd->pipes[1] != -1)
600 static int fio_netio_setup(struct thread_data *td)
602 struct netio_data *nd;
604 if (!td->io_ops->data) {
605 nd = malloc(sizeof(*nd));;
607 memset(nd, 0, sizeof(*nd));
609 nd->pipes[0] = nd->pipes[1] = -1;
610 td->io_ops->data = nd;
616 #ifdef FIO_HAVE_SPLICE
617 static int fio_netio_setup_splice(struct thread_data *td)
619 struct netio_data *nd;
623 nd = td->io_ops->data;
625 if (pipe(nd->pipes) < 0)
635 static struct ioengine_ops ioengine_splice = {
637 .version = FIO_IOOPS_VERSION,
638 .prep = fio_netio_prep,
639 .queue = fio_netio_queue,
640 .setup = fio_netio_setup_splice,
641 .init = fio_netio_init,
642 .cleanup = fio_netio_cleanup,
643 .open_file = fio_netio_open_file,
644 .close_file = generic_close_file,
645 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
646 FIO_SIGQUIT | FIO_PIPEIO,
650 static struct ioengine_ops ioengine_rw = {
652 .version = FIO_IOOPS_VERSION,
653 .prep = fio_netio_prep,
654 .queue = fio_netio_queue,
655 .setup = fio_netio_setup,
656 .init = fio_netio_init,
657 .cleanup = fio_netio_cleanup,
658 .open_file = fio_netio_open_file,
659 .close_file = fio_netio_close_file,
660 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
661 FIO_SIGQUIT | FIO_PIPEIO,
664 static void fio_init fio_netio_register(void)
666 register_ioengine(&ioengine_rw);
667 #ifdef FIO_HAVE_SPLICE
668 register_ioengine(&ioengine_splice);
672 static void fio_exit fio_netio_unregister(void)
674 unregister_ioengine(&ioengine_rw);
675 #ifdef FIO_HAVE_SPLICE
676 unregister_ioengine(&ioengine_splice);