Skip to content

Commit

Permalink
Merge branch 'Improve set_attach_target() and deprecate open_opts.att…
Browse files Browse the repository at this point in the history
…ach_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: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Alexei Starovoitov committed Sep 17, 2021
2 parents 3365627 + 942025c commit f706f6c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 81 deletions.
98 changes: 54 additions & 44 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ struct reloc_desc {

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 {
const char *sec;
Expand Down Expand Up @@ -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_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 ||
prog->sec_def->prog_type == BPF_PROG_TYPE_EXT)
prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0);
#pragma GCC diagnostic pop
}

return 0;
Expand Down Expand Up @@ -7944,12 +7947,12 @@ void bpf_program__set_expected_attach_type(struct bpf_program *prog,
__VA_ARGS__ \
}

static struct bpf_link *attach_kprobe(struct bpf_program *prog);
static struct bpf_link *attach_tp(struct bpf_program *prog);
static struct bpf_link *attach_raw_tp(struct bpf_program *prog);
static struct bpf_link *attach_trace(struct bpf_program *prog);
static struct bpf_link *attach_lsm(struct bpf_program *prog);
static struct bpf_link *attach_iter(struct bpf_program *prog);
static struct bpf_link *attach_kprobe(const struct bpf_program *prog);
static struct bpf_link *attach_tp(const struct bpf_program *prog);
static struct bpf_link *attach_raw_tp(const struct bpf_program *prog);
static struct bpf_link *attach_trace(const struct bpf_program *prog);
static struct bpf_link *attach_lsm(const struct bpf_program *prog);
static struct bpf_link *attach_iter(const struct bpf_program *prog);

static const struct bpf_sec_def section_defs[] = {
BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER),
Expand Down Expand Up @@ -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;
__u32 attach_prog_fd = prog->attach_prog_fd;
const char *name = prog->sec_name, *attach_name;
const struct bpf_sec_def *sec = NULL;
const char *attach_name;
int err = 0;

if (!name)
return -EINVAL;

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);
if (!prog->sec_def || !prog->sec_def->is_attach_btf) {
pr_warn("failed to identify BTF ID based on ELF section name '%s'\n",
prog->sec_name);
return -ESRCH;
}
attach_name = name + sec->len;
attach_name = prog->sec_name + prog->sec_def->len;

/* BPF program's BTF ID */
if (attach_prog_fd) {
Expand Down Expand Up @@ -9093,7 +9092,7 @@ static void bpf_link_perf_dealloc(struct bpf_link *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)
{
char errmsg[STRERR_BUFSIZE];
Expand Down Expand Up @@ -9168,7 +9167,7 @@ struct bpf_link *bpf_program__attach_perf_event_opts(struct bpf_program *prog, i
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);
}
Expand Down Expand Up @@ -9333,7 +9332,7 @@ static int perf_event_kprobe_open_legacy(bool retprobe, const char *name, uint64
}

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 struct bpf_kprobe_opts *opts)
{
Expand Down Expand Up @@ -9390,7 +9389,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog,
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,
const char *func_name)
{
Expand All @@ -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);
}

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);
unsigned long offset = 0;
Expand Down Expand Up @@ -9433,7 +9432,7 @@ static struct bpf_link *attach_kprobe(struct bpf_program *prog)
}

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 struct bpf_uprobe_opts *opts)
{
Expand Down Expand Up @@ -9473,7 +9472,7 @@ bpf_program__attach_uprobe_opts(struct bpf_program *prog, pid_t pid,
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,
const char *binary_path,
size_t func_offset)
Expand Down Expand Up @@ -9533,7 +9532,7 @@ static int perf_event_open_tracepoint(const char *tp_category,
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_name,
const struct bpf_tracepoint_opts *opts)
Expand Down Expand Up @@ -9567,14 +9566,14 @@ struct bpf_link *bpf_program__attach_tracepoint_opts(struct bpf_program *prog,
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_name)
{
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;
struct bpf_link *link;
Expand All @@ -9598,7 +9597,7 @@ static struct bpf_link *attach_tp(struct bpf_program *prog)
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)
{
char errmsg[STRERR_BUFSIZE];
Expand Down Expand Up @@ -9628,15 +9627,15 @@ struct bpf_link *bpf_program__attach_raw_tracepoint(struct bpf_program *prog,
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;

return bpf_program__attach_raw_tracepoint(prog, tp_name);
}

/* 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];
struct bpf_link *link;
Expand Down Expand Up @@ -9665,28 +9664,28 @@ static struct bpf_link *bpf_program__attach_btf_id(struct bpf_program *prog)
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);
}

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);
}

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);
}

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);
}

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)
{
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
Expand Down Expand Up @@ -9722,24 +9721,24 @@ bpf_program__attach_fd(struct bpf_program *prog, int target_fd, int btf_id,
}

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");
}

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");
}

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 */
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,
const char *attach_func_name)
{
Expand Down Expand Up @@ -9772,7 +9771,7 @@ struct bpf_link *bpf_program__attach_freplace(struct bpf_program *prog,
}

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)
{
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
Expand Down Expand Up @@ -9811,12 +9810,12 @@ bpf_program__attach_iter(struct bpf_program *prog,
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);
}

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)
return libbpf_err_ptr(-ESRCH);
Expand All @@ -9834,7 +9833,7 @@ static int bpf_link__detach_struct_ops(struct bpf_link *link)
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_link *link;
Expand Down Expand Up @@ -10647,18 +10646,29 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
{
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);

if (prog->obj->loaded)
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) {
btf_id = libbpf_find_prog_btf_id(attach_func_name,
attach_prog_fd);
if (btf_id < 0)
return libbpf_err(btf_id);
} else {
if (!attach_func_name)
return libbpf_err(-EINVAL);

/* load btf_vmlinux, if not yet */
err = bpf_object__load_vmlinux_btf(prog->obj, true);
if (err)
Expand Down Expand Up @@ -10908,7 +10918,7 @@ int bpf_object__attach_skeleton(struct bpf_object_skeleton *s)
if (!prog->sec_def || !prog->sec_def->attach_fn)
continue;

*link = prog->sec_def->attach_fn(prog);
*link = bpf_program__attach(prog);
err = libbpf_get_error(*link);
if (err) {
pr_warn("failed to auto-attach program '%s': %d\n",
Expand Down
Loading

0 comments on commit f706f6c

Please sign in to comment.