bpf: support setting max RTO for bpf_setsockopt
Subsystems (7)
v2 cover bpf: support setting max RTO for bpf_setsockopt
Support max RTO set by BPF program calling bpf_setsockopt(). Add corresponding selftests.
Jason Xing (3):
tcp: add TCP_RTO_MAX_MIN_SEC definition
bpf: support TCP_RTO_MAX_MS for bpf_setsockopt selftests/bpf: add rto max for bpf_setsockopt test
include/net/tcp.h | 1 + net/core/filter.c | 1 + net/ipv4/sysctl_net_ipv4.c | 3 ++- net/ipv4/tcp.c | 3 ++- tools/include/uapi/linux/tcp.h | 1 + tools/testing/selftests/bpf/progs/bpf_tracing_net.h | 1 + tools/testing/selftests/bpf/progs/setget_sockopt.c | 1 + 7 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.5
PATCH v2 1/3 tcp: add TCP_RTO_MAX_MIN_SEC definition
Add minimum value definition as the lower bound of RTO MAX set by users. No functional changes here.
--- include/net/tcp.h | 1 + net/ipv4/sysctl_net_ipv4.c | 3 ++- net/ipv4/tcp.c | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-)
include/net/tcp.h +1 -0
@@ -143,6 +143,7 @@ static_assert((1 << ATO_BITS) > TCP_DELACK_MAX);#define TCP_DELACK_MIN 4U#define TCP_ATO_MIN 4U#endif +#define TCP_RTO_MAX_MIN_SEC 1#define TCP_RTO_MAX_SEC 120#define TCP_RTO_MAX ((unsigned)(TCP_RTO_MAX_SEC * HZ))#define TCP_RTO_MIN ((unsigned)(HZ / 5))
net/ipv4/sysctl_net_ipv4.c +2 -1
@@ -28,6 +28,7 @@ static int tcp_adv_win_scale_max = 31;static int tcp_app_win_max =31 ;static int tcp_min_snd_mss_min =TCP_MIN_SND_MSS ;static int tcp_min_snd_mss_max =65535 ; +static int tcp_rto_max_min =TCP_RTO_MAX_MIN_SEC *MSEC_PER_SEC ;static int tcp_rto_max_max =TCP_RTO_MAX_SEC *MSEC_PER_SEC ;static int ip_privileged_port_min;static int ip_privileged_port_max =65535 ;
@@ -1590,7 +1591,7 @@ static struct ctl_table ipv4_net_table[] = { .maxlen = sizeof(int), .mode = 0644, .proc_handler = proc_dointvec_minmax, - .extra1 = SYSCTL_ONE_THOUSAND, + .extra1 = &tcp_rto_max_min, .extra2 = &tcp_rto_max_max, }, };
net/ipv4/tcp.c +2 -1
@@ -3812,7 +3812,8 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname, TCP_RTO_MAX / HZ));return 0 ;case TCP_RTO_MAX_MS : -if (val <MSEC_PER_SEC || val >TCP_RTO_MAX_SEC *MSEC_PER_SEC ) +if (val <TCP_RTO_MAX_MIN_SEC *MSEC_PER_SEC || + val >TCP_RTO_MAX_SEC *MSEC_PER_SEC )return -EINVAL ;WRITE_ONCE (inet_csk (sk)->icsk_rto_max,msecs_to_jiffies (val));return 0 ;
Signature · 1 line
2.43.5
PATCH v2 2/3 bpf: support TCP_RTO_MAX_MS for bpf_setsockopt
In reply to Jason XingSome applications don't want to wait for too long because the time of retransmission increases exponentially and can reach more than 10 seconds, for example. Eric implements the core logic on supporting rto max feature in the stack previously. Based on that, we can support it for BPF use.
This patch reuses the same logic of TCP_RTO_MAX_MS in do_tcp_setsockopt() and do_tcp_getsockopt(). BPF program can call bpf_{set/get}sockopt() to set/get the maximum value of RTO.
It would be good if a BPF program sets max value of RTO before transmission as we can see in the later patch (selftests).
--- net/core/filter.c | 1 + 1 file changed, 1 insertion(+)
net/core/filter.c +1 -0
@@ -5382,6 +5382,7 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,case TCP_USER_TIMEOUT :case TCP_NOTSENT_LOWAT :case TCP_SAVE_SYN : +case TCP_RTO_MAX_MS :if (*optlen !=sizeof (int ))return -EINVAL ;break ;
Signature · 1 line
2.43.5
PATCH v2 3/3 selftests/bpf: add rto max for bpf_setsockopt test
In reply to Jason XingAdd TCP_RTO_MAX_MS selftests for active and passive flows in various bpf callbacks. Even though the TCP_RTO_MAX_MS can be used in established phase, we highly discourage to do so because it may trigger unexpected behaviour. On the contrary, it's highly recommended that the maximum value of RTO is set before first time of transmission, such as BPF_SOCK_OPS_{PASSIVE|ACTIVE}_ESTABLISHED_CB,
--- tools/include/uapi/linux/tcp.h | 1 + tools/testing/selftests/bpf/progs/bpf_tracing_net.h | 1 + tools/testing/selftests/bpf/progs/setget_sockopt.c | 1 + 3 files changed, 3 insertions(+)
tools/include/uapi/linux/tcp.h +1 -0
@@ -128,6 +128,7 @@ enum {#define TCP_CM_INQ TCP_INQ#define TCP_TX_DELAY 37/* delay outgoing packets by XX usec */ +#define TCP_RTO_MAX_MS 44/* max rto time in ms */ #define TCP_REPAIR_ON 1
tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1 -0
@@ -49,6 +49,7 @@#define TCP_SAVED_SYN 28#define TCP_CA_NAME_MAX 16#define TCP_NAGLE_OFF 1 +#define TCP_RTO_MAX_MS 44#define TCP_ECN_OK 1#define TCP_ECN_QUEUE_CWR 2
tools/testing/selftests/bpf/progs/setget_sockopt.c +1 -0
@@ -61,6 +61,7 @@ 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_RTO_MAX_MS, .new = 2000, .expected = 2000, }, { .opt = 0, }, };
Signature · 1 line
2.43.5
Re: [PATCH bpf-next v2 2/3] bpf: support TCP_RTO_MAX_MS for bpf_setsockopt
In reply to Jason XingFrom: Jason Xing <[email protected]>
Date: Mon, 17 Feb 2025 11:42:44 +0800
Quoted 6 lines
Some applications don't want to wait for too long because the time of retransmission increases exponentially and can reach more than 10 seconds, for example. Eric implements the core logic on supporting rto max feature in the stack previously. Based on that, we can support it for BPF use.
Quoted 8 lines
This patch reuses the same logic of
TCP_RTO_MAX_MSindo_tcp_setsockopt()anddo_tcp_getsockopt(). BPF program can call bpf_{set/get}sockopt() to set/get the maximum value of RTO.It would be good if a BPF program sets max value of RTO before transmission as we can see in the later patch (selftests).
Signed-off-by Jason Xing <[email protected]>
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 1/3] tcp: add TCP_RTO_MAX_MIN_SEC definition
In reply to Jason XingQuoted 2 lines · Jason Xing
Add minimum value definition as the lower bound of RTO MAX set by users. No functional changes here.
If it is no-op, why it is needed? The commit message didn't explain it either. I also cannot guess how patch 2 depends on patch 1.
pw-bot: cr
Re: [PATCH bpf-next v2 1/3] tcp: add TCP_RTO_MAX_MIN_SEC definition
Quoted 7 lines · Martin KaFai Lau
Quoted 2 lines · Jason Xing
Add minimum value definition as the lower bound of RTO MAX set by users. No functional changes here.
If it is no-op, why it is needed? The commit message didn't explain it either. I also cannot guess how patch 2 depends on patch 1.
It's more of a cleanup patch :)
Thanks,
Jason
Jakub Kicinski
maintainerRe: [PATCH bpf-next v2 1/3] tcp: add TCP_RTO_MAX_MIN_SEC definition
In reply to Martin KaFai LauQuoted 6 lines · On Tue, 18 Feb 2025 15:38:17 -0800 Martin KaFai Lau wrote:
Quoted 2 lines · Jason Xing
Add minimum value definition as the lower bound of RTO MAX set by users. No functional changes here.
If it is no-op, why it is needed? The commit message didn't explain it either. I also cannot guess how patch 2 depends on patch 1.
FWIW this patch also gave me pause when looking at v1. I don't think this define makes the code any easier to follow.
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v2 3/3] selftests/bpf: add rto max for bpf_setsockopt test
In reply to Jason XingQuoted 7 lines · Jason Xing
Add
TCP_RTO_MAX_MSselftests for active and passive flows in various bpf callbacks. Even though theTCP_RTO_MAX_MScan be used in established phase, we highly discourage to do so because it may trigger unexpected behaviour. On the contrary, it's highly recommended that the maximum value of RTO is set before first time of transmission, such as BPF_SOCK_OPS_{PASSIVE|ACTIVE}_ESTABLISHED_CB,
s/,/./
What unexpected behavior when setting in BPF after the established state?
Setting it after the established state or not is not specific to BPF. syscall can choose to do it after the connection established also. The above makes it unclear what unexpected behavior that the BPF prog will cause if TCP_RTO_MAX_MS is used in BPF instead of syscall.
If there is subtle difference between calling TCP_RTO_MAX_MS from bpf and from syscall, please write it clearly what are the unexpected behaviors when calling in BPF after the established states.
Otherwise, the commit message can be just this:
Test the TCP_RTO_MAX_MS optname in the existing setget_sockopt test.
Quoted 13 lines
Signed-off-by Jason Xing <[email protected]>--- tools/include/uapi/linux/tcp.h | 1 + tools/testing/selftests/bpf/progs/bpf_tracing_net.h | 1 + tools/testing/selftests/bpf/progs/setget_sockopt.c | 1 + 3 files changed, 3 insertions(+)tools/include/uapi/linux/tcp.h +0
-0
tools/include/uapi/linux/tcp.h enum { · line 131
Quoted 4 lines
tools/include/uapi/linux/tcp.h +1
-0@@ -128,6 +128,7 @@ enum {#define TCP_CM_INQ TCP_INQ#define TCP_TX_DELAY 37/* delay outgoing packets by XX usec */ +#define TCP_RTO_MAX_MS 44/* max rto time in ms */
Have you checked if this change is really needed?
Quoted 27 lines
tools/include/uapi/linux/tcp.h +0
-0#define TCP_REPAIR_ON 1tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1
-0@@ -49,6 +49,7 @@#define TCP_SAVED_SYN 28#define TCP_CA_NAME_MAX 16#define TCP_NAGLE_OFF 1 +#define TCP_RTO_MAX_MS 44#define TCP_ECN_OK 1#define TCP_ECN_QUEUE_CWR 2tools/testing/selftests/bpf/progs/setget_sockopt.c +1
-0@@ -61,6 +61,7 @@ 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_RTO_MAX_MS, .new = 2000, .expected = 2000, }, { .opt = 0, }, };
Re: [PATCH bpf-next v2 1/3] tcp: add TCP_RTO_MAX_MIN_SEC definition
In reply to Jakub KicinskiQuoted 11 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 18 Feb 2025 15:38:17 -0800 Martin KaFai Lau wrote:
Quoted 2 lines · Jason Xing
Add minimum value definition as the lower bound of RTO MAX set by users. No functional changes here.
If it is no-op, why it is needed? The commit message didn't explain it either. I also cannot guess how patch 2 depends on patch 1.
FWIW this patch also gave me pause when looking at v1. I don't think this define makes the code any easier to follow.
Okay, I will drop this in the next re-spin.
Thanks,
Jason
Re: [PATCH bpf-next v2 3/3] selftests/bpf: add rto max for bpf_setsockopt test
In reply to Martin KaFai LauQuoted 14 lines · Martin KaFai Lau
Quoted 7 lines · Jason Xing
Add
TCP_RTO_MAX_MSselftests for active and passive flows in various bpf callbacks. Even though theTCP_RTO_MAX_MScan be used in established phase, we highly discourage to do so because it may trigger unexpected behaviour. On the contrary, it's highly recommended that the maximum value of RTO is set before first time of transmission, such as BPF_SOCK_OPS_{PASSIVE|ACTIVE}_ESTABLISHED_CB,s/,/./
What unexpected behavior when setting in BPF after the established state?
Quoted 8 lines
Setting it after the established state or not is not specific to BPF. syscall can choose to do it after the connection established also. The above makes it unclear what unexpected behavior that the BPF prog will cause if
TCP_RTO_MAX_MSis used in BPF instead of syscall.If there is subtle difference between calling
TCP_RTO_MAX_MSfrom bpf and from syscall, please write it clearly what are the unexpected behaviors when calling in BPF after the established states.
I don't think there is any difference between them. For both of them, It would be better to set before transmission.
Quoted 4 lines
Otherwise, the commit message can be just this:
Test the
TCP_RTO_MAX_MSoptname in the existingsetget_sockopttest.
Got it. Will use this instead.
Quoted 16 lines
Quoted 16 lines
Signed-off-by Jason Xing <[email protected]>--- tools/include/uapi/linux/tcp.h | 1 + tools/testing/selftests/bpf/progs/bpf_tracing_net.h | 1 + tools/testing/selftests/bpf/progs/setget_sockopt.c | 1 + 3 files changed, 3 insertions(+)tools/include/uapi/linux/tcp.h +0
-0@@ -128,6 +128,7 @@ enum {#define TCP_CM_INQ TCP_INQ
Quoted 4 lines
tools/include/uapi/linux/tcp.h enum { · line 131
Quoted 2 lines
tools/include/uapi/linux/tcp.h +1
-0#define TCP_TX_DELAY 37/* delay outgoing packets by XX usec */ +#define TCP_RTO_MAX_MS 44/* max rto time in ms */ Have you checked if this change is really needed?
I thought we needed to sync it from include/uapi/linux/tcp.h. Will test it then.
Thanks,
Jason
Quoted 29 lines
Quoted 28 lines
tools/include/uapi/linux/tcp.h +0
-0#define TCP_REPAIR_ON 1tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1
-0@@ -49,6 +49,7 @@#define TCP_SAVED_SYN 28#define TCP_CA_NAME_MAX 16#define TCP_NAGLE_OFF 1 +#define TCP_RTO_MAX_MS 44#define TCP_ECN_OK 1#define TCP_ECN_QUEUE_CWR 2tools/testing/selftests/bpf/progs/setget_sockopt.c +1
-0@@ -61,6 +61,7 @@ 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_RTO_MAX_MS, .new = 2000, .expected = 2000, }, { .opt = 0, }, };