Commit | Line | Data |
---|---|---|
26e90931 AS |
1 | /* Copyright (c) 2016 Facebook |
2 | * | |
3 | * This program is free software; you can redistribute it and/or | |
4 | * modify it under the terms of version 2 of the GNU General Public | |
5 | * License as published by the Free Software Foundation. | |
6 | */ | |
7 | #include <linux/skbuff.h> | |
8 | #include <linux/netdevice.h> | |
9 | #include <linux/version.h> | |
10 | #include <uapi/linux/bpf.h> | |
7cf245a3 | 11 | #include <bpf/bpf_helpers.h> |
36b5d471 | 12 | #include "bpf_legacy.h" |
7cf245a3 | 13 | #include <bpf/bpf_tracing.h> |
af9bd3e3 DL |
14 | #include <bpf/bpf_core_read.h> |
15 | #include "trace_common.h" | |
26e90931 AS |
16 | |
17 | #define MAX_ENTRIES 1000 | |
3a5795b8 | 18 | #define MAX_NR_CPUS 1024 |
26e90931 | 19 | |
36b5d471 | 20 | struct bpf_map_def_legacy SEC("maps") hash_map = { |
26e90931 AS |
21 | .type = BPF_MAP_TYPE_HASH, |
22 | .key_size = sizeof(u32), | |
23 | .value_size = sizeof(long), | |
24 | .max_entries = MAX_ENTRIES, | |
25 | }; | |
26 | ||
36b5d471 | 27 | struct bpf_map_def_legacy SEC("maps") lru_hash_map = { |
5db58faf MKL |
28 | .type = BPF_MAP_TYPE_LRU_HASH, |
29 | .key_size = sizeof(u32), | |
30 | .value_size = sizeof(long), | |
31 | .max_entries = 10000, | |
32 | }; | |
33 | ||
36b5d471 | 34 | struct bpf_map_def_legacy SEC("maps") nocommon_lru_hash_map = { |
5db58faf MKL |
35 | .type = BPF_MAP_TYPE_LRU_HASH, |
36 | .key_size = sizeof(u32), | |
37 | .value_size = sizeof(long), | |
38 | .max_entries = 10000, | |
39 | .map_flags = BPF_F_NO_COMMON_LRU, | |
40 | }; | |
41 | ||
36b5d471 | 42 | struct bpf_map_def_legacy SEC("maps") inner_lru_hash_map = { |
3a5795b8 MKL |
43 | .type = BPF_MAP_TYPE_LRU_HASH, |
44 | .key_size = sizeof(u32), | |
45 | .value_size = sizeof(long), | |
46 | .max_entries = MAX_ENTRIES, | |
ad17d0e6 MKL |
47 | .map_flags = BPF_F_NUMA_NODE, |
48 | .numa_node = 0, | |
3a5795b8 MKL |
49 | }; |
50 | ||
36b5d471 | 51 | struct bpf_map_def_legacy SEC("maps") array_of_lru_hashs = { |
3a5795b8 MKL |
52 | .type = BPF_MAP_TYPE_ARRAY_OF_MAPS, |
53 | .key_size = sizeof(u32), | |
54 | .max_entries = MAX_NR_CPUS, | |
55 | }; | |
56 | ||
36b5d471 | 57 | struct bpf_map_def_legacy SEC("maps") percpu_hash_map = { |
26e90931 AS |
58 | .type = BPF_MAP_TYPE_PERCPU_HASH, |
59 | .key_size = sizeof(u32), | |
60 | .value_size = sizeof(long), | |
61 | .max_entries = MAX_ENTRIES, | |
62 | }; | |
63 | ||
36b5d471 | 64 | struct bpf_map_def_legacy SEC("maps") hash_map_alloc = { |
26e90931 AS |
65 | .type = BPF_MAP_TYPE_HASH, |
66 | .key_size = sizeof(u32), | |
67 | .value_size = sizeof(long), | |
68 | .max_entries = MAX_ENTRIES, | |
69 | .map_flags = BPF_F_NO_PREALLOC, | |
70 | }; | |
71 | ||
36b5d471 | 72 | struct bpf_map_def_legacy SEC("maps") percpu_hash_map_alloc = { |
26e90931 AS |
73 | .type = BPF_MAP_TYPE_PERCPU_HASH, |
74 | .key_size = sizeof(u32), | |
75 | .value_size = sizeof(long), | |
76 | .max_entries = MAX_ENTRIES, | |
77 | .map_flags = BPF_F_NO_PREALLOC, | |
78 | }; | |
79 | ||
36b5d471 | 80 | struct bpf_map_def_legacy SEC("maps") lpm_trie_map_alloc = { |
b8a943e2 DH |
81 | .type = BPF_MAP_TYPE_LPM_TRIE, |
82 | .key_size = 8, | |
83 | .value_size = sizeof(long), | |
84 | .max_entries = 10000, | |
85 | .map_flags = BPF_F_NO_PREALLOC, | |
86 | }; | |
87 | ||
36b5d471 | 88 | struct bpf_map_def_legacy SEC("maps") array_map = { |
95ff141e AS |
89 | .type = BPF_MAP_TYPE_ARRAY, |
90 | .key_size = sizeof(u32), | |
91 | .value_size = sizeof(long), | |
92 | .max_entries = MAX_ENTRIES, | |
93 | }; | |
94 | ||
36b5d471 | 95 | struct bpf_map_def_legacy SEC("maps") lru_hash_lookup_map = { |
637cd8c3 MKL |
96 | .type = BPF_MAP_TYPE_LRU_HASH, |
97 | .key_size = sizeof(u32), | |
98 | .value_size = sizeof(long), | |
99 | .max_entries = MAX_ENTRIES, | |
100 | }; | |
101 | ||
26e90931 AS |
102 | SEC("kprobe/sys_getuid") |
103 | int stress_hmap(struct pt_regs *ctx) | |
104 | { | |
105 | u32 key = bpf_get_current_pid_tgid(); | |
106 | long init_val = 1; | |
107 | long *value; | |
108 | ||
109 | bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY); | |
110 | value = bpf_map_lookup_elem(&hash_map, &key); | |
111 | if (value) | |
112 | bpf_map_delete_elem(&hash_map, &key); | |
5db58faf | 113 | |
26e90931 AS |
114 | return 0; |
115 | } | |
116 | ||
117 | SEC("kprobe/sys_geteuid") | |
118 | int stress_percpu_hmap(struct pt_regs *ctx) | |
119 | { | |
120 | u32 key = bpf_get_current_pid_tgid(); | |
121 | long init_val = 1; | |
122 | long *value; | |
123 | ||
124 | bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY); | |
125 | value = bpf_map_lookup_elem(&percpu_hash_map, &key); | |
126 | if (value) | |
127 | bpf_map_delete_elem(&percpu_hash_map, &key); | |
128 | return 0; | |
129 | } | |
bf8db5d2 | 130 | |
26e90931 AS |
131 | SEC("kprobe/sys_getgid") |
132 | int stress_hmap_alloc(struct pt_regs *ctx) | |
133 | { | |
134 | u32 key = bpf_get_current_pid_tgid(); | |
135 | long init_val = 1; | |
136 | long *value; | |
137 | ||
138 | bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY); | |
139 | value = bpf_map_lookup_elem(&hash_map_alloc, &key); | |
140 | if (value) | |
141 | bpf_map_delete_elem(&hash_map_alloc, &key); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | SEC("kprobe/sys_getegid") | |
146 | int stress_percpu_hmap_alloc(struct pt_regs *ctx) | |
147 | { | |
148 | u32 key = bpf_get_current_pid_tgid(); | |
149 | long init_val = 1; | |
150 | long *value; | |
151 | ||
152 | bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY); | |
153 | value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key); | |
154 | if (value) | |
155 | bpf_map_delete_elem(&percpu_hash_map_alloc, &key); | |
156 | return 0; | |
157 | } | |
5db58faf | 158 | |
af9bd3e3 | 159 | SEC("kprobe/" SYSCALL(sys_connect)) |
5db58faf MKL |
160 | int stress_lru_hmap_alloc(struct pt_regs *ctx) |
161 | { | |
af9bd3e3 | 162 | struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx); |
637cd8c3 MKL |
163 | char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn"; |
164 | union { | |
165 | u16 dst6[8]; | |
166 | struct { | |
167 | u16 magic0; | |
168 | u16 magic1; | |
169 | u16 tcase; | |
170 | u16 unused16; | |
171 | u32 unused32; | |
172 | u32 key; | |
173 | }; | |
174 | } test_params; | |
bf8db5d2 | 175 | struct sockaddr_in6 *in6; |
637cd8c3 | 176 | u16 test_case; |
bf8db5d2 | 177 | int addrlen, ret; |
5db58faf | 178 | long val = 1; |
637cd8c3 | 179 | u32 key = 0; |
5db58faf | 180 | |
af9bd3e3 DL |
181 | in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(real_regs); |
182 | addrlen = (int)PT_REGS_PARM3_CORE(real_regs); | |
5db58faf | 183 | |
bf8db5d2 MKL |
184 | if (addrlen != sizeof(*in6)) |
185 | return 0; | |
5db58faf | 186 | |
251e2d33 DB |
187 | ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6), |
188 | &in6->sin6_addr); | |
bf8db5d2 MKL |
189 | if (ret) |
190 | goto done; | |
191 | ||
637cd8c3 MKL |
192 | if (test_params.magic0 != 0xdead || |
193 | test_params.magic1 != 0xbeef) | |
bf8db5d2 MKL |
194 | return 0; |
195 | ||
637cd8c3 MKL |
196 | test_case = test_params.tcase; |
197 | if (test_case != 3) | |
198 | key = bpf_get_prandom_u32(); | |
bf8db5d2 | 199 | |
3a5795b8 | 200 | if (test_case == 0) { |
bf8db5d2 | 201 | ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY); |
3a5795b8 | 202 | } else if (test_case == 1) { |
bf8db5d2 MKL |
203 | ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val, |
204 | BPF_ANY); | |
3a5795b8 MKL |
205 | } else if (test_case == 2) { |
206 | void *nolocal_lru_map; | |
207 | int cpu = bpf_get_smp_processor_id(); | |
208 | ||
209 | nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs, | |
210 | &cpu); | |
211 | if (!nolocal_lru_map) { | |
212 | ret = -ENOENT; | |
213 | goto done; | |
214 | } | |
215 | ||
216 | ret = bpf_map_update_elem(nolocal_lru_map, &key, &val, | |
217 | BPF_ANY); | |
637cd8c3 MKL |
218 | } else if (test_case == 3) { |
219 | u32 i; | |
220 | ||
221 | key = test_params.key; | |
222 | ||
223 | #pragma clang loop unroll(full) | |
224 | for (i = 0; i < 32; i++) { | |
225 | bpf_map_lookup_elem(&lru_hash_lookup_map, &key); | |
226 | key++; | |
227 | } | |
3a5795b8 | 228 | } else { |
bf8db5d2 | 229 | ret = -EINVAL; |
3a5795b8 | 230 | } |
5db58faf | 231 | |
bf8db5d2 MKL |
232 | done: |
233 | if (ret) | |
234 | bpf_trace_printk(fmt, sizeof(fmt), ret); | |
5db58faf MKL |
235 | |
236 | return 0; | |
237 | } | |
238 | ||
b8a943e2 DH |
239 | SEC("kprobe/sys_gettid") |
240 | int stress_lpm_trie_map_alloc(struct pt_regs *ctx) | |
241 | { | |
242 | union { | |
243 | u32 b32[2]; | |
244 | u8 b8[8]; | |
245 | } key; | |
246 | unsigned int i; | |
247 | ||
248 | key.b32[0] = 32; | |
249 | key.b8[4] = 192; | |
250 | key.b8[5] = 168; | |
251 | key.b8[6] = 0; | |
252 | key.b8[7] = 1; | |
253 | ||
254 | #pragma clang loop unroll(full) | |
255 | for (i = 0; i < 32; ++i) | |
256 | bpf_map_lookup_elem(&lpm_trie_map_alloc, &key); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
95ff141e AS |
261 | SEC("kprobe/sys_getpgid") |
262 | int stress_hash_map_lookup(struct pt_regs *ctx) | |
263 | { | |
264 | u32 key = 1, i; | |
265 | long *value; | |
266 | ||
267 | #pragma clang loop unroll(full) | |
268 | for (i = 0; i < 64; ++i) | |
269 | value = bpf_map_lookup_elem(&hash_map, &key); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
95ec6696 | 274 | SEC("kprobe/sys_getppid") |
95ff141e AS |
275 | int stress_array_map_lookup(struct pt_regs *ctx) |
276 | { | |
277 | u32 key = 1, i; | |
278 | long *value; | |
279 | ||
280 | #pragma clang loop unroll(full) | |
281 | for (i = 0; i < 64; ++i) | |
282 | value = bpf_map_lookup_elem(&array_map, &key); | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
26e90931 AS |
287 | char _license[] SEC("license") = "GPL"; |
288 | u32 _version SEC("version") = LINUX_VERSION_CODE; |