1
2
3
4#include <linux/types.h>
5#include <linux/bpf_verifier.h>
6#include <linux/bpf.h>
7#include <linux/btf.h>
8#include <linux/btf_ids.h>
9#include <linux/filter.h>
10#include <net/tcp.h>
11#include <net/bpf_sk_storage.h>
12
13
14extern struct bpf_struct_ops bpf_tcp_congestion_ops;
15
16static u32 optional_ops[] = {
17 offsetof(struct tcp_congestion_ops, init),
18 offsetof(struct tcp_congestion_ops, release),
19 offsetof(struct tcp_congestion_ops, set_state),
20 offsetof(struct tcp_congestion_ops, cwnd_event),
21 offsetof(struct tcp_congestion_ops, in_ack_event),
22 offsetof(struct tcp_congestion_ops, pkts_acked),
23 offsetof(struct tcp_congestion_ops, min_tso_segs),
24 offsetof(struct tcp_congestion_ops, sndbuf_expand),
25 offsetof(struct tcp_congestion_ops, cong_control),
26};
27
28static u32 unsupported_ops[] = {
29 offsetof(struct tcp_congestion_ops, get_info),
30};
31
32static const struct btf_type *tcp_sock_type;
33static u32 tcp_sock_id, sock_id;
34
35static int bpf_tcp_ca_init(struct btf *btf)
36{
37 s32 type_id;
38
39 type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
40 if (type_id < 0)
41 return -EINVAL;
42 sock_id = type_id;
43
44 type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
45 if (type_id < 0)
46 return -EINVAL;
47 tcp_sock_id = type_id;
48 tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
49
50 return 0;
51}
52
53static bool is_optional(u32 member_offset)
54{
55 unsigned int i;
56
57 for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
58 if (member_offset == optional_ops[i])
59 return true;
60 }
61
62 return false;
63}
64
65static bool is_unsupported(u32 member_offset)
66{
67 unsigned int i;
68
69 for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
70 if (member_offset == unsupported_ops[i])
71 return true;
72 }
73
74 return false;
75}
76
77extern struct btf *btf_vmlinux;
78
79static bool bpf_tcp_ca_is_valid_access(int off, int size,
80 enum bpf_access_type type,
81 const struct bpf_prog *prog,
82 struct bpf_insn_access_aux *info)
83{
84 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
85 return false;
86 if (type != BPF_READ)
87 return false;
88 if (off % size != 0)
89 return false;
90
91 if (!btf_ctx_access(off, size, type, prog, info))
92 return false;
93
94 if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
95
96 info->btf_id = tcp_sock_id;
97
98 return true;
99}
100
101static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
102 const struct btf *btf,
103 const struct btf_type *t, int off,
104 int size, enum bpf_access_type atype,
105 u32 *next_btf_id)
106{
107 size_t end;
108
109 if (atype == BPF_READ)
110 return btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
111
112 if (t != tcp_sock_type) {
113 bpf_log(log, "only read is supported\n");
114 return -EACCES;
115 }
116
117 switch (off) {
118 case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
119 end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
120 break;
121 case offsetof(struct inet_connection_sock, icsk_ack.pending):
122 end = offsetofend(struct inet_connection_sock,
123 icsk_ack.pending);
124 break;
125 case offsetof(struct tcp_sock, snd_cwnd):
126 end = offsetofend(struct tcp_sock, snd_cwnd);
127 break;
128 case offsetof(struct tcp_sock, snd_cwnd_cnt):
129 end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
130 break;
131 case offsetof(struct tcp_sock, snd_ssthresh):
132 end = offsetofend(struct tcp_sock, snd_ssthresh);
133 break;
134 case offsetof(struct tcp_sock, ecn_flags):
135 end = offsetofend(struct tcp_sock, ecn_flags);
136 break;
137 default:
138 bpf_log(log, "no write support to tcp_sock at off %d\n", off);
139 return -EACCES;
140 }
141
142 if (off + size > end) {
143 bpf_log(log,
144 "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
145 off, size, end);
146 return -EACCES;
147 }
148
149 return NOT_INIT;
150}
151
152BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
153{
154
155 __tcp_send_ack((struct sock *)tp, rcv_nxt);
156 return 0;
157}
158
159static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
160 .func = bpf_tcp_send_ack,
161 .gpl_only = false,
162
163 .ret_type = RET_INTEGER,
164 .arg1_type = ARG_PTR_TO_BTF_ID,
165 .arg1_btf_id = &tcp_sock_id,
166 .arg2_type = ARG_ANYTHING,
167};
168
169static u32 prog_ops_moff(const struct bpf_prog *prog)
170{
171 const struct btf_member *m;
172 const struct btf_type *t;
173 u32 midx;
174
175 midx = prog->expected_attach_type;
176 t = bpf_tcp_congestion_ops.type;
177 m = &btf_type_member(t)[midx];
178
179 return btf_member_bit_offset(t, m) / 8;
180}
181
182static const struct bpf_func_proto *
183bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
184 const struct bpf_prog *prog)
185{
186 switch (func_id) {
187 case BPF_FUNC_tcp_send_ack:
188 return &bpf_tcp_send_ack_proto;
189 case BPF_FUNC_sk_storage_get:
190 return &bpf_sk_storage_get_proto;
191 case BPF_FUNC_sk_storage_delete:
192 return &bpf_sk_storage_delete_proto;
193 case BPF_FUNC_setsockopt:
194
195
196
197
198
199
200 if (prog_ops_moff(prog) !=
201 offsetof(struct tcp_congestion_ops, release))
202 return &bpf_sk_setsockopt_proto;
203 return NULL;
204 case BPF_FUNC_getsockopt:
205
206
207
208
209
210
211 if (prog_ops_moff(prog) !=
212 offsetof(struct tcp_congestion_ops, release))
213 return &bpf_sk_getsockopt_proto;
214 return NULL;
215 default:
216 return bpf_base_func_proto(func_id);
217 }
218}
219
220BTF_SET_START(bpf_tcp_ca_kfunc_ids)
221BTF_ID(func, tcp_reno_ssthresh)
222BTF_ID(func, tcp_reno_cong_avoid)
223BTF_ID(func, tcp_reno_undo_cwnd)
224BTF_ID(func, tcp_slow_start)
225BTF_ID(func, tcp_cong_avoid_ai)
226#ifdef CONFIG_X86
227#ifdef CONFIG_DYNAMIC_FTRACE
228#if IS_BUILTIN(CONFIG_TCP_CONG_CUBIC)
229BTF_ID(func, cubictcp_init)
230BTF_ID(func, cubictcp_recalc_ssthresh)
231BTF_ID(func, cubictcp_cong_avoid)
232BTF_ID(func, cubictcp_state)
233BTF_ID(func, cubictcp_cwnd_event)
234BTF_ID(func, cubictcp_acked)
235#endif
236#if IS_BUILTIN(CONFIG_TCP_CONG_DCTCP)
237BTF_ID(func, dctcp_init)
238BTF_ID(func, dctcp_update_alpha)
239BTF_ID(func, dctcp_cwnd_event)
240BTF_ID(func, dctcp_ssthresh)
241BTF_ID(func, dctcp_cwnd_undo)
242BTF_ID(func, dctcp_state)
243#endif
244#if IS_BUILTIN(CONFIG_TCP_CONG_BBR)
245BTF_ID(func, bbr_init)
246BTF_ID(func, bbr_main)
247BTF_ID(func, bbr_sndbuf_expand)
248BTF_ID(func, bbr_undo_cwnd)
249BTF_ID(func, bbr_cwnd_event)
250BTF_ID(func, bbr_ssthresh)
251BTF_ID(func, bbr_min_tso_segs)
252BTF_ID(func, bbr_set_state)
253#endif
254#endif
255#endif
256BTF_SET_END(bpf_tcp_ca_kfunc_ids)
257
258static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id)
259{
260 return btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id);
261}
262
263static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
264 .get_func_proto = bpf_tcp_ca_get_func_proto,
265 .is_valid_access = bpf_tcp_ca_is_valid_access,
266 .btf_struct_access = bpf_tcp_ca_btf_struct_access,
267 .check_kfunc_call = bpf_tcp_ca_check_kfunc_call,
268};
269
270static int bpf_tcp_ca_init_member(const struct btf_type *t,
271 const struct btf_member *member,
272 void *kdata, const void *udata)
273{
274 const struct tcp_congestion_ops *utcp_ca;
275 struct tcp_congestion_ops *tcp_ca;
276 int prog_fd;
277 u32 moff;
278
279 utcp_ca = (const struct tcp_congestion_ops *)udata;
280 tcp_ca = (struct tcp_congestion_ops *)kdata;
281
282 moff = btf_member_bit_offset(t, member) / 8;
283 switch (moff) {
284 case offsetof(struct tcp_congestion_ops, flags):
285 if (utcp_ca->flags & ~TCP_CONG_MASK)
286 return -EINVAL;
287 tcp_ca->flags = utcp_ca->flags;
288 return 1;
289 case offsetof(struct tcp_congestion_ops, name):
290 if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
291 sizeof(tcp_ca->name)) <= 0)
292 return -EINVAL;
293 if (tcp_ca_find(utcp_ca->name))
294 return -EEXIST;
295 return 1;
296 }
297
298 if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
299 return 0;
300
301
302 prog_fd = (int)(*(unsigned long *)(udata + moff));
303 if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
304 return -EINVAL;
305
306 return 0;
307}
308
309static int bpf_tcp_ca_check_member(const struct btf_type *t,
310 const struct btf_member *member)
311{
312 if (is_unsupported(btf_member_bit_offset(t, member) / 8))
313 return -ENOTSUPP;
314 return 0;
315}
316
317static int bpf_tcp_ca_reg(void *kdata)
318{
319 return tcp_register_congestion_control(kdata);
320}
321
322static void bpf_tcp_ca_unreg(void *kdata)
323{
324 tcp_unregister_congestion_control(kdata);
325}
326
327struct bpf_struct_ops bpf_tcp_congestion_ops = {
328 .verifier_ops = &bpf_tcp_ca_verifier_ops,
329 .reg = bpf_tcp_ca_reg,
330 .unreg = bpf_tcp_ca_unreg,
331 .check_member = bpf_tcp_ca_check_member,
332 .init_member = bpf_tcp_ca_init_member,
333 .init = bpf_tcp_ca_init,
334 .name = "tcp_congestion_ops",
335};
336