Skip to content

Commit

Permalink
Merge pull request #41 from almaslennikov/sriov_get_uplink
Browse files Browse the repository at this point in the history
Reuse getUplinkRepresentor function from sriovnet
  • Loading branch information
adrianchiris authored Mar 29, 2022
2 parents dc1f764 + 79996f7 commit 4debff2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 82 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/k8snetworkplumbingwg/accelerated-bridge-cni
go 1.13

require (
github.com/Mellanox/sriovnet v1.0.1
github.com/Mellanox/sriovnet v1.1.0
github.com/containernetworking/cni v0.8.0
github.com/containernetworking/plugins v0.8.7
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/rs/zerolog v1.20.0
github.com/spf13/afero v1.6.0
github.com/stretchr/testify v1.5.1
github.com/vishvananda/netlink v1.1.0
github.com/stretchr/testify v1.7.0
github.com/vishvananda/netlink v1.1.1-0.20211101163509-b10eb8fe5cf6
)
25 changes: 14 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Mellanox/sriovnet v1.0.1 h1:g9FqYgcybIuDHKDy8ZEaILeTSXo0r0jzqTFK8rpUSas=
github.com/Mellanox/sriovnet v1.0.1/go.mod h1:zikbXOU755fnTeF858ym1z4BkQsWYOgW4RpoYCXre/g=
github.com/Mellanox/sriovnet v1.1.0 h1:j3KnktNJMHWPTqWXlf27OzQG0ahRO+88NauMjlazyko=
github.com/Mellanox/sriovnet v1.1.0/go.mod h1:P2Epf+52ZaPknkR60EUOvLABXZh3FBymcHPsUfikRVE=
github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/Microsoft/hcsshim v0.8.6/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg=
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
Expand Down Expand Up @@ -49,21 +49,21 @@ github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJ
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8 h1:2c1EFnZHIPCW8qKWgHMH/fX2PkSabFc5mrVzfUNdg5U=
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.4.1/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vishvananda/netlink v0.0.0-20181108222139-023a6dafdcdf/go.mod h1:+SR5DhBJrl6ZM7CoCKvpw5BKroDKQ+PJqOg65H/2ktk=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netlink v1.1.1-0.20211101163509-b10eb8fe5cf6 h1:167a2omrzz+nN9Of6lN/0yOB9itzw+IOioRThNZ30jA=
github.com/vishvananda/netlink v1.1.1-0.20211101163509-b10eb8fe5cf6/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
golang.org/x/crypto v0.0.0-20181009213950-7c1a557ab941/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -77,10 +77,11 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwglRwMkXSBzwVbkOjLLA=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1 h1:sIky/MyNRSHTrdxfsiUSS4WIAMvInbeXljJz+jDjeYE=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -99,3 +100,5 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 8 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"encoding/json"
"fmt"

"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/utils"

localtypes "github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/types"
"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/utils"
)

const (
Expand All @@ -20,11 +19,13 @@ type Loader interface {

// NewConfig create and initialize Config struct
func NewConfig() *Config {
return &Config{}
return &Config{sriovnetProvider: &utils.SriovnetWrapper{}}
}

// Config provides function to load and parse cni configuration
type Config struct{}
type Config struct {
sriovnetProvider utils.SriovnetProvider
}

// LoadConf load data from stdin to NetConf object
func (c *Config) LoadConf(bytes []byte, netConf *localtypes.NetConf) error {
Expand All @@ -50,7 +51,7 @@ func (c *Config) ParseConf(bytes []byte, conf *localtypes.PluginConf) error {

// Get rest of the VF information
var err error
conf.PFName, conf.VFID, err = getVfInfo(conf.DeviceID)
conf.PFName, conf.VFID, err = c.getVfInfo(conf.DeviceID)
if err != nil {
return fmt.Errorf("failed to get VF information: %q", err)
}
Expand Down Expand Up @@ -85,10 +86,10 @@ func (c *Config) ParseConf(bytes []byte, conf *localtypes.PluginConf) error {
return nil
}

func getVfInfo(vfPci string) (string, int, error) {
func (c *Config) getVfInfo(vfPci string) (string, int, error) {
var vfID int

pf, err := utils.GetPfName(vfPci)
pf, err := c.sriovnetProvider.GetUplinkRepresentor(vfPci)
if err != nil {
return "", vfID, err
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package config

import (
"fmt"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"

localtypes "github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/types"
"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/utils/mocks"
)

var _ = Describe("Config", func() {
const existingVfPrefix = "0000:af:06."
const nonExistentVF = "0000:af:07.0"
const existingPF = "enp175s0f1"

mockSriovnet := &mocks.Sriovnet{}
mockSriovnet.On("GetUplinkRepresentor", mock.MatchedBy(func(pciAddr string) bool {
return strings.HasPrefix(pciAddr, existingVfPrefix)
})).Return(existingPF, nil)
mockSriovnet.On("GetUplinkRepresentor", nonExistentVF).Return("", fmt.Errorf("nonexistent VF"))

conf := NewConfig()
conf := Config{sriovnetProvider: mockSriovnet}

Context("Checking ParseConf function", func() {
It("Assuming correct config file - existing DeviceID", func() {
Expand Down Expand Up @@ -134,11 +148,11 @@ var _ = Describe("Config", func() {
})
Context("Checking getVfInfo function", func() {
It("Assuming existing PF", func() {
_, _, err := getVfInfo("0000:af:06.0")
_, _, err := conf.getVfInfo("0000:af:06.0")
Expect(err).NotTo(HaveOccurred())
})
It("Assuming not existing PF", func() {
_, _, err := getVfInfo("0000:af:07.0")
_, _, err := conf.getVfInfo(nonExistentVF)
Expect(err).To(HaveOccurred())
})
})
Expand Down
13 changes: 7 additions & 6 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/vishvananda/netlink"

"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/types"
"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/utils"
)

// Manager provides interface invoke sriov nic related operations
Expand All @@ -23,14 +24,14 @@ type Manager interface {

type manager struct {
nLink Netlink
sriov Sriovnet
sriov utils.SriovnetProvider
}

// NewManager returns an instance of manager
func NewManager() Manager {
return &manager{
nLink: &netlinkWrapper{},
sriov: &sriovnetWrapper{},
sriov: &utils.SriovnetWrapper{},
}
}

Expand Down Expand Up @@ -156,9 +157,9 @@ func (m *manager) ReleaseVF(conf *types.PluginConf, podifName, cid string, netns

func getVfInfo(link netlink.Link, id int) *netlink.VfInfo {
attrs := link.Attrs()
for _, vf := range attrs.Vfs {
if vf.ID == id {
return &vf
for i := range attrs.Vfs {
if attrs.Vfs[i].ID == id {
return &attrs.Vfs[i]
}
}
return nil
Expand All @@ -174,7 +175,7 @@ func (m *manager) ApplyVFConfig(conf *types.PluginConf) error {
// Save current the VF state before modifying it
vfState := getVfInfo(pfLink, conf.VFID)
if vfState == nil {
return fmt.Errorf("failed to find vf %d", conf.VFID)
return fmt.Errorf("failed to find vf %d for PF %s", conf.VFID, conf.PFName)
}

conf.OrigVfState.AdminMAC = vfState.Mac.String() // Save administrative MAC for restoring it later
Expand Down
5 changes: 3 additions & 2 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/manager/mocks"
"github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/types"
sriovMocks "github.com/k8snetworkplumbingwg/accelerated-bridge-cni/pkg/utils/mocks"
)

// Fake NS - implements ns.NetNS interface
Expand Down Expand Up @@ -316,7 +317,7 @@ var _ = Describe("Manager", func() {
})
It("Attaching dummy link to the bridge (success)", func() {
mockedNl := &mocks.Netlink{}
mockedSr := &mocks.Sriovnet{}
mockedSr := &sriovMocks.Sriovnet{}
fakeBridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Index: 1000, Name: "cni0"}}
fakeLink := &FakeLink{netlink.LinkAttrs{
Name: netconf.Representor,
Expand Down Expand Up @@ -346,7 +347,7 @@ var _ = Describe("Manager", func() {
})
It("Attaching dummy link to the bridge (failure)", func() {
mockedNl := &mocks.Netlink{}
mockedSr := &mocks.Sriovnet{}
mockedSr := &sriovMocks.Sriovnet{}
fakeBridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: "cni0"}}
fakeLink := &FakeLink{netlink.LinkAttrs{
Name: netconf.Representor,
Expand Down
16 changes: 0 additions & 16 deletions pkg/manager/sriov.go

This file was deleted.

23 changes: 22 additions & 1 deletion pkg/manager/mocks/Sriovnet.go → pkg/utils/mocks/Sriovnet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/utils/sriov.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

import "github.com/Mellanox/sriovnet"

// SriovnetProvider represents limited subset of functions from sriovnet package
type SriovnetProvider interface {
GetVfRepresentor(string, int) (string, error)
GetUplinkRepresentor(string) (string, error)
}

type SriovnetWrapper struct{}

func (s *SriovnetWrapper) GetVfRepresentor(master string, vfid int) (string, error) {
return sriovnet.GetVfRepresentor(master, vfid)
}

func (s *SriovnetWrapper) GetUplinkRepresentor(vfPciAddress string) (string, error) {
return sriovnet.GetUplinkRepresentor(vfPciAddress)
}
20 changes: 0 additions & 20 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,6 @@ func GetVfid(addr, pfName string) (int, error) {
return id, fmt.Errorf("unable to get VF ID with PF: %s and VF pci address %v", pfName, addr)
}

// GetPfName returns PF net device name of a given VF pci address
func GetPfName(vf string) (string, error) {
pfSymLink := filepath.Join(SysBusPci, vf, "physfn", "net")
_, err := os.Lstat(pfSymLink)
if err != nil {
return "", err
}

files, err := ioutil.ReadDir(pfSymLink)
if err != nil {
return "", err
}

if len(files) < 1 {
return "", fmt.Errorf("PF network device not found")
}

return strings.TrimSpace(files[0].Name()), nil
}

// GetVFLinkName returns VF's network interface name given it's PCI addr
func GetVFLinkName(pciAddr string) (string, error) {
vfDir := filepath.Join(SysBusPci, pciAddr, "net")
Expand Down
13 changes: 0 additions & 13 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

var _ = Describe("Utils", func() {

Context("Checking GetSriovNumVfs function", func() {
It("Assuming existing interface", func() {
result, err := GetSriovNumVfs("enp175s0f1")
Expand All @@ -29,18 +28,6 @@ var _ = Describe("Utils", func() {
Expect(err).To(HaveOccurred(), "Not existing interface should return an error")
})
})
Context("Checking GetPfName function", func() {
It("Assuming existing vf", func() {
result, err := GetPfName("0000:af:06.0")
Expect(err).NotTo(HaveOccurred(), "Existing VF should not return an error")
Expect(result).To(Equal("enp175s0f1"), "Existing VF should return correct PF name")
})
It("Assuming not existing vf", func() {
result, err := GetPfName("0000:af:07.0")
Expect(result).To(Equal(""))
Expect(err).To(HaveOccurred(), "Not existing VF should return an error")
})
})
Context("Checking HasUserspaceDriver function", func() {
It("Use userspace driver", func() {
result, err := HasUserspaceDriver("0000:11:00.0")
Expand Down

0 comments on commit 4debff2

Please sign in to comment.