Skip to content

Commit

Permalink
Set the same MTU for the NSC internal interface as the base interface…
Browse files Browse the repository at this point in the history
… has

Closes: networkservicemesh#514

Signed-off-by: Laszlo Kiraly <[email protected]>
  • Loading branch information
ljkiraly committed Mar 8, 2022
1 parent 0ac4937 commit c3d90d8
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/networkservice/connectioncontext/mtu/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Cisco and/or its affiliates.
//
// Copyright (c) 2022 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,6 +24,7 @@ import (

"git.fd.io/govpp.git/api"
interfaces "github.com/edwarnicke/govpp/binapi/interface"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"

Expand All @@ -35,6 +38,9 @@ const (
)

func setVPPL2MTU(ctx context.Context, conn *networkservice.Connection, vppConn api.Connection, isClient bool) error {
if vlan.ToMechanism(conn.GetMechanism()) != nil {
return nil
}
now := time.Now()
swIfIndex, ok := ifindex.Load(ctx, isClient)
if !ok || conn.GetContext().GetMTU() == 0 {
Expand Down
4 changes: 3 additions & 1 deletion pkg/networkservice/mechanisms/vlan/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Nordix Foundation.
// Copyright (c) 2021-2022 Nordix Foundation.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -34,6 +34,7 @@ import (

"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/hwaddress"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/l2vtr"
"github.com/networkservicemesh/sdk-vpp/pkg/networkservice/mechanisms/vlan/mtu"
)

const (
Expand All @@ -49,6 +50,7 @@ type vlanClient struct {
func NewClient(vppConn api.Connection, domain2Device map[string]string) networkservice.NetworkServiceClient {
return chain.NewNetworkServiceClient(
hwaddress.NewClient(vppConn),
mtu.NewClient(vppConn),
l2vtr.NewClient(vppConn),
&vlanClient{
vppConn: vppConn,
Expand Down
86 changes: 86 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2022 Nordix Foundation.
//
// 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 mtu

import (
"context"

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

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

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

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

type mtuClient struct {
vppConn api.Connection
mtu mtuMap
}

// NewClient - returns client chain element to manage vlan MTU
func NewClient(vppConn api.Connection) networkservice.NetworkServiceClient {
return &mtuClient{
vppConn: vppConn,
}
}

func (m *mtuClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
postponeCtxFunc := postpone.ContextWithValues(ctx)

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}
swIfIndex, ok := ifindex.Load(ctx, metadata.IsClient(m))
if !ok {
return conn, nil
}
if mechanism := vlan.ToMechanism(conn.GetMechanism()); mechanism != nil {
localMtu, loaded := m.mtu.Load(swIfIndex)
if !loaded {
localMtu, err = getMTU(ctx, m.vppConn, swIfIndex)
if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
if _, closeErr := m.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}
return nil, err
}
m.mtu.Store(swIfIndex, localMtu)
}
if conn.GetContext().GetMTU() > localMtu || conn.GetContext().GetMTU() == 0 {
if conn.GetContext() == nil {
conn.Context = &networkservice.ConnectionContext{}
}
conn.GetContext().MTU = localMtu
}
}
return conn, nil
}

func (m *mtuClient) Close(ctx context.Context, conn *networkservice.Connection, opts ...grpc.CallOption) (*emptypb.Empty, error) {
return next.Client(ctx).Close(ctx, conn, opts...)
}
51 changes: 51 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2022 Nordix Foundation.
//
// 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 mtu

import (
"context"
"time"

"git.fd.io/govpp.git/api"
"github.com/pkg/errors"

interfaces "github.com/edwarnicke/govpp/binapi/interface"
"github.com/edwarnicke/govpp/binapi/interface_types"
"github.com/networkservicemesh/sdk/pkg/tools/log"
)

func getMTU(ctx context.Context, vppConn api.Connection, swIfIndex interface_types.InterfaceIndex) (uint32, error) {
now := time.Now()
dc, err := interfaces.NewServiceClient(vppConn).SwInterfaceDump(ctx, &interfaces.SwInterfaceDump{
SwIfIndex: swIfIndex,
})
if err != nil {
return 0, errors.Wrapf(err, "error attempting to get interface dump client to determine MTU for swIfIndex %d", swIfIndex)
}
defer func() { _ = dc.Close() }()

details, err := dc.Recv()
if err != nil {
return 0, errors.Wrapf(err, "error attempting to get interface details to determine MTU for swIfIndex %d", swIfIndex)
}
log.FromContext(ctx).
WithField("swIfIndex", swIfIndex).
WithField("details.LinkMtu", details.LinkMtu).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceDump").Debug("completed")
return uint32(details.LinkMtu), nil
}
18 changes: 18 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022 Nordix Foundation.
//
// 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 mtu computes the mtu for the vlan interface and adds it to context
package mtu
26 changes: 26 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2022 Nordix Foundation.
//
// 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 mtu

import (
"sync"
)

//go:generate go-syncmap -output mtu_map.gen.go -type mtuMap<github.com/edwarnicke/govpp/binapi/interface_types.InterfaceIndex,uint32>

// mtuMap - sync.Map with key type interface_types.InterfaceIndex value of int index
type mtuMap sync.Map
75 changes: 75 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/mtu_map.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c3d90d8

Please sign in to comment.