Skip to contentSkip to conversation

bpftool: support queue and stack

PATCH Applied Stanislav Fomichev 3 participants 10 messages Jan 16, 2019 – Jan 17, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover bpftool: support queue and stack

This patch series add support for queue/stack manipulations.

It goes like this:

#1 and #2 add support for queue/stack in existing 'update' and 'lookup' commands by permitting empty keys.
#3 make sure we don't print empty keys for queue/stack.
#4 adds peek command, it's essentially an alias for 'lookup'. #5 adds push/enqueue commands, an alias to 'update'.
#6 adds pop/dequeue commands that use bpf_map_lookup_and_delete_elem. #7 adds bash completions

v2:
* removed unneeded jsonw_null from patch #6
* improved bash completions (and moved them into separate patch #7)

Stanislav Fomichev (7):
bpftool: make key and value optional in update command
bpftool: make key optional in lookup command
bpftool: don't print empty key/value for maps
bpftool: add peek command
bpftool: add push and enqueue commands
bpftool: add pop and dequeue commands
bpftool: add bash completion for peek/push/enqueue/pop/dequeue

 .../bpf/bpftool/Documentation/bpftool-map.rst |  28 ++-
 tools/bpf/bpftool/bash-completion/bpftool     |  91 +++++--
 tools/bpf/bpftool/map.c                       | 225 ++++++++++++------
 3 files changed, 254 insertions(+), 90 deletions(-)
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#1 lore raw

PATCH v2 1/7 bpftool: make key and value optional in update command

Bpftool expects both key and value for 'update' operations. For some map types, key should not be specified. Support updating those map types.

Before:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3
Error: did not find key

After:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3

---
 .../bpf/bpftool/Documentation/bpftool-map.rst |  4 +--
 tools/bpf/bpftool/map.c                       | 36 +++++++++++++++----
 2 files changed, 31 insertions(+), 9 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +2 -2
index 64b001b4f777..1f1dfe06e66d 100644
@@ -25,7 +25,7 @@ MAP COMMANDS
 |	**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 update**     *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
 |	**bpftool** **map lookup**     *MAP*  **key** *DATA*
 |	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
 |	**bpftool** **map delete**     *MAP*  **key** *DATA*
@@ -62,7 +62,7 @@ DESCRIPTION
 	**bpftool map dump**    *MAP*
 		  Dump all entries in a given *MAP*.
 
-	**bpftool map update**  *MAP*  **key** *DATA*   **value** *VALUE* [*UPDATE_FLAGS*]
+	**bpftool map update**  *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
 		  Update map entry for a given *KEY*.
 
 		  *UPDATE_FLAGS* can be one of: **any** update existing entry
tools/bpf/bpftool/map.c +29 -7
index 2037e3dc864b..61453a1ba6e1 100644
@@ -779,6 +779,32 @@ static int do_dump(int argc, char **argv)
 	return err;
 }
 
+static int alloc_key_value(struct bpf_map_info *info, void **key, void **value)
+{
+	*key = NULL;
+	*value = NULL;
+
+	if (info->key_size) {
+		*key = malloc(info->key_size);
+		if (!*key) {
+			p_err("key mem alloc failed");
+			return -1;
+		}
+	}
+
+	if (info->value_size) {
+		*value = alloc_value(info);
+		if (!*value) {
+			p_err("value mem alloc failed");
+			free(*key);
+			*key = NULL;
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
 static int do_update(int argc, char **argv)
 {
 	struct bpf_map_info info = {};
@@ -795,13 +821,9 @@ static int do_update(int argc, char **argv)
 	if (fd < 0)
 		return -1;
 
-	key = malloc(info.key_size);
-	value = alloc_value(&info);
-	if (!key || !value) {
-		p_err("mem alloc failed");
-		err = -1;
+	err = alloc_key_value(&info, &key, &value);
+	if (err)
 		goto exit_free;
-	}
 
 	err = parse_elem(argv, &info, key, value, info.key_size,
 			 info.value_size, &flags, &value_fd);
@@ -1135,7 +1157,7 @@ static int do_help(int argc, char **argv)
 		"                              entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
 		"                              [dev NAME]\n"
 		"       %s %s dump       MAP\n"
-		"       %s %s update     MAP  key DATA value VALUE [UPDATE_FLAGS]\n"
+		"       %s %s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
 		"       %s %s lookup     MAP  key DATA\n"
 		"       %s %s getnext    MAP [key DATA]\n"
 		"       %s %s delete     MAP  key DATA\n"
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#2 lore raw

PATCH v2 2/7 bpftool: make key optional in lookup command

In reply to Stanislav Fomichev

Bpftool expects key for 'lookup' operations. For some map types, key should not be specified. Support looking up those map types.

Before:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map lookup pinned /sys/fs/bpf/q
Error: did not find key

After:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map lookup pinned /sys/fs/bpf/q
key: value: 00 01 02 03

---
 tools/bpf/bpftool/Documentation/bpftool-map.rst |  4 ++--
 tools/bpf/bpftool/map.c                         | 10 +++-------
 2 files changed, 5 insertions(+), 9 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +2 -2
index 1f1dfe06e66d..f34cace771bd 100644
@@ -26,7 +26,7 @@ MAP COMMANDS
 |		**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*
+|	**bpftool** **map lookup**     *MAP* [**key** *DATA*]
 |	**bpftool** **map getnext**    *MAP* [**key** *DATA*]
 |	**bpftool** **map delete**     *MAP*  **key** *DATA*
 |	**bpftool** **map pin**        *MAP*  *FILE*
@@ -75,7 +75,7 @@ DESCRIPTION
 		  the bytes are parsed as decimal values, unless a "0x" prefix
 		  (for hexadecimal) or a "0" prefix (for octal) is provided.
 
-	**bpftool map lookup**  *MAP*  **key** *DATA*
+	**bpftool map lookup**  *MAP* [**key** *DATA*]
 		  Lookup **key** in the map.
 
 	**bpftool map getnext** *MAP* [**key** *DATA*]
tools/bpf/bpftool/map.c +3 -7
index 61453a1ba6e1..4256842f9664 100644
@@ -865,13 +865,9 @@ static int do_lookup(int argc, char **argv)
 	if (fd < 0)
 		return -1;
 
-	key = malloc(info.key_size);
-	value = alloc_value(&info);
-	if (!key || !value) {
-		p_err("mem alloc failed");
-		err = -1;
+	err = alloc_key_value(&info, &key, &value);
+	if (err)
 		goto exit_free;
-	}
 
 	err = parse_elem(argv, &info, key, NULL, info.key_size, 0, NULL, NULL);
 	if (err)
@@ -1158,7 +1154,7 @@ static int do_help(int argc, char **argv)
 		"                              [dev NAME]\n"
 		"       %s %s dump       MAP\n"
 		"       %s %s update     MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
-		"       %s %s lookup     MAP  key DATA\n"
+		"       %s %s lookup     MAP [key DATA]\n"
 		"       %s %s getnext    MAP [key DATA]\n"
 		"       %s %s delete     MAP  key DATA\n"
 		"       %s %s pin        MAP  FILE\n"
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#3 lore raw

PATCH v2 3/7 bpftool: don't print empty key/value for maps

In reply to Stanislav Fomichev

When doing dump or lookup, don't print key if key_size == 0 or value if value_size == 0. The initial usecase is queue and stack, where we have only values.

This is for regular output only, json still has all the fields.

Before:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map lookup pinned /sys/fs/bpf/q
key: value: 00 01 02 03

After:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map lookup pinned /sys/fs/bpf/q
value: 00 01 02 03

---
 tools/bpf/bpftool/map.c | 47 ++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 19 deletions(-)
tools/bpf/bpftool/map.c +28 -19
index 4256842f9664..3f599399913b 100644
@@ -285,16 +285,21 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
 		single_line = info->key_size + info->value_size <= 24 &&
 			!break_names;
 
-		printf("key:%c", break_names ? '\n' : ' ');
-		fprint_hex(stdout, key, info->key_size, " ");
+		if (info->key_size) {
+			printf("key:%c", break_names ? '\n' : ' ');
+			fprint_hex(stdout, key, info->key_size, " ");
 
-		printf(single_line ? "  " : "\n");
+			printf(single_line ? "  " : "\n");
+		}
 
-		printf("value:%c", break_names ? '\n' : ' ');
-		if (value)
-			fprint_hex(stdout, value, info->value_size, " ");
-		else
-			printf("<no entry>");
+		if (info->value_size) {
+			printf("value:%c", break_names ? '\n' : ' ');
+			if (value)
+				fprint_hex(stdout, value, info->value_size,
+					   " ");
+			else
+				printf("<no entry>");
+		}
 
 		printf("\n");
 	} else {
@@ -303,19 +308,23 @@ static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
 		n = get_possible_cpus();
 		step = round_up(info->value_size, 8);
 
-		printf("key:\n");
-		fprint_hex(stdout, key, info->key_size, " ");
-		printf("\n");
-		for (i = 0; i < n; i++) {
-			printf("value (CPU %02d):%c",
-			       i, info->value_size > 16 ? '\n' : ' ');
-			if (value)
-				fprint_hex(stdout, value + i * step,
-					   info->value_size, " ");
-			else
-				printf("<no entry>");
+		if (info->key_size) {
+			printf("key:\n");
+			fprint_hex(stdout, key, info->key_size, " ");
 			printf("\n");
 		}
+		if (info->value_size) {
+			for (i = 0; i < n; i++) {
+				printf("value (CPU %02d):%c",
+				       i, info->value_size > 16 ? '\n' : ' ');
+				if (value)
+					fprint_hex(stdout, value + i * step,
+						   info->value_size, " ");
+				else
+					printf("<no entry>");
+				printf("\n");
+			}
+		}
 	}
 }
 
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#4 lore raw

PATCH v2 4/7 bpftool: add peek command

In reply to Stanislav Fomichev

This is intended to be used with queues and stacks and be more user-friendly than 'lookup' without key/value.

Example:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map update pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map peek pinned /sys/fs/bpf/q
value: 00 01 02 03

---
 tools/bpf/bpftool/Documentation/bpftool-map.rst | 4 ++++
 tools/bpf/bpftool/map.c                         | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +4 -0
index f34cace771bd..b79da683da88 100644
@@ -31,6 +31,7 @@ MAP COMMANDS
 |	**bpftool** **map delete**     *MAP*  **key** *DATA*
 |	**bpftool** **map pin**        *MAP*  *FILE*
 |	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
+|	**bpftool** **map peek**       *MAP*
 |	**bpftool** **map help**
 |
 |	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -107,6 +108,9 @@ DESCRIPTION
 		  replace any existing ring.  Any other application will stop
 		  receiving events if it installed its rings earlier.
 
+	**bpftool map peek**  *MAP*
+		  Peek next **value** in the queue or stack.
+
 	**bpftool map help**
 		  Print short help message.
 
tools/bpf/bpftool/map.c +3 -1
index 3f599399913b..d7344fcd2089 100644
@@ -1168,6 +1168,7 @@ static int do_help(int argc, char **argv)
 		"       %s %s delete     MAP  key DATA\n"
 		"       %s %s pin        MAP  FILE\n"
 		"       %s %s event_pipe MAP [cpu N index M]\n"
+		"       %s %s peek       MAP\n"
 		"       %s %s help\n"
 		"\n"
 		"       " HELP_SPEC_MAP "\n"
@@ -1185,7 +1186,7 @@ static int do_help(int argc, char **argv)
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
-		bin_name, argv[-2]);
+		bin_name, argv[-2], bin_name, argv[-2]);
 
 	return 0;
 }
@@ -1202,6 +1203,7 @@ static const struct cmd cmds[] = {
 	{ "pin",	do_pin },
 	{ "event_pipe",	do_event_pipe },
 	{ "create",	do_create },
+	{ "peek",	do_lookup },
 	{ 0 }
 };
 
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#5 lore raw

PATCH v2 5/7 bpftool: add push and enqueue commands

In reply to Stanislav Fomichev

This is intended to be used with queues and stacks and be more user-friendly than 'update' without the key.

Example:
bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map push pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map peek pinned /sys/fs/bpf/q
value: 00 01 02 03

bpftool map create /sys/fs/bpf/s type stack value 4 entries 10 name s bpftool map enqueue pinned /sys/fs/bpf/s value 0 1 2 3 bpftool map peek pinned /sys/fs/bpf/s
value: 00 01 02 03

---
 tools/bpf/bpftool/Documentation/bpftool-map.rst | 8 ++++++++
 tools/bpf/bpftool/map.c                         | 7 ++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +8 -0
index b79da683da88..435a79d2eed5 100644
@@ -32,6 +32,8 @@ MAP COMMANDS
 |	**bpftool** **map pin**        *MAP*  *FILE*
 |	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
 |	**bpftool** **map peek**       *MAP*
+|	**bpftool** **map push**       *MAP* **value** *VALUE*
+|	**bpftool** **map enqueue**    *MAP* **value** *VALUE*
 |	**bpftool** **map help**
 |
 |	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -111,6 +113,12 @@ DESCRIPTION
 	**bpftool map peek**  *MAP*
 		  Peek next **value** in the queue or stack.
 
+	**bpftool map push**  *MAP* **value** *VALUE*
+		  Push **value** onto the stack.
+
+	**bpftool map enqueue**  *MAP* **value** *VALUE*
+		  Enqueue **value** into the queue.
+
 	**bpftool map help**
 		  Print short help message.
 
tools/bpf/bpftool/map.c +6 -1
index d7344fcd2089..6b5fcbe2d9d4 100644
@@ -1169,6 +1169,8 @@ static int do_help(int argc, char **argv)
 		"       %s %s pin        MAP  FILE\n"
 		"       %s %s event_pipe MAP [cpu N index M]\n"
 		"       %s %s peek       MAP\n"
+		"       %s %s push       MAP value VALUE\n"
+		"       %s %s enqueue    MAP value VALUE\n"
 		"       %s %s help\n"
 		"\n"
 		"       " HELP_SPEC_MAP "\n"
@@ -1186,7 +1188,8 @@ static int do_help(int argc, char **argv)
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
-		bin_name, argv[-2], bin_name, argv[-2]);
+		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
+		bin_name, argv[-2]);
 
 	return 0;
 }
@@ -1204,6 +1207,8 @@ static const struct cmd cmds[] = {
 	{ "event_pipe",	do_event_pipe },
 	{ "create",	do_create },
 	{ "peek",	do_lookup },
+	{ "push",	do_update },
+	{ "enqueue",	do_update },
 	{ 0 }
 };
 
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#6 lore raw

PATCH v2 6/7 bpftool: add pop and dequeue commands

In reply to Stanislav Fomichev

This is intended to be used with queues and stacks, it pops and prints the last element via bpf_map_lookup_and_delete_elem.

Example:

bpftool map create /sys/fs/bpf/q type queue value 4 entries 10 name q bpftool map push pinned /sys/fs/bpf/q value 0 1 2 3 bpftool map pop pinned /sys/fs/bpf/q
value: 00 01 02 03
bpftool map pop pinned /sys/fs/bpf/q
Error: empty map

bpftool map create /sys/fs/bpf/s type stack value 4 entries 10 name s bpftool map enqueue pinned /sys/fs/bpf/s value 0 1 2 3 bpftool map dequeue pinned /sys/fs/bpf/s
value: 00 01 02 03
bpftool map dequeue pinned /sys/fs/bpf/s
Error: empty map

---
 .../bpf/bpftool/Documentation/bpftool-map.rst |   8 ++
 tools/bpf/bpftool/map.c                       | 125 +++++++++++++-----
 2 files changed, 98 insertions(+), 35 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +8 -0
index 435a79d2eed5..0584c31d3fe2 100644
@@ -33,7 +33,9 @@ MAP COMMANDS
 |	**bpftool** **map event_pipe** *MAP* [**cpu** *N* **index** *M*]
 |	**bpftool** **map peek**       *MAP*
 |	**bpftool** **map push**       *MAP* **value** *VALUE*
+|	**bpftool** **map pop**        *MAP*
 |	**bpftool** **map enqueue**    *MAP* **value** *VALUE*
+|	**bpftool** **map dequeue**    *MAP*
 |	**bpftool** **map help**
 |
 |	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
@@ -116,9 +118,15 @@ DESCRIPTION
 	**bpftool map push**  *MAP* **value** *VALUE*
 		  Push **value** onto the stack.
 
+	**bpftool map pop**  *MAP*
+		  Pop and print **value** from the stack.
+
 	**bpftool map enqueue**  *MAP* **value** *VALUE*
 		  Enqueue **value** into the queue.
 
+	**bpftool map dequeue**  *MAP*
+		  Dequeue and print **value** from the queue.
+
 	**bpftool map help**
 		  Print short help message.
 
tools/bpf/bpftool/map.c +90 -35

171 lines not on this page · raw message

Signature · 1 line
2.20.1.97.g81188d93c3-goog
#7 lore raw

PATCH v2 7/7 bpftool: add bash completion for peek/push/enqueue/pop/dequeue

In reply to Stanislav Fomichev
bpftool map peek id <TAB>      - suggests only queue and stack map ids
bpftool map pop id <TAB>       - suggests only stack map ids
bpftool map dequeue id <TAB>   - suggests only queue map ids

bpftool map push id <TAB> - suggests only stack map ids
bpftool map enqueue id <TAB> - suggests only queue map ids

bpftool map push id 1 <TAB> - suggests 'value', not 'key'
bpftool map enqueue id 2 <TAB> - suggests 'value', not 'key'

bpftool map update id <stack/queue type> - suggests 'value', not 'key' bpftool map lookup id <stack/queue type> - suggests nothing

---
 tools/bpf/bpftool/bash-completion/bpftool | 91 ++++++++++++++++++-----
 1 file changed, 73 insertions(+), 18 deletions(-)
tools/bpf/bpftool/bash-completion/bpftool +73 -18
index e4e4fab1b8c7..47143d793b33 100644
@@ -50,14 +50,15 @@ _bpftool_get_map_ids()
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
-_bpftool_get_perf_map_ids()
+# Takes map type and adds matching map ids to the list of suggestions.
+_bpftool_get_map_ids_for_type()
 {
+    local type="$1"
     COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
-        command grep -C2 perf_event_array | \
+        command grep -C2 "$type" | \
         command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
 }
 
-
 _bpftool_get_prog_ids()
 {
     COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
@@ -99,15 +100,25 @@ _sysfs_get_netdevs()
         "$cur" ) )
 }
 
-# For bpftool map update: retrieve type of the map to update.
-_bpftool_map_update_map_type()
+# Retrieve type of the map that we are operating on.
+_bpftool_map_guess_map_type()
 {
     local keyword ref
     for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
-        if [[ ${words[$((idx-2))]} == "update" ]]; then
-            keyword=${words[$((idx-1))]}
-            ref=${words[$((idx))]}
-        fi
+        case "${words[$((idx-2))]}" in
+            lookup|update)
+                keyword=${words[$((idx-1))]}
+                ref=${words[$((idx))]}
+                ;;
+            push)
+                printf "stack"
+                return 0
+                ;;
+            enqueue)
+                printf "queue"
+                return 0
+                ;;
+        esac
     done
     [[ -z $ref ]] && return 0
 
@@ -119,6 +130,8 @@ _bpftool_map_update_map_type()
 
 _bpftool_map_update_get_id()
 {
+    local command="$1"
+
     # Is it the map to update, or a map to insert into the map to update?
     # Search for "value" keyword.
     local idx value
@@ -128,11 +141,24 @@ _bpftool_map_update_get_id()
             break
         fi
     done
-    [[ $value -eq 0 ]] && _bpftool_get_map_ids && return 0
+    if [[ $value -eq 0 ]]; then
+        case "$command" in
+            push)
+                _bpftool_get_map_ids_for_type stack
+                ;;
+            enqueue)
+                _bpftool_get_map_ids_for_type queue
+                ;;
+            *)
+                _bpftool_get_map_ids
+                ;;
+        esac
+        return 0
+    fi
 
     # Id to complete is for a value. It can be either prog id or map id. This
     # depends on the type of the map to update.
-    local type=$(_bpftool_map_update_map_type)
+    local type=$(_bpftool_map_guess_map_type)
     case $type in
         array_of_maps|hash_of_maps)
             _bpftool_get_map_ids
@@ -382,14 +408,28 @@ _bpftool()
         map)
             local MAP_TYPE='id pinned'
             case $command in
-                show|list|dump)
+                show|list|dump|peek|pop|dequeue)
                     case $prev in
                         $command)
                             COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
                             return 0
                             ;;
                         id)
-                            _bpftool_get_map_ids
+                            case "$command" in
+                                peek)
+                                    _bpftool_get_map_ids_for_type stack
+                                    _bpftool_get_map_ids_for_type queue
+                                    ;;
+                                pop)
+                                    _bpftool_get_map_ids_for_type stack
+                                    ;;
+                                dequeue)
+                                    _bpftool_get_map_ids_for_type queue
+                                    ;;
+                                *)
+                                    _bpftool_get_map_ids
+                                    ;;
+                            esac
                             return 0
                             ;;
                         *)
@@ -447,19 +487,25 @@ _bpftool()
                             COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
                             ;;
                         *)
+                            case $(_bpftool_map_guess_map_type) in
+                                queue|stack)
+                                    return 0
+                                    ;;
+                            esac
+
                             _bpftool_once_attr 'key'
                             return 0
                             ;;
                     esac
                     ;;
-                update)
+                update|push|enqueue)
                     case $prev in
                         $command)
                             COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
                             return 0
                             ;;
                         id)
-                            _bpftool_map_update_get_id
+                            _bpftool_map_update_get_id $command
                             return 0
                             ;;
                         key)
@@ -468,7 +514,7 @@ _bpftool()
                         value)
                             # We can have bytes, or references to a prog or a
                             # map, depending on the type of the map to update.
-                            case $(_bpftool_map_update_map_type) in
+                            case "$(_bpftool_map_guess_map_type)" in
                                 array_of_maps|hash_of_maps)
                                     local MAP_TYPE='id pinned'
                                     COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
@@ -490,6 +536,13 @@ _bpftool()
                             return 0
                             ;;
                         *)
+                            case $(_bpftool_map_guess_map_type) in
+                                queue|stack)
+                                    _bpftool_once_attr 'value'
+                                    return 0;
+                                    ;;
+                            esac
+
                             _bpftool_once_attr 'key'
                             local UPDATE_FLAGS='any exist noexist'
                             for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
@@ -508,6 +561,7 @@ _bpftool()
                                     return 0
                                 fi
                             done
+
                             return 0
                             ;;
                     esac
@@ -527,7 +581,7 @@ _bpftool()
                             return 0
                             ;;
                         id)
-                            _bpftool_get_perf_map_ids
+                            _bpftool_get_map_ids_for_type perf_event_array
                             return 0
                             ;;
                         cpu)
@@ -546,7 +600,8 @@ _bpftool()
                 *)
                     [[ $prev == $object ]] && \
                         COMPREPLY=( $( compgen -W 'delete dump getnext help \
-                            lookup pin event_pipe show list update create' -- \
+                            lookup pin event_pipe show list update create \
+                            peek push enqueue pop dequeue' -- \
                             "$cur" ) )
                     ;;
             esac
Signature · 1 line
2.20.1.97.g81188d93c3-goog
Reviewed-by on the v2 series
#8 lore raw
In reply to Stanislav Fomichev
Quoted 12 lines · Stanislav Fomichev

This patch series add support for queue/stack manipulations.

It goes like this:

#1 and #2 add support for queue/stack in existing 'update' and 'lookup' commands by permitting empty keys.
#3 make sure we don't print empty keys for queue/stack.
#4 adds peek command, it's essentially an alias for 'lookup'. #5 adds push/enqueue commands, an alias to 'update'.
#6 adds pop/dequeue commands that use bpf_map_lookup_and_delete_elem. #7 adds bash completions

Quoted 3 lines

v2:
* removed unneeded jsonw_null from patch #6
* improved bash completions (and moved them into separate patch #7)

Thanks!

maintainer Applied
#9 lore raw
In reply to Stanislav Fomichev
Quoted 25 lines · Stanislav Fomichev

This patch series add support for queue/stack manipulations.

It goes like this:

#1 and #2 add support for queue/stack in existing 'update' and 'lookup' commands by permitting empty keys.
#3 make sure we don't print empty keys for queue/stack.
#4 adds peek command, it's essentially an alias for 'lookup'. #5 adds push/enqueue commands, an alias to 'update'.
#6 adds pop/dequeue commands that use bpf_map_lookup_and_delete_elem. #7 adds bash completions

v2:
* removed unneeded jsonw_null from patch #6
* improved bash completions (and moved them into separate patch #7)

Stanislav Fomichev (7):
bpftool: make key and value optional in update command
bpftool: make key optional in lookup command
bpftool: don't print empty key/value for maps
bpftool: add peek command
bpftool: add push and enqueue commands
bpftool: add pop and dequeue commands
bpftool: add bash completion for peek/push/enqueue/pop/dequeue

Quoted 4 lines
 .../bpf/bpftool/Documentation/bpftool-map.rst |  28 ++-
 tools/bpf/bpftool/bash-completion/bpftool     |  91 +++++--
 tools/bpf/bpftool/map.c                       | 225 ++++++++++++------
 3 files changed, 254 insertions(+), 90 deletions(-)

Applied, thanks!