Skip to content

Commit

Permalink
xdp-tools: add bpf map path as cmd line argument
Browse files Browse the repository at this point in the history
add XDP DNS and TLS SNI user space program command
line argument for bpf map so X86 and Loongarch can
share the same XDP user space program

Signed-off-by: Vincent Li <[email protected]>
  • Loading branch information
vincentmli committed Jan 7, 2025
1 parent 5d713b4 commit d18f8a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
21 changes: 11 additions & 10 deletions xdp-dns/xdp_dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,46 +73,47 @@ int main(int argc, char *argv[])
__u8 value = 1;

// Check for proper number of arguments
if (argc != 3) {
fprintf(stderr, "Usage: %s <add|delete> <domain>\n", argv[0]);
if (argc != 4) {
fprintf(stderr, "Usage: %s <map_path> <add|delete> <domain>\n", argv[0]);
return 1;
}

// Encode the domain name with label lengths
encode_domain(argv[2], dkey.data);
encode_domain(argv[3], dkey.data);
reverse_string(dkey.data);

// Set the LPM trie key prefix length
dkey.lpm_key.prefixlen = strlen(dkey.data) * 8;

// Open the BPF map
map_fd = bpf_obj_get("/sys/fs/bpf/xdp-dns-denylist/domain_denylist");
const char *map_path = argv[1];
map_fd = bpf_obj_get(map_path);
if (map_fd < 0) {
fprintf(stderr, "Failed to open map: %s\n", strerror(errno));
fprintf(stderr, "Failed to open map at %s: %s\n", map_path, strerror(errno));
return 1;
}

// Add or delete the domain based on the first argument
if (strcmp(argv[1], "add") == 0) {
if (strcmp(argv[2], "add") == 0) {
// Update the map with the encoded domain name
if (bpf_map_update_elem(map_fd, &dkey, &value, BPF_ANY) != 0) {
fprintf(stderr, "Failed to add domain to map: %s\n",
strerror(errno));
return 1;
}
printf("Domain %s added to denylist\n", argv[2]);
} else if (strcmp(argv[1], "delete") == 0) {
printf("Domain %s added to denylist\n", argv[3]);
} else if (strcmp(argv[2], "delete") == 0) {
// Remove the domain from the map
if (bpf_map_delete_elem(map_fd, &dkey) != 0) {
fprintf(stderr,
"Failed to remove domain from map: %s\n",
strerror(errno));
return 1;
}
printf("Domain %s removed from denylist\n", argv[2]);
printf("Domain %s removed from denylist\n", argv[3]);
} else {
fprintf(stderr, "Invalid command: %s. Use 'add' or 'delete'.\n",
argv[1]);
argv[2]);
return 1;
}

Expand Down
10 changes: 8 additions & 2 deletions xdp-dns/xdp_dns_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ int handle_event(void *ctx __attribute__((unused)), void *data,
return 0; // Return 0 to indicate success
}

int main()
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <path_to_ringbuf>\n", argv[0]);
return 1;
}

const char *ringbuf_path = argv[1];
struct ring_buffer *rb;
int ringbuf_fd;

openlog("qname_logger", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);

// Open the ring buffer
ringbuf_fd = bpf_obj_get("/sys/fs/bpf/xdp-dns-denylist/dns_ringbuf");
ringbuf_fd = bpf_obj_get(ringbuf_path);
if (ringbuf_fd < 0) {
perror("Failed to open ring buffer");
return 1;
Expand Down
21 changes: 11 additions & 10 deletions xdp-sni/xdp_sni.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,34 @@ int main(int argc, char *argv[])
__u8 value = 1;

// Check for proper number of arguments
if (argc != 3) {
fprintf(stderr, "Usage: %s <add|delete> <domain>\n", argv[0]);
if (argc != 4) {
fprintf(stderr, "Usage: %s <map_path> <add|delete> <domain>\n", argv[0]);
return 1;
}

// Reverse the input domain
strncpy(server_name, argv[2], MAX_DOMAIN_SIZE);
strncpy(server_name, argv[3], MAX_DOMAIN_SIZE);
server_name[MAX_DOMAIN_SIZE] = '\0'; // Ensure null termination

// Open the BPF map
map_fd = bpf_obj_get("/sys/fs/bpf/xdp-sni/sni_denylist");
const char *map_path = argv[1];
map_fd = bpf_obj_get(map_path);
if (map_fd < 0) {
fprintf(stderr, "Failed to open map: %s\n", strerror(errno));
fprintf(stderr, "Failed to open map at %s: %s\n", map_path, strerror(errno));
return 1;
}

// Add or delete the domain based on the first argument
if (strcmp(argv[1], "add") == 0) {
if (strcmp(argv[2], "add") == 0) {
// Update the map with the reversed domain name
if (bpf_map_update_elem(map_fd, server_name, &value, BPF_ANY) != 0) {
fprintf(stderr, "Failed to add domain to map: %s\n",
strerror(errno));
return 1;
}
printf("Domain %s (reversed: %s) added to denylist\n", argv[2],
printf("Domain %s (reversed: %s) added to denylist\n", argv[3],
server_name);
} else if (strcmp(argv[1], "delete") == 0) {
} else if (strcmp(argv[2], "delete") == 0) {
// Remove the reversed domain from the map
if (bpf_map_delete_elem(map_fd, server_name) != 0) {
fprintf(stderr,
Expand All @@ -66,10 +67,10 @@ int main(int argc, char *argv[])
return 1;
}
printf("Domain %s (reversed: %s) removed from denylist\n",
argv[2], server_name);
argv[3], server_name);
} else {
fprintf(stderr, "Invalid command: %s. Use 'add' or 'delete'.\n",
argv[1]);
argv[2]);
return 1;
}

Expand Down
11 changes: 9 additions & 2 deletions xdp-sni/xdp_sni_log.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
Expand Down Expand Up @@ -44,15 +45,21 @@ int handle_event(void *ctx __attribute__((unused)), void *data,
return 0; // Return 0 to indicate success
}

int main()
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <path_to_ringbuf>\n", argv[0]);
return 1;
}

const char *ringbuf_path = argv[1];
struct ring_buffer *rb;
int ringbuf_fd;

openlog("sni_logger", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);

// Open the ring buffer
ringbuf_fd = bpf_obj_get("/sys/fs/bpf/xdp-sni/sni_ringbuf");
ringbuf_fd = bpf_obj_get(ringbuf_path);
if (ringbuf_fd < 0) {
perror("Failed to open ring buffer");
return 1;
Expand Down

0 comments on commit d18f8a7

Please sign in to comment.