blob: 36d6ac3f0c1caa0c18b53a893fe486436eb8c630 [file] [log] [blame]
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +02001/*
2 * Testsuite for eBPF maps
3 *
4 * Copyright (c) 2014 PLUMgrid, http://plumgrid.com
5 * Copyright (c) 2016 Facebook
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General Public
9 * License as published by the Free Software Foundation.
10 */
11
12#include <stdio.h>
13#include <unistd.h>
14#include <errno.h>
15#include <string.h>
16#include <assert.h>
17#include <stdlib.h>
18
19#include <sys/wait.h>
20#include <sys/resource.h>
21
22#include <linux/bpf.h>
23
Mickaël Salaün10ecc722017-02-10 00:21:39 +010024#include <bpf/bpf.h>
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010025#include "bpf_util.h"
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020026
27static int map_flags;
28
29static void test_hashmap(int task, void *data)
30{
Teng Qin8fe45922017-04-24 19:00:37 -070031 long long key, next_key, first_key, value;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020032 int fd;
33
Mickaël Salaünf4874d02017-02-10 00:21:43 +010034 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020035 2, map_flags);
36 if (fd < 0) {
37 printf("Failed to create hashmap '%s'!\n", strerror(errno));
38 exit(1);
39 }
40
41 key = 1;
42 value = 1234;
43 /* Insert key=1 element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +010044 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020045
46 value = 0;
47 /* BPF_NOEXIST means add new element if it doesn't exist. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +010048 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020049 /* key=1 already exists. */
50 errno == EEXIST);
51
52 /* -1 is an invalid flag. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +010053 assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 &&
54 errno == EINVAL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020055
56 /* Check that key=1 can be found. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +010057 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020058
59 key = 2;
60 /* Check that key=2 is not found. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +010061 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020062
63 /* BPF_EXIST means update existing element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +010064 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020065 /* key=2 is not there. */
66 errno == ENOENT);
67
68 /* Insert key=2 element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +010069 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020070
71 /* key=1 and key=2 were inserted, check that key=0 cannot be
72 * inserted due to max_entries limit.
73 */
74 key = 0;
Mickaël Salaün10ecc722017-02-10 00:21:39 +010075 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020076 errno == E2BIG);
77
78 /* Update existing element, though the map is full. */
79 key = 1;
Mickaël Salaün10ecc722017-02-10 00:21:39 +010080 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020081 key = 2;
Mickaël Salaün10ecc722017-02-10 00:21:39 +010082 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -070083 key = 3;
84 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
85 errno == E2BIG);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020086
87 /* Check that key = 0 doesn't exist. */
88 key = 0;
Mickaël Salaüne58383b2017-02-10 00:21:41 +010089 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +020090
91 /* Iterate over two elements. */
Teng Qin8fe45922017-04-24 19:00:37 -070092 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
93 (first_key == 1 || first_key == 2));
Mickaël Salaün5f155c22017-02-10 00:21:42 +010094 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
Teng Qin8fe45922017-04-24 19:00:37 -070095 (next_key == first_key));
Mickaël Salaün5f155c22017-02-10 00:21:42 +010096 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
Teng Qin8fe45922017-04-24 19:00:37 -070097 (next_key == 1 || next_key == 2) &&
98 (next_key != first_key));
Mickaël Salaün5f155c22017-02-10 00:21:42 +010099 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200100 errno == ENOENT);
101
102 /* Delete both elements. */
103 key = 1;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100104 assert(bpf_map_delete_elem(fd, &key) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200105 key = 2;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100106 assert(bpf_map_delete_elem(fd, &key) == 0);
107 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200108
109 key = 0;
110 /* Check that map is empty. */
Teng Qin8fe45922017-04-24 19:00:37 -0700111 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
112 errno == ENOENT);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100113 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200114 errno == ENOENT);
115
116 close(fd);
117}
118
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700119static void test_hashmap_sizes(int task, void *data)
120{
121 int fd, i, j;
122
123 for (i = 1; i <= 512; i <<= 1)
124 for (j = 1; j <= 1 << 18; j <<= 1) {
125 fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j,
126 2, map_flags);
127 if (fd < 0) {
128 printf("Failed to create hashmap key=%d value=%d '%s'\n",
129 i, j, strerror(errno));
130 exit(1);
131 }
132 close(fd);
133 usleep(10); /* give kernel time to destroy */
134 }
135}
136
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200137static void test_hashmap_percpu(int task, void *data)
138{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +0100139 unsigned int nr_cpus = bpf_num_possible_cpus();
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200140 BPF_DECLARE_PERCPU(long, value);
Teng Qin8fe45922017-04-24 19:00:37 -0700141 long long key, next_key, first_key;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200142 int expected_key_mask = 0;
143 int fd, i;
144
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100145 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key),
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200146 sizeof(bpf_percpu(value, 0)), 2, map_flags);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200147 if (fd < 0) {
148 printf("Failed to create hashmap '%s'!\n", strerror(errno));
149 exit(1);
150 }
151
152 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200153 bpf_percpu(value, i) = i + 100;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200154
155 key = 1;
156 /* Insert key=1 element. */
157 assert(!(expected_key_mask & key));
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100158 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200159 expected_key_mask |= key;
160
161 /* BPF_NOEXIST means add new element if it doesn't exist. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100162 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200163 /* key=1 already exists. */
164 errno == EEXIST);
165
166 /* -1 is an invalid flag. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100167 assert(bpf_map_update_elem(fd, &key, value, -1) == -1 &&
168 errno == EINVAL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200169
170 /* Check that key=1 can be found. Value could be 0 if the lookup
171 * was run from a different CPU.
172 */
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200173 bpf_percpu(value, 0) = 1;
174 assert(bpf_map_lookup_elem(fd, &key, value) == 0 &&
175 bpf_percpu(value, 0) == 100);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200176
177 key = 2;
178 /* Check that key=2 is not found. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100179 assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200180
181 /* BPF_EXIST means update existing element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100182 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200183 /* key=2 is not there. */
184 errno == ENOENT);
185
186 /* Insert key=2 element. */
187 assert(!(expected_key_mask & key));
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100188 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200189 expected_key_mask |= key;
190
191 /* key=1 and key=2 were inserted, check that key=0 cannot be
192 * inserted due to max_entries limit.
193 */
194 key = 0;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100195 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200196 errno == E2BIG);
197
198 /* Check that key = 0 doesn't exist. */
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100199 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200200
201 /* Iterate over two elements. */
Teng Qin8fe45922017-04-24 19:00:37 -0700202 assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 &&
203 ((expected_key_mask & first_key) == first_key));
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100204 while (!bpf_map_get_next_key(fd, &key, &next_key)) {
Teng Qin8fe45922017-04-24 19:00:37 -0700205 if (first_key) {
206 assert(next_key == first_key);
207 first_key = 0;
208 }
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200209 assert((expected_key_mask & next_key) == next_key);
210 expected_key_mask &= ~next_key;
211
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100212 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200213
214 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200215 assert(bpf_percpu(value, i) == i + 100);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200216
217 key = next_key;
218 }
219 assert(errno == ENOENT);
220
221 /* Update with BPF_EXIST. */
222 key = 1;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100223 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200224
225 /* Delete both elements. */
226 key = 1;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100227 assert(bpf_map_delete_elem(fd, &key) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200228 key = 2;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100229 assert(bpf_map_delete_elem(fd, &key) == 0);
230 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200231
232 key = 0;
233 /* Check that map is empty. */
Teng Qin8fe45922017-04-24 19:00:37 -0700234 assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 &&
235 errno == ENOENT);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100236 assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200237 errno == ENOENT);
238
239 close(fd);
240}
241
Daniel Borkmann5ecf51f2017-06-11 00:50:44 +0200242static void test_hashmap_walk(int task, void *data)
243{
244 int fd, i, max_entries = 100000;
245 long long key, value, next_key;
246 bool next_key_valid = true;
247
248 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
249 max_entries, map_flags);
250 if (fd < 0) {
251 printf("Failed to create hashmap '%s'!\n", strerror(errno));
252 exit(1);
253 }
254
255 for (i = 0; i < max_entries; i++) {
256 key = i; value = key;
257 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
258 }
259
260 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
261 &next_key) == 0; i++) {
262 key = next_key;
263 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
264 }
265
266 assert(i == max_entries);
267
268 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
269 for (i = 0; next_key_valid; i++) {
270 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0;
271 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
272 value++;
273 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0);
274 key = next_key;
275 }
276
277 assert(i == max_entries);
278
279 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key,
280 &next_key) == 0; i++) {
281 key = next_key;
282 assert(bpf_map_lookup_elem(fd, &key, &value) == 0);
283 assert(value - 1 == key);
284 }
285
286 assert(i == max_entries);
287 close(fd);
288}
289
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200290static void test_arraymap(int task, void *data)
291{
292 int key, next_key, fd;
293 long long value;
294
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100295 fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value),
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200296 2, 0);
297 if (fd < 0) {
298 printf("Failed to create arraymap '%s'!\n", strerror(errno));
299 exit(1);
300 }
301
302 key = 1;
303 value = 1234;
304 /* Insert key=1 element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100305 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200306
307 value = 0;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100308 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200309 errno == EEXIST);
310
311 /* Check that key=1 can be found. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100312 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200313
314 key = 0;
315 /* Check that key=0 is also found and zero initialized. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100316 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200317
318 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted
319 * due to max_entries limit.
320 */
321 key = 2;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100322 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200323 errno == E2BIG);
324
325 /* Check that key = 2 doesn't exist. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100326 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200327
328 /* Iterate over two elements. */
Teng Qin8fe45922017-04-24 19:00:37 -0700329 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
330 next_key == 0);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100331 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200332 next_key == 0);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100333 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200334 next_key == 1);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100335 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200336 errno == ENOENT);
337
338 /* Delete shouldn't succeed. */
339 key = 1;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100340 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200341
342 close(fd);
343}
344
345static void test_arraymap_percpu(int task, void *data)
346{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +0100347 unsigned int nr_cpus = bpf_num_possible_cpus();
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200348 BPF_DECLARE_PERCPU(long, values);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200349 int key, next_key, fd, i;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200350
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100351 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200352 sizeof(bpf_percpu(values, 0)), 2, 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200353 if (fd < 0) {
354 printf("Failed to create arraymap '%s'!\n", strerror(errno));
355 exit(1);
356 }
357
358 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200359 bpf_percpu(values, i) = i + 100;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200360
361 key = 1;
362 /* Insert key=1 element. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100363 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200364
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200365 bpf_percpu(values, 0) = 0;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100366 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200367 errno == EEXIST);
368
369 /* Check that key=1 can be found. */
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200370 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
371 bpf_percpu(values, 0) == 100);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200372
373 key = 0;
374 /* Check that key=0 is also found and zero initialized. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100375 assert(bpf_map_lookup_elem(fd, &key, values) == 0 &&
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200376 bpf_percpu(values, 0) == 0 &&
377 bpf_percpu(values, nr_cpus - 1) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200378
379 /* Check that key=2 cannot be inserted due to max_entries limit. */
380 key = 2;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100381 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200382 errno == E2BIG);
383
384 /* Check that key = 2 doesn't exist. */
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100385 assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200386
387 /* Iterate over two elements. */
Teng Qin8fe45922017-04-24 19:00:37 -0700388 assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 &&
389 next_key == 0);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100390 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200391 next_key == 0);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100392 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200393 next_key == 1);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100394 assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200395 errno == ENOENT);
396
397 /* Delete shouldn't succeed. */
398 key = 1;
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100399 assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200400
401 close(fd);
402}
403
404static void test_arraymap_percpu_many_keys(void)
405{
Daniel Borkmanne00c7b22016-11-26 01:28:09 +0100406 unsigned int nr_cpus = bpf_num_possible_cpus();
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200407 BPF_DECLARE_PERCPU(long, values);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700408 /* nr_keys is not too large otherwise the test stresses percpu
409 * allocator more than anything else
410 */
411 unsigned int nr_keys = 2000;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200412 int key, fd, i;
413
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100414 fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key),
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200415 sizeof(bpf_percpu(values, 0)), nr_keys, 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200416 if (fd < 0) {
417 printf("Failed to create per-cpu arraymap '%s'!\n",
418 strerror(errno));
419 exit(1);
420 }
421
422 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200423 bpf_percpu(values, i) = i + 10;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200424
425 for (key = 0; key < nr_keys; key++)
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100426 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200427
428 for (key = 0; key < nr_keys; key++) {
429 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200430 bpf_percpu(values, i) = 0;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200431
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100432 assert(bpf_map_lookup_elem(fd, &key, values) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200433
434 for (i = 0; i < nr_cpus; i++)
Daniel Borkmannf3515b52017-04-27 01:39:35 +0200435 assert(bpf_percpu(values, i) == i + 10);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200436 }
437
438 close(fd);
439}
440
John Fastabend546ac1f2017-07-17 09:28:56 -0700441static void test_devmap(int task, void *data)
442{
443 int next_key, fd;
444 __u32 key, value;
445
446 fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value),
447 2, 0);
448 if (fd < 0) {
449 printf("Failed to create arraymap '%s'!\n", strerror(errno));
450 exit(1);
451 }
452
453 close(fd);
454}
455
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200456#define MAP_SIZE (32 * 1024)
457
458static void test_map_large(void)
459{
460 struct bigkey {
461 int a;
462 char b[116];
463 long long c;
464 } key;
465 int fd, i, value;
466
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100467 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200468 MAP_SIZE, map_flags);
469 if (fd < 0) {
470 printf("Failed to create large map '%s'!\n", strerror(errno));
471 exit(1);
472 }
473
474 for (i = 0; i < MAP_SIZE; i++) {
475 key = (struct bigkey) { .c = i };
476 value = i;
477
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100478 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200479 }
480
481 key.c = -1;
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100482 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200483 errno == E2BIG);
484
485 /* Iterate through all elements. */
Teng Qin8fe45922017-04-24 19:00:37 -0700486 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
487 key.c = -1;
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200488 for (i = 0; i < MAP_SIZE; i++)
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100489 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
490 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200491
492 key.c = 0;
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100493 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200494 key.a = 1;
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100495 assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200496
497 close(fd);
498}
499
500static void run_parallel(int tasks, void (*fn)(int task, void *data),
501 void *data)
502{
503 pid_t pid[tasks];
504 int i;
505
506 for (i = 0; i < tasks; i++) {
507 pid[i] = fork();
508 if (pid[i] == 0) {
509 fn(i, data);
510 exit(0);
511 } else if (pid[i] == -1) {
512 printf("Couldn't spawn #%d process!\n", i);
513 exit(1);
514 }
515 }
516
517 for (i = 0; i < tasks; i++) {
518 int status;
519
520 assert(waitpid(pid[i], &status, 0) == pid[i]);
521 assert(status == 0);
522 }
523}
524
525static void test_map_stress(void)
526{
527 run_parallel(100, test_hashmap, NULL);
528 run_parallel(100, test_hashmap_percpu, NULL);
Alexei Starovoitov8c290e62017-03-21 19:05:04 -0700529 run_parallel(100, test_hashmap_sizes, NULL);
Daniel Borkmann5ecf51f2017-06-11 00:50:44 +0200530 run_parallel(100, test_hashmap_walk, NULL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200531
532 run_parallel(100, test_arraymap, NULL);
533 run_parallel(100, test_arraymap_percpu, NULL);
534}
535
536#define TASKS 1024
537
538#define DO_UPDATE 1
539#define DO_DELETE 0
540
541static void do_work(int fn, void *data)
542{
543 int do_update = ((int *)data)[1];
544 int fd = ((int *)data)[0];
545 int i, key, value;
546
547 for (i = fn; i < MAP_SIZE; i += TASKS) {
548 key = value = i;
549
550 if (do_update) {
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100551 assert(bpf_map_update_elem(fd, &key, &value,
552 BPF_NOEXIST) == 0);
553 assert(bpf_map_update_elem(fd, &key, &value,
554 BPF_EXIST) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200555 } else {
Mickaël Salaüne58383b2017-02-10 00:21:41 +0100556 assert(bpf_map_delete_elem(fd, &key) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200557 }
558 }
559}
560
561static void test_map_parallel(void)
562{
563 int i, fd, key = 0, value = 0;
564 int data[2];
565
Mickaël Salaünf4874d02017-02-10 00:21:43 +0100566 fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value),
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200567 MAP_SIZE, map_flags);
568 if (fd < 0) {
569 printf("Failed to create map for parallel test '%s'!\n",
570 strerror(errno));
571 exit(1);
572 }
573
574 /* Use the same fd in children to add elements to this map:
575 * child_0 adds key=0, key=1024, key=2048, ...
576 * child_1 adds key=1, key=1025, key=2049, ...
577 * child_1023 adds key=1023, ...
578 */
579 data[0] = fd;
580 data[1] = DO_UPDATE;
581 run_parallel(TASKS, do_work, data);
582
583 /* Check that key=0 is already there. */
Mickaël Salaün10ecc722017-02-10 00:21:39 +0100584 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200585 errno == EEXIST);
586
587 /* Check that all elements were inserted. */
Teng Qin8fe45922017-04-24 19:00:37 -0700588 assert(bpf_map_get_next_key(fd, NULL, &key) == 0);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200589 key = -1;
590 for (i = 0; i < MAP_SIZE; i++)
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100591 assert(bpf_map_get_next_key(fd, &key, &key) == 0);
592 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200593
594 /* Another check for all elements */
595 for (i = 0; i < MAP_SIZE; i++) {
596 key = MAP_SIZE - i - 1;
597
Mickaël Salaüne5ff7c42017-02-10 00:21:40 +0100598 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 &&
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200599 value == key);
600 }
601
602 /* Now let's delete all elemenets in parallel. */
603 data[1] = DO_DELETE;
604 run_parallel(TASKS, do_work, data);
605
606 /* Nothing should be left. */
607 key = -1;
Teng Qin8fe45922017-04-24 19:00:37 -0700608 assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT);
Mickaël Salaün5f155c22017-02-10 00:21:42 +0100609 assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200610}
611
612static void run_all_tests(void)
613{
614 test_hashmap(0, NULL);
615 test_hashmap_percpu(0, NULL);
Daniel Borkmann5ecf51f2017-06-11 00:50:44 +0200616 test_hashmap_walk(0, NULL);
Daniel Borkmann5aa5bd12016-10-17 14:28:36 +0200617
618 test_arraymap(0, NULL);
619 test_arraymap_percpu(0, NULL);
620
621 test_arraymap_percpu_many_keys();
622
623 test_map_large();
624 test_map_parallel();
625 test_map_stress();
626}
627
628int main(void)
629{
630 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
631
632 setrlimit(RLIMIT_MEMLOCK, &rinf);
633
634 map_flags = 0;
635 run_all_tests();
636
637 map_flags = BPF_F_NO_PREALLOC;
638 run_all_tests();
639
640 printf("test_maps: OK\n");
641 return 0;
642}