Skip to content

Commit

Permalink
Add WithLoadSwIfIndex option to up chain element
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art authored and edwarnicke committed Nov 8, 2021
1 parent 547d76d commit 6fef5f6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
19 changes: 15 additions & 4 deletions pkg/networkservice/up/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,33 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

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

type upClient struct {
ctx context.Context
vppConn Connection
sync.Once
initErr error

loadIfIndex ifIndexFunc
}

// NewClient provides a NetworkServiceClient chain elements that 'up's the swIfIndex
func NewClient(ctx context.Context, vppConn Connection) networkservice.NetworkServiceClient {
func NewClient(ctx context.Context, vppConn Connection, opts ...Option) networkservice.NetworkServiceClient {
o := &options{
loadIfIndex: ifindex.Load,
}
for _, opt := range opts {
opt(o)
}

return chain.NewNetworkServiceClient(
peerup.NewClient(ctx, vppConn),
&upClient{
ctx: ctx,
vppConn: vppConn,
ctx: ctx,
vppConn: vppConn,
loadIfIndex: o.loadIfIndex,
},
)
}
Expand All @@ -64,7 +75,7 @@ func (u *upClient) Request(ctx context.Context, request *networkservice.NetworkS
return nil, err
}

if err := up(ctx, u.vppConn, metadata.IsClient(u)); err != nil {
if err := up(ctx, u.vppConn, u.loadIfIndex, metadata.IsClient(u)); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

Expand Down
6 changes: 2 additions & 4 deletions pkg/networkservice/up/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"github.com/edwarnicke/govpp/binapi/interface_types"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"

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

// Connection - simply combines tha api.Connection and api.ChannelProvider interfaces
Expand All @@ -36,8 +34,8 @@ type Connection interface {
api.ChannelProvider
}

func up(ctx context.Context, vppConn Connection, isClient bool) error {
swIfIndex, ok := ifindex.Load(ctx, isClient)
func up(ctx context.Context, vppConn Connection, loadIfIndex ifIndexFunc, isClient bool) error {
swIfIndex, ok := loadIfIndex(ctx, isClient)
if !ok {
return nil
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/networkservice/up/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 up

import (
"context"

"github.com/edwarnicke/govpp/binapi/interface_types"
)

type options struct {
loadIfIndex ifIndexFunc
}

// Option is an option pattern for upClient/Server
type Option func(o *options)

// ifIndexFunc is a function to load the interface index
type ifIndexFunc func(ctx context.Context, isClient bool) (value interface_types.InterfaceIndex, ok bool)

// WithLoadSwIfIndex - sets function to load the interface index
func WithLoadSwIfIndex(f ifIndexFunc) Option {
return func(o *options) {
o.loadIfIndex = f
}
}
22 changes: 17 additions & 5 deletions pkg/networkservice/up/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,32 @@ import (

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

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

type upServer struct {
ctx context.Context
vppConn Connection
sync.Once
initErr error

loadIfIndex ifIndexFunc
}

// NewServer provides a NetworkServiceServer chain elements that 'up's the swIfIndex
func NewServer(ctx context.Context, vppConn Connection) networkservice.NetworkServiceServer {
func NewServer(ctx context.Context, vppConn Connection, opts ...Option) networkservice.NetworkServiceServer {
o := &options{
loadIfIndex: ifindex.Load,
}
for _, opt := range opts {
opt(o)
}

return &upServer{
ctx: ctx,
vppConn: vppConn,
ctx: ctx,
vppConn: vppConn,
loadIfIndex: o.loadIfIndex,
}
}

Expand All @@ -56,14 +68,14 @@ func (u *upServer) Request(ctx context.Context, request *networkservice.NetworkS
return nil, err
}

if err := up(ctx, u.vppConn, true); err != nil {
if err := up(ctx, u.vppConn, u.loadIfIndex, true); err != nil {
if closeErr := u.closeOnFailure(postponeCtxFunc, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}
return nil, err
}

if err := up(ctx, u.vppConn, false); err != nil {
if err := up(ctx, u.vppConn, u.loadIfIndex, false); err != nil {
if closeErr := u.closeOnFailure(postponeCtxFunc, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}
Expand Down

0 comments on commit 6fef5f6

Please sign in to comment.