// Project Calico BPF dataplane programs.
// Copyright (c) 2026 Tigera, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later

#ifndef __CALI_RINGBUF_H__
#define __CALI_RINGBUF_H__

#include "cali_bpf.h"
#include "events_type.h"

// Ring buffer map macro — ring buffers have no key/value types and no
// lookup/update/delete helpers. max_entries is the buffer size in bytes.
#define CALI_RINGBUF(name, size_bytes)		\
struct {					\
	__uint(type, BPF_MAP_TYPE_RINGBUF);	\
	__uint(max_entries, size_bytes);		\
} name SEC(".maps");

// Shared event ring buffer.
// The 1MB compile-time value is a fallback default. At runtime, Go overrides
// max_entries via maps.SetSize() -> bpf_map__set_max_entries() to
// BPFExportBufferSizeMB * NumPossibleCPUs (preserving the same total buffer
// capacity as the old per-CPU perf event array). For ring buffers,
// max_entries IS the buffer size in bytes.
CALI_RINGBUF(cali_rb_evnt, 1024 * 1024)

// Shared drop counter and flush timestamp.
// Drops are emitted as EVENT_LOST_EVENTS through the ring buffer itself;
// Go never reads this map directly.
//
// No bpf_spin_lock: kprobe/tracing programs cannot reference maps whose
// value type contains a spin lock. Instead, count uses atomic XCHG
// (__sync_lock_test_and_set) for the read-and-zero in flush, and
// __sync_fetch_and_add for increments.
#define CALI_RB_FLUSH_INTERVAL_NS (5ULL * 1000000000ULL) /* 5 seconds */

struct rb_drops_val {
	__u64 count;
	__u64 last_flush_ts;
};

CALI_MAP_V1(cali_rb_drops, BPF_MAP_TYPE_ARRAY, __u32, struct rb_drops_val, 1, 0)

// ringbuf_bump_drops_at increments the drop counter for the given drops-map
// value pointer.  Centralised so any future change to the synchronisation
// primitive (e.g. swapping __sync builtins for explicit-ordering atomics)
// touches one site for every ring buffer that consumes this header.
// NULL-tolerant: BPF map lookups can in principle return NULL even for
// single-entry array maps, and the caller would otherwise have to repeat
// the guard.
static CALI_BPF_INLINE void ringbuf_bump_drops_at(struct rb_drops_val *val)
{
	if (val) {
		__sync_fetch_and_add(&val->count, 1);
	}
}

// ringbuf_flush_drops_to emits the accumulated drop count for `val` as an
// EVENT_LOST_EVENTS event through the ring buffer pointed to by `rb`, at
// most once every CALI_RB_FLUSH_INTERVAL_NS. Shared between ring buffers
// so the rate-limiting and atomic-restore semantics live in one place.
// Per-map wrappers handle the typed lookup that a generic helper can't
// share (the lookup function name is macro-expanded).
static CALI_BPF_INLINE void ringbuf_flush_drops_to(void *rb, struct rb_drops_val *val)
{
	if (!val) {
		return;
	}

	__u64 now = bpf_ktime_get_ns();
	if (now - val->last_flush_ts < CALI_RB_FLUSH_INTERVAL_NS) {
		return;
	}

	__u64 dropped = __sync_lock_test_and_set(&val->count, 0);
	if (dropped == 0) {
		return;
	}

	/* Emit the accumulated drop count as a lost-events notification. */
	struct {
		struct event_header hdr;
		__u64 count;
	} evt = {
		.hdr = {
			.type = EVENT_LOST_EVENTS,
			.len  = sizeof(evt),
		},
		.count = dropped,
	};
	if (bpf_ringbuf_output(rb, &evt, sizeof(evt), 0) != 0) {
		/* Ring still full — restore the counter so drops are not lost.
		 * Concurrent increments between unlock and here are preserved:
		 * the restore adds back the original amount on top of any new
		 * increments, so the total remains accurate. */
		__sync_fetch_and_add(&val->count, dropped);
		return;
	}

	/* No lock needed: if two CPUs write last_flush_ts concurrently,
	 * both values are near-identical (same bpf_ktime_get_ns epoch),
	 * so the difference is irrelevant for a 5-second interval. */
	val->last_flush_ts = now;
}

// Per-map wrapper for the shared event ring buffer.
static CALI_BPF_INLINE void ringbuf_flush_drops(void)
{
	__u32 key = 0;
	ringbuf_flush_drops_to(&cali_rb_evnt, cali_rb_drops_lookup_elem(&key));
}

// Submit helper — drop-in replacement for perf_commit_event().
// Unlike perf_commit_event, does not require a program context pointer.
// On failure (ring buffer full), increments the shared drop counter.
// On success, flushes accumulated drops if the flush interval has elapsed.
static CALI_BPF_INLINE int ringbuf_submit_event(void *data, __u64 size)
{
	int err = bpf_ringbuf_output(&cali_rb_evnt, data, size, 0);
	if (err != 0) {
		__u32 key = 0;
		ringbuf_bump_drops_at(cali_rb_drops_lookup_elem(&key));
	} else {
		ringbuf_flush_drops();
	}
	return err;
}

#endif /* __CALI_RINGBUF_H__ */
