bpf_util.h 982 B

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