Skip to content

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Sep 18, 2025

Description

This PR improves bpf errmetrics intoduced in #3205.
It does so with multiple things (split by commit):

  • added a generic with_errmetrics macro that supersedes map_update_elem__errmetrics
  • expose bpf helper name string too in errmetrics
  • use __FILE_NAME__ macro instead of just __FILE__; this prevents issues with relative includes: prior to this, the files (eg: bpf_process_event.h) included also with relative patsh (eg: ../bpf_process_event.h) would require to be added twice to the fileids with both paths. Now, we will only report the filename. We don't expect any name clash between bpf sources!
  • the trick used to trigger a comp time error in with_errmetrics macro worked fine until we had multiple comp time errors in the same source file. At that point, the compiler complained about multiple extern function definition with different error attributes. I fixed it by appending a __COUNTER__ to the end of the comp time error function name
  • bpf/tetragon/fileids.h should not be formatted; added it to the ignore list
  • expanded usage of with_errmetrics macro for map_update_elem
  • updated cmd/bpf-fileids go command to leverage cgo instead of manually parsing the C header. Only downside: we are no more able to customize fileids.h path. I don't think that is needed at all since this command is mostly made to be automatically ran by makefile.

Example output:

Location                            Error        Count
FnProbeRead@bpf_execve_event.c:61   E2BIG (7)    2

It allows to error-check any bpf helper that return an int,
appending a metrics in case of failure.

Signed-off-by: Federico Di Pierro <[email protected]>
@mtardy mtardy added the release-note/misc This PR makes changes that have no direct user impact. label Sep 18, 2025
@FedeDP FedeDP force-pushed the new/bpf_errmetrics_improvements branch from 0f5882b to 8d555c2 Compare September 18, 2025 09:13
@FedeDP FedeDP marked this pull request as ready for review September 18, 2025 10:36
@FedeDP FedeDP requested a review from a team as a code owner September 18, 2025 10:36
@FedeDP
Copy link
Contributor Author

FedeDP commented Sep 18, 2025

cc @kkourt since he made the initial PR :)

Adapted userspace `tetra` code to inject the bpf
func helper in the metric name, like:
```
Location                                        Error        Count
FnProbeRead@bpf/process/bpf_execve_event.c:61   E2BIG (7)    2
```

To avoid using a `funcids.h` file like was done for `fileids.h`,
i just cast the bpf helper to an u64, since its value is actually the
`BPF_FUNC_foo` enum value (`enum bpf_func_id`); see `bpf/include/api.h`.

In userspace instead, we leverage `github.com/cilium/ebpf/asm`
to cast the `bpf_func_id` back to its string value.

Signed-off-by: Federico Di Pierro <[email protected]>
Since it is much harder to prevent people from using relative includes,
this is a simple solution to avoid potential issues.
Of course we should avoid name clashes for our bpf source files.

Also, leverage clang builtin `__auto_type` to use
correct return type from real bpf helper call.

Signed-off-by: Federico Di Pierro <[email protected]>
… `compile_time_error`.

Compiler complained about multiple `extern` definition of
same function with different `error` function attributes.

Signed-off-by: Federico Di Pierro <[email protected]>
For now, only for `map_update_elem` helper.

Signed-off-by: Federico Di Pierro <[email protected]>
@FedeDP FedeDP force-pushed the new/bpf_errmetrics_improvements branch from 8d555c2 to fffe7e1 Compare September 18, 2025 10:43
@FedeDP
Copy link
Contributor Author

FedeDP commented Sep 18, 2025

Just force-pushed rewording a commit :)

Check pull request commits / checkpatch (pull_request)Failing after 14s

It is complaining about MACRO_ARG_REUSE. I think that is a false-positive.

@mtardy mtardy requested review from mtardy and kkourt and removed request for ExceptionalHandler September 18, 2025 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/misc This PR makes changes that have no direct user impact.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants