From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2022-06-29 14:35:26
Hi!
First of all, this might look incorrect as title has a 'fix' keyword and
is sent to bpf-next, but this is due to the fact that Andrii moved xsk
part of libbpf to selftests yesterday, so it's just not in the bpf tree
yet. Also, no new API to libbpf's xsk would be accepted, so that's the
only way to go with fixing xdpxceiver currently. Besides fixes (#2 and
#4), #1 is a small optimization to reduce times we query bpf_link
capability and #3 is a protection against the thing we're fixing here.
This set is about fixing TEST_MODE_SKB in xdpxceiver. Current function
for loading XDP prog independently from AF_XDP socket ignores the flags
from user, such as XDP_FLAGS_SKB_MODE, which makes it impossible to test
generic XDP. TEST_MODE_SKB was running with XDP prog in native mode,
which is not a thing that we want.
Had it been correctly in the first place, we would see that
refcounting/deleting XSK socket had issues as well. We need to free BPF
resources only when context refcount drops to zero. If its higher then
it means that we should not touch prog/map as other sockets are still
active.
Thanks,
Maciej
Maciej Fijalkowski (4):
selftests: xsk: avoid bpf_link probe for existing xsk
selftests: xsk: introduce XDP prog load based on existing AF_XDP
socket
selftests: xsk: verify correctness of XDP prog attach point
selftests: xsk: destroy BPF resources only when ctx refcount drops to
0
tools/testing/selftests/bpf/xdpxceiver.c | 19 ++++++++++++++++++-
tools/testing/selftests/bpf/xsk.c | 15 ++++++++++-----
tools/testing/selftests/bpf/xsk.h | 1 +
3 files changed, 29 insertions(+), 6 deletions(-)
--
2.27.0
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2022-06-29 14:35:26
Currently bpf_link probe is done for each call of xsk_socket__create().
For cases where xsk context was previously created and current socket
creation uses it, has_bpf_link will be overwritten, where it has already
been initialized.
Optimize this by moving the query to the xsk_create_ctx() so that when
xsk_get_ctx() finds a ctx then no further bpf_link probes are needed.
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
tools/testing/selftests/bpf/xsk.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2022-06-29 14:35:32
Currently, xsk_setup_xdp_prog() uses anonymous xsk_socket struct which
means that during xsk_create_bpf_link() call, xsk->config.xdp_flags is
always 0. This in turn means that from xdpxceiver it is impossible to
use xdpgeneric attachment, so since commit 3b22523bca02 ("selftests,
xsk: Fix bpf_res cleanup test") we were not testing SKB mode at all.
To fix this, introduce a function, called xsk_setup_xdp_prog_xsk(), that
will load XDP prog based on the existing xsk_socket, so that xsk
context's refcount is correctly bumped and flags from application side
are respected. Use this from xdpxceiver side so we get coverage of
generic and native XDP program attach points.
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
tools/testing/selftests/bpf/xdpxceiver.c | 2 +-
tools/testing/selftests/bpf/xsk.c | 5 +++++
tools/testing/selftests/bpf/xsk.h | 1 +
3 files changed, 7 insertions(+), 1 deletion(-)
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2022-06-29 14:35:33
To prevent the case we had previously where for TEST_MODE_SKB, XDP prog
was attached in native mode, call bpf_xdp_query() after loading prog and
make sure that attach_mode is as expected.
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
tools/testing/selftests/bpf/xdpxceiver.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2022-06-29 14:35:34
Currently, xsk_socket__delete frees BPF resources regardless of ctx
refcount. Xdpxceiver has a test to verify whether underlying BPF
resources would not be wiped out after closing XSK socket that was bound
to interface with other active sockets. From library's xsk part
perspective it also means that the internal xsk context is shared and
its refcount is bumped accordingly.
After a switch to loading XDP prog based on previously opened XSK
socket, mentioned xdpxceiver test fails with:
not ok 16 [xdpxceiver.c:swap_xsk_resources:1334]: ERROR: 9/"Bad file descriptor
which means that in swap_xsk_resources(), xsk_socket__delete() released
xskmap which in turn caused a failure of xsk_socket__update_xskmap().
To fix this, when deleting socket, decrement ctx refcount before
releasing BPF resources and do so only when refcount dropped to 0 which
means there are no more active sockets for this ctx so BPF resources can
be freed safely.
Fixes: 2f6324a3937f ("libbpf: Support shared umems between queues and devices")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
tools/testing/selftests/bpf/xsk.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
@@ -1257,7 +1258,6 @@ void xsk_socket__delete(struct xsk_socket *xsk)}}-xsk_put_ctx(ctx,true);umem->refcount--;/* Do not close an fd that also has an associated umem connected
From: Magnus Karlsson <hidden> Date: 2022-06-30 09:57:15
On Wed, Jun 29, 2022 at 4:38 PM Maciej Fijalkowski
[off-list ref] wrote:
Currently bpf_link probe is done for each call of xsk_socket__create().
For cases where xsk context was previously created and current socket
creation uses it, has_bpf_link will be overwritten, where it has already
been initialized.
Optimize this by moving the query to the xsk_create_ctx() so that when
xsk_get_ctx() finds a ctx then no further bpf_link probes are needed.
This would be a good optimization to libxdp too.
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
From: Magnus Karlsson <hidden> Date: 2022-06-30 09:57:36
On Wed, Jun 29, 2022 at 4:38 PM Maciej Fijalkowski
[off-list ref] wrote:
Currently, xsk_setup_xdp_prog() uses anonymous xsk_socket struct which
means that during xsk_create_bpf_link() call, xsk->config.xdp_flags is
always 0. This in turn means that from xdpxceiver it is impossible to
use xdpgeneric attachment, so since commit 3b22523bca02 ("selftests,
xsk: Fix bpf_res cleanup test") we were not testing SKB mode at all.
To fix this, introduce a function, called xsk_setup_xdp_prog_xsk(), that
will load XDP prog based on the existing xsk_socket, so that xsk
context's refcount is correctly bumped and flags from application side
are respected. Use this from xdpxceiver side so we get coverage of
generic and native XDP program attach points.
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
From: Magnus Karlsson <hidden> Date: 2022-06-30 09:57:51
On Wed, Jun 29, 2022 at 4:39 PM Maciej Fijalkowski
[off-list ref] wrote:
To prevent the case we had previously where for TEST_MODE_SKB, XDP prog
was attached in native mode, call bpf_xdp_query() after loading prog and
make sure that attach_mode is as expected.
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
From: Magnus Karlsson <hidden> Date: 2022-06-30 09:58:38
On Wed, Jun 29, 2022 at 4:39 PM Maciej Fijalkowski
[off-list ref] wrote:
Currently, xsk_socket__delete frees BPF resources regardless of ctx
refcount. Xdpxceiver has a test to verify whether underlying BPF
resources would not be wiped out after closing XSK socket that was bound
to interface with other active sockets. From library's xsk part
perspective it also means that the internal xsk context is shared and
its refcount is bumped accordingly.
After a switch to loading XDP prog based on previously opened XSK
socket, mentioned xdpxceiver test fails with:
not ok 16 [xdpxceiver.c:swap_xsk_resources:1334]: ERROR: 9/"Bad file descriptor
which means that in swap_xsk_resources(), xsk_socket__delete() released
xskmap which in turn caused a failure of xsk_socket__update_xskmap().
To fix this, when deleting socket, decrement ctx refcount before
releasing BPF resources and do so only when refcount dropped to 0 which
means there are no more active sockets for this ctx so BPF resources can
be freed safely.
Please fix this in libxdp too as the bug is present there also.
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
quoted hunk
Fixes: 2f6324a3937f ("libbpf: Support shared umems between queues and devices")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
tools/testing/selftests/bpf/xsk.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
@@ -1257,7 +1258,6 @@ void xsk_socket__delete(struct xsk_socket *xsk)}}-xsk_put_ctx(ctx,true);umem->refcount--;/* Do not close an fd that also has an associated umem connected--
From: Daniel Borkmann <daniel@iogearbox.net> Date: 2022-06-30 20:52:55
On 6/30/22 11:58 AM, Magnus Karlsson wrote:
On Wed, Jun 29, 2022 at 4:39 PM Maciej Fijalkowski
[off-list ref] wrote:
quoted
Currently, xsk_socket__delete frees BPF resources regardless of ctx
refcount. Xdpxceiver has a test to verify whether underlying BPF
resources would not be wiped out after closing XSK socket that was bound
to interface with other active sockets. From library's xsk part
perspective it also means that the internal xsk context is shared and
its refcount is bumped accordingly.
After a switch to loading XDP prog based on previously opened XSK
socket, mentioned xdpxceiver test fails with:
not ok 16 [xdpxceiver.c:swap_xsk_resources:1334]: ERROR: 9/"Bad file descriptor
which means that in swap_xsk_resources(), xsk_socket__delete() released
xskmap which in turn caused a failure of xsk_socket__update_xskmap().
To fix this, when deleting socket, decrement ctx refcount before
releasing BPF resources and do so only when refcount dropped to 0 which
means there are no more active sockets for this ctx so BPF resources can
be freed safely.
Please fix this in libxdp too as the bug is present there also.
Acked-by: Magnus Karlsson <magnus.karlsson@intel.com>
[...]
quoted
@@ -1238,7 +1236,10 @@ void xsk_socket__delete(struct xsk_socket *xsk) ctx = xsk->ctx; umem = ctx->umem;- if (ctx->prog_fd != -1) {++ xsk_put_ctx(ctx, true);++ if (!ctx->refcount) { xsk_delete_bpf_maps(xsk); close(ctx->prog_fd); if (ctx->has_bpf_link)