Lines Matching +full:key +full:- +full:2

1 // SPDX-License-Identifier: GPL-2.0-only
35 long long key, next_key, first_key, value; in test_hashmap() local
38 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 2, &map_opts); in test_hashmap()
44 key = 1; in test_hashmap()
46 /* Insert key=1 element. */ in test_hashmap()
47 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
51 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
52 /* key=1 already exists. */ in test_hashmap()
55 /* -1 is an invalid flag. */ in test_hashmap()
56 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 && in test_hashmap()
59 /* Check that key=1 can be found. */ in test_hashmap()
60 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); in test_hashmap()
62 key = 2; in test_hashmap()
64 /* Insert key=2 element. */ in test_hashmap()
65 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
67 /* Check that key=2 matches the value and delete it */ in test_hashmap()
68 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234); in test_hashmap()
70 /* Check that key=2 is not found. */ in test_hashmap()
71 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_hashmap()
74 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && in test_hashmap()
75 /* key=2 is not there. */ in test_hashmap()
78 /* Insert key=2 element. */ in test_hashmap()
79 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); in test_hashmap()
81 /* key=1 and key=2 were inserted, check that key=0 cannot be in test_hashmap()
84 key = 0; in test_hashmap()
85 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
89 key = 1; in test_hashmap()
90 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); in test_hashmap()
91 key = 2; in test_hashmap()
92 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
93 key = 3; in test_hashmap()
94 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
97 /* Check that key = 0 doesn't exist. */ in test_hashmap()
98 key = 0; in test_hashmap()
99 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap()
103 (first_key == 1 || first_key == 2)); in test_hashmap()
104 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_hashmap()
107 (next_key == 1 || next_key == 2) && in test_hashmap()
113 key = 1; in test_hashmap()
114 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap()
115 key = 2; in test_hashmap()
116 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap()
117 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap()
119 key = 0; in test_hashmap()
123 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && in test_hashmap()
135 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, i, j, 2, &map_opts); in test_hashmap_sizes()
139 printf("Failed to create hashmap key=%d value=%d '%s'\n", in test_hashmap_sizes()
152 long long key, next_key, first_key; in test_hashmap_percpu() local
156 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, NULL, sizeof(key), in test_hashmap_percpu()
157 sizeof(bpf_percpu(value, 0)), 2, &map_opts); in test_hashmap_percpu()
166 key = 1; in test_hashmap_percpu()
167 /* Insert key=1 element. */ in test_hashmap_percpu()
168 assert(!(expected_key_mask & key)); in test_hashmap_percpu()
169 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); in test_hashmap_percpu()
171 /* Lookup and delete elem key=1 and check value. */ in test_hashmap_percpu()
172 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 && in test_hashmap_percpu()
178 /* Insert key=1 element which should not exist. */ in test_hashmap_percpu()
179 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); in test_hashmap_percpu()
180 expected_key_mask |= key; in test_hashmap_percpu()
183 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && in test_hashmap_percpu()
184 /* key=1 already exists. */ in test_hashmap_percpu()
187 /* -1 is an invalid flag. */ in test_hashmap_percpu()
188 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 && in test_hashmap_percpu()
191 /* Check that key=1 can be found. Value could be 0 if the lookup in test_hashmap_percpu()
195 assert(bpf_map_lookup_elem(fd, &key, value) == 0 && in test_hashmap_percpu()
198 key = 2; in test_hashmap_percpu()
199 /* Check that key=2 is not found. */ in test_hashmap_percpu()
200 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT); in test_hashmap_percpu()
203 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 && in test_hashmap_percpu()
204 /* key=2 is not there. */ in test_hashmap_percpu()
207 /* Insert key=2 element. */ in test_hashmap_percpu()
208 assert(!(expected_key_mask & key)); in test_hashmap_percpu()
209 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); in test_hashmap_percpu()
210 expected_key_mask |= key; in test_hashmap_percpu()
212 /* key=1 and key=2 were inserted, check that key=0 cannot be in test_hashmap_percpu()
215 key = 0; in test_hashmap_percpu()
216 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && in test_hashmap_percpu()
219 /* Check that key = 0 doesn't exist. */ in test_hashmap_percpu()
220 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap_percpu()
225 while (!bpf_map_get_next_key(fd, &key, &next_key)) { in test_hashmap_percpu()
238 key = next_key; in test_hashmap_percpu()
243 key = 1; in test_hashmap_percpu()
244 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); in test_hashmap_percpu()
247 key = 1; in test_hashmap_percpu()
248 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap_percpu()
249 key = 2; in test_hashmap_percpu()
250 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap_percpu()
251 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap_percpu()
253 key = 0; in test_hashmap_percpu()
257 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && in test_hashmap_percpu()
267 long long key, value[VALUE_SIZE] = {}; in helper_fill_hashmap() local
269 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in helper_fill_hashmap()
276 key = i; value[0] = key; in helper_fill_hashmap()
277 ret = bpf_map_update_elem(fd, &key, value, BPF_NOEXIST); in helper_fill_hashmap()
289 long long key, value[VALUE_SIZE], next_key; in test_hashmap_walk() local
294 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, in test_hashmap_walk()
296 key = next_key; in test_hashmap_walk()
297 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
302 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_hashmap_walk()
304 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; in test_hashmap_walk()
305 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
307 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); in test_hashmap_walk()
308 key = next_key; in test_hashmap_walk()
313 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, in test_hashmap_walk()
315 key = next_key; in test_hashmap_walk()
316 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
317 assert(value[0] - 1 == key); in test_hashmap_walk()
327 long long key, next_first, next_second; in test_hashmap_zero_seed() local
336 void *key_ptr = !i ? NULL : &key; in test_hashmap_zero_seed()
349 key = next_first; in test_hashmap_zero_seed()
359 int key, next_key, fd; in test_arraymap() local
362 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), 2, NULL); in test_arraymap()
368 key = 1; in test_arraymap()
370 /* Insert key=1 element. */ in test_arraymap()
371 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_arraymap()
374 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_arraymap()
377 /* Check that key=1 can be found. */ in test_arraymap()
378 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); in test_arraymap()
380 key = 0; in test_arraymap()
381 /* Check that key=0 is also found and zero initialized. */ in test_arraymap()
382 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); in test_arraymap()
384 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted in test_arraymap()
387 key = 2; in test_arraymap()
388 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && in test_arraymap()
391 /* Check that key = 2 doesn't exist. */ in test_arraymap()
392 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_arraymap()
397 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_arraymap()
405 key = 1; in test_arraymap()
406 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); in test_arraymap()
415 int key, next_key, fd, i; in test_arraymap_percpu() local
417 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), in test_arraymap_percpu()
418 sizeof(bpf_percpu(values, 0)), 2, NULL); in test_arraymap_percpu()
427 key = 1; in test_arraymap_percpu()
428 /* Insert key=1 element. */ in test_arraymap_percpu()
429 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); in test_arraymap_percpu()
432 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 && in test_arraymap_percpu()
435 /* Check that key=1 can be found. */ in test_arraymap_percpu()
436 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && in test_arraymap_percpu()
439 key = 0; in test_arraymap_percpu()
440 /* Check that key=0 is also found and zero initialized. */ in test_arraymap_percpu()
441 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && in test_arraymap_percpu()
443 bpf_percpu(values, nr_cpus - 1) == 0); in test_arraymap_percpu()
445 /* Check that key=2 cannot be inserted due to max_entries limit. */ in test_arraymap_percpu()
446 key = 2; in test_arraymap_percpu()
447 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 && in test_arraymap_percpu()
450 /* Check that key = 2 doesn't exist. */ in test_arraymap_percpu()
451 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT); in test_arraymap_percpu()
456 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_arraymap_percpu()
464 key = 1; in test_arraymap_percpu()
465 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); in test_arraymap_percpu()
478 int key, fd, i; in test_arraymap_percpu_many_keys() local
480 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), in test_arraymap_percpu_many_keys()
483 printf("Failed to create per-cpu arraymap '%s'!\n", in test_arraymap_percpu_many_keys()
491 for (key = 0; key < nr_keys; key++) in test_arraymap_percpu_many_keys()
492 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); in test_arraymap_percpu_many_keys()
494 for (key = 0; key < nr_keys; key++) { in test_arraymap_percpu_many_keys()
498 assert(bpf_map_lookup_elem(fd, &key, values) == 0); in test_arraymap_percpu_many_keys()
510 __u32 key, value; in test_devmap() local
512 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP, NULL, sizeof(key), sizeof(value), 2, NULL); in test_devmap()
524 __u32 key, value; in test_devmap_hash() local
526 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP_HASH, NULL, sizeof(key), sizeof(value), 2, NULL); in test_devmap_hash()
538 __u32 vals[MAP_SIZE + MAP_SIZE/2], val; in test_queuemap()
542 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) in test_queuemap()
545 /* Invalid key size */ in test_queuemap()
572 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) in test_queuemap()
576 for (i = MAP_SIZE/2; i < MAP_SIZE + MAP_SIZE/2; i++) in test_queuemap()
594 __u32 vals[MAP_SIZE + MAP_SIZE/2], val; in test_stackmap()
598 for (i = 0; i < MAP_SIZE + MAP_SIZE/2; i++) in test_stackmap()
601 /* Invalid key size */ in test_stackmap()
625 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]); in test_stackmap()
628 for (i = MAP_SIZE; i < MAP_SIZE + MAP_SIZE/2; i++) in test_stackmap()
632 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--) in test_stackmap()
666 __u32 key, value; in test_sockmap() local
671 for (i = 0; i < 2; i++) { in test_sockmap()
703 for (i = 2; i < 4; i++) { in test_sockmap()
716 addr.sin_port = htons(ports[i - 2]); in test_sockmap()
726 sfd[i] = accept(sfd[i - 4], NULL, NULL); in test_sockmap()
735 sizeof(key), sizeof(value), in test_sockmap()
773 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0); in test_sockmap()
779 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0); in test_sockmap()
785 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0); in test_sockmap()
791 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0); in test_sockmap()
926 for (i = 2; i < 6; i++) { in test_sockmap()
942 for (i = 2; i < 4; i++) { in test_sockmap()
957 /* Put sfd[2] (sending fd below) into msg map to test sendmsg bpf */ in test_sockmap()
959 err = bpf_map_update_elem(map_fd_msg, &i, &sfd[2], BPF_ANY); in test_sockmap()
966 for (i = 0; i < 2; i++) { in test_sockmap()
969 sc = send(sfd[2], buf, 20, 0); in test_sockmap()
980 if (s == -1) { in test_sockmap()
1003 sc = send(sfd[2], buf, 20, 0); in test_sockmap()
1010 i = 2; in test_sockmap()
1030 for (i = 2; i < 6; i++) { in test_sockmap()
1086 } else if (pid[i] == -1) { in test_sockmap()
1161 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(int), sizeof(int), 2, NULL); in test_map_in_map()
1230 fd = -1; in test_map_in_map()
1311 } key; in test_map_large() local
1314 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_large()
1322 key = (struct bigkey) { .c = i }; in test_map_large()
1325 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); in test_map_large()
1328 key.c = -1; in test_map_large()
1329 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_map_large()
1333 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_map_large()
1334 key.c = -1; in test_map_large()
1336 assert(bpf_map_get_next_key(fd, &key, &key) == 0); in test_map_large()
1337 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_large()
1339 key.c = 0; in test_map_large()
1340 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); in test_map_large()
1341 key.a = 1; in test_map_large()
1342 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_map_large()
1365 } else if (pid[i] == -1) { in __run_parallel()
1404 int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts, in map_update_retriable() argument
1409 while (bpf_map_update_elem(map_fd, key, value, flags)) { in map_update_retriable()
1411 return -errno; in map_update_retriable()
1413 if (delay <= MAX_DELAY_US / 2) in map_update_retriable()
1414 delay *= 2; in map_update_retriable()
1417 attempts--; in map_update_retriable()
1423 static int map_delete_retriable(int map_fd, const void *key, int attempts) in map_delete_retriable() argument
1427 while (bpf_map_delete_elem(map_fd, key)) { in map_delete_retriable()
1429 return -errno; in map_delete_retriable()
1431 if (delay <= MAX_DELAY_US / 2) in map_delete_retriable()
1432 delay *= 2; in map_delete_retriable()
1435 attempts--; in map_delete_retriable()
1445 int i, key, value, err; in test_update_delete() local
1450 key = value = i; in test_update_delete()
1453 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES, in test_update_delete()
1458 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES, in test_update_delete()
1464 err = map_delete_retriable(fd, &key, MAP_RETRIES); in test_update_delete()
1474 int i, fd, key = 0, value = 0, j = 0; in test_map_parallel() local
1475 int data[2]; in test_map_parallel()
1477 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_parallel()
1487 * child_0 adds key=0, key=1024, key=2048, ... in test_map_parallel()
1488 * child_1 adds key=1, key=1025, key=2049, ... in test_map_parallel()
1489 * child_1023 adds key=1023, ... in test_map_parallel()
1495 /* Check that key=0 is already there. */ in test_map_parallel()
1496 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_map_parallel()
1500 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_map_parallel()
1501 key = -1; in test_map_parallel()
1503 assert(bpf_map_get_next_key(fd, &key, &key) == 0); in test_map_parallel()
1504 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_parallel()
1508 key = MAP_SIZE - i - 1; in test_map_parallel()
1510 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && in test_map_parallel()
1511 value == key); in test_map_parallel()
1519 key = -1; in test_map_parallel()
1520 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT); in test_map_parallel()
1521 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_parallel()
1523 key = 0; in test_map_parallel()
1524 bpf_map_delete_elem(fd, &key); in test_map_parallel()
1532 int fd, key = 0, value = 0; in test_map_rdonly() local
1537 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_rdonly()
1546 key = 1; in test_map_rdonly()
1548 /* Try to insert key=1 element. */ in test_map_rdonly()
1549 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 && in test_map_rdonly()
1552 /* Check that key=1 is not found. */ in test_map_rdonly()
1553 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_map_rdonly()
1554 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT); in test_map_rdonly()
1561 int fd, key = 0, value = 0; in test_map_wronly_hash() local
1566 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_wronly_hash()
1575 key = 1; in test_map_wronly_hash()
1577 /* Insert key=1 element. */ in test_map_wronly_hash()
1578 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_map_wronly_hash()
1581 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM); in test_map_wronly_hash()
1582 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM); in test_map_wronly_hash()
1652 CHECK(fd64 == -1, "socket()", in prepare_reuseport_grp()
1658 CHECK(err == -1, "setsockopt(SO_REUSEPORT)", in prepare_reuseport_grp()
1676 CHECK(err == -1, "bind()", in prepare_reuseport_grp()
1682 CHECK(err == -1, "getsockname()", in prepare_reuseport_grp()
1689 CHECK(err == -1, "getsockopt(SO_COOKIE)", in prepare_reuseport_grp()
1695 * non-listening tcp sk. in prepare_reuseport_grp()
1700 "reuseport array update non-listening sk", in prepare_reuseport_grp()
1704 CHECK(err == -1, "listen()", in prepare_reuseport_grp()
1719 int types[2] = { SOCK_STREAM, SOCK_DGRAM }, type; in test_reuseport_array()
1720 __u64 grpa_cookies[2], sk_cookie, map_cookie; in test_reuseport_array()
1721 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1; in test_reuseport_array()
1750 "reuseport array lookup not-exist elem", in test_reuseport_array()
1754 "reuseport array del not-exist elem", in test_reuseport_array()
1794 "reuseport array update non-empty elem BPF_NOEXIST", in test_reuseport_array()
1831 "reuseport array re-add with BPF_NOEXIST after del", in test_reuseport_array()
1837 "reuseport array lookup re-added sk", in test_reuseport_array()
1853 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n", in test_reuseport_array()