Linux Audio

Check our new training course

Loading...
v5.9
  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/filter.h>
  9#include <net/tcp.h>
 10#include <net/bpf_sk_storage.h>
 11
 12static u32 optional_ops[] = {
 13	offsetof(struct tcp_congestion_ops, init),
 14	offsetof(struct tcp_congestion_ops, release),
 15	offsetof(struct tcp_congestion_ops, set_state),
 16	offsetof(struct tcp_congestion_ops, cwnd_event),
 17	offsetof(struct tcp_congestion_ops, in_ack_event),
 18	offsetof(struct tcp_congestion_ops, pkts_acked),
 19	offsetof(struct tcp_congestion_ops, min_tso_segs),
 20	offsetof(struct tcp_congestion_ops, sndbuf_expand),
 21	offsetof(struct tcp_congestion_ops, cong_control),
 22};
 23
 24static u32 unsupported_ops[] = {
 25	offsetof(struct tcp_congestion_ops, get_info),
 26};
 27
 28static const struct btf_type *tcp_sock_type;
 29static u32 tcp_sock_id, sock_id;
 30
 31static int btf_sk_storage_get_ids[5];
 32static struct bpf_func_proto btf_sk_storage_get_proto __read_mostly;
 33
 34static int btf_sk_storage_delete_ids[5];
 35static struct bpf_func_proto btf_sk_storage_delete_proto __read_mostly;
 36
 37static void convert_sk_func_proto(struct bpf_func_proto *to, int *to_btf_ids,
 38				  const struct bpf_func_proto *from)
 39{
 40	int i;
 41
 42	*to = *from;
 43	to->btf_id = to_btf_ids;
 44	for (i = 0; i < ARRAY_SIZE(to->arg_type); i++) {
 45		if (to->arg_type[i] == ARG_PTR_TO_SOCKET) {
 46			to->arg_type[i] = ARG_PTR_TO_BTF_ID;
 47			to->btf_id[i] = tcp_sock_id;
 48		}
 49	}
 50}
 51
 52static int bpf_tcp_ca_init(struct btf *btf)
 53{
 54	s32 type_id;
 55
 56	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
 57	if (type_id < 0)
 58		return -EINVAL;
 59	sock_id = type_id;
 60
 61	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
 62	if (type_id < 0)
 63		return -EINVAL;
 64	tcp_sock_id = type_id;
 65	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 66
 67	convert_sk_func_proto(&btf_sk_storage_get_proto,
 68			      btf_sk_storage_get_ids,
 69			      &bpf_sk_storage_get_proto);
 70	convert_sk_func_proto(&btf_sk_storage_delete_proto,
 71			      btf_sk_storage_delete_ids,
 72			      &bpf_sk_storage_delete_proto);
 73
 74	return 0;
 75}
 76
 77static bool is_optional(u32 member_offset)
 78{
 79	unsigned int i;
 80
 81	for (i = 0; i < ARRAY_SIZE(optional_ops); i++) {
 82		if (member_offset == optional_ops[i])
 83			return true;
 84	}
 85
 86	return false;
 87}
 88
 89static bool is_unsupported(u32 member_offset)
 90{
 91	unsigned int i;
 92
 93	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
 94		if (member_offset == unsupported_ops[i])
 95			return true;
 96	}
 97
 98	return false;
 99}
100
101extern struct btf *btf_vmlinux;
102
103static bool bpf_tcp_ca_is_valid_access(int off, int size,
104				       enum bpf_access_type type,
105				       const struct bpf_prog *prog,
106				       struct bpf_insn_access_aux *info)
107{
108	if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
109		return false;
110	if (type != BPF_READ)
111		return false;
112	if (off % size != 0)
113		return false;
114
115	if (!btf_ctx_access(off, size, type, prog, info))
116		return false;
117
118	if (info->reg_type == PTR_TO_BTF_ID && info->btf_id == sock_id)
119		/* promote it to tcp_sock */
120		info->btf_id = tcp_sock_id;
121
122	return true;
123}
124
125static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
126					const struct btf_type *t, int off,
127					int size, enum bpf_access_type atype,
128					u32 *next_btf_id)
129{
 
130	size_t end;
131
132	if (atype == BPF_READ)
133		return btf_struct_access(log, t, off, size, atype, next_btf_id);
134
 
135	if (t != tcp_sock_type) {
136		bpf_log(log, "only read is supported\n");
137		return -EACCES;
138	}
139
140	switch (off) {
 
 
 
 
 
 
141	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
142		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
143		break;
144	case offsetof(struct inet_connection_sock, icsk_ack.pending):
145		end = offsetofend(struct inet_connection_sock,
146				  icsk_ack.pending);
147		break;
148	case offsetof(struct tcp_sock, snd_cwnd):
149		end = offsetofend(struct tcp_sock, snd_cwnd);
150		break;
151	case offsetof(struct tcp_sock, snd_cwnd_cnt):
152		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
153		break;
154	case offsetof(struct tcp_sock, snd_ssthresh):
155		end = offsetofend(struct tcp_sock, snd_ssthresh);
156		break;
157	case offsetof(struct tcp_sock, ecn_flags):
158		end = offsetofend(struct tcp_sock, ecn_flags);
159		break;
160	default:
161		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
162		return -EACCES;
163	}
164
165	if (off + size > end) {
166		bpf_log(log,
167			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
168			off, size, end);
169		return -EACCES;
170	}
171
172	return NOT_INIT;
173}
174
175BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
176{
177	/* bpf_tcp_ca prog cannot have NULL tp */
178	__tcp_send_ack((struct sock *)tp, rcv_nxt);
179	return 0;
180}
181
182static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
183	.func		= bpf_tcp_send_ack,
184	.gpl_only	= false,
185	/* In case we want to report error later */
186	.ret_type	= RET_INTEGER,
187	.arg1_type	= ARG_PTR_TO_BTF_ID,
 
188	.arg2_type	= ARG_ANYTHING,
189	.btf_id		= &tcp_sock_id,
190};
191
 
 
 
 
 
 
 
 
 
 
 
 
 
192static const struct bpf_func_proto *
193bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
194			  const struct bpf_prog *prog)
195{
196	switch (func_id) {
197	case BPF_FUNC_tcp_send_ack:
198		return &bpf_tcp_send_ack_proto;
199	case BPF_FUNC_sk_storage_get:
200		return &btf_sk_storage_get_proto;
201	case BPF_FUNC_sk_storage_delete:
202		return &btf_sk_storage_delete_proto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203	default:
204		return bpf_base_func_proto(func_id);
205	}
206}
207
 
 
 
 
 
 
 
 
 
 
 
 
 
208static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
209	.get_func_proto		= bpf_tcp_ca_get_func_proto,
210	.is_valid_access	= bpf_tcp_ca_is_valid_access,
211	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
212};
213
214static int bpf_tcp_ca_init_member(const struct btf_type *t,
215				  const struct btf_member *member,
216				  void *kdata, const void *udata)
217{
218	const struct tcp_congestion_ops *utcp_ca;
219	struct tcp_congestion_ops *tcp_ca;
220	int prog_fd;
221	u32 moff;
222
223	utcp_ca = (const struct tcp_congestion_ops *)udata;
224	tcp_ca = (struct tcp_congestion_ops *)kdata;
225
226	moff = btf_member_bit_offset(t, member) / 8;
227	switch (moff) {
228	case offsetof(struct tcp_congestion_ops, flags):
229		if (utcp_ca->flags & ~TCP_CONG_MASK)
230			return -EINVAL;
231		tcp_ca->flags = utcp_ca->flags;
232		return 1;
233	case offsetof(struct tcp_congestion_ops, name):
234		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
235				     sizeof(tcp_ca->name)) <= 0)
236			return -EINVAL;
237		if (tcp_ca_find(utcp_ca->name))
238			return -EEXIST;
239		return 1;
240	}
241
242	if (!btf_type_resolve_func_ptr(btf_vmlinux, member->type, NULL))
243		return 0;
244
245	/* Ensure bpf_prog is provided for compulsory func ptr */
246	prog_fd = (int)(*(unsigned long *)(udata + moff));
247	if (!prog_fd && !is_optional(moff) && !is_unsupported(moff))
248		return -EINVAL;
249
250	return 0;
251}
252
253static int bpf_tcp_ca_check_member(const struct btf_type *t,
254				   const struct btf_member *member)
255{
256	if (is_unsupported(btf_member_bit_offset(t, member) / 8))
257		return -ENOTSUPP;
258	return 0;
259}
260
261static int bpf_tcp_ca_reg(void *kdata)
262{
263	return tcp_register_congestion_control(kdata);
264}
265
266static void bpf_tcp_ca_unreg(void *kdata)
267{
268	tcp_unregister_congestion_control(kdata);
269}
270
271/* Avoid sparse warning.  It is only used in bpf_struct_ops.c. */
272extern struct bpf_struct_ops bpf_tcp_congestion_ops;
273
274struct bpf_struct_ops bpf_tcp_congestion_ops = {
275	.verifier_ops = &bpf_tcp_ca_verifier_ops,
276	.reg = bpf_tcp_ca_reg,
277	.unreg = bpf_tcp_ca_unreg,
278	.check_member = bpf_tcp_ca_check_member,
279	.init_member = bpf_tcp_ca_init_member,
280	.init = bpf_tcp_ca_init,
281	.name = "tcp_congestion_ops",
282};
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2019 Facebook  */
  3
  4#include <linux/init.h>
  5#include <linux/types.h>
  6#include <linux/bpf_verifier.h>
  7#include <linux/bpf.h>
  8#include <linux/btf.h>
  9#include <linux/btf_ids.h>
 10#include <linux/filter.h>
 11#include <net/tcp.h>
 12#include <net/bpf_sk_storage.h>
 13
 14/* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
 15extern struct bpf_struct_ops bpf_tcp_congestion_ops;
 
 
 
 
 
 
 
 
 
 16
 17static u32 unsupported_ops[] = {
 18	offsetof(struct tcp_congestion_ops, get_info),
 19};
 20
 21static const struct btf_type *tcp_sock_type;
 22static u32 tcp_sock_id, sock_id;
 23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 24static int bpf_tcp_ca_init(struct btf *btf)
 25{
 26	s32 type_id;
 27
 28	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
 29	if (type_id < 0)
 30		return -EINVAL;
 31	sock_id = type_id;
 32
 33	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
 34	if (type_id < 0)
 35		return -EINVAL;
 36	tcp_sock_id = type_id;
 37	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
 38
 
 
 
 
 
 
 
 39	return 0;
 40}
 41
 
 
 
 
 
 
 
 
 
 
 
 
 42static bool is_unsupported(u32 member_offset)
 43{
 44	unsigned int i;
 45
 46	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
 47		if (member_offset == unsupported_ops[i])
 48			return true;
 49	}
 50
 51	return false;
 52}
 53
 54extern struct btf *btf_vmlinux;
 55
 56static 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)
 60{
 61	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
 
 
 
 
 62		return false;
 63
 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;
 69
 70	return true;
 71}
 72
 73static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
 74					const struct bpf_reg_state *reg,
 75					int off, int size, enum bpf_access_type atype,
 76					u32 *next_btf_id, enum bpf_type_flag *flag)
 77{
 78	const struct btf_type *t;
 79	size_t end;
 80
 81	if (atype == BPF_READ)
 82		return btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
 83
 84	t = btf_type_by_id(reg->btf, reg->btf_id);
 85	if (t != tcp_sock_type) {
 86		bpf_log(log, "only read is supported\n");
 87		return -EACCES;
 88	}
 89
 90	switch (off) {
 91	case offsetof(struct sock, sk_pacing_rate):
 92		end = offsetofend(struct sock, sk_pacing_rate);
 93		break;
 94	case offsetof(struct sock, sk_pacing_status):
 95		end = offsetofend(struct sock, sk_pacing_status);
 96		break;
 97	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
 98		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
 99		break;
100	case offsetof(struct inet_connection_sock, icsk_ack.pending):
101		end = offsetofend(struct inet_connection_sock,
102				  icsk_ack.pending);
103		break;
104	case offsetof(struct tcp_sock, snd_cwnd):
105		end = offsetofend(struct tcp_sock, snd_cwnd);
106		break;
107	case offsetof(struct tcp_sock, snd_cwnd_cnt):
108		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
109		break;
110	case offsetof(struct tcp_sock, snd_ssthresh):
111		end = offsetofend(struct tcp_sock, snd_ssthresh);
112		break;
113	case offsetof(struct tcp_sock, ecn_flags):
114		end = offsetofend(struct tcp_sock, ecn_flags);
115		break;
116	default:
117		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
118		return -EACCES;
119	}
120
121	if (off + size > end) {
122		bpf_log(log,
123			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
124			off, size, end);
125		return -EACCES;
126	}
127
128	return 0;
129}
130
131BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
132{
133	/* bpf_tcp_ca prog cannot have NULL tp */
134	__tcp_send_ack((struct sock *)tp, rcv_nxt);
135	return 0;
136}
137
138static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
139	.func		= bpf_tcp_send_ack,
140	.gpl_only	= false,
141	/* In case we want to report error later */
142	.ret_type	= RET_INTEGER,
143	.arg1_type	= ARG_PTR_TO_BTF_ID,
144	.arg1_btf_id	= &tcp_sock_id,
145	.arg2_type	= ARG_ANYTHING,
 
146};
147
148static u32 prog_ops_moff(const struct bpf_prog *prog)
149{
150	const struct btf_member *m;
151	const struct btf_type *t;
152	u32 midx;
153
154	midx = prog->expected_attach_type;
155	t = bpf_tcp_congestion_ops.type;
156	m = &btf_type_member(t)[midx];
157
158	return __btf_member_bit_offset(t, m) / 8;
159}
160
161static const struct bpf_func_proto *
162bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
163			  const struct bpf_prog *prog)
164{
165	switch (func_id) {
166	case BPF_FUNC_tcp_send_ack:
167		return &bpf_tcp_send_ack_proto;
168	case BPF_FUNC_sk_storage_get:
169		return &bpf_sk_storage_get_proto;
170	case BPF_FUNC_sk_storage_delete:
171		return &bpf_sk_storage_delete_proto;
172	case BPF_FUNC_setsockopt:
173		/* Does not allow release() to call setsockopt.
174		 * release() is called when the current bpf-tcp-cc
175		 * is retiring.  It is not allowed to call
176		 * setsockopt() to make further changes which
177		 * may potentially allocate new resources.
178		 */
179		if (prog_ops_moff(prog) !=
180		    offsetof(struct tcp_congestion_ops, release))
181			return &bpf_sk_setsockopt_proto;
182		return NULL;
183	case BPF_FUNC_getsockopt:
184		/* Since get/setsockopt is usually expected to
185		 * be available together, disable getsockopt for
186		 * release also to avoid usage surprise.
187		 * The bpf-tcp-cc already has a more powerful way
188		 * to read tcp_sock from the PTR_TO_BTF_ID.
189		 */
190		if (prog_ops_moff(prog) !=
191		    offsetof(struct tcp_congestion_ops, release))
192			return &bpf_sk_getsockopt_proto;
193		return NULL;
194	case BPF_FUNC_ktime_get_coarse_ns:
195		return &bpf_ktime_get_coarse_ns_proto;
196	default:
197		return bpf_base_func_proto(func_id);
198	}
199}
200
201BTF_SET8_START(bpf_tcp_ca_check_kfunc_ids)
202BTF_ID_FLAGS(func, tcp_reno_ssthresh)
203BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
204BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
205BTF_ID_FLAGS(func, tcp_slow_start)
206BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
207BTF_SET8_END(bpf_tcp_ca_check_kfunc_ids)
208
209static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
210	.owner = THIS_MODULE,
211	.set   = &bpf_tcp_ca_check_kfunc_ids,
212};
213
214static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
215	.get_func_proto		= bpf_tcp_ca_get_func_proto,
216	.is_valid_access	= bpf_tcp_ca_is_valid_access,
217	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
218};
219
220static int bpf_tcp_ca_init_member(const struct btf_type *t,
221				  const struct btf_member *member,
222				  void *kdata, const void *udata)
223{
224	const struct tcp_congestion_ops *utcp_ca;
225	struct tcp_congestion_ops *tcp_ca;
 
226	u32 moff;
227
228	utcp_ca = (const struct tcp_congestion_ops *)udata;
229	tcp_ca = (struct tcp_congestion_ops *)kdata;
230
231	moff = __btf_member_bit_offset(t, member) / 8;
232	switch (moff) {
233	case offsetof(struct tcp_congestion_ops, flags):
234		if (utcp_ca->flags & ~TCP_CONG_MASK)
235			return -EINVAL;
236		tcp_ca->flags = utcp_ca->flags;
237		return 1;
238	case offsetof(struct tcp_congestion_ops, name):
239		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
240				     sizeof(tcp_ca->name)) <= 0)
241			return -EINVAL;
242		if (tcp_ca_find(utcp_ca->name))
243			return -EEXIST;
244		return 1;
245	}
246
 
 
 
 
 
 
 
 
247	return 0;
248}
249
250static int bpf_tcp_ca_check_member(const struct btf_type *t,
251				   const struct btf_member *member)
252{
253	if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
254		return -ENOTSUPP;
255	return 0;
256}
257
258static int bpf_tcp_ca_reg(void *kdata)
259{
260	return tcp_register_congestion_control(kdata);
261}
262
263static void bpf_tcp_ca_unreg(void *kdata)
264{
265	tcp_unregister_congestion_control(kdata);
266}
267
 
 
 
268struct bpf_struct_ops bpf_tcp_congestion_ops = {
269	.verifier_ops = &bpf_tcp_ca_verifier_ops,
270	.reg = bpf_tcp_ca_reg,
271	.unreg = bpf_tcp_ca_unreg,
272	.check_member = bpf_tcp_ca_check_member,
273	.init_member = bpf_tcp_ca_init_member,
274	.init = bpf_tcp_ca_init,
275	.name = "tcp_congestion_ops",
276};
277
278static int __init bpf_tcp_ca_kfunc_init(void)
279{
280	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
281}
282late_initcall(bpf_tcp_ca_kfunc_init);