Skip to content

Commit 81a0e6a

Browse files
authored
Add acpiIndex selector (#488)
acpiIndex selector selects devices based on their ACPI index if available. This is useful e.g in VM environment where a device's ACPI index can be predefined during VM creation time, device selection can then be based on that index.
1 parent 2cc723d commit 81a0e6a

File tree

15 files changed

+210
-48
lines changed

15 files changed

+210
-48
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ These selectors are applicable when "deviceType" is "accelerator".
309309
| "devices" | N | Target Devices' device Hex code as string | `string` list Default: `null` | "devices": ["154c", "1889", "1018"] |
310310
| "drivers" | N | Target device driver names as string | `string` list Default: `null` | "drivers": ["vfio-pci"] |
311311
| "pciAddresses" | N | Target device's pci address as string | `string` list Default: `null` | "pciAddresses": ["0000:03:02.0"] |
312+
| "acpiIndexes" | N | Target device's acpi index as string | `string` list Default: `null` | "acpiIndexes": ["101"] |
312313

313314

314315
#### Network devices selectors
@@ -321,6 +322,7 @@ These selectors are applicable when "deviceType" is "netDevice" (note: this is d
321322
| "devices" | N | Target Devices' device Hex code as string | `string` list Default: `null` | "devices": ["154c", "1889", "1018"] |
322323
| "drivers" | N | Target device driver names as string | `string` list Default: `null` | "drivers": ["vfio-pci"] |
323324
| "pciAddresses" | N | Target device's pci address as string | `string` list Default: `null` | "pciAddresses": ["0000:03:02.0"] |
325+
| "acpiIndexes" | N | Target device's acpi index as string | `string` list Default: `null` | "acpiIndexes": ["101"] |
324326
| "pfNames" | N | functions from PF matches list of PF names | `string` list Default: `null` | "pfNames": ["enp2s2f0"] (See follow-up sections for some advance usage of "pfNames") |
325327
| "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") |
326328
| "linkTypes" | N | The link type of the net device associated with the PCI device | `string` list Default: `null` | "linkTypes": ["ether"] |
@@ -441,10 +443,11 @@ The device plugin will initially discover all PCI network resources in the host
441443
2. "devices" - The device hex code of device
442444
3. "drivers" - The driver name the device is registered with
443445
4. "pciAddresses" - The pci address of the device in BDF notation (if device type is accelerator or netDevice)
444-
5. "auxTypes" - The type of auxiliary network device (if device type is auxNetDevice).
445-
6. "pfNames" - The Physical function name
446-
7. "rootDevices" - The Physical function PCI address
447-
8. "linkTypes" - The link type of the net device associated with the PCI device
446+
5. "acpiIndexes" - The acpi index of the device
447+
6. "auxTypes" - The type of auxiliary network device (if device type is auxNetDevice).
448+
7. "pfNames" - The Physical function name
449+
8. "rootDevices" - The Physical function PCI address
450+
9. "linkTypes" - The link type of the net device associated with the PCI device
448451

449452
If a single device matches multiple selector objects, it will only be allocated to the first one.
450453

pkg/devices/gen_pci.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,37 @@ package devices
1919

2020
import (
2121
"github.com/jaypipes/ghw"
22+
23+
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"
2224
)
2325

2426
// GenPciDevice implementation
2527
type GenPciDevice struct {
26-
pciAddr string
28+
pciAddr string
29+
acpiIndex string
2730
}
2831

2932
// NewGenPciDevice returns GenPciDevice instance
3033
func NewGenPciDevice(dev *ghw.PCIDevice) (*GenPciDevice, error) {
3134
pciAddr := dev.Address
3235

36+
acpiIndex, err := utils.GetAcpiIndex(dev.Address)
37+
if err != nil {
38+
return nil, err
39+
}
40+
3341
return &GenPciDevice{
34-
pciAddr: pciAddr,
42+
pciAddr: pciAddr,
43+
acpiIndex: acpiIndex,
3544
}, nil
3645
}
3746

3847
// GetPciAddr returns pci address of the device
3948
func (pd *GenPciDevice) GetPciAddr() string {
4049
return pd.pciAddr
4150
}
51+
52+
// GetAcpiIndex returns ACPI index of the device
53+
func (pd *GenPciDevice) GetAcpiIndex() string {
54+
return pd.acpiIndex
55+
}

pkg/factory/factory.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (rf *resourceFactory) GetSelector(attr string, values []string) (types.Devi
9393
return resources.NewRootDeviceSelector(values), nil
9494
case "linkTypes":
9595
return resources.NewLinkTypeSelector(values), nil
96+
case "acpiIndexes":
97+
return resources.NewAcpiIndexSelector(values), nil
9698
case "ddpProfiles":
9799
return resources.NewDdpSelector(values), nil
98100
case "auxTypes":
@@ -102,6 +104,15 @@ func (rf *resourceFactory) GetSelector(attr string, values []string) (types.Devi
102104
}
103105
}
104106

107+
func (rf *resourceFactory) FilterBySelector(selectorName string, values []string, devicesToFilter []types.HostDevice) []types.HostDevice {
108+
if len(values) > 0 {
109+
if selector, err := rf.GetSelector(selectorName, values); err == nil {
110+
return selector.Filter(devicesToFilter)
111+
}
112+
}
113+
return devicesToFilter
114+
}
115+
105116
// GetResourcePool returns an instance of resourcePool
106117
func (rf *resourceFactory) GetResourcePool(rc *types.ResourceConfig, filteredDevice []types.HostDevice) (types.ResourcePool, error) {
107118
devicePool := make(map[string]types.HostDevice)

pkg/netdevice/netDeviceProvider.go

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -94,64 +94,36 @@ func (np *netDeviceProvider) GetFilteredDevices(devices []types.HostDevice,
9494
}
9595

9696
rf := np.rFactory
97+
9798
// filter by vendor list
98-
if nf.Vendors != nil && len(nf.Vendors) > 0 {
99-
if selector, err := rf.GetSelector("vendors", nf.Vendors); err == nil {
100-
filteredDevice = selector.Filter(filteredDevice)
101-
}
102-
}
99+
filteredDevice = rf.FilterBySelector("vendors", nf.Vendors, filteredDevice)
103100

104101
// filter by device list
105-
if nf.Devices != nil && len(nf.Devices) > 0 {
106-
if selector, err := rf.GetSelector("devices", nf.Devices); err == nil {
107-
filteredDevice = selector.Filter(filteredDevice)
108-
}
109-
}
102+
filteredDevice = rf.FilterBySelector("devices", nf.Devices, filteredDevice)
110103

111104
// filter by driver list
112-
if nf.Drivers != nil && len(nf.Drivers) > 0 {
113-
if selector, err := rf.GetSelector("drivers", nf.Drivers); err == nil {
114-
filteredDevice = selector.Filter(filteredDevice)
115-
}
116-
}
105+
filteredDevice = rf.FilterBySelector("drivers", nf.Drivers, filteredDevice)
117106

118107
// filter by pciAddresses list
119-
if nf.PciAddresses != nil && len(nf.PciAddresses) > 0 {
120-
if selector, err := rf.GetSelector("pciAddresses", nf.PciAddresses); err == nil {
121-
filteredDevice = selector.Filter(filteredDevice)
122-
}
123-
}
108+
filteredDevice = rf.FilterBySelector("pciAddresses", nf.PciAddresses, filteredDevice)
109+
110+
// filter by acpiIndexes list
111+
filteredDevice = rf.FilterBySelector("acpiIndexes", nf.AcpiIndexes, filteredDevice)
124112

125113
// filter by PfNames list
126-
if nf.PfNames != nil && len(nf.PfNames) > 0 {
127-
if selector, err := rf.GetSelector("pfNames", nf.PfNames); err == nil {
128-
filteredDevice = selector.Filter(filteredDevice)
129-
}
130-
}
114+
filteredDevice = rf.FilterBySelector("pfNames", nf.PfNames, filteredDevice)
131115

132116
// filter by RootDevices list
133-
if nf.RootDevices != nil && len(nf.RootDevices) > 0 {
134-
if selector, err := rf.GetSelector("rootDevices", nf.RootDevices); err == nil {
135-
filteredDevice = selector.Filter(filteredDevice)
136-
}
137-
}
117+
filteredDevice = rf.FilterBySelector("rootDevices", nf.RootDevices, filteredDevice)
138118

139119
// filter by linkTypes list
140-
if nf.LinkTypes != nil && len(nf.LinkTypes) > 0 {
141-
if len(nf.LinkTypes) > 1 {
142-
glog.Warningf("Link type selector should have a single value.")
143-
}
144-
if selector, err := rf.GetSelector("linkTypes", nf.LinkTypes); err == nil {
145-
filteredDevice = selector.Filter(filteredDevice)
146-
}
120+
if len(nf.LinkTypes) > 1 {
121+
glog.Warningf("Link type selector should have a single value.")
147122
}
123+
filteredDevice = rf.FilterBySelector("linkTypes", nf.LinkTypes, filteredDevice)
148124

149125
// filter by DDP Profiles list
150-
if nf.DDPProfiles != nil && len(nf.DDPProfiles) > 0 {
151-
if selector, err := rf.GetSelector("ddpProfiles", nf.DDPProfiles); err == nil {
152-
filteredDevice = selector.Filter(filteredDevice)
153-
}
154-
}
126+
filteredDevice = rf.FilterBySelector("ddpProfiles", nf.DDPProfiles, filteredDevice)
155127

156128
// filter for rdma devices
157129
if nf.IsRdma {

pkg/resources/deviceSelectors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ func (s *pciAddressSelector) Filter(inDevices []types.HostDevice) []types.HostDe
9393
return filteredList
9494
}
9595

96+
// NewAcpiIndexSelector returns a NetDevSelector interface for netDev list
97+
func NewAcpiIndexSelector(acpiIndexes []string) types.DeviceSelector {
98+
return &acpiIndexSelector{acpiIndexes: acpiIndexes}
99+
}
100+
101+
type acpiIndexSelector struct {
102+
acpiIndexes []string
103+
}
104+
105+
func (s *acpiIndexSelector) Filter(inDevices []types.HostDevice) []types.HostDevice {
106+
filteredList := make([]types.HostDevice, 0)
107+
for _, dev := range inDevices {
108+
acpiIndex := dev.(types.PciDevice).GetAcpiIndex()
109+
if contains(s.acpiIndexes, acpiIndex) {
110+
filteredList = append(filteredList, dev)
111+
}
112+
}
113+
return filteredList
114+
}
115+
96116
// NewPfNameSelector returns a NetDevSelector interface for netDev list
97117
func NewPfNameSelector(pfNames []string) types.DeviceSelector {
98118
return &pfNameSelector{pfNames: pfNames}

pkg/resources/deviceSelectors_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,24 @@ var _ = Describe("DeviceSelectors", func() {
297297
})
298298
})
299299
})
300+
301+
Describe("acpiIndex selector", func() {
302+
Context("filtering", func() {
303+
It("should return devices matching the correct acpi index", func() {
304+
acpiIndexes := []string{"101"}
305+
sel := resources.NewAcpiIndexSelector(acpiIndexes)
306+
307+
dev0 := mocks.PciNetDevice{}
308+
dev0.On("GetAcpiIndex").Return("101")
309+
dev1 := mocks.PciNetDevice{}
310+
dev1.On("GetAcpiIndex").Return("102")
311+
312+
in := []types.HostDevice{&dev0, &dev1}
313+
filtered := sel.Filter(in)
314+
315+
Expect(filtered).To(ContainElement(&dev0))
316+
Expect(filtered).NotTo(ContainElement(&dev1))
317+
})
318+
})
319+
})
300320
})

pkg/types/mocks/AccelDevice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/types/mocks/AuxNetDevice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/types/mocks/HostDevice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/types/mocks/NetDevice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)