2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2017 Quentin Monnet <quentin.monnet@6wind.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifdef HAVE_LINUX_BPF_H
32 # include <linux/bpf.h>
35 #include "xlat/bpf_commands.h"
36 #include "xlat/bpf_map_types.h"
37 #include "xlat/bpf_prog_types.h"
38 #include "xlat/bpf_map_update_elem_flags.h"
39 #include "xlat/bpf_attach_type.h"
40 #include "xlat/bpf_attach_flags.h"
43 bpf_map_create(struct tcb *const tcp, const kernel_ulong_t addr,
47 uint32_t map_type, key_size, value_size, max_entries;
52 return RVAL_DECODED | RVAL_FD;
54 if (size > sizeof(attr))
56 if (umoven_or_printaddr(tcp, addr, size, &attr))
57 return RVAL_DECODED | RVAL_FD;
59 tprints("{map_type=");
60 printxval(bpf_map_types, attr.map_type, "BPF_MAP_TYPE_???");
61 tprintf(", key_size=%u, value_size=%u, max_entries=%u}",
62 attr.key_size, attr.value_size, attr.max_entries);
64 return RVAL_DECODED | RVAL_FD;
68 bpf_map_update_elem(struct tcb *const tcp, const kernel_ulong_t addr,
73 uint64_t ATTRIBUTE_ALIGNED(8) key;
74 uint64_t ATTRIBUTE_ALIGNED(8) value;
82 if (size > sizeof(attr))
84 if (umoven_or_printaddr(tcp, addr, size, &attr))
88 printfd(tcp, attr.map_fd);
89 tprintf(", key=%#" PRIx64 ", value=%#" PRIx64 ", flags=",
90 attr.key, attr.value);
91 printxval64(bpf_map_update_elem_flags, attr.flags, "BPF_???");
96 bpf_map_delete_elem(struct tcb *const tcp, const kernel_ulong_t addr,
101 uint64_t ATTRIBUTE_ALIGNED(8) key;
108 if (size > sizeof(attr))
110 if (umoven_or_printaddr(tcp, addr, size, &attr))
114 printfd(tcp, attr.map_fd);
115 tprintf(", key=%#" PRIx64 "}", attr.key);
119 bpf_map_io(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int size,
120 const char *const text)
122 struct bpf_io_elem_struct {
124 uint64_t ATTRIBUTE_ALIGNED(8) key;
125 uint64_t ATTRIBUTE_ALIGNED(8) value;
129 if (!syserror(tcp) && !umove_or_printaddr(tcp, addr, &attr))
130 tprintf(", %s=%#" PRIx64, text, attr.value);
139 if (size > sizeof(attr))
141 if (umoven_or_printaddr(tcp, addr, size, &attr))
145 printfd(tcp, attr.map_fd);
146 tprintf(", key=%#" PRIx64, attr.key);
152 bpf_prog_load(struct tcb *const tcp, const kernel_ulong_t addr,
156 uint32_t prog_type, insn_cnt;
157 uint64_t ATTRIBUTE_ALIGNED(8) insns, license;
158 uint32_t log_level, log_size;
159 uint64_t ATTRIBUTE_ALIGNED(8) log_buf;
160 uint32_t kern_version;
165 return RVAL_DECODED | RVAL_FD;
167 if (size > sizeof(attr))
169 if (umoven_or_printaddr(tcp, addr, size, &attr))
170 return RVAL_DECODED | RVAL_FD;
172 tprints("{prog_type=");
173 printxval(bpf_prog_types, attr.prog_type, "BPF_PROG_TYPE_???");
174 tprintf(", insn_cnt=%u, insns=%#" PRIx64 ", license=",
175 attr.insn_cnt, attr.insns);
176 printstr(tcp, attr.license);
177 tprintf(", log_level=%u, log_size=%u, log_buf=%#" PRIx64 ", kern_version=%u}",
178 attr.log_level, attr.log_size, attr.log_buf, attr.kern_version);
180 return RVAL_DECODED | RVAL_FD;
184 bpf_obj_manage(struct tcb *const tcp, const kernel_ulong_t addr,
188 uint64_t ATTRIBUTE_ALIGNED(8) pathname;
194 return RVAL_DECODED | RVAL_FD;
196 if (size > sizeof(attr))
198 if (umoven_or_printaddr(tcp, addr, size, &attr))
199 return RVAL_DECODED | RVAL_FD;
201 tprints("{pathname=");
202 printpath(tcp, attr.pathname);
203 tprints(", bpf_fd=");
204 printfd(tcp, attr.bpf_fd);
207 return RVAL_DECODED | RVAL_FD;
211 bpf_prog_attach_detach(struct tcb *const tcp, const kernel_ulong_t addr,
212 unsigned int size, bool print_attach)
215 uint32_t target_fd, attach_bpf_fd, attach_type, attach_flags;
222 if (size > sizeof(attr))
224 if (umoven_or_printaddr(tcp, addr, size, &attr))
227 tprints("{target_fd=");
228 printfd(tcp, attr.target_fd);
230 tprints(", attach_bpf_fd=");
231 printfd(tcp, attr.attach_bpf_fd);
233 tprints(", attach_type=");
234 printxval(bpf_attach_type, attr.attach_type, "BPF_???");
236 tprints(", attach_flags=");
237 printflags(bpf_attach_flags, attr.attach_flags, "BPF_F_???");
245 bpf_prog_attach(struct tcb *const tcp, const kernel_ulong_t addr,
248 return bpf_prog_attach_detach(tcp, addr, size, true);
252 bpf_prog_detach(struct tcb *const tcp, const kernel_ulong_t addr,
255 return bpf_prog_attach_detach(tcp, addr, size, false);
260 const unsigned int cmd = tcp->u_arg[0];
261 const kernel_ulong_t addr = tcp->u_arg[1];
262 const unsigned int size = tcp->u_arg[2];
263 int rc = RVAL_DECODED;
266 printxval(bpf_commands, cmd, "BPF_???");
272 rc = bpf_map_create(tcp, addr, size);
274 case BPF_MAP_LOOKUP_ELEM:
275 rc = bpf_map_io(tcp, addr, size, "value");
277 case BPF_MAP_UPDATE_ELEM:
278 bpf_map_update_elem(tcp, addr, size);
280 case BPF_MAP_DELETE_ELEM:
281 bpf_map_delete_elem(tcp, addr, size);
283 case BPF_MAP_GET_NEXT_KEY:
284 rc = bpf_map_io(tcp, addr, size, "next_key");
287 rc = bpf_prog_load(tcp, addr, size);
290 rc = bpf_obj_manage(tcp, addr, size);
293 rc = bpf_obj_manage(tcp, addr, size);
295 case BPF_PROG_ATTACH:
296 rc = bpf_prog_attach(tcp, addr, size);
298 case BPF_PROG_DETACH:
299 rc = bpf_prog_detach(tcp, addr, size);
306 if (rc & RVAL_DECODED)
307 tprintf(", %u", size);