xref: /openbmc/linux/net/ipv4/bpf_tcp_ca.c (revision fe7498ef)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook  */
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 /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
14 extern struct bpf_struct_ops bpf_tcp_congestion_ops;
15 
16 static 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 
28 static u32 unsupported_ops[] = {
29 	offsetof(struct tcp_congestion_ops, get_info),
30 };
31 
32 static const struct btf_type *tcp_sock_type;
33 static u32 tcp_sock_id, sock_id;
34 
35 static 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 
53 static 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 
65 static 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 
77 extern struct btf *btf_vmlinux;
78 
79 static 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 (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
85 		return false;
86 
87 	if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
88 		/* promote it to tcp_sock */
89 		info->btf_id = tcp_sock_id;
90 
91 	return true;
92 }
93 
94 static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
95 					const struct btf *btf,
96 					const struct btf_type *t, int off,
97 					int size, enum bpf_access_type atype,
98 					u32 *next_btf_id)
99 {
100 	size_t end;
101 
102 	if (atype == BPF_READ)
103 		return btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
104 
105 	if (t != tcp_sock_type) {
106 		bpf_log(log, "only read is supported\n");
107 		return -EACCES;
108 	}
109 
110 	switch (off) {
111 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
112 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
113 		break;
114 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
115 		end = offsetofend(struct inet_connection_sock,
116 				  icsk_ack.pending);
117 		break;
118 	case offsetof(struct tcp_sock, snd_cwnd):
119 		end = offsetofend(struct tcp_sock, snd_cwnd);
120 		break;
121 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
122 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
123 		break;
124 	case offsetof(struct tcp_sock, snd_ssthresh):
125 		end = offsetofend(struct tcp_sock, snd_ssthresh);
126 		break;
127 	case offsetof(struct tcp_sock, ecn_flags):
128 		end = offsetofend(struct tcp_sock, ecn_flags);
129 		break;
130 	default:
131 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
132 		return -EACCES;
133 	}
134 
135 	if (off + size > end) {
136 		bpf_log(log,
137 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
138 			off, size, end);
139 		return -EACCES;
140 	}
141 
142 	return NOT_INIT;
143 }
144 
145 BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
146 {
147 	/* bpf_tcp_ca prog cannot have NULL tp */
148 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
149 	return 0;
150 }
151 
152 static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
153 	.func		= bpf_tcp_send_ack,
154 	.gpl_only	= false,
155 	/* In case we want to report error later */
156 	.ret_type	= RET_INTEGER,
157 	.arg1_type	= ARG_PTR_TO_BTF_ID,
158 	.arg1_btf_id	= &tcp_sock_id,
159 	.arg2_type	= ARG_ANYTHING,
160 };
161 
162 static u32 prog_ops_moff(const struct bpf_prog *prog)
163 {
164 	const struct btf_member *m;
165 	const struct btf_type *t;
166 	u32 midx;
167 
168 	midx = prog->expected_attach_type;
169 	t = bpf_tcp_congestion_ops.type;
170 	m = &btf_type_member(t)[midx];
171 
172 	return btf_member_bit_offset(t, m) / 8;
173 }
174 
175 static const struct bpf_func_proto *
176 bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
177 			  const struct bpf_prog *prog)
178 {
179 	switch (func_id) {
180 	case BPF_FUNC_tcp_send_ack:
181 		return &bpf_tcp_send_ack_proto;
182 	case BPF_FUNC_sk_storage_get:
183 		return &bpf_sk_storage_get_proto;
184 	case BPF_FUNC_sk_storage_delete:
185 		return &bpf_sk_storage_delete_proto;
186 	case BPF_FUNC_setsockopt:
187 		/* Does not allow release() to call setsockopt.
188 		 * release() is called when the current bpf-tcp-cc
189 		 * is retiring.  It is not allowed to call
190 		 * setsockopt() to make further changes which
191 		 * may potentially allocate new resources.
192 		 */
193 		if (prog_ops_moff(prog) !=
194 		    offsetof(struct tcp_congestion_ops, release))
195 			return &bpf_sk_setsockopt_proto;
196 		return NULL;
197 	case BPF_FUNC_getsockopt:
198 		/* Since get/setsockopt is usually expected to
199 		 * be available together, disable getsockopt for
200 		 * release also to avoid usage surprise.
201 		 * The bpf-tcp-cc already has a more powerful way
202 		 * to read tcp_sock from the PTR_TO_BTF_ID.
203 		 */
204 		if (prog_ops_moff(prog) !=
205 		    offsetof(struct tcp_congestion_ops, release))
206 			return &bpf_sk_getsockopt_proto;
207 		return NULL;
208 	default:
209 		return bpf_base_func_proto(func_id);
210 	}
211 }
212 
213 BTF_SET_START(bpf_tcp_ca_kfunc_ids)
214 BTF_ID(func, tcp_reno_ssthresh)
215 BTF_ID(func, tcp_reno_cong_avoid)
216 BTF_ID(func, tcp_reno_undo_cwnd)
217 BTF_ID(func, tcp_slow_start)
218 BTF_ID(func, tcp_cong_avoid_ai)
219 BTF_SET_END(bpf_tcp_ca_kfunc_ids)
220 
221 static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id, struct module *owner)
222 {
223 	if (btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id))
224 		return true;
225 	return bpf_check_mod_kfunc_call(&bpf_tcp_ca_kfunc_list, kfunc_btf_id, owner);
226 }
227 
228 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
229 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
230 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
231 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
232 	.check_kfunc_call	= bpf_tcp_ca_check_kfunc_call,
233 };
234 
235 static int bpf_tcp_ca_init_member(const struct btf_type *t,
236 				  const struct btf_member *member,
237 				  void *kdata, const void *udata)
238 {
239 	const struct tcp_congestion_ops *utcp_ca;
240 	struct tcp_congestion_ops *tcp_ca;
241 	int prog_fd;
242 	u32 moff;
243 
244 	utcp_ca = (const struct tcp_congestion_ops *)udata;
245 	tcp_ca = (struct tcp_congestion_ops *)kdata;
246 
247 	moff = btf_member_bit_offset(t, member) / 8;
248 	switch (moff) {
249 	case offsetof(struct tcp_congestion_ops, flags):
250 		if (utcp_ca->flags & ~TCP_CONG_MASK)
251 			return -EINVAL;
252 		tcp_ca->flags = utcp_ca->flags;
253 		return 1;
254 	case offsetof(struct tcp_congestion_ops, name):
255 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
256 				     sizeof(tcp_ca->name)) <= 0)
257 			return -EINVAL;
258 		if (tcp_ca_find(utcp_ca->name))
259 			return -EEXIST;
260 		return 1;
261 	}
262 
263 	if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
264 		return 0;
265 
266 	/* Ensure bpf_prog is provided for compulsory func ptr */
267 	prog_fd = (int)(*(unsigned long *)(udata + moff));
268 	if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
269 		return -EINVAL;
270 
271 	return 0;
272 }
273 
274 static int bpf_tcp_ca_check_member(const struct btf_type *t,
275 				   const struct btf_member *member)
276 {
277 	if (is_unsupported(btf_member_bit_offset(t, member) / 8))
278 		return -ENOTSUPP;
279 	return 0;
280 }
281 
282 static int bpf_tcp_ca_reg(void *kdata)
283 {
284 	return tcp_register_congestion_control(kdata);
285 }
286 
287 static void bpf_tcp_ca_unreg(void *kdata)
288 {
289 	tcp_unregister_congestion_control(kdata);
290 }
291 
292 struct bpf_struct_ops bpf_tcp_congestion_ops = {
293 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
294 	.reg = bpf_tcp_ca_reg,
295 	.unreg = bpf_tcp_ca_unreg,
296 	.check_member = bpf_tcp_ca_check_member,
297 	.init_member = bpf_tcp_ca_init_member,
298 	.init = bpf_tcp_ca_init,
299 	.name = "tcp_congestion_ops",
300 };
301