-
Notifications
You must be signed in to change notification settings - Fork 442
cgroups: add basic cgroups tracking and make it part of the testing framework #471
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
20f4ff9
bpf:cgroup: add Cgroup operations definition
tixxdz 2f5874d
bpf:cgroup: add cgroups tracking structures and maps
tixxdz 5c1e8f0
bpf:cgroups: add cgroup helpers to retrieve cgroup level and ancestors
tixxdz 95c833b
bpf:conf: update tetragon_conf with necessary fields
tixxdz 5c3d841
bpf::cgroup: add send cgroup events helper
tixxdz f2584ea
bpf:cgroup: add bpf programs to track cgroup creation and deletion
tixxdz 697d9de
bpf:cgroup: track cgroups that belong to the right hierarchy
tixxdz 7bbc00d
bpf:make: compile new bpf cgroup programs
tixxdz 5af6747
tetragon:conf: store loglevel and cgroupFsMagic into BPF conf
tixxdz cdb50fe
pkg:api: add Cgroup message definition
tixxdz ad8d8eb
pkg:api: add Cgroup operation and message definitions
tixxdz 6f1c2ca
pkg:sensors: register cgroup events handler
tixxdz cac2771
pkg:sensors: add cgrouptrackmap to read tracked cgroups from bpf maps
tixxdz 4a076f3
pkg:sensors: add helper to read Tetragon Runtime configuration
tixxdz 3bb2c46
pkg:observer: pass custom BPF maps root dir to UpdateRuntimeConf()
tixxdz 51cd989
pkg:cgroups: add GetCgroupFSPath() to get real cgroupfs path
tixxdz 8fb1389
tests:ops: add Cgroup Operation code and state test
tixxdz d3003aa
tests:processapi: check Cgroup Structs against alignchecker
tixxdz 9a56c51
tests:cgrouptrackmap: check CgrpTrackingValue struct against alignche…
tixxdz 931c795
tests:cgrouptrackmap: add test for DeepCopyMapValue()
tixxdz ffd54d3
tests:cgroups: assert c strings conversion to golang ones
tixxdz 9a100ac
tests:cgroups: test cgroupfs path and magic number helpers
tixxdz ac8e0aa
tests:sensors: add bpf cgroups sensor to test
tixxdz 28d6815
tests:cgroups: add load cgroup bpf programs test
tixxdz 44372c5
tests:sensors: add TestTgRuntimeConf() to test runtime conf
tixxdz 59654da
tests:cgroups: ensure that we do not get bpf cgroup events
tixxdz 84fc957
tests:cgroups: ensure that we get cgroup mkdir and rmdir events
tixxdz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright Authors of Tetragon */ | ||
|
||
#ifndef _BPF_CGROUP_EVENTS__ | ||
#define _BPF_CGROUP_EVENTS__ | ||
|
||
#include "bpf_helpers.h" | ||
#include "bpf_events.h" | ||
#include "environ_conf.h" | ||
|
||
/* This function will send the cgroup events to the ring buffer */ | ||
static inline __attribute__((always_inline)) int | ||
send_cgrp_event(struct bpf_raw_tracepoint_args *ctx, | ||
struct cgroup_tracking_value *cgrp_track, __u64 cgrpid, | ||
__u32 op) | ||
{ | ||
pid_t pid; | ||
char *path; | ||
int zero = 0; | ||
uint64_t size; | ||
struct execve_map_value *curr; | ||
struct msg_cgroup_event *msg; | ||
|
||
msg = map_lookup_elem(&tg_cgrps_msg_heap, &zero); | ||
if (!msg) | ||
return 0; | ||
|
||
size = sizeof(struct msg_cgroup_event); | ||
msg->common.op = MSG_OP_CGROUP; | ||
msg->common.size = size; | ||
|
||
path = (char *)ctx->args[1]; | ||
pid = (get_current_pid_tgid() >> 32); | ||
|
||
curr = execve_map_get(pid); | ||
if (curr) { | ||
msg->common.ktime = curr->key.ktime; | ||
msg->parent = curr->pkey; | ||
msg->flags = curr->flags; | ||
msg->ktime = curr->key.ktime; | ||
} | ||
msg->cgrp_op = op; | ||
msg->pid = pid; | ||
msg->nspid = get_task_pid_vnr(); | ||
msg->cgrpid = cgrpid; | ||
/* It is same as we are not tracking nested cgroups */ | ||
msg->cgrpid_tracker = cgrpid; | ||
msg->cgrp_data.state = cgrp_track->state; | ||
msg->cgrp_data.level = cgrp_track->level; | ||
msg->cgrp_data.hierarchy_id = cgrp_track->hierarchy_id; | ||
memcpy(&msg->cgrp_data.name, &cgrp_track->name, KN_NAME_LENGTH); | ||
probe_read_str(&msg->path, PATH_MAP_SIZE - 1, path); | ||
|
||
perf_event_output(ctx, &tcpmon_map, BPF_F_CURRENT_CPU, msg, size); | ||
|
||
return 0; | ||
} | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright Authors of Tetragon */ | ||
|
||
#include "vmlinux.h" | ||
#include "api.h" | ||
|
||
#include "hubble_msg.h" | ||
#include "bpf_cgroup.h" | ||
#include "bpf_events.h" | ||
#include "bpf_cgroup_events.h" | ||
|
||
char _license[] __attribute__((section(("license")), used)) = "GPL"; | ||
#ifdef VMLINUX_KERNEL_VERSION | ||
int _version __attribute__((section(("version")), used)) = | ||
VMLINUX_KERNEL_VERSION; | ||
#endif | ||
|
||
__attribute__((section(("raw_tracepoint/cgroup_mkdir")), used)) int | ||
tg_tp_cgrp_mkdir(struct bpf_raw_tracepoint_args *ctx) | ||
{ | ||
uint64_t cgrpid; | ||
int level, hierarchy_id, zero = 0; | ||
struct cgroup *cgrp; | ||
struct cgroup_tracking_value *cgrp_heap; | ||
struct tetragon_conf *config; | ||
|
||
config = map_lookup_elem(&tg_conf_map, &zero); | ||
if (!config || config->tg_cgrp_level == 0) | ||
return 0; | ||
|
||
cgrp = (struct cgroup *)ctx->args[0]; | ||
|
||
hierarchy_id = get_cgroup_hierarchy_id(cgrp); | ||
/* | ||
* In a cgroupv1 setup, there can be multiple cgroup hierarchies but | ||
* we want to track only one If this is not the hierarchy we care | ||
* about, exit. | ||
*/ | ||
if (config->tg_cgrp_hierarchy != hierarchy_id) | ||
return 0; | ||
|
||
level = get_cgroup_level(cgrp); | ||
/* This should never happen as the cgroup hierarchy has already been | ||
* set (e.g., by systemd) | ||
*/ | ||
if (level == 0) | ||
return 0; | ||
|
||
cgrpid = get_cgroup_id(cgrp); | ||
/* This should never happen unless the bpf helper failed */ | ||
if (cgrpid == 0) | ||
return 0; | ||
|
||
/* We want to track all processes of a container system so that we can | ||
* provide proper identity to events. To do that, we use a certain cgroup | ||
* level. Any cgroups that are created under that level, we ignore. | ||
* That is, if we are monitoring level 5, we do not care about cgroup | ||
* events with level >5. | ||
*/ | ||
if (level <= config->tg_cgrp_level) { | ||
cgrp_heap = __init_cgrp_tracking_val_heap(cgrp, CGROUP_NEW); | ||
if (!cgrp_heap) | ||
return 0; | ||
|
||
/* We track only for now cgroups that are at same or above tetragon | ||
* level (ancestors level) | ||
*/ | ||
map_update_elem(&tg_cgrps_tracking_map, &cgrpid, cgrp_heap, | ||
BPF_ANY); | ||
|
||
/* We forward bpf events only under TraceLevel */ | ||
if (unlikely(config->loglevel == LOG_TRACE_LEVEL)) | ||
send_cgrp_event(ctx, cgrp_heap, cgrpid, | ||
MSG_OP_CGROUP_MKDIR); | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright Authors of Tetragon */ | ||
|
||
#include "vmlinux.h" | ||
#include "api.h" | ||
|
||
#include "hubble_msg.h" | ||
#include "bpf_cgroup.h" | ||
#include "bpf_events.h" | ||
#include "bpf_cgroup_events.h" | ||
|
||
char _license[] __attribute__((section(("license")), used)) = "GPL"; | ||
#ifdef VMLINUX_KERNEL_VERSION | ||
int _version __attribute__((section(("version")), used)) = | ||
VMLINUX_KERNEL_VERSION; | ||
#endif | ||
|
||
/* Ensure to remove tracked cgroups from bpf map */ | ||
__attribute__((section(("raw_tracepoint/cgroup_release")), used)) int | ||
tg_tp_cgrp_release(struct bpf_raw_tracepoint_args *ctx) | ||
{ | ||
int zero = 0; | ||
uint64_t cgrpid; | ||
struct cgroup *cgrp; | ||
struct tetragon_conf *conf; | ||
struct cgroup_tracking_value *cgrp_track; | ||
|
||
cgrp = (struct cgroup *)ctx->args[0]; | ||
cgrpid = get_cgroup_id(cgrp); | ||
/* This should never happen unless our helper failed */ | ||
if (cgrpid == 0) | ||
return 0; | ||
tixxdz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
cgrp_track = map_lookup_elem(&tg_cgrps_tracking_map, &cgrpid); | ||
/* TODO: check cgroup level if it is under our tracking level | ||
* then we probably did miss it and should report this. | ||
* Otherwise the cgroup was never tracked and let's exit. | ||
*/ | ||
if (!cgrp_track) | ||
return 0; | ||
tixxdz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
map_delete_elem(&tg_cgrps_tracking_map, &cgrpid); | ||
|
||
conf = map_lookup_elem(&tg_conf_map, &zero); | ||
if (!conf) | ||
return 0; | ||
|
||
/* We forward bpf events only under TraceLevel */ | ||
if (unlikely(conf->loglevel == LOG_TRACE_LEVEL)) | ||
send_cgrp_event(ctx, cgrp_track, cgrpid, MSG_OP_CGROUP_RELEASE); | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright Authors of Tetragon */ | ||
|
||
#include "vmlinux.h" | ||
#include "api.h" | ||
|
||
#include "hubble_msg.h" | ||
#include "bpf_cgroup.h" | ||
#include "bpf_events.h" | ||
#include "bpf_cgroup_events.h" | ||
|
||
char _license[] __attribute__((section(("license")), used)) = "GPL"; | ||
#ifdef VMLINUX_KERNEL_VERSION | ||
int _version __attribute__((section(("version")), used)) = | ||
VMLINUX_KERNEL_VERSION; | ||
#endif | ||
|
||
/* Remove tracked cgroups from bpf map */ | ||
__attribute__((section(("raw_tracepoint/cgroup_rmdir")), used)) int | ||
tg_tp_cgrp_rmdir(struct bpf_raw_tracepoint_args *ctx) | ||
{ | ||
int zero = 0; | ||
uint64_t cgrpid; | ||
struct cgroup *cgrp; | ||
struct tetragon_conf *conf; | ||
struct cgroup_tracking_value *cgrp_track; | ||
|
||
cgrp = (struct cgroup *)ctx->args[0]; | ||
cgrpid = get_cgroup_id(cgrp); | ||
/* This should never happen unless the bpf helper failed */ | ||
if (cgrpid == 0) | ||
return 0; | ||
|
||
cgrp_track = map_lookup_elem(&tg_cgrps_tracking_map, &cgrpid); | ||
/* TODO: check cgroup level if it is under our tracking level | ||
* then we probably did miss it and should report this. | ||
* Otherwise the cgroup was never tracked and let's exit. | ||
*/ | ||
if (!cgrp_track) | ||
return 0; | ||
|
||
map_delete_elem(&tg_cgrps_tracking_map, &cgrpid); | ||
|
||
conf = map_lookup_elem(&tg_conf_map, &zero); | ||
if (!conf) | ||
return 0; | ||
|
||
/* We forward bpf events only under TraceLevel */ | ||
if (unlikely(conf->loglevel == LOG_TRACE_LEVEL)) | ||
send_cgrp_event(ctx, cgrp_track, cgrpid, MSG_OP_CGROUP_RMDIR); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.