Skip to content

Commit f1674dc

Browse files
anakryikoborkmann
authored andcommitted
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent cb8edce commit f1674dc

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

tools/lib/bpf/bpf.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -572,35 +572,46 @@ int bpf_map_update_batch(int fd, const void *keys, const void *values, __u32 *co
572572
(void *)keys, (void *)values, count, opts);
573573
}
574574

575-
int bpf_obj_pin(int fd, const char *pathname)
575+
int bpf_obj_pin_opts(int fd, const char *pathname, const struct bpf_obj_pin_opts *opts)
576576
{
577-
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
577+
const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
578578
union bpf_attr attr;
579579
int ret;
580580

581+
if (!OPTS_VALID(opts, bpf_obj_pin_opts))
582+
return libbpf_err(-EINVAL);
583+
581584
memset(&attr, 0, attr_sz);
585+
attr.path_fd = OPTS_GET(opts, path_fd, 0);
582586
attr.pathname = ptr_to_u64((void *)pathname);
587+
attr.file_flags = OPTS_GET(opts, file_flags, 0);
583588
attr.bpf_fd = fd;
584589

585590
ret = sys_bpf(BPF_OBJ_PIN, &attr, attr_sz);
586591
return libbpf_err_errno(ret);
587592
}
588593

594+
int bpf_obj_pin(int fd, const char *pathname)
595+
{
596+
return bpf_obj_pin_opts(fd, pathname, NULL);
597+
}
598+
589599
int bpf_obj_get(const char *pathname)
590600
{
591601
return bpf_obj_get_opts(pathname, NULL);
592602
}
593603

594604
int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
595605
{
596-
const size_t attr_sz = offsetofend(union bpf_attr, file_flags);
606+
const size_t attr_sz = offsetofend(union bpf_attr, path_fd);
597607
union bpf_attr attr;
598608
int fd;
599609

600610
if (!OPTS_VALID(opts, bpf_obj_get_opts))
601611
return libbpf_err(-EINVAL);
602612

603613
memset(&attr, 0, attr_sz);
614+
attr.path_fd = OPTS_GET(opts, path_fd, 0);
604615
attr.pathname = ptr_to_u64((void *)pathname);
605616
attr.file_flags = OPTS_GET(opts, file_flags, 0);
606617

tools/lib/bpf/bpf.h

+16-2
Original file line numberDiff line numberDiff line change
@@ -284,16 +284,30 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
284284
__u32 *count,
285285
const struct bpf_map_batch_opts *opts);
286286

287-
struct bpf_obj_get_opts {
287+
struct bpf_obj_pin_opts {
288288
size_t sz; /* size of this struct for forward/backward compatibility */
289289

290290
__u32 file_flags;
291+
int path_fd;
291292

292293
size_t :0;
293294
};
294-
#define bpf_obj_get_opts__last_field file_flags
295+
#define bpf_obj_pin_opts__last_field path_fd
295296

296297
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
298+
LIBBPF_API int bpf_obj_pin_opts(int fd, const char *pathname,
299+
const struct bpf_obj_pin_opts *opts);
300+
301+
struct bpf_obj_get_opts {
302+
size_t sz; /* size of this struct for forward/backward compatibility */
303+
304+
__u32 file_flags;
305+
int path_fd;
306+
307+
size_t :0;
308+
};
309+
#define bpf_obj_get_opts__last_field path_fd
310+
297311
LIBBPF_API int bpf_obj_get(const char *pathname);
298312
LIBBPF_API int bpf_obj_get_opts(const char *pathname,
299313
const struct bpf_obj_get_opts *opts);

tools/lib/bpf/libbpf.map

+2
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,6 @@ LIBBPF_1.2.0 {
393393
} LIBBPF_1.1.0;
394394

395395
LIBBPF_1.3.0 {
396+
global:
397+
bpf_obj_pin_opts;
396398
} LIBBPF_1.2.0;

0 commit comments

Comments
 (0)