xdp-project / xdp-tutorial

XDP tutorial
2.49k stars 579 forks source link

Can I use a struct as a map key in XDP? #410

Open samueljaydan opened 7 months ago

samueljaydan commented 7 months ago

Is it possible to create a struct as shown below in the XDP part and use it as a key, then write to this map in the packet processor part?

Note: Packet processor is being writing GoLang language.

BPF_MAP_DEF(blacklistmap) = {
    .map_type = BPF_MAP_TYPE_HASH,
    .key_size = sizeof(struct flowblack),
    .value_size = sizeof(int),  // Change value size to sizeof(__u64)
    .max_entries = MAX_RULES,
};
BPF_MAP_ADD(blacklistmap);

struct flowblack {
    __u16 sourceport;
    __u16 destport;
    __u32 saddr;
    __u32 daddr;
};

int xdp_dump(struct xdp_md *ctx) {
// ....
    struct flowblack key = {0};
    key.sourceport = tcp->source;
    key.destport = tcp->dest;
    key.saddr = ip->saddr;
    key.daddr = ip->daddr;

    __u64 *rule_idx = bpf_map_lookup_elem(&blacklistmap, &key);
    if (rule_idx) {
        return XDP_DROP;   
    }
// ....
}
tohojo commented 7 months ago

samueljaydan @.***> writes:

Is it possible to create a struct as shown below in the XDP part and use it as a key, then write to this map in the packet processor part?

Did you try it?

samueljaydan commented 7 months ago

I did, but no succeeded. I've changed the topology using AF_SOCKET. So no need to use it anymore. Thanks.