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

     1  //go:build ignore
     2  
     3  #include "common.h"
     4  
     5  char __license[] SEC("license") = "Dual MIT/GPL";
     6  
     7  struct bpf_map_def SEC("maps") pkt_count = {
     8  	.type        = BPF_MAP_TYPE_ARRAY,
     9  	.key_size    = sizeof(u32),
    10  	.value_size  = sizeof(u64),
    11  	.max_entries = 1,
    12  };
    13  
    14  SEC("cgroup_skb/egress")
    15  int count_egress_packets(struct __sk_buff *skb) {
    16  	u32 key      = 0;
    17  	u64 init_val = 1;
    18  
    19  	u64 *count = bpf_map_lookup_elem(&pkt_count, &key);
    20  	if (!count) {
    21  		bpf_map_update_elem(&pkt_count, &key, &init_val, BPF_ANY);
    22  		return 1;
    23  	}
    24  	__sync_fetch_and_add(count, 1);
    25  
    26  	return 1;
    27  }