Skip to content

Commit 87cd177

Browse files
ameryhunganakryiko
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]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b5709f6 commit 87cd177

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14133,6 +14133,37 @@ int bpf_program__set_attach_target(struct bpf_program *prog,
1413314133
return 0;
1413414134
}
1413514135

14136+
int bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
14137+
struct bpf_prog_assoc_struct_ops_opts *opts)
14138+
{
14139+
int prog_fd, map_fd;
14140+
14141+
prog_fd = bpf_program__fd(prog);
14142+
if (prog_fd < 0) {
14143+
pr_warn("prog '%s': can't associate BPF program without FD (was it loaded?)\n",
14144+
prog->name);
14145+
return libbpf_err(-EINVAL);
14146+
}
14147+
14148+
if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
14149+
pr_warn("prog '%s': can't associate struct_ops program\n", prog->name);
14150+
return libbpf_err(-EINVAL);
14151+
}
14152+
14153+
map_fd = bpf_map__fd(map);
14154+
if (map_fd < 0) {
14155+
pr_warn("map '%s': can't associate BPF map without FD (was it created?)\n", map->name);
14156+
return libbpf_err(-EINVAL);
14157+
}
14158+
14159+
if (!bpf_map__is_struct_ops(map)) {
14160+
pr_warn("map '%s': can't associate non-struct_ops map\n", map->name);
14161+
return libbpf_err(-EINVAL);
14162+
}
14163+
14164+
return bpf_prog_assoc_struct_ops(prog_fd, map_fd, opts);
14165+
}
14166+
1413614167
int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
1413714168
{
1413814169
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
@@ -1006,6 +1006,22 @@ LIBBPF_API int
10061006
bpf_program__set_attach_target(struct bpf_program *prog, int attach_prog_fd,
10071007
const char *attach_func_name);
10081008

1009+
struct bpf_prog_assoc_struct_ops_opts; /* defined in bpf.h */
1010+
1011+
/**
1012+
* @brief **bpf_program__assoc_struct_ops()** associates a BPF program with a
1013+
* struct_ops map.
1014+
*
1015+
* @param prog BPF program
1016+
* @param map struct_ops map to be associated with the BPF program
1017+
* @param opts optional options, can be NULL
1018+
*
1019+
* @return 0, on success; negative error code, otherwise
1020+
*/
1021+
LIBBPF_API int
1022+
bpf_program__assoc_struct_ops(struct bpf_program *prog, struct bpf_map *map,
1023+
struct bpf_prog_assoc_struct_ops_opts *opts);
1024+
10091025
/**
10101026
* @brief **bpf_object__find_map_by_name()** returns BPF map of
10111027
* 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)