From 6925316542f1239095e1c71a02f6bb761b99de82 Mon Sep 17 00:00:00 2001 From: Artem Glazychev Date: Tue, 9 Nov 2021 06:30:28 +0700 Subject: [PATCH] Add WithLoadSwIfIndex option to up chain element (#381) Signed-off-by: Artem Glazychev --- pkg/networkservice/up/client.go | 19 ++++++++++++---- pkg/networkservice/up/common.go | 6 ++--- pkg/networkservice/up/option.go | 40 +++++++++++++++++++++++++++++++++ pkg/networkservice/up/server.go | 22 +++++++++++++----- 4 files changed, 74 insertions(+), 13 deletions(-) create mode 100644 pkg/networkservice/up/option.go diff --git a/pkg/networkservice/up/client.go b/pkg/networkservice/up/client.go index ad7928a8..d3e629fc 100644 --- a/pkg/networkservice/up/client.go +++ b/pkg/networkservice/up/client.go @@ -32,6 +32,7 @@ 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 { @@ -39,15 +40,25 @@ type upClient struct { 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, }, ) } @@ -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() diff --git a/pkg/networkservice/up/common.go b/pkg/networkservice/up/common.go index a4497283..1be59229 100644 --- a/pkg/networkservice/up/common.go +++ b/pkg/networkservice/up/common.go @@ -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 @@ -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 } diff --git a/pkg/networkservice/up/option.go b/pkg/networkservice/up/option.go new file mode 100644 index 00000000..4a5746bb --- /dev/null +++ b/pkg/networkservice/up/option.go @@ -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 + } +} diff --git a/pkg/networkservice/up/server.go b/pkg/networkservice/up/server.go index d186ba29..a9bde838 100644 --- a/pkg/networkservice/up/server.go +++ b/pkg/networkservice/up/server.go @@ -27,6 +27,8 @@ 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 { @@ -34,13 +36,23 @@ type upServer struct { 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, } } @@ -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()) }