Skip to content

Commit 590a008

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: libbpf: Add STRUCT_OPS support
This patch adds BPF STRUCT_OPS support to libbpf. The only sec_name convention is SEC(".struct_ops") to identify the struct_ops implemented in BPF, e.g. To implement a tcp_congestion_ops: SEC(".struct_ops") struct tcp_congestion_ops dctcp = { .init = (void *)dctcp_init, /* <-- a bpf_prog */ /* ... some more func prts ... */ .name = "bpf_dctcp", }; Each struct_ops is defined as a global variable under SEC(".struct_ops") as above. libbpf creates a map for each variable and the variable name is the map's name. Multiple struct_ops is supported under SEC(".struct_ops"). In the bpf_object__open phase, libbpf will look for the SEC(".struct_ops") section and find out what is the btf-type the struct_ops is implementing. Note that the btf-type here is referring to a type in the bpf_prog.o's btf. A "struct bpf_map" is added by bpf_object__add_map() as other maps do. It will then collect (through SHT_REL) where are the bpf progs that the func ptrs are referring to. No btf_vmlinux is needed in the open phase. In the bpf_object__load phase, the map-fields, which depend on the btf_vmlinux, are initialized (in bpf_map__init_kern_struct_ops()). It will also set the prog->type, prog->attach_btf_id, and prog->expected_attach_type. Thus, the prog's properties do not rely on its section name. [ Currently, the bpf_prog's btf-type ==> btf_vmlinux's btf-type matching process is as simple as: member-name match + btf-kind match + size match. If these matching conditions fail, libbpf will reject. The current targeting support is "struct tcp_congestion_ops" which most of its members are function pointers. The member ordering of the bpf_prog's btf-type can be different from the btf_vmlinux's btf-type. ] Then, all obj->maps are created as usual (in bpf_object__create_maps()). Once the maps are created and prog's properties are all set, the libbpf will proceed to load all the progs. bpf_map__attach_struct_ops() is added to register a struct_ops map to a kernel subsystem. Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 17328d6 commit 590a008

File tree

6 files changed

+661
-13
lines changed

6 files changed

+661
-13
lines changed

tools/lib/bpf/bpf.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
9595
attr.btf_key_type_id = create_attr->btf_key_type_id;
9696
attr.btf_value_type_id = create_attr->btf_value_type_id;
9797
attr.map_ifindex = create_attr->map_ifindex;
98-
attr.inner_map_fd = create_attr->inner_map_fd;
98+
if (attr.map_type == BPF_MAP_TYPE_STRUCT_OPS)
99+
attr.btf_vmlinux_value_type_id =
100+
create_attr->btf_vmlinux_value_type_id;
101+
else
102+
attr.inner_map_fd = create_attr->inner_map_fd;
99103

100104
return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
101105
}
@@ -228,7 +232,9 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
228232
memset(&attr, 0, sizeof(attr));
229233
attr.prog_type = load_attr->prog_type;
230234
attr.expected_attach_type = load_attr->expected_attach_type;
231-
if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
235+
if (attr.prog_type == BPF_PROG_TYPE_STRUCT_OPS) {
236+
attr.attach_btf_id = load_attr->attach_btf_id;
237+
} else if (attr.prog_type == BPF_PROG_TYPE_TRACING) {
232238
attr.attach_btf_id = load_attr->attach_btf_id;
233239
attr.attach_prog_fd = load_attr->attach_prog_fd;
234240
} else {

tools/lib/bpf/bpf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ struct bpf_create_map_attr {
4646
__u32 btf_key_type_id;
4747
__u32 btf_value_type_id;
4848
__u32 map_ifindex;
49-
__u32 inner_map_fd;
49+
union {
50+
__u32 inner_map_fd;
51+
__u32 btf_vmlinux_value_type_id;
52+
};
5053
};
5154

5255
LIBBPF_API int

0 commit comments

Comments
 (0)