History

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 */

Andrii Nakryiko committed 4 years ago

Commit bb8ffe61 parent d3b0b801 file · +22 −2Browse files

Unified · Split

tools/bpf/bpftool/gen.c+22−2
@@ -831,6 +831,16 @@ static int do_skeleton(int argc, char **argv) · 830 unmodified lines
831 831
832 codegen("\832 codegen("\
833 \n\833 \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\
834 }; \n\844 }; \n\
835 \n\845 \n\
836 static void \n\846 static void \n\
@@ -1025,9 +1035,19 @@ static int do_skeleton(int argc, char **argv) · 188 unmodified lines
1025 \"; \n\1035 \"; \n\
1026 } \n\1036 } \n\
1027 \n\1037 \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\
1029 ",1049 ",
1030 header_guard);1050 obj_name, header_guard);
1031 err = 0;1051 err = 0;
1032out:1052out:
1033 bpf_object__close(obj);1053 bpf_object__close(obj);