Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(sinsp): move engine/platform selection code to libsinsp #1401

Merged
merged 24 commits into from
Oct 30, 2023
Merged
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
0b3f788
fix(tests): SCAP_MODE_NODRIVER is invalid for the test_input engine
gnosek Oct 17, 2023
5d07d28
fix(build): fix shadowing of a local variable
gnosek Oct 17, 2023
00ed35b
fix(build): missing modern_bpf dependencies
gnosek Aug 21, 2023
2306ac8
fix(build): missing bpf engine dependency on zlib
gnosek Oct 11, 2023
8acb589
fix(build): wrap scap_engines.h in C++ check
gnosek Oct 12, 2023
242e673
chore(scap): initialize platform in scap_init, not scap_init_int
gnosek Aug 20, 2023
5052804
chore(scap): set up proc callbacks in platform alloc functions
gnosek Aug 20, 2023
e6f8951
chore(scap): move scap_generic_init_platform call to scap_platform_init
gnosek Aug 20, 2023
5706916
cleanup(scap): remove outdated comment
gnosek Aug 20, 2023
ff4ec99
chore(scap): extract two helpers from scap_init
gnosek Aug 20, 2023
57b6825
cleanup(build): introduce scap_config.h
gnosek Jun 20, 2023
37c4c63
cleanup(build): move engine selection to a separate cmakefile
gnosek Jul 13, 2023
da9bb70
new(sinsp): only implement open_* methods for supported engines
gnosek Sep 8, 2023
193221e
cleanup(scap,sinsp): move platform selection to sinsp
gnosek Sep 6, 2023
8e09e17
cleanup(scap): move bpf_stats check to the engines
gnosek Jun 20, 2023
d7de595
cleanup(scap, sinsp): remove proc callback params from scap_open_args
gnosek Aug 24, 2023
46c477a
cleanup(scap,sinsp): move engine selection to sinsp
gnosek Aug 21, 2023
df24a91
cleanup(scap,sinsp): remove scap_nodriver_engine_params
gnosek Aug 20, 2023
b36c003
cleanup(scap,sinsp): remove oargs->mode and scap_vtable->mode
gnosek Oct 26, 2023
146d472
cleanup(scap): rename scap_linux_platform variable to scap_linux_plat…
gnosek Oct 26, 2023
d9ff58c
cleanup(scap): rename scap_init_int to scap_init_engine
gnosek Oct 26, 2023
293d4a2
docs(scap): document why we initialize the engine first
gnosek Oct 26, 2023
8ad59e0
cleanup(scap): move scap_mode_t to sinsp
gnosek Oct 30, 2023
7a197f5
cleanup(sinsp): rename scap_mode_t and its values
gnosek Oct 30, 2023
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
30 changes: 30 additions & 0 deletions cmake/modules/engine_config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
option(CREATE_TEST_TARGETS "Enable make-targets for unit testing" ON)

if(CREATE_TEST_TARGETS)
# Add engine only used for testing
set(HAS_ENGINE_TEST_INPUT On)
endif()

set(HAS_ENGINE_NODRIVER On)
set(HAS_ENGINE_SAVEFILE On)
set(HAS_ENGINE_SOURCE_PLUGIN On)

if(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(HAS_ENGINE_KMOD On)
set(HAS_ENGINE_UDIG On)
set(HAS_ENGINE_BPF On)

option(BUILD_LIBSCAP_MODERN_BPF "Enable modern bpf probe" OFF)
if(BUILD_LIBSCAP_MODERN_BPF)
Andreagit97 marked this conversation as resolved.
Show resolved Hide resolved
set(HAS_ENGINE_MODERN_BPF On)
endif()
endif()

# gVisor is currently only supported on Linux x86_64
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_SYSTEM_NAME MATCHES "Linux" AND NOT MINIMAL_BUILD)
option(BUILD_LIBSCAP_GVISOR "Build gVisor support" ON)
if (BUILD_LIBSCAP_GVISOR)
set(HAS_ENGINE_GVISOR On)
endif()
endif()

3 changes: 2 additions & 1 deletion cmake/modules/libscap.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ endif()

get_filename_component(LIBSCAP_INCLUDE_DIR ${LIBSCAP_DIR}/userspace/libscap ABSOLUTE)
get_filename_component(LIBSCAP_COMMON_INCLUDE_DIR ${LIBSCAP_DIR}/userspace/common ABSOLUTE)
set(LIBSCAP_INCLUDE_DIRS ${LIBSCAP_INCLUDE_DIR} ${LIBSCAP_COMMON_INCLUDE_DIR} ${DRIVER_CONFIG_DIR})
set(LIBSCAP_INCLUDE_DIRS ${LIBSCAP_INCLUDE_DIR} ${LIBSCAP_COMMON_INCLUDE_DIR} ${PROJECT_BINARY_DIR}/libscap ${DRIVER_CONFIG_DIR})

function(set_scap_target_properties target)
set_target_properties(${target} PROPERTIES
Expand Down Expand Up @@ -147,5 +147,6 @@ install(DIRECTORY "${PROJECT_BINARY_DIR}/common" DESTINATION "${CMAKE_INSTALL_IN
install(DIRECTORY "${LIBSCAP_DIR}/userspace/plugin" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBS_PACKAGE_NAME}/userspace"
COMPONENT "scap"
FILES_MATCHING PATTERN "*.h")
install(FILES ${PROJECT_BINARY_DIR}/libscap/scap_config.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${LIBS_PACKAGE_NAME}/userspace/libscap)
install(FILES ${PROJECT_BINARY_DIR}/libscap/libscap.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()
4 changes: 3 additions & 1 deletion test/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ file(GLOB_RECURSE GENERIC_TRACEPOINTS_TEST_SUITE ${CMAKE_CURRENT_SOURCE_DIR}/tes
## Actions suite files
file(GLOB_RECURSE ACTIONS_TEST_SUITE ${CMAKE_CURRENT_SOURCE_DIR}/test_suites/actions_suite/*.cpp)

include(libscap)

set(DRIVERS_TEST_SOURCES
./start_tests.cpp
./event_class/event_class.cpp
Expand All @@ -34,7 +36,7 @@ set(DRIVERS_TEST_INCLUDE
../../userspace/common
"${GTEST_INCLUDE}"
"${LIBSCAP_DIR}/driver/"
"${LIBSCAP_DIR}/userspace/libscap"
"${LIBSCAP_INCLUDE_DIRS}"
)

set(DRIVERS_TEST_LINK_LIBRARIES
Expand Down
62 changes: 42 additions & 20 deletions test/drivers/start_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <iostream>
#include <string>
#include <scap.h>
#include <scap_engines.h>
#include <scap_vtable.h>
#include <getopt.h>
#include <gtest/gtest.h>
#include "./event_class/event_class.h"
#include "strl.h"

#define UNKNOWN_ENGINE "unknown"

/* We support only these arguments */
#define HELP_OPTION "help"
#define KMOD_OPTION "kmod"
Expand Down Expand Up @@ -77,11 +77,11 @@ int insert_kmod(const std::string& kmod_path)
return EXIT_SUCCESS;
}

void abort_if_already_configured(scap_open_args* oargs)
void abort_if_already_configured(const struct scap_vtable* vtable)
{
if(strcmp(oargs->engine_name, UNKNOWN_ENGINE) != 0)
if(vtable != nullptr)
{
std::cerr << "* '" << oargs->engine_name << "' engine is already configured. Please specify just one engine!" << std::endl;
std::cerr << "* '" << vtable->name << "' engine is already configured. Please specify just one engine!" << std::endl;
exit(EXIT_FAILURE);
}
}
Expand Down Expand Up @@ -123,12 +123,8 @@ int open_engine(int argc, char** argv)
{0, 0, 0, 0}};

int ret = 0;
scap_open_args oargs = {0};
struct scap_bpf_engine_params bpf_params = {0};
struct scap_kmod_engine_params kmod_params = {0};
struct scap_modern_bpf_engine_params modern_bpf_params = {0};
oargs.engine_name = UNKNOWN_ENGINE;
oargs.mode = SCAP_MODE_LIVE;
const struct scap_vtable* vtable = nullptr;
scap_open_args oargs = {};
unsigned long buffer_bytes_dim = DEFAULT_DRIVER_BUFFER_BYTES_DIM;
std::string kmod_path;

Expand Down Expand Up @@ -158,8 +154,12 @@ int open_engine(int argc, char** argv)
switch(op)
{
case 'b':
abort_if_already_configured(&oargs);
oargs.engine_name = BPF_ENGINE;
#ifdef HAS_ENGINE_BPF
{
struct scap_bpf_engine_params bpf_params = {0};

abort_if_already_configured(vtable);
vtable = &scap_bpf_engine;
bpf_params.buffer_bytes_dim = buffer_bytes_dim;
/* This should handle cases where we pass arguments with the space:
* `-b ./path/to/probe`. Without this `if` case we can accept arguments
Expand All @@ -181,19 +181,37 @@ int open_engine(int argc, char** argv)
oargs.engine_params = &bpf_params;

std::cout << "* Configure BPF probe tests! Probe path: " << bpf_params.bpf_probe << std::endl;
}
#else
std::cerr << "BPF engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;

case 'm':
abort_if_already_configured(&oargs);
oargs.engine_name = MODERN_BPF_ENGINE;
#ifdef HAS_ENGINE_MODERN_BPF
{
struct scap_modern_bpf_engine_params modern_bpf_params = {0};

abort_if_already_configured(vtable);
vtable = &scap_modern_bpf_engine;
modern_bpf_params.buffer_bytes_dim = buffer_bytes_dim;
oargs.engine_params = &modern_bpf_params;
std::cout << "* Configure modern BPF probe tests!" << std::endl;
}
#else
std::cerr << "Modern BPF engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;

case 'k':
abort_if_already_configured(&oargs);
oargs.engine_name = KMOD_ENGINE;
#ifdef HAS_ENGINE_KMOD
{
struct scap_kmod_engine_params kmod_params = {0};

abort_if_already_configured(vtable);
vtable = &scap_kmod_engine;
kmod_params.buffer_bytes_dim = buffer_bytes_dim;
if(optarg == NULL && optind < argc && argv[optind][0] != '-')
{
Expand All @@ -214,7 +232,11 @@ int open_engine(int argc, char** argv)
return EXIT_FAILURE;
}
std::cout << "* Configure kernel module tests! Kernel module path: " << kmod_path << std::endl;
;
}
#else
std::cerr << "Kernel module engine is not supported in this build" << std::endl;
return EXIT_FAILURE;
#endif
break;

case 'd':
Expand All @@ -231,14 +253,14 @@ int open_engine(int argc, char** argv)
}
std::cout << "* Using buffer dim: " << buffer_bytes_dim << std::endl;

if(strcmp(oargs.engine_name, UNKNOWN_ENGINE) == 0)
if(vtable == nullptr)
{
std::cerr << "Unsupported engine! Choose between: m, b, k" << std::endl;
return EXIT_FAILURE;
}

char error_buffer[FILENAME_MAX] = {0};
event_test::s_scap_handle = scap_open(&oargs, error_buffer, &ret);
event_test::s_scap_handle = scap_open(&oargs, vtable, nullptr, error_buffer, &ret);
if(!event_test::s_scap_handle)
{
std::cerr << "Unable to open the engine: " << error_buffer << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion test/libscap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ set(LIBSCAP_TESTS_INCLUDE
PRIVATE
"${GTEST_INCLUDE}"
"${CMAKE_CURRENT_SOURCE_DIR}" # for test helpers <helpers/...>
"${CMAKE_SOURCE_DIR}/userspace/libscap" # this is for libscap includes
"${LIBSCAP_INCLUDE_DIRS}" # this is for libscap includes
"${CMAKE_SOURCE_DIR}/userspace/common" # used to include `strl.h`
"${CMAKE_SOURCE_DIR}/driver" # for the event_stats.h file
"${CMAKE_CURRENT_BINARY_DIR}" # used to include `libscap_test_var.h`
Expand Down
8 changes: 3 additions & 5 deletions test/libscap/test_suites/engines/bpf/bpf.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include <scap.h>
#include <scap_engines.h>
#include <gtest/gtest.h>
#include <unordered_set>
#include <helpers/engines.h>
#include <libscap_test_var.h>

scap_t* open_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim, const char* name, std::unordered_set<uint32_t> ppm_sc_set = {})
{
struct scap_open_args oargs = {
.engine_name = BPF_ENGINE,
.mode = SCAP_MODE_LIVE,
};
struct scap_open_args oargs {};

/* If empty we fill with all syscalls */
if(ppm_sc_set.empty())
Expand All @@ -33,7 +31,7 @@ scap_t* open_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim,
};
oargs.engine_params = &bpf_params;

return scap_open(&oargs, error_buf, rc);
return scap_open(&oargs, &scap_bpf_engine, nullptr, error_buf, rc);
}

TEST(bpf, open_engine)
Expand Down
8 changes: 3 additions & 5 deletions test/libscap/test_suites/engines/kmod/kmod.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <scap.h>
#include <scap_engines.h>
#include <gtest/gtest.h>
#include <unordered_set>
#include <helpers/engines.h>
Expand Down Expand Up @@ -66,10 +67,7 @@ int insert_kmod(const char* kmod_path, char* error_buf)

scap_t* open_kmod_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim, const char* kmod_path, std::unordered_set<uint32_t> ppm_sc_set = {})
{
struct scap_open_args oargs = {
.engine_name = KMOD_ENGINE,
.mode = SCAP_MODE_LIVE,
};
struct scap_open_args oargs {};

/* Remove previously inserted kernel module */
if(remove_kmod(error_buf) != EXIT_SUCCESS)
Expand Down Expand Up @@ -104,7 +102,7 @@ scap_t* open_kmod_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim,
};
oargs.engine_params = &kmod_params;

return scap_open(&oargs, error_buf, rc);
return scap_open(&oargs, &scap_kmod_engine, nullptr, error_buf, rc);
}

TEST(kmod, open_engine)
Expand Down
8 changes: 3 additions & 5 deletions test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include <scap.h>
#include <scap_engines.h>
#include <gtest/gtest.h>
#include <unordered_set>
#include <syscall.h>
#include <helpers/engines.h>

scap_t* open_modern_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffer_dim, uint16_t cpus_for_each_buffer, bool online_only, std::unordered_set<uint32_t> ppm_sc_set = {})
{
struct scap_open_args oargs = {
.engine_name = MODERN_BPF_ENGINE,
.mode = SCAP_MODE_LIVE,
};
struct scap_open_args oargs {};

/* If empty we fill with all syscalls */
if(ppm_sc_set.empty())
Expand All @@ -35,7 +33,7 @@ scap_t* open_modern_bpf_engine(char* error_buf, int32_t* rc, unsigned long buffe
};
oargs.engine_params = &modern_bpf_params;

return scap_open(&oargs, error_buf, rc);
return scap_open(&oargs, &scap_modern_bpf_engine, nullptr, error_buf, rc);
}

TEST(modern_bpf, open_engine)
Expand Down
Loading