Skip to content

Commit

Permalink
Disable metrics also in eBPF if disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickpichler committed Aug 7, 2024
1 parent 6885586 commit 95c9e65
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 66 deletions.
21 changes: 3 additions & 18 deletions pkg/ebpftracer/c/headers/common/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ statfunc int save_str_arr_to_buf(args_buffer_t *, const char __user *const __use
statfunc int save_args_str_arr_to_buf(args_buffer_t *, const char *, const char *, int, u8);
statfunc int save_sockaddr_to_buf(args_buffer_t *, struct socket *, u8);
statfunc int save_args_to_submit_buf(event_data_t *, args_t *);
statfunc int events_perf_submit(program_data_t *, u32 id, long);
statfunc int signal_perf_submit(void *, controlplane_signal_t *sig, u32 id);

#define events_perf_submit(p, id, ret) do_perf_submit(&events, p, id, ret)
#define signal_events_perf_submit(p, id, ret) do_perf_submit(&signal_events, p, id, ret)

// FUNCTIONS

Expand Down Expand Up @@ -581,20 +582,4 @@ statfunc int net_events_perf_submit(void *ctx, u32 id, event_data_t *event)
return bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, event, size);
}

statfunc int signal_perf_submit(void *ctx, controlplane_signal_t *sig, u32 id)
{
sig->event_id = id;

u32 size =
sizeof(u32) + sizeof(u8) + sig->args_buf.offset; // signal id + argnum + arg buffer size

// inline bounds check to force compiler to use the register of size
asm volatile("if %[size] < %[max_size] goto +1;\n"
"%[size] = %[max_size];\n"
:
: [size] "r"(size), [max_size] "i"(MAX_SIGNAL_SIZE));

return bpf_perf_event_output(ctx, &signals, BPF_F_CURRENT_CPU, sig, size);
}

#endif
1 change: 1 addition & 0 deletions pkg/ebpftracer/c/headers/common/consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef struct {
u64 flow_sample_submit_interval_seconds;
u64 flow_grouping;
bool track_syscall_stats;
bool export_metrics;
} global_config_t;

volatile const global_config_t global_config;
Expand Down
8 changes: 7 additions & 1 deletion pkg/ebpftracer/c/headers/common/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
#define __COMMON_METRICS_H__

#include "common/common.h"
#include "common/consts.h"
#include "types.h"

statfunc void metrics_increase(enum metric m)
{
if (!global_config.export_metrics) {
return;
}

if (m >= MAX_METRIC) {
enum metric unknown = UNKNOWN_METRIC;
u64 *counter = bpf_map_lookup_elem(&metrics, &unknown);
Expand All @@ -19,7 +24,8 @@ statfunc void metrics_increase(enum metric m)
if (unlikely(counter == NULL)) {
return;
}
// When the u64 overflows, we should start back at 0, so there is no need to think about reseting the counter.
// When the u64 overflows, we should start back at 0, so there is no need to think about
// reseting the counter.
__sync_fetch_and_add(counter, 1);
}

Expand Down
21 changes: 0 additions & 21 deletions pkg/ebpftracer/c/headers/common/signal.h

This file was deleted.

1 change: 0 additions & 1 deletion pkg/ebpftracer/c/headers/maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ BPF_LRU_HASH(bpf_attach_map, u32, bpf_used_helpers_t, 1024);
BPF_LRU_HASH(bpf_attach_tmp_map, u32, bpf_used_helpers_t, 1024); // temporarily hold bpf_used_helpers_t
BPF_LRU_HASH(bpf_prog_load_map, u32, void *, 1024); // store bpf prog aux pointer between bpf_check and security_bpf_prog
BPF_PERCPU_ARRAY(event_data_map, event_data_t, 1); // persist event related data
BPF_PERCPU_ARRAY(signal_data_map, controlplane_signal_t, 1); // signal scratch map
BPF_PERCPU_ARRAY(netflows_data_map, event_data_t, SCRATCH_MAP_SIZE); // netflows scratch map
BPF_HASH(logs_count, bpf_log_t, bpf_log_count_t, 4096); // logs count
BPF_PERCPU_ARRAY(scratch_map, scratch_t, 2); // scratch space to avoid allocating stuff on the stack
Expand Down
18 changes: 3 additions & 15 deletions pkg/ebpftracer/c/headers/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ enum event_id_e {
MODULE_LOAD,
MODULE_FREE,
SOCK_SET_STATE,
MAX_EVENT_ID,
PROCESS_OOM_KILLED,
TTY_OPEN,

MAX_EVENT_ID,
};

enum signal_event_id_e {
Expand Down Expand Up @@ -363,20 +364,7 @@ typedef struct event_data {
u64 in_use;
} event_data_t;

// A control plane signal - sent to indicate some critical event which should be processed
// with priority.
//
// Signals currently consist of shortened events sent only with their arguments.
// As such, they consist of an event id and an argument buffer.
// If we ever require a signal independent of an event, the event_id field should change
// accordingly.
typedef struct controlplane_signal {
u32 event_id;
args_buffer_t args_buf;
} controlplane_signal_t;

#define MAX_EVENT_SIZE sizeof(event_context_t) + sizeof(u8) + ARGS_BUF_SIZE
#define MAX_SIGNAL_SIZE sizeof(u32) + sizeof(u8) + ARGS_BUF_SIZE
#define MAX_EVENT_SIZE sizeof(event_context_t) + sizeof(u8) + ARGS_BUF_SIZE

#define BPF_MAX_LOG_FILE_LEN 72

Expand Down
1 change: 0 additions & 1 deletion pkg/ebpftracer/c/tracee.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <common/memory.h>
#include <common/network.h>
#include <common/probes.h>
#include <common/signal.h>
#include <common/debug.h>
#include <common/stats.h>
#include <common/metrics.h>
Expand Down
3 changes: 2 additions & 1 deletion pkg/ebpftracer/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ const (
ModuleLoad
ModuleFree
SockSetState
MaxCommonID
ProcessOomKilled
TtyOpen

MaxCommonID
)

// Events originated from user-space
Expand Down
1 change: 1 addition & 0 deletions pkg/ebpftracer/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (m *module) load(cfg Config) error {
FlowSampleSubmitIntervalSeconds: cfg.NetflowSampleSubmitIntervalSeconds,
FlowGrouping: uint64(cfg.NetflowGrouping),
TrackSyscallStats: cfg.TrackSyscallStats,
ExportMetrics: cfg.MetricsReportingEnabled,
},
}); err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions pkg/ebpftracer/tracer_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpftracer/tracer_arm64_bpfel.o
Binary file not shown.
6 changes: 2 additions & 4 deletions pkg/ebpftracer/tracer_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpftracer/tracer_x86_bpfel.o
Binary file not shown.

0 comments on commit 95c9e65

Please sign in to comment.