1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2019 Facebook */
4 #include <linux/types.h>
5 #include <linux/bpf_verifier.h>
8 #include <linux/btf_ids.h>
9 #include <linux/filter.h>
11 #include <net/bpf_sk_storage.h>
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;
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),
28 static u32 unsupported_ops[] = {
29 offsetof(struct tcp_congestion_ops, get_info),
32 static const struct btf_type *tcp_sock_type;
33 static u32 tcp_sock_id, sock_id;
35 static int bpf_tcp_ca_init(struct btf *btf)
39 type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
44 type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
47 tcp_sock_id = type_id;
48 tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
53 static bool is_optional(u32 member_offset)
57 for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
58 if (member_offset == optional_ops[i])
65 static bool is_unsupported(u32 member_offset)
69 for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
70 if (member_offset == unsupported_ops[i])
77 extern struct btf *btf_vmlinux;
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)
84 if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
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;
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,
102 if (atype == BPF_READ)
103 return btf_struct_access(log, btf, t, off, size, atype, next_btf_id);
105 if (t != tcp_sock_type) {
106 bpf_log(log, "only read is supported\n");
111 case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
112 end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
114 case offsetof(struct inet_connection_sock, icsk_ack.pending):
115 end = offsetofend(struct inet_connection_sock,
118 case offsetof(struct tcp_sock, snd_cwnd):
119 end = offsetofend(struct tcp_sock, snd_cwnd);
121 case offsetof(struct tcp_sock, snd_cwnd_cnt):
122 end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
124 case offsetof(struct tcp_sock, snd_ssthresh):
125 end = offsetofend(struct tcp_sock, snd_ssthresh);
127 case offsetof(struct tcp_sock, ecn_flags):
128 end = offsetofend(struct tcp_sock, ecn_flags);
131 bpf_log(log, "no write support to tcp_sock at off %d\n", off);
135 if (off + size > end) {
137 "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
145 BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
147 /* bpf_tcp_ca prog cannot have NULL tp */
148 __tcp_send_ack((struct sock *)tp, rcv_nxt);
152 static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
153 .func = bpf_tcp_send_ack,
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,
162 static u32 prog_ops_moff(const struct bpf_prog *prog)
164 const struct btf_member *m;
165 const struct btf_type *t;
168 midx = prog->expected_attach_type;
169 t = bpf_tcp_congestion_ops.type;
170 m = &btf_type_member(t)[midx];
172 return __btf_member_bit_offset(t, m) / 8;
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)
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.
193 if (prog_ops_moff(prog) !=
194 offsetof(struct tcp_congestion_ops, release))
195 return &bpf_sk_setsockopt_proto;
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.
204 if (prog_ops_moff(prog) !=
205 offsetof(struct tcp_congestion_ops, release))
206 return &bpf_sk_getsockopt_proto;
208 case BPF_FUNC_ktime_get_coarse_ns:
209 return &bpf_ktime_get_coarse_ns_proto;
211 return bpf_base_func_proto(func_id);
215 BTF_SET_START(bpf_tcp_ca_kfunc_ids)
216 BTF_ID(func, tcp_reno_ssthresh)
217 BTF_ID(func, tcp_reno_cong_avoid)
218 BTF_ID(func, tcp_reno_undo_cwnd)
219 BTF_ID(func, tcp_slow_start)
220 BTF_ID(func, tcp_cong_avoid_ai)
221 BTF_SET_END(bpf_tcp_ca_kfunc_ids)
223 static bool bpf_tcp_ca_check_kfunc_call(u32 kfunc_btf_id, struct module *owner)
225 if (btf_id_set_contains(&bpf_tcp_ca_kfunc_ids, kfunc_btf_id))
227 return bpf_check_mod_kfunc_call(&bpf_tcp_ca_kfunc_list, kfunc_btf_id, owner);
230 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
231 .get_func_proto = bpf_tcp_ca_get_func_proto,
232 .is_valid_access = bpf_tcp_ca_is_valid_access,
233 .btf_struct_access = bpf_tcp_ca_btf_struct_access,
234 .check_kfunc_call = bpf_tcp_ca_check_kfunc_call,
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",