Add kfunc bpf_xdp_pull_data
Subsystems (9)
v4 cover Add kfunc bpf_xdp_pull_data
v3 -> v4
patch 2
- Improve comments (Jakub)
- Drop new_end and len_free to simplify code (Jakub)
patch 4
- Instead of adding is_xdp to bpf_test_init, move lower-bound check of user_size to callers (Martin)
- Simplify linear data size calculation (Martin)
patch 5
- Add static function identifier (Martin)
- Free calloc-ed buf (Martin)
v2 -> v3
Separate mlx5 fixes from the patchset
patch 2
- Use headroom for pulling data by shifting metadata and data down
(Jakub)
- Drop the flags argument (Martin)
patch 4
- Support empty linear xdp data for BPF_PROG_TEST_RUN
v1 -> v2
Rebase onto bpf-next
Try to build on top of the mlx5 patchset that avoids copying payload to linear part by Christoph but got a kernel panic. Will rebase on that patchset if it got merged first, or separate the mlx5 fix from this set.
patch 1
- Remove the unnecessary head frag search (Dragos)
- Rewind the end frag pointer to simplify the change (Dragos)
- Rewind the end frag pointer and recalculate truesize only when the number of frags changed (Dragos)
patch 3
- Fix len == zero behavior. To mirror bpf_skb_pull_data() correctly, the kfunc should do nothing (Stanislav)
- Fix a pointer wrap around bug (Jakub)
- Use memmove() when moving sinfo->frags (Jakub)
---
Hi all,
This patchset introduces a new kfunc bpf_xdp_pull_data() to allow pulling nonlinear xdp data. This may be useful when a driver places headers in fragments. When an xdp program would like to keep parsing packet headers using direct packet access, it can call bpf_xdp_pull_data() to make the header available in the linear data area. The kfunc can also be used to decapsulate the header in the nonlinear data, as currently there is no easy way to do this.
Tested with the added bpf selftest using bpf test_run and also on mlx5 with the tools/testing/selftests/drivers/net/{xdp.py, ping.py}. mlx5 with striding RQ enabled always passse xdp_buff with empty linear data to xdp programs. xdp.test_xdp_native_pass_mb would fail to parse the header before this patchset.
Thanks!
Amery
Amery Hung (6):
bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail
bpf: Support pulling non-linear xdp data
bpf: Clear packet pointers after changing packet data in kfuncs
bpf: Support specifying linear xdp packet data size for
BPF_PROG_TEST_RUN
selftests/bpf: Test bpf_xdp_pull_data
selftests: drv-net: Pull data before parsing headers
include/net/xdp_sock_drv.h | 21 ++- kernel/bpf/verifier.c | 13 ++ net/bpf/test_run.c | 9 +- net/core/filter.c | 119 ++++++++++-- .../bpf/prog_tests/xdp_context_test_run.c | 4 +- .../selftests/bpf/prog_tests/xdp_pull_data.c | 176 ++++++++++++++++++ .../selftests/bpf/progs/test_xdp_pull_data.c | 48 +++++ .../selftests/net/lib/xdp_native.bpf.c | 89 +++++++-- 8 files changed, 445 insertions(+), 34 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_pull_data.c
Signature · 1 line
2.47.3
PATCH v4 1/6 bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail
Move skb_frag_t adjustment into bpf_xdp_shrink_data() and extend its functionality to be able to shrink an xdp fragment from both head and tail. In a later patch, bpf_xdp_pull_data() will reuse it to shrink an xdp fragment from head.
Additionally, in bpf_xdp_frags_shrink_tail(), breaking the loop when bpf_xdp_shrink_data() returns false (i.e., not releasing the current fragment) is not necessary as the loop condition, offset > 0, has the same effect. Remove the else branch to simplify the code.
--- include/net/xdp_sock_drv.h | 21 ++++++++++++++++++--- net/core/filter.c | 28 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-)
include/net/xdp_sock_drv.h +18 -3
@@ -160,13 +160,23 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return ret; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) { - struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp); + struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);list_del (&xskb->list_node); } +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) +{ + struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); +struct xdp_buff_xsk *frag; + + frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk, + list_node); +return &frag->xdp; +} +static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first) { struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);
@@ -389,8 +399,13 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return NULL ; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) +{ +} + +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) { +return NULL ; }static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first)
net/core/filter.c +17 -11
@@ -4153,27 +4153,31 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)return 0 ; } -static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink, +static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink,bool tail,enum xdp_mem_type mem_type,bool release) { -struct xdp_buff *zc_frag =xsk_buff_get_tail (xdp); +struct xdp_buff *zc_frag = tail ?xsk_buff_get_tail (xdp) : +xsk_buff_get_head (xdp);if (release) { -xsk_buff_del_tail (zc_frag); +xsk_buff_del_frag (zc_frag);__xdp_return (0 , mem_type, false, zc_frag); }else { - zc_frag->data_end -= shrink; +if (tail) + zc_frag->data_end -= shrink; +else + zc_frag->data += shrink; } }static bool bpf_xdp_shrink_data (struct xdp_buff *xdp,skb_frag_t *frag, -int shrink) +int shrink,bool tail) {enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink;if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { -bpf_xdp_shrink_data_zc (xdp, shrink, mem_type, release); +bpf_xdp_shrink_data_zc (xdp, shrink, tail, mem_type, release); goto out; }
@@ -4181,6 +4185,12 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); out: +if (!release) { +if (!tail) +skb_frag_off_add (frag, shrink); +skb_frag_size_sub (frag, shrink); + } +return release; }
@@ -4198,12 +4208,8 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset) len_free += shrink; offset -= shrink; -if (bpf_xdp_shrink_data (xdp, frag, shrink)) { +if (bpf_xdp_shrink_data (xdp, frag, shrink, true)) n_frags_free++; - }else { -skb_frag_size_sub (frag, shrink); -break ; - } } sinfo->nr_frags -= n_frags_free; sinfo->xdp_frags_size -= len_free;
Signature · 1 line
2.47.3
Maciej Fijalkowski
maintainerRe: [PATCH bpf-next v4 1/6] bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail
Quoted 104 lines · Amery Hung
Move
skb_frag_tadjustment intobpf_xdp_shrink_data()and extend its functionality to be able to shrink an xdp fragment from both head and tail. In a later patch,bpf_xdp_pull_data()will reuse it to shrink an xdp fragment from head.Additionally, in
bpf_xdp_frags_shrink_tail(), breaking the loop whenbpf_xdp_shrink_data()returns false (i.e., not releasing the current fragment) is not necessary as the loop condition, offset > 0, has the same effect. Remove the else branch to simplify the code.Signed-off-by Amery Hung <[email protected]>--- include/net/xdp_sock_drv.h | 21 ++++++++++++++++++--- net/core/filter.c | 28 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-)include/net/xdp_sock_drv.h +18
-3@@ -160,13 +160,23 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return ret; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) { - struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp); + struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);list_del (&xskb->list_node); } +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) +{ + struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); +struct xdp_buff_xsk *frag; + + frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk, + list_node); +return &frag->xdp; +} +static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first) { struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);@@ -389,8 +399,13 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return NULL ; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) +{ +} + +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) { +return NULL ; }static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first)net/core/filter.c +10
-6@@ -4153,27 +4153,31 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)return 0 ; } -static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink, +static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink,bool tail,enum xdp_mem_type mem_type,bool release) { -struct xdp_buff *zc_frag =xsk_buff_get_tail (xdp); +struct xdp_buff *zc_frag = tail ?xsk_buff_get_tail (xdp) : +xsk_buff_get_head (xdp);if (release) { -xsk_buff_del_tail (zc_frag); +xsk_buff_del_frag (zc_frag);__xdp_return (0 , mem_type, false, zc_frag); }else { - zc_frag->data_end -= shrink; +if (tail) + zc_frag->data_end -= shrink; +else + zc_frag->data += shrink; } }static bool bpf_xdp_shrink_data (struct xdp_buff *xdp,skb_frag_t *frag, -int shrink) +int shrink,bool tail) {enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink;if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { -bpf_xdp_shrink_data_zc (xdp, shrink, mem_type, release); +bpf_xdp_shrink_data_zc (xdp, shrink, tail, mem_type, release); goto out; }
net/core/filter.c static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, · line 4192
Quoted 8 lines
net/core/filter.c +5
-0@@ -4181,6 +4185,12 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); out: +if (!release) { +if (!tail) +skb_frag_off_add (frag, shrink); +skb_frag_size_sub (frag, shrink); + }
Hi Amery,
it feels a bit off to have separate conditions around @release. How about something below?
net/core/filter.c +4 -7
@@ -4175,20 +4175,17 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, {enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink; +bool zc = mem_type ==MEM_TYPE_XSK_BUFF_POOL ; -if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { +if (zc)bpf_xdp_shrink_data_zc (xdp, shrink, tail, mem_type, release); - goto out; - } - -if (release) -__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); -out:if (!release) {if (!tail)skb_frag_off_add (frag, shrink);skb_frag_size_sub (frag, shrink); + }else if (!zc) { +__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); }return release;
Quoted 21 lines
net/core/filter.c +2
-5+return release; }@@ -4198,12 +4208,8 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset) len_free += shrink; offset -= shrink; -if (bpf_xdp_shrink_data (xdp, frag, shrink)) { +if (bpf_xdp_shrink_data (xdp, frag, shrink, true)) n_frags_free++; - }else { -skb_frag_size_sub (frag, shrink); -break ; - } } sinfo->nr_frags -= n_frags_free; sinfo->xdp_frags_size -= len_free;Signature · 1 line
2.47.3
Re: [PATCH bpf-next v4 1/6] bpf: Allow bpf_xdp_shrink_data to shrink a frag from head and tail
Quoted 117 lines · Maciej Fijalkowski
Quoted 104 lines · Amery Hung
Move
skb_frag_tadjustment intobpf_xdp_shrink_data()and extend its functionality to be able to shrink an xdp fragment from both head and tail. In a later patch,bpf_xdp_pull_data()will reuse it to shrink an xdp fragment from head.Additionally, in
bpf_xdp_frags_shrink_tail(), breaking the loop whenbpf_xdp_shrink_data()returns false (i.e., not releasing the current fragment) is not necessary as the loop condition, offset > 0, has the same effect. Remove the else branch to simplify the code.Signed-off-by Amery Hung <[email protected]>--- include/net/xdp_sock_drv.h | 21 ++++++++++++++++++--- net/core/filter.c | 28 +++++++++++++++++----------- 2 files changed, 35 insertions(+), 14 deletions(-)include/net/xdp_sock_drv.h +18
-3@@ -160,13 +160,23 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return ret; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) { - struct xdp_buff_xsk *xskb = container_of(tail, struct xdp_buff_xsk, xdp); + struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);list_del (&xskb->list_node); } +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) +{ + struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp); +struct xdp_buff_xsk *frag; + + frag = list_first_entry(&xskb->pool->xskb_list, struct xdp_buff_xsk, + list_node); +return &frag->xdp; +} +static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first) { struct xdp_buff_xsk *xskb = container_of(first, struct xdp_buff_xsk, xdp);@@ -389,8 +399,13 @@ static inline struct xdp_buff *xsk_buff_get_frag(const struct xdp_buff *first)return NULL ; } -static inline void xsk_buff_del_tail (struct xdp_buff *tail) +static inline void xsk_buff_del_frag (struct xdp_buff *xdp) +{ +} + +static inline struct xdp_buff *xsk_buff_get_head (struct xdp_buff *first) { +return NULL ; }static inline struct xdp_buff *xsk_buff_get_tail (struct xdp_buff *first)net/core/filter.c +10
-6@@ -4153,27 +4153,31 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)return 0 ; } -static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink, +static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink,bool tail,enum xdp_mem_type mem_type,bool release) { -struct xdp_buff *zc_frag =xsk_buff_get_tail (xdp); +struct xdp_buff *zc_frag = tail ?xsk_buff_get_tail (xdp) : +xsk_buff_get_head (xdp);if (release) { -xsk_buff_del_tail (zc_frag); +xsk_buff_del_frag (zc_frag);__xdp_return (0 , mem_type, false, zc_frag); }else { - zc_frag->data_end -= shrink; +if (tail) + zc_frag->data_end -= shrink; +else + zc_frag->data += shrink; } }static bool bpf_xdp_shrink_data (struct xdp_buff *xdp,skb_frag_t *frag, -int shrink) +int shrink,bool tail) {enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink;if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { -bpf_xdp_shrink_data_zc (xdp, shrink, mem_type, release); +bpf_xdp_shrink_data_zc (xdp, shrink, tail, mem_type, release); goto out; }net/core/filter.c static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, · line 4192
Quoted 8 lines
net/core/filter.c +5
-0@@ -4181,6 +4185,12 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); out: +if (!release) { +if (!tail) +skb_frag_off_add (frag, shrink); +skb_frag_size_sub (frag, shrink); + }Hi Amery,
Quoted 3 lines
it feels a bit off to have separate conditions around @release. How about something below?
Agree that it feels weird. It bugged me a little but when working on this set...
Let me try again. I moved common things out so that bpf_xdp_shrink_data_zc() just does adjustments specific to zerocopy. Does it look better to you?
static struct xdp_buff *bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,
bool tail, bool release)
{
struct xdp_buff *zc_frag = tail ? xsk_buff_get_tail(xdp) :
xsk_buff_get_head(xdp);
if (release) {
xsk_buff_del_frag(zc_frag);
} else {
if (tail)
zc_frag->data_end -= shrink;
else
zc_frag->data += shrink;
}
return zc_frag;
}static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
int shrink, bool tail)
{
enum xdp_mem_type mem_type = xdp->rxq->mem.type;
bool release = skb_frag_size(frag) == shrink;
netmem_ref netmem = skb_frag_netmem(frag);
struct xdp_buff *zc_frag = NULL;
if (mem_type == MEM_TYPE_XSK_BUFF_POOL) {
netmem = 0;
zc_frag = bpf_xdp_shrink_data_zc(xdp, shrink, tail, release);
}
if (release) {
__xdp_return(netmem, mem_type, false, zc_frag);
} else {
if (!tail)
skb_frag_off_add(frag, shrink);
skb_frag_size_sub(frag, shrink);
}
return release;
}Quoted 52 lines
net/core/filter.c +4
-7@@ -4175,20 +4175,17 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, {enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink; +bool zc = mem_type ==MEM_TYPE_XSK_BUFF_POOL ; -if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { +if (zc)bpf_xdp_shrink_data_zc (xdp, shrink, tail, mem_type, release); - goto out; - } - -if (release) -__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); -out:if (!release) {if (!tail)skb_frag_off_add (frag, shrink);skb_frag_size_sub (frag, shrink); + }else if (!zc) { +__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); }return release;Quoted 22 lines
net/core/filter.c +2
-6+return release; }@@ -4198,12 +4208,8 @@ static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset) len_free += shrink; offset -= shrink; -if (bpf_xdp_shrink_data (xdp, frag, shrink)) { +if (bpf_xdp_shrink_data (xdp, frag, shrink, true)) n_frags_free++; - }else { -skb_frag_size_sub (frag, shrink); -break ; - } } sinfo->nr_frags -= n_frags_free; sinfo->xdp_frags_size -= len_free; --2.47.3
PATCH v4 2/6 bpf: Support pulling non-linear xdp data
In reply to Amery HungAdd kfunc, bpf_xdp_pull_data(), to support pulling data from xdp fragments. Similar to bpf_skb_pull_data(), bpf_xdp_pull_data() makes the first len bytes of data directly readable and writable in bpf programs. If the "len" argument is larger than the linear data size, data in fragments will be copied to the linear data area when there is enough room. Specifically, the kfunc will try to use the tailroom first. When the tailroom is not enough, metadata and data will be shifted down to make room for pulling data.
A use case of the kfunc is to decapsulate headers residing in xdp fragments. It is possible for a NIC driver to place headers in xdp fragments. To keep using direct packet access for parsing and decapsulating headers, users can pull headers into the linear data area by calling bpf_xdp_pull_data() and then pop the header with bpf_xdp_adjust_head().
--- net/core/filter.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)
net/core/filter.c +91 -0
@@ -12212,6 +12212,96 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,return 0 ; } +/** +* bpf_xdp_pull_data() - Pull in non-linear xdp data. +* @x: &xdp_md associated with the XDP buffer +* @len: length of data to be made directly accessible in the linear part +* +* Pull in data in case the XDP buffer associated with @x is non-linear and +* not all @len are in the linear data area. +* +* Direct packet access allows reading and writing linear XDP data through +* packet pointers (i.e., &xdp_md->data + offsets). The amount of data which +* ends up in the linear part of the xdp_buff depends on the NIC and its +* configuration. When a frag-capable XDP program wants to directly access +* headers that may be in the non-linear area, call this kfunc to make sure +* the data is available in the linear area. Alternatively, use dynptr or +* bpf_xdp_{load,store}_bytes() to access data without pulling. +* +* This kfunc can also be used with bpf_xdp_adjust_head() to decapsulate +* headers in the non-linear data area. +* +* A call to this kfunc may reduce headroom. If there is not enough tailroom +* in the linear data area, metadata and data will be shifted down. +* +* A call to this kfunc is susceptible to change the buffer geometry. +* Therefore, at load time, all checks on pointers previously done by the +* verifier are invalidated and must be performed again, if the kfunc is used +* in combination with direct packet access. +* +* Return: +* * %0 - success +* * %-EINVAL - invalid len +*/ +__bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) +{ +struct xdp_buff *xdp = (struct xdp_buff *)x; +struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp); +int i, delta, shift, headroom, tailroom, n_frags_free =0 ; +void *data_hard_end =xdp_data_hard_end (xdp); +int data_len = xdp->data_end - xdp->data; +void *start; + +if (len <= data_len) +return 0 ; + +if (unlikely (len >xdp_get_buff_len (xdp))) +return -EINVAL ; + + start =xdp_data_meta_unsupported (xdp) ? xdp->data : xdp->data_meta; + + headroom = start - xdp->data_hard_start -sizeof (struct xdp_frame ); + tailroom = data_hard_end - xdp->data_end; + + delta = len - data_len; +if (unlikely (delta > tailroom + headroom)) +return -EINVAL ; + + shift = delta - tailroom; +if (shift >0 ) { +memmove (start - shift, start, xdp->data_end - start); + + xdp->data_meta -= shift; + xdp->data -= shift; + xdp->data_end -= shift; + } + +for (i =0 ; i < sinfo->nr_frags && delta; i++) { +skb_frag_t *frag = &sinfo->frags[i]; +u32 shrink =min_t (u32, delta,skb_frag_size (frag)); + +memcpy (xdp->data_end,skb_frag_address (frag), shrink); + + xdp->data_end += shrink; + sinfo->xdp_frags_size -= shrink; + delta -= shrink; +if (bpf_xdp_shrink_data (xdp, frag, shrink, false)) + n_frags_free++; + } + +if (unlikely (n_frags_free)) { +memmove (sinfo->frags, sinfo->frags + n_frags_free, + (sinfo->nr_frags - n_frags_free) *sizeof (skb_frag_t)); + + sinfo->nr_frags -= n_frags_free; + +if (!sinfo->nr_frags) +xdp_buff_clear_frags_flag (xdp); + } + +return 0 ; +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
@@ -12239,6 +12329,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_skb_meta) BTF_KFUNCS_START(bpf_kfunc_check_set_xdp) BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) +BTF_ID_FLAGS(func, bpf_xdp_pull_data) BTF_KFUNCS_END(bpf_kfunc_check_set_xdp) BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr)
Signature · 1 line
2.47.3
Maciej Fijalkowski
maintainerRe: [PATCH bpf-next v4 2/6] bpf: Support pulling non-linear xdp data
Quoted 105 lines · Amery Hung
Add kfunc,
bpf_xdp_pull_data(), to support pulling data from xdp fragments. Similar tobpf_skb_pull_data(),bpf_xdp_pull_data()makes the first len bytes of data directly readable and writable in bpf programs. If the "len" argument is larger than the linear data size, data in fragments will be copied to the linear data area when there is enough room. Specifically, the kfunc will try to use the tailroom first. When the tailroom is not enough, metadata and data will be shifted down to make room for pulling data.A use case of the kfunc is to decapsulate headers residing in xdp fragments. It is possible for a NIC driver to place headers in xdp fragments. To keep using direct packet access for parsing and decapsulating headers, users can pull headers into the linear data area by calling
bpf_xdp_pull_data()and then pop the header withbpf_xdp_adjust_head().Reviewed-by Jakub Kicinski <[email protected]>Signed-off-by Amery Hung <[email protected]>--- net/core/filter.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)net/core/filter.c +75
-0@@ -12212,6 +12212,96 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,return 0 ; } +/** +* bpf_xdp_pull_data() - Pull in non-linear xdp data. +* @x: &xdp_md associated with the XDP buffer +* @len: length of data to be made directly accessible in the linear part +* +* Pull in data in case the XDP buffer associated with @x is non-linear and +* not all @len are in the linear data area. +* +* Direct packet access allows reading and writing linear XDP data through +* packet pointers (i.e., &xdp_md->data + offsets). The amount of data which +* ends up in the linear part of the xdp_buff depends on the NIC and its +* configuration. When a frag-capable XDP program wants to directly access +* headers that may be in the non-linear area, call this kfunc to make sure +* the data is available in the linear area. Alternatively, use dynptr or +* bpf_xdp_{load,store}_bytes() to access data without pulling. +* +* This kfunc can also be used with bpf_xdp_adjust_head() to decapsulate +* headers in the non-linear data area. +* +* A call to this kfunc may reduce headroom. If there is not enough tailroom +* in the linear data area, metadata and data will be shifted down. +* +* A call to this kfunc is susceptible to change the buffer geometry. +* Therefore, at load time, all checks on pointers previously done by the +* verifier are invalidated and must be performed again, if the kfunc is used +* in combination with direct packet access. +* +* Return: +* * %0 - success +* * %-EINVAL - invalid len +*/ +__bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) +{ +struct xdp_buff *xdp = (struct xdp_buff *)x; +struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp); +int i, delta, shift, headroom, tailroom, n_frags_free =0 ; +void *data_hard_end =xdp_data_hard_end (xdp); +int data_len = xdp->data_end - xdp->data; +void *start; + +if (len <= data_len) +return 0 ; + +if (unlikely (len >xdp_get_buff_len (xdp))) +return -EINVAL ; + + start =xdp_data_meta_unsupported (xdp) ? xdp->data : xdp->data_meta; + + headroom = start - xdp->data_hard_start -sizeof (struct xdp_frame ); + tailroom = data_hard_end - xdp->data_end; + + delta = len - data_len; +if (unlikely (delta > tailroom + headroom)) +return -EINVAL ; + + shift = delta - tailroom; +if (shift >0 ) { +memmove (start - shift, start, xdp->data_end - start); + + xdp->data_meta -= shift; + xdp->data -= shift; + xdp->data_end -= shift; + } + +for (i =0 ; i < sinfo->nr_frags && delta; i++) { +skb_frag_t *frag = &sinfo->frags[i]; +u32 shrink =min_t (u32, delta,skb_frag_size (frag)); + +memcpy (xdp->data_end,skb_frag_address (frag), shrink); + + xdp->data_end += shrink; + sinfo->xdp_frags_size -= shrink; + delta -= shrink; +if (bpf_xdp_shrink_data (xdp, frag, shrink, false)) + n_frags_free++;
net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12299
Quoted 10 lines
net/core/filter.c +10
-0+ } + +if (unlikely (n_frags_free)) { +memmove (sinfo->frags, sinfo->frags + n_frags_free, + (sinfo->nr_frags - n_frags_free) *sizeof (skb_frag_t)); + + sinfo->nr_frags -= n_frags_free; + +if (!sinfo->nr_frags) +xdp_buff_clear_frags_flag (xdp);
Nit: should we take care of pfmemalloc flag as well?
Quoted 19 lines
net/core/filter.c +6
-0+ } + +return 0 ; +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,@@ -12239,6 +12329,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_skb_meta) BTF_KFUNCS_START(bpf_kfunc_check_set_xdp) BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) +BTF_ID_FLAGS(func, bpf_xdp_pull_data) BTF_KFUNCS_END(bpf_kfunc_check_set_xdp) BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr)Signature · 1 line
2.47.3
Re: [PATCH bpf-next v4 2/6] bpf: Support pulling non-linear xdp data
Quoted 111 lines · Maciej Fijalkowski
Quoted 109 lines · Amery Hung
Add kfunc,
bpf_xdp_pull_data(), to support pulling data from xdp fragments. Similar tobpf_skb_pull_data(),bpf_xdp_pull_data()makes the first len bytes of data directly readable and writable in bpf programs. If the "len" argument is larger than the linear data size, data in fragments will be copied to the linear data area when there is enough room. Specifically, the kfunc will try to use the tailroom first. When the tailroom is not enough, metadata and data will be shifted down to make room for pulling data.A use case of the kfunc is to decapsulate headers residing in xdp fragments. It is possible for a NIC driver to place headers in xdp fragments. To keep using direct packet access for parsing and decapsulating headers, users can pull headers into the linear data area by calling
bpf_xdp_pull_data()and then pop the header withbpf_xdp_adjust_head().Reviewed-by Jakub Kicinski <[email protected]>Signed-off-by Amery Hung <[email protected]>--- net/core/filter.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)net/core/filter.c +79
-0@@ -12212,6 +12212,96 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,return 0 ; } +/** +* bpf_xdp_pull_data() - Pull in non-linear xdp data. +* @x: &xdp_md associated with the XDP buffer +* @len: length of data to be made directly accessible in the linear part +* +* Pull in data in case the XDP buffer associated with @x is non-linear and +* not all @len are in the linear data area. +* +* Direct packet access allows reading and writing linear XDP data through +* packet pointers (i.e., &xdp_md->data + offsets). The amount of data which +* ends up in the linear part of the xdp_buff depends on the NIC and its +* configuration. When a frag-capable XDP program wants to directly access +* headers that may be in the non-linear area, call this kfunc to make sure +* the data is available in the linear area. Alternatively, use dynptr or +* bpf_xdp_{load,store}_bytes() to access data without pulling. +* +* This kfunc can also be used with bpf_xdp_adjust_head() to decapsulate +* headers in the non-linear data area. +* +* A call to this kfunc may reduce headroom. If there is not enough tailroom +* in the linear data area, metadata and data will be shifted down. +* +* A call to this kfunc is susceptible to change the buffer geometry. +* Therefore, at load time, all checks on pointers previously done by the +* verifier are invalidated and must be performed again, if the kfunc is used +* in combination with direct packet access. +* +* Return: +* * %0 - success +* * %-EINVAL - invalid len +*/ +__bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) +{ +struct xdp_buff *xdp = (struct xdp_buff *)x; +struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp); +int i, delta, shift, headroom, tailroom, n_frags_free =0 ; +void *data_hard_end =xdp_data_hard_end (xdp); +int data_len = xdp->data_end - xdp->data; +void *start; + +if (len <= data_len) +return 0 ; + +if (unlikely (len >xdp_get_buff_len (xdp))) +return -EINVAL ; + + start =xdp_data_meta_unsupported (xdp) ? xdp->data : xdp->data_meta; + + headroom = start - xdp->data_hard_start -sizeof (struct xdp_frame ); + tailroom = data_hard_end - xdp->data_end; + + delta = len - data_len; +if (unlikely (delta > tailroom + headroom)) +return -EINVAL ; + + shift = delta - tailroom; +if (shift >0 ) { +memmove (start - shift, start, xdp->data_end - start); + + xdp->data_meta -= shift; + xdp->data -= shift; + xdp->data_end -= shift; + } + +for (i =0 ; i < sinfo->nr_frags && delta; i++) { +skb_frag_t *frag = &sinfo->frags[i]; +u32 shrink =min_t (u32, delta,skb_frag_size (frag)); + +memcpy (xdp->data_end,skb_frag_address (frag), shrink); + + xdp->data_end += shrink; + sinfo->xdp_frags_size -= shrink; + delta -= shrink; +if (bpf_xdp_shrink_data (xdp, frag, shrink, false)) + n_frags_free++; + } + +if (unlikely (n_frags_free)) { + memmove(sinfo->frags, sinfo->frags + n_frags_free,
Quoted 9 lines
net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12299
Quoted 6 lines
net/core/filter.c +6
-0+ (sinfo->nr_frags - n_frags_free) * sizeof(skb_frag_t)); + + sinfo->nr_frags -= n_frags_free; + +if (!sinfo->nr_frags) +xdp_buff_clear_frags_flag (xdp);Nit: should we take care of pfmemalloc flag as well?
Does it mean we should also clear this bit in bpf_xdp_adjsut_tail()?
Quoted 19 lines
Quoted 19 lines
net/core/filter.c +6
-1+ } + +return 0 ; +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,@@ -12239,6 +12329,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_skb_meta) BTF_KFUNCS_START(bpf_kfunc_check_set_xdp) BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) +BTF_ID_FLAGS(func, bpf_xdp_pull_data) BTF_KFUNCS_END(bpf_kfunc_check_set_xdp) BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr) --2.47.3
Re: [PATCH bpf-next v4 2/6] bpf: Support pulling non-linear xdp data
Quoted 124 lines · Amery Hung
Quoted 111 lines · Maciej Fijalkowski
Quoted 109 lines · Amery Hung
Add kfunc,
bpf_xdp_pull_data(), to support pulling data from xdp fragments. Similar tobpf_skb_pull_data(),bpf_xdp_pull_data()makes the first len bytes of data directly readable and writable in bpf programs. If the "len" argument is larger than the linear data size, data in fragments will be copied to the linear data area when there is enough room. Specifically, the kfunc will try to use the tailroom first. When the tailroom is not enough, metadata and data will be shifted down to make room for pulling data.A use case of the kfunc is to decapsulate headers residing in xdp fragments. It is possible for a NIC driver to place headers in xdp fragments. To keep using direct packet access for parsing and decapsulating headers, users can pull headers into the linear data area by calling
bpf_xdp_pull_data()and then pop the header withbpf_xdp_adjust_head().Reviewed-by Jakub Kicinski <[email protected]>Signed-off-by Amery Hung <[email protected]>--- net/core/filter.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+)net/core/filter.c +79
-0@@ -12212,6 +12212,96 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,return 0 ; } +/** +* bpf_xdp_pull_data() - Pull in non-linear xdp data. +* @x: &xdp_md associated with the XDP buffer +* @len: length of data to be made directly accessible in the linear part +* +* Pull in data in case the XDP buffer associated with @x is non-linear and +* not all @len are in the linear data area. +* +* Direct packet access allows reading and writing linear XDP data through +* packet pointers (i.e., &xdp_md->data + offsets). The amount of data which +* ends up in the linear part of the xdp_buff depends on the NIC and its +* configuration. When a frag-capable XDP program wants to directly access +* headers that may be in the non-linear area, call this kfunc to make sure +* the data is available in the linear area. Alternatively, use dynptr or +* bpf_xdp_{load,store}_bytes() to access data without pulling. +* +* This kfunc can also be used with bpf_xdp_adjust_head() to decapsulate +* headers in the non-linear data area. +* +* A call to this kfunc may reduce headroom. If there is not enough tailroom +* in the linear data area, metadata and data will be shifted down. +* +* A call to this kfunc is susceptible to change the buffer geometry. +* Therefore, at load time, all checks on pointers previously done by the +* verifier are invalidated and must be performed again, if the kfunc is used +* in combination with direct packet access. +* +* Return: +* * %0 - success +* * %-EINVAL - invalid len +*/ +__bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len) +{ +struct xdp_buff *xdp = (struct xdp_buff *)x; +struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (xdp); +int i, delta, shift, headroom, tailroom, n_frags_free =0 ; +void *data_hard_end =xdp_data_hard_end (xdp); +int data_len = xdp->data_end - xdp->data; +void *start; + +if (len <= data_len) +return 0 ; + +if (unlikely (len >xdp_get_buff_len (xdp))) +return -EINVAL ; + + start =xdp_data_meta_unsupported (xdp) ? xdp->data : xdp->data_meta; + + headroom = start - xdp->data_hard_start -sizeof (struct xdp_frame ); + tailroom = data_hard_end - xdp->data_end; + + delta = len - data_len; +if (unlikely (delta > tailroom + headroom)) +return -EINVAL ; + + shift = delta - tailroom; +if (shift >0 ) { +memmove (start - shift, start, xdp->data_end - start); + + xdp->data_meta -= shift; + xdp->data -= shift; + xdp->data_end -= shift; + } + +for (i =0 ; i < sinfo->nr_frags && delta; i++) { +skb_frag_t *frag = &sinfo->frags[i]; +u32 shrink =min_t (u32, delta,skb_frag_size (frag)); + +memcpy (xdp->data_end,skb_frag_address (frag), shrink); + + xdp->data_end += shrink; + sinfo->xdp_frags_size -= shrink; + delta -= shrink; +if (bpf_xdp_shrink_data (xdp, frag, shrink, false)) + n_frags_free++; + } + +if (unlikely (n_frags_free)) { + memmove(sinfo->frags, sinfo->frags + n_frags_free,Quoted 9 lines
net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12299
Quoted 6 lines
net/core/filter.c +6
-0+ (sinfo->nr_frags - n_frags_free) * sizeof(skb_frag_t)); + + sinfo->nr_frags -= n_frags_free; + +if (!sinfo->nr_frags) +xdp_buff_clear_frags_flag (xdp);Nit: should we take care of pfmemalloc flag as well?
Quoted 1 line
Does it mean we should also clear this bit in
bpf_xdp_adjsut_tail()?
Looking at the code, it makes sense to clear it while it currently does not make any difference. All readers of this flag check against it only when there are fragments.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
net/core/filter.c +6
-1+ } + +return 0 ; +} +__bpf_kfunc_end_defs (); int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,@@ -12239,6 +12329,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_skb_meta) BTF_KFUNCS_START(bpf_kfunc_check_set_xdp) BTF_ID_FLAGS(func, bpf_dynptr_from_xdp) +BTF_ID_FLAGS(func, bpf_xdp_pull_data) BTF_KFUNCS_END(bpf_kfunc_check_set_xdp) BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr) --2.47.3
PATCH v4 3/6 bpf: Clear packet pointers after changing packet data in kfuncs
In reply to Amery Hungbpf_xdp_pull_data() may change packet data and therefore packet pointers need to be invalidated. Add bpf_xdp_pull_data() to the special kfunc list instead of introducing a new KF_ flag until there are more kfuncs changing packet data.
--- kernel/bpf/verifier.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
kernel/bpf/verifier.c +13 -0
@@ -12239,6 +12239,7 @@ enum special_kfunc_type { KF_bpf_dynptr_from_skb, KF_bpf_dynptr_from_xdp, KF_bpf_dynptr_from_skb_meta, + KF_bpf_xdp_pull_data, KF_bpf_dynptr_slice, KF_bpf_dynptr_slice_rdwr, KF_bpf_dynptr_clone,
@@ -12289,10 +12290,12 @@ BTF_ID(func, bpf_rbtree_right) BTF_ID(func, bpf_dynptr_from_skb) BTF_ID(func, bpf_dynptr_from_xdp) BTF_ID(func, bpf_dynptr_from_skb_meta) +BTF_ID(func, bpf_xdp_pull_data)#else BTF_ID_UNUSED BTF_ID_UNUSED BTF_ID_UNUSED +BTF_ID_UNUSED #endif BTF_ID(func, bpf_dynptr_slice) BTF_ID(func, bpf_dynptr_slice_rdwr)
@@ -12362,6 +12365,11 @@ static bool is_kfunc_bpf_preempt_enable(struct bpf_kfunc_call_arg_meta *meta)return meta->func_id == special_kfunc_list[KF_bpf_preempt_enable]; } +static bool is_kfunc_pkt_changing (struct bpf_kfunc_call_arg_meta *meta) +{ +return meta->func_id == special_kfunc_list[KF_bpf_xdp_pull_data]; +} + static enum kfunc_ptr_arg_type get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_kfunc_call_arg_meta *meta,
@@ -14081,6 +14089,9 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, } } +if (is_kfunc_pkt_changing (&meta)) +clear_all_pkt_pointers (env); + nargs =btf_type_vlen (meta.func_proto); args = (const struct btf_param *)(meta.func_proto +1 );for (i =0 ; i < nargs; i++) {
@@ -17802,6 +17813,8 @@ static int visit_insn(int t, struct bpf_verifier_env *env)*/ if (ret ==0 &&is_kfunc_sleepable (&meta))mark_subprog_might_sleep (env, t); +if (ret ==0 &&is_kfunc_pkt_changing (&meta)) +mark_subprog_changes_pkt_data (env, t); }return visit_func_call_insn (t, insns, env, insn->src_reg ==BPF_PSEUDO_CALL );
Signature · 1 line
2.47.3
PATCH v4 4/6 bpf: Support specifying linear xdp packet data size for BPF_PROG_TEST_RUN
In reply to Amery HungTo test bpf_xdp_pull_data(), an xdp packet containing fragments as well as free linear data area after xdp->data_end needs to be created. However, bpf_prog_test_run_xdp() always fills the linear area with data_in before creating fragments, leaving no space to pull data. This patch will allow users to specify the linear data size through ctx->data_end.
Currently, ctx_in->data_end must match data_size_in and will not be the final ctx->data_end seen by xdp programs. This is because ctx->data_end is populated according to the xdp_buff passed to test_run. The linear data area available in an xdp_buff, max_data_sz, is alawys filled up before copying data_in into fragments.
This patch will allow users to specify the size of data that goes into the linear area. When ctx_in->data_end is different from data_size_in, only ctx_in->data_end bytes of data will be put into the linear area when creating the xdp_buff.
While ctx_in->data_end will be allowed to be different from data_size_in, it cannot be larger than the data_size_in as there will be no data to copy from user space. If it is larger than the maximum linear data area size, the layout suggested by the user will not be honored. Data beyond max_data_sz bytes will still be copied into fragments.
Finally, since it is possible for a NIC to produce a xdp_buff with empty linear data area, allow it when calling bpf_test_init() from bpf_prog_test_run_xdp() so that we can test XDP kfuncs with such xdp_buff. This is done by moving lower-bound check to callers as most of them already do except bpf_prog_test_run_skb().
--- net/bpf/test_run.c | 9 +++++++-- .../selftests/bpf/prog_tests/xdp_context_test_run.c | 4 +--- 2 files changed, 8 insertions(+), 5 deletions(-)
net/bpf/test_run.c +7 -2
@@ -665,7 +665,7 @@ static void *bpf_test_init(const union bpf_attr *kattr, u32 user_size,void __user *data_in =u64_to_user_ptr (kattr->test.data_in);void *data; -if (user_size <ETH_HLEN || user_size >PAGE_SIZE - headroom - tailroom) +if (user_size >PAGE_SIZE - headroom - tailroom)return ERR_PTR (-EINVAL ); size =SKB_DATA_ALIGN (size);
@@ -1001,6 +1001,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, kattr->test.cpu || kattr->test.batch_size)return -EINVAL ; +if (size <ETH_HLEN ) +return -EINVAL ; + data =bpf_test_init (kattr, kattr->test.data_size_in, size,NET_SKB_PAD +NET_IP_ALIGN ,SKB_DATA_ALIGN (sizeof (struct skb_shared_info )));
@@ -1246,13 +1249,15 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,if (ctx) {/* There can't be user provided data before the meta data */ -if (ctx->data_meta || ctx->data_end != size || +if (ctx->data_meta || ctx->data_end > size || ctx->data > ctx->data_end ||unlikely (xdp_metalen_invalid (ctx->data)) || (do_live && (kattr->test.data_out || kattr->test.ctx_out))) goto free_ctx;/* Meta data is allocated from the headroom */ headroom -= ctx->data; + + size = ctx->data_end; } max_data_sz =PAGE_SIZE - headroom - tailroom;
tools/testing/selftests/bpf/prog_tests/xdp_context_test_run.c +1 -3
@@ -97,9 +97,7 @@ void test_xdp_context_test_run(void)/* Meta data must be 255 bytes or smaller */ test_xdp_context_error (prog_fd, opts,0 ,256 ,sizeof (data),0 ,0 ,0 ); -/* Total size of data must match data_end - data_meta */ -test_xdp_context_error (prog_fd, opts,0 ,sizeof (__u32), -sizeof (data) -1 ,0 ,0 ,0 ); +/* Total size of data must be data_end - data_meta or larger */ test_xdp_context_error (prog_fd, opts,0 ,sizeof (__u32),sizeof (data) +1 ,0 ,0 ,0 );
Signature · 1 line
2.47.3
PATCH v4 5/6 selftests/bpf: Test bpf_xdp_pull_data
In reply to Amery HungTest bpf_xdp_pull_data() with xdp packets with different layouts. The xdp bpf program first checks if the layout is as expected. Then, it calls bpf_xdp_pull_data(). Finally, it checks the 0xbb marker at offset 1024 using directly packet access.
--- .../selftests/bpf/prog_tests/xdp_pull_data.c | 176 ++++++++++++++++++ .../selftests/bpf/progs/test_xdp_pull_data.c | 48 +++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_pull_data.c
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +176 -0
@@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include <network_helpers.h> +#include "test_xdp_pull_data.skel.h" + +#define PULL_MAX (1 << 31) +#define PULL_PLUS_ONE (1 << 30) + +#define XDP_PACKET_HEADROOM 256 + +/* Find sizes of struct skb_shared_info and struct xdp_frame so that +* we can calculate the maximum pull lengths for test cases +*/ +static int find_xdp_sizes (struct test_xdp_pull_data *skel,int frame_sz) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (frame_sz,sizeof (__u8)); +if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return -ENOMEM ; + + topts.data_in = buf; + topts.data_out = buf; + topts.data_size_in = frame_sz; + topts.data_size_out = frame_sz; +/* Pass a data_end larger than the linear space available to make sure +* bpf_prog_test_run_xdp() will fill the linear data area so that +* xdp_find_data_hard_end can infer the size of struct skb_shared_info +*/ + ctx.data_end = frame_sz; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx; + topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + prog_fd =bpf_program__fd (skel->progs.xdp_find_sizes); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +free (buf); + +return err; +} + +/* xdp_pull_data_prog will directly read a marker 0xbb stored at buf[1024] +* so caller expecting XDP_PASS should always pass pull_len no less than 1024 +*/ +static void run_test (struct test_xdp_pull_data *skel,int retval, +int frame_sz,int buff_len,int meta_len,int data_len, +int pull_len) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (buff_len,sizeof (__u8)); +if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return ; + + buf[meta_len +1023 ] =0xaa ; + buf[meta_len +1024 ] =0xbb ; + buf[meta_len +1025 ] =0xcc ; + + topts.data_in = buf; + topts.data_out = buf; + topts.data_size_in = buff_len; + topts.data_size_out = buff_len; + ctx.data = meta_len; + ctx.data_end = meta_len + data_len; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx; + topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + skel->bss->data_len = data_len; +if (pull_len &PULL_MAX ) { +int headroom =XDP_PACKET_HEADROOM - meta_len - skel->bss->xdpf_sz; +int tailroom = frame_sz -XDP_PACKET_HEADROOM - + data_len - skel->bss->sinfo_sz; + + pull_len = pull_len &PULL_PLUS_ONE ?1 :0 ; + pull_len += headroom + tailroom + data_len; + } + skel->bss->pull_len = pull_len; + + prog_fd =bpf_program__fd (skel->progs.xdp_pull_data_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); +ASSERT_EQ (topts.retval, retval,"xdp_pull_data_prog retval" ); + +if (retval ==XDP_DROP ) + goto out; + +ASSERT_EQ (ctx.data_end, meta_len + pull_len,"linear data size" ); +ASSERT_EQ (topts.data_size_out, buff_len,"linear + non-linear data size" ); +/* Make sure data around xdp->data_end was not messed up by +* bpf_xdp_pull_data() +*/ +ASSERT_EQ (buf[meta_len +1023 ],0xaa ,"data[1023]" ); +ASSERT_EQ (buf[meta_len +1024 ],0xbb ,"data[1024]" ); +ASSERT_EQ (buf[meta_len +1025 ],0xcc ,"data[1025]" ); +out: +free (buf); +} + +static void test_xdp_pull_data_basic (void ) +{ +u32 pg_sz, max_meta_len, max_data_len; +struct test_xdp_pull_data *skel; + + skel =test_xdp_pull_data__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_xdp_pull_data__open_and_load" )) +return ; + + pg_sz =sysconf (_SC_PAGE_SIZE); + +if (find_xdp_sizes (skel, pg_sz)) + goto out; + + max_meta_len =XDP_PACKET_HEADROOM - skel->bss->xdpf_sz; + max_data_len = pg_sz -XDP_PACKET_HEADROOM - skel->bss->sinfo_sz; + +/* linear xdp pkt, pull 0 byte */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,2048 ,2048 ); + +/* multi-buf pkt, pull results in linear xdp pkt */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,1024 ,2048 ); + +/* multi-buf pkt, pull 1 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1024 ,1025 ); + +/* multi-buf pkt, pull 0 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1025 ,1025 ); + +/* multi-buf pkt, empty linear data area, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,0 ,PULL_MAX ); + +/* multi-buf pkt, no headroom */ +run_test (skel,XDP_PASS , pg_sz,9000 , max_meta_len,1024 ,PULL_MAX ); + +/* multi-buf pkt, no tailroom, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 , max_data_len,PULL_MAX ); + + +/* linear xdp pkt, pull more than total data len */ +run_test (skel,XDP_DROP , pg_sz,2048 ,0 ,2048 ,2049 ); + +/* multi-buf pkt with no space left in linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len, max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, empty linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 ,0 ,PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no headroom */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len,1024 , +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no tailroom */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 , max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +out: +test_xdp_pull_data__destroy (skel); +} + +void test_xdp_pull_data (void ) +{ +if (test__start_subtest ("xdp_pull_data" )) +test_xdp_pull_data_basic (); +}
tools/testing/selftests/bpf/progs/test_xdp_pull_data.c new file +48 -0
48 lines not on this page · raw message
Signature · 1 line
2.47.3
Maciej Fijalkowski
maintainerRe: [PATCH bpf-next v4 5/6] selftests/bpf: Test bpf_xdp_pull_data
Quoted 22 lines · Amery Hung
Test
bpf_xdp_pull_data()with xdp packets with different layouts. The xdp bpf program first checks if the layout is as expected. Then, it callsbpf_xdp_pull_data(). Finally, it checks the0xbbmarker at offset 1024 using directly packet access.Signed-off-by Amery Hung <[email protected]>--- .../selftests/bpf/prog_tests/xdp_pull_data.c | 176 ++++++++++++++++++ .../selftests/bpf/progs/test_xdp_pull_data.c | 48 +++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_pull_data.ctools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +3
-0@@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h>
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 13
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+#include <network_helpers.h> +#include "test_xdp_pull_data.skel.h" + +#define PULL_MAX (1 << 31) +#define PULL_PLUS_ONE (1 << 30) + +#define XDP_PACKET_HEADROOM 256 + +/* Find sizes of struct skb_shared_info and struct xdp_frame so that +* we can calculate the maximum pull lengths for test cases
do you really need this hack? Wouldn't it be possible to find these sizes via BTF?
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +9
-0+*/ +static int find_xdp_sizes (struct test_xdp_pull_data *skel,int frame_sz) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (frame_sz,sizeof (__u8));
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 32
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return -ENOMEM ; + + topts.data_in = buf; + topts.data_out = buf; + topts.data_size_in = frame_sz; + topts.data_size_out = frame_sz; +/* Pass a data_end larger than the linear space available to make sure +* bpf_prog_test_run_xdp() will fill the linear data area so that +* xdp_find_data_hard_end can infer the size of struct skb_shared_info
what is xdp_find_data_hard_end ?
Quoted 44 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +44
-0+*/ + ctx.data_end = frame_sz; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx; + topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + prog_fd =bpf_program__fd (skel->progs.xdp_find_sizes); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +free (buf); + +return err; +} + +/* xdp_pull_data_prog will directly read a marker 0xbb stored at buf[1024] +* so caller expecting XDP_PASS should always pass pull_len no less than 1024 +*/ +static void run_test (struct test_xdp_pull_data *skel,int retval, +int frame_sz,int buff_len,int meta_len,int data_len, +int pull_len) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (buff_len,sizeof (__u8)); +if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return ; + + buf[meta_len +1023 ] =0xaa ; + buf[meta_len +1024 ] =0xbb ; + buf[meta_len +1025 ] =0xcc ; + + topts.data_in = buf; + topts.data_out = buf; + topts.data_size_in = buff_len; + topts.data_size_out = buff_len; + ctx.data = meta_len; + ctx.data_end = meta_len + data_len; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx;
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 86
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+ topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + skel->bss->data_len = data_len; +if (pull_len &PULL_MAX ) { +int headroom =XDP_PACKET_HEADROOM - meta_len - skel->bss->xdpf_sz; +int tailroom = frame_sz -XDP_PACKET_HEADROOM - + data_len - skel->bss->sinfo_sz; + + pull_len = pull_len &PULL_PLUS_ONE ?1 :0 ;
nit: pull_len = !!(pull_len & PULL_PLUS_ONE);
Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +33
-0+ pull_len += headroom + tailroom + data_len; + } + skel->bss->pull_len = pull_len; + + prog_fd =bpf_program__fd (skel->progs.xdp_pull_data_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); +ASSERT_EQ (topts.retval, retval,"xdp_pull_data_prog retval" ); + +if (retval ==XDP_DROP ) + goto out; + +ASSERT_EQ (ctx.data_end, meta_len + pull_len,"linear data size" ); +ASSERT_EQ (topts.data_size_out, buff_len,"linear + non-linear data size" ); +/* Make sure data around xdp->data_end was not messed up by +* bpf_xdp_pull_data() +*/ +ASSERT_EQ (buf[meta_len +1023 ],0xaa ,"data[1023]" ); +ASSERT_EQ (buf[meta_len +1024 ],0xbb ,"data[1024]" ); +ASSERT_EQ (buf[meta_len +1025 ],0xcc ,"data[1025]" ); +out: +free (buf); +} + +static void test_xdp_pull_data_basic (void ) +{ +u32 pg_sz, max_meta_len, max_data_len; +struct test_xdp_pull_data *skel; + + skel =test_xdp_pull_data__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_xdp_pull_data__open_and_load" )) +return ; +
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 129
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+ pg_sz =sysconf (_SC_PAGE_SIZE); + +if (find_xdp_sizes (skel, pg_sz)) + goto out; + + max_meta_len =XDP_PACKET_HEADROOM - skel->bss->xdpf_sz; + max_data_len = pg_sz -XDP_PACKET_HEADROOM - skel->bss->sinfo_sz; + +/* linear xdp pkt, pull 0 byte */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,2048 ,2048 );
you're passing pg_sz to avoid repeated syscalls I assume? Is it worth to pass prog_fd as well?
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +9
-0+ +/* multi-buf pkt, pull results in linear xdp pkt */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,1024 ,2048 ); + +/* multi-buf pkt, pull 1 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1024 ,1025 ); + +/* multi-buf pkt, pull 0 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1025 ,1025 );
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 148
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+ +/* multi-buf pkt, empty linear data area, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,0 ,PULL_MAX ); + +/* multi-buf pkt, no headroom */ +run_test (skel,XDP_PASS , pg_sz,9000 , max_meta_len,1024 ,PULL_MAX ); + +/* multi-buf pkt, no tailroom, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 , max_data_len,PULL_MAX ); +
nit: double empty line
Quoted 85 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +28
-0+ +/* linear xdp pkt, pull more than total data len */ +run_test (skel,XDP_DROP , pg_sz,2048 ,0 ,2048 ,2049 ); + +/* multi-buf pkt with no space left in linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len, max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, empty linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 ,0 ,PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no headroom */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len,1024 , +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no tailroom */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 , max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +out: +test_xdp_pull_data__destroy (skel); +} + +void test_xdp_pull_data (void ) +{ +if (test__start_subtest ("xdp_pull_data" )) +test_xdp_pull_data_basic (); +}tools/testing/selftests/bpf/progs/test_xdp_pull_data.c new file +48
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +int xdpf_sz; +int sinfo_sz; +int data_len; +int pull_len; + +#define XDP_PACKET_HEADROOM 256 + +SEC("xdp.frags") +int xdp_find_sizes (struct xdp_md *ctx) +{ + xdpf_sz =sizeof (struct xdp_frame ); + sinfo_sz = __PAGE_SIZE -XDP_PACKET_HEADROOM - + (ctx->data_end - ctx->data); + +return XDP_PASS ; +} + +SEC("xdp.frags") +int xdp_pull_data_prog (struct xdp_md *ctx) +{ +__u8 *data_end = (void *)(long )ctx->data_end; +__u8 *data = (void *)(long )ctx->data; +__u8 *val_p; +int err; + +if (data_len != data_end - data) +return XDP_DROP ; + + err =bpf_xdp_pull_data (ctx, pull_len); +if (err) +return XDP_DROP ; + + val_p = (void *)(long )ctx->data +1024 ; +if (val_p +1 > (void *)(long )ctx->data_end) +return XDP_DROP ; + +if (*val_p !=0xbb ) +return XDP_DROP ; + +return XDP_PASS ; +} + +char _license[] SEC("license") = "GPL";Signature · 1 line
2.47.3
Re: [PATCH bpf-next v4 5/6] selftests/bpf: Test bpf_xdp_pull_data
Quoted 29 lines · Maciej Fijalkowski
Quoted 27 lines · Amery Hung
Test
bpf_xdp_pull_data()with xdp packets with different layouts. The xdp bpf program first checks if the layout is as expected. Then, it callsbpf_xdp_pull_data(). Finally, it checks the0xbbmarker at offset 1024 using directly packet access.Signed-off-by Amery Hung <[email protected]>--- .../selftests/bpf/prog_tests/xdp_pull_data.c | 176 ++++++++++++++++++ .../selftests/bpf/progs/test_xdp_pull_data.c | 48 +++++ 2 files changed, 224 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_pull_data.ctools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +8
-0@@ -0,0 +1,176 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include <network_helpers.h> +#include "test_xdp_pull_data.skel.h" + +#define PULL_MAX (1 << 31) +#define PULL_PLUS_ONE (1 << 30)
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 13
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +5
-0+ +#define XDP_PACKET_HEADROOM 256 + +/* Find sizes of struct skb_shared_info and struct xdp_frame so that +* we can calculate the maximum pull lengths for test cases do you really need this hack? Wouldn't it be possible to find these sizes via BTF?
It is possible. I will use kernel BTF to find the sizes.
Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +13
-0+*/ +static int find_xdp_sizes (struct test_xdp_pull_data *skel,int frame_sz) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (frame_sz,sizeof (__u8)); +if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return -ENOMEM ; + + topts.data_in = buf;
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 32
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +6
-0+ topts.data_out = buf; + topts.data_size_in = frame_sz; + topts.data_size_out = frame_sz; +/* Pass a data_end larger than the linear space available to make sure +* bpf_prog_test_run_xdp() will fill the linear data area so that +* xdp_find_data_hard_end can infer the size of struct skb_shared_info what is
xdp_find_data_hard_end?
It is supposed to be the XDP program, xdp_find_sizes. Will remove it as we use BTF to find sizes.
Quoted 96 lines
Quoted 45 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +44
-0+*/ + ctx.data_end = frame_sz; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx; + topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + prog_fd =bpf_program__fd (skel->progs.xdp_find_sizes); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); + +free (buf); + +return err; +} + +/* xdp_pull_data_prog will directly read a marker 0xbb stored at buf[1024] +* so caller expecting XDP_PASS should always pass pull_len no less than 1024 +*/ +static void run_test (struct test_xdp_pull_data *skel,int retval, +int frame_sz,int buff_len,int meta_len,int data_len, +int pull_len) +{ +LIBBPF_OPTS (bpf_test_run_opts, topts); +struct xdp_md ctx = {}; +int prog_fd, err; +__u8 *buf; + + buf =calloc (buff_len,sizeof (__u8)); +if (!ASSERT_OK_PTR (buf,"calloc buf" )) +return ; + + buf[meta_len +1023 ] =0xaa ; + buf[meta_len +1024 ] =0xbb ; + buf[meta_len +1025 ] =0xcc ; + + topts.data_in = buf; + topts.data_out = buf; + topts.data_size_in = buff_len; + topts.data_size_out = buff_len; + ctx.data = meta_len; + ctx.data_end = meta_len + data_len; + topts.ctx_in = &ctx; + topts.ctx_out = &ctx;tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 86
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +10
-0+ topts.ctx_size_in =sizeof (ctx); + topts.ctx_size_out =sizeof (ctx); + + skel->bss->data_len = data_len; +if (pull_len &PULL_MAX ) { +int headroom =XDP_PACKET_HEADROOM - meta_len - skel->bss->xdpf_sz; +int tailroom = frame_sz -XDP_PACKET_HEADROOM - + data_len - skel->bss->sinfo_sz; + + pull_len = pull_len &PULL_PLUS_ONE ?1 :0 ;nit:
pull_len= !!(pull_len &PULL_PLUS_ONE);Quoted 38 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +38
-0+ pull_len += headroom + tailroom + data_len; + } + skel->bss->pull_len = pull_len; + + prog_fd =bpf_program__fd (skel->progs.xdp_pull_data_prog); + err =bpf_prog_test_run_opts (prog_fd, &topts); +ASSERT_OK (err,"bpf_prog_test_run_opts" ); +ASSERT_EQ (topts.retval, retval,"xdp_pull_data_prog retval" ); + +if (retval ==XDP_DROP ) + goto out; + +ASSERT_EQ (ctx.data_end, meta_len + pull_len,"linear data size" ); +ASSERT_EQ (topts.data_size_out, buff_len,"linear + non-linear data size" ); +/* Make sure data around xdp->data_end was not messed up by +* bpf_xdp_pull_data() +*/ +ASSERT_EQ (buf[meta_len +1023 ],0xaa ,"data[1023]" ); +ASSERT_EQ (buf[meta_len +1024 ],0xbb ,"data[1024]" ); +ASSERT_EQ (buf[meta_len +1025 ],0xcc ,"data[1025]" ); +out: +free (buf); +} + +static void test_xdp_pull_data_basic (void ) +{ +u32 pg_sz, max_meta_len, max_data_len; +struct test_xdp_pull_data *skel; + + skel =test_xdp_pull_data__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_xdp_pull_data__open_and_load" )) +return ; + + pg_sz =sysconf (_SC_PAGE_SIZE); + +if (find_xdp_sizes (skel, pg_sz)) + goto out; +
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 129
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +5
-0+ max_meta_len =XDP_PACKET_HEADROOM - skel->bss->xdpf_sz; + max_data_len = pg_sz -XDP_PACKET_HEADROOM - skel->bss->sinfo_sz; + +/* linear xdp pkt, pull 0 byte */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,2048 ,2048 );you're passing
pg_szto avoid repeated syscalls I assume? Is it worth to passprog_fdas well?
The performance is not too much of a concern here as it is selftest. I would not add prog_fd to avoid confusion.
Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +13
-0+ +/* multi-buf pkt, pull results in linear xdp pkt */ +run_test (skel,XDP_PASS , pg_sz,2048 ,0 ,1024 ,2048 ); + +/* multi-buf pkt, pull 1 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1024 ,1025 ); + +/* multi-buf pkt, pull 0 byte to linear data area */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,1025 ,1025 ); + +/* multi-buf pkt, empty linear data area, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 ,0 ,PULL_MAX ); +
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c line 148
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +6
-0+/* multi-buf pkt, no headroom */ +run_test (skel,XDP_PASS , pg_sz,9000 , max_meta_len,1024 ,PULL_MAX ); + +/* multi-buf pkt, no tailroom, pull requires memmove */ +run_test (skel,XDP_PASS , pg_sz,9000 ,0 , max_data_len,PULL_MAX ); +nit: double empty line
Will add:
/* Test cases with invalid pull length */
Quoted 48 lines
Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/xdp_pull_data.c new file +28
-0+ +/* linear xdp pkt, pull more than total data len */ +run_test (skel,XDP_DROP , pg_sz,2048 ,0 ,2048 ,2049 ); + +/* multi-buf pkt with no space left in linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len, max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, empty linear data area */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 ,0 ,PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no headroom */ +run_test (skel,XDP_DROP , pg_sz,9000 , max_meta_len,1024 , +PULL_MAX |PULL_PLUS_ONE ); + +/* multi-buf pkt, no tailroom */ +run_test (skel,XDP_DROP , pg_sz,9000 ,0 , max_data_len, +PULL_MAX |PULL_PLUS_ONE ); + +out: +test_xdp_pull_data__destroy (skel); +} + +void test_xdp_pull_data (void ) +{ +if (test__start_subtest ("xdp_pull_data" )) +test_xdp_pull_data_basic (); +}tools/testing/selftests/bpf/progs/test_xdp_pull_data.c new file +13
-0@@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> + +int xdpf_sz; +int sinfo_sz; +int data_len; +int pull_len; + +#define XDP_PACKET_HEADROOM 256 + +SEC ("xdp.frags" )
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_xdp_pull_data.c line 21
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_xdp_pull_data.c new file +8
-0+int xdp_find_sizes (struct xdp_md *ctx) +{ + xdpf_sz =sizeof (struct xdp_frame ); + sinfo_sz = __PAGE_SIZE -XDP_PACKET_HEADROOM - + (ctx->data_end - ctx->data); + +return XDP_PASS ; +}
Will remove this XDP program
Thank you for your review!
[...]
Quoted 3 lines
Quoted 3 lines
--
2.47.3
PATCH v4 6/6 selftests: drv-net: Pull data before parsing headers
In reply to Amery HungIt is possible for drivers to generate xdp packets with data residing entirely in fragments. To keep parsing headers using direcy packet access, call bpf_xdp_pull_data() to pull headers into the linear data area.
--- .../selftests/net/lib/xdp_native.bpf.c | 89 +++++++++++++++---- 1 file changed, 74 insertions(+), 15 deletions(-)
tools/testing/selftests/net/lib/xdp_native.bpf.c +74 -15
@@ -14,6 +14,8 @@#define MAX_PAYLOAD_LEN 5000#define MAX_HDR_LEN 64 +extern int bpf_xdp_pull_data(struct xdp_md *xdp, __u32 len) __ksym __weak; + enum {XDP_MODE =0 , XDP_PORT = 1,
@@ -68,30 +70,57 @@ static void record_stats(struct xdp_md *ctx, __u32 stat_type)static struct udphdr *filter_udphdr (struct xdp_md *ctx,__u16 port) { -void *data_end = (void *)(long )ctx->data_end; -void *data = (void *)(long )ctx->data;struct udphdr *udph =NULL ; -struct ethhdr *eth = data; +void *data, *data_end; +struct ethhdr *eth; +int err; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth)); +if (err) +return NULL ; + + data_end = (void *)(long )ctx->data_end; + data = eth = (void *)(long )ctx->data;if (data +sizeof (*eth) > data_end)return NULL ;if (eth->h_proto ==bpf_htons (ETH_P_IP )) { -struct iphdr *iph = data +sizeof (*eth); +struct iphdr *iph; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth) +sizeof (*iph) + +sizeof (*udph)); +if (err) +return NULL ; + + data_end = (void *)(long )ctx->data_end; + data = (void *)(long )ctx->data; + + iph = data +sizeof (*eth);if (iph +1 > (struct iphdr *)data_end || iph->protocol !=IPPROTO_UDP )return NULL ; - udph = (void *)eth +sizeof (*iph) +sizeof (*eth); - }else if (eth->h_proto ==bpf_htons (ETH_P_IPV6 )) { -struct ipv6hdr *ipv6h = data +sizeof (*eth); + udph = data +sizeof (*iph) +sizeof (*eth); + }else if (eth->h_proto ==bpf_htons (ETH_P_IPV6 )) { +struct ipv6hdr *ipv6h; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth) +sizeof (*ipv6h) + +sizeof (*udph)); +if (err) +return NULL ; + + data_end = (void *)(long )ctx->data_end; + data = (void *)(long )ctx->data; + + ipv6h = data +sizeof (*eth);if (ipv6h +1 > (struct ipv6hdr *)data_end || ipv6h->nexthdr !=IPPROTO_UDP )return NULL ; - udph = (void *)eth +sizeof (*ipv6h) +sizeof (*eth); + udph = data +sizeof (*ipv6h) +sizeof (*eth); }else {return NULL ; }
@@ -145,17 +174,34 @@ static void swap_machdr(void *data)static int xdp_mode_tx_handler (struct xdp_md *ctx,__u16 port) { -void *data_end = (void *)(long )ctx->data_end; -void *data = (void *)(long )ctx->data;struct udphdr *udph =NULL ; -struct ethhdr *eth = data; +void *data, *data_end; +struct ethhdr *eth; +int err; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth)); +if (err) +return XDP_PASS ; + + data_end = (void *)(long )ctx->data_end; + data = eth = (void *)(long )ctx->data;if (data +sizeof (*eth) > data_end)return XDP_PASS ;if (eth->h_proto ==bpf_htons (ETH_P_IP )) { -struct iphdr *iph = data +sizeof (*eth); -__be32 tmp_ip = iph->saddr; +struct iphdr *iph; +__be32 tmp_ip; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth) +sizeof (*iph) + +sizeof (*udph)); +if (err) +return XDP_PASS ; + + data_end = (void *)(long )ctx->data_end; + data = (void *)(long )ctx->data; + + iph = data +sizeof (*eth);if (iph +1 > (struct iphdr *)data_end || iph->protocol !=IPPROTO_UDP )
@@ -169,8 +215,10 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port)return XDP_PASS ;record_stats (ctx,STATS_RX ); + eth = data;swap_machdr ((void *)eth); + tmp_ip = iph->saddr; iph->saddr = iph->daddr; iph->daddr = tmp_ip;
@@ -178,9 +226,19 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port)return XDP_TX ; - }else if (eth->h_proto ==bpf_htons (ETH_P_IPV6 )) { -struct ipv6hdr *ipv6h = data +sizeof (*eth); + }else if (eth->h_proto ==bpf_htons (ETH_P_IPV6 )) {struct in6_addr tmp_ipv6; +struct ipv6hdr *ipv6h; + + err =bpf_xdp_pull_data (ctx,sizeof (*eth) +sizeof (*ipv6h) + +sizeof (*udph)); +if (err) +return XDP_PASS ; + + data_end = (void *)(long )ctx->data_end; + data = (void *)(long )ctx->data; + + ipv6h = data +sizeof (*eth);if (ipv6h +1 > (struct ipv6hdr *)data_end || ipv6h->nexthdr !=IPPROTO_UDP )
@@ -194,6 +252,7 @@ static int xdp_mode_tx_handler(struct xdp_md *ctx, __u16 port)return XDP_PASS ;record_stats (ctx,STATS_RX ); + eth = data;swap_machdr ((void *)eth);__builtin_memcpy (&tmp_ipv6, &ipv6h->saddr,sizeof (tmp_ipv6));
Signature · 1 line
2.47.3