Skip to content

Fix wrong statement saying devmap_hash keys can have any size. - #271

Merged
dylandreimerink merged 1 commit into
isovalent:masterfrom
devcexx:master
May 11, 2026
Merged

dylandreimerink merged 1 commit into
isovalent:masterfrom
devcexx:master

Conversation

@devcexx

@devcexx devcexx commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

Hi,

Following this documentation, that was stating that the key_size for BPF_MAP_TYPE_DEVMAP_HASH could be freely chosen (ref) I've been trying to create a map of this kind with a key size of 8, always getting an EINVAL error.

After some debugging, I've found that the mainline kernel enforces a key_size of 4 for any kind of devmap, regardless of whether is a devmap or a devmap_hash (ref), which contradicts the current docs. This PR does that fix by explicitly stating that a devmap_hash must have a key size of 4.

I'm not an experienced eBPF developer, but after doing some debugging and some testing changing the values of key_size for my map, I couldn't find any other conclusion except that the docs were incorrect in this regard. Any feedback is appreciated.

I'm also leaving here the the eBPF program whose map I was unable to create, for reference:

struct {
    __uint(type, BPF_MAP_TYPE_DEVMAP_HASH);
    __type(key, __u64);
    __type(value, __u32);
    __uint(max_entries, 128);
} mac_tx_port SEC(".maps");

SEC("xdp")
int xdp_test(struct xdp_md *ctx)
{
    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";

@dylandreimerink dylandreimerink left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow. Yea, you are totally right. That was missed during the research for the docs. Its the only "_HASH" map type in eBPF that forces a 4 byte key, nuts.

Seems like the kernel contribution specifically focused on making a sparse ifindex -> dev mapping and not about allowing arbitrary keys:

A common pattern when using xdp_redirect_map() is to create a device map
where the lookup key is simply ifindex. Because device maps are arrays,
this leaves holes in the map, and the map has to be sized to fit the
largest ifindex, regardless of how many devices actually are actually
needed in the map.

This patch adds a second type of device map where the key is looked up
using a hashmap, instead of being used as an array index. This allows maps
to be densely packed, so they can be smaller.

Thank you for fixing the docs 🙏

@dylandreimerink
dylandreimerink merged commit 9e64180 into isovalent:master May 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants