opensnitch/ebpf_prog/event_stats.h
trvon 0be4f7c4c4
daemon: event-loss diagnostics via per-module event_stats counters
Add fixed-size, process-lifetime counters (received/enqueued/dropped/
drained/dequeued/decoded/decode-errors/reader-errors/canceled) for the
process and DNS eBPF event paths, exposed as individually-atomic
snapshots. Wire DNSEventCounters and ProcessEventCounters through the
perf reader/enqueue/decode loop in daemon/dns and daemon/procmon/ebpf,
with a matching ebpf_prog/event_stats.h and a structural eBPF test.
Counters hold totals only, never event payloads.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-05 16:58:11 -07:00

30 lines
937 B
C

#ifndef OPENSNITCH_EVENT_STATS_H
#define OPENSNITCH_EVENT_STATS_H
/* Include after the module's events map. Fixed cardinality, no payloads.
* Keep this value ABI in sync with daemon/core/event_stats.go. */
struct event_emission_counts {
u64 attempted;
u64 failed;
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, u32);
__type(value, struct event_emission_counts);
__uint(max_entries, 1);
} event_stats SEC(".maps");
static __always_inline long emit_counted_event(void *data, u64 size)
{
long ret = bpf_ringbuf_output(&events, data, size, 0);
u32 key = 0;
struct event_emission_counts *counts = bpf_map_lookup_elem(&event_stats, &key);
if (counts) {
__sync_fetch_and_add(&counts->attempted, 1);
if (ret != 0)
__sync_fetch_and_add(&counts->failed, 1);
}
/* Diagnostic accounting cannot replace or retry an emission. */
return ret;
}
#endif