Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __BPF_UTIL__
0003 #define __BPF_UTIL__
0004 
0005 #include <stdio.h>
0006 #include <stdlib.h>
0007 #include <string.h>
0008 #include <errno.h>
0009 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
0010 
0011 static inline unsigned int bpf_num_possible_cpus(void)
0012 {
0013     int possible_cpus = libbpf_num_possible_cpus();
0014 
0015     if (possible_cpus < 0) {
0016         printf("Failed to get # of possible cpus: '%s'!\n",
0017                strerror(-possible_cpus));
0018         exit(1);
0019     }
0020     return possible_cpus;
0021 }
0022 
0023 #define __bpf_percpu_val_align  __attribute__((__aligned__(8)))
0024 
0025 #define BPF_DECLARE_PERCPU(type, name)              \
0026     struct { type v; /* padding */ } __bpf_percpu_val_align \
0027         name[bpf_num_possible_cpus()]
0028 #define bpf_percpu(name, cpu) name[(cpu)].v
0029 
0030 #ifndef ARRAY_SIZE
0031 # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
0032 #endif
0033 
0034 #ifndef sizeof_field
0035 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
0036 #endif
0037 
0038 #ifndef offsetofend
0039 #define offsetofend(TYPE, MEMBER) \
0040     (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
0041 #endif
0042 
0043 #endif /* __BPF_UTIL__ */