Commit f706f6c6 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'Improve set_attach_target() and deprecate open_opts.attach_prog_fd'

Andrii Nakryiko says:

====================

This patch set deprecates bpf_object_open_opts.attach_prog_fd (in libbpf 0.7+)
by extending bpf_program__set_attach_target() to support some more flexible
scenarios. Existing fexit_bpf2bpf selftest is updated accordingly to not use
deprecated APIs.

While at it, also deprecate no-op relaxed_core_relocs option (they are always
"relaxed").

Last patch also const-ifies all high-level libbpf attach APIs, as there is no
reason for them to assume bpf_program/bpf_map modifications.

Patch #1 also removes one more unneeded use of find_sec_def(), relying on
prog->sec_def that's set during bpf_object__open() operation, simplifying
upcoming refactoring a little bit more.

All these changes are preparatory patches before SEC() handling refactoring
that will come next.
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 33656275 942025c9
...@@ -220,7 +220,7 @@ struct reloc_desc { ...@@ -220,7 +220,7 @@ struct reloc_desc {
struct bpf_sec_def; struct bpf_sec_def;
typedef struct bpf_link *(*attach_fn_t)(struct bpf_program *prog); typedef struct bpf_link *(*attach_fn_t)(const struct bpf_program *prog);
struct bpf_sec_def { struct bpf_sec_def {
const char *sec; const char *sec;
...@@ -6415,9 +6415,12 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object ...@@ -6415,9 +6415,12 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object
bpf_program__set_type(prog, prog->sec_def->prog_type); bpf_program__set_type(prog, prog->sec_def->prog_type);
bpf_program__set_expected_attach_type(prog, prog->sec_def->expected_attach_type); bpf_program__set_expected_attach_type(prog, prog->sec_def->expected_attach_type);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING || if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING ||
prog->sec_def->prog_type == BPF_PROG_TYPE_EXT) prog->sec_def->prog_type == BPF_PROG_TYPE_EXT)
prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0); prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
#pragma GCC diagnostic pop
} }
return 0; return 0;
...@@ -7944,12 +7947,12 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog, ...@@ -7944,12 +7947,12 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,
__VA_ARGS__ \ __VA_ARGS__ \
} }
static struct bpf_link *attach_kprobe(struct bpf_program *prog); static struct bpf_link *attach_kprobe(const struct bpf_program *prog);
static struct bpf_link *attach_tp(struct bpf_program *prog); static struct bpf_link *attach_tp(const struct bpf_program *prog);
static struct bpf_link *attach_raw_tp(struct bpf_program *prog); static struct bpf_link *attach_raw_tp(const struct bpf_program *prog);
static struct bpf_link *attach_trace(struct bpf_program *prog); static struct bpf_link *attach_trace(const struct bpf_program *prog);
static struct bpf_link *attach_lsm(struct bpf_program *prog); static struct bpf_link *attach_lsm(const struct bpf_program *prog);
static struct bpf_link *attach_iter(struct bpf_program *prog); static struct bpf_link *attach_iter(const struct bpf_program *prog);
static const struct bpf_sec_def section_defs[] = { static const struct bpf_sec_def section_defs[] = {
BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
...@@ -8461,19 +8464,15 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd, ...@@ -8461,19 +8464,15 @@ static int libbpf_find_attach_btf_id(struct bpf_program *prog, int *btf_obj_fd,
{ {
enum bpf_attach_type attach_type = prog->expected_attach_type; enum bpf_attach_type attach_type = prog->expected_attach_type;
__u32 attach_prog_fd = prog->attach_prog_fd; __u32 attach_prog_fd = prog->attach_prog_fd;
const char *name = prog->sec_name, *attach_name; const char *attach_name;
const struct bpf_sec_def *sec = NULL;
int err = 0; int err = 0;
if (!name) if (!prog->sec_def || !prog->sec_def->is_attach_btf) {
return -EINVAL; pr_warn("failed to identify BTF ID based on ELF section name '%s'\n",
prog->sec_name);
sec = find_sec_def(name);
if (!sec || !sec->is_attach_btf) {
pr_warn("failed to identify BTF ID based on ELF section name '%s'\n", name);
return -ESRCH; return -ESRCH;
} }
attach_name = name + sec->len; attach_name = prog->sec_name + prog->sec_def->len;
/* BPF program's BTF ID */ /* BPF program's BTF ID */
if (attach_prog_fd) { if (attach_prog_fd) {
...@@ -9093,7 +9092,7 @@ static void bpf_link_perf_dealloc(struct bpf_link *link) ...@@ -9093,7 +9092,7 @@ static void bpf_link_perf_dealloc(struct bpf_link *link)
free(perf_link); free(perf_link);
} }
struct bpf_link *bpf_program__attach_perf_event_opts(struct bpf_program *prog, int pfd, struct bpf_link *bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
const struct bpf_perf_event_opts *opts) const struct bpf_perf_event_opts *opts)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
...@@ -9168,7 +9167,7 @@ struct bpf_link *bpf_program__attach_perf_event_opts(struct bpf_program *prog, i ...@@ -9168,7 +9167,7 @@ struct bpf_link *bpf_program__attach_perf_event_opts(struct bpf_program *prog, i
return libbpf_err_ptr(err); return libbpf_err_ptr(err);
} }
struct bpf_link *bpf_program__attach_perf_event(struct bpf_program *prog, int pfd) struct bpf_link *bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd)
{ {
return bpf_program__attach_perf_event_opts(prog, pfd, NULL); return bpf_program__attach_perf_event_opts(prog, pfd, NULL);
} }
...@@ -9333,7 +9332,7 @@ static int perf_event_kprobe_open_legacy(bool retprobe, const char *name, uint64 ...@@ -9333,7 +9332,7 @@ static int perf_event_kprobe_open_legacy(bool retprobe, const char *name, uint64
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_kprobe_opts(struct bpf_program *prog, bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
const char *func_name, const char *func_name,
const struct bpf_kprobe_opts *opts) const struct bpf_kprobe_opts *opts)
{ {
...@@ -9390,7 +9389,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog, ...@@ -9390,7 +9389,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,
return link; return link;
} }
struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, struct bpf_link *bpf_program__attach_kprobe(const struct bpf_program *prog,
bool retprobe, bool retprobe,
const char *func_name) const char *func_name)
{ {
...@@ -9401,7 +9400,7 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, ...@@ -9401,7 +9400,7 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog,
return bpf_program__attach_kprobe_opts(prog, func_name, &opts); return bpf_program__attach_kprobe_opts(prog, func_name, &opts);
} }
static struct bpf_link *attach_kprobe(struct bpf_program *prog) static struct bpf_link *attach_kprobe(const struct bpf_program *prog)
{ {
DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts);
unsigned long offset = 0; unsigned long offset = 0;
...@@ -9433,7 +9432,7 @@ static struct bpf_link *attach_kprobe(struct bpf_program *prog) ...@@ -9433,7 +9432,7 @@ static struct bpf_link *attach_kprobe(struct bpf_program *prog)
} }
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid, bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
const char *binary_path, size_t func_offset, const char *binary_path, size_t func_offset,
const struct bpf_uprobe_opts *opts) const struct bpf_uprobe_opts *opts)
{ {
...@@ -9473,7 +9472,7 @@ bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid, ...@@ -9473,7 +9472,7 @@ bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid,
return link; return link;
} }
struct bpf_link *bpf_program__attach_uprobe(struct bpf_program *prog, struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,
bool retprobe, pid_t pid, bool retprobe, pid_t pid,
const char *binary_path, const char *binary_path,
size_t func_offset) size_t func_offset)
...@@ -9533,7 +9532,7 @@ static int perf_event_open_tracepoint(const char *tp_category, ...@@ -9533,7 +9532,7 @@ static int perf_event_open_tracepoint(const char *tp_category,
return pfd; return pfd;
} }
struct bpf_link *bpf_program__attach_tracepoint_opts(struct bpf_program *prog, struct bpf_link *bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name, const char *tp_name,
const struct bpf_tracepoint_opts *opts) const struct bpf_tracepoint_opts *opts)
...@@ -9567,14 +9566,14 @@ struct bpf_link *bpf_program__attach_tracepoint_opts(struct bpf_program *prog, ...@@ -9567,14 +9566,14 @@ struct bpf_link *bpf_program__attach_tracepoint_opts(struct bpf_program *prog,
return link; return link;
} }
struct bpf_link *bpf_program__attach_tracepoint(struct bpf_program *prog, struct bpf_link *bpf_program__attach_tracepoint(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name) const char *tp_name)
{ {
return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL); return bpf_program__attach_tracepoint_opts(prog, tp_category, tp_name, NULL);
} }
static struct bpf_link *attach_tp(struct bpf_program *prog) static struct bpf_link *attach_tp(const struct bpf_program *prog)
{ {
char *sec_name, *tp_cat, *tp_name; char *sec_name, *tp_cat, *tp_name;
struct bpf_link *link; struct bpf_link *link;
...@@ -9598,7 +9597,7 @@ static struct bpf_link *attach_tp(struct bpf_program *prog) ...@@ -9598,7 +9597,7 @@ static struct bpf_link *attach_tp(struct bpf_program *prog)
return link; return link;
} }
struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, struct bpf_link *bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
const char *tp_name) const char *tp_name)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
...@@ -9628,7 +9627,7 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog, ...@@ -9628,7 +9627,7 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_raw_tp(struct bpf_program *prog) static struct bpf_link *attach_raw_tp(const struct bpf_program *prog)
{ {
const char *tp_name = prog->sec_name + prog->sec_def->len; const char *tp_name = prog->sec_name + prog->sec_def->len;
...@@ -9636,7 +9635,7 @@ static struct bpf_link *attach_raw_tp(struct bpf_program *prog) ...@@ -9636,7 +9635,7 @@ static struct bpf_link *attach_raw_tp(struct bpf_program *prog)
} }
/* Common logic for all BPF program types that attach to a btf_id */ /* Common logic for all BPF program types that attach to a btf_id */
static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog) static struct bpf_link *bpf_program__attach_btf_id(const struct bpf_program *prog)
{ {
char errmsg[STRERR_BUFSIZE]; char errmsg[STRERR_BUFSIZE];
struct bpf_link *link; struct bpf_link *link;
...@@ -9665,28 +9664,28 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog) ...@@ -9665,28 +9664,28 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
return (struct bpf_link *)link; return (struct bpf_link *)link;
} }
struct bpf_link *bpf_program__attach_trace(struct bpf_program *prog) struct bpf_link *bpf_program__attach_trace(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
struct bpf_link *bpf_program__attach_lsm(struct bpf_program *prog) struct bpf_link *bpf_program__attach_lsm(const struct bpf_program *prog)
{ {
return bpf_program__attach_btf_id(prog); return bpf_program__attach_btf_id(prog);
} }
static struct bpf_link *attach_trace(struct bpf_program *prog) static struct bpf_link *attach_trace(const struct bpf_program *prog)
{ {
return bpf_program__attach_trace(prog); return bpf_program__attach_trace(prog);
} }
static struct bpf_link *attach_lsm(struct bpf_program *prog) static struct bpf_link *attach_lsm(const struct bpf_program *prog)
{ {
return bpf_program__attach_lsm(prog); return bpf_program__attach_lsm(prog);
} }
static struct bpf_link * static struct bpf_link *
bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id, bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id,
const char *target_name) const char *target_name)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
...@@ -9722,24 +9721,24 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id, ...@@ -9722,24 +9721,24 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd) bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
{ {
return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup"); return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd) bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
{ {
return bpf_program__attach_fd(prog, netns_fd, 0, "netns"); return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
} }
struct bpf_link *bpf_program__attach_xdp(struct bpf_program *prog, int ifindex) struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
{ {
/* target_fd/target_ifindex use the same field in LINK_CREATE */ /* target_fd/target_ifindex use the same field in LINK_CREATE */
return bpf_program__attach_fd(prog, ifindex, 0, "xdp"); return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
} }
struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog, struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, int target_fd,
const char *attach_func_name) const char *attach_func_name)
{ {
...@@ -9772,7 +9771,7 @@ struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog, ...@@ -9772,7 +9771,7 @@ struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
} }
struct bpf_link * struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog, bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts) const struct bpf_iter_attach_opts *opts)
{ {
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
...@@ -9811,12 +9810,12 @@ bpf_program__attach_iter(struct bpf_program *prog, ...@@ -9811,12 +9810,12 @@ bpf_program__attach_iter(struct bpf_program *prog,
return link; return link;
} }
static struct bpf_link *attach_iter(struct bpf_program *prog) static struct bpf_link *attach_iter(const struct bpf_program *prog)
{ {
return bpf_program__attach_iter(prog, NULL); return bpf_program__attach_iter(prog, NULL);
} }
struct bpf_link *bpf_program__attach(struct bpf_program *prog) struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
{ {
if (!prog->sec_def || !prog->sec_def->attach_fn) if (!prog->sec_def || !prog->sec_def->attach_fn)
return libbpf_err_ptr(-ESRCH); return libbpf_err_ptr(-ESRCH);
...@@ -9834,7 +9833,7 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link) ...@@ -9834,7 +9833,7 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link)
return 0; return 0;
} }
struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map) struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
{ {
struct bpf_struct_ops *st_ops; struct bpf_struct_ops *st_ops;
struct bpf_link *link; struct bpf_link *link;
...@@ -10647,18 +10646,29 @@ int bpf_program__set_attach_target(struct bpf_program *prog, ...@@ -10647,18 +10646,29 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
{ {
int btf_obj_fd = 0, btf_id = 0, err; int btf_obj_fd = 0, btf_id = 0, err;
if (!prog || attach_prog_fd < 0 || !attach_func_name) if (!prog || attach_prog_fd < 0)
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
if (prog->obj->loaded) if (prog->obj->loaded)
return libbpf_err(-EINVAL); return libbpf_err(-EINVAL);
if (attach_prog_fd && !attach_func_name) {
/* remember attach_prog_fd and let bpf_program__load() find
* BTF ID during the program load
*/
prog->attach_prog_fd = attach_prog_fd;
return 0;
}
if (attach_prog_fd) { if (attach_prog_fd) {
btf_id = libbpf_find_prog_btf_id(attach_func_name, btf_id = libbpf_find_prog_btf_id(attach_func_name,
attach_prog_fd); attach_prog_fd);
if (btf_id < 0) if (btf_id < 0)
return libbpf_err(btf_id); return libbpf_err(btf_id);
} else { } else {
if (!attach_func_name)
return libbpf_err(-EINVAL);
/* load btf_vmlinux, if not yet */ /* load btf_vmlinux, if not yet */
err = bpf_object__load_vmlinux_btf(prog->obj, true); err = bpf_object__load_vmlinux_btf(prog->obj, true);
if (err) if (err)
...@@ -10908,7 +10918,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s) ...@@ -10908,7 +10918,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
if (!prog->sec_def || !prog->sec_def->attach_fn) if (!prog->sec_def || !prog->sec_def->attach_fn)
continue; continue;
*link = prog->sec_def->attach_fn(prog); *link = bpf_program__attach(prog);
err = libbpf_get_error(*link); err = libbpf_get_error(*link);
if (err) { if (err) {
pr_warn("failed to auto-attach program '%s': %d\n", pr_warn("failed to auto-attach program '%s': %d\n",
......
...@@ -83,12 +83,15 @@ struct bpf_object_open_opts { ...@@ -83,12 +83,15 @@ struct bpf_object_open_opts {
* Non-relocatable instructions are replaced with invalid ones to * Non-relocatable instructions are replaced with invalid ones to
* prevent accidental errors. * prevent accidental errors.
* */ * */
LIBBPF_DEPRECATED_SINCE(0, 6, "field has no effect")
bool relaxed_core_relocs; bool relaxed_core_relocs;
/* maps that set the 'pinning' attribute in their definition will have /* maps that set the 'pinning' attribute in their definition will have
* their pin_path attribute set to a file in this directory, and be * their pin_path attribute set to a file in this directory, and be
* auto-pinned to that path on load; defaults to "/sys/fs/bpf". * auto-pinned to that path on load; defaults to "/sys/fs/bpf".
*/ */
const char *pin_root_path; const char *pin_root_path;
LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_program__set_attach_target() on each individual bpf_program")
__u32 attach_prog_fd; __u32 attach_prog_fd;
/* Additional kernel config content that augments and overrides /* Additional kernel config content that augments and overrides
* system Kconfig for CONFIG_xxx externs. * system Kconfig for CONFIG_xxx externs.
...@@ -243,7 +246,7 @@ LIBBPF_API int bpf_link__detach(struct bpf_link *link); ...@@ -243,7 +246,7 @@ LIBBPF_API int bpf_link__detach(struct bpf_link *link);
LIBBPF_API int bpf_link__destroy(struct bpf_link *link); LIBBPF_API int bpf_link__destroy(struct bpf_link *link);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach(struct bpf_program *prog); bpf_program__attach(const struct bpf_program *prog);
struct bpf_perf_event_opts { struct bpf_perf_event_opts {
/* size of this struct, for forward/backward compatiblity */ /* size of this struct, for forward/backward compatiblity */
...@@ -254,10 +257,10 @@ struct bpf_perf_event_opts { ...@@ -254,10 +257,10 @@ struct bpf_perf_event_opts {
#define bpf_perf_event_opts__last_field bpf_cookie #define bpf_perf_event_opts__last_field bpf_cookie
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_perf_event(struct bpf_program *prog, int pfd); bpf_program__attach_perf_event(const struct bpf_program *prog, int pfd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_perf_event_opts(struct bpf_program *prog, int pfd, bpf_program__attach_perf_event_opts(const struct bpf_program *prog, int pfd,
const struct bpf_perf_event_opts *opts); const struct bpf_perf_event_opts *opts);
struct bpf_kprobe_opts { struct bpf_kprobe_opts {
...@@ -274,10 +277,10 @@ struct bpf_kprobe_opts { ...@@ -274,10 +277,10 @@ struct bpf_kprobe_opts {
#define bpf_kprobe_opts__last_field retprobe #define bpf_kprobe_opts__last_field retprobe
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe, bpf_program__attach_kprobe(const struct bpf_program *prog, bool retprobe,
const char *func_name); const char *func_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_kprobe_opts(struct bpf_program *prog, bpf_program__attach_kprobe_opts(const struct bpf_program *prog,
const char *func_name, const char *func_name,
const struct bpf_kprobe_opts *opts); const struct bpf_kprobe_opts *opts);
...@@ -297,11 +300,11 @@ struct bpf_uprobe_opts { ...@@ -297,11 +300,11 @@ struct bpf_uprobe_opts {
#define bpf_uprobe_opts__last_field retprobe #define bpf_uprobe_opts__last_field retprobe
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe, bpf_program__attach_uprobe(const struct bpf_program *prog, bool retprobe,
pid_t pid, const char *binary_path, pid_t pid, const char *binary_path,
size_t func_offset); size_t func_offset);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid, bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
const char *binary_path, size_t func_offset, const char *binary_path, size_t func_offset,
const struct bpf_uprobe_opts *opts); const struct bpf_uprobe_opts *opts);
...@@ -314,35 +317,35 @@ struct bpf_tracepoint_opts { ...@@ -314,35 +317,35 @@ struct bpf_tracepoint_opts {
#define bpf_tracepoint_opts__last_field bpf_cookie #define bpf_tracepoint_opts__last_field bpf_cookie
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_tracepoint(struct bpf_program *prog, bpf_program__attach_tracepoint(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name); const char *tp_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_tracepoint_opts(struct bpf_program *prog, bpf_program__attach_tracepoint_opts(const struct bpf_program *prog,
const char *tp_category, const char *tp_category,
const char *tp_name, const char *tp_name,
const struct bpf_tracepoint_opts *opts); const struct bpf_tracepoint_opts *opts);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_raw_tracepoint(struct bpf_program *prog, bpf_program__attach_raw_tracepoint(const struct bpf_program *prog,
const char *tp_name); const char *tp_name);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_trace(struct bpf_program *prog); bpf_program__attach_trace(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_lsm(struct bpf_program *prog); bpf_program__attach_lsm(const struct bpf_program *prog);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(struct bpf_program *prog, int cgroup_fd); bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_netns(struct bpf_program *prog, int netns_fd); bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(struct bpf_program *prog, int ifindex); bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_freplace(struct bpf_program *prog, bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, const char *attach_func_name); int target_fd, const char *attach_func_name);
struct bpf_map; struct bpf_map;
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(struct bpf_map *map); LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
struct bpf_iter_attach_opts { struct bpf_iter_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */ size_t sz; /* size of this struct for forward/backward compatibility */
...@@ -352,7 +355,7 @@ struct bpf_iter_attach_opts { ...@@ -352,7 +355,7 @@ struct bpf_iter_attach_opts {
#define bpf_iter_attach_opts__last_field link_info_len #define bpf_iter_attach_opts__last_field link_info_len
LIBBPF_API struct bpf_link * LIBBPF_API struct bpf_link *
bpf_program__attach_iter(struct bpf_program *prog, bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts); const struct bpf_iter_attach_opts *opts);
struct bpf_insn; struct bpf_insn;
......
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
#else #else
#define __LIBBPF_MARK_DEPRECATED_0_6(X) #define __LIBBPF_MARK_DEPRECATED_0_6(X)
#endif #endif
#if __LIBBPF_CURRENT_VERSION_GEQ(0, 7)
#define __LIBBPF_MARK_DEPRECATED_0_7(X) X
#else
#define __LIBBPF_MARK_DEPRECATED_0_7(X)
#endif
/* Helper macro to declare and initialize libbpf options struct /* Helper macro to declare and initialize libbpf options struct
* *
......
...@@ -249,8 +249,7 @@ static int duration = 0; ...@@ -249,8 +249,7 @@ static int duration = 0;
#define SIZE_CASE_COMMON(name) \ #define SIZE_CASE_COMMON(name) \
.case_name = #name, \ .case_name = #name, \
.bpf_obj_file = "test_core_reloc_size.o", \ .bpf_obj_file = "test_core_reloc_size.o", \
.btf_src_file = "btf__core_reloc_" #name ".o", \ .btf_src_file = "btf__core_reloc_" #name ".o"
.relaxed_core_relocs = true
#define SIZE_OUTPUT_DATA(type) \ #define SIZE_OUTPUT_DATA(type) \
STRUCT_TO_CHAR_PTR(core_reloc_size_output) { \ STRUCT_TO_CHAR_PTR(core_reloc_size_output) { \
......
...@@ -60,7 +60,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, ...@@ -60,7 +60,7 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
struct bpf_object *obj = NULL, *tgt_obj; struct bpf_object *obj = NULL, *tgt_obj;
__u32 retval, tgt_prog_id, info_len; __u32 retval, tgt_prog_id, info_len;
struct bpf_prog_info prog_info = {}; struct bpf_prog_info prog_info = {};
struct bpf_program **prog = NULL; struct bpf_program **prog = NULL, *p;
struct bpf_link **link = NULL; struct bpf_link **link = NULL;
int err, tgt_fd, i; int err, tgt_fd, i;
struct btf *btf; struct btf *btf;
...@@ -69,9 +69,6 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, ...@@ -69,9 +69,6 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
&tgt_obj, &tgt_fd); &tgt_obj, &tgt_fd);
if (!ASSERT_OK(err, "tgt_prog_load")) if (!ASSERT_OK(err, "tgt_prog_load"))
return; return;
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.attach_prog_fd = tgt_fd,
);
info_len = sizeof(prog_info); info_len = sizeof(prog_info);
err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len); err = bpf_obj_get_info_by_fd(tgt_fd, &prog_info, &info_len);
...@@ -89,10 +86,15 @@ static void test_fexit_bpf2bpf_common(const char *obj_file, ...@@ -89,10 +86,15 @@ static void test_fexit_bpf2bpf_common(const char *obj_file,
if (!ASSERT_OK_PTR(prog, "prog_ptr")) if (!ASSERT_OK_PTR(prog, "prog_ptr"))
goto close_prog; goto close_prog;
obj = bpf_object__open_file(obj_file, &opts); obj = bpf_object__open_file(obj_file, NULL);
if (!ASSERT_OK_PTR(obj, "obj_open")) if (!ASSERT_OK_PTR(obj, "obj_open"))
goto close_prog; goto close_prog;
bpf_object__for_each_program(p, obj) {
err = bpf_program__set_attach_target(p, tgt_fd, NULL);
ASSERT_OK(err, "set_attach_target");
}
err = bpf_object__load(obj); err = bpf_object__load(obj);
if (!ASSERT_OK(err, "obj_load")) if (!ASSERT_OK(err, "obj_load"))
goto close_prog; goto close_prog;
...@@ -270,7 +272,7 @@ static void test_fmod_ret_freplace(void) ...@@ -270,7 +272,7 @@ static void test_fmod_ret_freplace(void)
struct bpf_link *freplace_link = NULL; struct bpf_link *freplace_link = NULL;
struct bpf_program *prog; struct bpf_program *prog;
__u32 duration = 0; __u32 duration = 0;
int err, pkt_fd; int err, pkt_fd, attach_prog_fd;
err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC, err = bpf_prog_load(tgt_name, BPF_PROG_TYPE_UNSPEC,
&pkt_obj, &pkt_fd); &pkt_obj, &pkt_fd);
...@@ -278,26 +280,32 @@ static void test_fmod_ret_freplace(void) ...@@ -278,26 +280,32 @@ static void test_fmod_ret_freplace(void)
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
tgt_name, err, errno)) tgt_name, err, errno))
return; return;
opts.attach_prog_fd = pkt_fd;
freplace_obj = bpf_object__open_file(freplace_name, &opts); freplace_obj = bpf_object__open_file(freplace_name, NULL);
if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open")) if (!ASSERT_OK_PTR(freplace_obj, "freplace_obj_open"))
goto out; goto out;
prog = bpf_program__next(NULL, freplace_obj);
err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
ASSERT_OK(err, "freplace__set_attach_target");
err = bpf_object__load(freplace_obj); err = bpf_object__load(freplace_obj);
if (CHECK(err, "freplace_obj_load", "err %d\n", err)) if (CHECK(err, "freplace_obj_load", "err %d\n", err))
goto out; goto out;
prog = bpf_program__next(NULL, freplace_obj);
freplace_link = bpf_program__attach_trace(prog); freplace_link = bpf_program__attach_trace(prog);
if (!ASSERT_OK_PTR(freplace_link, "freplace_attach_trace")) if (!ASSERT_OK_PTR(freplace_link, "freplace_attach_trace"))
goto out; goto out;
opts.attach_prog_fd = bpf_program__fd(prog); fmod_obj = bpf_object__open_file(fmod_ret_name, NULL);
fmod_obj = bpf_object__open_file(fmod_ret_name, &opts);
if (!ASSERT_OK_PTR(fmod_obj, "fmod_obj_open")) if (!ASSERT_OK_PTR(fmod_obj, "fmod_obj_open"))
goto out; goto out;
attach_prog_fd = bpf_program__fd(prog);
prog = bpf_program__next(NULL, fmod_obj);
err = bpf_program__set_attach_target(prog, attach_prog_fd, NULL);
ASSERT_OK(err, "fmod_ret_set_attach_target");
err = bpf_object__load(fmod_obj); err = bpf_object__load(fmod_obj);
if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n")) if (CHECK(!err, "fmod_obj_load", "loading fmod_ret should fail\n"))
goto out; goto out;
...@@ -322,14 +330,14 @@ static void test_func_sockmap_update(void) ...@@ -322,14 +330,14 @@ static void test_func_sockmap_update(void)
} }
static void test_obj_load_failure_common(const char *obj_file, static void test_obj_load_failure_common(const char *obj_file,
const char *target_obj_file) const char *target_obj_file)
{ {
/* /*
* standalone test that asserts failure to load freplace prog * standalone test that asserts failure to load freplace prog
* because of invalid return code. * because of invalid return code.
*/ */
struct bpf_object *obj = NULL, *pkt_obj; struct bpf_object *obj = NULL, *pkt_obj;
struct bpf_program *prog;
int err, pkt_fd; int err, pkt_fd;
__u32 duration = 0; __u32 duration = 0;
...@@ -339,14 +347,15 @@ static void test_obj_load_failure_common(const char *obj_file, ...@@ -339,14 +347,15 @@ static void test_obj_load_failure_common(const char *obj_file,
if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n", if (CHECK(err, "tgt_prog_load", "file %s err %d errno %d\n",
target_obj_file, err, errno)) target_obj_file, err, errno))
return; return;
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.attach_prog_fd = pkt_fd,
);
obj = bpf_object__open_file(obj_file, &opts); obj = bpf_object__open_file(obj_file, NULL);
if (!ASSERT_OK_PTR(obj, "obj_open")) if (!ASSERT_OK_PTR(obj, "obj_open"))
goto close_prog; goto close_prog;
prog = bpf_program__next(NULL, obj);
err = bpf_program__set_attach_target(prog, pkt_fd, NULL);
ASSERT_OK(err, "set_attach_target");
/* It should fail to load the program */ /* It should fail to load the program */
err = bpf_object__load(obj); err = bpf_object__load(obj);
if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err)) if (CHECK(!err, "bpf_obj_load should fail", "err %d\n", err))
......
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