hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/samples/bpf/map_perf_test_kern.c
....@@ -8,94 +8,101 @@
88 #include <linux/netdevice.h>
99 #include <linux/version.h>
1010 #include <uapi/linux/bpf.h>
11
-#include "bpf_helpers.h"
11
+#include <bpf/bpf_helpers.h>
12
+#include <bpf/bpf_tracing.h>
13
+#include <bpf/bpf_core_read.h>
14
+#include "trace_common.h"
1215
1316 #define MAX_ENTRIES 1000
1417 #define MAX_NR_CPUS 1024
1518
16
-struct bpf_map_def SEC("maps") hash_map = {
17
- .type = BPF_MAP_TYPE_HASH,
18
- .key_size = sizeof(u32),
19
- .value_size = sizeof(long),
20
- .max_entries = MAX_ENTRIES,
19
+struct {
20
+ __uint(type, BPF_MAP_TYPE_HASH);
21
+ __type(key, u32);
22
+ __type(value, long);
23
+ __uint(max_entries, MAX_ENTRIES);
24
+} hash_map SEC(".maps");
25
+
26
+struct {
27
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
28
+ __type(key, u32);
29
+ __type(value, long);
30
+ __uint(max_entries, 10000);
31
+} lru_hash_map SEC(".maps");
32
+
33
+struct {
34
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
35
+ __type(key, u32);
36
+ __type(value, long);
37
+ __uint(max_entries, 10000);
38
+ __uint(map_flags, BPF_F_NO_COMMON_LRU);
39
+} nocommon_lru_hash_map SEC(".maps");
40
+
41
+struct inner_lru {
42
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
43
+ __type(key, u32);
44
+ __type(value, long);
45
+ __uint(max_entries, MAX_ENTRIES);
46
+ __uint(map_flags, BPF_F_NUMA_NODE);
47
+ __uint(numa_node, 0);
48
+} inner_lru_hash_map SEC(".maps");
49
+
50
+struct {
51
+ __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
52
+ __uint(max_entries, MAX_NR_CPUS);
53
+ __uint(key_size, sizeof(u32));
54
+ __array(values, struct inner_lru); /* use inner_lru as inner map */
55
+} array_of_lru_hashs SEC(".maps") = {
56
+ /* statically initialize the first element */
57
+ .values = { &inner_lru_hash_map },
2158 };
2259
23
-struct bpf_map_def SEC("maps") lru_hash_map = {
24
- .type = BPF_MAP_TYPE_LRU_HASH,
25
- .key_size = sizeof(u32),
26
- .value_size = sizeof(long),
27
- .max_entries = 10000,
28
-};
60
+struct {
61
+ __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
62
+ __uint(key_size, sizeof(u32));
63
+ __uint(value_size, sizeof(long));
64
+ __uint(max_entries, MAX_ENTRIES);
65
+} percpu_hash_map SEC(".maps");
2966
30
-struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
31
- .type = BPF_MAP_TYPE_LRU_HASH,
32
- .key_size = sizeof(u32),
33
- .value_size = sizeof(long),
34
- .max_entries = 10000,
35
- .map_flags = BPF_F_NO_COMMON_LRU,
36
-};
67
+struct {
68
+ __uint(type, BPF_MAP_TYPE_HASH);
69
+ __type(key, u32);
70
+ __type(value, long);
71
+ __uint(max_entries, MAX_ENTRIES);
72
+ __uint(map_flags, BPF_F_NO_PREALLOC);
73
+} hash_map_alloc SEC(".maps");
3774
38
-struct bpf_map_def SEC("maps") inner_lru_hash_map = {
39
- .type = BPF_MAP_TYPE_LRU_HASH,
40
- .key_size = sizeof(u32),
41
- .value_size = sizeof(long),
42
- .max_entries = MAX_ENTRIES,
43
- .map_flags = BPF_F_NUMA_NODE,
44
- .numa_node = 0,
45
-};
75
+struct {
76
+ __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
77
+ __uint(key_size, sizeof(u32));
78
+ __uint(value_size, sizeof(long));
79
+ __uint(max_entries, MAX_ENTRIES);
80
+ __uint(map_flags, BPF_F_NO_PREALLOC);
81
+} percpu_hash_map_alloc SEC(".maps");
4682
47
-struct bpf_map_def SEC("maps") array_of_lru_hashs = {
48
- .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
49
- .key_size = sizeof(u32),
50
- .max_entries = MAX_NR_CPUS,
51
-};
83
+struct {
84
+ __uint(type, BPF_MAP_TYPE_LPM_TRIE);
85
+ __uint(key_size, 8);
86
+ __uint(value_size, sizeof(long));
87
+ __uint(max_entries, 10000);
88
+ __uint(map_flags, BPF_F_NO_PREALLOC);
89
+} lpm_trie_map_alloc SEC(".maps");
5290
53
-struct bpf_map_def SEC("maps") percpu_hash_map = {
54
- .type = BPF_MAP_TYPE_PERCPU_HASH,
55
- .key_size = sizeof(u32),
56
- .value_size = sizeof(long),
57
- .max_entries = MAX_ENTRIES,
58
-};
91
+struct {
92
+ __uint(type, BPF_MAP_TYPE_ARRAY);
93
+ __type(key, u32);
94
+ __type(value, long);
95
+ __uint(max_entries, MAX_ENTRIES);
96
+} array_map SEC(".maps");
5997
60
-struct bpf_map_def SEC("maps") hash_map_alloc = {
61
- .type = BPF_MAP_TYPE_HASH,
62
- .key_size = sizeof(u32),
63
- .value_size = sizeof(long),
64
- .max_entries = MAX_ENTRIES,
65
- .map_flags = BPF_F_NO_PREALLOC,
66
-};
98
+struct {
99
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
100
+ __type(key, u32);
101
+ __type(value, long);
102
+ __uint(max_entries, MAX_ENTRIES);
103
+} lru_hash_lookup_map SEC(".maps");
67104
68
-struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
69
- .type = BPF_MAP_TYPE_PERCPU_HASH,
70
- .key_size = sizeof(u32),
71
- .value_size = sizeof(long),
72
- .max_entries = MAX_ENTRIES,
73
- .map_flags = BPF_F_NO_PREALLOC,
74
-};
75
-
76
-struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
77
- .type = BPF_MAP_TYPE_LPM_TRIE,
78
- .key_size = 8,
79
- .value_size = sizeof(long),
80
- .max_entries = 10000,
81
- .map_flags = BPF_F_NO_PREALLOC,
82
-};
83
-
84
-struct bpf_map_def SEC("maps") array_map = {
85
- .type = BPF_MAP_TYPE_ARRAY,
86
- .key_size = sizeof(u32),
87
- .value_size = sizeof(long),
88
- .max_entries = MAX_ENTRIES,
89
-};
90
-
91
-struct bpf_map_def SEC("maps") lru_hash_lookup_map = {
92
- .type = BPF_MAP_TYPE_LRU_HASH,
93
- .key_size = sizeof(u32),
94
- .value_size = sizeof(long),
95
- .max_entries = MAX_ENTRIES,
96
-};
97
-
98
-SEC("kprobe/sys_getuid")
105
+SEC("kprobe/" SYSCALL(sys_getuid))
99106 int stress_hmap(struct pt_regs *ctx)
100107 {
101108 u32 key = bpf_get_current_pid_tgid();
....@@ -110,7 +117,7 @@
110117 return 0;
111118 }
112119
113
-SEC("kprobe/sys_geteuid")
120
+SEC("kprobe/" SYSCALL(sys_geteuid))
114121 int stress_percpu_hmap(struct pt_regs *ctx)
115122 {
116123 u32 key = bpf_get_current_pid_tgid();
....@@ -124,7 +131,7 @@
124131 return 0;
125132 }
126133
127
-SEC("kprobe/sys_getgid")
134
+SEC("kprobe/" SYSCALL(sys_getgid))
128135 int stress_hmap_alloc(struct pt_regs *ctx)
129136 {
130137 u32 key = bpf_get_current_pid_tgid();
....@@ -138,7 +145,7 @@
138145 return 0;
139146 }
140147
141
-SEC("kprobe/sys_getegid")
148
+SEC("kprobe/" SYSCALL(sys_getegid))
142149 int stress_percpu_hmap_alloc(struct pt_regs *ctx)
143150 {
144151 u32 key = bpf_get_current_pid_tgid();
....@@ -152,9 +159,10 @@
152159 return 0;
153160 }
154161
155
-SEC("kprobe/sys_connect")
162
+SEC("kprobe/" SYSCALL(sys_connect))
156163 int stress_lru_hmap_alloc(struct pt_regs *ctx)
157164 {
165
+ struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx);
158166 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
159167 union {
160168 u16 dst6[8];
....@@ -173,14 +181,14 @@
173181 long val = 1;
174182 u32 key = 0;
175183
176
- in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
177
- addrlen = (int)PT_REGS_PARM3(ctx);
184
+ in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(real_regs);
185
+ addrlen = (int)PT_REGS_PARM3_CORE(real_regs);
178186
179187 if (addrlen != sizeof(*in6))
180188 return 0;
181189
182
- ret = bpf_probe_read(test_params.dst6, sizeof(test_params.dst6),
183
- &in6->sin6_addr);
190
+ ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
191
+ &in6->sin6_addr);
184192 if (ret)
185193 goto done;
186194
....@@ -231,7 +239,7 @@
231239 return 0;
232240 }
233241
234
-SEC("kprobe/sys_gettid")
242
+SEC("kprobe/" SYSCALL(sys_gettid))
235243 int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
236244 {
237245 union {
....@@ -253,7 +261,7 @@
253261 return 0;
254262 }
255263
256
-SEC("kprobe/sys_getpgid")
264
+SEC("kprobe/" SYSCALL(sys_getpgid))
257265 int stress_hash_map_lookup(struct pt_regs *ctx)
258266 {
259267 u32 key = 1, i;
....@@ -266,7 +274,7 @@
266274 return 0;
267275 }
268276
269
-SEC("kprobe/sys_getppid")
277
+SEC("kprobe/" SYSCALL(sys_getppid))
270278 int stress_array_map_lookup(struct pt_regs *ctx)
271279 {
272280 u32 key = 1, i;