172874418SBrendan Gregg /* Copyright 2016 Netflix, Inc.
272874418SBrendan Gregg *
372874418SBrendan Gregg * This program is free software; you can redistribute it and/or
472874418SBrendan Gregg * modify it under the terms of version 2 of the GNU General Public
572874418SBrendan Gregg * License as published by the Free Software Foundation.
672874418SBrendan Gregg */
772874418SBrendan Gregg #include <linux/ptrace.h>
872874418SBrendan Gregg #include <uapi/linux/bpf.h>
972874418SBrendan Gregg #include <uapi/linux/bpf_perf_event.h>
107cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
117cf245a3SToke Høiland-Jørgensen #include <bpf/bpf_tracing.h>
1272874418SBrendan Gregg
1372874418SBrendan Gregg #define MAX_IPS 8192
1472874418SBrendan Gregg
15*59929cd1SDaniel T. Lee struct {
16*59929cd1SDaniel T. Lee __uint(type, BPF_MAP_TYPE_HASH);
17*59929cd1SDaniel T. Lee __type(key, u64);
18*59929cd1SDaniel T. Lee __type(value, u32);
19*59929cd1SDaniel T. Lee __uint(max_entries, MAX_IPS);
20*59929cd1SDaniel T. Lee } ip_map SEC(".maps");
2172874418SBrendan Gregg
2272874418SBrendan Gregg SEC("perf_event")
do_sample(struct bpf_perf_event_data * ctx)2372874418SBrendan Gregg int do_sample(struct bpf_perf_event_data *ctx)
2472874418SBrendan Gregg {
2572874418SBrendan Gregg u64 ip;
2672874418SBrendan Gregg u32 *value, init_val = 1;
2772874418SBrendan Gregg
282dbb4c05SMichael Holzheu ip = PT_REGS_IP(&ctx->regs);
2972874418SBrendan Gregg value = bpf_map_lookup_elem(&ip_map, &ip);
3072874418SBrendan Gregg if (value)
3172874418SBrendan Gregg *value += 1;
3272874418SBrendan Gregg else
3372874418SBrendan Gregg /* E2BIG not tested for this example only */
3472874418SBrendan Gregg bpf_map_update_elem(&ip_map, &ip, &init_val, BPF_NOEXIST);
3572874418SBrendan Gregg
3672874418SBrendan Gregg return 0;
3772874418SBrendan Gregg }
3872874418SBrendan Gregg char _license[] SEC("license") = "GPL";
39