]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bpf-program.c
2 This file is part of systemd.
4 Copyright 2016 Daniel Mack
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
25 #include "alloc-util.h"
26 #include "bpf-program.h"
31 int bpf_program_new(uint32_t prog_type
, BPFProgram
**ret
) {
32 _cleanup_(bpf_program_unrefp
) BPFProgram
*p
= NULL
;
34 p
= new0(BPFProgram
, 1);
38 p
->prog_type
= prog_type
;
46 BPFProgram
*bpf_program_unref(BPFProgram
*p
) {
50 safe_close(p
->kernel_fd
);
51 free(p
->instructions
);
56 int bpf_program_add_instructions(BPFProgram
*p
, const struct bpf_insn
*instructions
, size_t count
) {
60 if (!GREEDY_REALLOC(p
->instructions
, p
->allocated
, p
->n_instructions
+ count
))
63 memcpy(p
->instructions
+ p
->n_instructions
, instructions
, sizeof(struct bpf_insn
) * count
);
64 p
->n_instructions
+= count
;
69 int bpf_program_load_kernel(BPFProgram
*p
, char *log_buf
, size_t log_size
) {
74 if (p
->kernel_fd
>= 0)
77 attr
= (union bpf_attr
) {
78 .prog_type
= p
->prog_type
,
79 .insns
= PTR_TO_UINT64(p
->instructions
),
80 .insn_cnt
= p
->n_instructions
,
81 .license
= PTR_TO_UINT64("GPL"),
82 .log_buf
= PTR_TO_UINT64(log_buf
),
83 .log_level
= !!log_buf
,
87 p
->kernel_fd
= bpf(BPF_PROG_LOAD
, &attr
, sizeof(attr
));
94 int bpf_program_cgroup_attach(BPFProgram
*p
, int type
, const char *path
) {
95 _cleanup_close_
int fd
= -1;
102 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
106 attr
= (union bpf_attr
) {
109 .attach_bpf_fd
= p
->kernel_fd
,
112 if (bpf(BPF_PROG_ATTACH
, &attr
, sizeof(attr
)) < 0)
118 int bpf_program_cgroup_detach(int type
, const char *path
) {
119 _cleanup_close_
int fd
= -1;
124 fd
= open(path
, O_DIRECTORY
|O_RDONLY
|O_CLOEXEC
);
128 attr
= (union bpf_attr
) {
133 if (bpf(BPF_PROG_DETACH
, &attr
, sizeof(attr
)) < 0)
139 int bpf_map_new(enum bpf_map_type type
, size_t key_size
, size_t value_size
, size_t max_entries
, uint32_t flags
) {
140 union bpf_attr attr
= {
142 .key_size
= key_size
,
143 .value_size
= value_size
,
144 .max_entries
= max_entries
,
149 fd
= bpf(BPF_MAP_CREATE
, &attr
, sizeof(attr
));
156 int bpf_map_update_element(int fd
, const void *key
, void *value
) {
158 union bpf_attr attr
= {
160 .key
= PTR_TO_UINT64(key
),
161 .value
= PTR_TO_UINT64(value
),
164 if (bpf(BPF_MAP_UPDATE_ELEM
, &attr
, sizeof(attr
)) < 0)
170 int bpf_map_lookup_element(int fd
, const void *key
, void *value
) {
172 union bpf_attr attr
= {
174 .key
= PTR_TO_UINT64(key
),
175 .value
= PTR_TO_UINT64(value
),
178 if (bpf(BPF_MAP_LOOKUP_ELEM
, &attr
, sizeof(attr
)) < 0)