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 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
33 struct netio_data *nd = td->io_ops->data;
36 * Make sure we don't see spurious reads to a receiver, and vice versa
38 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
39 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
40 td_verror(td, EINVAL, "bad direction");
47 #ifdef FIO_HAVE_SPLICE
48 static int splice_io_u(int fdin, int fdout, unsigned int len)
53 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
71 * Receive bytes from a socket and fill them into the internal pipe
73 static int splice_in(struct thread_data *td, struct io_u *io_u)
75 struct netio_data *nd = td->io_ops->data;
77 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
81 * Transmit 'len' bytes from the internal pipe
83 static int splice_out(struct thread_data *td, struct io_u *io_u,
86 struct netio_data *nd = td->io_ops->data;
88 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
91 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
94 .iov_base = io_u->xfer_buf,
100 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
119 * vmsplice() pipe to io_u buffer
121 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
124 struct netio_data *nd = td->io_ops->data;
126 return vmsplice_io_u(io_u, nd->pipes[0], len);
130 * vmsplice() io_u to pipe
132 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
134 struct netio_data *nd = td->io_ops->data;
136 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
140 * splice receive - transfer socket data into a pipe using splice, then map
141 * that pipe data into the io_u using vmsplice.
143 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
147 ret = splice_in(td, io_u);
149 return vmsplice_io_u_out(td, io_u, ret);
155 * splice transmit - map data from the io_u into a pipe by using vmsplice,
156 * then transfer that pipe to a socket using splice.
158 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
162 ret = vmsplice_io_u_in(td, io_u);
164 return splice_out(td, io_u, ret);
169 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
175 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
182 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
184 struct netio_data *nd = td->io_ops->data;
188 * if we are going to write more, set MSG_MORE
191 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
195 if (nd->net_protocol == IPPROTO_UDP) {
196 return sendto(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
197 0, &nd->addr, sizeof(nd->addr));
199 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
204 static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
206 struct netio_data *nd = td->io_ops->data;
207 int flags = MSG_WAITALL;
209 if (nd->net_protocol == IPPROTO_UDP) {
210 socklen_t len = sizeof(nd->addr);
212 return recvfrom(io_u->file->fd, io_u->xfer_buf,
213 io_u->xfer_buflen, 0, &nd->addr, &len);
215 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
220 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
222 struct netio_data *nd = td->io_ops->data;
225 fio_ro_check(td, io_u);
227 if (io_u->ddir == DDIR_WRITE) {
228 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
229 ret = fio_netio_send(td, io_u);
231 ret = fio_netio_splice_out(td, io_u);
232 } else if (io_u->ddir == DDIR_READ) {
233 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
234 ret = fio_netio_recv(td, io_u);
236 ret = fio_netio_splice_in(td, io_u);
238 ret = 0; /* must be a SYNC */
240 if (ret != (int) io_u->xfer_buflen) {
242 io_u->resid = io_u->xfer_buflen - ret;
244 return FIO_Q_COMPLETED;
248 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
256 td_verror(td, io_u->error, "xfer");
258 return FIO_Q_COMPLETED;
261 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
263 struct netio_data *nd = td->io_ops->data;
266 if (nd->net_protocol == IPPROTO_TCP)
271 f->fd = socket(AF_INET, type, nd->net_protocol);
273 td_verror(td, errno, "socket");
277 if (nd->net_protocol == IPPROTO_UDP)
280 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
281 td_verror(td, errno, "connect");
288 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
290 struct netio_data *nd = td->io_ops->data;
291 socklen_t socklen = sizeof(nd->addr);
295 if (nd->net_protocol == IPPROTO_UDP) {
296 f->fd = nd->listenfd;
300 log_info("fio: waiting for connection\n");
303 * Accept loop. poll for incoming events, accept them. Repeat until we
304 * have all connections.
306 while (!td->terminate) {
307 pfd.fd = nd->listenfd;
310 ret = poll(&pfd, 1, -1);
315 td_verror(td, errno, "poll");
321 * should be impossible
323 if (!(pfd.revents & POLLIN))
326 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
328 td_verror(td, errno, "accept");
337 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
340 return fio_netio_accept(td, f);
342 return fio_netio_connect(td, f);
345 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
348 struct netio_data *nd = td->io_ops->data;
350 nd->addr.sin_family = AF_INET;
351 nd->addr.sin_port = htons(port);
353 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
354 struct hostent *hent;
356 hent = gethostbyname(host);
358 td_verror(td, errno, "gethostbyname");
362 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
368 static int fio_netio_setup_listen(struct thread_data *td, short port)
370 struct netio_data *nd = td->io_ops->data;
373 if (nd->net_protocol == IPPROTO_TCP)
378 fd = socket(AF_INET, type, nd->net_protocol);
380 td_verror(td, errno, "socket");
385 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
386 td_verror(td, errno, "setsockopt");
390 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
391 td_verror(td, errno, "setsockopt");
396 nd->addr.sin_family = AF_INET;
397 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
398 nd->addr.sin_port = htons(port);
400 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
401 td_verror(td, errno, "bind");
404 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
405 td_verror(td, errno, "listen");
413 static int fio_netio_init(struct thread_data *td)
415 struct netio_data *nd = td->io_ops->data;
417 char host[64], buf[128];
418 char *sep, *portp, *modep;
422 log_err("fio: network connections must be read OR write\n");
426 log_err("fio: network IO can't be random\n");
430 strcpy(buf, td->o.filename);
432 sep = strchr(buf, '/');
444 sep = strchr(portp, '/');
450 port = strtol(portp, NULL, 10);
451 if (!port || port > 65535)
455 if (!strncmp("tcp", modep, strlen(modep)) ||
456 !strncmp("TCP", modep, strlen(modep)))
457 nd->net_protocol = IPPROTO_TCP;
458 else if (!strncmp("udp", modep, strlen(modep)) ||
459 !strncmp("UDP", modep, strlen(modep)))
460 nd->net_protocol = IPPROTO_UDP;
464 nd->net_protocol = IPPROTO_TCP;
468 ret = fio_netio_setup_listen(td, port);
471 ret = fio_netio_setup_connect(td, host, port);
476 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
480 static void fio_netio_cleanup(struct thread_data *td)
482 struct netio_data *nd = td->io_ops->data;
485 if (nd->listenfd != -1)
487 if (nd->pipes[0] != -1)
489 if (nd->pipes[1] != -1)
496 static int fio_netio_setup(struct thread_data *td)
498 struct netio_data *nd;
500 if (!td->io_ops->data) {
501 nd = malloc(sizeof(*nd));;
503 memset(nd, 0, sizeof(*nd));
505 nd->pipes[0] = nd->pipes[1] = -1;
506 td->io_ops->data = nd;
512 #ifdef FIO_HAVE_SPLICE
513 static int fio_netio_setup_splice(struct thread_data *td)
515 struct netio_data *nd;
519 nd = td->io_ops->data;
521 if (pipe(nd->pipes) < 0)
531 static struct ioengine_ops ioengine_splice = {
533 .version = FIO_IOOPS_VERSION,
534 .prep = fio_netio_prep,
535 .queue = fio_netio_queue,
536 .setup = fio_netio_setup_splice,
537 .init = fio_netio_init,
538 .cleanup = fio_netio_cleanup,
539 .open_file = fio_netio_open_file,
540 .close_file = generic_close_file,
541 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
546 static struct ioengine_ops ioengine_rw = {
548 .version = FIO_IOOPS_VERSION,
549 .prep = fio_netio_prep,
550 .queue = fio_netio_queue,
551 .setup = fio_netio_setup,
552 .init = fio_netio_init,
553 .cleanup = fio_netio_cleanup,
554 .open_file = fio_netio_open_file,
555 .close_file = generic_close_file,
556 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
560 static void fio_init fio_netio_register(void)
562 register_ioengine(&ioengine_rw);
563 #ifdef FIO_HAVE_SPLICE
564 register_ioengine(&ioengine_splice);
568 static void fio_exit fio_netio_unregister(void)
570 unregister_ioengine(&ioengine_rw);
571 #ifdef FIO_HAVE_SPLICE
572 unregister_ioengine(&ioengine_splice);