Documentation
¶
Index ¶
- Constants
- Variables
- func ConfigureVGPU(ctx context.Context, vfAddress, profileType string) error
- func DestroyMdev(ctx context.Context, mdevUUID string) error
- func DestroyVGPU(ctx context.Context, assignment VGPUAssignment) error
- func DiscoverVGPU() (VGPUFramework, []VirtualFunction, error)
- func FormatVFAssignedAt(claimedAt time.Time) string
- func GetDeviceSysfsPath(pciAddress string) string
- func GetIOMMUGroupDevices(iommuGroup int) ([]string, error)
- func InitVFHealth(path string, threshold int) error
- func IsMdevInUse(mdevUUID string) bool
- func ListVendorVFIOProfileTypes(vfs []VirtualFunction) (map[string][]VGPUProfileType, error)
- func NVMLFallbacks() map[string]int64
- func QuarantinedVFAddresses() (map[string]struct{}, error)
- func ReconcileMdevs(ctx context.Context, instanceInfos []MdevReconcileInfo) error
- func ReconcileVGPUs(ctx context.Context, protectedDevicePaths map[string]struct{}) error
- func RefreshVGPUCapacity()
- func RepairVFHealthStore() error
- func SetGPUProfileCacheTTL(ttl string)
- func TotalQuarantinedVFs() int
- func VFHealthStoreUnavailable() bool
- func ValidateDeviceName(name string) bool
- func ValidatePCIAddress(addr string) bool
- type AvailableDevice
- type CreateDeviceRequest
- type Device
- type DeviceType
- type GPUMode
- type GPUProfile
- type HostCapabilities
- type InstanceLivenessChecker
- type Manager
- type MdevDevice
- type MdevReconcileInfo
- type PassthroughDevice
- type VFIOBinder
- func (v *VFIOBinder) BindToVFIO(pciAddress string) error
- func (v *VFIOBinder) CheckIOMMUGroupSafe(pciAddress string, allowedDevices []string) error
- func (v *VFIOBinder) GetVFIOGroupPath(pciAddress string) (string, error)
- func (v *VFIOBinder) IsDeviceBoundToVFIO(pciAddress string) bool
- func (v *VFIOBinder) IsVFIOAvailable() bool
- func (v *VFIOBinder) UnbindFromVFIO(pciAddress string) error
- type VFInitFailureReport
- type VFInitSuccessReport
- type VFReportOutcome
- type VFReportResult
- type VFSuccessResult
- type VGPUAssignment
- type VGPUAvailability
- type VGPUDevice
- type VGPUFramework
- type VGPUProfileType
- type VirtualFunction
Constants ¶
const ( VGPUFrameworkNone VGPUFramework = "" VGPUFrameworkMdev VGPUFramework = "mdev" VGPUFrameworkVendorVFIO VGPUFramework = "vendor-vfio" // GPUModePassthrough indicates whole GPU VFIO passthrough GPUModePassthrough GPUMode = "passthrough" // GPUModeVGPU indicates vGPU mode GPUModeVGPU GPUMode = "vgpu" // GPUModeNone indicates no GPU available GPUModeNone GPUMode = "none" )
Variables ¶
var ( // ErrNotFound is returned when a device is not found ErrNotFound = errors.New("device not found") // ErrInUse is returned when a device is currently attached to an instance ErrInUse = errors.New("device is in use") // ErrNotBound is returned when a VFIO operation requires the device to be bound ErrNotBound = errors.New("device is not bound to VFIO") // ErrAlreadyBound is returned when trying to bind a device that's already bound to VFIO ErrAlreadyBound = errors.New("device is already bound to VFIO") // ErrAlreadyExists is returned when trying to register a device that already exists ErrAlreadyExists = errors.New("device already exists") // ErrInvalidName is returned when the device name doesn't match the required pattern ErrInvalidName = errors.New("device name must match pattern ^[a-zA-Z0-9][a-zA-Z0-9_.-]+$") // ErrNameExists is returned when a device with the same name already exists ErrNameExists = errors.New("device name already exists") // ErrInvalidPCIAddress is returned when the PCI address format is invalid ErrInvalidPCIAddress = errors.New("invalid PCI address format") // ErrDeviceNotFound is returned when the PCI device doesn't exist on the host ErrDeviceNotFound = errors.New("PCI device not found on host") // ErrVFIONotAvailable is returned when VFIO modules are not loaded ErrVFIONotAvailable = errors.New("VFIO is not available (modules not loaded)") // ErrIOMMUGroupConflict is returned when not all devices in IOMMU group can be passed through ErrIOMMUGroupConflict = errors.New("IOMMU group contains other devices that must also be passed through") // ErrVGPUNotSupportedOnMacOS is returned for vGPU operations on macOS ErrVGPUNotSupportedOnMacOS = errors.New("vGPU (mdev) is not supported on macOS") )
var DeviceNamePattern = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`)
DeviceNamePattern is the regex pattern for valid device names Must start with alphanumeric, followed by alphanumeric, underscore, dot, or dash
Functions ¶
func ConfigureVGPU ¶ added in v0.4.0
ConfigureVGPU idempotently configures a claimed vendor VFIO VF. A failed configure may still have written the type, so the cached placements are refreshed either way.
func DestroyMdev ¶ added in v0.0.5
DestroyMdev removes an mdev device.
func DestroyVGPU ¶ added in v0.4.0
func DestroyVGPU(ctx context.Context, assignment VGPUAssignment) error
func DiscoverVGPU ¶ added in v0.4.0
func DiscoverVGPU() (VGPUFramework, []VirtualFunction, error)
DiscoverVGPU returns the host's active vGPU framework and virtual functions.
func FormatVFAssignedAt ¶ added in v0.4.0
FormatVFAssignedAt renders a claim time as the AssignedAt key used in VF health reports.
func GetDeviceSysfsPath ¶
GetDeviceSysfsPath returns the sysfs path for a PCI device (used by cloud-hypervisor)
func GetIOMMUGroupDevices ¶
GetIOMMUGroupDevices returns all PCI devices in the same IOMMU group
func InitVFHealth ¶ added in v0.4.0
InitVFHealth loads persisted VF health state from path and evaluates the loaded tallies against threshold, the number of failed assignments that quarantine a VF. A lowered threshold therefore applies to failures persisted before the change. An error leaves the store unavailable, which fails vGPU placement closed until a later load or write succeeds. An empty path keeps state in memory only.
func IsMdevInUse ¶ added in v0.0.5
IsMdevInUse checks if an mdev device is currently bound to a driver (in use by a VM). An mdev with a driver symlink is actively attached to a hypervisor/VFIO.
func ListVendorVFIOProfileTypes ¶ added in v0.4.0
func ListVendorVFIOProfileTypes(vfs []VirtualFunction) (map[string][]VGPUProfileType, error)
ListVendorVFIOProfileTypes returns the profiles advertised by each VF.
func NVMLFallbacks ¶ added in v0.4.0
NVMLFallbacks returns how many creatable-placement queries have fallen back to the sysfs lists for at least one GPU since startup, by reason: NVML failed (error), every GPU it failed for is in equal-size mode (equal_size), it timed out (timeout), or an earlier timed-out query had not returned yet (stuck).
func QuarantinedVFAddresses ¶ added in v0.4.0
QuarantinedVFAddresses returns the PCI addresses of quarantined VFs. It fails while the health store is unavailable so placement fails closed.
func ReconcileMdevs ¶ added in v0.0.5
func ReconcileMdevs(ctx context.Context, instanceInfos []MdevReconcileInfo) error
ReconcileMdevs destroys orphaned mdevs on managed VFs.
Policy:
- Consider only mdevs whose parent VF is currently managed by hypeman (discoverable via /sys/class/mdev_bus)
- Keep mdevs claimed by live instance metadata
- Keep mdevs whose VFIO group has an open file handle (/dev/vfio/<group>)
- Keep mdevs younger than a short grace period to avoid racing very recent state transitions
- Delete all remaining mdevs
func ReconcileVGPUs ¶ added in v0.4.0
ReconcileVGPUs releases orphaned vGPU assignments.
func RefreshVGPUCapacity ¶ added in v0.4.0
func RefreshVGPUCapacity()
RefreshVGPUCapacity reloads the vendor VFIO placements /resources counts from. It does nothing on hosts without vendor VFIO vGPUs.
func RepairVFHealthStore ¶ added in v0.4.0
func RepairVFHealthStore() error
RepairVFHealthStore retries a failed load or persist so the store recovers without waiting for a placement or /resources read to do it. It takes vendorVFIOMu like the report paths, so a reload that changes the quarantine set cannot interleave with a vendor-VFIO configure. The same retry also runs from checkedAddresses under only s.mu; there, configure's own re-check under vendorVFIOMu covers the selection-to-configure window instead. It returns without taking either lock while the store is healthy.
func SetGPUProfileCacheTTL ¶ added in v0.0.6
func SetGPUProfileCacheTTL(ttl string)
SetGPUProfileCacheTTL sets the TTL for GPU profile metadata cache. Should be called during application startup with the config value.
func TotalQuarantinedVFs ¶ added in v0.4.0
func TotalQuarantinedVFs() int
TotalQuarantinedVFs returns the number of quarantined VFs in persisted state.
func VFHealthStoreUnavailable ¶ added in v0.4.0
func VFHealthStoreUnavailable() bool
VFHealthStoreUnavailable reports whether persisted state failed to load or the last write failed.
func ValidateDeviceName ¶
ValidateDeviceName validates that a device name matches the required pattern
func ValidatePCIAddress ¶
ValidatePCIAddress validates that a string is a valid PCI address format
Types ¶
type AvailableDevice ¶
type AvailableDevice struct {
PCIAddress string `json:"pci_address"`
VendorID string `json:"vendor_id"`
DeviceID string `json:"device_id"`
VendorName string `json:"vendor_name"`
DeviceName string `json:"device_name"`
IOMMUGroup int `json:"iommu_group"`
CurrentDriver *string `json:"current_driver"` // nil if no driver bound
}
AvailableDevice represents a PCI device discovered on the host
func DiscoverAvailableDevices ¶
func DiscoverAvailableDevices() ([]AvailableDevice, error)
DiscoverAvailableDevices scans sysfs for PCI devices that can be used for passthrough It filters for devices that are likely candidates (GPUs, network cards, etc.)
func GetDeviceInfo ¶
func GetDeviceInfo(pciAddress string) (*AvailableDevice, error)
GetDeviceInfo reads information about a specific PCI device
type CreateDeviceRequest ¶
type CreateDeviceRequest struct {
Name string `json:"name,omitempty"` // optional: globally unique name (auto-generated if not provided)
PCIAddress string `json:"pci_address"` // required: PCI address (e.g., "0000:a2:00.0")
Tags tags.Tags `json:"tags,omitempty"`
}
CreateDeviceRequest is the request to register a new device
type Device ¶
type Device struct {
Id string `json:"id"` // cuid2 identifier
Name string `json:"name"` // user-provided globally unique name
Type DeviceType `json:"type"` // gpu or pci
Tags tags.Tags `json:"tags,omitempty"` // user-defined key-value tags
PCIAddress string `json:"pci_address"` // e.g., "0000:a2:00.0"
VendorID string `json:"vendor_id"` // e.g., "10de"
DeviceID string `json:"device_id"` // e.g., "27b8"
IOMMUGroup int `json:"iommu_group"` // IOMMU group number
BoundToVFIO bool `json:"bound_to_vfio"` // whether device is bound to vfio-pci
AttachedTo *string `json:"attached_to"` // instance ID if attached, nil otherwise
CreatedAt time.Time `json:"created_at"`
}
Device represents a registered PCI device for passthrough
type DeviceType ¶
type DeviceType string
DeviceType represents the type of PCI device
const ( DeviceTypeGPU DeviceType = "gpu" DeviceTypeGeneric DeviceType = "pci" )
func DetermineDeviceType ¶
func DetermineDeviceType(device *AvailableDevice) DeviceType
DetermineDeviceType determines the DeviceType based on device properties
type GPUMode ¶ added in v0.0.5
type GPUMode string
GPUMode represents the host's GPU configuration mode
type GPUProfile ¶ added in v0.0.5
type GPUProfile struct {
Name string `json:"name"` // user-facing name, e.g., "L40S-1Q"
FramebufferMB int `json:"framebuffer_mb"` // frame buffer size in MB
Available int `json:"available"` // more instances that fit if only this profile is added
}
GPUProfile describes an available vGPU profile type
func ListGPUProfiles ¶ added in v0.0.5
func ListGPUProfiles() ([]GPUProfile, error)
ListGPUProfiles returns available vGPU profiles with availability counts.
func ListGPUProfilesWithVFs ¶ added in v0.0.5
func ListGPUProfilesWithVFs(framework VGPUFramework, vfs []VirtualFunction, quarantined map[string]struct{}) ([]GPUProfile, error)
ListGPUProfilesWithVFs returns available profiles for discovered VFs. Quarantined VFs are excluded from vendor VFIO counts, which come from the cached placements rather than a driver query.
type HostCapabilities ¶ added in v0.4.0
type HostCapabilities struct {
// SupportsVGPU indicates whether the platform supports mediated vGPU operations.
SupportsVGPU bool
}
HostCapabilities describes optional device features supported by the host platform.
func Capabilities ¶ added in v0.4.0
func Capabilities() HostCapabilities
Capabilities returns the device features supported on Linux.
type InstanceLivenessChecker ¶
type InstanceLivenessChecker interface {
// IsInstanceRunning returns true if the instance exists and is in a running state
// (i.e., has an active VMM process). Returns false if the instance doesn't exist
// or is stopped/standby/unknown.
IsInstanceRunning(ctx context.Context, instanceID string) bool
// GetInstanceDevices returns the list of device IDs attached to an instance.
// Returns nil if the instance doesn't exist.
GetInstanceDevices(ctx context.Context, instanceID string) []string
// ListAllInstanceDevices returns a map of instanceID -> []deviceIDs for all instances.
ListAllInstanceDevices(ctx context.Context) map[string][]string
// DetectSuspiciousVMMProcesses finds cloud-hypervisor processes that don't match
// known instances and logs warnings. Returns the count of suspicious processes found.
DetectSuspiciousVMMProcesses(ctx context.Context) int
}
InstanceLivenessChecker provides a way to check if an instance is running. This interface allows devices to query instance state without a circular dependency.
type Manager ¶
type Manager interface {
// ListDevices returns all registered devices
ListDevices(ctx context.Context) ([]Device, error)
// ListAvailableDevices discovers passthrough-capable devices on the host
ListAvailableDevices(ctx context.Context) ([]AvailableDevice, error)
// CreateDevice registers a new device for passthrough
CreateDevice(ctx context.Context, req CreateDeviceRequest) (*Device, error)
// GetDevice returns a device by ID or name
GetDevice(ctx context.Context, idOrName string) (*Device, error)
// DeleteDevice unregisters a device
DeleteDevice(ctx context.Context, id string) error
// BindToVFIO binds a device to vfio-pci driver
BindToVFIO(ctx context.Context, id string) error
// UnbindFromVFIO unbinds a device from vfio-pci driver
UnbindFromVFIO(ctx context.Context, id string) error
// MarkAttached marks a device as attached to an instance
MarkAttached(ctx context.Context, deviceID, instanceID string) error
// MarkDetached marks a device as detached from an instance
MarkDetached(ctx context.Context, deviceID string) error
// ReconcileDevices cleans up stale device state on startup.
// It detects devices with AttachedTo referencing non-existent instances
// and clears the orphaned attachment state.
ReconcileDevices(ctx context.Context) error
// SetLivenessChecker sets the instance liveness checker after construction.
// This allows breaking the circular dependency between device and instance managers.
SetLivenessChecker(checker InstanceLivenessChecker)
}
Manager provides device management operations
func NewManager ¶
NewManager creates a new device manager. Use SetLivenessChecker after construction to enable accurate orphan detection.
type MdevDevice ¶ added in v0.0.5
type MdevDevice struct {
UUID string `json:"uuid"` // e.g., "aa618089-8b16-4d01-a136-25a0f3c73123"
VFAddress string `json:"vf_address"` // VF this mdev resides on
ProfileType string `json:"profile_type"` // internal type name, e.g., "nvidia-556"
ProfileName string `json:"profile_name"` // user-facing name, e.g., "L40S-1Q"
SysfsPath string `json:"sysfs_path"` // path for VMM device attachment
InstanceID string `json:"instance_id"` // instance this mdev is attached to
}
MdevDevice represents an active mediated device (vGPU instance)
func CreateMdev ¶ added in v0.0.5
func CreateMdev(ctx context.Context, profileName, instanceID string) (*MdevDevice, error)
CreateMdev creates an mdev device for the given profile and instance. It finds an available VF and creates the mdev, returning the device info. This function is thread-safe and uses a mutex to prevent race conditions when multiple instances request vGPUs concurrently.
func ListMdevDevices ¶ added in v0.0.5
func ListMdevDevices() ([]MdevDevice, error)
ListMdevDevices returns all active mdev devices on the host. Scans sysfs directly for fast, consistent results without external process overhead.
type MdevReconcileInfo ¶ added in v0.0.5
type MdevReconcileInfo struct {
InstanceID string
MdevUUID string
IsRunning bool // true if instance's VMM is running or state is unknown
}
MdevReconcileInfo contains information needed to reconcile mdevs for an instance
type PassthroughDevice ¶ added in v0.0.5
type PassthroughDevice struct {
Name string `json:"name"` // GPU name, e.g., "NVIDIA L40S"
Available bool `json:"available"` // true if not attached to an instance
}
PassthroughDevice describes a physical GPU available for passthrough
type VFIOBinder ¶
type VFIOBinder struct{}
VFIOBinder handles binding and unbinding devices to/from VFIO
func (*VFIOBinder) BindToVFIO ¶
func (v *VFIOBinder) BindToVFIO(pciAddress string) error
BindToVFIO binds a PCI device to the vfio-pci driver This requires: 1. Stopping any processes using the device (e.g., nvidia-persistenced for NVIDIA GPUs) 2. Unbinding the device from its current driver (if any) 3. Binding it to vfio-pci
func (*VFIOBinder) CheckIOMMUGroupSafe ¶
func (v *VFIOBinder) CheckIOMMUGroupSafe(pciAddress string, allowedDevices []string) error
CheckIOMMUGroupSafe checks if all devices in the IOMMU group are safe to pass through Returns an error if there are other devices in the group that aren't being passed through
func (*VFIOBinder) GetVFIOGroupPath ¶
func (v *VFIOBinder) GetVFIOGroupPath(pciAddress string) (string, error)
GetVFIOGroupPath returns the path to the VFIO group device for a PCI device
func (*VFIOBinder) IsDeviceBoundToVFIO ¶
func (v *VFIOBinder) IsDeviceBoundToVFIO(pciAddress string) bool
IsDeviceBoundToVFIO checks if a device is currently bound to vfio-pci
func (*VFIOBinder) IsVFIOAvailable ¶
func (v *VFIOBinder) IsVFIOAvailable() bool
IsVFIOAvailable checks if VFIO is available on the system
func (*VFIOBinder) UnbindFromVFIO ¶
func (v *VFIOBinder) UnbindFromVFIO(pciAddress string) error
UnbindFromVFIO unbinds a device from vfio-pci and restores the original driver
type VFInitFailureReport ¶ added in v0.4.0
VFInitFailureReport describes one guest-reported driver init failure.
InstanceID and AssignedAt together identify the assignment. AssignedAt is the instance's stored GPUClaimedAt rendered with FormatVFAssignedAt; a success report only clears a failure whose AssignedAt string matches exactly, so every reporter must use that formatting.
type VFInitSuccessReport ¶ added in v0.4.0
VFInitSuccessReport identifies the assignment that successfully initialized. AssignedAt follows the same format as VFInitFailureReport.AssignedAt.
A success clears the matched failure and every older tally. It rescinds a quarantine only when the matched failure is the most recent one recorded: the report that crossed the threshold, or the newest tally when a lowered threshold quarantined the VF at load.
type VFReportOutcome ¶ added in v0.4.0
type VFReportOutcome int
VFReportOutcome describes how a failure report changed a VF's health state.
const ( // VFReportUnchanged means the VF was already quarantined or this // assignment was already recorded. VFReportUnchanged VFReportOutcome = iota // VFReportRecorded means the failure was tallied below the quarantine threshold. VFReportRecorded // VFReportQuarantined means this report crossed the threshold and quarantined the VF. VFReportQuarantined )
type VFReportResult ¶ added in v0.4.0
type VFReportResult struct {
Outcome VFReportOutcome
Failures int
Threshold int
}
VFReportResult is the outcome of recording a driver init failure.
func ReportVFInitFailure ¶ added in v0.4.0
func ReportVFInitFailure(report VFInitFailureReport) (VFReportResult, error)
ReportVFInitFailure records a guest-reported driver init failure and quarantines the VF once failures from enough distinct assignments accumulate.
type VFSuccessResult ¶ added in v0.4.0
VFSuccessResult describes how a successful init changed a VF's health state.
func ReportVFInitSuccess ¶ added in v0.4.0
func ReportVFInitSuccess(report VFInitSuccessReport) (VFSuccessResult, error)
ReportVFInitSuccess clears failures through an exactly matched successful assignment. A quarantine is rescinded only when that assignment triggered it.
type VGPUAssignment ¶ added in v0.4.0
type VGPUAssignment struct {
Framework VGPUFramework
DevicePath string
MdevUUID string
InstanceID string
}
VGPUAssignment identifies an existing vGPU assignment to release.
type VGPUAvailability ¶ added in v0.4.0
type VGPUAvailability struct {
AllocatableSlots int // free VFs eligible for placement
QuarantinedSlots int
Quarantined map[string]struct{} // PCI addresses of quarantined VFs
}
VGPUAvailability is one VF health snapshot applied to discovered VFs. Pass Quarantined to ListGPUProfilesWithVFs so profile availability is computed from the same snapshot without reading the store again.
func GetVGPUAvailability ¶ added in v0.4.0
func GetVGPUAvailability(framework VGPUFramework, vfs []VirtualFunction) (VGPUAvailability, error)
GetVGPUAvailability counts free allocatable and quarantined VFs. It fails while the health store is unavailable so callers fail closed.
type VGPUDevice ¶ added in v0.4.0
type VGPUDevice struct {
Framework VGPUFramework
VFAddress string
ProfileType string
ProfileName string
SysfsPath string
MdevUUID string
}
func CreateVGPU ¶ added in v0.4.0
func CreateVGPU(ctx context.Context, profileName, instanceID string) (*VGPUDevice, error)
type VGPUFramework ¶ added in v0.4.0
type VGPUFramework string
type VGPUProfileType ¶ added in v0.4.0
VGPUProfileType describes a driver profile that can be configured on a VF.
type VirtualFunction ¶ added in v0.0.5
type VirtualFunction struct {
PCIAddress string `json:"pci_address"` // e.g., "0000:82:00.4"
ParentGPU string `json:"parent_gpu"` // e.g., "0000:82:00.0"
Allocated bool `json:"allocated"` // true if a vGPU is assigned to this VF
ProfileType string `json:"profile_type,omitempty"`
}
VirtualFunction represents an SR-IOV Virtual Function for vGPU