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

tetragon: Add map ownership #2945

Merged
merged 7 commits into from
Sep 30, 2024
Merged

tetragon: Add map ownership #2945

merged 7 commits into from
Sep 30, 2024

Conversation

olsajiri
Copy link
Contributor

@olsajiri olsajiri commented Sep 22, 2024

Adding the concept of owning map and splitting Map object to owners and others.

The idea is that the 'owned' Map object takes care of creating/overwriting the pin,
while the 'other' Map object is just a user, that can read/update map but can't create it.

I'm not really happy with the 'other' naming, so any ideas are welcome ;-)

@olsajiri olsajiri added the release-note/minor This PR introduces a minor user-visible change label Sep 22, 2024
@olsajiri olsajiri force-pushed the pr/olsajiri/map_fixes branch 4 times, most recently from 9959201 to e6b9e5b Compare September 24, 2024 08:22
Copy link

netlify bot commented Sep 24, 2024

Deploy Preview for tetragon ready!

Name Link
🔨 Latest commit f98ce9d
🔍 Latest deploy log https://app.netlify.com/sites/tetragon/deploys/66f3c117b3d7c1000848e0ff
😎 Deploy Preview https://deploy-preview-2945--tetragon.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@olsajiri olsajiri changed the title Pr/olsajiri/map fixes tetragon: Add map ownership Sep 24, 2024
@olsajiri olsajiri marked this pull request as ready for review September 24, 2024 12:56
@olsajiri olsajiri requested a review from a team as a code owner September 24, 2024 12:56
@tixxdz
Copy link
Member

tixxdz commented Sep 25, 2024

Adding the concept of owning map and splitting Map object to owners and others.

The idea is that the 'owned' Map object takes care of creating/overwriting the pin, while the 'other' Map object is just a user, that can read/update map but can't create it.

What are the possible use cases for it? restart ? or is it just to organize better?

I'm not really happy with the 'other' naming, so any ideas are welcome ;-)

Hmm others could be fine, or "non-owners"?, but those are certainly better than "users" ? or go with creators and users? ...

@olsajiri
Copy link
Contributor Author

Adding the concept of owning map and splitting Map object to owners and others.
The idea is that the 'owned' Map object takes care of creating/overwriting the pin, while the 'other' Map object is just a user, that can read/update map but can't create it.

What are the possible use cases for it? restart ? or is it just to organize better?

so at the moment any user of the map will overwrite the pin file with its spec and max entries,
while in reality we always have one user that creates the map (owner) and other users that
expect the map to exist and want just to open and use it

it has also another aspect where you need all the users of the map to match the same spec
and same max entries value which is not convenient from configuration point of view, because
all users must have access to the spec and max entries

having the owner and other user force the config duty only to owner and other users just use the map

I'm not really happy with the 'other' naming, so any ideas are welcome ;-)

Hmm others could be fine, or "non-owners"?, but those are certainly better than "users" ? or go with creators and users? ...

hm, right.. maybe MapBuilder(..) for owner and MapUser(..) (instead MapOther) for other users.. with:

also perhaps the MapOwner should be just bool.. iota is perhaps too much

let's see.. thanks a lot for review

@mtardy
Copy link
Member

mtardy commented Sep 26, 2024

This could be beneficial for accounting which sensor/progs/policies are responsible for BPF map memory use, cc @kkourt

@olsajiri olsajiri force-pushed the pr/olsajiri/map_fixes branch 2 times, most recently from ddc3888 to f5e02e3 Compare September 26, 2024 18:54
Copy link
Member

@tixxdz tixxdz left a comment

Choose a reason for hiding this comment

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

LGTM, minor suggestions with one log message that should be fixed. Thank you!

// Try to open the pinPath and if it exist use the previously
// pinned map otherwise pin the map and next user will find
// it here.
if _, err := os.Stat(pinPath); err == nil {
if _, err = os.Stat(pinPath); err == nil {
m, err := ebpf.LoadPinnedMap(pinPath, nil)
if err != nil {
return nil, fmt.Errorf("loading pinned map from path '%s' failed: %w", pinPath, err)
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to re-create here if we have create true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, so this leg is when there is already an existing pin and we check if the pin is compatible with what we want to create, if it is, we are done, if not, we remove the pin and create new one

@@ -260,12 +261,23 @@ func LoadOrCreatePinnedMap(pinPath string, mapSpec *ebpf.MapSpec) (*ebpf.Map, er
"map-name": mapSpec.Name,
}).Warn("tetragon, incompatible map found: will delete and recreate")
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this delete and recreate warn should be moved inside create true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch! will move, thanks

return m, nil
}
if create {
return createPinnedMap(pinPath, mapSpec)
Copy link
Member

Choose a reason for hiding this comment

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

Could move this create just after the state(pinPath) failure , and have the rest outside of if , just for readability up to you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, so it is already in the place after the stat failure.. not sure what you mean

Display pin path instead of the name, before:
  time="2024-09-21T21:09:35Z" level=info msg="map was unloaded" map=m1 pin=m1
  time="2024-09-21T21:09:35Z" level=info msg="map was unloaded" map=m2 pin=m2

after:
  time="2024-09-21T21:10:34Z" level=info msg="map was unloaded" map=m1 pin=m1
  time="2024-09-21T21:10:34Z" level=info msg="map was unloaded" map=m2 pin=policy/m2

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding GetDefaultSensorsWithBase helper function to allow
overloading of the base sensor.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Introduce map owner field in Map object to state if the object
is the real owner of the map.

Basically the map object can be now either owner of the map or
can just be its user. The ownership will matter when loading
the map in following changes.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Let's distinguish the map that 'owns' the pin and the 'user'
map user that's just using it.

The owner map gets to set (and overwrite if needed) pined map spec
and its max entries setup.

The user map just follows what is set in existing pinned map
and fails if it's expecting anything else.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding MapUser* interface to create map that you don't own.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding test for pinned maps in multiple sensors just to cover
common use case of using same maps cross sensors.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Adding tests for 'user' maps to cover following scenarios:

  - user map gets properly created from pinned map
  - user map fails to be loaded if the pinned map is missing
  - user map fails to be loaded if the pinned map has different
    max entries setup
  - user map fails to be loaded if the pinned map is not compatible

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Copy link
Contributor

@kkourt kkourt left a comment

Choose a reason for hiding this comment

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

🚀

//
// Each map declares the ownership of the map. The map can be either
// owner of the map (via MapBuilder* helpers) or as an user (MapUser*
// helpers.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding the comments!

@olsajiri olsajiri merged commit 19ba896 into main Sep 30, 2024
44 checks passed
@olsajiri olsajiri deleted the pr/olsajiri/map_fixes branch September 30, 2024 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/minor This PR introduces a minor user-visible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants