@cryptotaxi247 / netdata-1 / commits / aa6d30384

Move libbpf and eBPF CO-RE bundling into CMake. (#17484)

* Move libbpf and eBPF CO-RE bundling into CMake. * Silence CMP0135 warnings. * Fix handling of legacy eBPF code. * Only enable eBPF by default on Linux. * Correctly auto-detect the need for legacy libbpf. * Fix include paths for libbpf linking. * Add coreutils dependency on Alpine. * Fix ebpf code handling. * Fix lib path handling for libbpf.a. * Correctly fix libbpf lib directory handling. * Use correct comparison type.

Austin S. Hemmelgarn committed Apr 24, 2024 at 09:17 UTC aa6d30384d26c6df33acd67dc8402ae9e3195297
18 files changed +272 -319
CMakeLists.txt
+27 -37
@@ -2,6 +2,10 @@
2
3 cmake_minimum_required(VERSION 3.13.0)
4
5 +if(POLICY CMP0135)
6 + cmake_policy(SET CMP0135 NEW)
7 +endif()
8 +
9 #
10 # version atrocities
11 #
@@ -59,6 +63,7 @@ project(netdata
63 HOMEPAGE_URL "https://www.netdata.cloud"
64 LANGUAGES C CXX)
65 list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/packaging/cmake/Modules")
66 +include(CMakeDependentOption)
67
68 find_package(PkgConfig REQUIRED)
69
@@ -133,6 +138,9 @@ option(ENABLE_LOGS_MANAGEMENT_TESTS "enable logs management tests" True)
138 option(ENABLE_SENTRY "enable sentry" False)
139 option(ENABLE_WEBRTC "enable webrtc" False)
140
141 +cmake_dependent_option(FORCE_LEGACY_LIBBPF "Force usage of libbpf 0.0.9 instead of the latest version." False "ENABLE_PLUGIN_LIBBPF" False)
142 +mark_as_advanced(FORCE_LEGACY_LIBBPF)
143 +
144 if(ENABLE_ACLK OR ENABLE_EXPORTER_PROMETHEUS_REMOTE_WRITE)
145 set(NEED_PROTOBUF True)
146 else()
@@ -236,6 +244,18 @@ else()
244 add_definitions(-D_GNU_SOURCE)
245 endif()
246
247 +if(ENABLE_PLUGIN_EBPF)
248 + include(NetdataLibBPF)
249 + include(NetdataEBPFCORE)
250 +
251 + if(NOT LINUX)
252 + message(FATAL_ERROR "The eBPF plugin is not supported on non-Linux systems")
253 + endif()
254 +
255 + netdata_bundle_libbpf()
256 + netdata_fetch_ebpf_co_re()
257 +endif()
258 +
259 #
260 # Libm
261 #
@@ -657,17 +677,10 @@ set(LIBNETDATA_FILES
677 )
678
679 if(ENABLE_PLUGIN_EBPF)
660 - message(CHECK_START "Checking for vendored libbpf for eBPF plugin")
661 - if (EXISTS "${CMAKE_SOURCE_DIR}/externaldeps/libbpf/libbpf.a" AND EXISTS "${CMAKE_SOURCE_DIR}/src/libnetdata/ebpf/includes/dc.skel.h")
662 - message(CHECK_PASS "found")
663 - list(APPEND LIBNETDATA_FILES
664 - src/libnetdata/ebpf/ebpf.c
665 - src/libnetdata/ebpf/ebpf.h
666 - )
667 - else()
668 - message(CHECK_FAIL "not found")
669 - message(FATAL_ERROR "eBPF plugin requires a vendored copy of libbpf.")
670 - endif()
680 + list(APPEND LIBNETDATA_FILES
681 + src/libnetdata/ebpf/ebpf.c
682 + src/libnetdata/ebpf/ebpf.h
683 + )
684 endif()
685
686 set(LIBH2O_FILES
@@ -1502,16 +1515,7 @@ target_link_libraries(libnetdata PUBLIC
1515
1516 # ebpf
1517 if(ENABLE_PLUGIN_EBPF)
1505 - target_link_libraries(libnetdata PUBLIC ${CMAKE_SOURCE_DIR}/externaldeps/libbpf/libbpf.a)
1506 - target_include_directories(libnetdata BEFORE PUBLIC
1507 - ${CMAKE_SOURCE_DIR}/externaldeps/libbpf/include
1508 - ${CMAKE_SOURCE_DIR}/externaldeps/libbpf/include/uapi
1509 - )
1510 -
1511 - pkg_check_modules(ELF REQUIRED libelf)
1512 - target_include_directories(libnetdata BEFORE PUBLIC ${ELF_INCLUDE_DIRS})
1513 - target_compile_options(libnetdata PUBLIC ${ELF_CFLAGS_OTHER})
1514 - target_link_libraries(libnetdata PUBLIC ${ELF_LIBRARIES})
1518 + netdata_add_libbpf_to_target(libnetdata)
1519 endif()
1520
1521 # judy
@@ -1907,27 +1911,13 @@ if(ENABLE_PLUGIN_EBPF)
1911 src/collectors/ebpf.plugin/ebpf_unittest.h
1912 src/collectors/ebpf.plugin/ebpf_functions.c
1913 src/collectors/ebpf.plugin/ebpf_functions.h
1910 - src/libnetdata/ebpf/includes/cachestat.skel.h
1911 - src/libnetdata/ebpf/includes/dc.skel.h
1912 - src/libnetdata/ebpf/includes/disk.skel.h
1913 - src/libnetdata/ebpf/includes/fd.skel.h
1914 - src/libnetdata/ebpf/includes/filesystem.skel.h
1915 - src/libnetdata/ebpf/includes/hardirq.skel.h
1916 - src/libnetdata/ebpf/includes/mdflush.skel.h
1917 - src/libnetdata/ebpf/includes/mount.skel.h
1918 - src/libnetdata/ebpf/includes/oomkill.skel.h
1919 - src/libnetdata/ebpf/includes/process.skel.h
1920 - src/libnetdata/ebpf/includes/shm.skel.h
1921 - src/libnetdata/ebpf/includes/socket.skel.h
1922 - src/libnetdata/ebpf/includes/softirq.skel.h
1923 - src/libnetdata/ebpf/includes/swap.skel.h
1924 - src/libnetdata/ebpf/includes/sync.skel.h
1925 - src/libnetdata/ebpf/includes/vfs.skel.h
1914 )
1915
1916 add_executable(ebpf.plugin ${EBPF_PLUGIN_FILES})
1917 target_link_libraries(ebpf.plugin libnetdata)
1918
1919 + netdata_add_ebpf_co_re_to_target(ebpf.plugin)
1920 +
1921 install(TARGETS ebpf.plugin
1922 COMPONENT ebpf_plugin
1923 DESTINATION usr/libexec/netdata/plugins.d)
contrib/debian/rules
-4
@@ -64,10 +64,6 @@ override_dh_installinit:
64 dh_installinit
65
66 override_dh_auto_configure:
67 - if [ $(HAVE_EBPF) -eq 1 ]; then \
68 - packaging/bundle-libbpf.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
69 - packaging/bundle-ebpf-co-re.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
70 - fi
67 dh_auto_configure -- -G Ninja \
68 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
69 -DCMAKE_INSTALL_PREFIX=/ \
netdata-installer.sh
+39 -209
@@ -245,13 +245,16 @@ USAGE: ${PROGRAM} [options]
245 HEREDOC
246 }
247
248 +if [ "$(uname -s)" = "Linux" ]; then
249 + ENABLE_EBPF=1
250 +fi
251 +
252 DONOTSTART=0
253 DONOTWAIT=0
254 NETDATA_PREFIX=
255 LIBS_ARE_HERE=0
256 NETDATA_ENABLE_ML=""
257 ENABLE_DBENGINE=1
254 -ENABLE_EBPF=1
258 ENABLE_GO=1
259 ENABLE_H2O=1
260 ENABLE_CLOUD=1
@@ -323,14 +326,8 @@ while [ -n "${1}" ]; do
326 # XXX: No longer supported.
327 ;;
328 "--disable-telemetry") NETDATA_DISABLE_TELEMETRY=1 ;;
326 - "--enable-ebpf")
327 - ENABLE_EBPF=1
328 - NETDATA_DISABLE_EBPF=0
329 - ;;
330 - "--disable-ebpf")
331 - ENABLE_EBPF=0
332 - NETDATA_DISABLE_EBPF=1
333 - ;;
329 + "--enable-ebpf") ENABLE_EBPF=1 ;;
330 + "--disable-ebpf") ENABLE_EBPF=0 ;;
331 "--skip-available-ram-check") SKIP_RAM_CHECK=1 ;;
332 "--one-time-build")
333 # XXX: No longer supported
@@ -556,199 +553,6 @@ fi
553
554 trap build_error EXIT
555
559 -# -----------------------------------------------------------------------------
560 -
561 -get_kernel_version() {
562 - r="$(uname -r | cut -f 1 -d '-')"
563 -
564 - tmpfile="$(mktemp)"
565 - echo "${r}" | tr '.' ' ' > "${tmpfile}"
566 -
567 - read -r maj min patch _ < "${tmpfile}"
568 -
569 - rm -f "${tmpfile}"
570 -
571 - printf "%03d%03d%03d" "${maj}" "${min}" "${patch}"
572 -}
573 -
574 -detect_libc() {
575 - libc=
576 - if ldd --version 2>&1 | grep -q -i glibc; then
577 - echo >&2 " Detected GLIBC"
578 - libc="glibc"
579 - elif ldd --version 2>&1 | grep -q -i 'gnu libc'; then
580 - echo >&2 " Detected GLIBC"
581 - libc="glibc"
582 - elif ldd --version 2>&1 | grep -q -i musl; then
583 - echo >&2 " Detected musl"
584 - libc="musl"
585 - else
586 - cmd=$(ldd /bin/sh | grep -w libc | cut -d" " -f 3)
587 - if bash -c "${cmd}" 2>&1 | grep -q -i "GNU C Library"; then
588 - echo >&2 " Detected GLIBC"
589 - libc="glibc"
590 - fi
591 - fi
592 -
593 - if [ -z "$libc" ]; then
594 - warning "Cannot detect a supported libc on your system, eBPF support will be disabled."
595 - return 1
596 - fi
597 -
598 - echo "${libc}"
599 - return 0
600 -}
601 -
602 -build_libbpf() {
603 - cd "${1}/src" > /dev/null || return 1
604 - mkdir root build
605 - # shellcheck disable=SC2086
606 - run env CFLAGS='-fPIC -pipe' CXXFLAGS='-fPIC -pipe' LDFLAGS= BUILD_STATIC_ONLY=y OBJDIR=build DESTDIR=.. ${make} ${MAKEOPTS} install
607 - cd - > /dev/null || return 1
608 -}
609 -
610 -copy_libbpf() {
611 - target_dir="${PWD}/externaldeps/libbpf"
612 -
613 - if [ "$(uname -m)" = x86_64 ]; then
614 - lib_subdir="lib64"
615 - else
616 - lib_subdir="lib"
617 - fi
618 -
619 - run mkdir -p "${target_dir}" || return 1
620 -
621 - run cp "${1}/usr/${lib_subdir}/libbpf.a" "${target_dir}/libbpf.a" || return 1
622 - run cp -r "${1}/usr/include" "${target_dir}" || return 1
623 - run cp -r "${1}/include/uapi" "${target_dir}/include" || return 1
624 -}
625 -
626 -bundle_libbpf() {
627 - if { [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 1 ]; } || [ "$(uname -s)" != Linux ]; then
628 - ENABLE_EBPF=0
629 - NETDATA_DISABLE_EBPF=1
630 - return 0
631 - fi
632 -
633 - if [ -z "${make}" ]; then
634 - warning "No usable copy of Make found, which is required to bundle libbpf. Disabling eBPF support."
635 - ENABLE_EBPF=0
636 - NETDATA_DISABLE_EBPF=1
637 - return 0
638 - fi
639 -
640 - # When libc is not detected, we do not have necessity to compile libbpf and we should not do download of eBPF programs
641 - libc="${EBPF_LIBC:-"$(detect_libc)"}"
642 - if [ -z "$libc" ]; then
643 - NETDATA_DISABLE_EBPF=1
644 - ENABLE_EBPF=0
645 - return 0
646 - fi
647 -
648 - [ -n "${GITHUB_ACTIONS}" ] && echo "::group::Bundling libbpf."
649 -
650 - progress "Prepare libbpf"
651 -
652 - if [ "$(get_kernel_version)" -ge "004014000" ]; then
653 - LIBBPF_PACKAGE_VERSION="$(cat packaging/current_libbpf.version)"
654 - LIBBPF_PACKAGE_COMPONENT="current_libbpf"
655 - else
656 - LIBBPF_PACKAGE_VERSION="$(cat packaging/libbpf_0_0_9.version)"
657 - LIBBPF_PACKAGE_COMPONENT="libbpf_0_0_9"
658 - fi
659 -
660 - tmp="$(mktemp -d -t netdata-libbpf-XXXXXX)"
661 - LIBBPF_PACKAGE_BASENAME="v${LIBBPF_PACKAGE_VERSION}.tar.gz"
662 -
663 - if fetch_and_verify "${LIBBPF_PACKAGE_COMPONENT}" \
664 - "https://github.com/netdata/libbpf/archive/${LIBBPF_PACKAGE_BASENAME}" \
665 - "${LIBBPF_PACKAGE_BASENAME}" \
666 - "${tmp}" \
667 - "${NETDATA_LOCAL_TARBALL_OVERRIDE_LIBBPF}"; then
668 - if run tar --no-same-owner -xf "${tmp}/${LIBBPF_PACKAGE_BASENAME}" -C "${tmp}" &&
669 - build_libbpf "${tmp}/libbpf-${LIBBPF_PACKAGE_VERSION}" &&
670 - copy_libbpf "${tmp}/libbpf-${LIBBPF_PACKAGE_VERSION}" &&
671 - rm -rf "${tmp}"; then
672 - run_ok "libbpf built and prepared."
673 - ENABLE_EBPF=1
674 - else
675 - if [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 0 ]; then
676 - fatal "failed to build libbpf." I0005
677 - else
678 - run_failed "Failed to build libbpf. eBPF support will be disabled"
679 - ENABLE_EBPF=0
680 - NETDATA_DISABLE_EBPF=1
681 - fi
682 - fi
683 - else
684 - if [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 0 ]; then
685 - fatal "Failed to fetch sources for libbpf." I0006
686 - else
687 - run_failed "Unable to fetch sources for libbpf. eBPF support will be disabled"
688 - ENABLE_EBPF=0
689 - NETDATA_DISABLE_EBPF=1
690 - fi
691 - fi
692 -
693 - [ -n "${GITHUB_ACTIONS}" ] && echo "::endgroup::"
694 -}
695 -
696 -bundle_libbpf
697 -
698 -copy_co_re() {
699 - cp -R "${1}/includes" "src/libnetdata/ebpf/"
700 -}
701 -
702 -bundle_ebpf_co_re() {
703 - if { [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 1 ]; } || [ "$(uname -s)" != Linux ]; then
704 - return 0
705 - fi
706 -
707 - [ -n "${GITHUB_ACTIONS}" ] && echo "::group::Bundling libbpf."
708 -
709 - progress "eBPF CO-RE"
710 -
711 - CORE_PACKAGE_VERSION="$(cat packaging/ebpf-co-re.version)"
712 -
713 - tmp="$(mktemp -d -t netdata-ebpf-co-re-XXXXXX)"
714 - CORE_PACKAGE_BASENAME="netdata-ebpf-co-re-glibc-${CORE_PACKAGE_VERSION}.tar.xz"
715 -
716 - if fetch_and_verify "ebpf-co-re" \
717 - "https://github.com/netdata/ebpf-co-re/releases/download/${CORE_PACKAGE_VERSION}/${CORE_PACKAGE_BASENAME}" \
718 - "${CORE_PACKAGE_BASENAME}" \
719 - "${tmp}" \
720 - "${NETDATA_LOCAL_TARBALL_OVERRIDE_CORE}"; then
721 - if run tar --no-same-owner -xf "${tmp}/${CORE_PACKAGE_BASENAME}" -C "${tmp}" &&
722 - copy_co_re "${tmp}" &&
723 - rm -rf "${tmp}"; then
724 - run_ok "libbpf built and prepared."
725 - ENABLE_EBPF=1
726 - else
727 - if [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 0 ]; then
728 - fatal "Failed to get eBPF CO-RE files." I0007
729 - else
730 - run_failed "Failed to get eBPF CO-RE files. eBPF support will be disabled"
731 - NETDATA_DISABLE_EBPF=1
732 - ENABLE_EBPF=0
733 - enable_feature PLUGIN_EBPF 0
734 - fi
735 - fi
736 - else
737 - if [ -n "${NETDATA_DISABLE_EBPF}" ] && [ "${NETDATA_DISABLE_EBPF}" = 0 ]; then
738 - fatal "Failed to fetch eBPF CO-RE files." I0008
739 - else
740 - run_failed "Failed to fetch eBPF CO-RE files. eBPF support will be disabled"
741 - NETDATA_DISABLE_EBPF=1
742 - ENABLE_EBPF=0
743 - enable_feature PLUGIN_EBPF 0
744 - fi
745 - fi
746 -
747 - [ -n "${GITHUB_ACTIONS}" ] && echo "::endgroup::"
748 -}
749 -
750 -bundle_ebpf_co_re
751 -
556 # -----------------------------------------------------------------------------
557 build_fluentbit() {
558 env_cmd="env CFLAGS='-w' CXXFLAGS='-w' LDFLAGS="
@@ -1249,15 +1053,41 @@ fi
1053
1054 [ -n "${GITHUB_ACTIONS}" ] && echo "::endgroup::"
1055
1056 +detect_libc() {
1057 + libc=
1058 + if ldd --version 2>&1 | grep -q -i glibc; then
1059 + echo >&2 " Detected GLIBC"
1060 + libc="glibc"
1061 + elif ldd --version 2>&1 | grep -q -i 'gnu libc'; then
1062 + echo >&2 " Detected GLIBC"
1063 + libc="glibc"
1064 + elif ldd --version 2>&1 | grep -q -i musl; then
1065 + echo >&2 " Detected musl"
1066 + libc="musl"
1067 + else
1068 + cmd=$(ldd /bin/sh | grep -w libc | cut -d" " -f 3)
1069 + if bash -c "${cmd}" 2>&1 | grep -q -i "GNU C Library"; then
1070 + echo >&2 " Detected GLIBC"
1071 + libc="glibc"
1072 + fi
1073 + fi
1074 +
1075 + if [ -z "$libc" ]; then
1076 + warning "Cannot detect a supported libc on your system, eBPF support will be disabled."
1077 + return 1
1078 + fi
1079 +
1080 + echo "${libc}"
1081 +}
1082 +
1083 should_install_ebpf() {
1253 - if [ "${NETDATA_DISABLE_EBPF:=0}" -eq 1 ]; then
1254 - run_failed "eBPF has been explicitly disabled, it will not be available in this install."
1084 + if [ "${ENABLE_EBPF:-0}" -eq 0 ]; then
1085 return 1
1086 fi
1087
1258 - if [ "$(uname -s)" != "Linux" ] || [ "$(uname -m)" != "x86_64" ]; then
1259 - if [ "${NETDATA_DISABLE_EBPF:=1}" -eq 0 ]; then
1260 - run_failed "Currently eBPF is only supported on Linux on X86_64."
1088 + if [ "$(uname -m)" != "x86_64" ]; then
1089 + if [ "${ENABLE_EBPF:-0}" -eq 1 ]; then
1090 + run_failed "Currently eBPF is only supported on Linux on x86_64."
1091 fi
1092
1093 return 1
@@ -1328,10 +1158,10 @@ install_ebpf() {
1158
1159 remove_old_ebpf
1160
1331 - progress "Installing eBPF plugin"
1161 + progress "Installing eBPF programs"
1162
1163 # Detect libc
1334 - libc="${EBPF_LIBC:-"$(detect_libc)"}"
1164 + libc="$(detect_libc)"
1165
1166 EBPF_VERSION="$(cat packaging/ebpf.version)"
1167 EBPF_TARBALL="netdata-kernel-collector-${libc}-${EBPF_VERSION}.tar.xz"
netdata.spec.in
-14
@@ -315,20 +315,6 @@ happened, on your systems and applications.
315
316 %prep
317 %setup -q -n "%{name}-%{version}"
318 -%if 0%{?_have_ebpf}
319 -%if 0%{?centos_ver:1}
320 -%if %{centos_ver} < 8
321 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-libbpf.sh ${RPM_BUILD_DIR}/%{name}-%{version} centos7
322 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-ebpf-co-re.sh ${RPM_BUILD_DIR}/%{name}-%{version}
323 -%else
324 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-libbpf.sh ${RPM_BUILD_DIR}/%{name}-%{version} centos8
325 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-ebpf-co-re.sh ${RPM_BUILD_DIR}/%{name}-%{version}
326 -%endif
327 -%else
328 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-libbpf.sh ${RPM_BUILD_DIR}/%{name}-%{version} other
329 -export CFLAGS="${CFLAGS} -fPIC" && ${RPM_BUILD_DIR}/%{name}-%{version}/packaging/bundle-ebpf-co-re.sh ${RPM_BUILD_DIR}/%{name}-%{version}
330 -%endif
331 -%endif
318
319 %build
320 # Conf step
packaging/bundle-ebpf-co-re.sh deleted
-9
@@ -1,9 +0,0 @@
1 -#!/bin/sh
2 -
3 -SRCDIR="${1}"
4 -
5 -CORE_VERSION="$(cat "${SRCDIR}/packaging/ebpf-co-re.version")"
6 -CORE_TARBALL="netdata-ebpf-co-re-glibc-${CORE_VERSION}.tar.xz"
7 -curl -sSL --connect-timeout 10 --retry 3 "https://github.com/netdata/ebpf-co-re/releases/download/${CORE_VERSION}/${CORE_TARBALL}" > "${CORE_TARBALL}" || exit 1
8 -grep "${CORE_TARBALL}" "${SRCDIR}/packaging/ebpf-co-re.checksums" | sha256sum -c - || exit 1
9 -tar -xa --no-same-owner -f "${CORE_TARBALL}" -C "${SRCDIR}/src/libnetdata/ebpf" || exit 1
packaging/bundle-libbpf.sh deleted
-27
@@ -1,27 +0,0 @@
1 -#!/bin/bash
2 -
3 -if [ "$(uname -m)" = x86_64 ]; then
4 - lib_subdir="lib64"
5 -else
6 - lib_subdir="lib"
7 -fi
8 -
9 -if [ "${2}" != "centos7" ]; then
10 - cp "${1}/packaging/current_libbpf.checksums" "${1}/packaging/libbpf.checksums"
11 - cp "${1}/packaging/current_libbpf.version" "${1}/packaging/libbpf.version"
12 -else
13 - cp "${1}/packaging/libbpf_0_0_9.checksums" "${1}/packaging/libbpf.checksums"
14 - cp "${1}/packaging/libbpf_0_0_9.version" "${1}/packaging/libbpf.version"
15 -fi
16 -
17 -LIBBPF_TARBALL="v$(cat "${1}/packaging/libbpf.version").tar.gz"
18 -LIBBPF_BUILD_PATH="${1}/externaldeps/libbpf/libbpf-$(cat "${1}/packaging/libbpf.version")"
19 -
20 -mkdir -p "${1}/externaldeps/libbpf" || exit 1
21 -curl -sSL --connect-timeout 10 --retry 3 "https://github.com/netdata/libbpf/archive/${LIBBPF_TARBALL}" > "${LIBBPF_TARBALL}" || exit 1
22 -sha256sum -c "${1}/packaging/libbpf.checksums" || exit 1
23 -tar -xz --no-same-owner -f "${LIBBPF_TARBALL}" -C "${1}/externaldeps/libbpf" || exit 1
24 -make -C "${LIBBPF_BUILD_PATH}/src" BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install || exit 1
25 -cp -r "${LIBBPF_BUILD_PATH}/usr/${lib_subdir}/libbpf.a" "${1}/externaldeps/libbpf" || exit 1
26 -cp -r "${LIBBPF_BUILD_PATH}/usr/include" "${1}/externaldeps/libbpf" || exit 1
27 -cp -r "${LIBBPF_BUILD_PATH}/include/uapi" "${1}/externaldeps/libbpf/include" || exit 1
packaging/cmake/Modules/NetdataEBPFCORE.cmake new
+27
@@ -0,0 +1,27 @@
1 +# Handling for eBPF CO-RE files
2 +#
3 +# Copyright (c) 2024 Netdata Inc.
4 +# SPDX-License-Identifier: GPL-3.0-or-later
5 +
6 +include(ExternalProject)
7 +
8 +set(ebpf-co-re_SOURCE_DIR "${CMAKE_BINARY_DIR}/ebpf-co-re")
9 +
10 +# Fetch and install our eBPF CO-RE files
11 +function(netdata_fetch_ebpf_co_re)
12 + ExternalProject_Add(
13 + ebpf-co-re
14 + URL https://github.com/netdata/ebpf-co-re/releases/download/v1.4.0/netdata-ebpf-co-re-glibc-v1.4.0.tar.xz
15 + URL_HASH SHA256=e2283d6e78961c18e964666ee3468492a28454c9e60945027516bdd45e3f4395
16 + SOURCE_DIR "${ebpf-co-re_SOURCE_DIR}"
17 + CONFIGURE_COMMAND ""
18 + BUILD_COMMAND ""
19 + INSTALL_COMMAND ""
20 + EXCLUDE_FROM_ALL 1
21 + )
22 +endfunction()
23 +
24 +function(netdata_add_ebpf_co_re_to_target _target)
25 + add_dependencies(${_target} ebpf-co-re)
26 + target_include_directories(${_target} BEFORE PRIVATE "${ebpf-co-re_SOURCE_DIR}")
27 +endfunction()
packaging/cmake/Modules/NetdataLibBPF.cmake new
+125
@@ -0,0 +1,125 @@
1 +# Handling for libbpf (used by the eBPF plugin)
2 +#
3 +# Copyright (c) 2024 Netdata Inc.
4 +# SPDX-License-Identifier: GPL-3.0-or-later
5 +
6 +include(ExternalProject)
7 +include(NetdataUtil)
8 +
9 +set(libbpf_SOURCE_DIR "${CMAKE_BINARY_DIR}/libbpf")
10 +
11 +# Check what libc we're using.
12 +#
13 +# Sets the specified variable to the name of the libc or "unknown"
14 +function(netdata_identify_libc _libc_name)
15 + message(INFO "Detecting libc implementation")
16 +
17 + execute_process(COMMAND ldd --version
18 + COMMAND grep -q -i -E "glibc|gnu libc"
19 + RESULT_VARIABLE LDD_IS_GLIBC
20 + OUTPUT_VARIABLE LDD_OUTPUT
21 + ERROR_VARIABLE LDD_OUTPUT)
22 +
23 + if(LDD_IS_GLIBC)
24 + set(${_libc_name} glibc PARENT_SCOPE)
25 + return()
26 + endif()
27 +
28 + execute_process(COMMAND ldd --version
29 + COMMAND grep -q -i -E "musl"
30 + RESULT_VARIABLE LDD_IS_MUSL
31 + OUTPUT_VARIABLE LDD_OUTPUT
32 + ERROR_VARIABLE LDD_OUTPUT)
33 +
34 + if(LDD_IS_MUSL)
35 + set(${_libc_name} musl PARENT_SCOPE)
36 + return()
37 + endif()
38 +
39 + set(${_libc_name} unknown PARENT_SCOPE)
40 +endfunction()
41 +
42 +# Check if the kernel is old enough that we need to use a legacy copy of eBPF.
43 +function(_need_legacy_libbpf _var)
44 + if(FORCE_LEGACY_LIBBPF)
45 + set(${_var} TRUE PARENT_SCOPE)
46 + return()
47 + endif()
48 +
49 + netdata_detect_host_kernel_version()
50 +
51 + if(HOST_KERNEL_VERSION VERSION_LESS "4.14.0")
52 + set(${_var} TRUE PARENT_SCOPE)
53 + else()
54 + set(${_var} FALSE PARENT_SCOPE)
55 + endif()
56 +endfunction()
57 +
58 +# Prepare a vendored copy of libbpf
59 +function(netdata_bundle_libbpf)
60 + _need_legacy_libbpf(USE_LEGACY_LIBBPF)
61 +
62 + if(USE_LEGACY_LIBBPF)
63 + set(_libbpf_tag 673424c56127bb556e64095f41fd60c26f9083ec) # v0.0.9_netdata-1
64 + else()
65 + set(_libbpf_tag b981a3a138e3a30024e4e143d62cff2dc307121e) # v1.4.0p_netdata
66 + endif()
67 +
68 + netdata_identify_libc(_libc)
69 +
70 + string(REGEX MATCH "glibc|musl" _libc_supported "${_libc}")
71 +
72 + if(NOT _libc_supported)
73 + message(FATAL_ERROR "This system’s libc (detected: ${_libc}) is not not supported by the eBPF plugin.")
74 + endif()
75 +
76 + find_program(MAKE_COMMAND make)
77 +
78 + if(MAKE_COMMAND STREQUAL MAKE_COMMAND-NOTFOUND)
79 + message(FATAL_ERROR "GNU Make is required when building the eBPF plugin, but could not be found.")
80 + endif()
81 +
82 + pkg_check_modules(ELF REQUIRED libelf)
83 + pkg_check_modules(ZLIB REQUIRED zlib)
84 +
85 + set(_libbpf_lib_dir lib)
86 +
87 + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
88 + set(_libbpf_lib_dir lib64)
89 + endif()
90 +
91 + set(_libbpf_library "${libbpf_SOURCE_DIR}/usr/${_libbpf_lib_dir}/libbpf.a")
92 +
93 + ExternalProject_Add(
94 + libbpf
95 + GIT_REPOSITORY https://github.com/netdata/libbpf.git
96 + GIT_TAG ${_libbpf_tag}
97 + SOURCE_DIR "${libbpf_SOURCE_DIR}"
98 + CONFIGURE_COMMAND ""
99 + BUILD_COMMAND ${MAKE_COMMAND} -C src BUILD_STATIC_ONLY=1 OBJDIR=build/ DESTDIR=../ install
100 + BUILD_IN_SOURCE 1
101 + BUILD_BYPRODUCTS "${_libbpf_library}"
102 + INSTALL_COMMAND ""
103 + EXCLUDE_FROM_ALL 1
104 + )
105 +
106 + add_library(libbpf_library STATIC IMPORTED GLOBAL)
107 + set_property(
108 + TARGET libbpf_library
109 + PROPERTY IMPORTED_LOCATION "${_libbpf_library}"
110 + )
111 + set_property(
112 + TARGET libbpf_library
113 + PROPERTY INTERFACE_LINK_LIBRARIES "${ELF_LIBRARIES};${ZLIB_LIBRARIES}"
114 + )
115 + set(NETDATA_LIBBPF_INCLUDE_DIRECTORIES "${libbpf_SOURCE_DIR}/usr/include;${libbpf_SOURCE_DIR}/include;${ELF_INCLUDE_DIRECTORIES};${ZLIB_INCLUDE_DIRECTORIES}" PARENT_SCOPE)
116 + set(NETDATA_LIBBPF_COMPILE_DEFINITIONS "${ELF_CFLAGS_OTHER};${ZLIB_CFLAGS_OTHER}" PARENT_SCOPE)
117 +endfunction()
118 +
119 +# Add libbpf as a link dependency for the given target.
120 +function(netdata_add_libbpf_to_target _target)
121 + target_link_libraries(${_target} PUBLIC libbpf_library)
122 + target_include_directories(${_target} BEFORE PUBLIC "${NETDATA_LIBBPF_INCLUDE_DIRECTORIES}")
123 + target_compile_definitions(${_target} PUBLIC "${NETDATA_LIBBPF_COMPILE_DEFINITIONS}")
124 + add_dependencies(${_target} libbpf)
125 +endfunction()
packaging/cmake/Modules/NetdataUtil.cmake new
+34
@@ -0,0 +1,34 @@
1 +# Utility functions used by other modules.
2 +#
3 +# Copyright (c) 2024 Netdata Inc.
4 +# SPDX-License-Identifier: GPL-3.0-or-later
5 +
6 +include_guard()
7 +
8 +# Determine the version of the host kernel.
9 +#
10 +# Only works on UNIX-like systems, stores the version in the cache
11 +# variable HOST_KERNEL_VERSION.
12 +function(netdata_detect_host_kernel_version)
13 + if(DEFINED HOST_KERNEL_VERSION)
14 + if(NOT DEFINED CACHE HOST_KERNEL_VERSION)
15 + message(STATUS "Using user supplied kernel version (${HOST_KERNEL_VERSION}) instead of detecting it.")
16 + endif()
17 + endif()
18 +
19 + message(CHECK_START "Determining host kernel version")
20 +
21 + execute_process(COMMAND uname -r
22 + RESULT_VARIABLE _uname_result
23 + OUTPUT_VARIABLE _uname_output)
24 +
25 + if(NOT _uname_result)
26 + message(CHECK_FAIL "unknown")
27 + set(HOST_KERNEL_VERSION "0.0.0" CACHE STRING "Detected host kernel version")
28 + return()
29 + endif()
30 +
31 + string(REGEX REPLACE "-.+$" "" _kversion "${_uname_output}")
32 + message(CHECK_PASS "${_kversion}")
33 + set(HOST_KERNEL_VERSION "${_kversion}" CACHE STRING "Detected host kernel version")
34 +endfunction()
packaging/current_libbpf.checksums deleted
-1
@@ -1 +0,0 @@
1 -ee11746df46d5cb7f828ea3fea641d98ed8e3784d8505fbcd337767c31cc0e3e v1.4.0p_netdata.tar.gz
packaging/current_libbpf.version deleted
-1
@@ -1 +0,0 @@
1 -1.4.0p_netdata
packaging/ebpf-co-re.checksums deleted
-1
@@ -1 +0,0 @@
1 -e2283d6e78961c18e964666ee3468492a28454c9e60945027516bdd45e3f4395 netdata-ebpf-co-re-glibc-v1.4.0.tar.xz
packaging/ebpf-co-re.version deleted
-1
@@ -1 +0,0 @@
1 -v1.4.0
packaging/installer/dependencies/alpine.sh
+1
@@ -9,6 +9,7 @@ DONT_WAIT=0
9
10 package_tree="
11 alpine-sdk
12 + coreutils
13 git
14 gcc
15 g++
packaging/installer/install-required-packages.sh
+6
@@ -626,6 +626,11 @@ declare -A pkg_distro_sdk=(
626 ['default']="NOTREQUIRED"
627 )
628
629 +declare -A pkg_coreutils=(
630 + ['alpine']="coreutils"
631 + ['default']="NOTREQUIRED"
632 +)
633 +
634 declare -A pkg_autoconf=(
635 ['gentoo']="sys-devel/autoconf"
636 ['clearlinux']="c-basic"
@@ -1228,6 +1233,7 @@ packages() {
1233 # basic build environment
1234
1235 suitable_package distro-sdk
1236 + suitable_package coreutils
1237 suitable_package libatomic
1238
1239 require_cmd git || suitable_package git
packaging/libbpf_0_0_9.checksums deleted
-1
@@ -1 +0,0 @@
1 -fc33402ba33c8f8c5aa18afbb86a9932965886f2906c50e8f2110a1a2126e3ee v0.0.9_netdata-1.tar.gz
packaging/libbpf_0_0_9.version deleted
-1
@@ -1 +0,0 @@
1 -0.0.9_netdata-1
src/collectors/ebpf.plugin/ebpf.h
+13 -13
@@ -38,19 +38,19 @@
38 #define NETDATA_EBPF_CONFIG_FILE "ebpf.d.conf"
39
40 #ifdef LIBBPF_MAJOR_VERSION // BTF code
41 -#include "libnetdata/ebpf/includes/cachestat.skel.h"
42 -#include "libnetdata/ebpf/includes/dc.skel.h"
43 -#include "libnetdata/ebpf/includes/disk.skel.h"
44 -#include "libnetdata/ebpf/includes/fd.skel.h"
45 -#include "libnetdata/ebpf/includes/filesystem.skel.h"
46 -#include "libnetdata/ebpf/includes/hardirq.skel.h"
47 -#include "libnetdata/ebpf/includes/mdflush.skel.h"
48 -#include "libnetdata/ebpf/includes/mount.skel.h"
49 -#include "libnetdata/ebpf/includes/shm.skel.h"
50 -#include "libnetdata/ebpf/includes/sync.skel.h"
51 -#include "libnetdata/ebpf/includes/socket.skel.h"
52 -#include "libnetdata/ebpf/includes/swap.skel.h"
53 -#include "libnetdata/ebpf/includes/vfs.skel.h"
41 +#include "cachestat.skel.h"
42 +#include "dc.skel.h"
43 +#include "disk.skel.h"
44 +#include "fd.skel.h"
45 +#include "filesystem.skel.h"
46 +#include "hardirq.skel.h"
47 +#include "mdflush.skel.h"
48 +#include "mount.skel.h"
49 +#include "shm.skel.h"
50 +#include "sync.skel.h"
51 +#include "socket.skel.h"
52 +#include "swap.skel.h"
53 +#include "vfs.skel.h"
54
55 extern struct cachestat_bpf *cachestat_bpf_obj;
56 extern struct dc_bpf *dc_bpf_obj;