Skip to content

Commit cdbee38

Browse files
rdnaAlexei Starovoitov
authored andcommitted
libbpf: Introduce bpf_prog_attach_xattr
Introduce a new bpf_prog_attach_xattr function that, in addition to program fd, target fd and attach type, accepts an extendable struct bpf_prog_attach_opts. bpf_prog_attach_opts relies on DECLARE_LIBBPF_OPTS macro to maintain backward and forward compatibility and has the following "optional" attach attributes: * existing attach_flags, since it's not required when attaching in NONE mode. Even though it's quite often used in MULTI and OVERRIDE mode it seems to be a good idea to reduce number of arguments to bpf_prog_attach_xattr; * newly introduced attribute of BPF_PROG_ATTACH command: replace_prog_fd that is fd of previously attached cgroup-bpf program to replace if BPF_F_REPLACE flag is used. The new function is named to be consistent with other xattr-functions (bpf_prog_test_run_xattr, bpf_create_map_xattr, bpf_load_program_xattr). The struct bpf_prog_attach_opts is supposed to be used with DECLARE_LIBBPF_OPTS macro. Signed-off-by: Andrey Ignatov <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/bd6e0732303eb14e4b79cb128268d9e9ad6db208.1576741281.git.rdna@fb.com
1 parent 7dd68b3 commit cdbee38

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

tools/lib/bpf/bpf.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,29 @@ int bpf_obj_get(const char *pathname)
466466

467467
int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
468468
unsigned int flags)
469+
{
470+
DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
471+
.flags = flags,
472+
);
473+
474+
return bpf_prog_attach_xattr(prog_fd, target_fd, type, &opts);
475+
}
476+
477+
int bpf_prog_attach_xattr(int prog_fd, int target_fd,
478+
enum bpf_attach_type type,
479+
const struct bpf_prog_attach_opts *opts)
469480
{
470481
union bpf_attr attr;
471482

483+
if (!OPTS_VALID(opts, bpf_prog_attach_opts))
484+
return -EINVAL;
485+
472486
memset(&attr, 0, sizeof(attr));
473487
attr.target_fd = target_fd;
474488
attr.attach_bpf_fd = prog_fd;
475489
attr.attach_type = type;
476-
attr.attach_flags = flags;
490+
attr.attach_flags = OPTS_GET(opts, flags, 0);
491+
attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
477492

478493
return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
479494
}

tools/lib/bpf/bpf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,19 @@ LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
126126
LIBBPF_API int bpf_map_freeze(int fd);
127127
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
128128
LIBBPF_API int bpf_obj_get(const char *pathname);
129+
130+
struct bpf_prog_attach_opts {
131+
size_t sz; /* size of this struct for forward/backward compatibility */
132+
unsigned int flags;
133+
int replace_prog_fd;
134+
};
135+
#define bpf_prog_attach_opts__last_field replace_prog_fd
136+
129137
LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
130138
enum bpf_attach_type type, unsigned int flags);
139+
LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
140+
enum bpf_attach_type type,
141+
const struct bpf_prog_attach_opts *opts);
131142
LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
132143
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
133144
enum bpf_attach_type type);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ LIBBPF_0.0.7 {
219219
bpf_object__detach_skeleton;
220220
bpf_object__load_skeleton;
221221
bpf_object__open_skeleton;
222+
bpf_prog_attach_xattr;
222223
bpf_program__attach;
223224
bpf_program__name;
224225
btf__align_of;

0 commit comments

Comments
 (0)