18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __BPF_UTIL__
38c2ecf20Sopenharmony_ci#define __BPF_UTIL__
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <stdio.h>
68c2ecf20Sopenharmony_ci#include <stdlib.h>
78c2ecf20Sopenharmony_ci#include <string.h>
88c2ecf20Sopenharmony_ci#include <errno.h>
98c2ecf20Sopenharmony_ci#include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic inline unsigned int bpf_num_possible_cpus(void)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	int possible_cpus = libbpf_num_possible_cpus();
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	if (possible_cpus < 0) {
168c2ecf20Sopenharmony_ci		printf("Failed to get # of possible cpus: '%s'!\n",
178c2ecf20Sopenharmony_ci		       strerror(-possible_cpus));
188c2ecf20Sopenharmony_ci		exit(1);
198c2ecf20Sopenharmony_ci	}
208c2ecf20Sopenharmony_ci	return possible_cpus;
218c2ecf20Sopenharmony_ci}
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define __bpf_percpu_val_align	__attribute__((__aligned__(8)))
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define BPF_DECLARE_PERCPU(type, name)				\
268c2ecf20Sopenharmony_ci	struct { type v; /* padding */ } __bpf_percpu_val_align	\
278c2ecf20Sopenharmony_ci		name[bpf_num_possible_cpus()]
288c2ecf20Sopenharmony_ci#define bpf_percpu(name, cpu) name[(cpu)].v
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#ifndef ARRAY_SIZE
318c2ecf20Sopenharmony_ci# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#ifndef sizeof_field
358c2ecf20Sopenharmony_ci#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#ifndef offsetofend
398c2ecf20Sopenharmony_ci#define offsetofend(TYPE, MEMBER) \
408c2ecf20Sopenharmony_ci	(offsetof(TYPE, MEMBER)	+ sizeof_field(TYPE, MEMBER))
418c2ecf20Sopenharmony_ci#endif
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#endif /* __BPF_UTIL__ */
44