Skip to content

Commit

Permalink
Fix chain elements init function
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <[email protected]>
  • Loading branch information
glazychev-art committed Feb 3, 2022
1 parent 6572286 commit fd36f99
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 57 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/thanhpk/randstr v1.0.4
github.com/vishvananda/netlink v1.1.1-0.20220118170537-d6b03fdeb845
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74
go.uber.org/atomic v1.7.0
go.uber.org/goleak v1.1.12
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b
Expand Down
27 changes: 19 additions & 8 deletions pkg/networkservice/mechanisms/vxlan/mtu/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Cisco and/or its affiliates.
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,10 +22,12 @@ import (
"sync"

"git.fd.io/govpp.git/api"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

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

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vxlan"
)
Expand All @@ -34,8 +36,9 @@ type mtuClient struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited atomic.Bool
initMutex sync.Mutex
}

// NewClient - returns client chain element to manage vxlan MTU
Expand Down Expand Up @@ -70,8 +73,16 @@ func (m *mtuClient) Close(ctx context.Context, conn *networkservice.Connection,
}

func (m *mtuClient) init(ctx context.Context) error {
m.initOnce.Do(func() {
m.mtu, m.err = setMTU(ctx, m.vppConn, m.tunnelIP)
})
return m.err
if !m.inited.Load() {
m.initMutex.Lock()
defer m.initMutex.Unlock()

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
m.inited.Store(true)
}
return err
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/networkservice/mechanisms/vxlan/mtu/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -34,7 +34,7 @@ const (
overhead = 50
)

func setMTU(ctx context.Context, vppConn api.Connection, tunnelIP net.IP) (uint32, error) {
func getMTU(ctx context.Context, vppConn api.Connection, tunnelIP net.IP) (uint32, error) {
client, err := interfaces.NewServiceClient(vppConn).SwInterfaceDump(ctx, &interfaces.SwInterfaceDump{})
if err != nil {
return 0, errors.Wrapf(err, "error attempting to get interface dump client to determine MTU for tunnelIP %q", tunnelIP)
Expand Down
27 changes: 19 additions & 8 deletions pkg/networkservice/mechanisms/vxlan/mtu/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Cisco and/or its affiliates.
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,9 +22,11 @@ import (
"sync"

"git.fd.io/govpp.git/api"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"go.uber.org/atomic"
"google.golang.org/protobuf/types/known/emptypb"

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

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vxlan"
)
Expand All @@ -33,8 +35,9 @@ type mtuServer struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited atomic.Bool
initMutex sync.Mutex
}

// NewServer - server chain element to manage vxlan MTU
Expand Down Expand Up @@ -76,8 +79,16 @@ func (m *mtuServer) Close(ctx context.Context, conn *networkservice.Connection)
}

func (m *mtuServer) init(ctx context.Context) error {
m.initOnce.Do(func() {
m.mtu, m.err = setMTU(ctx, m.vppConn, m.tunnelIP)
})
return m.err
if !m.inited.Load() {
m.initMutex.Lock()
defer m.initMutex.Unlock()

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
m.inited.Store(true)
}
return err
}
return nil
}
24 changes: 17 additions & 7 deletions pkg/networkservice/mechanisms/wireguard/mtu/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Cisco and/or its affiliates.
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,6 +22,7 @@ import (
"sync"

"git.fd.io/govpp.git/api"
"go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

Expand All @@ -35,8 +36,9 @@ type mtuClient struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited atomic.Bool
initMutex sync.Mutex
}

// NewClient - returns client chain element to manage wireguard MTU
Expand Down Expand Up @@ -71,8 +73,16 @@ func (m *mtuClient) Close(ctx context.Context, conn *networkservice.Connection,
}

func (m *mtuClient) init(ctx context.Context) error {
m.initOnce.Do(func() {
m.mtu, m.err = setMTU(ctx, m.vppConn, m.tunnelIP)
})
return m.err
if !m.inited.Load() {
m.initMutex.Lock()
defer m.initMutex.Unlock()

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
m.inited.Store(true)
}
return err
}
return nil
}
4 changes: 2 additions & 2 deletions pkg/networkservice/mechanisms/wireguard/mtu/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -34,7 +34,7 @@ const (
overhead = 80
)

func setMTU(ctx context.Context, vppConn api.Connection, tunnelIP net.IP) (uint32, error) {
func getMTU(ctx context.Context, vppConn api.Connection, tunnelIP net.IP) (uint32, error) {
client, err := interfaces.NewServiceClient(vppConn).SwInterfaceDump(ctx, &interfaces.SwInterfaceDump{})
if err != nil {
return 0, errors.Wrapf(err, "error attempting to get interface dump client to determine MTU for tunnelIP %q", tunnelIP)
Expand Down
24 changes: 17 additions & 7 deletions pkg/networkservice/mechanisms/wireguard/mtu/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Cisco and/or its affiliates.
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,6 +22,7 @@ import (
"sync"

"git.fd.io/govpp.git/api"
"go.uber.org/atomic"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -34,8 +35,9 @@ type mtuServer struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited atomic.Bool
initMutex sync.Mutex
}

// NewServer - server chain element to manage wireguard MTU
Expand Down Expand Up @@ -77,8 +79,16 @@ func (m *mtuServer) Close(ctx context.Context, conn *networkservice.Connection)
}

func (m *mtuServer) init(ctx context.Context) error {
m.initOnce.Do(func() {
m.mtu, m.err = setMTU(ctx, m.vppConn, m.tunnelIP)
})
return m.err
if !m.inited.Load() {
m.initMutex.Lock()
defer m.initMutex.Unlock()

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
m.inited.Store(true)
}
return err
}
return nil
}
28 changes: 18 additions & 10 deletions pkg/networkservice/up/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -23,6 +23,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"go.uber.org/atomic"
"google.golang.org/grpc"

"github.com/networkservicemesh/api/pkg/api/networkservice"
Expand All @@ -36,12 +37,12 @@ import (
)

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

ctx context.Context
vppConn Connection
loadIfIndex ifIndexFunc

inited atomic.Bool
initMutex sync.Mutex
}

// NewClient provides a NetworkServiceClient chain elements that 'up's the swIfIndex
Expand Down Expand Up @@ -94,8 +95,15 @@ func (u *upClient) Close(ctx context.Context, conn *networkservice.Connection, o
}

func (u *upClient) init(ctx context.Context) error {
u.Do(func() {
u.initErr = initFunc(ctx, u.vppConn)
})
return u.initErr
if !u.inited.Load() {
u.initMutex.Lock()
defer u.initMutex.Unlock()

err := initFunc(ctx, u.vppConn)
if err == nil {
u.inited.Store(true)
}
return err
}
return nil
}
4 changes: 1 addition & 3 deletions pkg/networkservice/up/peerup/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -19,7 +19,6 @@ package peerup

import (
"context"
"sync"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
Expand All @@ -35,7 +34,6 @@ import (
type peerupClient struct {
ctx context.Context
vppConn Connection
sync.Once
}

// NewClient provides a NetworkServiceClient chain elements that 'up's the peer
Expand Down
28 changes: 18 additions & 10 deletions pkg/networkservice/up/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2021 Cisco and/or its affiliates.
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -22,6 +22,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"go.uber.org/atomic"

"github.com/networkservicemesh/api/pkg/api/networkservice"

Expand All @@ -32,12 +33,12 @@ import (
)

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

ctx context.Context
vppConn Connection
loadIfIndex ifIndexFunc

inited atomic.Bool
initMutex sync.Mutex
}

// NewServer provides a NetworkServiceServer chain elements that 'up's the swIfIndex
Expand Down Expand Up @@ -99,8 +100,15 @@ func (u *upServer) Close(ctx context.Context, conn *networkservice.Connection) (
}

func (u *upServer) init(ctx context.Context) error {
u.Do(func() {
u.initErr = initFunc(ctx, u.vppConn)
})
return u.initErr
if !u.inited.Load() {
u.initMutex.Lock()
defer u.initMutex.Unlock()

err := initFunc(ctx, u.vppConn)
if err == nil {
u.inited.Store(true)
}
return err
}
return nil
}

0 comments on commit fd36f99

Please sign in to comment.