Back to home page

OSCL-LXR

 
 

    


0001 /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
0002  *
0003  * This program is free software; you can redistribute it and/or
0004  * modify it under the terms of version 2 of the GNU General Public
0005  * License as published by the Free Software Foundation.
0006  */
0007 #include <linux/skbuff.h>
0008 #include <linux/netdevice.h>
0009 #include <linux/version.h>
0010 #include <uapi/linux/bpf.h>
0011 #include <bpf/bpf_helpers.h>
0012 #include <bpf/bpf_tracing.h>
0013 #include "trace_common.h"
0014 
0015 struct {
0016     __uint(type, BPF_MAP_TYPE_HASH);
0017     __type(key, long);
0018     __type(value, long);
0019     __uint(max_entries, 1024);
0020 } my_map SEC(".maps");
0021 
0022 /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
0023  * example will no longer be meaningful
0024  */
0025 SEC("kprobe/kfree_skb")
0026 int bpf_prog2(struct pt_regs *ctx)
0027 {
0028     long loc = 0;
0029     long init_val = 1;
0030     long *value;
0031 
0032     /* read ip of kfree_skb caller.
0033      * non-portable version of __builtin_return_address(0)
0034      */
0035     BPF_KPROBE_READ_RET_IP(loc, ctx);
0036 
0037     value = bpf_map_lookup_elem(&my_map, &loc);
0038     if (value)
0039         *value += 1;
0040     else
0041         bpf_map_update_elem(&my_map, &loc, &init_val, BPF_ANY);
0042     return 0;
0043 }
0044 
0045 static unsigned int log2(unsigned int v)
0046 {
0047     unsigned int r;
0048     unsigned int shift;
0049 
0050     r = (v > 0xFFFF) << 4; v >>= r;
0051     shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
0052     shift = (v > 0xF) << 2; v >>= shift; r |= shift;
0053     shift = (v > 0x3) << 1; v >>= shift; r |= shift;
0054     r |= (v >> 1);
0055     return r;
0056 }
0057 
0058 static unsigned int log2l(unsigned long v)
0059 {
0060     unsigned int hi = v >> 32;
0061     if (hi)
0062         return log2(hi) + 32;
0063     else
0064         return log2(v);
0065 }
0066 
0067 struct hist_key {
0068     char comm[16];
0069     u64 pid_tgid;
0070     u64 uid_gid;
0071     u64 index;
0072 };
0073 
0074 struct {
0075     __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
0076     __uint(key_size, sizeof(struct hist_key));
0077     __uint(value_size, sizeof(long));
0078     __uint(max_entries, 1024);
0079 } my_hist_map SEC(".maps");
0080 
0081 SEC("kprobe/" SYSCALL(sys_write))
0082 int bpf_prog3(struct pt_regs *ctx)
0083 {
0084     long write_size = PT_REGS_PARM3(ctx);
0085     long init_val = 1;
0086     long *value;
0087     struct hist_key key;
0088 
0089     key.index = log2l(write_size);
0090     key.pid_tgid = bpf_get_current_pid_tgid();
0091     key.uid_gid = bpf_get_current_uid_gid();
0092     bpf_get_current_comm(&key.comm, sizeof(key.comm));
0093 
0094     value = bpf_map_lookup_elem(&my_hist_map, &key);
0095     if (value)
0096         __sync_fetch_and_add(value, 1);
0097     else
0098         bpf_map_update_elem(&my_hist_map, &key, &init_val, BPF_ANY);
0099     return 0;
0100 }
0101 char _license[] SEC("license") = "GPL";
0102 u32 _version SEC("version") = LINUX_VERSION_CODE;