History

tcp: send in-queue bytes in cmsg upon read

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. V4 change-log: Removed inline from a static function.

Soheil Hassas Yeganeh committed 8 years ago

Commit b75eba71 parent ab855393 files · +43 −5Browse files

Showing the changes to include/uapi/linux (1 of 3 files). Show all

Unified · Split

include/uapi/linux/tcp.h+3−0
@@ -123,6 +123,9 @@ enum { · 122 unmodified lines
123123 #define TCP_FASTOPEN_KEY 33 /* Set the key for Fast Open (cookie) */
124124 #define TCP_FASTOPEN_NO_COOKIE 34 /* Enable TFO without a TFO cookie */
125125 #define TCP_ZEROCOPY_RECEIVE 35
126+#define TCP_INQ 36 /* Notify bytes available to read as a cmsg on read */
127+
128+#define TCP_CM_INQ TCP_INQ
126129
127130 struct tcp_repair_opt {
128131 __u32 opt_code;