Skip to content

Commit

Permalink
cleanup(scap,sinsp): move engine selection to sinsp
Browse files Browse the repository at this point in the history
AS of now, libscap doesn't know anything about selecting engines
or platforms and defers this to sinsp (or the consumer, when using
scap directly)

Signed-off-by: Grzegorz Nosek <[email protected]>
  • Loading branch information
gnosek committed Oct 20, 2023
1 parent 2d76000 commit 33b3802
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 140 deletions.
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
61 changes: 42 additions & 19 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,11 +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;
const struct scap_vtable* vtable = nullptr;
scap_open_args oargs = {};
oargs.mode = SCAP_MODE_LIVE;
unsigned long buffer_bytes_dim = DEFAULT_DRIVER_BUFFER_BYTES_DIM;
std::string kmod_path;
Expand Down Expand Up @@ -158,8 +155,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 +182,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 +233,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 +254,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, nullptr, 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
4 changes: 2 additions & 2 deletions test/libscap/test_suites/engines/bpf/bpf.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 All @@ -7,7 +8,6 @@
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,
};

Expand All @@ -33,7 +33,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, nullptr, error_buf, rc);
return scap_open(&oargs, &scap_bpf_engine, nullptr, error_buf, rc);
}

TEST(bpf, open_engine)
Expand Down
4 changes: 2 additions & 2 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 @@ -67,7 +68,6 @@ 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,
};

Expand Down Expand Up @@ -104,7 +104,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, nullptr, error_buf, rc);
return scap_open(&oargs, &scap_kmod_engine, nullptr, error_buf, rc);
}

TEST(kmod, open_engine)
Expand Down
4 changes: 2 additions & 2 deletions test/libscap/test_suites/engines/modern_bpf/modern_bpf.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 <syscall.h>
Expand All @@ -7,7 +8,6 @@
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,
};

Expand Down Expand Up @@ -35,7 +35,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, nullptr, error_buf, rc);
return scap_open(&oargs, &scap_modern_bpf_engine, nullptr, error_buf, rc);
}

TEST(modern_bpf, open_engine)
Expand Down
Loading

0 comments on commit 33b3802

Please sign in to comment.