Skip to content

Commit

Permalink
Search for interface names in child net namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-howell committed Mar 2, 2023
1 parent 19d4d0d commit c6b3e6d
Show file tree
Hide file tree
Showing 449 changed files with 44,304 additions and 8,135 deletions.
36 changes: 20 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ require (
github.com/k8snetworkplumbingwg/govdpa v0.1.4
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.1.1-0.20201119153432-9d213757d22d
github.com/k8snetworkplumbingwg/sriovnet v1.1.1-0.20221018183610-ecc82a4b8cfd
github.com/onsi/ginkgo v1.14.0
github.com/onsi/gomega v1.10.1
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.24.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.1
github.com/vishvananda/netlink v1.1.1-0.20211101163509-b10eb8fe5cf6
github.com/vishvananda/netlink v1.2.1-beta.2
google.golang.org/grpc v1.40.0
k8s.io/kubelet v0.24.0
)
Expand All @@ -23,47 +23,46 @@ require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/containernetworking/cni v0.7.1 // indirect
github.com/containernetworking/cni v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.16.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.4 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.4.1 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
k8s.io/api v0.24.0 // indirect
k8s.io/apimachinery v0.24.0 // indirect
Expand All @@ -76,6 +75,11 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)

require (
github.com/containernetworking/plugins v1.2.0
github.com/emicklei/go-restful v2.16.0+incompatible // indirect
)

replace (
github.com/containernetworking/cni => github.com/containernetworking/cni v0.8.1
github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
Expand Down
557 changes: 542 additions & 15 deletions go.sum

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions pkg/devices/gen_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type GenNetDevice struct {
// NewGenNetDevice returns GenNetDevice instance
func NewGenNetDevice(deviceID string, dt types.DeviceType, isRdma bool) (*GenNetDevice, error) {
var netNames []string
var netNamesFromNs []string
var pfName string
var pfAddr string
var funcID int
Expand All @@ -58,6 +59,7 @@ func NewGenNetDevice(deviceID string, dt types.DeviceType, isRdma bool) (*GenNet
return nil, err
}
netNames, _ = utils.GetNetNames(deviceID)
netNamesFromNs, _ = utils.GetNetNamesFromNetns(deviceID)
case types.AuxNetDeviceType:
if pfName, err = utils.GetSriovnetProvider().GetUplinkRepresentorFromAux(deviceID); err != nil {
// AuxNetDeviceType by design should have PF, return error if failed to get PF name
Expand All @@ -77,17 +79,18 @@ func NewGenNetDevice(deviceID string, dt types.DeviceType, isRdma bool) (*GenNet
}

ifName := ""
linkType := ""
if len(netNames) > 0 {
ifName = netNames[0]
}

linkType := ""
if len(ifName) > 0 {
la, err := utils.GetNetlinkProvider().GetLinkAttrs(ifName)
if err != nil {
return nil, err
if len(ifName) > 0 {
la, err := utils.GetNetlinkProvider().GetLinkAttrs(ifName)
if err != nil {
return nil, err
}
linkType = la.EncapType
}
linkType = la.EncapType
} else if len(netNamesFromNs) > 0 {
ifName = netNamesFromNs[0]
}

return &GenNetDevice{
Expand Down
86 changes: 85 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ package utils
import (
"bytes"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/golang/glog"
"github.com/vishvananda/netlink"
)

var (
sysBusPci = "/sys/bus/pci/devices"
sysBusPci = "/sys/bus/pci/devices"
varRunNetns = "/var/run/netns"
// golangci-lint doesn't see it is used in the testing.go
//nolint: unused
sysBusAux = "/sys/bus/auxiliary/devices"
Expand Down Expand Up @@ -372,6 +377,85 @@ func GetNetNames(pciAddr string) ([]string, error) {
return names, nil
}

// GetNetNamesFromNetns returns host net interface names as string for a PCI device from its pci address,
// given that the host net interface has been moved into a child network namespace
func GetNetNamesFromNetns(pciAddr string) ([]string, error) {
networkNamespaces, err := GetNetworkNamespaces()
if err != nil {
return nil, fmt.Errorf("GetNetNamesFromNetns(): failed to get network namespaces: %q", err)
}

// this temporary directory will be used as a mount point for each child namespace
tmpDir, err := os.MkdirTemp("/tmp", "")
if err != nil {
return nil, fmt.Errorf("GetNetNamesFromNetns(): could not create temp dir for mounting namespaces")
}

names := make([]string, 0)
for _, networkNamespace := range networkNamespaces {
netNsDir := filepath.Join(varRunNetns, networkNamespace)
ns.WithNetNSPath(netNsDir, func(childNs ns.NetNS) error {
// mount the network namespace to the previously defined temp directory
if err := syscall.Mount(netNsDir, tmpDir, "sysfs", 0, ""); err != nil {
return fmt.Errorf("GetNetNamesFromNetns(): failed to mount /sys: %v", err)
}

defer syscall.Unmount(tmpDir, 0)

// search for the interface
interfaceDir := fmt.Sprintf("%v/bus/pci/devices/%v/net", tmpDir, pciAddr)
fInfos, err := os.ReadDir(interfaceDir)
if err != nil {
return fmt.Errorf("GetNetNamesFromNetns(): unable to read %v: %w", interfaceDir, err)
}

for _, fInfo := range fInfos {
ifaceName := fInfo.Name()
alias, _ := GetInterfaceAlias(ifaceName)
if alias != "" {
names = append(names, alias)
}
}
return nil
})
}

return names, nil
}

// GetNetworkNamespaces resturns the list of child network namespaces
func GetNetworkNamespaces() ([]string, error) {
fInfos, err := os.ReadDir(varRunNetns)
if err != nil {
return nil, fmt.Errorf("failed to read netns directory %s: %q", varRunNetns, err)
}

names := make([]string, 0)
for _, fInfo := range fInfos {
names = append(names, fInfo.Name())
}
return names, nil
}

// GetInterfaceAlias returns the alias for the interface named by ifaceName
func GetInterfaceAlias(ifaceName string) (string, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return "", fmt.Errorf("failed to get interface %v: %w", ifaceName, err)
}
link, err := netlink.LinkByIndex(iface.Index)
if err != nil {
return "", fmt.Errorf("failed to find link for interface %v: %w", ifaceName, err)
}

attrs := link.Attrs()
if attrs != nil && attrs.Alias != "" {
return attrs.Alias, nil
}
return "", nil

}

// GetDriverName returns current driver attached to a pci device from its pci address
func GetDriverName(pciAddr string) (string, error) {
driverLink := filepath.Join(sysBusPci, pciAddr, "driver")
Expand Down
Loading

0 comments on commit c6b3e6d

Please sign in to comment.