libbpf_legacy.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
  2. /*
  3. * Libbpf legacy APIs (either discouraged or deprecated, as mentioned in [0])
  4. *
  5. * [0] https://docs.google.com/document/d/1UyjTZuPFWiPFyKk1tV5an11_iaRuec6U-ZESZ54nNTY
  6. *
  7. * Copyright (C) 2021 Facebook
  8. */
  9. #ifndef __LIBBPF_LEGACY_BPF_H
  10. #define __LIBBPF_LEGACY_BPF_H
  11. #include <linux/bpf.h>
  12. #include <stdbool.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include "libbpf_common.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* As of libbpf 1.0 libbpf_set_strict_mode() and enum libbpf_struct_mode have
  20. * no effect. But they are left in libbpf_legacy.h so that applications that
  21. * prepared for libbpf 1.0 before final release by using
  22. * libbpf_set_strict_mode() still work with libbpf 1.0+ without any changes.
  23. */
  24. enum libbpf_strict_mode {
  25. /* Turn on all supported strict features of libbpf to simulate libbpf
  26. * v1.0 behavior.
  27. * This will be the default behavior in libbpf v1.0.
  28. */
  29. LIBBPF_STRICT_ALL = 0xffffffff,
  30. /*
  31. * Disable any libbpf 1.0 behaviors. This is the default before libbpf
  32. * v1.0. It won't be supported anymore in v1.0, please update your
  33. * code so that it handles LIBBPF_STRICT_ALL mode before libbpf v1.0.
  34. */
  35. LIBBPF_STRICT_NONE = 0x00,
  36. /*
  37. * Return NULL pointers on error, not ERR_PTR(err).
  38. * Additionally, libbpf also always sets errno to corresponding Exx
  39. * (positive) error code.
  40. */
  41. LIBBPF_STRICT_CLEAN_PTRS = 0x01,
  42. /*
  43. * Return actual error codes from low-level APIs directly, not just -1.
  44. * Additionally, libbpf also always sets errno to corresponding Exx
  45. * (positive) error code.
  46. */
  47. LIBBPF_STRICT_DIRECT_ERRS = 0x02,
  48. /*
  49. * Enforce strict BPF program section (SEC()) names.
  50. * E.g., while prefiously SEC("xdp_whatever") or SEC("perf_event_blah") were
  51. * allowed, with LIBBPF_STRICT_SEC_PREFIX this will become
  52. * unrecognized by libbpf and would have to be just SEC("xdp") and
  53. * SEC("xdp") and SEC("perf_event").
  54. *
  55. * Note, in this mode the program pin path will be based on the
  56. * function name instead of section name.
  57. *
  58. * Additionally, routines in the .text section are always considered
  59. * sub-programs. Legacy behavior allows for a single routine in .text
  60. * to be a program.
  61. */
  62. LIBBPF_STRICT_SEC_NAME = 0x04,
  63. /*
  64. * Disable the global 'bpf_objects_list'. Maintaining this list adds
  65. * a race condition to bpf_object__open() and bpf_object__close().
  66. * Clients can maintain it on their own if it is valuable for them.
  67. */
  68. LIBBPF_STRICT_NO_OBJECT_LIST = 0x08,
  69. /*
  70. * Automatically bump RLIMIT_MEMLOCK using setrlimit() before the
  71. * first BPF program or map creation operation. This is done only if
  72. * kernel is too old to support memcg-based memory accounting for BPF
  73. * subsystem. By default, RLIMIT_MEMLOCK limit is set to RLIM_INFINITY,
  74. * but it can be overriden with libbpf_set_memlock_rlim() API.
  75. * Note that libbpf_set_memlock_rlim() needs to be called before
  76. * the very first bpf_prog_load(), bpf_map_create() or bpf_object__load()
  77. * operation.
  78. */
  79. LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK = 0x10,
  80. /*
  81. * Error out on any SEC("maps") map definition, which are deprecated
  82. * in favor of BTF-defined map definitions in SEC(".maps").
  83. */
  84. LIBBPF_STRICT_MAP_DEFINITIONS = 0x20,
  85. __LIBBPF_STRICT_LAST,
  86. };
  87. LIBBPF_API int libbpf_set_strict_mode(enum libbpf_strict_mode mode);
  88. /**
  89. * @brief **libbpf_get_error()** extracts the error code from the passed
  90. * pointer
  91. * @param ptr pointer returned from libbpf API function
  92. * @return error code; or 0 if no error occured
  93. *
  94. * Note, as of libbpf 1.0 this function is not necessary and not recommended
  95. * to be used. Libbpf doesn't return error code embedded into the pointer
  96. * itself. Instead, NULL is returned on error and error code is passed through
  97. * thread-local errno variable. **libbpf_get_error()** is just returning -errno
  98. * value if it receives NULL, which is correct only if errno hasn't been
  99. * modified between libbpf API call and corresponding **libbpf_get_error()**
  100. * call. Prefer to check return for NULL and use errno directly.
  101. *
  102. * This API is left in libbpf 1.0 to allow applications that were 1.0-ready
  103. * before final libbpf 1.0 without needing to change them.
  104. */
  105. LIBBPF_API long libbpf_get_error(const void *ptr);
  106. #define DECLARE_LIBBPF_OPTS LIBBPF_OPTS
  107. /* "Discouraged" APIs which don't follow consistent libbpf naming patterns.
  108. * They are normally a trivial aliases or wrappers for proper APIs and are
  109. * left to minimize unnecessary disruption for users of libbpf. But they
  110. * shouldn't be used going forward.
  111. */
  112. struct bpf_program;
  113. struct bpf_map;
  114. struct btf;
  115. struct btf_ext;
  116. LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
  117. LIBBPF_API enum bpf_prog_type bpf_program__get_type(const struct bpf_program *prog);
  118. LIBBPF_API enum bpf_attach_type bpf_program__get_expected_attach_type(const struct bpf_program *prog);
  119. LIBBPF_API const char *bpf_map__get_pin_path(const struct bpf_map *map);
  120. LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
  121. LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext, __u32 *size);
  122. #ifdef __cplusplus
  123. } /* extern "C" */
  124. #endif
  125. #endif /* __LIBBPF_LEGACY_BPF_H */