^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Copyright 2016 Netflix, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <uapi/linux/bpf_perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <bpf/bpf_tracing.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define MAX_IPS 8192
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) __uint(type, BPF_MAP_TYPE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) __type(key, u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) __type(value, u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) __uint(max_entries, MAX_IPS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) } ip_map SEC(".maps");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) SEC("perf_event")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int do_sample(struct bpf_perf_event_data *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u64 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 *value, init_val = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) ip = PT_REGS_IP(&ctx->regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) value = bpf_map_lookup_elem(&ip_map, &ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *value += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* E2BIG not tested for this example only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) bpf_map_update_elem(&ip_map, &ip, &init_val, BPF_NOEXIST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^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) char _license[] SEC("license") = "GPL";