| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "shared-forward.h" |
| 5 |
|
| 6 |
/* This encapsulates three different concepts: the loaded BPF program, the BPF code, and the attachment to a |
| 7 |
* cgroup. Typically our BPF programs go through all three stages: we build the code, we load it, and finally |
| 8 |
* we attach it, but it might happen that we operate with programs that aren't loaded or aren't attached, or |
| 9 |
* where we don't have the code. */ |
| 10 |
typedef struct BPFProgram { |
| 11 |
/* The loaded BPF program, if loaded */ |
| 12 |
int kernel_fd; |
| 13 |
uint32_t prog_type; |
| 14 |
char *prog_name; |
| 15 |
|
| 16 |
/* The code of it BPF program, if known */ |
| 17 |
size_t n_instructions; |
| 18 |
struct bpf_insn *instructions; |
| 19 |
|
| 20 |
/* The cgroup path the program is attached to, if it is attached. If non-NULL bpf_program_unref() |
| 21 |
* will detach on destruction. */ |
| 22 |
char *attached_path; |
| 23 |
int attached_type; |
| 24 |
uint32_t attached_flags; |
| 25 |
} BPFProgram; |
| 26 |
|
| 27 |
int bpf_program_supported(void); |
| 28 |
|
| 29 |
int bpf_program_new(uint32_t prog_type, const char *prog_name, BPFProgram **ret); |
| 30 |
int bpf_program_new_from_bpffs_path(const char *path, BPFProgram **ret); |
| 31 |
BPFProgram *bpf_program_free(BPFProgram *p); |
| 32 |
|
| 33 |
int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructions, size_t count); |
| 34 |
int bpf_program_load_kernel(BPFProgram *p, char *log_buf, size_t log_size); |
| 35 |
int bpf_program_load_from_bpf_fs(BPFProgram *p, const char *path); |
| 36 |
|
| 37 |
int bpf_program_cgroup_attach(BPFProgram *p, int type, const char *path, uint32_t flags); |
| 38 |
int bpf_program_cgroup_detach(BPFProgram *p); |
| 39 |
|
| 40 |
int bpf_program_pin(int prog_fd, const char *bpffs_path); |
| 41 |
int bpf_program_get_id_by_fd(int prog_fd, uint32_t *ret_id); |
| 42 |
|
| 43 |
int bpf_program_serialize_attachment(FILE *f, FDSet *fds, const char *key, BPFProgram *p); |
| 44 |
int bpf_program_serialize_attachment_set(FILE *f, FDSet *fds, const char *key, Set *set); |
| 45 |
int bpf_program_deserialize_attachment(const char *v, FDSet *fds, BPFProgram **bpfp); |
| 46 |
int bpf_program_deserialize_attachment_set(const char *v, FDSet *fds, Set **bpfsetp); |
| 47 |
|
| 48 |
extern const struct hash_ops bpf_program_hash_ops; |
| 49 |
|
| 50 |
int bpf_map_new(const char *name, enum bpf_map_type type, size_t key_size, size_t value_size, |
| 51 |
size_t max_entries, uint32_t flags); |
| 52 |
int bpf_map_update_element(int fd, const void *key, void *value); |
| 53 |
int bpf_map_lookup_element(int fd, const void *key, void *value); |
| 54 |
|
| 55 |
DECLARE_STRING_TABLE_LOOKUP(bpf_cgroup_attach_type, int); |
| 56 |
|
| 57 |
DEFINE_TRIVIAL_CLEANUP_FUNC(BPFProgram*, bpf_program_free); |