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 unsupported_ops[] = {
18 offsetof(struct tcp_congestion_ops, get_info),
21 static const struct btf_type *tcp_sock_type;
22 static u32 tcp_sock_id, sock_id;
24 static int bpf_tcp_ca_init(struct btf *btf)
28 type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
33 type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
36 tcp_sock_id = type_id;
37 tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
42 static bool is_unsupported(u32 member_offset)
46 for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
47 if (member_offset == unsupported_ops[i])
54 extern struct btf *btf_vmlinux;
56 static bool bpf_tcp_ca_is_valid_access(int off, int size,
57 enum bpf_access_type type,
58 const struct bpf_prog *prog,
59 struct bpf_insn_access_aux *info)
61 if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
64 if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
65 !bpf_type_has_unsafe_modifiers(info->reg_type) &&
66 info->btf_id == sock_id)
67 /* promote it to tcp_sock */
68 info->btf_id = tcp_sock_id;
73 static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
74 const struct bpf_reg_state *reg,
77 const struct btf_type *t;
80 t = btf_type_by_id(reg->btf, reg->btf_id);
81 if (t != tcp_sock_type) {
82 bpf_log(log, "only read is supported\n");
87 case offsetof(struct sock, sk_pacing_rate):
88 end = offsetofend(struct sock, sk_pacing_rate);
90 case offsetof(struct sock, sk_pacing_status):
91 end = offsetofend(struct sock, sk_pacing_status);
93 case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
94 end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
96 case offsetof(struct inet_connection_sock, icsk_ack.pending):
97 end = offsetofend(struct inet_connection_sock,
100 case offsetof(struct tcp_sock, snd_cwnd):
101 end = offsetofend(struct tcp_sock, snd_cwnd);
103 case offsetof(struct tcp_sock, snd_cwnd_cnt):
104 end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
106 case offsetof(struct tcp_sock, snd_ssthresh):
107 end = offsetofend(struct tcp_sock, snd_ssthresh);
109 case offsetof(struct tcp_sock, ecn_flags):
110 end = offsetofend(struct tcp_sock, ecn_flags);
112 case offsetof(struct tcp_sock, app_limited):
113 end = offsetofend(struct tcp_sock, app_limited);
116 bpf_log(log, "no write support to tcp_sock at off %d\n", off);
120 if (off + size > end) {
122 "write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
130 BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
132 /* bpf_tcp_ca prog cannot have NULL tp */
133 __tcp_send_ack((struct sock *)tp, rcv_nxt);
137 static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
138 .func = bpf_tcp_send_ack,
140 /* In case we want to report error later */
141 .ret_type = RET_INTEGER,
142 .arg1_type = ARG_PTR_TO_BTF_ID,
143 .arg1_btf_id = &tcp_sock_id,
144 .arg2_type = ARG_ANYTHING,
147 static u32 prog_ops_moff(const struct bpf_prog *prog)
149 const struct btf_member *m;
150 const struct btf_type *t;
153 midx = prog->expected_attach_type;
154 t = bpf_tcp_congestion_ops.type;
155 m = &btf_type_member(t)[midx];
157 return __btf_member_bit_offset(t, m) / 8;
160 static const struct bpf_func_proto *
161 bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
162 const struct bpf_prog *prog)
165 case BPF_FUNC_tcp_send_ack:
166 return &bpf_tcp_send_ack_proto;
167 case BPF_FUNC_sk_storage_get:
168 return &bpf_sk_storage_get_proto;
169 case BPF_FUNC_sk_storage_delete:
170 return &bpf_sk_storage_delete_proto;
171 case BPF_FUNC_setsockopt:
172 /* Does not allow release() to call setsockopt.
173 * release() is called when the current bpf-tcp-cc
174 * is retiring. It is not allowed to call
175 * setsockopt() to make further changes which
176 * may potentially allocate new resources.
178 if (prog_ops_moff(prog) !=
179 offsetof(struct tcp_congestion_ops, release))
180 return &bpf_sk_setsockopt_proto;
182 case BPF_FUNC_getsockopt:
183 /* Since get/setsockopt is usually expected to
184 * be available together, disable getsockopt for
185 * release also to avoid usage surprise.
186 * The bpf-tcp-cc already has a more powerful way
187 * to read tcp_sock from the PTR_TO_BTF_ID.
189 if (prog_ops_moff(prog) !=
190 offsetof(struct tcp_congestion_ops, release))
191 return &bpf_sk_getsockopt_proto;
193 case BPF_FUNC_ktime_get_coarse_ns:
194 return &bpf_ktime_get_coarse_ns_proto;
196 return bpf_base_func_proto(func_id);
200 BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
201 BTF_ID_FLAGS(func, tcp_reno_ssthresh)
202 BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
203 BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
204 BTF_ID_FLAGS(func, tcp_slow_start)
205 BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
206 BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
208 static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
209 .owner = THIS_MODULE,
210 .set = &bpf_tcp_ca_check_kfunc_ids,
213 static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
214 .get_func_proto = bpf_tcp_ca_get_func_proto,
215 .is_valid_access = bpf_tcp_ca_is_valid_access,
216 .btf_struct_access = bpf_tcp_ca_btf_struct_access,
219 static int bpf_tcp_ca_init_member(const struct btf_type *t,
220 const struct btf_member *member,
221 void *kdata, const void *udata)
223 const struct tcp_congestion_ops *utcp_ca;
224 struct tcp_congestion_ops *tcp_ca;
227 utcp_ca = (const struct tcp_congestion_ops *)udata;
228 tcp_ca = (struct tcp_congestion_ops *)kdata;
230 moff = __btf_member_bit_offset(t, member) / 8;
232 case offsetof(struct tcp_congestion_ops, flags):
233 if (utcp_ca->flags & ~TCP_CONG_MASK)
235 tcp_ca->flags = utcp_ca->flags;
237 case offsetof(struct tcp_congestion_ops, name):
238 if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
239 sizeof(tcp_ca->name)) <= 0)
247 static int bpf_tcp_ca_check_member(const struct btf_type *t,
248 const struct btf_member *member,
249 const struct bpf_prog *prog)
251 if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
256 static int bpf_tcp_ca_reg(void *kdata)
258 return tcp_register_congestion_control(kdata);
261 static void bpf_tcp_ca_unreg(void *kdata)
263 tcp_unregister_congestion_control(kdata);
266 static int bpf_tcp_ca_update(void *kdata, void *old_kdata)
268 return tcp_update_congestion_control(kdata, old_kdata);
271 static int bpf_tcp_ca_validate(void *kdata)
273 return tcp_validate_congestion_control(kdata);
276 struct bpf_struct_ops bpf_tcp_congestion_ops = {
277 .verifier_ops = &bpf_tcp_ca_verifier_ops,
278 .reg = bpf_tcp_ca_reg,
279 .unreg = bpf_tcp_ca_unreg,
280 .update = bpf_tcp_ca_update,
281 .check_member = bpf_tcp_ca_check_member,
282 .init_member = bpf_tcp_ca_init_member,
283 .init = bpf_tcp_ca_init,
284 .validate = bpf_tcp_ca_validate,
285 .name = "tcp_congestion_ops",
288 static int __init bpf_tcp_ca_kfunc_init(void)
290 return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
292 late_initcall(bpf_tcp_ca_kfunc_init);