tcp: carry a BPF-chosen identifier in TSval low bits
Subsystems (8)
cover tcp: carry a BPF-chosen identifier in TSval low bits
This series lets a listener encode a small per-connection identifier in the low bits of the TCP timestamp values it sends. The peer echoes TSval back in TSecr, so every segment the peer sends after the handshake carries the identifier in its TSecr.
In Anycast networks, mid-stream path changes caused by BGP route flapping or ECMP shifts frequently deliver active TCP packets to an ingress Point of Presence (PoP) that lacks the flow's socket state, resulting in abrupt connection resets (RST). While protocols like QUIC avoid this using explicit Connection IDs, TCP lacks an in-band routing token to help ingress nodes identify where a flow actually lives. To address this problem, we propose embedding an N-bit data center ID into the lower-order bits of the TCP Timestamp option (TSval, RFC 7323), allowing edge load balancers to recognize misdirected packets and steer them to the correct origin data center holding the active session.
How it works:
- tcp_ts_id_bits (per netns, 0..8, default 0) sets how many low TSval bits are reserved. The millisecond clock is shifted up by that many bits.
- A sock_ops program opts a listener in with BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG and is called with BPF_SOCK_OPS_TCP_TS_ID_CB for each SYN, before the SYN-ACK is sent. It sees the SYN and optionally returns the identifier
- The identifier and width are stored in the request socket, then in the full and timewait sockets. Incoming TSecr values are decoded before RTT estimation and PAWS use them.
- TCP_TS_ID reads back the identifier, and /proc/net/netstat gains TCPTSIdAssigned, TCPTSIdDeclined, TCPTSIdSkipped and TCPTSIdEchoMismatch.
RTT estimation on the encoding host sees the same decoded timestamps as before, so they are unchanged. On the peer, PAWS keeps working too; the only effects are a shorter idle limit and a narrower tolerance for reordered segments, both described below.
Scope and limitations:
- Passive opens only. SYN cookies, microsecond timestamps and peers without timestamps are left unencoded.
- The peer sees a clock running 2^bits times faster. That narrows the real-time span of its PAWS window, so the width should be no larger than the identifiers need.
- Peers that keep ts_recent for the RFC 7323 24 days (e.g. FreeBSD, macOS) stall connections that stay idle longer than 24.8/2^bits days (about 37 hours at 4 bits). Linux peers stop enforcing PAWS after TCP_PAWS_WRAP and are unaffected.
- TCP_REPAIR cannot restore the identifier, so hosts that migrate established connections should leave the feature off.
All of this is documented under tcp_ts_id_bits in ip-sysctl.rst. With the sysctl at 0 (the default) the only new cost on the established fast path is one likely() branch when parsing TSecr.
Questions for reviewers:
1. Is encoding an identifier in the low bits of TSval acceptable in principle, given the trade-offs above?
2. If so, what feedback do you have on the implementation?
Patch 1 adds the encoding and decoding, which stay inert until patch 2 lets BPF set an identifier. Patch 3 adds the TCP_TS_ID read-back, and patch 4 adds a BPF selftest.
Testing:
- A kernel with this series and tcp_ts_id_bits=7 is serving production traffic on a few Cloudflare hosts and assigning ids to all CDN connections. Key TCP metrics on those hosts show no difference from hosts without the series.
Willem Ferguson (4):
tcp: add plumbing to carry an identifier in TSval low bits
tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
tcp: add TCP_TS_ID socket option
selftests/bpf: add tcp_ts_id test
Documentation/networking/ip-sysctl.rst | 38 +++++ .../net_cachelines/netns_ipv4_sysctl.rst | 1 + .../networking/net_cachelines/tcp_sock.rst | 2 + include/linux/tcp.h | 8 + include/net/netns/ipv4.h | 1 + include/net/tcp.h | 58 ++++++- include/uapi/linux/bpf.h | 15 +- include/uapi/linux/snmp.h | 4 + include/uapi/linux/tcp.h | 8 + net/core/filter.c | 2 + net/ipv4/proc.c | 4 + net/ipv4/syncookies.c | 2 + net/ipv4/sysctl_net_ipv4.c | 10 ++ net/ipv4/tcp.c | 16 ++ net/ipv4/tcp_input.c | 61 ++++++- net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_minisocks.c | 27 +++- net/ipv4/tcp_output.c | 15 +- tools/include/uapi/linux/bpf.h | 15 +- .../selftests/bpf/prog_tests/tcp_ts_id.c | 150 ++++++++++++++++++ .../selftests/bpf/prog_tests/tcpbpf_user.c | 2 +- .../selftests/bpf/progs/test_tcp_ts_id_kern.c | 32 ++++ .../selftests/bpf/progs/test_tcpbpf_kern.c | 2 +- 23 files changed, 459 insertions(+), 15 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c
base-commit: d8674294aefef02266c4d47ad10131f1bffbe534 --
2.47.3
RFC PATCH 1/4 tcp: add plumbing to carry an identifier in TSval low bits
Add per-connection state that lets the low bits of locally generated TSvals carry a small identifier, and teach the receive side to strip it from echoed TSecr values:
- tcp_request_sock, tcp_sock and tcp_timewait_sock gain ts_id and ts_id_bits. They are copied from the request socket to the child and from the child to the timewait socket, and cleared on every path that creates a request socket without going through tcp_openreq_init() (SYN cookies and bpf_sk_assign_tcp_reqsk()).
- tcp_tsval_encode() shifts the timestamp clock left by ts_id_bits and ORs in ts_id. It is used for SYN-ACK, established, request socket and timewait TSvals. The SYN-ACK path keeps the identifier when it replaces a zero TSval with ~0U.
- tcp_tsecr_decode() undoes the encoding. The shift drops ts_id_bits of clock range, so the decoded echo is re-centred on the current clock with sign_extend32() before RTT estimation and PAWS consume it.
A BUILD_BUG_ON() checks that, at the widest identifier allowed, an encoded TSval cannot advance by 2^31 within TCP_PAWS_WRAP, the idle period after which Linux stops enforcing PAWS against ts_recent.
Nothing sets ts_id_bits yet, so this patch does not change behaviour. With ts_id_bits == 0, tcp_tsval_encode() returns its input and the established receive path adds a single likely() branch before the existing tsoffset subtraction.
--- .../networking/net_cachelines/tcp_sock.rst | 2 + include/linux/tcp.h | 8 +++ include/net/tcp.h | 50 ++++++++++++++++++- net/core/filter.c | 2 + net/ipv4/syncookies.c | 2 + net/ipv4/tcp.c | 13 +++++ net/ipv4/tcp_input.c | 11 ++-- net/ipv4/tcp_minisocks.c | 18 ++++++- net/ipv4/tcp_output.c | 15 ++++-- 9 files changed, 110 insertions(+), 11 deletions(-)
Documentation/networking/net_cachelines/tcp_sock.rst +2 -0
@@ -32,6 +32,8 @@ u32 lsndtime read_write u32 last_oow_ack_time u32 compressed_ack_rcv_nxt u32 tsoffset read_mostly read_mostly tcp_established_options(tx);tcp_fast_parse_options(rx) +u8 ts_id read_mostly read_mostly tcp_established_options(tx);tcp_fast_parse_options(rx) +u8 ts_id_bits read_mostly read_mostly tcp_established_options(tx);tcp_fast_parse_options(rx) struct list_head tsq_node struct list_head tsorted_sent_queue read_write tcp_update_skb_after_send u32 snd_wl1 read_mostly tcp_may_update_window
include/linux/tcp.h +8 -0
@@ -145,6 +145,8 @@ static inline void tcp_clear_options(struct tcp_options_received *rx_opt)* only four options will fit in a standard TCP header */ #define TCP_NUM_SACKS 4 +#define TCP_TS_ID_BITS_MAX 8 +struct tcp_request_sock_ops ;struct tcp_request_sock {
@@ -154,6 +156,8 @@ struct tcp_request_sock {bool tfo_listener;bool is_mptcp;bool req_usec_ts; +u8 ts_id; +u8 ts_id_bits;#if IS_ENABLED (CONFIG_MPTCP )bool drop_req;#endif
@@ -220,6 +224,8 @@ struct tcp_sock {/* TXRX read-mostly hotpath cache lines */ __cacheline_group_begin (tcp_sock_read_txrx);u32 tsoffset;/* timestamp offset */ +u8 ts_id;/* identifier encoded in TSval low bits */ +u8 ts_id_bits;/* number of identifier bits in TSval */ u32 snd_wnd;/* The window we expect to receive */ u32 mss_cache;/* Cached effective mss, not including SACKS */ u32 snd_cwnd;/* Sending congestion window */
@@ -575,6 +581,8 @@ struct tcp_timewait_sock {u32 tw_rcv_wnd;u32 tw_ts_offset;u32 tw_ts_recent; +u8 tw_ts_id; +u8 tw_ts_id_bits;/* The time we sent the last out-of-window ACK: */ u32 tw_last_oow_ack_time;
include/net/tcp.h +48 -2
62 lines not on this page · raw message
net/core/filter.c +2 -0
@@ -12579,6 +12579,8 @@ __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk, treq->req_usec_ts = !!attrs->usec_ts_ok; treq->ts_off = tsoff; + treq->ts_id =0 ; + treq->ts_id_bits =0 ;skb_orphan (skb); skb->sk =req_to_sk (req);
net/ipv4/syncookies.c +2 -0
@@ -280,6 +280,8 @@ static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb, treq->snt_synack =0 ; treq->snt_tsval_first =0 ; treq->tfo_listener = false; + treq->ts_id =0 ; + treq->ts_id_bits =0 ; treq->rcv_isn =ntohl (th->seq) -1 ; treq->snt_isn =ntohl (th->ack_seq) -1 ;/* The request socket was freed after the SYN-ACK; use the cookie
net/ipv4/tcp.c +13 -0
@@ -462,6 +462,8 @@ void tcp_init_sock(struct sock *sk)tcp_assign_congestion_control (sk); tp->tsoffset =0 ; + tp->ts_id =0 ; + tp->ts_id_bits =0 ; tp->rack.reo_wnd_steps =1 ; sk->sk_write_space = sk_stream_write_space;
@@ -3429,6 +3431,8 @@ int tcp_disconnect(struct sock *sk, int flags) tp->window_clamp =0 ; tp->delivered =0 ; tp->delivered_ce =0 ; + tp->ts_id =0 ; + tp->ts_id_bits =0 ; tp->accecn_fail_mode =0 ; tp->saw_accecn_opt =TCP_ACCECN_OPT_NOT_SEEN ;tcp_accecn_init_counters (tp);
@@ -5212,6 +5216,8 @@ static void __init tcp_struct_check(void)/* TXRX read-mostly hotpath cache lines */ CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, tsoffset); + CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, ts_id); + CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, ts_id_bits); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, snd_wnd); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, mss_cache); CACHELINE_ASSERT_GROUP_MEMBER(struct tcp_sock, tcp_sock_read_txrx, snd_cwnd);
@@ -5302,6 +5308,13 @@ void __init tcp_init(void)BUILD_BUG_ON (sizeof (struct tcp_skb_cb ) > sizeof_field(struct sk_buff, cb)); +/* An encoded TSval must not advance by 2^31 within the window that +* Linux PAWS keeps ts_recent valid; peers using RFC 7323's 24 day +* window are not covered (see tcp_ts_id_bits in ip-sysctl.rst). +*/ +BUILD_BUG_ON (((u64 )TCP_PAWS_WRAP *MSEC_PER_SEC ) << +TCP_TS_ID_BITS_MAX >= (1ULL <<31 )); +tcp_struct_check ();percpu_counter_init (&tcp_sockets_allocated,0 ,GFP_KERNEL );
net/ipv4/tcp_input.c +8 -3
@@ -4717,7 +4717,8 @@ static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr tp->rx_opt.rcv_tsval =ntohl (*ptr); ++ptr;if (*ptr) - tp->rx_opt.rcv_tsecr =ntohl (*ptr) - tp->tsoffset; + tp->rx_opt.rcv_tsecr =tcp_tsecr_decode_tp (ntohl (*ptr), + tp);else tp->rx_opt.rcv_tsecr =0 ;return true;
@@ -4749,7 +4750,8 @@ static bool tcp_fast_parse_options(const struct net *net,tcp_parse_options (net, skb, &tp->rx_opt,1 ,NULL );if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) - tp->rx_opt.rcv_tsecr -= tp->tsoffset; + tp->rx_opt.rcv_tsecr = +tcp_tsecr_decode_tp (tp->rx_opt.rcv_tsecr, tp);return true; }
@@ -6881,7 +6883,8 @@ static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,tcp_parse_options (sock_net (sk), skb, &tp->rx_opt,0 , &foc);if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) - tp->rx_opt.rcv_tsecr -= tp->tsoffset; + tp->rx_opt.rcv_tsecr = +tcp_tsecr_decode_tp (tp->rx_opt.rcv_tsecr, tp);if (th->ack) {/* rfc793:
@@ -7512,6 +7515,8 @@ static void tcp_openreq_init(struct request_sock *req,tcp_rsk (req)->rcv_nxt =TCP_SKB_CB (skb)->seq +1 ;tcp_rsk (req)->snt_synack =0 ;tcp_rsk (req)->snt_tsval_first =0 ; +tcp_rsk (req)->ts_id =0 ; +tcp_rsk (req)->ts_id_bits =0 ;tcp_rsk (req)->last_oow_ack_time =0 ;tcp_rsk (req)->accecn_ok =0 ;tcp_rsk (req)->saw_accecn_opt =TCP_ACCECN_OPT_NOT_SEEN ;
net/ipv4/tcp_minisocks.c +16 -2
@@ -122,7 +122,11 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,if (tmp_opt.saw_tstamp) {if (tmp_opt.rcv_tsecr) - tmp_opt.rcv_tsecr -= tcptw->tw_ts_offset; + tmp_opt.rcv_tsecr = +tcp_tsecr_decode (tmp_opt.rcv_tsecr, + tcptw->tw_ts_offset, + tcptw->tw_ts_id_bits, +tcp_clock_ts (tw->tw_usec_ts)); tmp_opt.ts_recent =READ_ONCE (tcptw->tw_ts_recent); tmp_opt.ts_recent_stamp = ts_recent_stamp; paws_reject =tcp_paws_reject (&tmp_opt, th->rst);
@@ -347,6 +351,8 @@ void tcp_time_wait(struct sock *sk, int state, int timeo) tcptw->tw_ts_recent = tp->rx_opt.ts_recent; tcptw->tw_ts_recent_stamp = tp->rx_opt.ts_recent_stamp; tcptw->tw_ts_offset = tp->tsoffset; + tcptw->tw_ts_id = tp->ts_id; + tcptw->tw_ts_id_bits = tp->ts_id_bits; tw->tw_usec_ts = tp->tcp_usec_ts; tcptw->tw_last_oow_ack_time =0 ; tcptw->tw_tx_delay = tp->tcp_tx_delay;
@@ -639,6 +645,8 @@ struct sock *tcp_create_openreq_child(const struct sock *sk, newtp->total_rto_recoveries =1 ; } newtp->tsoffset = treq->ts_off; + newtp->ts_id = treq->ts_id; + newtp->ts_id_bits = treq->ts_id_bits;#ifdef CONFIG_TCP_MD5SIG newtp->md5sig_info =NULL ;/*XXX*/ #endif
@@ -705,11 +713,17 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,if (tmp_opt.saw_tstamp) { tmp_opt.ts_recent = req->ts_recent;if (tmp_opt.rcv_tsecr) { +u32 now =tcp_clock_ts (tcp_rsk (req)->req_usec_ts); +if (inet_rsk (req)->tstamp_ok && !fastopen) tsecr_reject = !between (tmp_opt.rcv_tsecr,tcp_rsk (req)->snt_tsval_first,READ_ONCE (tcp_rsk (req)->snt_tsval_last)); - tmp_opt.rcv_tsecr -=tcp_rsk (req)->ts_off; + tmp_opt.rcv_tsecr = +tcp_tsecr_decode (tmp_opt.rcv_tsecr, +tcp_rsk (req)->ts_off, +tcp_rsk (req)->ts_id_bits, + now); }/* We do not store true stamp, but it is not required, * it can be estimated (approximately)
net/ipv4/tcp_output.c +11 -4
@@ -1101,11 +1101,15 @@ static unsigned int tcp_synack_options(const struct sock *sk, opts->options |=OPTION_TS ; opts->tsval =tcp_skb_timestamp_ts (tcp_rsk (req)->req_usec_ts, skb) +tcp_rsk (req)->ts_off; -if (!tcp_rsk (req)->snt_tsval_first) { -if (!opts->tsval) - opts->tsval = ~0U ; + opts->tsval =tcp_tsval_encode (opts->tsval, +tcp_rsk (req)->ts_id, +tcp_rsk (req)->ts_id_bits); +if (!tcp_rsk (req)->snt_tsval_first && !opts->tsval) + opts->tsval =tcp_tsval_encode (~0U , +tcp_rsk (req)->ts_id, +tcp_rsk (req)->ts_id_bits); +if (!tcp_rsk (req)->snt_tsval_first)tcp_rsk (req)->snt_tsval_first = opts->tsval; - }WRITE_ONCE (tcp_rsk (req)->snt_tsval_last, opts->tsval); opts->tsecr = req->ts_recent; remaining -=TCPOLEN_TSTAMP_ALIGNED ;
@@ -1171,6 +1175,9 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb opts->options |=OPTION_TS ; opts->tsval = skb ?tcp_skb_timestamp_ts (tp->tcp_usec_ts, skb) + tp->tsoffset :0 ; +if (skb) + opts->tsval =tcp_tsval_encode (opts->tsval, tp->ts_id, + tp->ts_id_bits); opts->tsecr = tp->rx_opt.ts_recent; size +=TCPOLEN_TSTAMP_ALIGNED ; }
Signature · 1 line
2.47.3
RFC PATCH 2/4 tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
In reply to Willem FergusonLet a sock_ops program pick the identifier that the previous patch encodes in the low bits of TSval, for passively opened connections.
The width is set per network namespace by the new tcp_ts_id_bits sysctl (0 to 8, default 0). A listener opts in by setting BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG, typically from BPF_SOCK_OPS_TCP_LISTEN_CB. tcp_conn_request() then runs BPF_SOCK_OPS_TCP_TS_ID_CB once the request socket exists and before the SYN-ACK is built, so the SYN-ACK already carries the identifier. The program sees the request socket, the SYN (bpf_load_hdr_opt() works on it) and the available width in args[1]. A reply that fits in that width is stored with the width in the request socket and is inherited by the child, including Fast Open children; any other reply leaves the connection unencoded.
The callback is not run for SYN cookies, which have nowhere to keep the identifier, or when the peer did not negotiate timestamps. It is skipped for connections using microsecond timestamps and while the sysctl is 0. The width is captured per connection, so changing the sysctl does not affect existing connections.
New /proc/net/netstat counters report how often an identifier was assigned, declined by the program or skipped. TCPTSIdEchoMismatch counts third ACKs whose TSecr no longer carries the identifier, which shows the echo was rewritten in flight.
BPF_SOCK_OPS_ALL_CB_FLAGS grows from 0x7f to 0xff. The tcpbpf selftest used 0x80 as an example of an unsupported flag, so move it to 0x100.
Encoding makes the timestamp clock appear 2^bits times faster to the peer. That shrinks the real-time span of the peer's PAWS window, and peers that keep ts_recent for the RFC 7323 24 days stall connections idle for longer than 24.8/2^bits days. ip-sysctl.rst documents both, along with the lack of TCP_REPAIR support.
--- Documentation/networking/ip-sysctl.rst | 37 ++++++++++++++ .../net_cachelines/netns_ipv4_sysctl.rst | 1 + include/net/netns/ipv4.h | 1 + include/net/tcp.h | 8 +++ include/uapi/linux/bpf.h | 15 +++++- include/uapi/linux/snmp.h | 4 ++ net/ipv4/proc.c | 4 ++ net/ipv4/sysctl_net_ipv4.c | 10 ++++ net/ipv4/tcp_input.c | 50 +++++++++++++++++++ net/ipv4/tcp_ipv4.c | 1 + net/ipv4/tcp_minisocks.c | 9 ++++ tools/include/uapi/linux/bpf.h | 15 +++++- .../selftests/bpf/prog_tests/tcpbpf_user.c | 2 +- .../selftests/bpf/progs/test_tcpbpf_kern.c | 2 +- 14 files changed, 155 insertions(+), 4 deletions(-)
Documentation/networking/ip-sysctl.rst +37 -0
@@ -1096,6 +1096,43 @@ tcp_timestamps - INTEGER Default: 1 +tcp_ts_id_bits - INTEGER + Number of low bits in locally generated TCP timestamp values that may + contain an identifier selected by BPF_SOCK_OPS_TCP_TS_ID_CB. The upper + bits contain the millisecond timestamp clock. Valid values are 0 to 8. + A value of 0 disables the callback and timestamp identifier encoding. + + The value is captured when the callback selects an identifier. Changing + it does not alter existing connections. Identifier encoding is disabled + for connections using microsecond timestamps and for SYN cookies. + + Encoding shifts the timestamp clock up by this many bits, so the TSval + wrap time falls from 49.7 days to 49.7/2^bits days and peers see an + apparently faster clock. Peers that size their PAWS replay window in + timestamp units (Linux does) therefore tolerate less real time before + rejecting a reordered pure ACK. The window shrinks with every extra + bit, so use the narrowest width that holds the identifiers needed. + + The faster clock also limits how long a connection may stay idle. + TSval advances by 2^31 after 24.8/2^bits days (about 2.3 hours at 8 + bits, 37 hours at 4 bits). Linux peers stop enforcing PAWS after 2147 + seconds of idle, so they are unaffected. Peers that follow RFC 7323 and + keep ts_recent for 24 days (e.g. FreeBSD, macOS) will drop every + segment once that point is passed. The connection then stalls until it + times out. Traffic in either direction, including TCP keepalives, more + often than this interval prevents it. + + Progress is visible through the TCPTSId* counters in + /proc/net/netstat. + + Checkpoint/restore is not supported. The identifier is selected by BPF + when the SYN arrives and cannot be restored onto a new socket, so a + connection carrying one will emit unencoded timestamp values after a + TCP_REPAIR restore and be rejected by the peer's PAWS check. Leave + this at 0 on hosts that migrate established connections. + + Default: 0 (disabled) + tcp_min_tso_segs - INTEGER Minimal number of segments per TSO frame.
Documentation/networking/net_cachelines/netns_ipv4_sysctl.rst +1 -0
@@ -87,6 +87,7 @@ unsigned_int sysctl_tcp_notsent_lowat rea u8 sysctl_tcp_sack tcp_syn_options u8 sysctl_tcp_window_scaling tcp_syn_options,tcp_parse_options u8 sysctl_tcp_timestamps +u8 sysctl_tcp_ts_id_bits bpf_skops_tcp_ts_id u8 sysctl_tcp_early_retrans read_mostly tcp_schedule_loss_probe(tcp_write_xmit) u32 sysctl_tcp_rto_max_ms u8 sysctl_tcp_recovery tcp_fastretrans_alert
include/net/netns/ipv4.h +1 -0
@@ -204,6 +204,7 @@ struct netns_ipv4 {u8 sysctl_tcp_sack;u8 sysctl_tcp_window_scaling;u8 sysctl_tcp_timestamps; +u8 sysctl_tcp_ts_id_bits;int sysctl_tcp_rto_min_us;int sysctl_tcp_rto_max_ms;u8 sysctl_tcp_recovery;
include/net/tcp.h +8 -0
@@ -1105,6 +1105,14 @@ static inline u32 tcp_tsecr_decode(u32 tsecr, u32 tsoffset, u8 ts_id_bits,return now +sign_extend32 (tsecr - now,31 - ts_id_bits); } +/* A TSecr echoing one of our TSvals must still carry our identifier in its +* low bits; anything else means the echo was rewritten in flight. +*/ +static inline bool tcp_tsecr_id_ok (u32 tsecr,u8 ts_id,u8 ts_id_bits) +{ +return (tsecr &tcp_ts_id_mask (ts_id_bits)) == ts_id; +} +/* Keeps the timestamp clock read behind the branch: it is only needed to * re-center an echo that was truncated to make room for an identifier. */
include/uapi/linux/bpf.h +14 -1
@@ -7062,6 +7062,7 @@ struct bpf_sock_ops {* header has not been written. * BPF_SOCK_OPS_WRITE_HDR_OPT_CB: The header and options have * been written so far. +* BPF_SOCK_OPS_TCP_TS_ID_CB: The SYN received by a listener. * BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: The SYNACK that concludes * the 3WHS. * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes
@@ -7137,8 +7138,12 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf to select a TCP timestamp connection ID for a passive +* connection before sending the SYN-ACK. +*/ +BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; enum {
@@ -7282,6 +7287,14 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_TCP_TS_ID_CB, /* Called after a passive SYN created a +* request socket and before the SYN-ACK is +* sent. Not called for SYN cookies. Arg2 is +* the number of low timestamp bits available +* for an identifier. Set reply to an identifier +* that fits in Arg2 bits. Replies are ignored +* for usec timestamps. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
include/uapi/linux/snmp.h +4 -0
@@ -306,6 +306,10 @@ enumLINUX_MIB_TCPAOKEYNOTFOUND ,/* TCPAOKeyNotFound */ LINUX_MIB_TCPAOGOOD ,/* TCPAOGood */ LINUX_MIB_TCPAODROPPEDICMPS ,/* TCPAODroppedIcmps */ +LINUX_MIB_TCPTSIDASSIGNED ,/* TCPTSIdAssigned */ +LINUX_MIB_TCPTSIDDECLINED ,/* TCPTSIdDeclined */ +LINUX_MIB_TCPTSIDSKIPPED ,/* TCPTSIdSkipped */ +LINUX_MIB_TCPTSIDECHOMISMATCH ,/* TCPTSIdEchoMismatch */ __LINUX_MIB_MAX };
net/ipv4/proc.c +4 -0
@@ -302,6 +302,10 @@ static const struct snmp_mib snmp4_net_list[] = {SNMP_MIB_ITEM ("TCPAOKeyNotFound" ,LINUX_MIB_TCPAOKEYNOTFOUND ),SNMP_MIB_ITEM ("TCPAOGood" ,LINUX_MIB_TCPAOGOOD ),SNMP_MIB_ITEM ("TCPAODroppedIcmps" ,LINUX_MIB_TCPAODROPPEDICMPS ), +SNMP_MIB_ITEM ("TCPTSIdAssigned" ,LINUX_MIB_TCPTSIDASSIGNED ), +SNMP_MIB_ITEM ("TCPTSIdDeclined" ,LINUX_MIB_TCPTSIDDECLINED ), +SNMP_MIB_ITEM ("TCPTSIdSkipped" ,LINUX_MIB_TCPTSIDSKIPPED ), + SNMP_MIB_ITEM("TCPTSIdEchoMismatch", LINUX_MIB_TCPTSIDECHOMISMATCH), }; static void icmpmsg_put_line(struct seq_file *seq, unsigned long *vals,
net/ipv4/sysctl_net_ipv4.c +10 -0
@@ -55,6 +55,7 @@ static int tcp_min_rcvbuf = 4096;/* obsolete */ static int sysctl_tcp_low_latency__read_mostly ; +static const unsigned int tcp_ts_id_bits_max =TCP_TS_ID_BITS_MAX ;/* Update system visible IP port range */ static void set_local_port_range (struct net *net,unsigned int low,unsigned int high)
@@ -1227,6 +1228,15 @@ static const struct ctl_table ipv4_net_table[] = { .mode = 0644, .proc_handler = proc_dou8vec_minmax, }, + { + .procname = "tcp_ts_id_bits", + .data = &init_net.ipv4.sysctl_tcp_ts_id_bits, + .maxlen = sizeof(u8), + .mode = 0644, + .proc_handler = proc_dou8vec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = (void *)&tcp_ts_id_bits_max, + }, { .procname = "tcp_early_retrans", .data = &init_net.ipv4.sysctl_tcp_early_retrans,
net/ipv4/tcp_input.c +50 -0
68 lines not on this page · raw message
net/ipv4/tcp_ipv4.c +1 -0
@@ -3473,6 +3473,7 @@ static int __net_init tcp_sk_init(struct net *net) net->ipv4.sysctl_tcp_sack =1 ; net->ipv4.sysctl_tcp_window_scaling =1 ; net->ipv4.sysctl_tcp_timestamps =1 ; + net->ipv4.sysctl_tcp_ts_id_bits =0 ;/* Disabled by default */ net->ipv4.sysctl_tcp_early_retrans =3 ; net->ipv4.sysctl_tcp_recovery =TCP_RACK_LOSS_DETECTION ; net->ipv4.sysctl_tcp_slow_start_after_idle =1 ;/* By default, RFC2861 behavior. */
net/ipv4/tcp_minisocks.c +9 -0
@@ -719,6 +719,15 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, tsecr_reject = !between (tmp_opt.rcv_tsecr,tcp_rsk (req)->snt_tsval_first,READ_ONCE (tcp_rsk (req)->snt_tsval_last)); +/* First echo of a TSval we encoded: a mismatch +* here means TSecr was rewritten in flight. +*/ +if (tcp_rsk (req)->ts_id_bits && + !tcp_tsecr_id_ok (tmp_opt.rcv_tsecr, +tcp_rsk (req)->ts_id, +tcp_rsk (req)->ts_id_bits)) +NET_INC_STATS (sock_net (sk), +LINUX_MIB_TCPTSIDECHOMISMATCH ); tmp_opt.rcv_tsecr = tcp_tsecr_decode(tmp_opt.rcv_tsecr, tcp_rsk(req)->ts_off,
tools/include/uapi/linux/bpf.h +14 -1
@@ -7062,6 +7062,7 @@ struct bpf_sock_ops {* header has not been written. * BPF_SOCK_OPS_WRITE_HDR_OPT_CB: The header and options have * been written so far. +* BPF_SOCK_OPS_TCP_TS_ID_CB: The SYN received by a listener. * BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: The SYNACK that concludes * the 3WHS. * BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: The ACK that concludes
@@ -7137,8 +7138,12 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf to select a TCP timestamp connection ID for a passive +* connection before sending the SYN-ACK. +*/ +BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG = (1 <<7 ),/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, }; enum {
@@ -7282,6 +7287,14 @@ enum {* sendmsg timestamp with corresponding * tskey. */ + BPF_SOCK_OPS_TCP_TS_ID_CB, /* Called after a passive SYN created a +* request socket and before the SYN-ACK is +* sent. Not called for SYN cookies. Arg2 is +* the number of low timestamp bits available +* for an identifier. Set reply to an identifier +* that fits in Arg2 bits. Replies are ignored +* for usec timestamps. +*/ };/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +1 -1
@@ -26,7 +26,7 @@ static void verify_result(struct tcpbpf_globals *result)ASSERT_EQ (result->bytes_acked,1002 ,"bytes_acked" );ASSERT_EQ (result->data_segs_in,1 ,"data_segs_in" );ASSERT_EQ (result->data_segs_out,1 ,"data_segs_out" ); -ASSERT_EQ (result->bad_cb_test_rv,0x80 ,"bad_cb_test_rv" ); +ASSERT_EQ (result->bad_cb_test_rv,0x100 ,"bad_cb_test_rv" );ASSERT_EQ (result->good_cb_test_rv,0 ,"good_cb_test_rv" );ASSERT_EQ (result->num_listen,1 ,"num_listen" );
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +1 -1
@@ -92,7 +92,7 @@ int bpf_testcb(struct bpf_sock_ops *skops)break ;case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB :/* Test failure to set largest cb flag (assumes not defined) */ - global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,0x80 ); + global.bad_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,0x100 );/* Set callback */ global.good_cb_test_rv =bpf_sock_ops_cb_flags_set (skops,BPF_SOCK_OPS_STATE_CB_FLAG );
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH net-next 2/4] tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] bpf: bpf_load_hdr_opt() returns -EOPNOTSUPP for BPF_SOCK_OPS_TCP_TS_ID_CB - [Low] bpf: UAPI…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] bpf: bpf_load_hdr_opt() returns -EOPNOTSUPP for BPF_SOCK_OPS_TCP_TS_ID_CB
- [Low] bpf: UAPI multi-line comments violate subsystem guidelines
--
commit 5e1ec68deb60daa79748ce9bcb640700acc7dd70
Author: Willem Ferguson <[email protected]>
tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
Let a sock_ops program pick the identifier that the previous patch encodes in the low bits of TSval, for passively opened connections.
Quoted 4 lines
include/uapi/linux/bpf.h +0
-0
[ ... ]
include/uapi/linux/bpf.h enum { · line 7144
Quoted 8 lines
include/uapi/linux/bpf.h +4
-0@@ -7137,8 +7138,12 @@ enum {* options first before the BPF program does. */ BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG = (1 <<6 ), +/* Call bpf to select a TCP timestamp connection ID for a passive +* connection before sending the SYN-ACK. +*/ + BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG = (1 << 7),
[Severity: Low]
Does this multi-line comment violate the BPF subsystem guidelines? The guidelines strictly require multi-line comments to have the opening /* on its own line, with the comment text starting on the next line.
Quoted 8 lines
include/uapi/linux/bpf.h +1
-1/* Mask of all currently supported cb flags */ - BPF_SOCK_OPS_ALL_CB_FLAGS = 0x7F, + BPF_SOCK_OPS_ALL_CB_FLAGS = 0xFF, };enum {@@ -7282,6 +7287,14 @@ enum {* sendmsg timestamp with corresponding
include/uapi/linux/bpf.h enum { · line 7297
Quoted 10 lines
include/uapi/linux/bpf.h +8
-0* tskey. */ +BPF_SOCK_OPS_TCP_TS_ID_CB ,/* Called after a passive SYN created a +* request socket and before the SYN-ACK is +* sent. Not called for SYN cookies. Arg2 is +* the number of low timestamp bits available +* for an identifier. Set reply to an identifier +* that fits in Arg2 bits. Replies are ignored +* for usec timestamps. + */
[Severity: Low]
Similar to the above, does this multi-line comment violate the BPF subsystem guidelines by starting text on the same line as the opening /*?
[Severity: Medium]
The commit message claims that the BPF program can use bpf_load_hdr_opt() to inspect the incoming SYN packet during the BPF_SOCK_OPS_TCP_TS_ID_CB callback.
However, the backing kernel helper bpf_sock_ops_load_hdr_opt() restricts access by calling is_locked_tcp_sock_ops():
net/core/filter.c:is_locked_tcp_sock_ops() {
return bpf_sock->op <= BPF_SOCK_OPS_WRITE_HDR_OPT_CB;
}Because BPF_SOCK_OPS_TCP_TS_ID_CB is appended to the bpf_sock_ops enum here, it has a higher integer value than BPF_SOCK_OPS_WRITE_HDR_OPT_CB and fails this check.
Does this cause bpf_load_hdr_opt() to unconditionally return -EOPNOTSUPP, silently breaking the documented feature capability?
Quoted 3 lines
include/uapi/linux/bpf.h +0
-0};/* List of TCP states. There is a build check in net/ipv4/tcp.c to detect
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [RFC PATCH net-next 2/4] tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
In reply to Willem FergusonQuoted 28 lines · Willem Ferguson
Let a
sock_opsprogram pick the identifier that the previous patch encodes in the low bits of TSval, for passively opened connections.The width is set per network namespace by the new
tcp_ts_id_bitssysctl (0 to 8, default 0). A listener opts in by settingBPF_SOCK_OPS_TCP_TS_ID_CB_FLAG, typically fromBPF_SOCK_OPS_TCP_LISTEN_CB.tcp_conn_request()then runsBPF_SOCK_OPS_TCP_TS_ID_CBonce the request socket exists and before the SYN-ACK is built, so the SYN-ACK already carries the identifier. The program sees the request socket, the SYN (bpf_load_hdr_opt()works on it) and the available width in args[1]. A reply that fits in that width is stored with the width in the request socket and is inherited by the child, including Fast Open children; any other reply leaves the connection unencoded.The callback is not run for SYN cookies, which have nowhere to keep the identifier, or when the peer did not negotiate timestamps. It is skipped for connections using microsecond timestamps and while the sysctl is 0. The width is captured per connection, so changing the sysctl does not affect existing connections.
New
/proc/net/netstatcounters report how often an identifier was assigned, declined by the program or skipped. TCPTSIdEchoMismatch counts third ACKs whose TSecr no longer carries the identifier, which shows the echo was rewritten in flight.
BPF_SOCK_OPS_ALL_CB_FLAGSgrows from0x7fto0xff. The tcpbpf selftest used0x80as an example of an unsupported flag, so move it to0x100.
Quoted 8 lines
Encoding makes the timestamp clock appear 2^bits times faster to the peer. That shrinks the real-time span of the peer's PAWS window, and peers that keep
ts_recentfor the RFC 7323 24 days stall connections idle for longer than 24.8/2^bits days.ip-sysctl.rstdocuments both, along with the lack ofTCP_REPAIRsupport.Signed-off-by Willem Ferguson <[email protected]>---
I think SOCK_OPS are considered legacy.
We probably want bpf_tcp_ops struct_ops.
See [1] for an example.
Re: [RFC PATCH net-next 2/4] tcp: add BPF_SOCK_OPS_TCP_TS_ID_CB to assign a TSval identifier
Quoted 36 lines · Jakub Sitnicki
Quoted 34 lines · Willem Ferguson
Let a
sock_opsprogram pick the identifier that the previous patch encodes in the low bits of TSval, for passively opened connections.The width is set per network namespace by the new
tcp_ts_id_bitssysctl (0 to 8, default 0). A listener opts in by settingBPF_SOCK_OPS_TCP_TS_ID_CB_FLAG, typically fromBPF_SOCK_OPS_TCP_LISTEN_CB.tcp_conn_request()then runsBPF_SOCK_OPS_TCP_TS_ID_CBonce the request socket exists and before the SYN-ACK is built, so the SYN-ACK already carries the identifier. The program sees the request socket, the SYN (bpf_load_hdr_opt()works on it) and the available width in args[1]. A reply that fits in that width is stored with the width in the request socket and is inherited by the child, including Fast Open children; any other reply leaves the connection unencoded.The callback is not run for SYN cookies, which have nowhere to keep the identifier, or when the peer did not negotiate timestamps. It is skipped for connections using microsecond timestamps and while the sysctl is 0. The width is captured per connection, so changing the sysctl does not affect existing connections.
New
/proc/net/netstatcounters report how often an identifier was assigned, declined by the program or skipped. TCPTSIdEchoMismatch counts third ACKs whose TSecr no longer carries the identifier, which shows the echo was rewritten in flight.
BPF_SOCK_OPS_ALL_CB_FLAGSgrows from0x7fto0xff. The tcpbpf selftest used0x80as an example of an unsupported flag, so move it to0x100.Encoding makes the timestamp clock appear 2^bits times faster to the peer. That shrinks the real-time span of the peer's PAWS window, and peers that keep
ts_recentfor the RFC 7323 24 days stall connections idle for longer than 24.8/2^bits days.ip-sysctl.rstdocuments both, along with the lack ofTCP_REPAIRsupport.
Quoted 9 lines
Quoted 2 lines
Signed-off-by Willem Ferguson <[email protected]>---
I think
SOCK_OPSare considered legacy.
We probably wantbpf_tcp_opsstruct_ops.
See [1] for an example.
Exactly!
Thanks,
Jason
RFC PATCH 3/4 tcp: add TCP_TS_ID socket option
In reply to Willem FergusonAdd a read-only TCP_TS_ID option so applications and tests can see which TSval identifier, if any, was assigned to a connection. The value packs the identifier width into bits 8..15 and the identifier into bits 0..7. Zero means no identifier is encoded.
The option cannot be set. The identifier is chosen by BPF_SOCK_OPS_TCP_TS_ID_CB when the SYN arrives, and there is no way to restore it onto a new socket, so connections that carry one do not survive TCP_REPAIR. ip-sysctl.rst already documents that limitation.
--- Documentation/networking/ip-sysctl.rst | 3 ++- include/uapi/linux/tcp.h | 8 ++++++++ net/ipv4/tcp.c | 3 +++ 3 files changed, 13 insertions(+), 1 deletion(-)
Documentation/networking/ip-sysctl.rst +2 -1
@@ -1123,7 +1123,8 @@ tcp_ts_id_bits - INTEGER often than this interval prevents it. Progress is visible through the TCPTSId* counters in - /proc/net/netstat. + /proc/net/netstat, and a connection's identifier can be read with the + TCP_TS_ID socket option. Checkpoint/restore is not supported. The identifier is selected by BPF when the SYN arrives and cannot be restored onto a new socket, so a
include/uapi/linux/tcp.h +8 -0
@@ -142,6 +142,14 @@ enum {#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_TS_ID 47/* Read TSval identifier and its width */ + +/* TCP_TS_ID value layout: identifier width in bits 8..15, identifier in +* bits 0..7. A value of 0 means no identifier is encoded. Read-only: the +* identifier is chosen by BPF_SOCK_OPS_TCP_TS_ID_CB and cannot be restored, +* so connections with an identifier do not survive TCP_REPAIR. +*/ +#define TCP_TS_ID_BITS_SHIFT 8#define TCP_REPAIR_ON 1#define TCP_REPAIR_OFF 0
net/ipv4/tcp.c +3 -0
@@ -4711,6 +4711,9 @@ int do_tcp_getsockopt(struct sock *sk, int level,else val &= ~1 ;break ; +case TCP_TS_ID : + val = ((u32 )tp->ts_id_bits <<TCP_TS_ID_BITS_SHIFT ) | tp->ts_id; +break ;case TCP_NOTSENT_LOWAT : val =READ_ONCE (tp->notsent_lowat);break ;
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [RFC PATCH net-next 3/4] tcp: add TCP_TS_ID socket option
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] tcp: lockless data race in do_tcp_getsockopt for TCP_TS_ID --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] tcp: lockless data race in do_tcp_getsockopt for TCP_TS_ID
--
commit 7c66b48d6e891deb8097ef54e673d4be713fb65b
Author: Willem Ferguson <[email protected]>
tcp: add TCP_TS_ID socket option
Add a read-only TCP_TS_ID socket option so applications and tests can see which TSval identifier was assigned to a connection. The value packs the identifier width and the identifier itself, chosen by BPF when the SYN arrives.
net/ipv4/tcp.c int do_tcp_getsockopt(struct sock *sk, int level, · line 4716
Quoted 11 lines
net/ipv4/tcp.c +3
-0@@ -4711,6 +4711,9 @@ int do_tcp_getsockopt(struct sock *sk, int level,else val &= ~1 ;break ; +case TCP_TS_ID : + val = ((u32 )tp->ts_id_bits <<TCP_TS_ID_BITS_SHIFT ) | tp->ts_id; +break ;
[Severity: Low]
Is it possible for tp->ts_id and tp->ts_id_bits to be concurrently modified here without the socket lock?
Looking at do_tcp_getsockopt(), these fields are read locklessly. If the socket is concurrently disconnected (for example, by calling connect() with AF_UNSPEC, which calls tcp_disconnect()), the fields are zeroed out:
net/ipv4/tcp.c:tcp_disconnect() {
...
tp->ts_id = 0;
tp->ts_id_bits = 0;
...
}Could this concurrent modification trigger a KCSAN data race warning or result in returning an inconsistent or torn value to userspace?
Other locklessly accessed fields in this switch statement use READ_ONCE(), like tp->notsent_lowat right below this. Would it make sense to use READ_ONCE() here and WRITE_ONCE() in tcp_disconnect() to prevent this?
Quoted 3 lines
net/ipv4/tcp.c +0
-0case TCP_NOTSENT_LOWAT : val =READ_ONCE (tp->notsent_lowat);break ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
RFC PATCH 4/4 selftests/bpf: add tcp_ts_id test
In reply to Willem FergusonExercise BPF_SOCK_OPS_TCP_TS_ID_CB end to end over IPv6 loopback.
A cgroup sock_ops program sets BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG on the listener and answers the callback. With tcp_timestamps=1 and tcp_ts_id_bits=4 the test checks that:
- the callback runs exactly once, on the SYN, with args[1] equal to the configured width;
- the accepted socket reports the identifier and width through TCP_TS_ID;
- the connecting socket reports no identifier, as only passive opens are covered;
- a reply that does not fit in the width ("decline" subtest) leaves the accepted socket without an identifier.
The test skips when tcp_ts_id_bits does not exist and restores both sysctls afterwards. It defines TCP_TS_ID locally when the libc headers do not have it yet.
--- .../selftests/bpf/prog_tests/tcp_ts_id.c | 150 ++++++++++++++++++ .../selftests/bpf/progs/test_tcp_ts_id_kern.c | 32 ++++ 2 files changed, 182 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c create mode 100644 tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c
tools/testing/selftests/bpf/prog_tests/tcp_ts_id.c new file +150 -0
@@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <errno.h> + +#include <test_progs.h> +#include <network_helpers.h> + +#include "test_tcp_ts_id_kern.skel.h" + +#define LO_ADDR6 "::1" +#define CG_NAME "/tcp-ts-id-test" + +#ifndef TCP_TS_ID +#define TCP_TS_ID 47 +#define TCP_TS_ID_BITS_SHIFT 8 +#endif + +#define TS_ID_BITS_SYSCTL "/proc/sys/net/ipv4/tcp_ts_id_bits" +#define TIMESTAMPS_SYSCTL "/proc/sys/net/ipv4/tcp_timestamps" +#define EXPECTED_TS_ID 7 +#define EXPECTED_TS_ID_BITS 4 +#define EXPECTED_TS_ID_BITS_STR "4" + +static char saved_ts_id_bits[16 ]; +static char saved_timestamps[16 ]; + +static int save_sysctl (const char *path,char *buf) +{ +int err =0 ; +FILE *f; + + f =fopen (path,"r" ); +if (!f) +return -errno; + +if (fscanf (f,"%15s" , buf) !=1 ) + err = -EINVAL ; +fclose (f); + +return err; +} + +static void restore_sysctl (const char *path,const char *buf) +{ +if (buf[0 ]) +write_sysctl (path, buf); +} + +static void run_ts_id_test (int cg_fd,bool decline) +{ +int listen_fd =-1 , cli_fd =-1 , accept_fd =-1 ; +struct test_tcp_ts_id_kern *skel; +socklen_t optlen; +int ts_id; + + skel =test_tcp_ts_id_kern__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + skel->bss->ts_id_reply =EXPECTED_TS_ID ; + skel->bss->ts_id_decline = decline; + + skel->links.tcp_ts_id_sockops = +bpf_program__attach_cgroup (skel->progs.tcp_ts_id_sockops, + cg_fd); +if (!ASSERT_OK_PTR (skel->links.tcp_ts_id_sockops,"attach_cgroup" )) + goto out; + +/* The listener has to be created after the attach to get LISTEN_CB. */ + listen_fd =start_server (AF_INET6 ,SOCK_STREAM ,LO_ADDR6 ,0 ,0 ); +if (!ASSERT_NEQ (listen_fd,-1 ,"start_server" )) + goto out; + + cli_fd =connect_to_fd (listen_fd,0 ); +if (!ASSERT_NEQ (cli_fd,-1 ,"connect_to_fd" )) + goto out; + + accept_fd =accept (listen_fd,NULL ,NULL ); +if (!ASSERT_NEQ (accept_fd,-1 ,"accept" )) + goto out; + +ASSERT_EQ (skel->bss->ts_id_cb_count,1 ,"cb_count" ); +ASSERT_EQ (skel->bss->ts_id_cb_syn,1 ,"cb_on_syn" ); +ASSERT_EQ (skel->bss->ts_id_cb_bits,EXPECTED_TS_ID_BITS ,"cb_bits" ); + +/* The identifier must reach the child socket created by the SYN. */ + optlen =sizeof (ts_id); +if (ASSERT_OK (getsockopt (accept_fd,IPPROTO_TCP ,TCP_TS_ID , &ts_id, + &optlen),"getsockopt(accept_fd, TCP_TS_ID)" )) +ASSERT_EQ (ts_id, decline ?0 : + (EXPECTED_TS_ID_BITS <<TCP_TS_ID_BITS_SHIFT ) | +EXPECTED_TS_ID ,"server_ts_id" ); + +/* Identifiers are passive-open only, so the opener must have none. */ + optlen =sizeof (ts_id); +if (ASSERT_OK (getsockopt (cli_fd,IPPROTO_TCP ,TCP_TS_ID , &ts_id, + &optlen),"getsockopt(cli_fd, TCP_TS_ID)" )) +ASSERT_EQ (ts_id,0 ,"client_ts_id" ); +out: +if (accept_fd !=-1 ) +close (accept_fd); +if (cli_fd !=-1 ) +close (cli_fd); +if (listen_fd !=-1 ) +close (listen_fd); +test_tcp_ts_id_kern__destroy (skel); +} + +void test_tcp_ts_id (void ) +{ +int cg_fd =-1 ; +int err; + + err =save_sysctl (TS_ID_BITS_SYSCTL , saved_ts_id_bits); +if (err == -ENOENT ) { +test__skip (); +return ; + } +if (!ASSERT_OK (err,"read " TS_ID_BITS_SYSCTL )) +return ; + +if (!ASSERT_OK (save_sysctl (TIMESTAMPS_SYSCTL , saved_timestamps), +"read " TIMESTAMPS_SYSCTL )) + goto out; + +/* The callback only runs when timestamps are negotiated. */ +if (!ASSERT_OK (write_sysctl (TIMESTAMPS_SYSCTL ,"1" ), +"write " TIMESTAMPS_SYSCTL )) + goto out; + +/* Identifiers are disabled by default, so the test has to opt in. */ +if (!ASSERT_OK (write_sysctl (TS_ID_BITS_SYSCTL ,EXPECTED_TS_ID_BITS_STR ), +"write " TS_ID_BITS_SYSCTL )) + goto out; + + cg_fd =test__join_cgroup (CG_NAME ); +if (!ASSERT_GE (cg_fd,0 ,"test__join_cgroup(" CG_NAME ")" )) + goto out; + +if (test__start_subtest ("assign" )) +run_ts_id_test (cg_fd, false); + +/* Only one sockops program may be attached to the cgroup at a time. */ +if (test__start_subtest ("decline" )) +run_ts_id_test (cg_fd, true); +out: +if (cg_fd !=-1 ) +close (cg_fd); +restore_sysctl (TIMESTAMPS_SYSCTL , saved_timestamps); +restore_sysctl (TS_ID_BITS_SYSCTL , saved_ts_id_bits); +}
tools/testing/selftests/bpf/progs/test_tcp_ts_id_kern.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +__u32 ts_id_cb_count; +__u32 ts_id_cb_syn; +__u32 ts_id_cb_bits; +__u32 ts_id_reply; +__u32 ts_id_decline;/* set by userspace to exercise the decline path */ + +SEC("sockops") +int tcp_ts_id_sockops (struct bpf_sock_ops *skops) +{ +if (skops->op ==BPF_SOCK_OPS_TCP_LISTEN_CB ) { +bpf_sock_ops_cb_flags_set (skops, +BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG ); +return 1 ; + } + +if (skops->op !=BPF_SOCK_OPS_TCP_TS_ID_CB ) +return 1 ; + + ts_id_cb_count++; + ts_id_cb_syn = !!(skops->skb_tcp_flags & (1U <<1 )); + ts_id_cb_bits = skops->args[1 ]; +/* -1 does not fit in Arg2 bits, so the kernel assigns no ID. */ + skops->reply = ts_id_decline ?-1 : ts_id_reply; + +return 1 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.47.3
Quoted 55 lines · Willem Ferguson
This series lets a listener encode a small per-connection identifier in the low bits of the TCP timestamp values it sends. The peer echoes TSval back in TSecr, so every segment the peer sends after the handshake carries the identifier in its TSecr.
In Anycast networks, mid-stream path changes caused by BGP route flapping or ECMP shifts frequently deliver active TCP packets to an ingress Point of Presence (PoP) that lacks the flow's socket state, resulting in abrupt connection resets (RST). While protocols like QUIC avoid this using explicit Connection IDs, TCP lacks an in-band routing token to help ingress nodes identify where a flow actually lives. To address this problem, we propose embedding an N-bit data center ID into the lower-order bits of the TCP Timestamp option (TSval, RFC 7323), allowing edge load balancers to recognize misdirected packets and steer them to the correct origin data center holding the active session.
How it works:
-
tcp_ts_id_bits(per netns, 0..8, default 0) sets how many low TSval bits are reserved. The millisecond clock is shifted up by that many bits.
- Asock_opsprogram opts a listener in withBPF_SOCK_OPS_TCP_TS_ID_CB_FLAGand is called withBPF_SOCK_OPS_TCP_TS_ID_CBfor each SYN, before the SYN-ACK is sent. It sees the SYN and optionally returns the identifier
- The identifier and width are stored in the request socket, then in the full and timewait sockets. Incoming TSecr values are decoded before RTT estimation and PAWS use them.
-TCP_TS_IDreads back the identifier, and/proc/net/netstatgains TCPTSIdAssigned, TCPTSIdDeclined, TCPTSIdSkipped and TCPTSIdEchoMismatch.RTT estimation on the encoding host sees the same decoded timestamps as before, so they are unchanged. On the peer, PAWS keeps working too; the only effects are a shorter idle limit and a narrower tolerance for reordered segments, both described below.
Scope and limitations:
- Passive opens only. SYN cookies, microsecond timestamps and peers without timestamps are left unencoded.
- The peer sees a clock running 2^bits times faster. That narrows the real-time span of its PAWS window, so the width should be no larger than the identifiers need.
- Peers that keepts_recentfor the RFC 7323 24 days (e.g. FreeBSD, macOS) stall connections that stay idle longer than 24.8/2^bits days (about 37 hours at 4 bits). Linux peers stop enforcing PAWS afterTCP_PAWS_WRAPand are unaffected.
-TCP_REPAIRcannot restore the identifier, so hosts that migrate established connections should leave the feature off.All of this is documented under
tcp_ts_id_bitsinip-sysctl.rst. With the sysctl at 0 (the default) the only new cost on the established fast path is onelikely()branch when parsing TSecr.
Quoted 5 lines
Questions for reviewers:
1. Is encoding an identifier in the low bits of TSval acceptable in principle, given the trade-offs above?
2. If so, what feedback do you have on the implementation?
Don't see any feedback yet, so from the top of my head: looks like you control two sides of the communication, so why not a custom (bpf) tcp option? Would you need that on every packet and it makes it expensive?
Or any other field you can abuse maybe? IPv6 Flow label?
Implementation-wise, the sock_ops is going away in favor of bpf_tcp_ops, so BPF_SOCK_OPS_TCP_TS_ID_CB_FLAG is a no go.
Hey Stan,
Quoted 1 line · Stanislav Fomichev
On 10/13, Willem Ferguson wrote:
[...]
Quoted 11 lines
Quoted 5 lines
Questions for reviewers:
1. Is encoding an identifier in the low bits of TSval acceptable in principle, given the trade-offs above?
2. If so, what feedback do you have on the implementation?Don't see any feedback yet, so from the top of my head: looks like you control two sides of the communication, so why not a custom (bpf) tcp option? Would you need that on every packet and it makes it expensive?
Or any other field you can abuse maybe? IPv6 Flow label?
Nah, we don't control the client unfortunately.
Client here is any device that connects to our edge PoP.
Quoted 2 lines
Implementation-wise, the
sock_opsis going away in favor ofbpf_tcp_ops, soBPF_SOCK_OPS_TCP_TS_ID_CB_FLAGis a no go.
Yeah, we have just recently realized [1].
Thanks for feedback.
Quoted 17 lines · Jakub Sitnicki
Hey Stan,
Quoted 1 line · Stanislav Fomichev
On 10/13, Willem Ferguson wrote:
[...]
Quoted 10 lines
Quoted 5 lines
Questions for reviewers:
1. Is encoding an identifier in the low bits of TSval acceptable in principle, given the trade-offs above?
2. If so, what feedback do you have on the implementation?Don't see any feedback yet, so from the top of my head: looks like you control two sides of the communication, so why not a custom (bpf) tcp option? Would you need that on every packet and it makes it expensive?
Quoted 5 lines
Quoted 1 line
Or any other field you can abuse maybe? IPv6 Flow label?
Nah, we don't control the client unfortunately.
Client here is any device that connects to our edge PoP.
I see, so you rely on the fact that it gets mirrored back.. Btw, looks like there is already some similar tsoffset/ts_off infra? Have you considered plugging into that?