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

Add vlan remote mechanism support (see networkservicemesh/sdk-vpp#399) #355

Merged
merged 1 commit into from
Nov 22, 2021
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
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