Lines Matching +full:key +full:- +full:value

1 // SPDX-License-Identifier: GPL-2.0-only
39 long long key, next_key, first_key, value; in test_hashmap() local
42 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), 2, &map_opts); in test_hashmap()
48 key = 1; in test_hashmap()
49 value = 1234; in test_hashmap()
50 /* Insert key=1 element. */ in test_hashmap()
51 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
53 value = 0; in test_hashmap()
55 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
56 /* key=1 already exists. */ in test_hashmap()
59 /* -1 is an invalid flag. */ in test_hashmap()
60 assert(bpf_map_update_elem(fd, &key, &value, -1) < 0 && in test_hashmap()
63 /* Check that key=1 can be found. */ in test_hashmap()
64 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); in test_hashmap()
66 key = 2; in test_hashmap()
67 value = 1234; in test_hashmap()
68 /* Insert key=2 element. */ in test_hashmap()
69 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
71 /* Check that key=2 matches the value and delete it */ in test_hashmap()
72 assert(bpf_map_lookup_and_delete_elem(fd, &key, &value) == 0 && value == 1234); in test_hashmap()
74 /* Check that key=2 is not found. */ in test_hashmap()
75 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_hashmap()
78 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && in test_hashmap()
79 /* key=2 is not there. */ in test_hashmap()
82 /* Insert key=2 element. */ in test_hashmap()
83 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); in test_hashmap()
85 /* key=1 and key=2 were inserted, check that key=0 cannot be in test_hashmap()
88 key = 0; in test_hashmap()
89 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
93 key = 1; in test_hashmap()
94 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) == 0); in test_hashmap()
95 key = 2; in test_hashmap()
96 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_hashmap()
97 key = 3; in test_hashmap()
98 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_hashmap()
101 /* Check that key = 0 doesn't exist. */ in test_hashmap()
102 key = 0; in test_hashmap()
103 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap()
108 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_hashmap()
117 key = 1; in test_hashmap()
118 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap()
119 key = 2; in test_hashmap()
120 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap()
121 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap()
123 key = 0; in test_hashmap()
127 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && in test_hashmap()
143 printf("Failed to create hashmap key=%d value=%d '%s'\n", in test_hashmap_sizes()
155 BPF_DECLARE_PERCPU(long, value); in test_hashmap_percpu()
156 long long key, next_key, first_key; in test_hashmap_percpu() local
160 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_HASH, NULL, sizeof(key), in test_hashmap_percpu()
161 sizeof(bpf_percpu(value, 0)), 2, &map_opts); in test_hashmap_percpu()
168 bpf_percpu(value, i) = i + 100; in test_hashmap_percpu()
170 key = 1; in test_hashmap_percpu()
171 /* Insert key=1 element. */ in test_hashmap_percpu()
172 assert(!(expected_key_mask & key)); in test_hashmap_percpu()
173 assert(bpf_map_update_elem(fd, &key, value, BPF_ANY) == 0); in test_hashmap_percpu()
175 /* Lookup and delete elem key=1 and check value. */ in test_hashmap_percpu()
176 assert(bpf_map_lookup_and_delete_elem(fd, &key, value) == 0 && in test_hashmap_percpu()
177 bpf_percpu(value,0) == 100); in test_hashmap_percpu()
180 bpf_percpu(value,i) = i + 100; in test_hashmap_percpu()
182 /* Insert key=1 element which should not exist. */ in test_hashmap_percpu()
183 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); in test_hashmap_percpu()
184 expected_key_mask |= key; in test_hashmap_percpu()
187 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && in test_hashmap_percpu()
188 /* key=1 already exists. */ in test_hashmap_percpu()
191 /* -1 is an invalid flag. */ in test_hashmap_percpu()
192 assert(bpf_map_update_elem(fd, &key, value, -1) < 0 && in test_hashmap_percpu()
195 /* Check that key=1 can be found. Value could be 0 if the lookup in test_hashmap_percpu()
198 bpf_percpu(value, 0) = 1; in test_hashmap_percpu()
199 assert(bpf_map_lookup_elem(fd, &key, value) == 0 && in test_hashmap_percpu()
200 bpf_percpu(value, 0) == 100); in test_hashmap_percpu()
202 key = 2; in test_hashmap_percpu()
203 /* Check that key=2 is not found. */ in test_hashmap_percpu()
204 assert(bpf_map_lookup_elem(fd, &key, value) < 0 && errno == ENOENT); in test_hashmap_percpu()
207 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) < 0 && in test_hashmap_percpu()
208 /* key=2 is not there. */ in test_hashmap_percpu()
211 /* Insert key=2 element. */ in test_hashmap_percpu()
212 assert(!(expected_key_mask & key)); in test_hashmap_percpu()
213 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) == 0); in test_hashmap_percpu()
214 expected_key_mask |= key; in test_hashmap_percpu()
216 /* key=1 and key=2 were inserted, check that key=0 cannot be in test_hashmap_percpu()
219 key = 0; in test_hashmap_percpu()
220 assert(bpf_map_update_elem(fd, &key, value, BPF_NOEXIST) < 0 && in test_hashmap_percpu()
223 /* Check that key = 0 doesn't exist. */ in test_hashmap_percpu()
224 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap_percpu()
229 while (!bpf_map_get_next_key(fd, &key, &next_key)) { in test_hashmap_percpu()
237 assert(bpf_map_lookup_elem(fd, &next_key, value) == 0); in test_hashmap_percpu()
240 assert(bpf_percpu(value, i) == i + 100); in test_hashmap_percpu()
242 key = next_key; in test_hashmap_percpu()
247 key = 1; in test_hashmap_percpu()
248 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); in test_hashmap_percpu()
251 key = 1; in test_hashmap_percpu()
252 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap_percpu()
253 key = 2; in test_hashmap_percpu()
254 assert(bpf_map_delete_elem(fd, &key) == 0); in test_hashmap_percpu()
255 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == ENOENT); in test_hashmap_percpu()
257 key = 0; in test_hashmap_percpu()
261 assert(bpf_map_get_next_key(fd, &key, &next_key) < 0 && in test_hashmap_percpu()
271 long long key, value[VALUE_SIZE] = {}; in helper_fill_hashmap() local
273 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in helper_fill_hashmap()
280 key = i; value[0] = key; in helper_fill_hashmap()
281 ret = bpf_map_update_elem(fd, &key, value, BPF_NOEXIST); in helper_fill_hashmap()
293 long long key, value[VALUE_SIZE], next_key; in test_hashmap_walk() local
298 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, in test_hashmap_walk()
300 key = next_key; in test_hashmap_walk()
301 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
306 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_hashmap_walk()
308 next_key_valid = bpf_map_get_next_key(fd, &key, &next_key) == 0; in test_hashmap_walk()
309 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
310 value[0]++; in test_hashmap_walk()
311 assert(bpf_map_update_elem(fd, &key, value, BPF_EXIST) == 0); in test_hashmap_walk()
312 key = next_key; in test_hashmap_walk()
317 for (i = 0; bpf_map_get_next_key(fd, !i ? NULL : &key, in test_hashmap_walk()
319 key = next_key; in test_hashmap_walk()
320 assert(bpf_map_lookup_elem(fd, &key, value) == 0); in test_hashmap_walk()
321 assert(value[0] - 1 == key); in test_hashmap_walk()
331 long long key, next_first, next_second; in test_hashmap_zero_seed() local
340 void *key_ptr = !i ? NULL : &key; in test_hashmap_zero_seed()
353 key = next_first; in test_hashmap_zero_seed()
363 int key, next_key, fd; in test_arraymap() local
364 long long value; in test_arraymap() local
366 fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, NULL, sizeof(key), sizeof(value), 2, NULL); in test_arraymap()
372 key = 1; in test_arraymap()
373 value = 1234; in test_arraymap()
374 /* Insert key=1 element. */ in test_arraymap()
375 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_arraymap()
377 value = 0; in test_arraymap()
378 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_arraymap()
381 /* Check that key=1 can be found. */ in test_arraymap()
382 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 1234); in test_arraymap()
384 key = 0; in test_arraymap()
385 /* Check that key=0 is also found and zero initialized. */ in test_arraymap()
386 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); in test_arraymap()
388 /* key=0 and key=1 were inserted, check that key=2 cannot be inserted in test_arraymap()
391 key = 2; in test_arraymap()
392 assert(bpf_map_update_elem(fd, &key, &value, BPF_EXIST) < 0 && in test_arraymap()
395 /* Check that key = 2 doesn't exist. */ in test_arraymap()
396 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_arraymap()
401 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_arraymap()
409 key = 1; in test_arraymap()
410 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); in test_arraymap()
419 int key, next_key, fd, i; in test_arraymap_percpu() local
421 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), in test_arraymap_percpu()
431 key = 1; in test_arraymap_percpu()
432 /* Insert key=1 element. */ in test_arraymap_percpu()
433 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); in test_arraymap_percpu()
436 assert(bpf_map_update_elem(fd, &key, values, BPF_NOEXIST) < 0 && in test_arraymap_percpu()
439 /* Check that key=1 can be found. */ in test_arraymap_percpu()
440 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && in test_arraymap_percpu()
443 key = 0; in test_arraymap_percpu()
444 /* Check that key=0 is also found and zero initialized. */ in test_arraymap_percpu()
445 assert(bpf_map_lookup_elem(fd, &key, values) == 0 && in test_arraymap_percpu()
447 bpf_percpu(values, nr_cpus - 1) == 0); in test_arraymap_percpu()
449 /* Check that key=2 cannot be inserted due to max_entries limit. */ in test_arraymap_percpu()
450 key = 2; in test_arraymap_percpu()
451 assert(bpf_map_update_elem(fd, &key, values, BPF_EXIST) < 0 && in test_arraymap_percpu()
454 /* Check that key = 2 doesn't exist. */ in test_arraymap_percpu()
455 assert(bpf_map_lookup_elem(fd, &key, values) < 0 && errno == ENOENT); in test_arraymap_percpu()
460 assert(bpf_map_get_next_key(fd, &key, &next_key) == 0 && in test_arraymap_percpu()
468 key = 1; in test_arraymap_percpu()
469 assert(bpf_map_delete_elem(fd, &key) < 0 && errno == EINVAL); in test_arraymap_percpu()
482 int key, fd, i; in test_arraymap_percpu_many_keys() local
484 fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, NULL, sizeof(key), in test_arraymap_percpu_many_keys()
487 printf("Failed to create per-cpu arraymap '%s'!\n", in test_arraymap_percpu_many_keys()
495 for (key = 0; key < nr_keys; key++) in test_arraymap_percpu_many_keys()
496 assert(bpf_map_update_elem(fd, &key, values, BPF_ANY) == 0); in test_arraymap_percpu_many_keys()
498 for (key = 0; key < nr_keys; key++) { in test_arraymap_percpu_many_keys()
502 assert(bpf_map_lookup_elem(fd, &key, values) == 0); in test_arraymap_percpu_many_keys()
514 __u32 key, value; in test_devmap() local
516 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP, NULL, sizeof(key), sizeof(value), 2, NULL); in test_devmap()
528 __u32 key, value; in test_devmap_hash() local
530 fd = bpf_map_create(BPF_MAP_TYPE_DEVMAP_HASH, NULL, sizeof(key), sizeof(value), 2, NULL); in test_devmap_hash()
549 /* Invalid key size */ in test_queuemap()
605 /* Invalid key size */ in test_stackmap()
629 assert(bpf_map_lookup_elem(fd, NULL, &val) == 0 && val == vals[i - 1]); in test_stackmap()
636 for (i = MAP_SIZE + MAP_SIZE/2 - 1; i >= MAP_SIZE/2; i--) in test_stackmap()
670 __u32 key, value; in test_sockmap() local
720 addr.sin_port = htons(ports[i - 2]); in test_sockmap()
730 sfd[i] = accept(sfd[i - 4], NULL, NULL); in test_sockmap()
739 sizeof(key), sizeof(value), in test_sockmap()
777 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_PARSER, 0); in test_sockmap()
783 err = bpf_prog_attach(-1, fd, BPF_SK_SKB_STREAM_VERDICT, 0); in test_sockmap()
789 err = bpf_prog_attach(-1, fd, BPF_SK_MSG_VERDICT, 0); in test_sockmap()
795 err = bpf_prog_attach(-1, fd, __MAX_BPF_ATTACH_TYPE, 0); in test_sockmap()
984 if (s == -1) { in test_sockmap()
1090 } else if (pid[i] == -1) { in test_sockmap()
1234 fd = -1; in test_map_in_map()
1315 } key; in test_map_large() local
1316 int fd, i, value; in test_map_large() local
1318 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_large()
1326 key = (struct bigkey) { .c = i }; in test_map_large()
1327 value = i; in test_map_large()
1329 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) == 0); in test_map_large()
1332 key.c = -1; in test_map_large()
1333 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_map_large()
1337 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_map_large()
1338 key.c = -1; in test_map_large()
1340 assert(bpf_map_get_next_key(fd, &key, &key) == 0); in test_map_large()
1341 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_large()
1343 key.c = 0; in test_map_large()
1344 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && value == 0); in test_map_large()
1345 key.a = 1; in test_map_large()
1346 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_map_large()
1369 } else if (pid[i] == -1) { in __run_parallel()
1408 int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts, in map_update_retriable() argument
1413 while (bpf_map_update_elem(map_fd, key, value, flags)) { in map_update_retriable()
1415 return -errno; in map_update_retriable()
1421 attempts--; in map_update_retriable()
1427 static int map_delete_retriable(int map_fd, const void *key, int attempts) in map_delete_retriable() argument
1431 while (bpf_map_delete_elem(map_fd, key)) { in map_delete_retriable()
1433 return -errno; in map_delete_retriable()
1439 attempts--; in map_delete_retriable()
1449 int i, key, value, err; in test_update_delete() local
1454 key = value = i; in test_update_delete()
1457 err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES, in test_update_delete()
1462 err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES, in test_update_delete()
1468 err = map_delete_retriable(fd, &key, MAP_RETRIES); in test_update_delete()
1478 int i, fd, key = 0, value = 0, j = 0; in test_map_parallel() local
1481 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_parallel()
1491 * child_0 adds key=0, key=1024, key=2048, ... in test_map_parallel()
1492 * child_1 adds key=1, key=1025, key=2049, ... in test_map_parallel()
1493 * child_1023 adds key=1023, ... in test_map_parallel()
1499 /* Check that key=0 is already there. */ in test_map_parallel()
1500 assert(bpf_map_update_elem(fd, &key, &value, BPF_NOEXIST) < 0 && in test_map_parallel()
1504 assert(bpf_map_get_next_key(fd, NULL, &key) == 0); in test_map_parallel()
1505 key = -1; in test_map_parallel()
1507 assert(bpf_map_get_next_key(fd, &key, &key) == 0); in test_map_parallel()
1508 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_parallel()
1512 key = MAP_SIZE - i - 1; in test_map_parallel()
1514 assert(bpf_map_lookup_elem(fd, &key, &value) == 0 && in test_map_parallel()
1515 value == key); in test_map_parallel()
1523 key = -1; in test_map_parallel()
1524 assert(bpf_map_get_next_key(fd, NULL, &key) < 0 && errno == ENOENT); in test_map_parallel()
1525 assert(bpf_map_get_next_key(fd, &key, &key) < 0 && errno == ENOENT); in test_map_parallel()
1527 key = 0; in test_map_parallel()
1528 bpf_map_delete_elem(fd, &key); in test_map_parallel()
1536 int fd, key = 0, value = 0; in test_map_rdonly() local
1541 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_rdonly()
1550 key = 1; in test_map_rdonly()
1551 value = 1234; in test_map_rdonly()
1552 /* Try to insert key=1 element. */ in test_map_rdonly()
1553 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) < 0 && in test_map_rdonly()
1556 /* Check that key=1 is not found. */ in test_map_rdonly()
1557 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == ENOENT); in test_map_rdonly()
1558 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == ENOENT); in test_map_rdonly()
1565 int fd, key = 0, value = 0; in test_map_wronly_hash() local
1570 fd = bpf_map_create(BPF_MAP_TYPE_HASH, NULL, sizeof(key), sizeof(value), in test_map_wronly_hash()
1579 key = 1; in test_map_wronly_hash()
1580 value = 1234; in test_map_wronly_hash()
1581 /* Insert key=1 element. */ in test_map_wronly_hash()
1582 assert(bpf_map_update_elem(fd, &key, &value, BPF_ANY) == 0); in test_map_wronly_hash()
1585 assert(bpf_map_lookup_elem(fd, &key, &value) < 0 && errno == EPERM); in test_map_wronly_hash()
1586 assert(bpf_map_get_next_key(fd, &key, &value) < 0 && errno == EPERM); in test_map_wronly_hash()
1593 int fd, value = 0; in test_map_wronly_stack_or_queue() local
1601 fd = bpf_map_create(map_type, NULL, 0, sizeof(value), MAP_SIZE, &map_opts); in test_map_wronly_stack_or_queue()
1613 value = 1234; in test_map_wronly_stack_or_queue()
1614 assert(bpf_map_update_elem(fd, NULL, &value, BPF_ANY) == 0); in test_map_wronly_stack_or_queue()
1617 assert(bpf_map_lookup_elem(fd, NULL, &value) < 0 && errno == EPERM); in test_map_wronly_stack_or_queue()
1620 assert(bpf_map_lookup_and_delete_elem(fd, NULL, &value) < 0 && in test_map_wronly_stack_or_queue()
1643 void *value; in prepare_reuseport_grp() local
1656 CHECK(fd64 == -1, "socket()", in prepare_reuseport_grp()
1662 CHECK(err == -1, "setsockopt(SO_REUSEPORT)", in prepare_reuseport_grp()
1667 value = &fd64; in prepare_reuseport_grp()
1671 value = &fd32; in prepare_reuseport_grp()
1673 err = bpf_map_update_elem(map_fd, &index0, value, BPF_ANY); in prepare_reuseport_grp()
1680 CHECK(err == -1, "bind()", in prepare_reuseport_grp()
1686 CHECK(err == -1, "getsockname()", in prepare_reuseport_grp()
1693 CHECK(err == -1, "getsockopt(SO_COOKIE)", in prepare_reuseport_grp()
1699 * non-listening tcp sk. in prepare_reuseport_grp()
1701 err = bpf_map_update_elem(map_fd, &index0, value, in prepare_reuseport_grp()
1704 "reuseport array update non-listening sk", in prepare_reuseport_grp()
1708 CHECK(err == -1, "listen()", in prepare_reuseport_grp()
1725 __s64 grpa_fds64[2] = { -1, -1 }, fd64 = -1; in test_reuseport_array()
1754 "reuseport array lookup not-exist elem", in test_reuseport_array()
1758 "reuseport array del not-exist elem", in test_reuseport_array()
1798 "reuseport array update non-empty elem BPF_NOEXIST", in test_reuseport_array()
1835 "reuseport array re-add with BPF_NOEXIST after del", in test_reuseport_array()
1841 "reuseport array lookup re-added sk", in test_reuseport_array()
1857 CHECK(fd64 == -1, "socket(SOCK_RAW)", "err:%d errno:%d\n", in test_reuseport_array()
1864 /* Close the 64 bit value map */ in test_reuseport_array()