blob: 6c53a8906eff4c4fe6d98863050132d928de50f8 [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 Borkmanne00c7b22016-11-26 01:28:09 +01009static inline unsigned int bpf_num_possible_cpus(void)
10{
11 static const char *fcpu = "/sys/devices/system/cpu/possible";
12 unsigned int start, end, possible_cpus = 0;
13 char buff[128];
14 FILE *fp;
Thomas Meyer56a268c2017-09-08 13:19:23 +020015 int n;
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010016
17 fp = fopen(fcpu, "r");
18 if (!fp) {
19 printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
20 exit(1);
21 }
22
23 while (fgets(buff, sizeof(buff), fp)) {
Thomas Meyer56a268c2017-09-08 13:19:23 +020024 n = sscanf(buff, "%u-%u", &start, &end);
25 if (n == 0) {
26 printf("Failed to retrieve # possible CPUs!\n");
27 exit(1);
28 } else if (n == 1) {
29 end = start;
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010030 }
Thomas Meyer56a268c2017-09-08 13:19:23 +020031 possible_cpus = start == 0 ? end + 1 : 0;
32 break;
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010033 }
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010034 fclose(fp);
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010035
36 return possible_cpus;
37}
38
Daniel Borkmannf3515b52017-04-27 01:39:35 +020039#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
40
41#define BPF_DECLARE_PERCPU(type, name) \
42 struct { type v; /* padding */ } __bpf_percpu_val_align \
43 name[bpf_num_possible_cpus()]
44#define bpf_percpu(name, cpu) name[(cpu)].v
45
Daniel Borkmanne00c7b22016-11-26 01:28:09 +010046#endif /* __BPF_UTIL__ */