-
Notifications
You must be signed in to change notification settings - Fork 36
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
Added chain element that excludes already used prefixes #1197
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c4688ef
Added client chain element that excludes already used prefixes
sol-0 9e67583
Addressed review comments
sol-0 91a5b2b
Addressed review comments
sol-0 8e22833
Added IPs/routes validation
sol-0 0f5b909
Addressed review comments
sol-0 a25a0d8
Addressed review comments
sol-0 8aa4a0b
Addressed review comments
sol-0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// Copyright (c) 2021 Doc.ai and/or its affiliates. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at: | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package excludedprefixes | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/edwarnicke/serialize" | ||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/pkg/errors" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
"github.com/networkservicemesh/sdk/pkg/tools/ippool" | ||
"github.com/networkservicemesh/sdk/pkg/tools/log" | ||
"github.com/networkservicemesh/sdk/pkg/tools/postpone" | ||
) | ||
|
||
type excludedPrefixesClient struct { | ||
excludedPrefixes []string | ||
executor serialize.Executor | ||
} | ||
|
||
// NewClient - creates a networkservice.NetworkServiceClient chain element that excludes prefixes already used by other NetworkServices | ||
func NewClient() networkservice.NetworkServiceClient { | ||
return &excludedPrefixesClient{ | ||
excludedPrefixes: make([]string, 0), | ||
} | ||
} | ||
|
||
func (epc *excludedPrefixesClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) { | ||
conn := request.GetConnection() | ||
if conn.GetContext() == nil { | ||
conn.Context = &networkservice.ConnectionContext{} | ||
} | ||
|
||
if conn.GetContext().GetIpContext() == nil { | ||
conn.Context.IpContext = &networkservice.IPContext{} | ||
} | ||
|
||
logger := log.FromContext(ctx).WithField("ExcludedPrefixesClient", "Request") | ||
ipCtx := conn.GetContext().GetIpContext() | ||
|
||
var newExcludedPrefixes []string | ||
oldExcludedPrefixes := ipCtx.GetExcludedPrefixes() | ||
if len(epc.excludedPrefixes) > 0 { | ||
<-epc.executor.AsyncExec(func() { | ||
logger.Debugf("Adding new excluded IPs to the request: %+v", epc.excludedPrefixes) | ||
newExcludedPrefixes = ipCtx.GetExcludedPrefixes() | ||
newExcludedPrefixes = append(newExcludedPrefixes, epc.excludedPrefixes...) | ||
newExcludedPrefixes = removeDuplicates(newExcludedPrefixes) | ||
|
||
// excluding IPs for current request/connection before calling next client for the refresh use-case | ||
newExcludedPrefixes = exclude(newExcludedPrefixes, append(ipCtx.GetSrcIpAddrs(), ipCtx.GetDstIpAddrs()...)) | ||
|
||
logger.Debugf("Excluded prefixes from request - %+v", newExcludedPrefixes) | ||
ipCtx.ExcludedPrefixes = newExcludedPrefixes | ||
}) | ||
} | ||
|
||
postponeCtxFunc := postpone.ContextWithValues(ctx) | ||
|
||
resp, err := next.Client(ctx).Request(ctx, request, opts...) | ||
if err != nil { | ||
ipCtx.ExcludedPrefixes = oldExcludedPrefixes | ||
return resp, err | ||
} | ||
|
||
respIPContext := resp.GetContext().GetIpContext() | ||
|
||
err = validateIPs(respIPContext, newExcludedPrefixes) | ||
if err != nil { | ||
closeCtx, cancelFunc := postponeCtxFunc() | ||
defer cancelFunc() | ||
|
||
logger.Errorf("Source or destination IPs are overlapping with excluded prefixes, srcIPs: %+v, dstIPs: %+v, excluded prefixes: %+v, error: %s", | ||
respIPContext.GetSrcIpAddrs(), respIPContext.GetDstIpAddrs(), newExcludedPrefixes, err.Error()) | ||
|
||
if _, closeErr := next.Client(ctx).Close(closeCtx, conn, opts...); closeErr != nil { | ||
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error()) | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
logger.Debugf("Request excluded IPs - srcIPs: %v, dstIPs: %v, excluded prefixes: %v", respIPContext.GetSrcIpAddrs(), | ||
respIPContext.GetDstIpAddrs(), respIPContext.GetExcludedPrefixes()) | ||
|
||
<-epc.executor.AsyncExec(func() { | ||
epc.excludedPrefixes = append(epc.excludedPrefixes, respIPContext.GetSrcIpAddrs()...) | ||
epc.excludedPrefixes = append(epc.excludedPrefixes, respIPContext.GetDstIpAddrs()...) | ||
epc.excludedPrefixes = append(epc.excludedPrefixes, getRoutePrefixes(respIPContext.GetSrcRoutes())...) | ||
epc.excludedPrefixes = append(epc.excludedPrefixes, getRoutePrefixes(respIPContext.GetDstRoutes())...) | ||
epc.excludedPrefixes = append(epc.excludedPrefixes, respIPContext.GetExcludedPrefixes()...) | ||
epc.excludedPrefixes = removeDuplicates(epc.excludedPrefixes) | ||
logger.Debugf("Added excluded prefixes: %+v", epc.excludedPrefixes) | ||
}) | ||
|
||
return resp, err | ||
} | ||
|
||
func (epc *excludedPrefixesClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) { | ||
logger := log.FromContext(ctx).WithField("ExcludedPrefixesClient", "Close") | ||
ipCtx := conn.GetContext().GetIpContext() | ||
|
||
<-epc.executor.AsyncExec(func() { | ||
epc.excludedPrefixes = exclude(epc.excludedPrefixes, ipCtx.GetSrcIpAddrs()) | ||
epc.excludedPrefixes = exclude(epc.excludedPrefixes, ipCtx.GetDstIpAddrs()) | ||
epc.excludedPrefixes = exclude(epc.excludedPrefixes, getRoutePrefixes(ipCtx.GetSrcRoutes())) | ||
epc.excludedPrefixes = exclude(epc.excludedPrefixes, getRoutePrefixes(ipCtx.GetDstRoutes())) | ||
epc.excludedPrefixes = exclude(epc.excludedPrefixes, ipCtx.GetExcludedPrefixes()) | ||
logger.Debugf("Excluded prefixes after closing connection: %+v", epc.excludedPrefixes) | ||
}) | ||
|
||
return next.Client(ctx).Close(ctx, conn, opts...) | ||
} | ||
|
||
func getRoutePrefixes(routes []*networkservice.Route) []string { | ||
var rv []string | ||
for _, route := range routes { | ||
rv = append(rv, route.GetPrefix()) | ||
} | ||
|
||
return rv | ||
} | ||
|
||
func validateIPs(ipContext *networkservice.IPContext, excludedPrefixes []string) error { | ||
ip4Pool := ippool.New(net.IPv4len) | ||
ip6Pool := ippool.New(net.IPv6len) | ||
|
||
for _, prefix := range excludedPrefixes { | ||
_, ipNet, err := net.ParseCIDR(prefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ip4Pool.AddNet(ipNet) | ||
ip6Pool.AddNet(ipNet) | ||
} | ||
|
||
prefixes := make([]string, 0, len(ipContext.GetSrcIpAddrs())+len(ipContext.GetDstIpAddrs())) | ||
prefixes = append(prefixes, ipContext.GetSrcIpAddrs()...) | ||
prefixes = append(prefixes, ipContext.GetDstIpAddrs()...) | ||
|
||
for _, prefix := range prefixes { | ||
ip, _, err := net.ParseCIDR(prefix) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ip4Pool.Contains(ip) || ip6Pool.Contains(ip) { | ||
return errors.Errorf("IP %v is excluded, but it was found in response IPs", ip) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger = logger.FromContext(ctx).WithField("ExcludedPrefixesClient", "Request")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done