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 Aug 1, 2024
1 parent 90b888f commit 2118fcd
Show file tree
Hide file tree
Showing 3 changed files with 40 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::SINSP_PLATFORM_GENERIC:
mode = SINSP_MODE_PLUGIN;
platform = scap_generic_alloc_platform(::on_new_entry_from_proc, this);
break;
case sinsp_plugin_platform::SINSP_PLATFORM_HOSTINFO:
mode = SINSP_MODE_PLUGIN;
platform = scap_linux_hostinfo_alloc_platform();
break;
case sinsp_plugin_platform::SINSP_PLATFORM_FULL:
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 @@ -139,6 +139,16 @@ enum sinsp_mode_t
SINSP_MODE_TEST,
};

/**
* @brief Possible platforms to use with plugins
*/
enum class sinsp_plugin_platform
{
SINSP_PLATFORM_GENERIC, //!< generic platform, no system information collected
SINSP_PLATFORM_HOSTINFO, //!< basic host information collected, for non-syscall source plugins
SINSP_PLATFORM_FULL, //!< full system information collected, for syscall source plugins
};

/** @defgroup inspector Main library
@{
*/
Expand Down Expand Up @@ -168,7 +178,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
13 changes: 11 additions & 2 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::SINSP_PLATFORM_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);
Expand Down Expand Up @@ -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::SINSP_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);
Expand Down

0 comments on commit 2118fcd

Please sign in to comment.