How nextmn/srv6 works
Project Type & Purpose
• Type: A single Go‐based network agent/CLI daemon that implements SRv6 MUP endpoint behaviors.
• Purpose: Load user configuration, register SRv6 “tasks” (behaviors), expose an optional HTTP control API, and perform data‐plane packet handling by invoking Linux networking primitives (via iproute2/iptables).
File Structure
Top‐level:
– main.go: entrypoint.
– config/: static YAML example.
– internal/app: application bootstrap, API registry.
– internal/config: all config‐loading and feature flags.
– internal/ctrl: control‐plane HTTP server and endpoint registry.
– internal/database: persistence layer (embedded SQL).
– internal/iana: protocol constants.
– internal/iproute2: wrapper over Linux iproute2/tun interfaces.
– internal/netfunc: SRv6 behavior implementations (“net functions”).
– internal/tasks: task registry and implementations (glue code between controller, netfunc, iproute2).
– etc/, bash‐completion/: shipping example configs and shell completions.
CI/CD: .github/workflows; linting: .pre‐commit‐config.yaml; Makefile targets.
README Insights
– Incoming‐packet‐flow SVG shows separation of control vs. data paths.
– Runtime deps: iproute2 & iptables (interact with Linux kernel).
– Optional Docker deployment requiring NET_ADMIN capabilities.
– Roadmap tables list which SRv6 behaviors are implemented.
Main Components & Their Relationships
a. Core Components
• CLI/Daemon (main.go + internal/app): reads CLI flags, loads config, starts subsystems.
• Configuration Loader (internal/config): parses YAML and environment overrides.
• Control Plane (internal/ctrl + internal/app/api): optional HTTP server exposing REST endpoints to dynamically add/remove policies.
• Persistence Layer (internal/database): initializes embedded SQL store (database.sql), CRUD APIs for uplink/downlink sessions.
• Task Registry (internal/tasks): “Command” pattern where each SRv6 behavior is registered, configured, and executed.
• SRv6 Functions (internal/netfunc): actual packet‐processing logic implementing each endpoint behavior.
• IPRoute2 Adapter (internal/iproute2): interfaces for creating TUN devices, adding ip rules, SRv6 policy.
• Operating System Kernel (Linux Networking Stack): performs packet encapsulation, forwarding, and actual SRv6 data path.
• External Tools: iptables (for filtering), iproute2 CLI under the hood.
b. Data & Control Flows
– Control Flow: User CLI → config loader → app bootstrap → register netfuncs/tasks → start HTTP server → HTTP API calls → ctrl layer → tasks → iproute2 adapter → kernel.
– Data Flow: Inbound packet arrives at kernel → SRv6 routing (seg6) hands to TUN or hook → NextMN‐SRv6 netfunc handler processes packet → potentially modifies headers or forwards → kernel.
Architectural Patterns & Design Principles
• Layered modularization: config → app → controller → tasks → netfunc → platform adapter.
• Registry/Plugin pattern: all behaviors (“tasks” and “netfuncs”) self‐register via init/registry.go.
• Separation of Concerns: distinct packages for control plane vs. data plane vs. persistence vs. system adapters.
• Dependency injection via configuration (internal/config + registry.go).
Detail
– Capture all internal packages and their high‐level responsibility.
– Reflect the plugin registry mechanism.
– Explicitly show how configuration and database tie into the controller.
– Don’t over‐aggregate: represent at least one example netfunc and one example task to illustrate extensibility.