Skip to content

Commit

Permalink
Fix MTU adjustment in case of remote vlan mechanism
Browse files Browse the repository at this point in the history
The MTU should be updated in connection context based on the base interface MTU

Relates PR: networkservicemesh/sdk-vpp/pull/656

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Nov 10, 2022
1 parent e19ec59 commit 668a540
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/networkservice/mechanisms/vlan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls"
vlanmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk-ovs/pkg/networkservice/mechanisms/vlan/mtu"
"github.com/networkservicemesh/sdk-ovs/pkg/tools/ifnames"
ovsutil "github.com/networkservicemesh/sdk-ovs/pkg/tools/utils"
)
Expand All @@ -51,7 +53,10 @@ type vlanClient struct {

// NewClient returns a client chain element implementing VLAN breakout for NS client
func NewClient(bridgeName string, l2Connections map[string]*ovsutil.L2ConnectionPoint) networkservice.NetworkServiceClient {
return &vlanClient{bridgeName: bridgeName, l2Connections: l2Connections}
return chain.NewNetworkServiceClient(
mtu.NewClient(l2Connections),
&vlanClient{bridgeName: bridgeName, l2Connections: l2Connections},
)
}

func (c *vlanClient) Request(ctx context.Context, request *networkservice.NetworkServiceRequest, opts ...grpc.CallOption) (*networkservice.Connection, error) {
Expand Down
89 changes: 89 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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"

"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vlan"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/networkservicemesh/sdk/pkg/tools/postpone"

ovsutil "github.com/networkservicemesh/sdk-ovs/pkg/tools/utils"
)

const (
viaLabel = "via"
)

type mtuClient struct {
l2Connections map[string]*ovsutil.L2ConnectionPoint
}

// NewClient - returns client chain element to manage vlan MTU
func NewClient(l2Connections map[string]*ovsutil.L2ConnectionPoint) networkservice.NetworkServiceClient {
return &mtuClient{
l2Connections: l2Connections,
}
}

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

conn, err := next.Client(ctx).Request(ctx, request, opts...)
if err != nil {
return nil, err
}

if mechanism := vlan.ToMechanism(conn.GetMechanism()); mechanism != nil {
viaSelector, ok := conn.GetLabels()[viaLabel]
if !ok {
return conn, nil
}
l2Point, ok := m.l2Connections[viaSelector]
if !ok {
return conn, nil
}
localMTU, err := getMTU(l2Point, logger)
if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
if _, closeErr := m.Close(closeCtx, conn, opts...); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}
return nil, err
}
if localMTU > 0 && (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...)
}
42 changes: 42 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 (
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"
"github.com/vishvananda/netlink"

ovsutil "github.com/networkservicemesh/sdk-ovs/pkg/tools/utils"
)

func getMTU(l2CP *ovsutil.L2ConnectionPoint, logger log.Logger) (uint32, error) {
if l2CP.Interface == "" {
return 0, nil
}
link, err := netlink.LinkByName(l2CP.Interface)
if err != nil {
return 0, nil
}
mtu := link.Attrs().MTU
logger.Infof("Get MTU of interface %s %d", l2CP.Interface, mtu)
if mtu >= 0 && mtu <= 65535 {
return uint32(mtu), nil
}

return 0, errors.New("invalid MTU value")
}
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

0 comments on commit 668a540

Please sign in to comment.