blob: ca3b22ed577a3f016b861272f9a3166e5909347d [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
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070014#define MAX_NR_CPUS 1024
Alexei Starovoitov26e90932016-03-08 15:07:54 -080015
16struct 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,
21};
22
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080023struct 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),
27 .max_entries = 10000,
28};
29
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070030struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080031 .type = BPF_MAP_TYPE_LRU_HASH,
32 .key_size = sizeof(u32),
33 .value_size = sizeof(long),
34 .max_entries = 10000,
35 .map_flags = BPF_F_NO_COMMON_LRU,
36};
37
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070038struct 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,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -070043 .map_flags = BPF_F_NUMA_NODE,
44 .numa_node = 0,
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -070045};
46
47struct 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,
51};
52
Alexei Starovoitov26e90932016-03-08 15:07:54 -080053struct 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,
58};
59
60struct 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,
66};
67
68struct 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,
74};
75
David Herrmannb8a943e2017-01-21 17:26:13 +010076struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
77 .type = BPF_MAP_TYPE_LPM_TRIE,
78 .key_size = 8,
79 .value_size = sizeof(long),
80 .max_entries = 10000,
81 .map_flags = BPF_F_NO_PREALLOC,
82};
83
Alexei Starovoitov95ff1412017-03-15 18:26:44 -070084struct 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,
89};
90
Alexei Starovoitov26e90932016-03-08 15:07:54 -080091SEC("kprobe/sys_getuid")
92int stress_hmap(struct pt_regs *ctx)
93{
94 u32 key = bpf_get_current_pid_tgid();
95 long init_val = 1;
96 long *value;
97
98 bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
99 value = bpf_map_lookup_elem(&hash_map, &key);
100 if (value)
101 bpf_map_delete_elem(&hash_map, &key);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800102
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800103 return 0;
104}
105
106SEC("kprobe/sys_geteuid")
107int stress_percpu_hmap(struct pt_regs *ctx)
108{
109 u32 key = bpf_get_current_pid_tgid();
110 long init_val = 1;
111 long *value;
112
113 bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
114 value = bpf_map_lookup_elem(&percpu_hash_map, &key);
115 if (value)
116 bpf_map_delete_elem(&percpu_hash_map, &key);
117 return 0;
118}
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700119
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800120SEC("kprobe/sys_getgid")
121int stress_hmap_alloc(struct pt_regs *ctx)
122{
123 u32 key = bpf_get_current_pid_tgid();
124 long init_val = 1;
125 long *value;
126
127 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
128 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
129 if (value)
130 bpf_map_delete_elem(&hash_map_alloc, &key);
131 return 0;
132}
133
134SEC("kprobe/sys_getegid")
135int stress_percpu_hmap_alloc(struct pt_regs *ctx)
136{
137 u32 key = bpf_get_current_pid_tgid();
138 long init_val = 1;
139 long *value;
140
141 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
142 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
143 if (value)
144 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
145 return 0;
146}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800147
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700148SEC("kprobe/sys_connect")
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800149int stress_lru_hmap_alloc(struct pt_regs *ctx)
150{
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700151 struct sockaddr_in6 *in6;
152 u16 test_case, dst6[8];
153 int addrlen, ret;
154 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n";
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800155 long val = 1;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800156 u32 key = bpf_get_prandom_u32();
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800157
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700158 in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
159 addrlen = (int)PT_REGS_PARM3(ctx);
160
161 if (addrlen != sizeof(*in6))
162 return 0;
163
164 ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
165 if (ret)
166 goto done;
167
168 if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
169 return 0;
170
171 test_case = dst6[7];
172
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700173 if (test_case == 0) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700174 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700175 } else if (test_case == 1) {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700176 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
177 BPF_ANY);
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700178 } else if (test_case == 2) {
179 void *nolocal_lru_map;
180 int cpu = bpf_get_smp_processor_id();
181
182 nolocal_lru_map = bpf_map_lookup_elem(&array_of_lru_hashs,
183 &cpu);
184 if (!nolocal_lru_map) {
185 ret = -ENOENT;
186 goto done;
187 }
188
189 ret = bpf_map_update_elem(nolocal_lru_map, &key, &val,
190 BPF_ANY);
191 } else {
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700192 ret = -EINVAL;
Martin KaFai Lau3a5795b2017-04-14 10:30:30 -0700193 }
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700194
195done:
196 if (ret)
197 bpf_trace_printk(fmt, sizeof(fmt), ret);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800198
199 return 0;
200}
201
David Herrmannb8a943e2017-01-21 17:26:13 +0100202SEC("kprobe/sys_gettid")
203int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
204{
205 union {
206 u32 b32[2];
207 u8 b8[8];
208 } key;
209 unsigned int i;
210
211 key.b32[0] = 32;
212 key.b8[4] = 192;
213 key.b8[5] = 168;
214 key.b8[6] = 0;
215 key.b8[7] = 1;
216
217#pragma clang loop unroll(full)
218 for (i = 0; i < 32; ++i)
219 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
220
221 return 0;
222}
223
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700224SEC("kprobe/sys_getpgid")
225int stress_hash_map_lookup(struct pt_regs *ctx)
226{
227 u32 key = 1, i;
228 long *value;
229
230#pragma clang loop unroll(full)
231 for (i = 0; i < 64; ++i)
232 value = bpf_map_lookup_elem(&hash_map, &key);
233
234 return 0;
235}
236
237SEC("kprobe/sys_getpgrp")
238int stress_array_map_lookup(struct pt_regs *ctx)
239{
240 u32 key = 1, i;
241 long *value;
242
243#pragma clang loop unroll(full)
244 for (i = 0; i < 64; ++i)
245 value = bpf_map_lookup_elem(&array_map, &key);
246
247 return 0;
248}
249
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800250char _license[] SEC("license") = "GPL";
251u32 _version SEC("version") = LINUX_VERSION_CODE;