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_helpers.h"
13 #define MAX_ENTRIES 1000
14 #define MAX_NR_CPUS 1024
16 struct bpf_map_def
SEC("maps") hash_map
= {
17 .type
= BPF_MAP_TYPE_HASH
,
18 .key_size
= sizeof(u32
),
19 .value_size
= sizeof(long),
20 .max_entries
= MAX_ENTRIES
,
23 struct bpf_map_def
SEC("maps") lru_hash_map
= {
24 .type
= BPF_MAP_TYPE_LRU_HASH
,
25 .key_size
= sizeof(u32
),
26 .value_size
= sizeof(long),
30 struct bpf_map_def
SEC("maps") nocommon_lru_hash_map
= {
31 .type
= BPF_MAP_TYPE_LRU_HASH
,
32 .key_size
= sizeof(u32
),
33 .value_size
= sizeof(long),
35 .map_flags
= BPF_F_NO_COMMON_LRU
,
38 struct bpf_map_def
SEC("maps") inner_lru_hash_map
= {
39 .type
= BPF_MAP_TYPE_LRU_HASH
,
40 .key_size
= sizeof(u32
),
41 .value_size
= sizeof(long),
42 .max_entries
= MAX_ENTRIES
,
43 .map_flags
= BPF_F_NUMA_NODE
,
47 struct bpf_map_def
SEC("maps") array_of_lru_hashs
= {
48 .type
= BPF_MAP_TYPE_ARRAY_OF_MAPS
,
49 .key_size
= sizeof(u32
),
50 .max_entries
= MAX_NR_CPUS
,
53 struct bpf_map_def
SEC("maps") percpu_hash_map
= {
54 .type
= BPF_MAP_TYPE_PERCPU_HASH
,
55 .key_size
= sizeof(u32
),
56 .value_size
= sizeof(long),
57 .max_entries
= MAX_ENTRIES
,
60 struct bpf_map_def
SEC("maps") hash_map_alloc
= {
61 .type
= BPF_MAP_TYPE_HASH
,
62 .key_size
= sizeof(u32
),
63 .value_size
= sizeof(long),
64 .max_entries
= MAX_ENTRIES
,
65 .map_flags
= BPF_F_NO_PREALLOC
,
68 struct bpf_map_def
SEC("maps") percpu_hash_map_alloc
= {
69 .type
= BPF_MAP_TYPE_PERCPU_HASH
,
70 .key_size
= sizeof(u32
),
71 .value_size
= sizeof(long),
72 .max_entries
= MAX_ENTRIES
,
73 .map_flags
= BPF_F_NO_PREALLOC
,
76 struct bpf_map_def
SEC("maps") lpm_trie_map_alloc
= {
77 .type
= BPF_MAP_TYPE_LPM_TRIE
,
79 .value_size
= sizeof(long),
81 .map_flags
= BPF_F_NO_PREALLOC
,
84 struct bpf_map_def
SEC("maps") array_map
= {
85 .type
= BPF_MAP_TYPE_ARRAY
,
86 .key_size
= sizeof(u32
),
87 .value_size
= sizeof(long),
88 .max_entries
= MAX_ENTRIES
,
91 struct bpf_map_def
SEC("maps") lru_hash_lookup_map
= {
92 .type
= BPF_MAP_TYPE_LRU_HASH
,
93 .key_size
= sizeof(u32
),
94 .value_size
= sizeof(long),
95 .max_entries
= MAX_ENTRIES
,
98 SEC("kprobe/sys_getuid")
99 int stress_hmap(struct pt_regs
*ctx
)
101 u32 key
= bpf_get_current_pid_tgid();
105 bpf_map_update_elem(&hash_map
, &key
, &init_val
, BPF_ANY
);
106 value
= bpf_map_lookup_elem(&hash_map
, &key
);
108 bpf_map_delete_elem(&hash_map
, &key
);
113 SEC("kprobe/sys_geteuid")
114 int stress_percpu_hmap(struct pt_regs
*ctx
)
116 u32 key
= bpf_get_current_pid_tgid();
120 bpf_map_update_elem(&percpu_hash_map
, &key
, &init_val
, BPF_ANY
);
121 value
= bpf_map_lookup_elem(&percpu_hash_map
, &key
);
123 bpf_map_delete_elem(&percpu_hash_map
, &key
);
127 SEC("kprobe/sys_getgid")
128 int stress_hmap_alloc(struct pt_regs
*ctx
)
130 u32 key
= bpf_get_current_pid_tgid();
134 bpf_map_update_elem(&hash_map_alloc
, &key
, &init_val
, BPF_ANY
);
135 value
= bpf_map_lookup_elem(&hash_map_alloc
, &key
);
137 bpf_map_delete_elem(&hash_map_alloc
, &key
);
141 SEC("kprobe/sys_getegid")
142 int stress_percpu_hmap_alloc(struct pt_regs
*ctx
)
144 u32 key
= bpf_get_current_pid_tgid();
148 bpf_map_update_elem(&percpu_hash_map_alloc
, &key
, &init_val
, BPF_ANY
);
149 value
= bpf_map_lookup_elem(&percpu_hash_map_alloc
, &key
);
151 bpf_map_delete_elem(&percpu_hash_map_alloc
, &key
);
155 SEC("kprobe/sys_connect")
156 int stress_lru_hmap_alloc(struct pt_regs
*ctx
)
158 char fmt
[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
170 struct sockaddr_in6
*in6
;
176 in6
= (struct sockaddr_in6
*)PT_REGS_PARM2(ctx
);
177 addrlen
= (int)PT_REGS_PARM3(ctx
);
179 if (addrlen
!= sizeof(*in6
))
182 ret
= bpf_probe_read(test_params
.dst6
, sizeof(test_params
.dst6
),
187 if (test_params
.magic0
!= 0xdead ||
188 test_params
.magic1
!= 0xbeef)
191 test_case
= test_params
.tcase
;
193 key
= bpf_get_prandom_u32();
195 if (test_case
== 0) {
196 ret
= bpf_map_update_elem(&lru_hash_map
, &key
, &val
, BPF_ANY
);
197 } else if (test_case
== 1) {
198 ret
= bpf_map_update_elem(&nocommon_lru_hash_map
, &key
, &val
,
200 } else if (test_case
== 2) {
201 void *nolocal_lru_map
;
202 int cpu
= bpf_get_smp_processor_id();
204 nolocal_lru_map
= bpf_map_lookup_elem(&array_of_lru_hashs
,
206 if (!nolocal_lru_map
) {
211 ret
= bpf_map_update_elem(nolocal_lru_map
, &key
, &val
,
213 } else if (test_case
== 3) {
216 key
= test_params
.key
;
218 #pragma clang loop unroll(full)
219 for (i
= 0; i
< 32; i
++) {
220 bpf_map_lookup_elem(&lru_hash_lookup_map
, &key
);
229 bpf_trace_printk(fmt
, sizeof(fmt
), ret
);
234 SEC("kprobe/sys_gettid")
235 int stress_lpm_trie_map_alloc(struct pt_regs
*ctx
)
249 #pragma clang loop unroll(full)
250 for (i
= 0; i
< 32; ++i
)
251 bpf_map_lookup_elem(&lpm_trie_map_alloc
, &key
);
256 SEC("kprobe/sys_getpgid")
257 int stress_hash_map_lookup(struct pt_regs
*ctx
)
262 #pragma clang loop unroll(full)
263 for (i
= 0; i
< 64; ++i
)
264 value
= bpf_map_lookup_elem(&hash_map
, &key
);
269 SEC("kprobe/sys_getppid")
270 int stress_array_map_lookup(struct pt_regs
*ctx
)
275 #pragma clang loop unroll(full)
276 for (i
= 0; i
< 64; ++i
)
277 value
= bpf_map_lookup_elem(&array_map
, &key
);
282 char _license
[] SEC("license") = "GPL";
283 u32 _version
SEC("version") = LINUX_VERSION_CODE
;