| Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 1 | #ifndef __BPF_UTIL__ |
| 2 | #define __BPF_UTIL__ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <errno.h> |
| 8 | |
| Daniel Borkmann | 43bcf70 | 2017-04-27 01:39:34 +0200 | [diff] [blame] | 9 | #include <asm/byteorder.h> |
| 10 | |
| 11 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 12 | # define __bpf_ntohs(x) __builtin_bswap16(x) |
| 13 | # define __bpf_htons(x) __builtin_bswap16(x) |
| 14 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 15 | # define __bpf_ntohs(x) (x) |
| 16 | # define __bpf_htons(x) (x) |
| 17 | #else |
| 18 | # error "Fix your __BYTE_ORDER?!" |
| 19 | #endif |
| 20 | |
| 21 | #define bpf_htons(x) \ |
| 22 | (__builtin_constant_p(x) ? \ |
| 23 | __constant_htons(x) : __bpf_htons(x)) |
| 24 | #define bpf_ntohs(x) \ |
| 25 | (__builtin_constant_p(x) ? \ |
| 26 | __constant_ntohs(x) : __bpf_ntohs(x)) |
| 27 | |
| Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 28 | static inline unsigned int bpf_num_possible_cpus(void) |
| 29 | { |
| 30 | static const char *fcpu = "/sys/devices/system/cpu/possible"; |
| 31 | unsigned int start, end, possible_cpus = 0; |
| 32 | char buff[128]; |
| 33 | FILE *fp; |
| 34 | |
| 35 | fp = fopen(fcpu, "r"); |
| 36 | if (!fp) { |
| 37 | printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno)); |
| 38 | exit(1); |
| 39 | } |
| 40 | |
| 41 | while (fgets(buff, sizeof(buff), fp)) { |
| 42 | if (sscanf(buff, "%u-%u", &start, &end) == 2) { |
| 43 | possible_cpus = start == 0 ? end + 1 : 0; |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fclose(fp); |
| 49 | if (!possible_cpus) { |
| 50 | printf("Failed to retrieve # possible CPUs!\n"); |
| 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | return possible_cpus; |
| 55 | } |
| 56 | |
| Daniel Borkmann | f3515b5 | 2017-04-27 01:39:35 +0200 | [diff] [blame^] | 57 | #define __bpf_percpu_val_align __attribute__((__aligned__(8))) |
| 58 | |
| 59 | #define BPF_DECLARE_PERCPU(type, name) \ |
| 60 | struct { type v; /* padding */ } __bpf_percpu_val_align \ |
| 61 | name[bpf_num_possible_cpus()] |
| 62 | #define bpf_percpu(name, cpu) name[(cpu)].v |
| 63 | |
| Daniel Borkmann | e00c7b2 | 2016-11-26 01:28:09 +0100 | [diff] [blame] | 64 | #endif /* __BPF_UTIL__ */ |