-
Notifications
You must be signed in to change notification settings - Fork 19
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 sriov token server chain element #249
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2021 Nordix Foundation. | ||
// | ||
// 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 token | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
) | ||
|
||
type tokenElement struct { | ||
lock sync.Mutex | ||
tokens map[string][]string // tokens[tokenName] -> []tokenIDs | ||
connectionsByTokens map[string]string // connectionsByTokens[tokenID] -> connectionID | ||
tokensByConnections map[string]string // tokensByConnections[connectionID] -> tokenID | ||
} | ||
|
||
type tokenConfig interface { | ||
assign(tokenName string, conn *networkservice.Connection) (tokenID string) | ||
release(conn *networkservice.Connection) | ||
} | ||
|
||
func createTokenElement(allocatableTokens map[string][]string) tokenConfig { | ||
return &tokenElement{tokens: allocatableTokens, | ||
connectionsByTokens: map[string]string{}, | ||
tokensByConnections: map[string]string{}} | ||
} | ||
|
||
func (c *tokenElement) assign(tokenName string, conn *networkservice.Connection) (tokenID string) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
for _, tokenID = range c.tokens[tokenName] { | ||
if _, ok := c.connectionsByTokens[tokenID]; !ok { | ||
c.connectionsByTokens[tokenID] = conn.GetId() | ||
c.tokensByConnections[conn.GetId()] = tokenID | ||
break | ||
} else { | ||
tokenID = "" | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (c *tokenElement) release(conn *networkservice.Connection) { | ||
c.lock.Lock() | ||
defer c.lock.Unlock() | ||
|
||
if tokenID, ok := c.tokensByConnections[conn.GetId()]; ok { | ||
delete(c.connectionsByTokens, tokenID) | ||
delete(c.tokensByConnections, conn.GetId()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2021 Nordix Foundation. | ||
// | ||
// 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 token | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/golang/protobuf/ptypes/empty" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" | ||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||
|
||
"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/resourcepool" | ||
"github.com/networkservicemesh/sdk-sriov/pkg/tools/tokens" | ||
) | ||
|
||
type tokenServer struct { | ||
tokenName string | ||
config tokenConfig | ||
} | ||
|
||
// NewServer returns a new token server chain element for the given tokenKey | ||
func NewServer(tokenKey string) networkservice.NetworkServiceServer { | ||
return &tokenServer{ | ||
tokenName: tokenKey, | ||
config: createTokenElement(tokens.GetTokensFromEnv(os.Environ(), tokenKey)), | ||
} | ||
} | ||
|
||
func (s *tokenServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
if mechanism := kernel.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil || mechanism.GetPCIAddress() == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose that it would be better to check here for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, changed it to use token id check. |
||
tokenID := s.config.assign(s.tokenName, request.GetConnection()) | ||
if tokenID != "" { | ||
mechanism.Parameters[resourcepool.TokenIDKey] = tokenID | ||
} | ||
} | ||
return next.Server(ctx).Request(ctx, request) | ||
} | ||
|
||
func (s *tokenServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) { | ||
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil { | ||
s.config.release(conn) | ||
} | ||
return next.Server(ctx).Close(ctx, conn) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// Copyright (c) 2020 Doc.ai and/or its affiliates. | ||
// | ||
// Copyright (c) 2021 Nordix Foundation. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
|
@@ -23,23 +25,37 @@ import ( | |
) | ||
|
||
const ( | ||
envPrefix = "NSM_SRIOV_TOKENS_" | ||
// EnvPrefix sriov token env name prefix | ||
EnvPrefix = "NSM_SRIOV_TOKENS_" | ||
) | ||
|
||
// ToEnv returns a (name, value) pair to store given tokens into the environment variable | ||
func ToEnv(tokenName string, tokenIDs []string) (name, value string) { | ||
return fmt.Sprintf("%s%s", envPrefix, tokenName), strings.Join(tokenIDs, ",") | ||
return fmt.Sprintf("%s%s", EnvPrefix, tokenName), strings.Join(tokenIDs, ",") | ||
} | ||
|
||
// FromEnv returns all stored tokens from the list of environment variables | ||
func FromEnv(envs []string) map[string][]string { | ||
tokens := map[string][]string{} | ||
for _, env := range envs { | ||
if !strings.HasPrefix(env, envPrefix) { | ||
if !strings.HasPrefix(env, EnvPrefix) { | ||
continue | ||
} | ||
nameIDs := strings.Split(strings.TrimPrefix(env, envPrefix), "=") | ||
nameIDs := strings.Split(strings.TrimPrefix(env, EnvPrefix), "=") | ||
tokens[nameIDs[0]] = strings.Split(nameIDs[1], ",") | ||
} | ||
return tokens | ||
} | ||
|
||
// GetTokensFromEnv returns stored token ids from env for given tokenKey | ||
func GetTokensFromEnv(envs []string, tokenKey string) map[string][]string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does it return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case of token server chain element, we should just retrieve tokens for only one token key and also to make compatible with tokenElement#tokens, I added this method. are you not liking it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. What do you think about doing like this: #249 (comment). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
tokens := map[string][]string{} | ||
for _, env := range envs { | ||
if !strings.HasPrefix(env, EnvPrefix) || !strings.EqualFold(strings.TrimPrefix(env, EnvPrefix), tokenKey) { | ||
continue | ||
} | ||
tokens[tokenKey] = strings.Split(strings.Split(env, "=")[1], ",") | ||
break | ||
} | ||
return tokens | ||
} |
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.
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