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;
31 struct fio_file *f = io_u->file;
34 * Make sure we don't see spurious reads to a receiver, and vice versa
36 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
37 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
38 td_verror(td, EINVAL, "bad direction");
42 if (io_u->ddir == DDIR_SYNC)
44 if (io_u->offset == f->last_completed_pos)
48 * If offset is different from last end position, it's a seek.
49 * As network io is purely sequential, we don't allow seeks.
51 td_verror(td, EINVAL, "cannot seek");
55 static int splice_io_u(int fdin, int fdout, unsigned int len)
60 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
78 * Receive bytes from a socket and fill them into the internal pipe
80 static int splice_in(struct thread_data *td, struct io_u *io_u)
82 struct netio_data *nd = td->io_ops->data;
84 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
88 * Transmit 'len' bytes from the internal pipe
90 static int splice_out(struct thread_data *td, struct io_u *io_u,
93 struct netio_data *nd = td->io_ops->data;
95 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
98 static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
101 .iov_base = io_u->xfer_buf,
106 while (iov.iov_len) {
107 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
126 * vmsplice() pipe to io_u buffer
128 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
131 struct netio_data *nd = td->io_ops->data;
133 return vmsplice_io_u(io_u, nd->pipes[0], len);
137 * vmsplice() io_u to pipe
139 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
141 struct netio_data *nd = td->io_ops->data;
143 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
147 * splice receive - transfer socket data into a pipe using splice, then map
148 * that pipe data into the io_u using vmsplice.
150 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
154 ret = splice_in(td, io_u);
156 return vmsplice_io_u_out(td, io_u, ret);
162 * splice transmit - map data from the io_u into a pipe by using vmsplice,
163 * then transfer that pipe to a socket using splice.
165 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
169 ret = vmsplice_io_u_in(td, io_u);
171 return splice_out(td, io_u, ret);
176 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
181 * if we are going to write more, set MSG_MORE
183 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
186 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
189 static int fio_netio_recv(struct io_u *io_u)
191 int flags = MSG_WAITALL;
193 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
196 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
198 struct netio_data *nd = td->io_ops->data;
201 if (io_u->ddir == DDIR_WRITE) {
203 ret = fio_netio_splice_out(td, io_u);
205 ret = fio_netio_send(td, io_u);
206 } else if (io_u->ddir == DDIR_READ) {
208 ret = fio_netio_splice_in(td, io_u);
210 ret = fio_netio_recv(io_u);
212 ret = 0; /* must be a SYNC */
214 if (ret != (int) io_u->xfer_buflen) {
216 io_u->resid = io_u->xfer_buflen - ret;
218 return FIO_Q_COMPLETED;
224 td_verror(td, io_u->error, "xfer");
226 return FIO_Q_COMPLETED;
229 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
231 struct netio_data *nd = td->io_ops->data;
233 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
235 td_verror(td, errno, "socket");
239 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
240 td_verror(td, errno, "connect");
247 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
249 struct netio_data *nd = td->io_ops->data;
250 socklen_t socklen = sizeof(nd->addr);
254 log_info("fio: waiting for connection\n");
257 * Accept loop. poll for incoming events, accept them. Repeat until we
258 * have all connections.
260 while (!td->terminate) {
261 pfd.fd = nd->listenfd;
264 ret = poll(&pfd, 1, -1);
269 td_verror(td, errno, "poll");
275 * should be impossible
277 if (!(pfd.revents & POLLIN))
280 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
282 td_verror(td, errno, "accept");
291 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
294 return fio_netio_accept(td, f);
296 return fio_netio_connect(td, f);
299 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
302 struct netio_data *nd = td->io_ops->data;
304 nd->addr.sin_family = AF_INET;
305 nd->addr.sin_port = htons(port);
307 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
308 struct hostent *hent;
310 hent = gethostbyname(host);
312 td_verror(td, errno, "gethostbyname");
316 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
322 static int fio_netio_setup_listen(struct thread_data *td, short port)
324 struct netio_data *nd = td->io_ops->data;
327 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
329 td_verror(td, errno, "socket");
334 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
335 td_verror(td, errno, "setsockopt");
339 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
340 td_verror(td, errno, "setsockopt");
345 nd->addr.sin_family = AF_INET;
346 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
347 nd->addr.sin_port = htons(port);
349 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
350 td_verror(td, errno, "bind");
353 if (listen(fd, 1) < 0) {
354 td_verror(td, errno, "listen");
362 static int fio_netio_init(struct thread_data *td)
364 struct netio_data *nd = td->io_ops->data;
366 char host[64], buf[128];
371 log_err("fio: network connections must be read OR write\n");
375 log_err("fio: network IO can't be random\n");
379 strcpy(buf, td->o.filename);
381 sep = strchr(buf, '/');
383 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
394 ret = fio_netio_setup_listen(td, port);
397 ret = fio_netio_setup_connect(td, host, port);
403 static void fio_netio_cleanup(struct thread_data *td)
405 struct netio_data *nd = td->io_ops->data;
408 if (nd->listenfd != -1)
410 if (nd->pipes[0] != -1)
412 if (nd->pipes[1] != -1)
416 td->io_ops->data = NULL;
420 static int fio_netio_setup(struct thread_data *td)
422 struct netio_data *nd;
424 if (!td->io_ops->data) {
425 nd = malloc(sizeof(*nd));;
427 memset(nd, 0, sizeof(*nd));
429 nd->pipes[0] = nd->pipes[1] = -1;
430 td->io_ops->data = nd;
436 static int fio_netio_setup_splice(struct thread_data *td)
438 struct netio_data *nd;
442 nd = td->io_ops->data;
444 if (pipe(nd->pipes) < 0)
454 static struct ioengine_ops ioengine_rw = {
456 .version = FIO_IOOPS_VERSION,
457 .prep = fio_netio_prep,
458 .queue = fio_netio_queue,
459 .setup = fio_netio_setup,
460 .init = fio_netio_init,
461 .cleanup = fio_netio_cleanup,
462 .open_file = fio_netio_open_file,
463 .close_file = generic_close_file,
464 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR,
467 static struct ioengine_ops ioengine_splice = {
469 .version = FIO_IOOPS_VERSION,
470 .prep = fio_netio_prep,
471 .queue = fio_netio_queue,
472 .setup = fio_netio_setup_splice,
473 .init = fio_netio_init,
474 .cleanup = fio_netio_cleanup,
475 .open_file = fio_netio_open_file,
476 .close_file = generic_close_file,
477 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR,
480 static void fio_init fio_netio_register(void)
482 register_ioengine(&ioengine_rw);
483 register_ioengine(&ioengine_splice);
486 static void fio_exit fio_netio_unregister(void)
488 unregister_ioengine(&ioengine_rw);
489 unregister_ioengine(&ioengine_splice);