Skip to contentSkip to conversation

bpf: add sacked flag in BPF_SOCK_OPS_RETRANS_CB

PATCH In review Philo Lu 3 participants 4 messages Apr 17, 2024 – Apr 18, 2024

Subsystems (4)
lore
Share & tools
lore

Philo Lu

author
#0 lore raw

PATCH 1/1 bpf: add sacked flag in BPF_SOCK_OPS_RETRANS_CB

Add TCP_SKB_CB(skb)->sacked as the 4th arg of sockops passed to bpf program. Then we can get the retransmission efficiency by counting skbs w/ and w/o TCPCB_EVER_RETRANS mark. And for this purpose, sacked updating is moved after the BPF_SOCK_OPS_RETRANS_CB hook.

---
 include/net/tcp.h              | 14 ++++++++++++++
 include/uapi/linux/bpf.h       |  2 ++
 net/ipv4/tcp_output.c          |  9 +++++----
 tools/include/uapi/linux/bpf.h |  2 ++
 4 files changed, 23 insertions(+), 4 deletions(-)
include/net/tcp.h +14 -0
index 6ae35199d3b3..7defe67183c9 100644
@@ -2660,6 +2660,14 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
 	return tcp_call_bpf(sk, op, 3, args);
 }
 
+static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2,
+				    u32 arg3, u32 arg4)
+{
+	u32 args[4] = {arg1, arg2, arg3, arg4};
+
+	return tcp_call_bpf(sk, op, 4, args);
+}
+
 #else
 static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
 {
@@ -2677,6 +2685,12 @@ static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
 	return -EPERM;
 }
 
+static inline int tcp_call_bpf_4arg(struct sock *sk, int op, u32 arg1, u32 arg2,
+				    u32 arg3, u32 arg4)
+{
+	return -EPERM;
+}
+
 #endif
 
 static inline u32 tcp_timeout_init(struct sock *sk)
include/uapi/linux/bpf.h +2 -0
index cee0a7915c08..df6bb9a62e0b 100644
@@ -6938,6 +6938,8 @@ enum {
 					 * Arg2: # segments
 					 * Arg3: return value of
 					 *       tcp_transmit_skb (0 => success)
+					 * Arg4: TCP_SKB_CB(skb)->sacked before
+					 *       TCPCB_EVER_RETRANS marking
 					 */
 	BPF_SOCK_OPS_STATE_CB,		/* Called when TCP changes state.
 					 * Arg1: old_state
net/ipv4/tcp_output.c +5 -4
index e3167ad96567..370e6cee6794 100644
@@ -3387,15 +3387,16 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)
 		err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
 	}
 
+	if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG))
+		tcp_call_bpf_4arg(sk, BPF_SOCK_OPS_RETRANS_CB,
+				  TCP_SKB_CB(skb)->seq, segs, err,
+				  TCP_SKB_CB(skb)->sacked);
+
 	/* To avoid taking spuriously low RTT samples based on a timestamp
 	 * for a transmit that never happened, always mark EVER_RETRANS
 	 */
 	TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;
 
-	if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG))
-		tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB,
-				  TCP_SKB_CB(skb)->seq, segs, err);
-
 	if (likely(!err)) {
 		trace_tcp_retransmit_skb(sk, skb);
 	} else if (err != -EBUSY) {
tools/include/uapi/linux/bpf.h +2 -0
index cee0a7915c08..df6bb9a62e0b 100644
@@ -6938,6 +6938,8 @@ enum {
 					 * Arg2: # segments
 					 * Arg3: return value of
 					 *       tcp_transmit_skb (0 => success)
+					 * Arg4: TCP_SKB_CB(skb)->sacked before
+					 *       TCPCB_EVER_RETRANS marking
 					 */
 	BPF_SOCK_OPS_STATE_CB,		/* Called when TCP changes state.
 					 * Arg1: old_state
Signature · 1 line
2.32.0.3.g01195cf9f
#1 lore raw
Quoted 7 lines · Philo Lu

Add TCP_SKB_CB(skb)->sacked as the 4th arg of sockops passed to bpf program. Then we can get the retransmission efficiency by counting skbs w/ and w/o TCPCB_EVER_RETRANS mark. And for this purpose, sacked updating is moved after the BPF_SOCK_OPS_RETRANS_CB hook.

This might be a naive question, but how the bpf program know what is the meaning of each bit ?

Are they exposed already, and how future changes in TCP stack could break old bpf programs ?

#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */ #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */ #define TCPCB_LOST 0x04 /* SKB is lost */
#define TCPCB_TAGBITS 0x07 /* All tag bits */
#define TCPCB_REPAIRED 0x10 /* SKB repaired (no skb_mstamp_ns) */ #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */ #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
TCPCB_REPAIRED)

#2 lore raw
Quoted 15 lines · Eric Dumazet
Quoted 7 lines · Philo Lu

Add TCP_SKB_CB(skb)->sacked as the 4th arg of sockops passed to bpf program. Then we can get the retransmission efficiency by counting skbs w/ and w/o TCPCB_EVER_RETRANS mark. And for this purpose, sacked updating is moved after the BPF_SOCK_OPS_RETRANS_CB hook.

This might be a naive question, but how the bpf program know what is the meaning of each bit ?

Are they exposed already, and how future changes in TCP stack could break old bpf programs ?

Quoted 8 lines

#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */ #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */ #define TCPCB_LOST 0x04 /* SKB is lost */
#define TCPCB_TAGBITS 0x07 /* All tag bits */
#define TCPCB_REPAIRED 0x10 /* SKB repaired (no skb_mstamp_ns) */ #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */ #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
TCPCB_REPAIRED)

I think it is the best to use the trace_tcp_retransmit_skb() tracepoint instead.

iiuc the use case, moving the "TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;" after the tracepoint should have similar effect.

If the TCPCB_* is moved to a enum, it will be included in the "vmlinux.h" that the bpf prog can use and no need to expose them in uapi.

author
#3 lore raw
Quoted 25 lines · Martin KaFai Lau
Quoted 15 lines · Eric Dumazet
Quoted 7 lines · Philo Lu

Add TCP_SKB_CB(skb)->sacked as the 4th arg of sockops passed to bpf program. Then we can get the retransmission efficiency by counting skbs w/ and w/o TCPCB_EVER_RETRANS mark. And for this purpose, sacked updating is moved after the BPF_SOCK_OPS_RETRANS_CB hook.

This might be a naive question, but how the bpf program know what is the meaning of each bit ?

Are they exposed already, and how future changes in TCP stack could break old bpf programs ?

Quoted 8 lines

#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */ #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */ #define TCPCB_LOST 0x04 /* SKB is lost */
#define TCPCB_TAGBITS 0x07 /* All tag bits */
#define TCPCB_REPAIRED 0x10 /* SKB repaired (no skb_mstamp_ns) */ #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */ #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
TCPCB_REPAIRED)

Quoted 3 lines

I think it is the best to use the trace_tcp_retransmit_skb() tracepoint instead.

iiuc the use case, moving the "TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;" after the tracepoint should have similar effect.

Good idea. This does also achieve this goal. So it would be like:

```
-TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;

 if (likely(!err)) {
 	trace_tcp_retransmit_skb(sk, skb);
 } else if (err != -EBUSY) {
 	NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs);
 }

+TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;
 return err;
```
Quoted 2 lines

If the TCPCB_* is moved to a enum, it will be included in the "vmlinux.h" that the bpf prog can use and no need to expose them in uapi.

This is okay for me. Though I'm not sure if moving to enum brings any unexpected side effect?

BTW, need we concern about those that use trace_tcp_retransmit_skb to check TCPCB_EVER_RETRANS before?

Thanks.