blob: 404ed53b8a53de1174906343b2a92b1e40d38caf [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
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -070029struct bpf_map_def SEC("maps") nocommon_lru_hash_map = {
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -080030 .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}
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700103
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800104SEC("kprobe/sys_getgid")
105int stress_hmap_alloc(struct pt_regs *ctx)
106{
107 u32 key = bpf_get_current_pid_tgid();
108 long init_val = 1;
109 long *value;
110
111 bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
112 value = bpf_map_lookup_elem(&hash_map_alloc, &key);
113 if (value)
114 bpf_map_delete_elem(&hash_map_alloc, &key);
115 return 0;
116}
117
118SEC("kprobe/sys_getegid")
119int stress_percpu_hmap_alloc(struct pt_regs *ctx)
120{
121 u32 key = bpf_get_current_pid_tgid();
122 long init_val = 1;
123 long *value;
124
125 bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
126 value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
127 if (value)
128 bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
129 return 0;
130}
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800131
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700132SEC("kprobe/sys_connect")
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800133int stress_lru_hmap_alloc(struct pt_regs *ctx)
134{
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700135 struct sockaddr_in6 *in6;
136 u16 test_case, dst6[8];
137 int addrlen, ret;
138 char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%d\n";
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800139 long val = 1;
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800140 u32 key = bpf_get_prandom_u32();
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800141
Martin KaFai Laubf8db5d2017-04-14 10:30:27 -0700142 in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
143 addrlen = (int)PT_REGS_PARM3(ctx);
144
145 if (addrlen != sizeof(*in6))
146 return 0;
147
148 ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
149 if (ret)
150 goto done;
151
152 if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
153 return 0;
154
155 test_case = dst6[7];
156
157 if (test_case == 0)
158 ret = bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
159 else if (test_case == 1)
160 ret = bpf_map_update_elem(&nocommon_lru_hash_map, &key, &val,
161 BPF_ANY);
162 else
163 ret = -EINVAL;
164
165done:
166 if (ret)
167 bpf_trace_printk(fmt, sizeof(fmt), ret);
Martin KaFai Lau5db58fa2016-11-11 10:55:11 -0800168
169 return 0;
170}
171
David Herrmannb8a943e2017-01-21 17:26:13 +0100172SEC("kprobe/sys_gettid")
173int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
174{
175 union {
176 u32 b32[2];
177 u8 b8[8];
178 } key;
179 unsigned int i;
180
181 key.b32[0] = 32;
182 key.b8[4] = 192;
183 key.b8[5] = 168;
184 key.b8[6] = 0;
185 key.b8[7] = 1;
186
187#pragma clang loop unroll(full)
188 for (i = 0; i < 32; ++i)
189 bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
190
191 return 0;
192}
193
Alexei Starovoitov95ff1412017-03-15 18:26:44 -0700194SEC("kprobe/sys_getpgid")
195int stress_hash_map_lookup(struct pt_regs *ctx)
196{
197 u32 key = 1, i;
198 long *value;
199
200#pragma clang loop unroll(full)
201 for (i = 0; i < 64; ++i)
202 value = bpf_map_lookup_elem(&hash_map, &key);
203
204 return 0;
205}
206
207SEC("kprobe/sys_getpgrp")
208int stress_array_map_lookup(struct pt_regs *ctx)
209{
210 u32 key = 1, i;
211 long *value;
212
213#pragma clang loop unroll(full)
214 for (i = 0; i < 64; ++i)
215 value = bpf_map_lookup_elem(&array_map, &key);
216
217 return 0;
218}
219
Alexei Starovoitov26e90932016-03-08 15:07:54 -0800220char _license[] SEC("license") = "GPL";
221u32 _version SEC("version") = LINUX_VERSION_CODE;