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;
226 int ret, flags = MSG_DONTWAIT;
229 if (nd->net_protocol == IPPROTO_UDP) {
230 ret = sendto(io_u->file->fd, io_u->xfer_buf,
231 io_u->xfer_buflen, flags, &nd->addr,
235 * if we are going to write more, set MSG_MORE
238 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
242 ret = send(io_u->file->fd, io_u->xfer_buf,
243 io_u->xfer_buflen, flags);
248 ret = poll_wait(td, io_u->file->fd, POLLOUT);
252 flags &= ~MSG_DONTWAIT;
258 static int is_udp_close(struct io_u *io_u, int len)
260 struct udp_close_msg *msg;
262 if (len != sizeof(struct udp_close_msg))
265 msg = io_u->xfer_buf;
266 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
268 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
274 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
276 struct netio_data *nd = td->io_ops->data;
277 int ret, flags = MSG_DONTWAIT;
280 if (nd->net_protocol == IPPROTO_UDP) {
281 socklen_t len = sizeof(nd->addr);
283 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
284 io_u->xfer_buflen, flags, &nd->addr,
286 if (is_udp_close(io_u, ret)) {
291 ret = recv(io_u->file->fd, io_u->xfer_buf,
292 io_u->xfer_buflen, flags);
297 ret = poll_wait(td, io_u->file->fd, POLLIN);
300 flags &= ~MSG_DONTWAIT;
301 flags |= MSG_WAITALL;
307 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
309 struct netio_data *nd = td->io_ops->data;
312 fio_ro_check(td, io_u);
314 if (io_u->ddir == DDIR_WRITE) {
315 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
316 ret = fio_netio_send(td, io_u);
318 ret = fio_netio_splice_out(td, io_u);
319 } else if (io_u->ddir == DDIR_READ) {
320 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
321 ret = fio_netio_recv(td, io_u);
323 ret = fio_netio_splice_in(td, io_u);
325 ret = 0; /* must be a SYNC */
327 if (ret != (int) io_u->xfer_buflen) {
329 io_u->resid = io_u->xfer_buflen - ret;
331 return FIO_Q_COMPLETED;
335 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
343 td_verror(td, io_u->error, "xfer");
345 return FIO_Q_COMPLETED;
348 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
350 struct netio_data *nd = td->io_ops->data;
353 if (nd->net_protocol == IPPROTO_TCP)
358 f->fd = socket(AF_INET, type, nd->net_protocol);
360 td_verror(td, errno, "socket");
364 if (nd->net_protocol == IPPROTO_UDP)
367 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
368 td_verror(td, errno, "connect");
375 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
377 struct netio_data *nd = td->io_ops->data;
378 socklen_t socklen = sizeof(nd->addr);
380 if (nd->net_protocol == IPPROTO_UDP) {
381 f->fd = nd->listenfd;
385 log_info("fio: waiting for connection\n");
387 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
390 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
392 td_verror(td, errno, "accept");
399 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
402 return fio_netio_accept(td, f);
404 return fio_netio_connect(td, f);
407 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
409 struct netio_data *nd = td->io_ops->data;
410 struct udp_close_msg msg;
413 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
414 msg.cmd = htonl(FIO_LINK_CLOSE);
416 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, &nd->addr,
419 td_verror(td, errno, "sendto udp link close");
422 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
424 struct netio_data *nd = td->io_ops->data;
427 * If this is an UDP connection, notify the receiver that we are
428 * closing down the link
430 if (nd->net_protocol == IPPROTO_UDP)
431 fio_netio_udp_close(td, f);
433 return generic_close_file(td, f);
436 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
439 struct netio_data *nd = td->io_ops->data;
441 nd->addr.sin_family = AF_INET;
442 nd->addr.sin_port = htons(port);
444 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
445 struct hostent *hent;
447 hent = gethostbyname(host);
449 td_verror(td, errno, "gethostbyname");
453 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
459 static int fio_netio_setup_listen(struct thread_data *td, short port)
461 struct netio_data *nd = td->io_ops->data;
464 if (nd->net_protocol == IPPROTO_TCP)
469 fd = socket(AF_INET, type, nd->net_protocol);
471 td_verror(td, errno, "socket");
476 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
477 td_verror(td, errno, "setsockopt");
481 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
482 td_verror(td, errno, "setsockopt");
487 nd->addr.sin_family = AF_INET;
488 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
489 nd->addr.sin_port = htons(port);
491 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
492 td_verror(td, errno, "bind");
495 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
496 td_verror(td, errno, "listen");
504 static int fio_netio_init(struct thread_data *td)
506 struct netio_data *nd = td->io_ops->data;
508 char host[64], buf[128];
509 char *sep, *portp, *modep;
513 log_err("fio: network connections must be read OR write\n");
517 log_err("fio: network IO can't be random\n");
521 strcpy(buf, td->o.filename);
523 sep = strchr(buf, '/');
535 sep = strchr(portp, '/');
541 port = strtol(portp, NULL, 10);
542 if (!port || port > 65535)
546 if (!strncmp("tcp", modep, strlen(modep)) ||
547 !strncmp("TCP", modep, strlen(modep)))
548 nd->net_protocol = IPPROTO_TCP;
549 else if (!strncmp("udp", modep, strlen(modep)) ||
550 !strncmp("UDP", modep, strlen(modep)))
551 nd->net_protocol = IPPROTO_UDP;
555 nd->net_protocol = IPPROTO_TCP;
559 ret = fio_netio_setup_listen(td, port);
562 ret = fio_netio_setup_connect(td, host, port);
567 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
571 static void fio_netio_cleanup(struct thread_data *td)
573 struct netio_data *nd = td->io_ops->data;
576 if (nd->listenfd != -1)
578 if (nd->pipes[0] != -1)
580 if (nd->pipes[1] != -1)
587 static int fio_netio_setup(struct thread_data *td)
589 struct netio_data *nd;
591 if (!td->io_ops->data) {
592 nd = malloc(sizeof(*nd));;
594 memset(nd, 0, sizeof(*nd));
596 nd->pipes[0] = nd->pipes[1] = -1;
597 td->io_ops->data = nd;
603 #ifdef FIO_HAVE_SPLICE
604 static int fio_netio_setup_splice(struct thread_data *td)
606 struct netio_data *nd;
610 nd = td->io_ops->data;
612 if (pipe(nd->pipes) < 0)
622 static struct ioengine_ops ioengine_splice = {
624 .version = FIO_IOOPS_VERSION,
625 .prep = fio_netio_prep,
626 .queue = fio_netio_queue,
627 .setup = fio_netio_setup_splice,
628 .init = fio_netio_init,
629 .cleanup = fio_netio_cleanup,
630 .open_file = fio_netio_open_file,
631 .close_file = generic_close_file,
632 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
637 static struct ioengine_ops ioengine_rw = {
639 .version = FIO_IOOPS_VERSION,
640 .prep = fio_netio_prep,
641 .queue = fio_netio_queue,
642 .setup = fio_netio_setup,
643 .init = fio_netio_init,
644 .cleanup = fio_netio_cleanup,
645 .open_file = fio_netio_open_file,
646 .close_file = fio_netio_close_file,
647 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
651 static void fio_init fio_netio_register(void)
653 register_ioengine(&ioengine_rw);
654 #ifdef FIO_HAVE_SPLICE
655 register_ioengine(&ioengine_splice);
659 static void fio_exit fio_netio_unregister(void)
661 unregister_ioengine(&ioengine_rw);
662 #ifdef FIO_HAVE_SPLICE
663 unregister_ioengine(&ioengine_splice);