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

egressgw: optimize policy matching logic #24042

Merged
merged 3 commits into from Feb 28, 2023

Conversation

jibi
Copy link
Member

@jibi jibi commented Feb 27, 2023

Whenever we need to determine if a policy is a match for a given BPF map entry or IP rule, we first select from the policyConfigsBySourceIP cache all the policies that match the entry/rule's source IP, and from there we call the matches or matchesMinusExcludedCIDRs PolicyConfig methods, which iterate again through all the policy's endpoint/source IPs.

As this isn't optimal, this commit introduces a couple of new manager's methods, that replace the PolicyConfig ones:

  • policyMatches
  • policyMatchesMinusExcludedCIDRs

these work like matches and matchesMinusExcludedCIDRs, but iterate through the entire set of policies (instead of on a single one), and are optimized to skip iterating through the endpoint IPs we are not interested in.

@jibi jibi added release-note/misc This PR makes changes that have no direct user impact. feature/egress-gateway Impacts the egress IP gateway feature. labels Feb 27, 2023
@jibi jibi requested a review from a team as a code owner February 27, 2023 11:28
@jibi jibi force-pushed the pr/jibi/improve-egressgw-policy-match-logic branch 2 times, most recently from cdfead8 to 8e6cafd Compare February 27, 2023 16:02
Whenever we need to determine if a policy is a match for a given BPF map
entry or IP rule, we first select from the policyConfigsBySourceIP cache
all the policies that match the entry/rule's source IP, and from there
we call the matches or matchesMinusExcludedCIDRs PolicyConfig methods,
which iterate again through all the policy's endpoint/source IPs.

As this isn't optimal, this commit introduces a couple of new manager's
methods, that replace the PolicyConfig ones:

* policyMatches
* policyMatchesMinusExcludedCIDRs

these work like matches and matchesMinusExcludedCIDRs, but iterate
through the entire set of policies (instead of on a single one), and are
optimized to skip iterating through the endpoint IPs we are not
interested in.

Signed-off-by: Gilberto Bertin <jibi@cilium.io>
Instead of just storing the endpoint IDs in the PolicyConfig
matchedEndpointIDs cache, store also a pointer to the actual endpoint
metadata, so that we don't need to rely on the manager's epDataStore
every time we need to resolve the endpoint ID to its actual metadata.

Also rename the cache to matchedEndpoints.

Signed-off-by: Gilberto Bertin <jibi@cilium.io>
Now that the policy configs store a list of matching endpoints, we can
replace the following logic, used to determine if a given policy has at
least one matching endpoint:

	for _, endpoint := range manager.epDataStore {
		if policyConfig.selectsEndpoint(endpoint) {
			// ..
		}
	}

with:

	if len(policyConfig.matchedEndpoints) != 0 {
		// ..
	}

Signed-off-by: Gilberto Bertin <jibi@cilium.io>
@jibi jibi force-pushed the pr/jibi/improve-egressgw-policy-match-logic branch from 8e6cafd to 9973f15 Compare February 27, 2023 17:36
@jibi
Copy link
Member Author

jibi commented Feb 27, 2023

/test

Copy link
Member

@julianwiedmann julianwiedmann left a comment

Choose a reason for hiding this comment

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

thanks Jibi!

@jibi jibi added the ready-to-merge This PR has passed all tests and received consensus from code owners to merge. label Feb 28, 2023
@sayboras sayboras merged commit a0e27e6 into master Feb 28, 2023
@sayboras sayboras deleted the pr/jibi/improve-egressgw-policy-match-logic branch February 28, 2023 09:16
@jibi jibi added affects/v1.12 This issue affects v1.12 branch affects/v1.13 This issue affects v1.13 branch labels Mar 24, 2023
@jibi jibi mentioned this pull request Apr 5, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects/v1.12 This issue affects v1.12 branch affects/v1.13 This issue affects v1.13 branch feature/egress-gateway Impacts the egress IP gateway feature. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. 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.

None yet

4 participants