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

libbpf: Remove internal use of deprecated bpf_prog_load() variants

Remove all the internal uses of bpf_load_program_xattr(), which is
slated for deprecation in v0.7.
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-5-andrii@kernel.org
parent d10ef2b8
...@@ -401,8 +401,12 @@ int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type, ...@@ -401,8 +401,12 @@ int bpf_prog_load_v0_6_0(enum bpf_prog_type prog_type,
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
} }
__attribute__((alias("bpf_load_program_xattr2")))
int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
char *log_buf, size_t log_buf_sz) char *log_buf, size_t log_buf_sz);
static int bpf_load_program_xattr2(const struct bpf_load_program_attr *load_attr,
char *log_buf, size_t log_buf_sz)
{ {
LIBBPF_OPTS(bpf_prog_load_opts, p); LIBBPF_OPTS(bpf_prog_load_opts, p);
...@@ -456,7 +460,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns, ...@@ -456,7 +460,7 @@ int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
load_attr.license = license; load_attr.license = license;
load_attr.kern_version = kern_version; load_attr.kern_version = kern_version;
return bpf_load_program_xattr(&load_attr, log_buf, log_buf_sz); return bpf_load_program_xattr2(&load_attr, log_buf, log_buf_sz);
} }
int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns, int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
......
...@@ -4282,30 +4282,20 @@ int bpf_map__resize(struct bpf_map *map, __u32 max_entries) ...@@ -4282,30 +4282,20 @@ int bpf_map__resize(struct bpf_map *map, __u32 max_entries)
static int static int
bpf_object__probe_loading(struct bpf_object *obj) bpf_object__probe_loading(struct bpf_object *obj)
{ {
struct bpf_load_program_attr attr;
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int ret; int ret, insn_cnt = ARRAY_SIZE(insns);
if (obj->gen_loader) if (obj->gen_loader)
return 0; return 0;
/* make sure basic loading works */ /* make sure basic loading works */
ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
memset(&attr, 0, sizeof(attr)); if (ret < 0)
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER; ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, NULL);
attr.insns = insns;
attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL";
ret = bpf_load_program_xattr(&attr, NULL, 0);
if (ret < 0) {
attr.prog_type = BPF_PROG_TYPE_TRACEPOINT;
ret = bpf_load_program_xattr(&attr, NULL, 0);
}
if (ret < 0) { if (ret < 0) {
ret = errno; ret = errno;
cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg)); cp = libbpf_strerror_r(ret, errmsg, sizeof(errmsg));
...@@ -4329,28 +4319,19 @@ static int probe_fd(int fd) ...@@ -4329,28 +4319,19 @@ static int probe_fd(int fd)
static int probe_kern_prog_name(void) static int probe_kern_prog_name(void)
{ {
struct bpf_load_program_attr attr;
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int ret; int ret, insn_cnt = ARRAY_SIZE(insns);
/* make sure loading with name works */ /* make sure loading with name works */
ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "test", "GPL", insns, insn_cnt, NULL);
memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr.insns = insns;
attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL";
attr.name = "test";
ret = bpf_load_program_xattr(&attr, NULL, 0);
return probe_fd(ret); return probe_fd(ret);
} }
static int probe_kern_global_data(void) static int probe_kern_global_data(void)
{ {
struct bpf_load_program_attr prg_attr;
struct bpf_create_map_attr map_attr; struct bpf_create_map_attr map_attr;
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
...@@ -4359,7 +4340,7 @@ static int probe_kern_global_data(void) ...@@ -4359,7 +4340,7 @@ static int probe_kern_global_data(void)
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int ret, map; int ret, map, insn_cnt = ARRAY_SIZE(insns);
memset(&map_attr, 0, sizeof(map_attr)); memset(&map_attr, 0, sizeof(map_attr));
map_attr.map_type = BPF_MAP_TYPE_ARRAY; map_attr.map_type = BPF_MAP_TYPE_ARRAY;
...@@ -4378,13 +4359,7 @@ static int probe_kern_global_data(void) ...@@ -4378,13 +4359,7 @@ static int probe_kern_global_data(void)
insns[0].imm = map; insns[0].imm = map;
memset(&prg_attr, 0, sizeof(prg_attr)); ret = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
prg_attr.insns = insns;
prg_attr.insns_cnt = ARRAY_SIZE(insns);
prg_attr.license = "GPL";
ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
close(map); close(map);
return probe_fd(ret); return probe_fd(ret);
} }
...@@ -4500,30 +4475,24 @@ static int probe_kern_array_mmap(void) ...@@ -4500,30 +4475,24 @@ static int probe_kern_array_mmap(void)
static int probe_kern_exp_attach_type(void) static int probe_kern_exp_attach_type(void)
{ {
struct bpf_load_program_attr attr; LIBBPF_OPTS(bpf_prog_load_opts, opts, .expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE);
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int fd, insn_cnt = ARRAY_SIZE(insns);
memset(&attr, 0, sizeof(attr));
/* use any valid combination of program type and (optional) /* use any valid combination of program type and (optional)
* non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS) * non-zero expected attach type (i.e., not a BPF_CGROUP_INET_INGRESS)
* to see if kernel supports expected_attach_type field for * to see if kernel supports expected_attach_type field for
* BPF_PROG_LOAD command * BPF_PROG_LOAD command
*/ */
attr.prog_type = BPF_PROG_TYPE_CGROUP_SOCK; fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK, NULL, "GPL", insns, insn_cnt, &opts);
attr.expected_attach_type = BPF_CGROUP_INET_SOCK_CREATE; return probe_fd(fd);
attr.insns = insns;
attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL";
return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
} }
static int probe_kern_probe_read_kernel(void) static int probe_kern_probe_read_kernel(void)
{ {
struct bpf_load_program_attr attr;
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */ BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), /* r1 = r10 (fp) */
BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */ BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -8), /* r1 += -8 */
...@@ -4532,26 +4501,21 @@ static int probe_kern_probe_read_kernel(void) ...@@ -4532,26 +4501,21 @@ static int probe_kern_probe_read_kernel(void)
BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel), BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_probe_read_kernel),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int fd, insn_cnt = ARRAY_SIZE(insns);
memset(&attr, 0, sizeof(attr)); fd = bpf_prog_load(BPF_PROG_TYPE_KPROBE, NULL, "GPL", insns, insn_cnt, NULL);
attr.prog_type = BPF_PROG_TYPE_KPROBE; return probe_fd(fd);
attr.insns = insns;
attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL";
return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
} }
static int probe_prog_bind_map(void) static int probe_prog_bind_map(void)
{ {
struct bpf_load_program_attr prg_attr;
struct bpf_create_map_attr map_attr; struct bpf_create_map_attr map_attr;
char *cp, errmsg[STRERR_BUFSIZE]; char *cp, errmsg[STRERR_BUFSIZE];
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int ret, map, prog; int ret, map, prog, insn_cnt = ARRAY_SIZE(insns);
memset(&map_attr, 0, sizeof(map_attr)); memset(&map_attr, 0, sizeof(map_attr));
map_attr.map_type = BPF_MAP_TYPE_ARRAY; map_attr.map_type = BPF_MAP_TYPE_ARRAY;
...@@ -4568,13 +4532,7 @@ static int probe_prog_bind_map(void) ...@@ -4568,13 +4532,7 @@ static int probe_prog_bind_map(void)
return ret; return ret;
} }
memset(&prg_attr, 0, sizeof(prg_attr)); prog = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, NULL, "GPL", insns, insn_cnt, NULL);
prg_attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
prg_attr.insns = insns;
prg_attr.insns_cnt = ARRAY_SIZE(insns);
prg_attr.license = "GPL";
prog = bpf_load_program_xattr(&prg_attr, NULL, 0);
if (prog < 0) { if (prog < 0) {
close(map); close(map);
return 0; return 0;
...@@ -4619,19 +4577,14 @@ static int probe_module_btf(void) ...@@ -4619,19 +4577,14 @@ static int probe_module_btf(void)
static int probe_perf_link(void) static int probe_perf_link(void)
{ {
struct bpf_load_program_attr attr;
struct bpf_insn insns[] = { struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int prog_fd, link_fd, err; int prog_fd, link_fd, err;
memset(&attr, 0, sizeof(attr)); prog_fd = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL",
attr.prog_type = BPF_PROG_TYPE_TRACEPOINT; insns, ARRAY_SIZE(insns), NULL);
attr.insns = insns;
attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL";
prog_fd = bpf_load_program_xattr(&attr, NULL, 0);
if (prog_fd < 0) if (prog_fd < 0)
return -errno; return -errno;
...@@ -9168,22 +9121,12 @@ long libbpf_get_error(const void *ptr) ...@@ -9168,22 +9121,12 @@ long libbpf_get_error(const void *ptr)
return -errno; return -errno;
} }
COMPAT_VERSION(bpf_prog_load_deprecated, bpf_prog_load, LIBBPF_0.0.1) __attribute__((alias("bpf_prog_load_xattr2")))
int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd)
{
struct bpf_prog_load_attr attr;
memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
attr.file = file;
attr.prog_type = type;
attr.expected_attach_type = 0;
return bpf_prog_load_xattr(&attr, pobj, prog_fd);
}
int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
struct bpf_object **pobj, int *prog_fd) struct bpf_object **pobj, int *prog_fd);
static int bpf_prog_load_xattr2(const struct bpf_prog_load_attr *attr,
struct bpf_object **pobj, int *prog_fd)
{ {
struct bpf_object_open_attr open_attr = {}; struct bpf_object_open_attr open_attr = {};
struct bpf_program *prog, *first_prog = NULL; struct bpf_program *prog, *first_prog = NULL;
...@@ -9254,6 +9197,20 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, ...@@ -9254,6 +9197,20 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
return 0; return 0;
} }
COMPAT_VERSION(bpf_prog_load_deprecated, bpf_prog_load, LIBBPF_0.0.1)
int bpf_prog_load_deprecated(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd)
{
struct bpf_prog_load_attr attr;
memset(&attr, 0, sizeof(struct bpf_prog_load_attr));
attr.file = file;
attr.prog_type = type;
attr.expected_attach_type = 0;
return bpf_prog_load_xattr2(&attr, pobj, prog_fd);
}
struct bpf_link { struct bpf_link {
int (*detach)(struct bpf_link *link); int (*detach)(struct bpf_link *link);
void (*dealloc)(struct bpf_link *link); void (*dealloc)(struct bpf_link *link);
......
...@@ -68,21 +68,21 @@ static void ...@@ -68,21 +68,21 @@ static void
probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex) size_t insns_cnt, char *buf, size_t buf_len, __u32 ifindex)
{ {
struct bpf_load_program_attr xattr = {}; LIBBPF_OPTS(bpf_prog_load_opts, opts);
int fd; int fd;
switch (prog_type) { switch (prog_type) {
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
xattr.expected_attach_type = BPF_CGROUP_INET4_CONNECT; opts.expected_attach_type = BPF_CGROUP_INET4_CONNECT;
break; break;
case BPF_PROG_TYPE_CGROUP_SOCKOPT: case BPF_PROG_TYPE_CGROUP_SOCKOPT:
xattr.expected_attach_type = BPF_CGROUP_GETSOCKOPT; opts.expected_attach_type = BPF_CGROUP_GETSOCKOPT;
break; break;
case BPF_PROG_TYPE_SK_LOOKUP: case BPF_PROG_TYPE_SK_LOOKUP:
xattr.expected_attach_type = BPF_SK_LOOKUP; opts.expected_attach_type = BPF_SK_LOOKUP;
break; break;
case BPF_PROG_TYPE_KPROBE: case BPF_PROG_TYPE_KPROBE:
xattr.kern_version = get_kernel_version(); opts.kern_version = get_kernel_version();
break; break;
case BPF_PROG_TYPE_UNSPEC: case BPF_PROG_TYPE_UNSPEC:
case BPF_PROG_TYPE_SOCKET_FILTER: case BPF_PROG_TYPE_SOCKET_FILTER:
...@@ -115,13 +115,11 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns, ...@@ -115,13 +115,11 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
break; break;
} }
xattr.prog_type = prog_type; opts.prog_ifindex = ifindex;
xattr.insns = insns; opts.log_buf = buf;
xattr.insns_cnt = insns_cnt; opts.log_size = buf_len;
xattr.license = "GPL";
xattr.prog_ifindex = ifindex;
fd = bpf_load_program_xattr(&xattr, buf, buf_len); fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, NULL);
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
} }
......
...@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area, ...@@ -364,7 +364,6 @@ int xsk_umem__create_v0_0_2(struct xsk_umem **umem_ptr, void *umem_area,
static enum xsk_prog get_xsk_prog(void) static enum xsk_prog get_xsk_prog(void)
{ {
enum xsk_prog detected = XSK_PROG_FALLBACK; enum xsk_prog detected = XSK_PROG_FALLBACK;
struct bpf_load_program_attr prog_attr;
struct bpf_create_map_attr map_attr; struct bpf_create_map_attr map_attr;
__u32 size_out, retval, duration; __u32 size_out, retval, duration;
char data_in = 0, data_out; char data_in = 0, data_out;
...@@ -375,7 +374,7 @@ static enum xsk_prog get_xsk_prog(void) ...@@ -375,7 +374,7 @@ static enum xsk_prog get_xsk_prog(void)
BPF_EMIT_CALL(BPF_FUNC_redirect_map), BPF_EMIT_CALL(BPF_FUNC_redirect_map),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int prog_fd, map_fd, ret; int prog_fd, map_fd, ret, insn_cnt = ARRAY_SIZE(insns);
memset(&map_attr, 0, sizeof(map_attr)); memset(&map_attr, 0, sizeof(map_attr));
map_attr.map_type = BPF_MAP_TYPE_XSKMAP; map_attr.map_type = BPF_MAP_TYPE_XSKMAP;
...@@ -389,13 +388,7 @@ static enum xsk_prog get_xsk_prog(void) ...@@ -389,13 +388,7 @@ static enum xsk_prog get_xsk_prog(void)
insns[0].imm = map_fd; insns[0].imm = map_fd;
memset(&prog_attr, 0, sizeof(prog_attr)); prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL);
prog_attr.prog_type = BPF_PROG_TYPE_XDP;
prog_attr.insns = insns;
prog_attr.insns_cnt = ARRAY_SIZE(insns);
prog_attr.license = "GPL";
prog_fd = bpf_load_program_xattr(&prog_attr, NULL, 0);
if (prog_fd < 0) { if (prog_fd < 0) {
close(map_fd); close(map_fd);
return detected; return detected;
...@@ -495,10 +488,13 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk) ...@@ -495,10 +488,13 @@ static int xsk_load_xdp_prog(struct xsk_socket *xsk)
}; };
struct bpf_insn *progs[] = {prog, prog_redirect_flags}; struct bpf_insn *progs[] = {prog, prog_redirect_flags};
enum xsk_prog option = get_xsk_prog(); enum xsk_prog option = get_xsk_prog();
LIBBPF_OPTS(bpf_prog_load_opts, opts,
.log_buf = log_buf,
.log_size = log_buf_size,
);
prog_fd = bpf_load_program(BPF_PROG_TYPE_XDP, progs[option], insns_cnt[option], prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "LGPL-2.1 or BSD-2-Clause",
"LGPL-2.1 or BSD-2-Clause", 0, log_buf, progs[option], insns_cnt[option], &opts);
log_buf_size);
if (prog_fd < 0) { if (prog_fd < 0) {
pr_warn("BPF log buffer:\n%s", log_buf); pr_warn("BPF log buffer:\n%s", log_buf);
return prog_fd; return prog_fd;
...@@ -725,14 +721,12 @@ static int xsk_link_lookup(int ifindex, __u32 *prog_id, int *link_fd) ...@@ -725,14 +721,12 @@ static int xsk_link_lookup(int ifindex, __u32 *prog_id, int *link_fd)
static bool xsk_probe_bpf_link(void) static bool xsk_probe_bpf_link(void)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = XDP_FLAGS_SKB_MODE);
.flags = XDP_FLAGS_SKB_MODE);
struct bpf_load_program_attr prog_attr;
struct bpf_insn insns[2] = { struct bpf_insn insns[2] = {
BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), BPF_MOV64_IMM(BPF_REG_0, XDP_PASS),
BPF_EXIT_INSN() BPF_EXIT_INSN()
}; };
int prog_fd, link_fd = -1; int prog_fd, link_fd = -1, insn_cnt = ARRAY_SIZE(insns);
int ifindex_lo = 1; int ifindex_lo = 1;
bool ret = false; bool ret = false;
int err; int err;
...@@ -744,13 +738,7 @@ static bool xsk_probe_bpf_link(void) ...@@ -744,13 +738,7 @@ static bool xsk_probe_bpf_link(void)
if (link_fd >= 0) if (link_fd >= 0)
return true; return true;
memset(&prog_attr, 0, sizeof(prog_attr)); prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL);
prog_attr.prog_type = BPF_PROG_TYPE_XDP;
prog_attr.insns = insns;
prog_attr.insns_cnt = ARRAY_SIZE(insns);
prog_attr.license = "GPL";
prog_fd = bpf_load_program_xattr(&prog_attr, NULL, 0);
if (prog_fd < 0) if (prog_fd < 0)
return ret; return ret;
......
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