Skip to content

Commit

Permalink
libbpf: Add extended attach/detach opts
Browse files Browse the repository at this point in the history
Extend libbpf attach opts and add a new detach opts API so this can be used
to add/remove fd-based tc BPF programs. For concrete usage examples, see the
extensive selftests that have been developed as part of this series.

Co-developed-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
Co-developed-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
borkmann committed Oct 3, 2022
1 parent b89b4ff commit f363c5c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
20 changes: 20 additions & 0 deletions tools/lib/bpf/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,26 @@ int bpf_prog_detach2(int prog_fd, int target_fd, enum bpf_attach_type type)
return libbpf_err_errno(ret);
}

int bpf_prog_detach_opts(int prog_fd, int target_fd,
enum bpf_attach_type type,
const struct bpf_prog_detach_opts *opts)
{
union bpf_attr attr;
int ret;

if (!OPTS_VALID(opts, bpf_prog_detach_opts))
return libbpf_err(-EINVAL);

memset(&attr, 0, sizeof(attr));
attr.target_fd = target_fd;
attr.attach_bpf_fd = prog_fd;
attr.attach_type = type;
attr.attach_priority = OPTS_GET(opts, attach_priority, 0);

ret = sys_bpf(BPF_PROG_DETACH, &attr, sizeof(attr));
return libbpf_err_errno(ret);
}

int bpf_link_create(int prog_fd, int target_fd,
enum bpf_attach_type attach_type,
const struct bpf_link_create_opts *opts)
Expand Down
17 changes: 15 additions & 2 deletions tools/lib/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,11 @@ LIBBPF_API int bpf_obj_get_opts(const char *pathname,

struct bpf_prog_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
unsigned int flags;
int replace_prog_fd;
__u32 flags;
union {
int replace_prog_fd;
__u32 attach_priority;
};
};
#define bpf_prog_attach_opts__last_field replace_prog_fd

Expand All @@ -296,9 +299,19 @@ LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
enum bpf_attach_type type,
const struct bpf_prog_attach_opts *opts);

struct bpf_prog_detach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 attach_priority;
};
#define bpf_prog_detach_opts__last_field attach_priority

LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
enum bpf_attach_type type);
LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target_fd,
enum bpf_attach_type type,
const struct bpf_prog_detach_opts *opts);

union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
struct bpf_link_create_opts {
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ LIBBPF_1.0.0 {
global:
bpf_obj_get_opts;
bpf_prog_query_opts;
bpf_prog_detach_opts;
bpf_program__attach_ksyscall;
btf__add_enum64;
btf__add_enum64_value;
Expand Down

0 comments on commit f363c5c

Please sign in to comment.