github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/docs/examples/getting_started/counter.c (about)

     1  // getting_started_counter {
     2  // (1)!
     3  //go:build ignore
     4  
     5  #include <linux/bpf.h> // (2)!
     6  #include <bpf/bpf_helpers.h>
     7  
     8  struct {
     9  	__uint(type, BPF_MAP_TYPE_ARRAY); // (3)!
    10  	__type(key, __u32);
    11  	__type(value, __u64);
    12  	__uint(max_entries, 1);
    13  } pkt_count SEC(".maps"); // (4)!
    14  
    15  // count_packets atomically increases a packet counter on every invocation.
    16  SEC("xdp") // (5)!
    17  int count_packets() {
    18  	__u32 key    = 0; // (6)!
    19  	__u64 *count = bpf_map_lookup_elem(&pkt_count, &key); // (7)!
    20  	if (count) { // (8)!
    21  		__sync_fetch_and_add(count, 1); // (9)!
    22  	}
    23  
    24  	return XDP_PASS; // (10)!
    25  }
    26  
    27  char __license[] SEC("license") = "Dual MIT/GPL"; // (11)!
    28  
    29  // }