Skip to content

Commit 1e000bc

Browse files
sm00thKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: fix map tests errno checks
Switching to libbpf 1.0 API broke test_lpm_map and test_lru_map as error reporting changed. Instead of setting errno and returning -1 bpf calls now return -Exxx directly. Drop errno checks and look at return code directly. Fixes: b858ba8 ("selftests/bpf: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK") Signed-off-by: Artem Savkov <[email protected]> Reviewed-by: Yafang Shao <[email protected]>
1 parent e616540 commit 1e000bc

File tree

2 files changed

+37
-68
lines changed

2 files changed

+37
-68
lines changed

tools/testing/selftests/bpf/test_lpm_map.c

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,13 @@ static void test_lpm_ipaddr(void)
408408

409409
/* Test some lookups that should not match any entry */
410410
inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
411-
assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
412-
errno == ENOENT);
411+
assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -ENOENT);
413412

414413
inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
415-
assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
416-
errno == ENOENT);
414+
assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -ENOENT);
417415

418416
inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
419-
assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
420-
errno == ENOENT);
417+
assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -ENOENT);
421418

422419
close(map_fd_ipv4);
423420
close(map_fd_ipv6);
@@ -474,18 +471,15 @@ static void test_lpm_delete(void)
474471
/* remove non-existent node */
475472
key->prefixlen = 32;
476473
inet_pton(AF_INET, "10.0.0.1", key->data);
477-
assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
478-
errno == ENOENT);
474+
assert(bpf_map_lookup_elem(map_fd, key, &value) == -ENOENT);
479475

480476
key->prefixlen = 30; // unused prefix so far
481477
inet_pton(AF_INET, "192.255.0.0", key->data);
482-
assert(bpf_map_delete_elem(map_fd, key) == -1 &&
483-
errno == ENOENT);
478+
assert(bpf_map_delete_elem(map_fd, key) == -ENOENT);
484479

485480
key->prefixlen = 16; // same prefix as the root node
486481
inet_pton(AF_INET, "192.255.0.0", key->data);
487-
assert(bpf_map_delete_elem(map_fd, key) == -1 &&
488-
errno == ENOENT);
482+
assert(bpf_map_delete_elem(map_fd, key) == -ENOENT);
489483

490484
/* assert initial lookup */
491485
key->prefixlen = 32;
@@ -530,8 +524,7 @@ static void test_lpm_delete(void)
530524

531525
key->prefixlen = 32;
532526
inet_pton(AF_INET, "192.168.128.1", key->data);
533-
assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
534-
errno == ENOENT);
527+
assert(bpf_map_lookup_elem(map_fd, key, &value) == -ENOENT);
535528

536529
close(map_fd);
537530
}
@@ -552,8 +545,7 @@ static void test_lpm_get_next_key(void)
552545
assert(map_fd >= 0);
553546

554547
/* empty tree. get_next_key should return ENOENT */
555-
assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -1 &&
556-
errno == ENOENT);
548+
assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -ENOENT);
557549

558550
/* get and verify the first key, get the second one should fail. */
559551
key_p->prefixlen = 16;
@@ -565,8 +557,7 @@ static void test_lpm_get_next_key(void)
565557
assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
566558
key_p->data[1] == 168);
567559

568-
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
569-
errno == ENOENT);
560+
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
570561

571562
/* no exact matching key should get the first one in post order. */
572563
key_p->prefixlen = 8;
@@ -590,8 +581,7 @@ static void test_lpm_get_next_key(void)
590581
next_key_p->data[1] == 168);
591582

592583
memcpy(key_p, next_key_p, key_size);
593-
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
594-
errno == ENOENT);
584+
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
595585

596586
/* Add one more element (total three) */
597587
key_p->prefixlen = 24;
@@ -614,8 +604,7 @@ static void test_lpm_get_next_key(void)
614604
next_key_p->data[1] == 168);
615605

616606
memcpy(key_p, next_key_p, key_size);
617-
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
618-
errno == ENOENT);
607+
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
619608

620609
/* Add one more element (total four) */
621610
key_p->prefixlen = 24;
@@ -643,8 +632,7 @@ static void test_lpm_get_next_key(void)
643632
next_key_p->data[1] == 168);
644633

645634
memcpy(key_p, next_key_p, key_size);
646-
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
647-
errno == ENOENT);
635+
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
648636

649637
/* Add one more element (total five) */
650638
key_p->prefixlen = 28;
@@ -678,8 +666,7 @@ static void test_lpm_get_next_key(void)
678666
next_key_p->data[1] == 168);
679667

680668
memcpy(key_p, next_key_p, key_size);
681-
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
682-
errno == ENOENT);
669+
assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -ENOENT);
683670

684671
/* no exact matching key should return the first one in post order */
685672
key_p->prefixlen = 22;

tools/testing/selftests/bpf/test_lru_map.c

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,28 @@ static void test_lru_sanity0(int map_type, int map_flags)
175175
BPF_NOEXIST));
176176

177177
/* BPF_NOEXIST means: add new element if it doesn't exist */
178-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -1
179-
/* key=1 already exists */
180-
&& errno == EEXIST);
178+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -EEXIST);
179+
/* key=1 already exists */
181180

182-
assert(bpf_map_update_elem(lru_map_fd, &key, value, -1) == -1 &&
183-
errno == EINVAL);
181+
assert(bpf_map_update_elem(lru_map_fd, &key, value, -1) == -EINVAL);
184182

185183
/* insert key=2 element */
186184

187185
/* check that key=2 is not found */
188186
key = 2;
189-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
190-
errno == ENOENT);
187+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
191188

192189
/* BPF_EXIST means: update existing element */
193-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
194-
/* key=2 is not there */
195-
errno == ENOENT);
190+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -ENOENT);
191+
/* key=2 is not there */
196192

197193
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
198194

199195
/* insert key=3 element */
200196

201197
/* check that key=3 is not found */
202198
key = 3;
203-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
204-
errno == ENOENT);
199+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
205200

206201
/* check that key=1 can be found and mark the ref bit to
207202
* stop LRU from removing key=1
@@ -217,8 +212,7 @@ static void test_lru_sanity0(int map_type, int map_flags)
217212

218213
/* key=2 has been removed from the LRU */
219214
key = 2;
220-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
221-
errno == ENOENT);
215+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
222216

223217
/* lookup elem key=1 and delete it, then check it doesn't exist */
224218
key = 1;
@@ -381,8 +375,7 @@ static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
381375
end_key = 1 + batch_size;
382376
value[0] = 4321;
383377
for (key = 1; key < end_key; key++) {
384-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
385-
errno == ENOENT);
378+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
386379
assert(!bpf_map_update_elem(lru_map_fd, &key, value,
387380
BPF_NOEXIST));
388381
assert(!bpf_map_lookup_elem_with_ref_bit(lru_map_fd, key, value));
@@ -562,8 +555,7 @@ static void do_test_lru_sanity5(unsigned long long last_key, int map_fd)
562555
assert(!bpf_map_lookup_elem_with_ref_bit(map_fd, key, value));
563556

564557
/* Cannot find the last key because it was removed by LRU */
565-
assert(bpf_map_lookup_elem(map_fd, &last_key, value) == -1 &&
566-
errno == ENOENT);
558+
assert(bpf_map_lookup_elem(map_fd, &last_key, value) == -ENOENT);
567559
}
568560

569561
/* Test map with only one element */
@@ -711,30 +703,26 @@ static void test_lru_sanity7(int map_type, int map_flags)
711703
BPF_NOEXIST));
712704

713705
/* BPF_NOEXIST means: add new element if it doesn't exist */
714-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -1
715-
/* key=1 already exists */
716-
&& errno == EEXIST);
706+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -EEXIST);
707+
/* key=1 already exists */
717708

718709
/* insert key=2 element */
719710

720711
/* check that key=2 is not found */
721712
key = 2;
722-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
723-
errno == ENOENT);
713+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
724714

725715
/* BPF_EXIST means: update existing element */
726-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
727-
/* key=2 is not there */
728-
errno == ENOENT);
716+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -ENOENT);
717+
/* key=2 is not there */
729718

730719
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
731720

732721
/* insert key=3 element */
733722

734723
/* check that key=3 is not found */
735724
key = 3;
736-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
737-
errno == ENOENT);
725+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
738726

739727
/* check that key=1 can be found and mark the ref bit to
740728
* stop LRU from removing key=1
@@ -757,8 +745,7 @@ static void test_lru_sanity7(int map_type, int map_flags)
757745

758746
/* key=2 has been removed from the LRU */
759747
key = 2;
760-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
761-
errno == ENOENT);
748+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
762749

763750
assert(map_equal(lru_map_fd, expected_map_fd));
764751

@@ -805,21 +792,18 @@ static void test_lru_sanity8(int map_type, int map_flags)
805792
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
806793

807794
/* BPF_NOEXIST means: add new element if it doesn't exist */
808-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -1
809-
/* key=1 already exists */
810-
&& errno == EEXIST);
795+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -EEXIST);
796+
/* key=1 already exists */
811797

812798
/* insert key=2 element */
813799

814800
/* check that key=2 is not found */
815801
key = 2;
816-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
817-
errno == ENOENT);
802+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
818803

819804
/* BPF_EXIST means: update existing element */
820-
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
821-
/* key=2 is not there */
822-
errno == ENOENT);
805+
assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -ENOENT);
806+
/* key=2 is not there */
823807

824808
assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
825809
assert(!bpf_map_update_elem(expected_map_fd, &key, value,
@@ -829,8 +813,7 @@ static void test_lru_sanity8(int map_type, int map_flags)
829813

830814
/* check that key=3 is not found */
831815
key = 3;
832-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
833-
errno == ENOENT);
816+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
834817

835818
/* check that key=1 can be found and do _not_ mark ref bit.
836819
* this will be evicted on next update.
@@ -853,8 +836,7 @@ static void test_lru_sanity8(int map_type, int map_flags)
853836

854837
/* key=1 has been removed from the LRU */
855838
key = 1;
856-
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
857-
errno == ENOENT);
839+
assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -ENOENT);
858840

859841
assert(map_equal(lru_map_fd, expected_map_fd));
860842

0 commit comments

Comments
 (0)