github.com/cilium/ebpf@v0.16.0/examples/tcx/tcx.c (about)

     1  //go:build ignore
     2  
     3  #include "common.h"
     4  
     5  char __license[] SEC("license") = "Dual MIT/GPL";
     6  
     7  /* Define an ARRAY map for storing ingress packet count */
     8  struct {
     9  	__uint(type, BPF_MAP_TYPE_ARRAY);
    10  	__type(key, __u32);
    11  	__type(value, __u64);
    12  	__uint(max_entries, 1);
    13  } ingress_pkt_count SEC(".maps");
    14  
    15  /* Define an ARRAY map for storing egress packet count */
    16  struct {
    17  	__uint(type, BPF_MAP_TYPE_ARRAY);
    18  	__type(key, __u32);
    19  	__type(value, __u64);
    20  	__uint(max_entries, 1);
    21  } egress_pkt_count SEC(".maps");
    22  
    23  /*
    24  Upon arrival of each network packet, retrieve and increment
    25  the packet count from the provided map.
    26  Returns TC_ACT_OK, allowing the packet to proceed.
    27  */
    28  static __always_inline int update_map_pkt_count(void *map) {
    29  	__u32 key    = 0;
    30  	__u64 *count = bpf_map_lookup_elem(map, &key);
    31  	if (count) {
    32  		__sync_fetch_and_add(count, 1);
    33  	}
    34  
    35  	return TC_ACT_OK;
    36  }
    37  
    38  SEC("tc")
    39  int ingress_prog_func(struct __sk_buff *skb) {
    40  	return update_map_pkt_count(&ingress_pkt_count);
    41  }
    42  
    43  SEC("tc")
    44  int egress_prog_func(struct __sk_buff *skb) {
    45  	return update_map_pkt_count(&egress_pkt_count);
    46  }