#include #include #include #define PACKET_ID_MASK 0x0FFFFFFF #define VERDICT_MASK 0xF0000000 struct counter_state { struct bpf_spin_lock lock; __u32 value; }; struct { __uint(type, BPF_MAP_TYPE_ARRAY); __uint(max_entries, 1); __type(key, __u32); __type(value, struct counter_state); } packet_counter SEC(".maps"); SEC("classifier") int classifier(struct __sk_buff *skb) { __u32 key = 0; struct counter_state *state = bpf_map_lookup_elem(&packet_counter, &key); __u32 next = 1; if (state) { bpf_spin_lock(&state->lock); state->value += 1; next = state->value; bpf_spin_unlock(&state->lock); } skb->mark = (skb->mark & VERDICT_MASK) | (next & PACKET_ID_MASK); return TC_ACT_OK; } char LICENSE[] SEC("license") = "GPL";