lwt_len_hist.bpf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
  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 "vmlinux.h"
  13. #include <bpf/bpf_helpers.h>
  14. struct {
  15. __uint(type, BPF_MAP_TYPE_PERCPU_HASH);
  16. __type(key, u64);
  17. __type(value, u64);
  18. __uint(pinning, LIBBPF_PIN_BY_NAME);
  19. __uint(max_entries, 1024);
  20. } lwt_len_hist_map SEC(".maps");
  21. static unsigned int log2(unsigned int v)
  22. {
  23. unsigned int r;
  24. unsigned int shift;
  25. r = (v > 0xFFFF) << 4; v >>= r;
  26. shift = (v > 0xFF) << 3; v >>= shift; r |= shift;
  27. shift = (v > 0xF) << 2; v >>= shift; r |= shift;
  28. shift = (v > 0x3) << 1; v >>= shift; r |= shift;
  29. r |= (v >> 1);
  30. return r;
  31. }
  32. static unsigned int log2l(unsigned long v)
  33. {
  34. unsigned int hi = v >> 32;
  35. if (hi)
  36. return log2(hi) + 32;
  37. else
  38. return log2(v);
  39. }
  40. SEC("len_hist")
  41. int do_len_hist(struct __sk_buff *skb)
  42. {
  43. __u64 *value, key, init_val = 1;
  44. key = log2l(skb->len);
  45. value = bpf_map_lookup_elem(&lwt_len_hist_map, &key);
  46. if (value)
  47. __sync_fetch_and_add(value, 1);
  48. else
  49. bpf_map_update_elem(&lwt_len_hist_map, &key, &init_val, BPF_ANY);
  50. return BPF_OK;
  51. }
  52. char _license[] SEC("license") = "GPL";