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 <snoonan@amazon.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/engines/net.c b/engines/net.c
index de7cdb5..b728df1 100644
--- a/engines/net.c
+++ b/engines/net.c
@@ -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 @@
 	unsigned int proto;
 	unsigned int listen;
 	unsigned int pingpong;
+	unsigned int nodelay;
 };
 
 struct udp_close_msg {
@@ -91,6 +93,12 @@
 		},
 	},
 	{
+		.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,
 		.off1	= offsetof(struct netio_options, listen),
@@ -448,7 +456,7 @@
 {
 	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 @@
 		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 @@
 	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 @@
 		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;