struct bpf_map_def SEC("maps") blacklist = { .type = BPF_MAP_TYPE_PERCPU_HASH, .key_size = sizeof(u32), .value_size = sizeof(u64), /* Drop counter */ .max_entries = 100000, .map_flags = BPF_F_NO_PREALLOC, }; /* eBPF xxx_kern.c side map-lookup */ static __always_inline u32 parse_ipv4(struct xdp_md *ctx, u64 l3_offset) { void *data_end = (void *)(long)ctx->data_end; void *data = (void *)(long)ctx->data; struct iphdr *iph = data + l3_offset; u64 *value; u32 ip_src; /* type need to match map */ /* Hint: +1 is sizeof(struct iphdr) */ if (iph + 1 > data_end) { return XDP_ABORTED; } ip_src = iph->saddr; /* Extract key */ value = bpf_map_lookup_elem(&blacklist, &ip_src); if (value) { /* Don't need __sync_fetch_and_add(); as percpu map */ *value += 1; /* Keep a counter for drop matches */ return XDP_DROP; } return XDP_PASS; }