On 12/17/23 2:06 AM, Alexei Starovoitov wrote:
From: Alexei Starovoitov <ast@xxxxxxxxxx>
Compilers optimize conditional operators at will, but often bpf programmers
want to force compilers to keep the same operator in asm as it's written in C.
Introduce CMP(var1, conditional_op, var2) macro that can be used as:
- if (seen >= 1000)
+ if (CMP(seen, >=, 1000))
The macro takes advantage of BPF assembly that is C like.
The macro checks the sign of variable 'seen' and emits either
signed or unsigned compare.
For example:
int a;
CMP(a, >, 0) will be translted to 'if rX s> 0 goto' in BPF assembly.
nit: translted
unsigned int a;
CMP(a, >, 0) will be translted to 'if rX > 0 goto' in BPF assembly.
ditto
The right hand side isn't checked yet. The macro needs more safety checks.
Signed-off-by: Alexei Starovoitov <ast@xxxxxxxxxx>
---
As next step we can remove all of bpf_assert_ne|eq|... macros.
Also I'd like to remove bpf_assert_with. Open coding it is imo cleaner.
.../testing/selftests/bpf/bpf_experimental.h | 28 +++++++++++++++++++
.../testing/selftests/bpf/progs/exceptions.c | 20 ++++++-------
.../selftests/bpf/progs/iters_task_vma.c | 3 +-
3 files changed, 39 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index 1386baf9ae4a..a3248b086e4b 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -254,6 +254,34 @@ extern void bpf_throw(u64 cookie) __ksym;
} \
})
+#define __eauality(x) \
+ __builtin_strcmp(#x, "==") == 0 || __builtin_strcmp(#x, "!=") == 0
nit: s/__eauality/__equality/ ?
Otherwise, looks good:
Acked-by: Daniel Borkmann <daniel@xxxxxxxxxxxxx>