Skip to content

Commit

Permalink
Merge pull request networkservicemesh#307 from Nordix/metadata-vfconfig
Browse files Browse the repository at this point in the history
use metadata map for storing vfconfig
  • Loading branch information
denis-tingaikin authored Aug 29, 2021
2 parents 3641f22 + fc9c8b6 commit eba0791
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 64 deletions.
10 changes: 10 additions & 0 deletions pkg/kernel/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type Link interface {
MoveToNetns(target netns.NsHandle) error
SetAdminState(state State) error
SetName(name string) error
GetName() string
GetLink() netlink.Link
}

// link provides Link interface implementation
Expand Down Expand Up @@ -153,6 +155,14 @@ func (l *link) SetName(name string) error {
return nil
}

func (l *link) GetName() string {
return l.link.Attrs().Name
}

func (l *link) GetLink() netlink.Link {
return l.link
}

// FindHostDevice returns a new instance of link representing host device, based on the PCI
// address and/or target interface name.
func FindHostDevice(pciAddress, name string, namespaces ...netns.NsHandle) (Link, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/networkservice/ethernetcontext/vf_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (i *vfEthernetClient) Request(ctx context.Context, request *networkservice.
return nil, err
}

if vfConfig := vfconfig.Config(ctx); vfConfig != nil {
if vfConfig, ok := vfconfig.Load(ctx, true); ok {
if err := vfCreate(vfConfig, conn, true); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/networkservice/ethernetcontext/vf_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewVFServer() networkservice.NetworkServiceServer {
}

func (s *vfEthernetContextServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if vfConfig := vfconfig.Config(ctx); vfConfig != nil {
if vfConfig, ok := vfconfig.Load(ctx, false); ok {
err := vfCreate(vfConfig, request.Connection, false)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions pkg/kernel/networkservice/inject/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"
)

Expand All @@ -48,7 +49,7 @@ func (c *injectClient) Request(ctx context.Context, request *networkservice.Netw
}

if !isEstablished {
if err := move(ctx, conn, false); err != nil {
if err := move(ctx, conn, metadata.IsClient(c), false); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

Expand All @@ -66,7 +67,7 @@ func (c *injectClient) Request(ctx context.Context, request *networkservice.Netw
func (c *injectClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*empty.Empty, error) {
rv, err := next.Client(ctx).Close(ctx, conn, opts...)

injectErr := move(ctx, conn, true)
injectErr := move(ctx, conn, metadata.IsClient(c), true)

if err != nil && injectErr != nil {
return nil, errors.Wrap(err, injectErr.Error())
Expand Down
17 changes: 12 additions & 5 deletions pkg/kernel/networkservice/inject/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func renameInterface(origIfName, desiredIfName string, curNetNS, targetNetNS net
})
}

func move(ctx context.Context, conn *networkservice.Connection, isMoveBack bool) error {
func move(ctx context.Context, conn *networkservice.Connection, isClient, isMoveBack bool) error {
mech := kernel.ToMechanism(conn.GetMechanism())
if mech == nil {
return nil
Expand All @@ -79,7 +79,10 @@ func move(ctx context.Context, conn *networkservice.Connection, isMoveBack bool)
}
defer func() { _ = contNetNS.Close() }()

vfConfig := vfconfig.Config(ctx)
vfConfig, ok := vfconfig.Load(ctx, isClient)
if !ok {
return nil
}
ifName := mech.GetInterfaceName()
if !isMoveBack {
err = moveToContNetNS(vfConfig, ifName, hostNetNS, contNetNS)
Expand Down Expand Up @@ -115,10 +118,14 @@ func moveToContNetNS(vfConfig *vfconfig.VFConfig, ifName string, hostNetNS, cont

func moveToHostNetNS(vfConfig *vfconfig.VFConfig, ifName string, hostNetNS, contNetNS netns.NsHandle) (err error) {
if vfConfig != nil && vfConfig.VFInterfaceName != ifName {
link, _ := kernellink.FindHostDevice("", vfConfig.VFInterfaceName, hostNetNS)
link, _ := kernellink.FindHostDevice(vfConfig.VFPCIAddress, vfConfig.VFInterfaceName, hostNetNS)
if link != nil {
// TODO: rename (if necessary) interface back to its original name.
// FindHostDevice with vf's pci address in this case.
linkName := link.GetName()
if linkName != vfConfig.VFInterfaceName {
if err = netlink.LinkSetName(link.GetLink(), vfConfig.VFInterfaceName); err != nil {
err = errors.Wrapf(err, "failed to rename interface from %s to %s", linkName, vfConfig.VFInterfaceName)
}
}
return
}
err = renameInterface(ifName, vfConfig.VFInterfaceName, hostNetNS, contNetNS)
Expand Down
7 changes: 4 additions & 3 deletions pkg/kernel/networkservice/inject/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"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/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"
)

Expand All @@ -45,7 +46,7 @@ func (s *injectServer) Request(ctx context.Context, request *networkservice.Netw

isEstablished := request.GetConnection().GetNextPathSegment() != nil
if !isEstablished {
if err := move(ctx, request.GetConnection(), false); err != nil {
if err := move(ctx, request.GetConnection(), metadata.IsClient(s), false); err != nil {
return nil, err
}
}
Expand All @@ -57,7 +58,7 @@ func (s *injectServer) Request(ctx context.Context, request *networkservice.Netw
moveCtx, cancelMove := postponeCtxFunc()
defer cancelMove()

if moveRenameErr := move(moveCtx, request.GetConnection(), true); moveRenameErr != nil {
if moveRenameErr := move(moveCtx, request.GetConnection(), metadata.IsClient(s), true); moveRenameErr != nil {
err = errors.Wrapf(err, "server request failed, failed to move back the interface: %s", moveRenameErr.Error())
}
}
Expand All @@ -68,7 +69,7 @@ func (s *injectServer) Request(ctx context.Context, request *networkservice.Netw
func (s *injectServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
_, err := next.Server(ctx).Close(ctx, conn)

moveRenameErr := move(ctx, conn, true)
moveRenameErr := move(ctx, conn, metadata.IsClient(s), true)

if err != nil && moveRenameErr != nil {
return nil, errors.Wrap(err, moveRenameErr.Error())
Expand Down
52 changes: 0 additions & 52 deletions pkg/kernel/networkservice/vfconfig/context.go

This file was deleted.

85 changes: 85 additions & 0 deletions pkg/kernel/networkservice/vfconfig/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2020-2021 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");
// 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 vfconfig provides VF config
package vfconfig

import (
"context"

"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
)

type key struct{}

// VFConfig is a config for VF
type VFConfig struct {
// PFInterfaceName is a parent PF net interface name
PFInterfaceName string
// VFInterfaceName is a VF net interface name
VFInterfaceName string
// VFPCIAddress is a VF pci address
VFPCIAddress string
// VFNum is a VF num for the parent PF
VFNum int
}

// Store sets the VFConfig stored in per Connection.Id metadata.
func Store(ctx context.Context, isClient bool, config *VFConfig) {
metadata.Map(ctx, isClient).Store(key{}, config)
}

// Delete deletes the VFConfig stored in per Connection.Id metadata
func Delete(ctx context.Context, isClient bool) {
metadata.Map(ctx, isClient).Delete(key{})
}

// Load returns the VFConfig stored in per Connection.Id metadata, or nil if no
// value is present.
// The ok result indicates whether value was found in the per Connection.Id metadata.
func Load(ctx context.Context, isClient bool) (config *VFConfig, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).Load(key{})
if !ok {
return
}
config, ok = rawValue.(*VFConfig)
return config, ok
}

// LoadOrStore returns the existing VFConfig stored in per Connection.Id metadata if present.
// Otherwise, it stores and returns the given VFConfig.
// The loaded result is true if the value was loaded, false if stored.
func LoadOrStore(ctx context.Context, isClient bool, config *VFConfig) (value *VFConfig, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).LoadOrStore(key{}, config)
if !ok {
return config, ok
}
value, ok = rawValue.(*VFConfig)
return value, ok
}

// LoadAndDelete deletes the VFConfig stored in per Connection.Id metadata,
// returning the previous value if any. The loaded result reports whether the key was present.
func LoadAndDelete(ctx context.Context, isClient bool) (config *VFConfig, ok bool) {
rawValue, ok := metadata.Map(ctx, isClient).LoadAndDelete(key{})
if !ok {
return
}
config, ok = rawValue.(*VFConfig)
return config, ok
}

0 comments on commit eba0791

Please sign in to comment.