^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) __uint(key_size, sizeof(int));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) __uint(value_size, sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) __uint(max_entries, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) } counters SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) __type(key, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) __type(value, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) __uint(max_entries, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) } values SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) __type(key, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) __type(value, struct bpf_perf_event_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) __uint(max_entries, 64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) } values2 SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) SEC("kprobe/htab_map_get_next_key")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int bpf_prog1(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u32 key = bpf_get_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u64 count, *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) s64 error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) count = bpf_perf_event_read(&counters, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) error = (s64)count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (error <= -2 && error >= -22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) val = bpf_map_lookup_elem(&values, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *val = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) SEC("kprobe/htab_map_lookup_elem")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int bpf_prog2(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u32 key = bpf_get_smp_processor_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct bpf_perf_event_value *val, buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) val = bpf_map_lookup_elem(&values2, &key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *val = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) char _license[] SEC("license") = "GPL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u32 _version SEC("version") = LINUX_VERSION_CODE;