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 | */ |
| 7 | #include <linux/skbuff.h> |
| 8 | #include <linux/netdevice.h> |
| 9 | #include <linux/version.h> |
| 10 | #include <uapi/linux/bpf.h> |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 11 | #include <bpf/bpf_helpers.h> |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 12 | #include "bpf_legacy.h" |
Toke Høiland-Jørgensen | 7cf245a | 2020-01-20 14:06:49 +0100 | [diff] [blame] | 13 | #include <bpf/bpf_tracing.h> |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 14 | |
| 15 | #define MAX_ENTRIES 1000 |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 16 | #define MAX_NR_CPUS 1024 |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 17 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 18 | struct bpf_map_def_legacy SEC("maps") hash_map = { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 19 | .type = BPF_MAP_TYPE_HASH, |
| 20 | .key_size = sizeof(u32), |
| 21 | .value_size = sizeof(long), |
| 22 | .max_entries = MAX_ENTRIES, |
| 23 | }; |
| 24 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 25 | struct bpf_map_def_legacy SEC("maps") lru_hash_map = { |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 26 | .type = BPF_MAP_TYPE_LRU_HASH, |
| 27 | .key_size = sizeof(u32), |
| 28 | .value_size = sizeof(long), |
| 29 | .max_entries = 10000, |
| 30 | }; |
| 31 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 32 | struct bpf_map_def_legacy SEC("maps") nocommon_lru_hash_map = { |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 33 | .type = BPF_MAP_TYPE_LRU_HASH, |
| 34 | .key_size = sizeof(u32), |
| 35 | .value_size = sizeof(long), |
| 36 | .max_entries = 10000, |
| 37 | .map_flags = BPF_F_NO_COMMON_LRU, |
| 38 | }; |
| 39 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 40 | struct bpf_map_def_legacy SEC("maps") inner_lru_hash_map = { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 41 | .type = BPF_MAP_TYPE_LRU_HASH, |
| 42 | .key_size = sizeof(u32), |
| 43 | .value_size = sizeof(long), |
| 44 | .max_entries = MAX_ENTRIES, |
Martin KaFai Lau | ad17d0e | 2017-08-18 11:28:01 -0700 | [diff] [blame] | 45 | .map_flags = BPF_F_NUMA_NODE, |
| 46 | .numa_node = 0, |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 49 | struct bpf_map_def_legacy SEC("maps") array_of_lru_hashs = { |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 50 | .type = BPF_MAP_TYPE_ARRAY_OF_MAPS, |
| 51 | .key_size = sizeof(u32), |
| 52 | .max_entries = MAX_NR_CPUS, |
| 53 | }; |
| 54 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 55 | struct bpf_map_def_legacy SEC("maps") percpu_hash_map = { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 56 | .type = BPF_MAP_TYPE_PERCPU_HASH, |
| 57 | .key_size = sizeof(u32), |
| 58 | .value_size = sizeof(long), |
| 59 | .max_entries = MAX_ENTRIES, |
| 60 | }; |
| 61 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 62 | struct bpf_map_def_legacy SEC("maps") hash_map_alloc = { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 63 | .type = BPF_MAP_TYPE_HASH, |
| 64 | .key_size = sizeof(u32), |
| 65 | .value_size = sizeof(long), |
| 66 | .max_entries = MAX_ENTRIES, |
| 67 | .map_flags = BPF_F_NO_PREALLOC, |
| 68 | }; |
| 69 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 70 | struct bpf_map_def_legacy SEC("maps") percpu_hash_map_alloc = { |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 71 | .type = BPF_MAP_TYPE_PERCPU_HASH, |
| 72 | .key_size = sizeof(u32), |
| 73 | .value_size = sizeof(long), |
| 74 | .max_entries = MAX_ENTRIES, |
| 75 | .map_flags = BPF_F_NO_PREALLOC, |
| 76 | }; |
| 77 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 78 | struct bpf_map_def_legacy SEC("maps") lpm_trie_map_alloc = { |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 79 | .type = BPF_MAP_TYPE_LPM_TRIE, |
| 80 | .key_size = 8, |
| 81 | .value_size = sizeof(long), |
| 82 | .max_entries = 10000, |
| 83 | .map_flags = BPF_F_NO_PREALLOC, |
| 84 | }; |
| 85 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 86 | struct bpf_map_def_legacy SEC("maps") array_map = { |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 87 | .type = BPF_MAP_TYPE_ARRAY, |
| 88 | .key_size = sizeof(u32), |
| 89 | .value_size = sizeof(long), |
| 90 | .max_entries = MAX_ENTRIES, |
| 91 | }; |
| 92 | |
Andrii Nakryiko | 36b5d47 | 2019-10-08 10:59:37 -0700 | [diff] [blame] | 93 | struct bpf_map_def_legacy SEC("maps") lru_hash_lookup_map = { |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 94 | .type = BPF_MAP_TYPE_LRU_HASH, |
| 95 | .key_size = sizeof(u32), |
| 96 | .value_size = sizeof(long), |
| 97 | .max_entries = MAX_ENTRIES, |
| 98 | }; |
| 99 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 100 | SEC("kprobe/sys_getuid") |
| 101 | int stress_hmap(struct pt_regs *ctx) |
| 102 | { |
| 103 | u32 key = bpf_get_current_pid_tgid(); |
| 104 | long init_val = 1; |
| 105 | long *value; |
| 106 | |
| 107 | bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY); |
| 108 | value = bpf_map_lookup_elem(&hash_map, &key); |
| 109 | if (value) |
| 110 | bpf_map_delete_elem(&hash_map, &key); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 111 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | SEC("kprobe/sys_geteuid") |
| 116 | int stress_percpu_hmap(struct pt_regs *ctx) |
| 117 | { |
| 118 | u32 key = bpf_get_current_pid_tgid(); |
| 119 | long init_val = 1; |
| 120 | long *value; |
| 121 | |
| 122 | bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY); |
| 123 | value = bpf_map_lookup_elem(&percpu_hash_map, &key); |
| 124 | if (value) |
| 125 | bpf_map_delete_elem(&percpu_hash_map, &key); |
| 126 | return 0; |
| 127 | } |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 128 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 129 | SEC("kprobe/sys_getgid") |
| 130 | int stress_hmap_alloc(struct pt_regs *ctx) |
| 131 | { |
| 132 | u32 key = bpf_get_current_pid_tgid(); |
| 133 | long init_val = 1; |
| 134 | long *value; |
| 135 | |
| 136 | bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY); |
| 137 | value = bpf_map_lookup_elem(&hash_map_alloc, &key); |
| 138 | if (value) |
| 139 | bpf_map_delete_elem(&hash_map_alloc, &key); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | SEC("kprobe/sys_getegid") |
| 144 | int stress_percpu_hmap_alloc(struct pt_regs *ctx) |
| 145 | { |
| 146 | u32 key = bpf_get_current_pid_tgid(); |
| 147 | long init_val = 1; |
| 148 | long *value; |
| 149 | |
| 150 | bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY); |
| 151 | value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key); |
| 152 | if (value) |
| 153 | bpf_map_delete_elem(&percpu_hash_map_alloc, &key); |
| 154 | return 0; |
| 155 | } |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 156 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 157 | SEC("kprobe/sys_connect") |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 158 | int stress_lru_hmap_alloc(struct pt_regs *ctx) |
| 159 | { |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 160 | char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn"; |
| 161 | union { |
| 162 | u16 dst6[8]; |
| 163 | struct { |
| 164 | u16 magic0; |
| 165 | u16 magic1; |
| 166 | u16 tcase; |
| 167 | u16 unused16; |
| 168 | u32 unused32; |
| 169 | u32 key; |
| 170 | }; |
| 171 | } test_params; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 172 | struct sockaddr_in6 *in6; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 173 | u16 test_case; |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 174 | int addrlen, ret; |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 175 | long val = 1; |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 176 | u32 key = 0; |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 177 | |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 178 | in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx); |
| 179 | addrlen = (int)PT_REGS_PARM3(ctx); |
| 180 | |
| 181 | if (addrlen != sizeof(*in6)) |
| 182 | return 0; |
| 183 | |
Daniel Borkmann | 251e2d3 | 2019-11-02 00:18:01 +0100 | [diff] [blame] | 184 | ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6), |
| 185 | &in6->sin6_addr); |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 186 | if (ret) |
| 187 | goto done; |
| 188 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 189 | if (test_params.magic0 != 0xdead || |
| 190 | test_params.magic1 != 0xbeef) |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 191 | return 0; |
| 192 | |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 193 | test_case = test_params.tcase; |
| 194 | if (test_case != 3) |
| 195 | key = bpf_get_prandom_u32(); |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 196 | |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 197 | if (test_case == 0) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 198 | 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] | 199 | } else if (test_case == 1) { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 200 | ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val, |
| 201 | BPF_ANY); |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 202 | } else if (test_case == 2) { |
| 203 | void *nolocal_lru_map; |
| 204 | int cpu = bpf_get_smp_processor_id(); |
| 205 | |
| 206 | nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs, |
| 207 | &cpu); |
| 208 | if (!nolocal_lru_map) { |
| 209 | ret = -ENOENT; |
| 210 | goto done; |
| 211 | } |
| 212 | |
| 213 | ret = bpf_map_update_elem(nolocal_lru_map, &key, &val, |
| 214 | BPF_ANY); |
Martin KaFai Lau | 637cd8c | 2017-08-31 23:27:11 -0700 | [diff] [blame] | 215 | } else if (test_case == 3) { |
| 216 | u32 i; |
| 217 | |
| 218 | key = test_params.key; |
| 219 | |
| 220 | #pragma clang loop unroll(full) |
| 221 | for (i = 0; i < 32; i++) { |
| 222 | bpf_map_lookup_elem(&lru_hash_lookup_map, &key); |
| 223 | key++; |
| 224 | } |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 225 | } else { |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 226 | ret = -EINVAL; |
Martin KaFai Lau | 3a5795b | 2017-04-14 10:30:30 -0700 | [diff] [blame] | 227 | } |
Martin KaFai Lau | bf8db5d | 2017-04-14 10:30:27 -0700 | [diff] [blame] | 228 | |
| 229 | done: |
| 230 | if (ret) |
| 231 | bpf_trace_printk(fmt, sizeof(fmt), ret); |
Martin KaFai Lau | 5db58fa | 2016-11-11 10:55:11 -0800 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
David Herrmann | b8a943e | 2017-01-21 17:26:13 +0100 | [diff] [blame] | 236 | SEC("kprobe/sys_gettid") |
| 237 | int stress_lpm_trie_map_alloc(struct pt_regs *ctx) |
| 238 | { |
| 239 | union { |
| 240 | u32 b32[2]; |
| 241 | u8 b8[8]; |
| 242 | } key; |
| 243 | unsigned int i; |
| 244 | |
| 245 | key.b32[0] = 32; |
| 246 | key.b8[4] = 192; |
| 247 | key.b8[5] = 168; |
| 248 | key.b8[6] = 0; |
| 249 | key.b8[7] = 1; |
| 250 | |
| 251 | #pragma clang loop unroll(full) |
| 252 | for (i = 0; i < 32; ++i) |
| 253 | bpf_map_lookup_elem(&lpm_trie_map_alloc, &key); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 258 | SEC("kprobe/sys_getpgid") |
| 259 | int stress_hash_map_lookup(struct pt_regs *ctx) |
| 260 | { |
| 261 | u32 key = 1, i; |
| 262 | long *value; |
| 263 | |
| 264 | #pragma clang loop unroll(full) |
| 265 | for (i = 0; i < 64; ++i) |
| 266 | value = bpf_map_lookup_elem(&hash_map, &key); |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Joel Fernandes | 95ec6696 | 2017-09-20 09:11:56 -0700 | [diff] [blame] | 271 | SEC("kprobe/sys_getppid") |
Alexei Starovoitov | 95ff141 | 2017-03-15 18:26:44 -0700 | [diff] [blame] | 272 | int stress_array_map_lookup(struct pt_regs *ctx) |
| 273 | { |
| 274 | u32 key = 1, i; |
| 275 | long *value; |
| 276 | |
| 277 | #pragma clang loop unroll(full) |
| 278 | for (i = 0; i < 64; ++i) |
| 279 | value = bpf_map_lookup_elem(&array_map, &key); |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
Alexei Starovoitov | 26e9093 | 2016-03-08 15:07:54 -0800 | [diff] [blame] | 284 | char _license[] SEC("license") = "GPL"; |
| 285 | u32 _version SEC("version") = LINUX_VERSION_CODE; |