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
15 struct bpf_map_def SEC("maps") hash_map = {
16 .type = BPF_MAP_TYPE_HASH,
17 .key_size = sizeof(u32),
18 .value_size = sizeof(long),
19 .max_entries = MAX_ENTRIES,
22 struct bpf_map_def SEC("maps") lru_hash_map = {
23 .type = BPF_MAP_TYPE_LRU_HASH,
24 .key_size = sizeof(u32),
25 .value_size = sizeof(long),
29 struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
30 .type = BPF_MAP_TYPE_LRU_HASH,
31 .key_size = sizeof(u32),
32 .value_size = sizeof(long),
34 .map_flags = BPF_F_NO_COMMON_LRU,
37 struct bpf_map_def SEC("maps") percpu_hash_map = {
38 .type = BPF_MAP_TYPE_PERCPU_HASH,
39 .key_size = sizeof(u32),
40 .value_size = sizeof(long),
41 .max_entries = MAX_ENTRIES,
44 struct bpf_map_def SEC("maps") hash_map_alloc = {
45 .type = BPF_MAP_TYPE_HASH,
46 .key_size = sizeof(u32),
47 .value_size = sizeof(long),
48 .max_entries = MAX_ENTRIES,
49 .map_flags = BPF_F_NO_PREALLOC,
52 struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
53 .type = BPF_MAP_TYPE_PERCPU_HASH,
54 .key_size = sizeof(u32),
55 .value_size = sizeof(long),
56 .max_entries = MAX_ENTRIES,
57 .map_flags = BPF_F_NO_PREALLOC,
60 SEC("kprobe/sys_getuid")
61 int stress_hmap(struct pt_regs *ctx)
63 u32 key = bpf_get_current_pid_tgid();
67 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
68 value = bpf_map_lookup_elem(&hash_map, &key);
70 bpf_map_delete_elem(&hash_map, &key);
75 SEC("kprobe/sys_geteuid")
76 int stress_percpu_hmap(struct pt_regs *ctx)
78 u32 key = bpf_get_current_pid_tgid();
82 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
83 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
85 bpf_map_delete_elem(&percpu_hash_map, &key);
88 SEC("kprobe/sys_getgid")
89 int stress_hmap_alloc(struct pt_regs *ctx)
91 u32 key = bpf_get_current_pid_tgid();
95 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
96 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
98 bpf_map_delete_elem(&hash_map_alloc, &key);
102 SEC("kprobe/sys_getegid")
103 int stress_percpu_hmap_alloc(struct pt_regs *ctx)
105 u32 key = bpf_get_current_pid_tgid();
109 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
110 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
112 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
116 SEC("kprobe/sys_getpid")
117 int stress_lru_hmap_alloc(struct pt_regs *ctx)
119 u32 key = bpf_get_prandom_u32();
122 bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
127 SEC("kprobe/sys_getppid")
128 int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
130 u32 key = bpf_get_prandom_u32();
133 bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
138 char _license[] SEC("license") = "GPL";
139 u32 _version SEC("version") = LINUX_VERSION_CODE;