Subject: [PATCH bpf-next] bpftool: allow match by name prefixes

From: Paul Chaignon <[email-masked]>
Date: Fri, 27 Dec 2019 17:16:58 +0100

This patch extends bpftool to support lookup of programs and maps by
name prefixes (instead of full name only), as follows.

  $ ./bpftool prog show name tcp_
  19: kprobe  name tcp_cleanup_rbu  tag 639217cf5b184808  gpl
      [...]
  20: kprobe  name tcp_sendmsg  tag 6546b9784163ee69  gpl
      [...]

Signed-off-by: Paul Chaignon <[email-masked]>
---
 tools/bpf/bpftool/Documentation/bpftool-map.rst  | 4 ++--
 tools/bpf/bpftool/Documentation/bpftool-prog.rst | 2 +-
 tools/bpf/bpftool/main.h                         | 4 ++--
 tools/bpf/bpftool/map.c                          | 2 +-
 tools/bpf/bpftool/prog.c                         | 2 +-
 5 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-map.rst b/tools/bpf/bpftool/Documentation/bpftool-map.rst
index cdeae8ae90ba..bcc38ff9da65 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-map.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-map.rst
@@ -39,9 +39,9 @@ MAP COMMANDS
 |	**bpftool** **map freeze**     *MAP*
 |	**bpftool** **map help**
 |
-|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *MAP_NAME* }
+|	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* | **name** *NAME_PREFIX* }
 |	*DATA* := { [**hex**] *BYTES* }
-|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
+|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *NAME_PREFIX* }
 |	*VALUE* := { *DATA* | *MAP* | *PROG* }
 |	*UPDATE_FLAGS* := { **any** | **exist** | **noexist** }
 |	*TYPE* := { **hash** | **array** | **prog_array** | **perf_event_array** | **percpu_hash**
diff --git a/tools/bpf/bpftool/Documentation/bpftool-prog.rst b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
index 64ddf8a4c518..8e39ac362c16 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-prog.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-prog.rst
@@ -33,7 +33,7 @@ PROG COMMANDS
 |	**bpftool** **prog help**
 |
 |	*MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
-|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
+|	*PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *NAME_PREFIX* }
 |	*TYPE* := {
 |		**socket** | **kprobe** | **kretprobe** | **classifier** | **action** |
 |		**tracepoint** | **raw_tracepoint** | **xdp** | **perf_event** | **cgroup/skb** |
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 4e75b58d3989..8e70617e55fb 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -42,12 +42,12 @@
 #define BPF_TAG_FMT	"%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
 
 #define HELP_SPEC_PROGRAM						\
-	"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }"
+	"PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name NAME_PREFIX }"
 #define HELP_SPEC_OPTIONS						\
 	"OPTIONS := { {-j|--json} [{-p|--pretty}] | {-f|--bpffs} |\n"	\
 	"\t            {-m|--mapcompat} | {-n|--nomount} }"
 #define HELP_SPEC_MAP							\
-	"MAP := { id MAP_ID | pinned FILE | name MAP_NAME }"
+	"MAP := { id MAP_ID | pinned FILE | name NAME_PREFIX }"
 
 static const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_UNSPEC]			= "unspec",
diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index c01f76fa6876..79e0d72ffdd2 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -125,7 +125,7 @@ static int map_fd_by_name(char *name, int **fds)
 			goto err_close_fd;
 		}
 
-		if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) {
+		if (!is_prefix(name, info.name)) {
 			close(fd);
 			continue;
 		}
diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c
index 2221bae037f1..295509548c8b 100644
--- a/tools/bpf/bpftool/prog.c
+++ b/tools/bpf/bpftool/prog.c
@@ -117,7 +117,7 @@ static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
 		}
 
 		if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) ||
-		    (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) {
+		    (!tag && !is_prefix(nametag, info.name))) {
 			close(fd);
 			continue;
 		}
-- 
2.17.1