1
0
Fork 0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-05-21 23:46:27 +02:00
fastnetmon-rewritten/src/xdp_plugin/xdp_kernel.c
2023-12-03 14:02:25 +03:00

54 lines
1.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
#define KBUILD_MODNAME "foo"
#include <linux/types.h>
#include <bpf/bpf_helpers.h>
#include <linux/bpf.h>
//
// To compile it on Ubuntu 22.04 x86_64 you will need following packages:
// sudo apt install -y clang libbpf-dev gcc-multilib
//
// Sadly ARM64 has no gcc-multilib package and we cannot compile it on ARM64 boxes
//
// Compile command:
//
// clang -c -g -O2 -target bpf xdp_kernel.c -o xdp_kernel.o
//
// To unload BPF for specific interface you need to apply following command:
//
// sudo xdp-loader unload <interface> --all
//
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} qidconf_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_XSKMAP);
__uint(max_entries, 4);
__type(key, int);
__type(value, int);
} xsks_map SEC(".maps");
SEC("xdp_sock")
int xdp_sock_prog(struct xdp_md* ctx) {
int *qidconf, key = 0, idx;
unsigned int* rr;
qidconf = bpf_map_lookup_elem(&qidconf_map, &key);
if (!qidconf) return XDP_ABORTED;
if (*qidconf != ctx->rx_queue_index) return XDP_PASS;
idx = 0;
return bpf_redirect_map(&xsks_map, idx, 0);
}
char _license[] SEC("license") = "GPL";