Source file c_function_description.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
open! Ctypes

(* The C_types_generated module is generated by the build
   system to grab the type definitions from the header files in C to
   ensure that offsets and structs are aligned. *)
module Types = C_type_description.Types (C_types_generated)

module Functions (F : Ctypes.FOREIGN) = struct
  open F

  (* ======================================== Generics ======================================== *)
  let libbpf_major_version =
    foreign "libbpf_major_version" (void @-> returning uint32_t)

  let libbpf_minor_version =
    foreign "libbpf_minor_version" (void @-> returning uint32_t)

  let libbpf_version_string =
    foreign "libbpf_version_string" (void @-> returning string)

  let libbpf_strerror =
    foreign "libbpf_strerror" (int @-> ptr char @-> size_t @-> returning int)

  let libbpf_bpf_attach_type_str =
    foreign "libbpf_bpf_attach_type_str"
      (Types.Bpf_attach_type.t @-> returning string)

  let libbpf_bpf_link_type_str =
    foreign "libbpf_bpf_link_type_str"
      (Types.Bpf_link_type.t @-> returning string)

  let libbpf_bpf_map_type_str =
    foreign "libbpf_bpf_map_type_str" (Types.Bpf_map_type.t @-> returning string)

  let libbpf_bpf_prog_type_str =
    foreign "libbpf_bpf_prog_type_str"
      (Types.Bpf_prog_type.t @-> returning string)

  let libbpf_set_print =
    foreign "libbpf_set_print"
      (Types.libbpf_print_fn_t @-> returning Types.libbpf_print_fn_t)

  (* ================================= Open / Load / Close  =================================== *)

  (** [bpf_object__open path] creates a bpf_object by opening the BPF
      ELF object file pointed to by the passed [path] and loading it
      into memory.

      Returns pointer to the new bpf_object; or NULL is returned on
      error, error code is stored in errno. *)
  let bpf_object__open =
    foreign "bpf_object__open" (string @-> returning (ptr_opt Types.bpf_object))

  (** [bpf_object__load obj_ptr] loads the BPF object into the
      kernel. [obj_ptr] must be a valid BPF object instance returned
      by a successful call to [bpf_object__open].

      Returns 0, on success; negative error code, otherwise, error code is stored in errno *)
  let bpf_object__load =
    foreign "bpf_object__load" (ptr Types.bpf_object @-> returning int)

  (** [bpf_object__find_program_by_name name] returns the BPF program
      of the given [name], if it exists within the passed BPF object

      Returns the pointer to the BPF program instance, if such program
      exists within the BPF object; or NULL otherwise.  *)
  let bpf_object__find_program_by_name =
    foreign "bpf_object__find_program_by_name"
      (ptr Types.bpf_object @-> string @-> returning (ptr_opt Types.bpf_program))

  (** [bpf_object__next_program obj_ptr prog_ptr] returns the next
      program after [prog_ptr] found in the passed BPF object *)
  let bpf_object__next_program =
    foreign "bpf_object__next_program"
      (ptr Types.bpf_object @-> ptr Types.bpf_program
      @-> returning (ptr Types.bpf_program))

  (** [bpf_program__pin prog path] pins the BPF program to a file in
      the BPF FS specified by a [path]. This increments the programs
      reference count, allowing it to stay loaded after the process
      which loaded it has exited.

      @param prog BPF program to pin, must already be loaded
      @param path file path in a BPF file system
      @return 0, on success; negative error code, otherwise *)
  let bpf_program__pin =
    foreign "bpf_program__pin"
      (ptr Types.bpf_program @-> ptr char @-> returning int)

  (** [bpf_program__unpin prog path] unpins the BPF program from a file in the
      BPFFS specified by a path. This decrements the programs
      reference count. The file pinning the BPF program can also be
      unlinked by a different process in which case this function will
      return an error.

      @param prog BPF program to unpin
      @param path file path to the pin in a BPF file system
      @return 0, on success; negative error code, otherwise  *)
  let bpf_program__unpin =
    foreign "bpf_program__unpin"
      (ptr Types.bpf_program @-> ptr char @-> returning int)

  (** [bpf_program__attach prog] is a generic function for
      attaching a BPF program based on auto-detection of program type,
      attach type, and extra paremeters, where applicable.

      This is supported for:
      - kprobe/kretprobe (depends on SEC() definition)
      - uprobe/uretprobe (depends on SEC() definition)
      - tracepoint
      - raw tracepoint
      - tracing programs (typed raw TP/fentry/fexit/fmod_ret)

      Returns pointer to the newly created BPF link; or NULL is
      returned on error, error code is stored in errno *)
  let bpf_program__attach =
    foreign "bpf_program__attach"
      (ptr Types.bpf_program @-> returning (ptr_opt Types.bpf_link))

  let bpf_program__fd =
    foreign "bpf_program__fd" (ptr Types.bpf_program @-> returning int)

  (** [bpf_link__pin link path] pins the BPF link to a file in the
      BPF FS specified by a path. This increments the links reference
      count, allowing it to stay loaded after the process which loaded
      it has exited.

      @param link BPF link to pin, must already be loaded
      @param path file path in a BPF file system
      @return 0, on success; negative error code, otherwise *)
  let bpf_link__pin =
    foreign "bpf_link__pin" (ptr Types.bpf_link @-> ptr char @-> returning int)

  (** [bpf_link__unpin link path] unpins the BPF link from a file in
      the BPFFS specified by a path. This decrements the links
      reference count. The file pinning the BPF link can also be
      unlinked by a different process in which case this function will
      return an error.

      @param prog BPF program to unpin
      @param path file path to the pin in a BPF file system
      @return 0, on success; negative error code, otherwise *)
  let bpf_link__unpin =
    foreign "bpf_link__unpin" (ptr Types.bpf_link @-> returning int)

  (** [bpf_link__destroy link_ptr] Removes the link to the BPF program.
      Returns 0 on success or -errno *)
  let bpf_link__destroy =
    foreign "bpf_link__destroy" (ptr Types.bpf_link @-> returning int)

  (** [bpf_object__close obj_ptr] closes a BPF object and releases all
      resources. *)
  let bpf_object__close =
    foreign "bpf_object__close" (ptr Types.bpf_object @-> returning void)

  (* ======================================== Maps ======================================== *)
  (* Not explicitly mentioned in the documentation but keys and values look
     like they're copied into the internal bpf map structure, so we
     don't need to be worried about keeping references around. *)

  (** [bpf_object__find_map_by_name obj_ptr name] returns BPF map of the given
      [name], if it exists within the passed BPF object.

      Returns the pointer to the BPF map instance, if such map exists
      within the BPF object; or NULL otherwise.  *)
  let bpf_object__find_map_by_name =
    foreign "bpf_object__find_map_by_name"
      (ptr Types.bpf_object @-> string @-> returning (ptr_opt Types.bpf_map))

  (** [bpf_map__fd map_ptr] gets the file descriptor of the passed BPF
      map

      Returns the file descriptor; or -EINVAL in case of an error  *)
  let bpf_map__fd = foreign "bpf_map__fd" (ptr Types.bpf_map @-> returning int)

  (** [bpf_map__lookup_elem map_ptr key_ptr key_sz val_ptr val_sz
      flags] allows to lookup BPF map value corresponding to provided
      key.

     [bpf_map__lookup_elem] is high-level equivalent of
     [bpf_map_lookup_elem] API with added check for key and value
      size.

      sizes are in bytes of key and value data. For per-CPU BPF maps
      value size has to be a product of BPF map value size and number
      of possible CPUs in the system (could be fetched with
      libbpf_num_possible_cpus()). Note also that for per-CPU values
      value size has to be aligned up to closest 8 bytes for alignment
      reasons, so expected size is: round_up(value_size, 8)

      Returns 0, on success; negative error, otherwise *)
  let bpf_map__lookup_elem =
    foreign "bpf_map__lookup_elem"
      (ptr Types.bpf_map @-> ptr void @-> size_t @-> ptr void @-> size_t
     @-> uint64_t @-> returning int)

  (** [bpf_map__update_elem map_ptr key_ptr key_sz val_ptr val_sz
      flags] allows to insert or update value in BPF map that
      corresponds to provided key.

      [bpf_map__update_elem] is high-level equivalent of
      [bpf_map_update_elem] API with added check for key and value
      size.

      Check [bpf_map__lookup_elem] for details on sizes.
      Returns 0, on success; negative error, otherwise *)
  let bpf_map__update_elem =
    foreign "bpf_map__update_elem"
      (ptr Types.bpf_map @-> ptr void @-> size_t @-> ptr void @-> size_t
     @-> uint64_t @-> returning int)

  (** [bpf_map__delete_elem map_ptr key_ptr key_sz flags] allows to
      delete element in BPF map that corresponds to provided key.

      [bpf_map__delete_elem] is high-level equivalent of
      [bpf_map_delete_elem] API with added check for key size.

      Returns 0, on success; negative error, otherwise *)
  let bpf_map__delete_elem =
    foreign "bpf_map__delete_elem"
      (ptr Types.bpf_map @-> ptr void @-> size_t @-> uint64_t @-> returning int)

  (* ================================== Traffic control ================================== *)

  let bpf_tc_hook_create =
    foreign "bpf_tc_hook_create" (ptr Types.Bpf_tc.hook @-> returning int)

  let bpf_tc_hook_destroy =
    foreign "bpf_tc_hook_destroy" (ptr Types.Bpf_tc.hook @-> returning int)

  let bpf_tc_attach =
    foreign "bpf_tc_attach"
      (ptr Types.Bpf_tc.hook @-> ptr Types.Bpf_tc.Opts.t @-> returning int)

  let bpf_tc_detach =
    foreign "bpf_tc_detach"
      (ptr Types.Bpf_tc.hook @-> ptr Types.Bpf_tc.Opts.t @-> returning int)

  (* ====================================== RingBuf ===================================== *)

  (** [ring_buffer__new map_fd fn ctx_ptr opts] loads the callback
      function [fn] into the ring buffer map provided by the file
      descriptor [map_fd]. [ctx_ptr] allows the callback function to
      access user provided context.

      Returns pointer to the ring_buffer manager instance or NULL
      otherwise *)
  let ring_buffer__new =
    foreign "ring_buffer__new"
      (int @-> Types.ring_buffer_sample_fn @-> ptr void
     @-> ptr Types.ring_buffer_opts
      @-> returning (ptr_opt Types.ring_buffer))

  (** [ring_buffer__poll ring_buf_ptr timeout] poll for available
      data and consume records, if any are available.

      Returns number of records consumed (or INT_MAX, whichever is
      less), or negative number, if any of the registered callbacks
      returned error. *)
  let ring_buffer__poll =
    foreign "ring_buffer__poll" (ptr Types.ring_buffer @-> int @-> returning int)

  (** [ring_buffer__free ring_buf_ptr] Frees resources of the ring
      buffer manager *)
  let ring_buffer__free =
    foreign "ring_buffer__free" (ptr Types.ring_buffer @-> returning void)

  (** [ring_buffer__consume ring_buf_ptr] Consume available ring
      buffer(s) data without event polling.

      Returns number of records consumed across all registered ring
      buffers (or INT_MAX, whichever is less), or negative number if
      any of the callbacks return error.  *)
  let ring_buffer__consume =
    foreign "ring_buffer__consume" (ptr Types.ring_buffer @-> returning int)

  (** [ring_buffer__epoll_fd ring_buf_ptr] Gets an fd that can be used
      to sleep until data is available in the ring(s) *)
  let ring_buffer__epoll_fd =
    foreign "ring_buffer__epoll_fd" (ptr Types.ring_buffer @-> returning int)
end