Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
0002 
0003 /*
0004  * common eBPF ELF operations.
0005  *
0006  * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org>
0007  * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com>
0008  * Copyright (C) 2015 Huawei Inc.
0009  *
0010  * This program is free software; you can redistribute it and/or
0011  * modify it under the terms of the GNU Lesser General Public
0012  * License as published by the Free Software Foundation;
0013  * version 2.1 of the License (not later!)
0014  *
0015  * This program is distributed in the hope that it will be useful,
0016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  * GNU Lesser General Public License for more details.
0019  *
0020  * You should have received a copy of the GNU Lesser General Public
0021  * License along with this program; if not,  see <http://www.gnu.org/licenses>
0022  */
0023 #ifndef __LIBBPF_BPF_H
0024 #define __LIBBPF_BPF_H
0025 
0026 #include <linux/bpf.h>
0027 #include <stdbool.h>
0028 #include <stddef.h>
0029 #include <stdint.h>
0030 
0031 #include "libbpf_common.h"
0032 #include "libbpf_legacy.h"
0033 
0034 #ifdef __cplusplus
0035 extern "C" {
0036 #endif
0037 
0038 int libbpf_set_memlock_rlim(size_t memlock_bytes);
0039 
0040 struct bpf_map_create_opts {
0041     size_t sz; /* size of this struct for forward/backward compatibility */
0042 
0043     __u32 btf_fd;
0044     __u32 btf_key_type_id;
0045     __u32 btf_value_type_id;
0046     __u32 btf_vmlinux_value_type_id;
0047 
0048     __u32 inner_map_fd;
0049     __u32 map_flags;
0050     __u64 map_extra;
0051 
0052     __u32 numa_node;
0053     __u32 map_ifindex;
0054 };
0055 #define bpf_map_create_opts__last_field map_ifindex
0056 
0057 LIBBPF_API int bpf_map_create(enum bpf_map_type map_type,
0058                   const char *map_name,
0059                   __u32 key_size,
0060                   __u32 value_size,
0061                   __u32 max_entries,
0062                   const struct bpf_map_create_opts *opts);
0063 
0064 struct bpf_prog_load_opts {
0065     size_t sz; /* size of this struct for forward/backward compatibility */
0066 
0067     /* libbpf can retry BPF_PROG_LOAD command if bpf() syscall returns
0068      * -EAGAIN. This field determines how many attempts libbpf has to
0069      *  make. If not specified, libbpf will use default value of 5.
0070      */
0071     int attempts;
0072 
0073     enum bpf_attach_type expected_attach_type;
0074     __u32 prog_btf_fd;
0075     __u32 prog_flags;
0076     __u32 prog_ifindex;
0077     __u32 kern_version;
0078 
0079     __u32 attach_btf_id;
0080     __u32 attach_prog_fd;
0081     __u32 attach_btf_obj_fd;
0082 
0083     const int *fd_array;
0084 
0085     /* .BTF.ext func info data */
0086     const void *func_info;
0087     __u32 func_info_cnt;
0088     __u32 func_info_rec_size;
0089 
0090     /* .BTF.ext line info data */
0091     const void *line_info;
0092     __u32 line_info_cnt;
0093     __u32 line_info_rec_size;
0094 
0095     /* verifier log options */
0096     __u32 log_level;
0097     __u32 log_size;
0098     char *log_buf;
0099 };
0100 #define bpf_prog_load_opts__last_field log_buf
0101 
0102 LIBBPF_API int bpf_prog_load(enum bpf_prog_type prog_type,
0103                  const char *prog_name, const char *license,
0104                  const struct bpf_insn *insns, size_t insn_cnt,
0105                  const struct bpf_prog_load_opts *opts);
0106 
0107 /* Flags to direct loading requirements */
0108 #define MAPS_RELAX_COMPAT   0x01
0109 
0110 /* Recommended log buffer size */
0111 #define BPF_LOG_BUF_SIZE (UINT32_MAX >> 8) /* verifier maximum in kernels <= 5.1 */
0112 
0113 struct bpf_btf_load_opts {
0114     size_t sz; /* size of this struct for forward/backward compatibility */
0115 
0116     /* kernel log options */
0117     char *log_buf;
0118     __u32 log_level;
0119     __u32 log_size;
0120 };
0121 #define bpf_btf_load_opts__last_field log_size
0122 
0123 LIBBPF_API int bpf_btf_load(const void *btf_data, size_t btf_size,
0124                 const struct bpf_btf_load_opts *opts);
0125 
0126 LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value,
0127                    __u64 flags);
0128 
0129 LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value);
0130 LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value,
0131                      __u64 flags);
0132 LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,
0133                           void *value);
0134 LIBBPF_API int bpf_map_lookup_and_delete_elem_flags(int fd, const void *key,
0135                             void *value, __u64 flags);
0136 LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
0137 LIBBPF_API int bpf_map_delete_elem_flags(int fd, const void *key, __u64 flags);
0138 LIBBPF_API int bpf_map_get_next_key(int fd, const void *key, void *next_key);
0139 LIBBPF_API int bpf_map_freeze(int fd);
0140 
0141 struct bpf_map_batch_opts {
0142     size_t sz; /* size of this struct for forward/backward compatibility */
0143     __u64 elem_flags;
0144     __u64 flags;
0145 };
0146 #define bpf_map_batch_opts__last_field flags
0147 
0148 
0149 /**
0150  * @brief **bpf_map_delete_batch()** allows for batch deletion of multiple
0151  * elements in a BPF map.
0152  *
0153  * @param fd BPF map file descriptor
0154  * @param keys pointer to an array of *count* keys
0155  * @param count input and output parameter; on input **count** represents the
0156  * number of  elements in the map to delete in batch;
0157  * on output if a non-EFAULT error is returned, **count** represents the number of deleted
0158  * elements if the output **count** value is not equal to the input **count** value
0159  * If EFAULT is returned, **count** should not be trusted to be correct.
0160  * @param opts options for configuring the way the batch deletion works
0161  * @return 0, on success; negative error code, otherwise (errno is also set to
0162  * the error code)
0163  */
0164 LIBBPF_API int bpf_map_delete_batch(int fd, const void *keys,
0165                     __u32 *count,
0166                     const struct bpf_map_batch_opts *opts);
0167 
0168 /**
0169  * @brief **bpf_map_lookup_batch()** allows for batch lookup of BPF map elements.
0170  *
0171  * The parameter *in_batch* is the address of the first element in the batch to read.
0172  * *out_batch* is an output parameter that should be passed as *in_batch* to subsequent
0173  * calls to **bpf_map_lookup_batch()**. NULL can be passed for *in_batch* to indicate
0174  * that the batched lookup starts from the beginning of the map.
0175  *
0176  * The *keys* and *values* are output parameters which must point to memory large enough to
0177  * hold *count* items based on the key and value size of the map *map_fd*. The *keys*
0178  * buffer must be of *key_size* * *count*. The *values* buffer must be of
0179  * *value_size* * *count*.
0180  *
0181  * @param fd BPF map file descriptor
0182  * @param in_batch address of the first element in batch to read, can pass NULL to
0183  * indicate that the batched lookup starts from the beginning of the map.
0184  * @param out_batch output parameter that should be passed to next call as *in_batch*
0185  * @param keys pointer to an array large enough for *count* keys
0186  * @param values pointer to an array large enough for *count* values
0187  * @param count input and output parameter; on input it's the number of elements
0188  * in the map to read in batch; on output it's the number of elements that were
0189  * successfully read.
0190  * If a non-EFAULT error is returned, count will be set as the number of elements
0191  * that were read before the error occurred.
0192  * If EFAULT is returned, **count** should not be trusted to be correct.
0193  * @param opts options for configuring the way the batch lookup works
0194  * @return 0, on success; negative error code, otherwise (errno is also set to
0195  * the error code)
0196  */
0197 LIBBPF_API int bpf_map_lookup_batch(int fd, void *in_batch, void *out_batch,
0198                     void *keys, void *values, __u32 *count,
0199                     const struct bpf_map_batch_opts *opts);
0200 
0201 /**
0202  * @brief **bpf_map_lookup_and_delete_batch()** allows for batch lookup and deletion
0203  * of BPF map elements where each element is deleted after being retrieved.
0204  *
0205  * @param fd BPF map file descriptor
0206  * @param in_batch address of the first element in batch to read, can pass NULL to
0207  * get address of the first element in *out_batch*
0208  * @param out_batch output parameter that should be passed to next call as *in_batch*
0209  * @param keys pointer to an array of *count* keys
0210  * @param values pointer to an array large enough for *count* values
0211  * @param count input and output parameter; on input it's the number of elements
0212  * in the map to read and delete in batch; on output it represents the number of
0213  * elements that were successfully read and deleted
0214  * If a non-**EFAULT** error code is returned and if the output **count** value
0215  * is not equal to the input **count** value, up to **count** elements may
0216  * have been deleted.
0217  * if **EFAULT** is returned up to *count* elements may have been deleted without
0218  * being returned via the *keys* and *values* output parameters.
0219  * @param opts options for configuring the way the batch lookup and delete works
0220  * @return 0, on success; negative error code, otherwise (errno is also set to
0221  * the error code)
0222  */
0223 LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
0224                     void *out_batch, void *keys,
0225                     void *values, __u32 *count,
0226                     const struct bpf_map_batch_opts *opts);
0227 
0228 /**
0229  * @brief **bpf_map_update_batch()** updates multiple elements in a map
0230  * by specifying keys and their corresponding values.
0231  *
0232  * The *keys* and *values* parameters must point to memory large enough
0233  * to hold *count* items based on the key and value size of the map.
0234  *
0235  * The *opts* parameter can be used to control how *bpf_map_update_batch()*
0236  * should handle keys that either do or do not already exist in the map.
0237  * In particular the *flags* parameter of *bpf_map_batch_opts* can be
0238  * one of the following:
0239  *
0240  * Note that *count* is an input and output parameter, where on output it
0241  * represents how many elements were successfully updated. Also note that if
0242  * **EFAULT** then *count* should not be trusted to be correct.
0243  *
0244  * **BPF_ANY**
0245  *    Create new elements or update existing.
0246  *
0247  * **BPF_NOEXIST**
0248  *    Create new elements only if they do not exist.
0249  *
0250  * **BPF_EXIST**
0251  *    Update existing elements.
0252  *
0253  * **BPF_F_LOCK**
0254  *    Update spin_lock-ed map elements. This must be
0255  *    specified if the map value contains a spinlock.
0256  *
0257  * @param fd BPF map file descriptor
0258  * @param keys pointer to an array of *count* keys
0259  * @param values pointer to an array of *count* values
0260  * @param count input and output parameter; on input it's the number of elements
0261  * in the map to update in batch; on output if a non-EFAULT error is returned,
0262  * **count** represents the number of updated elements if the output **count**
0263  * value is not equal to the input **count** value.
0264  * If EFAULT is returned, **count** should not be trusted to be correct.
0265  * @param opts options for configuring the way the batch update works
0266  * @return 0, on success; negative error code, otherwise (errno is also set to
0267  * the error code)
0268  */
0269 LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values,
0270                     __u32 *count,
0271                     const struct bpf_map_batch_opts *opts);
0272 
0273 struct bpf_obj_get_opts {
0274     size_t sz; /* size of this struct for forward/backward compatibility */
0275 
0276     __u32 file_flags;
0277 
0278     size_t :0;
0279 };
0280 #define bpf_obj_get_opts__last_field file_flags
0281 
0282 LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
0283 LIBBPF_API int bpf_obj_get(const char *pathname);
0284 LIBBPF_API int bpf_obj_get_opts(const char *pathname,
0285                 const struct bpf_obj_get_opts *opts);
0286 
0287 struct bpf_prog_attach_opts {
0288     size_t sz; /* size of this struct for forward/backward compatibility */
0289     unsigned int flags;
0290     int replace_prog_fd;
0291 };
0292 #define bpf_prog_attach_opts__last_field replace_prog_fd
0293 
0294 LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
0295                    enum bpf_attach_type type, unsigned int flags);
0296 LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
0297                      enum bpf_attach_type type,
0298                      const struct bpf_prog_attach_opts *opts);
0299 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
0300 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
0301                 enum bpf_attach_type type);
0302 
0303 union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
0304 struct bpf_link_create_opts {
0305     size_t sz; /* size of this struct for forward/backward compatibility */
0306     __u32 flags;
0307     union bpf_iter_link_info *iter_info;
0308     __u32 iter_info_len;
0309     __u32 target_btf_id;
0310     union {
0311         struct {
0312             __u64 bpf_cookie;
0313         } perf_event;
0314         struct {
0315             __u32 flags;
0316             __u32 cnt;
0317             const char **syms;
0318             const unsigned long *addrs;
0319             const __u64 *cookies;
0320         } kprobe_multi;
0321         struct {
0322             __u64 cookie;
0323         } tracing;
0324     };
0325     size_t :0;
0326 };
0327 #define bpf_link_create_opts__last_field kprobe_multi.cookies
0328 
0329 LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
0330                    enum bpf_attach_type attach_type,
0331                    const struct bpf_link_create_opts *opts);
0332 
0333 LIBBPF_API int bpf_link_detach(int link_fd);
0334 
0335 struct bpf_link_update_opts {
0336     size_t sz; /* size of this struct for forward/backward compatibility */
0337     __u32 flags;       /* extra flags */
0338     __u32 old_prog_fd; /* expected old program FD */
0339 };
0340 #define bpf_link_update_opts__last_field old_prog_fd
0341 
0342 LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
0343                    const struct bpf_link_update_opts *opts);
0344 
0345 LIBBPF_API int bpf_iter_create(int link_fd);
0346 
0347 struct bpf_prog_test_run_attr {
0348     int prog_fd;
0349     int repeat;
0350     const void *data_in;
0351     __u32 data_size_in;
0352     void *data_out;      /* optional */
0353     __u32 data_size_out; /* in: max length of data_out
0354                   * out: length of data_out */
0355     __u32 retval;        /* out: return code of the BPF program */
0356     __u32 duration;      /* out: average per repetition in ns */
0357     const void *ctx_in; /* optional */
0358     __u32 ctx_size_in;
0359     void *ctx_out;      /* optional */
0360     __u32 ctx_size_out; /* in: max length of ctx_out
0361                  * out: length of cxt_out */
0362 };
0363 
0364 LIBBPF_API int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id);
0365 LIBBPF_API int bpf_map_get_next_id(__u32 start_id, __u32 *next_id);
0366 LIBBPF_API int bpf_btf_get_next_id(__u32 start_id, __u32 *next_id);
0367 LIBBPF_API int bpf_link_get_next_id(__u32 start_id, __u32 *next_id);
0368 LIBBPF_API int bpf_prog_get_fd_by_id(__u32 id);
0369 LIBBPF_API int bpf_map_get_fd_by_id(__u32 id);
0370 LIBBPF_API int bpf_btf_get_fd_by_id(__u32 id);
0371 LIBBPF_API int bpf_link_get_fd_by_id(__u32 id);
0372 LIBBPF_API int bpf_obj_get_info_by_fd(int bpf_fd, void *info, __u32 *info_len);
0373 
0374 struct bpf_prog_query_opts {
0375     size_t sz; /* size of this struct for forward/backward compatibility */
0376     __u32 query_flags;
0377     __u32 attach_flags; /* output argument */
0378     __u32 *prog_ids;
0379     __u32 prog_cnt; /* input+output argument */
0380     __u32 *prog_attach_flags;
0381 };
0382 #define bpf_prog_query_opts__last_field prog_attach_flags
0383 
0384 LIBBPF_API int bpf_prog_query_opts(int target_fd,
0385                    enum bpf_attach_type type,
0386                    struct bpf_prog_query_opts *opts);
0387 LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
0388                   __u32 query_flags, __u32 *attach_flags,
0389                   __u32 *prog_ids, __u32 *prog_cnt);
0390 
0391 LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd);
0392 LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf,
0393                  __u32 *buf_len, __u32 *prog_id, __u32 *fd_type,
0394                  __u64 *probe_offset, __u64 *probe_addr);
0395 
0396 enum bpf_stats_type; /* defined in up-to-date linux/bpf.h */
0397 LIBBPF_API int bpf_enable_stats(enum bpf_stats_type type);
0398 
0399 struct bpf_prog_bind_opts {
0400     size_t sz; /* size of this struct for forward/backward compatibility */
0401     __u32 flags;
0402 };
0403 #define bpf_prog_bind_opts__last_field flags
0404 
0405 LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
0406                  const struct bpf_prog_bind_opts *opts);
0407 
0408 struct bpf_test_run_opts {
0409     size_t sz; /* size of this struct for forward/backward compatibility */
0410     const void *data_in; /* optional */
0411     void *data_out;      /* optional */
0412     __u32 data_size_in;
0413     __u32 data_size_out; /* in: max length of data_out
0414                   * out: length of data_out
0415                   */
0416     const void *ctx_in; /* optional */
0417     void *ctx_out;      /* optional */
0418     __u32 ctx_size_in;
0419     __u32 ctx_size_out; /* in: max length of ctx_out
0420                  * out: length of cxt_out
0421                  */
0422     __u32 retval;        /* out: return code of the BPF program */
0423     int repeat;
0424     __u32 duration;      /* out: average per repetition in ns */
0425     __u32 flags;
0426     __u32 cpu;
0427     __u32 batch_size;
0428 };
0429 #define bpf_test_run_opts__last_field batch_size
0430 
0431 LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
0432                       struct bpf_test_run_opts *opts);
0433 
0434 #ifdef __cplusplus
0435 } /* extern "C" */
0436 #endif
0437 
0438 #endif /* __LIBBPF_BPF_H */