bpf: introduce bpf_xdp_get_buff_len helper
authorLorenzo Bianconi <lorenzo@kernel.org>
Fri, 21 Jan 2022 10:09:54 +0000 (11:09 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 21 Jan 2022 22:14:02 +0000 (14:14 -0800)
Introduce bpf_xdp_get_buff_len helper in order to return the xdp buffer
total size (linear and paged area)

Acked-by: Toke Hoiland-Jorgensen <toke@redhat.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
Link: https://lore.kernel.org/r/aac9ac3504c84026cf66a3c71b7c5ae89bc991be.1642758637.git.lorenzo@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
include/net/xdp.h
include/uapi/linux/bpf.h
net/core/filter.c
tools/include/uapi/linux/bpf.h

index 8463dea8b4db1dcd697bc960dc54bca2739fa570..52b593321956e4b7fc3b5275000a08e761bdbf0a 100644 (file)
@@ -145,6 +145,20 @@ xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
        return (struct skb_shared_info *)xdp_data_hard_end(xdp);
 }
 
+static __always_inline unsigned int xdp_get_buff_len(struct xdp_buff *xdp)
+{
+       unsigned int len = xdp->data_end - xdp->data;
+       struct skb_shared_info *sinfo;
+
+       if (likely(!xdp_buff_has_frags(xdp)))
+               goto out;
+
+       sinfo = xdp_get_shared_info_from_buff(xdp);
+       len += sinfo->xdp_frags_size;
+out:
+       return len;
+}
+
 struct xdp_frame {
        void *data;
        u16 len;
index 945649c67e03f8b39525f8a6f86449e045611d46..5a28772063f68ee419be7d9e65fc844cfc986b5a 100644 (file)
@@ -5054,6 +5054,12 @@ union bpf_attr {
  *             This helper is currently supported by cgroup programs only.
  *     Return
  *             0 on success, or a negative error in case of failure.
+ *
+ * u64 bpf_xdp_get_buff_len(struct xdp_buff *xdp_md)
+ *     Description
+ *             Get the total size of a given xdp buff (linear and paged area)
+ *     Return
+ *             The total size of a given xdp buffer.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5244,6 +5250,7 @@ union bpf_attr {
        FN(get_func_arg_cnt),           \
        FN(get_retval),                 \
        FN(set_retval),                 \
+       FN(xdp_get_buff_len),           \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
index f73a84c75970e658a1cf22827ec40fddf590566c..a7f03bbca4654091a58a530ecfd0923ad7a3327a 100644 (file)
@@ -3783,6 +3783,19 @@ static const struct bpf_func_proto sk_skb_change_head_proto = {
        .arg2_type      = ARG_ANYTHING,
        .arg3_type      = ARG_ANYTHING,
 };
+
+BPF_CALL_1(bpf_xdp_get_buff_len, struct  xdp_buff*, xdp)
+{
+       return xdp_get_buff_len(xdp);
+}
+
+static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
+       .func           = bpf_xdp_get_buff_len,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_PTR_TO_CTX,
+};
+
 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
 {
        return xdp_data_meta_unsupported(xdp) ? 0 :
@@ -7533,6 +7546,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_xdp_redirect_map_proto;
        case BPF_FUNC_xdp_adjust_tail:
                return &bpf_xdp_adjust_tail_proto;
+       case BPF_FUNC_xdp_get_buff_len:
+               return &bpf_xdp_get_buff_len_proto;
        case BPF_FUNC_fib_lookup:
                return &bpf_xdp_fib_lookup_proto;
        case BPF_FUNC_check_mtu:
index 945649c67e03f8b39525f8a6f86449e045611d46..5a28772063f68ee419be7d9e65fc844cfc986b5a 100644 (file)
@@ -5054,6 +5054,12 @@ union bpf_attr {
  *             This helper is currently supported by cgroup programs only.
  *     Return
  *             0 on success, or a negative error in case of failure.
+ *
+ * u64 bpf_xdp_get_buff_len(struct xdp_buff *xdp_md)
+ *     Description
+ *             Get the total size of a given xdp buff (linear and paged area)
+ *     Return
+ *             The total size of a given xdp buffer.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -5244,6 +5250,7 @@ union bpf_attr {
        FN(get_func_arg_cnt),           \
        FN(get_retval),                 \
        FN(set_retval),                 \
+       FN(xdp_get_buff_len),           \
        /* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper