blob: 9da2a3441b0a2e88eb63c3b60b2e8b76949b72fe [file] [log] [blame]
Alexei Starovoitov26e90932016-03-08 15:07:54 -08001/* Copyright (c) 2016 Facebook
2 *
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.
6 */
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"
12
13#define MAX_ENTRIES 1000
14
15struct 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,
20};
21
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080022struct 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),
26 .max_entries = 10000,
27};
28
29struct 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),
33 .max_entries = 10000,
34 .map_flags = BPF_F_NO_COMMON_LRU,
35};
36
Alexei Starovoitov26e90932016-03-08 15:07:54 -080037struct 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,
42};
43
44struct 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,
50};
51
52struct 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,
58};
59
David Herrmannb8a943e2017-01-21 17:26:13 +010060struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
61 .type = BPF_MAP_TYPE_LPM_TRIE,
62 .key_size = 8,
63 .value_size = sizeof(long),
64 .max_entries = 10000,
65 .map_flags = BPF_F_NO_PREALLOC,
66};
67
Alexei Starovoitov95ff1412017-03-15 18:26:44 -070068struct bpf_map_def SEC("maps") array_map = {
69 .type = BPF_MAP_TYPE_ARRAY,
70 .key_size = sizeof(u32),
71 .value_size = sizeof(long),
72 .max_entries = MAX_ENTRIES,
73};
74
Alexei Starovoitov26e90932016-03-08 15:07:54 -080075SEC("kprobe/sys_getuid")
76int stress_hmap(struct pt_regs *ctx)
77{
78 u32 key = bpf_get_current_pid_tgid();
79 long init_val = 1;
80 long *value;
81
82 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
83 value = bpf_map_lookup_elem(&hash_map, &key);
84 if (value)
85 bpf_map_delete_elem(&hash_map, &key);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080086
Alexei Starovoitov26e90932016-03-08 15:07:54 -080087 return 0;
88}
89
90SEC("kprobe/sys_geteuid")
91int stress_percpu_hmap(struct pt_regs *ctx)
92{
93 u32 key = bpf_get_current_pid_tgid();
94 long init_val = 1;
95 long *value;
96
97 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
98 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
99 if (value)
100 bpf_map_delete_elem(&percpu_hash_map, &key);
101 return 0;
102}
103SEC("kprobe/sys_getgid")
104int stress_hmap_alloc(struct pt_regs *ctx)
105{
106 u32 key = bpf_get_current_pid_tgid();
107 long init_val = 1;
108 long *value;
109
110 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
111 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
112 if (value)
113 bpf_map_delete_elem(&hash_map_alloc, &key);
114 return 0;
115}
116
117SEC("kprobe/sys_getegid")
118int stress_percpu_hmap_alloc(struct pt_regs *ctx)
119{
120 u32 key = bpf_get_current_pid_tgid();
121 long init_val = 1;
122 long *value;
123
124 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
125 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
126 if (value)
127 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
128 return 0;
129}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800130
131SEC("kprobe/sys_getpid")
132int stress_lru_hmap_alloc(struct pt_regs *ctx)
133{
134 u32 key = bpf_get_prandom_u32();
135 long val = 1;
136
137 bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
138
139 return 0;
140}
141
142SEC("kprobe/sys_getppid")
143int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
144{
145 u32 key = bpf_get_prandom_u32();
146 long val = 1;
147
148 bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
149
150 return 0;
151}
152
David Herrmannb8a943e2017-01-21 17:26:13 +0100153SEC("kprobe/sys_gettid")
154int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
155{
156 union {
157 u32 b32[2];
158 u8 b8[8];
159 } key;
160 unsigned int i;
161
162 key.b32[0] = 32;
163 key.b8[4] = 192;
164 key.b8[5] = 168;
165 key.b8[6] = 0;
166 key.b8[7] = 1;
167
168#pragma clang loop unroll(full)
169 for (i = 0; i < 32; ++i)
170 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
171
172 return 0;
173}
174
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700175SEC("kprobe/sys_getpgid")
176int stress_hash_map_lookup(struct pt_regs *ctx)
177{
178 u32 key = 1, i;
179 long *value;
180
181#pragma clang loop unroll(full)
182 for (i = 0; i < 64; ++i)
183 value = bpf_map_lookup_elem(&hash_map, &key);
184
185 return 0;
186}
187
188SEC("kprobe/sys_getpgrp")
189int stress_array_map_lookup(struct pt_regs *ctx)
190{
191 u32 key = 1, i;
192 long *value;
193
194#pragma clang loop unroll(full)
195 for (i = 0; i < 64; ++i)
196 value = bpf_map_lookup_elem(&array_map, &key);
197
198 return 0;
199}
200
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800201char _license[] SEC("license") = "GPL";
202u32 _version SEC("version") = LINUX_VERSION_CODE;