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

add vfconfig client chain element #223

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/uuid v1.2.0
github.com/networkservicemesh/api v1.0.1-0.20210811070028-10403c0f20c8
github.com/networkservicemesh/sdk v0.5.1-0.20210827110808-c2178d55e7a9
github.com/networkservicemesh/sdk-kernel v0.0.0-20210827111123-3641f2295562
github.com/networkservicemesh/sdk-kernel v0.0.0-20210829205539-eba0791ab5ef
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
go.uber.org/goleak v1.1.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/networkservicemesh/api v1.0.1-0.20210811070028-10403c0f20c8 h1:kr2/dA
github.com/networkservicemesh/api v1.0.1-0.20210811070028-10403c0f20c8/go.mod h1:B6meq/SWjWR6bGXZdXPfbOeaBK+T1JayLdtEJQCsXKU=
github.com/networkservicemesh/sdk v0.5.1-0.20210827110808-c2178d55e7a9 h1:97Y7eS6KdVrDgJhyiI7TmoIe70hpT0T+Fsh78kV6X38=
github.com/networkservicemesh/sdk v0.5.1-0.20210827110808-c2178d55e7a9/go.mod h1:t4mwbFHDeOUbmrrFhkcpcyYd5CgkrzmaD7uu8LiLpKs=
github.com/networkservicemesh/sdk-kernel v0.0.0-20210827111123-3641f2295562 h1:UWSIYeNtBX8lSFeJirJQ9d+cQWc9OU87t2PQSvB6usA=
github.com/networkservicemesh/sdk-kernel v0.0.0-20210827111123-3641f2295562/go.mod h1:LBphTaZW3dXn9sfPlNkvcfrhueCa2LlKU6izB6z+L18=
github.com/networkservicemesh/sdk-kernel v0.0.0-20210829205539-eba0791ab5ef h1:0Gk/WZ5tUnczQoUQrSUK9VYcmmUyJ8XM/JbEru+QILQ=
github.com/networkservicemesh/sdk-kernel v0.0.0-20210829205539-eba0791ab5ef/go.mod h1:LBphTaZW3dXn9sfPlNkvcfrhueCa2LlKU6izB6z+L18=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/chains/xconnectns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ import (
"github.com/networkservicemesh/sdk/pkg/networkservice/common/mechanismtranslation"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/addressof"
"github.com/networkservicemesh/sdk/pkg/tools/token"

"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/mechanisms/noop"
"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/mechanisms/vfio"
"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/resetmechanism"
"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/resourcepool"
"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/vfconfig"
"github.com/networkservicemesh/sdk-sriov/pkg/sriov"
"github.com/networkservicemesh/sdk-sriov/pkg/sriov/config"
)
Expand Down Expand Up @@ -108,8 +108,8 @@ func NewServer(

resourceLock := &sync.Mutex{}
sriovChain := chain.NewNetworkServiceServer(
metadata.NewServer(),
recvfd.NewServer(),
vfconfig.NewServer(),
resetmechanism.NewServer(
mechanisms.NewServer(map[string]networkservice.NetworkServiceServer{
kernel.MECHANISM: chain.NewNetworkServiceServer(
Expand Down
3 changes: 2 additions & 1 deletion pkg/networkservice/common/resourcepool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/common"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

Expand Down Expand Up @@ -77,7 +78,7 @@ func (i *resourcePoolClient) Request(ctx context.Context, request *networkservic
return conn, nil
}

err = assignVF(ctx, logger, conn, tokenID, i.resourcePool)
err = assignVF(ctx, logger, conn, tokenID, i.resourcePool, metadata.IsClient(i))
if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
Expand Down
7 changes: 5 additions & 2 deletions pkg/networkservice/common/resourcepool/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ func (s *resourcePoolConfig) close(conn *networkservice.Connection) error {
return s.resourcePool.Free(vfPCIAddr)
}

func assignVF(ctx context.Context, logger log.Logger, conn *networkservice.Connection, tokenID string, resourcePool *resourcePoolConfig) error {
vfConfig := vfconfig.Config(ctx)
func assignVF(ctx context.Context, logger log.Logger, conn *networkservice.Connection, tokenID string, resourcePool *resourcePoolConfig, isClient bool) error {
resourcePool.resourceLock.Lock()
defer resourcePool.resourceLock.Unlock()

vfConfig := &vfconfig.VFConfig{}

logger.Infof("trying to select VF for %v", resourcePool.driverType)
vf, err := resourcePool.selectVF(conn.GetId(), vfConfig, tokenID)
if err != nil {
Expand Down Expand Up @@ -138,5 +139,7 @@ func assignVF(ctx context.Context, logger log.Logger, conn *networkservice.Conne
}
conn.GetMechanism().GetParameters()[common.PCIAddressKey] = vf.GetPCIAddress()

vfconfig.Store(ctx, isClient, vfConfig)

return nil
}
3 changes: 2 additions & 1 deletion pkg/networkservice/common/resourcepool/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/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"
"github.com/networkservicemesh/sdk/pkg/tools/log"

"github.com/networkservicemesh/sdk-sriov/pkg/sriov"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *resourcePoolServer) Request(ctx context.Context, request *networkservic
return next.Server(ctx).Request(ctx, request)
}

err := assignVF(ctx, logger, conn, tokenID, s.resourcePool)
err := assignVF(ctx, logger, conn, tokenID, s.resourcePool, metadata.IsClient(s))
if err != nil {
_ = s.resourcePool.close(conn)
return nil, err
Expand Down
45 changes: 38 additions & 7 deletions pkg/networkservice/common/resourcepool/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import (
"sync"
"testing"

"github.com/golang/protobuf/ptypes/empty"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vfio"
"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/vfconfig"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"

"github.com/networkservicemesh/sdk-sriov/pkg/networkservice/common/resourcepool"
"github.com/networkservicemesh/sdk-sriov/pkg/sriov"
Expand Down Expand Up @@ -82,13 +86,36 @@ var samples = []*sample{
},
}

type vfResource struct {
vfConfig *vfconfig.VFConfig
}

type vfResourceServer interface {
networkservice.NetworkServiceServer
getVFConfig() *vfconfig.VFConfig
}

func newVFResourceServer() vfResourceServer {
return &vfResource{}
}

func (s *vfResource) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
s.vfConfig, _ = vfconfig.Load(ctx, false)
return next.Server(ctx).Request(ctx, request)
}

func (s *vfResource) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}

func (s *vfResource) getVFConfig() *vfconfig.VFConfig {
return s.vfConfig
}

func TestResourcePoolServer_Request(t *testing.T) {
for i := range samples {
sample := samples[i]
t.Run(sample.mechanism, func(t *testing.T) {
vfConfig := new(vfconfig.VFConfig)
ctx := vfconfig.WithConfig(context.TODO(), vfConfig)

var pfs map[string]*sriovtest.PCIPhysicalFunction
_ = yamlhelper.UnmarshalFile(physicalFunctionsFilename, &pfs)

Expand All @@ -99,14 +126,19 @@ func TestResourcePoolServer_Request(t *testing.T) {
require.NoError(t, err)

resourcePool := new(resourcePoolMock)
resourceServerChainElem := newVFResourceServer()

server := resourcepool.NewServer(sample.driverType, new(sync.Mutex), pciPool, resourcePool, conf)
server := chain.NewNetworkServiceServer(
metadata.NewServer(),
resourcepool.NewServer(sample.driverType, new(sync.Mutex), pciPool, resourcePool, conf),
resourceServerChainElem)

// 1. Request

resourcePool.mock.On("Select", "1", sample.driverType).
Return(pfs[pf2PciAddr].Vfs[1].Addr, nil)

ctx := context.TODO()
conn, err := server.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "id",
Expand All @@ -121,15 +153,14 @@ func TestResourcePoolServer_Request(t *testing.T) {
require.NoError(t, err)

resourcePool.mock.AssertNumberOfCalls(t, "Select", 1)

sample.test(t, pfs, vfConfig, conn)
sample.test(t, pfs, resourceServerChainElem.getVFConfig(), conn)

// 2. Close

resourcePool.mock.On("Free", pfs[pf2PciAddr].Vfs[1].Addr).
Return(nil)

_, err = server.Close(ctx, conn)
_, err = server.Close(context.TODO(), conn)
require.NoError(t, err)

resourcePool.mock.AssertNumberOfCalls(t, "Free", 1)
Expand Down
57 changes: 0 additions & 57 deletions pkg/networkservice/common/vfconfig/server.go

This file was deleted.