blob: 3cdeba2afe12d53321a7da1cd81eb36047535458 [file] [log] [blame]
Alexei Starovoitov26e90932016-03-08 15:07:54 -08001/* 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 */
Daniel T. Lee8a4dd0b2022-12-24 16:15:23 +09007#include "vmlinux.h"
8#include <errno.h>
Alexei Starovoitov26e90932016-03-08 15:07:54 -08009#include <linux/version.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010010#include <bpf/bpf_helpers.h>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010011#include <bpf/bpf_tracing.h>
Daniel T. Leeaf9bd3e2020-07-08 03:48:52 +090012#include <bpf/bpf_core_read.h>
Alexei Starovoitov26e90932016-03-08 15:07:54 -080013
14#define MAX_ENTRIES 1000
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070015#define MAX_NR_CPUS 1024
Alexei Starovoitov26e90932016-03-08 15:07:54 -080016
Daniel T. Leecc7f6412020-07-08 03:48:54 +090017struct {
18 __uint(type, BPF_MAP_TYPE_HASH);
19 __type(key, u32);
20 __type(value, long);
21 __uint(max_entries, MAX_ENTRIES);
22} hash_map SEC(".maps");
23
24struct {
25 __uint(type, BPF_MAP_TYPE_LRU_HASH);
26 __type(key, u32);
27 __type(value, long);
28 __uint(max_entries, 10000);
29} lru_hash_map SEC(".maps");
30
31struct {
32 __uint(type, BPF_MAP_TYPE_LRU_HASH);
33 __type(key, u32);
34 __type(value, long);
35 __uint(max_entries, 10000);
36 __uint(map_flags, BPF_F_NO_COMMON_LRU);
37} nocommon_lru_hash_map SEC(".maps");
38
39struct inner_lru {
40 __uint(type, BPF_MAP_TYPE_LRU_HASH);
41 __type(key, u32);
42 __type(value, long);
43 __uint(max_entries, MAX_ENTRIES);
44 __uint(map_flags, BPF_F_NUMA_NODE);
45 __uint(numa_node, 0);
46} inner_lru_hash_map SEC(".maps");
47
48struct {
49 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
50 __uint(max_entries, MAX_NR_CPUS);
51 __uint(key_size, sizeof(u32));
52 __array(values, struct inner_lru); /* use inner_lru as inner map */
53} array_of_lru_hashs SEC(".maps") = {
54 /* statically initialize the first element */
55 .values = { &inner_lru_hash_map },
Alexei Starovoitov26e90932016-03-08 15:07:54 -080056};
57
Daniel T. Leecc7f6412020-07-08 03:48:54 +090058struct {
59 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
60 __uint(key_size, sizeof(u32));
61 __uint(value_size, sizeof(long));
62 __uint(max_entries, MAX_ENTRIES);
63} percpu_hash_map SEC(".maps");
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080064
Daniel T. Leecc7f6412020-07-08 03:48:54 +090065struct {
66 __uint(type, BPF_MAP_TYPE_HASH);
67 __type(key, u32);
68 __type(value, long);
69 __uint(max_entries, MAX_ENTRIES);
70 __uint(map_flags, BPF_F_NO_PREALLOC);
71} hash_map_alloc SEC(".maps");
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080072
Daniel T. Leecc7f6412020-07-08 03:48:54 +090073struct {
74 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
75 __uint(key_size, sizeof(u32));
76 __uint(value_size, sizeof(long));
77 __uint(max_entries, MAX_ENTRIES);
78 __uint(map_flags, BPF_F_NO_PREALLOC);
79} percpu_hash_map_alloc SEC(".maps");
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070080
Daniel T. Leecc7f6412020-07-08 03:48:54 +090081struct {
82 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
83 __uint(key_size, 8);
84 __uint(value_size, sizeof(long));
85 __uint(max_entries, 10000);
86 __uint(map_flags, BPF_F_NO_PREALLOC);
87} lpm_trie_map_alloc SEC(".maps");
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070088
Daniel T. Leecc7f6412020-07-08 03:48:54 +090089struct {
90 __uint(type, BPF_MAP_TYPE_ARRAY);
91 __type(key, u32);
92 __type(value, long);
93 __uint(max_entries, MAX_ENTRIES);
94} array_map SEC(".maps");
Alexei Starovoitov26e90932016-03-08 15:07:54 -080095
Daniel T. Leecc7f6412020-07-08 03:48:54 +090096struct {
97 __uint(type, BPF_MAP_TYPE_LRU_HASH);
98 __type(key, u32);
99 __type(value, long);
100 __uint(max_entries, MAX_ENTRIES);
101} lru_hash_lookup_map SEC(".maps");
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800102
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900103SEC("ksyscall/getuid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900104int BPF_KSYSCALL(stress_hmap)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800105{
106 u32 key = bpf_get_current_pid_tgid();
107 long init_val = 1;
108 long *value;
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700109 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800110
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700111 for (i = 0; i < 10; i++) {
112 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
113 value = bpf_map_lookup_elem(&hash_map, &key);
114 if (value)
115 bpf_map_delete_elem(&hash_map, &key);
116 }
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800117
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800118 return 0;
119}
120
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900121SEC("ksyscall/geteuid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900122int BPF_KSYSCALL(stress_percpu_hmap)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800123{
124 u32 key = bpf_get_current_pid_tgid();
125 long init_val = 1;
126 long *value;
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700127 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800128
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700129 for (i = 0; i < 10; i++) {
130 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
131 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
132 if (value)
133 bpf_map_delete_elem(&percpu_hash_map, &key);
134 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800135 return 0;
136}
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700137
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900138SEC("ksyscall/getgid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900139int BPF_KSYSCALL(stress_hmap_alloc)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800140{
141 u32 key = bpf_get_current_pid_tgid();
142 long init_val = 1;
143 long *value;
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700144 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800145
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700146 for (i = 0; i < 10; i++) {
147 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
148 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
149 if (value)
150 bpf_map_delete_elem(&hash_map_alloc, &key);
151 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800152 return 0;
153}
154
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900155SEC("ksyscall/getegid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900156int BPF_KSYSCALL(stress_percpu_hmap_alloc)
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800157{
158 u32 key = bpf_get_current_pid_tgid();
159 long init_val = 1;
160 long *value;
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700161 int i;
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800162
Alexei Starovoitov89dc8d02022-09-02 14:10:46 -0700163 for (i = 0; i < 10; i++) {
164 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
165 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
166 if (value)
167 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
168 }
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800169 return 0;
170}
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900171SEC("ksyscall/connect")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900172int BPF_KSYSCALL(stress_lru_hmap_alloc, int fd, struct sockaddr_in *uservaddr,
173 int addrlen)
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800174{
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700175 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
176 union {
177 u16 dst6[8];
178 struct {
179 u16 magic0;
180 u16 magic1;
181 u16 tcase;
182 u16 unused16;
183 u32 unused32;
184 u32 key;
185 };
186 } test_params;
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900187 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)uservaddr;
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700188 u16 test_case;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800189 long val = 1;
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700190 u32 key = 0;
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900191 int ret;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700192
193 if (addrlen != sizeof(*in6))
194 return 0;
195
Daniel Borkmann251e2d32019-11-02 00:18:01 +0100196 ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
197 &in6->sin6_addr);
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700198 if (ret)
199 goto done;
200
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700201 if (test_params.magic0 != 0xdead ||
202 test_params.magic1 != 0xbeef)
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700203 return 0;
204
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700205 test_case = test_params.tcase;
206 if (test_case != 3)
207 key = bpf_get_prandom_u32();
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700208
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700209 if (test_case == 0) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700210 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700211 } else if (test_case == 1) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700212 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
213 BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700214 } else if (test_case == 2) {
215 void *nolocal_lru_map;
216 int cpu = bpf_get_smp_processor_id();
217
218 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
219 &cpu);
220 if (!nolocal_lru_map) {
221 ret = -ENOENT;
222 goto done;
223 }
224
225 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
226 BPF_ANY);
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700227 } else if (test_case == 3) {
228 u32 i;
229
230 key = test_params.key;
231
232#pragma clang loop unroll(full)
233 for (i = 0; i < 32; i++) {
234 bpf_map_lookup_elem(&lru_hash_lookup_map, &key);
235 key++;
236 }
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700237 } else {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700238 ret = -EINVAL;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700239 }
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700240
241done:
242 if (ret)
243 bpf_trace_printk(fmt, sizeof(fmt), ret);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800244
245 return 0;
246}
247
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900248SEC("ksyscall/gettid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900249int BPF_KSYSCALL(stress_lpm_trie_map_alloc)
David Herrmannb8a943e2017-01-21 17:26:13 +0100250{
251 union {
252 u32 b32[2];
253 u8 b8[8];
254 } key;
255 unsigned int i;
256
257 key.b32[0] = 32;
258 key.b8[4] = 192;
259 key.b8[5] = 168;
260 key.b8[6] = 0;
261 key.b8[7] = 1;
262
263#pragma clang loop unroll(full)
264 for (i = 0; i < 32; ++i)
265 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
266
267 return 0;
268}
269
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900270SEC("ksyscall/getpgid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900271int BPF_KSYSCALL(stress_hash_map_lookup)
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700272{
273 u32 key = 1, i;
274 long *value;
275
276#pragma clang loop unroll(full)
277 for (i = 0; i < 64; ++i)
278 value = bpf_map_lookup_elem(&hash_map, &key);
279
280 return 0;
281}
282
Daniel T. Lee1d0c5f62022-12-24 16:15:22 +0900283SEC("ksyscall/getppid")
Daniel T. Leec5ffb262022-12-24 16:15:26 +0900284int BPF_KSYSCALL(stress_array_map_lookup)
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700285{
286 u32 key = 1, i;
287 long *value;
288
289#pragma clang loop unroll(full)
290 for (i = 0; i < 64; ++i)
291 value = bpf_map_lookup_elem(&array_map, &key);
292
293 return 0;
294}
295
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800296char _license[] SEC("license") = "GPL";
297u32 _version SEC("version") = LINUX_VERSION_CODE;