lwt_len_hist_kern.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* Copyright (c) 2016 Thomas Graf <[email protected]>
  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. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <uapi/linux/bpf.h>
  13. #include <uapi/linux/if_ether.h>
  14. #include <uapi/linux/ip.h>
  15. #include <uapi/linux/in.h>
  16. #include <bpf/bpf_helpers.h>
  17. struct bpf_elf_map {
  18. __u32 type;
  19. __u32 size_key;
  20. __u32 size_value;
  21. __u32 max_elem;
  22. __u32 flags;
  23. __u32 id;
  24. __u32 pinning;
  25. };
  26. struct bpf_elf_map SEC("maps") lwt_len_hist_map = {
  27. .type = BPF_MAP_TYPE_PERCPU_HASH,
  28. .size_key = sizeof(__u64),
  29. .size_value = sizeof(__u64),
  30. .pinning = 2,
  31. .max_elem = 1024,
  32. };
  33. static unsigned int log2(unsigned int v)
  34. {
  35. unsigned int r;
  36. unsigned int shift;
  37. r = (v > 0xFFFF) << 4; v >>= r;
  38. shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
  39. shift = (v > 0xF) << 2; v >>= shift; r |= shift;
  40. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  41. r |= (v >> 1);
  42. return r;
  43. }
  44. static unsigned int log2l(unsigned long v)
  45. {
  46. unsigned int hi = v >> 32;
  47. if (hi)
  48. return log2(hi) + 32;
  49. else
  50. return log2(v);
  51. }
  52. SEC("len_hist")
  53. int do_len_hist(struct __sk_buff *skb)
  54. {
  55. __u64 *value, key, init_val = 1;
  56. key = log2l(skb->len);
  57. value = bpf_map_lookup_elem(&lwt_len_hist_map, &key);
  58. if (value)
  59. __sync_fetch_and_add(value, 1);
  60. else
  61. bpf_map_update_elem(&lwt_len_hist_map, &key, &init_val, BPF_ANY);
  62. return BPF_OK;
  63. }
  64. char _license[] SEC("license") = "GPL";