xref: /openbmc/linux/tools/lib/bpf/libbpf_common.h (revision be80e9cd)
1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2 
3 /*
4  * Common user-facing libbpf helpers.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 
9 #ifndef __LIBBPF_LIBBPF_COMMON_H
10 #define __LIBBPF_LIBBPF_COMMON_H
11 
12 #include <string.h>
13 #include "libbpf_version.h"
14 
15 #ifndef LIBBPF_API
16 #define LIBBPF_API __attribute__((visibility("default")))
17 #endif
18 
19 #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
20 
21 /* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */
22 #define LIBBPF_DEPRECATED_SINCE(major, minor, msg)			    \
23 	__LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor		    \
24 		(LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
25 
26 #define __LIBBPF_CURRENT_VERSION_GEQ(major, minor)			    \
27 	(LIBBPF_MAJOR_VERSION > (major) ||				    \
28 	 (LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor)))
29 
30 /* Add checks for other versions below when planning deprecation of API symbols
31  * with the LIBBPF_DEPRECATED_SINCE macro.
32  */
33 #if __LIBBPF_CURRENT_VERSION_GEQ(0, 6)
34 #define __LIBBPF_MARK_DEPRECATED_0_6(X) X
35 #else
36 #define __LIBBPF_MARK_DEPRECATED_0_6(X)
37 #endif
38 #if __LIBBPF_CURRENT_VERSION_GEQ(0, 7)
39 #define __LIBBPF_MARK_DEPRECATED_0_7(X) X
40 #else
41 #define __LIBBPF_MARK_DEPRECATED_0_7(X)
42 #endif
43 
44 /* Helper macro to declare and initialize libbpf options struct
45  *
46  * This dance with uninitialized declaration, followed by memset to zero,
47  * followed by assignment using compound literal syntax is done to preserve
48  * ability to use a nice struct field initialization syntax and **hopefully**
49  * have all the padding bytes initialized to zero. It's not guaranteed though,
50  * when copying literal, that compiler won't copy garbage in literal's padding
51  * bytes, but that's the best way I've found and it seems to work in practice.
52  *
53  * Macro declares opts struct of given type and name, zero-initializes,
54  * including any extra padding, it with memset() and then assigns initial
55  * values provided by users in struct initializer-syntax as varargs.
56  */
57 #define LIBBPF_OPTS(TYPE, NAME, ...)					    \
58 	struct TYPE NAME = ({ 						    \
59 		memset(&NAME, 0, sizeof(struct TYPE));			    \
60 		(struct TYPE) {						    \
61 			.sz = sizeof(struct TYPE),			    \
62 			__VA_ARGS__					    \
63 		};							    \
64 	})
65 
66 #endif /* __LIBBPF_LIBBPF_COMMON_H */
67