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

Commit 0b592b5a authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Alexei Starovoitov
Browse files

tools: bpftool: add map create command



Add a way of creating maps from user space.  The command takes
as parameters most of the attributes of the map creation system
call command.  After map is created its pinned to bpffs.  This makes
it possible to easily and dynamically (without rebuilding programs)
test various corner cases related to map creation.

Map type names are taken from bpftool's array used for printing.
In general these days we try to make use of libbpf type names, but
there are no map type names in libbpf as of today.

As with most features I add the motivation is testing (offloads) :)

Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 2f1d774f
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -15,13 +15,15 @@ SYNOPSIS
	*OPTIONS* := { { **-j** | **--json** } [{ **-p** | **--pretty** }] | { **-f** | **--bpffs** } }

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

MAP COMMANDS
=============

|	**bpftool** **map { show | list }**   [*MAP*]
|	**bpftool** **map create**     *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
|		**entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
|	**bpftool** **map dump**       *MAP*
|	**bpftool** **map update**     *MAP*  **key** *DATA*   **value** *VALUE* [*UPDATE_FLAGS*]
|	**bpftool** **map lookup**     *MAP*  **key** *DATA*
@@ -36,6 +38,11 @@ MAP COMMANDS
|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
|	*VALUE* := { *DATA* | *MAP* | *PROG* }
|	*UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
|	*TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
|		| **percpu_array** | **stack_trace** | **cgroup_array** | **lru_hash**
|		| **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps**
|		| **devmap** | **sockmap** | **cpumap** | **xskmap** | **sockhash**
|		| **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage** }

DESCRIPTION
===========
@@ -47,6 +54,10 @@ DESCRIPTION
		  Output will start with map ID followed by map type and
		  zero or more named attributes (depending on kernel version).

	**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE*  **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*]
		  Create a new map with given parameters and pin it to *bpffs*
		  as *FILE*.

	**bpftool map dump**    *MAP*
		  Dump all entries in a given *MAP*.

+2 −2
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ SYNOPSIS
	| { **-j** | **--json** } [{ **-p** | **--pretty** }] }

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

	*PROG-COMMANDS* := { **show** | **list** | **dump jited** | **dump xlated** | **pin**
	| **load** | **attach** | **detach** | **help** }
+37 −1
Original line number Diff line number Diff line
@@ -387,6 +387,42 @@ _bpftool()
                            ;;
                    esac
                    ;;
                create)
                    case $prev in
                        $command)
                            _filedir
                            return 0
                            ;;
                        type)
                            COMPREPLY=( $( compgen -W 'hash array prog_array \
                                perf_event_array percpu_hash percpu_array \
                                stack_trace cgroup_array lru_hash \
                                lru_percpu_hash lpm_trie array_of_maps \
                                hash_of_maps devmap sockmap cpumap xskmap \
                                sockhash cgroup_storage reuseport_sockarray \
                                percpu_cgroup_storage' -- \
                                                   "$cur" ) )
                            return 0
                            ;;
                        key|value|flags|name|entries)
                            return 0
                            ;;
                        dev)
                            _sysfs_get_netdevs
                            return 0
                            ;;
                        *)
                            _bpftool_once_attr 'type'
                            _bpftool_once_attr 'key'
                            _bpftool_once_attr 'value'
                            _bpftool_once_attr 'entries'
                            _bpftool_once_attr 'name'
                            _bpftool_once_attr 'flags'
                            _bpftool_once_attr 'dev'
                            return 0
                            ;;
                    esac
                    ;;
                lookup|getnext|delete)
                    case $prev in
                        $command)
@@ -500,7 +536,7 @@ _bpftool()
                *)
                    [[ $prev == $object ]] && \
                        COMPREPLY=( $( compgen -W 'delete dump getnext help \
                            lookup pin event_pipe show list update' -- \
                            lookup pin event_pipe show list update create' -- \
                            "$cur" ) )
                    ;;
            esac
+21 −0
Original line number Diff line number Diff line
@@ -618,3 +618,24 @@ void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
		jsonw_string_field(json_wtr, "ifname", name);
	jsonw_end_object(json_wtr);
}

int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
{
	char *endptr;

	NEXT_ARGP();

	if (*val) {
		p_err("%s already specified", what);
		return -1;
	}

	*val = strtoul(**argv, &endptr, 0);
	if (*endptr) {
		p_err("can't parse %s as %s", **argv, what);
		return -1;
	}
	NEXT_ARGP();

	return 0;
}
+1 −0
Original line number Diff line number Diff line
@@ -139,6 +139,7 @@ int do_cgroup(int argc, char **arg);
int do_perf(int argc, char **arg);
int do_net(int argc, char **arg);

int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what);
int prog_parse_fd(int *argc, char ***argv);
int map_parse_fd(int *argc, char ***argv);
int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len);
Loading