xref: /linux/net/ipv4/bpf_tcp_ca.c (revision 0325cbd2)
10baf26b0SMartin KaFai Lau // SPDX-License-Identifier: GPL-2.0
20baf26b0SMartin KaFai Lau /* Copyright (c) 2019 Facebook  */
30baf26b0SMartin KaFai Lau 
4b202d844SKumar Kartikeya Dwivedi #include <linux/init.h>
50baf26b0SMartin KaFai Lau #include <linux/types.h>
60baf26b0SMartin KaFai Lau #include <linux/bpf_verifier.h>
70baf26b0SMartin KaFai Lau #include <linux/bpf.h>
80baf26b0SMartin KaFai Lau #include <linux/btf.h>
9e78aea8bSMartin KaFai Lau #include <linux/btf_ids.h>
100baf26b0SMartin KaFai Lau #include <linux/filter.h>
110baf26b0SMartin KaFai Lau #include <net/tcp.h>
12ab14fd4eSMartin KaFai Lau #include <net/bpf_sk_storage.h>
130baf26b0SMartin KaFai Lau 
14eb18b49eSMartin KaFai Lau /* "extern" is to avoid sparse warning.  It is only used in bpf_struct_ops.c. */
15f6be98d1SKui-Feng Lee static struct bpf_struct_ops bpf_tcp_congestion_ops;
16eb18b49eSMartin KaFai Lau 
170baf26b0SMartin KaFai Lau static u32 unsupported_ops[] = {
180baf26b0SMartin KaFai Lau 	offsetof(struct tcp_congestion_ops, get_info),
190baf26b0SMartin KaFai Lau };
200baf26b0SMartin KaFai Lau 
210baf26b0SMartin KaFai Lau static const struct btf_type *tcp_sock_type;
220baf26b0SMartin KaFai Lau static u32 tcp_sock_id, sock_id;
234c5763edSKui-Feng Lee static const struct btf_type *tcp_congestion_ops_type;
240baf26b0SMartin KaFai Lau 
bpf_tcp_ca_init(struct btf * btf)250baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init(struct btf *btf)
260baf26b0SMartin KaFai Lau {
270baf26b0SMartin KaFai Lau 	s32 type_id;
280baf26b0SMartin KaFai Lau 
290baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "sock", BTF_KIND_STRUCT);
300baf26b0SMartin KaFai Lau 	if (type_id < 0)
310baf26b0SMartin KaFai Lau 		return -EINVAL;
320baf26b0SMartin KaFai Lau 	sock_id = type_id;
330baf26b0SMartin KaFai Lau 
340baf26b0SMartin KaFai Lau 	type_id = btf_find_by_name_kind(btf, "tcp_sock", BTF_KIND_STRUCT);
350baf26b0SMartin KaFai Lau 	if (type_id < 0)
360baf26b0SMartin KaFai Lau 		return -EINVAL;
370baf26b0SMartin KaFai Lau 	tcp_sock_id = type_id;
380baf26b0SMartin KaFai Lau 	tcp_sock_type = btf_type_by_id(btf, tcp_sock_id);
390baf26b0SMartin KaFai Lau 
404c5763edSKui-Feng Lee 	type_id = btf_find_by_name_kind(btf, "tcp_congestion_ops", BTF_KIND_STRUCT);
414c5763edSKui-Feng Lee 	if (type_id < 0)
424c5763edSKui-Feng Lee 		return -EINVAL;
434c5763edSKui-Feng Lee 	tcp_congestion_ops_type = btf_type_by_id(btf, type_id);
444c5763edSKui-Feng Lee 
450baf26b0SMartin KaFai Lau 	return 0;
460baf26b0SMartin KaFai Lau }
470baf26b0SMartin KaFai Lau 
is_unsupported(u32 member_offset)480baf26b0SMartin KaFai Lau static bool is_unsupported(u32 member_offset)
490baf26b0SMartin KaFai Lau {
500baf26b0SMartin KaFai Lau 	unsigned int i;
510baf26b0SMartin KaFai Lau 
520baf26b0SMartin KaFai Lau 	for (i = 0; i < ARRAY_SIZE(unsupported_ops); i++) {
530baf26b0SMartin KaFai Lau 		if (member_offset == unsupported_ops[i])
540baf26b0SMartin KaFai Lau 			return true;
550baf26b0SMartin KaFai Lau 	}
560baf26b0SMartin KaFai Lau 
570baf26b0SMartin KaFai Lau 	return false;
580baf26b0SMartin KaFai Lau }
590baf26b0SMartin KaFai Lau 
bpf_tcp_ca_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)600baf26b0SMartin KaFai Lau static bool bpf_tcp_ca_is_valid_access(int off, int size,
610baf26b0SMartin KaFai Lau 				       enum bpf_access_type type,
620baf26b0SMartin KaFai Lau 				       const struct bpf_prog *prog,
630baf26b0SMartin KaFai Lau 				       struct bpf_insn_access_aux *info)
640baf26b0SMartin KaFai Lau {
6535346ab6SHou Tao 	if (!bpf_tracing_btf_ctx_access(off, size, type, prog, info))
660baf26b0SMartin KaFai Lau 		return false;
670baf26b0SMartin KaFai Lau 
683f00c523SDavid Vernet 	if (base_type(info->reg_type) == PTR_TO_BTF_ID &&
693f00c523SDavid Vernet 	    !bpf_type_has_unsafe_modifiers(info->reg_type) &&
703f00c523SDavid Vernet 	    info->btf_id == sock_id)
710baf26b0SMartin KaFai Lau 		/* promote it to tcp_sock */
720baf26b0SMartin KaFai Lau 		info->btf_id = tcp_sock_id;
730baf26b0SMartin KaFai Lau 
740baf26b0SMartin KaFai Lau 	return true;
750baf26b0SMartin KaFai Lau }
760baf26b0SMartin KaFai Lau 
bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)770baf26b0SMartin KaFai Lau static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,
786728aea7SKumar Kartikeya Dwivedi 					const struct bpf_reg_state *reg,
79b7e852a9SAlexei Starovoitov 					int off, int size)
800baf26b0SMartin KaFai Lau {
816728aea7SKumar Kartikeya Dwivedi 	const struct btf_type *t;
820baf26b0SMartin KaFai Lau 	size_t end;
830baf26b0SMartin KaFai Lau 
846728aea7SKumar Kartikeya Dwivedi 	t = btf_type_by_id(reg->btf, reg->btf_id);
850baf26b0SMartin KaFai Lau 	if (t != tcp_sock_type) {
860baf26b0SMartin KaFai Lau 		bpf_log(log, "only read is supported\n");
870baf26b0SMartin KaFai Lau 		return -EACCES;
880baf26b0SMartin KaFai Lau 	}
890baf26b0SMartin KaFai Lau 
900baf26b0SMartin KaFai Lau 	switch (off) {
9141c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_rate):
9241c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_rate);
9341c95dd6SJörn-Thorben Hinz 		break;
9441c95dd6SJörn-Thorben Hinz 	case offsetof(struct sock, sk_pacing_status):
9541c95dd6SJörn-Thorben Hinz 		end = offsetofend(struct sock, sk_pacing_status);
9641c95dd6SJörn-Thorben Hinz 		break;
970baf26b0SMartin KaFai Lau 	case bpf_ctx_range(struct inet_connection_sock, icsk_ca_priv):
980baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock, icsk_ca_priv);
990baf26b0SMartin KaFai Lau 		break;
1000baf26b0SMartin KaFai Lau 	case offsetof(struct inet_connection_sock, icsk_ack.pending):
1010baf26b0SMartin KaFai Lau 		end = offsetofend(struct inet_connection_sock,
1020baf26b0SMartin KaFai Lau 				  icsk_ack.pending);
1030baf26b0SMartin KaFai Lau 		break;
1040baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd):
1050baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd);
1060baf26b0SMartin KaFai Lau 		break;
1070baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_cwnd_cnt):
1080baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_cwnd_cnt);
1090baf26b0SMartin KaFai Lau 		break;
110*0325cbd2SMiao Xu 	case offsetof(struct tcp_sock, snd_cwnd_stamp):
111*0325cbd2SMiao Xu 		end = offsetofend(struct tcp_sock, snd_cwnd_stamp);
112*0325cbd2SMiao Xu 		break;
1130baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, snd_ssthresh):
1140baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, snd_ssthresh);
1150baf26b0SMartin KaFai Lau 		break;
1160baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_sock, ecn_flags):
1170baf26b0SMartin KaFai Lau 		end = offsetofend(struct tcp_sock, ecn_flags);
1180baf26b0SMartin KaFai Lau 		break;
119562dc56aSYixin Shen 	case offsetof(struct tcp_sock, app_limited):
120562dc56aSYixin Shen 		end = offsetofend(struct tcp_sock, app_limited);
121562dc56aSYixin Shen 		break;
1220baf26b0SMartin KaFai Lau 	default:
1230baf26b0SMartin KaFai Lau 		bpf_log(log, "no write support to tcp_sock at off %d\n", off);
1240baf26b0SMartin KaFai Lau 		return -EACCES;
1250baf26b0SMartin KaFai Lau 	}
1260baf26b0SMartin KaFai Lau 
1270baf26b0SMartin KaFai Lau 	if (off + size > end) {
1280baf26b0SMartin KaFai Lau 		bpf_log(log,
1290baf26b0SMartin KaFai Lau 			"write access at off %d with size %d beyond the member of tcp_sock ended at %zu\n",
1300baf26b0SMartin KaFai Lau 			off, size, end);
1310baf26b0SMartin KaFai Lau 		return -EACCES;
1320baf26b0SMartin KaFai Lau 	}
1330baf26b0SMartin KaFai Lau 
134896f07c0SDaniel Xu 	return 0;
1350baf26b0SMartin KaFai Lau }
1360baf26b0SMartin KaFai Lau 
BPF_CALL_2(bpf_tcp_send_ack,struct tcp_sock *,tp,u32,rcv_nxt)137206057feSMartin KaFai Lau BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
138206057feSMartin KaFai Lau {
139206057feSMartin KaFai Lau 	/* bpf_tcp_ca prog cannot have NULL tp */
140206057feSMartin KaFai Lau 	__tcp_send_ack((struct sock *)tp, rcv_nxt);
141206057feSMartin KaFai Lau 	return 0;
142206057feSMartin KaFai Lau }
143206057feSMartin KaFai Lau 
144206057feSMartin KaFai Lau static const struct bpf_func_proto bpf_tcp_send_ack_proto = {
145206057feSMartin KaFai Lau 	.func		= bpf_tcp_send_ack,
146206057feSMartin KaFai Lau 	.gpl_only	= false,
147206057feSMartin KaFai Lau 	/* In case we want to report error later */
148206057feSMartin KaFai Lau 	.ret_type	= RET_INTEGER,
149206057feSMartin KaFai Lau 	.arg1_type	= ARG_PTR_TO_BTF_ID,
1509436ef6eSLorenz Bauer 	.arg1_btf_id	= &tcp_sock_id,
151206057feSMartin KaFai Lau 	.arg2_type	= ARG_ANYTHING,
152206057feSMartin KaFai Lau };
153206057feSMartin KaFai Lau 
prog_ops_moff(const struct bpf_prog * prog)154eb18b49eSMartin KaFai Lau static u32 prog_ops_moff(const struct bpf_prog *prog)
155eb18b49eSMartin KaFai Lau {
156eb18b49eSMartin KaFai Lau 	const struct btf_member *m;
157eb18b49eSMartin KaFai Lau 	const struct btf_type *t;
158eb18b49eSMartin KaFai Lau 	u32 midx;
159eb18b49eSMartin KaFai Lau 
160eb18b49eSMartin KaFai Lau 	midx = prog->expected_attach_type;
1614c5763edSKui-Feng Lee 	t = tcp_congestion_ops_type;
162eb18b49eSMartin KaFai Lau 	m = &btf_type_member(t)[midx];
163eb18b49eSMartin KaFai Lau 
1648293eb99SAlexei Starovoitov 	return __btf_member_bit_offset(t, m) / 8;
165eb18b49eSMartin KaFai Lau }
166eb18b49eSMartin KaFai Lau 
1670baf26b0SMartin KaFai Lau static const struct bpf_func_proto *
bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)1680baf26b0SMartin KaFai Lau bpf_tcp_ca_get_func_proto(enum bpf_func_id func_id,
1690baf26b0SMartin KaFai Lau 			  const struct bpf_prog *prog)
1700baf26b0SMartin KaFai Lau {
171206057feSMartin KaFai Lau 	switch (func_id) {
172206057feSMartin KaFai Lau 	case BPF_FUNC_tcp_send_ack:
173206057feSMartin KaFai Lau 		return &bpf_tcp_send_ack_proto;
174ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_get:
175592a3498SMartin KaFai Lau 		return &bpf_sk_storage_get_proto;
176ab14fd4eSMartin KaFai Lau 	case BPF_FUNC_sk_storage_delete:
177592a3498SMartin KaFai Lau 		return &bpf_sk_storage_delete_proto;
178eb18b49eSMartin KaFai Lau 	case BPF_FUNC_setsockopt:
179eb18b49eSMartin KaFai Lau 		/* Does not allow release() to call setsockopt.
180eb18b49eSMartin KaFai Lau 		 * release() is called when the current bpf-tcp-cc
181eb18b49eSMartin KaFai Lau 		 * is retiring.  It is not allowed to call
182eb18b49eSMartin KaFai Lau 		 * setsockopt() to make further changes which
183eb18b49eSMartin KaFai Lau 		 * may potentially allocate new resources.
184eb18b49eSMartin KaFai Lau 		 */
185eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
186eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
187eb18b49eSMartin KaFai Lau 			return &bpf_sk_setsockopt_proto;
188eb18b49eSMartin KaFai Lau 		return NULL;
189eb18b49eSMartin KaFai Lau 	case BPF_FUNC_getsockopt:
190eb18b49eSMartin KaFai Lau 		/* Since get/setsockopt is usually expected to
191eb18b49eSMartin KaFai Lau 		 * be available together, disable getsockopt for
192eb18b49eSMartin KaFai Lau 		 * release also to avoid usage surprise.
193eb18b49eSMartin KaFai Lau 		 * The bpf-tcp-cc already has a more powerful way
194eb18b49eSMartin KaFai Lau 		 * to read tcp_sock from the PTR_TO_BTF_ID.
195eb18b49eSMartin KaFai Lau 		 */
196eb18b49eSMartin KaFai Lau 		if (prog_ops_moff(prog) !=
197eb18b49eSMartin KaFai Lau 		    offsetof(struct tcp_congestion_ops, release))
198eb18b49eSMartin KaFai Lau 			return &bpf_sk_getsockopt_proto;
199eb18b49eSMartin KaFai Lau 		return NULL;
2005e0bc308SDmitrii Banshchikov 	case BPF_FUNC_ktime_get_coarse_ns:
2015e0bc308SDmitrii Banshchikov 		return &bpf_ktime_get_coarse_ns_proto;
202206057feSMartin KaFai Lau 	default:
203bbc1d247SAndrii Nakryiko 		return bpf_base_func_proto(func_id, prog);
2040baf26b0SMartin KaFai Lau 	}
205206057feSMartin KaFai Lau }
2060baf26b0SMartin KaFai Lau 
2076f3189f3SDaniel Xu BTF_KFUNCS_START(bpf_tcp_ca_check_kfunc_ids)
208a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_ssthresh)
209a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_cong_avoid)
210a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_reno_undo_cwnd)
211a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_slow_start)
212a4703e31SKumar Kartikeya Dwivedi BTF_ID_FLAGS(func, tcp_cong_avoid_ai)
2136f3189f3SDaniel Xu BTF_KFUNCS_END(bpf_tcp_ca_check_kfunc_ids)
214e78aea8bSMartin KaFai Lau 
215b202d844SKumar Kartikeya Dwivedi static const struct btf_kfunc_id_set bpf_tcp_ca_kfunc_set = {
216b202d844SKumar Kartikeya Dwivedi 	.owner = THIS_MODULE,
217a4703e31SKumar Kartikeya Dwivedi 	.set   = &bpf_tcp_ca_check_kfunc_ids,
218b202d844SKumar Kartikeya Dwivedi };
219e78aea8bSMartin KaFai Lau 
2200baf26b0SMartin KaFai Lau static const struct bpf_verifier_ops bpf_tcp_ca_verifier_ops = {
2210baf26b0SMartin KaFai Lau 	.get_func_proto		= bpf_tcp_ca_get_func_proto,
2220baf26b0SMartin KaFai Lau 	.is_valid_access	= bpf_tcp_ca_is_valid_access,
2230baf26b0SMartin KaFai Lau 	.btf_struct_access	= bpf_tcp_ca_btf_struct_access,
2240baf26b0SMartin KaFai Lau };
2250baf26b0SMartin KaFai Lau 
bpf_tcp_ca_init_member(const struct btf_type * t,const struct btf_member * member,void * kdata,const void * udata)2260baf26b0SMartin KaFai Lau static int bpf_tcp_ca_init_member(const struct btf_type *t,
2270baf26b0SMartin KaFai Lau 				  const struct btf_member *member,
2280baf26b0SMartin KaFai Lau 				  void *kdata, const void *udata)
2290baf26b0SMartin KaFai Lau {
2300baf26b0SMartin KaFai Lau 	const struct tcp_congestion_ops *utcp_ca;
2310baf26b0SMartin KaFai Lau 	struct tcp_congestion_ops *tcp_ca;
2320baf26b0SMartin KaFai Lau 	u32 moff;
2330baf26b0SMartin KaFai Lau 
2340baf26b0SMartin KaFai Lau 	utcp_ca = (const struct tcp_congestion_ops *)udata;
2350baf26b0SMartin KaFai Lau 	tcp_ca = (struct tcp_congestion_ops *)kdata;
2360baf26b0SMartin KaFai Lau 
2378293eb99SAlexei Starovoitov 	moff = __btf_member_bit_offset(t, member) / 8;
2380baf26b0SMartin KaFai Lau 	switch (moff) {
2390baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, flags):
2400baf26b0SMartin KaFai Lau 		if (utcp_ca->flags & ~TCP_CONG_MASK)
2410baf26b0SMartin KaFai Lau 			return -EINVAL;
2420baf26b0SMartin KaFai Lau 		tcp_ca->flags = utcp_ca->flags;
2430baf26b0SMartin KaFai Lau 		return 1;
2440baf26b0SMartin KaFai Lau 	case offsetof(struct tcp_congestion_ops, name):
2458e7ae251SMartin KaFai Lau 		if (bpf_obj_name_cpy(tcp_ca->name, utcp_ca->name,
2468e7ae251SMartin KaFai Lau 				     sizeof(tcp_ca->name)) <= 0)
2470baf26b0SMartin KaFai Lau 			return -EINVAL;
2480baf26b0SMartin KaFai Lau 		return 1;
2490baf26b0SMartin KaFai Lau 	}
2500baf26b0SMartin KaFai Lau 
2510baf26b0SMartin KaFai Lau 	return 0;
2520baf26b0SMartin KaFai Lau }
2530baf26b0SMartin KaFai Lau 
bpf_tcp_ca_check_member(const struct btf_type * t,const struct btf_member * member,const struct bpf_prog * prog)2540baf26b0SMartin KaFai Lau static int bpf_tcp_ca_check_member(const struct btf_type *t,
25551a52a29SDavid Vernet 				   const struct btf_member *member,
25651a52a29SDavid Vernet 				   const struct bpf_prog *prog)
2570baf26b0SMartin KaFai Lau {
2588293eb99SAlexei Starovoitov 	if (is_unsupported(__btf_member_bit_offset(t, member) / 8))
2590baf26b0SMartin KaFai Lau 		return -ENOTSUPP;
2600baf26b0SMartin KaFai Lau 	return 0;
2610baf26b0SMartin KaFai Lau }
2620baf26b0SMartin KaFai Lau 
bpf_tcp_ca_reg(void * kdata)2630baf26b0SMartin KaFai Lau static int bpf_tcp_ca_reg(void *kdata)
2640baf26b0SMartin KaFai Lau {
2650baf26b0SMartin KaFai Lau 	return tcp_register_congestion_control(kdata);
2660baf26b0SMartin KaFai Lau }
2670baf26b0SMartin KaFai Lau 
bpf_tcp_ca_unreg(void * kdata)2680baf26b0SMartin KaFai Lau static void bpf_tcp_ca_unreg(void *kdata)
2690baf26b0SMartin KaFai Lau {
2700baf26b0SMartin KaFai Lau 	tcp_unregister_congestion_control(kdata);
2710baf26b0SMartin KaFai Lau }
2720baf26b0SMartin KaFai Lau 
bpf_tcp_ca_update(void * kdata,void * old_kdata)273aef56f2eSKui-Feng Lee static int bpf_tcp_ca_update(void *kdata, void *old_kdata)
274aef56f2eSKui-Feng Lee {
275aef56f2eSKui-Feng Lee 	return tcp_update_congestion_control(kdata, old_kdata);
276aef56f2eSKui-Feng Lee }
277aef56f2eSKui-Feng Lee 
bpf_tcp_ca_validate(void * kdata)27868b04864SKui-Feng Lee static int bpf_tcp_ca_validate(void *kdata)
27968b04864SKui-Feng Lee {
28068b04864SKui-Feng Lee 	return tcp_validate_congestion_control(kdata);
28168b04864SKui-Feng Lee }
28268b04864SKui-Feng Lee 
bpf_tcp_ca_ssthresh(struct sock * sk)2832cd3e377SPeter Zijlstra static u32 bpf_tcp_ca_ssthresh(struct sock *sk)
2842cd3e377SPeter Zijlstra {
2852cd3e377SPeter Zijlstra 	return 0;
2862cd3e377SPeter Zijlstra }
2872cd3e377SPeter Zijlstra 
bpf_tcp_ca_cong_avoid(struct sock * sk,u32 ack,u32 acked)2882cd3e377SPeter Zijlstra static void bpf_tcp_ca_cong_avoid(struct sock *sk, u32 ack, u32 acked)
2892cd3e377SPeter Zijlstra {
2902cd3e377SPeter Zijlstra }
2912cd3e377SPeter Zijlstra 
bpf_tcp_ca_set_state(struct sock * sk,u8 new_state)2922cd3e377SPeter Zijlstra static void bpf_tcp_ca_set_state(struct sock *sk, u8 new_state)
2932cd3e377SPeter Zijlstra {
2942cd3e377SPeter Zijlstra }
2952cd3e377SPeter Zijlstra 
bpf_tcp_ca_cwnd_event(struct sock * sk,enum tcp_ca_event ev)2962cd3e377SPeter Zijlstra static void bpf_tcp_ca_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
2972cd3e377SPeter Zijlstra {
2982cd3e377SPeter Zijlstra }
2992cd3e377SPeter Zijlstra 
bpf_tcp_ca_in_ack_event(struct sock * sk,u32 flags)3002cd3e377SPeter Zijlstra static void bpf_tcp_ca_in_ack_event(struct sock *sk, u32 flags)
3012cd3e377SPeter Zijlstra {
3022cd3e377SPeter Zijlstra }
3032cd3e377SPeter Zijlstra 
bpf_tcp_ca_pkts_acked(struct sock * sk,const struct ack_sample * sample)3042cd3e377SPeter Zijlstra static void bpf_tcp_ca_pkts_acked(struct sock *sk, const struct ack_sample *sample)
3052cd3e377SPeter Zijlstra {
3062cd3e377SPeter Zijlstra }
3072cd3e377SPeter Zijlstra 
bpf_tcp_ca_min_tso_segs(struct sock * sk)3082cd3e377SPeter Zijlstra static u32 bpf_tcp_ca_min_tso_segs(struct sock *sk)
3092cd3e377SPeter Zijlstra {
3102cd3e377SPeter Zijlstra 	return 0;
3112cd3e377SPeter Zijlstra }
3122cd3e377SPeter Zijlstra 
bpf_tcp_ca_cong_control(struct sock * sk,u32 ack,int flag,const struct rate_sample * rs)31357bfc760SMiao Xu static void bpf_tcp_ca_cong_control(struct sock *sk, u32 ack, int flag,
31457bfc760SMiao Xu 				    const struct rate_sample *rs)
3152cd3e377SPeter Zijlstra {
3162cd3e377SPeter Zijlstra }
3172cd3e377SPeter Zijlstra 
bpf_tcp_ca_undo_cwnd(struct sock * sk)3182cd3e377SPeter Zijlstra static u32 bpf_tcp_ca_undo_cwnd(struct sock *sk)
3192cd3e377SPeter Zijlstra {
3202cd3e377SPeter Zijlstra 	return 0;
3212cd3e377SPeter Zijlstra }
3222cd3e377SPeter Zijlstra 
bpf_tcp_ca_sndbuf_expand(struct sock * sk)3232cd3e377SPeter Zijlstra static u32 bpf_tcp_ca_sndbuf_expand(struct sock *sk)
3242cd3e377SPeter Zijlstra {
3252cd3e377SPeter Zijlstra 	return 0;
3262cd3e377SPeter Zijlstra }
3272cd3e377SPeter Zijlstra 
__bpf_tcp_ca_init(struct sock * sk)3282cd3e377SPeter Zijlstra static void __bpf_tcp_ca_init(struct sock *sk)
3292cd3e377SPeter Zijlstra {
3302cd3e377SPeter Zijlstra }
3312cd3e377SPeter Zijlstra 
__bpf_tcp_ca_release(struct sock * sk)3322cd3e377SPeter Zijlstra static void __bpf_tcp_ca_release(struct sock *sk)
3332cd3e377SPeter Zijlstra {
3342cd3e377SPeter Zijlstra }
3352cd3e377SPeter Zijlstra 
3362cd3e377SPeter Zijlstra static struct tcp_congestion_ops __bpf_ops_tcp_congestion_ops = {
3372cd3e377SPeter Zijlstra 	.ssthresh = bpf_tcp_ca_ssthresh,
3382cd3e377SPeter Zijlstra 	.cong_avoid = bpf_tcp_ca_cong_avoid,
3392cd3e377SPeter Zijlstra 	.set_state = bpf_tcp_ca_set_state,
3402cd3e377SPeter Zijlstra 	.cwnd_event = bpf_tcp_ca_cwnd_event,
3412cd3e377SPeter Zijlstra 	.in_ack_event = bpf_tcp_ca_in_ack_event,
3422cd3e377SPeter Zijlstra 	.pkts_acked = bpf_tcp_ca_pkts_acked,
3432cd3e377SPeter Zijlstra 	.min_tso_segs = bpf_tcp_ca_min_tso_segs,
3442cd3e377SPeter Zijlstra 	.cong_control = bpf_tcp_ca_cong_control,
3452cd3e377SPeter Zijlstra 	.undo_cwnd = bpf_tcp_ca_undo_cwnd,
3462cd3e377SPeter Zijlstra 	.sndbuf_expand = bpf_tcp_ca_sndbuf_expand,
3472cd3e377SPeter Zijlstra 
3482cd3e377SPeter Zijlstra 	.init = __bpf_tcp_ca_init,
3492cd3e377SPeter Zijlstra 	.release = __bpf_tcp_ca_release,
3502cd3e377SPeter Zijlstra };
3512cd3e377SPeter Zijlstra 
352f6be98d1SKui-Feng Lee static struct bpf_struct_ops bpf_tcp_congestion_ops = {
3530baf26b0SMartin KaFai Lau 	.verifier_ops = &bpf_tcp_ca_verifier_ops,
3540baf26b0SMartin KaFai Lau 	.reg = bpf_tcp_ca_reg,
3550baf26b0SMartin KaFai Lau 	.unreg = bpf_tcp_ca_unreg,
356aef56f2eSKui-Feng Lee 	.update = bpf_tcp_ca_update,
3570baf26b0SMartin KaFai Lau 	.check_member = bpf_tcp_ca_check_member,
3580baf26b0SMartin KaFai Lau 	.init_member = bpf_tcp_ca_init_member,
3590baf26b0SMartin KaFai Lau 	.init = bpf_tcp_ca_init,
36068b04864SKui-Feng Lee 	.validate = bpf_tcp_ca_validate,
3610baf26b0SMartin KaFai Lau 	.name = "tcp_congestion_ops",
3622cd3e377SPeter Zijlstra 	.cfi_stubs = &__bpf_ops_tcp_congestion_ops,
363f6be98d1SKui-Feng Lee 	.owner = THIS_MODULE,
3640baf26b0SMartin KaFai Lau };
365b202d844SKumar Kartikeya Dwivedi 
bpf_tcp_ca_kfunc_init(void)366b202d844SKumar Kartikeya Dwivedi static int __init bpf_tcp_ca_kfunc_init(void)
367b202d844SKumar Kartikeya Dwivedi {
368f6be98d1SKui-Feng Lee 	int ret;
369f6be98d1SKui-Feng Lee 
370f6be98d1SKui-Feng Lee 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &bpf_tcp_ca_kfunc_set);
371f6be98d1SKui-Feng Lee 	ret = ret ?: register_bpf_struct_ops(&bpf_tcp_congestion_ops, tcp_congestion_ops);
372f6be98d1SKui-Feng Lee 
373f6be98d1SKui-Feng Lee 	return ret;
374b202d844SKumar Kartikeya Dwivedi }
375b202d844SKumar Kartikeya Dwivedi late_initcall(bpf_tcp_ca_kfunc_init);
376