Add bpf_xdp_get_xfrm_state() kfunc
Subsystems (7)
v3 cover Add bpf_xdp_get_xfrm_state() kfunc
This patchset adds two kfunc helpers, bpf_xdp_get_xfrm_state() and bpf_xdp_xfrm_state_release() that wrap xfrm_state_lookup() and xfrm_state_put(). The intent is to support software RSS (via XDP) for the ongoing/upcoming ipsec pcpu work [0]. Recent experiments performed on (hopefully) reproducible AWS testbeds indicate that single tunnel pcpu ipsec can reach line rate on 100G ENA nics.
Note this patchset only tests/shows generic xfrm_state access. The "secret sauce" (if you can really even call it that) involves accessing a soon-to-be-upstreamed pcpu_num field in xfrm_state. Early example is available here [1].
[0]: https://datatracker.ietf.org/doc/draft-ietf-ipsecme-multi-sa-performance/03/
[1]: https://github.com/danobi/xdp-tools/blob/e89a1c617aba3b50d990f779357d6ce2863ecb27/xdp-bench/xdp_redirect_cpumap.bpf.c#L385-L406
Changes from v2:
* Fix/simplify BPF_CORE_WRITE_BITFIELD() algorithm
* Added verifier tests for bitfield writes
* Fix state leakage across test_tunnel subtests
Changes from v1:
* Move xfrm tunnel tests to test_progs
* Fix writing to opts->error when opts is invalid
* Use __bpf_kfunc_start_defs()
* Remove unused vxlanhdr definition
* Add and use BPF_CORE_WRITE_BITFIELD() macro
* Make series bisect clean
Changes from RFCv2:
* Rebased to ipsec-next
* Fix netns leak
Changes from RFCv1:
* Add Antony's commit tags
* Add KF_ACQUIRE and KF_RELEASE semantics
Daniel Xu (9):
bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc
bpf: xfrm: Add bpf_xdp_xfrm_state_release() kfunc
libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
bpf: selftests: test_loader: Support __btf_path() annotation
libbpf: selftests: Add verifier tests for CO-RE bitfield writes
bpf: selftests: test_tunnel: Setup fresh topology for each subtest
bpf: selftests: test_tunnel: Use vmlinux.h declarations
bpf: selftests: Move xfrm tunnel test to test_progs
bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state()
include/net/xfrm.h | 9 + net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 128 ++++++++++++++ tools/lib/bpf/bpf_core_read.h | 34 ++++ .../selftests/bpf/prog_tests/test_tunnel.c | 162 +++++++++++++++++- .../selftests/bpf/prog_tests/verifier.c | 2 + tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 138 ++++++++------- .../bpf/progs/verifier_bitfield_write.c | 100 +++++++++++ tools/testing/selftests/bpf/test_loader.c | 7 + tools/testing/selftests/bpf/test_tunnel.sh | 92 ---------- 13 files changed, 522 insertions(+), 155 deletions(-) create mode 100644 net/xfrm/xfrm_state_bpf.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_bitfield_write.c
Signature · 1 line
2.42.1
PATCH v3 1/9 bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc
This commit adds an unstable kfunc helper to access internal xfrm_state associated with an SA. This is intended to be used for the upcoming IPsec pcpu work to assign special pcpu SAs to a particular CPU. In other
words: for custom software RSS.
That being said, the function that this kfunc wraps is fairly generic and used for a lot of xfrm tasks. I'm sure people will find uses elsewhere over time.
--- include/net/xfrm.h | 9 +++ net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 112 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 net/xfrm/xfrm_state_bpf.c
include/net/xfrm.h +9 -0
@@ -2190,4 +2190,13 @@ static inline int register_xfrm_interface_bpf(void)#endif +#if IS_ENABLED (CONFIG_DEBUG_INFO_BTF ) +int register_xfrm_state_bpf (void ); +#else +static inline int register_xfrm_state_bpf (void ) +{ +return 0 ; +} +#endif +#endif /* _NET_XFRM_H */
net/xfrm/Makefile +1 -0
@@ -21,3 +21,4 @@ obj-$(CONFIG_XFRM_USER_COMPAT) += xfrm_compat.o obj-$(CONFIG_XFRM_IPCOMP) += xfrm_ipcomp.o obj-$(CONFIG_XFRM_INTERFACE) += xfrm_interface.o obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o +obj-$(CONFIG_DEBUG_INFO_BTF) += xfrm_state_bpf.o
net/xfrm/xfrm_policy.c +2 -0
@@ -4218,6 +4218,8 @@ void __init xfrm_init(void)#ifdef CONFIG_XFRM_ESPINTCP espintcp_init ();#endif + +register_xfrm_state_bpf (); }#ifdef CONFIG_AUDITSYSCALL
net/xfrm/xfrm_state_bpf.c new file +112 -0
112 lines not on this page · raw message
Signature · 1 line
2.42.1
PATCH v3 2/9 bpf: xfrm: Add bpf_xdp_xfrm_state_release() kfunc
In reply to Daniel XuThis kfunc releases a previously acquired xfrm_state from bpf_xdp_get_xfrm_state().
--- net/xfrm/xfrm_state_bpf.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
net/xfrm/xfrm_state_bpf.c +16 -0
@@ -94,10 +94,26 @@ bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state_opts *opts, u32return x; } +/* bpf_xdp_xfrm_state_release - Release acquired xfrm_state object +* +* This must be invoked for referenced PTR_TO_BTF_ID, and the verifier rejects +* the program if any references remain in the program in all of the explored +* states. +* +* Parameters: +* @x - Pointer to referenced xfrm_state object, obtained using +* bpf_xdp_get_xfrm_state. +*/ +__bpf_kfunc void bpf_xdp_xfrm_state_release(struct xfrm_state *x) +{ +xfrm_state_put (x); +} +__bpf_kfunc_end_defs (); BTF_SET8_START(xfrm_state_kfunc_set) BTF_ID_FLAGS(func, bpf_xdp_get_xfrm_state, KF_RET_NULL | KF_ACQUIRE) +BTF_ID_FLAGS(func, bpf_xdp_xfrm_state_release, KF_RELEASE) BTF_SET8_END(xfrm_state_kfunc_set) static const struct btf_kfunc_id_set xfrm_state_xdp_kfunc_set = {
Signature · 1 line
2.42.1
PATCH v3 3/9 libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
In reply to Daniel Xu=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming preserve_static_offset [0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.
(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated vmlinux.h. It's also not generally available in released LLVM versions yet.
(2) makes the code quite hard to read and write. And especially if BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to to have an inverse helper for writing.
=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
To start, it helps by explaining what LSHIFT_U64 (lshift) and RSHIFT_U64 (rshift) is designed to mean. Consider the core of the BPF_CORE_READ_BITFIELD() algorithm:
val <<= __CORE_RELO(s, field, LSHIFT_U64);
val = val >> __CORE_RELO(s, field, RSHIFT_U64);Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX......
Lshift: XXX......00000000
Rshift: 00000000000000XXXwhere . means blank bit, 0 means 0 bit, and X means bitfield bit.
After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64
Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield
_
| |
0.....00XXX0...00
| | | |
|______| | |
lshift | |
|____|
(rshift - lshift)That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1
nval = new_val; // 2
nval = (nval << rpad) & mask; // 3
val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.
[0]: https://reviews.llvm.org/D133361
--- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
tools/lib/bpf/bpf_core_read.h +34 -0
@@ -111,6 +111,40 @@ enum bpf_enum_value_kind { val; \ }) +/* +* Write to a bitfield, identified by s->field. +* This is the inverse of BPF_CORE_WRITE_BITFIELD(). +*/ +#define BPF_CORE_WRITE_BITFIELD (s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned int rpad = rshift - lshift; \ + unsigned long long nval, mask, val; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \ + case 1: val = *(unsigned char *)p; break; \ + case 2: val = *(unsigned short *)p; break; \ + case 4: val = *(unsigned int *)p; break; \ + case 8: val = *(unsigned long long *)p; break; \ + } \ + \ + mask = (~0ULL << rshift) >> lshift; \ + nval = new_val; \ + nval = (nval << rpad) & mask; \ + val = (val & ~mask) | nval; \ + \ + switch (byte_size) { \ + case 1: *(unsigned char *)p = val; break; \ + case 2: *(unsigned short *)p = val; break; \ + case 4: *(unsigned int *)p = val; break; \ + case 8: *(unsigned long long *)p = val; break; \ + } \ +}) +#define ___bpf_field_ref1 (field) (field)#define ___bpf_field_ref2 (type, field) (((typeof(type) *)0)->field) #define ___bpf_field_ref(args...) \
Signature · 1 line
2.42.1
Re: [devel-ipsec] [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
Quoted 84 lines · Daniel Xu via Devel
=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming
preserve_static_offset[0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated
vmlinux.h. It's also not generally available in released LLVM versions yet.(2) makes the code quite hard to read and write. And especially if
BPF_CORE_READ_BITFIELD()is already being used, it makes more sense to to have an inverse helper for writing.=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
To start, it helps by explaining what
LSHIFT_U64(lshift) andRSHIFT_U64(rshift) is designed to mean. Consider the core of theBPF_CORE_READ_BITFIELD()algorithm:val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64);Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXXwhere
.means blank bit,0means 0 bit, andXmeans bitfield bit.After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift)That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.
Quoted 3 lines
[0]: https://reviews.llvm.org/D133361
Co-authored-by Eduard Zingerman <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>
Just pointing out I inserted Eduard's tags here. Eduard - I hope that's OK. Not sure what the usual procedure for this is.
Quoted 7 lines
Co-authored-by Jonathan Lemon <[email protected]>Signed-off-by Jonathan Lemon <[email protected]>Signed-off-by Daniel Xu <[email protected]>--- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
[..]
Eduard Zingerman
maintainerRe: [devel-ipsec] [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
Quoted 85 lines · Daniel Xu
Quoted 84 lines · Daniel Xu via Devel
=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming
preserve_static_offset[0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated
vmlinux.h. It's also not generally available in released LLVM versions yet.(2) makes the code quite hard to read and write. And especially if
BPF_CORE_READ_BITFIELD()is already being used, it makes more sense to to have an inverse helper for writing.=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
To start, it helps by explaining what
LSHIFT_U64(lshift) andRSHIFT_U64(rshift) is designed to mean. Consider the core of theBPF_CORE_READ_BITFIELD()algorithm:val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64);Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXXwhere
.means blank bit,0means 0 bit, andXmeans bitfield bit.After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift)That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.
Quoted 6 lines
Quoted 3 lines
[0]: https://reviews.llvm.org/D133361
Co-authored-by Eduard Zingerman <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>Just pointing out I inserted Eduard's tags here. Eduard - I hope that's OK. Not sure what the usual procedure for this is.
Not that I did a big contribution, you and Andrii figured out a much better (and correct) expression :) I'm fine with and without this tag, thank you for working on this.
Re: [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
In reply to Daniel XuQuoted 25 lines · Daniel Xu
=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming
preserve_static_offset[0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated
vmlinux.h. It's also not generally available in released LLVM versions yet.(2) makes the code quite hard to read and write. And especially if
BPF_CORE_READ_BITFIELD()is already being used, it makes more sense to to have an inverse helper for writing.=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
Quoted 6 lines
To start, it helps by explaining what
LSHIFT_U64(lshift) andRSHIFT_U64(rshift) is designed to mean. Consider the core of theBPF_CORE_READ_BITFIELD()algorithm:val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64);
nit: indentation is off?
Quoted 36 lines
Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXXwhere
.means blank bit,0means 0 bit, andXmeans bitfield bit.After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift)
Quoted 3 lines
That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Nice diagrams and description, thanks!
Quoted 48 lines
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.[0]: https://reviews.llvm.org/D133361
Co-authored-by Eduard Zingerman <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>Co-authored-by Jonathan Lemon <[email protected]>Signed-off-by Jonathan Lemon <[email protected]>Signed-off-by Daniel Xu <[email protected]>--- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)tools/lib/bpf/bpf_core_read.h +15
-0@@ -111,6 +111,40 @@ enum bpf_enum_value_kind { val; \ }) +/* +* Write to a bitfield, identified by s->field. +* This is the inverse of BPF_CORE_WRITE_BITFIELD(). +*/ +#define BPF_CORE_WRITE_BITFIELD (s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned int rpad = rshift - lshift; \ + unsigned long long nval, mask, val; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \
tools/lib/bpf/bpf_core_read.h enum bpf_enum_value_kind { · line 138
Quoted 10 lines
tools/lib/bpf/bpf_core_read.h +10
-0+case 1 : val = *(unsigned char *)p;break ; \ +case 2 : val = *(unsigned short *)p;break ; \ +case 4 : val = *(unsigned int *)p;break ; \ +case 8 : val = *(unsigned long long *)p;break ; \ + } \ + \ + mask = (~0ULL << rshift) >> lshift; \ + nval = new_val; \ + nval = (nval << rpad) & mask; \ + val = (val & ~mask) | nval; \
I'd simplify it to not need nval at all
val = (val & ~mask) | ((new_val << rpad) & mask);
I actually find it easier to follow and make sure we are not doing anything unexpected. First part before |, we take old value and clear bits we are about to set, second part after |, we take bitfield value, shift it in position, and just in case mask it out if it's too big to fit. Combine, done.
Other than that, it looks good.
Quoted 15 lines
tools/lib/bpf/bpf_core_read.h +9
-1+ \ +switch (byte_size) { \ +case 1 : *(unsigned char *)p = val;break ; \ +case 2 : *(unsigned short *)p = val;break ; \ +case 4 : *(unsigned int *)p = val;break ; \ +case 8 : *(unsigned long long *)p = val;break ; \ + } \ +}) +#define ___bpf_field_ref1 (field) (field)#define ___bpf_field_ref2 (type, field) (((typeof(type) *)0)->field) #define ___bpf_field_ref(args...) \ --2.42.1
Re: [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
Quoted 26 lines · Andrii Nakryiko
Quoted 25 lines · Daniel Xu
=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming
preserve_static_offset[0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated
vmlinux.h. It's also not generally available in released LLVM versions yet.(2) makes the code quite hard to read and write. And especially if
BPF_CORE_READ_BITFIELD()is already being used, it makes more sense to to have an inverse helper for writing.=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
Quoted 8 lines
Quoted 6 lines
To start, it helps by explaining what
LSHIFT_U64(lshift) andRSHIFT_U64(rshift) is designed to mean. Consider the core of theBPF_CORE_READ_BITFIELD()algorithm:val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64);nit: indentation is off?
Oops, it's cuz I only deleted the SIGNED check. Will fix.
Quoted 37 lines
Quoted 37 lines
Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXXwhere
.means blank bit,0means 0 bit, andXmeans bitfield bit.After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift)
Quoted 5 lines
Quoted 3 lines
That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Nice diagrams and description, thanks!
Thanks!
Quoted 64 lines
Quoted 49 lines
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.[0]: https://reviews.llvm.org/D133361
Co-authored-by Eduard Zingerman <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>Co-authored-by Jonathan Lemon <[email protected]>Signed-off-by Jonathan Lemon <[email protected]>Signed-off-by Daniel Xu <[email protected]>--- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)tools/lib/bpf/bpf_core_read.h +15
-0@@ -111,6 +111,40 @@ enum bpf_enum_value_kind { val; \ }) +/* +* Write to a bitfield, identified by s->field. +* This is the inverse of BPF_CORE_WRITE_BITFIELD(). +*/ +#define BPF_CORE_WRITE_BITFIELD (s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned int rpad = rshift - lshift; \ + unsigned long long nval, mask, val; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \tools/lib/bpf/bpf_core_read.h enum bpf_enum_value_kind { · line 138
Quoted 10 lines
tools/lib/bpf/bpf_core_read.h +10
-0+case 1 : val = *(unsigned char *)p;break ; \ +case 2 : val = *(unsigned short *)p;break ; \ +case 4 : val = *(unsigned int *)p;break ; \ +case 8 : val = *(unsigned long long *)p;break ; \ + } \ + \ + mask = (~0ULL << rshift) >> lshift; \ + nval = new_val; \ + nval = (nval << rpad) & mask; \ + val = (val & ~mask) | nval; \I'd simplify it to not need nval at all
val = (val & ~mask) | ((
new_val<< rpad) & mask);
Quoted 7 lines
I actually find it easier to follow and make sure we are not doing anything unexpected. First part before |, we take old value and clear bits we are about to set, second part after |, we take bitfield value, shift it in position, and just in case mask it out if it's too big to fit. Combine, done.
Other than that, it looks good.
I mostly left it there for the cast. Cuz injecting the unsigned long long cast made the line really long. How about this instead?
tools/lib/bpf/bpf_core_read.h +2 -4
@@ -120,8 +120,8 @@ enum bpf_enum_value_kind {unsigned int byte_size =__CORE_RELO (s, field,BYTE_SIZE ); \unsigned int lshift =__CORE_RELO (s, field,LSHIFT_U64 ); \unsigned int rshift =__CORE_RELO (s, field,RSHIFT_U64 ); \ +unsigned long long mask, val, nval = new_val; \unsigned int rpad = rshift - lshift; \ -unsigned long long nval, mask, val; \ \ asmvolatile ("" :"+r" (p)); \ \
@@ -133,9 +133,7 @@ enum bpf_enum_value_kind { } \ \ mask = (~0ULL << rshift) >> lshift; \ - nval = new_val; \ - nval = (nval << rpad) & mask; \ - val = (val & ~mask) | nval; \ + val = (val & ~mask) | ((nval << rpad) & mask); \ \switch (byte_size) { \case 1 : *(unsigned char *)p = val;break ; \
Thanks,
Daniel
Re: [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro
Quoted 173 lines · Daniel Xu
Quoted 26 lines · Andrii Nakryiko
Quoted 25 lines · Daniel Xu
=== Motivation ===
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy.
Two alternatives to this approach are:
1. Use the upcoming
preserve_static_offset[0] attribute to disable CO-RE on specific structs.
2. Use broader byte-sized writes to write to bitfields.(1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated
vmlinux.h. It's also not generally available in released LLVM versions yet.(2) makes the code quite hard to read and write. And especially if
BPF_CORE_READ_BITFIELD()is already being used, it makes more sense to to have an inverse helper for writing.=== Implementation details ===
Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on.
Quoted 8 lines
Quoted 6 lines
To start, it helps by explaining what
LSHIFT_U64(lshift) andRSHIFT_U64(rshift) is designed to mean. Consider the core of theBPF_CORE_READ_BITFIELD()algorithm:val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64);nit: indentation is off?
Oops, it's cuz I only deleted the SIGNED check. Will fix.
Quoted 37 lines
Quoted 37 lines
Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate:
Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXXwhere
.means blank bit,0means 0 bit, andXmeans bitfield bit.After the two operations, the bitfield is ready to be interpreted as a regular integer.
Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider:
* rshift as the total number of blank bits in the u64
* lshift as number of blank bits left of the bitfield in the u64Take a moment to consider why that is true by consulting the above diagram.
With this insight, we can how define the following relationship:
bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift)Quoted 5 lines
Quoted 3 lines
That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift).
Nice diagrams and description, thanks!
Thanks!
Quoted 64 lines
Quoted 49 lines
Finally, we can examine the core of the write side algorithm:
mask = (~0ULL << rshift) >> lshift; // 1 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4(1): Compute a mask where the set bits are the bitfield bits. The first
left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits.
(2): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits.[0]: https://reviews.llvm.org/D133361
Co-authored-by Eduard Zingerman <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>Co-authored-by Jonathan Lemon <[email protected]>Signed-off-by Jonathan Lemon <[email protected]>Signed-off-by Daniel Xu <[email protected]>--- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)tools/lib/bpf/bpf_core_read.h +15
-0@@ -111,6 +111,40 @@ enum bpf_enum_value_kind { val; \ }) +/* +* Write to a bitfield, identified by s->field. +* This is the inverse of BPF_CORE_WRITE_BITFIELD(). +*/ +#define BPF_CORE_WRITE_BITFIELD (s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned int rpad = rshift - lshift; \ + unsigned long long nval, mask, val; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \tools/lib/bpf/bpf_core_read.h enum bpf_enum_value_kind { · line 138
Quoted 10 lines
tools/lib/bpf/bpf_core_read.h +10
-0+case 1 : val = *(unsigned char *)p;break ; \ +case 2 : val = *(unsigned short *)p;break ; \ +case 4 : val = *(unsigned int *)p;break ; \ +case 8 : val = *(unsigned long long *)p;break ; \ + } \ + \ + mask = (~0ULL << rshift) >> lshift; \ + nval = new_val; \ + nval = (nval << rpad) & mask; \ + val = (val & ~mask) | nval; \I'd simplify it to not need nval at all
val = (val & ~mask) | ((
new_val<< rpad) & mask);Quoted 7 lines
I actually find it easier to follow and make sure we are not doing anything unexpected. First part before |, we take old value and clear bits we are about to set, second part after |, we take bitfield value, shift it in position, and just in case mask it out if it's too big to fit. Combine, done.
Other than that, it looks good.
I mostly left it there for the cast. Cuz injecting the
unsigned long longcast made the line really long. How about this instead?tools/lib/bpf/bpf_core_read.h +1
-1@@ -120,8 +120,8 @@ enum bpf_enum_value_kind {unsigned int byte_size =__CORE_RELO (s, field,BYTE_SIZE ); \unsigned int lshift =__CORE_RELO (s, field,LSHIFT_U64 ); \unsigned int rshift =__CORE_RELO (s, field,RSHIFT_U64 ); \ +unsigned long long mask, val, nval = new_val; \unsigned int rpad = rshift - lshift; \ -unsigned long long nval, mask, val; \ \ asmvolatile ("" :"+r" (p)); \ \
tools/lib/bpf/bpf_core_read.h enum bpf_enum_value_kind { · line 136
Quoted 7 lines
tools/lib/bpf/bpf_core_read.h +1
-3@@ -133,9 +133,7 @@ enum bpf_enum_value_kind { } \ \ mask = (~0ULL << rshift) >> lshift; \ - nval = new_val; \ - nval = (nval << rpad) & mask; \ - val = (val & ~mask) | nval; \ + val = (val & ~mask) | ((nval << rpad) & mask); \
sgtm
Quoted 7 lines
tools/lib/bpf/bpf_core_read.h +0
-0\switch (byte_size) { \case 1 : *(unsigned char *)p = val;break ; \Thanks,
Daniel
PATCH v3 4/9 bpf: selftests: test_loader: Support __btf_path() annotation
In reply to Daniel XuThis commit adds support for per-prog btf_custom_path. This is necessary for testing CO-RE relocations on non-vmlinux types using test_loader infrastructure.
--- tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + tools/testing/selftests/bpf/test_loader.c | 7 +++++++ 2 files changed, 8 insertions(+)
tools/testing/selftests/bpf/progs/bpf_misc.h +1 -0
@@ -71,6 +71,7 @@#define __retval_unpriv (val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))#define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary")))#define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv"))) +#define __btf_path (path) __attribute__((btf_decl_tag("comment:test_btf_path=" path)))/* Convenience macro for use with 'asm volatile' blocks */ #define __naked __attribute__((naked))
tools/testing/selftests/bpf/test_loader.c +7 -0
@@ -27,6 +27,7 @@#define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv="#define TEST_TAG_AUXILIARY "comment:test_auxiliary"#define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv" +#define TEST_BTF_PATH "comment:test_btf_path="/* Warning: duplicated in bpf_misc.h */ #define POINTER_VALUE 0xcafe4all
@@ -58,6 +59,7 @@ struct test_spec {const char *prog_name;struct test_subspec priv;struct test_subspec unpriv; +const char *btf_custom_path;int log_level;int prog_flags;int mode_mask;
@@ -288,6 +290,8 @@ static int parse_test_spec(struct test_loader *tester, goto cleanup;update_flags (&spec->prog_flags, flags, clear); } + }else if (str_has_pfx (s,TEST_BTF_PATH )) { + spec->btf_custom_path = s +sizeof (TEST_BTF_PATH ) -1 ; } }
@@ -578,6 +582,9 @@ void run_subtest(struct test_loader *tester, } } +/* Implicitly reset to NULL if next test case doesn't specify */ + open_opts->btf_custom_path = spec->btf_custom_path; + tobj =bpf_object__open_mem (obj_bytes, obj_byte_cnt, open_opts);if (!ASSERT_OK_PTR (tobj,"obj_open_mem" ))/* shouldn't happen */ goto subtest_cleanup;
Signature · 1 line
2.42.1
Re: [PATCH ipsec-next v3 4/9] bpf: selftests: test_loader: Support __btf_path() annotation
Quoted 11 lines · Daniel Xu
This commit adds support for per-prog
btf_custom_path. This is necessary for testing CO-RE relocations on non-vmlinux types usingtest_loaderinfrastructure.Signed-off-by Daniel Xu <[email protected]>--- tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + tools/testing/selftests/bpf/test_loader.c | 7 +++++++ 2 files changed, 8 insertions(+)
Quoted 54 lines
tools/testing/selftests/bpf/progs/bpf_misc.h +1
-0@@ -71,6 +71,7 @@#define __retval_unpriv (val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val)))#define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary")))#define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv"))) +#define __btf_path (path) __attribute__((btf_decl_tag("comment:test_btf_path=" path)))/* Convenience macro for use with 'asm volatile' blocks */ #define __naked __attribute__((naked))tools/testing/selftests/bpf/test_loader.c +7
-1@@ -27,6 +27,7 @@#define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv="#define TEST_TAG_AUXILIARY "comment:test_auxiliary"#define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv" +#define TEST_BTF_PATH "comment:test_btf_path="/* Warning: duplicated in bpf_misc.h */ #define POINTER_VALUE 0xcafe4all@@ -58,6 +59,7 @@ struct test_spec {const char *prog_name;struct test_subspec priv;struct test_subspec unpriv; +const char *btf_custom_path;int log_level;int prog_flags;int mode_mask;@@ -288,6 +290,8 @@ static int parse_test_spec(struct test_loader *tester, goto cleanup;update_flags (&spec->prog_flags, flags, clear); } + }else if (str_has_pfx (s,TEST_BTF_PATH )) { + spec->btf_custom_path = s +sizeof (TEST_BTF_PATH ) -1 ; } }@@ -578,6 +582,9 @@ void run_subtest(struct test_loader *tester, } } +/* Implicitly reset to NULL if next test case doesn't specify */ + open_opts->btf_custom_path = spec->btf_custom_path; + tobj =bpf_object__open_mem (obj_bytes, obj_byte_cnt, open_opts);if (!ASSERT_OK_PTR (tobj,"obj_open_mem" ))/* shouldn't happen */ goto subtest_cleanup; --2.42.1
PATCH v3 5/9 libbpf: selftests: Add verifier tests for CO-RE bitfield writes
In reply to Daniel XuAdd some tests that exercise BPF_CORE_WRITE_BITFIELD() macro. Since some non-trivial bit fiddling is going on, make sure various edge cases (such as adjacent bitfields and bitfields at the edge of structs) are exercised.
--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_bitfield_write.c | 100 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_bitfield_write.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -6,6 +6,7 @@#include "verifier_and.skel.h" #include "verifier_array_access.skel.h" #include "verifier_basic_stack.skel.h" +#include "verifier_bitfield_write.skel.h" #include "verifier_bounds.skel.h" #include "verifier_bounds_deduction.skel.h" #include "verifier_bounds_deduction_non_const.skel.h"
@@ -115,6 +116,7 @@ static void run_tests_aux(const char *skel_name,void test_verifier_and (void ) {RUN (verifier_and); }void test_verifier_basic_stack (void ) {RUN (verifier_basic_stack); } +void test_verifier_bitfield_write (void ) {RUN (verifier_bitfield_write); }void test_verifier_bounds (void ) {RUN (verifier_bounds); }void test_verifier_bounds_deduction (void ) {RUN (verifier_bounds_deduction); }void test_verifier_bounds_deduction_non_const (void ) {RUN (verifier_bounds_deduction_non_const); }
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.42.1
Re: [PATCH ipsec-next v3 5/9] libbpf: selftests: Add verifier tests for CO-RE bitfield writes
Quoted 6 lines · Daniel Xu
Add some tests that exercise
BPF_CORE_WRITE_BITFIELD()macro. Since some non-trivial bit fiddling is going on, make sure various edge cases (such as adjacent bitfields and bitfields at the edge of structs) are exercised.
Quoted 7 lines
Signed-off-by Daniel Xu <[email protected]>--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_bitfield_write.c | 100 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_bitfield_write.c
LGTM, but I'm not sure why we need all those __failure_unpriv, see below. Regardless:
Quoted 43 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +2
-0@@ -6,6 +6,7 @@#include "verifier_and.skel.h" #include "verifier_array_access.skel.h" #include "verifier_basic_stack.skel.h" +#include "verifier_bitfield_write.skel.h" #include "verifier_bounds.skel.h" #include "verifier_bounds_deduction.skel.h" #include "verifier_bounds_deduction_non_const.skel.h" @@ -115,6 +116,7 @@ static void run_tests_aux(const char *skel_name,void test_verifier_and (void ) {RUN (verifier_and); }void test_verifier_basic_stack (void ) {RUN (verifier_basic_stack); } +void test_verifier_bitfield_write (void ) {RUN (verifier_bitfield_write); }void test_verifier_bounds (void ) {RUN (verifier_bounds); }void test_verifier_bounds_deduction (void ) {RUN (verifier_bounds_deduction); }void test_verifier_bounds_deduction_non_const (void ) {RUN (verifier_bounds_deduction_non_const); }tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +17
-0@@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <stdint.h> + +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> + +#include "bpf_misc.h" + +struct core_reloc_bitfields { +/* unsigned bitfields */ +uint8_t ub1:1 ; +uint8_t ub2:2 ; +uint32_t ub7:7 ; +/* signed bitfields */ +int8_t sb4:4 ;
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c line 27
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +10
-0+ int32_t sb20: 20; +/* non-bitfields */ +uint32_t u32; +int32_t s32; +} __attribute__((preserve_access_index)); + +SEC("tc") +__description("single CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success __failure_unpriv
do we want __failure_unpriv at all? Is this failure related to *bitfield* logic at all?
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +10
-0+__retval(3) +int single_field_roundtrip (struct __sk_buff *ctx) +{ +struct core_reloc_bitfields bitfields; + +__builtin_memset (&bitfields,0 ,sizeof (bitfields)); +BPF_CORE_WRITE_BITFIELD (&bitfields, ub2,3 ); +return BPF_CORE_READ_BITFIELD (&bitfields, ub2); +} +
[...]
Re: [PATCH ipsec-next v3 5/9] libbpf: selftests: Add verifier tests for CO-RE bitfield writes
Hi Andrii,
Quoted 68 lines · Andrii Nakryiko
Quoted 6 lines · Daniel Xu
Add some tests that exercise
BPF_CORE_WRITE_BITFIELD()macro. Since some non-trivial bit fiddling is going on, make sure various edge cases (such as adjacent bitfields and bitfields at the edge of structs) are exercised.Quoted 7 lines
Signed-off-by Daniel Xu <[email protected]>--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_bitfield_write.c | 100 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_bitfield_write.cLGTM, but I'm not sure why we need all those
__failure_unpriv, see below. Regardless:Acked-by Andrii Nakryiko <[email protected]>Quoted 48 lines
tools/testing/selftests/bpf/prog_tests/verifier.c +2
-0@@ -6,6 +6,7 @@#include "verifier_and.skel.h" #include "verifier_array_access.skel.h" #include "verifier_basic_stack.skel.h" +#include "verifier_bitfield_write.skel.h" #include "verifier_bounds.skel.h" #include "verifier_bounds_deduction.skel.h" #include "verifier_bounds_deduction_non_const.skel.h" @@ -115,6 +116,7 @@ static void run_tests_aux(const char *skel_name,void test_verifier_and (void ) {RUN (verifier_and); }void test_verifier_basic_stack (void ) {RUN (verifier_basic_stack); } +void test_verifier_bitfield_write (void ) {RUN (verifier_bitfield_write); }void test_verifier_bounds (void ) {RUN (verifier_bounds); }void test_verifier_bounds_deduction (void ) {RUN (verifier_bounds_deduction); }void test_verifier_bounds_deduction_non_const (void ) {RUN (verifier_bounds_deduction_non_const); }tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +22
-0@@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <stdint.h> + +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> + +#include "bpf_misc.h" + +struct core_reloc_bitfields { +/* unsigned bitfields */ +uint8_t ub1:1 ; +uint8_t ub2:2 ; +uint32_t ub7:7 ; +/* signed bitfields */ +int8_t sb4:4 ; +int32_t sb20:20 ; +/* non-bitfields */ +uint32_t u32; +int32_t s32; +} __attribute__((preserve_access_index));
Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c line 27
Quoted 5 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +5
-0+ +SEC("tc") +__description("single CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success __failure_unprivdo we want
__failure_unprivat all? Is this failure related to *bitfield* logic at all?
Oh, I pre-emptively added it. From the docs, I thought __failure_unpriv meant "don't try to load this as an unprivileged used cuz it'll fail". And since I used the tc hook, I figured it'd fail.
Removing the annotation doesn't seem to do anything bad so I'll drop it for v4.
[...]
Thanks,
Daniel
Eduard Zingerman
maintainerRe: [PATCH ipsec-next v3 5/9] libbpf: selftests: Add verifier tests for CO-RE bitfield writes
On Fri, 2023-12-01 at 17:10 -0700, Daniel Xu wrote:
[...]
Quoted 11 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c line 27
Quoted 4 lines
tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file +4
-0+SEC("tc") +__description("single CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success __failure_unprivdo we want
__failure_unprivat all? Is this failure related to *bitfield* logic at all?Oh, I pre-emptively added it. From the docs, I thought
__failure_unprivmeant "don't try to load this as an unprivileged used cuz it'll fail". And since I used the tc hook, I figured it'd fail.
Actually it means:
"try to load as unprivileged user and expect failure,
report error on successful load".
In general, the meaning of "___xxx" and "___xxx_unpriv" annotations is identical, except first instructs to run the test in privileged mode, while second instructs to run test in unprivileged mode:
- if only annotations w/o "*_unpriv" suffix are present the test would be executed as privileged;
- if only annotations with "*_unpriv" suffix are present the test would be executed as unprivileged;
- if both kinds of annotations are present the test would be executed in both modes.
[...]
PATCH v3 6/9 bpf: selftests: test_tunnel: Setup fresh topology for each subtest
In reply to Daniel XuThis helps with determinism b/c individual setup/teardown prevents leaking state between different subtests.
--- tools/testing/selftests/bpf/prog_tests/test_tunnel.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +2 -5
@@ -535,23 +535,20 @@ static void test_ipip_tunnel(enum ipip_encap encap)#define RUN_TEST (name, ...) \ ({ \ if (test__start_subtest(#name)) { \ + config_device(); \ test_ ## name(__VA_ARGS__); \ + cleanup(); \ } \ })static void *test_tunnel_run_tests (void *arg) { -cleanup (); -config_device (); -RUN_TEST (vxlan_tunnel);RUN_TEST (ip6vxlan_tunnel);RUN_TEST (ipip_tunnel,NONE );RUN_TEST (ipip_tunnel,FOU );RUN_TEST (ipip_tunnel,GUE ); -cleanup (); -return NULL ; }
Signature · 1 line
2.42.1
PATCH v3 7/9 bpf: selftests: test_tunnel: Use vmlinux.h declarations
In reply to Daniel Xuvmlinux.h declarations are more ergnomic, especially when working with kfuncs. The uapi headers are often incomplete for kfunc definitions.
This commit also switches bitfield accesses to use CO-RE helpers. Switching to vmlinux.h definitions makes the verifier very unhappy with raw bitfield accesses. The error is:
; md.u.md2.dir = direction;
33: (69) r1 = *(u16 *)(r2 +11)
misaligned stack access off (0x0; 0x0)+-64+11 size 2Fix by using CO-RE-aware bitfield reads and writes.
--- .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 76 +++++-------------- 2 files changed, 22 insertions(+), 55 deletions(-)
tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1 -0
@@ -26,6 +26,7 @@#define IPV6_AUTOFLOWLABEL 70#define TC_ACT_UNSPEC (-1) +#define TC_ACT_OK 0#define TC_ACT_SHOT 2#define SOL_TCP 6
tools/testing/selftests/bpf/progs/test_tunnel_kern.c +21 -55
117 lines not on this page · raw message
Signature · 1 line
2.42.1
PATCH v3 8/9 bpf: selftests: Move xfrm tunnel test to test_progs
In reply to Daniel Xutest_progs is better than a shell script b/c C is a bit easier to maintain than shell. Also it's easier to use new infra like memory mapped global variables from C via bpf skeleton.
--- .../selftests/bpf/prog_tests/test_tunnel.c | 143 ++++++++++++++++++ .../selftests/bpf/progs/test_tunnel_kern.c | 11 +- tools/testing/selftests/bpf/test_tunnel.sh | 92 ----------- 3 files changed, 151 insertions(+), 95 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +143 -0
@@ -50,6 +50,7 @@*/ #include <arpa/inet.h> +#include <linux/if_link.h> #include <linux/if_tun.h> #include <linux/limits.h> #include <linux/sysctl.h>
@@ -92,6 +93,11 @@#define IPIP_TUNL_DEV0 "ipip00"#define IPIP_TUNL_DEV1 "ipip11" +#define XFRM_AUTH "0x1111111111111111111111111111111111111111" +#define XFRM_ENC "0x22222222222222222222222222222222" +#define XFRM_SPI_IN_TO_OUT 0x1 +#define XFRM_SPI_OUT_TO_IN 0x2 +#define PING_ARGS "-i 0.01 -c 3 -w 10 -q"static int config_device (void )
@@ -264,6 +270,92 @@ static void delete_ipip_tunnel(void)SYS_NOFAIL ("ip fou del port 5555 2> /dev/null" ); } +static int add_xfrm_tunnel (void ) +{ +/* at_ns0 namespace +* at_ns0 -> root +*/ +SYS (fail, +"ip netns exec at_ns0 " +"ip xfrm state add src %s dst %s proto esp " +"spi %d reqid 1 mode tunnel " +"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" , +IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ,XFRM_SPI_IN_TO_OUT ,XFRM_AUTH ,XFRM_ENC ); +SYS (fail, +"ip netns exec at_ns0 " +"ip xfrm policy add src %s/32 dst %s/32 dir out " +"tmpl src %s dst %s proto esp reqid 1 " +"mode tunnel" , +IP4_ADDR_TUNL_DEV0 ,IP4_ADDR_TUNL_DEV1 ,IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ); + +/* root -> at_ns0 */ +SYS (fail, +"ip netns exec at_ns0 " +"ip xfrm state add src %s dst %s proto esp " +"spi %d reqid 2 mode tunnel " +"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" , +IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ,XFRM_SPI_OUT_TO_IN ,XFRM_AUTH ,XFRM_ENC ); +SYS (fail, +"ip netns exec at_ns0 " +"ip xfrm policy add src %s/32 dst %s/32 dir in " +"tmpl src %s dst %s proto esp reqid 2 " +"mode tunnel" , +IP4_ADDR_TUNL_DEV1 ,IP4_ADDR_TUNL_DEV0 ,IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ); + +/* address & route */ +SYS (fail,"ip netns exec at_ns0 ip addr add dev veth0 %s/32" , +IP4_ADDR_TUNL_DEV0 ); +SYS (fail,"ip netns exec at_ns0 ip route add %s dev veth0 via %s src %s" , +IP4_ADDR_TUNL_DEV1 ,IP4_ADDR1_VETH1 ,IP4_ADDR_TUNL_DEV0 ); + +/* root namespace +* at_ns0 -> root +*/ +SYS (fail, +"ip xfrm state add src %s dst %s proto esp " +"spi %d reqid 1 mode tunnel " +"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" , +IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ,XFRM_SPI_IN_TO_OUT ,XFRM_AUTH ,XFRM_ENC ); +SYS (fail, +"ip xfrm policy add src %s/32 dst %s/32 dir in " +"tmpl src %s dst %s proto esp reqid 1 " +"mode tunnel" , +IP4_ADDR_TUNL_DEV0 ,IP4_ADDR_TUNL_DEV1 ,IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ); + +/* root -> at_ns0 */ +SYS (fail, +"ip xfrm state add src %s dst %s proto esp " +"spi %d reqid 2 mode tunnel " +"auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" , +IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ,XFRM_SPI_OUT_TO_IN ,XFRM_AUTH ,XFRM_ENC ); +SYS (fail, +"ip xfrm policy add src %s/32 dst %s/32 dir out " +"tmpl src %s dst %s proto esp reqid 2 " +"mode tunnel" , +IP4_ADDR_TUNL_DEV1 ,IP4_ADDR_TUNL_DEV0 ,IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ); + +/* address & route */ +SYS (fail,"ip addr add dev veth1 %s/32" ,IP4_ADDR_TUNL_DEV1 ); +SYS (fail,"ip route add %s dev veth1 via %s src %s" , +IP4_ADDR_TUNL_DEV0 ,IP4_ADDR_VETH0 ,IP4_ADDR_TUNL_DEV1 ); + +return 0 ; +fail: +return -1 ; +} + +static void delete_xfrm_tunnel (void ) +{ +SYS_NOFAIL ("ip xfrm policy delete dir out src %s/32 dst %s/32 2> /dev/null" , +IP4_ADDR_TUNL_DEV1 ,IP4_ADDR_TUNL_DEV0 ); +SYS_NOFAIL ("ip xfrm policy delete dir in src %s/32 dst %s/32 2> /dev/null" , +IP4_ADDR_TUNL_DEV0 ,IP4_ADDR_TUNL_DEV1 ); +SYS_NOFAIL ("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null" , +IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ,XFRM_SPI_IN_TO_OUT ); +SYS_NOFAIL ("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null" , +IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ,XFRM_SPI_OUT_TO_IN ); +} +static int test_ping (int family,const char *addr) {SYS (fail,"%s %s %s > /dev/null" ,ping_command (family),PING_ARGS , addr);
@@ -532,6 +624,56 @@ static void test_ipip_tunnel(enum ipip_encap encap)test_tunnel_kern__destroy (skel); } +static void test_xfrm_tunnel (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_tc_hook, tc_hook, + .attach_point = BPF_TC_INGRESS); +struct test_tunnel_kern *skel =NULL ; +struct nstoken *nstoken; +int tc_prog_fd; +int ifindex; +int err; + + err =add_xfrm_tunnel (); +if (!ASSERT_OK (err,"add_xfrm_tunnel" )) +return ; + + skel =test_tunnel_kern__open_and_load (); +if (!ASSERT_OK_PTR (skel,"test_tunnel_kern__open_and_load" )) + goto done; + + ifindex =if_nametoindex ("veth1" ); +if (!ASSERT_NEQ (ifindex,0 ,"veth1 ifindex" )) + goto done; + +/* attach tc prog to tunnel dev */ + tc_hook.ifindex = ifindex; + tc_prog_fd =bpf_program__fd (skel->progs.xfrm_get_state); +if (!ASSERT_GE (tc_prog_fd,0 ,"bpf_program__fd" )) + goto done; +if (attach_tc_prog (&tc_hook, tc_prog_fd,-1 )) + goto done; + +/* ping from at_ns0 namespace test */ + nstoken =open_netns ("at_ns0" ); + err =test_ping (AF_INET ,IP4_ADDR_TUNL_DEV1 ); +close_netns (nstoken); +if (!ASSERT_OK (err,"test_ping" )) + goto done; + +if (!ASSERT_EQ (skel->bss->xfrm_reqid,1 ,"req_id" )) + goto done; +if (!ASSERT_EQ (skel->bss->xfrm_spi,XFRM_SPI_IN_TO_OUT ,"spi" )) + goto done; +if (!ASSERT_EQ (skel->bss->xfrm_remote_ip,0xac100164 ,"remote_ip" )) + goto done; + +done: +delete_xfrm_tunnel (); +if (skel) +test_tunnel_kern__destroy (skel); +} +#define RUN_TEST (name, ...) \ ({ \ if (test__start_subtest(#name)) { \
@@ -548,6 +690,7 @@ static void *test_tunnel_run_tests(void *arg)RUN_TEST (ipip_tunnel,NONE );RUN_TEST (ipip_tunnel,FOU );RUN_TEST (ipip_tunnel,GUE ); +RUN_TEST (xfrm_tunnel);return NULL ; }
tools/testing/selftests/bpf/progs/test_tunnel_kern.c +8 -3
@@ -929,6 +929,10 @@ int ip6ip6_get_tunnel(struct __sk_buff *skb)return TC_ACT_OK ; } +volatile int xfrm_reqid =0 ; +volatile int xfrm_spi =0 ; +volatile int xfrm_remote_ip =0 ; + SEC("tc")int xfrm_get_state (struct __sk_buff *skb) {
@@ -939,9 +943,10 @@ int xfrm_get_state(struct __sk_buff *skb)if (ret <0 )return TC_ACT_OK ; -bpf_printk ("reqid %d spi 0x%x remote ip 0x%x\n" , - x.reqid,bpf_ntohl (x.spi), -bpf_ntohl (x.remote_ipv4)); + xfrm_reqid = x.reqid; + xfrm_spi =bpf_ntohl (x.spi); + xfrm_remote_ip =bpf_ntohl (x.remote_ipv4); +return TC_ACT_OK ; }
tools/testing/selftests/bpf/test_tunnel.sh +0 -92
110 lines not on this page · raw message
Signature · 1 line
2.42.1
PATCH v3 9/9 bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state()
In reply to Daniel XuThis commit extends test_tunnel selftest to test the new XDP xfrm state lookup kfunc.
--- .../selftests/bpf/prog_tests/test_tunnel.c | 20 ++++++-- .../selftests/bpf/progs/test_tunnel_kern.c | 51 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_tunnel.c +16 -4
@@ -278,7 +278,7 @@ static int add_xfrm_tunnel(void)SYS (fail,"ip netns exec at_ns0 " "ip xfrm state add src %s dst %s proto esp " -"spi %d reqid 1 mode tunnel " +"spi %d reqid 1 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" ,IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ,XFRM_SPI_IN_TO_OUT ,XFRM_AUTH ,XFRM_ENC ); SYS(fail,
@@ -292,7 +292,7 @@ static int add_xfrm_tunnel(void)SYS (fail,"ip netns exec at_ns0 " "ip xfrm state add src %s dst %s proto esp " -"spi %d reqid 2 mode tunnel " +"spi %d reqid 2 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" ,IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ,XFRM_SPI_OUT_TO_IN ,XFRM_AUTH ,XFRM_ENC ); SYS(fail,
@@ -313,7 +313,7 @@ static int add_xfrm_tunnel(void)*/ SYS (fail,"ip xfrm state add src %s dst %s proto esp " -"spi %d reqid 1 mode tunnel " +"spi %d reqid 1 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" ,IP4_ADDR_VETH0 ,IP4_ADDR1_VETH1 ,XFRM_SPI_IN_TO_OUT ,XFRM_AUTH ,XFRM_ENC ); SYS(fail,
@@ -325,7 +325,7 @@ static int add_xfrm_tunnel(void)/* root -> at_ns0 */ SYS (fail,"ip xfrm state add src %s dst %s proto esp " -"spi %d reqid 2 mode tunnel " +"spi %d reqid 2 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s" ,IP4_ADDR1_VETH1 ,IP4_ADDR_VETH0 ,XFRM_SPI_OUT_TO_IN ,XFRM_AUTH ,XFRM_ENC ); SYS(fail,
@@ -628,8 +628,10 @@ static void test_xfrm_tunnel(void) {DECLARE_LIBBPF_OPTS (bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS); +LIBBPF_OPTS (bpf_xdp_attach_opts, opts);struct test_tunnel_kern *skel =NULL ;struct nstoken *nstoken; +int xdp_prog_fd;int tc_prog_fd;int ifindex;int err;
@@ -654,6 +656,14 @@ static void test_xfrm_tunnel(void)if (attach_tc_prog (&tc_hook, tc_prog_fd,-1 )) goto done; +/* attach xdp prog to tunnel dev */ + xdp_prog_fd =bpf_program__fd (skel->progs.xfrm_get_state_xdp); +if (!ASSERT_GE (xdp_prog_fd,0 ,"bpf_program__fd" )) + goto done; + err =bpf_xdp_attach (ifindex, xdp_prog_fd,XDP_FLAGS_REPLACE , &opts); +if (!ASSERT_OK (err,"bpf_xdp_attach" )) + goto done; +/* ping from at_ns0 namespace test */ nstoken =open_netns ("at_ns0" ); err =test_ping (AF_INET ,IP4_ADDR_TUNL_DEV1 );
@@ -667,6 +677,8 @@ static void test_xfrm_tunnel(void) goto done;if (!ASSERT_EQ (skel->bss->xfrm_remote_ip,0xac100164 ,"remote_ip" )) goto done; +if (!ASSERT_EQ (skel->bss->xfrm_replay_window,42 ,"replay_window" )) + goto done; done:delete_xfrm_tunnel ();
tools/testing/selftests/bpf/progs/test_tunnel_kern.c +51 -0
61 lines not on this page · raw message
Signature · 1 line
2.42.1
Quoted 48 lines · Daniel Xu
This patchset adds two kfunc helpers,
bpf_xdp_get_xfrm_state()andbpf_xdp_xfrm_state_release()that wrapxfrm_state_lookup()andxfrm_state_put(). The intent is to support software RSS (via XDP) for the ongoing/upcoming ipsec pcpu work [0]. Recent experiments performed on (hopefully) reproducible AWS testbeds indicate that single tunnel pcpu ipsec can reach line rate on 100G ENA nics.Note this patchset only tests/shows generic
xfrm_stateaccess. The "secret sauce" (if you can really even call it that) involves accessing a soon-to-be-upstreamedpcpu_numfield inxfrm_state. Early example is available here [1].[0]: https://datatracker.ietf.org/doc/draft-ietf-ipsecme-multi-sa-performance/03/
[1]: https://github.com/danobi/xdp-tools/blob/e89a1c617aba3b50d990f779357d6ce2863ecb27/xdp-bench/xdp_redirect_cpumap.bpf.c#L385-L406Changes from v2:
* Fix/simplifyBPF_CORE_WRITE_BITFIELD()algorithm
* Added verifier tests for bitfield writes
* Fix state leakage acrosstest_tunnelsubtestsChanges from v1:
* Move xfrm tunnel tests totest_progs
* Fix writing toopts->errorwhen opts is invalid
* Use__bpf_kfunc_start_defs()
* Remove unused vxlanhdr definition
* Add and useBPF_CORE_WRITE_BITFIELD()macro
* Make series bisect cleanChanges from RFCv2:
* Rebased to ipsec-next
* Fix netns leakChanges from RFCv1:
* Add Antony's commit tags
* AddKF_ACQUIREandKF_RELEASEsemanticsDaniel Xu (9):
bpf: xfrm: Addbpf_xdp_get_xfrm_state()kfunc
bpf: xfrm: Addbpf_xdp_xfrm_state_release()kfunc
libbpf: AddBPF_CORE_WRITE_BITFIELD()macro
bpf: selftests:test_loader: Support__btf_path()annotation
libbpf: selftests: Add verifier tests for CO-RE bitfield writes
bpf: selftests:test_tunnel: Setup fresh topology for each subtest
bpf: selftests:test_tunnel: Usevmlinux.hdeclarations
bpf: selftests: Move xfrm tunnel test totest_progs
bpf: xfrm: Add selftest forbpf_xdp_get_xfrm_state()
Quoted 14 lines
include/net/xfrm.h | 9 + net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 128 ++++++++++++++ tools/lib/bpf/bpf_core_read.h | 34 ++++ .../selftests/bpf/prog_tests/test_tunnel.c | 162 +++++++++++++++++- .../selftests/bpf/prog_tests/verifier.c | 2 + tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 138 ++++++++------- .../bpf/progs/verifier_bitfield_write.c | 100 +++++++++++ tools/testing/selftests/bpf/test_loader.c | 7 + tools/testing/selftests/bpf/test_tunnel.sh | 92 ---------- 13 files changed, 522 insertions(+), 155 deletions(-)
I really think this should go via bpf-next tree.
The bpf changes are much bigger than ipsec.
Quoted 64 lines · Alexei Starovoitov
Quoted 48 lines · Daniel Xu
This patchset adds two kfunc helpers,
bpf_xdp_get_xfrm_state()andbpf_xdp_xfrm_state_release()that wrapxfrm_state_lookup()andxfrm_state_put(). The intent is to support software RSS (via XDP) for the ongoing/upcoming ipsec pcpu work [0]. Recent experiments performed on (hopefully) reproducible AWS testbeds indicate that single tunnel pcpu ipsec can reach line rate on 100G ENA nics.Note this patchset only tests/shows generic
xfrm_stateaccess. The "secret sauce" (if you can really even call it that) involves accessing a soon-to-be-upstreamedpcpu_numfield inxfrm_state. Early example is available here [1].[0]: https://datatracker.ietf.org/doc/draft-ietf-ipsecme-multi-sa-performance/03/
[1]: https://github.com/danobi/xdp-tools/blob/e89a1c617aba3b50d990f779357d6ce2863ecb27/xdp-bench/xdp_redirect_cpumap.bpf.c#L385-L406Changes from v2:
* Fix/simplifyBPF_CORE_WRITE_BITFIELD()algorithm
* Added verifier tests for bitfield writes
* Fix state leakage acrosstest_tunnelsubtestsChanges from v1:
* Move xfrm tunnel tests totest_progs
* Fix writing toopts->errorwhen opts is invalid
* Use__bpf_kfunc_start_defs()
* Remove unused vxlanhdr definition
* Add and useBPF_CORE_WRITE_BITFIELD()macro
* Make series bisect cleanChanges from RFCv2:
* Rebased to ipsec-next
* Fix netns leakChanges from RFCv1:
* Add Antony's commit tags
* AddKF_ACQUIREandKF_RELEASEsemanticsDaniel Xu (9):
bpf: xfrm: Addbpf_xdp_get_xfrm_state()kfunc
bpf: xfrm: Addbpf_xdp_xfrm_state_release()kfunc
libbpf: AddBPF_CORE_WRITE_BITFIELD()macro
bpf: selftests:test_loader: Support__btf_path()annotation
libbpf: selftests: Add verifier tests for CO-RE bitfield writes
bpf: selftests:test_tunnel: Setup fresh topology for each subtest
bpf: selftests:test_tunnel: Usevmlinux.hdeclarations
bpf: selftests: Move xfrm tunnel test totest_progs
bpf: xfrm: Add selftest forbpf_xdp_get_xfrm_state()Quoted 14 lines
include/net/xfrm.h | 9 + net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 128 ++++++++++++++ tools/lib/bpf/bpf_core_read.h | 34 ++++ .../selftests/bpf/prog_tests/test_tunnel.c | 162 +++++++++++++++++- .../selftests/bpf/prog_tests/verifier.c | 2 + tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 138 ++++++++------- .../bpf/progs/verifier_bitfield_write.c | 100 +++++++++++ tools/testing/selftests/bpf/test_loader.c | 7 + tools/testing/selftests/bpf/test_tunnel.sh | 92 ---------- 13 files changed, 522 insertions(+), 155 deletions(-)
Quoted 2 lines
I really think this should go via bpf-next tree.
The bpf changes are much bigger than ipsec.
Ack. Ended up picking up a lot of stuff along the way.
Steffen Klassert
maintainerQuoted 18 lines · Daniel Xu
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Daniel Xu
include/net/xfrm.h | 9 + net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 128 ++++++++++++++ tools/lib/bpf/bpf_core_read.h | 34 ++++ .../selftests/bpf/prog_tests/test_tunnel.c | 162 +++++++++++++++++- .../selftests/bpf/prog_tests/verifier.c | 2 + tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 138 ++++++++------- .../bpf/progs/verifier_bitfield_write.c | 100 +++++++++++ tools/testing/selftests/bpf/test_loader.c | 7 + tools/testing/selftests/bpf/test_tunnel.sh | 92 ---------- 13 files changed, 522 insertions(+), 155 deletions(-)
Quoted 4 lines
Quoted 2 lines
I really think this should go via bpf-next tree.
The bpf changes are much bigger than ipsec.Ack. Ended up picking up a lot of stuff along the way.
I'm fine with merging this via the bpf-next tree.
Please consider to merge the bpf hepler functions
to one file. We have already xfrm_interface_bpf.c
and now you introduce xfrm_state_bpf.c.
Try to merge this into a single xfrm_bpf.c file.