-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.c
59 lines (54 loc) · 1.72 KB
/
counter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//go:build ignore
#include <linux/bpf.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include "headers/bpf_helpers.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, __be32);
__type(value, __u64);
__uint(max_entries, 1024);
} ipv4_counter_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct in6_addr);
__type(value, __u64);
__uint(max_entries, 1024);
} ipv6_counter_map SEC(".maps");
SEC("xdp")
int xdp_ip_packet_counter(struct xdp_md* ctx) {
void *data = (void*)(long)ctx->data;
void *data_end = (void*)(long)ctx->data_end;
struct ethhdr *eth = data;
if ((void*)(eth + 1) > data_end) {
return XDP_PASS;
}
if (eth->h_proto == __constant_htons(ETH_P_IP)) {
struct iphdr *ip = (struct iphdr*)(eth + 1);
if ((void*)(ip + 1) > data_end) {
return XDP_PASS;
}
__u64 *count, init_val = 1;
count = bpf_map_lookup_elem(&ipv4_counter_map, &ip->saddr);
if (count) {
__sync_fetch_and_add(count, 1);
} else {
bpf_map_update_elem(&ipv4_counter_map, &ip->saddr, &init_val, BPF_ANY);
}
} else if (eth->h_proto == __constant_htons(ETH_P_IPV6)) {
struct ipv6hdr *ip6 = (struct ipv6hdr*)(eth + 1);
if ((void*)(ip6 + 1) > data_end) {
return XDP_PASS;
}
__u64 *count, init_val = 1;
count = bpf_map_lookup_elem(&ipv6_counter_map, &ip6->saddr);
if (count) {
__sync_fetch_and_add(count, 1);
} else {
bpf_map_update_elem(&ipv6_counter_map, &ip6->saddr, &init_val, BPF_ANY);
}
}
return XDP_PASS;
}
char __license[] SEC("license") = "Dual MIT/GPL";