blob: 12e91ae64d4d9deddcfcb8e692b4144219e72569 [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>
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010011#include <bpf/bpf_helpers.h>
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070012#include "bpf_legacy.h"
Toke Høiland-Jørgensen7cf245a2020-01-20 14:06:49 +010013#include <bpf/bpf_tracing.h>
Alexei Starovoitov26e90932016-03-08 15:07:54 -080014
15#define MAX_ENTRIES 1000
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070016#define MAX_NR_CPUS 1024
Alexei Starovoitov26e90932016-03-08 15:07:54 -080017
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070018struct bpf_map_def_legacy SEC("maps") hash_map = {
Alexei Starovoitov26e90932016-03-08 15:07:54 -080019 .type = BPF_MAP_TYPE_HASH,
20 .key_size = sizeof(u32),
21 .value_size = sizeof(long),
22 .max_entries = MAX_ENTRIES,
23};
24
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070025struct bpf_map_def_legacy SEC("maps") lru_hash_map = {
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080026 .type = BPF_MAP_TYPE_LRU_HASH,
27 .key_size = sizeof(u32),
28 .value_size = sizeof(long),
29 .max_entries = 10000,
30};
31
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070032struct bpf_map_def_legacy SEC("maps") nocommon_lru_hash_map = {
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080033 .type = BPF_MAP_TYPE_LRU_HASH,
34 .key_size = sizeof(u32),
35 .value_size = sizeof(long),
36 .max_entries = 10000,
37 .map_flags = BPF_F_NO_COMMON_LRU,
38};
39
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070040struct bpf_map_def_legacy SEC("maps") inner_lru_hash_map = {
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070041 .type = BPF_MAP_TYPE_LRU_HASH,
42 .key_size = sizeof(u32),
43 .value_size = sizeof(long),
44 .max_entries = MAX_ENTRIES,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -070045 .map_flags = BPF_F_NUMA_NODE,
46 .numa_node = 0,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070047};
48
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070049struct bpf_map_def_legacy SEC("maps") array_of_lru_hashs = {
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070050 .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
51 .key_size = sizeof(u32),
52 .max_entries = MAX_NR_CPUS,
53};
54
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070055struct bpf_map_def_legacy SEC("maps") percpu_hash_map = {
Alexei Starovoitov26e90932016-03-08 15:07:54 -080056 .type = BPF_MAP_TYPE_PERCPU_HASH,
57 .key_size = sizeof(u32),
58 .value_size = sizeof(long),
59 .max_entries = MAX_ENTRIES,
60};
61
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070062struct bpf_map_def_legacy SEC("maps") hash_map_alloc = {
Alexei Starovoitov26e90932016-03-08 15:07:54 -080063 .type = BPF_MAP_TYPE_HASH,
64 .key_size = sizeof(u32),
65 .value_size = sizeof(long),
66 .max_entries = MAX_ENTRIES,
67 .map_flags = BPF_F_NO_PREALLOC,
68};
69
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070070struct bpf_map_def_legacy SEC("maps") percpu_hash_map_alloc = {
Alexei Starovoitov26e90932016-03-08 15:07:54 -080071 .type = BPF_MAP_TYPE_PERCPU_HASH,
72 .key_size = sizeof(u32),
73 .value_size = sizeof(long),
74 .max_entries = MAX_ENTRIES,
75 .map_flags = BPF_F_NO_PREALLOC,
76};
77
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070078struct bpf_map_def_legacy SEC("maps") lpm_trie_map_alloc = {
David Herrmannb8a943e2017-01-21 17:26:13 +010079 .type = BPF_MAP_TYPE_LPM_TRIE,
80 .key_size = 8,
81 .value_size = sizeof(long),
82 .max_entries = 10000,
83 .map_flags = BPF_F_NO_PREALLOC,
84};
85
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070086struct bpf_map_def_legacy SEC("maps") array_map = {
Alexei Starovoitov95ff1412017-03-15 18:26:44 -070087 .type = BPF_MAP_TYPE_ARRAY,
88 .key_size = sizeof(u32),
89 .value_size = sizeof(long),
90 .max_entries = MAX_ENTRIES,
91};
92
Andrii Nakryiko36b5d472019-10-08 10:59:37 -070093struct bpf_map_def_legacy SEC("maps") lru_hash_lookup_map = {
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -070094 .type = BPF_MAP_TYPE_LRU_HASH,
95 .key_size = sizeof(u32),
96 .value_size = sizeof(long),
97 .max_entries = MAX_ENTRIES,
98};
99
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800100SEC("kprobe/sys_getuid")
101int stress_hmap(struct pt_regs *ctx)
102{
103 u32 key = bpf_get_current_pid_tgid();
104 long init_val = 1;
105 long *value;
106
107 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
108 value = bpf_map_lookup_elem(&hash_map, &key);
109 if (value)
110 bpf_map_delete_elem(&hash_map, &key);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800111
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800112 return 0;
113}
114
115SEC("kprobe/sys_geteuid")
116int stress_percpu_hmap(struct pt_regs *ctx)
117{
118 u32 key = bpf_get_current_pid_tgid();
119 long init_val = 1;
120 long *value;
121
122 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
123 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
124 if (value)
125 bpf_map_delete_elem(&percpu_hash_map, &key);
126 return 0;
127}
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700128
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800129SEC("kprobe/sys_getgid")
130int stress_hmap_alloc(struct pt_regs *ctx)
131{
132 u32 key = bpf_get_current_pid_tgid();
133 long init_val = 1;
134 long *value;
135
136 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
137 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
138 if (value)
139 bpf_map_delete_elem(&hash_map_alloc, &key);
140 return 0;
141}
142
143SEC("kprobe/sys_getegid")
144int stress_percpu_hmap_alloc(struct pt_regs *ctx)
145{
146 u32 key = bpf_get_current_pid_tgid();
147 long init_val = 1;
148 long *value;
149
150 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
151 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
152 if (value)
153 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
154 return 0;
155}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800156
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700157SEC("kprobe/sys_connect")
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800158int stress_lru_hmap_alloc(struct pt_regs *ctx)
159{
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700160 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn";
161 union {
162 u16 dst6[8];
163 struct {
164 u16 magic0;
165 u16 magic1;
166 u16 tcase;
167 u16 unused16;
168 u32 unused32;
169 u32 key;
170 };
171 } test_params;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700172 struct sockaddr_in6 *in6;
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700173 u16 test_case;
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700174 int addrlen, ret;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800175 long val = 1;
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700176 u32 key = 0;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800177
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700178 in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
179 addrlen = (int)PT_REGS_PARM3(ctx);
180
181 if (addrlen != sizeof(*in6))
182 return 0;
183
Daniel Borkmann251e2d32019-11-02 00:18:01 +0100184 ret = bpf_probe_read_user(test_params.dst6, sizeof(test_params.dst6),
185 &in6->sin6_addr);
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700186 if (ret)
187 goto done;
188
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700189 if (test_params.magic0 != 0xdead ||
190 test_params.magic1 != 0xbeef)
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700191 return 0;
192
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700193 test_case = test_params.tcase;
194 if (test_case != 3)
195 key = bpf_get_prandom_u32();
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700196
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700197 if (test_case == 0) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700198 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700199 } else if (test_case == 1) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700200 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
201 BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700202 } else if (test_case == 2) {
203 void *nolocal_lru_map;
204 int cpu = bpf_get_smp_processor_id();
205
206 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
207 &cpu);
208 if (!nolocal_lru_map) {
209 ret = -ENOENT;
210 goto done;
211 }
212
213 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
214 BPF_ANY);
Martin KaFai Lau637cd8c2017-08-31 23:27:11 -0700215 } else if (test_case == 3) {
216 u32 i;
217
218 key = test_params.key;
219
220#pragma clang loop unroll(full)
221 for (i = 0; i < 32; i++) {
222 bpf_map_lookup_elem(&lru_hash_lookup_map, &key);
223 key++;
224 }
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700225 } else {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700226 ret = -EINVAL;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700227 }
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700228
229done:
230 if (ret)
231 bpf_trace_printk(fmt, sizeof(fmt), ret);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800232
233 return 0;
234}
235
David Herrmannb8a943e2017-01-21 17:26:13 +0100236SEC("kprobe/sys_gettid")
237int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
238{
239 union {
240 u32 b32[2];
241 u8 b8[8];
242 } key;
243 unsigned int i;
244
245 key.b32[0] = 32;
246 key.b8[4] = 192;
247 key.b8[5] = 168;
248 key.b8[6] = 0;
249 key.b8[7] = 1;
250
251#pragma clang loop unroll(full)
252 for (i = 0; i < 32; ++i)
253 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
254
255 return 0;
256}
257
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700258SEC("kprobe/sys_getpgid")
259int stress_hash_map_lookup(struct pt_regs *ctx)
260{
261 u32 key = 1, i;
262 long *value;
263
264#pragma clang loop unroll(full)
265 for (i = 0; i < 64; ++i)
266 value = bpf_map_lookup_elem(&hash_map, &key);
267
268 return 0;
269}
270
Joel Fernandes95ec66962017-09-20 09:11:56 -0700271SEC("kprobe/sys_getppid")
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700272int stress_array_map_lookup(struct pt_regs *ctx)
273{
274 u32 key = 1, i;
275 long *value;
276
277#pragma clang loop unroll(full)
278 for (i = 0; i < 64; ++i)
279 value = bpf_map_lookup_elem(&array_map, &key);
280
281 return 0;
282}
283
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800284char _license[] SEC("license") = "GPL";
285u32 _version SEC("version") = LINUX_VERSION_CODE;