xref: /linux/tools/testing/selftests/bpf/progs/tcp_ca_kfunc.c (revision 4ce06406958b67fdddcc2e6948237dd6ff6ba112)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024 Facebook */
3 
4 #include "vmlinux.h"
5 #include <bpf/bpf_tracing.h>
6 
7 extern void bbr_init(struct sock *sk) __ksym;
8 extern void bbr_main(struct sock *sk, u32 ack, int flag, const struct rate_sample *rs) __ksym;
9 extern u32 bbr_sndbuf_expand(struct sock *sk) __ksym;
10 extern u32 bbr_undo_cwnd(struct sock *sk) __ksym;
11 extern void bbr_cwnd_event_tx_start(struct sock *sk) __ksym;
12 extern u32 bbr_ssthresh(struct sock *sk) __ksym;
13 extern u32 bbr_min_tso_segs(struct sock *sk) __ksym;
14 extern void bbr_set_state(struct sock *sk, u8 new_state) __ksym;
15 
16 extern void dctcp_init(struct sock *sk) __ksym;
17 extern void dctcp_update_alpha(struct sock *sk, u32 flags) __ksym;
18 extern void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev) __ksym;
19 extern void dctcp_cwnd_event_tx_start(struct sock *sk) __ksym;
20 extern u32 dctcp_ssthresh(struct sock *sk) __ksym;
21 extern u32 dctcp_cwnd_undo(struct sock *sk) __ksym;
22 extern void dctcp_state(struct sock *sk, u8 new_state) __ksym;
23 
24 extern void cubictcp_init(struct sock *sk) __ksym;
25 extern u32 cubictcp_recalc_ssthresh(struct sock *sk) __ksym;
26 extern void cubictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked) __ksym;
27 extern void cubictcp_state(struct sock *sk, u8 new_state) __ksym;
28 extern void cubictcp_cwnd_event_tx_start(struct sock *sk) __ksym;
29 extern void cubictcp_acked(struct sock *sk, const struct ack_sample *sample) __ksym;
30 
31 SEC("struct_ops")
32 void BPF_PROG(init, struct sock *sk)
33 {
34 	bbr_init(sk);
35 	dctcp_init(sk);
36 	cubictcp_init(sk);
37 }
38 
39 SEC("struct_ops")
40 void BPF_PROG(in_ack_event, struct sock *sk, u32 flags)
41 {
42 	dctcp_update_alpha(sk, flags);
43 }
44 
45 SEC("struct_ops")
46 void BPF_PROG(cong_control, struct sock *sk, u32 ack, int flag, const struct rate_sample *rs)
47 {
48 	bbr_main(sk, ack, flag, rs);
49 }
50 
51 SEC("struct_ops")
52 void BPF_PROG(cong_avoid, struct sock *sk, u32 ack, u32 acked)
53 {
54 	cubictcp_cong_avoid(sk, ack, acked);
55 }
56 
57 SEC("struct_ops")
58 u32 BPF_PROG(sndbuf_expand, struct sock *sk)
59 {
60 	return bbr_sndbuf_expand(sk);
61 }
62 
63 SEC("struct_ops")
64 u32 BPF_PROG(undo_cwnd, struct sock *sk)
65 {
66 	bbr_undo_cwnd(sk);
67 	return dctcp_cwnd_undo(sk);
68 }
69 
70 SEC("struct_ops")
71 void BPF_PROG(cwnd_event, struct sock *sk, enum tcp_ca_event event)
72 {
73 	dctcp_cwnd_event(sk, event);
74 }
75 
76 SEC("struct_ops")
77 void BPF_PROG(cwnd_event_tx_start, struct sock *sk)
78 {
79 	bbr_cwnd_event_tx_start(sk);
80 	dctcp_cwnd_event_tx_start(sk);
81 	cubictcp_cwnd_event_tx_start(sk);
82 }
83 
84 SEC("struct_ops")
85 u32 BPF_PROG(ssthresh, struct sock *sk)
86 {
87 	bbr_ssthresh(sk);
88 	dctcp_ssthresh(sk);
89 	return cubictcp_recalc_ssthresh(sk);
90 }
91 
92 SEC("struct_ops")
93 u32 BPF_PROG(min_tso_segs, struct sock *sk)
94 {
95 	return bbr_min_tso_segs(sk);
96 }
97 
98 SEC("struct_ops")
99 void BPF_PROG(set_state, struct sock *sk, u8 new_state)
100 {
101 	bbr_set_state(sk, new_state);
102 	dctcp_state(sk, new_state);
103 	cubictcp_state(sk, new_state);
104 }
105 
106 SEC("struct_ops")
107 void BPF_PROG(pkts_acked, struct sock *sk, const struct ack_sample *sample)
108 {
109 	cubictcp_acked(sk, sample);
110 }
111 
112 SEC(".struct_ops")
113 struct tcp_congestion_ops tcp_ca_kfunc = {
114 	.init		= (void *)init,
115 	.in_ack_event	= (void *)in_ack_event,
116 	.cong_control	= (void *)cong_control,
117 	.cong_avoid	= (void *)cong_avoid,
118 	.sndbuf_expand	= (void *)sndbuf_expand,
119 	.undo_cwnd	= (void *)undo_cwnd,
120 	.cwnd_event	= (void *)cwnd_event,
121 	.cwnd_event_tx_start = (void *)cwnd_event_tx_start,
122 	.ssthresh	= (void *)ssthresh,
123 	.min_tso_segs	= (void *)min_tso_segs,
124 	.set_state	= (void *)set_state,
125 	.pkts_acked     = (void *)pkts_acked,
126 	.name		= "tcp_ca_kfunc",
127 };
128 
129 char _license[] SEC("license") = "GPL";
130