Skip to content

Commit

Permalink
fix: check group map before enable pci device passthrough (#97)
Browse files Browse the repository at this point in the history
Signed-off-by: Webber Huang <[email protected]>
  • Loading branch information
WebberHuang1118 authored Dec 17, 2024
1 parent c77a231 commit 0cd5672
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/controller/pcideviceclaim/pcideviceclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/harvester/pcidevices/pkg/config"
"github.com/harvester/pcidevices/pkg/deviceplugins"
v1beta1gen "github.com/harvester/pcidevices/pkg/generated/controllers/devices.harvesterhci.io/v1beta1"
"github.com/harvester/pcidevices/pkg/iommu"
)

var (
Expand Down Expand Up @@ -277,6 +278,18 @@ func getOrphanedPCIDevices(
return &pdsOrphaned, nil
}

func checkGroupMap(pd *v1beta1.PCIDevice) error {
group, err := iommu.GetGroupMap(pd.Status.Address)
if err != nil {
return err
}

if group != pd.Status.IOMMUGroup {
return fmt.Errorf("group mismatch: sys %s pd %s", group, pd.Status.IOMMUGroup)
}
return nil
}

func (h *Handler) reconcilePCIDeviceClaims(_ string, pdc *v1beta1.PCIDeviceClaim) (*v1beta1.PCIDeviceClaim, error) {

if pdc == nil || pdc.DeletionTimestamp != nil || (pdc.Spec.NodeName != h.nodeName) {
Expand All @@ -290,6 +303,10 @@ func (h *Handler) reconcilePCIDeviceClaims(_ string, pdc *v1beta1.PCIDeviceClaim
return pdc, err
}

if err := checkGroupMap(pd); err != nil {
return pdc, err
}

lock.Lock()
defer lock.Unlock()

Expand Down
14 changes: 14 additions & 0 deletions pkg/iommu/iommu.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ func GroupPaths() ([]string, error) {
}
return groupPaths, nil
}

func GetGroupMap(address string) (string, error) {
iommuGroupPaths, err := GroupPaths()
if err != nil {
return "", err
}

iommuGroupMap := GroupMapForPCIDevices(iommuGroupPaths)
if group, found := iommuGroupMap[address]; found {
return strconv.Itoa(group), nil
}

return "", fmt.Errorf("missing group for address: %s", address)
}

0 comments on commit 0cd5672

Please sign in to comment.