Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
0002 
0003 /*
0004  * Common user-facing libbpf helpers.
0005  *
0006  * Copyright (c) 2019 Facebook
0007  */
0008 
0009 #ifndef __LIBBPF_LIBBPF_COMMON_H
0010 #define __LIBBPF_LIBBPF_COMMON_H
0011 
0012 #include <string.h>
0013 #include "libbpf_version.h"
0014 
0015 #ifndef LIBBPF_API
0016 #define LIBBPF_API __attribute__((visibility("default")))
0017 #endif
0018 
0019 #define LIBBPF_DEPRECATED(msg) __attribute__((deprecated(msg)))
0020 
0021 /* Mark a symbol as deprecated when libbpf version is >= {major}.{minor} */
0022 #define LIBBPF_DEPRECATED_SINCE(major, minor, msg)              \
0023     __LIBBPF_MARK_DEPRECATED_ ## major ## _ ## minor            \
0024         (LIBBPF_DEPRECATED("libbpf v" # major "." # minor "+: " msg))
0025 
0026 #define __LIBBPF_CURRENT_VERSION_GEQ(major, minor)              \
0027     (LIBBPF_MAJOR_VERSION > (major) ||                  \
0028      (LIBBPF_MAJOR_VERSION == (major) && LIBBPF_MINOR_VERSION >= (minor)))
0029 
0030 /* Add checks for other versions below when planning deprecation of API symbols
0031  * with the LIBBPF_DEPRECATED_SINCE macro.
0032  */
0033 #if __LIBBPF_CURRENT_VERSION_GEQ(1, 0)
0034 #define __LIBBPF_MARK_DEPRECATED_1_0(X) X
0035 #else
0036 #define __LIBBPF_MARK_DEPRECATED_1_0(X)
0037 #endif
0038 
0039 /* This set of internal macros allows to do "function overloading" based on
0040  * number of arguments provided by used in backwards-compatible way during the
0041  * transition to libbpf 1.0
0042  * It's ugly but necessary evil that will be cleaned up when we get to 1.0.
0043  * See bpf_prog_load() overload for example.
0044  */
0045 #define ___libbpf_cat(A, B) A ## B
0046 #define ___libbpf_select(NAME, NUM) ___libbpf_cat(NAME, NUM)
0047 #define ___libbpf_nth(_1, _2, _3, _4, _5, _6, N, ...) N
0048 #define ___libbpf_cnt(...) ___libbpf_nth(__VA_ARGS__, 6, 5, 4, 3, 2, 1)
0049 #define ___libbpf_overload(NAME, ...) ___libbpf_select(NAME, ___libbpf_cnt(__VA_ARGS__))(__VA_ARGS__)
0050 
0051 /* Helper macro to declare and initialize libbpf options struct
0052  *
0053  * This dance with uninitialized declaration, followed by memset to zero,
0054  * followed by assignment using compound literal syntax is done to preserve
0055  * ability to use a nice struct field initialization syntax and **hopefully**
0056  * have all the padding bytes initialized to zero. It's not guaranteed though,
0057  * when copying literal, that compiler won't copy garbage in literal's padding
0058  * bytes, but that's the best way I've found and it seems to work in practice.
0059  *
0060  * Macro declares opts struct of given type and name, zero-initializes,
0061  * including any extra padding, it with memset() and then assigns initial
0062  * values provided by users in struct initializer-syntax as varargs.
0063  */
0064 #define LIBBPF_OPTS(TYPE, NAME, ...)                        \
0065     struct TYPE NAME = ({                           \
0066         memset(&NAME, 0, sizeof(struct TYPE));              \
0067         (struct TYPE) {                         \
0068             .sz = sizeof(struct TYPE),              \
0069             __VA_ARGS__                     \
0070         };                              \
0071     })
0072 
0073 #endif /* __LIBBPF_LIBBPF_COMMON_H */