Skip to content

Commit

Permalink
new(sinsp)!: support linux_hostinfo_platform in sinsp::open_plugin
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gnosek committed Jul 22, 2024
1 parent c340ce4 commit 51e01cd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
28 changes: 18 additions & 10 deletions userspace/libsinsp/sinsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand All @@ -564,16 +565,23 @@ void sinsp::open_plugin(const std::string& plugin_name, const std::string& plugi
oargs.engine_params = &params;

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();

Check warning on line 577 in userspace/libsinsp/sinsp.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp.cpp#L575-L577

Added lines #L575 - L577 were not covered by tests
break;
case sinsp_plugin_platform::LINUX:
mode = SINSP_MODE_LIVE;
platform = scap_linux_alloc_platform(::on_new_entry_from_proc, this);

Check warning on line 581 in userspace/libsinsp/sinsp.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp.cpp#L579-L581

Added lines #L579 - L581 were not covered by tests
break;
default:

Check warning on line 583 in userspace/libsinsp/sinsp.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/sinsp.cpp#L583

Added line #L583 was not covered by tests
throw sinsp_exception("Unsupported mode for SOURCE_PLUGIN engine");
}
open_common(&oargs, &scap_source_plugin_engine, platform, mode);
#else
Expand Down
12 changes: 11 additions & 1 deletion userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
@{
*/
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ 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::GENERIC);

auto evt = next_event();
ASSERT_NE(evt, nullptr);
Expand Down Expand Up @@ -310,7 +310,7 @@ 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);

auto evt = next_event();
ASSERT_NE(evt, nullptr);
Expand Down

0 comments on commit 51e01cd

Please sign in to comment.