github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/examples/per_cpu_map/bpf/percpu.c (about)

     1  
     2  
     3  /* SPDX-License-Identifier: GPL-2.0 */
     4  #include <stddef.h>
     5  #include <linux/bpf.h>
     6  
     7  /*
     8   * bpf_map_lookup_elem
     9   *
    10   * 	Perform a lookup in *map* for an entry associated to *key*.
    11   *
    12   * Returns
    13   * 	Map value associated to *key*, or **NULL** if no entry was
    14   * 	found.
    15   */
    16  static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1;
    17  
    18  #define SEC(NAME) __attribute__((section(NAME), used))
    19  
    20  struct bpf_map_def {
    21  	unsigned int type;
    22  	unsigned int key_size;
    23  	unsigned int value_size;
    24  	unsigned int max_entries;
    25  	unsigned int map_flags;
    26  };
    27  
    28  
    29  // Stats on packets keyed by protocol number
    30  struct bpf_map_def SEC("maps") cnt_map = {
    31  	.type        = BPF_MAP_TYPE_PERCPU_ARRAY,
    32  	.key_size    = sizeof(__u32),
    33  	.value_size  = sizeof(__u64),
    34  	.max_entries = 8,
    35  };
    36  
    37  SEC("xdp")
    38  int percpumap_prog(struct xdp_md *ctx)
    39  {
    40      __u32* key = 0;
    41  	__u64* val = bpf_map_lookup_elem(&cnt_map, &key);
    42      if( val != NULL ){
    43          (*val)++;
    44      }
    45  
    46  	return XDP_PASS;
    47  }
    48  
    49  char _license[] SEC("license") = "GPL";