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

[switchdev 3/x] Extend host pkg with required functions #588

Merged
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/jaypipes/ghw v0.9.0
github.com/k8snetworkplumbingwg/govdpa v0.1.4
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/k8snetworkplumbingwg/sriov-network-device-plugin v0.0.0-20221127172732-a5a7395122e3
github.com/onsi/ginkgo/v2 v2.11.0
Expand Down Expand Up @@ -91,7 +92,6 @@ require (
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k8snetworkplumbingwg/govdpa v0.1.4 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand Down
13 changes: 12 additions & 1 deletion pkg/consts/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,18 @@ const (
UdevRulesFolder = UdevFolder + "/rules.d"
HostUdevRulesFolder = Host + UdevRulesFolder
UdevDisableNM = "/bindata/scripts/udev-find-sriov-pf.sh"
NMUdevRule = "SUBSYSTEM==\"net\", ACTION==\"add|change|move\", ATTRS{device}==\"%s\", IMPORT{program}=\"/etc/udev/disable-nm-sriov.sh $env{INTERFACE} %s\""
// nolint:goconst
NMUdevRule = `SUBSYSTEM=="net", ` +
`ACTION=="add|change|move", ` +
`ATTRS{device}=="%s", ` +
`IMPORT{program}="/etc/udev/disable-nm-sriov.sh $env{INTERFACE} %s"`
// nolint:goconst
SwitchdevUdevRule = `SUBSYSTEM=="net", ` +
`ACTION=="add|move", ` +
`ATTRS{phys_switch_id}=="%s", ` +
`ATTR{phys_port_name}=="pf%svf*", ` +
`IMPORT{program}="/etc/udev/switchdev-vf-link-name.sh $attr{phys_port_name}", ` +
`NAME="%s_$env{NUMBER}"`

KernelArgPciRealloc = "pci=realloc"
KernelArgIntelIommu = "intel_iommu=on"
Expand Down
84 changes: 84 additions & 0 deletions pkg/helper/mock/mock_helper.go

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

64 changes: 23 additions & 41 deletions pkg/host/internal/kernel/kernel_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
package kernel

import (
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/helpers"
)

func assertFileContentsEquals(path, expectedContent string) {
d, err := os.ReadFile(filepath.Join(vars.FilesystemRoot, path))
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, string(d)).To(Equal(expectedContent))
}

var _ = Describe("Kernel", func() {
Context("Drivers", func() {
var (
k types.KernelInterface
)
configureFS := func(f *fakefilesystem.FS) {
var (
cleanFakeFs func()
err error
)
vars.FilesystemRoot, cleanFakeFs, err = f.Use()
Expect(err).ToNot(HaveOccurred())
DeferCleanup(cleanFakeFs)
}
BeforeEach(func() {
k = New(nil)
})
Expand All @@ -41,11 +23,11 @@ var _ = Describe("Kernel", func() {
Expect(k.UnbindDriverByBusAndDevice(consts.BusPci, "unknown-dev")).NotTo(HaveOccurred())
})
It("known device, no driver", func() {
configureFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}})
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}})
Expect(k.Unbind("0000:d8:00.0")).NotTo(HaveOccurred())
})
It("has driver, succeed", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/test-driver"},
Expand All @@ -56,10 +38,10 @@ var _ = Describe("Kernel", func() {
})
Expect(k.Unbind("0000:d8:00.0")).NotTo(HaveOccurred())
// check that echo to unbind path was done
assertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
})
It("has driver, failed to unbind", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0"},
Symlinks: map[string]string{
Expand All @@ -75,13 +57,13 @@ var _ = Describe("Kernel", func() {
Expect(driver).To(BeEmpty())
})
It("known device, no driver", func() {
configureFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}})
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{Dirs: []string{"/sys/bus/pci/devices/0000:d8:00.0"}})
has, driver := k.HasDriver("0000:d8:00.0")
Expect(has).To(BeFalse())
Expect(driver).To(BeEmpty())
})
It("has driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/test-driver"},
Expand All @@ -98,18 +80,18 @@ var _ = Describe("Kernel", func() {
Expect(k.BindDefaultDriver("unknown-dev")).To(HaveOccurred())
})
It("no driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0"},
Files: map[string][]byte{
"/sys/bus/pci/drivers_probe": {}, "/sys/bus/pci/devices/0000:d8:00.0/driver_override": {}},
})
Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred())
// should probe driver for dev
assertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0")
})
It("already bind to default driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0"},
Symlinks: map[string]string{
Expand All @@ -118,7 +100,7 @@ var _ = Describe("Kernel", func() {
Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred())
})
It("bind to dpdk driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/vfio-pci"},
Expand All @@ -130,17 +112,17 @@ var _ = Describe("Kernel", func() {
})
Expect(k.BindDefaultDriver("0000:d8:00.0")).NotTo(HaveOccurred())
// should unbind from dpdk driver
assertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/unbind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/unbind", "0000:d8:00.0")
// should probe driver for dev
assertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers_probe", "0000:d8:00.0")
})
})
Context("BindDpdkDriver", func() {
It("unknown device", func() {
Expect(k.BindDpdkDriver("unknown-dev", "vfio-pci")).To(HaveOccurred())
})
It("no driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/vfio-pci"},
Expand All @@ -149,10 +131,10 @@ var _ = Describe("Kernel", func() {
})
Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred())
// should reset driver override
assertFileContentsEquals("/sys/bus/pci/devices/0000:d8:00.0/driver_override", "\x00")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/devices/0000:d8:00.0/driver_override", "\x00")
})
It("already bind to required driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0"},
Symlinks: map[string]string{
Expand All @@ -161,7 +143,7 @@ var _ = Describe("Kernel", func() {
Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred())
})
It("bind to wrong driver", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/test-driver",
Expand All @@ -175,12 +157,12 @@ var _ = Describe("Kernel", func() {
})
Expect(k.BindDpdkDriver("0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred())
// should unbind from driver1
assertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
// should bind to driver2
assertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0")
})
It("fail to bind", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/test-driver"},
Expand All @@ -195,7 +177,7 @@ var _ = Describe("Kernel", func() {
})
Context("BindDriverByBusAndDevice", func() {
It("device doesn't support driver_override", func() {
configureFS(&fakefilesystem.FS{
helpers.GinkgoConfigureFakeFS(&fakefilesystem.FS{
Dirs: []string{
"/sys/bus/pci/devices/0000:d8:00.0",
"/sys/bus/pci/drivers/test-driver",
Expand All @@ -208,9 +190,9 @@ var _ = Describe("Kernel", func() {
})
Expect(k.BindDriverByBusAndDevice(consts.BusPci, "0000:d8:00.0", "vfio-pci")).NotTo(HaveOccurred())
// should unbind from driver1
assertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/test-driver/unbind", "0000:d8:00.0")
// should bind to driver2
assertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0")
helpers.GinkgoAssertFileContentsEquals("/sys/bus/pci/drivers/vfio-pci/bind", "0000:d8:00.0")
})
})
})
Expand Down
40 changes: 40 additions & 0 deletions pkg/host/internal/lib/govdpa/govdpa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package govdpa

import (
"github.com/k8snetworkplumbingwg/govdpa/pkg/kvdpa"
)

func New() GoVdpaLib {
return &libWrapper{}
}

type VdpaDevice interface {
kvdpa.VdpaDevice
}

//go:generate ../../../../../bin/mockgen -destination mock/mock_govdpa.go -source govdpa.go
type GoVdpaLib interface {
// GetVdpaDevice returns the vdpa device information by a vdpa device name
GetVdpaDevice(vdpaDeviceName string) (VdpaDevice, error)
// AddVdpaDevice adds a new vdpa device to the given management device
AddVdpaDevice(mgmtDeviceName string, vdpaDeviceName string) error
// DeleteVdpaDevice deletes a vdpa device
DeleteVdpaDevice(vdpaDeviceName string) error
}

type libWrapper struct{}

// GetVdpaDevice returns the vdpa device information by a vdpa device name
func (w *libWrapper) GetVdpaDevice(name string) (VdpaDevice, error) {
return kvdpa.GetVdpaDevice(name)
}

// AddVdpaDevice adds a new vdpa device to the given management device
func (w *libWrapper) AddVdpaDevice(mgmtDeviceName string, vdpaDeviceName string) error {
return kvdpa.AddVdpaDevice(mgmtDeviceName, vdpaDeviceName)
}

// DeleteVdpaDevice deletes a vdpa device
func (w *libWrapper) DeleteVdpaDevice(name string) error {
return kvdpa.DeleteVdpaDevice(name)
}
Loading
Loading