Add GoVPP integration and GetVPPInfo gRPC call

VPP client (internal/vpp/)
- New package managing connections to both VPP API and stats sockets,
  treated as a unit: if either drops, both are torn down and
  re-established together.
- Run() loop: connect, fetch version via vpe.ShowVersion, read
  /sys/boottime from the stats segment, log vpp-connect, then monitor
  with control_ping every 10s. On failure, disconnect both, retry
  after 5s.
- Registers as client name "vpp-maglev" (visible in VPP's
  "show api clients").
- Flags: --vpp-api-addr (default /run/vpp/api.sock) and
  --vpp-stats-addr (default /run/vpp/stats.sock). Empty api addr
  disables VPP integration entirely.

gRPC / proto
- Add GetVPPInfo RPC returning VPPInfo: version, build_date,
  build_directory, pid, boottime_ns, connecttime_ns. Both times are
  unix timestamps in nanoseconds — the client computes durations
  locally for display.
- Returns codes.Unavailable if VPP is disabled or not connected.

maglevc
- Add 'show vpp info' command displaying version, build-date,
  build-dir, vpp-pid, vpp-boottime (with duration), and connected
  time (with duration).
This commit is contained in:
2026-04-11 22:03:22 +02:00
parent 74448cf6d0
commit 3227263d68
13 changed files with 690 additions and 120 deletions

View File

@@ -71,11 +71,20 @@ func buildTree() *Node {
Children: []*Node{showHealthCheckName},
}
// show vpp info
showVPPInfo := &Node{Word: "info", Help: "Show VPP version, uptime, and connection status", Run: runShowVPPInfo}
showVPP := &Node{
Word: "vpp",
Help: "VPP dataplane information",
Children: []*Node{showVPPInfo},
}
show.Children = []*Node{
showVersion,
showFrontends,
showBackends,
showHealthChecks,
showVPP,
}
// set backend <name> pause|resume|disabled|enabled
@@ -201,6 +210,31 @@ func dynNone(_ context.Context, _ grpcapi.MaglevClient) []string { return nil }
// ---- run functions ---------------------------------------------------------
func runShowVPPInfo(ctx context.Context, client grpcapi.MaglevClient, _ []string) error {
ctx, cancel := context.WithTimeout(ctx, callTimeout)
defer cancel()
info, err := client.GetVPPInfo(ctx, &grpcapi.GetVPPInfoRequest{})
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintf(w, "%s\t%s\n", label("version"), info.Version)
fmt.Fprintf(w, "%s\t%s\n", label("build-date"), info.BuildDate)
fmt.Fprintf(w, "%s\t%s\n", label("build-dir"), info.BuildDirectory)
fmt.Fprintf(w, "%s\t%d\n", label("vpp-pid"), info.Pid)
if info.BoottimeNs > 0 {
bootTime := time.Unix(0, info.BoottimeNs)
fmt.Fprintf(w, "%s\t%s (%s)\n", label("vpp-boottime"),
bootTime.Format("2006-01-02 15:04:05"),
formatDuration(time.Since(bootTime)))
}
connTime := time.Unix(0, info.ConnecttimeNs)
fmt.Fprintf(w, "%s\t%s (%s)\n", label("connected"),
connTime.Format("2006-01-02 15:04:05"),
formatDuration(time.Since(connTime)))
return w.Flush()
}
func runShowVersion(_ context.Context, _ grpcapi.MaglevClient, _ []string) error {
fmt.Printf("maglevc %s (commit %s, built %s)\n",
buildinfo.Version(), buildinfo.Commit(), buildinfo.Date())

View File

@@ -28,6 +28,7 @@ func TestExpandPathsRoot(t *testing.T) {
"watch events",
"watch events <opt>",
"config check",
"show vpp info",
"config reload",
"quit",
"exit",
@@ -59,9 +60,9 @@ func TestExpandPathsShow(t *testing.T) {
}
}
// version, frontends, frontends <name>, backends, backends <name>,
// healthchecks, healthchecks <name> = 7 lines
if len(lines) != 7 {
t.Errorf("expected exactly 7 show subcommands, got %d", len(lines))
// healthchecks, healthchecks <name>, vpp info = 8 lines
if len(lines) != 8 {
t.Errorf("expected exactly 8 show subcommands, got %d", len(lines))
}
}

View File

@@ -23,6 +23,7 @@ import (
"git.ipng.ch/ipng/vpp-maglev/internal/config"
"git.ipng.ch/ipng/vpp-maglev/internal/grpcapi"
"git.ipng.ch/ipng/vpp-maglev/internal/metrics"
"git.ipng.ch/ipng/vpp-maglev/internal/vpp"
)
func main() {
@@ -40,6 +41,8 @@ func run() error {
configPath := stringFlag("config", "/etc/vpp-maglev/maglev.yaml", "MAGLEV_CONFIG", "path to maglev.yaml")
grpcAddr := stringFlag("grpc-addr", ":9090", "MAGLEV_GRPC_ADDR", "gRPC listen address")
metricsAddr := stringFlag("metrics-addr", ":9091", "MAGLEV_METRICS_ADDR", "Prometheus /metrics listen address (empty to disable)")
vppAPIAddr := stringFlag("vpp-api-addr", "/run/vpp/api.sock", "MAGLEV_VPP_API_ADDR", "VPP binary API socket path (empty to disable)")
vppStatsAddr := stringFlag("vpp-stats-addr", "/run/vpp/stats.sock", "MAGLEV_VPP_STATS_ADDR", "VPP stats socket path")
logLevel := stringFlag("log-level", "info", "MAGLEV_LOG_LEVEL", "log level (debug|info|warn|error)")
flag.Parse()
@@ -92,13 +95,20 @@ func run() error {
}
}()
// ---- VPP connection -----------------------------------------------------
var vppClient *vpp.Client
if *vppAPIAddr != "" {
vppClient = vpp.New(*vppAPIAddr, *vppStatsAddr)
go vppClient.Run(ctx)
}
// ---- gRPC server --------------------------------------------------------
lis, err := net.Listen("tcp", *grpcAddr)
if err != nil {
return fmt.Errorf("listen %s: %w", *grpcAddr, err)
}
srv := grpc.NewServer()
maglevServer := grpcapi.NewServer(ctx, chkr, logBroadcaster, *configPath)
maglevServer := grpcapi.NewServer(ctx, chkr, logBroadcaster, *configPath, vppClient)
grpcapi.RegisterMaglevServer(srv, maglevServer)
if *enableReflection {
reflection.Register(srv)