Skip to content

Commit 0165cc8

Browse files
LorenzoBianconiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: introduce bpf_xdp_get_buff_len helper
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 <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: Lorenzo Bianconi <[email protected]> Link: https://lore.kernel.org/r/aac9ac3504c84026cf66a3c71b7c5ae89bc991be.1642758637.git.lorenzo@kernel.org Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent e121d27 commit 0165cc8

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

include/net/xdp.h

+14
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ xdp_get_shared_info_from_buff(struct xdp_buff *xdp)
145145
return (struct skb_shared_info *)xdp_data_hard_end(xdp);
146146
}
147147

148+
static __always_inline unsigned int xdp_get_buff_len(struct xdp_buff *xdp)
149+
{
150+
unsigned int len = xdp->data_end - xdp->data;
151+
struct skb_shared_info *sinfo;
152+
153+
if (likely(!xdp_buff_has_frags(xdp)))
154+
goto out;
155+
156+
sinfo = xdp_get_shared_info_from_buff(xdp);
157+
len += sinfo->xdp_frags_size;
158+
out:
159+
return len;
160+
}
161+
148162
struct xdp_frame {
149163
void *data;
150164
u16 len;

include/uapi/linux/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -5054,6 +5054,12 @@ union bpf_attr {
50545054
* This helper is currently supported by cgroup programs only.
50555055
* Return
50565056
* 0 on success, or a negative error in case of failure.
5057+
*
5058+
* u64 bpf_xdp_get_buff_len(struct xdp_buff *xdp_md)
5059+
* Description
5060+
* Get the total size of a given xdp buff (linear and paged area)
5061+
* Return
5062+
* The total size of a given xdp buffer.
50575063
*/
50585064
#define __BPF_FUNC_MAPPER(FN) \
50595065
FN(unspec), \
@@ -5244,6 +5250,7 @@ union bpf_attr {
52445250
FN(get_func_arg_cnt), \
52455251
FN(get_retval), \
52465252
FN(set_retval), \
5253+
FN(xdp_get_buff_len), \
52475254
/* */
52485255

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

net/core/filter.c

+15
Original file line numberDiff line numberDiff line change
@@ -3783,6 +3783,19 @@ static const struct bpf_func_proto sk_skb_change_head_proto = {
37833783
.arg2_type = ARG_ANYTHING,
37843784
.arg3_type = ARG_ANYTHING,
37853785
};
3786+
3787+
BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3788+
{
3789+
return xdp_get_buff_len(xdp);
3790+
}
3791+
3792+
static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3793+
.func = bpf_xdp_get_buff_len,
3794+
.gpl_only = false,
3795+
.ret_type = RET_INTEGER,
3796+
.arg1_type = ARG_PTR_TO_CTX,
3797+
};
3798+
37863799
static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
37873800
{
37883801
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)
75337546
return &bpf_xdp_redirect_map_proto;
75347547
case BPF_FUNC_xdp_adjust_tail:
75357548
return &bpf_xdp_adjust_tail_proto;
7549+
case BPF_FUNC_xdp_get_buff_len:
7550+
return &bpf_xdp_get_buff_len_proto;
75367551
case BPF_FUNC_fib_lookup:
75377552
return &bpf_xdp_fib_lookup_proto;
75387553
case BPF_FUNC_check_mtu:

tools/include/uapi/linux/bpf.h

+7
Original file line numberDiff line numberDiff line change
@@ -5054,6 +5054,12 @@ union bpf_attr {
50545054
* This helper is currently supported by cgroup programs only.
50555055
* Return
50565056
* 0 on success, or a negative error in case of failure.
5057+
*
5058+
* u64 bpf_xdp_get_buff_len(struct xdp_buff *xdp_md)
5059+
* Description
5060+
* Get the total size of a given xdp buff (linear and paged area)
5061+
* Return
5062+
* The total size of a given xdp buffer.
50575063
*/
50585064
#define __BPF_FUNC_MAPPER(FN) \
50595065
FN(unspec), \
@@ -5244,6 +5250,7 @@ union bpf_attr {
52445250
FN(get_func_arg_cnt), \
52455251
FN(get_retval), \
52465252
FN(set_retval), \
5253+
FN(xdp_get_buff_len), \
52475254
/* */
52485255

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

0 commit comments

Comments
 (0)