Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 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 | */ |
Daniel T. Lee | 8a4dd0b | 2022-12-24 16:15:23 +0900 | [diff] [blame] | 7 | #include "vmlinux.h" |
| 8 | #include <errno.h> |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 9 | #include <linux/version.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 10 | #include <bpf/bpf_helpers.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 11 | #include <bpf/bpf_tracing.h> |
Daniel T. Lee | af9bd3e | 2020-07-08 03:48:52 +0900 | [diff] [blame] | 12 | #include <bpf/bpf_core_read.h> |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 13 | |
| 14 | #define MAX_ENTRIES 1000 |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 15 | #define MAX_NR_CPUS 1024 |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 16 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 17 | struct { |
| 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 | |
| 24 | struct { |
| 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 | |
| 31 | struct { |
| 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 | |
| 39 | struct 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 | |
| 48 | struct { |
| 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 58 | struct { |
| 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 Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 64 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 65 | struct { |
| 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 Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 72 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 73 | struct { |
| 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 Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 80 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 81 | struct { |
| 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 Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 88 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 89 | struct { |
| 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 95 | |
Daniel T. Lee | cc7f641 | 2020-07-08 03:48:54 +0900 | [diff] [blame] | 96 | struct { |
| 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 102 | |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 103 | SEC("ksyscall/getuid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 104 | int BPF_KSYSCALL(stress_hmap) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 105 | { |
| 106 | u32 key = bpf_get_current_pid_tgid(); |
| 107 | long init_val = 1; |
| 108 | long *value; |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 109 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 110 | |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 111 | 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 Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 117 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 121 | SEC("ksyscall/geteuid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 122 | int BPF_KSYSCALL(stress_percpu_hmap) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 123 | { |
| 124 | u32 key = bpf_get_current_pid_tgid(); |
| 125 | long init_val = 1; |
| 126 | long *value; |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 127 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 128 | |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 129 | 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 135 | return 0; |
| 136 | } |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 137 | |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 138 | SEC("ksyscall/getgid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 139 | int BPF_KSYSCALL(stress_hmap_alloc) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 140 | { |
| 141 | u32 key = bpf_get_current_pid_tgid(); |
| 142 | long init_val = 1; |
| 143 | long *value; |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 144 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 145 | |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 146 | 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 155 | SEC("ksyscall/getegid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 156 | int BPF_KSYSCALL(stress_percpu_hmap_alloc) |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 157 | { |
| 158 | u32 key = bpf_get_current_pid_tgid(); |
| 159 | long init_val = 1; |
| 160 | long *value; |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 161 | int i; |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 162 | |
Alexei Starovoitov | 89dc8d0 | 2022-09-02 14:10:46 -0700 | [diff] [blame] | 163 | 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 169 | return 0; |
| 170 | } |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 171 | SEC("ksyscall/connect") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 172 | int BPF_KSYSCALL(stress_lru_hmap_alloc, int fd, struct sockaddr_in *uservaddr, |
| 173 | int addrlen) |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 174 | { |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 175 | 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. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 187 | struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)uservaddr; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 188 | u16 test_case; |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 189 | long val = 1; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 190 | u32 key = 0; |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 191 | int ret; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 192 | |
| 193 | if (addrlen != sizeof(*in6)) |
| 194 | return 0; |
| 195 | |
Daniel Borkmann | 251e2d3 | 2019-11-02 00:18:01 +0100 | [diff] [blame] | 196 | ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6), |
| 197 | &in6->sin6_addr); |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 198 | if (ret) |
| 199 | goto done; |
| 200 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 201 | if (test_params.magic0 != 0xdead || |
| 202 | test_params.magic1 != 0xbeef) |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 205 | test_case = test_params.tcase; |
| 206 | if (test_case != 3) |
| 207 | key = bpf_get_prandom_u32(); |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 208 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 209 | if (test_case == 0) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 210 | ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY); |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 211 | } else if (test_case == 1) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 212 | ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val, |
| 213 | BPF_ANY); |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 214 | } 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 Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 227 | } 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 Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 237 | } else { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 238 | ret = -EINVAL; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 239 | } |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 240 | |
| 241 | done: |
| 242 | if (ret) |
| 243 | bpf_trace_printk(fmt, sizeof(fmt), ret); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Daniel T. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 248 | SEC("ksyscall/gettid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 249 | int BPF_KSYSCALL(stress_lpm_trie_map_alloc) |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 250 | { |
| 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. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 270 | SEC("ksyscall/getpgid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 271 | int BPF_KSYSCALL(stress_hash_map_lookup) |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 272 | { |
| 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. Lee | 1d0c5f6 | 2022-12-24 16:15:22 +0900 | [diff] [blame] | 283 | SEC("ksyscall/getppid") |
Daniel T. Lee | c5ffb26 | 2022-12-24 16:15:26 +0900 | [diff] [blame] | 284 | int BPF_KSYSCALL(stress_array_map_lookup) |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 285 | { |
| 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 Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 296 | char _license[] SEC("license") = "GPL"; |
| 297 | u32 _version SEC("version") = LINUX_VERSION_CODE; |