libbpf_common.h 2.7 KB

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