bpf_util.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __BPF_UTIL__
  3. #define __BPF_UTIL__
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
  9. static inline unsigned int bpf_num_possible_cpus(void)
  10. {
  11. int possible_cpus = libbpf_num_possible_cpus();
  12. if (possible_cpus < 0) {
  13. printf("Failed to get # of possible cpus: '%s'!\n",
  14. strerror(-possible_cpus));
  15. exit(1);
  16. }
  17. return possible_cpus;
  18. }
  19. #define __bpf_percpu_val_align __attribute__((__aligned__(8)))
  20. #define BPF_DECLARE_PERCPU(type, name) \
  21. struct { type v; /* padding */ } __bpf_percpu_val_align \
  22. name[bpf_num_possible_cpus()]
  23. #define bpf_percpu(name, cpu) name[(cpu)].v
  24. #ifndef ARRAY_SIZE
  25. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  26. #endif
  27. #ifndef sizeof_field
  28. #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
  29. #endif
  30. #ifndef offsetofend
  31. #define offsetofend(TYPE, MEMBER) \
  32. (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
  33. #endif
  34. #endif /* __BPF_UTIL__ */