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 host
interface MTU in vpp instead of the sub-interface MTU.

Related PR: networkservicemesh/cmd-nse-remote-vlan/pull/107

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Nov 10, 2022
1 parent db9c967 commit e3b851e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pkg/networkservice/mechanisms/vlan/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +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),
mtu.NewClient(vppConn, domain2Device),
l2vtr.NewClient(vppConn),
&vlanClient{
vppConn: vppConn,
Expand Down
23 changes: 15 additions & 8 deletions pkg/networkservice/mechanisms/vlan/mtu/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ import (
)

type mtuClient struct {
vppConn api.Connection
mtu mtuMap
vppConn api.Connection
mtu mtuMap
deviceNames map[string]string
}

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

Expand All @@ -53,14 +55,19 @@ func (m *mtuClient) Request(ctx context.Context, request *networkservice.Network
if err != nil {
return nil, err
}
swIfIndex, ok := ifindex.Load(ctx, metadata.IsClient(m))
_, 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)
via := conn.GetLabels()[viaLabel]
hostIFName, ok := m.deviceNames[via]
if !ok {
return nil, errors.New("can not find device name for via label")
}
localMtu, loaded := m.mtu.Load(hostIFName)
if !loaded {
localMtu, err = getL3MTU(ctx, m.vppConn, swIfIndex)
localMtu, err = getMTU(ctx, m.vppConn, hostIFName)
if err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()
Expand All @@ -69,7 +76,7 @@ func (m *mtuClient) Request(ctx context.Context, request *networkservice.Network
}
return nil, err
}
m.mtu.Store(swIfIndex, localMtu)
m.mtu.Store(hostIFName, localMtu)
}
if conn.GetContext().GetMTU() > localMtu || conn.GetContext().GetMTU() == 0 {
if conn.GetContext() == nil {
Expand Down
43 changes: 27 additions & 16 deletions pkg/networkservice/mechanisms/vlan/mtu/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,47 @@ package mtu

import (
"context"
"io"
"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"
)

const l3MtuIndex = 0

func getL3MTU(ctx context.Context, vppConn api.Connection, swIfIndex interface_types.InterfaceIndex) (uint32, error) {
func getMTU(ctx context.Context, vppConn api.Connection, ifName string) (uint32, error) {
now := time.Now()
dc, err := interfaces.NewServiceClient(vppConn).SwInterfaceDump(ctx, &interfaces.SwInterfaceDump{
SwIfIndex: swIfIndex,
NameFilterValid: true,
NameFilter: ifName,
})
if err != nil {
return 0, errors.Wrapf(err, "error attempting to get interface dump client to determine MTU for swIfIndex %d", swIfIndex)
return 0, errors.Wrapf(err, "failed to get interface dump client to determine MTU on %s", ifName)
}
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)
for {
details, err := dc.Recv()
if err == io.EOF {
break
}
if err != nil {
return 0, errors.Wrapf(err, "failed to get interface details to determine MTU on %s", ifName)
}

if (ifName != details.InterfaceName) && (afPacketNamePrefix+ifName != details.InterfaceName) {
log.FromContext(ctx).
WithField("InterfaceName", details.InterfaceName).
WithField("vppapi", "SwInterfaceDetails").Debug("skipped")
continue
}
log.FromContext(ctx).
WithField("InterfaceName", details.InterfaceName).
WithField("L3 MTU", details.Mtu[l3MtuIndex]).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceDump").Debug("completed")
return details.Mtu[l3MtuIndex], nil
}
log.FromContext(ctx).
WithField("swIfIndex", swIfIndex).
WithField("details.LinkMtu", details.LinkMtu).
WithField("duration", time.Since(now)).
WithField("vppapi", "SwInterfaceDump").Debug("completed")
return details.Mtu[l3MtuIndex], nil
return 0, errors.New("unable to find interface in vpp")
}
26 changes: 26 additions & 0 deletions pkg/networkservice/mechanisms/vlan/mtu/const.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

const (
// In remote vlan mechanism this label used to identify the host interface
viaLabel = "via"

// vpp constants
afPacketNamePrefix = "host-"
l3MtuIndex = 0
)
4 changes: 2 additions & 2 deletions pkg/networkservice/mechanisms/vlan/mtu/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"sync"
)

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

// mtuMap - sync.Map with key type interface_types.InterfaceIndex value of int index
// mtuMap - sync.Map with key as interface name and value as MTU
type mtuMap sync.Map
18 changes: 8 additions & 10 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 e3b851e

Please sign in to comment.