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);
256 printf("got ret %d\n", ret);
261 td_verror(td, errno, "poll");
267 * should be impossible
269 if (!(pfd.revents & POLLIN))
272 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
274 td_verror(td, errno, "accept");
283 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
286 return fio_netio_accept(td, f);
288 return fio_netio_connect(td, f);
291 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
294 struct netio_data *nd = td->io_ops->data;
296 nd->addr.sin_family = AF_INET;
297 nd->addr.sin_port = htons(port);
299 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
300 struct hostent *hent;
302 hent = gethostbyname(host);
304 td_verror(td, errno, "gethostbyname");
308 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
314 static int fio_netio_setup_listen(struct thread_data *td, short port)
316 struct netio_data *nd = td->io_ops->data;
319 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
321 td_verror(td, errno, "socket");
326 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
327 td_verror(td, errno, "setsockopt");
331 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
332 td_verror(td, errno, "setsockopt");
337 nd->addr.sin_family = AF_INET;
338 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
339 nd->addr.sin_port = htons(port);
341 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
342 td_verror(td, errno, "bind");
345 if (listen(fd, 1) < 0) {
346 td_verror(td, errno, "listen");
354 static int fio_netio_init(struct thread_data *td)
356 struct netio_data *nd = td->io_ops->data;
358 char host[64], buf[128];
363 log_err("fio: network connections must be read OR write\n");
367 log_err("fio: network IO can't be random\n");
371 strcpy(buf, td->o.filename);
373 sep = strchr(buf, '/');
375 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
386 ret = fio_netio_setup_listen(td, port);
389 ret = fio_netio_setup_connect(td, host, port);
395 static void fio_netio_cleanup(struct thread_data *td)
397 struct netio_data *nd = td->io_ops->data;
400 if (nd->listenfd != -1)
402 if (nd->pipes[0] != -1)
404 if (nd->pipes[1] != -1)
408 td->io_ops->data = NULL;
412 static int fio_netio_setup(struct thread_data *td)
414 struct netio_data *nd;
416 if (!td->io_ops->data) {
417 nd = malloc(sizeof(*nd));;
419 memset(nd, 0, sizeof(*nd));
421 nd->pipes[0] = nd->pipes[1] = -1;
422 td->io_ops->data = nd;
428 static int fio_netio_setup_splice(struct thread_data *td)
430 struct netio_data *nd;
434 nd = td->io_ops->data;
436 if (pipe(nd->pipes) < 0)
446 static struct ioengine_ops ioengine_rw = {
448 .version = FIO_IOOPS_VERSION,
449 .prep = fio_netio_prep,
450 .queue = fio_netio_queue,
451 .setup = fio_netio_setup,
452 .init = fio_netio_init,
453 .cleanup = fio_netio_cleanup,
454 .open_file = fio_netio_open_file,
455 .close_file = generic_close_file,
456 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
460 static struct ioengine_ops ioengine_splice = {
462 .version = FIO_IOOPS_VERSION,
463 .prep = fio_netio_prep,
464 .queue = fio_netio_queue,
465 .setup = fio_netio_setup_splice,
466 .init = fio_netio_init,
467 .cleanup = fio_netio_cleanup,
468 .open_file = fio_netio_open_file,
469 .close_file = generic_close_file,
470 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
474 static void fio_init fio_netio_register(void)
476 register_ioengine(&ioengine_rw);
477 register_ioengine(&ioengine_splice);
480 static void fio_exit fio_netio_unregister(void)
482 unregister_ioengine(&ioengine_rw);
483 unregister_ioengine(&ioengine_splice);