• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __BPF_UTIL__
3 #define __BPF_UTIL__
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 
bpf_num_possible_cpus(void)10 static inline unsigned int bpf_num_possible_cpus(void)
11 {
12 	static const char *fcpu = "/sys/devices/system/cpu/possible";
13 	unsigned int start, end, possible_cpus = 0;
14 	char buff[128];
15 	FILE *fp;
16 	int n;
17 
18 	fp = fopen(fcpu, "r");
19 	if (!fp) {
20 		printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
21 		exit(1);
22 	}
23 
24 	while (fgets(buff, sizeof(buff), fp)) {
25 		n = sscanf(buff, "%u-%u", &start, &end);
26 		if (n == 0) {
27 			printf("Failed to retrieve # possible CPUs!\n");
28 			exit(1);
29 		} else if (n == 1) {
30 			end = start;
31 		}
32 		possible_cpus = start == 0 ? end + 1 : 0;
33 		break;
34 	}
35 	fclose(fp);
36 
37 	return possible_cpus;
38 }
39 
40 #define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
41 
42 #define BPF_DECLARE_PERCPU(type, name)				\
43 	struct { type v; /* padding */ } __bpf_percpu_val_align	\
44 		name[bpf_num_possible_cpus()]
45 #define bpf_percpu(name, cpu) name[(cpu)].v
46 
47 #ifndef ARRAY_SIZE
48 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
49 #endif
50 
51 #endif /* __BPF_UTIL__ */
52