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

Fix chain elements init function #506

Merged
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
30 changes: 22 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 @@ -20,12 +20,14 @@ import (
"context"
"net"
"sync"
"sync/atomic"

"git.fd.io/govpp.git/api"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"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 uint32
initMutex sync.Mutex
}

// NewClient - returns client chain element to manage vxlan MTU
Expand Down Expand Up @@ -70,8 +73,19 @@ 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 atomic.LoadUint32(&m.inited) > 0 {
return nil
}
m.initMutex.Lock()
defer m.initMutex.Unlock()
if atomic.LoadUint32(&m.inited) > 0 {
return nil
}

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
atomic.StoreUint32(&m.inited, 1)
}
return err
}
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
30 changes: 22 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 @@ -20,11 +20,13 @@ import (
"context"
"net"
"sync"
"sync/atomic"

"git.fd.io/govpp.git/api"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"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 uint32
initMutex sync.Mutex
}

// NewServer - server chain element to manage vxlan MTU
Expand Down Expand Up @@ -76,8 +79,19 @@ 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 atomic.LoadUint32(&m.inited) > 0 {
return nil
}
m.initMutex.Lock()
defer m.initMutex.Unlock()
if atomic.LoadUint32(&m.inited) > 0 {
return nil
}

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
atomic.StoreUint32(&m.inited, 1)
}
return err
}
27 changes: 20 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 @@ -20,6 +20,7 @@ import (
"context"
"net"
"sync"
"sync/atomic"

"git.fd.io/govpp.git/api"
"google.golang.org/grpc"
Expand All @@ -35,8 +36,9 @@ type mtuClient struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited uint32
initMutex sync.Mutex
}

// NewClient - returns client chain element to manage wireguard MTU
Expand Down Expand Up @@ -71,8 +73,19 @@ 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 atomic.LoadUint32(&m.inited) > 0 {
return nil
}
m.initMutex.Lock()
defer m.initMutex.Unlock()
if atomic.LoadUint32(&m.inited) > 0 {
return nil
}

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
atomic.StoreUint32(&m.inited, 1)
}
return err
}
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
27 changes: 20 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 @@ -20,6 +20,7 @@ import (
"context"
"net"
"sync"
"sync/atomic"

"git.fd.io/govpp.git/api"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -34,8 +35,9 @@ type mtuServer struct {
vppConn api.Connection
tunnelIP net.IP
mtu uint32
initOnce sync.Once
err error

inited uint32
initMutex sync.Mutex
}

// NewServer - server chain element to manage wireguard MTU
Expand Down Expand Up @@ -77,8 +79,19 @@ 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 atomic.LoadUint32(&m.inited) > 0 {
return nil
}
m.initMutex.Lock()
defer m.initMutex.Unlock()
if atomic.LoadUint32(&m.inited) > 0 {
return nil
}

var err error
m.mtu, err = getMTU(ctx, m.vppConn, m.tunnelIP)
if err == nil {
atomic.StoreUint32(&m.inited, 1)
}
return err
}
31 changes: 21 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 @@ -20,6 +20,7 @@ package up
import (
"context"
"sync"
"sync/atomic"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
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 uint32
initMutex sync.Mutex
}

// NewClient provides a NetworkServiceClient chain elements that 'up's the swIfIndex
Expand Down Expand Up @@ -94,8 +95,18 @@ 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 atomic.LoadUint32(&u.inited) > 0 {
return nil
}
u.initMutex.Lock()
defer u.initMutex.Unlock()
if atomic.LoadUint32(&u.inited) > 0 {
return nil
}

err := initFunc(ctx, u.vppConn)
if err == nil {
atomic.StoreUint32(&u.inited, 1)
}
return err
}
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
31 changes: 21 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 @@ -19,6 +19,7 @@ package up
import (
"context"
"sync"
"sync/atomic"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
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 uint32
initMutex sync.Mutex
}

// NewServer provides a NetworkServiceServer chain elements that 'up's the swIfIndex
Expand Down Expand Up @@ -99,8 +100,18 @@ 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 atomic.LoadUint32(&u.inited) > 0 {
return nil
}
u.initMutex.Lock()
defer u.initMutex.Unlock()
if atomic.LoadUint32(&u.inited) > 0 {
return nil
}

err := initFunc(ctx, u.vppConn)
if err == nil {
atomic.StoreUint32(&u.inited, 1)
}
return err
}