Skip to content

Commit

Permalink
direct memif functionality controled by option
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Avramenko <avramenkomihail15@gmail.com>
  • Loading branch information
Mixaster995 committed Jul 1, 2021
1 parent 7780191 commit f538ce8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 20 deletions.
13 changes: 0 additions & 13 deletions pkg/networkservice/mechanisms/memif/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,6 @@ func deleteMemif(ctx context.Context, vppConn api.Connection, isClient bool) err

func create(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient bool) error {
if mechanism := memifMech.ToMechanism(conn.GetMechanism()); mechanism != nil {
// Direct memif if applicable
if memifSocketAddDel, ok := load(ctx, true); ok && !isClient {
_, ok := ifindex.Load(ctx, !isClient)
if ok {
if err := del(ctx, conn, vppConn, !isClient); err != nil {
return err
}
mechanism.SetSocketFileURL((&url.URL{Scheme: memifMech.SocketFileScheme, Path: memifSocketAddDel.SocketFilename}).String())
dElete(ctx, !isClient)
ifindex.Delete(ctx, !isClient)
return nil
}
}
// This connection has already been created
if _, ok := ifindex.Load(ctx, isClient); ok {
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/mechanisms/memif/metatdata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 Cisco and/or its affiliates.
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -30,7 +30,7 @@ func store(ctx context.Context, isClient bool, socket *memif.MemifSocketFilename
metadata.Map(ctx, isClient).Store(key{}, socket)
}

func dElete(ctx context.Context, isClient bool) {
func delete(ctx context.Context, isClient bool) {
metadata.Map(ctx, isClient).Delete(key{})
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/networkservice/mechanisms/memif/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 memif

// Option is an option for the connect server
type Option func(s *memifServer)

// WithDirectMemifDisabled turn off direct memif logic
func WithDirectMemifDisabled() Option {
return func(s *memifServer) {
s.directMemifEnabled = false
}
}
41 changes: 36 additions & 5 deletions pkg/networkservice/mechanisms/memif/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,62 @@ package memif

import (
"context"
"net/url"

"git.fd.io/govpp.git/api"
"github.com/golang/protobuf/ptypes/empty"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
memifMech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/memif"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata"

"github.com/networkservicemesh/sdk-vpp/pkg/tools/ifindex"
)

type memifServer struct {
vppConn api.Connection
vppConn api.Connection
directMemifEnabled bool
}

// NewServer provides a NetworkServiceServer chain elements that support the memif Mechanism
func NewServer(vppConn api.Connection) networkservice.NetworkServiceServer {
return &memifServer{
vppConn: vppConn,
func NewServer(vppConn api.Connection, options ...Option) networkservice.NetworkServiceServer {
m := &memifServer{
vppConn: vppConn,
directMemifEnabled: true,
}

for _, opt := range options {
opt(m)
}

return m
}

func (m *memifServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
conn, err := next.Server(ctx).Request(ctx, request)
if err != nil {
return nil, err
}

if mechanism := memifMech.ToMechanism(conn.GetMechanism()); mechanism != nil {
// Direct memif if applicable
if memifSocketAddDel, ok := load(ctx, true); ok && m.directMemifEnabled {
_, ok := ifindex.Load(ctx, true)
if ok {
if err := del(ctx, conn, m.vppConn, true); err != nil {
_, _ = m.Close(ctx, conn)
return nil, err
}
mechanism.SetSocketFileURL((&url.URL{Scheme: memifMech.SocketFileScheme, Path: memifSocketAddDel.SocketFilename}).String())
delete(ctx, true)
ifindex.Delete(ctx, true)
return conn, nil
}
}
}

if err := create(ctx, conn, m.vppConn, metadata.IsClient(m)); err != nil {
_, _ = m.Close(ctx, conn)
return nil, err
Expand Down

0 comments on commit f538ce8

Please sign in to comment.