1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2020 Facebook
3
4#include <linux/bpf.h>
5#include <stdint.h>
6#include <stdbool.h>
7#include <bpf/bpf_helpers.h>
8#include <bpf/bpf_core_read.h>
9
10char _license[] SEC("license") = "GPL";
11
12struct {
13 char in[256];
14 char out[256];
15 bool skip;
16} data = {};
17
18enum named_enum {
19 NAMED_ENUM_VAL1 = 1,
20 NAMED_ENUM_VAL2 = 2,
21 NAMED_ENUM_VAL3 = 3,
22};
23
24typedef enum {
25 ANON_ENUM_VAL1 = 0x10,
26 ANON_ENUM_VAL2 = 0x20,
27 ANON_ENUM_VAL3 = 0x30,
28} anon_enum;
29
30struct core_reloc_enumval_output {
31 bool named_val1_exists;
32 bool named_val2_exists;
33 bool named_val3_exists;
34 bool anon_val1_exists;
35 bool anon_val2_exists;
36 bool anon_val3_exists;
37
38 int named_val1;
39 int named_val2;
40 int anon_val1;
41 int anon_val2;
42};
43
44SEC("raw_tracepoint/sys_enter")
45int test_core_enumval(void *ctx)
46{
47#if __has_builtin(__builtin_preserve_enum_value)
48 struct core_reloc_enumval_output *out = (void *)&data.out;
49 enum named_enum named = 0;
50 anon_enum anon = 0;
51
52 out->named_val1_exists = bpf_core_enum_value_exists(named, NAMED_ENUM_VAL1);
53 out->named_val2_exists = bpf_core_enum_value_exists(enum named_enum, NAMED_ENUM_VAL2);
54 out->named_val3_exists = bpf_core_enum_value_exists(enum named_enum, NAMED_ENUM_VAL3);
55
56 out->anon_val1_exists = bpf_core_enum_value_exists(anon, ANON_ENUM_VAL1);
57 out->anon_val2_exists = bpf_core_enum_value_exists(anon_enum, ANON_ENUM_VAL2);
58 out->anon_val3_exists = bpf_core_enum_value_exists(anon_enum, ANON_ENUM_VAL3);
59
60 out->named_val1 = bpf_core_enum_value(named, NAMED_ENUM_VAL1);
61 out->named_val2 = bpf_core_enum_value(named, NAMED_ENUM_VAL2);
62 /* NAMED_ENUM_VAL3 value is optional */
63
64 out->anon_val1 = bpf_core_enum_value(anon, ANON_ENUM_VAL1);
65 out->anon_val2 = bpf_core_enum_value(anon, ANON_ENUM_VAL2);
66 /* ANON_ENUM_VAL3 value is optional */
67#else
68 data.skip = true;
69#endif
70
71 return 0;
72}
73

source code of linux/tools/testing/selftests/bpf/progs/test_core_reloc_enumval.c