Skip to content

Commit 38c84e2

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
libbpf: Add support for associating BPF program with struct_ops
Add low-level wrapper and libbpf API for BPF_PROG_ASSOC_STRUCT_OPS command in the bpf() syscall. Signed-off-by: Amery Hung <[email protected]>
1 parent bb31d99 commit 38c84e2

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-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 prog_fd, int map_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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,27 @@ 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+
/**
744+
* @brief **bpf_prog_assoc_struct_ops** associates a BPF program with a
745+
* struct_ops map.
746+
*
747+
* @param prog_fd FD for the BPF program
748+
* @param map_fd FD for the struct_ops map to be associated with the BPF program
749+
* @param opts optional options, can be NULL
750+
*
751+
* @return 0 on success; negative error code, otherwise (errno is also set to
752+
* the error code)
753+
*/
754+
LIBBPF_API int bpf_prog_assoc_struct_ops(int prog_fd, int map_fd,
755+
struct bpf_prog_assoc_struct_ops_opts *opts);
756+
736757
#ifdef __cplusplus
737758
} /* extern "C" */
738759
#endif

tools/lib/bpf/libbpf.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13891,6 +13891,36 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
1389113891
return 0;
1389213892
}
1389313893

13894+
int bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
13895+
struct bpf_prog_assoc_struct_ops_opts *opts)
13896+
{
13897+
int prog_fd;
13898+
13899+
prog_fd = bpf_program__fd(prog);
13900+
if (prog_fd < 0) {
13901+
pr_warn("prog '%s': can't associate BPF program without FD (was it loaded?)\n",
13902+
prog->name);
13903+
return -EINVAL;
13904+
}
13905+
13906+
if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
13907+
pr_warn("prog '%s': can't associate struct_ops program\n", prog->name);
13908+
return -EINVAL;
13909+
}
13910+
13911+
if (map->fd < 0) {
13912+
pr_warn("map '%s': can't associate BPF map without FD (was it created?)\n", map->name);
13913+
return -EINVAL;
13914+
}
13915+
13916+
if (!bpf_map__is_struct_ops(map)) {
13917+
pr_warn("map '%s': can't associate non-struct_ops map\n", map->name);
13918+
return -EINVAL;
13919+
}
13920+
13921+
return bpf_prog_assoc_struct_ops(prog_fd, map->fd, opts);
13922+
}
13923+
1389413924
int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
1389513925
{
1389613926
int err = 0, n, len, start, end = -1;

tools/lib/bpf/libbpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,22 @@ LIBBPF_API int
10031003
bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
10041004
const char *attach_func_name);
10051005

1006+
struct bpf_prog_assoc_struct_ops_opts; /* defined in bpf.h */
1007+
1008+
/**
1009+
* @brief **bpf_program__assoc_struct_ops()** associates a BPF program with a
1010+
* struct_ops map.
1011+
*
1012+
* @param prog BPF program
1013+
* @param map struct_ops map to be associated with the BPF program
1014+
* @param opts optional options, can be NULL
1015+
*
1016+
* @return error code; or 0 if no error occurred.
1017+
*/
1018+
LIBBPF_API int
1019+
bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
1020+
struct bpf_prog_assoc_struct_ops_opts *opts);
1021+
10061022
/**
10071023
* @brief **bpf_object__find_map_by_name()** returns BPF map of
10081024
* the given name, if it exists within the passed BPF object

tools/lib/bpf/libbpf.map

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,6 @@ LIBBPF_1.7.0 {
451451
global:
452452
bpf_map__set_exclusive_program;
453453
bpf_map__exclusive_program;
454+
bpf_prog_assoc_struct_ops;
455+
bpf_program__assoc_struct_ops;
454456
} LIBBPF_1.6.0;

0 commit comments

Comments
 (0)