You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
377 lines
11 KiB
Go
377 lines
11 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
const (
|
|
libvirtStreamWouldBlock = -2
|
|
libvirtEventLoopHeartbeatMS = 100
|
|
libvirtEventLoopErrorBackoff = 100 * time.Millisecond
|
|
storageRollbackTimeout = 30 * time.Second
|
|
)
|
|
|
|
var (
|
|
libvirtEventLoopOnce sync.Once
|
|
libvirtEventLoopMu sync.RWMutex
|
|
libvirtEventLoopErr error
|
|
storageStreamEvents = newStorageStreamEventBroadcaster()
|
|
)
|
|
|
|
// startLibvirtEventLoop must run before the first libvirt connection is
|
|
// opened. The heartbeat is registered before EventRunDefaultImpl starts,
|
|
// because libvirt documents that an empty default loop may block forever and
|
|
// fail to notice handles registered later. It also lets a new stream wake a
|
|
// loop that was between registrations within a bounded interval.
|
|
func startLibvirtEventLoop() error {
|
|
libvirtEventLoopOnce.Do(func() {
|
|
if err := libvirt.EventRegisterDefaultImpl(); err != nil {
|
|
setLibvirtEventLoopError(err)
|
|
return
|
|
}
|
|
if _, err := libvirt.EventAddTimeout(libvirtEventLoopHeartbeatMS, libvirtEventLoopHeartbeat); err != nil {
|
|
setLibvirtEventLoopError(err)
|
|
return
|
|
}
|
|
go runLibvirtEventLoop()
|
|
})
|
|
return currentLibvirtEventLoopError()
|
|
}
|
|
|
|
func libvirtEventLoopHeartbeat(_ int) {}
|
|
|
|
func runLibvirtEventLoop() {
|
|
for {
|
|
if err := libvirt.EventRunDefaultImpl(); err != nil {
|
|
setLibvirtEventLoopError(err)
|
|
storageStreamEvents.notify()
|
|
time.Sleep(libvirtEventLoopErrorBackoff)
|
|
continue
|
|
}
|
|
setLibvirtEventLoopError(nil)
|
|
}
|
|
}
|
|
|
|
func setLibvirtEventLoopError(err error) {
|
|
libvirtEventLoopMu.Lock()
|
|
libvirtEventLoopErr = err
|
|
libvirtEventLoopMu.Unlock()
|
|
}
|
|
|
|
func currentLibvirtEventLoopError() error {
|
|
libvirtEventLoopMu.RLock()
|
|
defer libvirtEventLoopMu.RUnlock()
|
|
return libvirtEventLoopErr
|
|
}
|
|
|
|
// libvirtEventBroadcaster is a coalescing generation signal shared by stream
|
|
// readiness and domain job completion. Events carry no durable evidence; they
|
|
// only tell waiters to re-read the corresponding libvirt state.
|
|
type libvirtEventBroadcaster struct {
|
|
mu sync.Mutex
|
|
generation uint64
|
|
wake chan struct{}
|
|
}
|
|
|
|
func newLibvirtEventBroadcaster() *libvirtEventBroadcaster {
|
|
return &libvirtEventBroadcaster{wake: make(chan struct{})}
|
|
}
|
|
|
|
func (b *libvirtEventBroadcaster) snapshot() (uint64, <-chan struct{}) {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
return b.generation, b.wake
|
|
}
|
|
|
|
func (b *libvirtEventBroadcaster) notify() {
|
|
b.mu.Lock()
|
|
close(b.wake)
|
|
b.generation++
|
|
b.wake = make(chan struct{})
|
|
b.mu.Unlock()
|
|
}
|
|
|
|
func (b *libvirtEventBroadcaster) wait(ctx context.Context, generation uint64, wake <-chan struct{}) error {
|
|
b.mu.Lock()
|
|
changed := b.generation != generation
|
|
b.mu.Unlock()
|
|
if changed {
|
|
return nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-wake:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
type storageStreamEventBroadcaster struct{ *libvirtEventBroadcaster }
|
|
|
|
func newStorageStreamEventBroadcaster() *storageStreamEventBroadcaster {
|
|
return &storageStreamEventBroadcaster{libvirtEventBroadcaster: newLibvirtEventBroadcaster()}
|
|
}
|
|
|
|
func (b *storageStreamEventBroadcaster) wait(ctx context.Context, generation uint64, wake <-chan struct{}) error {
|
|
if err := b.libvirtEventBroadcaster.wait(ctx, generation, wake); err != nil {
|
|
return err
|
|
}
|
|
return currentStreamEventLoopError()
|
|
}
|
|
|
|
func currentStreamEventLoopError() error {
|
|
if err := currentLibvirtEventLoopError(); err != nil {
|
|
return fmt.Errorf("libvirt event loop stopped: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type streamEventUpdater interface {
|
|
EventUpdateCallback(libvirt.StreamEventType) error
|
|
}
|
|
|
|
func storageStreamEventReady(stream streamEventUpdater) {
|
|
// Stream readiness callbacks are level-triggered. Disarm before waking
|
|
// waiters so a writable stream cannot make the event loop spin while its
|
|
// producer is waiting for the next HTTP/source chunk.
|
|
if stream != nil {
|
|
_ = stream.EventUpdateCallback(0)
|
|
}
|
|
storageStreamEvents.notify()
|
|
}
|
|
|
|
// libvirtStorageStreamEventCallback intentionally has no closure state. In
|
|
// libvirt-go v1.12007.0, EventAddCallback allocates a Go callback ID but the C
|
|
// helper supplies no free callback, so EventRemoveCallback cannot release that
|
|
// ID. Registering per-transfer closures would retain their contexts, buffers,
|
|
// and stream state forever. The package-level callback limits each leaked map
|
|
// entry to the same stateless function value; the generation broadcast wakes
|
|
// every waiter, which safely retries its own non-blocking stream operation.
|
|
func libvirtStorageStreamEventCallback(stream *libvirt.Stream, _ libvirt.StreamEventType) {
|
|
storageStreamEventReady(stream)
|
|
}
|
|
|
|
type storageStream interface {
|
|
Send([]byte) (int, error)
|
|
Recv([]byte) (int, error)
|
|
EventAddCallback(libvirt.StreamEventType, libvirt.StreamEventCallback) error
|
|
EventUpdateCallback(libvirt.StreamEventType) error
|
|
EventRemoveCallback() error
|
|
Finish() error
|
|
Abort() error
|
|
Free() error
|
|
}
|
|
|
|
type storageStreamTransfer struct {
|
|
stream storageStream
|
|
callbackRegistered bool
|
|
closed bool
|
|
}
|
|
|
|
func (t *storageStreamTransfer) addCallback(events libvirt.StreamEventType) error {
|
|
events |= libvirt.STREAM_EVENT_ERROR | libvirt.STREAM_EVENT_HANGUP
|
|
if t.callbackRegistered {
|
|
return t.stream.EventUpdateCallback(events)
|
|
}
|
|
if err := t.stream.EventAddCallback(events, libvirtStorageStreamEventCallback); err != nil {
|
|
return err
|
|
}
|
|
t.callbackRegistered = true
|
|
return nil
|
|
}
|
|
|
|
func (t *storageStreamTransfer) removeCallback() error {
|
|
if !t.callbackRegistered {
|
|
return nil
|
|
}
|
|
if err := t.stream.EventRemoveCallback(); err != nil {
|
|
return err
|
|
}
|
|
t.callbackRegistered = false
|
|
return nil
|
|
}
|
|
|
|
func (t *storageStreamTransfer) send(ctx context.Context, contents []byte) (int, error) {
|
|
total := 0
|
|
for len(contents) != 0 {
|
|
if err := ctx.Err(); err != nil {
|
|
return total, err
|
|
}
|
|
generation, wake := storageStreamEvents.snapshot()
|
|
count, err := t.stream.Send(contents)
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
if count == libvirtStreamWouldBlock {
|
|
alreadyRegistered := t.callbackRegistered
|
|
if err := t.addCallback(libvirt.STREAM_EVENT_WRITABLE); err != nil {
|
|
return total, err
|
|
}
|
|
if !alreadyRegistered {
|
|
// Retry once after installing the level-triggered callback so a
|
|
// readiness transition immediately before registration cannot
|
|
// leave this transfer asleep without a notification.
|
|
continue
|
|
}
|
|
if err := storageStreamEvents.wait(ctx, generation, wake); err != nil {
|
|
return total, err
|
|
}
|
|
continue
|
|
}
|
|
if count <= 0 || count > len(contents) {
|
|
return total, io.ErrNoProgress
|
|
}
|
|
total += count
|
|
contents = contents[count:]
|
|
}
|
|
return total, nil
|
|
}
|
|
|
|
func (t *storageStreamTransfer) recv(ctx context.Context, buffer []byte) (int, error) {
|
|
for {
|
|
if err := ctx.Err(); err != nil {
|
|
return 0, err
|
|
}
|
|
generation, wake := storageStreamEvents.snapshot()
|
|
count, err := t.stream.Recv(buffer)
|
|
if err != nil {
|
|
return count, err
|
|
}
|
|
if count == libvirtStreamWouldBlock {
|
|
alreadyRegistered := t.callbackRegistered
|
|
if err := t.addCallback(libvirt.STREAM_EVENT_READABLE); err != nil {
|
|
return 0, err
|
|
}
|
|
if !alreadyRegistered {
|
|
continue
|
|
}
|
|
if err := storageStreamEvents.wait(ctx, generation, wake); err != nil {
|
|
return 0, err
|
|
}
|
|
continue
|
|
}
|
|
if count < 0 || count > len(buffer) {
|
|
return 0, io.ErrNoProgress
|
|
}
|
|
return count, nil
|
|
}
|
|
}
|
|
|
|
func (t *storageStreamTransfer) finish(ctx context.Context) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return t.abort(err)
|
|
}
|
|
if err := t.removeCallback(); err != nil {
|
|
return t.abort(fmt.Errorf("remove storage stream callback: %w", err))
|
|
}
|
|
if err := t.stream.Finish(); err != nil {
|
|
return t.abort(err)
|
|
}
|
|
t.closed = true
|
|
return t.stream.Free()
|
|
}
|
|
|
|
func (t *storageStreamTransfer) abort(cause error) error {
|
|
if t.closed {
|
|
return cause
|
|
}
|
|
removeErr := t.removeCallback()
|
|
if removeErr != nil {
|
|
// Libvirt requires an event callback to be removed before Abort or
|
|
// Free. Retry once, but if removal still fails retain the underlying
|
|
// stream: freeing it could let the registered callback run against a
|
|
// released virStreamPtr. This rare leak is safer than use-after-free
|
|
// and is surfaced to operation reconciliation as a cleanup failure.
|
|
removeRetryErr := t.removeCallback()
|
|
if removeRetryErr != nil {
|
|
t.closed = true
|
|
return operation.MarkUncertain(errors.Join(cause, wrapStreamCleanupError("remove callback", removeErr), wrapStreamCleanupError("remove callback retry", removeRetryErr)))
|
|
}
|
|
}
|
|
abortErr := t.stream.Abort()
|
|
if abortErr != nil {
|
|
// A failed Abort does not prove that the transfer stopped. Do not
|
|
// release the final stream reference while libvirt may still own an
|
|
// active transfer; retain it and require reconciliation instead.
|
|
t.closed = true
|
|
return operation.MarkUncertain(errors.Join(cause, wrapStreamCleanupError("abort", abortErr)))
|
|
}
|
|
freeErr := t.stream.Free()
|
|
t.closed = true
|
|
return errors.Join(cause, wrapStreamCleanupError("free", freeErr))
|
|
}
|
|
|
|
func wrapStreamCleanupError(action string, err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s storage stream: %w", action, err)
|
|
}
|
|
|
|
func storageRollbackContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
|
return context.WithTimeout(context.WithoutCancel(ctx), storageRollbackTimeout)
|
|
}
|
|
|
|
type storageStreamWriter struct {
|
|
ctx context.Context
|
|
transfer *storageStreamTransfer
|
|
}
|
|
|
|
func (w *storageStreamWriter) Write(contents []byte) (int, error) {
|
|
return w.transfer.send(w.ctx, contents)
|
|
}
|
|
|
|
func (l *Libvirt) downloadStorageVolume(ctx context.Context, volume *libvirt.StorageVol, offset, length uint64) ([]byte, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if length == 0 || length > uint64(^uint(0)>>1) {
|
|
return nil, fmt.Errorf("invalid storage download length: %d", length)
|
|
}
|
|
stream, err := l.conn.NewStream(libvirt.STREAM_NONBLOCK)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
transfer := &storageStreamTransfer{stream: stream}
|
|
if err := volume.Download(stream, offset, length, 0); err != nil {
|
|
return nil, transfer.abort(err)
|
|
}
|
|
contents := make([]byte, 0, int(length))
|
|
buffer := make([]byte, 64<<10)
|
|
for {
|
|
count, recvErr := transfer.recv(ctx, buffer)
|
|
if count > 0 {
|
|
if uint64(len(contents)+count) > length {
|
|
return nil, transfer.abort(fmt.Errorf("storage download exceeded expected length"))
|
|
}
|
|
contents = append(contents, buffer[:count]...)
|
|
}
|
|
if errors.Is(recvErr, io.EOF) {
|
|
break
|
|
}
|
|
if recvErr != nil {
|
|
return nil, transfer.abort(recvErr)
|
|
}
|
|
if count == 0 {
|
|
return nil, transfer.abort(io.ErrNoProgress)
|
|
}
|
|
}
|
|
if uint64(len(contents)) != length {
|
|
return nil, transfer.abort(io.ErrUnexpectedEOF)
|
|
}
|
|
if err := transfer.finish(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return contents, nil
|
|
}
|