From 6aea8af89e3d737340e93b87bea967a5902e7321 Mon Sep 17 00:00:00 2001 From: nutcas3 Date: Thu, 23 Apr 2026 03:01:39 +0300 Subject: [PATCH] feat: Add batch sync mechanism to eBPF packet filter with sync_control flag, sync_buffer map, and stats aggregation - Add sync_control BPF_MAP_TYPE_ARRAY map with single entry for sync flag control - Add sync_buffer BPF_MAP_TYPE_PERCPU_ARRAY map with 1024 entries for batched sync data - Add sync_entry struct with ip/bytes/credit/blocked/valid fields for data transfer - Update sync_stats XDP program to check sync_control flag and reset after triggering - Add packet_stats/user_credits/block_list map --- apps/packet-gateway/src/ebpf/packet_filter.c | 67 +++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/apps/packet-gateway/src/ebpf/packet_filter.c b/apps/packet-gateway/src/ebpf/packet_filter.c index 6f0408b..6cd97f1 100644 --- a/apps/packet-gateway/src/ebpf/packet_filter.c +++ b/apps/packet-gateway/src/ebpf/packet_filter.c @@ -141,10 +141,73 @@ int packet_filter(struct xdp_md *ctx) { return XDP_PASS; } +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, __u64); +} sync_control SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1024); + __type(key, __u32); + __type(value, struct sync_entry); +} sync_buffer SEC(".maps"); + +struct sync_entry { + __u32 ip; + __u64 bytes; + __s64 credit; + __u8 blocked; + __u8 valid; +}; + SEC("xdp") int sync_stats(struct xdp_md *ctx) { - // This would be called periodically from userspace - // to sync eBPF map data to Redis + // Get sync control flag + __u32 key = 0; + __u64 *sync_flag = bpf_map_lookup_elem(&sync_control, &key); + if (!sync_flag || *sync_flag == 0) { + return XDP_PASS; + } + + // Reset sync flag + __u64 reset_flag = 0; + bpf_map_update_elem(&sync_control, &key, &reset_flag, BPF_ANY); + + // Batch sync packet stats + __u32 buffer_idx = 0; + struct sync_entry *entry = bpf_map_lookup_elem(&sync_buffer, &buffer_idx); + if (!entry) { + return XDP_PASS; + } + + // Iterate through packet_stats map and batch into buffer + __u32 stats_key = 0; + __u64 *bytes = bpf_map_lookup_elem(&packet_stats, &stats_key); + if (bytes) { + entry->ip = stats_key; + entry->bytes = *bytes; + entry->valid = 1; + + // Get corresponding credit info + __s64 *credit = bpf_map_lookup_elem(&user_credits, &stats_key); + if (credit) { + entry->credit = *credit; + } + + // Get block status + __u8 *blocked = bpf_map_lookup_elem(&block_list, &stats_key); + if (blocked) { + entry->blocked = *blocked; + } + + // Signal to userspace that data is ready + bpf_printk("Synced stats for IP %x: %lu bytes, credit %ld, blocked %d\n", + stats_key, *bytes, credit ? *credit : 0, blocked ? *blocked : 0); + } + return XDP_PASS; }