bpf_util.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <asm/byteorder.h>
  8. #if __BYTE_ORDER == __LITTLE_ENDIAN
  9. # define __bpf_ntohs(x) __builtin_bswap16(x)
  10. # define __bpf_htons(x) __builtin_bswap16(x)
  11. #elif __BYTE_ORDER == __BIG_ENDIAN
  12. # define __bpf_ntohs(x) (x)
  13. # define __bpf_htons(x) (x)
  14. #else
  15. # error "Fix your __BYTE_ORDER?!"
  16. #endif
  17. #define bpf_htons(x) \
  18. (__builtin_constant_p(x) ? \
  19. __constant_htons(x) : __bpf_htons(x))
  20. #define bpf_ntohs(x) \
  21. (__builtin_constant_p(x) ? \
  22. __constant_ntohs(x) : __bpf_ntohs(x))
  23. static inline unsigned int bpf_num_possible_cpus(void)
  24. {
  25. static const char *fcpu = "/sys/devices/system/cpu/possible";
  26. unsigned int start, end, possible_cpus = 0;
  27. char buff[128];
  28. FILE *fp;
  29. fp = fopen(fcpu, "r");
  30. if (!fp) {
  31. printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
  32. exit(1);
  33. }
  34. while (fgets(buff, sizeof(buff), fp)) {
  35. if (sscanf(buff, "%u-%u", &start, &end) == 2) {
  36. possible_cpus = start == 0 ? end + 1 : 0;
  37. break;
  38. }
  39. }
  40. fclose(fp);
  41. if (!possible_cpus) {
  42. printf("Failed to retrieve # possible CPUs!\n");
  43. exit(1);
  44. }
  45. return possible_cpus;
  46. }
  47. #endif /* __BPF_UTIL__ */