Skip to content

Commit

Permalink
feat: add IPv4 map update functionality with traffic statistics initi…
Browse files Browse the repository at this point in the history
…alization

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Feb 3, 2025
1 parent 83b5d6d commit 5dc2cdc
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions ebpf/update-ipv4_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h> // or include <bpf/bpf.h> if libbpf not used

// Define the structure exactly as in the BPF program.
struct counters {
__u32 cur_s_bytes;
__u32 prev_s_bytes;
__u64 total_bytes;
__u64 total_packets;
__u32 est_slot;
__u32 reserved;
};

struct traffic_stats {
struct counters incoming;
struct counters outgoing;
};

int main(int argc, char **argv)
{
const char *pin_path = "/sys/fs/bpf/aw_bpf/ipv4_map"; // adjust if your map is pinned here
int map_fd = bpf_obj_get(pin_path);
if (map_fd < 0) {
perror("bpf_obj_get");
return EXIT_FAILURE;
}

// The key is an IPv4 address in network byte order.
__u32 ip_key;
// Convert string IP to network order.
if (inet_pton(AF_INET, "192.168.1.100", &ip_key) != 1) {
perror("inet_pton");
return EXIT_FAILURE;
}

// Initialize value as needed.
struct traffic_stats stats = {0};

if (bpf_map_update_elem(map_fd, &ip_key, &stats, 0)) {
perror("bpf_map_update_elem");
return EXIT_FAILURE;
}

printf("IPv4 map updated successfully.\n");
return EXIT_SUCCESS;
}

0 comments on commit 5dc2cdc

Please sign in to comment.