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.
7 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
9 #include <linux/version.h>
10 #include <uapi/linux/bpf.h>
11 #include <bpf/bpf_helpers.h>
12 #include <bpf/bpf_tracing.h>
13 #include <bpf/bpf_core_read.h>
14 #include "trace_common.h"
16 #define MAX_ENTRIES 1000
17 #define MAX_NR_CPUS 1024
20 __uint(type, BPF_MAP_TYPE_HASH);
23 __uint(max_entries, MAX_ENTRIES);
24 } hash_map SEC(".maps");
27 __uint(type, BPF_MAP_TYPE_LRU_HASH);
30 __uint(max_entries, 10000);
31 } lru_hash_map SEC(".maps");
34 __uint(type, BPF_MAP_TYPE_LRU_HASH);
37 __uint(max_entries, 10000);
38 __uint(map_flags, BPF_F_NO_COMMON_LRU);
39 } nocommon_lru_hash_map SEC(".maps");
42 __uint(type, BPF_MAP_TYPE_LRU_HASH);
45 __uint(max_entries, MAX_ENTRIES);
46 __uint(map_flags, BPF_F_NUMA_NODE);
48 } inner_lru_hash_map SEC(".maps");
51 __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
52 __uint(max_entries, MAX_NR_CPUS);
53 __uint(key_size, sizeof(u32));
54 __array(values, struct inner_lru); /* use inner_lru as inner map */
55 } array_of_lru_hashs SEC(".maps") = {
56 /* statically initialize the first element */
57 .values = { &inner_lru_hash_map },
61 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
62 __uint(key_size, sizeof(u32));
63 __uint(value_size, sizeof(long));
64 __uint(max_entries, MAX_ENTRIES);
65 } percpu_hash_map SEC(".maps");
68 __uint(type, BPF_MAP_TYPE_HASH);
71 __uint(max_entries, MAX_ENTRIES);
72 __uint(map_flags, BPF_F_NO_PREALLOC);
73 } hash_map_alloc SEC(".maps");
76 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
77 __uint(key_size, sizeof(u32));
78 __uint(value_size, sizeof(long));
79 __uint(max_entries, MAX_ENTRIES);
80 __uint(map_flags, BPF_F_NO_PREALLOC);
81 } percpu_hash_map_alloc SEC(".maps");
84 __uint(type, BPF_MAP_TYPE_LPM_TRIE);
86 __uint(value_size, sizeof(long));
87 __uint(max_entries, 10000);
88 __uint(map_flags, BPF_F_NO_PREALLOC);
89 } lpm_trie_map_alloc SEC(".maps");
92 __uint(type, BPF_MAP_TYPE_ARRAY);
95 __uint(max_entries, MAX_ENTRIES);
96 } array_map SEC(".maps");
99 __uint(type, BPF_MAP_TYPE_LRU_HASH);
102 __uint(max_entries, MAX_ENTRIES);
103 } lru_hash_lookup_map SEC(".maps");
105 SEC("kprobe/" SYSCALL(sys_getuid))
106 int stress_hmap(struct pt_regs *ctx)
108 u32 key = bpf_get_current_pid_tgid();
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);
120 SEC("kprobe/" SYSCALL(sys_geteuid))
121 int stress_percpu_hmap(struct pt_regs *ctx)
123 u32 key = bpf_get_current_pid_tgid();
127 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
128 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
130 bpf_map_delete_elem(&percpu_hash_map, &key);
134 SEC("kprobe/" SYSCALL(sys_getgid))
135 int stress_hmap_alloc(struct pt_regs *ctx)
137 u32 key = bpf_get_current_pid_tgid();
141 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
142 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
144 bpf_map_delete_elem(&hash_map_alloc, &key);
148 SEC("kprobe/" SYSCALL(sys_getegid))
149 int stress_percpu_hmap_alloc(struct pt_regs *ctx)
151 u32 key = bpf_get_current_pid_tgid();
155 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
156 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
158 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
162 SEC("kprobe/" SYSCALL(sys_connect))
163 int stress_lru_hmap_alloc(struct pt_regs *ctx)
165 struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx);
166 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
178 struct sockaddr_in6 *in6;
184 in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(real_regs);
185 addrlen = (int)PT_REGS_PARM3_CORE(real_regs);
187 if (addrlen != sizeof(*in6))
190 ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
195 if (test_params.magic0 != 0xdead ||
196 test_params.magic1 != 0xbeef)
199 test_case = test_params.tcase;
201 key = bpf_get_prandom_u32();
203 if (test_case == 0) {
204 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
205 } else if (test_case == 1) {
206 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
208 } else if (test_case == 2) {
209 void *nolocal_lru_map;
210 int cpu = bpf_get_smp_processor_id();
212 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
214 if (!nolocal_lru_map) {
219 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
221 } else if (test_case == 3) {
224 key = test_params.key;
226 #pragma clang loop unroll(full)
227 for (i = 0; i < 32; i++) {
228 bpf_map_lookup_elem(&lru_hash_lookup_map, &key);
237 bpf_trace_printk(fmt, sizeof(fmt), ret);
242 SEC("kprobe/" SYSCALL(sys_gettid))
243 int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
257 #pragma clang loop unroll(full)
258 for (i = 0; i < 32; ++i)
259 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
264 SEC("kprobe/" SYSCALL(sys_getpgid))
265 int stress_hash_map_lookup(struct pt_regs *ctx)
270 #pragma clang loop unroll(full)
271 for (i = 0; i < 64; ++i)
272 value = bpf_map_lookup_elem(&hash_map, &key);
277 SEC("kprobe/" SYSCALL(sys_getppid))
278 int stress_array_map_lookup(struct pt_regs *ctx)
283 #pragma clang loop unroll(full)
284 for (i = 0; i < 64; ++i)
285 value = bpf_map_lookup_elem(&array_map, &key);
290 char _license[] SEC("license") = "GPL";
291 u32 _version SEC("version") = LINUX_VERSION_CODE;