[RFC PATCH bpf-next RESEND 10/16] bpf/crib: Add struct sk_buff related CRIB kfuncs
From: Juntong Deng <hidden>
Date: 2024-07-11 11:23:56
Also in:
bpf, lkml
Subsystem:
bpf [general] (safe dynamic programs and tools), the rest · Maintainers:
Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Linus Torvalds
This patch adds struct sk_buff related CRIB kfuncs. bpf_skb_peek_tail() is used to get a pointer to the skb at the tail of the socket queue. bpf_cal_skb_size() is used to calculate the overall size of the skb data (starting from the head). bpf_skb_acquire()/bpf_skb_release() are used to acquire/release references on struct sk_buff. Signed-off-by: Juntong Deng <redacted> --- kernel/bpf/crib/bpf_checkpoint.c | 14 +++++++++ kernel/bpf/crib/bpf_crib.c | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+)
diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c
index 4d48f08324ef..d8cd4a1b73dc 100644
--- a/kernel/bpf/crib/bpf_checkpoint.c
+++ b/kernel/bpf/crib/bpf_checkpoint.c@@ -10,6 +10,7 @@ #include <linux/fdtable.h> #include <net/inet_common.h> #include <net/ipv6.h> +#include <linux/skbuff.h> extern void bpf_file_release(struct file *file);
@@ -148,4 +149,17 @@ __bpf_kfunc int bpf_inet6_dst_addr_from_socket(struct socket *sock, struct socka return inet6_getname(sock, (struct sockaddr *)addr, 1); } +/** + * bpf_cal_skb_size() - Calculate the overall size of the data of specified skb + * (starting from the head) + * + * @skb: specified skb + * + * @returns the overall size of the data + */ +__bpf_kfunc int bpf_cal_skb_size(struct sk_buff *skb) +{ + return skb_end_offset(skb) + skb->data_len; +} + __bpf_kfunc_end_defs();
diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c
index e33fa37f8f72..21889efa620c 100644
--- a/kernel/bpf/crib/bpf_crib.c
+++ b/kernel/bpf/crib/bpf_crib.c@@ -13,6 +13,8 @@ #include <linux/net.h> #include <linux/udp.h> #include <linux/tcp.h> +#include <linux/skbuff.h> +#include <linux/spinlock.h> __bpf_kfunc_start_defs();
@@ -209,6 +211,49 @@ __bpf_kfunc struct sk_buff_head *bpf_reader_queue_from_udp_sock(struct udp_sock return &up->reader_queue; } +/** + * bpf_skb_acquire() - Acquire a reference to struct sk_buff + * + * @skb: struct sk_buff that needs to acquire a reference + * + * @returns struct sk_buff that has acquired the reference + */ +__bpf_kfunc struct sk_buff *bpf_skb_acquire(struct sk_buff *skb) +{ + return skb_get(skb); +} + +/** + * bpf_skb_release() - Release the reference acquired on struct sk_buff + * + * @skb: struct sk_buff that has acquired the reference + */ +__bpf_kfunc void bpf_skb_release(struct sk_buff *skb) +{ + consume_skb(skb); +} + +/** + * bpf_skb_peek_tail() - peek at the tail of socket queue (sk_buff_head) + * + * Note that this function acquires a reference to struct sk_buff. + * + * @head: socket queue + * + * @returns pointer to the tail skb (sk_buff) + */ +__bpf_kfunc struct sk_buff *bpf_skb_peek_tail(struct sk_buff_head *head) +{ + struct sk_buff *skb; + unsigned long flags; + + spin_lock_irqsave(&head->lock, flags); + skb = skb_peek_tail(head); + spin_unlock_irqrestore(&head->lock, flags); + + return bpf_skb_acquire(skb); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_crib_kfuncs)
@@ -239,6 +284,11 @@ BTF_ID_FLAGS(func, bpf_inet_dst_addr_from_socket, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_inet6_src_addr_from_socket, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_inet6_dst_addr_from_socket, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_cal_skb_size, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_peek_tail, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog,
--
2.39.2