* [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API in C++ mode
@ 2022-11-30 20:00 Andrii Nakryiko
2022-11-30 20:00 ` [PATCH bpf-next 2/2] selftests/bpf: make sure enum-less bpf_enable_stats() API works " Andrii Nakryiko
2022-11-30 22:00 ` [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API " patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2022-11-30 20:00 UTC (permalink / raw)
To: bpf, ast, daniel; +Cc: andrii, kernel-team
C++ enum forward declarations are fundamentally not compatible with pure
C enum definitions, and so libbpf's use of `enum bpf_stats_type;`
forward declaration in libbpf/bpf.h public API header is causing C++
compilation issues.
More details can be found in [0], but it comes down to C++ supporting
enum forward declaration only with explicitly specified backing type:
enum bpf_stats_type: int;
In C (and I believe it's a GCC extension also), such forward declaration
is simply:
enum bpf_stats_type;
Further, in Linux UAPI this enum is defined in pure C way:
enum bpf_stats_type { BPF_STATS_RUN_TIME = 0; }
And even though in both cases backing type is int, which can be
confirmed by looking at DWARF information, for C++ compiler actual enum
definition and forward declaration are incompatible.
To eliminate this problem, for C++ mode define input argument as int,
which makes enum unnecessary in libbpf public header. This solves the
issue and as demonstrated by next patch doesn't cause any unwanted
compiler warnings, at least with default warnings setting.
[0] https://stackoverflow.com/questions/42766839/c11-enum-forward-causes-underlying-type-mismatch
[1] Closes: https://github.com/libbpf/libbpf/issues/249
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
tools/lib/bpf/bpf.h | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index a112e0ed1b19..7468978d3c27 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -409,8 +409,15 @@ LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
__u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
__u64 *probe_offset, __u64 *probe_addr);
+#ifdef __cplusplus
+/* forward-declaring enums in C++ isn't compatible with pure C enums, so
+ * instead define bpf_enable_stats() as accepting int as an input
+ */
+LIBBPF_API int bpf_enable_stats(int type);
+#else
enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
+#endif
struct bpf_prog_bind_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH bpf-next 2/2] selftests/bpf: make sure enum-less bpf_enable_stats() API works in C++ mode
2022-11-30 20:00 [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API in C++ mode Andrii Nakryiko
@ 2022-11-30 20:00 ` Andrii Nakryiko
2022-11-30 22:00 ` [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API " patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2022-11-30 20:00 UTC (permalink / raw)
To: bpf, ast, daniel; +Cc: andrii, kernel-team
Just a simple test to make sure we don't introduce unwanted compiler
warnings and API still supports passing enums as input argument.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
tools/testing/selftests/bpf/test_cpp.cpp | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/bpf/test_cpp.cpp b/tools/testing/selftests/bpf/test_cpp.cpp
index 19ad172036da..0bd9990e83fa 100644
--- a/tools/testing/selftests/bpf/test_cpp.cpp
+++ b/tools/testing/selftests/bpf/test_cpp.cpp
@@ -1,9 +1,9 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#include <iostream>
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#include <unistd.h>
+#include <linux/bpf.h>
+#include <linux/btf.h>
#include <bpf/libbpf.h>
-#pragma GCC diagnostic pop
#include <bpf/bpf.h>
#include <bpf/btf.h>
#include "test_core_extern.skel.h"
@@ -99,6 +99,7 @@ int main(int argc, char *argv[])
struct btf_dump_opts opts = { };
struct test_core_extern *skel;
struct btf *btf;
+ int fd;
try_skeleton_template();
@@ -117,6 +118,12 @@ int main(int argc, char *argv[])
skel = test_core_extern__open_and_load();
test_core_extern__destroy(skel);
+ fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
+ if (fd < 0)
+ std::cout << "FAILED to enable stats: " << fd << std::endl;
+ else
+ ::close(fd);
+
std::cout << "DONE!" << std::endl;
return 0;
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API in C++ mode
2022-11-30 20:00 [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API in C++ mode Andrii Nakryiko
2022-11-30 20:00 ` [PATCH bpf-next 2/2] selftests/bpf: make sure enum-less bpf_enable_stats() API works " Andrii Nakryiko
@ 2022-11-30 22:00 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-11-30 22:00 UTC (permalink / raw)
To: Andrii Nakryiko; +Cc: bpf, ast, daniel, kernel-team
Hello:
This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:
On Wed, 30 Nov 2022 12:00:12 -0800 you wrote:
> C++ enum forward declarations are fundamentally not compatible with pure
> C enum definitions, and so libbpf's use of `enum bpf_stats_type;`
> forward declaration in libbpf/bpf.h public API header is causing C++
> compilation issues.
>
> More details can be found in [0], but it comes down to C++ supporting
> enum forward declaration only with explicitly specified backing type:
>
> [...]
Here is the summary with links:
- [bpf-next,1/2] libbpf: avoid enum forward-declarations in public API in C++ mode
https://git.kernel.org/bpf/bpf-next/c/b42693415b86
- [bpf-next,2/2] selftests/bpf: make sure enum-less bpf_enable_stats() API works in C++ mode
https://git.kernel.org/bpf/bpf-next/c/f8186bf65ae6
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-11-30 22:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-30 20:00 [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API in C++ mode Andrii Nakryiko
2022-11-30 20:00 ` [PATCH bpf-next 2/2] selftests/bpf: make sure enum-less bpf_enable_stats() API works " Andrii Nakryiko
2022-11-30 22:00 ` [PATCH bpf-next 1/2] libbpf: avoid enum forward-declarations in public API " patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox