Skip to content

Commit

Permalink
feat: add new utility for updating IPv4 and IPv6 BPF maps
Browse files Browse the repository at this point in the history
Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Feb 3, 2025
1 parent 0a0083a commit 2496eb6
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 50 deletions.
79 changes: 79 additions & 0 deletions ebpf/aw-bpfctl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// update_maps.c
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>

#include "aw-bpf.h"

int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <ipv4|ipv6> <IP_ADDRESS>\n", argv[0]);
return EXIT_FAILURE;
}

// Open the map (assuming maps are pinned)
const char *map_type = argv[1];
const char *map_path = NULL;
if (strcmp(map_type, "ipv4") == 0) {
map_path = "/sys/fs/bpf/tc/globals/ipv4_map";
} else if (strcmp(map_type, "ipv6") == 0) {
map_path = "/sys/fs/bpf/tc/globals/ipv6_map";
} else {
fprintf(stderr, "Invalid map type. Use 'ipv4' or 'ipv6'.\n");
return EXIT_FAILURE;
}

int map_fd = bpf_obj_get(map_path);
if (map_fd < 0) {
perror("bpf_obj_get");
return EXIT_FAILURE;
}

// Initialize key
union {
__be32 ipv4_key;
struct in6_addr ipv6_key;
} key = {0};

// Convert IP string to key
if (strcmp(map_type, "ipv4") == 0) {
if (inet_pton(AF_INET, argv[2], &key.ipv4_key) != 1) {
perror("inet_pton (IPv4)");
return EXIT_FAILURE;
}
} else {
if (inet_pton(AF_INET6, argv[2], &key.ipv6_key) != 1) {
perror("inet_pton (IPv6)");
return EXIT_FAILURE;
}
}

// Check if key exists
void *temp_value;
if (bpf_map_lookup_elem(map_fd,
(strcmp(map_type, "ipv4") == 0) ? (void *)&key.ipv4_key : (void *)&key.ipv6_key,
&temp_value) == 0) {
printf("Key %s already exists in %s map.\n", argv[2], map_type);
return EXIT_SUCCESS;
}

// Add the key with zero-initialized value
struct traffic_stats stats = {0};
int ret = bpf_map_update_elem(
map_fd,
(strcmp(map_type, "ipv4") == 0) ? (void *)&key.ipv4_key : (void *)&key.ipv6_key,
&stats,
BPF_NOEXIST // Only add if key doesn't exist
);

if (ret < 0) {
perror("bpf_map_update_elem");
return EXIT_FAILURE;
}

printf("Successfully added %s to %s map.\n", argv[2], map_type);
return EXIT_SUCCESS;
}
50 changes: 0 additions & 50 deletions ebpf/update-ipv4_map.c

This file was deleted.

0 comments on commit 2496eb6

Please sign in to comment.