Skip to content

Commit

Permalink
bpf: introduce bpf_xdp_get_buff_len helper
Browse files Browse the repository at this point in the history
Introduce bpf_xdp_get_buff_len helper in order to return the xdp buffer
total size (linear and paged area)

Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
  • Loading branch information
LorenzoBianconi authored and intel-lab-lkp committed Aug 13, 2021
1 parent c386279 commit cc503b9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/uapi/linux/bpf.h
Expand Up @@ -4847,6 +4847,12 @@ union bpf_attr {
* Get address of the traced function (for tracing and kprobe programs).
* Return
* Address of the traced function.
*
* 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), \
Expand Down Expand Up @@ -5023,6 +5029,7 @@ union bpf_attr {
FN(timer_start), \
FN(timer_cancel), \
FN(get_func_ip), \
FN(xdp_get_buff_len), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down
23 changes: 23 additions & 0 deletions net/core/filter.c
Expand Up @@ -3784,6 +3784,27 @@ 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)
{
u64 len = xdp->data_end - xdp->data;

if (unlikely(xdp_buff_is_mb(xdp))) {
struct skb_shared_info *sinfo;

sinfo = xdp_get_shared_info_from_buff(xdp);
len += sinfo->xdp_frags_size;
}
return len;
}

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 :
Expand Down Expand Up @@ -7501,6 +7522,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:
Expand Down
7 changes: 7 additions & 0 deletions tools/include/uapi/linux/bpf.h
Expand Up @@ -4847,6 +4847,12 @@ union bpf_attr {
* Get address of the traced function (for tracing and kprobe programs).
* Return
* Address of the traced function.
*
* 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), \
Expand Down Expand Up @@ -5023,6 +5029,7 @@ union bpf_attr {
FN(timer_start), \
FN(timer_cancel), \
FN(get_func_ip), \
FN(xdp_get_buff_len), \
/* */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
Expand Down

0 comments on commit cc503b9

Please sign in to comment.