Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] A fallible from_kernel_errno with Result<Error> return #347

Closed

Conversation

foxhlchen
Copy link

@foxhlchen foxhlchen commented Jun 4, 2021

Currently, from_kernel_errno() is an infallible function acting as a constructor for Error. In order to achieve its type invariant, We add a check in it which will prompt a warning and return Error::EINVL when errno given is invalid.

While this approach ensures type invariant, it brings great ambiguities. When Error::EINVL is returned, the caller has no way to recognize whether it is a valid errno coming from the kernel or an error issued by the check. This tricky behavior may confuse developers and introduce subtle bugs. Since Error will be used in all respects of the kernel, It's definitely not
a sound solution.

This RFC proposes that we make from_kernel_errno() return a Result<Error>. Thus, we have an explicit, clear, and fallible version of from_kernel_errno() by which callers are able to know what really happened behind the scene. And it also provides certain flexibility. We pass the power to callers, they can decide how to deal with invalid errno case by case.

Note:

  • The commit is a proposal that can't be compiled at this moment. Because a few other codes rely on it, they need to be changed as well.
  • Another approach @ojeda mentioned here is to propose upstream to have a dedicated errno (e.g. EBUG or EIKE). This is also viable, however: 1) it needs interaction with upstream 2) from_kernel_errno is still an infallible function that can fail.

Reference:
#324 #283

May influence:
#335

@ksquirrel

This comment has been minimized.

Currently, from_kernel_errno is an infallible function acting as a
constructor for Error. In order to achieve its type invariant, We add
a check in it which will prompt a warning and return Error::EINVL when
errno given is invalid.

While this approach ensures type invariant, it brings great
ambiguities. When Error::EINVL is returned, the caller has no way to
recognize whether it is a valid errno coming from the kernel or an
error issued by the check. This tricky behavior may confuse developers
and introduce subtle bugs. Since Error will be used in all respects of
the kernel, It's definitely not a sound solution.

This RFC proposes that we make from_kernel_errno return a
Result<Error>. Thus, we have an explicit, clear, and fallible version
of from_kernel_errno by which callers are able to know what really
happened behind the scene. And it also provides certain flexibility.
We pass the power to callers, they can decide how to deal with invalid
`errno` case by case.

Signed-off-by: Fox Chen <[email protected]>
@foxhlchen foxhlchen force-pushed the fallible-from_kernel_errno branch from d0ac814 to b11524b Compare June 5, 2021 03:39
@ksquirrel
Copy link
Member

Review of b11524bfec5f:

  • ✔️ Commit b11524b: Looks fine!

@@ -62,21 +62,16 @@ impl Error {

/// Creates an [`Error`] from a kernel error code.
///
/// It is a bug to pass an out-of-range `errno`. `EINVAL` would
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// It is a bug to pass an out-of-range `errno`. Err(`EINVAL`) would
/// It is a bug to pass an out-of-range `errno`. `Err(EINVAL)` would

@foxhlchen foxhlchen marked this pull request as draft July 29, 2021 01:19
@foxhlchen foxhlchen closed this Jan 3, 2023
ojeda pushed a commit that referenced this pull request Jun 11, 2024
Add a test case to assert that the skb->pkt_type which was set from the BPF
program is retained from the netkit xmit side to the peer's device at tcx
ingress location.

  # ./vmtest.sh -- ./test_progs -t netkit
  [...]
  ./test_progs -t netkit
  [    1.140780] bpf_testmod: loading out-of-tree module taints kernel.
  [    1.141127] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
  [    1.284601] tsc: Refined TSC clocksource calibration: 3408.006 MHz
  [    1.286672] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x311fd9b189d, max_idle_ns: 440795225691 ns
  [    1.290384] clocksource: Switched to clocksource tsc
  #345     tc_netkit_basic:OK
  #346     tc_netkit_device:OK
  #347     tc_netkit_multi_links:OK
  #348     tc_netkit_multi_opts:OK
  #349     tc_netkit_neigh_links:OK
  #350     tc_netkit_pkt_type:OK
  Summary: 6/0 PASSED, 0 SKIPPED, 0 FAILED

Signed-off-by: Daniel Borkmann <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alexei Starovoitov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants