Search code examples
cebpfbpfxdp-bpf

libbpf: map 'hash_map': unknown field 'values' in hash_map with struct as value


So I am compiling xdp program with hash map having struct in value

struct hash_elem {
    int cnt;
    struct bpf_spin_lock lock;
};
struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 100);
    __uint(key_size, sizeof(__u32));
    __type(values, struct hash_elem);
} hash_map SEC(".maps");
struct a{struct bpf_spin_lock lock;};

struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __type(key, __u32);
    __type(value, long);
    __uint(max_entries, 2);
} hash_map1 SEC(".maps");
//static __u32 i=0;



SEC("xdp")
int  xdp_prog_simple(struct xdp_md *ctx)
{
    int key=1;
    struct hash_elem * val = bpf_map_lookup_elem(&hash_map, &key);
    if (val) {
        bpf_spin_lock(&val->lock);
        val->cnt++;
        bpf_spin_unlock(&val->lock);
    }
}

But while loading the program it causes exception that

libbpf: map 'hash_map': unknown field 'values'.
ERR: loading BPF-OBJ file(./k.o) (-2): No such file or directory
ERR: loading file: ./k.o

Solution

  • This is happening because of a typo in your map definition. I think you want:

    struct {
        __uint(type, BPF_MAP_TYPE_HASH);
        __type(key, __u32);
        __type(value, struct hash_elem);
        __uint(max_entries, 100);
    } hash_map SEC(".maps");
    struct a{struct bpf_spin_lock lock;};
    
    struct {
        __uint(type, BPF_MAP_TYPE_HASH);
        __type(key, __u32);
        __type(value, long);
        __uint(max_entries, 2);
    } hash_map1 SEC(".maps");
    

    Note the difference on the first value field. I've also switched to using __type wherever possible, although that's not required.