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 = OS_MSG_DONTWAIT;
229 if (nd->net_protocol == IPPROTO_UDP) {
230 struct sockaddr *to = (struct sockaddr *) &nd->addr;
232 ret = sendto(io_u->file->fd, io_u->xfer_buf,
233 io_u->xfer_buflen, flags, to,
237 * if we are going to write more, set MSG_MORE
240 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
244 ret = send(io_u->file->fd, io_u->xfer_buf,
245 io_u->xfer_buflen, flags);
250 ret = poll_wait(td, io_u->file->fd, POLLOUT);
254 flags &= ~OS_MSG_DONTWAIT;
260 static int is_udp_close(struct io_u *io_u, int len)
262 struct udp_close_msg *msg;
264 if (len != sizeof(struct udp_close_msg))
267 msg = io_u->xfer_buf;
268 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
270 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
276 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
278 struct netio_data *nd = td->io_ops->data;
279 int ret, flags = OS_MSG_DONTWAIT;
282 if (nd->net_protocol == IPPROTO_UDP) {
284 int len = sizeof(nd->addr);
286 socklen_t len = sizeof(nd->addr);
288 struct sockaddr *from = (struct sockaddr *) &nd->addr;
290 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
291 io_u->xfer_buflen, flags, from, &len);
292 if (is_udp_close(io_u, ret)) {
297 ret = recv(io_u->file->fd, io_u->xfer_buf,
298 io_u->xfer_buflen, flags);
303 ret = poll_wait(td, io_u->file->fd, POLLIN);
306 flags &= ~OS_MSG_DONTWAIT;
307 flags |= MSG_WAITALL;
313 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
315 struct netio_data *nd = td->io_ops->data;
318 fio_ro_check(td, io_u);
320 if (io_u->ddir == DDIR_WRITE) {
321 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
322 ret = fio_netio_send(td, io_u);
324 ret = fio_netio_splice_out(td, io_u);
325 } else if (io_u->ddir == DDIR_READ) {
326 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
327 ret = fio_netio_recv(td, io_u);
329 ret = fio_netio_splice_in(td, io_u);
331 ret = 0; /* must be a SYNC */
333 if (ret != (int) io_u->xfer_buflen) {
335 io_u->resid = io_u->xfer_buflen - ret;
337 return FIO_Q_COMPLETED;
341 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
349 td_verror(td, io_u->error, "xfer");
351 return FIO_Q_COMPLETED;
354 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
356 struct netio_data *nd = td->io_ops->data;
359 if (nd->net_protocol == IPPROTO_TCP)
364 f->fd = socket(AF_INET, type, nd->net_protocol);
366 td_verror(td, errno, "socket");
370 if (nd->net_protocol == IPPROTO_UDP)
373 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
374 td_verror(td, errno, "connect");
381 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
383 struct netio_data *nd = td->io_ops->data;
385 int socklen = sizeof(nd->addr);
387 socklen_t socklen = sizeof(nd->addr);
390 if (nd->net_protocol == IPPROTO_UDP) {
391 f->fd = nd->listenfd;
395 log_info("fio: waiting for connection\n");
397 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
400 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
402 td_verror(td, errno, "accept");
409 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
412 return fio_netio_accept(td, f);
414 return fio_netio_connect(td, f);
417 static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
419 struct netio_data *nd = td->io_ops->data;
420 struct udp_close_msg msg;
421 struct sockaddr *to = (struct sockaddr *) &nd->addr;
424 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
425 msg.cmd = htonl(FIO_LINK_CLOSE);
427 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
430 td_verror(td, errno, "sendto udp link close");
433 static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
435 struct netio_data *nd = td->io_ops->data;
438 * If this is an UDP connection, notify the receiver that we are
439 * closing down the link
441 if (nd->net_protocol == IPPROTO_UDP)
442 fio_netio_udp_close(td, f);
444 return generic_close_file(td, f);
447 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
450 struct netio_data *nd = td->io_ops->data;
452 nd->addr.sin_family = AF_INET;
453 nd->addr.sin_port = htons(port);
455 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
456 struct hostent *hent;
458 hent = gethostbyname(host);
460 td_verror(td, errno, "gethostbyname");
464 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
470 static int fio_netio_setup_listen(struct thread_data *td, short port)
472 struct netio_data *nd = td->io_ops->data;
475 if (nd->net_protocol == IPPROTO_TCP)
480 fd = socket(AF_INET, type, nd->net_protocol);
482 td_verror(td, errno, "socket");
487 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
488 td_verror(td, errno, "setsockopt");
492 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
493 td_verror(td, errno, "setsockopt");
498 nd->addr.sin_family = AF_INET;
499 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
500 nd->addr.sin_port = htons(port);
502 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
503 td_verror(td, errno, "bind");
506 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
507 td_verror(td, errno, "listen");
515 static int fio_netio_init(struct thread_data *td)
517 struct netio_data *nd = td->io_ops->data;
519 char host[64], buf[128];
520 char *sep, *portp, *modep;
524 log_err("fio: network connections must be read OR write\n");
528 log_err("fio: network IO can't be random\n");
532 strcpy(buf, td->o.filename);
534 sep = strchr(buf, '/');
546 sep = strchr(portp, '/');
552 port = strtol(portp, NULL, 10);
553 if (!port || port > 65535)
557 if (!strncmp("tcp", modep, strlen(modep)) ||
558 !strncmp("TCP", modep, strlen(modep)))
559 nd->net_protocol = IPPROTO_TCP;
560 else if (!strncmp("udp", modep, strlen(modep)) ||
561 !strncmp("UDP", modep, strlen(modep)))
562 nd->net_protocol = IPPROTO_UDP;
566 nd->net_protocol = IPPROTO_TCP;
570 ret = fio_netio_setup_listen(td, port);
573 ret = fio_netio_setup_connect(td, host, port);
578 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
582 static void fio_netio_cleanup(struct thread_data *td)
584 struct netio_data *nd = td->io_ops->data;
587 if (nd->listenfd != -1)
589 if (nd->pipes[0] != -1)
591 if (nd->pipes[1] != -1)
598 static int fio_netio_setup(struct thread_data *td)
600 struct netio_data *nd;
602 if (!td->io_ops->data) {
603 nd = malloc(sizeof(*nd));;
605 memset(nd, 0, sizeof(*nd));
607 nd->pipes[0] = nd->pipes[1] = -1;
608 td->io_ops->data = nd;
614 #ifdef FIO_HAVE_SPLICE
615 static int fio_netio_setup_splice(struct thread_data *td)
617 struct netio_data *nd;
621 nd = td->io_ops->data;
623 if (pipe(nd->pipes) < 0)
633 static struct ioengine_ops ioengine_splice = {
635 .version = FIO_IOOPS_VERSION,
636 .prep = fio_netio_prep,
637 .queue = fio_netio_queue,
638 .setup = fio_netio_setup_splice,
639 .init = fio_netio_init,
640 .cleanup = fio_netio_cleanup,
641 .open_file = fio_netio_open_file,
642 .close_file = generic_close_file,
643 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
644 FIO_SIGTERM | FIO_PIPEIO,
648 static struct ioengine_ops ioengine_rw = {
650 .version = FIO_IOOPS_VERSION,
651 .prep = fio_netio_prep,
652 .queue = fio_netio_queue,
653 .setup = fio_netio_setup,
654 .init = fio_netio_init,
655 .cleanup = fio_netio_cleanup,
656 .open_file = fio_netio_open_file,
657 .close_file = fio_netio_close_file,
658 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
659 FIO_SIGTERM | FIO_PIPEIO,
662 static void fio_init fio_netio_register(void)
664 register_ioengine(&ioengine_rw);
665 #ifdef FIO_HAVE_SPLICE
666 register_ioengine(&ioengine_splice);
670 static void fio_exit fio_netio_unregister(void)
672 unregister_ioengine(&ioengine_rw);
673 #ifdef FIO_HAVE_SPLICE
674 unregister_ioengine(&ioengine_splice);