1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
4 #include <linux/init.h>
5 #include <linux/types.h>
6 #include <linux/bpf_verifier.h>
9 #include <linux/btf_ids.h>
10 #include <linux/filter.h>
12 #include <net/bpf_sk_storage.h>
14 /* "extern" is to avoid sparse warning. It is only used in bpf_struct_ops.c. */
15 extern struct bpf_struct_ops bpf_tcp_congestion_ops;
17 static u32 optional_ops[] = {
18 offsetof(struct tcp_congestion_ops, init),
19 offsetof(struct tcp_congestion_ops, release),
20 offsetof(struct tcp_congestion_ops, set_state),
21 offsetof(struct tcp_congestion_ops, cwnd_event),
22 offsetof(struct tcp_congestion_ops, in_ack_event),
23 offsetof(struct tcp_congestion_ops, pkts_acked),
24 offsetof(struct tcp_congestion_ops, min_tso_segs),
25 offsetof(struct tcp_congestion_ops, sndbuf_expand),
26 offsetof(struct tcp_congestion_ops, cong_control),
29 static u32 unsupported_ops[] = {
30 offsetof(struct tcp_congestion_ops, get_info),
33 static const struct btf_type *tcp_sock_type;
34 static u32 tcp_sock_id, sock_id;
36 static int bpf_tcp_ca_init(struct btf *btf)
40 type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
45 type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
48 tcp_sock_id = type_id;
49 tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
54 static bool is_optional(u32 member_offset)
58 for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
59 if (member_offset == optional_ops[i])
66 static bool is_unsupported(u32 member_offset)
70 for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
71 if (member_offset == unsupported_ops[i])
78 extern struct btf *btf_vmlinux;
80 static bool bpf_tcp_ca_is_valid_access(int off, int size,
81 enum bpf_access_type type,
82 const struct bpf_prog *prog,
83 struct bpf_insn_access_aux *info)
85 if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
88 if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
89 /* promote it to tcp_sock */
90 info->btf_id = tcp_sock_id;
95 static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
96 const struct btf *btf,
97 const struct btf_type *t, int off,
98 int size, enum bpf_access_type atype,
100 enum bpf_type_flag *flag)
104 if (atype == BPF_READ)
105 return btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
108 if (t != tcp_sock_type) {
109 bpf_log(log, "only read is supported\n");
114 case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
115 end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
117 case offsetof(struct inet_connection_sock, icsk_ack.pending):
118 end = offsetofend(struct inet_connection_sock,
121 case offsetof(struct tcp_sock, snd_cwnd):
122 end = offsetofend(struct tcp_sock, snd_cwnd);
124 case offsetof(struct tcp_sock, snd_cwnd_cnt):
125 end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
127 case offsetof(struct tcp_sock, snd_ssthresh):
128 end = offsetofend(struct tcp_sock, snd_ssthresh);
130 case offsetof(struct tcp_sock, ecn_flags):
131 end = offsetofend(struct tcp_sock, ecn_flags);
134 bpf_log(log, "no write support to tcp_sock at off %d\n", off);
138 if (off + size > end) {
140 "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
148 BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
150 /* bpf_tcp_ca prog cannot have NULL tp */
151 __tcp_send_ack((struct sock *)tp, rcv_nxt);
155 static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
156 .func = bpf_tcp_send_ack,
158 /* In case we want to report error later */
159 .ret_type = RET_INTEGER,
160 .arg1_type = ARG_PTR_TO_BTF_ID,
161 .arg1_btf_id = &tcp_sock_id,
162 .arg2_type = ARG_ANYTHING,
165 static u32 prog_ops_moff(const struct bpf_prog *prog)
167 const struct btf_member *m;
168 const struct btf_type *t;
171 midx = prog->expected_attach_type;
172 t = bpf_tcp_congestion_ops.type;
173 m = &btf_type_member(t)[midx];
175 return __btf_member_bit_offset(t, m) / 8;
178 static const struct bpf_func_proto *
179 bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
180 const struct bpf_prog *prog)
183 case BPF_FUNC_tcp_send_ack:
184 return &bpf_tcp_send_ack_proto;
185 case BPF_FUNC_sk_storage_get:
186 return &bpf_sk_storage_get_proto;
187 case BPF_FUNC_sk_storage_delete:
188 return &bpf_sk_storage_delete_proto;
189 case BPF_FUNC_setsockopt:
190 /* Does not allow release() to call setsockopt.
191 * release() is called when the current bpf-tcp-cc
192 * is retiring. It is not allowed to call
193 * setsockopt() to make further changes which
194 * may potentially allocate new resources.
196 if (prog_ops_moff(prog) !=
197 offsetof(struct tcp_congestion_ops, release))
198 return &bpf_sk_setsockopt_proto;
200 case BPF_FUNC_getsockopt:
201 /* Since get/setsockopt is usually expected to
202 * be available together, disable getsockopt for
203 * release also to avoid usage surprise.
204 * The bpf-tcp-cc already has a more powerful way
205 * to read tcp_sock from the PTR_TO_BTF_ID.
207 if (prog_ops_moff(prog) !=
208 offsetof(struct tcp_congestion_ops, release))
209 return &bpf_sk_getsockopt_proto;
211 case BPF_FUNC_ktime_get_coarse_ns:
212 return &bpf_ktime_get_coarse_ns_proto;
214 return bpf_base_func_proto(func_id);
218 BTF_SET_START(bpf_tcp_ca_check_kfunc_ids)
219 BTF_ID(func, tcp_reno_ssthresh)
220 BTF_ID(func, tcp_reno_cong_avoid)
221 BTF_ID(func, tcp_reno_undo_cwnd)
222 BTF_ID(func, tcp_slow_start)
223 BTF_ID(func, tcp_cong_avoid_ai)
224 BTF_SET_END(bpf_tcp_ca_check_kfunc_ids)
226 static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
227 .owner = THIS_MODULE,
228 .check_set = &bpf_tcp_ca_check_kfunc_ids,
231 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
232 .get_func_proto = bpf_tcp_ca_get_func_proto,
233 .is_valid_access = bpf_tcp_ca_is_valid_access,
234 .btf_struct_access = bpf_tcp_ca_btf_struct_access,
237 static int bpf_tcp_ca_init_member(const struct btf_type *t,
238 const struct btf_member *member,
239 void *kdata, const void *udata)
241 const struct tcp_congestion_ops *utcp_ca;
242 struct tcp_congestion_ops *tcp_ca;
246 utcp_ca = (const struct tcp_congestion_ops *)udata;
247 tcp_ca = (struct tcp_congestion_ops *)kdata;
249 moff = __btf_member_bit_offset(t, member) / 8;
251 case offsetof(struct tcp_congestion_ops, flags):
252 if (utcp_ca->flags & ~TCP_CONG_MASK)
254 tcp_ca->flags = utcp_ca->flags;
256 case offsetof(struct tcp_congestion_ops, name):
257 if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
258 sizeof(tcp_ca->name)) <= 0)
260 if (tcp_ca_find(utcp_ca->name))
265 if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
268 /* Ensure bpf_prog is provided for compulsory func ptr */
269 prog_fd = (int)(*(unsigned long *)(udata + moff));
270 if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
276 static int bpf_tcp_ca_check_member(const struct btf_type *t,
277 const struct btf_member *member)
279 if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
284 static int bpf_tcp_ca_reg(void *kdata)
286 return tcp_register_congestion_control(kdata);
289 static void bpf_tcp_ca_unreg(void *kdata)
291 tcp_unregister_congestion_control(kdata);
294 struct bpf_struct_ops bpf_tcp_congestion_ops = {
295 .verifier_ops = &bpf_tcp_ca_verifier_ops,
296 .reg = bpf_tcp_ca_reg,
297 .unreg = bpf_tcp_ca_unreg,
298 .check_member = bpf_tcp_ca_check_member,
299 .init_member = bpf_tcp_ca_init_member,
300 .init = bpf_tcp_ca_init,
301 .name = "tcp_congestion_ops",
304 static int __init bpf_tcp_ca_kfunc_init(void)
306 return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
308 late_initcall(bpf_tcp_ca_kfunc_init);