-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for acquiring DDP profile using devlink
- Loading branch information
Showing
4 changed files
with
336 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
const ( | ||
// fwAppNameKey is a key to extract DDP name | ||
fwAppNameKey = "fw.app.name" | ||
pciBus = "pci" | ||
) | ||
|
||
// IsDevlinkDDPSupportedByPCIDevice checks if DDP of PCI device can be acquired with devlink info command | ||
func IsDevlinkDDPSupportedByPCIDevice(device string) bool { | ||
return IsDevlinkDDPSupportedByDevice(pciBus, device) | ||
} | ||
|
||
// IsDevlinkDDPSupportedByDevice checks if DDP of device can be acquired with devlink info command | ||
func IsDevlinkDDPSupportedByDevice(bus, device string) bool { | ||
if _, err := DevlinkGetDeviceInfoByNameAndKey(bus, device, fwAppNameKey); err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
// DevlinkGetDDPProfiles returns DDP for selected device | ||
func DevlinkGetDDPProfiles(device string) (string, error) { | ||
return DevlinkGetDeviceInfoByNameAndKey(pciBus, device, fwAppNameKey) | ||
} | ||
|
||
// DevlinkGetDeviceInfoByNameAndKeys returns values for selected keys in device info | ||
func DevlinkGetDeviceInfoByNameAndKeys(bus, device string, | ||
keys []string) (map[string]string, error) { | ||
data, err := netlink.DevlinkGetDeviceInfoByNameAsMap(bus, device) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
info := make(map[string]string) | ||
|
||
for _, key := range keys { | ||
if value, exists := data[key]; exists { | ||
info[key] = value | ||
} else { | ||
return nil, KeyNotFoundError("DevlinkGetDeviceInfoByNameAndKeys", key) | ||
} | ||
} | ||
|
||
return info, nil | ||
} | ||
|
||
// DevlinkGetDeviceInfoByNameAndKey returns values for selected key in device info | ||
func DevlinkGetDeviceInfoByNameAndKey(bus, device, key string) (string, error) { | ||
keys := []string{key} | ||
info, err := DevlinkGetDeviceInfoByNameAndKeys(bus, device, keys) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if value, exists := info[key]; exists { | ||
return value, nil | ||
} | ||
|
||
return "", KeyNotFoundError("DevlinkGetDeviceInfoByNameAndKey", key) | ||
} | ||
|
||
// ErrKeyNotFound error when key is not found in the parsed response | ||
var ErrKeyNotFound = errors.New("key could not be found") | ||
|
||
// ErrMessageTooShort error when netlink message is too short | ||
var ErrMessageTooShort = errors.New("message too short") | ||
|
||
// ErrReadAttributes error when netlink message attributes cannot be read | ||
var ErrReadAttributes = errors.New("could not read attributes") | ||
|
||
// ErrDDPNotSupported error when device does not support DDP | ||
var ErrDDPNotSupported = errors.New("this device seems not to support DDP") | ||
|
||
// KeyNotFoundError returns ErrKeyNotFound | ||
func KeyNotFoundError(function, key string) error { | ||
return fmt.Errorf("%s - %w: %s", function, ErrKeyNotFound, key) | ||
} | ||
|
||
// ReadAttributesError returns ErrReadAttributes | ||
func ReadAttributesError(function, message string) error { | ||
return fmt.Errorf("%s - %w: %s", function, ErrReadAttributes, message) | ||
} | ||
|
||
// DDPNotSupportedError returns ErrDDPNotSupported | ||
func DDPNotSupportedError(device string) error { | ||
return fmt.Errorf("%w: %s", ErrDDPNotSupported, device) | ||
} |
Oops, something went wrong.