Skip to contentSkip to conversation

tcp: add CMSG+rx timestamps to rx. zerocopy

PATCH Applied Arjun Roy 5 participants 12 messages Jan 21, 2021 – Feb 5, 2021

Subsystems (3)
lore
Share & tools
lore
author
#1 lore raw

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

In reply to Arjun Roy

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.

---
 net/ipv4/tcp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
net/ipv4/tcp.c +10 -4
index 856ae516ac18..28ca6a024f63 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.30.0.284.gd98b1dd5eaa7-goog
author
#2 lore raw

PATCH v2 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           | 116 ++++++++++++++++++++++++++++-----------
 2 files changed, 88 insertions(+), 32 deletions(-)
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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 +84 -32

210 lines not on this page · raw message

Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog

Arjun Roy

author
#0 lore raw

v2 cover tcp: add CMSG+rx timestamps to rx. zerocopy

From: Arjun Roy <[email protected]>

Provide 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.

v2: Fixes various stylistic comments and introduces a helper method to reduce indentation.

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           | 130 ++++++++++++++++++++++++++++-----------
 2 files changed, 98 insertions(+), 36 deletions(-)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
maintainer Applied
#3 lore raw

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

In reply to Arjun Roy

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Applied, thanks!

#4 lore raw

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

Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

#5 lore raw

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

Quoted 18 lines · David Ahern
Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Quoted 4 lines

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

This is the schema (reserved field without union) used by the RDMA UAPIs from the beginning (>20 years already) and it works like a charm.

Highly recommend :).

Thanks

Quoted 1 line
#6 lore raw

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

Quoted 24 lines · Leon Romanovsky
Quoted 18 lines · David Ahern
Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Quoted 4 lines

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

Quoted 5 lines

This is the schema (reserved field without union) used by the RDMA UAPIs from the beginning (>20 years already) and it works like a charm.

Highly recommend :).

agreed.

Arjun: would you mind following up with a patch to make this hole explicit and usable for the next extension? Thanks,

#7 lore raw

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

Quoted 29 lines · David Ahern
Quoted 27 lines · Leon Romanovsky
Quoted 18 lines · David Ahern
Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Quoted 4 lines

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

This is the schema (reserved field without union) used by the RDMA UAPIs from the beginning (>20 years already) and it works like a charm.

Quoted 7 lines
Quoted 2 lines

Highly recommend :).

agreed.

Arjun: would you mind following up with a patch to make this hole explicit and usable for the next extension? Thanks,

Will do.

-Arjun

#8 lore raw

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

Quoted 33 lines · Arjun Roy
Quoted 32 lines · David Ahern
Quoted 29 lines · Leon Romanovsky
Quoted 18 lines · David Ahern
Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Quoted 4 lines

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

This is the schema (reserved field without union) used by the RDMA UAPIs from the beginning (>20 years already) and it works like a charm.

Highly recommend :).

Quoted 6 lines
Quoted 4 lines

agreed.

Arjun: would you mind following up with a patch to make this hole explicit and usable for the next extension? Thanks,

Will do.

Please pay attention that all "in" and "out" fields that marked as reserved should be zeroed and kernel must check "in" field to ensure future compatibility.

Thanks

Quoted 2 lines

-Arjun

#9 lore raw

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

Quoted 40 lines · Leon Romanovsky
Quoted 38 lines · Arjun Roy
Quoted 36 lines · David Ahern
Quoted 24 lines · Leon Romanovsky
Quoted 18 lines · David Ahern
Quoted 16 lines · Jakub Kicinski

include/uapi/linux/tcp.h struct tcp_zerocopy_receive { · line 359

Quoted 12 lines · On Wed, 20 Jan 2021 16:41:48 -0800 Arjun Roy wrote:
include/uapi/linux/tcp.h +4 -0
index 768e93bd5b51..b216270105af 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. */

Well, let's hope nobody steps on this landmine.. :)

Quoted 4 lines

Past suggestions were made to use anonymous declarations - e.g., __u32 :32; - as a way of reserving the space for future use. That or declare '__u32 resvd', check that it must be 0 and makes it available for later (either directly or with a union).

Quoted 5 lines

This is the schema (reserved field without union) used by the RDMA UAPIs from the beginning (>20 years already) and it works like a charm.

Highly recommend :).

agreed.

Arjun: would you mind following up with a patch to make this hole explicit and usable for the next extension? Thanks,

Quoted 7 lines
Quoted 1 line

Will do.

Please pay attention that all "in" and "out" fields that marked as reserved should be zeroed and kernel must check "in" field to ensure future compatibility.

Thanks

A question about the approach where we mandate it as a reserved field; assuming in the future it is only used as an OUT field where 0 is a meaningful no-op value, then just setting it to 0 works just fine.

But, if it's an IN or IN-OUT field, it seems like mandating that the application set it to 0 could break the case where a future application sets it to some non-zero value and runs on an older kernel. And allowing it to be non-zero can maybe yield an unexpected outcome if an old application that did not zero it runs on a newer kernel.

So: maybe the right move is to mark it as reserved, not care what the input value is, always set it to 0 before returning to the user, and explicitly mandate that any future use of the field must be as an OUT-only parameter?

Thanks,
-Arjun

Quoted 2 lines
Quoted 2 lines

-Arjun

maintainer
#10 lore raw

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

Quoted 4 lines · On Thu, 4 Feb 2021 15:03:40 -0800 Arjun Roy wrote:

But, if it's an IN or IN-OUT field, it seems like mandating that the application set it to 0 could break the case where a future application sets it to some non-zero value and runs on an older kernel.

That usually works fine in practice, 0 means "do what old kernels did / feature not requested", then if newer userspace sets the field to non-0 that means it requires a feature the kernel doesn't support. So -EINVAL / -EOPNOTSUPP is right. BPF syscall has been successfully doing this since day 1, I'm not aware of any major snags.

Quoted 3 lines

And allowing it to be non-zero can maybe yield an unexpected outcome if an old application that did not zero it runs on a newer kernel.

Could you refresh our memory as to why we can't require the application to pass zero-ed memory to TCP ZC? In practice is there are max reasonable length of the argument that such legacy application may pass so that we can start checking at a certain offset?

Quoted 4 lines

So: maybe the right move is to mark it as reserved, not care what the input value is, always set it to 0 before returning to the user, and explicitly mandate that any future use of the field must be as an OUT-only parameter?

#11 lore raw

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

Quoted 7 lines · Jakub Kicinski
Quoted 4 lines · On Thu, 4 Feb 2021 15:03:40 -0800 Arjun Roy wrote:

But, if it's an IN or IN-OUT field, it seems like mandating that the application set it to 0 could break the case where a future application sets it to some non-zero value and runs on an older kernel.

Quoted 6 lines

That usually works fine in practice, 0 means "do what old kernels did / feature not requested", then if newer userspace sets the field to non-0 that means it requires a feature the kernel doesn't support. So -EINVAL / -EOPNOTSUPP is right. BPF syscall has been successfully doing this since day 1, I'm not aware of any major snags.

Alright, sounds good.

Quoted 9 lines
Quoted 3 lines

And allowing it to be non-zero can maybe yield an unexpected outcome if an old application that did not zero it runs on a newer kernel.

Could you refresh our memory as to why we can't require the application to pass zero-ed memory to TCP ZC? In practice is there are max reasonable length of the argument that such legacy application may pass so that we can start checking at a certain offset?

Actually I think that's fine. We have hitherto been just using length checks to distinguish between feature capability for rx. zerocopy but I think we can draw the line at this point (now that there's ambiguity) and start requiring zero'd memory.

I will send out a patch soon; reserved u32 field, must be set to 0 for the current kernel, can be non-zero and in/out in future kernels as discussed.

Thanks,
-Arjun

Quoted 5 lines
Quoted 4 lines

So: maybe the right move is to mark it as reserved, not care what the input value is, always set it to 0 before returning to the user, and explicitly mandate that any future use of the field must be as an OUT-only parameter?