2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef HAVE_LINUX_BPF_H
31 # include <linux/bpf.h>
34 #include "xlat/bpf_commands.h"
35 #include "xlat/bpf_map_types.h"
36 #include "xlat/bpf_prog_types.h"
37 #include "xlat/bpf_map_update_elem_flags.h"
40 bpf_map_create(struct tcb *const tcp, const kernel_ulong_t addr,
44 uint32_t map_type, key_size, value_size, max_entries;
49 return RVAL_DECODED | RVAL_FD;
51 if (size > sizeof(attr))
53 if (umoven_or_printaddr(tcp, addr, size, &attr))
54 return RVAL_DECODED | RVAL_FD;
56 tprints("{map_type=");
57 printxval(bpf_map_types, attr.map_type, "BPF_MAP_TYPE_???");
58 tprintf(", key_size=%u, value_size=%u, max_entries=%u}",
59 attr.key_size, attr.value_size, attr.max_entries);
61 return RVAL_DECODED | RVAL_FD;
65 bpf_map_update_elem(struct tcb *const tcp, const kernel_ulong_t addr,
70 uint64_t ATTRIBUTE_ALIGNED(8) key;
71 uint64_t ATTRIBUTE_ALIGNED(8) value;
79 if (size > sizeof(attr))
81 if (umoven_or_printaddr(tcp, addr, size, &attr))
85 printfd(tcp, attr.map_fd);
86 tprintf(", key=%#" PRIx64 ", value=%#" PRIx64 ", flags=",
87 attr.key, attr.value);
88 printxval64(bpf_map_update_elem_flags, attr.flags, "BPF_???");
93 bpf_map_delete_elem(struct tcb *const tcp, const kernel_ulong_t addr,
98 uint64_t ATTRIBUTE_ALIGNED(8) key;
105 if (size > sizeof(attr))
107 if (umoven_or_printaddr(tcp, addr, size, &attr))
111 printfd(tcp, attr.map_fd);
112 tprintf(", key=%#" PRIx64 "}", attr.key);
116 bpf_map_io(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int size,
117 const char *const text)
119 struct bpf_io_elem_struct {
121 uint64_t ATTRIBUTE_ALIGNED(8) key;
122 uint64_t ATTRIBUTE_ALIGNED(8) value;
126 if (!syserror(tcp) && !umove_or_printaddr(tcp, addr, &attr))
127 tprintf(", %s=%#" PRIx64, text, attr.value);
136 if (size > sizeof(attr))
138 if (umoven_or_printaddr(tcp, addr, size, &attr))
142 printfd(tcp, attr.map_fd);
143 tprintf(", key=%#" PRIx64, attr.key);
149 bpf_prog_load(struct tcb *const tcp, const kernel_ulong_t addr,
153 uint32_t prog_type, insn_cnt;
154 uint64_t ATTRIBUTE_ALIGNED(8) insns, license;
155 uint32_t log_level, log_size;
156 uint64_t ATTRIBUTE_ALIGNED(8) log_buf;
157 uint32_t kern_version;
162 return RVAL_DECODED | RVAL_FD;
164 if (size > sizeof(attr))
166 if (umoven_or_printaddr(tcp, addr, size, &attr))
167 return RVAL_DECODED | RVAL_FD;
169 tprints("{prog_type=");
170 printxval(bpf_prog_types, attr.prog_type, "BPF_PROG_TYPE_???");
171 tprintf(", insn_cnt=%u, insns=%#" PRIx64 ", license=",
172 attr.insn_cnt, attr.insns);
173 printstr(tcp, attr.license);
174 tprintf(", log_level=%u, log_size=%u, log_buf=%#" PRIx64 ", kern_version=%u}",
175 attr.log_level, attr.log_size, attr.log_buf, attr.kern_version);
177 return RVAL_DECODED | RVAL_FD;
182 const unsigned int cmd = tcp->u_arg[0];
183 const kernel_ulong_t addr = tcp->u_arg[1];
184 const unsigned int size = tcp->u_arg[2];
185 int rc = RVAL_DECODED;
188 printxval(bpf_commands, cmd, "BPF_???");
194 rc = bpf_map_create(tcp, addr, size);
196 case BPF_MAP_LOOKUP_ELEM:
197 rc = bpf_map_io(tcp, addr, size, "value");
199 case BPF_MAP_UPDATE_ELEM:
200 bpf_map_update_elem(tcp, addr, size);
202 case BPF_MAP_DELETE_ELEM:
203 bpf_map_delete_elem(tcp, addr, size);
205 case BPF_MAP_GET_NEXT_KEY:
206 rc = bpf_map_io(tcp, addr, size, "next_key");
209 rc = bpf_prog_load(tcp, addr, size);
216 if (rc & RVAL_DECODED)
217 tprintf(", %u", size);