Skip to content

Commit

Permalink
Add vlan remote mechanism support (see networkservicemesh/sdk-vpp#399)
Browse files Browse the repository at this point in the history
Fixed search for link by PCI address in pci devices subdirectories
Added code for procssing ethernet context set by remote vlan mechanism

Signed-off-by: Laszlo Kiraly <laszlo.kiraly@est.tech>
  • Loading branch information
ljkiraly committed Nov 15, 2021
1 parent 603e70a commit 055f0a1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
33 changes: 30 additions & 3 deletions pkg/kernel/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ func FindHostDevice(pciAddress, name string, namespaces ...netns.NsHandle) (Link
}
}
}

return nil, errors.Errorf("failed to obtain netlink link matching criteria: name=%s or pciAddress=%s", name, pciAddress)
}

Expand All @@ -211,8 +210,9 @@ func searchByPCIAddress(ns netns.NsHandle, name, pciAddress string) (netlink.Lin
return nil, errors.Errorf("failed to enter namespace: %s", err)
}

netDir := filepath.Join("/sys/bus/pci/devices", pciAddress, "net")
if _, err = os.Lstat(netDir); err != nil {
pciDevicePath := filepath.Join("/sys/bus/pci/devices", pciAddress)
netDir, err := findNetDir(pciDevicePath)
if err != nil {
return nil, errors.Errorf("no net directory under pci device %s: %q", pciAddress, err)
}

Expand All @@ -238,6 +238,33 @@ func searchByPCIAddress(ns netns.NsHandle, name, pciAddress string) (netlink.Lin
return link, nil
}

func findNetDir(basePath string) (string, error) {
subDir := filepath.Join(basePath, "net")
if _, err := os.Lstat(subDir); err == nil {
return subDir, nil
}
files, err := ioutil.ReadDir(basePath)
if err != nil {
return "", errors.Wrapf(err, "failed to read directory %s", basePath)
}
for _, file := range files {
if file.IsDir() {
subDir = filepath.Join(basePath, file.Name())
subdirFiles, err := ioutil.ReadDir(subDir)
if err != nil {
return "", errors.Wrapf(err, "failed to read subdirectory %s", subDir)
}
for _, subdirFile := range subdirFiles {
if subdirFile.IsDir() && subdirFile.Name() == "net" {
subDir = filepath.Join(subDir, subdirFile.Name())
return subDir, nil
}
}
}
}
return "", errors.Errorf("failed to find net directory")
}

func searchByName(ns netns.NsHandle, name, pciAddress string) (netlink.Link, error) {
// execute in context of the pod's namespace
err := netns.Set(ns)
Expand Down
11 changes: 11 additions & 0 deletions pkg/kernel/networkservice/ethernetcontext/vf_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ func (i *vfEthernetClient) Request(ctx context.Context, request *networkservice.
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}
} else {
if err := setKernelHwAddress(ctx, conn, true); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

if _, closeErr := i.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/kernel/networkservice/ethernetcontext/vf_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,70 @@
package ethernetcontext

import (
"context"
"net"
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel"
"github.com/networkservicemesh/sdk/pkg/tools/log"
"github.com/pkg/errors"
"github.com/vishvananda/netlink"

link "github.com/networkservicemesh/sdk-kernel/pkg/kernel"
"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/vfconfig"
)

func setKernelHwAddress(ctx context.Context, conn *networkservice.Connection, isClient bool) error {
if mechanism := kernel.ToMechanism(conn.GetMechanism()); mechanism != nil {
netlinkHandle, err := link.GetNetlinkHandle(mechanism.GetNetNSURL())
if err != nil {
return errors.WithStack(err)
}
defer netlinkHandle.Delete()

ifName := mechanism.GetInterfaceName()

l, err := netlinkHandle.LinkByName(ifName)
if err != nil {
return errors.WithStack(err)
}

if ethernetContext := conn.GetContext().GetEthernetContext(); ethernetContext != nil {
var macAddrString string
if isClient {
macAddrString = ethernetContext.GetDstMac()
} else {
macAddrString = ethernetContext.GetSrcMac()
}

if macAddrString != "" {
now := time.Now()
var macAddr net.HardwareAddr
macAddr, err := net.ParseMAC(macAddrString)
if err != nil {
return errors.Wrapf(err, "invalid MAC address: %v", macAddrString)
}
if err = netlinkHandle.LinkSetDown(l); err != nil {
return errors.WithStack(err)
}
if err = netlinkHandle.LinkSetHardwareAddr(l, macAddr); err != nil {
return errors.Wrapf(err, "failed to set MAC address for the VF: %v", macAddr)
}
if err = netlinkHandle.LinkSetUp(l); err != nil {
return errors.WithStack(err)
}
log.FromContext(ctx).
WithField("link.Name", l.Attrs().Name).
WithField("MACAddr", macAddrString).
WithField("duration", time.Since(now)).
WithField("netlink", "LinkSetHardwareAddr").Debug("completed")
}
}
}
return nil
}

func vfCreate(vfConfig *vfconfig.VFConfig, conn *networkservice.Connection, isClient bool) error {
pfLink, err := netlink.LinkByName(vfConfig.PFInterfaceName)
if err != nil {
Expand Down
23 changes: 22 additions & 1 deletion pkg/kernel/networkservice/ethernetcontext/vf_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

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

"github.com/networkservicemesh/sdk-kernel/pkg/kernel/networkservice/vfconfig"
)
Expand All @@ -38,13 +40,32 @@ func NewVFServer() networkservice.NetworkServiceServer {
}

func (s *vfEthernetContextServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
postponeCtxFunc := postpone.ContextWithValues(ctx)
if vfConfig, ok := vfconfig.Load(ctx, false); ok {
err := vfCreate(vfConfig, request.Connection, false)
if err != nil {
return nil, err
}
}
return next.Server(ctx).Request(ctx, request)

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

if _, ok := vfconfig.Load(ctx, false); !ok {
if err := setKernelHwAddress(ctx, conn, false); err != nil {
closeCtx, cancelClose := postponeCtxFunc()
defer cancelClose()

if _, closeErr := s.Close(closeCtx, conn); closeErr != nil {
err = errors.Wrapf(err, "connection closed with error: %s", closeErr.Error())
}

return nil, err
}
}
return conn, nil
}

func (s *vfEthernetContextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
Expand Down

0 comments on commit 055f0a1

Please sign in to comment.