net engine: implement option "nodelay" for TCP sockets
authorSteven Noonan <snoonan@amazon.com>
Wed, 28 Nov 2012 22:52:36 +0000 (14:52 -0800)
committerJens Axboe <axboe@kernel.dk>
Wed, 30 Jan 2013 20:39:11 +0000 (21:39 +0100)
With disks, O_DIRECT effectively bypasses all buffering/caching mechanisms and
ensures that the I/O is going directly to the disk. Since TCP is a streaming
protocol (like disk I/O), it also has a buffering mechanism. As with disks, it
is sometimes desirable to bypass buffering. To that end, we can use
TCP_NODELAY, which transmits the packet as soon as data is assembled,
regardless of whether it occupies a full frame.

Signed-off-by: Steven Noonan <snoonan@amazon.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
engines/net.c

index de7cdb541824bc35d8eb3cb97121f24bcce5bde3..b728df1f7385f751d86ed63e526be0c71782745a 100644 (file)
@@ -11,6 +11,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <netinet/in.h>
+#include <netinet/tcp.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <sys/poll.h>
@@ -35,6 +36,7 @@ struct netio_options {
        unsigned int proto;
        unsigned int listen;
        unsigned int pingpong;
+       unsigned int nodelay;
 };
 
 struct udp_close_msg {
@@ -90,6 +92,12 @@ static struct fio_option options[] = {
                          },
                },
        },
+       {
+               .name   = "nodelay",
+               .type   = FIO_OPT_BOOL,
+               .off1   = offsetof(struct netio_options, nodelay),
+               .help   = "Use TCP_NODELAY on TCP connections",
+       },
        {
                .name   = "listen",
                .type   = FIO_OPT_STR_SET,
@@ -448,7 +456,7 @@ static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
 {
        struct netio_data *nd = td->io_ops->data;
        struct netio_options *o = td->eo;
-       int type, domain;
+       int type, domain, optval;
 
        if (o->proto == FIO_TYPE_TCP) {
                domain = AF_INET;
@@ -471,6 +479,14 @@ static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
                return 1;
        }
 
+       if (o->nodelay && o->proto == FIO_TYPE_TCP) {
+               optval = 1;
+               if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)) < 0) {
+                       log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
+                       return 1;
+               }
+       }
+
        if (o->proto == FIO_TYPE_UDP)
                return 0;
        else if (o->proto == FIO_TYPE_TCP) {
@@ -502,7 +518,7 @@ static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
        struct netio_data *nd = td->io_ops->data;
        struct netio_options *o = td->eo;
        socklen_t socklen = sizeof(nd->addr);
-       int state;
+       int state, optval;
 
        if (o->proto == FIO_TYPE_UDP) {
                f->fd = nd->listenfd;
@@ -523,6 +539,14 @@ static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
                goto err;
        }
 
+       if (o->nodelay && o->proto == FIO_TYPE_TCP) {
+               optval = 1;
+               if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(int)) < 0) {
+                       log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
+                       return 1;
+               }
+       }
+
        reset_all_stats(td);
        td_set_runstate(td, state);
        return 0;