License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / tools / testing / selftests / bpf / bpf_util.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
e00c7b21
DB
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
10static 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;
56a268cd 16 int n;
e00c7b21
DB
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)) {
56a268cd
TM
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;
e00c7b21 31 }
56a268cd
TM
32 possible_cpus = start == 0 ? end + 1 : 0;
33 break;
e00c7b21 34 }
e00c7b21 35 fclose(fp);
e00c7b21
DB
36
37 return possible_cpus;
38}
39
f3515b5d
DB
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
e00c7b21 47#endif /* __BPF_UTIL__ */