this repo has no description
at main 12 kB view raw
1open Ctypes 2 3(** You probably don't mean to be looking into this section, it is 4 part of the stub generation process of the bindings.*) 5 6(* The C_types_generated module is generated by the build system to 7 grab the type definitions from the header files in C to ensure 8 that offsets and structs are aligned. *) 9module Types = C_type_description.Types (C_types_generated) 10 11module Functions (F : Ctypes.FOREIGN) = struct 12 open F 13 14 (* ======================================== Generics ======================================== *) 15 let libbpf_major_version = 16 foreign "libbpf_major_version" (void @-> returning uint32_t) 17 18 let libbpf_minor_version = 19 foreign "libbpf_minor_version" (void @-> returning uint32_t) 20 21 let libbpf_version_string = 22 foreign "libbpf_version_string" (void @-> returning string) 23 24 let libbpf_strerror = 25 foreign "libbpf_strerror" (int @-> ptr char @-> size_t @-> returning int) 26 27 let libbpf_bpf_attach_type_str = 28 foreign "libbpf_bpf_attach_type_str" 29 (Types.Bpf_attach_type.t @-> returning string) 30 31 let libbpf_bpf_link_type_str = 32 foreign "libbpf_bpf_link_type_str" 33 (Types.Bpf_link_type.t @-> returning string) 34 35 let libbpf_bpf_map_type_str = 36 foreign "libbpf_bpf_map_type_str" (Types.Bpf_map_type.t @-> returning string) 37 38 let libbpf_bpf_prog_type_str = 39 foreign "libbpf_bpf_prog_type_str" 40 (Types.Bpf_prog_type.t @-> returning string) 41 42 let libbpf_set_print = 43 foreign "libbpf_set_print" 44 (Types.libbpf_print_fn_t @-> returning Types.libbpf_print_fn_t) 45 46 (* ================================= Open / Load / Close =================================== *) 47 48 (** [bpf_object__open path] creates a bpf_object by opening the BPF 49 ELF object file pointed to by the passed [path] and loading it 50 into memory. 51 52 Returns pointer to the new bpf_object; or NULL is returned on 53 error, error code is stored in errno. *) 54 let bpf_object__open = 55 foreign "bpf_object__open" (string @-> returning (ptr_opt Types.bpf_object)) 56 57 (** [bpf_object__load obj_ptr] loads the BPF object into the 58 kernel. [obj_ptr] must be a valid BPF object instance returned 59 by a successful call to [bpf_object__open]. 60 61 Returns 0, on success; negative error code, otherwise, error code is stored in errno *) 62 let bpf_object__load = 63 foreign "bpf_object__load" (ptr Types.bpf_object @-> returning int) 64 65 (** [bpf_object__find_program_by_name name] returns the BPF program 66 of the given [name], if it exists within the passed BPF object 67 68 Returns the pointer to the BPF program instance, if such program 69 exists within the BPF object; or NULL otherwise. *) 70 let bpf_object__find_program_by_name = 71 foreign "bpf_object__find_program_by_name" 72 (ptr Types.bpf_object @-> string @-> returning (ptr_opt Types.bpf_program)) 73 74 (** [bpf_object__next_program obj_ptr prog_ptr] returns the next 75 program after [prog_ptr] found in the passed BPF object *) 76 let bpf_object__next_program = 77 foreign "bpf_object__next_program" 78 (ptr Types.bpf_object @-> ptr Types.bpf_program 79 @-> returning (ptr Types.bpf_program)) 80 81 (** [bpf_program__pin prog path] pins the BPF program to a file in 82 the BPF FS specified by a [path]. This increments the programs 83 reference count, allowing it to stay loaded after the process 84 which loaded it has exited. 85 86 @param prog BPF program to pin, must already be loaded 87 @param path file path in a BPF file system 88 @return 0, on success; negative error code, otherwise *) 89 let bpf_program__pin = 90 foreign "bpf_program__pin" 91 (ptr Types.bpf_program @-> ptr char @-> returning int) 92 93 (** [bpf_program__unpin prog path] unpins the BPF program from a file in the 94 BPFFS specified by a path. This decrements the programs 95 reference count. The file pinning the BPF program can also be 96 unlinked by a different process in which case this function will 97 return an error. 98 99 @param prog BPF program to unpin 100 @param path file path to the pin in a BPF file system 101 @return 0, on success; negative error code, otherwise *) 102 let bpf_program__unpin = 103 foreign "bpf_program__unpin" 104 (ptr Types.bpf_program @-> ptr char @-> returning int) 105 106 (** [bpf_program__attach prog] is a generic function for 107 attaching a BPF program based on auto-detection of program type, 108 attach type, and extra paremeters, where applicable. 109 110 This is supported for: 111 - kprobe/kretprobe (depends on SEC() definition) 112 - uprobe/uretprobe (depends on SEC() definition) 113 - tracepoint 114 - raw tracepoint 115 - tracing programs (typed raw TP/fentry/fexit/fmod_ret) 116 117 Returns pointer to the newly created BPF link; or NULL is 118 returned on error, error code is stored in errno *) 119 let bpf_program__attach = 120 foreign "bpf_program__attach" 121 (ptr Types.bpf_program @-> returning (ptr_opt Types.bpf_link)) 122 123 let bpf_program__fd = 124 foreign "bpf_program__fd" (ptr Types.bpf_program @-> returning int) 125 126 (** [bpf_link__pin link path] pins the BPF link to a file in the 127 BPF FS specified by a path. This increments the links reference 128 count, allowing it to stay loaded after the process which loaded 129 it has exited. 130 131 @param link BPF link to pin, must already be loaded 132 @param path file path in a BPF file system 133 @return 0, on success; negative error code, otherwise *) 134 let bpf_link__pin = 135 foreign "bpf_link__pin" (ptr Types.bpf_link @-> ptr char @-> returning int) 136 137 (** [bpf_link__unpin link path] unpins the BPF link from a file in 138 the BPFFS specified by a path. This decrements the links 139 reference count. The file pinning the BPF link can also be 140 unlinked by a different process in which case this function will 141 return an error. 142 143 @param prog BPF program to unpin 144 @param path file path to the pin in a BPF file system 145 @return 0, on success; negative error code, otherwise *) 146 let bpf_link__unpin = 147 foreign "bpf_link__unpin" (ptr Types.bpf_link @-> returning int) 148 149 (** [bpf_link__destroy link_ptr] Removes the link to the BPF program. 150 Returns 0 on success or -errno *) 151 let bpf_link__destroy = 152 foreign "bpf_link__destroy" (ptr Types.bpf_link @-> returning int) 153 154 (** [bpf_object__close obj_ptr] closes a BPF object and releases all 155 resources. *) 156 let bpf_object__close = 157 foreign "bpf_object__close" (ptr Types.bpf_object @-> returning void) 158 159 (* ======================================== Maps ======================================== *) 160 (* Not explicitly mentioned in the documentation but keys and values look 161 like they're copied into the internal bpf map structure, so we 162 don't need to be worried about keeping references around. *) 163 164 (** [bpf_object__find_map_by_name obj_ptr name] returns BPF map of the given 165 [name], if it exists within the passed BPF object. 166 167 Returns the pointer to the BPF map instance, if such map exists 168 within the BPF object; or NULL otherwise. *) 169 let bpf_object__find_map_by_name = 170 foreign "bpf_object__find_map_by_name" 171 (ptr Types.bpf_object @-> string @-> returning (ptr_opt Types.bpf_map)) 172 173 (** [bpf_map__fd map_ptr] gets the file descriptor of the passed BPF 174 map 175 176 Returns the file descriptor; or -EINVAL in case of an error *) 177 let bpf_map__fd = foreign "bpf_map__fd" (ptr Types.bpf_map @-> returning int) 178 179 (** [bpf_map__lookup_elem map_ptr key_ptr key_sz val_ptr val_sz 180 flags] allows to lookup BPF map value corresponding to provided 181 key. 182 183 [bpf_map__lookup_elem] is high-level equivalent of 184 [bpf_map_lookup_elem] API with added check for key and value 185 size. 186 187 sizes are in bytes of key and value data. For per-CPU BPF maps 188 value size has to be a product of BPF map value size and number 189 of possible CPUs in the system (could be fetched with 190 libbpf_num_possible_cpus()). Note also that for per-CPU values 191 value size has to be aligned up to closest 8 bytes for alignment 192 reasons, so expected size is: round_up(value_size, 8) 193 194 Returns 0, on success; negative error, otherwise *) 195 let bpf_map__lookup_elem = 196 foreign "bpf_map__lookup_elem" 197 (ptr Types.bpf_map @-> ptr void @-> size_t @-> ptr void @-> size_t 198 @-> uint64_t @-> returning int) 199 200 (** [bpf_map__update_elem map_ptr key_ptr key_sz val_ptr val_sz 201 flags] allows to insert or update value in BPF map that 202 corresponds to provided key. 203 204 [bpf_map__update_elem] is high-level equivalent of 205 [bpf_map_update_elem] API with added check for key and value 206 size. 207 208 Check [bpf_map__lookup_elem] for details on sizes. 209 Returns 0, on success; negative error, otherwise *) 210 let bpf_map__update_elem = 211 foreign "bpf_map__update_elem" 212 (ptr Types.bpf_map @-> ptr void @-> size_t @-> ptr void @-> size_t 213 @-> uint64_t @-> returning int) 214 215 (** [bpf_map__delete_elem map_ptr key_ptr key_sz flags] allows to 216 delete element in BPF map that corresponds to provided key. 217 218 [bpf_map__delete_elem] is high-level equivalent of 219 [bpf_map_delete_elem] API with added check for key size. 220 221 Returns 0, on success; negative error, otherwise *) 222 let bpf_map__delete_elem = 223 foreign "bpf_map__delete_elem" 224 (ptr Types.bpf_map @-> ptr void @-> size_t @-> uint64_t @-> returning int) 225 226 (* ================================== Traffic control ================================== *) 227 228 let bpf_tc_hook_create = 229 foreign "bpf_tc_hook_create" (ptr Types.Bpf_tc.hook @-> returning int) 230 231 let bpf_tc_hook_destroy = 232 foreign "bpf_tc_hook_destroy" (ptr Types.Bpf_tc.hook @-> returning int) 233 234 let bpf_tc_attach = 235 foreign "bpf_tc_attach" 236 (ptr Types.Bpf_tc.hook @-> ptr Types.Bpf_tc.Opts.t @-> returning int) 237 238 let bpf_tc_detach = 239 foreign "bpf_tc_detach" 240 (ptr Types.Bpf_tc.hook @-> ptr Types.Bpf_tc.Opts.t @-> returning int) 241 242 (* ====================================== RingBuf ===================================== *) 243 244 (** [ring_buffer__new map_fd fn ctx_ptr opts] loads the callback 245 function [fn] into the ring buffer map provided by the file 246 descriptor [map_fd]. [ctx_ptr] allows the callback function to 247 access user provided context. 248 249 Returns pointer to the ring_buffer manager instance or NULL 250 otherwise *) 251 let ring_buffer__new = 252 foreign "ring_buffer__new" 253 (int @-> Types.ring_buffer_sample_fn @-> ptr void 254 @-> ptr Types.ring_buffer_opts 255 @-> returning (ptr_opt Types.ring_buffer)) 256 257 (** [ring_buffer__poll ring_buf_ptr timeout] poll for available 258 data and consume records, if any are available. 259 260 Returns number of records consumed (or INT_MAX, whichever is 261 less), or negative number, if any of the registered callbacks 262 returned error. *) 263 let ring_buffer__poll = 264 foreign "ring_buffer__poll" (ptr Types.ring_buffer @-> int @-> returning int) 265 266 (** [ring_buffer__free ring_buf_ptr] Frees resources of the ring 267 buffer manager *) 268 let ring_buffer__free = 269 foreign "ring_buffer__free" (ptr Types.ring_buffer @-> returning void) 270 271 (** [ring_buffer__consume ring_buf_ptr] Consume available ring 272 buffer(s) data without event polling. 273 274 Returns number of records consumed across all registered ring 275 buffers (or INT_MAX, whichever is less), or negative number if 276 any of the callbacks return error. *) 277 let ring_buffer__consume = 278 foreign "ring_buffer__consume" (ptr Types.ring_buffer @-> returning int) 279 280 (** [ring_buffer__epoll_fd ring_buf_ptr] Gets an fd that can be used 281 to sleep until data is available in the ring(s) *) 282 let ring_buffer__epoll_fd = 283 foreign "ring_buffer__epoll_fd" (ptr Types.ring_buffer @-> returning int) 284end