Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f412eed9 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Daniel Borkmann
Browse files

tools: bpftool: add simple perf event output reader



Users of BPF sooner or later discover perf_event_output() helpers
and BPF_MAP_TYPE_PERF_EVENT_ARRAY.  Dumping this array type is
not possible, however, we can add simple reading of perf events.
Create a new event_pipe subcommand for maps, this sub command
will only work with BPF_MAP_TYPE_PERF_EVENT_ARRAY maps.

Parts of the code from samples/bpf/trace_output_user.c.

Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Acked-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent e64d5256
Loading
Loading
Loading
Loading
+23 −6
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@ MAP COMMANDS
|	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
|	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
|	**bpftool** **map delete**     *MAP*  **key** *DATA*
|	**bpftool** **map delete**     *MAP*  **key** *DATA*
|	**bpftool** **map pin**        *MAP*  *FILE*
|	**bpftool** **map pin**        *MAP*  *FILE*
|	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
|	**bpftool** **map help**
|	**bpftool** **map help**
|
|
|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -76,6 +77,22 @@ DESCRIPTION


		  Note: *FILE* must be located in *bpffs* mount.
		  Note: *FILE* must be located in *bpffs* mount.


	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
		  Read events from a BPF_MAP_TYPE_PERF_EVENT_ARRAY map.

		  Install perf rings into a perf event array map and dump
		  output of any bpf_perf_event_output() call in the kernel.
		  By default read the number of CPUs on the system and
		  install perf ring for each CPU in the corresponding index
		  in the array.

		  If **cpu** and **index** are specified, install perf ring
		  for given **cpu** at **index** in the array (single ring).

		  Note that installing a perf ring into an array will silently
		  replace any existing ring.  Any other application will stop
		  receiving events if it installed its rings earlier.

	**bpftool map help**
	**bpftool map help**
		  Print short help message.
		  Print short help message.


+1 −1
Original line number Original line Diff line number Diff line
@@ -23,7 +23,7 @@ SYNOPSIS


	*MAP-COMMANDS* :=
	*MAP-COMMANDS* :=
	{ **show** | **list** | **dump** | **update** | **lookup** | **getnext** | **delete**
	{ **show** | **list** | **dump** | **update** | **lookup** | **getnext** | **delete**
	| **pin** | **help** }
	| **pin** | **event_pipe** | **help** }


	*PROG-COMMANDS* := { **show** | **list** | **dump jited** | **dump xlated** | **pin**
	*PROG-COMMANDS* := { **show** | **list** | **dump jited** | **dump xlated** | **pin**
	| **load** | **help** }
	| **load** | **help** }
+6 −1
Original line number Original line Diff line number Diff line
@@ -39,7 +39,12 @@ CC = gcc


CFLAGS += -O2
CFLAGS += -O2
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ -I$(srctree)/tools/include/uapi -I$(srctree)/tools/include -I$(srctree)/tools/lib/bpf -I$(srctree)/kernel/bpf/
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
	-I$(srctree)/kernel/bpf/ \
	-I$(srctree)/tools/include \
	-I$(srctree)/tools/include/uapi \
	-I$(srctree)/tools/lib/bpf \
	-I$(srctree)/tools/perf
CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
CFLAGS += -DBPFTOOL_VERSION='"$(BPFTOOL_VERSION)"'
LIBS = -lelf -lbfd -lopcodes $(LIBBPF)
LIBS = -lelf -lbfd -lopcodes $(LIBBPF)


+34 −2
Original line number Original line Diff line number Diff line
# bpftool(8) bash completion                               -*- shell-script -*-
# bpftool(8) bash completion                               -*- shell-script -*-
#
#
# Copyright (C) 2017 Netronome Systems, Inc.
# Copyright (C) 2017-2018 Netronome Systems, Inc.
#
#
# This software is dual licensed under the GNU General License
# This software is dual licensed under the GNU General License
# Version 2, June 1991 as shown in the file COPYING in the top-level
# Version 2, June 1991 as shown in the file COPYING in the top-level
@@ -79,6 +79,14 @@ _bpftool_get_map_ids()
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
}
}


_bpftool_get_perf_map_ids()
{
    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
        command grep -C2 perf_event_array | \
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
}


_bpftool_get_prog_ids()
_bpftool_get_prog_ids()
{
{
    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
@@ -359,10 +367,34 @@ _bpftool()
                    fi
                    fi
                    return 0
                    return 0
                    ;;
                    ;;
                event_pipe)
                    case $prev in
                        $command)
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
                            return 0
                            ;;
                        id)
                            _bpftool_get_perf_map_ids
                            return 0
                            ;;
                        cpu)
                            return 0
                            ;;
                        index)
                            return 0
                            ;;
                        *)
                            _bpftool_once_attr 'cpu'
                            _bpftool_once_attr 'index'
                            return 0
                            ;;
                    esac
                    ;;
                *)
                *)
                    [[ $prev == $object ]] && \
                    [[ $prev == $object ]] && \
                        COMPREPLY=( $( compgen -W 'delete dump getnext help \
                        COMPREPLY=( $( compgen -W 'delete dump getnext help \
                            lookup pin show list update' -- "$cur" ) )
                            lookup pin event_pipe show list update' -- \
                            "$cur" ) )
                    ;;
                    ;;
            esac
            esac
            ;;
            ;;
+19 −0
Original line number Original line Diff line number Diff line
@@ -331,6 +331,16 @@ char *get_fdinfo(int fd, const char *key)
	return NULL;
	return NULL;
}
}


void print_data_json(uint8_t *data, size_t len)
{
	unsigned int i;

	jsonw_start_array(json_wtr);
	for (i = 0; i < len; i++)
		jsonw_printf(json_wtr, "%d", data[i]);
	jsonw_end_array(json_wtr);
}

void print_hex_data_json(uint8_t *data, size_t len)
void print_hex_data_json(uint8_t *data, size_t len)
{
{
	unsigned int i;
	unsigned int i;
@@ -421,6 +431,15 @@ void delete_pinned_obj_table(struct pinned_obj_table *tab)
	}
	}
}
}


unsigned int get_page_size(void)
{
	static int result;

	if (!result)
		result = getpagesize();
	return result;
}

unsigned int get_possible_cpus(void)
unsigned int get_possible_cpus(void)
{
{
	static unsigned int result;
	static unsigned int result;
Loading