Skip to content

Commit bb8ffe6

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpftool: Add C++-specific open/load/etc skeleton wrappers
Add C++-specific static methods for code-generated BPF skeleton for each skeleton operation: open, open_opts, open_and_load, load, attach, detach, destroy, and elf_bytes. This is to facilitate easier C++ templating on top of pure C BPF skeleton. In C, open/load/destroy/etc "methods" are of the form <skeleton_name>__<method>() to avoid name collision with similar "methods" of other skeletons withint the same application. This works well, but is very inconvenient for C++ applications that would like to write generic (templated) wrappers around BPF skeleton to fit in with C++ code base and take advantage of destructors and other convenient C++ constructs. This patch makes it easier to build such generic templated wrappers by additionally defining C++ static methods for skeleton's struct with fixed names. This allows to refer to, say, open method as `T::open()` instead of having to somehow generate `T__open()` function call. Next patch adds an example template to test_cpp selftest to demonstrate how it's possible to have all the operations wrapped in a generic Skeleton<my_skeleton> type without explicitly passing function references. An example of generated declaration section without %1$s placeholders: #ifdef __cplusplus static struct test_attach_probe *open(const struct bpf_object_open_opts *opts = nullptr); static struct test_attach_probe *open_and_load(); static int load(struct test_attach_probe *skel); static int attach(struct test_attach_probe *skel); static void detach(struct test_attach_probe *skel); static void destroy(struct test_attach_probe *skel); static const void *elf_bytes(size_t *sz); #endif /* __cplusplus */ Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent d3b0b80 commit bb8ffe6

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

tools/bpf/bpftool/gen.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,16 @@ static int do_skeleton(int argc, char **argv)
831831

832832
codegen("\
833833
\n\
834+
\n\
835+
#ifdef __cplusplus \n\
836+
static struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\
837+
static struct %1$s *open_and_load(); \n\
838+
static int load(struct %1$s *skel); \n\
839+
static int attach(struct %1$s *skel); \n\
840+
static void detach(struct %1$s *skel); \n\
841+
static void destroy(struct %1$s *skel); \n\
842+
static const void *elf_bytes(size_t *sz); \n\
843+
#endif /* __cplusplus */ \n\
834844
}; \n\
835845
\n\
836846
static void \n\
@@ -1025,9 +1035,19 @@ static int do_skeleton(int argc, char **argv)
10251035
\"; \n\
10261036
} \n\
10271037
\n\
1028-
#endif /* %s */ \n\
1038+
#ifdef __cplusplus \n\
1039+
struct %1$s *%1$s::open(const struct bpf_object_open_opts *opts) { return %1$s__open_opts(opts); }\n\
1040+
struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); } \n\
1041+
int %1$s::load(struct %1$s *skel) { return %1$s__load(skel); } \n\
1042+
int %1$s::attach(struct %1$s *skel) { return %1$s__attach(skel); } \n\
1043+
void %1$s::detach(struct %1$s *skel) { %1$s__detach(skel); } \n\
1044+
void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); } \n\
1045+
const void *%1$s::elf_bytes(size_t *sz) { return %1$s__elf_bytes(sz); } \n\
1046+
#endif /* __cplusplus */ \n\
1047+
\n\
1048+
#endif /* %2$s */ \n\
10291049
",
1030-
header_guard);
1050+
obj_name, header_guard);
10311051
err = 0;
10321052
out:
10331053
bpf_object__close(obj);

0 commit comments

Comments
 (0)