From 83a09f8a7c34fd6853daa31f9dc4f460f29decb3 Mon Sep 17 00:00:00 2001 From: Mauro Ezequiel Moltrasio Date: Wed, 18 Oct 2023 13:42:55 +0200 Subject: [PATCH] Add ARM64 heuristic --- collector/lib/HostHeuristics.cpp | 13 ++++++++++++ collector/test/HostHeuristicsTest.cpp | 30 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/collector/lib/HostHeuristics.cpp b/collector/lib/HostHeuristics.cpp index 7aa0c20337..c303b49ae5 100644 --- a/collector/lib/HostHeuristics.cpp +++ b/collector/lib/HostHeuristics.cpp @@ -87,10 +87,23 @@ class S390XHeuristic : public Heuristic { } }; +class ARM64Heuristic : public Heuristic { + public: + void Process(HostInfo& host, const CollectorConfig& config, HostConfig* hconfig) const { + auto kernel = host.GetKernelVersion(); + + if (kernel.machine == "aarch64" && config.GetCollectionMethod() == CollectionMethod::EBPF) { + CLOG(WARNING) << "eBPF collection method is not supported on ARM, switching to CO-RE BPF collection method."; + hconfig->SetCollectionMethod(CollectionMethod::CORE_BPF); + } + } +}; + const std::unique_ptr g_host_heuristics[] = { std::unique_ptr(new CollectionHeuristic), std::unique_ptr(new DockerDesktopHeuristic), std::unique_ptr(new S390XHeuristic), + std::unique_ptr(new ARM64Heuristic), }; } // namespace diff --git a/collector/test/HostHeuristicsTest.cpp b/collector/test/HostHeuristicsTest.cpp index 8b591237f3..e685d72868 100644 --- a/collector/test/HostHeuristicsTest.cpp +++ b/collector/test/HostHeuristicsTest.cpp @@ -36,6 +36,11 @@ class MockS390xHeuristics : public S390XHeuristic { MockS390xHeuristics() = default; }; +class MockARM64Heuristics : public ARM64Heuristic { + public: + MockARM64Heuristics() = default; +}; + class MockHostInfoHeuristics : public HostInfo { public: MockHostInfoHeuristics() = default; @@ -56,6 +61,13 @@ class MockCollectorConfig : public CollectorConfig { : CollectorConfig(collectorArgs){}; MOCK_CONST_METHOD0(UseEbpf, bool()); + + void SetCollectionMethod(CollectionMethod cm) { + if (host_config_.HasCollectionMethod()) { + host_config_.SetCollectionMethod(cm); + } + collection_method_ = cm; + } }; TEST(HostHeuristicsTest, TestS390XRHEL84) { @@ -66,6 +78,7 @@ TEST(HostHeuristicsTest, TestS390XRHEL84) { MockCollectorConfig config(args); HostConfig hconfig; + config.SetCollectionMethod(CollectionMethod::EBPF); hconfig.SetCollectionMethod(CollectionMethod::EBPF); EXPECT_CALL(host, GetKernelVersion()).WillOnce(Return(version)); @@ -74,4 +87,21 @@ TEST(HostHeuristicsTest, TestS390XRHEL84) { EXPECT_EQ(hconfig.GetCollectionMethod(), CollectionMethod::CORE_BPF); } +TEST(HostHeuristicsTest, TestARM64Heuristic) { + MockARM64Heuristics heuristic; + MockHostInfoHeuristics host; + KernelVersion version = KernelVersion("6.5.5-200.fc38.aarch64", "", "aarch64"); + CollectorArgs* args = CollectorArgs::getInstance(); + MockCollectorConfig config(args); + HostConfig hconfig; + + config.SetCollectionMethod(CollectionMethod::EBPF); + hconfig.SetCollectionMethod(CollectionMethod::EBPF); + EXPECT_CALL(host, GetKernelVersion()).WillOnce(Return(version)); + + heuristic.Process(host, config, &hconfig); + + EXPECT_EQ(hconfig.GetCollectionMethod(), CollectionMethod::CORE_BPF); +} + } // namespace collector