File Index
Files and directories
Sign in
FILE

bpf_misc.h

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/progs/bpf_misc.h C
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef __BPF_MISC_H__
  3#define __BPF_MISC_H__
  4
  5#define XSTR(s) STR(s)
  6#define STR(s) #s
  7
  8/* Expand a macro and then stringize the expansion */
  9#define QUOTE(str) #str
 10#define EXPAND_QUOTE(str) QUOTE(str)
 11
 12/* This set of attributes controls behavior of the
 13 * test_loader.c:test_loader__run_subtests().
 14 *
 15 * The test_loader sequentially loads each program in a skeleton.
 16 * Programs could be loaded in privileged and unprivileged modes.
 17 * - __success, __failure, __msg, __regex imply privileged mode;
 18 * - __success_unpriv, __failure_unpriv, __msg_unpriv, __regex_unpriv
 19 *   imply unprivileged mode.
 20 * If combination of privileged and unprivileged attributes is present
 21 * both modes are used. If none are present privileged mode is implied.
 22 *
 23 * See test_loader.c:drop_capabilities() for exact set of capabilities
 24 * that differ between privileged and unprivileged modes.
 25 *
 26 * For test filtering purposes the name of the program loaded in
 27 * unprivileged mode is derived from the usual program name by adding
 28 * `@unpriv' suffix.
 29 *
 30 * __msg             Message expected to be found in the verifier log.
 31 *                   Multiple __msg attributes could be specified.
 32 *                   To match a regular expression use "{{" "}}" brackets,
 33 *                   e.g. "foo{{[0-9]+}}"  matches strings like "foo007".
 34 *                   Extended POSIX regular expression syntax is allowed
 35 *                   inside the brackets.
 36 * __not_msg         Message not expected to be found in verifier log.
 37 *                   If __msg_not is situated between __msg tags
 38 *                   framework matches __msg tags first, and then
 39 *                   checks that __msg_not is not present in a portion of
 40 *                   a log between bracketing __msg tags.
 41 *                   Same regex syntax as for __msg is supported.
 42 * __msg_unpriv      Same as __msg but for unprivileged mode.
 43 * __not_msg_unpriv  Same as __not_msg but for unprivileged mode.
 44 *
 45 * __stderr          Message expected to be found in bpf stderr stream. The
 46 *                   same regex rules apply like __msg.
 47 * __stderr_unpriv   Same as __stderr but for unpriveleged mode.
 48 * __stdout          Same as __stderr but for stdout stream.
 49 * __stdout_unpriv   Same as __stdout but for unpriveleged mode.
 50 *
 51 * __xlated          Expect a line in a disassembly log after verifier applies rewrites.
 52 *                   Multiple __xlated attributes could be specified.
 53 *                   Regular expressions could be specified same way as in __msg.
 54 * __xlated_unpriv   Same as __xlated but for unprivileged mode.
 55 *
 56 * __jited           Match a line in a disassembly of the jited BPF program.
 57 *                   Has to be used after __arch_* macro.
 58 *                   For example:
 59 *
 60 *                       __arch_x86_64
 61 *                       __jited("   endbr64")
 62 *                       __jited("   nopl    (%rax,%rax)")
 63 *                       __jited("   xorq    %rax, %rax")
 64 *                       ...
 65 *                       __naked void some_test(void)
 66 *                       {
 67 *                           asm volatile (... ::: __clobber_all);
 68 *                       }
 69 *
 70 *                   Regular expressions could be included in patterns same way
 71 *                   as in __msg.
 72 *
 73 *                   By default assume that each pattern has to be matched on the
 74 *                   next consecutive line of disassembly, e.g.:
 75 *
 76 *                       __jited("   endbr64")             # matched on line N
 77 *                       __jited("   nopl    (%rax,%rax)") # matched on line N+1
 78 *
 79 *                   If match occurs on a wrong line an error is reported.
 80 *                   To override this behaviour use literal "...", e.g.:
 81 *
 82 *                       __jited("   endbr64")             # matched on line N
 83 *                       __jited("...")                    # not matched
 84 *                       __jited("   nopl    (%rax,%rax)") # matched on any line >= N
 85 *
 86 * __jited_unpriv    Same as __jited but for unprivileged mode.
 87 *
 88 *
 89 * __success         Expect program load success in privileged mode.
 90 * __success_unpriv  Expect program load success in unprivileged mode.
 91 *
 92 * __failure         Expect program load failure in privileged mode.
 93 * __failure_unpriv  Expect program load failure in unprivileged mode.
 94 *
 95 * __retval          Execute the program using BPF_PROG_TEST_RUN command,
 96 *                   expect return value to match passed parameter:
 97 *                   - a decimal number
 98 *                   - a hexadecimal number, when starts from 0x
 99 *                   - a macro which expands to one of the above
100 *                   - literal _INT_MIN (expands to INT_MIN)
101 *                   In addition, two special macros are defined below:
102 *                   - POINTER_VALUE
103 *                   - TEST_DATA_LEN
104 * __retval_unpriv   Same, but load program in unprivileged mode.
105 *
106 * __description     Text to be used instead of a program name for display
107 *                   and filtering purposes.
108 *
109 * __log_level       Log level to use for the program, numeric value expected.
110 *
111 * __flag            Adds one flag use for the program, the following values are valid:
112 *                   - BPF_F_STRICT_ALIGNMENT;
113 *                   - BPF_F_TEST_RND_HI32;
114 *                   - BPF_F_TEST_STATE_FREQ;
115 *                   - BPF_F_SLEEPABLE;
116 *                   - BPF_F_XDP_HAS_FRAGS;
117 *                   - A numeric value.
118 *                   Multiple __flag attributes could be specified, the final flags
119 *                   value is derived by applying binary "or" to all specified values.
120 *
121 * __auxiliary         Annotated program is not a separate test, but used as auxiliary
122 *                     for some other test cases and should always be loaded.
123 * __auxiliary_unpriv  Same, but load program in unprivileged mode.
124 *
125 * __arch_*          Specify on which architecture the test case should be tested.
126 *                   Several __arch_* annotations could be specified at once.
127 *                   When test case is not run on current arch it is marked as skipped.
128 * __caps_unpriv     Specify the capabilities that should be set when running the test.
129 *
130 * __linear_size     Specify the size of the linear area of non-linear skbs, or
131 *                   0 for linear skbs.
132 */
133#define __msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_msg=" XSTR(__COUNTER__) "=" msg)))
134#define __not_msg(msg)		__attribute__((btf_decl_tag("comment:test_expect_not_msg=" XSTR(__COUNTER__) "=" msg)))
135#define __xlated(msg)		__attribute__((btf_decl_tag("comment:test_expect_xlated=" XSTR(__COUNTER__) "=" msg)))
136#define __jited(msg)		__attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg)))
137#define __failure		__attribute__((btf_decl_tag("comment:test_expect_failure")))
138#define __success		__attribute__((btf_decl_tag("comment:test_expect_success")))
139#define __description(desc)	__attribute__((btf_decl_tag("comment:test_description=" desc)))
140#define __msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_msg_unpriv=" XSTR(__COUNTER__) "=" msg)))
141#define __not_msg_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_not_msg_unpriv=" XSTR(__COUNTER__) "=" msg)))
142#define __xlated_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_xlated_unpriv=" XSTR(__COUNTER__) "=" msg)))
143#define __jited_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_jited=" XSTR(__COUNTER__) "=" msg)))
144#define __failure_unpriv	__attribute__((btf_decl_tag("comment:test_expect_failure_unpriv")))
145#define __success_unpriv	__attribute__((btf_decl_tag("comment:test_expect_success_unpriv")))
146#define __log_level(lvl)	__attribute__((btf_decl_tag("comment:test_log_level="#lvl)))
147#define __flag(flag)		__attribute__((btf_decl_tag("comment:test_prog_flags="#flag)))
148#define __retval(val)		__attribute__((btf_decl_tag("comment:test_retval="XSTR(val))))
149#define __retval_unpriv(val)	__attribute__((btf_decl_tag("comment:test_retval_unpriv="XSTR(val))))
150#define __auxiliary		__attribute__((btf_decl_tag("comment:test_auxiliary")))
151#define __auxiliary_unpriv	__attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
152#define __btf_path(path)	__attribute__((btf_decl_tag("comment:test_btf_path=" path)))
153#define __arch(arch)		__attribute__((btf_decl_tag("comment:test_arch=" arch)))
154#define __arch_x86_64		__arch("X86_64")
155#define __arch_arm64		__arch("ARM64")
156#define __arch_riscv64		__arch("RISCV64")
157#define __arch_s390x		__arch("s390x")
158#define __caps_unpriv(caps)	__attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
159#define __load_if_JITed()	__attribute__((btf_decl_tag("comment:load_mode=jited")))
160#define __load_if_no_JITed()	__attribute__((btf_decl_tag("comment:load_mode=no_jited")))
161#define __stderr(msg)		__attribute__((btf_decl_tag("comment:test_expect_stderr=" XSTR(__COUNTER__) "=" msg)))
162#define __stderr_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_stderr_unpriv=" XSTR(__COUNTER__) "=" msg)))
163#define __stdout(msg)		__attribute__((btf_decl_tag("comment:test_expect_stdout=" XSTR(__COUNTER__) "=" msg)))
164#define __stdout_unpriv(msg)	__attribute__((btf_decl_tag("comment:test_expect_stdout_unpriv=" XSTR(__COUNTER__) "=" msg)))
165#define __linear_size(sz)	__attribute__((btf_decl_tag("comment:test_linear_size=" XSTR(sz))))
166
167/* Define common capabilities tested using __caps_unpriv */
168#define CAP_NET_ADMIN		12
169#define CAP_SYS_ADMIN		21
170#define CAP_PERFMON		38
171#define CAP_BPF			39
172
173/* Convenience macro for use with 'asm volatile' blocks */
174#define __naked __attribute__((naked))
175#define __clobber_all "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "memory"
176#define __clobber_common "r0", "r1", "r2", "r3", "r4", "r5", "memory"
177#define __imm(name) [name]"i"(name)
178#define __imm_const(name, expr) [name]"i"(expr)
179#define __imm_addr(name) [name]"i"(&name)
180#define __imm_ptr(name) [name]"r"(&name)
181#define __imm_insn(name, expr) [name]"i"(*(long *)&(expr))
182
183#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
184#define offsetofend(TYPE, MEMBER) \
185	(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
186
187/* Magic constants used with __retval() */
188#define POINTER_VALUE	0xbadcafe
189#define TEST_DATA_LEN	64
190
191#ifndef __used
192#define __used __attribute__((used))
193#endif
194
195#if defined(__TARGET_ARCH_x86)
196#define SYSCALL_WRAPPER 1
197#define SYS_PREFIX "__x64_"
198#elif defined(__TARGET_ARCH_s390)
199#define SYSCALL_WRAPPER 1
200#define SYS_PREFIX "__s390x_"
201#elif defined(__TARGET_ARCH_arm64)
202#define SYSCALL_WRAPPER 1
203#define SYS_PREFIX "__arm64_"
204#elif defined(__TARGET_ARCH_riscv)
205#define SYSCALL_WRAPPER 1
206#define SYS_PREFIX "__riscv_"
207#elif defined(__TARGET_ARCH_powerpc)
208#define SYSCALL_WRAPPER 1
209#define SYS_PREFIX ""
210#else
211#define SYSCALL_WRAPPER 0
212#define SYS_PREFIX "__se_"
213#endif
214
215/* How many arguments are passed to function in register */
216#if defined(__TARGET_ARCH_x86) || defined(__x86_64__)
217#define FUNC_REG_ARG_CNT 6
218#elif defined(__i386__)
219#define FUNC_REG_ARG_CNT 3
220#elif defined(__TARGET_ARCH_s390) || defined(__s390x__)
221#define FUNC_REG_ARG_CNT 5
222#elif defined(__TARGET_ARCH_arm) || defined(__arm__)
223#define FUNC_REG_ARG_CNT 4
224#elif defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
225#define FUNC_REG_ARG_CNT 8
226#elif defined(__TARGET_ARCH_mips) || defined(__mips__)
227#define FUNC_REG_ARG_CNT 8
228#elif defined(__TARGET_ARCH_powerpc) || defined(__powerpc__) || defined(__powerpc64__)
229#define FUNC_REG_ARG_CNT 8
230#elif defined(__TARGET_ARCH_sparc) || defined(__sparc__)
231#define FUNC_REG_ARG_CNT 6
232#elif defined(__TARGET_ARCH_riscv) || defined(__riscv__)
233#define FUNC_REG_ARG_CNT 8
234#else
235/* default to 5 for others */
236#define FUNC_REG_ARG_CNT 5
237#endif
238
239/* make it look to compiler like value is read and written */
240#define __sink(expr) asm volatile("" : "+g"(expr))
241
242#ifndef ARRAY_SIZE
243#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
244#endif
245
246#if (defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) ||	\
247     (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64) ||		\
248     defined(__TARGET_ARCH_arm) || defined(__TARGET_ARCH_s390) ||	\
249     defined(__TARGET_ARCH_loongarch)) &&				\
250	__clang_major__ >= 18
251#define CAN_USE_GOTOL
252#endif
253
254#if __clang_major__ >= 18
255#define CAN_USE_BPF_ST
256#endif
257
258#if __clang_major__ >= 18 && defined(ENABLE_ATOMICS_TESTS) &&		\
259	(defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86) ||	\
260	 (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64)) || \
261	  (defined(__TARGET_ARCH_powerpc))
262#define CAN_USE_LOAD_ACQ_STORE_REL
263#endif
264
265#if defined(__TARGET_ARCH_arm64) || defined(__TARGET_ARCH_x86)
266#define SPEC_V1
267#endif
268
269#if defined(__TARGET_ARCH_x86)
270#define SPEC_V4
271#endif
272
273#endif
~/ Read-only file preview.