Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ebpf plugins #2049

Open
wants to merge 46 commits into
base: dev/ebpf_observe
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
d6e1030
add some utils
KayzzzZ Dec 27, 2024
04f6599
add protocol parser and aggregator
KayzzzZ Dec 27, 2024
d4aafbd
1. add some third party utils
KayzzzZ Jan 2, 2025
c290c30
update coolbpf and add driver layer framework
KayzzzZ Jan 2, 2025
feeb539
compile coolbpf and driver lib
KayzzzZ Jan 3, 2025
b575f94
export some functions for driver
KayzzzZ Jan 6, 2025
ca8b008
network observer start done ..
KayzzzZ Jan 8, 2025
bdd653d
network observer support log scenario
KayzzzZ Jan 8, 2025
a660ac8
fix enterprise compile
KayzzzZ Jan 8, 2025
0186592
update
KayzzzZ Jan 9, 2025
e9b9297
move id allocator
KayzzzZ Jan 9, 2025
96a4cca
format && add some framework
KayzzzZ Jan 13, 2025
cf01f07
update
KayzzzZ Jan 13, 2025
58962e7
update
KayzzzZ Jan 13, 2025
b438364
update
KayzzzZ Jan 16, 2025
697b82c
update
KayzzzZ Jan 16, 2025
58ad778
update
KayzzzZ Jan 17, 2025
497b097
add aggregator ut and setup aggregator for networksecurity & filesecu…
KayzzzZ Jan 17, 2025
fa8bee1
add some annotations
KayzzzZ Jan 17, 2025
6313b5e
add lock
KayzzzZ Jan 17, 2025
0374e83
add data handler
KayzzzZ Jan 17, 2025
08b5201
update
KayzzzZ Jan 18, 2025
cdf4ff3
update
KayzzzZ Jan 19, 2025
65cca2c
fix conflict
KayzzzZ Jan 19, 2025
3176f41
update
KayzzzZ Jan 19, 2025
978b903
flush span
KayzzzZ Jan 20, 2025
4d15e92
fix url path
KayzzzZ Jan 20, 2025
0f916e7
set to event tag instead of event group tag
KayzzzZ Jan 21, 2025
3545ec5
update
KayzzzZ Jan 21, 2025
10217de
clang format
KayzzzZ Jan 21, 2025
364dbc2
add some unittests
KayzzzZ Jan 22, 2025
96f80a9
clone event
KayzzzZ Jan 23, 2025
8c0850b
rebase main
KayzzzZ Jan 23, 2025
ed80d72
update coolbpf
KayzzzZ Jan 23, 2025
0c94a5a
fix
KayzzzZ Jan 26, 2025
823a4f1
update
KayzzzZ Feb 7, 2025
3a6b2b1
Update submodule coolbpf to commit c24d3993c16051c00d86eae063958898e4…
yyuuttaaoo Feb 7, 2025
9696ee8
try to fix compile failed with -std=c90
yyuuttaaoo Feb 7, 2025
68ba6eb
fix stadalone binary compile issue with compatible mode
yyuuttaaoo Feb 8, 2025
6ab0ab9
fix build script
yyuuttaaoo Feb 8, 2025
08afa01
Merge remote-tracking branch 'upstream/main' into ql/add-coolbpf
yyuuttaaoo Feb 8, 2025
3503e15
add some uts
KayzzzZ Feb 8, 2025
e99eee7
fix timer unittest
KayzzzZ Feb 10, 2025
6c56bf6
enable privileged
KayzzzZ Feb 10, 2025
272b8cc
fix ut
KayzzzZ Feb 11, 2025
5bc8c4b
fix log format string and remove useless files
yyuuttaaoo Feb 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
}
},
"initializeCommand": ".devcontainer/gen_env.sh",
"privileged": true,
"mounts": [
{ "source": "/sys", "target": "/sys", "type": "bind" },
{ "source": "/", "target": "/logtail_host", "type": "bind" }
],
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
"onCreateCommand": "sudo chown -R $(id -un):$(id -gn) /root",
"customizations": {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-core-ut.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
run: docker build -t unittest_coverage -f ./docker/Dockerfile_coverage .

- name: Unit Test
run: docker run -v $(pwd):$(pwd) unittest_coverage bash -c "cd $(pwd) && make unittest_core"
run: docker run --privileged -v /sys:/sys -v /:/logtail_host -v $(pwd):$(pwd) unittest_coverage bash -c "cd $(pwd) && make unittest_core"

- name: Unit Test Coverage
run: docker run -v $(pwd):$(pwd) unittest_coverage bash -c "cd $(pwd)/core && gcovr --gcov-ignore-errors=no_working_dir_found --root . --json coverage.json --json-summary-pretty --json-summary summary.json -e \".*\.pb\.cc\" -e \".*\.pb\.h\" -e \".*unittest.*\" -e \".*sdk.*\" -e \".*logger.*\" -e \".*config_server.*\" -e \".*go_pipeline.*\" -e \".*application.*\" -e \".*runner.*\""
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
with:
clang-format-version: '18'
check-path: 'core'
exclude-regex: 'common/xxhash|labels/Relabel\.cpp|ProcessorParseContainerLogNative\.cpp|FlusherSLS\.cpp'
exclude-regex: 'common/xxhash|labels/Relabel\.cpp|ProcessorParseContainerLogNative\.cpp|FlusherSLS\.cpp|ebpf/driver/coolbpf'
include-regex: '.*\.(cpp|h)$'

- name: Go Plugin Lint
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "core/ebpf/driver/coolbpf"]
path = core/ebpf/driver/coolbpf
url = https://gitee.com/anolis/coolbpf.git
7 changes: 5 additions & 2 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ endif ()
# To be compatible with low version Linux.
if (ENABLE_COMPATIBLE_MODE)
message(STATUS "Enable compatible mode.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c90")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--wrap=memcpy")
add_definitions(-DENABLE_COMPATIBLE_MODE)
endif ()
Expand Down Expand Up @@ -127,7 +126,7 @@ set(SUB_DIRECTORIES_LIST
protobuf/sls protobuf/models
file_server file_server/event file_server/event_handler file_server/event_listener file_server/reader file_server/polling
prometheus prometheus/labels prometheus/schedulers prometheus/async prometheus/component
ebpf ebpf/observer ebpf/security ebpf/handler
ebpf ebpf/util ebpf/util/sampler ebpf/protocol/http ebpf/protocol ebpf/plugin/file_security ebpf/plugin/network_observer ebpf/plugin/process_security ebpf/plugin/network_security ebpf/plugin ebpf/observer ebpf/security ebpf/handler
parser
)
if (LINUX)
Expand All @@ -145,6 +144,7 @@ endif()

# Module includes & set files.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories("${DEPS_INCLUDE_ROOT}/coolbpf")

foreach (DIR_NAME ${SUB_DIRECTORIES_LIST})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/${DIR_NAME})
Expand Down Expand Up @@ -221,6 +221,8 @@ endif ()
# Generate independent libraries.
add_subdirectory(go_pipeline)
add_subdirectory(common)
# Build eBPF dependencies
add_subdirectory(ebpf/driver)

# Link libraries.
if(BUILD_LOGTAIL OR BUILD_LOGTAIL_SHARED_LIBRARY)
Expand All @@ -230,6 +232,7 @@ if(BUILD_LOGTAIL OR BUILD_LOGTAIL_SHARED_LIBRARY)
all_link(${LOGTAIL_TARGET})
common_link(${LOGTAIL_TARGET})
target_link_libraries(${LOGTAIL_TARGET} provider)
add_dependencies(${LOGTAIL_TARGET} install_coolbpf)
endif()

# Logtail UT.
Expand Down
6 changes: 6 additions & 0 deletions core/app_config/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ DEFINE_FLAG_STRING(metrics_report_method,
DEFINE_FLAG_STRING(operator_service, "loong collector operator service", "");
DEFINE_FLAG_INT32(operator_service_port, "loong collector operator service port", 8888);
DEFINE_FLAG_INT32(k8s_meta_service_port, "loong collector operator service port", 9000);

DEFINE_FLAG_STRING(loong_collector_singleton_service, "loong collector singleton service", "loongcollector-singleton");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

命名都去掉loong_collector_

DEFINE_FLAG_INT32(loong_collector_singleton_port, "loong collector singleton service port", 8899);
DEFINE_FLAG_STRING(loong_collector_operator_service, "loong collector operator service", "");
DEFINE_FLAG_INT32(loong_collector_operator_service_port, "loong collector operator service port", 8888);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个和上面重复了

DEFINE_FLAG_INT32(loong_collector_k8s_meta_service_port, "loong collector operator service port", 9000);
DEFINE_FLAG_STRING(_pod_name_, "agent pod name", "");

DEFINE_FLAG_STRING(app_info_file, "", "app_info.json");
Expand Down
31 changes: 30 additions & 1 deletion core/collection_pipeline/serializer/SLSSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,30 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri
case PipelineEvent::Type::METRIC: {
for (size_t i = 0; i < group.mEvents.size(); ++i) {
const auto& e = group.mEvents[i].Cast<MetricEvent>();
for (auto tag = e.TagsBegin(); tag != e.TagsEnd(); tag++) {
LOG_DEBUG(sLogger,
("event tags for metricname", std::string(e.GetName()))
(std::string(tag->first), std::string(tag->second))
);
}
for (auto tag = group.mTags.mInner.begin(); tag != group.mTags.mInner.end(); tag++) {
LOG_DEBUG(sLogger,
("group tags for metricname", std::string(e.GetName()))
(std::string(tag->first), std::string(tag->second))
);
}


if (e.Is<UntypedSingleValue>()) {
metricEventContentCache[i].first = to_string(e.GetValue<UntypedSingleValue>()->mValue);
// should not happen
LOG_ERROR(sLogger,
("config", mFlusher->GetContext().GetConfigName()) ("metricname", e.GetName()));
} else {
// should not happen
LOG_ERROR(sLogger,
("unexpected error",
"invalid metric event type")("config", mFlusher->GetContext().GetConfigName()));
"invalid metric event type")("config", mFlusher->GetContext().GetConfigName()) ("metricname", e.GetName()));
continue;
}
metricEventContentCache[i].second = GetMetricLabelSize(e);
Expand All @@ -141,6 +158,18 @@ bool SLSEventGroupSerializer::Serialize(BatchedEvents&& group, string& res, stri
case PipelineEvent::Type::SPAN:
for (size_t i = 0; i < group.mEvents.size(); ++i) {
const auto& e = group.mEvents[i].Cast<SpanEvent>();
for (auto tag = e.TagsBegin(); tag != e.TagsEnd(); tag++) {
LOG_DEBUG(sLogger,
("event tags for spanname", std::string(e.GetName()))
(std::string(tag->first), std::string(tag->second))
);
}
for (auto tag = group.mTags.mInner.begin(); tag != group.mTags.mInner.end(); tag++) {
LOG_DEBUG(sLogger,
("group tags for spanname", std::string(e.GetName()))
(std::string(tag->first), std::string(tag->second))
);
}
size_t contentSZ = 0;
contentSZ += GetLogContentSize(DEFAULT_TRACE_TAG_TRACE_ID.size(), e.GetTraceId().size());
contentSZ += GetLogContentSize(DEFAULT_TRACE_TAG_SPAN_ID.size(), e.GetSpanId().size());
Expand Down
109 changes: 109 additions & 0 deletions core/common/CapabilityUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2023 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "CapabilityUtil.h"
#include <unordered_map>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <sstream>

namespace logtail {

std::unordered_map<uint64_t, std::string> capabilitiesString = {
{0, "CAP_CHOWN"},
{1, "DAC_OVERRIDE"},
{2, "CAP_DAC_READ_SEARCH"},
{3, "CAP_FOWNER"},
{4, "CAP_FSETID"},
{5, "CAP_KILL"},
{6, "CAP_SETGID"},
{7, "CAP_SETUID"},
{8, "CAP_SETPCAP"},
{9, "CAP_LINUX_IMMUTABLE"},
{10, "CAP_NET_BIND_SERVICE"},
{11, "CAP_NET_BROADCAST"},
{12, "CAP_NET_ADMIN"},
{13, "CAP_NET_RAW"},
{14, "CAP_IPC_LOCK"},
{15, "CAP_IPC_OWNER"},
{16, "CAP_SYS_MODULE"},
{17, "CAP_SYS_RAWIO"},
{18, "CAP_SYS_CHROOT"},
{19, "CAP_SYS_PTRACE"},
{20, "CAP_SYS_PACCT"},
{21, "CAP_SYS_ADMIN"},
{22, "CAP_SYS_BOOT"},
{23, "CAP_SYS_NICE"},
{24, "CAP_SYS_RESOURCE"},
{25, "CAP_SYS_TIME"},
{26, "CAP_SYS_TTY_CONFIG"},
{27, "CAP_MKNOD"},
{28, "CAP_LEASE"},
{29, "CAP_AUDIT_WRITE"},
{30, "CAP_AUDIT_CONTROL"},
{31, "CAP_SETFCAP"},
{32, "CAP_MAC_OVERRIDE"},
{33, "CAP_MAC_ADMIN"},
{34, "CAP_SYSLOG"},
{35, "CAP_WAKE_ALARM"},
{36, "CAP_BLOCK_SUSPEND"},
{37, "CAP_AUDIT_READ"},
{38, "CAP_PERFMON"},
{39, "CAP_BPF"},
{40, "CAP_CHECKPOINT_RESTORE"}
};

const int32_t CAP_LAST_CAP = 40;

bool IsCapValid(int32_t capInt) {
return (capInt >= 0 && capInt <= CAP_LAST_CAP);
}

std::string GetCapability(int32_t capInt) {
if (!IsCapValid(capInt)) {
throw std::invalid_argument("invalid capability value " + std::to_string(capInt));
}

auto it = capabilitiesString.find(static_cast<uint64_t>(capInt));
if (it == capabilitiesString.end()) {
throw std::invalid_argument("could not map capability value " + std::to_string(capInt));
}

return it->second;
}

std::string GetCapabilities(uint64_t capInt) {
std::vector<std::string> caps;

for (uint64_t i = 0; i < 64; ++i) {
if ((1ULL << i) & capInt) {
auto it = capabilitiesString.find(i);
if (it != capabilitiesString.end()) {
caps.push_back(it->second);
}
}
}

std::ostringstream oss;
for (size_t i = 0; i < caps.size(); ++i) {
if (i > 0) {
oss << " ";
}
oss << caps[i];
}
return oss.str();
}

}
25 changes: 25 additions & 0 deletions core/common/CapabilityUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <string>

namespace logtail {

std::string GetCapabilities(uint64_t capInt);

std::string GetCapability(int32_t capInt);

}
15 changes: 1 addition & 14 deletions core/common/LRUCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,9 @@ class Cache {
* directly anyway! :)
*/
explicit Cache(size_t maxSize = 64, size_t elasticity = 10)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件本身是不是应该放到opt目录里

: maxSize_(maxSize), elasticity_(elasticity), prune_thread(&Cache::pruneThreadFunc, this) {}
: maxSize_(maxSize), elasticity_(elasticity) {}

virtual ~Cache() {
stop_pruning = true;
if (prune_thread.joinable()) {
prune_thread.join();
}
}

size_t size() const {
Expand Down Expand Up @@ -252,13 +248,6 @@ class Cache {
return count;
}

void pruneThreadFunc() {
while (!stop_pruning) {
pruneExpired();
std::this_thread::sleep_for(std::chrono::seconds(60)); // 每60秒检查一次
}
}

private:
// Disallow copying.
Cache(const Cache&) = delete;
Expand All @@ -269,8 +258,6 @@ class Cache {
list_type keys_;
size_t maxSize_;
size_t elasticity_;
std::atomic<bool> stop_pruning{false};
std::thread prune_thread;

#ifdef APSARA_UNIT_TEST_MAIN
friend class LRUCacheUnittest;
Expand Down
Loading