tcp: send in-queue bytes in cmsg upon read
Subsystems (4)
PATCH v3 1/2 tcp: send in-queue bytes in cmsg upon read
From: Soheil Hassas Yeganeh <[email protected]>
Applications with many concurrent connections, high variance in receive queue length and tight memory bounds cannot allocate worst-case buffer size to drain sockets. Knowing the size of receive queue length, applications can optimize how they allocate buffers to read from the socket.
The number of bytes pending on the socket is directly available through ioctl(FIONREAD/SIOCINQ) and can be approximated using getsockopt(MEMINFO) (rmem_alloc includes skb overheads in addition to application data). But, both of these options add an extra syscall per recvmsg. Moreover, ioctl(FIONREAD/SIOCINQ) takes the socket lock.
Add the TCP_INQ socket option to TCP. When this socket option is set, recvmsg() relays the number of bytes available on the socket for reading to the application via the TCP_CM_INQ control message.
Calculate the number of bytes after releasing the socket lock to include the processed backlog, if any. To avoid an extra branch in the hot path of recvmsg() for this new control message, move all cmsg processing inside an existing branch for processing receive timestamps. Since the socket lock is not held when calculating the size of receive queue, TCP_INQ is a hint. For example, it can overestimate the queue size by one byte, if FIN is received.
With this method, applications can start reading from the socket using a small buffer, and then use larger buffers based on the remaining data when needed.
V3 change-log:
As suggested by David Miller, added loads with barrier to check whether we have multiple threads calling recvmsg in parallel. When that happens we lock the socket to calculate inq.
--- include/linux/tcp.h | 2 +- include/uapi/linux/tcp.h | 3 +++ net/ipv4/tcp.c | 43 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-)
include/linux/tcp.h +1 -1
@@ -228,7 +228,7 @@ struct tcp_sock { unused:2 ; u8 nonagle : 4,/* Disable Nagle algorithm? */ thin_lto : 1,/* Use linear timeouts for thin streams */ - unused1 : 1, + recvmsg_inq : 1,/* Indicate # of bytes in queue upon recvmsg */ repair : 1, frto : 1;/* F-RTO (RFC5682) activated in CA_Loss */u8 repair_queue;
include/uapi/linux/tcp.h +3 -0
@@ -123,6 +123,9 @@ enum {#define TCP_FASTOPEN_KEY 33/* Set the key for Fast Open (cookie) */ #define TCP_FASTOPEN_NO_COOKIE 34/* Enable TFO without a TFO cookie */ #define TCP_ZEROCOPY_RECEIVE 35 +#define TCP_INQ 36/* Notify bytes available to read as a cmsg on read */ + +#define TCP_CM_INQ TCP_INQstruct tcp_repair_opt {__u32 opt_code;
net/ipv4/tcp.c +39 -4
95 lines not on this page · raw message
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
PATCH v3 2/2 selftest: add test for TCP_INQ
From: Soheil Hassas Yeganeh <[email protected]>
--- tools/testing/selftests/net/Makefile | 3 +- tools/testing/selftests/net/tcp_inq.c | 189 ++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/net/tcp_inq.c
tools/testing/selftests/net/Makefile +2 -1
@@ -9,7 +9,7 @@ TEST_PROGS += fib_tests.sh fib-onlink-tests.sh in_netns.sh pmtu.sh udpgso.sh TEST_PROGS += udpgso_bench.sh TEST_GEN_FILES = socket TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy -TEST_GEN_FILES += tcp_mmap +TEST_GEN_FILES += tcp_mmap tcp_inq TEST_GEN_PROGS = reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa TEST_GEN_PROGS += reuseport_dualstack reuseaddr_conflict TEST_GEN_PROGS += udpgso udpgso_bench_tx udpgso_bench_rx
@@ -18,3 +18,4 @@ include ../lib.mk $(OUTPUT)/reuseport_bpf_numa: LDFLAGS += -lnuma $(OUTPUT)/tcp_mmap: LDFLAGS += -lpthread +$(OUTPUT)/tcp_inq: LDFLAGS += -lpthread
tools/testing/selftests/net/tcp_inq.c new file +189 -0
189 lines not on this page · raw message
Signature · 1 line
2.17.0.441.gb46fe60e1d-goog
David Miller
maintainerFrom: Soheil Hassas Yeganeh <[email protected]>
Date: Tue, 1 May 2018 10:11:27 -0400
Quoted 1 line
+static inline int
tcp_inq_hint(struct sock *sk)
Please do not use 'inline' in foo.c files, let the compiler decide.
Otherwise looks great, thanks.
Quoted 8 lines · David Miller
From: Soheil Hassas Yeganeh <[email protected]>
Date: Tue, 1 May 2018 10:11:27 -0400Quoted 1 line
+static inline int
tcp_inq_hint(struct sock *sk)Please do not use 'inline' in
foo.cfiles, let the compiler decide.Otherwise looks great, thanks.
Oops, sorry about this. Will send a v4.