diff --git a/pkg/host/internal/sriov/sriov.go b/pkg/host/internal/sriov/sriov.go index 64f1fa537e..a408c40e3d 100644 --- a/pkg/host/internal/sriov/sriov.go +++ b/pkg/host/internal/sriov/sriov.go @@ -986,22 +986,30 @@ func (s *sriov) createVFs(iface *sriovnetworkv1.Interface) error { return s.setEswitchModeAndNumVFs(iface.PciAddress, expectedEswitchMode, iface.NumVfs) } -func (s *sriov) setEswitchMode(pciAddr, eswitchMode string) error { - log.Log.V(2).Info("setEswitchMode(): set eswitch mode", "device", pciAddr, "mode", eswitchMode) - if err := s.unbindAllVFsOnPF(pciAddr); err != nil { - log.Log.Error(err, "setEswitchMode(): failed to unbind VFs", "device", pciAddr, "mode", eswitchMode) +func (s *sriov) setEswitchModeAndNumVFs(pciAddr string, desiredEswitchMode string, numVFs int) error { + pfDriverName, err := s.dputilsLib.GetDriverName(pciAddr) + if err != nil { return err } - if err := s.SetNicSriovMode(pciAddr, eswitchMode); err != nil { - err = fmt.Errorf("failed to switch NIC to SRIOV %s mode: %v", eswitchMode, err) - log.Log.Error(err, "setEswitchMode(): failed to set mode", "device", pciAddr, "mode", eswitchMode) - return err + + type setEswitchModeAndNumVFsFn func (string, string, int) error + + var setEswitchModeAndNumVFsByDriverName map[string]setEswitchModeAndNumVFsFn = map[string]setEswitchModeAndNumVFsFn{ + "ice": s.setEswitchModeAndNumVFsIce, + "mlx5_core": s.setEswitchModeAndNumVFsMlx, } - return nil + + fn, ok := setEswitchModeAndNumVFsByDriverName[pfDriverName] + if !ok { + // Fallback to mlx5 driver + fn = s.setEswitchModeAndNumVFsMlx + } + + return fn(pciAddr, desiredEswitchMode, numVFs) } -func (s *sriov) setEswitchModeAndNumVFs(pciAddr string, desiredEswitchMode string, numVFs int) error { - log.Log.V(2).Info("setEswitchModeAndNumVFs(): configure VFs for device", +func (s *sriov) setEswitchModeAndNumVFsMlx(pciAddr string, desiredEswitchMode string, numVFs int) error { + log.Log.V(2).Info("setEswitchModeAndNumVFsMlx(): configure VFs for device", "device", pciAddr, "count", numVFs, "mode", desiredEswitchMode) // always switch NIC to the legacy mode before creating VFs. This is required because some drivers @@ -1011,6 +1019,7 @@ func (s *sriov) setEswitchModeAndNumVFs(pciAddr string, desiredEswitchMode strin return err } } + if err := s.SetSriovNumVfs(pciAddr, numVFs); err != nil { return err } @@ -1018,6 +1027,38 @@ func (s *sriov) setEswitchModeAndNumVFs(pciAddr string, desiredEswitchMode strin if desiredEswitchMode == sriovnetworkv1.ESwithModeSwitchDev { return s.setEswitchMode(pciAddr, sriovnetworkv1.ESwithModeSwitchDev) } + + return nil +} + +func (s *sriov) setEswitchModeAndNumVFsIce(pciAddr string, desiredEswitchMode string, numVFs int) error { + log.Log.V(2).Info("setEswitchModeAndNumVFsIce(): configure VFs for device", + "device", pciAddr, "count", numVFs, "mode", desiredEswitchMode) + + if desiredEswitchMode == sriovnetworkv1.ESwithModeSwitchDev { + if err := s.setEswitchMode(pciAddr, sriovnetworkv1.ESwithModeSwitchDev); err != nil { + return err + } + } + + if err := s.SetSriovNumVfs(pciAddr, numVFs); err != nil { + return err + } + + return nil +} + +func (s *sriov) setEswitchMode(pciAddr, eswitchMode string) error { + log.Log.V(2).Info("setEswitchMode(): set eswitch mode", "device", pciAddr, "mode", eswitchMode) + if err := s.unbindAllVFsOnPF(pciAddr); err != nil { + log.Log.Error(err, "setEswitchMode(): failed to unbind VFs", "device", pciAddr, "mode", eswitchMode) + return err + } + if err := s.SetNicSriovMode(pciAddr, eswitchMode); err != nil { + err = fmt.Errorf("failed to switch NIC to SRIOV %s mode: %v", eswitchMode, err) + log.Log.Error(err, "setEswitchMode(): failed to set mode", "device", pciAddr, "mode", eswitchMode) + return err + } return nil } @@ -1035,3 +1076,5 @@ func (s *sriov) unbindAllVFsOnPF(addr string) error { } return nil } + +