Skip to content

Map type BPF_MAP_TYPE_XSKMAP

v4.18

This XDP Socket map is a specialized map which references XDP Sockets.

Usage

This map type is used in combination with the bpf_redirect_map helper to redirect traffic to userspace, bypassing the kernel network stack. It is an array style map, where the indices go from 0 to max_entries-1. The values of this map are the file descriptor of specially prepared network sockets.

For details on the usage and preparation of AF_XDP sockets checkout out the concept page and/or kernel docs.

Example

The following examples shows how to use the map and helper from the BPF/kernel side. Important to note is that we always use the rx_queue_index as key, not doing so will cause the packet to be dropped.

struct {
    __uint(type, BPF_MAP_TYPE_XSKMAP);
    __type(key, __u32);
    __type(value, __u32);
    __uint(max_entries, 64);
} xsks_map SEC(".maps");


SEC("xdp")
int xsk_redir_prog(struct xdp_md *ctx)
{
    __u32 index = ctx->rx_queue_index;

    if (bpf_map_lookup_elem(&xsks_map, &index))
            return bpf_redirect_map(&xsks_map, index, 0);
    return XDP_PASS;
}

Attributes

The value_size must always be 4. The key_size must always be 4.

Syscall commands

The following syscall commands work with this map type:

Helper functions

Flags

BPF_F_NUMA_NODE

v4.14

When set, the numa_node attribute is respected during map creation.

BPF_F_RDONLY

v4.15

Setting this flag will make it so the map can only be read via the syscall interface, but not written to.

For details please check the generic description.

BPF_F_WRONLY

v4.15

Setting this flag will make it so the map can only be written to via the syscall interface, but not read from.