tracex2_kern.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. */
  7. #include <linux/skbuff.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/version.h>
  10. #include <uapi/linux/bpf.h>
  11. #include <bpf/bpf_helpers.h>
  12. #include <bpf/bpf_tracing.h>
  13. #include "trace_common.h"
  14. struct {
  15. __uint(type, BPF_MAP_TYPE_HASH);
  16. __type(key, long);
  17. __type(value, long);
  18. __uint(max_entries, 1024);
  19. } my_map SEC(".maps");
  20. /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
  21. * example will no longer be meaningful
  22. */
  23. SEC("kprobe/kfree_skb")
  24. int bpf_prog2(struct pt_regs *ctx)
  25. {
  26. long loc = 0;
  27. long init_val = 1;
  28. long *value;
  29. /* read ip of kfree_skb caller.
  30. * non-portable version of __builtin_return_address(0)
  31. */
  32. BPF_KPROBE_READ_RET_IP(loc, ctx);
  33. value = bpf_map_lookup_elem(&my_map, &loc);
  34. if (value)
  35. *value += 1;
  36. else
  37. bpf_map_update_elem(&my_map, &loc, &init_val, BPF_ANY);
  38. return 0;
  39. }
  40. static unsigned int log2(unsigned int v)
  41. {
  42. unsigned int r;
  43. unsigned int shift;
  44. r = (v > 0xFFFF) << 4; v >>= r;
  45. shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
  46. shift = (v > 0xF) << 2; v >>= shift; r |= shift;
  47. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  48. r |= (v >> 1);
  49. return r;
  50. }
  51. static unsigned int log2l(unsigned long v)
  52. {
  53. unsigned int hi = v >> 32;
  54. if (hi)
  55. return log2(hi) + 32;
  56. else
  57. return log2(v);
  58. }
  59. struct hist_key {
  60. char comm[16];
  61. u64 pid_tgid;
  62. u64 uid_gid;
  63. u64 index;
  64. };
  65. struct {
  66. __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
  67. __uint(key_size, sizeof(struct hist_key));
  68. __uint(value_size, sizeof(long));
  69. __uint(max_entries, 1024);
  70. } my_hist_map SEC(".maps");
  71. SEC("kprobe/" SYSCALL(sys_write))
  72. int bpf_prog3(struct pt_regs *ctx)
  73. {
  74. long write_size = PT_REGS_PARM3(ctx);
  75. long init_val = 1;
  76. long *value;
  77. struct hist_key key;
  78. key.index = log2l(write_size);
  79. key.pid_tgid = bpf_get_current_pid_tgid();
  80. key.uid_gid = bpf_get_current_uid_gid();
  81. bpf_get_current_comm(&key.comm, sizeof(key.comm));
  82. value = bpf_map_lookup_elem(&my_hist_map, &key);
  83. if (value)
  84. __sync_fetch_and_add(value, 1);
  85. else
  86. bpf_map_update_elem(&my_hist_map, &key, &init_val, BPF_ANY);
  87. return 0;
  88. }
  89. char _license[] SEC("license") = "GPL";
  90. u32 _version SEC("version") = LINUX_VERSION_CODE;