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 *tcp, const long addr, unsigned int size)
43 uint32_t map_type, key_size, value_size, max_entries;
48 return RVAL_DECODED | RVAL_FD;
50 if (size > sizeof(attr))
52 if (umoven_or_printaddr(tcp, addr, size, &attr))
53 return RVAL_DECODED | RVAL_FD;
55 tprints("{map_type=");
56 printxval(bpf_map_types, attr.map_type, "BPF_MAP_TYPE_???");
57 tprintf(", key_size=%u, value_size=%u, max_entries=%u}",
58 attr.key_size, attr.value_size, attr.max_entries);
60 return RVAL_DECODED | RVAL_FD;
64 bpf_map_update_elem(struct tcb *tcp, const long addr, unsigned int size)
68 uint64_t ATTRIBUTE_ALIGNED(8) key;
69 uint64_t ATTRIBUTE_ALIGNED(8) value;
77 if (size > sizeof(attr))
79 if (umoven_or_printaddr(tcp, addr, size, &attr))
83 printfd(tcp, attr.map_fd);
84 tprintf(", key=%#" PRIx64 ", value=%#" PRIx64 ", flags=",
85 attr.key, attr.value);
86 printxval64(bpf_map_update_elem_flags, attr.flags, "BPF_???");
91 bpf_map_delete_elem(struct tcb *tcp, const long addr, unsigned int size)
95 uint64_t ATTRIBUTE_ALIGNED(8) key;
102 if (size > sizeof(attr))
104 if (umoven_or_printaddr(tcp, addr, size, &attr))
108 printfd(tcp, attr.map_fd);
109 tprintf(", key=%#" PRIx64 "}", attr.key);
113 bpf_map_io(struct tcb *tcp, const long addr, unsigned int size, const char *text)
115 struct bpf_io_elem_struct {
117 uint64_t ATTRIBUTE_ALIGNED(8) key;
118 uint64_t ATTRIBUTE_ALIGNED(8) value;
122 if (!syserror(tcp) && !umove_or_printaddr(tcp, addr, &attr))
123 tprintf(", %s=%#" PRIx64, text, attr.value);
132 if (size > sizeof(attr))
134 if (umoven_or_printaddr(tcp, addr, size, &attr))
138 printfd(tcp, attr.map_fd);
139 tprintf(", key=%#" PRIx64, attr.key);
145 bpf_prog_load(struct tcb *tcp, const long addr, unsigned int size)
148 uint32_t prog_type, insn_cnt;
149 uint64_t ATTRIBUTE_ALIGNED(8) insns, license;
150 uint32_t log_level, log_size;
151 uint64_t ATTRIBUTE_ALIGNED(8) log_buf;
152 uint32_t kern_version;
157 return RVAL_DECODED | RVAL_FD;
159 if (size > sizeof(attr))
161 if (umoven_or_printaddr(tcp, addr, size, &attr))
162 return RVAL_DECODED | RVAL_FD;
164 tprints("{prog_type=");
165 printxval(bpf_prog_types, attr.prog_type, "BPF_PROG_TYPE_???");
166 tprintf(", insn_cnt=%u, insns=%#" PRIx64 ", license=",
167 attr.insn_cnt, attr.insns);
168 printstr(tcp, attr.license, -1);
169 tprintf(", log_level=%u, log_size=%u, log_buf=%#" PRIx64 ", kern_version=%u}",
170 attr.log_level, attr.log_size, attr.log_buf, attr.kern_version);
172 return RVAL_DECODED | RVAL_FD;
177 const int cmd = tcp->u_arg[0];
178 const long addr = tcp->u_arg[1];
179 const unsigned int size = tcp->u_arg[2];
180 int rc = RVAL_DECODED;
183 printxval(bpf_commands, cmd, "BPF_???");
189 rc = bpf_map_create(tcp, addr, size);
191 case BPF_MAP_LOOKUP_ELEM:
192 rc = bpf_map_io(tcp, addr, size, "value");
194 case BPF_MAP_UPDATE_ELEM:
195 bpf_map_update_elem(tcp, addr, size);
197 case BPF_MAP_DELETE_ELEM:
198 bpf_map_delete_elem(tcp, addr, size);
200 case BPF_MAP_GET_NEXT_KEY:
201 rc = bpf_map_io(tcp, addr, size, "next_key");
204 rc = bpf_prog_load(tcp, addr, size);
211 if (rc & RVAL_DECODED)
212 tprintf(", %u", size);