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

new: driver selection logic #484

Merged
merged 8 commits into from
Mar 25, 2024
Merged

new: driver selection logic #484

merged 8 commits into from
Mar 25, 2024

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Mar 20, 2024

What type of PR is this?

/kind feature
when a
Any specific area of the project related to this PR?

/area library
/area cli

What this PR does / why we need it:

This PR expands the driver automatic selection logic, in this way:

  • --type CLI option/type config key/FALCOCTL_DRIVER_TYPE env var do now support multiple driver types to be specified
  • if a single type is specified, we retain the old behavior
  • when multiple types are passed, the automatic selection logic will choose the best-fit given:
    • types are passed in decreasing priority order
    • driver must be supported on the machine

New default is to enable all drivers in this order: modern_ebpf,ebpf,kmod.
COS target does now specialize PreferredDriver method since it does not support kmod.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

…ogic.

`Driver.Type` will now be a slice in config.
Then, cmd/driver_linux will take care of discovering,
given the list of allowed driver types loaded from the config,
the correct driver to be used.

This has multple consequencies:
* enforcing a single `--type` works like the existing behavior
* allowed driver types are in descending priority order

Signed-off-by: Federico Di Pierro <[email protected]>
@FedeDP
Copy link
Contributor Author

FedeDP commented Mar 20, 2024

/milestone v0.8.0

@poiana poiana added this to the v0.8.0 milestone Mar 20, 2024
// type Tracing, attachType AttachTraceRawTp program availability.
//
//go:linkname probeProgram github.com/cilium/ebpf/features.probeProgram
func probeProgram(spec *ebpf.ProgramSpec) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To avoid fully rewriting the function, we "unsafely" import it using go:linkname.
I think this is better than copying the function locally, because then we get any change probeProgram might get with new versions, and if probeProgram changes signature/name, our build will fail while bumping the library dep.
This way, we always know if something has changed.

// err := features.HaveProgramType(ebpf.Tracing)
// Therefore, we need to manually build a feature test.
// Empty tracing program that just returns 0
progSpec := &ebpf.ProgramSpec{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pass a prog spec with correct AttachType type.

@FedeDP FedeDP changed the title wip: new: driver selection logic new: driver selection logic Mar 20, 2024
@@ -162,11 +183,22 @@ func NewDriverCmd(ctx context.Context, opt *options.Common) *cobra.Command {
},
}

cmd.PersistentFlags().Var(driverTypes, "type", "Driver type to be used "+driverTypes.Allowed())
cmd.PersistentFlags().StringSliceVar(&driverTypesStr, "type", config.DefaultDriver.Type,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

type is now a slice.

cmd.PersistentFlags().StringVar(&driver.Version, "version", config.DefaultDriver.Version, "Driver version to be used.")
cmd.PersistentFlags().StringSliceVar(&driver.Repos, "repo", config.DefaultDriver.Repos, "Driver repo to be used.")
cmd.PersistentFlags().StringVar(&driver.Name, "name", config.DefaultDriver.Name, "Driver name to be used.")
cmd.PersistentFlags().StringVar(&driver.HostRoot, "host-root", config.DefaultDriver.HostRoot, "Driver host root to be used.")
cmd.PersistentFlags().StringVar(&driverKernelRelease,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved --kernelrelease and --kernelversion CLI options directly to driver root command.

return fmt.Errorf("automatic driver selection failed")
// Step 2: fetch system info (kernel release/version and distro)
var err error
driver.Kr, err = driverkernel.FetchInfo(driverKernelRelease, driverKernelVersion)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

options.Driver now holds run kernelrelease and distro too.
This way, we discover them just once, here, and then pass the driver options down to all driver related commands, without needing to discover distro or kernel infos twice.

} else {
return "", fmt.Errorf("detected an unsupported target system, please get in touch with the Falco community")
}
if o.Distro.String() == driverdistro.UndeterminedDistro && o.Compile {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check is now a bit different because we are not discovering the distro here anymore; instead we receive a distro from driver root command.

We need to check if ErrUnsupported was returned, ie: if the distro is UndeterminedDistro.

…md/driver_linux root command.

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

Moreover, added a version for others OS.

Signed-off-by: Federico Di Pierro <[email protected]>
@FedeDP FedeDP force-pushed the new/driver_selection_logic branch from 8f33dbc to 2a56cf9 Compare March 21, 2024 07:57
func probeProgram(spec *ebpf.ProgramSpec) error

//nolint:gocritic // the method shall not be able to modify kr
func (m *modernBpf) Supported(_ kernelrelease.KernelRelease) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: modern bpf will check directly against running kernel, to see if all required features are supported.
This means that we cannot check against "foreign" kernel releases.

Copy link
Member

@leogr leogr left a comment

Choose a reason for hiding this comment

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

First quick look. I really like the new UX! 🤩

It SGTM already, still I'd like to get a second opinion before approving.
cc @alacuku

pkg/driver/kernel/kernel_others.go Outdated Show resolved Hide resolved
Also, allow `FetchInfo` to override kernelrelease/version if only one of them is enforced.

Signed-off-by: Federico Di Pierro <[email protected]>
@poiana
Copy link
Contributor

poiana commented Mar 25, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, leogr

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit cf958dc into main Mar 25, 2024
16 of 17 checks passed
@poiana poiana deleted the new/driver_selection_logic branch March 25, 2024 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants