| 1 |
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
| 2 |
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */ |
| 3 |
|
| 4 |
#include <errno.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <linux/err.h> |
| 7 |
#include <linux/kernel.h> |
| 8 |
#include <net/if.h> |
| 9 |
#include <stdbool.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <sys/types.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
|
| 17 |
#include <bpf/bpf.h> |
| 18 |
#include <bpf/btf.h> |
| 19 |
#include <bpf/hashmap.h> |
| 20 |
|
| 21 |
#include "json_writer.h" |
| 22 |
#include "main.h" |
| 23 |
|
| 24 |
static struct hashmap *map_table; |
| 25 |
|
| 26 |
static bool map_is_per_cpu(__u32 type) |
| 27 |
{ |
| 28 |
return type == BPF_MAP_TYPE_PERCPU_HASH || |
| 29 |
type == BPF_MAP_TYPE_PERCPU_ARRAY || |
| 30 |
type == BPF_MAP_TYPE_LRU_PERCPU_HASH || |
| 31 |
type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE; |
| 32 |
} |
| 33 |
|
| 34 |
static bool map_is_map_of_maps(__u32 type) |
| 35 |
{ |
| 36 |
return type == BPF_MAP_TYPE_ARRAY_OF_MAPS || |
| 37 |
type == BPF_MAP_TYPE_HASH_OF_MAPS; |
| 38 |
} |
| 39 |
|
| 40 |
static bool map_is_map_of_progs(__u32 type) |
| 41 |
{ |
| 42 |
return type == BPF_MAP_TYPE_PROG_ARRAY; |
| 43 |
} |
| 44 |
|
| 45 |
static int map_type_from_str(const char *type) |
| 46 |
{ |
| 47 |
const char *map_type_str; |
| 48 |
unsigned int i; |
| 49 |
|
| 50 |
for (i = 0; ; i++) { |
| 51 |
map_type_str = libbpf_bpf_map_type_str(i); |
| 52 |
if (!map_type_str) |
| 53 |
break; |
| 54 |
|
| 55 |
/* Don't allow prefixing in case of possible future shadowing */ |
| 56 |
if (!strcmp(map_type_str, type)) |
| 57 |
return i; |
| 58 |
} |
| 59 |
return -1; |
| 60 |
} |
| 61 |
|
| 62 |
static void *alloc_value(struct bpf_map_info *info) |
| 63 |
{ |
| 64 |
if (map_is_per_cpu(info->type)) |
| 65 |
return malloc(round_up(info->value_size, 8) * |
| 66 |
get_possible_cpus()); |
| 67 |
else |
| 68 |
return malloc(info->value_size); |
| 69 |
} |
| 70 |
|
| 71 |
static int do_dump_btf(const struct btf_dumper *d, |
| 72 |
struct bpf_map_info *map_info, void *key, |
| 73 |
void *value) |
| 74 |
{ |
| 75 |
__u32 value_id; |
| 76 |
int ret = 0; |
| 77 |
|
| 78 |
/* start of key-value pair */ |
| 79 |
jsonw_start_object(d->jw); |
| 80 |
|
| 81 |
if (map_info->btf_key_type_id) { |
| 82 |
jsonw_name(d->jw, "key"); |
| 83 |
|
| 84 |
ret = btf_dumper_type(d, map_info->btf_key_type_id, key); |
| 85 |
if (ret) |
| 86 |
goto err_end_obj; |
| 87 |
} |
| 88 |
|
| 89 |
value_id = map_info->btf_vmlinux_value_type_id ? |
| 90 |
: map_info->btf_value_type_id; |
| 91 |
|
| 92 |
if (!map_is_per_cpu(map_info->type)) { |
| 93 |
jsonw_name(d->jw, "value"); |
| 94 |
ret = btf_dumper_type(d, value_id, value); |
| 95 |
} else { |
| 96 |
unsigned int i, n, step; |
| 97 |
|
| 98 |
jsonw_name(d->jw, "values"); |
| 99 |
jsonw_start_array(d->jw); |
| 100 |
n = get_possible_cpus(); |
| 101 |
step = round_up(map_info->value_size, 8); |
| 102 |
for (i = 0; i < n; i++) { |
| 103 |
jsonw_start_object(d->jw); |
| 104 |
jsonw_int_field(d->jw, "cpu", i); |
| 105 |
jsonw_name(d->jw, "value"); |
| 106 |
ret = btf_dumper_type(d, value_id, value + i * step); |
| 107 |
jsonw_end_object(d->jw); |
| 108 |
if (ret) |
| 109 |
break; |
| 110 |
} |
| 111 |
jsonw_end_array(d->jw); |
| 112 |
} |
| 113 |
|
| 114 |
err_end_obj: |
| 115 |
/* end of key-value pair */ |
| 116 |
jsonw_end_object(d->jw); |
| 117 |
|
| 118 |
return ret; |
| 119 |
} |
| 120 |
|
| 121 |
static json_writer_t *get_btf_writer(void) |
| 122 |
{ |
| 123 |
json_writer_t *jw = jsonw_new(stdout); |
| 124 |
|
| 125 |
if (!jw) |
| 126 |
return NULL; |
| 127 |
jsonw_pretty(jw, true); |
| 128 |
|
| 129 |
return jw; |
| 130 |
} |
| 131 |
|
| 132 |
static void print_entry_json(struct bpf_map_info *info, unsigned char *key, |
| 133 |
unsigned char *value, struct btf *btf) |
| 134 |
{ |
| 135 |
jsonw_start_object(json_wtr); |
| 136 |
|
| 137 |
if (!map_is_per_cpu(info->type)) { |
| 138 |
jsonw_name(json_wtr, "key"); |
| 139 |
print_hex_data_json(key, info->key_size); |
| 140 |
jsonw_name(json_wtr, "value"); |
| 141 |
print_hex_data_json(value, info->value_size); |
| 142 |
if (map_is_map_of_maps(info->type)) |
| 143 |
jsonw_uint_field(json_wtr, "inner_map_id", |
| 144 |
*(unsigned int *)value); |
| 145 |
if (btf) { |
| 146 |
struct btf_dumper d = { |
| 147 |
.btf = btf, |
| 148 |
.jw = json_wtr, |
| 149 |
.is_plain_text = false, |
| 150 |
}; |
| 151 |
|
| 152 |
jsonw_name(json_wtr, "formatted"); |
| 153 |
do_dump_btf(&d, info, key, value); |
| 154 |
} |
| 155 |
} else { |
| 156 |
unsigned int i, n, step; |
| 157 |
|
| 158 |
n = get_possible_cpus(); |
| 159 |
step = round_up(info->value_size, 8); |
| 160 |
|
| 161 |
jsonw_name(json_wtr, "key"); |
| 162 |
print_hex_data_json(key, info->key_size); |
| 163 |
|
| 164 |
jsonw_name(json_wtr, "values"); |
| 165 |
jsonw_start_array(json_wtr); |
| 166 |
for (i = 0; i < n; i++) { |
| 167 |
jsonw_start_object(json_wtr); |
| 168 |
|
| 169 |
jsonw_int_field(json_wtr, "cpu", i); |
| 170 |
|
| 171 |
jsonw_name(json_wtr, "value"); |
| 172 |
print_hex_data_json(value + i * step, |
| 173 |
info->value_size); |
| 174 |
|
| 175 |
jsonw_end_object(json_wtr); |
| 176 |
} |
| 177 |
jsonw_end_array(json_wtr); |
| 178 |
if (btf) { |
| 179 |
struct btf_dumper d = { |
| 180 |
.btf = btf, |
| 181 |
.jw = json_wtr, |
| 182 |
.is_plain_text = false, |
| 183 |
}; |
| 184 |
|
| 185 |
jsonw_name(json_wtr, "formatted"); |
| 186 |
do_dump_btf(&d, info, key, value); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
jsonw_end_object(json_wtr); |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
print_entry_error_msg(struct bpf_map_info *info, unsigned char *key, |
| 195 |
const char *error_msg) |
| 196 |
{ |
| 197 |
int msg_size = strlen(error_msg); |
| 198 |
bool single_line, break_names; |
| 199 |
|
| 200 |
break_names = info->key_size > 16 || msg_size > 16; |
| 201 |
single_line = info->key_size + msg_size <= 24 && !break_names; |
| 202 |
|
| 203 |
printf("key:%c", break_names ? '\n' : ' '); |
| 204 |
fprint_hex(stdout, key, info->key_size, " "); |
| 205 |
|
| 206 |
printf(single_line ? " " : "\n"); |
| 207 |
|
| 208 |
printf("value:%c%s", break_names ? '\n' : ' ', error_msg); |
| 209 |
|
| 210 |
printf("\n"); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
print_entry_error(struct bpf_map_info *map_info, void *key, int lookup_errno) |
| 215 |
{ |
| 216 |
/* For prog_array maps or arrays of maps, failure to lookup the value |
| 217 |
* means there is no entry for that key. Do not print an error message |
| 218 |
* in that case. |
| 219 |
*/ |
| 220 |
if ((map_is_map_of_maps(map_info->type) || |
| 221 |
map_is_map_of_progs(map_info->type)) && lookup_errno == ENOENT) |
| 222 |
return; |
| 223 |
|
| 224 |
if (json_output) { |
| 225 |
jsonw_start_object(json_wtr); /* entry */ |
| 226 |
jsonw_name(json_wtr, "key"); |
| 227 |
print_hex_data_json(key, map_info->key_size); |
| 228 |
jsonw_name(json_wtr, "value"); |
| 229 |
jsonw_start_object(json_wtr); /* error */ |
| 230 |
jsonw_string_field(json_wtr, "error", strerror(lookup_errno)); |
| 231 |
jsonw_end_object(json_wtr); /* error */ |
| 232 |
jsonw_end_object(json_wtr); /* entry */ |
| 233 |
} else { |
| 234 |
const char *msg = NULL; |
| 235 |
|
| 236 |
if (lookup_errno == ENOENT) |
| 237 |
msg = "<no entry>"; |
| 238 |
else if (lookup_errno == ENOSPC && |
| 239 |
map_info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) |
| 240 |
msg = "<cannot read>"; |
| 241 |
|
| 242 |
print_entry_error_msg(map_info, key, |
| 243 |
msg ? : strerror(lookup_errno)); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
static void print_entry_plain(struct bpf_map_info *info, unsigned char *key, |
| 248 |
unsigned char *value) |
| 249 |
{ |
| 250 |
if (!map_is_per_cpu(info->type)) { |
| 251 |
bool single_line, break_names; |
| 252 |
|
| 253 |
break_names = info->key_size > 16 || info->value_size > 16; |
| 254 |
single_line = info->key_size + info->value_size <= 24 && |
| 255 |
!break_names; |
| 256 |
|
| 257 |
if (info->key_size) { |
| 258 |
printf("key:%c", break_names ? '\n' : ' '); |
| 259 |
fprint_hex(stdout, key, info->key_size, " "); |
| 260 |
|
| 261 |
printf(single_line ? " " : "\n"); |
| 262 |
} |
| 263 |
|
| 264 |
if (info->value_size) { |
| 265 |
if (map_is_map_of_maps(info->type)) { |
| 266 |
printf("inner_map_id:%c", break_names ? '\n' : ' '); |
| 267 |
printf("%u ", *(unsigned int *)value); |
| 268 |
} else { |
| 269 |
printf("value:%c", break_names ? '\n' : ' '); |
| 270 |
fprint_hex(stdout, value, info->value_size, " "); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
printf("\n"); |
| 275 |
} else { |
| 276 |
unsigned int i, n, step; |
| 277 |
|
| 278 |
n = get_possible_cpus(); |
| 279 |
step = round_up(info->value_size, 8); |
| 280 |
|
| 281 |
if (info->key_size) { |
| 282 |
printf("key:\n"); |
| 283 |
fprint_hex(stdout, key, info->key_size, " "); |
| 284 |
printf("\n"); |
| 285 |
} |
| 286 |
if (info->value_size) { |
| 287 |
for (i = 0; i < n; i++) { |
| 288 |
printf("value (CPU %02u):%c", |
| 289 |
i, info->value_size > 16 ? '\n' : ' '); |
| 290 |
fprint_hex(stdout, value + i * step, |
| 291 |
info->value_size, " "); |
| 292 |
printf("\n"); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
static char **parse_bytes(char **argv, const char *name, unsigned char *val, |
| 299 |
unsigned int n) |
| 300 |
{ |
| 301 |
unsigned int i = 0, base = 0; |
| 302 |
char *endptr; |
| 303 |
|
| 304 |
if (is_prefix(*argv, "hex")) { |
| 305 |
base = 16; |
| 306 |
argv++; |
| 307 |
} |
| 308 |
|
| 309 |
while (i < n && argv[i]) { |
| 310 |
val[i] = strtoul(argv[i], &endptr, base); |
| 311 |
if (*endptr) { |
| 312 |
p_err("error parsing byte: %s", argv[i]); |
| 313 |
return NULL; |
| 314 |
} |
| 315 |
i++; |
| 316 |
} |
| 317 |
|
| 318 |
if (i != n) { |
| 319 |
p_err("%s expected %u bytes got %u", name, n, i); |
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
return argv + i; |
| 324 |
} |
| 325 |
|
| 326 |
/* on per cpu maps we must copy the provided value on all value instances */ |
| 327 |
static void fill_per_cpu_value(struct bpf_map_info *info, void *value) |
| 328 |
{ |
| 329 |
unsigned int i, n, step; |
| 330 |
|
| 331 |
if (!map_is_per_cpu(info->type)) |
| 332 |
return; |
| 333 |
|
| 334 |
n = get_possible_cpus(); |
| 335 |
step = round_up(info->value_size, 8); |
| 336 |
for (i = 1; i < n; i++) |
| 337 |
memcpy(value + i * step, value, info->value_size); |
| 338 |
} |
| 339 |
|
| 340 |
static int parse_elem(char **argv, struct bpf_map_info *info, void *key, |
| 341 |
void *value, __u32 key_size, __u32 value_size, |
| 342 |
__u32 *flags, __u32 **value_fd, __u32 open_flags) |
| 343 |
{ |
| 344 |
if (!*argv) { |
| 345 |
if (!key && !value) |
| 346 |
return 0; |
| 347 |
p_err("did not find %s", key ? "key" : "value"); |
| 348 |
return -1; |
| 349 |
} |
| 350 |
|
| 351 |
if (is_prefix(*argv, "key")) { |
| 352 |
if (!key) { |
| 353 |
if (key_size) |
| 354 |
p_err("duplicate key"); |
| 355 |
else |
| 356 |
p_err("unnecessary key"); |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
argv = parse_bytes(argv + 1, "key", key, key_size); |
| 361 |
if (!argv) |
| 362 |
return -1; |
| 363 |
|
| 364 |
return parse_elem(argv, info, NULL, value, key_size, value_size, |
| 365 |
flags, value_fd, open_flags); |
| 366 |
} else if (is_prefix(*argv, "value")) { |
| 367 |
int fd; |
| 368 |
|
| 369 |
if (!value) { |
| 370 |
if (value_size) |
| 371 |
p_err("duplicate value"); |
| 372 |
else |
| 373 |
p_err("unnecessary value"); |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
argv++; |
| 378 |
|
| 379 |
if (map_is_map_of_maps(info->type)) { |
| 380 |
int argc = 2; |
| 381 |
|
| 382 |
if (value_size != 4) { |
| 383 |
p_err("value smaller than 4B for map in map?"); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
if (!argv[0] || !argv[1]) { |
| 387 |
p_err("not enough value arguments for map in map"); |
| 388 |
return -1; |
| 389 |
} |
| 390 |
|
| 391 |
fd = map_parse_fd(&argc, &argv, open_flags); |
| 392 |
if (fd < 0) |
| 393 |
return -1; |
| 394 |
|
| 395 |
*value_fd = value; |
| 396 |
**value_fd = fd; |
| 397 |
} else if (map_is_map_of_progs(info->type)) { |
| 398 |
int argc = 2; |
| 399 |
|
| 400 |
if (value_size != 4) { |
| 401 |
p_err("value smaller than 4B for map of progs?"); |
| 402 |
return -1; |
| 403 |
} |
| 404 |
if (!argv[0] || !argv[1]) { |
| 405 |
p_err("not enough value arguments for map of progs"); |
| 406 |
return -1; |
| 407 |
} |
| 408 |
if (is_prefix(*argv, "id")) |
| 409 |
p_info("Warning: updating program array via MAP_ID, make sure this map is kept open\n" |
| 410 |
" by some process or pinned otherwise update will be lost"); |
| 411 |
|
| 412 |
fd = prog_parse_fd(&argc, &argv); |
| 413 |
if (fd < 0) |
| 414 |
return -1; |
| 415 |
|
| 416 |
*value_fd = value; |
| 417 |
**value_fd = fd; |
| 418 |
} else { |
| 419 |
argv = parse_bytes(argv, "value", value, value_size); |
| 420 |
if (!argv) |
| 421 |
return -1; |
| 422 |
|
| 423 |
fill_per_cpu_value(info, value); |
| 424 |
} |
| 425 |
|
| 426 |
return parse_elem(argv, info, key, NULL, key_size, value_size, |
| 427 |
flags, NULL, open_flags); |
| 428 |
} else if (is_prefix(*argv, "any") || is_prefix(*argv, "noexist") || |
| 429 |
is_prefix(*argv, "exist")) { |
| 430 |
if (!flags) { |
| 431 |
p_err("flags specified multiple times: %s", *argv); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
|
| 435 |
if (is_prefix(*argv, "any")) |
| 436 |
*flags = BPF_ANY; |
| 437 |
else if (is_prefix(*argv, "noexist")) |
| 438 |
*flags = BPF_NOEXIST; |
| 439 |
else if (is_prefix(*argv, "exist")) |
| 440 |
*flags = BPF_EXIST; |
| 441 |
|
| 442 |
return parse_elem(argv + 1, info, key, value, key_size, |
| 443 |
value_size, NULL, value_fd, open_flags); |
| 444 |
} |
| 445 |
|
| 446 |
p_err("expected key or value, got: %s", *argv); |
| 447 |
return -1; |
| 448 |
} |
| 449 |
|
| 450 |
static void show_map_header_json(struct bpf_map_info *info, json_writer_t *wtr) |
| 451 |
{ |
| 452 |
const char *map_type_str; |
| 453 |
|
| 454 |
jsonw_uint_field(wtr, "id", info->id); |
| 455 |
map_type_str = libbpf_bpf_map_type_str(info->type); |
| 456 |
if (map_type_str) |
| 457 |
jsonw_string_field(wtr, "type", map_type_str); |
| 458 |
else |
| 459 |
jsonw_uint_field(wtr, "type", info->type); |
| 460 |
|
| 461 |
if (*info->name) |
| 462 |
jsonw_string_field(wtr, "name", info->name); |
| 463 |
|
| 464 |
jsonw_name(wtr, "flags"); |
| 465 |
jsonw_printf(wtr, "%u", info->map_flags); |
| 466 |
} |
| 467 |
|
| 468 |
static int show_map_close_json(int fd, struct bpf_map_info *info) |
| 469 |
{ |
| 470 |
char *memlock, *frozen_str; |
| 471 |
int frozen = 0; |
| 472 |
|
| 473 |
memlock = get_fdinfo(fd, "memlock"); |
| 474 |
frozen_str = get_fdinfo(fd, "frozen"); |
| 475 |
|
| 476 |
jsonw_start_object(json_wtr); |
| 477 |
|
| 478 |
show_map_header_json(info, json_wtr); |
| 479 |
|
| 480 |
print_dev_json(info->ifindex, info->netns_dev, info->netns_ino); |
| 481 |
|
| 482 |
jsonw_uint_field(json_wtr, "bytes_key", info->key_size); |
| 483 |
jsonw_uint_field(json_wtr, "bytes_value", info->value_size); |
| 484 |
jsonw_uint_field(json_wtr, "max_entries", info->max_entries); |
| 485 |
|
| 486 |
if (memlock) |
| 487 |
jsonw_int_field(json_wtr, "bytes_memlock", atoll(memlock)); |
| 488 |
free(memlock); |
| 489 |
|
| 490 |
if (info->type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 491 |
char *owner_prog_type = get_fdinfo(fd, "owner_prog_type"); |
| 492 |
char *owner_jited = get_fdinfo(fd, "owner_jited"); |
| 493 |
|
| 494 |
if (owner_prog_type) { |
| 495 |
unsigned int prog_type = atoi(owner_prog_type); |
| 496 |
const char *prog_type_str; |
| 497 |
|
| 498 |
prog_type_str = libbpf_bpf_prog_type_str(prog_type); |
| 499 |
if (prog_type_str) |
| 500 |
jsonw_string_field(json_wtr, "owner_prog_type", |
| 501 |
prog_type_str); |
| 502 |
else |
| 503 |
jsonw_uint_field(json_wtr, "owner_prog_type", |
| 504 |
prog_type); |
| 505 |
} |
| 506 |
if (owner_jited) |
| 507 |
jsonw_bool_field(json_wtr, "owner_jited", |
| 508 |
!!atoi(owner_jited)); |
| 509 |
|
| 510 |
free(owner_prog_type); |
| 511 |
free(owner_jited); |
| 512 |
} |
| 513 |
close(fd); |
| 514 |
|
| 515 |
if (frozen_str) { |
| 516 |
frozen = atoi(frozen_str); |
| 517 |
free(frozen_str); |
| 518 |
} |
| 519 |
jsonw_int_field(json_wtr, "frozen", frozen); |
| 520 |
|
| 521 |
if (info->btf_id) |
| 522 |
jsonw_int_field(json_wtr, "btf_id", info->btf_id); |
| 523 |
|
| 524 |
if (!hashmap__empty(map_table)) { |
| 525 |
struct hashmap_entry *entry; |
| 526 |
|
| 527 |
jsonw_name(json_wtr, "pinned"); |
| 528 |
jsonw_start_array(json_wtr); |
| 529 |
hashmap__for_each_key_entry(map_table, entry, info->id) |
| 530 |
jsonw_string(json_wtr, entry->pvalue); |
| 531 |
jsonw_end_array(json_wtr); |
| 532 |
} |
| 533 |
|
| 534 |
emit_obj_refs_json(refs_table, info->id, json_wtr); |
| 535 |
|
| 536 |
jsonw_end_object(json_wtr); |
| 537 |
|
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
static void show_map_header_plain(struct bpf_map_info *info) |
| 542 |
{ |
| 543 |
const char *map_type_str; |
| 544 |
|
| 545 |
printf("%u: ", info->id); |
| 546 |
|
| 547 |
map_type_str = libbpf_bpf_map_type_str(info->type); |
| 548 |
if (map_type_str) |
| 549 |
printf("%s ", map_type_str); |
| 550 |
else |
| 551 |
printf("type %u ", info->type); |
| 552 |
|
| 553 |
if (*info->name) |
| 554 |
printf("name %s ", info->name); |
| 555 |
|
| 556 |
printf("flags 0x%x", info->map_flags); |
| 557 |
print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino); |
| 558 |
printf("\n"); |
| 559 |
} |
| 560 |
|
| 561 |
static int show_map_close_plain(int fd, struct bpf_map_info *info) |
| 562 |
{ |
| 563 |
char *memlock, *frozen_str; |
| 564 |
int frozen = 0; |
| 565 |
|
| 566 |
memlock = get_fdinfo(fd, "memlock"); |
| 567 |
frozen_str = get_fdinfo(fd, "frozen"); |
| 568 |
|
| 569 |
show_map_header_plain(info); |
| 570 |
printf("\tkey %uB value %uB max_entries %u", |
| 571 |
info->key_size, info->value_size, info->max_entries); |
| 572 |
|
| 573 |
if (memlock) |
| 574 |
printf(" memlock %sB", memlock); |
| 575 |
free(memlock); |
| 576 |
|
| 577 |
if (info->type == BPF_MAP_TYPE_PROG_ARRAY) { |
| 578 |
char *owner_prog_type = get_fdinfo(fd, "owner_prog_type"); |
| 579 |
char *owner_jited = get_fdinfo(fd, "owner_jited"); |
| 580 |
|
| 581 |
if (owner_prog_type || owner_jited) |
| 582 |
printf("\n\t"); |
| 583 |
if (owner_prog_type) { |
| 584 |
unsigned int prog_type = atoi(owner_prog_type); |
| 585 |
const char *prog_type_str; |
| 586 |
|
| 587 |
prog_type_str = libbpf_bpf_prog_type_str(prog_type); |
| 588 |
if (prog_type_str) |
| 589 |
printf("owner_prog_type %s ", prog_type_str); |
| 590 |
else |
| 591 |
printf("owner_prog_type %u ", prog_type); |
| 592 |
} |
| 593 |
if (owner_jited) |
| 594 |
printf("owner%s jited", |
| 595 |
atoi(owner_jited) ? "" : " not"); |
| 596 |
|
| 597 |
free(owner_prog_type); |
| 598 |
free(owner_jited); |
| 599 |
} |
| 600 |
close(fd); |
| 601 |
|
| 602 |
if (!hashmap__empty(map_table)) { |
| 603 |
struct hashmap_entry *entry; |
| 604 |
|
| 605 |
hashmap__for_each_key_entry(map_table, entry, info->id) |
| 606 |
printf("\n\tpinned %s", (char *)entry->pvalue); |
| 607 |
} |
| 608 |
|
| 609 |
if (frozen_str) { |
| 610 |
frozen = atoi(frozen_str); |
| 611 |
free(frozen_str); |
| 612 |
} |
| 613 |
|
| 614 |
if (info->btf_id || frozen) |
| 615 |
printf("\n\t"); |
| 616 |
|
| 617 |
if (info->btf_id) |
| 618 |
printf("btf_id %u", info->btf_id); |
| 619 |
|
| 620 |
if (frozen) |
| 621 |
printf("%sfrozen", info->btf_id ? " " : ""); |
| 622 |
|
| 623 |
emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); |
| 624 |
|
| 625 |
printf("\n"); |
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
static int do_show_subset(int argc, char **argv) |
| 630 |
{ |
| 631 |
struct bpf_map_info info = {}; |
| 632 |
__u32 len = sizeof(info); |
| 633 |
int *fds = NULL; |
| 634 |
int nb_fds, i; |
| 635 |
int err = -1; |
| 636 |
|
| 637 |
fds = malloc(sizeof(int)); |
| 638 |
if (!fds) { |
| 639 |
p_err("mem alloc failed"); |
| 640 |
return -1; |
| 641 |
} |
| 642 |
nb_fds = map_parse_fds(&argc, &argv, &fds, BPF_F_RDONLY); |
| 643 |
if (nb_fds < 1) |
| 644 |
goto exit_free; |
| 645 |
|
| 646 |
if (json_output && nb_fds > 1) |
| 647 |
jsonw_start_array(json_wtr); /* root array */ |
| 648 |
for (i = 0; i < nb_fds; i++) { |
| 649 |
err = bpf_map_get_info_by_fd(fds[i], &info, &len); |
| 650 |
if (err) { |
| 651 |
p_err("can't get map info: %s", |
| 652 |
strerror(errno)); |
| 653 |
for (; i < nb_fds; i++) |
| 654 |
close(fds[i]); |
| 655 |
break; |
| 656 |
} |
| 657 |
|
| 658 |
if (json_output) |
| 659 |
show_map_close_json(fds[i], &info); |
| 660 |
else |
| 661 |
show_map_close_plain(fds[i], &info); |
| 662 |
|
| 663 |
close(fds[i]); |
| 664 |
} |
| 665 |
if (json_output && nb_fds > 1) |
| 666 |
jsonw_end_array(json_wtr); /* root array */ |
| 667 |
|
| 668 |
exit_free: |
| 669 |
free(fds); |
| 670 |
return err; |
| 671 |
} |
| 672 |
|
| 673 |
static int do_show(int argc, char **argv) |
| 674 |
{ |
| 675 |
LIBBPF_OPTS(bpf_get_fd_by_id_opts, opts); |
| 676 |
struct bpf_map_info info = {}; |
| 677 |
__u32 len = sizeof(info); |
| 678 |
__u32 id = 0; |
| 679 |
int err; |
| 680 |
int fd; |
| 681 |
|
| 682 |
opts.open_flags = BPF_F_RDONLY; |
| 683 |
|
| 684 |
if (show_pinned) { |
| 685 |
map_table = hashmap__new(hash_fn_for_key_as_id, |
| 686 |
equal_fn_for_key_as_id, NULL); |
| 687 |
if (IS_ERR(map_table)) { |
| 688 |
p_err("failed to create hashmap for pinned paths"); |
| 689 |
return -1; |
| 690 |
} |
| 691 |
build_pinned_obj_table(map_table, BPF_OBJ_MAP); |
| 692 |
} |
| 693 |
build_obj_refs_table(&refs_table, BPF_OBJ_MAP); |
| 694 |
|
| 695 |
if (argc == 2) |
| 696 |
return do_show_subset(argc, argv); |
| 697 |
|
| 698 |
if (argc) |
| 699 |
return BAD_ARG(); |
| 700 |
|
| 701 |
if (json_output) |
| 702 |
jsonw_start_array(json_wtr); |
| 703 |
while (true) { |
| 704 |
err = bpf_map_get_next_id(id, &id); |
| 705 |
if (err) { |
| 706 |
if (errno == ENOENT) |
| 707 |
break; |
| 708 |
p_err("can't get next map: %s%s", strerror(errno), |
| 709 |
errno == EINVAL ? " -- kernel too old?" : ""); |
| 710 |
break; |
| 711 |
} |
| 712 |
|
| 713 |
fd = bpf_map_get_fd_by_id_opts(id, &opts); |
| 714 |
if (fd < 0) { |
| 715 |
if (errno == ENOENT) |
| 716 |
continue; |
| 717 |
p_err("can't get map by id (%u): %s", |
| 718 |
id, strerror(errno)); |
| 719 |
break; |
| 720 |
} |
| 721 |
|
| 722 |
err = bpf_map_get_info_by_fd(fd, &info, &len); |
| 723 |
if (err) { |
| 724 |
p_err("can't get map info: %s", strerror(errno)); |
| 725 |
close(fd); |
| 726 |
break; |
| 727 |
} |
| 728 |
|
| 729 |
if (json_output) |
| 730 |
show_map_close_json(fd, &info); |
| 731 |
else |
| 732 |
show_map_close_plain(fd, &info); |
| 733 |
} |
| 734 |
if (json_output) |
| 735 |
jsonw_end_array(json_wtr); |
| 736 |
|
| 737 |
delete_obj_refs_table(refs_table); |
| 738 |
|
| 739 |
if (show_pinned) |
| 740 |
delete_pinned_obj_table(map_table); |
| 741 |
|
| 742 |
return errno == ENOENT ? 0 : -1; |
| 743 |
} |
| 744 |
|
| 745 |
static int dump_map_elem(int fd, void *key, void *value, |
| 746 |
struct bpf_map_info *map_info, struct btf *btf, |
| 747 |
json_writer_t *btf_wtr) |
| 748 |
{ |
| 749 |
if (bpf_map_lookup_elem(fd, key, value)) { |
| 750 |
print_entry_error(map_info, key, errno); |
| 751 |
return -1; |
| 752 |
} |
| 753 |
|
| 754 |
if (json_output) { |
| 755 |
print_entry_json(map_info, key, value, btf); |
| 756 |
} else if (btf) { |
| 757 |
struct btf_dumper d = { |
| 758 |
.btf = btf, |
| 759 |
.jw = btf_wtr, |
| 760 |
.is_plain_text = true, |
| 761 |
}; |
| 762 |
|
| 763 |
do_dump_btf(&d, map_info, key, value); |
| 764 |
} else { |
| 765 |
print_entry_plain(map_info, key, value); |
| 766 |
} |
| 767 |
|
| 768 |
return 0; |
| 769 |
} |
| 770 |
|
| 771 |
static int maps_have_btf(int *fds, int nb_fds) |
| 772 |
{ |
| 773 |
struct bpf_map_info info = {}; |
| 774 |
__u32 len = sizeof(info); |
| 775 |
int err, i; |
| 776 |
|
| 777 |
for (i = 0; i < nb_fds; i++) { |
| 778 |
err = bpf_map_get_info_by_fd(fds[i], &info, &len); |
| 779 |
if (err) { |
| 780 |
p_err("can't get map info: %s", strerror(errno)); |
| 781 |
return -1; |
| 782 |
} |
| 783 |
|
| 784 |
if (!info.btf_id) |
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
return 1; |
| 789 |
} |
| 790 |
|
| 791 |
static struct btf *btf_vmlinux; |
| 792 |
|
| 793 |
static int get_map_kv_btf(const struct bpf_map_info *info, struct btf **btf) |
| 794 |
{ |
| 795 |
int err = 0; |
| 796 |
|
| 797 |
if (info->btf_vmlinux_value_type_id) { |
| 798 |
if (!btf_vmlinux) { |
| 799 |
btf_vmlinux = libbpf_find_kernel_btf(); |
| 800 |
if (!btf_vmlinux) { |
| 801 |
p_err("failed to get kernel btf"); |
| 802 |
return -errno; |
| 803 |
} |
| 804 |
} |
| 805 |
*btf = btf_vmlinux; |
| 806 |
} else if (info->btf_value_type_id) { |
| 807 |
*btf = btf__load_from_kernel_by_id(info->btf_id); |
| 808 |
if (!*btf) { |
| 809 |
err = -errno; |
| 810 |
p_err("failed to get btf"); |
| 811 |
} |
| 812 |
} else { |
| 813 |
*btf = NULL; |
| 814 |
} |
| 815 |
|
| 816 |
return err; |
| 817 |
} |
| 818 |
|
| 819 |
static void free_map_kv_btf(struct btf *btf) |
| 820 |
{ |
| 821 |
if (btf != btf_vmlinux) |
| 822 |
btf__free(btf); |
| 823 |
} |
| 824 |
|
| 825 |
static int |
| 826 |
map_dump(int fd, struct bpf_map_info *info, json_writer_t *wtr, |
| 827 |
bool show_header) |
| 828 |
{ |
| 829 |
void *key, *value, *prev_key; |
| 830 |
unsigned int num_elems = 0; |
| 831 |
struct btf *btf = NULL; |
| 832 |
int err; |
| 833 |
|
| 834 |
key = malloc(info->key_size); |
| 835 |
value = alloc_value(info); |
| 836 |
if (!key || !value) { |
| 837 |
p_err("mem alloc failed"); |
| 838 |
err = -1; |
| 839 |
goto exit_free; |
| 840 |
} |
| 841 |
|
| 842 |
prev_key = NULL; |
| 843 |
|
| 844 |
if (wtr) { |
| 845 |
err = get_map_kv_btf(info, &btf); |
| 846 |
if (err) { |
| 847 |
goto exit_free; |
| 848 |
} |
| 849 |
|
| 850 |
if (show_header) { |
| 851 |
jsonw_start_object(wtr); /* map object */ |
| 852 |
show_map_header_json(info, wtr); |
| 853 |
jsonw_name(wtr, "elements"); |
| 854 |
} |
| 855 |
jsonw_start_array(wtr); /* elements */ |
| 856 |
} else if (show_header) { |
| 857 |
show_map_header_plain(info); |
| 858 |
} |
| 859 |
|
| 860 |
if (info->type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 861 |
info->value_size != 8) { |
| 862 |
const char *map_type_str; |
| 863 |
|
| 864 |
map_type_str = libbpf_bpf_map_type_str(info->type); |
| 865 |
p_info("Warning: cannot read values from %s map with value_size != 8", |
| 866 |
map_type_str); |
| 867 |
} |
| 868 |
while (true) { |
| 869 |
err = bpf_map_get_next_key(fd, prev_key, key); |
| 870 |
if (err) { |
| 871 |
if (errno == ENOENT) |
| 872 |
err = 0; |
| 873 |
break; |
| 874 |
} |
| 875 |
if (!dump_map_elem(fd, key, value, info, btf, wtr)) |
| 876 |
num_elems++; |
| 877 |
prev_key = key; |
| 878 |
} |
| 879 |
|
| 880 |
if (wtr) { |
| 881 |
jsonw_end_array(wtr); /* elements */ |
| 882 |
if (show_header) |
| 883 |
jsonw_end_object(wtr); /* map object */ |
| 884 |
} else { |
| 885 |
printf("Found %u element%s\n", num_elems, |
| 886 |
num_elems != 1 ? "s" : ""); |
| 887 |
} |
| 888 |
|
| 889 |
exit_free: |
| 890 |
free(key); |
| 891 |
free(value); |
| 892 |
close(fd); |
| 893 |
free_map_kv_btf(btf); |
| 894 |
|
| 895 |
return err; |
| 896 |
} |
| 897 |
|
| 898 |
static int do_dump(int argc, char **argv) |
| 899 |
{ |
| 900 |
json_writer_t *wtr = NULL, *btf_wtr = NULL; |
| 901 |
struct bpf_map_info info = {}; |
| 902 |
int nb_fds, i = 0; |
| 903 |
__u32 len = sizeof(info); |
| 904 |
int *fds = NULL; |
| 905 |
int err = -1; |
| 906 |
|
| 907 |
if (argc != 2) |
| 908 |
usage(); |
| 909 |
|
| 910 |
fds = malloc(sizeof(int)); |
| 911 |
if (!fds) { |
| 912 |
p_err("mem alloc failed"); |
| 913 |
return -1; |
| 914 |
} |
| 915 |
nb_fds = map_parse_fds(&argc, &argv, &fds, BPF_F_RDONLY); |
| 916 |
if (nb_fds < 1) |
| 917 |
goto exit_free; |
| 918 |
|
| 919 |
if (json_output) { |
| 920 |
wtr = json_wtr; |
| 921 |
} else { |
| 922 |
int do_plain_btf; |
| 923 |
|
| 924 |
do_plain_btf = maps_have_btf(fds, nb_fds); |
| 925 |
if (do_plain_btf < 0) |
| 926 |
goto exit_close; |
| 927 |
|
| 928 |
if (do_plain_btf) { |
| 929 |
btf_wtr = get_btf_writer(); |
| 930 |
wtr = btf_wtr; |
| 931 |
if (!btf_wtr) |
| 932 |
p_info("failed to create json writer for btf. falling back to plain output"); |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
if (wtr && nb_fds > 1) |
| 937 |
jsonw_start_array(wtr); /* root array */ |
| 938 |
for (i = 0; i < nb_fds; i++) { |
| 939 |
if (bpf_map_get_info_by_fd(fds[i], &info, &len)) { |
| 940 |
p_err("can't get map info: %s", strerror(errno)); |
| 941 |
break; |
| 942 |
} |
| 943 |
err = map_dump(fds[i], &info, wtr, nb_fds > 1); |
| 944 |
if (!wtr && i != nb_fds - 1) |
| 945 |
printf("\n"); |
| 946 |
|
| 947 |
if (err) |
| 948 |
break; |
| 949 |
close(fds[i]); |
| 950 |
} |
| 951 |
if (wtr && nb_fds > 1) |
| 952 |
jsonw_end_array(wtr); /* root array */ |
| 953 |
|
| 954 |
if (btf_wtr) |
| 955 |
jsonw_destroy(&btf_wtr); |
| 956 |
exit_close: |
| 957 |
for (; i < nb_fds; i++) |
| 958 |
close(fds[i]); |
| 959 |
exit_free: |
| 960 |
free(fds); |
| 961 |
btf__free(btf_vmlinux); |
| 962 |
return err; |
| 963 |
} |
| 964 |
|
| 965 |
static int alloc_key_value(struct bpf_map_info *info, void **key, void **value) |
| 966 |
{ |
| 967 |
*key = NULL; |
| 968 |
*value = NULL; |
| 969 |
|
| 970 |
if (info->key_size) { |
| 971 |
*key = malloc(info->key_size); |
| 972 |
if (!*key) { |
| 973 |
p_err("key mem alloc failed"); |
| 974 |
return -1; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
if (info->value_size) { |
| 979 |
*value = alloc_value(info); |
| 980 |
if (!*value) { |
| 981 |
p_err("value mem alloc failed"); |
| 982 |
free(*key); |
| 983 |
*key = NULL; |
| 984 |
return -1; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
static int do_update(int argc, char **argv) |
| 992 |
{ |
| 993 |
struct bpf_map_info info = {}; |
| 994 |
__u32 len = sizeof(info); |
| 995 |
__u32 *value_fd = NULL; |
| 996 |
__u32 flags = BPF_ANY; |
| 997 |
void *key, *value; |
| 998 |
int fd, err; |
| 999 |
|
| 1000 |
if (argc < 2) |
| 1001 |
usage(); |
| 1002 |
|
| 1003 |
fd = map_parse_fd_and_info(&argc, &argv, &info, &len, 0); |
| 1004 |
if (fd < 0) |
| 1005 |
return -1; |
| 1006 |
|
| 1007 |
err = alloc_key_value(&info, &key, &value); |
| 1008 |
if (err) |
| 1009 |
goto exit_free; |
| 1010 |
|
| 1011 |
err = parse_elem(argv, &info, key, value, info.key_size, |
| 1012 |
info.value_size, &flags, &value_fd, 0); |
| 1013 |
if (err) |
| 1014 |
goto exit_free; |
| 1015 |
|
| 1016 |
err = bpf_map_update_elem(fd, key, value, flags); |
| 1017 |
if (err) { |
| 1018 |
p_err("update failed: %s", strerror(errno)); |
| 1019 |
goto exit_free; |
| 1020 |
} |
| 1021 |
|
| 1022 |
exit_free: |
| 1023 |
if (value_fd) |
| 1024 |
close(*value_fd); |
| 1025 |
free(key); |
| 1026 |
free(value); |
| 1027 |
close(fd); |
| 1028 |
|
| 1029 |
if (!err && json_output) |
| 1030 |
jsonw_null(json_wtr); |
| 1031 |
return err; |
| 1032 |
} |
| 1033 |
|
| 1034 |
static void print_key_value(struct bpf_map_info *info, void *key, |
| 1035 |
void *value) |
| 1036 |
{ |
| 1037 |
json_writer_t *btf_wtr; |
| 1038 |
struct btf *btf; |
| 1039 |
|
| 1040 |
if (get_map_kv_btf(info, &btf)) |
| 1041 |
return; |
| 1042 |
|
| 1043 |
if (json_output) { |
| 1044 |
print_entry_json(info, key, value, btf); |
| 1045 |
} else if (btf) { |
| 1046 |
/* if here json_wtr wouldn't have been initialised, |
| 1047 |
* so let's create separate writer for btf |
| 1048 |
*/ |
| 1049 |
btf_wtr = get_btf_writer(); |
| 1050 |
if (!btf_wtr) { |
| 1051 |
p_info("failed to create json writer for btf. falling back to plain output"); |
| 1052 |
btf__free(btf); |
| 1053 |
btf = NULL; |
| 1054 |
print_entry_plain(info, key, value); |
| 1055 |
} else { |
| 1056 |
struct btf_dumper d = { |
| 1057 |
.btf = btf, |
| 1058 |
.jw = btf_wtr, |
| 1059 |
.is_plain_text = true, |
| 1060 |
}; |
| 1061 |
|
| 1062 |
do_dump_btf(&d, info, key, value); |
| 1063 |
jsonw_destroy(&btf_wtr); |
| 1064 |
} |
| 1065 |
} else { |
| 1066 |
print_entry_plain(info, key, value); |
| 1067 |
} |
| 1068 |
btf__free(btf); |
| 1069 |
} |
| 1070 |
|
| 1071 |
static int do_lookup(int argc, char **argv) |
| 1072 |
{ |
| 1073 |
struct bpf_map_info info = {}; |
| 1074 |
__u32 len = sizeof(info); |
| 1075 |
void *key, *value; |
| 1076 |
int err; |
| 1077 |
int fd; |
| 1078 |
|
| 1079 |
if (argc < 2) |
| 1080 |
usage(); |
| 1081 |
|
| 1082 |
fd = map_parse_fd_and_info(&argc, &argv, &info, &len, BPF_F_RDONLY); |
| 1083 |
if (fd < 0) |
| 1084 |
return -1; |
| 1085 |
|
| 1086 |
err = alloc_key_value(&info, &key, &value); |
| 1087 |
if (err) |
| 1088 |
goto exit_free; |
| 1089 |
|
| 1090 |
err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL, |
| 1091 |
BPF_F_RDONLY); |
| 1092 |
if (err) |
| 1093 |
goto exit_free; |
| 1094 |
|
| 1095 |
err = bpf_map_lookup_elem(fd, key, value); |
| 1096 |
if (err) { |
| 1097 |
if (errno == ENOENT) { |
| 1098 |
if (json_output) { |
| 1099 |
jsonw_null(json_wtr); |
| 1100 |
} else { |
| 1101 |
printf("key:\n"); |
| 1102 |
fprint_hex(stdout, key, info.key_size, " "); |
| 1103 |
printf("\n\nNot found\n"); |
| 1104 |
} |
| 1105 |
} else { |
| 1106 |
p_err("lookup failed: %s", strerror(errno)); |
| 1107 |
} |
| 1108 |
|
| 1109 |
goto exit_free; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* here means bpf_map_lookup_elem() succeeded */ |
| 1113 |
print_key_value(&info, key, value); |
| 1114 |
|
| 1115 |
exit_free: |
| 1116 |
free(key); |
| 1117 |
free(value); |
| 1118 |
close(fd); |
| 1119 |
|
| 1120 |
return err; |
| 1121 |
} |
| 1122 |
|
| 1123 |
static int do_getnext(int argc, char **argv) |
| 1124 |
{ |
| 1125 |
struct bpf_map_info info = {}; |
| 1126 |
__u32 len = sizeof(info); |
| 1127 |
void *key, *nextkey; |
| 1128 |
int err; |
| 1129 |
int fd; |
| 1130 |
|
| 1131 |
if (argc < 2) |
| 1132 |
usage(); |
| 1133 |
|
| 1134 |
fd = map_parse_fd_and_info(&argc, &argv, &info, &len, BPF_F_RDONLY); |
| 1135 |
if (fd < 0) |
| 1136 |
return -1; |
| 1137 |
|
| 1138 |
key = malloc(info.key_size); |
| 1139 |
nextkey = malloc(info.key_size); |
| 1140 |
if (!key || !nextkey) { |
| 1141 |
p_err("mem alloc failed"); |
| 1142 |
err = -1; |
| 1143 |
goto exit_free; |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (argc) { |
| 1147 |
err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, |
| 1148 |
NULL, BPF_F_RDONLY); |
| 1149 |
if (err) |
| 1150 |
goto exit_free; |
| 1151 |
} else { |
| 1152 |
free(key); |
| 1153 |
key = NULL; |
| 1154 |
} |
| 1155 |
|
| 1156 |
err = bpf_map_get_next_key(fd, key, nextkey); |
| 1157 |
if (err) { |
| 1158 |
p_err("can't get next key: %s", strerror(errno)); |
| 1159 |
goto exit_free; |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (json_output) { |
| 1163 |
jsonw_start_object(json_wtr); |
| 1164 |
if (key) { |
| 1165 |
jsonw_name(json_wtr, "key"); |
| 1166 |
print_hex_data_json(key, info.key_size); |
| 1167 |
} else { |
| 1168 |
jsonw_null_field(json_wtr, "key"); |
| 1169 |
} |
| 1170 |
jsonw_name(json_wtr, "next_key"); |
| 1171 |
print_hex_data_json(nextkey, info.key_size); |
| 1172 |
jsonw_end_object(json_wtr); |
| 1173 |
} else { |
| 1174 |
if (key) { |
| 1175 |
printf("key:\n"); |
| 1176 |
fprint_hex(stdout, key, info.key_size, " "); |
| 1177 |
printf("\n"); |
| 1178 |
} else { |
| 1179 |
printf("key: None\n"); |
| 1180 |
} |
| 1181 |
printf("next key:\n"); |
| 1182 |
fprint_hex(stdout, nextkey, info.key_size, " "); |
| 1183 |
printf("\n"); |
| 1184 |
} |
| 1185 |
|
| 1186 |
exit_free: |
| 1187 |
free(nextkey); |
| 1188 |
free(key); |
| 1189 |
close(fd); |
| 1190 |
|
| 1191 |
return err; |
| 1192 |
} |
| 1193 |
|
| 1194 |
static int do_delete(int argc, char **argv) |
| 1195 |
{ |
| 1196 |
struct bpf_map_info info = {}; |
| 1197 |
__u32 len = sizeof(info); |
| 1198 |
void *key; |
| 1199 |
int err; |
| 1200 |
int fd; |
| 1201 |
|
| 1202 |
if (argc < 2) |
| 1203 |
usage(); |
| 1204 |
|
| 1205 |
fd = map_parse_fd_and_info(&argc, &argv, &info, &len, 0); |
| 1206 |
if (fd < 0) |
| 1207 |
return -1; |
| 1208 |
|
| 1209 |
key = malloc(info.key_size); |
| 1210 |
if (!key) { |
| 1211 |
p_err("mem alloc failed"); |
| 1212 |
err = -1; |
| 1213 |
goto exit_free; |
| 1214 |
} |
| 1215 |
|
| 1216 |
err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL, |
| 1217 |
0); |
| 1218 |
if (err) |
| 1219 |
goto exit_free; |
| 1220 |
|
| 1221 |
err = bpf_map_delete_elem(fd, key); |
| 1222 |
if (err) |
| 1223 |
p_err("delete failed: %s", strerror(errno)); |
| 1224 |
|
| 1225 |
exit_free: |
| 1226 |
free(key); |
| 1227 |
close(fd); |
| 1228 |
|
| 1229 |
if (!err && json_output) |
| 1230 |
jsonw_null(json_wtr); |
| 1231 |
return err; |
| 1232 |
} |
| 1233 |
|
| 1234 |
static int map_parse_read_only_fd(int *argc, char ***argv) |
| 1235 |
{ |
| 1236 |
return map_parse_fd(argc, argv, BPF_F_RDONLY); |
| 1237 |
} |
| 1238 |
|
| 1239 |
static int do_pin(int argc, char **argv) |
| 1240 |
{ |
| 1241 |
int err; |
| 1242 |
|
| 1243 |
err = do_pin_any(argc, argv, map_parse_read_only_fd); |
| 1244 |
if (!err && json_output) |
| 1245 |
jsonw_null(json_wtr); |
| 1246 |
return err; |
| 1247 |
} |
| 1248 |
|
| 1249 |
static int do_create(int argc, char **argv) |
| 1250 |
{ |
| 1251 |
LIBBPF_OPTS(bpf_map_create_opts, attr); |
| 1252 |
enum bpf_map_type map_type = BPF_MAP_TYPE_UNSPEC; |
| 1253 |
__u32 key_size = 0, value_size = 0, max_entries = 0; |
| 1254 |
const char *map_name = NULL; |
| 1255 |
const char *pinfile; |
| 1256 |
int err = -1, fd; |
| 1257 |
|
| 1258 |
if (!REQ_ARGS(7)) |
| 1259 |
return -1; |
| 1260 |
pinfile = GET_ARG(); |
| 1261 |
|
| 1262 |
while (argc) { |
| 1263 |
if (!REQ_ARGS(2)) |
| 1264 |
return -1; |
| 1265 |
|
| 1266 |
if (is_prefix(*argv, "type")) { |
| 1267 |
NEXT_ARG(); |
| 1268 |
|
| 1269 |
if (map_type) { |
| 1270 |
p_err("map type already specified"); |
| 1271 |
goto exit; |
| 1272 |
} |
| 1273 |
|
| 1274 |
map_type = map_type_from_str(*argv); |
| 1275 |
if ((int)map_type < 0) { |
| 1276 |
p_err("unrecognized map type: %s", *argv); |
| 1277 |
goto exit; |
| 1278 |
} |
| 1279 |
NEXT_ARG(); |
| 1280 |
} else if (is_prefix(*argv, "name")) { |
| 1281 |
NEXT_ARG(); |
| 1282 |
map_name = GET_ARG(); |
| 1283 |
if (strlen(map_name) > BPF_OBJ_NAME_LEN - 1) { |
| 1284 |
p_info("Warning: map name is longer than %u characters, it will be truncated.", |
| 1285 |
BPF_OBJ_NAME_LEN - 1); |
| 1286 |
} |
| 1287 |
} else if (is_prefix(*argv, "key")) { |
| 1288 |
if (parse_u32_arg(&argc, &argv, &key_size, |
| 1289 |
"key size")) |
| 1290 |
goto exit; |
| 1291 |
} else if (is_prefix(*argv, "value")) { |
| 1292 |
if (parse_u32_arg(&argc, &argv, &value_size, |
| 1293 |
"value size")) |
| 1294 |
goto exit; |
| 1295 |
} else if (is_prefix(*argv, "entries")) { |
| 1296 |
if (parse_u32_arg(&argc, &argv, &max_entries, |
| 1297 |
"max entries")) |
| 1298 |
goto exit; |
| 1299 |
} else if (is_prefix(*argv, "flags")) { |
| 1300 |
if (parse_u32_arg(&argc, &argv, &attr.map_flags, |
| 1301 |
"flags")) |
| 1302 |
goto exit; |
| 1303 |
} else if (is_prefix(*argv, "dev")) { |
| 1304 |
p_info("Warning: 'bpftool map create [...] dev <ifname>' syntax is deprecated.\n" |
| 1305 |
"Going further, please use 'offload_dev <ifname>' to request hardware offload for the map."); |
| 1306 |
goto offload_dev; |
| 1307 |
} else if (is_prefix(*argv, "offload_dev")) { |
| 1308 |
offload_dev: |
| 1309 |
NEXT_ARG(); |
| 1310 |
|
| 1311 |
if (attr.map_ifindex) { |
| 1312 |
p_err("offload device already specified"); |
| 1313 |
goto exit; |
| 1314 |
} |
| 1315 |
|
| 1316 |
attr.map_ifindex = if_nametoindex(*argv); |
| 1317 |
if (!attr.map_ifindex) { |
| 1318 |
p_err("unrecognized netdevice '%s': %s", |
| 1319 |
*argv, strerror(errno)); |
| 1320 |
goto exit; |
| 1321 |
} |
| 1322 |
NEXT_ARG(); |
| 1323 |
} else if (is_prefix(*argv, "inner_map")) { |
| 1324 |
struct bpf_map_info info = {}; |
| 1325 |
__u32 len = sizeof(info); |
| 1326 |
int inner_map_fd; |
| 1327 |
|
| 1328 |
NEXT_ARG(); |
| 1329 |
if (!REQ_ARGS(2)) |
| 1330 |
usage(); |
| 1331 |
inner_map_fd = map_parse_fd_and_info(&argc, &argv, |
| 1332 |
&info, &len, BPF_F_RDONLY); |
| 1333 |
if (inner_map_fd < 0) |
| 1334 |
return -1; |
| 1335 |
attr.inner_map_fd = inner_map_fd; |
| 1336 |
} else { |
| 1337 |
p_err("unknown arg %s", *argv); |
| 1338 |
goto exit; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
if (!map_name) { |
| 1343 |
p_err("map name not specified"); |
| 1344 |
goto exit; |
| 1345 |
} |
| 1346 |
|
| 1347 |
set_max_rlimit(); |
| 1348 |
|
| 1349 |
fd = bpf_map_create(map_type, map_name, key_size, value_size, max_entries, &attr); |
| 1350 |
if (fd < 0) { |
| 1351 |
p_err("map create failed: %s", strerror(errno)); |
| 1352 |
goto exit; |
| 1353 |
} |
| 1354 |
|
| 1355 |
err = do_pin_fd(fd, pinfile); |
| 1356 |
close(fd); |
| 1357 |
if (err) |
| 1358 |
goto exit; |
| 1359 |
|
| 1360 |
if (json_output) |
| 1361 |
jsonw_null(json_wtr); |
| 1362 |
|
| 1363 |
exit: |
| 1364 |
if (attr.inner_map_fd > 0) |
| 1365 |
close(attr.inner_map_fd); |
| 1366 |
|
| 1367 |
return err; |
| 1368 |
} |
| 1369 |
|
| 1370 |
static int do_pop_dequeue(int argc, char **argv) |
| 1371 |
{ |
| 1372 |
struct bpf_map_info info = {}; |
| 1373 |
__u32 len = sizeof(info); |
| 1374 |
void *key, *value; |
| 1375 |
int err; |
| 1376 |
int fd; |
| 1377 |
|
| 1378 |
if (argc < 2) |
| 1379 |
usage(); |
| 1380 |
|
| 1381 |
fd = map_parse_fd_and_info(&argc, &argv, &info, &len, 0); |
| 1382 |
if (fd < 0) |
| 1383 |
return -1; |
| 1384 |
|
| 1385 |
err = alloc_key_value(&info, &key, &value); |
| 1386 |
if (err) |
| 1387 |
goto exit_free; |
| 1388 |
|
| 1389 |
err = bpf_map_lookup_and_delete_elem(fd, key, value); |
| 1390 |
if (err) { |
| 1391 |
if (errno == ENOENT) { |
| 1392 |
if (json_output) |
| 1393 |
jsonw_null(json_wtr); |
| 1394 |
else |
| 1395 |
printf("Error: empty map\n"); |
| 1396 |
} else { |
| 1397 |
p_err("pop failed: %s", strerror(errno)); |
| 1398 |
} |
| 1399 |
|
| 1400 |
goto exit_free; |
| 1401 |
} |
| 1402 |
|
| 1403 |
print_key_value(&info, key, value); |
| 1404 |
|
| 1405 |
exit_free: |
| 1406 |
free(key); |
| 1407 |
free(value); |
| 1408 |
close(fd); |
| 1409 |
|
| 1410 |
return err; |
| 1411 |
} |
| 1412 |
|
| 1413 |
static int do_freeze(int argc, char **argv) |
| 1414 |
{ |
| 1415 |
int err, fd; |
| 1416 |
|
| 1417 |
if (!REQ_ARGS(2)) |
| 1418 |
return -1; |
| 1419 |
|
| 1420 |
fd = map_parse_fd(&argc, &argv, 0); |
| 1421 |
if (fd < 0) |
| 1422 |
return -1; |
| 1423 |
|
| 1424 |
if (argc) { |
| 1425 |
close(fd); |
| 1426 |
return BAD_ARG(); |
| 1427 |
} |
| 1428 |
|
| 1429 |
err = bpf_map_freeze(fd); |
| 1430 |
close(fd); |
| 1431 |
if (err) { |
| 1432 |
p_err("failed to freeze map: %s", strerror(errno)); |
| 1433 |
return err; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if (json_output) |
| 1437 |
jsonw_null(json_wtr); |
| 1438 |
|
| 1439 |
return 0; |
| 1440 |
} |
| 1441 |
|
| 1442 |
static int do_help(int argc, char **argv) |
| 1443 |
{ |
| 1444 |
if (json_output) { |
| 1445 |
jsonw_null(json_wtr); |
| 1446 |
return 0; |
| 1447 |
} |
| 1448 |
|
| 1449 |
fprintf(stderr, |
| 1450 |
"Usage: %1$s %2$s { show | list } [MAP]\n" |
| 1451 |
" %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n" |
| 1452 |
" entries MAX_ENTRIES name NAME [flags FLAGS] \\\n" |
| 1453 |
" [inner_map MAP] [offload_dev NAME]\n" |
| 1454 |
" %1$s %2$s dump MAP\n" |
| 1455 |
" %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n" |
| 1456 |
" %1$s %2$s lookup MAP [key DATA]\n" |
| 1457 |
" %1$s %2$s getnext MAP [key DATA]\n" |
| 1458 |
" %1$s %2$s delete MAP key DATA\n" |
| 1459 |
" %1$s %2$s pin MAP FILE\n" |
| 1460 |
" %1$s %2$s event_pipe MAP [cpu N index M]\n" |
| 1461 |
" %1$s %2$s peek MAP\n" |
| 1462 |
" %1$s %2$s push MAP value VALUE\n" |
| 1463 |
" %1$s %2$s pop MAP\n" |
| 1464 |
" %1$s %2$s enqueue MAP value VALUE\n" |
| 1465 |
" %1$s %2$s dequeue MAP\n" |
| 1466 |
" %1$s %2$s freeze MAP\n" |
| 1467 |
" %1$s %2$s help\n" |
| 1468 |
"\n" |
| 1469 |
" " HELP_SPEC_MAP "\n" |
| 1470 |
" DATA := { [hex] BYTES }\n" |
| 1471 |
" " HELP_SPEC_PROGRAM "\n" |
| 1472 |
" VALUE := { DATA | MAP | PROG }\n" |
| 1473 |
" UPDATE_FLAGS := { any | exist | noexist }\n" |
| 1474 |
" TYPE := { hash | array | prog_array | perf_event_array | percpu_hash |\n" |
| 1475 |
" percpu_array | stack_trace | cgroup_array | lru_hash |\n" |
| 1476 |
" lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n" |
| 1477 |
" devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n" |
| 1478 |
" cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n" |
| 1479 |
" queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n" |
| 1480 |
" task_storage | bloom_filter | user_ringbuf | cgrp_storage | arena |\n" |
| 1481 |
" insn_array }\n" |
| 1482 |
" " HELP_SPEC_OPTIONS " |\n" |
| 1483 |
" {-f|--bpffs} | {-n|--nomount} }\n" |
| 1484 |
"", |
| 1485 |
bin_name, argv[-2]); |
| 1486 |
|
| 1487 |
return 0; |
| 1488 |
} |
| 1489 |
|
| 1490 |
static const struct cmd cmds[] = { |
| 1491 |
{ "show", do_show }, |
| 1492 |
{ "list", do_show }, |
| 1493 |
{ "help", do_help }, |
| 1494 |
{ "dump", do_dump }, |
| 1495 |
{ "update", do_update }, |
| 1496 |
{ "lookup", do_lookup }, |
| 1497 |
{ "getnext", do_getnext }, |
| 1498 |
{ "delete", do_delete }, |
| 1499 |
{ "pin", do_pin }, |
| 1500 |
{ "event_pipe", do_event_pipe }, |
| 1501 |
{ "create", do_create }, |
| 1502 |
{ "peek", do_lookup }, |
| 1503 |
{ "push", do_update }, |
| 1504 |
{ "enqueue", do_update }, |
| 1505 |
{ "pop", do_pop_dequeue }, |
| 1506 |
{ "dequeue", do_pop_dequeue }, |
| 1507 |
{ "freeze", do_freeze }, |
| 1508 |
{ 0 } |
| 1509 |
}; |
| 1510 |
|
| 1511 |
int do_map(int argc, char **argv) |
| 1512 |
{ |
| 1513 |
return cmd_select(cmds, argc, argv, do_help); |
| 1514 |
} |