php_ebpf.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7, 8 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: carl.guo [email protected] |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifndef PHP_EBPF_H
  20. #define PHP_EBPF_H
  21. #include <iostream>
  22. #include <unordered_set>
  23. extern zend_module_entry ebpf_module_entry;
  24. #define phpext_ebpf_ptr &ebpf_module_entry
  25. #define PHP_EBPF_VERSION "1.0.0" /* Replace with version number for your extension */
  26. #ifdef PHP_WIN32
  27. # define PHP_EBPF_API __declspec(dllexport)
  28. #elif defined(__GNUC__) && __GNUC__ >= 4
  29. # define PHP_EBPF_API __attribute__ ((visibility("default")))
  30. #else
  31. # define PHP_EBPF_API
  32. #endif
  33. extern "C" {
  34. #ifdef ZTS
  35. #include "TSRM.h"
  36. #endif
  37. }
  38. // Common definitions
  39. #define TRACE_PIPE_PATH "/sys/kernel/debug/tracing/trace_pipe"
  40. #define DEBUGFS "/sys/kernel/debug"
  41. #define EXT_NAME "ebpf"
  42. #define EXT_VERSION "1.0.0"
  43. #define REGISTER_BPF_CLASS(ce, create_obj, php_class_name, cls, method) \
  44. INIT_CLASS_ENTRY(ce, php_class_name, method); \
  45. ce.create_object = create_obj; \
  46. cls = zend_register_internal_class(&ce);
  47. // Common constants
  48. const std::vector<std::string> syscall_prefixes = {
  49. "sys_",
  50. "__x64_sys_",
  51. "__x32_compat_sys_",
  52. "__ia32_compat_sys_",
  53. "__arm64_sys_",
  54. "__s390x_sys_",
  55. "__s390_sys_",
  56. "__riscv_sys_"
  57. };
  58. void callbackfn(void *cookie, void *data, int data_size);
  59. class EbpfExtension {
  60. private:
  61. void *mod;
  62. public:
  63. zval _class_perf_event_obj;
  64. ebpf::BPF bpf;
  65. /**
  66. * @brief Default constructor for EbpfExtension
  67. */
  68. EbpfExtension() {
  69. ZVAL_UNDEF(&_class_perf_event_obj);
  70. };
  71. /**
  72. * @brief Virtual destructor for EbpfExtension
  73. */
  74. virtual ~EbpfExtension() {
  75. if (Z_TYPE(_class_perf_event_obj) != IS_UNDEF) {
  76. zval_ptr_dtor(&_class_perf_event_obj);
  77. }
  78. };
  79. ebpf::StatusTuple init(const std::string &bpf_program) {
  80. auto res = this->bpf.init(bpf_program);
  81. if (res.code() == 0)
  82. this->mod = (void *) this->bpf.get_mod();
  83. return res;
  84. }
  85. /**
  86. * @brief Add a prefix to a function name
  87. * @param prefix The prefix to add
  88. * @param name The original function name
  89. * @return The function name with prefix added
  90. */
  91. static std::string add_prefix(const std::string &prefix, const std::string &name);
  92. /**
  93. * @brief Fix syscall function name by adding appropriate prefix
  94. * @param name The original syscall function name
  95. * @return The fixed syscall function name with proper prefix
  96. */
  97. std::string fix_syscall_fnname(const std::string &name);
  98. /**
  99. * @brief Automatically load trace functions
  100. * This method is responsible for loading and initializing trace-related functions
  101. */
  102. void _trace_autoload();
  103. /**
  104. * @brief Get kprobe functions matching a regular expression
  105. * @param event_re Regular expression to match function names
  106. * @return Set of matching function names
  107. */
  108. std::unordered_set<std::string> get_kprobe_functions(const std::string &event_re);
  109. /**
  110. * @brief Attach a kfunc (kernel function) probe
  111. * @param kfn The kernel function name to attach to
  112. * @return Status tuple indicating success or failure
  113. */
  114. ebpf::StatusTuple attach_kfunc(const std::string &kfn);
  115. /**
  116. * @brief Attach an LSM (Linux Security Module) probe
  117. * @param lsm The LSM hook name to attach to
  118. * @return Status tuple indicating success or failure
  119. */
  120. ebpf::StatusTuple attach_lsm(const std::string &lsm);
  121. /**
  122. * @brief Get a BPF table class
  123. * @param table_name Name of the BPF table
  124. * @param from_attr Attribute to get from the table
  125. * @return The table class object
  126. */
  127. zval get_table_cls(const char *table_name, int from_attr);
  128. };
  129. class BPFProgType {
  130. public:
  131. static constexpr int SOCKET_FILTER = 1;
  132. static constexpr int KPROBE = 2;
  133. static constexpr int SCHED_CLS = 3;
  134. static constexpr int SCHED_ACT = 4;
  135. static constexpr int TRACEPOINT = 5;
  136. static constexpr int XDP = 6;
  137. static constexpr int PERF_EVENT = 7;
  138. static constexpr int CGROUP_SKB = 8;
  139. static constexpr int CGROUP_SOCK = 9;
  140. static constexpr int LWT_IN = 10;
  141. static constexpr int LWT_OUT = 11;
  142. static constexpr int LWT_XMIT = 12;
  143. static constexpr int SOCK_OPS = 13;
  144. static constexpr int SK_SKB = 14;
  145. static constexpr int CGROUP_DEVICE = 15;
  146. static constexpr int SK_MSG = 16;
  147. static constexpr int RAW_TRACEPOINT = 17;
  148. static constexpr int CGROUP_SOCK_ADDR = 18;
  149. static constexpr int CGROUP_SOCKOPT = 25;
  150. static constexpr int TRACING = 26;
  151. static constexpr int LSM = 29;
  152. };
  153. #define REGISTER_BPF_CONST(name) \
  154. zend_declare_class_constant_long(bpf_ce, #name, sizeof(#name) - 1, BPFProgType::name)
  155. /*
  156. Declare any global variables you may need between the BEGIN
  157. and END macros here:
  158. ZEND_BEGIN_MODULE_GLOBALS(ebpf)
  159. zend_long global_value;
  160. char *global_string;
  161. ZEND_END_MODULE_GLOBALS(ebpf)
  162. */
  163. /* Always refer to the globals in your function as EBPF_G(variable).
  164. You are encouraged to rename these macros something shorter, see
  165. examples in any other php module directory.
  166. */
  167. #define EBPF_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(ebpf, v)
  168. #if defined(ZTS) && defined(COMPILE_DL_EBPF)
  169. ZEND_TSRMLS_CACHE_EXTERN()
  170. #endif
  171. #endif /* PHP_EBPF_H */
  172. /*
  173. * Local variables:
  174. * tab-width: 4
  175. * c-basic-offset: 4
  176. * End:
  177. * vim600: noet sw=4 ts=4 fdm=marker
  178. * vim<600: noet sw=4 ts=4
  179. */