Skip to contentSkip to conversation

tcp: add some RTO MIN and DELACK MAX

PATCH Superseded Jason Xing 2 participants 8 messages Mar 9, 2025 – Mar 11, 2025

Subsystems (8)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

cover tcp: add some RTO MIN and DELACK MAX

Add bpf_getsockopt for RTO MIN and DELACK MAX.

Add setsockopt/getsockopt for RTO MIN and DELACK MAX.

Add corresponding selftests for bpf.

Jason Xing (5):
tcp: bpf: support bpf_getsockopt for TCP_BPF_RTO_MIN
tcp: bpf: support bpf_getsockopt for TCP_BPF_DELACK_MAX
tcp: support TCP_RTO_MIN_US for set/getsockopt use
tcp: support TCP_DELACK_MAX_US for set/getsockopt use
selftests: add bpf_set/getsockopt() for TCP_BPF_DELACK_MAX and
TCP_BPF_RTO_MIN

 Documentation/networking/ip-sysctl.rst        |  4 +--
 include/net/tcp.h                             |  2 +-
 include/uapi/linux/tcp.h                      |  2 ++
 net/core/filter.c                             | 22 +++++++++++++
 net/ipv4/tcp.c                                | 32 +++++++++++++++++--
 net/ipv4/tcp_output.c                         |  2 +-
 .../selftests/bpf/progs/setget_sockopt.c      |  2 ++
 7 files changed, 60 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/5 tcp: bpf: support bpf_getsockopt for TCP_BPF_RTO_MIN

Support bpf_getsockopt if application tries to know what the RTO MIN of this socket is.

---
 net/core/filter.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/core/filter.c +11 -0
index a0867c5b32b3..31aef259e104 100644
@@ -5415,6 +5415,17 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 		if (*optlen < 1)
 			return -EINVAL;
 		break;
+	case TCP_BPF_RTO_MIN:
+		if (*optlen != sizeof(int))
+			return -EINVAL;
+		if (getopt) {
+			int rto_min = inet_csk(sk)->icsk_rto_min;
+			int rto_min_us = jiffies_to_usecs(rto_min);
+
+			memcpy(optval, &rto_min_us, *optlen);
+			return 0;
+		}
+		return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
 	case TCP_BPF_SOCK_OPS_CB_FLAGS:
 		if (*optlen != sizeof(int))
 			return -EINVAL;
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 2/5 tcp: bpf: support bpf_getsockopt for TCP_BPF_DELACK_MAX

In reply to Jason Xing

Support bpf_getsockopt if application tries to know what the delayed ack max time is.

---
 net/core/filter.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/core/filter.c +11 -0
index 31aef259e104..5564917e0c6d 100644
@@ -5415,6 +5415,17 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 		if (*optlen < 1)
 			return -EINVAL;
 		break;
+	case TCP_BPF_DELACK_MAX:
+		if (*optlen != sizeof(int))
+			return -EINVAL;
+		if (getopt) {
+			int delack_max = inet_csk(sk)->icsk_delack_max;
+			int delack_max_us = jiffies_to_usecs(delack_max);
+
+			memcpy(optval, &delack_max_us, *optlen);
+			return 0;
+		}
+		return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
 	case TCP_BPF_RTO_MIN:
 		if (*optlen != sizeof(int))
 			return -EINVAL;
Signature · 1 line
2.43.5
#3 lore raw

Re: [PATCH net-next 2/5] tcp: bpf: support bpf_getsockopt for TCP_BPF_DELACK_MAX

Quoted 17 lines · Jason Xing

Support bpf_getsockopt if application tries to know what the delayed ack max time is.

---
  net/core/filter.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
net/core/filter.c +1 -0
index 31aef259e104..5564917e0c6d 100644
@@ -5415,6 +5415,17 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
  		if (*optlen < 1)
  			return -EINVAL;
  		break;
+	case TCP_BPF_DELACK_MAX:

net/core/filter.c static int sol_tcp_sockopt(struct sock *sk, int optname, · line 5428

Quoted 10 lines
net/core/filter.c +10 -0
+		if (*optlen != sizeof(int))
+			return -EINVAL;
+		if (getopt) {
+			int delack_max = inet_csk(sk)->icsk_delack_max;
+			int delack_max_us = jiffies_to_usecs(delack_max);
+
+			memcpy(optval, &delack_max_us, *optlen);
+			return 0;
+		}
+		return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);

There are three TCP_BPF_* specific optnames supported by bpf_getsockopt now.
Please take this chance to create a bpf_sol_tcp_getsockopt and refactor the existing bpf_getsockopt(TCP_BPF_SOCK_OPS_CB_FLAGS) support into it also. The new bpf_sol_tcp_getsockopt can reject the TCP_BPF_IW and TCP_BPF_SNDCWND_CLAMP.

author
#4 lore raw

Re: [PATCH net-next 2/5] tcp: bpf: support bpf_getsockopt for TCP_BPF_DELACK_MAX

Quoted 26 lines · Martin KaFai Lau
Quoted 24 lines · Jason Xing

Support bpf_getsockopt if application tries to know what the delayed ack max time is.

---
  net/core/filter.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
net/core/filter.c +8 -0
index 31aef259e104..5564917e0c6d 100644
@@ -5415,6 +5415,17 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
              if (*optlen < 1)
                      return -EINVAL;
              break;
+     case TCP_BPF_DELACK_MAX:
+             if (*optlen != sizeof(int))
+                     return -EINVAL;
+             if (getopt) {
+                     int delack_max = inet_csk(sk)->icsk_delack_max;
+                     int delack_max_us = jiffies_to_usecs(delack_max);
+
+                     memcpy(optval, &delack_max_us, *optlen);
Quoted 9 lines

net/core/filter.c static int sol_tcp_sockopt(struct sock *sk, int optname, · line 5428

Quoted 3 lines
net/core/filter.c +3 -0
+                     return 0;
+             }
+             return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);

There are three TCP_BPF_* specific optnames supported by bpf_getsockopt now. Please take this chance to create a bpf_sol_tcp_getsockopt and refactor the existing bpf_getsockopt(TCP_BPF_SOCK_OPS_CB_FLAGS) support into it also. The new bpf_sol_tcp_getsockopt can reject the TCP_BPF_IW and TCP_BPF_SNDCWND_CLAMP.

Will do it. BTW, it seems then I'm supposed to target bpf-next net branch?

Thanks,
Jason

author
#5 lore raw

PATCH 3/5 tcp: support TCP_RTO_MIN_US for set/getsockopt use

In reply to Jason Xing

Support adjusting RTO MIN for socket level in non BPF case.

---
 Documentation/networking/ip-sysctl.rst |  4 ++--
 include/net/tcp.h                      |  2 +-
 include/uapi/linux/tcp.h               |  1 +
 net/ipv4/tcp.c                         | 16 +++++++++++++++-
 4 files changed, 19 insertions(+), 4 deletions(-)
Documentation/networking/ip-sysctl.rst +2 -2
index 054561f8dcae..56eabcff0ed0 100644
@@ -1229,8 +1229,8 @@ tcp_pingpong_thresh - INTEGER
 tcp_rto_min_us - INTEGER
 	Minimal TCP retransmission timeout (in microseconds). Note that the
 	rto_min route option has the highest precedence for configuring this
-	setting, followed by the TCP_BPF_RTO_MIN socket option, followed by
-	this tcp_rto_min_us sysctl.
+	setting, followed by the TCP_BPF_RTO_MIN and TCP_RTO_MIN_US socket
+        options, followed by this tcp_rto_min_us sysctl.
 
 	The recommended practice is to use a value less or equal to 200000
 	microseconds.
include/net/tcp.h +1 -1
index a9bc959fb102..b5873ac43483 100644
@@ -806,7 +806,7 @@ u32 tcp_delack_max(const struct sock *sk);
 static inline u32 tcp_rto_min(const struct sock *sk)
 {
 	const struct dst_entry *dst = __sk_dst_get(sk);
-	u32 rto_min = inet_csk(sk)->icsk_rto_min;
+	u32 rto_min = READ_ONCE(inet_csk(sk)->icsk_rto_min);
 
 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
include/uapi/linux/tcp.h +1 -0
index 32a27b4a5020..b2476cf7058e 100644
@@ -137,6 +137,7 @@ enum {
 
 #define TCP_IS_MPTCP		43	/* Is MPTCP being used? */
 #define TCP_RTO_MAX_MS		44	/* max rto time in ms */
+#define TCP_RTO_MIN_US		45	/* min rto time in us */
 
 #define TCP_REPAIR_ON		1
 #define TCP_REPAIR_OFF		0
net/ipv4/tcp.c +15 -1
index eb5a60c7a9cc..cec944a83c2d 100644
@@ -3352,7 +3352,7 @@ int tcp_disconnect(struct sock *sk, int flags)
 	icsk->icsk_probes_out = 0;
 	icsk->icsk_probes_tstamp = 0;
 	icsk->icsk_rto = TCP_TIMEOUT_INIT;
-	icsk->icsk_rto_min = TCP_RTO_MIN;
+	WRITE_ONCE(icsk->icsk_rto_min, TCP_RTO_MIN);
 	icsk->icsk_delack_max = TCP_DELACK_MAX;
 	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
 	tcp_snd_cwnd_set(tp, TCP_INIT_CWND);
@@ -3833,6 +3833,14 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
 			return -EINVAL;
 		WRITE_ONCE(inet_csk(sk)->icsk_rto_max, msecs_to_jiffies(val));
 		return 0;
+	case TCP_RTO_MIN_US: {
+		int rto_min = usecs_to_jiffies(val);
+
+		if (rto_min > TCP_RTO_MIN || rto_min < TCP_TIMEOUT_MIN)
+			return -EINVAL;
+		WRITE_ONCE(inet_csk(sk)->icsk_rto_min, rto_min);
+		return 0;
+	}
 	}
 
 	sockopt_lock_sock(sk);
@@ -4672,6 +4680,12 @@ int do_tcp_getsockopt(struct sock *sk, int level,
 	case TCP_RTO_MAX_MS:
 		val = jiffies_to_msecs(tcp_rto_max(sk));
 		break;
+	case TCP_RTO_MIN_US: {
+		int rto_min = READ_ONCE(inet_csk(sk)->icsk_rto_min);
+
+		val = jiffies_to_usecs(rto_min);
+		break;
+	}
 	default:
 		return -ENOPROTOOPT;
 	}
Signature · 1 line
2.43.5
author
#6 lore raw

PATCH 4/5 tcp: support TCP_DELACK_MAX_US for set/getsockopt use

In reply to Jason Xing

Support adjusting delayed ack max for socket level in non BPF case.

---
 include/uapi/linux/tcp.h |  1 +
 net/ipv4/tcp.c           | 16 +++++++++++++++-
 net/ipv4/tcp_output.c    |  2 +-
 3 files changed, 17 insertions(+), 2 deletions(-)
include/uapi/linux/tcp.h +1 -0
index b2476cf7058e..2377e22f2c4b 100644
@@ -138,6 +138,7 @@ enum {
 #define TCP_IS_MPTCP		43	/* Is MPTCP being used? */
 #define TCP_RTO_MAX_MS		44	/* max rto time in ms */
 #define TCP_RTO_MIN_US		45	/* min rto time in us */
+#define TCP_DELACK_MAX_US	46	/* max delayed ack time in us */
 
 #define TCP_REPAIR_ON		1
 #define TCP_REPAIR_OFF		0
net/ipv4/tcp.c +15 -1
index cec944a83c2d..100135117cf2 100644
@@ -3353,7 +3353,7 @@ int tcp_disconnect(struct sock *sk, int flags)
 	icsk->icsk_probes_tstamp = 0;
 	icsk->icsk_rto = TCP_TIMEOUT_INIT;
 	WRITE_ONCE(icsk->icsk_rto_min, TCP_RTO_MIN);
-	icsk->icsk_delack_max = TCP_DELACK_MAX;
+	WRITE_ONCE(icsk->icsk_delack_max, TCP_DELACK_MAX);
 	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
 	tcp_snd_cwnd_set(tp, TCP_INIT_CWND);
 	tp->snd_cwnd_cnt = 0;
@@ -3841,6 +3841,14 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
 		WRITE_ONCE(inet_csk(sk)->icsk_rto_min, rto_min);
 		return 0;
 	}
+	case TCP_DELACK_MAX_US: {
+		int delack_max = usecs_to_jiffies(val);
+
+		if (delack_max > TCP_DELACK_MAX || delack_max < TCP_TIMEOUT_MIN)
+			return -EINVAL;
+		WRITE_ONCE(inet_csk(sk)->icsk_delack_max, delack_max);
+		return 0;
+	}
 	}
 
 	sockopt_lock_sock(sk);
@@ -4686,6 +4694,12 @@ int do_tcp_getsockopt(struct sock *sk, int level,
 		val = jiffies_to_usecs(rto_min);
 		break;
 	}
+	case TCP_DELACK_MAX_US: {
+		int delack_max = READ_ONCE(inet_csk(sk)->icsk_delack_max);
+
+		val = jiffies_to_usecs(delack_max);
+		break;
+	}
 	default:
 		return -ENOPROTOOPT;
 	}
net/ipv4/tcp_output.c +1 -1
index 24e56bf96747..65aa26d65987 100644
@@ -4179,7 +4179,7 @@ u32 tcp_delack_max(const struct sock *sk)
 {
 	u32 delack_from_rto_min = max(tcp_rto_min(sk), 2) - 1;
 
-	return min(inet_csk(sk)->icsk_delack_max, delack_from_rto_min);
+	return min(READ_ONCE(inet_csk(sk)->icsk_delack_max), delack_from_rto_min);
 }
 
 /* Send out a delayed ack, the caller does the policy checking
Signature · 1 line
2.43.5
author
#7 lore raw

PATCH 5/5 selftests: add bpf_set/getsockopt() for TCP_BPF_DELACK_MAX and TCP_BPF_RTO_MIN

In reply to Jason Xing

Add selftests for TCP_BPF_DELACK_MAX and TCP_BPF_RTO_MIN BPF socket cases.

---
 tools/testing/selftests/bpf/progs/setget_sockopt.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/bpf/progs/setget_sockopt.c +2 -0
index 106fe430f41b..7a18a2d089bb 100644
@@ -61,6 +61,8 @@ static const struct sockopt_test sol_tcp_tests[] = {
 	{ .opt = TCP_NOTSENT_LOWAT, .new = 1314, .expected = 1314, },
 	{ .opt = TCP_BPF_SOCK_OPS_CB_FLAGS, .new = BPF_SOCK_OPS_ALL_CB_FLAGS,
 	  .expected = BPF_SOCK_OPS_ALL_CB_FLAGS, },
+	{ .opt = TCP_BPF_DELACK_MAX, .new = 10000, .expected = 10000, },
+	{ .opt = TCP_BPF_RTO_MIN, .new = 2000, .expected = 2000, },
 	{ .opt = TCP_RTO_MAX_MS, .new = 2000, .expected = 2000, },
 	{ .opt = 0, },
 };
Signature · 1 line
2.43.5