Skip to content

Commit 79d6717

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
libbpf: Add bpf_struct_ops_associate_prog() API
Add low-level wrapper API for BPF_STRUCT_OPS_ASSOCIATE_PROG command in bpf() syscall. Signed-off-by: Amery Hung <[email protected]>
1 parent 3f9ca60 commit 79d6717

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,3 +1397,21 @@ int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
13971397
err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz);
13981398
return libbpf_err_errno(err);
13991399
}
1400+
1401+
int bpf_struct_ops_associate_prog(int map_fd, int prog_fd,
1402+
struct bpf_struct_ops_associate_prog_opts *opts)
1403+
{
1404+
const size_t attr_sz = offsetofend(union bpf_attr, struct_ops_assoc_prog);
1405+
union bpf_attr attr;
1406+
int err;
1407+
1408+
if (!OPTS_VALID(opts, bpf_struct_ops_associate_prog_opts))
1409+
return libbpf_err(-EINVAL);
1410+
1411+
memset(&attr, 0, attr_sz);
1412+
attr.struct_ops_assoc_prog.map_fd = map_fd;
1413+
attr.struct_ops_assoc_prog.prog_fd = prog_fd;
1414+
1415+
err = sys_bpf(BPF_STRUCT_OPS_ASSOCIATE_PROG, &attr, attr_sz);
1416+
return libbpf_err_errno(err);
1417+
}

tools/lib/bpf/bpf.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,25 @@ struct bpf_prog_stream_read_opts {
733733
LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
734734
struct bpf_prog_stream_read_opts *opts);
735735

736+
struct bpf_struct_ops_associate_prog_opts {
737+
size_t sz;
738+
size_t :0;
739+
};
740+
#define bpf_struct_ops_associate_prog_opts__last_field sz
741+
/**
742+
* @brief **bpf_struct_ops_associate_prog** associate a BPF program with a
743+
* struct_ops map.
744+
*
745+
* @param map_fd FD for the struct_ops map to be associated with a BPF progam
746+
* @param prog_fd FD for the BPF program
747+
* @param opts optional options, can be NULL
748+
*
749+
* @return 0 on success; negative error code, otherwise (errno is also set to
750+
* the error code)
751+
*/
752+
LIBBPF_API int bpf_struct_ops_associate_prog(int map_fd, int prog_fd,
753+
struct bpf_struct_ops_associate_prog_opts *opts);
754+
736755
#ifdef __cplusplus
737756
} /* extern "C" */
738757
#endif

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,5 @@ LIBBPF_1.7.0 {
451451
global:
452452
bpf_map__set_exclusive_program;
453453
bpf_map__exclusive_program;
454+
bpf_struct_ops_associate_prog;
454455
} LIBBPF_1.6.0;

0 commit comments

Comments
 (0)