bpf_util.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. static inline unsigned int bpf_num_possible_cpus(void)
  9. {
  10. static const char *fcpu = "/sys/devices/system/cpu/possible";
  11. unsigned int start, end, possible_cpus = 0;
  12. char buff[128];
  13. FILE *fp;
  14. int n;
  15. fp = fopen(fcpu, "r");
  16. if (!fp) {
  17. printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
  18. exit(1);
  19. }
  20. while (fgets(buff, sizeof(buff), fp)) {
  21. n = sscanf(buff, "%u-%u", &start, &end);
  22. if (n == 0) {
  23. printf("Failed to retrieve # possible CPUs!\n");
  24. exit(1);
  25. } else if (n == 1) {
  26. end = start;
  27. }
  28. possible_cpus = start == 0 ? end + 1 : 0;
  29. break;
  30. }
  31. fclose(fp);
  32. return possible_cpus;
  33. }
  34. #define __bpf_percpu_val_align __attribute__((__aligned__(8)))
  35. #define BPF_DECLARE_PERCPU(type, name) \
  36. struct { type v; /* padding */ } __bpf_percpu_val_align \
  37. name[bpf_num_possible_cpus()]
  38. #define bpf_percpu(name, cpu) name[(cpu)].v
  39. #ifndef ARRAY_SIZE
  40. # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  41. #endif
  42. #endif /* __BPF_UTIL__ */