Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add test for bpf hash map iterators

Two subtests are added.
$ ./test_progs -n 4
...
#4/18 bpf_hash_map:OK
#4/19 bpf_percpu_hash_map:OK
...

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200723184120.590916-1-yhs@fb.com

authored by

Yonghong Song and committed by
Alexei Starovoitov
2a7c2fff d8793aca

+337
+187
tools/testing/selftests/bpf/prog_tests/bpf_iter.c
··· 15 15 #include "bpf_iter_test_kern2.skel.h" 16 16 #include "bpf_iter_test_kern3.skel.h" 17 17 #include "bpf_iter_test_kern4.skel.h" 18 + #include "bpf_iter_bpf_hash_map.skel.h" 19 + #include "bpf_iter_bpf_percpu_hash_map.skel.h" 18 20 19 21 static int duration; 20 22 ··· 457 455 bpf_iter_test_kern4__destroy(skel); 458 456 } 459 457 458 + static void test_bpf_hash_map(void) 459 + { 460 + __u32 expected_key_a = 0, expected_key_b = 0, expected_key_c = 0; 461 + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); 462 + struct bpf_iter_bpf_hash_map *skel; 463 + int err, i, len, map_fd, iter_fd; 464 + __u64 val, expected_val = 0; 465 + struct bpf_link *link; 466 + struct key_t { 467 + int a; 468 + int b; 469 + int c; 470 + } key; 471 + char buf[64]; 472 + 473 + skel = bpf_iter_bpf_hash_map__open(); 474 + if (CHECK(!skel, "bpf_iter_bpf_hash_map__open", 475 + "skeleton open failed\n")) 476 + return; 477 + 478 + skel->bss->in_test_mode = true; 479 + 480 + err = bpf_iter_bpf_hash_map__load(skel); 481 + if (CHECK(!skel, "bpf_iter_bpf_hash_map__load", 482 + "skeleton load failed\n")) 483 + goto out; 484 + 485 + /* iterator with hashmap2 and hashmap3 should fail */ 486 + opts.map_fd = bpf_map__fd(skel->maps.hashmap2); 487 + link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); 488 + if (CHECK(!IS_ERR(link), "attach_iter", 489 + "attach_iter for hashmap2 unexpected succeeded\n")) 490 + goto out; 491 + 492 + opts.map_fd = bpf_map__fd(skel->maps.hashmap3); 493 + link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); 494 + if (CHECK(!IS_ERR(link), "attach_iter", 495 + "attach_iter for hashmap3 unexpected succeeded\n")) 496 + goto out; 497 + 498 + /* hashmap1 should be good, update map values here */ 499 + map_fd = bpf_map__fd(skel->maps.hashmap1); 500 + for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap1); i++) { 501 + key.a = i + 1; 502 + key.b = i + 2; 503 + key.c = i + 3; 504 + val = i + 4; 505 + expected_key_a += key.a; 506 + expected_key_b += key.b; 507 + expected_key_c += key.c; 508 + expected_val += val; 509 + 510 + err = bpf_map_update_elem(map_fd, &key, &val, BPF_ANY); 511 + if (CHECK(err, "map_update", "map_update failed\n")) 512 + goto out; 513 + } 514 + 515 + opts.map_fd = map_fd; 516 + link = bpf_program__attach_iter(skel->progs.dump_bpf_hash_map, &opts); 517 + if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) 518 + goto out; 519 + 520 + iter_fd = bpf_iter_create(bpf_link__fd(link)); 521 + if (CHECK(iter_fd < 0, "create_iter", "create_iter failed\n")) 522 + goto free_link; 523 + 524 + /* do some tests */ 525 + while ((len = read(iter_fd, buf, sizeof(buf))) > 0) 526 + ; 527 + if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) 528 + goto close_iter; 529 + 530 + /* test results */ 531 + if (CHECK(skel->bss->key_sum_a != expected_key_a, 532 + "key_sum_a", "got %u expected %u\n", 533 + skel->bss->key_sum_a, expected_key_a)) 534 + goto close_iter; 535 + if (CHECK(skel->bss->key_sum_b != expected_key_b, 536 + "key_sum_b", "got %u expected %u\n", 537 + skel->bss->key_sum_b, expected_key_b)) 538 + goto close_iter; 539 + if (CHECK(skel->bss->val_sum != expected_val, 540 + "val_sum", "got %llu expected %llu\n", 541 + skel->bss->val_sum, expected_val)) 542 + goto close_iter; 543 + 544 + close_iter: 545 + close(iter_fd); 546 + free_link: 547 + bpf_link__destroy(link); 548 + out: 549 + bpf_iter_bpf_hash_map__destroy(skel); 550 + } 551 + 552 + static void test_bpf_percpu_hash_map(void) 553 + { 554 + __u32 expected_key_a = 0, expected_key_b = 0, expected_key_c = 0; 555 + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); 556 + struct bpf_iter_bpf_percpu_hash_map *skel; 557 + int err, i, j, len, map_fd, iter_fd; 558 + __u32 expected_val = 0; 559 + struct bpf_link *link; 560 + struct key_t { 561 + int a; 562 + int b; 563 + int c; 564 + } key; 565 + char buf[64]; 566 + void *val; 567 + 568 + val = malloc(8 * bpf_num_possible_cpus()); 569 + 570 + skel = bpf_iter_bpf_percpu_hash_map__open(); 571 + if (CHECK(!skel, "bpf_iter_bpf_percpu_hash_map__open", 572 + "skeleton open failed\n")) 573 + return; 574 + 575 + skel->rodata->num_cpus = bpf_num_possible_cpus(); 576 + 577 + err = bpf_iter_bpf_percpu_hash_map__load(skel); 578 + if (CHECK(!skel, "bpf_iter_bpf_percpu_hash_map__load", 579 + "skeleton load failed\n")) 580 + goto out; 581 + 582 + /* update map values here */ 583 + map_fd = bpf_map__fd(skel->maps.hashmap1); 584 + for (i = 0; i < bpf_map__max_entries(skel->maps.hashmap1); i++) { 585 + key.a = i + 1; 586 + key.b = i + 2; 587 + key.c = i + 3; 588 + expected_key_a += key.a; 589 + expected_key_b += key.b; 590 + expected_key_c += key.c; 591 + 592 + for (j = 0; j < bpf_num_possible_cpus(); j++) { 593 + *(__u32 *)(val + j * 8) = i + j; 594 + expected_val += i + j; 595 + } 596 + 597 + err = bpf_map_update_elem(map_fd, &key, val, BPF_ANY); 598 + if (CHECK(err, "map_update", "map_update failed\n")) 599 + goto out; 600 + } 601 + 602 + opts.map_fd = map_fd; 603 + link = bpf_program__attach_iter(skel->progs.dump_bpf_percpu_hash_map, &opts); 604 + if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) 605 + goto out; 606 + 607 + iter_fd = bpf_iter_create(bpf_link__fd(link)); 608 + if (CHECK(iter_fd < 0, "create_iter", "create_iter failed\n")) 609 + goto free_link; 610 + 611 + /* do some tests */ 612 + while ((len = read(iter_fd, buf, sizeof(buf))) > 0) 613 + ; 614 + if (CHECK(len < 0, "read", "read failed: %s\n", strerror(errno))) 615 + goto close_iter; 616 + 617 + /* test results */ 618 + if (CHECK(skel->bss->key_sum_a != expected_key_a, 619 + "key_sum_a", "got %u expected %u\n", 620 + skel->bss->key_sum_a, expected_key_a)) 621 + goto close_iter; 622 + if (CHECK(skel->bss->key_sum_b != expected_key_b, 623 + "key_sum_b", "got %u expected %u\n", 624 + skel->bss->key_sum_b, expected_key_b)) 625 + goto close_iter; 626 + if (CHECK(skel->bss->val_sum != expected_val, 627 + "val_sum", "got %u expected %u\n", 628 + skel->bss->val_sum, expected_val)) 629 + goto close_iter; 630 + 631 + close_iter: 632 + close(iter_fd); 633 + free_link: 634 + bpf_link__destroy(link); 635 + out: 636 + bpf_iter_bpf_percpu_hash_map__destroy(skel); 637 + } 638 + 460 639 void test_bpf_iter(void) 461 640 { 462 641 if (test__start_subtest("btf_id_or_null")) ··· 674 491 test_overflow(true, false); 675 492 if (test__start_subtest("prog-ret-1")) 676 493 test_overflow(false, true); 494 + if (test__start_subtest("bpf_hash_map")) 495 + test_bpf_hash_map(); 496 + if (test__start_subtest("bpf_percpu_hash_map")) 497 + test_bpf_percpu_hash_map(); 677 498 }
+100
tools/testing/selftests/bpf/progs/bpf_iter_bpf_hash_map.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2020 Facebook */ 3 + #include "bpf_iter.h" 4 + #include <bpf/bpf_helpers.h> 5 + #include <bpf/bpf_tracing.h> 6 + 7 + char _license[] SEC("license") = "GPL"; 8 + 9 + struct key_t { 10 + int a; 11 + int b; 12 + int c; 13 + }; 14 + 15 + struct { 16 + __uint(type, BPF_MAP_TYPE_HASH); 17 + __uint(max_entries, 3); 18 + __type(key, struct key_t); 19 + __type(value, __u64); 20 + } hashmap1 SEC(".maps"); 21 + 22 + struct { 23 + __uint(type, BPF_MAP_TYPE_HASH); 24 + __uint(max_entries, 3); 25 + __type(key, __u64); 26 + __type(value, __u64); 27 + } hashmap2 SEC(".maps"); 28 + 29 + struct { 30 + __uint(type, BPF_MAP_TYPE_HASH); 31 + __uint(max_entries, 3); 32 + __type(key, struct key_t); 33 + __type(value, __u32); 34 + } hashmap3 SEC(".maps"); 35 + 36 + /* will set before prog run */ 37 + bool in_test_mode = 0; 38 + 39 + /* will collect results during prog run */ 40 + __u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0; 41 + __u64 val_sum = 0; 42 + 43 + SEC("iter/bpf_map_elem") 44 + int dump_bpf_hash_map(struct bpf_iter__bpf_map_elem *ctx) 45 + { 46 + struct seq_file *seq = ctx->meta->seq; 47 + __u32 seq_num = ctx->meta->seq_num; 48 + struct bpf_map *map = ctx->map; 49 + struct key_t *key = ctx->key; 50 + __u64 *val = ctx->value; 51 + 52 + if (in_test_mode) { 53 + /* test mode is used by selftests to 54 + * test functionality of bpf_hash_map iter. 55 + * 56 + * the above hashmap1 will have correct size 57 + * and will be accepted, hashmap2 and hashmap3 58 + * should be rejected due to smaller key/value 59 + * size. 60 + */ 61 + if (key == (void *)0 || val == (void *)0) 62 + return 0; 63 + 64 + key_sum_a += key->a; 65 + key_sum_b += key->b; 66 + key_sum_c += key->c; 67 + val_sum += *val; 68 + return 0; 69 + } 70 + 71 + /* non-test mode, the map is prepared with the 72 + * below bpftool command sequence: 73 + * bpftool map create /sys/fs/bpf/m1 type hash \ 74 + * key 12 value 8 entries 3 name map1 75 + * bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 1 \ 76 + * value 0 0 0 1 0 0 0 1 77 + * bpftool map update id 77 key 0 0 0 1 0 0 0 0 0 0 0 2 \ 78 + * value 0 0 0 1 0 0 0 2 79 + * The bpftool iter command line: 80 + * bpftool iter pin ./bpf_iter_bpf_hash_map.o /sys/fs/bpf/p1 \ 81 + * map id 77 82 + * The below output will be: 83 + * map dump starts 84 + * 77: (1000000 0 2000000) (200000001000000) 85 + * 77: (1000000 0 1000000) (100000001000000) 86 + * map dump ends 87 + */ 88 + if (seq_num == 0) 89 + BPF_SEQ_PRINTF(seq, "map dump starts\n"); 90 + 91 + if (key == (void *)0 || val == (void *)0) { 92 + BPF_SEQ_PRINTF(seq, "map dump ends\n"); 93 + return 0; 94 + } 95 + 96 + BPF_SEQ_PRINTF(seq, "%d: (%x %d %x) (%llx)\n", map->id, 97 + key->a, key->b, key->c, *val); 98 + 99 + return 0; 100 + }
+50
tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_hash_map.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2020 Facebook */ 3 + #include "bpf_iter.h" 4 + #include <bpf/bpf_helpers.h> 5 + #include <bpf/bpf_tracing.h> 6 + 7 + char _license[] SEC("license") = "GPL"; 8 + 9 + struct key_t { 10 + int a; 11 + int b; 12 + int c; 13 + }; 14 + 15 + struct { 16 + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); 17 + __uint(max_entries, 3); 18 + __type(key, struct key_t); 19 + __type(value, __u32); 20 + } hashmap1 SEC(".maps"); 21 + 22 + /* will set before prog run */ 23 + volatile const __u32 num_cpus = 0; 24 + 25 + /* will collect results during prog run */ 26 + __u32 key_sum_a = 0, key_sum_b = 0, key_sum_c = 0; 27 + __u32 val_sum = 0; 28 + 29 + SEC("iter/bpf_map_elem") 30 + int dump_bpf_percpu_hash_map(struct bpf_iter__bpf_map_elem *ctx) 31 + { 32 + struct key_t *key = ctx->key; 33 + void *pptr = ctx->value; 34 + __u32 step; 35 + int i; 36 + 37 + if (key == (void *)0 || pptr == (void *)0) 38 + return 0; 39 + 40 + key_sum_a += key->a; 41 + key_sum_b += key->b; 42 + key_sum_c += key->c; 43 + 44 + step = 8; 45 + for (i = 0; i < num_cpus; i++) { 46 + val_sum += *(__u32 *)pptr; 47 + pptr += step; 48 + } 49 + return 0; 50 + }