^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "xdpsock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* This XDP program is only needed for the XDP_SHARED_UMEM mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * If you do not use this mode, libbpf can supply an XDP program for you.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) __uint(type, BPF_MAP_TYPE_XSKMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) __uint(max_entries, MAX_SOCKS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) __uint(key_size, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) __uint(value_size, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) } xsks_map SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static unsigned int rr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) rr = (rr + 1) & (MAX_SOCKS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return bpf_redirect_map(&xsks_map, rr, XDP_DROP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }