]>
Commit | Line | Data |
---|---|---|
5aa5bd14 DB |
1 | /* |
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> | |
43b987d2 | 18 | #include <time.h> |
5aa5bd14 DB |
19 | |
20 | #include <sys/wait.h> | |
6bc8529c MKL |
21 | #include <sys/socket.h> |
22 | #include <netinet/in.h> | |
5aa5bd14 DB |
23 | #include <linux/bpf.h> |
24 | ||
10ecc728 | 25 | #include <bpf/bpf.h> |
6f6d33f3 | 26 | #include <bpf/libbpf.h> |
fe8d662a | 27 | |
e00c7b21 | 28 | #include "bpf_util.h" |
fe8d662a | 29 | #include "bpf_rlimit.h" |
5aa5bd14 | 30 | |
6bc8529c MKL |
31 | #ifndef ENOTSUPP |
32 | #define ENOTSUPP 524 | |
33 | #endif | |
34 | ||
5aa5bd14 DB |
35 | static int map_flags; |
36 | ||
6bc8529c MKL |
37 | #define CHECK(condition, tag, format...) ({ \ |
38 | int __ret = !!(condition); \ | |
39 | if (__ret) { \ | |
40 | printf("%s(%d):FAIL:%s ", __func__, __LINE__, tag); \ | |
41 | printf(format); \ | |
42 | exit(-1); \ | |
43 | } \ | |
44 | }) | |
45 | ||
90c93fbe | 46 | static void test_hashmap(unsigned int task, void *data) |
5aa5bd14 | 47 | { |
8fe45924 | 48 | long long key, next_key, first_key, value; |
5aa5bd14 DB |
49 | int fd; |
50 | ||
f4874d01 | 51 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), |
5aa5bd14 DB |
52 | 2, map_flags); |
53 | if (fd < 0) { | |
54 | printf("Failed to create hashmap '%s'!\n", strerror(errno)); | |
55 | exit(1); | |
56 | } | |
57 | ||
58 | key = 1; | |
59 | value = 1234; | |
60 | /* Insert key=1 element. */ | |
10ecc728 | 61 | assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); |
5aa5bd14 DB |
62 | |
63 | value = 0; | |
64 | /* BPF_NOEXIST means add new element if it doesn't exist. */ | |
10ecc728 | 65 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
66 | /* key=1 already exists. */ |
67 | errno == EEXIST); | |
68 | ||
69 | /* -1 is an invalid flag. */ | |
10ecc728 MS |
70 | assert(bpf_map_update_elem(fd, &key, &value, -1) == -1 && |
71 | errno == EINVAL); | |
5aa5bd14 DB |
72 | |
73 | /* Check that key=1 can be found. */ | |
e5ff7c40 | 74 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); |
5aa5bd14 DB |
75 | |
76 | key = 2; | |
77 | /* Check that key=2 is not found. */ | |
e5ff7c40 | 78 | assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); |
5aa5bd14 DB |
79 | |
80 | /* BPF_EXIST means update existing element. */ | |
10ecc728 | 81 | assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && |
5aa5bd14 DB |
82 | /* key=2 is not there. */ |
83 | errno == ENOENT); | |
84 | ||
85 | /* Insert key=2 element. */ | |
10ecc728 | 86 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); |
5aa5bd14 DB |
87 | |
88 | /* key=1 and key=2 were inserted, check that key=0 cannot be | |
89 | * inserted due to max_entries limit. | |
90 | */ | |
91 | key = 0; | |
10ecc728 | 92 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
93 | errno == E2BIG); |
94 | ||
95 | /* Update existing element, though the map is full. */ | |
96 | key = 1; | |
10ecc728 | 97 | assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); |
5aa5bd14 | 98 | key = 2; |
10ecc728 | 99 | assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); |
8c290e60 AS |
100 | key = 3; |
101 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && | |
102 | errno == E2BIG); | |
5aa5bd14 DB |
103 | |
104 | /* Check that key = 0 doesn't exist. */ | |
105 | key = 0; | |
e58383b8 | 106 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); |
5aa5bd14 DB |
107 | |
108 | /* Iterate over two elements. */ | |
8fe45924 TQ |
109 | assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && |
110 | (first_key == 1 || first_key == 2)); | |
5f155c25 | 111 | assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && |
8fe45924 | 112 | (next_key == first_key)); |
5f155c25 | 113 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && |
8fe45924 TQ |
114 | (next_key == 1 || next_key == 2) && |
115 | (next_key != first_key)); | |
5f155c25 | 116 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && |
5aa5bd14 DB |
117 | errno == ENOENT); |
118 | ||
119 | /* Delete both elements. */ | |
120 | key = 1; | |
e58383b8 | 121 | assert(bpf_map_delete_elem(fd, &key) == 0); |
5aa5bd14 | 122 | key = 2; |
e58383b8 MS |
123 | assert(bpf_map_delete_elem(fd, &key) == 0); |
124 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); | |
5aa5bd14 DB |
125 | |
126 | key = 0; | |
127 | /* Check that map is empty. */ | |
8fe45924 TQ |
128 | assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && |
129 | errno == ENOENT); | |
5f155c25 | 130 | assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && |
5aa5bd14 DB |
131 | errno == ENOENT); |
132 | ||
133 | close(fd); | |
134 | } | |
135 | ||
90c93fbe | 136 | static void test_hashmap_sizes(unsigned int task, void *data) |
8c290e60 AS |
137 | { |
138 | int fd, i, j; | |
139 | ||
140 | for (i = 1; i <= 512; i <<= 1) | |
141 | for (j = 1; j <= 1 << 18; j <<= 1) { | |
142 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, i, j, | |
143 | 2, map_flags); | |
144 | if (fd < 0) { | |
80475c48 LZ |
145 | if (errno == ENOMEM) |
146 | return; | |
8c290e60 AS |
147 | printf("Failed to create hashmap key=%d value=%d '%s'\n", |
148 | i, j, strerror(errno)); | |
149 | exit(1); | |
150 | } | |
151 | close(fd); | |
152 | usleep(10); /* give kernel time to destroy */ | |
153 | } | |
154 | } | |
155 | ||
90c93fbe | 156 | static void test_hashmap_percpu(unsigned int task, void *data) |
5aa5bd14 | 157 | { |
e00c7b21 | 158 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
f3515b5d | 159 | BPF_DECLARE_PERCPU(long, value); |
8fe45924 | 160 | long long key, next_key, first_key; |
5aa5bd14 DB |
161 | int expected_key_mask = 0; |
162 | int fd, i; | |
163 | ||
f4874d01 | 164 | fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_HASH, sizeof(key), |
f3515b5d | 165 | sizeof(bpf_percpu(value, 0)), 2, map_flags); |
5aa5bd14 DB |
166 | if (fd < 0) { |
167 | printf("Failed to create hashmap '%s'!\n", strerror(errno)); | |
168 | exit(1); | |
169 | } | |
170 | ||
171 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 172 | bpf_percpu(value, i) = i + 100; |
5aa5bd14 DB |
173 | |
174 | key = 1; | |
175 | /* Insert key=1 element. */ | |
176 | assert(!(expected_key_mask & key)); | |
10ecc728 | 177 | assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); |
5aa5bd14 DB |
178 | expected_key_mask |= key; |
179 | ||
180 | /* BPF_NOEXIST means add new element if it doesn't exist. */ | |
10ecc728 | 181 | assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
182 | /* key=1 already exists. */ |
183 | errno == EEXIST); | |
184 | ||
185 | /* -1 is an invalid flag. */ | |
10ecc728 MS |
186 | assert(bpf_map_update_elem(fd, &key, value, -1) == -1 && |
187 | errno == EINVAL); | |
5aa5bd14 DB |
188 | |
189 | /* Check that key=1 can be found. Value could be 0 if the lookup | |
190 | * was run from a different CPU. | |
191 | */ | |
f3515b5d DB |
192 | bpf_percpu(value, 0) = 1; |
193 | assert(bpf_map_lookup_elem(fd, &key, value) == 0 && | |
194 | bpf_percpu(value, 0) == 100); | |
5aa5bd14 DB |
195 | |
196 | key = 2; | |
197 | /* Check that key=2 is not found. */ | |
e5ff7c40 | 198 | assert(bpf_map_lookup_elem(fd, &key, value) == -1 && errno == ENOENT); |
5aa5bd14 DB |
199 | |
200 | /* BPF_EXIST means update existing element. */ | |
10ecc728 | 201 | assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == -1 && |
5aa5bd14 DB |
202 | /* key=2 is not there. */ |
203 | errno == ENOENT); | |
204 | ||
205 | /* Insert key=2 element. */ | |
206 | assert(!(expected_key_mask & key)); | |
10ecc728 | 207 | assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); |
5aa5bd14 DB |
208 | expected_key_mask |= key; |
209 | ||
210 | /* key=1 and key=2 were inserted, check that key=0 cannot be | |
211 | * inserted due to max_entries limit. | |
212 | */ | |
213 | key = 0; | |
10ecc728 | 214 | assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
215 | errno == E2BIG); |
216 | ||
217 | /* Check that key = 0 doesn't exist. */ | |
e58383b8 | 218 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); |
5aa5bd14 DB |
219 | |
220 | /* Iterate over two elements. */ | |
8fe45924 TQ |
221 | assert(bpf_map_get_next_key(fd, NULL, &first_key) == 0 && |
222 | ((expected_key_mask & first_key) == first_key)); | |
5f155c25 | 223 | while (!bpf_map_get_next_key(fd, &key, &next_key)) { |
8fe45924 TQ |
224 | if (first_key) { |
225 | assert(next_key == first_key); | |
226 | first_key = 0; | |
227 | } | |
5aa5bd14 DB |
228 | assert((expected_key_mask & next_key) == next_key); |
229 | expected_key_mask &= ~next_key; | |
230 | ||
e5ff7c40 | 231 | assert(bpf_map_lookup_elem(fd, &next_key, value) == 0); |
5aa5bd14 DB |
232 | |
233 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 234 | assert(bpf_percpu(value, i) == i + 100); |
5aa5bd14 DB |
235 | |
236 | key = next_key; | |
237 | } | |
238 | assert(errno == ENOENT); | |
239 | ||
240 | /* Update with BPF_EXIST. */ | |
241 | key = 1; | |
10ecc728 | 242 | assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); |
5aa5bd14 DB |
243 | |
244 | /* Delete both elements. */ | |
245 | key = 1; | |
e58383b8 | 246 | assert(bpf_map_delete_elem(fd, &key) == 0); |
5aa5bd14 | 247 | key = 2; |
e58383b8 MS |
248 | assert(bpf_map_delete_elem(fd, &key) == 0); |
249 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == ENOENT); | |
5aa5bd14 DB |
250 | |
251 | key = 0; | |
252 | /* Check that map is empty. */ | |
8fe45924 TQ |
253 | assert(bpf_map_get_next_key(fd, NULL, &next_key) == -1 && |
254 | errno == ENOENT); | |
5f155c25 | 255 | assert(bpf_map_get_next_key(fd, &key, &next_key) == -1 && |
5aa5bd14 DB |
256 | errno == ENOENT); |
257 | ||
258 | close(fd); | |
259 | } | |
260 | ||
bf5d68c7 | 261 | static int helper_fill_hashmap(int max_entries) |
5ecf51fd | 262 | { |
bf5d68c7 LB |
263 | int i, fd, ret; |
264 | long long key, value; | |
5ecf51fd DB |
265 | |
266 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), | |
267 | max_entries, map_flags); | |
bf5d68c7 LB |
268 | CHECK(fd < 0, |
269 | "failed to create hashmap", | |
270 | "err: %s, flags: 0x%x\n", strerror(errno), map_flags); | |
5ecf51fd DB |
271 | |
272 | for (i = 0; i < max_entries; i++) { | |
273 | key = i; value = key; | |
bf5d68c7 LB |
274 | ret = bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST); |
275 | CHECK(ret != 0, | |
276 | "can't update hashmap", | |
277 | "err: %s\n", strerror(ret)); | |
5ecf51fd DB |
278 | } |
279 | ||
bf5d68c7 LB |
280 | return fd; |
281 | } | |
282 | ||
90c93fbe | 283 | static void test_hashmap_walk(unsigned int task, void *data) |
bf5d68c7 LB |
284 | { |
285 | int fd, i, max_entries = 1000; | |
286 | long long key, value, next_key; | |
287 | bool next_key_valid = true; | |
288 | ||
289 | fd = helper_fill_hashmap(max_entries); | |
290 | ||
5ecf51fd DB |
291 | for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, |
292 | &next_key) == 0; i++) { | |
293 | key = next_key; | |
294 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0); | |
295 | } | |
296 | ||
297 | assert(i == max_entries); | |
298 | ||
299 | assert(bpf_map_get_next_key(fd, NULL, &key) == 0); | |
300 | for (i = 0; next_key_valid; i++) { | |
301 | next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; | |
302 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0); | |
303 | value++; | |
304 | assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); | |
305 | key = next_key; | |
306 | } | |
307 | ||
308 | assert(i == max_entries); | |
309 | ||
310 | for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, | |
311 | &next_key) == 0; i++) { | |
312 | key = next_key; | |
313 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0); | |
314 | assert(value - 1 == key); | |
315 | } | |
316 | ||
317 | assert(i == max_entries); | |
318 | close(fd); | |
319 | } | |
320 | ||
bf5d68c7 LB |
321 | static void test_hashmap_zero_seed(void) |
322 | { | |
323 | int i, first, second, old_flags; | |
324 | long long key, next_first, next_second; | |
325 | ||
326 | old_flags = map_flags; | |
327 | map_flags |= BPF_F_ZERO_SEED; | |
328 | ||
329 | first = helper_fill_hashmap(3); | |
330 | second = helper_fill_hashmap(3); | |
331 | ||
332 | for (i = 0; ; i++) { | |
333 | void *key_ptr = !i ? NULL : &key; | |
334 | ||
335 | if (bpf_map_get_next_key(first, key_ptr, &next_first) != 0) | |
336 | break; | |
337 | ||
338 | CHECK(bpf_map_get_next_key(second, key_ptr, &next_second) != 0, | |
339 | "next_key for second map must succeed", | |
340 | "key_ptr: %p", key_ptr); | |
341 | CHECK(next_first != next_second, | |
342 | "keys must match", | |
343 | "i: %d first: %lld second: %lld\n", i, | |
344 | next_first, next_second); | |
345 | ||
346 | key = next_first; | |
347 | } | |
348 | ||
349 | map_flags = old_flags; | |
350 | close(first); | |
351 | close(second); | |
352 | } | |
353 | ||
90c93fbe | 354 | static void test_arraymap(unsigned int task, void *data) |
5aa5bd14 DB |
355 | { |
356 | int key, next_key, fd; | |
357 | long long value; | |
358 | ||
f4874d01 | 359 | fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, sizeof(key), sizeof(value), |
5aa5bd14 DB |
360 | 2, 0); |
361 | if (fd < 0) { | |
362 | printf("Failed to create arraymap '%s'!\n", strerror(errno)); | |
363 | exit(1); | |
364 | } | |
365 | ||
366 | key = 1; | |
367 | value = 1234; | |
368 | /* Insert key=1 element. */ | |
10ecc728 | 369 | assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); |
5aa5bd14 DB |
370 | |
371 | value = 0; | |
10ecc728 | 372 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
373 | errno == EEXIST); |
374 | ||
375 | /* Check that key=1 can be found. */ | |
e5ff7c40 | 376 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); |
5aa5bd14 DB |
377 | |
378 | key = 0; | |
379 | /* Check that key=0 is also found and zero initialized. */ | |
e5ff7c40 | 380 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); |
5aa5bd14 DB |
381 | |
382 | /* key=0 and key=1 were inserted, check that key=2 cannot be inserted | |
383 | * due to max_entries limit. | |
384 | */ | |
385 | key = 2; | |
10ecc728 | 386 | assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == -1 && |
5aa5bd14 DB |
387 | errno == E2BIG); |
388 | ||
389 | /* Check that key = 2 doesn't exist. */ | |
e5ff7c40 | 390 | assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); |
5aa5bd14 DB |
391 | |
392 | /* Iterate over two elements. */ | |
8fe45924 TQ |
393 | assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && |
394 | next_key == 0); | |
5f155c25 | 395 | assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && |
5aa5bd14 | 396 | next_key == 0); |
5f155c25 | 397 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && |
5aa5bd14 | 398 | next_key == 1); |
5f155c25 | 399 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && |
5aa5bd14 DB |
400 | errno == ENOENT); |
401 | ||
402 | /* Delete shouldn't succeed. */ | |
403 | key = 1; | |
e58383b8 | 404 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); |
5aa5bd14 DB |
405 | |
406 | close(fd); | |
407 | } | |
408 | ||
90c93fbe | 409 | static void test_arraymap_percpu(unsigned int task, void *data) |
5aa5bd14 | 410 | { |
e00c7b21 | 411 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
f3515b5d | 412 | BPF_DECLARE_PERCPU(long, values); |
5aa5bd14 | 413 | int key, next_key, fd, i; |
5aa5bd14 | 414 | |
f4874d01 | 415 | fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), |
f3515b5d | 416 | sizeof(bpf_percpu(values, 0)), 2, 0); |
5aa5bd14 DB |
417 | if (fd < 0) { |
418 | printf("Failed to create arraymap '%s'!\n", strerror(errno)); | |
419 | exit(1); | |
420 | } | |
421 | ||
422 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 423 | bpf_percpu(values, i) = i + 100; |
5aa5bd14 DB |
424 | |
425 | key = 1; | |
426 | /* Insert key=1 element. */ | |
10ecc728 | 427 | assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); |
5aa5bd14 | 428 | |
f3515b5d | 429 | bpf_percpu(values, 0) = 0; |
10ecc728 | 430 | assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
431 | errno == EEXIST); |
432 | ||
433 | /* Check that key=1 can be found. */ | |
f3515b5d DB |
434 | assert(bpf_map_lookup_elem(fd, &key, values) == 0 && |
435 | bpf_percpu(values, 0) == 100); | |
5aa5bd14 DB |
436 | |
437 | key = 0; | |
438 | /* Check that key=0 is also found and zero initialized. */ | |
e5ff7c40 | 439 | assert(bpf_map_lookup_elem(fd, &key, values) == 0 && |
f3515b5d DB |
440 | bpf_percpu(values, 0) == 0 && |
441 | bpf_percpu(values, nr_cpus - 1) == 0); | |
5aa5bd14 DB |
442 | |
443 | /* Check that key=2 cannot be inserted due to max_entries limit. */ | |
444 | key = 2; | |
10ecc728 | 445 | assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) == -1 && |
5aa5bd14 DB |
446 | errno == E2BIG); |
447 | ||
448 | /* Check that key = 2 doesn't exist. */ | |
e5ff7c40 | 449 | assert(bpf_map_lookup_elem(fd, &key, values) == -1 && errno == ENOENT); |
5aa5bd14 DB |
450 | |
451 | /* Iterate over two elements. */ | |
8fe45924 TQ |
452 | assert(bpf_map_get_next_key(fd, NULL, &next_key) == 0 && |
453 | next_key == 0); | |
5f155c25 | 454 | assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && |
5aa5bd14 | 455 | next_key == 0); |
5f155c25 | 456 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == 0 && |
5aa5bd14 | 457 | next_key == 1); |
5f155c25 | 458 | assert(bpf_map_get_next_key(fd, &next_key, &next_key) == -1 && |
5aa5bd14 DB |
459 | errno == ENOENT); |
460 | ||
461 | /* Delete shouldn't succeed. */ | |
462 | key = 1; | |
e58383b8 | 463 | assert(bpf_map_delete_elem(fd, &key) == -1 && errno == EINVAL); |
5aa5bd14 DB |
464 | |
465 | close(fd); | |
466 | } | |
467 | ||
468 | static void test_arraymap_percpu_many_keys(void) | |
469 | { | |
e00c7b21 | 470 | unsigned int nr_cpus = bpf_num_possible_cpus(); |
f3515b5d | 471 | BPF_DECLARE_PERCPU(long, values); |
8c290e60 AS |
472 | /* nr_keys is not too large otherwise the test stresses percpu |
473 | * allocator more than anything else | |
474 | */ | |
475 | unsigned int nr_keys = 2000; | |
5aa5bd14 DB |
476 | int key, fd, i; |
477 | ||
f4874d01 | 478 | fd = bpf_create_map(BPF_MAP_TYPE_PERCPU_ARRAY, sizeof(key), |
f3515b5d | 479 | sizeof(bpf_percpu(values, 0)), nr_keys, 0); |
5aa5bd14 DB |
480 | if (fd < 0) { |
481 | printf("Failed to create per-cpu arraymap '%s'!\n", | |
482 | strerror(errno)); | |
483 | exit(1); | |
484 | } | |
485 | ||
486 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 487 | bpf_percpu(values, i) = i + 10; |
5aa5bd14 DB |
488 | |
489 | for (key = 0; key < nr_keys; key++) | |
10ecc728 | 490 | assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); |
5aa5bd14 DB |
491 | |
492 | for (key = 0; key < nr_keys; key++) { | |
493 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 494 | bpf_percpu(values, i) = 0; |
5aa5bd14 | 495 | |
e5ff7c40 | 496 | assert(bpf_map_lookup_elem(fd, &key, values) == 0); |
5aa5bd14 DB |
497 | |
498 | for (i = 0; i < nr_cpus; i++) | |
f3515b5d | 499 | assert(bpf_percpu(values, i) == i + 10); |
5aa5bd14 DB |
500 | } |
501 | ||
502 | close(fd); | |
503 | } | |
504 | ||
90c93fbe | 505 | static void test_devmap(unsigned int task, void *data) |
546ac1ff | 506 | { |
81f6bf81 | 507 | int fd; |
546ac1ff JF |
508 | __u32 key, value; |
509 | ||
510 | fd = bpf_create_map(BPF_MAP_TYPE_DEVMAP, sizeof(key), sizeof(value), | |
511 | 2, 0); | |
512 | if (fd < 0) { | |
8b6b25cf | 513 | printf("Failed to create devmap '%s'!\n", strerror(errno)); |
546ac1ff JF |
514 | exit(1); |
515 | } | |
516 | ||
517 | close(fd); | |
518 | } | |
519 | ||
90c93fbe | 520 | static void test_queuemap(unsigned int task, void *data) |
43b987d2 MV |
521 | { |
522 | const int MAP_SIZE = 32; | |
523 | __u32 vals[MAP_SIZE + MAP_SIZE/2], val; | |
524 | int fd, i; | |
525 | ||
526 | /* Fill test values to be used */ | |
527 | for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) | |
528 | vals[i] = rand(); | |
529 | ||
530 | /* Invalid key size */ | |
531 | fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 4, sizeof(val), MAP_SIZE, | |
532 | map_flags); | |
533 | assert(fd < 0 && errno == EINVAL); | |
534 | ||
535 | fd = bpf_create_map(BPF_MAP_TYPE_QUEUE, 0, sizeof(val), MAP_SIZE, | |
536 | map_flags); | |
537 | /* Queue map does not support BPF_F_NO_PREALLOC */ | |
538 | if (map_flags & BPF_F_NO_PREALLOC) { | |
539 | assert(fd < 0 && errno == EINVAL); | |
540 | return; | |
541 | } | |
542 | if (fd < 0) { | |
543 | printf("Failed to create queuemap '%s'!\n", strerror(errno)); | |
544 | exit(1); | |
545 | } | |
546 | ||
547 | /* Push MAP_SIZE elements */ | |
548 | for (i = 0; i < MAP_SIZE; i++) | |
549 | assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); | |
550 | ||
551 | /* Check that element cannot be pushed due to max_entries limit */ | |
552 | assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 && | |
553 | errno == E2BIG); | |
554 | ||
555 | /* Peek element */ | |
556 | assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[0]); | |
557 | ||
558 | /* Replace half elements */ | |
559 | for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) | |
560 | assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); | |
561 | ||
562 | /* Pop all elements */ | |
563 | for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++) | |
564 | assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && | |
565 | val == vals[i]); | |
566 | ||
567 | /* Check that there are not elements left */ | |
568 | assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 && | |
569 | errno == ENOENT); | |
570 | ||
571 | /* Check that non supported functions set errno to EINVAL */ | |
572 | assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL); | |
573 | assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL); | |
574 | ||
575 | close(fd); | |
576 | } | |
577 | ||
90c93fbe | 578 | static void test_stackmap(unsigned int task, void *data) |
43b987d2 MV |
579 | { |
580 | const int MAP_SIZE = 32; | |
581 | __u32 vals[MAP_SIZE + MAP_SIZE/2], val; | |
582 | int fd, i; | |
583 | ||
584 | /* Fill test values to be used */ | |
585 | for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) | |
586 | vals[i] = rand(); | |
587 | ||
588 | /* Invalid key size */ | |
589 | fd = bpf_create_map(BPF_MAP_TYPE_STACK, 4, sizeof(val), MAP_SIZE, | |
590 | map_flags); | |
591 | assert(fd < 0 && errno == EINVAL); | |
592 | ||
593 | fd = bpf_create_map(BPF_MAP_TYPE_STACK, 0, sizeof(val), MAP_SIZE, | |
594 | map_flags); | |
595 | /* Stack map does not support BPF_F_NO_PREALLOC */ | |
596 | if (map_flags & BPF_F_NO_PREALLOC) { | |
597 | assert(fd < 0 && errno == EINVAL); | |
598 | return; | |
599 | } | |
600 | if (fd < 0) { | |
601 | printf("Failed to create stackmap '%s'!\n", strerror(errno)); | |
602 | exit(1); | |
603 | } | |
604 | ||
605 | /* Push MAP_SIZE elements */ | |
606 | for (i = 0; i < MAP_SIZE; i++) | |
607 | assert(bpf_map_update_elem(fd, NULL, &vals[i], 0) == 0); | |
608 | ||
609 | /* Check that element cannot be pushed due to max_entries limit */ | |
610 | assert(bpf_map_update_elem(fd, NULL, &val, 0) == -1 && | |
611 | errno == E2BIG); | |
612 | ||
613 | /* Peek element */ | |
614 | assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]); | |
615 | ||
616 | /* Replace half elements */ | |
617 | for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) | |
618 | assert(bpf_map_update_elem(fd, NULL, &vals[i], BPF_EXIST) == 0); | |
619 | ||
620 | /* Pop all elements */ | |
621 | for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--) | |
622 | assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == 0 && | |
623 | val == vals[i]); | |
624 | ||
625 | /* Check that there are not elements left */ | |
626 | assert(bpf_map_lookup_and_delete_elem(fd, NULL, &val) == -1 && | |
627 | errno == ENOENT); | |
628 | ||
629 | /* Check that non supported functions set errno to EINVAL */ | |
630 | assert(bpf_map_delete_elem(fd, NULL) == -1 && errno == EINVAL); | |
631 | assert(bpf_map_get_next_key(fd, NULL, NULL) == -1 && errno == EINVAL); | |
632 | ||
633 | close(fd); | |
634 | } | |
635 | ||
6f6d33f3 JF |
636 | #include <sys/socket.h> |
637 | #include <sys/ioctl.h> | |
638 | #include <arpa/inet.h> | |
639 | #include <sys/select.h> | |
640 | #include <linux/err.h> | |
641 | #define SOCKMAP_PARSE_PROG "./sockmap_parse_prog.o" | |
642 | #define SOCKMAP_VERDICT_PROG "./sockmap_verdict_prog.o" | |
82a86168 | 643 | #define SOCKMAP_TCP_MSG_PROG "./sockmap_tcp_msg_prog.o" |
90c93fbe | 644 | static void test_sockmap(unsigned int tasks, void *data) |
6f6d33f3 | 645 | { |
82a86168 JF |
646 | struct bpf_map *bpf_map_rx, *bpf_map_tx, *bpf_map_msg, *bpf_map_break; |
647 | int map_fd_msg = 0, map_fd_rx = 0, map_fd_tx = 0, map_fd_break; | |
6f6d33f3 | 648 | int ports[] = {50200, 50201, 50202, 50204}; |
435bf0d3 | 649 | int err, i, fd, udp, sfd[6] = {0xdeadbeef}; |
6fd28865 | 650 | u8 buf[20] = {0x0, 0x5, 0x3, 0x2, 0x1, 0x0}; |
82a86168 | 651 | int parse_prog, verdict_prog, msg_prog; |
6f6d33f3 | 652 | struct sockaddr_in addr; |
82a86168 | 653 | int one = 1, s, sc, rc; |
6f6d33f3 JF |
654 | struct bpf_object *obj; |
655 | struct timeval to; | |
656 | __u32 key, value; | |
3f0d6a16 | 657 | pid_t pid[tasks]; |
6f6d33f3 JF |
658 | fd_set w; |
659 | ||
660 | /* Create some sockets to use with sockmap */ | |
661 | for (i = 0; i < 2; i++) { | |
662 | sfd[i] = socket(AF_INET, SOCK_STREAM, 0); | |
663 | if (sfd[i] < 0) | |
664 | goto out; | |
665 | err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, | |
666 | (char *)&one, sizeof(one)); | |
667 | if (err) { | |
668 | printf("failed to setsockopt\n"); | |
669 | goto out; | |
670 | } | |
671 | err = ioctl(sfd[i], FIONBIO, (char *)&one); | |
672 | if (err < 0) { | |
673 | printf("failed to ioctl\n"); | |
674 | goto out; | |
675 | } | |
676 | memset(&addr, 0, sizeof(struct sockaddr_in)); | |
677 | addr.sin_family = AF_INET; | |
678 | addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
679 | addr.sin_port = htons(ports[i]); | |
680 | err = bind(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); | |
681 | if (err < 0) { | |
682 | printf("failed to bind: err %i: %i:%i\n", | |
683 | err, i, sfd[i]); | |
684 | goto out; | |
685 | } | |
686 | err = listen(sfd[i], 32); | |
687 | if (err < 0) { | |
90774a93 | 688 | printf("failed to listen\n"); |
6f6d33f3 JF |
689 | goto out; |
690 | } | |
691 | } | |
692 | ||
693 | for (i = 2; i < 4; i++) { | |
694 | sfd[i] = socket(AF_INET, SOCK_STREAM, 0); | |
695 | if (sfd[i] < 0) | |
696 | goto out; | |
697 | err = setsockopt(sfd[i], SOL_SOCKET, SO_REUSEADDR, | |
698 | (char *)&one, sizeof(one)); | |
699 | if (err) { | |
700 | printf("set sock opt\n"); | |
701 | goto out; | |
702 | } | |
703 | memset(&addr, 0, sizeof(struct sockaddr_in)); | |
704 | addr.sin_family = AF_INET; | |
705 | addr.sin_addr.s_addr = inet_addr("127.0.0.1"); | |
706 | addr.sin_port = htons(ports[i - 2]); | |
707 | err = connect(sfd[i], (struct sockaddr *)&addr, sizeof(addr)); | |
708 | if (err) { | |
90774a93 | 709 | printf("failed to connect\n"); |
6f6d33f3 JF |
710 | goto out; |
711 | } | |
712 | } | |
713 | ||
714 | ||
715 | for (i = 4; i < 6; i++) { | |
716 | sfd[i] = accept(sfd[i - 4], NULL, NULL); | |
717 | if (sfd[i] < 0) { | |
718 | printf("accept failed\n"); | |
719 | goto out; | |
720 | } | |
721 | } | |
722 | ||
723 | /* Test sockmap with connected sockets */ | |
724 | fd = bpf_create_map(BPF_MAP_TYPE_SOCKMAP, | |
725 | sizeof(key), sizeof(value), | |
726 | 6, 0); | |
727 | if (fd < 0) { | |
728 | printf("Failed to create sockmap %i\n", fd); | |
729 | goto out_sockmap; | |
730 | } | |
731 | ||
435bf0d3 JF |
732 | /* Test update with unsupported UDP socket */ |
733 | udp = socket(AF_INET, SOCK_DGRAM, 0); | |
734 | i = 0; | |
735 | err = bpf_map_update_elem(fd, &i, &udp, BPF_ANY); | |
736 | if (!err) { | |
737 | printf("Failed socket SOCK_DGRAM allowed '%i:%i'\n", | |
738 | i, udp); | |
739 | goto out_sockmap; | |
6f6d33f3 JF |
740 | } |
741 | ||
464bc0fd | 742 | /* Test update without programs */ |
6f6d33f3 JF |
743 | for (i = 0; i < 6; i++) { |
744 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); | |
50280278 JF |
745 | if (i < 2 && !err) { |
746 | printf("Allowed update sockmap '%i:%i' not in ESTABLISHED\n", | |
747 | i, sfd[i]); | |
748 | goto out_sockmap; | |
749 | } else if (i >= 2 && err) { | |
464bc0fd | 750 | printf("Failed noprog update sockmap '%i:%i'\n", |
6f6d33f3 JF |
751 | i, sfd[i]); |
752 | goto out_sockmap; | |
753 | } | |
754 | } | |
755 | ||
5a67da2a | 756 | /* Test attaching/detaching bad fds */ |
464bc0fd | 757 | err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0); |
6f6d33f3 | 758 | if (!err) { |
464bc0fd JF |
759 | printf("Failed invalid parser prog attach\n"); |
760 | goto out_sockmap; | |
761 | } | |
762 | ||
763 | err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0); | |
764 | if (!err) { | |
765 | printf("Failed invalid verdict prog attach\n"); | |
6f6d33f3 JF |
766 | goto out_sockmap; |
767 | } | |
768 | ||
82a86168 JF |
769 | err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0); |
770 | if (!err) { | |
771 | printf("Failed invalid msg verdict prog attach\n"); | |
772 | goto out_sockmap; | |
773 | } | |
774 | ||
5a67da2a JF |
775 | err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0); |
776 | if (!err) { | |
777 | printf("Failed unknown prog attach\n"); | |
778 | goto out_sockmap; | |
779 | } | |
780 | ||
781 | err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_PARSER); | |
782 | if (err) { | |
783 | printf("Failed empty parser prog detach\n"); | |
784 | goto out_sockmap; | |
785 | } | |
786 | ||
787 | err = bpf_prog_detach(fd, BPF_SK_SKB_STREAM_VERDICT); | |
788 | if (err) { | |
789 | printf("Failed empty verdict prog detach\n"); | |
790 | goto out_sockmap; | |
791 | } | |
792 | ||
82a86168 JF |
793 | err = bpf_prog_detach(fd, BPF_SK_MSG_VERDICT); |
794 | if (err) { | |
795 | printf("Failed empty msg verdict prog detach\n"); | |
796 | goto out_sockmap; | |
797 | } | |
798 | ||
5a67da2a JF |
799 | err = bpf_prog_detach(fd, __MAX_BPF_ATTACH_TYPE); |
800 | if (!err) { | |
801 | printf("Detach invalid prog successful\n"); | |
802 | goto out_sockmap; | |
803 | } | |
804 | ||
6f6d33f3 JF |
805 | /* Load SK_SKB program and Attach */ |
806 | err = bpf_prog_load(SOCKMAP_PARSE_PROG, | |
807 | BPF_PROG_TYPE_SK_SKB, &obj, &parse_prog); | |
808 | if (err) { | |
809 | printf("Failed to load SK_SKB parse prog\n"); | |
810 | goto out_sockmap; | |
811 | } | |
812 | ||
82a86168 JF |
813 | err = bpf_prog_load(SOCKMAP_TCP_MSG_PROG, |
814 | BPF_PROG_TYPE_SK_MSG, &obj, &msg_prog); | |
815 | if (err) { | |
816 | printf("Failed to load SK_SKB msg prog\n"); | |
817 | goto out_sockmap; | |
818 | } | |
819 | ||
6f6d33f3 JF |
820 | err = bpf_prog_load(SOCKMAP_VERDICT_PROG, |
821 | BPF_PROG_TYPE_SK_SKB, &obj, &verdict_prog); | |
822 | if (err) { | |
823 | printf("Failed to load SK_SKB verdict prog\n"); | |
824 | goto out_sockmap; | |
825 | } | |
826 | ||
6fd28865 JF |
827 | bpf_map_rx = bpf_object__find_map_by_name(obj, "sock_map_rx"); |
828 | if (IS_ERR(bpf_map_rx)) { | |
829 | printf("Failed to load map rx from verdict prog\n"); | |
6f6d33f3 JF |
830 | goto out_sockmap; |
831 | } | |
832 | ||
6fd28865 JF |
833 | map_fd_rx = bpf_map__fd(bpf_map_rx); |
834 | if (map_fd_rx < 0) { | |
82a86168 | 835 | printf("Failed to get map rx fd\n"); |
6f6d33f3 JF |
836 | goto out_sockmap; |
837 | } | |
838 | ||
6fd28865 JF |
839 | bpf_map_tx = bpf_object__find_map_by_name(obj, "sock_map_tx"); |
840 | if (IS_ERR(bpf_map_tx)) { | |
841 | printf("Failed to load map tx from verdict prog\n"); | |
842 | goto out_sockmap; | |
843 | } | |
844 | ||
845 | map_fd_tx = bpf_map__fd(bpf_map_tx); | |
846 | if (map_fd_tx < 0) { | |
847 | printf("Failed to get map tx fd\n"); | |
848 | goto out_sockmap; | |
849 | } | |
850 | ||
82a86168 JF |
851 | bpf_map_msg = bpf_object__find_map_by_name(obj, "sock_map_msg"); |
852 | if (IS_ERR(bpf_map_msg)) { | |
853 | printf("Failed to load map msg from msg_verdict prog\n"); | |
854 | goto out_sockmap; | |
855 | } | |
856 | ||
857 | map_fd_msg = bpf_map__fd(bpf_map_msg); | |
858 | if (map_fd_msg < 0) { | |
859 | printf("Failed to get map msg fd\n"); | |
860 | goto out_sockmap; | |
861 | } | |
862 | ||
81374aaa JF |
863 | bpf_map_break = bpf_object__find_map_by_name(obj, "sock_map_break"); |
864 | if (IS_ERR(bpf_map_break)) { | |
865 | printf("Failed to load map tx from verdict prog\n"); | |
866 | goto out_sockmap; | |
867 | } | |
868 | ||
869 | map_fd_break = bpf_map__fd(bpf_map_break); | |
870 | if (map_fd_break < 0) { | |
871 | printf("Failed to get map tx fd\n"); | |
872 | goto out_sockmap; | |
873 | } | |
874 | ||
875 | err = bpf_prog_attach(parse_prog, map_fd_break, | |
876 | BPF_SK_SKB_STREAM_PARSER, 0); | |
877 | if (!err) { | |
878 | printf("Allowed attaching SK_SKB program to invalid map\n"); | |
879 | goto out_sockmap; | |
880 | } | |
881 | ||
6fd28865 | 882 | err = bpf_prog_attach(parse_prog, map_fd_rx, |
464bc0fd JF |
883 | BPF_SK_SKB_STREAM_PARSER, 0); |
884 | if (err) { | |
81374aaa | 885 | printf("Failed stream parser bpf prog attach\n"); |
464bc0fd JF |
886 | goto out_sockmap; |
887 | } | |
888 | ||
6fd28865 | 889 | err = bpf_prog_attach(verdict_prog, map_fd_rx, |
464bc0fd | 890 | BPF_SK_SKB_STREAM_VERDICT, 0); |
6f6d33f3 | 891 | if (err) { |
81374aaa | 892 | printf("Failed stream verdict bpf prog attach\n"); |
6f6d33f3 JF |
893 | goto out_sockmap; |
894 | } | |
895 | ||
82a86168 JF |
896 | err = bpf_prog_attach(msg_prog, map_fd_msg, BPF_SK_MSG_VERDICT, 0); |
897 | if (err) { | |
898 | printf("Failed msg verdict bpf prog attach\n"); | |
899 | goto out_sockmap; | |
900 | } | |
901 | ||
5a67da2a JF |
902 | err = bpf_prog_attach(verdict_prog, map_fd_rx, |
903 | __MAX_BPF_ATTACH_TYPE, 0); | |
904 | if (!err) { | |
905 | printf("Attached unknown bpf prog\n"); | |
906 | goto out_sockmap; | |
907 | } | |
908 | ||
464bc0fd | 909 | /* Test map update elem afterwards fd lives in fd and map_fd */ |
50280278 | 910 | for (i = 2; i < 6; i++) { |
6fd28865 JF |
911 | err = bpf_map_update_elem(map_fd_rx, &i, &sfd[i], BPF_ANY); |
912 | if (err) { | |
913 | printf("Failed map_fd_rx update sockmap %i '%i:%i'\n", | |
914 | err, i, sfd[i]); | |
915 | goto out_sockmap; | |
916 | } | |
917 | err = bpf_map_update_elem(map_fd_tx, &i, &sfd[i], BPF_ANY); | |
6f6d33f3 | 918 | if (err) { |
6fd28865 | 919 | printf("Failed map_fd_tx update sockmap %i '%i:%i'\n", |
6f6d33f3 JF |
920 | err, i, sfd[i]); |
921 | goto out_sockmap; | |
922 | } | |
923 | } | |
924 | ||
925 | /* Test map delete elem and remove send/recv sockets */ | |
926 | for (i = 2; i < 4; i++) { | |
6fd28865 JF |
927 | err = bpf_map_delete_elem(map_fd_rx, &i); |
928 | if (err) { | |
929 | printf("Failed delete sockmap rx %i '%i:%i'\n", | |
930 | err, i, sfd[i]); | |
931 | goto out_sockmap; | |
932 | } | |
933 | err = bpf_map_delete_elem(map_fd_tx, &i); | |
6f6d33f3 | 934 | if (err) { |
6fd28865 | 935 | printf("Failed delete sockmap tx %i '%i:%i'\n", |
6f6d33f3 JF |
936 | err, i, sfd[i]); |
937 | goto out_sockmap; | |
938 | } | |
939 | } | |
940 | ||
82a86168 JF |
941 | /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */ |
942 | i = 0; | |
943 | err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY); | |
944 | if (err) { | |
945 | printf("Failed map_fd_msg update sockmap %i\n", err); | |
946 | goto out_sockmap; | |
947 | } | |
948 | ||
6f6d33f3 | 949 | /* Test map send/recv */ |
6fd28865 JF |
950 | for (i = 0; i < 2; i++) { |
951 | buf[0] = i; | |
952 | buf[1] = 0x5; | |
953 | sc = send(sfd[2], buf, 20, 0); | |
954 | if (sc < 0) { | |
955 | printf("Failed sockmap send\n"); | |
956 | goto out_sockmap; | |
957 | } | |
6f6d33f3 | 958 | |
6fd28865 JF |
959 | FD_ZERO(&w); |
960 | FD_SET(sfd[3], &w); | |
961 | to.tv_sec = 1; | |
962 | to.tv_usec = 0; | |
963 | s = select(sfd[3] + 1, &w, NULL, NULL, &to); | |
964 | if (s == -1) { | |
965 | perror("Failed sockmap select()"); | |
966 | goto out_sockmap; | |
967 | } else if (!s) { | |
968 | printf("Failed sockmap unexpected timeout\n"); | |
969 | goto out_sockmap; | |
970 | } | |
6f6d33f3 | 971 | |
6fd28865 JF |
972 | if (!FD_ISSET(sfd[3], &w)) { |
973 | printf("Failed sockmap select/recv\n"); | |
974 | goto out_sockmap; | |
975 | } | |
976 | ||
977 | rc = recv(sfd[3], buf, sizeof(buf), 0); | |
978 | if (rc < 0) { | |
979 | printf("Failed sockmap recv\n"); | |
980 | goto out_sockmap; | |
981 | } | |
6f6d33f3 JF |
982 | } |
983 | ||
6fd28865 JF |
984 | /* Negative null entry lookup from datapath should be dropped */ |
985 | buf[0] = 1; | |
986 | buf[1] = 12; | |
987 | sc = send(sfd[2], buf, 20, 0); | |
988 | if (sc < 0) { | |
989 | printf("Failed sockmap send\n"); | |
6f6d33f3 JF |
990 | goto out_sockmap; |
991 | } | |
992 | ||
464bc0fd JF |
993 | /* Push fd into same slot */ |
994 | i = 2; | |
6f6d33f3 JF |
995 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); |
996 | if (!err) { | |
464bc0fd | 997 | printf("Failed allowed sockmap dup slot BPF_NOEXIST\n"); |
6f6d33f3 JF |
998 | goto out_sockmap; |
999 | } | |
1000 | ||
1001 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); | |
1002 | if (err) { | |
464bc0fd | 1003 | printf("Failed sockmap update new slot BPF_ANY\n"); |
6f6d33f3 JF |
1004 | goto out_sockmap; |
1005 | } | |
1006 | ||
1007 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); | |
1008 | if (err) { | |
464bc0fd | 1009 | printf("Failed sockmap update new slot BPF_EXIST\n"); |
6f6d33f3 JF |
1010 | goto out_sockmap; |
1011 | } | |
1012 | ||
464bc0fd | 1013 | /* Delete the elems without programs */ |
50280278 | 1014 | for (i = 2; i < 6; i++) { |
464bc0fd JF |
1015 | err = bpf_map_delete_elem(fd, &i); |
1016 | if (err) { | |
1017 | printf("Failed delete sockmap %i '%i:%i'\n", | |
1018 | err, i, sfd[i]); | |
1019 | } | |
6f6d33f3 JF |
1020 | } |
1021 | ||
464bc0fd JF |
1022 | /* Test having multiple maps open and set with programs on same fds */ |
1023 | err = bpf_prog_attach(parse_prog, fd, | |
1024 | BPF_SK_SKB_STREAM_PARSER, 0); | |
6f6d33f3 | 1025 | if (err) { |
464bc0fd | 1026 | printf("Failed fd bpf parse prog attach\n"); |
6f6d33f3 JF |
1027 | goto out_sockmap; |
1028 | } | |
464bc0fd JF |
1029 | err = bpf_prog_attach(verdict_prog, fd, |
1030 | BPF_SK_SKB_STREAM_VERDICT, 0); | |
6f6d33f3 | 1031 | if (err) { |
464bc0fd | 1032 | printf("Failed fd bpf verdict prog attach\n"); |
6f6d33f3 JF |
1033 | goto out_sockmap; |
1034 | } | |
1035 | ||
464bc0fd JF |
1036 | for (i = 4; i < 6; i++) { |
1037 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_ANY); | |
1038 | if (!err) { | |
1039 | printf("Failed allowed duplicate programs in update ANY sockmap %i '%i:%i'\n", | |
1040 | err, i, sfd[i]); | |
1041 | goto out_sockmap; | |
1042 | } | |
1043 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_NOEXIST); | |
1044 | if (!err) { | |
1045 | printf("Failed allowed duplicate program in update NOEXIST sockmap %i '%i:%i'\n", | |
1046 | err, i, sfd[i]); | |
1047 | goto out_sockmap; | |
1048 | } | |
1049 | err = bpf_map_update_elem(fd, &i, &sfd[i], BPF_EXIST); | |
1050 | if (!err) { | |
1051 | printf("Failed allowed duplicate program in update EXIST sockmap %i '%i:%i'\n", | |
1052 | err, i, sfd[i]); | |
1053 | goto out_sockmap; | |
1054 | } | |
6f6d33f3 JF |
1055 | } |
1056 | ||
3f0d6a16 JF |
1057 | /* Test tasks number of forked operations */ |
1058 | for (i = 0; i < tasks; i++) { | |
1059 | pid[i] = fork(); | |
1060 | if (pid[i] == 0) { | |
1061 | for (i = 0; i < 6; i++) { | |
1062 | bpf_map_delete_elem(map_fd_tx, &i); | |
1063 | bpf_map_delete_elem(map_fd_rx, &i); | |
1064 | bpf_map_update_elem(map_fd_tx, &i, | |
1065 | &sfd[i], BPF_ANY); | |
1066 | bpf_map_update_elem(map_fd_rx, &i, | |
1067 | &sfd[i], BPF_ANY); | |
1068 | } | |
1069 | exit(0); | |
1070 | } else if (pid[i] == -1) { | |
1071 | printf("Couldn't spawn #%d process!\n", i); | |
1072 | exit(1); | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | for (i = 0; i < tasks; i++) { | |
1077 | int status; | |
1078 | ||
1079 | assert(waitpid(pid[i], &status, 0) == pid[i]); | |
1080 | assert(status == 0); | |
1081 | } | |
1082 | ||
5a67da2a JF |
1083 | err = bpf_prog_detach(map_fd_rx, __MAX_BPF_ATTACH_TYPE); |
1084 | if (!err) { | |
1085 | printf("Detached an invalid prog type.\n"); | |
1086 | goto out_sockmap; | |
1087 | } | |
1088 | ||
1089 | err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_PARSER); | |
1090 | if (err) { | |
1091 | printf("Failed parser prog detach\n"); | |
1092 | goto out_sockmap; | |
1093 | } | |
1094 | ||
1095 | err = bpf_prog_detach(map_fd_rx, BPF_SK_SKB_STREAM_VERDICT); | |
1096 | if (err) { | |
1097 | printf("Failed parser prog detach\n"); | |
1098 | goto out_sockmap; | |
1099 | } | |
1100 | ||
78368781 PB |
1101 | /* Test map close sockets and empty maps */ |
1102 | for (i = 0; i < 6; i++) { | |
1103 | bpf_map_delete_elem(map_fd_tx, &i); | |
1104 | bpf_map_delete_elem(map_fd_rx, &i); | |
6f6d33f3 | 1105 | close(sfd[i]); |
78368781 | 1106 | } |
6f6d33f3 | 1107 | close(fd); |
6fd28865 | 1108 | close(map_fd_rx); |
6f6d33f3 JF |
1109 | bpf_object__close(obj); |
1110 | return; | |
1111 | out: | |
1112 | for (i = 0; i < 6; i++) | |
1113 | close(sfd[i]); | |
1114 | printf("Failed to create sockmap '%i:%s'!\n", i, strerror(errno)); | |
1115 | exit(1); | |
1116 | out_sockmap: | |
78368781 PB |
1117 | for (i = 0; i < 6; i++) { |
1118 | if (map_fd_tx) | |
1119 | bpf_map_delete_elem(map_fd_tx, &i); | |
1120 | if (map_fd_rx) | |
1121 | bpf_map_delete_elem(map_fd_rx, &i); | |
6f6d33f3 | 1122 | close(sfd[i]); |
78368781 | 1123 | } |
6f6d33f3 JF |
1124 | close(fd); |
1125 | exit(1); | |
1126 | } | |
1127 | ||
b1957c92 NS |
1128 | #define MAPINMAP_PROG "./test_map_in_map.o" |
1129 | static void test_map_in_map(void) | |
1130 | { | |
1131 | struct bpf_program *prog; | |
1132 | struct bpf_object *obj; | |
1133 | struct bpf_map *map; | |
1134 | int mim_fd, fd, err; | |
1135 | int pos = 0; | |
1136 | ||
1137 | obj = bpf_object__open(MAPINMAP_PROG); | |
1138 | ||
1139 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(int), sizeof(int), | |
1140 | 2, 0); | |
1141 | if (fd < 0) { | |
1142 | printf("Failed to create hashmap '%s'!\n", strerror(errno)); | |
1143 | exit(1); | |
1144 | } | |
1145 | ||
1146 | map = bpf_object__find_map_by_name(obj, "mim_array"); | |
1147 | if (IS_ERR(map)) { | |
1148 | printf("Failed to load array of maps from test prog\n"); | |
1149 | goto out_map_in_map; | |
1150 | } | |
1151 | err = bpf_map__set_inner_map_fd(map, fd); | |
1152 | if (err) { | |
1153 | printf("Failed to set inner_map_fd for array of maps\n"); | |
1154 | goto out_map_in_map; | |
1155 | } | |
1156 | ||
1157 | map = bpf_object__find_map_by_name(obj, "mim_hash"); | |
1158 | if (IS_ERR(map)) { | |
1159 | printf("Failed to load hash of maps from test prog\n"); | |
1160 | goto out_map_in_map; | |
1161 | } | |
1162 | err = bpf_map__set_inner_map_fd(map, fd); | |
1163 | if (err) { | |
1164 | printf("Failed to set inner_map_fd for hash of maps\n"); | |
1165 | goto out_map_in_map; | |
1166 | } | |
1167 | ||
1168 | bpf_object__for_each_program(prog, obj) { | |
1169 | bpf_program__set_xdp(prog); | |
1170 | } | |
1171 | bpf_object__load(obj); | |
1172 | ||
1173 | map = bpf_object__find_map_by_name(obj, "mim_array"); | |
1174 | if (IS_ERR(map)) { | |
1175 | printf("Failed to load array of maps from test prog\n"); | |
1176 | goto out_map_in_map; | |
1177 | } | |
1178 | mim_fd = bpf_map__fd(map); | |
1179 | if (mim_fd < 0) { | |
1180 | printf("Failed to get descriptor for array of maps\n"); | |
1181 | goto out_map_in_map; | |
1182 | } | |
1183 | ||
1184 | err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); | |
1185 | if (err) { | |
1186 | printf("Failed to update array of maps\n"); | |
1187 | goto out_map_in_map; | |
1188 | } | |
1189 | ||
1190 | map = bpf_object__find_map_by_name(obj, "mim_hash"); | |
1191 | if (IS_ERR(map)) { | |
1192 | printf("Failed to load hash of maps from test prog\n"); | |
1193 | goto out_map_in_map; | |
1194 | } | |
1195 | mim_fd = bpf_map__fd(map); | |
1196 | if (mim_fd < 0) { | |
1197 | printf("Failed to get descriptor for hash of maps\n"); | |
1198 | goto out_map_in_map; | |
1199 | } | |
1200 | ||
1201 | err = bpf_map_update_elem(mim_fd, &pos, &fd, 0); | |
1202 | if (err) { | |
1203 | printf("Failed to update hash of maps\n"); | |
1204 | goto out_map_in_map; | |
1205 | } | |
1206 | ||
1207 | close(fd); | |
1208 | bpf_object__close(obj); | |
1209 | return; | |
1210 | ||
1211 | out_map_in_map: | |
1212 | close(fd); | |
1213 | exit(1); | |
1214 | } | |
1215 | ||
5aa5bd14 DB |
1216 | #define MAP_SIZE (32 * 1024) |
1217 | ||
1218 | static void test_map_large(void) | |
1219 | { | |
1220 | struct bigkey { | |
1221 | int a; | |
1222 | char b[116]; | |
1223 | long long c; | |
1224 | } key; | |
1225 | int fd, i, value; | |
1226 | ||
f4874d01 | 1227 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), |
5aa5bd14 DB |
1228 | MAP_SIZE, map_flags); |
1229 | if (fd < 0) { | |
1230 | printf("Failed to create large map '%s'!\n", strerror(errno)); | |
1231 | exit(1); | |
1232 | } | |
1233 | ||
1234 | for (i = 0; i < MAP_SIZE; i++) { | |
1235 | key = (struct bigkey) { .c = i }; | |
1236 | value = i; | |
1237 | ||
10ecc728 | 1238 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); |
5aa5bd14 DB |
1239 | } |
1240 | ||
1241 | key.c = -1; | |
10ecc728 | 1242 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
1243 | errno == E2BIG); |
1244 | ||
1245 | /* Iterate through all elements. */ | |
8fe45924 TQ |
1246 | assert(bpf_map_get_next_key(fd, NULL, &key) == 0); |
1247 | key.c = -1; | |
5aa5bd14 | 1248 | for (i = 0; i < MAP_SIZE; i++) |
5f155c25 MS |
1249 | assert(bpf_map_get_next_key(fd, &key, &key) == 0); |
1250 | assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); | |
5aa5bd14 DB |
1251 | |
1252 | key.c = 0; | |
e5ff7c40 | 1253 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); |
5aa5bd14 | 1254 | key.a = 1; |
e5ff7c40 | 1255 | assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); |
5aa5bd14 DB |
1256 | |
1257 | close(fd); | |
1258 | } | |
1259 | ||
1a97cf1f | 1260 | #define run_parallel(N, FN, DATA) \ |
90c93fbe | 1261 | printf("Fork %u tasks to '" #FN "'\n", N); \ |
1a97cf1f AS |
1262 | __run_parallel(N, FN, DATA) |
1263 | ||
90c93fbe BL |
1264 | static void __run_parallel(unsigned int tasks, |
1265 | void (*fn)(unsigned int task, void *data), | |
1a97cf1f | 1266 | void *data) |
5aa5bd14 DB |
1267 | { |
1268 | pid_t pid[tasks]; | |
1269 | int i; | |
1270 | ||
1271 | for (i = 0; i < tasks; i++) { | |
1272 | pid[i] = fork(); | |
1273 | if (pid[i] == 0) { | |
1274 | fn(i, data); | |
1275 | exit(0); | |
1276 | } else if (pid[i] == -1) { | |
1277 | printf("Couldn't spawn #%d process!\n", i); | |
1278 | exit(1); | |
1279 | } | |
1280 | } | |
1281 | ||
1282 | for (i = 0; i < tasks; i++) { | |
1283 | int status; | |
1284 | ||
1285 | assert(waitpid(pid[i], &status, 0) == pid[i]); | |
1286 | assert(status == 0); | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | static void test_map_stress(void) | |
1291 | { | |
1292 | run_parallel(100, test_hashmap, NULL); | |
1293 | run_parallel(100, test_hashmap_percpu, NULL); | |
8c290e60 | 1294 | run_parallel(100, test_hashmap_sizes, NULL); |
5ecf51fd | 1295 | run_parallel(100, test_hashmap_walk, NULL); |
5aa5bd14 DB |
1296 | |
1297 | run_parallel(100, test_arraymap, NULL); | |
1298 | run_parallel(100, test_arraymap_percpu, NULL); | |
1299 | } | |
1300 | ||
1301 | #define TASKS 1024 | |
1302 | ||
1303 | #define DO_UPDATE 1 | |
1304 | #define DO_DELETE 0 | |
1305 | ||
90c93fbe | 1306 | static void test_update_delete(unsigned int fn, void *data) |
5aa5bd14 DB |
1307 | { |
1308 | int do_update = ((int *)data)[1]; | |
1309 | int fd = ((int *)data)[0]; | |
1310 | int i, key, value; | |
1311 | ||
1312 | for (i = fn; i < MAP_SIZE; i += TASKS) { | |
1313 | key = value = i; | |
1314 | ||
1315 | if (do_update) { | |
10ecc728 MS |
1316 | assert(bpf_map_update_elem(fd, &key, &value, |
1317 | BPF_NOEXIST) == 0); | |
1318 | assert(bpf_map_update_elem(fd, &key, &value, | |
1319 | BPF_EXIST) == 0); | |
5aa5bd14 | 1320 | } else { |
e58383b8 | 1321 | assert(bpf_map_delete_elem(fd, &key) == 0); |
5aa5bd14 DB |
1322 | } |
1323 | } | |
1324 | } | |
1325 | ||
1326 | static void test_map_parallel(void) | |
1327 | { | |
1328 | int i, fd, key = 0, value = 0; | |
1329 | int data[2]; | |
1330 | ||
f4874d01 | 1331 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), |
5aa5bd14 DB |
1332 | MAP_SIZE, map_flags); |
1333 | if (fd < 0) { | |
1334 | printf("Failed to create map for parallel test '%s'!\n", | |
1335 | strerror(errno)); | |
1336 | exit(1); | |
1337 | } | |
1338 | ||
1339 | /* Use the same fd in children to add elements to this map: | |
1340 | * child_0 adds key=0, key=1024, key=2048, ... | |
1341 | * child_1 adds key=1, key=1025, key=2049, ... | |
1342 | * child_1023 adds key=1023, ... | |
1343 | */ | |
1344 | data[0] = fd; | |
1345 | data[1] = DO_UPDATE; | |
1a97cf1f | 1346 | run_parallel(TASKS, test_update_delete, data); |
5aa5bd14 DB |
1347 | |
1348 | /* Check that key=0 is already there. */ | |
10ecc728 | 1349 | assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == -1 && |
5aa5bd14 DB |
1350 | errno == EEXIST); |
1351 | ||
1352 | /* Check that all elements were inserted. */ | |
8fe45924 | 1353 | assert(bpf_map_get_next_key(fd, NULL, &key) == 0); |
5aa5bd14 DB |
1354 | key = -1; |
1355 | for (i = 0; i < MAP_SIZE; i++) | |
5f155c25 MS |
1356 | assert(bpf_map_get_next_key(fd, &key, &key) == 0); |
1357 | assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); | |
5aa5bd14 DB |
1358 | |
1359 | /* Another check for all elements */ | |
1360 | for (i = 0; i < MAP_SIZE; i++) { | |
1361 | key = MAP_SIZE - i - 1; | |
1362 | ||
e5ff7c40 | 1363 | assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && |
5aa5bd14 DB |
1364 | value == key); |
1365 | } | |
1366 | ||
1367 | /* Now let's delete all elemenets in parallel. */ | |
1368 | data[1] = DO_DELETE; | |
1a97cf1f | 1369 | run_parallel(TASKS, test_update_delete, data); |
5aa5bd14 DB |
1370 | |
1371 | /* Nothing should be left. */ | |
1372 | key = -1; | |
8fe45924 | 1373 | assert(bpf_map_get_next_key(fd, NULL, &key) == -1 && errno == ENOENT); |
5f155c25 | 1374 | assert(bpf_map_get_next_key(fd, &key, &key) == -1 && errno == ENOENT); |
5aa5bd14 DB |
1375 | } |
1376 | ||
e043325b CF |
1377 | static void test_map_rdonly(void) |
1378 | { | |
e27afb84 | 1379 | int fd, key = 0, value = 0; |
e043325b CF |
1380 | |
1381 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), | |
1382 | MAP_SIZE, map_flags | BPF_F_RDONLY); | |
1383 | if (fd < 0) { | |
1384 | printf("Failed to create map for read only test '%s'!\n", | |
1385 | strerror(errno)); | |
1386 | exit(1); | |
1387 | } | |
1388 | ||
1389 | key = 1; | |
1390 | value = 1234; | |
1391 | /* Insert key=1 element. */ | |
1392 | assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == -1 && | |
1393 | errno == EPERM); | |
1394 | ||
1395 | /* Check that key=2 is not found. */ | |
1396 | assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == ENOENT); | |
1397 | assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == ENOENT); | |
1398 | } | |
1399 | ||
1400 | static void test_map_wronly(void) | |
1401 | { | |
e27afb84 | 1402 | int fd, key = 0, value = 0; |
e043325b CF |
1403 | |
1404 | fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(key), sizeof(value), | |
1405 | MAP_SIZE, map_flags | BPF_F_WRONLY); | |
1406 | if (fd < 0) { | |
1407 | printf("Failed to create map for read only test '%s'!\n", | |
1408 | strerror(errno)); | |
1409 | exit(1); | |
1410 | } | |
1411 | ||
1412 | key = 1; | |
1413 | value = 1234; | |
1414 | /* Insert key=1 element. */ | |
e27afb84 | 1415 | assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); |
e043325b CF |
1416 | |
1417 | /* Check that key=2 is not found. */ | |
1418 | assert(bpf_map_lookup_elem(fd, &key, &value) == -1 && errno == EPERM); | |
1419 | assert(bpf_map_get_next_key(fd, &key, &value) == -1 && errno == EPERM); | |
1420 | } | |
1421 | ||
6bc8529c MKL |
1422 | static void prepare_reuseport_grp(int type, int map_fd, |
1423 | __s64 *fds64, __u64 *sk_cookies, | |
1424 | unsigned int n) | |
1425 | { | |
1426 | socklen_t optlen, addrlen; | |
1427 | struct sockaddr_in6 s6; | |
1428 | const __u32 index0 = 0; | |
1429 | const int optval = 1; | |
1430 | unsigned int i; | |
1431 | u64 sk_cookie; | |
1432 | __s64 fd64; | |
1433 | int err; | |
1434 | ||
1435 | s6.sin6_family = AF_INET6; | |
1436 | s6.sin6_addr = in6addr_any; | |
1437 | s6.sin6_port = 0; | |
1438 | addrlen = sizeof(s6); | |
1439 | optlen = sizeof(sk_cookie); | |
1440 | ||
1441 | for (i = 0; i < n; i++) { | |
1442 | fd64 = socket(AF_INET6, type, 0); | |
1443 | CHECK(fd64 == -1, "socket()", | |
1444 | "sock_type:%d fd64:%lld errno:%d\n", | |
1445 | type, fd64, errno); | |
1446 | ||
1447 | err = setsockopt(fd64, SOL_SOCKET, SO_REUSEPORT, | |
1448 | &optval, sizeof(optval)); | |
26a1ccc6 | 1449 | CHECK(err == -1, "setsockopt(SO_REUSEPORT)", |
6bc8529c MKL |
1450 | "err:%d errno:%d\n", err, errno); |
1451 | ||
1452 | /* reuseport_array does not allow unbound sk */ | |
1453 | err = bpf_map_update_elem(map_fd, &index0, &fd64, | |
1454 | BPF_ANY); | |
1455 | CHECK(err != -1 || errno != EINVAL, | |
1456 | "reuseport array update unbound sk", | |
1457 | "sock_type:%d err:%d errno:%d\n", | |
1458 | type, err, errno); | |
1459 | ||
1460 | err = bind(fd64, (struct sockaddr *)&s6, sizeof(s6)); | |
1461 | CHECK(err == -1, "bind()", | |
1462 | "sock_type:%d err:%d errno:%d\n", type, err, errno); | |
1463 | ||
1464 | if (i == 0) { | |
1465 | err = getsockname(fd64, (struct sockaddr *)&s6, | |
1466 | &addrlen); | |
1467 | CHECK(err == -1, "getsockname()", | |
1468 | "sock_type:%d err:%d errno:%d\n", | |
1469 | type, err, errno); | |
1470 | } | |
1471 | ||
1472 | err = getsockopt(fd64, SOL_SOCKET, SO_COOKIE, &sk_cookie, | |
1473 | &optlen); | |
1474 | CHECK(err == -1, "getsockopt(SO_COOKIE)", | |
1475 | "sock_type:%d err:%d errno:%d\n", type, err, errno); | |
1476 | ||
1477 | if (type == SOCK_STREAM) { | |
1478 | /* | |
1479 | * reuseport_array does not allow | |
1480 | * non-listening tcp sk. | |
1481 | */ | |
1482 | err = bpf_map_update_elem(map_fd, &index0, &fd64, | |
1483 | BPF_ANY); | |
1484 | CHECK(err != -1 || errno != EINVAL, | |
1485 | "reuseport array update non-listening sk", | |
1486 | "sock_type:%d err:%d errno:%d\n", | |
1487 | type, err, errno); | |
1488 | err = listen(fd64, 0); | |
1489 | CHECK(err == -1, "listen()", | |
1490 | "sock_type:%d, err:%d errno:%d\n", | |
1491 | type, err, errno); | |
1492 | } | |
1493 | ||
1494 | fds64[i] = fd64; | |
1495 | sk_cookies[i] = sk_cookie; | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | static void test_reuseport_array(void) | |
1500 | { | |
1501 | #define REUSEPORT_FD_IDX(err, last) ({ (err) ? last : !last; }) | |
1502 | ||
1503 | const __u32 array_size = 4, index0 = 0, index3 = 3; | |
1504 | int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type; | |
1505 | __u64 grpa_cookies[2], sk_cookie, map_cookie; | |
1506 | __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1; | |
1507 | const __u32 bad_index = array_size; | |
1508 | int map_fd, err, t, f; | |
1509 | __u32 fds_idx = 0; | |
1510 | int fd; | |
1511 | ||
1512 | map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, | |
1513 | sizeof(__u32), sizeof(__u64), array_size, 0); | |
1514 | CHECK(map_fd == -1, "reuseport array create", | |
1515 | "map_fd:%d, errno:%d\n", map_fd, errno); | |
1516 | ||
1517 | /* Test lookup/update/delete with invalid index */ | |
1518 | err = bpf_map_delete_elem(map_fd, &bad_index); | |
1519 | CHECK(err != -1 || errno != E2BIG, "reuseport array del >=max_entries", | |
1520 | "err:%d errno:%d\n", err, errno); | |
1521 | ||
1522 | err = bpf_map_update_elem(map_fd, &bad_index, &fd64, BPF_ANY); | |
1523 | CHECK(err != -1 || errno != E2BIG, | |
1524 | "reuseport array update >=max_entries", | |
1525 | "err:%d errno:%d\n", err, errno); | |
1526 | ||
1527 | err = bpf_map_lookup_elem(map_fd, &bad_index, &map_cookie); | |
1528 | CHECK(err != -1 || errno != ENOENT, | |
1529 | "reuseport array update >=max_entries", | |
1530 | "err:%d errno:%d\n", err, errno); | |
1531 | ||
1532 | /* Test lookup/delete non existence elem */ | |
1533 | err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); | |
1534 | CHECK(err != -1 || errno != ENOENT, | |
1535 | "reuseport array lookup not-exist elem", | |
1536 | "err:%d errno:%d\n", err, errno); | |
1537 | err = bpf_map_delete_elem(map_fd, &index3); | |
1538 | CHECK(err != -1 || errno != ENOENT, | |
1539 | "reuseport array del not-exist elem", | |
1540 | "err:%d errno:%d\n", err, errno); | |
1541 | ||
1542 | for (t = 0; t < ARRAY_SIZE(types); t++) { | |
1543 | type = types[t]; | |
1544 | ||
1545 | prepare_reuseport_grp(type, map_fd, grpa_fds64, | |
1546 | grpa_cookies, ARRAY_SIZE(grpa_fds64)); | |
1547 | ||
1548 | /* Test BPF_* update flags */ | |
1549 | /* BPF_EXIST failure case */ | |
1550 | err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], | |
1551 | BPF_EXIST); | |
1552 | CHECK(err != -1 || errno != ENOENT, | |
1553 | "reuseport array update empty elem BPF_EXIST", | |
1554 | "sock_type:%d err:%d errno:%d\n", | |
1555 | type, err, errno); | |
1556 | fds_idx = REUSEPORT_FD_IDX(err, fds_idx); | |
1557 | ||
1558 | /* BPF_NOEXIST success case */ | |
1559 | err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], | |
1560 | BPF_NOEXIST); | |
1561 | CHECK(err == -1, | |
1562 | "reuseport array update empty elem BPF_NOEXIST", | |
1563 | "sock_type:%d err:%d errno:%d\n", | |
1564 | type, err, errno); | |
1565 | fds_idx = REUSEPORT_FD_IDX(err, fds_idx); | |
1566 | ||
1567 | /* BPF_EXIST success case. */ | |
1568 | err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], | |
1569 | BPF_EXIST); | |
1570 | CHECK(err == -1, | |
1571 | "reuseport array update same elem BPF_EXIST", | |
1572 | "sock_type:%d err:%d errno:%d\n", type, err, errno); | |
1573 | fds_idx = REUSEPORT_FD_IDX(err, fds_idx); | |
1574 | ||
1575 | /* BPF_NOEXIST failure case */ | |
1576 | err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], | |
1577 | BPF_NOEXIST); | |
1578 | CHECK(err != -1 || errno != EEXIST, | |
1579 | "reuseport array update non-empty elem BPF_NOEXIST", | |
1580 | "sock_type:%d err:%d errno:%d\n", | |
1581 | type, err, errno); | |
1582 | fds_idx = REUSEPORT_FD_IDX(err, fds_idx); | |
1583 | ||
1584 | /* BPF_ANY case (always succeed) */ | |
1585 | err = bpf_map_update_elem(map_fd, &index3, &grpa_fds64[fds_idx], | |
1586 | BPF_ANY); | |
1587 | CHECK(err == -1, | |
1588 | "reuseport array update same sk with BPF_ANY", | |
1589 | "sock_type:%d err:%d errno:%d\n", type, err, errno); | |
1590 | ||
1591 | fd64 = grpa_fds64[fds_idx]; | |
1592 | sk_cookie = grpa_cookies[fds_idx]; | |
1593 | ||
1594 | /* The same sk cannot be added to reuseport_array twice */ | |
1595 | err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_ANY); | |
1596 | CHECK(err != -1 || errno != EBUSY, | |
1597 | "reuseport array update same sk with same index", | |
1598 | "sock_type:%d err:%d errno:%d\n", | |
1599 | type, err, errno); | |
1600 | ||
1601 | err = bpf_map_update_elem(map_fd, &index0, &fd64, BPF_ANY); | |
1602 | CHECK(err != -1 || errno != EBUSY, | |
1603 | "reuseport array update same sk with different index", | |
1604 | "sock_type:%d err:%d errno:%d\n", | |
1605 | type, err, errno); | |
1606 | ||
1607 | /* Test delete elem */ | |
1608 | err = bpf_map_delete_elem(map_fd, &index3); | |
1609 | CHECK(err == -1, "reuseport array delete sk", | |
1610 | "sock_type:%d err:%d errno:%d\n", | |
1611 | type, err, errno); | |
1612 | ||
1613 | /* Add it back with BPF_NOEXIST */ | |
1614 | err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); | |
1615 | CHECK(err == -1, | |
1616 | "reuseport array re-add with BPF_NOEXIST after del", | |
1617 | "sock_type:%d err:%d errno:%d\n", type, err, errno); | |
1618 | ||
1619 | /* Test cookie */ | |
1620 | err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); | |
1621 | CHECK(err == -1 || sk_cookie != map_cookie, | |
1622 | "reuseport array lookup re-added sk", | |
1623 | "sock_type:%d err:%d errno:%d sk_cookie:0x%llx map_cookie:0x%llxn", | |
1624 | type, err, errno, sk_cookie, map_cookie); | |
1625 | ||
1626 | /* Test elem removed by close() */ | |
1627 | for (f = 0; f < ARRAY_SIZE(grpa_fds64); f++) | |
1628 | close(grpa_fds64[f]); | |
1629 | err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); | |
1630 | CHECK(err != -1 || errno != ENOENT, | |
1631 | "reuseport array lookup after close()", | |
1632 | "sock_type:%d err:%d errno:%d\n", | |
1633 | type, err, errno); | |
1634 | } | |
1635 | ||
1636 | /* Test SOCK_RAW */ | |
1637 | fd64 = socket(AF_INET6, SOCK_RAW, IPPROTO_UDP); | |
1638 | CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n", | |
1639 | err, errno); | |
1640 | err = bpf_map_update_elem(map_fd, &index3, &fd64, BPF_NOEXIST); | |
1641 | CHECK(err != -1 || errno != ENOTSUPP, "reuseport array update SOCK_RAW", | |
1642 | "err:%d errno:%d\n", err, errno); | |
1643 | close(fd64); | |
1644 | ||
1645 | /* Close the 64 bit value map */ | |
1646 | close(map_fd); | |
1647 | ||
1648 | /* Test 32 bit fd */ | |
1649 | map_fd = bpf_create_map(BPF_MAP_TYPE_REUSEPORT_SOCKARRAY, | |
1650 | sizeof(__u32), sizeof(__u32), array_size, 0); | |
1651 | CHECK(map_fd == -1, "reuseport array create", | |
1652 | "map_fd:%d, errno:%d\n", map_fd, errno); | |
1653 | prepare_reuseport_grp(SOCK_STREAM, map_fd, &fd64, &sk_cookie, 1); | |
1654 | fd = fd64; | |
1655 | err = bpf_map_update_elem(map_fd, &index3, &fd, BPF_NOEXIST); | |
1656 | CHECK(err == -1, "reuseport array update 32 bit fd", | |
1657 | "err:%d errno:%d\n", err, errno); | |
1658 | err = bpf_map_lookup_elem(map_fd, &index3, &map_cookie); | |
1659 | CHECK(err != -1 || errno != ENOSPC, | |
1660 | "reuseport array lookup 32 bit fd", | |
1661 | "err:%d errno:%d\n", err, errno); | |
1662 | close(fd); | |
1663 | close(map_fd); | |
1664 | } | |
1665 | ||
5aa5bd14 DB |
1666 | static void run_all_tests(void) |
1667 | { | |
1668 | test_hashmap(0, NULL); | |
1669 | test_hashmap_percpu(0, NULL); | |
5ecf51fd | 1670 | test_hashmap_walk(0, NULL); |
bf5d68c7 | 1671 | test_hashmap_zero_seed(); |
5aa5bd14 DB |
1672 | |
1673 | test_arraymap(0, NULL); | |
1674 | test_arraymap_percpu(0, NULL); | |
1675 | ||
1676 | test_arraymap_percpu_many_keys(); | |
1677 | ||
81f6bf81 | 1678 | test_devmap(0, NULL); |
6f6d33f3 | 1679 | test_sockmap(0, NULL); |
81f6bf81 | 1680 | |
5aa5bd14 DB |
1681 | test_map_large(); |
1682 | test_map_parallel(); | |
1683 | test_map_stress(); | |
e043325b CF |
1684 | |
1685 | test_map_rdonly(); | |
1686 | test_map_wronly(); | |
6bc8529c MKL |
1687 | |
1688 | test_reuseport_array(); | |
43b987d2 MV |
1689 | |
1690 | test_queuemap(0, NULL); | |
1691 | test_stackmap(0, NULL); | |
b1957c92 NS |
1692 | |
1693 | test_map_in_map(); | |
5aa5bd14 DB |
1694 | } |
1695 | ||
1696 | int main(void) | |
1697 | { | |
43b987d2 MV |
1698 | srand(time(NULL)); |
1699 | ||
5aa5bd14 DB |
1700 | map_flags = 0; |
1701 | run_all_tests(); | |
1702 | ||
1703 | map_flags = BPF_F_NO_PREALLOC; | |
1704 | run_all_tests(); | |
1705 | ||
1706 | printf("test_maps: OK\n"); | |
1707 | return 0; | |
1708 | } |