From ee8829ec4d07f4055063a79f8fd6465d2059fd77 Mon Sep 17 00:00:00 2001 From: Grzegorz Nosek Date: Mon, 22 Jul 2024 08:16:11 +0200 Subject: [PATCH] new(sinsp)!: support linux_hostinfo_platform in sinsp::open_plugin Rather than passing the mode directly, introduce a new enum that describes both the mode and the platform to use. Fixes: #2281 Signed-off-by: Grzegorz Nosek --- userspace/libsinsp/sinsp.cpp | 28 +++++++++++++++++--------- userspace/libsinsp/sinsp.h | 12 ++++++++++- userspace/libsinsp/test/plugins.ut.cpp | 13 ++++++++++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/userspace/libsinsp/sinsp.cpp b/userspace/libsinsp/sinsp.cpp index 86b722df41..05ecd40a00 100644 --- a/userspace/libsinsp/sinsp.cpp +++ b/userspace/libsinsp/sinsp.cpp @@ -553,7 +553,8 @@ void sinsp::open_savefile(const std::string& filename, int fd) #endif } -void sinsp::open_plugin(const std::string& plugin_name, const std::string& plugin_open_params, sinsp_mode_t mode) +void sinsp::open_plugin(const std::string& plugin_name, const std::string& plugin_open_params, + sinsp_plugin_platform platform_type) { #ifdef HAS_ENGINE_SOURCE_PLUGIN scap_open_args oargs {}; @@ -564,16 +565,23 @@ void sinsp::open_plugin(const std::string& plugin_name, const std::string& plugi oargs.engine_params = ¶ms; scap_platform* platform; - switch(mode) + sinsp_mode_t mode; + switch(platform_type) { - case SINSP_MODE_PLUGIN: - platform = scap_generic_alloc_platform(::on_new_entry_from_proc, this); - break; - case SINSP_MODE_LIVE: - platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this); - break; - default: - throw sinsp_exception("Unsupported mode for SOURCE_PLUGIN engine"); + case sinsp_plugin_platform::GENERIC: + mode = SINSP_MODE_PLUGIN; + platform = scap_generic_alloc_platform(::on_new_entry_from_proc, this); + break; + case sinsp_plugin_platform::LINUX_HOSTINFO: + mode = SINSP_MODE_PLUGIN; + platform = scap_linux_hostinfo_alloc_platform(); + break; + case sinsp_plugin_platform::LINUX: + mode = SINSP_MODE_LIVE; + platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this); + break; + default: + throw sinsp_exception("Unsupported mode for SOURCE_PLUGIN engine"); } open_common(&oargs, &scap_source_plugin_engine, platform, mode); #else diff --git a/userspace/libsinsp/sinsp.h b/userspace/libsinsp/sinsp.h index 66632da5ae..9dae016098 100644 --- a/userspace/libsinsp/sinsp.h +++ b/userspace/libsinsp/sinsp.h @@ -141,6 +141,16 @@ enum sinsp_mode_t SINSP_MODE_TEST, }; +/** + * @brief Possible platforms to use with plugins + */ +enum class sinsp_plugin_platform +{ + GENERIC, //!< generic platform, no system information collected + LINUX_HOSTINFO, //!< basic host information collected, for non-syscall source plugins + LINUX, //!< full system information collected, for syscall source plugins +}; + /** @defgroup inspector Main library @{ */ @@ -170,7 +180,7 @@ class SINSP_PUBLIC sinsp : public capture_stats_source virtual void open_nodriver(bool full_proc_scan = false); virtual void open_savefile(const std::string &filename, int fd = 0); virtual void open_plugin(const std::string& plugin_name, const std::string& plugin_open_params, - sinsp_mode_t mode = SINSP_MODE_PLUGIN); + sinsp_plugin_platform platform_type); virtual void open_gvisor(const std::string &config_path, const std::string &root_path, bool no_events = false, int epoll_timeout = -1); /*[EXPERIMENTAL] This API could change between releases, we are trying to find the right configuration to deploy the modern bpf probe: * `cpus_for_each_buffer` and `online_only` are the 2 experimental params. The first one allows associating more than one CPU to a single ring buffer. diff --git a/userspace/libsinsp/test/plugins.ut.cpp b/userspace/libsinsp/test/plugins.ut.cpp index 1821ddc35d..dd5e353a42 100644 --- a/userspace/libsinsp/test/plugins.ut.cpp +++ b/userspace/libsinsp/test/plugins.ut.cpp @@ -273,7 +273,13 @@ TEST_F(sinsp_with_test_input, plugin_syscall_source) // we will not use the test scap engine here, but open the src plugin instead // note: we configure the plugin to just emit 1 event through its open params - m_inspector.open_plugin(src_pl->name(), "1"); + m_inspector.open_plugin(src_pl->name(), "1", sinsp_plugin_platform::LINUX_HOSTINFO); + +#ifdef __linux__ + // The LINUX_HOSTINFO platform type fills in machine_info, but only on Linux + // (non-Linux platforms have a stub implementation in scap.c) + ASSERT_GT(m_inspector.get_machine_info()->num_cpus, 0); +#endif auto evt = next_event(); ASSERT_NE(evt, nullptr); @@ -310,7 +316,10 @@ TEST_F(sinsp_with_test_input, plugin_custom_source) // we will not use the test scap engine here, but open the src plugin instead // note: we configure the plugin to just emit 1 event through its open params - m_inspector.open_plugin(src_pl->name(), "1"); + m_inspector.open_plugin(src_pl->name(), "1", sinsp_plugin_platform::GENERIC); + + // the GENERIC platform type does not fill in machine_info + ASSERT_EQ(m_inspector.get_machine_info()->num_cpus, 0); auto evt = next_event(); ASSERT_NE(evt, nullptr);