mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-09 18:19:06 +08:00
libbpf: Add opts-based bpf_obj_pin() API and add support for path_fd
Add path_fd support for bpf_obj_pin() and bpf_obj_get() operations (through their opts-based variants). This allows to take advantage of new kernel-side support for O_PATH-based pin/get location specification. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20230523170013.728457-4-andrii@kernel.org Signed-off-by: Daniel Müller <deso@posteo.net>
This commit is contained in:
committed by
Andrii Nakryiko
parent
bfb0454244
commit
a50544ef45
17
src/bpf.c
17
src/bpf.c
@@ -572,20 +572,30 @@ int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *co
|
|||||||
(void *)keys, (void *)values, count, opts);
|
(void *)keys, (void *)values, count, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
int bpf_obj_pin(int fd, const char *pathname)
|
int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts)
|
||||||
{
|
{
|
||||||
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
|
const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
|
||||||
union bpf_attr attr;
|
union bpf_attr attr;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
if (!OPTS_VALID(opts, bpf_obj_pin_opts))
|
||||||
|
return libbpf_err(-EINVAL);
|
||||||
|
|
||||||
memset(&attr, 0, attr_sz);
|
memset(&attr, 0, attr_sz);
|
||||||
|
attr.path_fd = OPTS_GET(opts, path_fd, 0);
|
||||||
attr.pathname = ptr_to_u64((void *)pathname);
|
attr.pathname = ptr_to_u64((void *)pathname);
|
||||||
|
attr.file_flags = OPTS_GET(opts, file_flags, 0);
|
||||||
attr.bpf_fd = fd;
|
attr.bpf_fd = fd;
|
||||||
|
|
||||||
ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
|
ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
|
||||||
return libbpf_err_errno(ret);
|
return libbpf_err_errno(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int bpf_obj_pin(int fd, const char *pathname)
|
||||||
|
{
|
||||||
|
return bpf_obj_pin_opts(fd, pathname, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
int bpf_obj_get(const char *pathname)
|
int bpf_obj_get(const char *pathname)
|
||||||
{
|
{
|
||||||
return bpf_obj_get_opts(pathname, NULL);
|
return bpf_obj_get_opts(pathname, NULL);
|
||||||
@@ -593,7 +603,7 @@ int bpf_obj_get(const char *pathname)
|
|||||||
|
|
||||||
int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
|
int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
|
||||||
{
|
{
|
||||||
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
|
const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
|
||||||
union bpf_attr attr;
|
union bpf_attr attr;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
@@ -601,6 +611,7 @@ int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
|
|||||||
return libbpf_err(-EINVAL);
|
return libbpf_err(-EINVAL);
|
||||||
|
|
||||||
memset(&attr, 0, attr_sz);
|
memset(&attr, 0, attr_sz);
|
||||||
|
attr.path_fd = OPTS_GET(opts, path_fd, 0);
|
||||||
attr.pathname = ptr_to_u64((void *)pathname);
|
attr.pathname = ptr_to_u64((void *)pathname);
|
||||||
attr.file_flags = OPTS_GET(opts, file_flags, 0);
|
attr.file_flags = OPTS_GET(opts, file_flags, 0);
|
||||||
|
|
||||||
|
|||||||
18
src/bpf.h
18
src/bpf.h
@@ -284,16 +284,30 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
|
|||||||
__u32 *count,
|
__u32 *count,
|
||||||
const struct bpf_map_batch_opts *opts);
|
const struct bpf_map_batch_opts *opts);
|
||||||
|
|
||||||
|
struct bpf_obj_pin_opts {
|
||||||
|
size_t sz; /* size of this struct for forward/backward compatibility */
|
||||||
|
|
||||||
|
__u32 file_flags;
|
||||||
|
int path_fd;
|
||||||
|
|
||||||
|
size_t :0;
|
||||||
|
};
|
||||||
|
#define bpf_obj_pin_opts__last_field path_fd
|
||||||
|
|
||||||
|
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
|
||||||
|
LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname,
|
||||||
|
const struct bpf_obj_pin_opts *opts);
|
||||||
|
|
||||||
struct bpf_obj_get_opts {
|
struct bpf_obj_get_opts {
|
||||||
size_t sz; /* size of this struct for forward/backward compatibility */
|
size_t sz; /* size of this struct for forward/backward compatibility */
|
||||||
|
|
||||||
__u32 file_flags;
|
__u32 file_flags;
|
||||||
|
int path_fd;
|
||||||
|
|
||||||
size_t :0;
|
size_t :0;
|
||||||
};
|
};
|
||||||
#define bpf_obj_get_opts__last_field file_flags
|
#define bpf_obj_get_opts__last_field path_fd
|
||||||
|
|
||||||
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
|
|
||||||
LIBBPF_API int bpf_obj_get(const char *pathname);
|
LIBBPF_API int bpf_obj_get(const char *pathname);
|
||||||
LIBBPF_API int bpf_obj_get_opts(const char *pathname,
|
LIBBPF_API int bpf_obj_get_opts(const char *pathname,
|
||||||
const struct bpf_obj_get_opts *opts);
|
const struct bpf_obj_get_opts *opts);
|
||||||
|
|||||||
@@ -393,4 +393,6 @@ LIBBPF_1.2.0 {
|
|||||||
} LIBBPF_1.1.0;
|
} LIBBPF_1.1.0;
|
||||||
|
|
||||||
LIBBPF_1.3.0 {
|
LIBBPF_1.3.0 {
|
||||||
|
global:
|
||||||
|
bpf_obj_pin_opts;
|
||||||
} LIBBPF_1.2.0;
|
} LIBBPF_1.2.0;
|
||||||
|
|||||||
Reference in New Issue
Block a user