Commit d8e86407 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

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: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211103220845.2676888-10-andrii@kernel.org
parent 3d1d6239
...@@ -594,6 +594,12 @@ static int do_test_single(struct bpf_align_test *test) ...@@ -594,6 +594,12 @@ static int do_test_single(struct bpf_align_test *test)
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
int prog_type = test->prog_type; int prog_type = test->prog_type;
char bpf_vlog_copy[32768]; char bpf_vlog_copy[32768];
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_flags = BPF_F_STRICT_ALIGNMENT,
.log_buf = bpf_vlog,
.log_size = sizeof(bpf_vlog),
.log_level = 2,
);
const char *line_ptr; const char *line_ptr;
int cur_line = -1; int cur_line = -1;
int prog_len, i; int prog_len, i;
...@@ -601,9 +607,8 @@ static int do_test_single(struct bpf_align_test *test) ...@@ -601,9 +607,8 @@ static int do_test_single(struct bpf_align_test *test)
int ret; int ret;
prog_len = probe_filter_length(prog); prog_len = probe_filter_length(prog);
fd_prog = bpf_verify_program(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, fd_prog = bpf_prog_load(prog_type ? : BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
prog, prog_len, BPF_F_STRICT_ALIGNMENT, prog, prog_len, &opts);
"GPL", 0, bpf_vlog, sizeof(bpf_vlog), 2);
if (fd_prog < 0 && test->result != REJECT) { if (fd_prog < 0 && test->result != REJECT) {
printf("Failed to load program.\n"); printf("Failed to load program.\n");
printf("%s", bpf_vlog); printf("%s", bpf_vlog);
......
...@@ -20,34 +20,33 @@ void test_fexit_stress(void) ...@@ -20,34 +20,33 @@ void test_fexit_stress(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
.prog_type = BPF_PROG_TYPE_TRACING,
.license = "GPL",
.insns = trace_program,
.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
.expected_attach_type = BPF_TRACE_FEXIT, .expected_attach_type = BPF_TRACE_FEXIT,
}; .log_buf = error,
.log_size = sizeof(error),
);
const struct bpf_insn skb_program[] = { const struct bpf_insn skb_program[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr skb_load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .log_buf = error,
.license = "GPL", .log_size = sizeof(error),
.insns = skb_program, );
.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
};
err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1", err = libbpf_find_vmlinux_btf_id("bpf_fentry_test1",
load_attr.expected_attach_type); trace_opts.expected_attach_type);
if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err)) if (CHECK(err <= 0, "find_vmlinux_btf_id", "failed: %d\n", err))
goto out; goto out;
load_attr.attach_btf_id = err; trace_opts.attach_btf_id = err;
for (i = 0; i < CNT; i++) { for (i = 0; i < CNT; i++) {
fexit_fd[i] = bpf_load_program_xattr(&load_attr, error, sizeof(error)); fexit_fd[i] = bpf_prog_load(BPF_PROG_TYPE_TRACING, NULL, "GPL",
trace_program,
sizeof(trace_program) / sizeof(struct bpf_insn),
&trace_opts);
if (CHECK(fexit_fd[i] < 0, "fexit loaded", if (CHECK(fexit_fd[i] < 0, "fexit loaded",
"failed: %d errno %d\n", fexit_fd[i], errno)) "failed: %d errno %d\n", fexit_fd[i], errno))
goto out; goto out;
...@@ -57,7 +56,9 @@ void test_fexit_stress(void) ...@@ -57,7 +56,9 @@ void test_fexit_stress(void)
goto out; goto out;
} }
filter_fd = bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL",
skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
&skb_opts);
if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
filter_fd, errno)) filter_fd, errno))
goto out; goto out;
......
...@@ -18,15 +18,15 @@ void test_raw_tp_writable_reject_nbd_invalid(void) ...@@ -18,15 +18,15 @@ void test_raw_tp_writable_reject_nbd_invalid(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
.license = "GPL v2",
.insns = program,
.insns_cnt = sizeof(program) / sizeof(struct bpf_insn),
.log_level = 2, .log_level = 2,
}; .log_buf = error,
.log_size = sizeof(error),
);
bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
program, sizeof(program) / sizeof(struct bpf_insn),
&opts);
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load", if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable load",
"failed: %d errno %d\n", bpf_fd, errno)) "failed: %d errno %d\n", bpf_fd, errno))
return; return;
......
...@@ -17,15 +17,15 @@ void serial_test_raw_tp_writable_test_run(void) ...@@ -17,15 +17,15 @@ void serial_test_raw_tp_writable_test_run(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, trace_opts,
.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
.license = "GPL v2",
.insns = trace_program,
.insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn),
.log_level = 2, .log_level = 2,
}; .log_buf = error,
.log_size = sizeof(error),
);
int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); int bpf_fd = bpf_prog_load(BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, NULL, "GPL v2",
trace_program, sizeof(trace_program) / sizeof(struct bpf_insn),
&trace_opts);
if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded", if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded",
"failed: %d errno %d\n", bpf_fd, errno)) "failed: %d errno %d\n", bpf_fd, errno))
return; return;
...@@ -35,15 +35,14 @@ void serial_test_raw_tp_writable_test_run(void) ...@@ -35,15 +35,14 @@ void serial_test_raw_tp_writable_test_run(void)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
struct bpf_load_program_attr skb_load_attr = { LIBBPF_OPTS(bpf_prog_load_opts, skb_opts,
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER, .log_buf = error,
.license = "GPL v2", .log_size = sizeof(error),
.insns = skb_program, );
.insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn),
};
int filter_fd = int filter_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL v2",
bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); skb_program, sizeof(skb_program) / sizeof(struct bpf_insn),
&skb_opts);
if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n",
filter_fd, errno)) filter_fd, errno))
goto out_bpffd; goto out_bpffd;
......
...@@ -852,22 +852,21 @@ static struct sockopt_test { ...@@ -852,22 +852,21 @@ static struct sockopt_test {
static int load_prog(const struct bpf_insn *insns, static int load_prog(const struct bpf_insn *insns,
enum bpf_attach_type expected_attach_type) enum bpf_attach_type expected_attach_type)
{ {
struct bpf_load_program_attr attr = { LIBBPF_OPTS(bpf_prog_load_opts, opts,
.prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT,
.expected_attach_type = expected_attach_type, .expected_attach_type = expected_attach_type,
.insns = insns,
.license = "GPL",
.log_level = 2, .log_level = 2,
}; .log_buf = bpf_log_buf,
int fd; .log_size = sizeof(bpf_log_buf),
);
int fd, insns_cnt = 0;
for (; for (;
insns[attr.insns_cnt].code != (BPF_JMP | BPF_EXIT); insns[insns_cnt].code != (BPF_JMP | BPF_EXIT);
attr.insns_cnt++) { insns_cnt++) {
} }
attr.insns_cnt++; insns_cnt++;
fd = bpf_load_program_xattr(&attr, bpf_log_buf, sizeof(bpf_log_buf)); fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCKOPT, NULL, "GPL", insns, insns_cnt, &opts);
if (verbose && fd < 0) if (verbose && fd < 0)
fprintf(stderr, "%s\n", bpf_log_buf); fprintf(stderr, "%s\n", bpf_log_buf);
......
...@@ -42,7 +42,6 @@ static int create_map(int map_type, int map_flags, unsigned int size) ...@@ -42,7 +42,6 @@ static int create_map(int map_type, int map_flags, unsigned int size)
static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key, static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key,
void *value) void *value)
{ {
struct bpf_load_program_attr prog;
struct bpf_create_map_attr map; struct bpf_create_map_attr map;
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_LD_MAP_VALUE(BPF_REG_9, 0, 0), BPF_LD_MAP_VALUE(BPF_REG_9, 0, 0),
...@@ -76,13 +75,7 @@ static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key, ...@@ -76,13 +75,7 @@ static int bpf_map_lookup_elem_with_ref_bit(int fd, unsigned long long key,
insns[0].imm = mfd; insns[0].imm = mfd;
memset(&prog, 0, sizeof(prog)); pfd = bpf_prog_load(BPF_PROG_TYPE_SCHED_CLS, NULL, "GPL", insns, ARRAY_SIZE(insns), NULL);
prog.prog_type = BPF_PROG_TYPE_SCHED_CLS;
prog.insns = insns;
prog.insns_cnt = ARRAY_SIZE(insns);
prog.license = "GPL";
pfd = bpf_load_program_xattr(&prog, NULL, 0);
if (pfd < 0) { if (pfd < 0) {
close(mfd); close(mfd);
return -1; return -1;
......
...@@ -328,18 +328,17 @@ static size_t probe_prog_length(const struct bpf_insn *fp) ...@@ -328,18 +328,17 @@ static size_t probe_prog_length(const struct bpf_insn *fp)
static int load_sock_prog(const struct bpf_insn *prog, static int load_sock_prog(const struct bpf_insn *prog,
enum bpf_attach_type attach_type) enum bpf_attach_type attach_type)
{ {
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret, insn_cnt;
memset(&attr, 0, sizeof(struct bpf_load_program_attr)); insn_cnt = probe_prog_length(prog);
attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
attr.expected_attach_type = attach_type; opts.expected_attach_type = attach_type;
attr.insns = prog; opts.log_buf = bpf_log_buf;
attr.insns_cnt = probe_prog_length(attr.insns); opts.log_size = BPF_LOG_BUF_SIZE;
attr.license = "GPL"; opts.log_level = 2;
attr.log_level = 2;
ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", prog, insn_cnt, &opts);
ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE);
if (verbose && ret < 0) if (verbose && ret < 0)
fprintf(stderr, "%s\n", bpf_log_buf); fprintf(stderr, "%s\n", bpf_log_buf);
......
...@@ -645,17 +645,14 @@ static int mk_sockaddr(int domain, const char *ip, unsigned short port, ...@@ -645,17 +645,14 @@ static int mk_sockaddr(int domain, const char *ip, unsigned short port,
static int load_insns(const struct sock_addr_test *test, static int load_insns(const struct sock_addr_test *test,
const struct bpf_insn *insns, size_t insns_cnt) const struct bpf_insn *insns, size_t insns_cnt)
{ {
struct bpf_load_program_attr load_attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret;
memset(&load_attr, 0, sizeof(struct bpf_load_program_attr)); opts.expected_attach_type = test->expected_attach_type;
load_attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR; opts.log_buf = bpf_log_buf;
load_attr.expected_attach_type = test->expected_attach_type; opts.log_size = BPF_LOG_BUF_SIZE;
load_attr.insns = insns;
load_attr.insns_cnt = insns_cnt;
load_attr.license = "GPL";
ret = bpf_load_program_xattr(&load_attr, bpf_log_buf, BPF_LOG_BUF_SIZE); ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, NULL, "GPL", insns, insns_cnt, &opts);
if (ret < 0 && test->expected_result != LOAD_REJECT) { if (ret < 0 && test->expected_result != LOAD_REJECT) {
log_err(">>> Loading program error.\n" log_err(">>> Loading program error.\n"
">>> Verifier output:\n%s\n-------\n", bpf_log_buf); ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
......
...@@ -1435,14 +1435,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1435,14 +1435,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
const char *sysctl_path) const char *sysctl_path)
{ {
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int ret; int ret, insn_cnt;
memset(&attr, 0, sizeof(struct bpf_load_program_attr)); insn_cnt = probe_prog_length(prog);
attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL;
attr.insns = prog;
attr.insns_cnt = probe_prog_length(attr.insns);
attr.license = "GPL";
if (test->fixup_value_insn) { if (test->fixup_value_insn) {
char buf[128]; char buf[128];
...@@ -1465,7 +1461,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1465,7 +1461,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
return -1; return -1;
} }
ret = bpf_load_program_xattr(&attr, bpf_log_buf, BPF_LOG_BUF_SIZE); opts.log_buf = bpf_log_buf;
opts.log_size = BPF_LOG_BUF_SIZE;
ret = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SYSCTL, NULL, "GPL", prog, insn_cnt, &opts);
if (ret < 0 && test->result != LOAD_REJECT) { if (ret < 0 && test->result != LOAD_REJECT) {
log_err(">>> Loading program error.\n" log_err(">>> Loading program error.\n"
">>> Verifier output:\n%s\n-------\n", bpf_log_buf); ">>> Verifier output:\n%s\n-------\n", bpf_log_buf);
...@@ -1476,15 +1475,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test, ...@@ -1476,15 +1475,10 @@ static int load_sysctl_prog_insns(struct sysctl_test *test,
static int load_sysctl_prog_file(struct sysctl_test *test) static int load_sysctl_prog_file(struct sysctl_test *test)
{ {
struct bpf_prog_load_attr attr;
struct bpf_object *obj; struct bpf_object *obj;
int prog_fd; int prog_fd;
memset(&attr, 0, sizeof(struct bpf_prog_load_attr)); if (bpf_prog_test_load(test->prog_file, BPF_PROG_TYPE_CGROUP_SYSCTL, &obj, &prog_fd)) {
attr.file = test->prog_file;
attr.prog_type = BPF_PROG_TYPE_CGROUP_SYSCTL;
if (bpf_prog_load_xattr(&attr, &obj, &prog_fd)) {
if (test->result != LOAD_REJECT) if (test->result != LOAD_REJECT)
log_err(">>> Loading program (%s) error.\n", log_err(">>> Loading program (%s) error.\n",
test->prog_file); test->prog_file);
......
...@@ -498,8 +498,7 @@ static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret) ...@@ -498,8 +498,7 @@ static int create_prog_dummy_simple(enum bpf_prog_type prog_type, int ret)
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(prog_type, prog, return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
...@@ -514,8 +513,7 @@ static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd, ...@@ -514,8 +513,7 @@ static int create_prog_dummy_loop(enum bpf_prog_type prog_type, int mfd,
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
return bpf_load_program(prog_type, prog, return bpf_prog_load(prog_type, NULL, "GPL", prog, ARRAY_SIZE(prog), NULL);
ARRAY_SIZE(prog), "GPL", 0, NULL, 0);
} }
static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem, static int create_prog_array(enum bpf_prog_type prog_type, uint32_t max_elem,
...@@ -1045,7 +1043,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv, ...@@ -1045,7 +1043,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
int fd_prog, expected_ret, alignment_prevented_execution; int fd_prog, expected_ret, alignment_prevented_execution;
int prog_len, prog_type = test->prog_type; int prog_len, prog_type = test->prog_type;
struct bpf_insn *prog = test->insns; struct bpf_insn *prog = test->insns;
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int run_errs, run_successes; int run_errs, run_successes;
int map_fds[MAX_NR_MAPS]; int map_fds[MAX_NR_MAPS];
const char *expected_err; const char *expected_err;
...@@ -1085,32 +1083,34 @@ static void do_test_single(struct bpf_test *test, bool unpriv, ...@@ -1085,32 +1083,34 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
test->result_unpriv : test->result; test->result_unpriv : test->result;
expected_err = unpriv && test->errstr_unpriv ? expected_err = unpriv && test->errstr_unpriv ?
test->errstr_unpriv : test->errstr; test->errstr_unpriv : test->errstr;
memset(&attr, 0, sizeof(attr));
attr.prog_type = prog_type; opts.expected_attach_type = test->expected_attach_type;
attr.expected_attach_type = test->expected_attach_type;
attr.insns = prog;
attr.insns_cnt = prog_len;
attr.license = "GPL";
if (verbose) if (verbose)
attr.log_level = 1; opts.log_level = 1;
else if (expected_ret == VERBOSE_ACCEPT) else if (expected_ret == VERBOSE_ACCEPT)
attr.log_level = 2; opts.log_level = 2;
else else
attr.log_level = 4; opts.log_level = 4;
attr.prog_flags = pflags; opts.prog_flags = pflags;
if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) { if (prog_type == BPF_PROG_TYPE_TRACING && test->kfunc) {
attr.attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc, int attach_btf_id;
attr.expected_attach_type);
if (attr.attach_btf_id < 0) { attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
opts.expected_attach_type);
if (attach_btf_id < 0) {
printf("FAIL\nFailed to find BTF ID for '%s'!\n", printf("FAIL\nFailed to find BTF ID for '%s'!\n",
test->kfunc); test->kfunc);
(*errors)++; (*errors)++;
return; return;
} }
opts.attach_btf_id = attach_btf_id;
} }
fd_prog = bpf_load_program_xattr(&attr, bpf_vlog, sizeof(bpf_vlog)); opts.log_buf = bpf_vlog;
opts.log_size = sizeof(bpf_vlog);
fd_prog = bpf_prog_load(prog_type, NULL, "GPL", prog, prog_len, &opts);
saved_errno = errno; saved_errno = errno;
/* BPF_PROG_TYPE_TRACING requires more setup and /* BPF_PROG_TYPE_TRACING requires more setup and
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment