Files
vpp-maglev/internal/vpp/reconciler.go
Pim van Pelt fb62532fd5 VPP LB counters, src-ip-sticky, and frontend state aggregation
New feature: per-VIP / per-backend runtime counters
  * New GetVPPLBCounters RPC serving an in-process snapshot refreshed
    by a 5s scrape loop (internal/vpp/lbstats.go). Each cycle pulls
    the LB plugin's four SimpleCounters (next, first, untracked,
    no-server) plus the FIB /net/route/to CombinedCounter for every
    VIP and every backend host prefix via a single DumpStats call.
  * FIB stats-index discovery via ip_route_lookup (internal/vpp/
    fibstats.go); per-worker reduction happens in the collector.
  * Prometheus collector exports vip_packets_total (kind label),
    vip_route_{packets,bytes}_total, and backend_route_{packets,
    bytes}_total. Metrics source interface extended with VIPStats /
    BackendRouteStats; vpp.Client publishes snapshots via
    atomic.Pointer and clears them on disconnect.
  * New 'show vpp lb counters' CLI command. The 'show vpp lbstate'
    and 'sync vpp lbstate' commands are restructured under 'show
    vpp lb {state,counters}' / 'sync vpp lb state' to make room
    for the new verb.

New feature: src-ip-sticky frontends
  * New frontend YAML key 'src-ip-sticky' (bool). Plumbed through
    config.Frontend, desiredVIP, and the lb_add_del_vip_v2 call.
  * Reflected in gRPC FrontendInfo.src_ip_sticky and VPPLBVIP.
    src_ip_sticky, and shown in 'show vpp lb state' output.
  * Scraped back from VPP by parsing 'show lb vips verbose' through
    cli_inband — lb_vip_details does not expose the flag. The same
    scrape also recovers the LB pool index for each VIP, which the
    stats-segment counters are keyed on. This is a documented
    temporary workaround until VPP ships an lb_vip_v2_dump.
  * src_ip_sticky cannot be mutated on a live VIP, so a flipped flag
    triggers a tear-down-and-recreate in reconcileVIP (ASes deleted
    with flush, VIP deleted, then re-added). Flip is logged.

New feature: frontend state aggregation and events
  * New health.FrontendState (unknown/up/down) and FrontendTransition
    types. A frontend is 'up' iff at least one backend has a nonzero
    effective weight, 'unknown' iff no backend has real state yet,
    and 'down' otherwise.
  * Checker tracks per-frontend aggregate state, recomputing after
    each backend transition and emitting a frontend-transition Event
    on change. Reload drops entries for removed frontends.
  * checker.Event gains an optional FrontendTransition pointer;
    backend- vs. frontend-transition events are demultiplexed on
    that field.
  * WatchEvents now sends an initial snapshot of frontend state on
    connect (mirroring the existing backend snapshot), subscribes
    once to the checker stream, and fans out to backend/frontend
    handlers based on the client's filter flags. The proto
    FrontendEvent message grows name + transition fields.
  * New Checker.FrontendState accessor.

Refactor: pure health helpers
  * Moved the priority-failover selector and the (pool idx, active
    pool, state, cfg weight) → (vpp weight, flush) mapping out of
    internal/vpp/lbsync.go into a new internal/health/weights.go so
    the checker can reuse them for frontend-state computation
    without importing internal/vpp.
  * New functions: health.ActivePoolIndex, BackendEffectiveWeight,
    EffectiveWeights, ComputeFrontendState. lbsync.go now calls
    these directly; vpp.EffectiveWeights is a thin wrapper over
    health.EffectiveWeights retained for the gRPC observability
    path. Fully unit-tested in internal/health/weights_test.go.

maglevc polish
  * --color default is now mode-aware: on in the interactive shell,
    off in one-shot mode so piped output is script-safe. Explicit
    --color=true/false still overrides.
  * New stripHostMask helper drops /32 and /128 from VIP display;
    non-host prefixes pass through unchanged.
  * Counter table column order fixed (first before next) and
    packets/bytes columns renamed to fib-packets/fib-bytes to
    clarify they come from the FIB, not the LB plugin.

Docs
  * config-guide: document src-ip-sticky, including the VIP
    recreate-on-change caveat.
  * user-guide, maglevc.1, maglevd.8: updated command tree, new
    counters command, color defaults, and the src-ip-sticky field.
2026-04-12 16:07:39 +02:00

104 lines
3.2 KiB
Go

// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
package vpp
import (
"context"
"errors"
"log/slog"
"git.ipng.ch/ipng/vpp-maglev/internal/checker"
)
// EventSource is the subset of checker.Checker that Reconciler needs.
// Decoupling via an interface keeps the dependency direction
// vpp → checker (checker never imports vpp).
type EventSource interface {
Subscribe() (<-chan checker.Event, func())
}
// Reconciler bridges checker state transitions to VPP dataplane changes.
// It subscribes to the checker's event channel and, for every transition,
// runs SyncLBStateVIP for the frontend the backend belongs to. This is
// the ONLY place in the codebase where backend state transitions cause
// VPP calls — every LB change flows through Client.SyncLBStateVIP.
//
// The reconciler carries no state of its own. Idempotency is guaranteed
// by SyncLBStateVIP itself (diff-based, driven by the pure asFromBackend
// mapping in lbsync.go).
type Reconciler struct {
client *Client
events EventSource
stateSrc StateSource
}
// NewReconciler creates a Reconciler. client is the VPP client, events is
// the checker (or anything that implements Subscribe), and stateSrc provides
// the live config for SyncLBStateVIP calls. All three are normally the
// checker/vpp client pair constructed at daemon startup.
func NewReconciler(client *Client, events EventSource, stateSrc StateSource) *Reconciler {
return &Reconciler{client: client, events: events, stateSrc: stateSrc}
}
// Run subscribes to the checker and loops until ctx is cancelled. Each
// received event fires a single-VIP sync for the frontend the transitioned
// backend belongs to.
func (r *Reconciler) Run(ctx context.Context) {
ch, unsub := r.events.Subscribe()
defer unsub()
slog.Info("vpp-reconciler-start")
defer slog.Info("vpp-reconciler-stop")
for {
select {
case <-ctx.Done():
return
case ev, ok := <-ch:
if !ok {
return
}
r.handle(ev)
}
}
}
// handle reconciles one event. Operates only on backend-transition events
// that carry a frontend name (the checker emits one event per frontend that
// references the backend, so a backend shared across multiple frontends
// produces multiple events and all relevant VIPs are reconciled).
// Frontend-transition events are observational only — the dataplane work
// they would imply has already been done by the backend-transition event
// that triggered them.
func (r *Reconciler) handle(ev checker.Event) {
if ev.FrontendTransition != nil {
return // frontend-only event; no dataplane work
}
if ev.FrontendName == "" {
return
}
cfg := r.stateSrc.Config()
if cfg == nil {
return
}
slog.Debug("vpp-reconciler-event",
"frontend", ev.FrontendName,
"backend", ev.BackendName,
"from", ev.Transition.From.String(),
"to", ev.Transition.To.String())
if err := r.client.SyncLBStateVIP(cfg, ev.FrontendName); err != nil {
if errors.Is(err, ErrFrontendNotFound) {
// Frontend was removed between the event being emitted and
// us handling it; a periodic SyncLBStateAll will clean it up.
return
}
slog.Warn("vpp-reconciler-error",
"frontend", ev.FrontendName,
"backend", ev.BackendName,
"from", ev.Transition.From.String(),
"to", ev.Transition.To.String(),
"err", err)
}
}