tracex2_kern.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_helpers.h"
  12. struct bpf_map_def SEC("maps") my_map = {
  13. .type = BPF_MAP_TYPE_HASH,
  14. .key_size = sizeof(long),
  15. .value_size = sizeof(long),
  16. .max_entries = 1024,
  17. };
  18. /* kprobe is NOT a stable ABI. If kernel internals change this bpf+kprobe
  19. * example will no longer be meaningful
  20. */
  21. SEC("kprobe/kfree_skb")
  22. int bpf_prog2(struct pt_regs *ctx)
  23. {
  24. long loc = 0;
  25. long init_val = 1;
  26. long *value;
  27. /* read ip of kfree_skb caller.
  28. * non-portable version of __builtin_return_address(0)
  29. */
  30. BPF_KPROBE_READ_RET_IP(loc, ctx);
  31. value = bpf_map_lookup_elem(&my_map, &loc);
  32. if (value)
  33. *value += 1;
  34. else
  35. bpf_map_update_elem(&my_map, &loc, &init_val, BPF_ANY);
  36. return 0;
  37. }
  38. static unsigned int log2(unsigned int v)
  39. {
  40. unsigned int r;
  41. unsigned int shift;
  42. r = (v > 0xFFFF) << 4; v >>= r;
  43. shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
  44. shift = (v > 0xF) << 2; v >>= shift; r |= shift;
  45. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  46. r |= (v >> 1);
  47. return r;
  48. }
  49. static unsigned int log2l(unsigned long v)
  50. {
  51. unsigned int hi = v >> 32;
  52. if (hi)
  53. return log2(hi) + 32;
  54. else
  55. return log2(v);
  56. }
  57. struct hist_key {
  58. char comm[16];
  59. u64 pid_tgid;
  60. u64 uid_gid;
  61. u64 index;
  62. };
  63. struct bpf_map_def SEC("maps") my_hist_map = {
  64. .type = BPF_MAP_TYPE_PERCPU_HASH,
  65. .key_size = sizeof(struct hist_key),
  66. .value_size = sizeof(long),
  67. .max_entries = 1024,
  68. };
  69. SEC("kprobe/sys_write")
  70. int bpf_prog3(struct pt_regs *ctx)
  71. {
  72. long write_size = PT_REGS_PARM3(ctx);
  73. long init_val = 1;
  74. long *value;
  75. struct hist_key key;
  76. key.index = log2l(write_size);
  77. key.pid_tgid = bpf_get_current_pid_tgid();
  78. key.uid_gid = bpf_get_current_uid_gid();
  79. bpf_get_current_comm(&key.comm, sizeof(key.comm));
  80. value = bpf_map_lookup_elem(&my_hist_map, &key);
  81. if (value)
  82. __sync_fetch_and_add(value, 1);
  83. else
  84. bpf_map_update_elem(&my_hist_map, &key, &init_val, BPF_ANY);
  85. return 0;
  86. }
  87. char _license[] SEC("license") = "GPL";
  88. u32 _version SEC("version") = LINUX_VERSION_CODE;