Skip to content

Commit

Permalink
Merge branch 'Make BPF skeleton easier to use from C++ code'
Browse files Browse the repository at this point in the history
Andrii Nakryiko says:

====================

Add minimal C++-specific additions to BPF skeleton codegen to facilitate
easier use of C skeletons in C++ applications. These additions don't add any
extra ongoing maintenance and allows C++ users to fit pure C skeleton better
into their C++ code base. All that without the need to design, implement and
support a separate C++ BPF skeleton implementation.

v1->v2:
  - use default argument values in T::open() (Alexei).
====================

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Alexei Starovoitov committed Feb 15, 2022
2 parents d3b0b80 + 189e0ec commit d2b94f3
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
24 changes: 22 additions & 2 deletions tools/bpf/bpftool/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,16 @@ static int do_skeleton(int argc, char **argv)

codegen("\
\n\
\n\
#ifdef __cplusplus \n\
static struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\
static struct %1$s *open_and_load(); \n\
static int load(struct %1$s *skel); \n\
static int attach(struct %1$s *skel); \n\
static void detach(struct %1$s *skel); \n\
static void destroy(struct %1$s *skel); \n\
static const void *elf_bytes(size_t *sz); \n\
#endif /* __cplusplus */ \n\
}; \n\
\n\
static void \n\
Expand Down Expand Up @@ -1025,9 +1035,19 @@ static int do_skeleton(int argc, char **argv)
\"; \n\
} \n\
\n\
#endif /* %s */ \n\
#ifdef __cplusplus \n\
struct %1$s *%1$s::open(const struct bpf_object_open_opts *opts) { return %1$s__open_opts(opts); }\n\
struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); } \n\
int %1$s::load(struct %1$s *skel) { return %1$s__load(skel); } \n\
int %1$s::attach(struct %1$s *skel) { return %1$s__attach(skel); } \n\
void %1$s::detach(struct %1$s *skel) { %1$s__detach(skel); } \n\
void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); } \n\
const void *%1$s::elf_bytes(size_t *sz) { return %1$s__elf_bytes(sz); } \n\
#endif /* __cplusplus */ \n\
\n\
#endif /* %2$s */ \n\
",
header_guard);
obj_name, header_guard);
err = 0;
out:
bpf_object__close(obj);
Expand Down
87 changes: 85 additions & 2 deletions tools/testing/selftests/bpf/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,100 @@
#include <bpf/btf.h>
#include "test_core_extern.skel.h"

/* do nothing, just make sure we can link successfully */
template <typename T>
class Skeleton {
private:
T *skel;
public:
Skeleton(): skel(nullptr) { }

~Skeleton() { if (skel) T::destroy(skel); }

int open(const struct bpf_object_open_opts *opts = nullptr)
{
int err;

if (skel)
return -EBUSY;

skel = T::open(opts);
err = libbpf_get_error(skel);
if (err) {
skel = nullptr;
return err;
}

return 0;
}

int load() { return T::load(skel); }

int attach() { return T::attach(skel); }

void detach() { return T::detach(skel); }

const T* operator->() const { return skel; }

T* operator->() { return skel; }

const T *get() const { return skel; }
};

static void dump_printf(void *ctx, const char *fmt, va_list args)
{
}

static void try_skeleton_template()
{
Skeleton<test_core_extern> skel;
std::string prog_name;
int err;
LIBBPF_OPTS(bpf_object_open_opts, opts);

err = skel.open(&opts);
if (err) {
fprintf(stderr, "Skeleton open failed: %d\n", err);
return;
}

skel->data->kern_ver = 123;
skel->data->int_val = skel->data->ushort_val;

err = skel.load();
if (err) {
fprintf(stderr, "Skeleton load failed: %d\n", err);
return;
}

if (!skel->kconfig->CONFIG_BPF_SYSCALL)
fprintf(stderr, "Seems like CONFIG_BPF_SYSCALL isn't set?!\n");

err = skel.attach();
if (err) {
fprintf(stderr, "Skeleton attach failed: %d\n", err);
return;
}

prog_name = bpf_program__name(skel->progs.handle_sys_enter);
if (prog_name != "handle_sys_enter")
fprintf(stderr, "Unexpected program name: %s\n", prog_name.c_str());

bpf_link__destroy(skel->links.handle_sys_enter);
skel->links.handle_sys_enter = bpf_program__attach(skel->progs.handle_sys_enter);

skel.detach();

/* destructor will destory underlying skeleton */
}

int main(int argc, char *argv[])
{
struct btf_dump_opts opts = { };
struct test_core_extern *skel;
struct btf *btf;

try_skeleton_template();

/* libbpf.h */
libbpf_set_print(NULL);

Expand All @@ -25,7 +107,8 @@ int main(int argc, char *argv[])

/* btf.h */
btf = btf__new(NULL, 0);
btf_dump__new(btf, dump_printf, nullptr, &opts);
if (!libbpf_get_error(btf))
btf_dump__new(btf, dump_printf, nullptr, &opts);

/* BPF skeleton */
skel = test_core_extern__open_and_load();
Expand Down

0 comments on commit d2b94f3

Please sign in to comment.