diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/bpf/bpftool/Documentation/bpftool-map.rst | 40 | ||||
| -rw-r--r-- | tools/bpf/bpftool/Documentation/bpftool.rst | 2 | ||||
| -rw-r--r-- | tools/bpf/bpftool/Makefile | 7 | ||||
| -rw-r--r-- | tools/bpf/bpftool/bash-completion/bpftool | 36 | ||||
| -rw-r--r-- | tools/bpf/bpftool/common.c | 77 | ||||
| -rw-r--r-- | tools/bpf/bpftool/main.h | 7 | ||||
| -rw-r--r-- | tools/bpf/bpftool/map.c | 80 | ||||
| -rw-r--r-- | tools/bpf/bpftool/map_perf_ring.c | 347 |
8 files changed, 512 insertions, 84 deletions
diff --git a/tools/bpf/bpftool/Documentation/bpftool-map.rst b/tools/bpf/bpftool/Documentation/bpftool-map.rst index 5f512b14bff9..a6258bc8ec4f 100644 --- a/tools/bpf/bpftool/Documentation/bpftool-map.rst +++ b/tools/bpf/bpftool/Documentation/bpftool-map.rst | |||
| @@ -22,17 +22,19 @@ MAP COMMANDS | |||
| 22 | ============= | 22 | ============= |
| 23 | 23 | ||
| 24 | | **bpftool** **map { show | list }** [*MAP*] | 24 | | **bpftool** **map { show | list }** [*MAP*] |
| 25 | | **bpftool** **map dump** *MAP* | 25 | | **bpftool** **map dump** *MAP* |
| 26 | | **bpftool** **map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*] | 26 | | **bpftool** **map update** *MAP* **key** *DATA* **value** *VALUE* [*UPDATE_FLAGS*] |
| 27 | | **bpftool** **map lookup** *MAP* **key** [**hex**] *BYTES* | 27 | | **bpftool** **map lookup** *MAP* **key** *DATA* |
| 28 | | **bpftool** **map getnext** *MAP* [**key** [**hex**] *BYTES*] | 28 | | **bpftool** **map getnext** *MAP* [**key** *DATA*] |
| 29 | | **bpftool** **map delete** *MAP* **key** [**hex**] *BYTES* | 29 | | **bpftool** **map delete** *MAP* **key** *DATA* |
| 30 | | **bpftool** **map pin** *MAP* *FILE* | 30 | | **bpftool** **map pin** *MAP* *FILE* |
| 31 | | **bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*] | ||
| 31 | | **bpftool** **map help** | 32 | | **bpftool** **map help** |
| 32 | | | 33 | | |
| 33 | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } | 34 | | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* } |
| 35 | | *DATA* := { [**hex**] *BYTES* } | ||
| 34 | | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } | 36 | | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* } |
| 35 | | *VALUE* := { *BYTES* | *MAP* | *PROG* } | 37 | | *VALUE* := { *DATA* | *MAP* | *PROG* } |
| 36 | | *UPDATE_FLAGS* := { **any** | **exist** | **noexist** } | 38 | | *UPDATE_FLAGS* := { **any** | **exist** | **noexist** } |
| 37 | 39 | ||
| 38 | DESCRIPTION | 40 | DESCRIPTION |
| @@ -48,7 +50,7 @@ DESCRIPTION | |||
| 48 | **bpftool map dump** *MAP* | 50 | **bpftool map dump** *MAP* |
| 49 | Dump all entries in a given *MAP*. | 51 | Dump all entries in a given *MAP*. |
| 50 | 52 | ||
| 51 | **bpftool map update** *MAP* **key** [**hex**] *BYTES* **value** [**hex**] *VALUE* [*UPDATE_FLAGS*] | 53 | **bpftool map update** *MAP* **key** *DATA* **value** *VALUE* [*UPDATE_FLAGS*] |
| 52 | Update map entry for a given *KEY*. | 54 | Update map entry for a given *KEY*. |
| 53 | 55 | ||
| 54 | *UPDATE_FLAGS* can be one of: **any** update existing entry | 56 | *UPDATE_FLAGS* can be one of: **any** update existing entry |
| @@ -61,13 +63,13 @@ DESCRIPTION | |||
| 61 | the bytes are parsed as decimal values, unless a "0x" prefix | 63 | the bytes are parsed as decimal values, unless a "0x" prefix |
| 62 | (for hexadecimal) or a "0" prefix (for octal) is provided. | 64 | (for hexadecimal) or a "0" prefix (for octal) is provided. |
| 63 | 65 | ||
| 64 | **bpftool map lookup** *MAP* **key** [**hex**] *BYTES* | 66 | **bpftool map lookup** *MAP* **key** *DATA* |
| 65 | Lookup **key** in the map. | 67 | Lookup **key** in the map. |
| 66 | 68 | ||
| 67 | **bpftool map getnext** *MAP* [**key** [**hex**] *BYTES*] | 69 | **bpftool map getnext** *MAP* [**key** *DATA*] |
| 68 | Get next key. If *key* is not specified, get first key. | 70 | Get next key. If *key* is not specified, get first key. |
| 69 | 71 | ||
| 70 | **bpftool map delete** *MAP* **key** [**hex**] *BYTES* | 72 | **bpftool map delete** *MAP* **key** *DATA* |
| 71 | Remove entry from the map. | 73 | Remove entry from the map. |
| 72 | 74 | ||
| 73 | **bpftool map pin** *MAP* *FILE* | 75 | **bpftool map pin** *MAP* *FILE* |
| @@ -75,6 +77,22 @@ DESCRIPTION | |||
| 75 | 77 | ||
| 76 | Note: *FILE* must be located in *bpffs* mount. | 78 | Note: *FILE* must be located in *bpffs* mount. |
| 77 | 79 | ||
| 80 | **bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*] | ||
| 81 | Read events from a BPF_MAP_TYPE_PERF_EVENT_ARRAY map. | ||
| 82 | |||
| 83 | Install perf rings into a perf event array map and dump | ||
| 84 | output of any bpf_perf_event_output() call in the kernel. | ||
| 85 | By default read the number of CPUs on the system and | ||
| 86 | install perf ring for each CPU in the corresponding index | ||
| 87 | in the array. | ||
| 88 | |||
| 89 | If **cpu** and **index** are specified, install perf ring | ||
| 90 | for given **cpu** at **index** in the array (single ring). | ||
| 91 | |||
| 92 | Note that installing a perf ring into an array will silently | ||
| 93 | replace any existing ring. Any other application will stop | ||
| 94 | receiving events if it installed its rings earlier. | ||
| 95 | |||
| 78 | **bpftool map help** | 96 | **bpftool map help** |
| 79 | Print short help message. | 97 | Print short help message. |
| 80 | 98 | ||
diff --git a/tools/bpf/bpftool/Documentation/bpftool.rst b/tools/bpf/bpftool/Documentation/bpftool.rst index 20689a321ffe..564cb0d9692b 100644 --- a/tools/bpf/bpftool/Documentation/bpftool.rst +++ b/tools/bpf/bpftool/Documentation/bpftool.rst | |||
| @@ -23,7 +23,7 @@ SYNOPSIS | |||
| 23 | 23 | ||
| 24 | *MAP-COMMANDS* := | 24 | *MAP-COMMANDS* := |
| 25 | { **show** | **list** | **dump** | **update** | **lookup** | **getnext** | **delete** | 25 | { **show** | **list** | **dump** | **update** | **lookup** | **getnext** | **delete** |
| 26 | | **pin** | **help** } | 26 | | **pin** | **event_pipe** | **help** } |
| 27 | 27 | ||
| 28 | *PROG-COMMANDS* := { **show** | **list** | **dump jited** | **dump xlated** | **pin** | 28 | *PROG-COMMANDS* := { **show** | **list** | **dump jited** | **dump xlated** | **pin** |
| 29 | | **load** | **help** } | 29 | | **load** | **help** } |
diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 4e69782c4a79..892dbf095bff 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile | |||
| @@ -39,7 +39,12 @@ CC = gcc | |||
| 39 | 39 | ||
| 40 | CFLAGS += -O2 | 40 | CFLAGS += -O2 |
| 41 | CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers | 41 | CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers |
| 42 | CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/ | 42 | CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ |
| 43 | -I$(srctree)/kernel/bpf/ \ | ||
| 44 | -I$(srctree)/tools/include \ | ||
| 45 | -I$(srctree)/tools/include/uapi \ | ||
| 46 | -I$(srctree)/tools/lib/bpf \ | ||
| 47 | -I$(srctree)/tools/perf | ||
| 43 | CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' | 48 | CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"' |
| 44 | LIBS = -lelf -lbfd -lopcodes $(LIBBPF) | 49 | LIBS = -lelf -lbfd -lopcodes $(LIBBPF) |
| 45 | 50 | ||
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool index 852d84a98acd..b301c9b315f1 100644 --- a/tools/bpf/bpftool/bash-completion/bpftool +++ b/tools/bpf/bpftool/bash-completion/bpftool | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | # bpftool(8) bash completion -*- shell-script -*- | 1 | # bpftool(8) bash completion -*- shell-script -*- |
| 2 | # | 2 | # |
| 3 | # Copyright (C) 2017 Netronome Systems, Inc. | 3 | # Copyright (C) 2017-2018 Netronome Systems, Inc. |
| 4 | # | 4 | # |
| 5 | # This software is dual licensed under the GNU General License | 5 | # This software is dual licensed under the GNU General License |
| 6 | # Version 2, June 1991 as shown in the file COPYING in the top-level | 6 | # Version 2, June 1991 as shown in the file COPYING in the top-level |
| @@ -79,6 +79,14 @@ _bpftool_get_map_ids() | |||
| 79 | command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) ) | 79 | command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) ) |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | _bpftool_get_perf_map_ids() | ||
| 83 | { | ||
| 84 | COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \ | ||
| 85 | command grep -C2 perf_event_array | \ | ||
| 86 | command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) ) | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 82 | _bpftool_get_prog_ids() | 90 | _bpftool_get_prog_ids() |
| 83 | { | 91 | { |
| 84 | COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \ | 92 | COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \ |
| @@ -359,10 +367,34 @@ _bpftool() | |||
| 359 | fi | 367 | fi |
| 360 | return 0 | 368 | return 0 |
| 361 | ;; | 369 | ;; |
| 370 | event_pipe) | ||
| 371 | case $prev in | ||
| 372 | $command) | ||
| 373 | COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) ) | ||
| 374 | return 0 | ||
| 375 | ;; | ||
| 376 | id) | ||
| 377 | _bpftool_get_perf_map_ids | ||
| 378 | return 0 | ||
| 379 | ;; | ||
| 380 | cpu) | ||
| 381 | return 0 | ||
| 382 | ;; | ||
| 383 | index) | ||
| 384 | return 0 | ||
| 385 | ;; | ||
| 386 | *) | ||
| 387 | _bpftool_once_attr 'cpu' | ||
| 388 | _bpftool_once_attr 'index' | ||
| 389 | return 0 | ||
| 390 | ;; | ||
| 391 | esac | ||
| 392 | ;; | ||
| 362 | *) | 393 | *) |
| 363 | [[ $prev == $object ]] && \ | 394 | [[ $prev == $object ]] && \ |
| 364 | COMPREPLY=( $( compgen -W 'delete dump getnext help \ | 395 | COMPREPLY=( $( compgen -W 'delete dump getnext help \ |
| 365 | lookup pin show list update' -- "$cur" ) ) | 396 | lookup pin event_pipe show list update' -- \ |
| 397 | "$cur" ) ) | ||
| 366 | ;; | 398 | ;; |
| 367 | esac | 399 | esac |
| 368 | ;; | 400 | ;; |
diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 465995281dcd..32f9e397a6c0 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
| 3 | * | 3 | * |
| 4 | * This software is dual licensed under the GNU General License Version 2, | 4 | * This software is dual licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this | 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| @@ -33,6 +33,7 @@ | |||
| 33 | 33 | ||
| 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ | 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ |
| 35 | 35 | ||
| 36 | #include <ctype.h> | ||
| 36 | #include <errno.h> | 37 | #include <errno.h> |
| 37 | #include <fcntl.h> | 38 | #include <fcntl.h> |
| 38 | #include <fts.h> | 39 | #include <fts.h> |
| @@ -330,6 +331,16 @@ char *get_fdinfo(int fd, const char *key) | |||
| 330 | return NULL; | 331 | return NULL; |
| 331 | } | 332 | } |
| 332 | 333 | ||
| 334 | void print_data_json(uint8_t *data, size_t len) | ||
| 335 | { | ||
| 336 | unsigned int i; | ||
| 337 | |||
| 338 | jsonw_start_array(json_wtr); | ||
| 339 | for (i = 0; i < len; i++) | ||
| 340 | jsonw_printf(json_wtr, "%d", data[i]); | ||
| 341 | jsonw_end_array(json_wtr); | ||
| 342 | } | ||
| 343 | |||
| 333 | void print_hex_data_json(uint8_t *data, size_t len) | 344 | void print_hex_data_json(uint8_t *data, size_t len) |
| 334 | { | 345 | { |
| 335 | unsigned int i; | 346 | unsigned int i; |
| @@ -420,6 +431,70 @@ void delete_pinned_obj_table(struct pinned_obj_table *tab) | |||
| 420 | } | 431 | } |
| 421 | } | 432 | } |
| 422 | 433 | ||
| 434 | unsigned int get_page_size(void) | ||
| 435 | { | ||
| 436 | static int result; | ||
| 437 | |||
| 438 | if (!result) | ||
| 439 | result = getpagesize(); | ||
| 440 | return result; | ||
| 441 | } | ||
| 442 | |||
| 443 | unsigned int get_possible_cpus(void) | ||
| 444 | { | ||
| 445 | static unsigned int result; | ||
| 446 | char buf[128]; | ||
| 447 | long int n; | ||
| 448 | char *ptr; | ||
| 449 | int fd; | ||
| 450 | |||
| 451 | if (result) | ||
| 452 | return result; | ||
| 453 | |||
| 454 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); | ||
| 455 | if (fd < 0) { | ||
| 456 | p_err("can't open sysfs possible cpus"); | ||
| 457 | exit(-1); | ||
| 458 | } | ||
| 459 | |||
| 460 | n = read(fd, buf, sizeof(buf)); | ||
| 461 | if (n < 2) { | ||
| 462 | p_err("can't read sysfs possible cpus"); | ||
| 463 | exit(-1); | ||
| 464 | } | ||
| 465 | close(fd); | ||
| 466 | |||
| 467 | if (n == sizeof(buf)) { | ||
| 468 | p_err("read sysfs possible cpus overflow"); | ||
| 469 | exit(-1); | ||
| 470 | } | ||
| 471 | |||
| 472 | ptr = buf; | ||
| 473 | n = 0; | ||
| 474 | while (*ptr && *ptr != '\n') { | ||
| 475 | unsigned int a, b; | ||
| 476 | |||
| 477 | if (sscanf(ptr, "%u-%u", &a, &b) == 2) { | ||
| 478 | n += b - a + 1; | ||
| 479 | |||
| 480 | ptr = strchr(ptr, '-') + 1; | ||
| 481 | } else if (sscanf(ptr, "%u", &a) == 1) { | ||
| 482 | n++; | ||
| 483 | } else { | ||
| 484 | assert(0); | ||
| 485 | } | ||
| 486 | |||
| 487 | while (isdigit(*ptr)) | ||
| 488 | ptr++; | ||
| 489 | if (*ptr == ',') | ||
| 490 | ptr++; | ||
| 491 | } | ||
| 492 | |||
| 493 | result = n; | ||
| 494 | |||
| 495 | return result; | ||
| 496 | } | ||
| 497 | |||
| 423 | static char * | 498 | static char * |
| 424 | ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) | 499 | ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) |
| 425 | { | 500 | { |
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h index b8e9584d6246..6173cd997e7a 100644 --- a/tools/bpf/bpftool/main.h +++ b/tools/bpf/bpftool/main.h | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
| 3 | * | 3 | * |
| 4 | * This software is dual licensed under the GNU General License Version 2, | 4 | * This software is dual licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this | 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| @@ -117,14 +117,19 @@ int do_pin_fd(int fd, const char *name); | |||
| 117 | 117 | ||
| 118 | int do_prog(int argc, char **arg); | 118 | int do_prog(int argc, char **arg); |
| 119 | int do_map(int argc, char **arg); | 119 | int do_map(int argc, char **arg); |
| 120 | int do_event_pipe(int argc, char **argv); | ||
| 120 | int do_cgroup(int argc, char **arg); | 121 | int do_cgroup(int argc, char **arg); |
| 121 | 122 | ||
| 122 | int prog_parse_fd(int *argc, char ***argv); | 123 | int prog_parse_fd(int *argc, char ***argv); |
| 124 | int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len); | ||
| 123 | 125 | ||
| 124 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, | 126 | void disasm_print_insn(unsigned char *image, ssize_t len, int opcodes, |
| 125 | const char *arch); | 127 | const char *arch); |
| 128 | void print_data_json(uint8_t *data, size_t len); | ||
| 126 | void print_hex_data_json(uint8_t *data, size_t len); | 129 | void print_hex_data_json(uint8_t *data, size_t len); |
| 127 | 130 | ||
| 131 | unsigned int get_page_size(void); | ||
| 132 | unsigned int get_possible_cpus(void); | ||
| 128 | const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino); | 133 | const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino); |
| 129 | 134 | ||
| 130 | #endif | 135 | #endif |
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c index a6cdb640a0d7..af6766e956ba 100644 --- a/tools/bpf/bpftool/map.c +++ b/tools/bpf/bpftool/map.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (C) 2017 Netronome Systems, Inc. | 2 | * Copyright (C) 2017-2018 Netronome Systems, Inc. |
| 3 | * | 3 | * |
| 4 | * This software is dual licensed under the GNU General License Version 2, | 4 | * This software is dual licensed under the GNU General License Version 2, |
| 5 | * June 1991 as shown in the file COPYING in the top-level directory of this | 5 | * June 1991 as shown in the file COPYING in the top-level directory of this |
| @@ -34,7 +34,6 @@ | |||
| 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ | 34 | /* Author: Jakub Kicinski <kubakici@wp.pl> */ |
| 35 | 35 | ||
| 36 | #include <assert.h> | 36 | #include <assert.h> |
| 37 | #include <ctype.h> | ||
| 38 | #include <errno.h> | 37 | #include <errno.h> |
| 39 | #include <fcntl.h> | 38 | #include <fcntl.h> |
| 40 | #include <stdbool.h> | 39 | #include <stdbool.h> |
| @@ -69,61 +68,6 @@ static const char * const map_type_name[] = { | |||
| 69 | [BPF_MAP_TYPE_CPUMAP] = "cpumap", | 68 | [BPF_MAP_TYPE_CPUMAP] = "cpumap", |
| 70 | }; | 69 | }; |
| 71 | 70 | ||
| 72 | static unsigned int get_possible_cpus(void) | ||
| 73 | { | ||
| 74 | static unsigned int result; | ||
| 75 | char buf[128]; | ||
| 76 | long int n; | ||
| 77 | char *ptr; | ||
| 78 | int fd; | ||
| 79 | |||
| 80 | if (result) | ||
| 81 | return result; | ||
| 82 | |||
| 83 | fd = open("/sys/devices/system/cpu/possible", O_RDONLY); | ||
| 84 | if (fd < 0) { | ||
| 85 | p_err("can't open sysfs possible cpus"); | ||
| 86 | exit(-1); | ||
| 87 | } | ||
| 88 | |||
| 89 | n = read(fd, buf, sizeof(buf)); | ||
| 90 | if (n < 2) { | ||
| 91 | p_err("can't read sysfs possible cpus"); | ||
| 92 | exit(-1); | ||
| 93 | } | ||
| 94 | close(fd); | ||
| 95 | |||
| 96 | if (n == sizeof(buf)) { | ||
| 97 | p_err("read sysfs possible cpus overflow"); | ||
| 98 | exit(-1); | ||
| 99 | } | ||
| 100 | |||
| 101 | ptr = buf; | ||
| 102 | n = 0; | ||
| 103 | while (*ptr && *ptr != '\n') { | ||
| 104 | unsigned int a, b; | ||
| 105 | |||
| 106 | if (sscanf(ptr, "%u-%u", &a, &b) == 2) { | ||
| 107 | n += b - a + 1; | ||
| 108 | |||
| 109 | ptr = strchr(ptr, '-') + 1; | ||
| 110 | } else if (sscanf(ptr, "%u", &a) == 1) { | ||
| 111 | n++; | ||
| 112 | } else { | ||
| 113 | assert(0); | ||
| 114 | } | ||
| 115 | |||
| 116 | while (isdigit(*ptr)) | ||
| 117 | ptr++; | ||
| 118 | if (*ptr == ',') | ||
| 119 | ptr++; | ||
| 120 | } | ||
| 121 | |||
| 122 | result = n; | ||
| 123 | |||
| 124 | return result; | ||
| 125 | } | ||
| 126 | |||
| 127 | static bool map_is_per_cpu(__u32 type) | 71 | static bool map_is_per_cpu(__u32 type) |
| 128 | { | 72 | { |
| 129 | return type == BPF_MAP_TYPE_PERCPU_HASH || | 73 | return type == BPF_MAP_TYPE_PERCPU_HASH || |
| @@ -186,8 +130,7 @@ static int map_parse_fd(int *argc, char ***argv) | |||
| 186 | return -1; | 130 | return -1; |
| 187 | } | 131 | } |
| 188 | 132 | ||
| 189 | static int | 133 | int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len) |
| 190 | map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len) | ||
| 191 | { | 134 | { |
| 192 | int err; | 135 | int err; |
| 193 | int fd; | 136 | int fd; |
| @@ -873,23 +816,25 @@ static int do_help(int argc, char **argv) | |||
| 873 | 816 | ||
| 874 | fprintf(stderr, | 817 | fprintf(stderr, |
| 875 | "Usage: %s %s { show | list } [MAP]\n" | 818 | "Usage: %s %s { show | list } [MAP]\n" |
| 876 | " %s %s dump MAP\n" | 819 | " %s %s dump MAP\n" |
| 877 | " %s %s update MAP key [hex] BYTES value [hex] VALUE [UPDATE_FLAGS]\n" | 820 | " %s %s update MAP key DATA value VALUE [UPDATE_FLAGS]\n" |
| 878 | " %s %s lookup MAP key [hex] BYTES\n" | 821 | " %s %s lookup MAP key DATA\n" |
| 879 | " %s %s getnext MAP [key [hex] BYTES]\n" | 822 | " %s %s getnext MAP [key DATA]\n" |
| 880 | " %s %s delete MAP key [hex] BYTES\n" | 823 | " %s %s delete MAP key DATA\n" |
| 881 | " %s %s pin MAP FILE\n" | 824 | " %s %s pin MAP FILE\n" |
| 825 | " %s %s event_pipe MAP [cpu N index M]\n" | ||
| 882 | " %s %s help\n" | 826 | " %s %s help\n" |
| 883 | "\n" | 827 | "\n" |
| 884 | " MAP := { id MAP_ID | pinned FILE }\n" | 828 | " MAP := { id MAP_ID | pinned FILE }\n" |
| 829 | " DATA := { [hex] BYTES }\n" | ||
| 885 | " " HELP_SPEC_PROGRAM "\n" | 830 | " " HELP_SPEC_PROGRAM "\n" |
| 886 | " VALUE := { BYTES | MAP | PROG }\n" | 831 | " VALUE := { DATA | MAP | PROG }\n" |
| 887 | " UPDATE_FLAGS := { any | exist | noexist }\n" | 832 | " UPDATE_FLAGS := { any | exist | noexist }\n" |
| 888 | " " HELP_SPEC_OPTIONS "\n" | 833 | " " HELP_SPEC_OPTIONS "\n" |
| 889 | "", | 834 | "", |
| 890 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], | 835 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], |
| 891 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], | 836 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2], |
| 892 | bin_name, argv[-2], bin_name, argv[-2]); | 837 | bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2]); |
| 893 | 838 | ||
| 894 | return 0; | 839 | return 0; |
| 895 | } | 840 | } |
| @@ -904,6 +849,7 @@ static const struct cmd cmds[] = { | |||
| 904 | { "getnext", do_getnext }, | 849 | { "getnext", do_getnext }, |
| 905 | { "delete", do_delete }, | 850 | { "delete", do_delete }, |
| 906 | { "pin", do_pin }, | 851 | { "pin", do_pin }, |
| 852 | { "event_pipe", do_event_pipe }, | ||
| 907 | { 0 } | 853 | { 0 } |
| 908 | }; | 854 | }; |
| 909 | 855 | ||
diff --git a/tools/bpf/bpftool/map_perf_ring.c b/tools/bpf/bpftool/map_perf_ring.c new file mode 100644 index 000000000000..c5a2ced8552d --- /dev/null +++ b/tools/bpf/bpftool/map_perf_ring.c | |||
| @@ -0,0 +1,347 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0-only | ||
| 2 | /* Copyright (C) 2018 Netronome Systems, Inc. */ | ||
| 3 | /* This program is free software; you can redistribute it and/or | ||
| 4 | * modify it under the terms of version 2 of the GNU General Public | ||
| 5 | * License as published by the Free Software Foundation. | ||
| 6 | */ | ||
| 7 | #include <errno.h> | ||
| 8 | #include <fcntl.h> | ||
| 9 | #include <libbpf.h> | ||
| 10 | #include <poll.h> | ||
| 11 | #include <signal.h> | ||
| 12 | #include <stdbool.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <string.h> | ||
| 16 | #include <time.h> | ||
| 17 | #include <unistd.h> | ||
| 18 | #include <linux/bpf.h> | ||
| 19 | #include <linux/perf_event.h> | ||
| 20 | #include <sys/ioctl.h> | ||
| 21 | #include <sys/mman.h> | ||
| 22 | #include <sys/syscall.h> | ||
| 23 | |||
| 24 | #include <bpf.h> | ||
| 25 | #include <perf-sys.h> | ||
| 26 | |||
| 27 | #include "main.h" | ||
| 28 | |||
| 29 | #define MMAP_PAGE_CNT 16 | ||
| 30 | |||
| 31 | static bool stop; | ||
| 32 | |||
| 33 | struct event_ring_info { | ||
| 34 | int fd; | ||
| 35 | int key; | ||
| 36 | unsigned int cpu; | ||
| 37 | void *mem; | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct perf_event_sample { | ||
| 41 | struct perf_event_header header; | ||
| 42 | __u32 size; | ||
| 43 | unsigned char data[]; | ||
| 44 | }; | ||
| 45 | |||
| 46 | static void int_exit(int signo) | ||
| 47 | { | ||
| 48 | fprintf(stderr, "Stopping...\n"); | ||
| 49 | stop = true; | ||
| 50 | } | ||
| 51 | |||
| 52 | static void | ||
| 53 | print_bpf_output(struct event_ring_info *ring, struct perf_event_sample *e) | ||
| 54 | { | ||
| 55 | struct { | ||
| 56 | struct perf_event_header header; | ||
| 57 | __u64 id; | ||
| 58 | __u64 lost; | ||
| 59 | } *lost = (void *)e; | ||
| 60 | struct timespec ts; | ||
| 61 | |||
| 62 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) { | ||
| 63 | perror("Can't read clock for timestamp"); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | if (json_output) { | ||
| 68 | jsonw_start_object(json_wtr); | ||
| 69 | jsonw_name(json_wtr, "timestamp"); | ||
| 70 | jsonw_uint(json_wtr, ts.tv_sec * 1000000000ull + ts.tv_nsec); | ||
| 71 | jsonw_name(json_wtr, "type"); | ||
| 72 | jsonw_uint(json_wtr, e->header.type); | ||
| 73 | jsonw_name(json_wtr, "cpu"); | ||
| 74 | jsonw_uint(json_wtr, ring->cpu); | ||
| 75 | jsonw_name(json_wtr, "index"); | ||
| 76 | jsonw_uint(json_wtr, ring->key); | ||
| 77 | if (e->header.type == PERF_RECORD_SAMPLE) { | ||
| 78 | jsonw_name(json_wtr, "data"); | ||
| 79 | print_data_json(e->data, e->size); | ||
| 80 | } else if (e->header.type == PERF_RECORD_LOST) { | ||
| 81 | jsonw_name(json_wtr, "lost"); | ||
| 82 | jsonw_start_object(json_wtr); | ||
| 83 | jsonw_name(json_wtr, "id"); | ||
| 84 | jsonw_uint(json_wtr, lost->id); | ||
| 85 | jsonw_name(json_wtr, "count"); | ||
| 86 | jsonw_uint(json_wtr, lost->lost); | ||
| 87 | jsonw_end_object(json_wtr); | ||
| 88 | } | ||
| 89 | jsonw_end_object(json_wtr); | ||
| 90 | } else { | ||
| 91 | if (e->header.type == PERF_RECORD_SAMPLE) { | ||
| 92 | printf("== @%ld.%ld CPU: %d index: %d =====\n", | ||
| 93 | (long)ts.tv_sec, ts.tv_nsec, | ||
| 94 | ring->cpu, ring->key); | ||
| 95 | fprint_hex(stdout, e->data, e->size, " "); | ||
| 96 | printf("\n"); | ||
| 97 | } else if (e->header.type == PERF_RECORD_LOST) { | ||
| 98 | printf("lost %lld events\n", lost->lost); | ||
| 99 | } else { | ||
| 100 | printf("unknown event type=%d size=%d\n", | ||
| 101 | e->header.type, e->header.size); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | static void | ||
| 107 | perf_event_read(struct event_ring_info *ring, void **buf, size_t *buf_len) | ||
| 108 | { | ||
| 109 | volatile struct perf_event_mmap_page *header = ring->mem; | ||
| 110 | __u64 buffer_size = MMAP_PAGE_CNT * get_page_size(); | ||
| 111 | __u64 data_tail = header->data_tail; | ||
| 112 | __u64 data_head = header->data_head; | ||
| 113 | void *base, *begin, *end; | ||
| 114 | |||
| 115 | asm volatile("" ::: "memory"); /* in real code it should be smp_rmb() */ | ||
| 116 | if (data_head == data_tail) | ||
| 117 | return; | ||
| 118 | |||
| 119 | base = ((char *)header) + get_page_size(); | ||
| 120 | |||
| 121 | begin = base + data_tail % buffer_size; | ||
| 122 | end = base + data_head % buffer_size; | ||
| 123 | |||
| 124 | while (begin != end) { | ||
| 125 | struct perf_event_sample *e; | ||
| 126 | |||
| 127 | e = begin; | ||
| 128 | if (begin + e->header.size > base + buffer_size) { | ||
| 129 | long len = base + buffer_size - begin; | ||
| 130 | |||
| 131 | if (*buf_len < e->header.size) { | ||
| 132 | free(*buf); | ||
| 133 | *buf = malloc(e->header.size); | ||
| 134 | if (!*buf) { | ||
| 135 | fprintf(stderr, | ||
| 136 | "can't allocate memory"); | ||
| 137 | stop = true; | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | *buf_len = e->header.size; | ||
| 141 | } | ||
| 142 | |||
| 143 | memcpy(*buf, begin, len); | ||
| 144 | memcpy(*buf + len, base, e->header.size - len); | ||
| 145 | e = (void *)*buf; | ||
| 146 | begin = base + e->header.size - len; | ||
| 147 | } else if (begin + e->header.size == base + buffer_size) { | ||
| 148 | begin = base; | ||
| 149 | } else { | ||
| 150 | begin += e->header.size; | ||
| 151 | } | ||
| 152 | |||
| 153 | print_bpf_output(ring, e); | ||
| 154 | } | ||
| 155 | |||
| 156 | __sync_synchronize(); /* smp_mb() */ | ||
| 157 | header->data_tail = data_head; | ||
| 158 | } | ||
| 159 | |||
| 160 | static int perf_mmap_size(void) | ||
| 161 | { | ||
| 162 | return get_page_size() * (MMAP_PAGE_CNT + 1); | ||
| 163 | } | ||
| 164 | |||
| 165 | static void *perf_event_mmap(int fd) | ||
| 166 | { | ||
| 167 | int mmap_size = perf_mmap_size(); | ||
| 168 | void *base; | ||
| 169 | |||
| 170 | base = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | ||
| 171 | if (base == MAP_FAILED) { | ||
| 172 | p_err("event mmap failed: %s\n", strerror(errno)); | ||
| 173 | return NULL; | ||
| 174 | } | ||
| 175 | |||
| 176 | return base; | ||
| 177 | } | ||
| 178 | |||
| 179 | static void perf_event_unmap(void *mem) | ||
| 180 | { | ||
| 181 | if (munmap(mem, perf_mmap_size())) | ||
| 182 | fprintf(stderr, "Can't unmap ring memory!\n"); | ||
| 183 | } | ||
| 184 | |||
| 185 | static int bpf_perf_event_open(int map_fd, int key, int cpu) | ||
| 186 | { | ||
| 187 | struct perf_event_attr attr = { | ||
| 188 | .sample_type = PERF_SAMPLE_RAW, | ||
| 189 | .type = PERF_TYPE_SOFTWARE, | ||
| 190 | .config = PERF_COUNT_SW_BPF_OUTPUT, | ||
| 191 | }; | ||
| 192 | int pmu_fd; | ||
| 193 | |||
| 194 | pmu_fd = sys_perf_event_open(&attr, -1, cpu, -1, 0); | ||
| 195 | if (pmu_fd < 0) { | ||
| 196 | p_err("failed to open perf event %d for CPU %d", key, cpu); | ||
| 197 | return -1; | ||
| 198 | } | ||
| 199 | |||
| 200 | if (bpf_map_update_elem(map_fd, &key, &pmu_fd, BPF_ANY)) { | ||
| 201 | p_err("failed to update map for event %d for CPU %d", key, cpu); | ||
| 202 | goto err_close; | ||
| 203 | } | ||
| 204 | if (ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0)) { | ||
| 205 | p_err("failed to enable event %d for CPU %d", key, cpu); | ||
| 206 | goto err_close; | ||
| 207 | } | ||
| 208 | |||
| 209 | return pmu_fd; | ||
| 210 | |||
| 211 | err_close: | ||
| 212 | close(pmu_fd); | ||
| 213 | return -1; | ||
| 214 | } | ||
| 215 | |||
| 216 | int do_event_pipe(int argc, char **argv) | ||
| 217 | { | ||
| 218 | int i, nfds, map_fd, index = -1, cpu = -1; | ||
| 219 | struct bpf_map_info map_info = {}; | ||
| 220 | struct event_ring_info *rings; | ||
| 221 | size_t tmp_buf_sz = 0; | ||
| 222 | void *tmp_buf = NULL; | ||
| 223 | struct pollfd *pfds; | ||
| 224 | __u32 map_info_len; | ||
| 225 | bool do_all = true; | ||
| 226 | |||
| 227 | map_info_len = sizeof(map_info); | ||
| 228 | map_fd = map_parse_fd_and_info(&argc, &argv, &map_info, &map_info_len); | ||
| 229 | if (map_fd < 0) | ||
| 230 | return -1; | ||
| 231 | |||
| 232 | if (map_info.type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) { | ||
| 233 | p_err("map is not a perf event array"); | ||
| 234 | goto err_close_map; | ||
| 235 | } | ||
| 236 | |||
| 237 | while (argc) { | ||
| 238 | if (argc < 2) | ||
| 239 | BAD_ARG(); | ||
| 240 | |||
| 241 | if (is_prefix(*argv, "cpu")) { | ||
| 242 | char *endptr; | ||
| 243 | |||
| 244 | NEXT_ARG(); | ||
| 245 | cpu = strtoul(*argv, &endptr, 0); | ||
| 246 | if (*endptr) { | ||
| 247 | p_err("can't parse %s as CPU ID", **argv); | ||
| 248 | goto err_close_map; | ||
| 249 | } | ||
| 250 | |||
| 251 | NEXT_ARG(); | ||
| 252 | } else if (is_prefix(*argv, "index")) { | ||
| 253 | char *endptr; | ||
| 254 | |||
| 255 | NEXT_ARG(); | ||
| 256 | index = strtoul(*argv, &endptr, 0); | ||
| 257 | if (*endptr) { | ||
| 258 | p_err("can't parse %s as index", **argv); | ||
| 259 | goto err_close_map; | ||
| 260 | } | ||
| 261 | |||
| 262 | NEXT_ARG(); | ||
| 263 | } else { | ||
| 264 | BAD_ARG(); | ||
| 265 | } | ||
| 266 | |||
| 267 | do_all = false; | ||
| 268 | } | ||
| 269 | |||
| 270 | if (!do_all) { | ||
| 271 | if (index == -1 || cpu == -1) { | ||
| 272 | p_err("cpu and index must be specified together"); | ||
| 273 | goto err_close_map; | ||
| 274 | } | ||
| 275 | |||
| 276 | nfds = 1; | ||
| 277 | } else { | ||
| 278 | nfds = min(get_possible_cpus(), map_info.max_entries); | ||
| 279 | cpu = 0; | ||
| 280 | index = 0; | ||
| 281 | } | ||
| 282 | |||
| 283 | rings = calloc(nfds, sizeof(rings[0])); | ||
| 284 | if (!rings) | ||
| 285 | goto err_close_map; | ||
| 286 | |||
| 287 | pfds = calloc(nfds, sizeof(pfds[0])); | ||
| 288 | if (!pfds) | ||
| 289 | goto err_free_rings; | ||
| 290 | |||
| 291 | for (i = 0; i < nfds; i++) { | ||
| 292 | rings[i].cpu = cpu + i; | ||
| 293 | rings[i].key = index + i; | ||
| 294 | |||
| 295 | rings[i].fd = bpf_perf_event_open(map_fd, rings[i].key, | ||
| 296 | rings[i].cpu); | ||
| 297 | if (rings[i].fd < 0) | ||
| 298 | goto err_close_fds_prev; | ||
| 299 | |||
| 300 | rings[i].mem = perf_event_mmap(rings[i].fd); | ||
| 301 | if (!rings[i].mem) | ||
| 302 | goto err_close_fds_current; | ||
| 303 | |||
| 304 | pfds[i].fd = rings[i].fd; | ||
| 305 | pfds[i].events = POLLIN; | ||
| 306 | } | ||
| 307 | |||
| 308 | signal(SIGINT, int_exit); | ||
| 309 | signal(SIGHUP, int_exit); | ||
| 310 | signal(SIGTERM, int_exit); | ||
| 311 | |||
| 312 | if (json_output) | ||
| 313 | jsonw_start_array(json_wtr); | ||
| 314 | |||
| 315 | while (!stop) { | ||
| 316 | poll(pfds, nfds, 200); | ||
| 317 | for (i = 0; i < nfds; i++) | ||
| 318 | perf_event_read(&rings[i], &tmp_buf, &tmp_buf_sz); | ||
| 319 | } | ||
| 320 | free(tmp_buf); | ||
| 321 | |||
| 322 | if (json_output) | ||
| 323 | jsonw_end_array(json_wtr); | ||
| 324 | |||
| 325 | for (i = 0; i < nfds; i++) { | ||
| 326 | perf_event_unmap(rings[i].mem); | ||
| 327 | close(rings[i].fd); | ||
| 328 | } | ||
| 329 | free(pfds); | ||
| 330 | free(rings); | ||
| 331 | close(map_fd); | ||
| 332 | |||
| 333 | return 0; | ||
| 334 | |||
| 335 | err_close_fds_prev: | ||
| 336 | while (i--) { | ||
| 337 | perf_event_unmap(rings[i].mem); | ||
| 338 | err_close_fds_current: | ||
| 339 | close(rings[i].fd); | ||
| 340 | } | ||
| 341 | free(pfds); | ||
| 342 | err_free_rings: | ||
| 343 | free(rings); | ||
| 344 | err_close_map: | ||
| 345 | close(map_fd); | ||
| 346 | return -1; | ||
| 347 | } | ||
