Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

libbpf: make sure bpf headers are c++ include-able

Wrap headers in extern "C", to turn off C++ mangling.
This simplifies including libbpf in c++ and linking against it.

v2 changes:
* do the same for btf.h

v3 changes:
* test_libbpf.cpp to test for possible future c++ breakages

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

authored by

Stanislav Fomichev and committed by
Daniel Borkmann
8c4905b9 462c124c

+56 -3
+12 -3
tools/lib/bpf/Makefile
··· 66 66 endif 67 67 68 68 FEATURE_USER = .libbpf 69 - FEATURE_TESTS = libelf libelf-mmap bpf reallocarray 69 + FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx 70 70 FEATURE_DISPLAY = libelf bpf 71 71 72 72 INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi ··· 148 148 149 149 CMD_TARGETS = $(LIB_FILE) 150 150 151 + CXX_TEST_TARGET = $(OUTPUT)test_libbpf 152 + 153 + ifeq ($(feature-cxx), 1) 154 + CMD_TARGETS += $(CXX_TEST_TARGET) 155 + endif 156 + 151 157 TARGETS = $(CMD_TARGETS) 152 158 153 159 all: fixdep all_cmd ··· 181 175 $(OUTPUT)libbpf.a: $(BPF_IN) 182 176 $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 183 177 178 + $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a 179 + $(QUIET_LINK)$(CXX) $^ -lelf -o $@ 180 + 184 181 define do_install 185 182 if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 186 183 $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ ··· 210 201 $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 211 202 212 203 clean: 213 - $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so .*.d .*.cmd \ 214 - $(RM) LIBBPF-CFLAGS 204 + $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ 205 + *.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS 215 206 $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf 216 207 217 208
+9
tools/lib/bpf/bpf.h
··· 27 27 #include <stdbool.h> 28 28 #include <stddef.h> 29 29 30 + #ifdef __cplusplus 31 + extern "C" { 32 + #endif 33 + 30 34 #ifndef LIBBPF_API 31 35 #define LIBBPF_API __attribute__((visibility("default"))) 32 36 #endif ··· 136 132 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, 137 133 __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, 138 134 __u64 *probe_offset, __u64 *probe_addr); 135 + 136 + #ifdef __cplusplus 137 + } /* extern "C" */ 138 + #endif 139 + 139 140 #endif /* __LIBBPF_BPF_H */
+8
tools/lib/bpf/btf.h
··· 6 6 7 7 #include <linux/types.h> 8 8 9 + #ifdef __cplusplus 10 + extern "C" { 11 + #endif 12 + 9 13 #ifndef LIBBPF_API 10 14 #define LIBBPF_API __attribute__((visibility("default"))) 11 15 #endif ··· 83 79 int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext, 84 80 const char *sec_name, __u32 insns_cnt, void **func_info, 85 81 __u32 *func_info_len); 82 + 83 + #ifdef __cplusplus 84 + } /* extern "C" */ 85 + #endif 86 86 87 87 #endif /* __LIBBPF_BTF_H */
+9
tools/lib/bpf/libbpf.h
··· 16 16 #include <sys/types.h> // for size_t 17 17 #include <linux/bpf.h> 18 18 19 + #ifdef __cplusplus 20 + extern "C" { 21 + #endif 22 + 19 23 #ifndef LIBBPF_API 20 24 #define LIBBPF_API __attribute__((visibility("default"))) 21 25 #endif ··· 339 335 libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); 340 336 int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, 341 337 libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); 338 + 339 + #ifdef __cplusplus 340 + } /* extern "C" */ 341 + #endif 342 + 342 343 #endif /* __LIBBPF_LIBBPF_H */
+18
tools/lib/bpf/test_libbpf.cpp
··· 1 + /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 + #include "libbpf.h" 3 + #include "bpf.h" 4 + #include "btf.h" 5 + 6 + /* do nothing, just make sure we can link successfully */ 7 + 8 + int main(int argc, char *argv[]) 9 + { 10 + /* libbpf.h */ 11 + libbpf_set_print(NULL, NULL, NULL); 12 + 13 + /* bpf.h */ 14 + bpf_prog_get_fd_by_id(0); 15 + 16 + /* btf.h */ 17 + btf__new(NULL, 0, NULL); 18 + }