linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alban Crequy <alban.crequy@gmail.com>
To: ast@kernel.org, daniel@iogearbox.net
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	alban@kinvolk.io, iago@kinvolk.io
Subject: [PATCH bpf-next v1 3/7] tools: bpftool: create map of maps
Date: Wed, 20 Mar 2019 18:33:28 +0100	[thread overview]
Message-ID: <20190320173332.18105-3-alban@kinvolk.io> (raw)
In-Reply-To: <20190320173332.18105-1-alban@kinvolk.io>

From: Alban Crequy <alban@kinvolk.io>

Before this patch, there was no way to fill attr.inner_map_fd, necessary
for array_of_maps or hash_of_maps.

This patch adds keyword 'innermap' to pass the innermap, either as an id
or as a pinned map.

Example of commands:

$ sudo bpftool map create /sys/fs/bpf/innermap type hash \
        key 8 value 8 entries 64 name innermap flags 1
$ sudo bpftool map create /sys/fs/bpf/outermap type hash_of_maps \
        innermap pinned /sys/fs/bpf/innermap key 64 value 4 \
        entries 64 name myoutermap flags 1
$ sudo bpftool map show pinned /sys/fs/bpf/outermap
47: hash_of_maps  name myoutermap  flags 0x1
	key 64B  value 4B  max_entries 64  memlock 12288B

Documentation and bash completion updated as well.

Signed-off-by: Alban Crequy <alban@kinvolk.io>

---

Previous version of this patch was sent while bpf-next was closed.
https://marc.info/?l=linux-kernel&m=155180393501258&w=2

Since then, the following changes were done:
- error management when calling map_parse_fd (review from Jakub)
- fix documentation and bash completion (review from Quentin)
---
 .../bpf/bpftool/Documentation/bpftool-map.rst |  9 ++-
 tools/bpf/bpftool/bash-completion/bpftool     |  9 +++
 tools/bpf/bpftool/map.c                       | 75 +++++++++++++++++--
 3 files changed, 83 insertions(+), 10 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-map.rst b/tools/bpf/bpftool/Documentation/bpftool-map.rst
index 5c984ffc9f01..b685641bfd74 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-map.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-map.rst
@@ -23,7 +23,7 @@ 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*]
+|		**entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*] [**innermap** MAP]
 |	**bpftool** **map dump**       *MAP*
 |	**bpftool** **map update**     *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
 |	**bpftool** **map lookup**     *MAP* [**key** *DATA*]
@@ -60,10 +60,15 @@ 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*]
+	**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE*  **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**dev** *NAME*] [**innermap** MAP]
 		  Create a new map with given parameters and pin it to *bpffs*
 		  as *FILE*.
 
+		  To create a map of type **array_of_maps** or
+		  **hash_of_maps**, the additional parameter **innermap** needs
+		  to reference an existing map with the type and size of the
+		  maps in the values.
+
 	**bpftool map dump**    *MAP*
 		  Dump all entries in a given *MAP*.
 
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index b803827d01e8..9e37de8bb227 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -461,6 +461,14 @@ _bpftool()
                             _sysfs_get_netdevs
                             return 0
                             ;;
+                        innermap)
+                            COMPREPLY+=( $( compgen -W "id pinned" -- "$cur" ) )
+                            return 0
+                            ;;
+                        id)
+                            _bpftool_get_map_ids
+                            return 0
+                            ;;
                         *)
                             _bpftool_once_attr 'type'
                             _bpftool_once_attr 'key'
@@ -469,6 +477,7 @@ _bpftool()
                             _bpftool_once_attr 'name'
                             _bpftool_once_attr 'flags'
                             _bpftool_once_attr 'dev'
+                            _bpftool_once_attr 'innermap'
                             return 0
                             ;;
                     esac
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 18f9bc3aed4f..a576f2a019be 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -1100,6 +1100,7 @@ static int do_create(int argc, char **argv)
 	struct bpf_create_map_attr attr = { NULL, };
 	const char *pinfile;
 	int err, fd;
+	int inner_map_fd = -1;
 
 	if (!REQ_ARGS(7))
 		return -1;
@@ -1108,48 +1109,75 @@ static int do_create(int argc, char **argv)
 	while (argc) {
 		if (is_prefix(*argv, "type")) {
 			NEXT_ARG();
-			if (!REQ_ARGS(1))
+			if (!REQ_ARGS(1)) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 
 			if (attr.map_type) {
 				p_err("map type already specified");
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
 			}
 
 			attr.map_type = map_type_from_str(*argv);
 			if ((int)attr.map_type < 0) {
 				p_err("unrecognized map type: %s", *argv);
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
 			}
 			NEXT_ARG();
 		} else if (is_prefix(*argv, "name")) {
 			NEXT_ARG();
-			if (!REQ_ARGS(1))
+			if (!REQ_ARGS(1)) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 			attr.name = GET_ARG();
 		} else if (is_prefix(*argv, "key")) {
 			if (parse_u32_arg(&argc, &argv, &attr.key_size,
-					  "key size"))
+					  "key size")) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 		} else if (is_prefix(*argv, "value")) {
 			if (parse_u32_arg(&argc, &argv, &attr.value_size,
-					  "value size"))
+					  "value size")) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 		} else if (is_prefix(*argv, "entries")) {
 			if (parse_u32_arg(&argc, &argv, &attr.max_entries,
-					  "max entries"))
+					  "max entries")) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 		} else if (is_prefix(*argv, "flags")) {
 			if (parse_u32_arg(&argc, &argv, &attr.map_flags,
-					  "flags"))
+					  "flags")) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 		} else if (is_prefix(*argv, "dev")) {
 			NEXT_ARG();
-			if (!REQ_ARGS(1))
+			if (!REQ_ARGS(1)) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
+			}
 
 			if (attr.map_ifindex) {
 				p_err("offload device already specified");
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
 			}
 
@@ -1157,28 +1185,59 @@ static int do_create(int argc, char **argv)
 			if (!attr.map_ifindex) {
 				p_err("unrecognized netdevice '%s': %s",
 				      *argv, strerror(errno));
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
 				return -1;
 			}
 			NEXT_ARG();
+		} else if (is_prefix(*argv, "innermap")) {
+			NEXT_ARG();
+			if (!REQ_ARGS(1)) {
+				if (inner_map_fd != -1)
+					close(inner_map_fd);
+				return -1;
+			}
+
+			if (inner_map_fd != -1) {
+				close(inner_map_fd);
+				p_err("innermap already specified");
+				return -1;
+			}
+
+			inner_map_fd = map_parse_fd(&argc, &argv);
+			if (inner_map_fd < 0)
+				return -1;
 		} else {
 			p_err("unknown arg %s", *argv);
+			if (inner_map_fd != -1)
+				close(inner_map_fd);
 			return -1;
 		}
 	}
 
 	if (!attr.name) {
 		p_err("map name not specified");
+		if (inner_map_fd != -1)
+			close(inner_map_fd);
 		return -1;
 	}
 
 	set_max_rlimit();
 
+	if (inner_map_fd != -1)
+		attr.inner_map_fd = inner_map_fd;
+
 	fd = bpf_create_map_xattr(&attr);
 	if (fd < 0) {
 		p_err("map create failed: %s", strerror(errno));
+		if (inner_map_fd != -1)
+			close(inner_map_fd);
 		return -1;
 	}
 
+	if (inner_map_fd != -1)
+		close(inner_map_fd);
+
 	err = do_pin_fd(fd, pinfile);
 	close(fd);
 	if (err)
@@ -1243,7 +1302,7 @@ static int do_help(int argc, char **argv)
 		"Usage: %s %s { show | list }   [MAP]\n"
 		"       %s %s create     FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
 		"                              entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
-		"                              [dev NAME]\n"
+		"                              [dev NAME] [innermap MAP]\n"
 		"       %s %s dump       MAP\n"
 		"       %s %s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
 		"       %s %s lookup     MAP [key DATA]\n"
-- 
2.20.1


  parent reply	other threads:[~2019-03-20 17:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-20 17:33 [PATCH bpf-next v1 1/7] tools: bpftool: fix infinite loop Alban Crequy
2019-03-20 17:33 ` [PATCH bpf-next v1 2/7] tools: bpftool: fix error message on invalid argument count Alban Crequy
2019-03-20 21:02   ` Jakub Kicinski
2019-03-20 17:33 ` Alban Crequy [this message]
2019-03-20 19:00   ` [PATCH bpf-next v1 3/7] tools: bpftool: create map of maps Quentin Monnet
2019-03-20 17:33 ` [PATCH bpf-next v1 4/7] tools: bpftool: implement map exec command Alban Crequy
2019-03-20 19:01   ` Quentin Monnet
2019-03-20 21:23   ` Jakub Kicinski
2019-03-21  9:57     ` Alban Crequy
2019-03-21 20:16       ` Jakub Kicinski
2019-03-20 17:33 ` [PATCH bpf-next v1 5/7] tools: bpftool: support loading map by fd from parent process Alban Crequy
2019-03-20 19:05   ` Quentin Monnet
2019-03-20 17:33 ` [PATCH bpf-next v1 6/7] tools: bpftool: fix bpffs mount when pinning subdirectories Alban Crequy
2019-03-20 19:01   ` Quentin Monnet
2019-03-20 17:33 ` [PATCH bpf-next v1 7/7] tools: bpftool: add error message on map pinning failure Alban Crequy
2019-03-20 19:01   ` Quentin Monnet
2019-03-20 20:54 ` [PATCH bpf-next v1 1/7] tools: bpftool: fix infinite loop Jakub Kicinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190320173332.18105-3-alban@kinvolk.io \
    --to=alban.crequy@gmail.com \
    --cc=alban@kinvolk.io \
    --cc=ast@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=iago@kinvolk.io \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).