This commit is contained in:
@@ -5,21 +5,29 @@
|
||||
#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, __u32);
|
||||
__type(value, struct counter_state);
|
||||
} packet_counter SEC(".maps");
|
||||
|
||||
SEC("classifier")
|
||||
int classifier(struct __sk_buff *skb) {
|
||||
__u32 key = 0;
|
||||
__u32 *counter = bpf_map_lookup_elem(&packet_counter, &key);
|
||||
struct counter_state *state = bpf_map_lookup_elem(&packet_counter, &key);
|
||||
__u32 next = 1;
|
||||
|
||||
if (counter) {
|
||||
next = __sync_add_and_fetch(counter, 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);
|
||||
|
||||
Reference in New Issue
Block a user