0001
0002
0003
0004 #include "vmlinux.h"
0005 #include <bpf/bpf_helpers.h>
0006
0007 __u64 percpu_array_elem_sum = 0;
0008 __u64 percpu_hash_elem_sum = 0;
0009 __u64 percpu_lru_hash_elem_sum = 0;
0010 const volatile int nr_cpus;
0011 const volatile int my_pid;
0012
0013 struct {
0014 __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
0015 __uint(max_entries, 1);
0016 __type(key, __u32);
0017 __type(value, __u64);
0018 } percpu_array_map SEC(".maps");
0019
0020 struct {
0021 __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
0022 __uint(max_entries, 1);
0023 __type(key, __u64);
0024 __type(value, __u64);
0025 } percpu_hash_map SEC(".maps");
0026
0027 struct {
0028 __uint(type, BPF_MAP_TYPE_LRU_PERCPU_HASH);
0029 __uint(max_entries, 1);
0030 __type(key, __u64);
0031 __type(value, __u64);
0032 } percpu_lru_hash_map SEC(".maps");
0033
0034 struct read_percpu_elem_ctx {
0035 void *map;
0036 __u64 sum;
0037 };
0038
0039 static int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx)
0040 {
0041 __u64 key = 0;
0042 __u64 *value;
0043
0044 value = bpf_map_lookup_percpu_elem(ctx->map, &key, index);
0045 if (value)
0046 ctx->sum += *value;
0047 return 0;
0048 }
0049
0050 SEC("tp/syscalls/sys_enter_getuid")
0051 int sysenter_getuid(const void *ctx)
0052 {
0053 struct read_percpu_elem_ctx map_ctx;
0054
0055 if (my_pid != (bpf_get_current_pid_tgid() >> 32))
0056 return 0;
0057
0058 map_ctx.map = &percpu_array_map;
0059 map_ctx.sum = 0;
0060 bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
0061 percpu_array_elem_sum = map_ctx.sum;
0062
0063 map_ctx.map = &percpu_hash_map;
0064 map_ctx.sum = 0;
0065 bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
0066 percpu_hash_elem_sum = map_ctx.sum;
0067
0068 map_ctx.map = &percpu_lru_hash_map;
0069 map_ctx.sum = 0;
0070 bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
0071 percpu_lru_hash_elem_sum = map_ctx.sum;
0072
0073 return 0;
0074 }
0075
0076 char _license[] SEC("license") = "GPL";