Loading...
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#include <syscall.h>
10#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
11
12static inline unsigned int bpf_num_possible_cpus(void)
13{
14 int possible_cpus = libbpf_num_possible_cpus();
15
16 if (possible_cpus < 0) {
17 printf("Failed to get # of possible cpus: '%s'!\n",
18 strerror(-possible_cpus));
19 exit(1);
20 }
21 return possible_cpus;
22}
23
24/* Copy up to sz - 1 bytes from zero-terminated src string and ensure that dst
25 * is zero-terminated string no matter what (unless sz == 0, in which case
26 * it's a no-op). It's conceptually close to FreeBSD's strlcpy(), but differs
27 * in what is returned. Given this is internal helper, it's trivial to extend
28 * this, when necessary. Use this instead of strncpy inside libbpf source code.
29 */
30static inline void bpf_strlcpy(char *dst, const char *src, size_t sz)
31{
32 size_t i;
33
34 if (sz == 0)
35 return;
36
37 sz--;
38 for (i = 0; i < sz && src[i]; i++)
39 dst[i] = src[i];
40 dst[i] = '\0';
41}
42
43#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
44
45#define BPF_DECLARE_PERCPU(type, name) \
46 struct { type v; /* padding */ } __bpf_percpu_val_align \
47 name[bpf_num_possible_cpus()]
48#define bpf_percpu(name, cpu) name[(cpu)].v
49
50#ifndef ARRAY_SIZE
51# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
52#endif
53
54#ifndef sizeof_field
55#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
56#endif
57
58#ifndef offsetofend
59#define offsetofend(TYPE, MEMBER) \
60 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
61#endif
62
63/* Availability of gettid across glibc versions is hit-and-miss, therefore
64 * fallback to syscall in this macro and use it everywhere.
65 */
66#ifndef sys_gettid
67#define sys_gettid() syscall(SYS_gettid)
68#endif
69
70#ifndef ENOTSUPP
71#define ENOTSUPP 524
72#endif
73
74#endif /* __BPF_UTIL__ */
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
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;
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#endif /* __BPF_UTIL__ */