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 static int splice_io_u(int fdin, int fdout, unsigned int len)
49 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
67 * Receive bytes from a socket and fill them into the internal pipe
69 static int splice_in(struct thread_data *td, struct io_u *io_u)
71 struct netio_data *nd = td->io_ops->data;
73 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
77 * Transmit 'len' bytes from the internal pipe
79 static int splice_out(struct thread_data *td, struct io_u *io_u,
82 struct netio_data *nd = td->io_ops->data;
84 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
87 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
90 .iov_base = io_u->xfer_buf,
96 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
115 * vmsplice() pipe to io_u buffer
117 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
120 struct netio_data *nd = td->io_ops->data;
122 return vmsplice_io_u(io_u, nd->pipes[0], len);
126 * vmsplice() io_u to pipe
128 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
130 struct netio_data *nd = td->io_ops->data;
132 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
136 * splice receive - transfer socket data into a pipe using splice, then map
137 * that pipe data into the io_u using vmsplice.
139 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
143 ret = splice_in(td, io_u);
145 return vmsplice_io_u_out(td, io_u, ret);
151 * splice transmit - map data from the io_u into a pipe by using vmsplice,
152 * then transfer that pipe to a socket using splice.
154 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
158 ret = vmsplice_io_u_in(td, io_u);
160 return splice_out(td, io_u, ret);
165 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
170 * if we are going to write more, set MSG_MORE
172 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
175 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
178 static int fio_netio_recv(struct io_u *io_u)
180 int flags = MSG_WAITALL;
182 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
185 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
187 struct netio_data *nd = td->io_ops->data;
190 fio_ro_check(td, io_u);
192 if (io_u->ddir == DDIR_WRITE) {
194 ret = fio_netio_splice_out(td, io_u);
196 ret = fio_netio_send(td, io_u);
197 } else if (io_u->ddir == DDIR_READ) {
199 ret = fio_netio_splice_in(td, io_u);
201 ret = fio_netio_recv(io_u);
203 ret = 0; /* must be a SYNC */
205 if (ret != (int) io_u->xfer_buflen) {
207 io_u->resid = io_u->xfer_buflen - ret;
209 return FIO_Q_COMPLETED;
215 td_verror(td, io_u->error, "xfer");
217 return FIO_Q_COMPLETED;
220 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
222 struct netio_data *nd = td->io_ops->data;
224 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
226 td_verror(td, errno, "socket");
230 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
231 td_verror(td, errno, "connect");
238 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
240 struct netio_data *nd = td->io_ops->data;
241 socklen_t socklen = sizeof(nd->addr);
245 log_info("fio: waiting for connection\n");
248 * Accept loop. poll for incoming events, accept them. Repeat until we
249 * have all connections.
251 while (!td->terminate) {
252 pfd.fd = nd->listenfd;
255 ret = poll(&pfd, 1, -1);
260 td_verror(td, errno, "poll");
266 * should be impossible
268 if (!(pfd.revents & POLLIN))
271 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
273 td_verror(td, errno, "accept");
282 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
285 return fio_netio_accept(td, f);
287 return fio_netio_connect(td, f);
290 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
293 struct netio_data *nd = td->io_ops->data;
295 nd->addr.sin_family = AF_INET;
296 nd->addr.sin_port = htons(port);
298 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
299 struct hostent *hent;
301 hent = gethostbyname(host);
303 td_verror(td, errno, "gethostbyname");
307 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
313 static int fio_netio_setup_listen(struct thread_data *td, short port)
315 struct netio_data *nd = td->io_ops->data;
318 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
320 td_verror(td, errno, "socket");
325 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
326 td_verror(td, errno, "setsockopt");
330 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
331 td_verror(td, errno, "setsockopt");
336 nd->addr.sin_family = AF_INET;
337 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
338 nd->addr.sin_port = htons(port);
340 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
341 td_verror(td, errno, "bind");
344 if (listen(fd, 1) < 0) {
345 td_verror(td, errno, "listen");
353 static int fio_netio_init(struct thread_data *td)
355 struct netio_data *nd = td->io_ops->data;
357 char host[64], buf[128];
362 log_err("fio: network connections must be read OR write\n");
366 log_err("fio: network IO can't be random\n");
370 strcpy(buf, td->o.filename);
372 sep = strchr(buf, '/');
374 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
385 ret = fio_netio_setup_listen(td, port);
388 ret = fio_netio_setup_connect(td, host, port);
394 static void fio_netio_cleanup(struct thread_data *td)
396 struct netio_data *nd = td->io_ops->data;
399 if (nd->listenfd != -1)
401 if (nd->pipes[0] != -1)
403 if (nd->pipes[1] != -1)
407 td->io_ops->data = NULL;
411 static int fio_netio_setup(struct thread_data *td)
413 struct netio_data *nd;
415 if (!td->io_ops->data) {
416 nd = malloc(sizeof(*nd));;
418 memset(nd, 0, sizeof(*nd));
420 nd->pipes[0] = nd->pipes[1] = -1;
421 td->io_ops->data = nd;
427 static int fio_netio_setup_splice(struct thread_data *td)
429 struct netio_data *nd;
433 nd = td->io_ops->data;
435 if (pipe(nd->pipes) < 0)
445 static struct ioengine_ops ioengine_rw = {
447 .version = FIO_IOOPS_VERSION,
448 .prep = fio_netio_prep,
449 .queue = fio_netio_queue,
450 .setup = fio_netio_setup,
451 .init = fio_netio_init,
452 .cleanup = fio_netio_cleanup,
453 .open_file = fio_netio_open_file,
454 .close_file = generic_close_file,
455 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
459 static struct ioengine_ops ioengine_splice = {
461 .version = FIO_IOOPS_VERSION,
462 .prep = fio_netio_prep,
463 .queue = fio_netio_queue,
464 .setup = fio_netio_setup_splice,
465 .init = fio_netio_init,
466 .cleanup = fio_netio_cleanup,
467 .open_file = fio_netio_open_file,
468 .close_file = generic_close_file,
469 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
473 static void fio_init fio_netio_register(void)
475 register_ioengine(&ioengine_rw);
476 register_ioengine(&ioengine_splice);
479 static void fio_exit fio_netio_unregister(void)
481 unregister_ioengine(&ioengine_rw);
482 unregister_ioengine(&ioengine_splice);