File: counter.c

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (29 lines) | stat: -rw-r--r-- 643 bytes parent folder | download
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
// getting_started_counter {
// (1)!
//go:build ignore

#include <linux/bpf.h> // (2)!
#include <bpf/bpf_helpers.h>

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY); // (3)!
	__type(key, __u32);
	__type(value, __u64);
	__uint(max_entries, 1);
} pkt_count SEC(".maps"); // (4)!

// count_packets atomically increases a packet counter on every invocation.
SEC("xdp") // (5)!
int count_packets() {
	__u32 key    = 0; // (6)!
	__u64 *count = bpf_map_lookup_elem(&pkt_count, &key); // (7)!
	if (count) { // (8)!
		__sync_fetch_and_add(count, 1); // (9)!
	}

	return XDP_PASS; // (10)!
}

char __license[] SEC("license") = "Dual MIT/GPL"; // (11)!

// }