Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
72f9e55
build(userspace/libscap): libbpf cmake defs
fntlnz Jan 26, 2021
927da46
update(driver/bpf): adapt bpf driver code to use libbpf
fntlnz Jan 29, 2021
4c0cea8
update(userspace/libscap): adapt libscap scap-bpf to use libbpf
fntlnz Jan 29, 2021
96bf48d
build: include libbpf
fntlnz Jan 29, 2021
be910b5
update(driver/bpf): define kernel flags that are not available for
fntlnz Feb 2, 2021
81228e8
update(driver/bpf): redefine read calls to use co-re when available
fntlnz Feb 3, 2021
a5384fe
update(driver/bpf): make the necessary arrangements to include
fntlnz Feb 3, 2021
8224b64
update(driver/bpf): specify sunaddr only for btf
fntlnz Feb 4, 2021
d9b66bf
update(driver/bpf): divide the btf build from the btf-core build
fntlnz Feb 4, 2021
c07591a
update(driver): formatting and explaination for BPF targets
fntlnz Feb 4, 2021
a8ee9aa
update(driver/bpf): fix core leftover in filler_helpers.h
fntlnz Feb 4, 2021
a137c7f
chore(userspace/libscap): cmake formatting
fntlnz Feb 5, 2021
b3c6be7
update(driver/bpf): improve btf compatibility for 4.x kernels
fntlnz Feb 5, 2021
1e70b28
build: libbpf dependency for driver and libscap targets
fntlnz Feb 10, 2021
45a0b27
wip: initial bpf fillers main test routine
fntlnz Feb 11, 2021
81e9eac
chore: access fillers perf buffer
fntlnz Feb 14, 2021
5f002b2
update(driver/bpf/test): load the specific perf end tail maps needed
fntlnz Feb 15, 2021
39832ba
update(driver/bpf/test): map setup code
fntlnz Feb 15, 2021
eadafad
update(driver/bpf/test): extract perf header
fntlnz Feb 15, 2021
207de73
update(driver/bpf/test): simple field printer
fntlnz Feb 16, 2021
4392be1
update(driver/bpf/test): initial testing framework structure
fntlnz Feb 16, 2021
27dc52e
update(driver/bpf/test): initial perf based assertion mechanism
fntlnz Feb 16, 2021
1f650ec
update(driver/bpf/test): experimenting with different test structures…
fntlnz Feb 17, 2021
3f5808a
update(driver/bpf/test): filler guards for enter/exit
fntlnz Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -8
BreakBeforeBraces: Allman
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
DerivePointerAlignment: true
IndentWidth: 8
SortIncludes: false
SpaceAfterTemplateKeyword: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeParens: Never
UseTab: Always
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ if(MUSL_OPTIMIZED_BUILD)
set(SYSDIG_MUSL_FLAGS "-static -Os")
endif()

include(ExternalProject)
if(NOT WIN32)

set(SYSDIG_DEBUG_FLAGS "-D_DEBUG")
Expand Down Expand Up @@ -108,6 +109,7 @@ if(NOT WIN32)
set(PROBE_DEVICE_NAME "sysdig")
endif()

include(libbpf)
add_subdirectory(driver)
add_definitions(-DHAS_CAPTURE)
endif()
Expand Down
47 changes: 47 additions & 0 deletions cmake/modules/libbpf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Copyright (C) 2013-2021 Sysdig Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#

option(USE_BUNDLED_LIBBPF "Enable building of the bundled libbpf"
${USE_BUNDLED_DEPS})

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
if (NOT USE_BUNDLED_LIBBPF)
find_path(LIBBPF_INCLUDE bpf/libbpf.h)
find_library(LIBBPF_LIB NAMES bpf)
if (LIBBPF_INCLUDE AND LIBBPF_LIB)
message(
STATUS "Found libbpf: include: ${LIBBPF_INCLUDE}, lib: ${LIBBPF_LIB}")
else ()
message(FATAL_ERROR "Couldn't find system libbpf")
endif ()
else ()

set(LIBBPF_SRC "${CMAKE_CURRENT_BINARY_DIR}/libbpf-prefix/src")
set(LIBBPF_BUILD_DIR "${LIBBPF_SRC}/libbpf-build")
set(LIBBPF_INCLUDE "${LIBBPF_BUILD_DIR}/root/usr/include")
set(LIBBPF_LIB "${LIBBPF_BUILD_DIR}/root/usr/lib64/libbpf.a")
ExternalProject_Add(
libbpf
URL "https://github.com/libbpf/libbpf/archive/a199b854156ccac574eb031d464d8fd1a5523ce2.tar.gz"
URL_HASH
"SHA256=9519fb0df06db85484ce934adf7a4b0ea9363c9496a2b427acdd03a0a9d9348d"
CONFIGURE_COMMAND mkdir -p build root
BUILD_COMMAND BUILD_STATIC_ONLY=y OBJDIR=${LIBBPF_BUILD_DIR}/build DESTDIR=${LIBBPF_BUILD_DIR}/root make -C ${LIBBPF_SRC}/libbpf/src install
INSTALL_COMMAND "")

message(STATUS "Using bundled libbpf: include'${LIBBPF_INCLUDE}', lib: ${LIBBPF_LIB}")
endif ()
endif ()
1 change: 1 addition & 0 deletions driver/bpf/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
built-in.a
*.tmp
57 changes: 37 additions & 20 deletions driver/bpf/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2013-2018 Draios Inc. dba Sysdig.
# Copyright (c) 2013-2021 Sysdig Inc.
#
# This file is dual licensed under either the MIT or GPL 2. See
# MIT.txt or GPL.txt for full copies of the license.
Expand All @@ -9,24 +9,41 @@ configure_file(../driver_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/../driver_confi

option(BUILD_BPF "Build the BPF driver on Linux" OFF)

if(BUILD_BPF)
add_custom_target(bpf ALL
COMMAND make
COMMAND "${CMAKE_COMMAND}" -E copy_if_different probe.o "${CMAKE_CURRENT_BINARY_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)
endif()
if (BUILD_BPF)
add_custom_target(bpf ALL
COMMAND make LIBBPF_INCLUDE=-I${LIBBPF_INCLUDE}
COMMAND "${CMAKE_COMMAND}" -E copy_if_different probe.o "${CMAKE_CURRENT_BINARY_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)
add_custom_target(bpf-btf ALL
COMMAND make LIBBPF_INCLUDE=-I${LIBBPF_INCLUDE} btf-probe.o
COMMAND "${CMAKE_COMMAND}" -E copy_if_different btf-probe.o "${CMAKE_CURRENT_BINARY_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)
add_custom_target(bpf-btf-core ALL
COMMAND make LIBBPF_INCLUDE=-I${LIBBPF_INCLUDE} btf-core-probe.o
COMMAND "${CMAKE_COMMAND}" -E copy_if_different btf-core-probe.o "${CMAKE_CURRENT_BINARY_DIR}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
VERBATIM)

if (USE_BUNDLED_LIBBPF)
add_dependencies(bpf libbpf)
add_dependencies(bpf-btf libbpf)
add_dependencies(bpf-btf-core libbpf)
endif ()
endif ()

install(FILES
bpf_helpers.h
filler_helpers.h
fillers.h
Makefile
maps.h
plumbing_helpers.h
probe.c
quirks.h
ring_helpers.h
types.h
DESTINATION "src/${PACKAGE_NAME}-${PROBE_VERSION}/bpf"
COMPONENT agent-kmodule)
filler_helpers.h
fillers.h
Makefile
maps.h
plumbing_helpers.h
probe.c
quirks.h
ring_helpers.h
types.h
DESTINATION "src/${PACKAGE_NAME}-${PROBE_VERSION}/bpf"
COMPONENT agent-kmodule)

add_subdirectory(test)
91 changes: 89 additions & 2 deletions driver/bpf/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2013-2018 Draios Inc. dba Sysdig.
# Copyright (c) 2013-2021 Draios Inc. dba Sysdig.
#
# This file is dual licensed under either the MIT or GPL 2. See
# MIT.txt or GPL.txt for full copies of the license.
Expand All @@ -9,6 +9,9 @@ always += probe.o

LLC ?= llc
CLANG ?= clang
LLVM_STRIP ?= llvm-strip

BPFTOOL ?= bpftool

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

Expand All @@ -20,9 +23,28 @@ all:
clean:
$(MAKE) -C $(KERNELDIR) M=$$PWD clean
@rm -f *~
# rm -f btf-probe.h

# Get Clang's default includes on this system. We'll explicitly add these dirs
# to the includes list when compiling with `-target bpf` because otherwise some
# architecture-specific dirs will be "missing" on some architectures/distros -
# headers such as asm/types.h, asm/byteorder.h, asm/socket.h, asm/sockios.h,
# sys/cdefs.h etc. might be missing.
#
# Use '-idirafter': Don't interfere with include mechanics except where the
# build would have failed anyways.
# This approach comes from libbpf-bootstrap
# https://github.com/libbpf/libbpf-bootstrap/blob/eb6709ae72f37e7af89b8527c3f8923b1721a6d5/src/Makefile#L14
CLANG_BPF_SYS_INCLUDES = $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

# compiles a bpf probe called probe.o
# using libbpf for the helpers and the kernel headers
# for all the other macro definitions
# - requires the kernel headers
# - compatible with 4.x kernels and newer
# - requires clang >= 7
$(obj)/probe.o: $(src)/probe.c \
$(src)/bpf_helpers.h \
$(src)/filler_helpers.h \
$(src)/fillers.h \
$(src)/maps.h \
Expand All @@ -34,6 +56,7 @@ $(obj)/probe.o: $(src)/probe.c \
$(KBUILD_CPPFLAGS) \
$(KBUILD_EXTRA_CPPFLAGS) \
$(DEBUG) \
$(LIBBPF_INCLUDE) \
-D__KERNEL__ \
-D__BPF_TRACING__ \
-Wno-gnu-variable-sized-type-not-at-end \
Expand All @@ -43,3 +66,67 @@ $(obj)/probe.o: $(src)/probe.c \
-Wno-tautological-compare \
-O2 -g -emit-llvm -c $< -o $(patsubst %.o,%.ll,$@)
$(LLC) -march=bpf -filetype=obj -o $@ $(patsubst %.o,%.ll,$@)

# compiles a bpf probe called btf-probe.o
# using libbpf for the helpers and a vmlinux.h
# generated with bpftool from /sys/kernel/btf/vmlinux
# - the probe contains BTF type information
# - DOES NOT require the kernel headers.
# - requires a kernel compiled with CONFIG_DEBUG_INFO_BTF
# - requires clang >= 10.0.0
btf-probe.o: probe.c \
filler_helpers.h \
fillers.h \
maps.h \
plumbing_helpers.h \
quirks.h \
ring_helpers.h \
types.h \
kernel_flags.h \
vmlinux.h
$(CLANG) \
$(DEBUG) \
$(CLANG_BPF_SYS_INCLUDES) \
$(LIBBPF_INCLUDE) \
-D__KERNEL__ \
-D__SYSDIG_BTF_BUILD__ \
-D__BPF_TRACING__ \
-D__TARGET_ARCH_X86__ \
-O2 -target bpf -g -c $< -o $@
$(LLVM_STRIP) -g $@

# compiles a bpf probe called btf-core-probe.o
# using libbpf for the helpers and a vmlinux.h
# generated with bpftool from /sys/kernel/btf/vmlinux
# - the probe contains BTF type information
# - the probe uses CO-RE to access internal kernel data structures and supports fields relocation
# - DOES NOT require the kernel headers.
# - requires a kernel compiled with CONFIG_DEBUG_INFO_BTF
# - requires clang >= 10.0.0
btf-core-probe.o: probe.c \
filler_helpers.h \
fillers.h \
maps.h \
plumbing_helpers.h \
quirks.h \
ring_helpers.h \
types.h \
kernel_flags.h \
vmlinux.h
$(CLANG) \
$(DEBUG) \
$(CLANG_BPF_SYS_INCLUDES) \
$(LIBBPF_INCLUDE) \
-D__KERNEL__ \
-D__SYSDIG_BTF_BUILD__ \
-D__SYSDIG_BTF_CORE_BUILD__ \
-D__BPF_TRACING__ \
-D__TARGET_ARCH_X86__ \
-O2 -target bpf -g -c $< -o $@
$(LLVM_STRIP) -g $@

vmlinux.h:
$(BPFTOOL) btf dump file /sys/kernel/btf/vmlinux format c > $@

# btf-probe.h: btf-probe.o
# $(BPFTOOL) gen skeleton $(patsubst %.h,%.o,$@) > $@
80 changes: 0 additions & 80 deletions driver/bpf/bpf_helpers.h

This file was deleted.

Loading