Placement knew how to pick the node with the GPU. It did not know which GPUs were already taken, in two separate ways. Device.AllocatedTo was read in three places and written in none, so every discovery reported every card as free. Two clusters provisioned in sequence would both be assigned the same GPU; the second VM fails to start, hours after the portal promised the hardware. Ownership now comes from the hypervisor, which also accounts for guests created outside hpcod. Two details that are easy to get wrong and are covered by tests: a stopped guest still owns its card, because the assignment lives in the config and not in the running process; and ownership is per slot, not per function, so a guest holding 01:00.0 has the whole card. Measured against the live site. A stopped VM was given the Quadro K620: discovery went from alloc='' to alloc='vm 9099 (hpcod-devtest)', the card dropped out of the schedulable set, and a GPU request went from being rejected for want of cores to "gpu: none present (need 1)". Destroying the VM put it back. The ConnectX-4 Lx on the same host is classified roce, not infiniband, and stays unschedulable for want of agent confirmation - which is the correct answer for that card. The second way is the window the hypervisor cannot see. A cluster takes minutes to build, and until its VMs exist the cards it was promised still look free. So a reservation is written when the cluster is allocated, in the same transaction as its address, and the planner reads both. The partial unique index on live devices enforces one holder per card exactly as the cluster_id index does for addresses. Allocation now takes the whole placement rather than a site name, because storing an address without a record of where the cluster runs creates a cluster nothing will ever release. A device conflict therefore rolls the address back too - tested by checking that the next allocation gets the number the failed one would have consumed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
209 lines
5.6 KiB
Go
209 lines
5.6 KiB
Go
package proxmox
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// Device ownership.
|
|
//
|
|
// Discovery used to classify every PCI device on a node and offer all of
|
|
// them, because nothing ever set Device.AllocatedTo. Placement therefore
|
|
// believed a GPU already passed through to somebody else was still free, and
|
|
// would hand the same card to the next tenant. The second VM does not start;
|
|
// the error surfaces at boot, long after the portal has promised the
|
|
// hardware.
|
|
//
|
|
// The hypervisor is the source of truth here, not our own records. Reading
|
|
// the guests' configs also accounts for VMs created outside hpcod, which a
|
|
// database of our own allocations never would.
|
|
//
|
|
// Two details that are easy to get wrong:
|
|
//
|
|
// - A stopped guest still owns its device. Unlike cores and memory, a
|
|
// passthrough assignment is held by the configuration, not by the running
|
|
// process, and starting that guest later would fail if we had given the
|
|
// card away in the meantime.
|
|
//
|
|
// - Ownership is recorded per slot, not per function. A guest handed
|
|
// 0000:01:00.0 has taken the whole card; offering 0000:01:00.1 to someone
|
|
// else would be offering half of a device that is already spoken for.
|
|
|
|
// deviceOwners maps "node/slot" to a label naming the guest that holds it.
|
|
func (p *Provider) deviceOwners(ctx context.Context) map[string]string {
|
|
guests := p.guests(ctx)
|
|
if len(guests) == 0 {
|
|
return nil
|
|
}
|
|
mappings := p.pciMappings(ctx)
|
|
|
|
owners := map[string]string{}
|
|
var mu sync.Mutex
|
|
|
|
sem := make(chan struct{}, 8) // a busy cluster has hundreds of guests
|
|
var wg sync.WaitGroup
|
|
for _, g := range guests {
|
|
wg.Add(1)
|
|
go func(g guestRef) {
|
|
defer wg.Done()
|
|
sem <- struct{}{}
|
|
defer func() { <-sem }()
|
|
|
|
var cfg map[string]any
|
|
if err := p.cli.get(ctx, fmt.Sprintf("/nodes/%s/qemu/%d/config", g.Node, g.VMID), &cfg); err != nil {
|
|
return
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for key, raw := range cfg {
|
|
if !strings.HasPrefix(key, "hostpci") {
|
|
continue
|
|
}
|
|
val, ok := raw.(string)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, slot := range hostPCISlots(val, g.Node, mappings) {
|
|
owners[g.Node+"/"+slot] = g.label()
|
|
}
|
|
}
|
|
}(g)
|
|
}
|
|
wg.Wait()
|
|
return owners
|
|
}
|
|
|
|
type guestRef struct {
|
|
VMID int
|
|
Node string
|
|
Name string
|
|
}
|
|
|
|
func (g guestRef) label() string {
|
|
if g.Name == "" {
|
|
return fmt.Sprintf("vm %d", g.VMID)
|
|
}
|
|
return fmt.Sprintf("vm %d (%s)", g.VMID, g.Name)
|
|
}
|
|
|
|
// guests lists every QEMU guest, running or not - see the note above on why
|
|
// stopped ones matter here even though they do not for cores and memory.
|
|
func (p *Provider) guests(ctx context.Context) []guestRef {
|
|
var res []struct {
|
|
Type string `json:"type"`
|
|
Node string `json:"node"`
|
|
VMID flexInt `json:"vmid"`
|
|
Name string `json:"name"`
|
|
}
|
|
if err := p.cli.get(ctx, "/cluster/resources?type=vm", &res); err != nil {
|
|
return nil
|
|
}
|
|
out := make([]guestRef, 0, len(res))
|
|
for _, r := range res {
|
|
if r.Type != "qemu" || r.Node == "" {
|
|
continue
|
|
}
|
|
out = append(out, guestRef{VMID: int(r.VMID), Node: r.Node, Name: r.Name})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// pciMappings resolves Proxmox resource mappings, which let a guest say
|
|
// "mapping=gpu" and get whichever card that name points at on the node it
|
|
// happens to run on. Without this, a mapped passthrough looks like no
|
|
// passthrough at all and the card would be offered twice.
|
|
//
|
|
// Returns mapping name -> the "node/slot" entries it covers.
|
|
func (p *Provider) pciMappings(ctx context.Context) map[string][]string {
|
|
var maps []struct {
|
|
ID string `json:"id"`
|
|
Map []string `json:"map"`
|
|
}
|
|
if err := p.cli.get(ctx, "/cluster/mapping/pci", &maps); err != nil {
|
|
return nil
|
|
}
|
|
out := map[string][]string{}
|
|
for _, m := range maps {
|
|
for _, entry := range m.Map {
|
|
var node, path string
|
|
for _, field := range strings.Split(entry, ",") {
|
|
k, v, ok := strings.Cut(strings.TrimSpace(field), "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
switch k {
|
|
case "node":
|
|
node = v
|
|
case "path":
|
|
path = v
|
|
}
|
|
}
|
|
if node == "" || path == "" {
|
|
continue
|
|
}
|
|
if slot := normalizeSlot(path); slot != "" {
|
|
out[m.ID] = append(out[m.ID], node+"/"+slot)
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// hostPCISlots reads the slots named by one hostpciN value.
|
|
//
|
|
// The value is a comma-separated option list whose first field is either a
|
|
// PCI id or "mapping=<name>", and several devices may be joined with ";".
|
|
// Proxmox accepts the id in short form (01:00.0), slot form (0000:01:00) and
|
|
// full form (0000:01:00.0), so all three have to normalise to the same thing.
|
|
func hostPCISlots(value, node string, mappings map[string][]string) []string {
|
|
var out []string
|
|
seen := map[string]bool{}
|
|
add := func(slot string) {
|
|
if slot != "" && !seen[slot] {
|
|
seen[slot] = true
|
|
out = append(out, slot)
|
|
}
|
|
}
|
|
|
|
for _, field := range strings.Split(value, ",") {
|
|
field = strings.TrimSpace(field)
|
|
if name, ok := strings.CutPrefix(field, "mapping="); ok {
|
|
for _, ns := range mappings[name] {
|
|
if n, slot, found := strings.Cut(ns, "/"); found && n == node {
|
|
add(slot)
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
if strings.Contains(field, "=") {
|
|
continue // pcie=1, x-vga=1, rombar=0, ...
|
|
}
|
|
for _, id := range strings.Split(field, ";") {
|
|
add(normalizeSlot(id))
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// normalizeSlot turns any accepted spelling of a PCI address into
|
|
// "domain:bus:device", dropping the function. Returns "" if it is not one.
|
|
func normalizeSlot(id string) string {
|
|
id = strings.TrimSpace(strings.ToLower(id))
|
|
if id == "" {
|
|
return ""
|
|
}
|
|
if i := strings.LastIndex(id, "."); i > 0 {
|
|
id = id[:i]
|
|
}
|
|
switch strings.Count(id, ":") {
|
|
case 1:
|
|
return "0000:" + id // 01:00 -> 0000:01:00
|
|
case 2:
|
|
return id
|
|
default:
|
|
return ""
|
|
}
|
|
}
|