]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
libbpf: Introduce bpf_prog_attach_xattr
authorAndrey Ignatov <rdna@fb.com>
Thu, 19 Dec 2019 07:44:36 +0000 (23:44 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 20 Dec 2019 05:22:25 +0000 (21:22 -0800)
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 <rdna@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/bd6e0732303eb14e4b79cb128268d9e9ad6db208.1576741281.git.rdna@fb.com
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h
tools/lib/bpf/libbpf.map

index 98596e15390fb7d9a8159745942cc8634f5b243d..a787d53699c8193492b2919dfbc551fd2ee29db3 100644 (file)
@@ -466,14 +466,29 @@ int bpf_obj_get(const char *pathname)
 
 int bpf_prog_attach(int prog_fd, int target_fd, enum bpf_attach_type type,
                    unsigned int flags)
+{
+       DECLARE_LIBBPF_OPTS(bpf_prog_attach_opts, opts,
+               .flags = flags,
+       );
+
+       return bpf_prog_attach_xattr(prog_fd, target_fd, type, &opts);
+}
+
+int bpf_prog_attach_xattr(int prog_fd, int target_fd,
+                         enum bpf_attach_type type,
+                         const struct bpf_prog_attach_opts *opts)
 {
        union bpf_attr attr;
 
+       if (!OPTS_VALID(opts, bpf_prog_attach_opts))
+               return -EINVAL;
+
        memset(&attr, 0, sizeof(attr));
        attr.target_fd     = target_fd;
        attr.attach_bpf_fd = prog_fd;
        attr.attach_type   = type;
-       attr.attach_flags  = flags;
+       attr.attach_flags  = OPTS_GET(opts, flags, 0);
+       attr.replace_bpf_fd = OPTS_GET(opts, replace_prog_fd, 0);
 
        return sys_bpf(BPF_PROG_ATTACH, &attr, sizeof(attr));
 }
index 269807ce9ef5289d62c648845211023f694bc3ad..f0ab8519986ec05b6274118395adaf7a568f8da9 100644 (file)
@@ -126,8 +126,19 @@ LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
 LIBBPF_API int bpf_map_freeze(int fd);
 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
 LIBBPF_API int bpf_obj_get(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;
+};
+#define bpf_prog_attach_opts__last_field replace_prog_fd
+
 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
                               enum bpf_attach_type type, unsigned int flags);
+LIBBPF_API int bpf_prog_attach_xattr(int prog_fd, int attachable_fd,
+                                    enum bpf_attach_type type,
+                                    const struct bpf_prog_attach_opts *opts);
 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);
index e3a471f38a715698952b1e31fbf1c734ad2c39f6..e9713a57424325c6e69092e0390c42f89bd17f45 100644 (file)
@@ -219,6 +219,7 @@ LIBBPF_0.0.7 {
                bpf_object__detach_skeleton;
                bpf_object__load_skeleton;
                bpf_object__open_skeleton;
+               bpf_prog_attach_xattr;
                bpf_program__attach;
                bpf_program__name;
                btf__align_of;