blob: 7de27966103d310ba4c9905ace65bb5b6594b45e [file] [log] [blame]
Daniel Borkmanne00c7b22016-11-26 01:28:09 +01001#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 Borkmann43bcf702017-04-27 01:39:34 +02009#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 Borkmanne00c7b22016-11-26 01:28:09 +010028static 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
57#endif /* __BPF_UTIL__ */