summaryrefslogtreecommitdiffstats
path: root/xdp-dump/xdpdump_xdp.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 07:10:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 07:10:00 +0000
commit4ba2b326284765e942044db13a7f0dae702bec93 (patch)
treecbdfaec33eed4f3a970c54cd10e8ddfe3003b3b1 /xdp-dump/xdpdump_xdp.c
parentInitial commit. (diff)
downloadxdp-tools-4ba2b326284765e942044db13a7f0dae702bec93.tar.xz
xdp-tools-4ba2b326284765e942044db13a7f0dae702bec93.zip
Adding upstream version 1.3.1.upstream/1.3.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--xdp-dump/xdpdump_xdp.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/xdp-dump/xdpdump_xdp.c b/xdp-dump/xdpdump_xdp.c
new file mode 100644
index 0000000..76e7509
--- /dev/null
+++ b/xdp-dump/xdpdump_xdp.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*****************************************************************************
+ * Include files
+ *****************************************************************************/
+#include <stdbool.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_trace_helpers.h>
+#include "xdpdump.h"
+
+/*****************************************************************************
+ * Macros
+ *****************************************************************************/
+#define min(x, y) ((x) < (y) ? x : y)
+
+
+/*****************************************************************************
+ * Local definitions and global variables
+ *****************************************************************************/
+struct {
+ __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+ __uint(max_entries, MAX_CPUS);
+ __type(key, int);
+ __type(value, __u32);
+} xdpdump_perf_map SEC(".maps");
+
+
+/*****************************************************************************
+ * .data section value storing the capture configuration
+ *****************************************************************************/
+struct trace_configuration trace_cfg SEC(".data");
+
+
+/*****************************************************************************
+ * XDP trace program
+ *****************************************************************************/
+SEC("xdp")
+int xdpdump(struct xdp_md *xdp)
+{
+ void *data_end = (void *)(long)xdp->data_end;
+ void *data = (void *)(long)xdp->data;
+ struct pkt_trace_metadata metadata;
+
+ if (data >= data_end ||
+ trace_cfg.capture_if_ifindex != xdp->ingress_ifindex)
+ return XDP_PASS;
+
+ metadata.prog_index = trace_cfg.capture_prog_index;
+ metadata.ifindex = xdp->ingress_ifindex;
+ metadata.rx_queue = xdp->rx_queue_index;
+ metadata.pkt_len = (__u16)(data_end - data);
+ metadata.cap_len = min(metadata.pkt_len, trace_cfg.capture_snaplen);
+ metadata.action = 0;
+ metadata.flags = 0;
+
+ bpf_perf_event_output(xdp, &xdpdump_perf_map,
+ ((__u64) metadata.cap_len << 32) |
+ BPF_F_CURRENT_CPU,
+ &metadata, sizeof(metadata));
+
+ return XDP_PASS;
+}
+
+
+/*****************************************************************************
+ * License
+ *****************************************************************************/
+char _license[] SEC("license") = "GPL";