Skip to content

Commit

Permalink
Fix: GetDevlinkDeviceParam to handle edge-cases correctly
Browse files Browse the repository at this point in the history
On some kernels GetDevlinkDeviceParam may return empty values
for some kernel parameters. The netlink library is able to handle this, but
the code in GetDevlinkDeviceParam function may panic if unexpected value
received. Add extra checks to avoid panics
  • Loading branch information
ykulazhenkov committed Sep 23, 2024
1 parent 4bae6ce commit 254dd8f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/host/internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,12 @@ func (n *network) GetDevlinkDeviceParam(pciAddr, paramName string) (string, erro
funcLog.Error(err, "GetDevlinkDeviceParam(): fail to get devlink device param")
return "", err
}
if len(param.Values) == 0 {
err = fmt.Errorf("param %s has no value", paramName)
funcLog.Error(err, "GetDevlinkDeviceParam(): error")
return "", err
if len(param.Values) == 0 || param.Values[0].Data == nil {
funcLog.Info("GetDevlinkDeviceParam(): WARNING: can't read devlink parameter from the device, an empty value received")
return "", nil
}
var value string
var ok bool
switch param.Type {
case nl.DEVLINK_PARAM_TYPE_U8, nl.DEVLINK_PARAM_TYPE_U16, nl.DEVLINK_PARAM_TYPE_U32:
var valData uint64
Expand All @@ -281,14 +281,22 @@ func (n *network) GetDevlinkDeviceParam(pciAddr, paramName string) (string, erro
case uint32:
valData = uint64(v)
default:
return "", fmt.Errorf("unexpected uint type type")
return "", fmt.Errorf("value is not unit")
}
value = strconv.FormatUint(valData, 10)

case nl.DEVLINK_PARAM_TYPE_STRING:
value = param.Values[0].Data.(string)
value, ok = param.Values[0].Data.(string)
if !ok {
return "", fmt.Errorf("value is not a string")
}
case nl.DEVLINK_PARAM_TYPE_BOOL:
value = strconv.FormatBool(param.Values[0].Data.(bool))
var boolValue bool
boolValue, ok = param.Values[0].Data.(bool)
if !ok {
return "", fmt.Errorf("value is not a bool")
}
value = strconv.FormatBool(boolValue)
default:
return "", fmt.Errorf("unknown value type: %d", param.Type)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/host/internal/sriov/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ func (s *sriov) configureHWOptionsForSwitchdev(iface *sriovnetworkv1.Interface)
log.Log.Error(err, "configureHWOptionsForSwitchdev(): fail to read current flow steering mode for the device", "device", iface.PciAddress)
return err
}
if currentFlowSteeringMode == "" {
log.Log.V(2).Info("configureHWOptionsForSwitchdev(): can't detect current flow_steering_mode mode for the device, skip",
"device", iface.PciAddress)
return nil
}
if currentFlowSteeringMode == desiredFlowSteeringMode {
return nil
}
Expand Down

0 comments on commit 254dd8f

Please sign in to comment.