Skip to content

Commit

Permalink
Merge pull request #517 from almaslennikov/pkey-selector
Browse files Browse the repository at this point in the history
Allow to filter net devices by Infiniband Partition Key
  • Loading branch information
adrianchiris authored Feb 18, 2024
2 parents 7720052 + 93507ca commit 1a99d09
Show file tree
Hide file tree
Showing 28 changed files with 446 additions and 65 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ These selectors are applicable when "deviceType" is "accelerator".
These selectors are applicable when "deviceType" is "netDevice" (note: this is default)


| Field | Required | Description | Type/Defaults | Example/Accepted values |
| Field | Required | Description | Type/Defaults | Example/Accepted values |
|----------------|----------|--------------------------------------------------------------------------|-----------------------------------------------------|--------------------------------------------------------------------------------------------------|
| "vendors" | N | Target device's vendor Hex code as string | `string` list Default: `null` | "vendors": ["8086", "15b3"] |
| "devices" | N | Target Devices' device Hex code as string | `string` list Default: `null` | "devices": ["154c", "1889", "1018"] |
Expand All @@ -337,6 +337,7 @@ These selectors are applicable when "deviceType" is "netDevice" (note: this is d
| "rootDevices" | N | functions from PF matches list of PF PCI addresses | `string` list Default: `null` | "rootDevices": ["0000:86:00.0"] (See follow-up sections for some advance usage of "rootDevices") |
| "linkTypes" | N | The link type of the net device associated with the PCI device | `string` list Default: `null` | "linkTypes": ["ether"] |
| "ddpProfiles" | N | A map of device selectors | `string` list Default: `null` | "ddpProfiles": ["GTPv1-C/U IPv4/IPv6 payload"] |
| "pKeys" | N | Infiniband Partition Keys. Will match only to the devices' default (index0) PKeys. Compatible only with linkTypes = infiniband | `string` list Default: `null` | "pKeys": ["0x1", "0xABCD", "0x50"] |
| "isRdma" | N | Mount RDMA resources. Incompatible with vdpaType | `bool` values `true` or `false` Default: `false` | "isRdma": `true` |
| "needVhostNet" | N | Share /dev/vhost-net and /dev/net/tun | `bool` values `true` or `false` Default: `false` | "needVhostNet": `true` |
| "vdpaType" | N | The type of vDPA device (virtio, vhost). Incompatible with isRdma = true | `string` values `vhost` or `virtio` Default: `null` | "vdpaType": "vhost" |
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/jaypipes/pcidb v1.0.0
github.com/k8snetworkplumbingwg/govdpa v0.1.4
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
github.com/k8snetworkplumbingwg/sriovnet v1.2.0
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -50,7 +50,7 @@ require (
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb // indirect
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.4 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
Expand Down
13 changes: 8 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ github.com/k8snetworkplumbingwg/govdpa v0.1.4 h1:e6mM7JFZkLVJeMQw3px96EigHAhnb4V
github.com/k8snetworkplumbingwg/govdpa v0.1.4/go.mod h1:UQR1xu7A+nnRK1dkLEi12OnNL0OiBPpIKOYDuaQQkck=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 h1:VzM3TYHDgqPkettiP6I6q2jOeQFL4nrJM+UcAc4f6Fs=
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0/go.mod h1:nqCI7aelBJU61wiBeeZWJ6oi4bJy5nrjkM6lWIMA4j0=
github.com/k8snetworkplumbingwg/sriovnet v1.2.0 h1:6ELfAxCB1dvosGUy3DVRmfH+HWTzmPD3W67HKQvMR1M=
github.com/k8snetworkplumbingwg/sriovnet v1.2.0/go.mod h1:jyWzGe6ZtYiPq6ih6aXCOy6mZ49Y9mNyBOLBBXnli+k=
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6 h1:Ho6fhRwqgow7ytMgQ1/55j1gG5AbkxIYRbAhJnXr/MM=
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6/go.mod h1:LuzcqxxXdSgopWe1yo2kQFSgFTz9Ec5qLu6bb0s5Ut4=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down Expand Up @@ -269,8 +269,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/afero v1.9.4 h1:Sd43wM1IWz/s1aVXdOBkjJvuP8UdyqioeE4AmM0QsBs=
github.com/spf13/afero v1.9.4/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -322,7 +322,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -392,6 +392,7 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
Expand Down Expand Up @@ -462,6 +463,7 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -482,6 +484,7 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
Expand Down
2 changes: 2 additions & 0 deletions pkg/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ func (rf *resourceFactory) GetSelector(attr string, values []string) (types.Devi
return resources.NewDdpSelector(values), nil
case "auxTypes":
return resources.NewAuxTypeSelector(values), nil
case "pKeys":
return resources.NewPKeySelector(values), nil
default:
return nil, fmt.Errorf("GetSelector(): invalid attribute %s", attr)
}
Expand Down
34 changes: 24 additions & 10 deletions pkg/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var _ = Describe("Factory", func() {
Entry("rootDevices", "rootDevices", true, reflect.TypeOf(resources.NewRootDeviceSelector([]string{}))),
Entry("linkTypes", "linkTypes", true, reflect.TypeOf(resources.NewLinkTypeSelector([]string{}))),
Entry("ddpProfiles", "ddpProfiles", true, reflect.TypeOf(resources.NewDdpSelector([]string{}))),
Entry("pKeys", "pKeys", true, reflect.TypeOf(resources.NewPKeySelector([]string{}))),
Entry("invalid", "fakeAndInvalid", false, reflect.TypeOf(nil)),
)
Describe("getting resource pool for netdevice", func() {
Expand All @@ -119,6 +120,7 @@ var _ = Describe("Factory", func() {
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
linkTypes := []string{"ether", "infiniband", "other", "other2"}
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
for i := range devs {
d := &mocks.PciNetDevice{}
d.On("GetVendor").Return(vendors[i]).
Expand All @@ -130,7 +132,8 @@ var _ = Describe("Factory", func() {
On("GetPfPciAddr").Return(rootDevices[i]).
On("GetAPIDevice").Return(&pluginapi.Device{}).
On("GetLinkType").Return(linkTypes[i]).
On("GetDDPProfiles").Return(ddpProfiles[i])
On("GetDDPProfiles").Return(ddpProfiles[i]).
On("GetPKey").Return(pKeys[i])
devs[i] = d
}

Expand All @@ -145,7 +148,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}
]`),
)
Expand Down Expand Up @@ -197,6 +201,7 @@ var _ = Describe("Factory", func() {
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
linkTypes := []string{"ether", "infiniband", "other", "other2"}
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
for i := range devs {
d := &mocks.PciNetDevice{}
d.On("GetVendor").Return(vendors[i]).
Expand All @@ -209,7 +214,8 @@ var _ = Describe("Factory", func() {
On("GetAPIDevice").Return(&pluginapi.Device{}).
On("GetLinkType").Return(linkTypes[i]).
On("GetDDPProfiles").Return(ddpProfiles[i]).
On("GetFuncID").Return(-1)
On("GetFuncID").Return(-1).
On("GetPKey").Return(pKeys[i])
devs[i] = d
}

Expand Down Expand Up @@ -254,7 +260,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}`), []string{"0000:03:02.0"}),
Entry("with a slice of one selector object it should match devices", []byte(`
[{
Expand All @@ -265,7 +272,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}]`), []string{"0000:03:02.0"}),
Entry("with more than one selector object, it should match devices from all selector objects", []byte(`
[{
Expand All @@ -276,7 +284,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}, {
"vendors": ["8086"],
"devices": ["1111"],
Expand All @@ -285,7 +294,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["net2"],
"rootDevices": ["0000:86:00.3"],
"linkTypes": ["other2"],
"ddpProfiles": ["PPPoE"]
"ddpProfiles": ["PPPoE"],
"pKeys": ["0x50"]
}]`), []string{"0000:03:02.0", "0000:03:02.3"}),
)
Describe("getting exclusive resource pool for netdevice", func() {
Expand All @@ -308,6 +318,7 @@ var _ = Describe("Factory", func() {
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
linkTypes := []string{"ether", "infiniband", "other", "other2"}
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
for i := range devs {
d := &mocks.PciNetDevice{}
d.On("GetVendor").Return(vendors[i]).
Expand All @@ -320,7 +331,8 @@ var _ = Describe("Factory", func() {
On("GetAPIDevice").Return(&pluginapi.Device{}).
On("GetLinkType").Return(linkTypes[i]).
On("GetDDPProfiles").Return(ddpProfiles[i]).
On("GetFuncID").Return(-1)
On("GetFuncID").Return(-1).
On("GetPKey").Return(pKeys[i])
devs[i] = d
}

Expand All @@ -334,7 +346,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}
`),
)
Expand All @@ -350,7 +363,8 @@ var _ = Describe("Factory", func() {
"pfNames": ["enp2s0f2"],
"rootDevices": ["0000:86:00.0"],
"linkTypes": ["ether"],
"ddpProfiles": ["GTP"]
"ddpProfiles": ["GTP"],
"pKeys": ["0x1"]
}]
`),
)
Expand Down
3 changes: 3 additions & 0 deletions pkg/netdevice/netDeviceProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (np *netDeviceProvider) GetFilteredDevices(devices []types.HostDevice,
// filter by DDP Profiles list
filteredDevice = rf.FilterBySelector("ddpProfiles", nf.DDPProfiles, filteredDevice)

// filter by PKeys list
filteredDevice = rf.FilterBySelector("pKeys", nf.PKeys, filteredDevice)

// filter for rdma devices
if nf.IsRdma {
rdmaDevices := make([]types.HostDevice, 0)
Expand Down
17 changes: 17 additions & 0 deletions pkg/netdevice/pciNetDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ type pciNetDevice struct {
devices.GenPciDevice
devices.GenNetDevice
vdpaDev types.VdpaDevice
pKey string
}

// NewPciNetDevice returns an instance of PciNetDevice interface
//
//nolint:gocyclo
func NewPciNetDevice(dev *ghw.PCIDevice,
rFactory types.ResourceFactory, rc *types.ResourceConfig, selectorIndex int) (types.PciNetDevice, error) {
var vdpaDev types.VdpaDevice
Expand Down Expand Up @@ -95,11 +98,21 @@ func NewPciNetDevice(dev *ghw.PCIDevice,
return nil, err
}

pKey := ""
if netDev.GetLinkType() == "infiniband" {
pciAddr := pciDev.GetPciAddr()
pKey, err = utils.GetPKey(pciAddr)
if err != nil {
glog.Infof("getPKey(): unable to get PKey for device %s : %q", pciAddr, err)
}
}

return &pciNetDevice{
HostDevice: hostDev,
GenPciDevice: *pciDev,
GenNetDevice: *netDev,
vdpaDev: vdpaDev,
pKey: pKey,
}, nil
}

Expand All @@ -116,3 +129,7 @@ func (nd *pciNetDevice) GetDDPProfiles() string {
func (nd *pciNetDevice) GetVdpaDevice() types.VdpaDevice {
return nd.vdpaDev
}

func (nd *pciNetDevice) GetPKey() string {
return nd.pKey
}
27 changes: 27 additions & 0 deletions pkg/resources/pKeySelector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package resources

import (
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
)

type pKeySelector struct {
pKeys []string
}

// NewPKeySelector returns a DeviceSelector interface to filter devices based on available PKeys
func NewPKeySelector(pKeys []string) types.DeviceSelector {
return &pKeySelector{pKeys: pKeys}
}

func (ds *pKeySelector) Filter(inDevices []types.HostDevice) []types.HostDevice {
filteredList := make([]types.HostDevice, 0)

for _, dev := range inDevices {
pKey := dev.(types.PciNetDevice).GetPKey()
if pKey != "" && contains(ds.pKeys, pKey) {
filteredList = append(filteredList, dev)
}
}

return filteredList
}
37 changes: 37 additions & 0 deletions pkg/resources/pKeySelector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package resources_test

import (
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/resources"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types/mocks"

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

var _ = Describe("PKeySelector", func() {
Describe("PKey selector", func() {
Context("filtering", func() {
It("should return devices matching given PKeys", func() {
pKeys := []string{"0x1", "0x2"}
sel := resources.NewPKeySelector(pKeys)

dev0 := mocks.PciNetDevice{}
dev0.On("GetPKey").Return("0x1")

dev1 := mocks.PciNetDevice{}
dev1.On("GetPKey").Return("0x2")

dev2 := mocks.PciNetDevice{}
dev2.On("GetPKey").Return("0x3")

in := []types.HostDevice{&dev0, &dev1, &dev2}
filtered := sel.Filter(in)

Expect(filtered).To(ContainElement(&dev0))
Expect(filtered).To(ContainElement(&dev1))
Expect(filtered).NotTo(ContainElement(&dev2))
})
})
})
})
14 changes: 14 additions & 0 deletions pkg/types/mocks/PciNetDevice.go

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

3 changes: 3 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type NetDeviceSelectors struct {
DDPProfiles []string `json:"ddpProfiles,omitempty"`
NeedVhostNet bool // share vhost-net along the selected resource
VdpaType VdpaType `json:"vdpaType,omitempty"`
PKeys []string `json:"pKeys,omitempty"`
}

// AccelDeviceSelectors contains accelerator(FPGA etc.) related selectors fields
Expand Down Expand Up @@ -281,6 +282,8 @@ type PciNetDevice interface {
GetDDPProfiles() string
// GetVdpaDevice returns VDPA device
GetVdpaDevice() VdpaDevice
// GetPKey return IB Partition key
GetPKey() string
}

// AccelDevice extends PciDevice interface
Expand Down
Loading

0 comments on commit 1a99d09

Please sign in to comment.