policycompiler

package
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package policycompiler computes effector operations from a desired target→policy map and the daemon's current state. Order of operations is rigid (see spec §7.4) and reverse-order rollback runs on any failure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ARP

type ARP interface {
	Start(t arpengine.Target) error
	Stop(t arpengine.Target) error
	StopAll() error
}

ARP uses arpengine.Target so the implementation (arpengine.Engine) satisfies this interface by structural typing without an adapter.

type Compiler

type Compiler struct {
	// contains filtered or unexported fields
}

Compiler orchestrates the four effectors.

func New

func New(nft NFT, tc TC, pcap Pcap, arp ARP, realIface string, operatorMAC net.HardwareAddr) *Compiler

New constructs a compiler bound to the operator's real iface (used by nft and tc when installing per-target rules and filters).

operatorMAC is the hardware address of the operator's interface. Policies targeting this MAC are rejected to prevent self-DoS.

func (*Compiler) Apply

func (c *Compiler) Apply(ctx context.Context, desired map[string]Spec) error

Apply moves the system to desired.

Phase 1 (teardown) is best-effort across all targets: a failing teardown for target A does NOT stop teardown of B/C/D. This matters at daemon shutdown where comp.Apply(empty) is called — one bad cleanup mustn't abandon every other target's kernel state. The c.current entry for a target is removed only on successful teardown so retrying Apply will retry the cleanup. Errors are aggregated and returned at the end.

Phase 2 (bringup) is strict: a failing bringup short-circuits, with the partial step list reverted in reverse order. The caller can retry by resending the same desired-state map.

c.current is kept in sync with reality: an entry is only present when its kernel-side state has been successfully built up.

func (*Compiler) Snapshot

func (c *Compiler) Snapshot() map[string]Spec

Snapshot returns a copy of the current desired state. Target slice fields are deep-copied so callers can mutate the result without corrupting the compiler's internal state.

type Kind

type Kind int

Kind is the policy variant.

const (
	KindNone Kind = iota
	KindDrop
	KindThrottle
	KindPcap
)

type NFT

type NFT interface {
	AddTargetDrop(ctx context.Context, mac net.HardwareAddr) error
	AddTargetMark(ctx context.Context, mac net.HardwareAddr, mark uint32) error
	AddReturnMark(ctx context.Context, mac, gwMAC net.HardwareAddr, targetIP net.IP, mark uint32) error
	RemoveTarget(ctx context.Context, mac net.HardwareAddr) error
}

NFT and TC interfaces are deliberately scoped to what the compiler actually calls: per-target rule add/clear. Daemon-lifecycle methods (EnsureTables, EnsureIFB, EnsureRedirect, Teardown) are concrete methods on the *Manager structs in nftmgr/tcmgr and called directly from the daemon main, not via these interfaces. Keeping the interface thin makes the compiler trivially mockable and prevents the test stubs from drifting when daemon-lifecycle methods evolve.

type Pcap

type Pcap interface {
	Open(mac, ipStr, srcIface, dir string, maxBytes int64, maxAge time.Duration) error
	Close(mac string) error
}

type Spec

type Spec struct {
	Target arpengine.Target
	Kind   Kind

	// Throttle:
	RateKbit int

	// Pcap:
	PcapDir  string
	MaxBytes int64
	MaxAge   time.Duration
}

Spec is the desired policy for a target. Target is an arpengine.Target so the same value flows directly into ARP.Start without a conversion.

type TC

type TC interface {
	SetThrottle(ctx context.Context, realIface, mac, rate string, mark uint32) error
	ClearThrottle(ctx context.Context, realIface, mac string, mark uint32) error
	SetCapture(ctx context.Context, realIface, mac string, mark uint32) error
	ClearCapture(ctx context.Context, realIface string, mark uint32) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL