summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/bpf_misc.h
diff options
context:
space:
mode:
Diffstat (limited to 'tools/testing/selftests/bpf/progs/bpf_misc.h')
-rw-r--r--tools/testing/selftests/bpf/progs/bpf_misc.h109
1 files changed, 6 insertions, 103 deletions
diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h
index 6e3b4903c541..d3c1217ba79a 100644
--- a/tools/testing/selftests/bpf/progs/bpf_misc.h
+++ b/tools/testing/selftests/bpf/progs/bpf_misc.h
@@ -53,6 +53,10 @@
* - A numeric value.
* Multiple __flag attributes could be specified, the final flags
* value is derived by applying binary "or" to all specified values.
+ *
+ * __auxiliary Annotated program is not a separate test, but used as auxiliary
+ * for some other test cases and should always be loaded.
+ * __auxiliary_unpriv Same, but load program in unprivileged mode.
*/
#define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
#define __failure __attribute__((btf_decl_tag("comment:test_expect_failure")))
@@ -65,6 +69,8 @@
#define __flag(flag) __attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
#define __retval(val) __attribute__((btf_decl_tag("comment:test_retval="#val)))
#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")))
/* Convenience macro for use with 'asm volatile' blocks */
#define __naked __attribute__((naked))
@@ -121,107 +127,4 @@
/* make it look to compiler like value is read and written */
#define __sink(expr) asm volatile("" : "+g"(expr))
-struct bpf_iter_num;
-
-extern int bpf_iter_num_new(struct bpf_iter_num *it, int start, int end) __ksym;
-extern int *bpf_iter_num_next(struct bpf_iter_num *it) __ksym;
-extern void bpf_iter_num_destroy(struct bpf_iter_num *it) __ksym;
-
-#ifndef bpf_for_each
-/* bpf_for_each(iter_type, cur_elem, args...) provides generic construct for
- * using BPF open-coded iterators without having to write mundane explicit
- * low-level loop logic. Instead, it provides for()-like generic construct
- * that can be used pretty naturally. E.g., for some hypothetical cgroup
- * iterator, you'd write:
- *
- * struct cgroup *cg, *parent_cg = <...>;
- *
- * bpf_for_each(cgroup, cg, parent_cg, CG_ITER_CHILDREN) {
- * bpf_printk("Child cgroup id = %d", cg->cgroup_id);
- * if (cg->cgroup_id == 123)
- * break;
- * }
- *
- * I.e., it looks almost like high-level for each loop in other languages,
- * supports continue/break, and is verifiable by BPF verifier.
- *
- * For iterating integers, the difference betwen bpf_for_each(num, i, N, M)
- * and bpf_for(i, N, M) is in that bpf_for() provides additional proof to
- * verifier that i is in [N, M) range, and in bpf_for_each() case i is `int
- * *`, not just `int`. So for integers bpf_for() is more convenient.
- *
- * Note: this macro relies on C99 feature of allowing to declare variables
- * inside for() loop, bound to for() loop lifetime. It also utilizes GCC
- * extension: __attribute__((cleanup(<func>))), supported by both GCC and
- * Clang.
- */
-#define bpf_for_each(type, cur, args...) for ( \
- /* initialize and define destructor */ \
- struct bpf_iter_##type ___it __attribute__((aligned(8), /* enforce, just in case */, \
- cleanup(bpf_iter_##type##_destroy))), \
- /* ___p pointer is just to call bpf_iter_##type##_new() *once* to init ___it */ \
- *___p __attribute__((unused)) = ( \
- bpf_iter_##type##_new(&___it, ##args), \
- /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
- /* for bpf_iter_##type##_destroy() when used from cleanup() attribute */ \
- (void)bpf_iter_##type##_destroy, (void *)0); \
- /* iteration and termination check */ \
- (((cur) = bpf_iter_##type##_next(&___it))); \
-)
-#endif /* bpf_for_each */
-
-#ifndef bpf_for
-/* bpf_for(i, start, end) implements a for()-like looping construct that sets
- * provided integer variable *i* to values starting from *start* through,
- * but not including, *end*. It also proves to BPF verifier that *i* belongs
- * to range [start, end), so this can be used for accessing arrays without
- * extra checks.
- *
- * Note: *start* and *end* are assumed to be expressions with no side effects
- * and whose values do not change throughout bpf_for() loop execution. They do
- * not have to be statically known or constant, though.
- *
- * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
- * loop bound variables and cleanup attribute, supported by GCC and Clang.
- */
-#define bpf_for(i, start, end) for ( \
- /* initialize and define destructor */ \
- struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
- cleanup(bpf_iter_num_destroy))), \
- /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
- *___p __attribute__((unused)) = ( \
- bpf_iter_num_new(&___it, (start), (end)), \
- /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
- /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
- (void)bpf_iter_num_destroy, (void *)0); \
- ({ \
- /* iteration step */ \
- int *___t = bpf_iter_num_next(&___it); \
- /* termination and bounds check */ \
- (___t && ((i) = *___t, (i) >= (start) && (i) < (end))); \
- }); \
-)
-#endif /* bpf_for */
-
-#ifndef bpf_repeat
-/* bpf_repeat(N) performs N iterations without exposing iteration number
- *
- * Note: similarly to bpf_for_each(), it relies on C99 feature of declaring for()
- * loop bound variables and cleanup attribute, supported by GCC and Clang.
- */
-#define bpf_repeat(N) for ( \
- /* initialize and define destructor */ \
- struct bpf_iter_num ___it __attribute__((aligned(8), /* enforce, just in case */ \
- cleanup(bpf_iter_num_destroy))), \
- /* ___p pointer is necessary to call bpf_iter_num_new() *once* to init ___it */ \
- *___p __attribute__((unused)) = ( \
- bpf_iter_num_new(&___it, 0, (N)), \
- /* this is a workaround for Clang bug: it currently doesn't emit BTF */ \
- /* for bpf_iter_num_destroy() when used from cleanup() attribute */ \
- (void)bpf_iter_num_destroy, (void *)0); \
- bpf_iter_num_next(&___it); \
- /* nothing here */ \
-)
-#endif /* bpf_repeat */
-
#endif