diff --git a/test/drivers/CMakeLists.txt b/test/drivers/CMakeLists.txt index 9c2de5cdabb..37657840ad7 100644 --- a/test/drivers/CMakeLists.txt +++ b/test/drivers/CMakeLists.txt @@ -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 @@ -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 diff --git a/test/drivers/start_tests.cpp b/test/drivers/start_tests.cpp index 74958f38561..f0b0138e391 100644 --- a/test/drivers/start_tests.cpp +++ b/test/drivers/start_tests.cpp @@ -1,13 +1,13 @@ #include #include #include +#include +#include #include #include #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" @@ -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); } } @@ -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; @@ -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 @@ -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] != '-') { @@ -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': @@ -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; diff --git a/test/libscap/CMakeLists.txt b/test/libscap/CMakeLists.txt index b718508848e..3af932aa96e 100644 --- a/test/libscap/CMakeLists.txt +++ b/test/libscap/CMakeLists.txt @@ -32,7 +32,7 @@ set(LIBSCAP_TESTS_INCLUDE PRIVATE "${GTEST_INCLUDE}" "${CMAKE_CURRENT_SOURCE_DIR}" # for test 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` diff --git a/test/libscap/test_suites/engines/bpf/bpf.cpp b/test/libscap/test_suites/engines/bpf/bpf.cpp index 436981e6fe6..be87dc0c191 100644 --- a/test/libscap/test_suites/engines/bpf/bpf.cpp +++ b/test/libscap/test_suites/engines/bpf/bpf.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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 ppm_sc_set = {}) { struct scap_open_args oargs = { - .engine_name = BPF_ENGINE, .mode = SCAP_MODE_LIVE, }; @@ -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) diff --git a/test/libscap/test_suites/engines/kmod/kmod.cpp b/test/libscap/test_suites/engines/kmod/kmod.cpp index a25a8f3c689..9ba14b81933 100644 --- a/test/libscap/test_suites/engines/kmod/kmod.cpp +++ b/test/libscap/test_suites/engines/kmod/kmod.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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 ppm_sc_set = {}) { struct scap_open_args oargs = { - .engine_name = KMOD_ENGINE, .mode = SCAP_MODE_LIVE, }; @@ -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) diff --git a/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp b/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp index b0787011c0d..6d24a7e1178 100644 --- a/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp +++ b/test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -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 ppm_sc_set = {}) { struct scap_open_args oargs = { - .engine_name = MODERN_BPF_ENGINE, .mode = SCAP_MODE_LIVE, }; @@ -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) diff --git a/userspace/libscap/examples/01-open/scap_open.c b/userspace/libscap/examples/01-open/scap_open.c index 1fea02191c8..73bde23e66a 100644 --- a/userspace/libscap/examples/01-open/scap_open.c +++ b/userspace/libscap/examples/01-open/scap_open.c @@ -24,9 +24,9 @@ limitations under the License. #include #include #include "strl.h" +#include "scap_engines.h" #define SYSCALL_NAME_MAX_LEN 40 -#define UNKNOWN_ENGINE "unknown" /* SCAP SOURCES */ #define KMOD_OPTION "--kmod" @@ -149,7 +149,8 @@ static int simple_set[] = { }; /* Generic global variables. */ -static scap_open_args oargs = {.engine_name = UNKNOWN_ENGINE}; /* scap oargs used in `scap_open`. */ +static scap_open_args oargs = {}; /* scap oargs used in `scap_open`. */ +static const struct scap_vtable* vtable = NULL; static uint64_t g_nevts = 0; /* total number of events captured. */ static scap_t* g_h = NULL; /* global scap handler. */ static uint16_t* lens16 = NULL; /* pointer used to print the length of event params. */ @@ -550,25 +551,36 @@ void print_help() void print_scap_source() { printf("\n--------------------------- SCAP SOURCE --------------------------\n"); - if(strcmp(oargs.engine_name, KMOD_ENGINE) == 0) + if(false) + { + } +#ifdef HAS_ENGINE_KMOD + else if(vtable == &scap_kmod_engine) { printf("* Kernel module.\n"); } - else if(strcmp(oargs.engine_name, BPF_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_BPF + else if(vtable == &scap_bpf_engine) { struct scap_bpf_engine_params* params = oargs.engine_params; printf("* BPF probe: '%s'\n", params->bpf_probe); } - else if(strcmp(oargs.engine_name, MODERN_BPF_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_MODERN_BPF + else if(vtable == &scap_modern_bpf_engine) { struct scap_modern_bpf_engine_params* params = oargs.engine_params; printf("* Modern BPF probe, 1 ring buffer every %d CPUs\n", params->cpus_for_each_buffer); } - else if(strcmp(oargs.engine_name, SAVEFILE_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_SAVEFILE + else if(vtable == &scap_savefile_engine) { struct scap_savefile_engine_params* params = oargs.engine_params; printf("* Scap file: '%s'.\n", params->fname); } +#endif else { printf("* Unknown scap source! Bye!\n"); @@ -588,24 +600,35 @@ void print_configurations() void print_start_capture() { - if(strcmp(oargs.engine_name, KMOD_ENGINE) == 0) + if(false) + { + } +#ifdef HAS_ENGINE_KMOD + else if(vtable == &scap_kmod_engine) { printf("* OK! Kernel module correctly loaded.\n"); } - else if(strcmp(oargs.engine_name, BPF_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_BPF + else if(vtable == &scap_bpf_engine) { printf("* OK! BPF probe correctly loaded: NO VERIFIER ISSUES :)\n"); } - else if(strcmp(oargs.engine_name, MODERN_BPF_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_MODERN_BPF + else if(vtable == &scap_modern_bpf_engine) { printf("* OK! modern BPF probe correctly loaded: NO VERIFIER ISSUES :)\n"); } - else if(strcmp(oargs.engine_name, SAVEFILE_ENGINE) == 0) +#endif +#ifdef HAS_ENGINE_SAVEFILE + else if(vtable == &scap_savefile_engine) { printf("* OK! Ready to read from scap file.\n"); printf("\n* Reading from scap file...\n"); return; } +#endif else { printf("Cannot start the capture! Bye\n"); @@ -621,13 +644,16 @@ void parse_CLI_options(int argc, char** argv) { /*=============================== SCAP SOURCES ===========================*/ +#ifdef HAS_ENGINE_KMOD if(!strcmp(argv[i], KMOD_OPTION)) { - oargs.engine_name = KMOD_ENGINE; + vtable = &scap_kmod_engine; oargs.mode = SCAP_MODE_LIVE; kmod_params.buffer_bytes_dim = buffer_bytes_dim; oargs.engine_params = &kmod_params; } +#endif +#ifdef HAS_ENGINE_BPF if(!strcmp(argv[i], BPF_OPTION)) { if(!(i + 1 < argc)) @@ -635,21 +661,25 @@ void parse_CLI_options(int argc, char** argv) printf("\nYou need to specify also the BPF probe path! Bye!\n"); exit(EXIT_FAILURE); } - oargs.engine_name = BPF_ENGINE; + vtable = &scap_bpf_engine; oargs.mode = SCAP_MODE_LIVE; bpf_params.bpf_probe = argv[++i]; bpf_params.buffer_bytes_dim = buffer_bytes_dim; oargs.engine_params = &bpf_params; } +#endif +#ifdef HAS_ENGINE_MODERN_BPF if(!strcmp(argv[i], MODERN_BPF_OPTION)) { - oargs.engine_name = MODERN_BPF_ENGINE; + vtable = &scap_modern_bpf_engine; oargs.mode = SCAP_MODE_LIVE; modern_bpf_params.buffer_bytes_dim = buffer_bytes_dim; modern_bpf_params.cpus_for_each_buffer = DEFAULT_CPU_FOR_EACH_BUFFER; modern_bpf_params.allocate_online_only = true; oargs.engine_params = &modern_bpf_params; } +#endif +#ifdef HAS_ENGINE_SAVEFILE if(!strcmp(argv[i], SCAP_FILE_OPTION)) { if(!(i + 1 < argc)) @@ -657,11 +687,12 @@ void parse_CLI_options(int argc, char** argv) printf("\nYou need to specify also the scap file path! Bye!\n"); exit(EXIT_FAILURE); } - oargs.engine_name = SAVEFILE_ENGINE; + vtable = &scap_savefile_engine; oargs.mode = SCAP_MODE_CAPTURE; savefile_params.fname = argv[++i]; oargs.engine_params = &savefile_params; } +#endif /*=============================== SCAP SOURCES ===========================*/ @@ -751,13 +782,30 @@ void parse_CLI_options(int argc, char** argv) /*=============================== PRINT ===========================*/ } - if(strcmp(oargs.engine_name, UNKNOWN_ENGINE) == 0) + if(!vtable) { printf("\nSource not specified! Bye!\n"); exit(EXIT_FAILURE); } } +static inline bool engine_uses_bpf() +{ +#ifdef HAS_ENGINE_BPF + if(vtable == &scap_bpf_engine) + { + return true; + } +#endif +#ifdef HAS_ENGINE_MODERN_BPF + if(vtable == &scap_modern_bpf_engine) + { + return true; + } +#endif + return false; +} + void print_stats() { gettimeofday(&tval_end, NULL); @@ -797,7 +845,7 @@ void print_stats() printf("\n[SCAP-OPEN]: Stats v2.\n"); printf("\n[SCAP-OPEN]: %u metrics in total\n", nstats); - if((strncmp(oargs.engine_name, BPF_ENGINE, 3) == 0) || (strncmp(oargs.engine_name, MODERN_BPF_ENGINE, 10) == 0)) + if(engine_uses_bpf()) { printf("[SCAP-OPEN]: [1] kernel-side counters\n"); printf("[SCAP-OPEN]: [2] libbpf stats (compare to `bpftool prog show` CLI)\n\n"); @@ -855,7 +903,7 @@ int main(int argc, char** argv) enable_sc_and_print(); - g_h = scap_open(&oargs, NULL, error, &res); + g_h = scap_open(&oargs, vtable, NULL, error, &res); if(g_h == NULL || res != SCAP_SUCCESS) { fprintf(stderr, "%s (%d)\n", error, res); diff --git a/userspace/libscap/examples/02-validatebuffer/test.c b/userspace/libscap/examples/02-validatebuffer/test.c index 378ed0dc072..79eb8977504 100644 --- a/userspace/libscap/examples/02-validatebuffer/test.c +++ b/userspace/libscap/examples/02-validatebuffer/test.c @@ -24,6 +24,7 @@ limitations under the License. #include #include "../../../../driver/ppm_events_public.h" +#include "scap_engines.h" extern const struct ppm_event_info g_event_info[]; @@ -170,7 +171,7 @@ int main() scap_open_args args = {.mode = SCAP_MODE_LIVE}; - scap_t* h = scap_open(&args, NULL, error, &ret); + scap_t* h = scap_open(&args, &scap_kmod_engine, NULL, error, &ret); if(h == NULL) { fprintf(stderr, "%s (%d)\n", error, ret); diff --git a/userspace/libscap/scap.c b/userspace/libscap/scap.c index 09560208814..e0bf804049b 100644 --- a/userspace/libscap/scap.c +++ b/userspace/libscap/scap.c @@ -86,71 +86,14 @@ scap_t* scap_alloc(void) return calloc(1, sizeof(scap_t)); } -int32_t scap_handle_init_engine(scap_t* handle, scap_open_args* oargs) +int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable, + struct scap_platform* platform) { - const char* engine_name = oargs->engine_name; - const struct scap_vtable* vtable = NULL; int32_t rc; -#ifdef HAS_ENGINE_SAVEFILE - if(strcmp(engine_name, SAVEFILE_ENGINE) == 0) - { - vtable = &scap_savefile_engine; - } -#endif -#ifdef HAS_ENGINE_UDIG - if(strcmp(engine_name, UDIG_ENGINE) == 0) - { - vtable = &scap_udig_engine; - } -#endif -#ifdef HAS_ENGINE_GVISOR - if(strcmp(engine_name, GVISOR_ENGINE) == 0) - { - vtable = &scap_gvisor_engine; - } -#endif -#ifdef HAS_ENGINE_TEST_INPUT - if(strcmp(engine_name, TEST_INPUT_ENGINE) == 0) - { - vtable = &scap_test_input_engine; - } -#endif -#ifdef HAS_ENGINE_KMOD - if(strcmp(engine_name, KMOD_ENGINE) == 0) - { - vtable = &scap_kmod_engine; - } -#endif -#ifdef HAS_ENGINE_BPF - if(strcmp(engine_name, BPF_ENGINE) == 0) - { - vtable = &scap_bpf_engine; - } -#endif -#ifdef HAS_ENGINE_MODERN_BPF - if(strcmp(engine_name, MODERN_BPF_ENGINE) == 0) - { - vtable = &scap_modern_bpf_engine; - } -#endif -#ifdef HAS_ENGINE_NODRIVER - if(strcmp(engine_name, NODRIVER_ENGINE) == 0) - { - vtable = &scap_nodriver_engine; - } -#endif -#ifdef HAS_ENGINE_SOURCE_PLUGIN - if(strcmp(engine_name, SOURCE_PLUGIN_ENGINE) == 0) - { - vtable = &scap_source_plugin_engine; - } -#endif + handle->m_platform = platform; - if(!vtable) - { - return scap_errprintf(handle->m_lasterr, 0, "incorrect engine '%s'", engine_name); - } + ASSERT(vtable != NULL); rc = scap_init_int(handle, oargs, vtable); if(rc != SCAP_SUCCESS) @@ -158,21 +101,6 @@ int32_t scap_handle_init_engine(scap_t* handle, scap_open_args* oargs) return rc; } - return SCAP_SUCCESS; -} - -int32_t scap_init(scap_t* handle, scap_open_args* oargs, struct scap_platform* platform) -{ - int32_t rc; - - handle->m_platform = platform; - - rc = scap_handle_init_engine(handle, oargs); - if(rc != SCAP_SUCCESS) - { - return rc; - } - if(handle->m_platform) { if((rc = scap_platform_init(handle->m_platform, handle->m_lasterr, handle->m_engine, oargs)) != SCAP_SUCCESS) @@ -184,7 +112,8 @@ int32_t scap_init(scap_t* handle, scap_open_args* oargs, struct scap_platform* p return SCAP_SUCCESS; } -scap_t* scap_open(scap_open_args* oargs, struct scap_platform* platform, char* error, int32_t* rc) +scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform, char* error, + int32_t* rc) { scap_t* handle = scap_alloc(); if(!handle) @@ -193,7 +122,7 @@ scap_t* scap_open(scap_open_args* oargs, struct scap_platform* platform, char* e return NULL; } - *rc = scap_init(handle, oargs, platform); + *rc = scap_init(handle, oargs, vtable, platform); if(*rc != SCAP_SUCCESS) { strlcpy(error, handle->m_lasterr, SCAP_LASTERR_SIZE); diff --git a/userspace/libscap/scap.h b/userspace/libscap/scap.h index 871f877260f..85212eae9d5 100644 --- a/userspace/libscap/scap.h +++ b/userspace/libscap/scap.h @@ -495,8 +495,8 @@ scap_t* scap_alloc(void); If this function fails, the only thing you can safely do with the handle is to call \ref scap_deinit on it. */ -int32_t scap_init(scap_t* handle, scap_open_args* oargs, struct scap_platform* platform); -int32_t scap_handle_init_engine(scap_t* handle, scap_open_args* oargs); +int32_t scap_init(scap_t* handle, scap_open_args* oargs, const struct scap_vtable* vtable, + struct scap_platform* platform); /*! \brief Allocate and initialize a handle @@ -517,7 +517,8 @@ int32_t scap_handle_init_engine(scap_t* handle, scap_open_args* oargs); \return The capture instance handle in case of success. NULL in case of failure. */ -scap_t* scap_open(scap_open_args* oargs, struct scap_platform* platform, char* error, int32_t* rc); +scap_t* scap_open(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform, char* error, + int32_t* rc); /*! \brief Deinitialize a capture handle. diff --git a/userspace/libscap/scap_engines.h b/userspace/libscap/scap_engines.h index f01462163c1..5b1e3f89745 100644 --- a/userspace/libscap/scap_engines.h +++ b/userspace/libscap/scap_engines.h @@ -18,6 +18,10 @@ limitations under the License. #pragma once +#ifndef SCAP_HANDLE_T +#define SCAP_HANDLE_T void +#endif + #include "scap_vtable.h" #include "scap_config.h" diff --git a/userspace/libscap/scap_open.h b/userspace/libscap/scap_open.h index 1404effac95..be887d095bb 100644 --- a/userspace/libscap/scap_open.h +++ b/userspace/libscap/scap_open.h @@ -81,7 +81,6 @@ extern "C" typedef struct scap_open_args { - const char* engine_name; ///< engine name ("kmod", "bpf", ...). scap_mode_t mode; ///< scap-mode required by the engine. bool import_users; ///< true if the user list should be created when opening the capture. interesting_ppm_sc_set ppm_sc_of_interest; ///< syscalls of interest. diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 94953f44642..aa944c64d8c 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -28,6 +28,7 @@ limitations under the License. #endif // _WIN32 #include "scap_config.h" +#include "scap_engines.h" #include "scap_open_exception.h" #include "scap_platform.h" #include "sinsp.h" @@ -462,7 +463,7 @@ void sinsp::set_import_users(bool import_users) /*=============================== OPEN METHODS ===============================*/ -void sinsp::open_common(scap_open_args* oargs, struct scap_platform* platform) +void sinsp::open_common(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform) { g_logger.log("Trying to open the right engine!"); @@ -487,7 +488,7 @@ void sinsp::open_common(scap_open_args* oargs, struct scap_platform* platform) throw scap_open_exception("failed to allocate scap handle", SCAP_FAILURE); } - int32_t scap_rc = scap_init(m_h, oargs, platform); + int32_t scap_rc = scap_init(m_h, oargs, vtable, platform); if(scap_rc != SCAP_SUCCESS) { std::string error = scap_getlasterr(m_h); @@ -532,7 +533,6 @@ void sinsp::open_common(scap_open_args* oargs, struct scap_platform* platform) scap_open_args sinsp::factory_open_args(const char* engine_name, scap_mode_t scap_mode) { scap_open_args oargs{}; - oargs.engine_name = engine_name; oargs.mode = scap_mode; return oargs; } @@ -592,7 +592,7 @@ void sinsp::open_kmod(unsigned long driver_buffer_bytes_dim, const libsinsp::eve linux_plat->m_linux_vtable = &scap_kmod_linux_vtable; } - open_common(&oargs, platform); + open_common(&oargs, &scap_kmod_engine, platform); #else throw sinsp_exception("KMOD engine is not supported in this build"); #endif @@ -619,7 +619,7 @@ void sinsp::open_bpf(const std::string& bpf_path, unsigned long driver_buffer_by oargs.engine_params = ¶ms; struct scap_platform* platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this); - open_common(&oargs, platform); + open_common(&oargs, &scap_bpf_engine, platform); #else throw sinsp_exception("BPF engine is not supported in this build"); #endif @@ -631,7 +631,7 @@ void sinsp::open_udig() scap_open_args oargs = factory_open_args(UDIG_ENGINE, SCAP_MODE_LIVE); struct scap_platform* platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this); - open_common(&oargs, platform); + open_common(&oargs, &scap_udig_engine, platform); #else throw sinsp_exception("UDIG engine is not supported in this build"); @@ -662,7 +662,7 @@ void sinsp::open_nodriver(bool full_proc_scan) platform = scap_generic_alloc_platform(::on_new_entry_from_proc, this); } - open_common(&oargs, platform); + open_common(&oargs, &scap_nodriver_engine, platform); #else throw sinsp_exception("NODRIVER engine is not supported in this build"); #endif @@ -708,7 +708,7 @@ void sinsp::open_savefile(const std::string& filename, int fd) // AFAICT this is because tinfo==NULL when calling the callback in scap_read_fdlist struct scap_platform* platform = scap_savefile_alloc_platform(nullptr, nullptr); - open_common(&oargs, platform); + open_common(&oargs, &scap_savefile_engine, platform); #else throw sinsp_exception("SAVEFILE engine is not supported in this build"); #endif @@ -736,7 +736,7 @@ void sinsp::open_plugin(const std::string& plugin_name, const std::string& plugi default: throw sinsp_exception("Unsupported mode for SOURCE_PLUGIN engine"); } - open_common(&oargs, platform); + open_common(&oargs, &scap_source_plugin_engine, platform); #else throw sinsp_exception("SOURCE_PLUGIN engine is not supported in this build"); #endif @@ -759,7 +759,7 @@ void sinsp::open_gvisor(const std::string& config_path, const std::string& root_ oargs.engine_params = ¶ms; struct scap_platform* platform = scap_gvisor_alloc_platform(::on_new_entry_from_proc, this); - open_common(&oargs, platform); + open_common(&oargs, &scap_gvisor_engine, platform); set_get_procs_cpu_from_driver(false); #else @@ -784,7 +784,7 @@ void sinsp::open_modern_bpf(unsigned long driver_buffer_bytes_dim, uint16_t cpus oargs.engine_params = ¶ms; struct scap_platform* platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this); - open_common(&oargs, platform); + open_common(&oargs, &scap_modern_bpf_engine, platform); #else throw sinsp_exception("MODERN_BPF engine is not supported in this build"); #endif @@ -810,7 +810,7 @@ void sinsp::open_test_input(scap_test_input_data* data, scap_mode_t mode) default: throw sinsp_exception("Unsupported mode for TEST_INPUT engine"); } - open_common(&oargs, platform); + open_common(&oargs, &scap_test_input_engine, platform); set_get_procs_cpu_from_driver(false); #else diff --git a/userspace/libsinsp/sinsp.h b/userspace/libsinsp/sinsp.h index 298958d5db7..d867b309ba5 100644 --- a/userspace/libsinsp/sinsp.h +++ b/userspace/libsinsp/sinsp.h @@ -1042,7 +1042,7 @@ VISIBILITY_PRIVATE #endif void set_input_plugin(const std::string& name, const std::string& params); - void open_common(scap_open_args* oargs, struct scap_platform* platform); + void open_common(scap_open_args* oargs, const struct scap_vtable* vtable, struct scap_platform* platform); void init(); void deinit_state(); void consume_initialstate_events();