linux/tools/testing/selftests/bpf/progs/bpf_iter_bpf_percpu_array_map.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2020 Facebook */
   3#include "bpf_iter.h"
   4#include <bpf/bpf_helpers.h>
   5#include <bpf/bpf_tracing.h>
   6
   7char _license[] SEC("license") = "GPL";
   8
   9struct key_t {
  10        int a;
  11        int b;
  12        int c;
  13};
  14
  15struct {
  16        __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
  17        __uint(max_entries, 3);
  18        __type(key, __u32);
  19        __type(value, __u32);
  20} arraymap1 SEC(".maps");
  21
  22/* will set before prog run */
  23volatile const __u32 num_cpus = 0;
  24
  25__u32 key_sum = 0, val_sum = 0;
  26
  27SEC("iter/bpf_map_elem")
  28int dump_bpf_percpu_array_map(struct bpf_iter__bpf_map_elem *ctx)
  29{
  30        __u32 *key = ctx->key;
  31        void *pptr = ctx->value;
  32        __u32 step;
  33        int i;
  34
  35        if (key == (void *)0 || pptr == (void *)0)
  36                return 0;
  37
  38        key_sum += *key;
  39
  40        step = 8;
  41        for (i = 0; i < num_cpus; i++) {
  42                val_sum += *(__u32 *)pptr;
  43                pptr += step;
  44        }
  45        return 0;
  46}
  47