-
Notifications
You must be signed in to change notification settings - Fork 199
Add custom filters for processing accelerators #395
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,12 @@ | |
package accelerator | ||
|
||
import ( | ||
"github.com/samber/lo" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/jaypipes/ghw/pkg/context" | ||
"github.com/jaypipes/ghw/pkg/pci" | ||
"github.com/samber/lo" | ||
) | ||
|
||
// PCI IDs list available at https://admin.pci-ids.ucw.cz/read/PD | ||
|
@@ -60,13 +62,69 @@ func (i *Info) load() error { | |
if !isAccelerator(device) { | ||
continue | ||
} | ||
accelDev := &AcceleratorDevice{ | ||
Address: device.Address, | ||
PCIDevice: device, | ||
if len(i.DiscoveryFilters) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the way I envisioned is that the user should be able to feed more filters in addition to the unavoidable buliltin probably using a different interface to maximize the safety: user-provided filters should never be allowed to modify the |
||
for _, filter := range i.DiscoveryFilters { | ||
if validate(filter, device) { | ||
accelDev := &AcceleratorDevice{ | ||
Address: device.Address, | ||
PCIDevice: device, | ||
} | ||
accelDevices = append(accelDevices, accelDev) | ||
break | ||
} | ||
} | ||
} else { | ||
accelDev := &AcceleratorDevice{ | ||
Address: device.Address, | ||
PCIDevice: device, | ||
} | ||
accelDevices = append(accelDevices, accelDev) | ||
} | ||
accelDevices = append(accelDevices, accelDev) | ||
} | ||
|
||
i.Devices = accelDevices | ||
return nil | ||
} | ||
|
||
// validate checks if a given PCI device matches the provided filter string. | ||
// | ||
// The filter string is expected to be in the format "VendorID:ProductID:Class+Subclass". | ||
// Each part of the filter (VendorID, ProductID, Class+Subclass) is optional and can be | ||
// left empty, in which case the corresponding attribute is ignored during validation. | ||
// | ||
// Parameters: | ||
// - filter: A string in the form "VendorID:ProductID:Class+Subclass", where | ||
// any part of the string may be empty to represent a wildcard match. | ||
// - device: A pointer to a `pci.Device` structure. | ||
// | ||
// Returns: | ||
// - true: If the device matches the filter criteria (wildcards are supported). | ||
// - false: If the device does not match the filter criteria. | ||
// | ||
// Matching criteria: | ||
// - VendorID must match `device.Vendor.ID` if provided. | ||
// - ProductID must match `device.Product.ID` if provided. | ||
// - Class and Subclass must match the concatenated result of `device.Class.ID` and `device.Subclass.ID` if provided. | ||
// | ||
// Example: | ||
// | ||
// filter := "8086:1234:1200" | ||
// device := pci.Device{Vendor: Vendor{ID: "8086"}, Product: Product{ID: "1234"}, Class: Class{ID: "12"}, Subclass: Subclass{ID: "00"}} | ||
// isValid := validate(filter, &device) // returns true | ||
// | ||
// filter := "8086::1200" // Wildcard for ProductID | ||
// isValid := validate(filter, &device) // returns true | ||
// | ||
// filter := "::1200" // Wildcard for ProductID and VendorID | ||
// isValid := validate(filter, &device) // returns true | ||
func validate(filter string, device *pci.Device) bool { | ||
ids := strings.Split(filter, ":") | ||
|
||
if (ids[0] == "" || ids[0] == device.Vendor.ID) && | ||
(len(ids) < 2 || (ids[1] == "" || ids[1] == device.Product.ID)) && | ||
(len(ids) < 3 || (ids[2] == "" || ids[2] == fmt.Sprintf("%s%s", device.Class.ID, device.Subclass.ID))) { | ||
return true | ||
} | ||
|
||
return false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the filtering functionality is something we want, no questions about it.
I imagined it a bit higher level though. Like, pass a single function which takes a
pci.Device
object and return(bool, string)
to convey if it should be accepted, and if not why. Or perhaps we can just (ab)use aerror
as return value: ifnil
, accept, if noterr.Error()
can tell us why.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something like