1 /* Copyright (c) 2016 Facebook
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.
9 #include <linux/version.h>
10 #include <bpf/bpf_helpers.h>
11 #include <bpf/bpf_tracing.h>
12 #include <bpf/bpf_core_read.h>
14 #define MAX_ENTRIES 1000
15 #define MAX_NR_CPUS 1024
18 __uint(type, BPF_MAP_TYPE_HASH);
21 __uint(max_entries, MAX_ENTRIES);
22 } hash_map SEC(".maps");
25 __uint(type, BPF_MAP_TYPE_LRU_HASH);
28 __uint(max_entries, 10000);
29 } lru_hash_map SEC(".maps");
32 __uint(type, BPF_MAP_TYPE_LRU_HASH);
35 __uint(max_entries, 10000);
36 __uint(map_flags, BPF_F_NO_COMMON_LRU);
37 } nocommon_lru_hash_map SEC(".maps");
40 __uint(type, BPF_MAP_TYPE_LRU_HASH);
43 __uint(max_entries, MAX_ENTRIES);
44 __uint(map_flags, BPF_F_NUMA_NODE);
46 } inner_lru_hash_map SEC(".maps");
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 },
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");
66 __uint(type, BPF_MAP_TYPE_HASH);
69 __uint(max_entries, MAX_ENTRIES);
70 __uint(map_flags, BPF_F_NO_PREALLOC);
71 } hash_map_alloc SEC(".maps");
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");
82 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
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");
90 __uint(type, BPF_MAP_TYPE_ARRAY);
93 __uint(max_entries, MAX_ENTRIES);
94 } array_map SEC(".maps");
97 __uint(type, BPF_MAP_TYPE_LRU_HASH);
100 __uint(max_entries, MAX_ENTRIES);
101 } lru_hash_lookup_map SEC(".maps");
103 SEC("ksyscall/getuid")
104 int BPF_KSYSCALL(stress_hmap)
106 u32 key = bpf_get_current_pid_tgid();
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);
115 bpf_map_delete_elem(&hash_map, &key);
121 SEC("ksyscall/geteuid")
122 int BPF_KSYSCALL(stress_percpu_hmap)
124 u32 key = bpf_get_current_pid_tgid();
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);
133 bpf_map_delete_elem(&percpu_hash_map, &key);
138 SEC("ksyscall/getgid")
139 int BPF_KSYSCALL(stress_hmap_alloc)
141 u32 key = bpf_get_current_pid_tgid();
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);
150 bpf_map_delete_elem(&hash_map_alloc, &key);
155 SEC("ksyscall/getegid")
156 int BPF_KSYSCALL(stress_percpu_hmap_alloc)
158 u32 key = bpf_get_current_pid_tgid();
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);
167 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
171 SEC("ksyscall/connect")
172 int BPF_KSYSCALL(stress_lru_hmap_alloc, int fd, struct sockaddr_in *uservaddr,
175 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
187 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)uservaddr;
193 if (addrlen != sizeof(*in6))
196 ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
201 if (test_params.magic0 != 0xdead ||
202 test_params.magic1 != 0xbeef)
205 test_case = test_params.tcase;
207 key = bpf_get_prandom_u32();
209 if (test_case == 0) {
210 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
211 } else if (test_case == 1) {
212 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
214 } else if (test_case == 2) {
215 void *nolocal_lru_map;
216 int cpu = bpf_get_smp_processor_id();
218 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
220 if (!nolocal_lru_map) {
225 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
227 } else if (test_case == 3) {
230 key = test_params.key;
232 #pragma clang loop unroll(full)
233 for (i = 0; i < 32; i++) {
234 bpf_map_lookup_elem(&lru_hash_lookup_map, &key);
243 bpf_trace_printk(fmt, sizeof(fmt), ret);
248 SEC("ksyscall/gettid")
249 int BPF_KSYSCALL(stress_lpm_trie_map_alloc)
263 #pragma clang loop unroll(full)
264 for (i = 0; i < 32; ++i)
265 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
270 SEC("ksyscall/getpgid")
271 int BPF_KSYSCALL(stress_hash_map_lookup)
276 #pragma clang loop unroll(full)
277 for (i = 0; i < 64; ++i)
278 value = bpf_map_lookup_elem(&hash_map, &key);
283 SEC("ksyscall/getppid")
284 int BPF_KSYSCALL(stress_array_map_lookup)
289 #pragma clang loop unroll(full)
290 for (i = 0; i < 64; ++i)
291 value = bpf_map_lookup_elem(&array_map, &key);
296 char _license[] SEC("license") = "GPL";
297 u32 _version SEC("version") = LINUX_VERSION_CODE;