Skip to contentSkip to conversation

Adds CMSG+rx timestamps to TCP rx. zerocopy

PATCH Superseded Arjun Roy 2 participants 5 messages Dec 11, 2020 – Dec 15, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Arjun Roy

author
#0 lore raw

cover Adds CMSG+rx timestamps to TCP rx. zerocopy

From: Arjun Roy <[email protected]>

This patch series provides CMSG and receive timestamp support to TCP receive zerocopy. Patch 1 refactors CMSG pending state for tcp_recvmsg() to avoid the use of magic numbers; patch 2 implements receive timestamp via CMSG support for receive zerocopy, and uses the constants added in patch 1.

Arjun Roy (2):
tcp: Remove CMSG magic numbers for tcp_recvmsg().
tcp: Add receive timestamp support for receive zerocopy.

 include/uapi/linux/tcp.h |   4 ++
 net/ipv4/tcp.c           | 114 ++++++++++++++++++++++++++++-----------
 2 files changed, 87 insertions(+), 31 deletions(-)
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#1 lore raw

PATCH 1/2 tcp: Remove CMSG magic numbers for tcp_recvmsg().

From: Arjun Roy <[email protected]>

At present, tcp_recvmsg() uses flags to track if any CMSGs are pending and what those CMSGs are. These flags are currently magic numbers, used only within tcp_recvmsg().

To prepare for receive timestamp support in tcp receive zerocopy, gently refactor these magic numbers into enums. This is purely a clean-up patch and introduces no change in behaviour.

---
 net/ipv4/tcp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
net/ipv4/tcp.c +10 -4
index ed42d2193c5c..d2d9f62dfc88 100644
@@ -280,6 +280,12 @@
 #include <asm/ioctls.h>
 #include <net/busy_poll.h>
 
+/* Track pending CMSGs. */
+enum {
+	TCP_CMSG_INQ = 1,
+	TCP_CMSG_TS = 2
+};
+
 struct percpu_counter tcp_orphan_count;
 EXPORT_SYMBOL_GPL(tcp_orphan_count);
 
@@ -2272,7 +2278,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 		goto out;
 
 	if (tp->recvmsg_inq)
-		*cmsg_flags = 1;
+		*cmsg_flags = TCP_CMSG_INQ;
 	timeo = sock_rcvtimeo(sk, nonblock);
 
 	/* Urgent data needs to be handled specially. */
@@ -2453,7 +2459,7 @@ static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len,
 
 		if (TCP_SKB_CB(skb)->has_rxtstamp) {
 			tcp_update_recv_tstamps(skb, tss);
-			*cmsg_flags |= 2;
+			*cmsg_flags |= TCP_CMSG_TS;
 		}
 
 		if (used + offset < skb->len)
@@ -2513,9 +2519,9 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
 	release_sock(sk);
 
 	if (cmsg_flags && ret >= 0) {
-		if (cmsg_flags & 2)
+		if (cmsg_flags & TCP_CMSG_TS)
 			tcp_recv_timestamp(msg, sk, &tss);
-		if (cmsg_flags & 1) {
+		if (cmsg_flags & TCP_CMSG_INQ) {
 			inq = tcp_inq_hint(sk);
 			put_cmsg(msg, SOL_TCP, TCP_CM_INQ, sizeof(inq), &inq);
 		}
Signature · 1 line
2.29.2.576.ga3fc446d84-goog
author
#2 lore raw

PATCH 2/2 tcp: Add receive timestamp support for receive zerocopy.

In reply to Arjun Roy

From: Arjun Roy <[email protected]>

tcp_recvmsg() uses the CMSG mechanism to receive control information like packet receive timestamps. This patch adds CMSG fields to struct tcp_zerocopy_receive, and provides receive timestamps if available to the user.

---
 include/uapi/linux/tcp.h |   4 ++
 net/ipv4/tcp.c           | 100 ++++++++++++++++++++++++++++-----------
 2 files changed, 77 insertions(+), 27 deletions(-)
include/uapi/linux/tcp.h +4 -0
index 13ceeb395eb8..50ce6cfd1416 100644
@@ -353,5 +353,9 @@ struct tcp_zerocopy_receive {
 	__u64 copybuf_address;	/* in: copybuf address (small reads) */
 	__s32 copybuf_len; /* in/out: copybuf bytes avail/used or error */
 	__u32 flags; /* in: flags */
+	__u64 msg_control; /* ancillary data */
+	__u64 msg_controllen;
+	__u32 msg_flags;
+	/* __u32 hole;  Next we must add >1 u32 otherwise length checks fail. */
 };
 #endif /* _UAPI_LINUX_TCP_H */
net/ipv4/tcp.c +73 -27

190 lines not on this page · raw message

Signature · 1 line
2.29.2.576.ga3fc446d84-goog
maintainer
#3 lore raw

Re: [net-next 2/2] tcp: Add receive timestamp support for receive zerocopy.

Quoted 15 lines · On Fri, 11 Dec 2020 10:44:19 -0800 Arjun Roy wrote:

From: Arjun Roy <[email protected]>

tcp_recvmsg() uses the CMSG mechanism to receive control information like packet receive timestamps. This patch adds CMSG fields to struct tcp_zerocopy_receive, and provides receive timestamps if available to the user.

net/ipv4/tcp.c +0 -0
 			      struct scm_timestamping_internal *tss,
 			      int *cmsg_flags);
 static int receive_fallback_to_copy(struct sock *sk,

net/ipv4/tcp.c static int tcp_recvmsg_locked(struct sock *sk, struct msghdr *msg, size_t len, · line 1871

Quoted 10 lines
net/ipv4/tcp.c +3 -3
-				    struct tcp_zerocopy_receive *zc, int inq)
+				    struct tcp_zerocopy_receive *zc, int inq,
+				    struct scm_timestamping_internal *tss)
 {
 	unsigned long copy_address = (unsigned long)zc->copybuf_address;
-	struct scm_timestamping_internal tss_unused;
-	int err, cmsg_flags_unused;
+	int err;
 	struct msghdr msg = {};
 	struct iovec iov;

rev xmas tree

net/ipv4/tcp.c static int tcp_zerocopy_handle_leftover_data(struct tcp_zerocopy_receive *zc, · line 1932

Quoted 8 lines
net/ipv4/tcp.c +3 -1
@@ -1913,17 +1927,24 @@ static int tcp_zerocopy_handle_leftover_data(struct tcp_zerocopy_receive *zc,
 					     struct sock *sk,
 					     struct sk_buff *skb,
 					     u32 *seq,
-					     s32 copybuf_len)
+					     s32 copybuf_len,
+					     struct scm_timestamping_internal
+						*tss)

I appreciate the attempt to make the code fit 80 chars, but this particular wrapping does more harm than good.

Quoted 14 lines
net/ipv4/tcp.c +10 -0
@@ -4116,6 +4138,30 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
 		default:
 			goto zerocopy_rcv_out;
 		}
+zerocopy_rcv_cmsg:
+		if (zc.msg_flags & TCP_CMSG_TS) {
+			unsigned long msg_control_addr;
+			struct msghdr cmsg_dummy;
+
+			msg_control_addr = (unsigned long)zc.msg_control;
+			cmsg_dummy.msg_control = (void *)msg_control_addr;
+			cmsg_dummy.msg_controllen =
+				(__kernel_size_t)zc.msg_controllen;
+			cmsg_dummy.msg_flags = in_compat_syscall()

net/ipv4/tcp.c static int do_tcp_getsockopt(struct sock *sk, int level, · line 4160

Quoted 10 lines
net/ipv4/tcp.c +10 -0
+				? MSG_CMSG_COMPAT : 0;
+			zc.msg_flags = 0;
+			if (zc.msg_control == msg_control_addr &&
+			    zc.msg_controllen == cmsg_dummy.msg_controllen) {
+				tcp_recv_timestamp(&cmsg_dummy, sk, &tss);
+				zc.msg_control = (__u64)
+					((uintptr_t)cmsg_dummy.msg_control);
+				zc.msg_controllen =
+					(__u64)cmsg_dummy.msg_controllen;
+				zc.msg_flags = (__u32)cmsg_dummy.msg_flags;

This is indented by 4 levels. Time to create a helper?

Do we really need to cast each of these assignments explicitly?

maintainer
#4 lore raw
In reply to Arjun Roy
Quoted 7 lines · On Fri, 11 Dec 2020 10:44:17 -0800 Arjun Roy wrote:

From: Arjun Roy <[email protected]>

This patch series provides CMSG and receive timestamp support to TCP receive zerocopy. Patch 1 refactors CMSG pending state for tcp_recvmsg() to avoid the use of magic numbers; patch 2 implements receive timestamp via CMSG support for receive zerocopy, and uses the constants added in patch 1.

Imperative please:

tcp: add CMSG+rx timestamps to rx. zerocopy

Provide CMSG and receive timestamp...