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");
56 * Receive bytes from a socket and fill them into the internal pipe
58 static int splice_in(struct thread_data *td, struct io_u *io_u)
60 struct netio_data *nd = td->io_ops->data;
61 unsigned int len = io_u->xfer_buflen;
62 struct fio_file *f = io_u->file;
66 int ret = splice(nd->pipes[1], NULL, f->fd, NULL, len, 0);
83 * Transmit 'len' bytes from the internal pipe
85 static int splice_out(struct thread_data *td, struct io_u *io_u,
88 struct netio_data *nd = td->io_ops->data;
89 struct fio_file *f = io_u->file;
93 int ret = splice(nd->pipes[0], NULL, f->fd, NULL, len, 0);
111 * vmsplice() pipe to io_u buffer
113 static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
116 struct netio_data *nd = td->io_ops->data;
118 .iov_base = io_u->xfer_buf,
123 while (iov.iov_len) {
124 int ret = vmsplice(nd->pipes[0], &iov, 1, 0);
142 * vmsplice() io_u to pipe
144 static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
146 struct netio_data *nd = td->io_ops->data;
148 .iov_base = io_u->xfer_buf,
149 .iov_len = io_u->xfer_buflen,
151 unsigned int bytes = 0;
153 while (iov.iov_len) {
154 int ret = vmsplice(nd->pipes[1], &iov, 1, 0);
170 static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
174 ret = splice_in(td, io_u);
178 return vmsplice_io_u_out(td, io_u, ret);
181 static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
185 ret = vmsplice_io_u_in(td, io_u);
189 return splice_out(td, io_u, ret);
192 static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
197 * if we are going to write more, set MSG_MORE
199 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
202 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
205 static int fio_netio_recv(struct io_u *io_u)
207 int flags = MSG_WAITALL;
209 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
212 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
214 struct netio_data *nd = td->io_ops->data;
217 if (io_u->ddir == DDIR_WRITE) {
219 ret = fio_netio_splice_out(td, io_u);
221 ret = fio_netio_send(td, io_u);
222 } else if (io_u->ddir == DDIR_READ) {
224 ret = fio_netio_splice_in(td, io_u);
226 ret = fio_netio_recv(io_u);
228 ret = 0; /* must be a SYNC */
230 if (ret != (int) io_u->xfer_buflen) {
232 io_u->resid = io_u->xfer_buflen - ret;
234 return FIO_Q_COMPLETED;
240 td_verror(td, io_u->error, "xfer");
242 return FIO_Q_COMPLETED;
245 static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
247 struct netio_data *nd = td->io_ops->data;
249 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
251 td_verror(td, errno, "socket");
255 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
256 td_verror(td, errno, "connect");
263 static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
265 struct netio_data *nd = td->io_ops->data;
266 socklen_t socklen = sizeof(nd->addr);
270 log_info("fio: waiting for connection\n");
273 * Accept loop. poll for incoming events, accept them. Repeat until we
274 * have all connections.
276 while (!td->terminate) {
277 pfd.fd = nd->listenfd;
280 ret = poll(&pfd, 1, -1);
285 td_verror(td, errno, "poll");
291 * should be impossible
293 if (!(pfd.revents & POLLIN))
296 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
298 td_verror(td, errno, "accept");
308 static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
311 return fio_netio_accept(td, f);
313 return fio_netio_connect(td, f);
316 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
319 struct netio_data *nd = td->io_ops->data;
321 nd->addr.sin_family = AF_INET;
322 nd->addr.sin_port = htons(port);
324 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
325 struct hostent *hent;
327 hent = gethostbyname(host);
329 td_verror(td, errno, "gethostbyname");
333 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
339 static int fio_netio_setup_listen(struct thread_data *td, short port)
341 struct netio_data *nd = td->io_ops->data;
344 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
346 td_verror(td, errno, "socket");
351 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
352 td_verror(td, errno, "setsockopt");
356 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
357 td_verror(td, errno, "setsockopt");
362 nd->addr.sin_family = AF_INET;
363 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
364 nd->addr.sin_port = htons(port);
366 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
367 td_verror(td, errno, "bind");
370 if (listen(fd, 1) < 0) {
371 td_verror(td, errno, "listen");
379 static int fio_netio_init(struct thread_data *td)
381 struct netio_data *nd = td->io_ops->data;
383 char host[64], buf[128];
388 log_err("fio: network connections must be read OR write\n");
392 log_err("fio: network IO can't be random\n");
396 strcpy(buf, td->o.filename);
398 sep = strchr(buf, '/');
400 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
411 ret = fio_netio_setup_listen(td, port);
414 ret = fio_netio_setup_connect(td, host, port);
420 static void fio_netio_cleanup(struct thread_data *td)
422 struct netio_data *nd = td->io_ops->data;
426 td->io_ops->data = NULL;
430 static int fio_netio_setup(struct thread_data *td)
432 struct netio_data *nd;
434 if (!td->io_ops->data) {
435 nd = malloc(sizeof(*nd));;
437 memset(nd, 0, sizeof(*nd));
439 td->io_ops->data = nd;
445 static int fio_netio_setup_splice(struct thread_data *td)
447 struct netio_data *nd;
451 nd = td->io_ops->data;
453 if (pipe(nd->pipes) < 0)
463 static struct ioengine_ops ioengine_rw = {
465 .version = FIO_IOOPS_VERSION,
466 .prep = fio_netio_prep,
467 .queue = fio_netio_queue,
468 .setup = fio_netio_setup,
469 .init = fio_netio_init,
470 .cleanup = fio_netio_cleanup,
471 .open_file = fio_netio_open_file,
472 .close_file = generic_close_file,
473 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
476 static struct ioengine_ops ioengine_splice = {
478 .version = FIO_IOOPS_VERSION,
479 .prep = fio_netio_prep,
480 .queue = fio_netio_queue,
481 .setup = fio_netio_setup_splice,
482 .init = fio_netio_init,
483 .cleanup = fio_netio_cleanup,
484 .open_file = fio_netio_open_file,
485 .close_file = generic_close_file,
486 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
489 static void fio_init fio_netio_register(void)
491 register_ioengine(&ioengine_rw);
492 register_ioengine(&ioengine_splice);
495 static void fio_exit fio_netio_unregister(void)
497 unregister_ioengine(&ioengine_rw);
498 unregister_ioengine(&ioengine_splice);