Skip to content

Commit 8ecffed

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
libbpf: Add bpf_prog_assoc_struct_ops() API
Add low-level wrapper API for BPF_PROG_ASSOC_STRUCT_OPS command in the bpf() syscall. Signed-off-by: Amery Hung <[email protected]>
1 parent 13d637f commit 8ecffed

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,3 +1397,22 @@ 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_prog_assoc_struct_ops(int map_fd, int prog_fd,
1402+
struct bpf_prog_assoc_struct_ops_opts *opts)
1403+
{
1404+
const size_t attr_sz = offsetofend(union bpf_attr, prog_assoc_struct_ops);
1405+
union bpf_attr attr;
1406+
int err;
1407+
1408+
if (!OPTS_VALID(opts, bpf_prog_assoc_struct_ops_opts))
1409+
return libbpf_err(-EINVAL);
1410+
1411+
memset(&attr, 0, attr_sz);
1412+
attr.prog_assoc_struct_ops.map_fd = map_fd;
1413+
attr.prog_assoc_struct_ops.prog_fd = prog_fd;
1414+
attr.prog_assoc_struct_ops.flags = OPTS_GET(opts, flags, 0);
1415+
1416+
err = sys_bpf(BPF_PROG_ASSOC_STRUCT_OPS, &attr, attr_sz);
1417+
return libbpf_err_errno(err);
1418+
}

tools/lib/bpf/bpf.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,26 @@ 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_prog_assoc_struct_ops_opts {
737+
size_t sz;
738+
__u32 flags;
739+
size_t :0;
740+
};
741+
#define bpf_prog_assoc_struct_ops_opts__last_field flags
742+
/**
743+
* @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a
744+
* struct_ops map.
745+
*
746+
* @param map_fd FD for the struct_ops map to be associated with a BPF program
747+
* @param prog_fd FD for the BPF program
748+
* @param opts optional options, can be NULL
749+
*
750+
* @return 0 on success; negative error code, otherwise (errno is also set to
751+
* the error code)
752+
*/
753+
LIBBPF_API int bpf_prog_assoc_struct_ops(int map_fd, int prog_fd,
754+
struct bpf_prog_assoc_struct_ops_opts *opts);
755+
736756
#ifdef __cplusplus
737757
} /* extern "C" */
738758
#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_prog_assoc_struct_ops;
454455
} LIBBPF_1.6.0;

0 commit comments

Comments
 (0)