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,
45 struct bpf_map_def SEC("maps") array_of_lru_hashs = {
46 .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
47 .key_size = sizeof(u32),
48 .max_entries = MAX_NR_CPUS,
51 struct bpf_map_def SEC("maps") percpu_hash_map = {
52 .type = BPF_MAP_TYPE_PERCPU_HASH,
53 .key_size = sizeof(u32),
54 .value_size = sizeof(long),
55 .max_entries = MAX_ENTRIES,
58 struct bpf_map_def SEC("maps") hash_map_alloc = {
59 .type = BPF_MAP_TYPE_HASH,
60 .key_size = sizeof(u32),
61 .value_size = sizeof(long),
62 .max_entries = MAX_ENTRIES,
63 .map_flags = BPF_F_NO_PREALLOC,
66 struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
67 .type = BPF_MAP_TYPE_PERCPU_HASH,
68 .key_size = sizeof(u32),
69 .value_size = sizeof(long),
70 .max_entries = MAX_ENTRIES,
71 .map_flags = BPF_F_NO_PREALLOC,
74 struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
75 .type = BPF_MAP_TYPE_LPM_TRIE,
77 .value_size = sizeof(long),
79 .map_flags = BPF_F_NO_PREALLOC,
82 struct bpf_map_def SEC("maps") array_map = {
83 .type = BPF_MAP_TYPE_ARRAY,
84 .key_size = sizeof(u32),
85 .value_size = sizeof(long),
86 .max_entries = MAX_ENTRIES,
89 SEC("kprobe/sys_getuid")
90 int stress_hmap(struct pt_regs *ctx)
92 u32 key = bpf_get_current_pid_tgid();
96 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
97 value = bpf_map_lookup_elem(&hash_map, &key);
99 bpf_map_delete_elem(&hash_map, &key);
104 SEC("kprobe/sys_geteuid")
105 int stress_percpu_hmap(struct pt_regs *ctx)
107 u32 key = bpf_get_current_pid_tgid();
111 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
112 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
114 bpf_map_delete_elem(&percpu_hash_map, &key);
118 SEC("kprobe/sys_getgid")
119 int stress_hmap_alloc(struct pt_regs *ctx)
121 u32 key = bpf_get_current_pid_tgid();
125 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
126 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
128 bpf_map_delete_elem(&hash_map_alloc, &key);
132 SEC("kprobe/sys_getegid")
133 int stress_percpu_hmap_alloc(struct pt_regs *ctx)
135 u32 key = bpf_get_current_pid_tgid();
139 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
140 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
142 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
146 SEC("kprobe/sys_connect")
147 int stress_lru_hmap_alloc(struct pt_regs *ctx)
149 struct sockaddr_in6 *in6;
150 u16 test_case, dst6[8];
152 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n";
154 u32 key = bpf_get_prandom_u32();
156 in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
157 addrlen = (int)PT_REGS_PARM3(ctx);
159 if (addrlen != sizeof(*in6))
162 ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
166 if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
171 if (test_case == 0) {
172 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
173 } else if (test_case == 1) {
174 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
176 } else if (test_case == 2) {
177 void *nolocal_lru_map;
178 int cpu = bpf_get_smp_processor_id();
180 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
182 if (!nolocal_lru_map) {
187 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
195 bpf_trace_printk(fmt, sizeof(fmt), ret);
200 SEC("kprobe/sys_gettid")
201 int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
215 #pragma clang loop unroll(full)
216 for (i = 0; i < 32; ++i)
217 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
222 SEC("kprobe/sys_getpgid")
223 int stress_hash_map_lookup(struct pt_regs *ctx)
228 #pragma clang loop unroll(full)
229 for (i = 0; i < 64; ++i)
230 value = bpf_map_lookup_elem(&hash_map, &key);
235 SEC("kprobe/sys_getpgrp")
236 int stress_array_map_lookup(struct pt_regs *ctx)
241 #pragma clang loop unroll(full)
242 for (i = 0; i < 64; ++i)
243 value = bpf_map_lookup_elem(&array_map, &key);
248 char _license[] SEC("license") = "GPL";
249 u32 _version SEC("version") = LINUX_VERSION_CODE;