xref: /aosp_15_r20/external/libbpf/src/libbpf_common.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker 
3*f7c14bbaSAndroid Build Coastguard Worker /*
4*f7c14bbaSAndroid Build Coastguard Worker  * Common user-facing libbpf helpers.
5*f7c14bbaSAndroid Build Coastguard Worker  *
6*f7c14bbaSAndroid Build Coastguard Worker  * Copyright (c) 2019 Facebook
7*f7c14bbaSAndroid Build Coastguard Worker  */
8*f7c14bbaSAndroid Build Coastguard Worker 
9*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LIBBPF_LIBBPF_COMMON_H
10*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_LIBBPF_COMMON_H
11*f7c14bbaSAndroid Build Coastguard Worker 
12*f7c14bbaSAndroid Build Coastguard Worker #include <string.h>
13*f7c14bbaSAndroid Build Coastguard Worker #include "libbpf_version.h"
14*f7c14bbaSAndroid Build Coastguard Worker 
15*f7c14bbaSAndroid Build Coastguard Worker #ifndef LIBBPF_API
16*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_API __attribute__((visibility("default")))
17*f7c14bbaSAndroid Build Coastguard Worker #endif
18*f7c14bbaSAndroid Build Coastguard Worker 
19*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
20*f7c14bbaSAndroid Build Coastguard Worker 
21*f7c14bbaSAndroid Build Coastguard Worker /* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */
22*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_DEPRECATED_SINCE(major, minor, msg)			    \
23*f7c14bbaSAndroid Build Coastguard Worker 	__LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor		    \
24*f7c14bbaSAndroid Build Coastguard Worker 		(LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
25*f7c14bbaSAndroid Build Coastguard Worker 
26*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_CURRENT_VERSION_GEQ(major, minor)			    \
27*f7c14bbaSAndroid Build Coastguard Worker 	(LIBBPF_MAJOR_VERSION > (major) ||				    \
28*f7c14bbaSAndroid Build Coastguard Worker 	 (LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor)))
29*f7c14bbaSAndroid Build Coastguard Worker 
30*f7c14bbaSAndroid Build Coastguard Worker /* Add checks for other versions below when planning deprecation of API symbols
31*f7c14bbaSAndroid Build Coastguard Worker  * with the LIBBPF_DEPRECATED_SINCE macro.
32*f7c14bbaSAndroid Build Coastguard Worker  */
33*f7c14bbaSAndroid Build Coastguard Worker #if __LIBBPF_CURRENT_VERSION_GEQ(1, 0)
34*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_MARK_DEPRECATED_1_0(X) X
35*f7c14bbaSAndroid Build Coastguard Worker #else
36*f7c14bbaSAndroid Build Coastguard Worker #define __LIBBPF_MARK_DEPRECATED_1_0(X)
37*f7c14bbaSAndroid Build Coastguard Worker #endif
38*f7c14bbaSAndroid Build Coastguard Worker 
39*f7c14bbaSAndroid Build Coastguard Worker /* This set of internal macros allows to do "function overloading" based on
40*f7c14bbaSAndroid Build Coastguard Worker  * number of arguments provided by used in backwards-compatible way during the
41*f7c14bbaSAndroid Build Coastguard Worker  * transition to libbpf 1.0
42*f7c14bbaSAndroid Build Coastguard Worker  * It's ugly but necessary evil that will be cleaned up when we get to 1.0.
43*f7c14bbaSAndroid Build Coastguard Worker  * See bpf_prog_load() overload for example.
44*f7c14bbaSAndroid Build Coastguard Worker  */
45*f7c14bbaSAndroid Build Coastguard Worker #define ___libbpf_cat(A, B) A ## B
46*f7c14bbaSAndroid Build Coastguard Worker #define ___libbpf_select(NAME, NUM) ___libbpf_cat(NAME, NUM)
47*f7c14bbaSAndroid Build Coastguard Worker #define ___libbpf_nth(_1, _2, _3, _4, _5, _6, N, ...) N
48*f7c14bbaSAndroid Build Coastguard Worker #define ___libbpf_cnt(...) ___libbpf_nth(__VA_ARGS__, 6, 5, 4, 3, 2, 1)
49*f7c14bbaSAndroid Build Coastguard Worker #define ___libbpf_overload(NAME, ...) ___libbpf_select(NAME, ___libbpf_cnt(__VA_ARGS__))(__VA_ARGS__)
50*f7c14bbaSAndroid Build Coastguard Worker 
51*f7c14bbaSAndroid Build Coastguard Worker /* Helper macro to declare and initialize libbpf options struct
52*f7c14bbaSAndroid Build Coastguard Worker  *
53*f7c14bbaSAndroid Build Coastguard Worker  * This dance with uninitialized declaration, followed by memset to zero,
54*f7c14bbaSAndroid Build Coastguard Worker  * followed by assignment using compound literal syntax is done to preserve
55*f7c14bbaSAndroid Build Coastguard Worker  * ability to use a nice struct field initialization syntax and **hopefully**
56*f7c14bbaSAndroid Build Coastguard Worker  * have all the padding bytes initialized to zero. It's not guaranteed though,
57*f7c14bbaSAndroid Build Coastguard Worker  * when copying literal, that compiler won't copy garbage in literal's padding
58*f7c14bbaSAndroid Build Coastguard Worker  * bytes, but that's the best way I've found and it seems to work in practice.
59*f7c14bbaSAndroid Build Coastguard Worker  *
60*f7c14bbaSAndroid Build Coastguard Worker  * Macro declares opts struct of given type and name, zero-initializes,
61*f7c14bbaSAndroid Build Coastguard Worker  * including any extra padding, it with memset() and then assigns initial
62*f7c14bbaSAndroid Build Coastguard Worker  * values provided by users in struct initializer-syntax as varargs.
63*f7c14bbaSAndroid Build Coastguard Worker  */
64*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_OPTS(TYPE, NAME, ...)					    \
65*f7c14bbaSAndroid Build Coastguard Worker 	struct TYPE NAME = ({ 						    \
66*f7c14bbaSAndroid Build Coastguard Worker 		memset(&NAME, 0, sizeof(struct TYPE));			    \
67*f7c14bbaSAndroid Build Coastguard Worker 		(struct TYPE) {						    \
68*f7c14bbaSAndroid Build Coastguard Worker 			.sz = sizeof(struct TYPE),			    \
69*f7c14bbaSAndroid Build Coastguard Worker 			__VA_ARGS__					    \
70*f7c14bbaSAndroid Build Coastguard Worker 		};							    \
71*f7c14bbaSAndroid Build Coastguard Worker 	})
72*f7c14bbaSAndroid Build Coastguard Worker 
73*f7c14bbaSAndroid Build Coastguard Worker /* Helper macro to clear and optionally reinitialize libbpf options struct
74*f7c14bbaSAndroid Build Coastguard Worker  *
75*f7c14bbaSAndroid Build Coastguard Worker  * Small helper macro to reset all fields and to reinitialize the common
76*f7c14bbaSAndroid Build Coastguard Worker  * structure size member. Values provided by users in struct initializer-
77*f7c14bbaSAndroid Build Coastguard Worker  * syntax as varargs can be provided as well to reinitialize options struct
78*f7c14bbaSAndroid Build Coastguard Worker  * specific members.
79*f7c14bbaSAndroid Build Coastguard Worker  */
80*f7c14bbaSAndroid Build Coastguard Worker #define LIBBPF_OPTS_RESET(NAME, ...)					    \
81*f7c14bbaSAndroid Build Coastguard Worker 	do {								    \
82*f7c14bbaSAndroid Build Coastguard Worker 		typeof(NAME) ___##NAME = ({ 				    \
83*f7c14bbaSAndroid Build Coastguard Worker 			memset(&___##NAME, 0, sizeof(NAME));		    \
84*f7c14bbaSAndroid Build Coastguard Worker 			(typeof(NAME)) {				    \
85*f7c14bbaSAndroid Build Coastguard Worker 				.sz = sizeof(NAME),			    \
86*f7c14bbaSAndroid Build Coastguard Worker 				__VA_ARGS__				    \
87*f7c14bbaSAndroid Build Coastguard Worker 			};						    \
88*f7c14bbaSAndroid Build Coastguard Worker 		});							    \
89*f7c14bbaSAndroid Build Coastguard Worker 		memcpy(&NAME, &___##NAME, sizeof(NAME));		    \
90*f7c14bbaSAndroid Build Coastguard Worker 	} while (0)
91*f7c14bbaSAndroid Build Coastguard Worker 
92*f7c14bbaSAndroid Build Coastguard Worker #endif /* __LIBBPF_LIBBPF_COMMON_H */
93