-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from Nordix/sriov-token-server-elem
add sriov token server chain element
- Loading branch information
Showing
4 changed files
with
165 additions
and
34 deletions.
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,72 @@ | ||
// 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() | ||
|
||
var ok bool | ||
if tokenID, ok = c.tokensByConnections[conn.GetId()]; ok { | ||
return tokenID | ||
} | ||
|
||
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()) | ||
} | ||
} |
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,70 @@ | ||
// 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(map[string][]string{ | ||
tokenKey: tokens.FromEnv(os.Environ())[tokenKey], | ||
}), | ||
} | ||
} | ||
|
||
func (s *tokenServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) { | ||
var tokenID string | ||
if mechanism := kernel.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil || mechanism.Parameters[resourcepool.TokenIDKey] == "" { | ||
tokenID = s.config.assign(s.tokenName, request.GetConnection()) | ||
if tokenID != "" { | ||
mechanism.Parameters[resourcepool.TokenIDKey] = tokenID | ||
} | ||
} | ||
|
||
isEstablished := request.GetConnection().GetNextPathSegment() != nil | ||
conn, err := next.Server(ctx).Request(ctx, request) | ||
if err != nil && tokenID != "" && !isEstablished { | ||
s.config.release(request.GetConnection()) | ||
} | ||
|
||
return conn, err | ||
} | ||
|
||
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) | ||
} |
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