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

selftests/bpf: Convert legacy prog load APIs to bpf_prog_load()

Convert all the uses of legacy low-level BPF program loading APIs
(mostly bpf_load_program_xattr(), but also some bpf_verify_program()) to
bpf_prog_load() uses.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211103220845.2676888-10-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
d8e86407 3d1d6239

+97 -110
+8 -3
tools/testing/selftests/bpf/prog_tests/align.c
··· 594 594 struct bpf_insn *prog = test->insns; 595 595 int prog_type = test->prog_type; 596 596 char bpf_vlog_copy[32768]; 597 + LIBBPF_OPTS(bpf_prog_load_opts, opts, 598 + .prog_flags = BPF_F_STRICT_ALIGNMENT, 599 + .log_buf = bpf_vlog, 600 + .log_size = sizeof(bpf_vlog), 601 + .log_level = 2, 602 + ); 597 603 const char *line_ptr; 598 604 int cur_line = -1; 599 605 int prog_len, i; ··· 607 601 int ret; 608 602 609 603 prog_len = probe_filter_length(prog); 610 - fd_prog = bpf_verify_program(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, 611 - prog, prog_len, BPF_F_STRICT_ALIGNMENT, 612 - "GPL", 0, bpf_vlog, sizeof(bpf_vlog), 2); 604 + fd_prog = bpf_prog_load(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", 605 + prog, prog_len, &opts); 613 606 if (fd_prog < 0 && test->result != REJECT) { 614 607 printf("Failed to load program.\n"); 615 608 printf("%s", bpf_vlog);
+17 -16
tools/testing/selftests/bpf/prog_tests/fexit_stress.c
··· 20 20 BPF_EXIT_INSN(), 21 21 }; 22 22 23 - struct bpf_load_program_attr load_attr = { 24 - .prog_type = BPF_PROG_TYPE_TRACING, 25 - .license = "GPL", 26 - .insns = trace_program, 27 - .insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn), 23 + LIBBPF_OPTS(bpf_prog_load_opts, trace_opts, 28 24 .expected_attach_type = BPF_TRACE_FEXIT, 29 - }; 25 + .log_buf = error, 26 + .log_size = sizeof(error), 27 + ); 30 28 31 29 const struct bpf_insn skb_program[] = { 32 30 BPF_MOV64_IMM(BPF_REG_0, 0), 33 31 BPF_EXIT_INSN(), 34 32 }; 35 33 36 - struct bpf_load_program_attr skb_load_attr = { 37 - .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, 38 - .license = "GPL", 39 - .insns = skb_program, 40 - .insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn), 41 - }; 34 + LIBBPF_OPTS(bpf_prog_load_opts, skb_opts, 35 + .log_buf = error, 36 + .log_size = sizeof(error), 37 + ); 42 38 43 39 err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1", 44 - load_attr.expected_attach_type); 40 + trace_opts.expected_attach_type); 45 41 if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err)) 46 42 goto out; 47 - load_attr.attach_btf_id = err; 43 + trace_opts.attach_btf_id = err; 48 44 49 45 for (i = 0; i < CNT; i++) { 50 - fexit_fd[i] = bpf_load_program_xattr(&load_attr, error, sizeof(error)); 46 + fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL", 47 + trace_program, 48 + sizeof(trace_program) / sizeof(struct bpf_insn), 49 + &trace_opts); 51 50 if (CHECK(fexit_fd[i] < 0, "fexit loaded", 52 51 "failed: %d errno %d\n", fexit_fd[i], errno)) 53 52 goto out; ··· 56 57 goto out; 57 58 } 58 59 59 - filter_fd = bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); 60 + filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", 61 + skb_program, sizeof(skb_program) / sizeof(struct bpf_insn), 62 + &skb_opts); 60 63 if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", 61 64 filter_fd, errno)) 62 65 goto out;
+7 -7
tools/testing/selftests/bpf/prog_tests/raw_tp_writable_reject_nbd_invalid.c
··· 18 18 BPF_EXIT_INSN(), 19 19 }; 20 20 21 - struct bpf_load_program_attr load_attr = { 22 - .prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 23 - .license = "GPL v2", 24 - .insns = program, 25 - .insns_cnt = sizeof(program) / sizeof(struct bpf_insn), 21 + LIBBPF_OPTS(bpf_prog_load_opts, opts, 26 22 .log_level = 2, 27 - }; 23 + .log_buf = error, 24 + .log_size = sizeof(error), 25 + ); 28 26 29 - bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); 27 + bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2", 28 + program, sizeof(program) / sizeof(struct bpf_insn), 29 + &opts); 30 30 if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load", 31 31 "failed: %d errno %d\n", bpf_fd, errno)) 32 32 return;
+14 -15
tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_run.c
··· 17 17 BPF_EXIT_INSN(), 18 18 }; 19 19 20 - struct bpf_load_program_attr load_attr = { 21 - .prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, 22 - .license = "GPL v2", 23 - .insns = trace_program, 24 - .insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn), 20 + LIBBPF_OPTS(bpf_prog_load_opts, trace_opts, 25 21 .log_level = 2, 26 - }; 22 + .log_buf = error, 23 + .log_size = sizeof(error), 24 + ); 27 25 28 - int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); 26 + int bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2", 27 + trace_program, sizeof(trace_program) / sizeof(struct bpf_insn), 28 + &trace_opts); 29 29 if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded", 30 30 "failed: %d errno %d\n", bpf_fd, errno)) 31 31 return; ··· 35 35 BPF_EXIT_INSN(), 36 36 }; 37 37 38 - struct bpf_load_program_attr skb_load_attr = { 39 - .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, 40 - .license = "GPL v2", 41 - .insns = skb_program, 42 - .insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn), 43 - }; 38 + LIBBPF_OPTS(bpf_prog_load_opts, skb_opts, 39 + .log_buf = error, 40 + .log_size = sizeof(error), 41 + ); 44 42 45 - int filter_fd = 46 - bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); 43 + int filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL v2", 44 + skb_program, sizeof(skb_program) / sizeof(struct bpf_insn), 45 + &skb_opts); 47 46 if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", 48 47 filter_fd, errno)) 49 48 goto out_bpffd;
+9 -10
tools/testing/selftests/bpf/prog_tests/sockopt.c
··· 852 852 static int load_prog(const struct bpf_insn *insns, 853 853 enum bpf_attach_type expected_attach_type) 854 854 { 855 - struct bpf_load_program_attr attr = { 856 - .prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT, 855 + LIBBPF_OPTS(bpf_prog_load_opts, opts, 857 856 .expected_attach_type = expected_attach_type, 858 - .insns = insns, 859 - .license = "GPL", 860 857 .log_level = 2, 861 - }; 862 - int fd; 858 + .log_buf = bpf_log_buf, 859 + .log_size = sizeof(bpf_log_buf), 860 + ); 861 + int fd, insns_cnt = 0; 863 862 864 863 for (; 865 - insns[attr.insns_cnt].code != (BPF_JMP | BPF_EXIT); 866 - attr.insns_cnt++) { 864 + insns[insns_cnt].code != (BPF_JMP | BPF_EXIT); 865 + insns_cnt++) { 867 866 } 868 - attr.insns_cnt++; 867 + insns_cnt++; 869 868 870 - fd = bpf_load_program_xattr(&attr, bpf_log_buf, sizeof(bpf_log_buf)); 869 + fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCKOPT, NULL, "GPL", insns, insns_cnt, &opts); 871 870 if (verbose && fd < 0) 872 871 fprintf(stderr, "%s\n", bpf_log_buf); 873 872
+1 -8
tools/testing/selftests/bpf/test_lru_map.c
··· 42 42 static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key, 43 43 void *value) 44 44 { 45 - struct bpf_load_program_attr prog; 46 45 struct bpf_create_map_attr map; 47 46 struct bpf_insn insns[] = { 48 47 BPF_LD_MAP_VALUE(BPF_REG_9, 0, 0), ··· 75 76 76 77 insns[0].imm = mfd; 77 78 78 - memset(&prog, 0, sizeof(prog)); 79 - prog.prog_type = BPF_PROG_TYPE_SCHED_CLS; 80 - prog.insns = insns; 81 - prog.insns_cnt = ARRAY_SIZE(insns); 82 - prog.license = "GPL"; 83 - 84 - pfd = bpf_load_program_xattr(&prog, NULL, 0); 79 + pfd = bpf_prog_load(BPF_PROG_TYPE_SCHED_CLS, NULL, "GPL", insns, ARRAY_SIZE(insns), NULL); 85 80 if (pfd < 0) { 86 81 close(mfd); 87 82 return -1;
+9 -10
tools/testing/selftests/bpf/test_sock.c
··· 328 328 static int load_sock_prog(const struct bpf_insn *prog, 329 329 enum bpf_attach_type attach_type) 330 330 { 331 - struct bpf_load_program_attr attr; 332 - int ret; 331 + LIBBPF_OPTS(bpf_prog_load_opts, opts); 332 + int ret, insn_cnt; 333 333 334 - memset(&attr, 0, sizeof(struct bpf_load_program_attr)); 335 - attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK; 336 - attr.expected_attach_type = attach_type; 337 - attr.insns = prog; 338 - attr.insns_cnt = probe_prog_length(attr.insns); 339 - attr.license = "GPL"; 340 - attr.log_level = 2; 334 + insn_cnt = probe_prog_length(prog); 341 335 342 - ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE); 336 + opts.expected_attach_type = attach_type; 337 + opts.log_buf = bpf_log_buf; 338 + opts.log_size = BPF_LOG_BUF_SIZE; 339 + opts.log_level = 2; 340 + 341 + ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", prog, insn_cnt, &opts); 343 342 if (verbose && ret < 0) 344 343 fprintf(stderr, "%s\n", bpf_log_buf); 345 344
+5 -8
tools/testing/selftests/bpf/test_sock_addr.c
··· 645 645 static int load_insns(const struct sock_addr_test *test, 646 646 const struct bpf_insn *insns, size_t insns_cnt) 647 647 { 648 - struct bpf_load_program_attr load_attr; 648 + LIBBPF_OPTS(bpf_prog_load_opts, opts); 649 649 int ret; 650 650 651 - memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); 652 - load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; 653 - load_attr.expected_attach_type = test->expected_attach_type; 654 - load_attr.insns = insns; 655 - load_attr.insns_cnt = insns_cnt; 656 - load_attr.license = "GPL"; 651 + opts.expected_attach_type = test->expected_attach_type; 652 + opts.log_buf = bpf_log_buf; 653 + opts.log_size = BPF_LOG_BUF_SIZE; 657 654 658 - ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE); 655 + ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, NULL, "GPL", insns, insns_cnt, &opts); 659 656 if (ret < 0 && test->expected_result != LOAD_REJECT) { 660 657 log_err(">>> Loading program error.\n" 661 658 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
+8 -14
tools/testing/selftests/bpf/test_sysctl.c
··· 1435 1435 const char *sysctl_path) 1436 1436 { 1437 1437 struct bpf_insn *prog = test->insns; 1438 - struct bpf_load_program_attr attr; 1439 - int ret; 1438 + LIBBPF_OPTS(bpf_prog_load_opts, opts); 1439 + int ret, insn_cnt; 1440 1440 1441 - memset(&attr, 0, sizeof(struct bpf_load_program_attr)); 1442 - attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL; 1443 - attr.insns = prog; 1444 - attr.insns_cnt = probe_prog_length(attr.insns); 1445 - attr.license = "GPL"; 1441 + insn_cnt = probe_prog_length(prog); 1446 1442 1447 1443 if (test->fixup_value_insn) { 1448 1444 char buf[128]; ··· 1461 1465 return -1; 1462 1466 } 1463 1467 1464 - ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE); 1468 + opts.log_buf = bpf_log_buf; 1469 + opts.log_size = BPF_LOG_BUF_SIZE; 1470 + 1471 + ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SYSCTL, NULL, "GPL", prog, insn_cnt, &opts); 1465 1472 if (ret < 0 && test->result != LOAD_REJECT) { 1466 1473 log_err(">>> Loading program error.\n" 1467 1474 ">>> Verifier output:\n%s\n-------\n", bpf_log_buf); ··· 1475 1476 1476 1477 static int load_sysctl_prog_file(struct sysctl_test *test) 1477 1478 { 1478 - struct bpf_prog_load_attr attr; 1479 1479 struct bpf_object *obj; 1480 1480 int prog_fd; 1481 1481 1482 - memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); 1483 - attr.file = test->prog_file; 1484 - attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL; 1485 - 1486 - if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) { 1482 + if (bpf_prog_test_load(test->prog_file, BPF_PROG_TYPE_CGROUP_SYSCTL, &obj, &prog_fd)) { 1487 1483 if (test->result != LOAD_REJECT) 1488 1484 log_err(">>> Loading program (%s) error.\n", 1489 1485 test->prog_file);
+19 -19
tools/testing/selftests/bpf/test_verifier.c
··· 498 498 BPF_EXIT_INSN(), 499 499 }; 500 500 501 - return bpf_load_program(prog_type, prog, 502 - ARRAY_SIZE(prog), "GPL", 0, NULL, 0); 501 + return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 503 502 } 504 503 505 504 static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, ··· 513 514 BPF_EXIT_INSN(), 514 515 }; 515 516 516 - return bpf_load_program(prog_type, prog, 517 - ARRAY_SIZE(prog), "GPL", 0, NULL, 0); 517 + return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL); 518 518 } 519 519 520 520 static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem, ··· 1043 1045 int fd_prog, expected_ret, alignment_prevented_execution; 1044 1046 int prog_len, prog_type = test->prog_type; 1045 1047 struct bpf_insn *prog = test->insns; 1046 - struct bpf_load_program_attr attr; 1048 + LIBBPF_OPTS(bpf_prog_load_opts, opts); 1047 1049 int run_errs, run_successes; 1048 1050 int map_fds[MAX_NR_MAPS]; 1049 1051 const char *expected_err; ··· 1083 1085 test->result_unpriv : test->result; 1084 1086 expected_err = unpriv && test->errstr_unpriv ? 1085 1087 test->errstr_unpriv : test->errstr; 1086 - memset(&attr, 0, sizeof(attr)); 1087 - attr.prog_type = prog_type; 1088 - attr.expected_attach_type = test->expected_attach_type; 1089 - attr.insns = prog; 1090 - attr.insns_cnt = prog_len; 1091 - attr.license = "GPL"; 1088 + 1089 + opts.expected_attach_type = test->expected_attach_type; 1092 1090 if (verbose) 1093 - attr.log_level = 1; 1091 + opts.log_level = 1; 1094 1092 else if (expected_ret == VERBOSE_ACCEPT) 1095 - attr.log_level = 2; 1093 + opts.log_level = 2; 1096 1094 else 1097 - attr.log_level = 4; 1098 - attr.prog_flags = pflags; 1095 + opts.log_level = 4; 1096 + opts.prog_flags = pflags; 1099 1097 1100 1098 if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { 1101 - attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, 1102 - attr.expected_attach_type); 1103 - if (attr.attach_btf_id < 0) { 1099 + int attach_btf_id; 1100 + 1101 + attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, 1102 + opts.expected_attach_type); 1103 + if (attach_btf_id < 0) { 1104 1104 printf("FAIL\nFailed to find BTF ID for '%s'!\n", 1105 1105 test->kfunc); 1106 1106 (*errors)++; 1107 1107 return; 1108 1108 } 1109 + 1110 + opts.attach_btf_id = attach_btf_id; 1109 1111 } 1110 1112 1111 - fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog)); 1113 + opts.log_buf = bpf_vlog; 1114 + opts.log_size = sizeof(bpf_vlog); 1115 + fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts); 1112 1116 saved_errno = errno; 1113 1117 1114 1118 /* BPF_PROG_TYPE_TRACING requires more setup and