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.
virt/agent/cmd/virt-agent/network_observation.go

193 lines
6.2 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"net/netip"
"sync"
"git.luoli.live/root/virt/agent/hypervisor"
"git.luoli.live/root/virt/agent/networkobserve"
)
type networkObservationInventoryBackend interface {
ListVMs(context.Context) ([]hypervisor.VMMetadata, error)
GetMetrics(context.Context, string) (hypervisor.Metrics, error)
}
// hypervisorNetworkObservationInventory builds attribution only from the one
// canonical public-network identity frozen in managed libvirt metadata. VPC
// addresses and interface discovery heuristics never enter this inventory.
type hypervisorNetworkObservationInventory struct {
backend networkObservationInventoryBackend
}
func (inventory hypervisorNetworkObservationInventory) NetworkObservationInventory(ctx context.Context) ([]networkobserve.VMTarget, error) {
if inventory.backend == nil {
return nil, errors.New("network observation hypervisor is unavailable")
}
vms, err := inventory.backend.ListVMs(ctx)
if err != nil {
return nil, fmt.Errorf("list managed VMs for network observation: %w", err)
}
targets := make([]networkobserve.VMTarget, 0, len(vms))
for _, vm := range vms {
if err := ctx.Err(); err != nil {
return nil, err
}
public, err := vm.PublicNetwork.Canonical()
if err != nil {
// An invalid frozen identity makes host-wide address attribution
// ambiguous. Fail this observation cycle instead of guessing from a
// bridge, MAC, domain name, or private/VPC address.
return nil, fmt.Errorf("VM %s has invalid public network identity: %w", vm.UUID, err)
}
ipv4, err := publicAddress(public.IPv4.AddressCIDR, true)
if err != nil {
return nil, fmt.Errorf("VM %s IPv4 identity: %w", vm.UUID, err)
}
ipv6, err := publicAddress(public.IPv6.AddressCIDR, false)
if err != nil {
return nil, fmt.Errorf("VM %s IPv6 identity: %w", vm.UUID, err)
}
target := networkobserve.VMTarget{UUID: vm.UUID, IPv4: ipv4, IPv6: ipv6}
metrics, metricsErr := inventory.backend.GetMetrics(ctx, vm.UUID)
if metricsErr == nil && metrics.UUID == vm.UUID {
target.Interface = interfaceCounters(metrics)
}
// A stopped domain or one transiently unavailable metrics call does not
// remove its exact public address from attribution. Nil counters make the
// window explicitly incomplete while the other VMs remain observable.
targets = append(targets, target)
}
return targets, nil
}
func publicAddress(cidr string, ipv4 bool) (netip.Addr, error) {
prefix, err := netip.ParsePrefix(cidr)
if err != nil || prefix.Addr().Is4() != ipv4 || prefix.Addr().Is4In6() {
return netip.Addr{}, errors.New("address is not a canonical public prefix")
}
return prefix.Addr(), nil
}
func interfaceCounters(metrics hypervisor.Metrics) networkobserve.InterfaceCounters {
rxBytes, txBytes := metrics.RXBytes, metrics.TXBytes
return networkobserve.InterfaceCounters{
RXBytes: &rxBytes, TXBytes: &txBytes,
RXPackets: cloneUint64(metrics.RXPackets), TXPackets: cloneUint64(metrics.TXPackets),
RXErrors: cloneUint64(metrics.RXErrors), TXErrors: cloneUint64(metrics.TXErrors),
RXDrops: cloneUint64(metrics.RXDrops), TXDrops: cloneUint64(metrics.TXDrops),
}
}
func cloneUint64(value *uint64) *uint64 {
if value == nil {
return nil
}
copy := *value
return &copy
}
type unavailableConntrackSource struct{ err error }
func (source unavailableConntrackSource) Probe(context.Context) (networkobserve.ProbeEvidence, error) {
return networkobserve.ProbeEvidence{}, source.err
}
func (source unavailableConntrackSource) Snapshot(context.Context) (networkobserve.KernelSnapshot, error) {
return networkobserve.KernelSnapshot{}, source.err
}
func (unavailableConntrackSource) Close() error { return nil }
type networkObservationRuntime struct {
collector *networkobserve.Collector
mu sync.Mutex
cancel context.CancelFunc
done chan error
started bool
}
func newNetworkObservationRuntime(hv networkObservationInventoryBackend) (*networkObservationRuntime, error) {
generation, err := networkobserve.NewGeneration()
if err != nil {
return nil, fmt.Errorf("create network observation generation: %w", err)
}
source, err := networkobserve.OpenNetlinkSource()
if err != nil {
return newNetworkObservationRuntimeWithSource(hv, unavailableConntrackSource{err: err}, generation)
}
return newNetworkObservationRuntimeWithSource(hv, source, generation)
}
func newNetworkObservationRuntimeWithSource(hv networkObservationInventoryBackend, source networkobserve.ConntrackSource,
generation string) (*networkObservationRuntime, error) {
if source == nil {
return nil, errors.New("network observation source is required")
}
collector, err := networkobserve.NewCollector(
hypervisorNetworkObservationInventory{backend: hv}, source, generation,
networkobserve.DefaultCollectorConfig(),
)
if err != nil {
_ = source.Close()
return nil, fmt.Errorf("initialize network observation collector: %w", err)
}
return &networkObservationRuntime{collector: collector}, nil
}
func (runtime *networkObservationRuntime) Start(parent context.Context) error {
if runtime == nil || runtime.collector == nil {
return errors.New("network observation runtime is unavailable")
}
runtime.mu.Lock()
defer runtime.mu.Unlock()
if runtime.started {
return errors.New("network observation runtime is already started")
}
ctx, cancel := context.WithCancel(parent)
runtime.cancel = cancel
runtime.done = make(chan error, 1)
runtime.started = true
go func() {
runtime.done <- runtime.collector.Run(ctx)
close(runtime.done)
}()
return nil
}
func (runtime *networkObservationRuntime) Shutdown(ctx context.Context) error {
if runtime == nil || runtime.collector == nil {
return nil
}
runtime.mu.Lock()
started, cancel, done := runtime.started, runtime.cancel, runtime.done
runtime.mu.Unlock()
if cancel != nil {
cancel()
}
closeResult := make(chan error, 1)
go func() { closeResult <- runtime.collector.Close() }()
var closeErr error
select {
case closeErr = <-closeResult:
case <-ctx.Done():
return ctx.Err()
}
if !started {
return closeErr
}
select {
case runErr := <-done:
if runErr != nil && !errors.Is(runErr, context.Canceled) {
return errors.Join(closeErr, runErr)
}
return closeErr
case <-ctx.Done():
return errors.Join(closeErr, ctx.Err())
}
}