Skip to contentSkip to conversation

bpf/bpftool: add program & link type names

PATCH Applied Milan Landaverde 4 participants 14 messages Mar 31, 2022 – Apr 4, 2022

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

cover bpf/bpftool: add program & link type names

With the addition of the syscall prog type we should now be able to see feature probe info for that prog type:

    $ bpftool feature probe kernel
    ...
    eBPF program_type syscall is available
    ...
    eBPF helpers supported for program type syscall:
        ...
        - bpf_sys_bpf
        - bpf_sys_close

And for the link types, their names should aid in
the output.

Before:

    $ bpftool link show
    50: type 7  prog 5042
	    bpf_cookie 0
	    pids vfsstat(394433)

After:

    $ bpftool link show
    57: perf_event  prog 5058
	    bpf_cookie 0
	    pids vfsstat(394725)

Milan Landaverde (3):
bpf/bpftool: add syscall prog type bpf/bpftool: add missing link types bpf/bpftool: handle libbpf_probe_prog_type errors

 tools/bpf/bpftool/feature.c | 2 +-
 tools/bpf/bpftool/link.c    | 2 ++
 tools/bpf/bpftool/prog.c    | 1 +
 3 files changed, 4 insertions(+), 1 deletion(-)

--
2.32.0

#1 lore raw

PATCH 1/3 bpf/bpftool: add syscall prog type

In addition to displaying the program type in bpftool prog show this enables us to be able to query bpf_prog_type_syscall availability through feature probe as well as see which helpers are available in those programs (such as bpf_sys_bpf and bpf_sys_close)

---
 tools/bpf/bpftool/prog.c | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/prog.c +1 -0
index bc4e05542c2b..8643b37d4e43 100644
@@ -68,6 +68,7 @@ const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_EXT]			= "ext",
 	[BPF_PROG_TYPE_LSM]			= "lsm",
 	[BPF_PROG_TYPE_SK_LOOKUP]		= "sk_lookup",
+	[BPF_PROG_TYPE_SYSCALL]			= "syscall",
 };
 
 const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);
Signature · 1 line
2.32.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH bpf-next 1/3] bpf/bpftool: add syscall prog type

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 16 lines

In addition to displaying the program type in bpftool prog show this enables us to be able to query bpf_prog_type_syscall availability through feature probe as well as see which helpers are available in those programs (such as bpf_sys_bpf and bpf_sys_close)

---
 tools/bpf/bpftool/prog.c | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/prog.c +0 -0
index bc4e05542c2b..8643b37d4e43 100644

tools/bpf/bpftool/prog.c const char * const prog_type_name[] = { · line 74

Quoted 7 lines
tools/bpf/bpftool/prog.c +1 -0
@@ -68,6 +68,7 @@ const char * const prog_type_name[] = {
 	[BPF_PROG_TYPE_EXT]			= "ext",
 	[BPF_PROG_TYPE_LSM]			= "lsm",
 	[BPF_PROG_TYPE_SK_LOOKUP]		= "sk_lookup",
+	[BPF_PROG_TYPE_SYSCALL]			= "syscall",
 };
 
 const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);

Thanks! This one should have been caught by CI :/. Instead it complains when you add it. This is because BPF_PROG_TYPE_SYSCALL in the UAPI header has a comment next to it, and the regex used in tools/testing/selftests/bpf/test_bpftool_synctypes.py to extract the program types does not account for it. The fix should be:

------

(quoted diff) +0 -0

b/tools/testing/selftests/bpf/test_bpftool_synctypes.py index 6bf21e47882a..cd239cbfd80c 100755

tools/testing/selftests/bpf/test_bpftool_synctypes.py +1 -1
@@ -180,7 +180,7 @@ class FileExtractor(object):
         @enum_name: name of the enum to parse
         """
         start_marker = re.compile(f'enum {enum_name} {{\n')
-        pattern = re.compile('^\s*(BPF_\w+),?$')
+        pattern = re.compile('^\s*(BPF_\w+),?( /\* .* \*/)?$')
         end_marker = re.compile('^};')
         parser = BlockParser(self.reader)
         parser.search_block(start_marker)

------

I can submit this separately as a patch.

Quentin

#3 lore raw

Re: [PATCH bpf-next 1/3] bpf/bpftool: add syscall prog type

Quoted 41 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 16 lines

In addition to displaying the program type in bpftool prog show this enables us to be able to query bpf_prog_type_syscall availability through feature probe as well as see which helpers are available in those programs (such as bpf_sys_bpf and bpf_sys_close)

---
 tools/bpf/bpftool/prog.c | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/prog.c +0 -0
index bc4e05542c2b..8643b37d4e43 100644

tools/bpf/bpftool/prog.c const char * const prog_type_name[] = { · line 74

Quoted 7 lines
tools/bpf/bpftool/prog.c +1 -0
@@ -68,6 +68,7 @@ const char * const prog_type_name[] = {
      [BPF_PROG_TYPE_EXT]                     = "ext",
      [BPF_PROG_TYPE_LSM]                     = "lsm",
      [BPF_PROG_TYPE_SK_LOOKUP]               = "sk_lookup",
+     [BPF_PROG_TYPE_SYSCALL]                 = "syscall",
 };
 
 const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);

Thanks! This one should have been caught by CI :/. Instead it complains when you add it. This is because BPF_PROG_TYPE_SYSCALL in the UAPI header has a comment next to it, and the regex used in tools/testing/selftests/bpf/test_bpftool_synctypes.py to extract the program types does not account for it. The fix should be:

------

tools/testing/selftests/bpf/test_bpftool_synctypes.py +0 -0
index 6bf21e47882a..cd239cbfd80c 100755

tools/testing/selftests/bpf/test_bpftool_synctypes.py class FileExtractor(object): · line 183

Quoted 5 lines
tools/testing/selftests/bpf/test_bpftool_synctypes.py +1 -1
@@ -180,7 +180,7 @@ class FileExtractor(object):
         @enum_name: name of the enum to parse
         """
         start_marker = re.compile(f'enum {enum_name} {{\n')
-        pattern = re.compile('^\s*(BPF_\w+),?$')
+        pattern = re.compile('^\s*(BPF_\w+),?( /\* .* \*/)?$')

small nit: do you need those spaces inside /* and */? why make unnecessary assumptions about proper formatting? ;)

Quoted 8 lines
tools/testing/selftests/bpf/test_bpftool_synctypes.py +0 -1
         end_marker = re.compile('^};')
         parser = BlockParser(self.reader)
         parser.search_block(start_marker)
------

I can submit this separately as a patch.

Quentin

#4 lore raw

Re: [PATCH bpf-next 1/3] bpf/bpftool: add syscall prog type

Quoted 36 lines · Andrii Nakryiko
Quoted 34 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 16 lines

In addition to displaying the program type in bpftool prog show this enables us to be able to query bpf_prog_type_syscall availability through feature probe as well as see which helpers are available in those programs (such as bpf_sys_bpf and bpf_sys_close)

---
 tools/bpf/bpftool/prog.c | 1 +
 1 file changed, 1 insertion(+)
tools/bpf/bpftool/prog.c +0 -0
index bc4e05542c2b..8643b37d4e43 100644

tools/bpf/bpftool/prog.c const char * const prog_type_name[] = { · line 74

Quoted 7 lines
tools/bpf/bpftool/prog.c +1 -0
@@ -68,6 +68,7 @@ const char * const prog_type_name[] = {
      [BPF_PROG_TYPE_EXT]                     = "ext",
      [BPF_PROG_TYPE_LSM]                     = "lsm",
      [BPF_PROG_TYPE_SK_LOOKUP]               = "sk_lookup",
+     [BPF_PROG_TYPE_SYSCALL]                 = "syscall",
 };
 
 const size_t prog_type_name_size = ARRAY_SIZE(prog_type_name);

Thanks! This one should have been caught by CI :/. Instead it complains when you add it. This is because BPF_PROG_TYPE_SYSCALL in the UAPI header has a comment next to it, and the regex used in tools/testing/selftests/bpf/test_bpftool_synctypes.py to extract the program types does not account for it. The fix should be:

Quoted 15 lines

tools/testing/selftests/bpf/test_bpftool_synctypes.py class FileExtractor(object): · line 183

Quoted 12 lines

------

tools/testing/selftests/bpf/test_bpftool_synctypes.py +1 -1
index 6bf21e47882a..cd239cbfd80c 100755
@@ -180,7 +180,7 @@ class FileExtractor(object):
         @enum_name: name of the enum to parse
         """
         start_marker = re.compile(f'enum {enum_name} {{\n')
-        pattern = re.compile('^\s*(BPF_\w+),?$')
+        pattern = re.compile('^\s*(BPF_\w+),?( /\* .* \*/)?$')

small nit: do you need those spaces inside /* and */? why make unnecessary assumptions about proper formatting? ;)

No I don't need the spaces, I'll remove them indeed, thanks. I'll send the patch next week.

#5 lore raw

PATCH 2/3 bpf/bpftool: add missing link types

In reply to Milan Landaverde

Will display the link type names in bpftool link show output

---
 tools/bpf/bpftool/link.c | 2 ++
 1 file changed, 2 insertions(+)
tools/bpf/bpftool/link.c +2 -0
index 97dec81950e5..9392ef390828 100644
@@ -20,6 +20,8 @@ static const char * const link_type_name[] = {
 	[BPF_LINK_TYPE_CGROUP]			= "cgroup",
 	[BPF_LINK_TYPE_ITER]			= "iter",
 	[BPF_LINK_TYPE_NETNS]			= "netns",
+	[BPF_LINK_TYPE_XDP]				= "xdp",
+	[BPF_LINK_TYPE_PERF_EVENT]		= "perf_event",
 };
 
 static struct hashmap *link_table;
Signature · 1 line
2.32.0
#6 lore raw

Re: [PATCH bpf-next 2/3] bpf/bpftool: add missing link types

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 12 lines

Will display the link type names in bpftool link show output

---
 tools/bpf/bpftool/link.c | 2 ++
 1 file changed, 2 insertions(+)
tools/bpf/bpftool/link.c +0 -0
index 97dec81950e5..9392ef390828 100644

tools/bpf/bpftool/link.c static const char * const link_type_name[] = { · line 24

Quoted 5 lines
tools/bpf/bpftool/link.c +2 -0
@@ -20,6 +20,8 @@ static const char * const link_type_name[] = {
 	[BPF_LINK_TYPE_CGROUP]			= "cgroup",
 	[BPF_LINK_TYPE_ITER]			= "iter",
 	[BPF_LINK_TYPE_NETNS]			= "netns",
+	[BPF_LINK_TYPE_XDP]				= "xdp",
+	[BPF_LINK_TYPE_PERF_EVENT]		= "perf_event",

Since this goes into bpf-next, we should add BPF_LINK_TYPE_KPROBE_MULTI as well.

Quentin

#7 lore raw

Re: [PATCH bpf-next 2/3] bpf/bpftool: add missing link types

Quoted 9 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 7 lines

Will display the link type names in bpftool link show output

---
 tools/bpf/bpftool/link.c | 2 ++
 1 file changed, 2 insertions(+)
Quoted 9 lines

tools/bpf/bpftool/link.c static const char * const link_type_name[] = { · line 23

Quoted 9 lines
tools/bpf/bpftool/link.c +1 -0
index 97dec81950e5..9392ef390828 100644
@@ -20,6 +20,8 @@ static const char * const link_type_name[] = {
      [BPF_LINK_TYPE_CGROUP]                  = "cgroup",
      [BPF_LINK_TYPE_ITER]                    = "iter",
      [BPF_LINK_TYPE_NETNS]                   = "netns",
+     [BPF_LINK_TYPE_XDP]                             = "xdp",

fixed indentation here and added KPROBE_MULTI while applying

Quoted 6 lines

tools/bpf/bpftool/link.c static const char * const link_type_name[] = { · line 24

Quoted 1 line
tools/bpf/bpftool/link.c +1 -0
+     [BPF_LINK_TYPE_PERF_EVENT]              = "perf_event",

Since this goes into bpf-next, we should add BPF_LINK_TYPE_KPROBE_MULTI as well.

Quentin

#8 lore raw

PATCH 3/3 bpf/bpftool: handle libbpf_probe_prog_type errors

In reply to Milan Landaverde

Previously [1], we were using bpf_probe_prog_type which returned a bool, but the new libbpf_probe_bpf_prog_type can return a negative error code on failure. This change decides for bpftool to declare a program type is not available on probe failure.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/feature.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/feature.c +1 -1
index c2f43a5d38e0..b2fbaa7a6b15 100644
@@ -564,7 +564,7 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
 
 		res = probe_prog_type_ifindex(prog_type, ifindex);
 	} else {
-		res = libbpf_probe_bpf_prog_type(prog_type, NULL);
+		res = libbpf_probe_bpf_prog_type(prog_type, NULL) > 0;
 	}
 
 #ifdef USE_LIBCAP
Signature · 1 line
2.32.0
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH bpf-next 3/3] bpf/bpftool: handle libbpf_probe_prog_type errors

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 17 lines

Previously [1], we were using bpf_probe_prog_type which returned a bool, but the new libbpf_probe_bpf_prog_type can return a negative error code on failure. This change decides for bpftool to declare a program type is not available on probe failure.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/feature.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/feature.c +0 -0
index c2f43a5d38e0..b2fbaa7a6b15 100644

tools/bpf/bpftool/feature.c probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types, · line 570

Quoted 8 lines
tools/bpf/bpftool/feature.c +1 -1
@@ -564,7 +564,7 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
 
 		res = probe_prog_type_ifindex(prog_type, ifindex);
 	} else {
-		res = libbpf_probe_bpf_prog_type(prog_type, NULL);
+		res = libbpf_probe_bpf_prog_type(prog_type, NULL) > 0;
 	}
 
 #ifdef USE_LIBCAP

Thanks!

#10 lore raw

Re: [PATCH bpf-next 3/3] bpf/bpftool: handle libbpf_probe_prog_type errors

Quoted 20 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 18 lines

Previously [1], we were using bpf_probe_prog_type which returned a bool, but the new libbpf_probe_bpf_prog_type can return a negative error code on failure. This change decides for bpftool to declare a program type is not available on probe failure.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/feature.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/feature.c +0 -0
index c2f43a5d38e0..b2fbaa7a6b15 100644
@@ -564,7 +564,7 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
 
Quoted 8 lines

tools/bpf/bpftool/feature.c probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types, · line 570

Quoted 7 lines
tools/bpf/bpftool/feature.c +1 -1
              res = probe_prog_type_ifindex(prog_type, ifindex);
      } else {
-             res = libbpf_probe_bpf_prog_type(prog_type, NULL);
+             res = libbpf_probe_bpf_prog_type(prog_type, NULL) > 0;
      }
 
 #ifdef USE_LIBCAP

A completely unrelated question to you, Quentin. How hard is bpftool's dependency on libcap? We've recently removed libcap from selftests, I wonder if it would be possible to do that for bpftool as well to reduce amount of shared libraries bpftool depends on.

Quoted 3 lines

Thanks!

#11 lore raw

Re: [PATCH bpf-next 3/3] bpf/bpftool: handle libbpf_probe_prog_type errors

Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 24 lines

Previously [1], we were using bpf_probe_prog_type which returned a bool, but the new libbpf_probe_bpf_prog_type can return a negative error code on failure. This change decides for bpftool to declare a program type is not available on probe failure.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/feature.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/feature.c +1 -1
index c2f43a5d38e0..b2fbaa7a6b15 100644
@@ -564,7 +564,7 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
 
              res = probe_prog_type_ifindex(prog_type, ifindex);
      } else {
-             res = libbpf_probe_bpf_prog_type(prog_type, NULL);
+             res = libbpf_probe_bpf_prog_type(prog_type, NULL) > 0;
      }
 
Quoted 7 lines
Quoted 2 lines

tools/bpf/bpftool/feature.c probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types, · line 570

Quoted 1 line
tools/bpf/bpftool/feature.c +0 -0
 #ifdef USE_LIBCAP

A completely unrelated question to you, Quentin. How hard is bpftool's dependency on libcap? We've recently removed libcap from selftests, I wonder if it would be possible to do that for bpftool as well to reduce amount of shared libraries bpftool depends on.

There's not a super-strong dependency on it. It's used in feature probing, for two things.

First one is to be accurate when we check that the user has the right capabilities for probing efficiently the system. A workaround consists in checking that we run with uid=0 (root), although it's less accurate.

Second thing is probing as an unprivileged user: if bpftool is run to probe as root but with the "unprivileged" keyword, libcap is used to drop the CAP_SYS_ADMIN and run the probes without it. I don't know if there's an easy alternative to libcap for that. Also I don't know how many people use this feature, but I remember that this was added because there was some demand at the time, so presumably there are users relying on this.

This being said, libcap is optional for compiling bpftool, so you should be able to have it work just as well if the library is not available on the system? Basically you'd just lose the ability to probe as an unprivileged user. Do you need to remove the optional dependency completely?

Quentin

PS: Not directly related but since we're talking of libcap, we recently discovered that the lib is apparently changing errno when it maybe shouldn't and plays badly with batch mode:
https://stackoverflow.com/questions/71608181/bpf-xdp-bpftool-batch-file-returns-error-reading-batch-file-failed-opera

#12 lore raw

Re: [PATCH bpf-next 3/3] bpf/bpftool: handle libbpf_probe_prog_type errors

Quoted 54 lines · Quentin Monnet
Quoted 28 lines · Andrii Nakryiko
Quoted 26 lines · Quentin Monnet

2022-03-31 11:45 UTC-0400 ~ Milan Landaverde <[email protected]>

Quoted 24 lines

Previously [1], we were using bpf_probe_prog_type which returned a bool, but the new libbpf_probe_bpf_prog_type can return a negative error code on failure. This change decides for bpftool to declare a program type is not available on probe failure.

[1] https://lore.kernel.org/bpf/[email protected]/

---
 tools/bpf/bpftool/feature.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/feature.c +1 -1
index c2f43a5d38e0..b2fbaa7a6b15 100644
@@ -564,7 +564,7 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
 
              res = probe_prog_type_ifindex(prog_type, ifindex);
      } else {
-             res = libbpf_probe_bpf_prog_type(prog_type, NULL);
+             res = libbpf_probe_bpf_prog_type(prog_type, NULL) > 0;
      }
 
Quoted 7 lines
Quoted 2 lines

tools/bpf/bpftool/feature.c probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types, · line 570

Quoted 1 line
tools/bpf/bpftool/feature.c +0 -0
 #ifdef USE_LIBCAP

A completely unrelated question to you, Quentin. How hard is bpftool's dependency on libcap? We've recently removed libcap from selftests, I wonder if it would be possible to do that for bpftool as well to reduce amount of shared libraries bpftool depends on.

There's not a super-strong dependency on it. It's used in feature probing, for two things.

First one is to be accurate when we check that the user has the right capabilities for probing efficiently the system. A workaround consists in checking that we run with uid=0 (root), although it's less accurate.

Second thing is probing as an unprivileged user: if bpftool is run to probe as root but with the "unprivileged" keyword, libcap is used to drop the CAP_SYS_ADMIN and run the probes without it. I don't know if there's an easy alternative to libcap for that. Also I don't know how many people use this feature, but I remember that this was added because there was some demand at the time, so presumably there are users relying on this.

Quoted 5 lines

This being said, libcap is optional for compiling bpftool, so you should be able to have it work just as well if the library is not available on the system? Basically you'd just lose the ability to probe as an unprivileged user. Do you need to remove the optional dependency completely?

Well, see recent patches from Martin:

82cb2b30773e bpf: selftests: Remove libcap usage from test_progs b1c2768a82b9 bpf: selftests: Remove libcap usage from test_verifier 663af70aabb7 bpf: selftests: Add helpers to directly use the capget and capset syscall

We completely got rid of libcap dependency and ended up with more straightforward code. So I was wondering if this is possible for bpftool. The less unnecessary library dependencies - the better. The less feature detection -- the better. That's my only line of thought here :)

Quoted 7 lines

Quentin

PS: Not directly related but since we're talking of libcap, we recently discovered that the lib is apparently changing errno when it maybe shouldn't and plays badly with batch mode:
https://stackoverflow.com/questions/71608181/bpf-xdp-bpftool-batch-file-returns-error-reading-batch-file-failed-opera

just another argument in favor of getting rid of extra layers of dependencies

#13 lore raw
In reply to Milan Landaverde
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 12 lines · On Thu, 31 Mar 2022 11:45:52 -0400 you wrote:

With the addition of the syscall prog type we should now be able to see feature probe info for that prog type:

    $ bpftool feature probe kernel
    ...
    eBPF program_type syscall is available
    ...
    eBPF helpers supported for program type syscall:
        ...
        - bpf_sys_bpf
        - bpf_sys_close
Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next,1/3] bpf/bpftool: add syscall prog type

    https://git.kernel.org/bpf/bpf-next/c/380341637ebb
  - [bpf-next,2/3] bpf/bpftool: add missing link types
    https://git.kernel.org/bpf/bpf-next/c/fff3dfab1786
  - [bpf-next,3/3] bpf/bpftool: handle libbpf_probe_prog_type errors
    https://git.kernel.org/bpf/bpf-next/c/7b53eaa656c3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html