From: Steven Noonan Date: Wed, 28 Nov 2012 22:52:36 +0000 (-0800) Subject: net engine: implement option "nodelay" for TCP sockets X-Git-Tag: fio-2.0.14~58 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=70a7878c00e130affc3e0bd7d59bd83d57c3268e;p=fio.git net engine: implement option "nodelay" for TCP sockets 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 Signed-off-by: Jens Axboe --- diff --git a/engines/net.c b/engines/net.c index de7cdb54..b728df1f 100644 --- a/engines/net.c +++ b/engines/net.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -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;