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

Add ability to mock kernel feature prober and expand BPF map tests #14876

Merged
merged 4 commits into from
Feb 17, 2021

Conversation

christarazi
Copy link
Member

@christarazi christarazi commented Feb 5, 2021

See commit msgs.

Followup from #14853

@christarazi christarazi added release-note/misc This PR makes changes that have no direct user impact. sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages. kind/enhancement This would improve or streamline existing functionality. labels Feb 5, 2021
@maintainer-s-little-helper maintainer-s-little-helper bot added this to In progress in 1.10.0 Feb 5, 2021
@christarazi christarazi changed the title pr/christarazi/bpftool mock Add ability to mock kernel feature prober Feb 5, 2021
@christarazi christarazi changed the title Add ability to mock kernel feature prober Add ability to mock kernel feature prober and expand BPF map tests Feb 5, 2021
@joestringer joestringer closed this Feb 8, 2021
@joestringer joestringer deleted the branch cilium:master February 8, 2021 22:58
@joestringer joestringer reopened this Feb 8, 2021
@christarazi christarazi changed the base branch from pr/christarazi/fix-bpf-maps-preallocate-flags-fatal to master February 8, 2021 23:41
@christarazi christarazi marked this pull request as ready for review February 8, 2021 23:43
@christarazi christarazi requested review from a team and jrfastab February 8, 2021 23:43
@christarazi
Copy link
Member Author

test-me-please

Copy link
Member

@joestringer joestringer left a comment

Choose a reason for hiding this comment

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

Couple of minor comments, otherwise LGTM!

Comment on lines 154 to 161
// Prober abstracts the notion of a kernel feature prober. This is useful for
// testing purposes as it allows us to mock out the kernel, enabling control
// control over what features are returned.
type Prober interface {
// Probe returns the kernel feaures available on machine.
Probe() Features
}

Copy link
Member

Choose a reason for hiding this comment

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

Usually we define the interface at the place where it will be consumed, I think this is in pkg/bpf instead since that's where the function is that receives this type. Otherwise we are forced to pull in this package with the concrete implementation at the same time that we pull in the interface. Is there a motivation for defining this interface in the probes package?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I initially thought to provide the interface inside the probes package simply because proximity and that pkg/bpf was already importing probes anyway. However, that doesn't preclude the pattern of providing the interface at the consumer site, so I've made that change.

c.Assert(err, IsNil)

upgrade := upgradeMap.CheckAndUpgrade(&upgradeMap.MapInfo)
c.Assert(upgrade, Equals, false)
Copy link
Member

Choose a reason for hiding this comment

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

If I follow right, the upgrade == false case here is basically saying that:

  • Typically, LRU requires pre-allocation.
  • Given the underlying lack of LRU support from the "kernel" prober, this map type would actually use hashmap
  • Since the map type is switched to hashmap, now pre-allocation can be disabled
  • When we try to upgrade the map, defining that its type should be LRU, there's no intermediate state where we decide that the map should be upgraded because the desired type is LRU (or the preallocation flags are mismatched)
  • Instead, every single time the map info is evaluated, the type & flags are evaluated first and then the upgrade decision is made based on the attributes afterwards.

At a glance I found this a little bit cryptic in the reading of the test, you might consider stashing a comment something like this in the code. But not such a big deal.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I'll add this snippet as a comment. It can indeed be hard to follow at first glance, as it's very subtle. Also amended the commit to include a reference to the commit that fixed this to establish a link between this regression test and the fix.

@christarazi christarazi force-pushed the pr/christarazi/bpftool-mock branch 2 times, most recently from 143fb44 to cf24464 Compare February 10, 2021 07:29
@christarazi
Copy link
Member Author

test-me-please

Copy link
Member

@aditighag aditighag left a comment

Choose a reason for hiding this comment

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

Cool 🚀

pkg/bpf/map.go Outdated
// SetMapTypesFromProber initializes the supported map types from the given
// prober. This function is useful for testing purposes, as we require
// injecting our own mocked prober.
func SetMapTypesFromProber(prober prober) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this method exported if it's only being used from the test file which is in the same package?

Copy link
Member Author

Choose a reason for hiding this comment

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

I figured it might be useful in the future, but I can unexport it for now. If it needs to be exported at some point, we can make that change.

@joestringer joestringer moved this from Done to In progress in 1.10.0 Feb 12, 2021
This is a preparatory commit to allow mocking of the probe manager for
testing purposes.

Signed-off-by: Chris Tarazi <chris@isovalent.com>
This commit allows us to mock out kernel probing so that unit tests
aren't constrained to the underlying kernel of the running system.

Note, special care was taken for bpf.GetMapType() to keep the function
API untouched. The main motivation is to avoid having to modify each
call site. The logic relies on the fact that if no supported map types
have been loaded into memory, we will default to probing the underlying
kernel. The unit tests will leverage this in order to inject their own
supported may types by calling bpf.SetMapTypesFromProber(), and thus
bypassing this check.

Signed-off-by: Chris Tarazi <chris@isovalent.com>
This commit preparatively refactors the test to be table-driven for
later commits to expand on.

Signed-off-by: Chris Tarazi <chris@isovalent.com>
This commit builds upon the previous commit by expanding the
table-driven test and leveraging the ability to mock the kernel prober
to assert against 4.9 kernel supported maps. The test is a regression
test against the bpf.(*Map).CheckAndUpgrade() bug that was fixed in
7e5a97f ("bpf: Fix wrong map type when fetching preallocate map
flags").

Signed-off-by: Chris Tarazi <chris@isovalent.com>
@aditighag aditighag removed their assignment Feb 17, 2021
@aditighag aditighag merged commit a6e8ae8 into cilium:master Feb 17, 2021
1.10.0 automation moved this from In progress to Done Feb 17, 2021
@christarazi christarazi deleted the pr/christarazi/bpftool-mock branch February 17, 2021 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement This would improve or streamline existing functionality. release-note/misc This PR makes changes that have no direct user impact. sig/datapath Impacts bpf/ or low-level forwarding details, including map management and monitor messages.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants