fwstatemap

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultListEntriesBatchSize is the batch size used when the caller
	// sends zero in the request.
	DefaultListEntriesBatchSize uint32 = 100

	// MaxListEntriesBatchSize caps the number of entries fetched per
	// ListEntries round-trip to prevent unbounded allocation under the
	// service mutex.
	MaxListEntriesBatchSize uint32 = 10000
)
View Source
const DefaultStaleLayerSweepInterval = 30 * time.Second

DefaultStaleLayerSweepInterval is the default period between stale-layer sweeps.

Variables

ServiceName is the fully-qualified gRPC service name for the fwstate-map management service, derived from the generated service descriptor.

Functions

func ClampBatchSize

func ClampBatchSize(n uint32) uint32

ClampBatchSize returns a batch size that is within the allowed range: zero is replaced with DefaultListEntriesBatchSize, and values above MaxListEntriesBatchSize are clamped to MaxListEntriesBatchSize.

func MapLabeler

func MapLabeler(fullMethod string, req any) metrics.Labels

MapLabeler extracts the map name from map-scoped requests for gRPC metrics labeling.

func MapStatsToProto

func MapStatsToProto(stats mapStats) *fwstatemappb.MapStats

MapStatsToProto converts bindings-level map stats into the proto form.

The values here describe the active head layer only (one layer is walked for them; only the layer count spans the chain), so the note says so — they must not be presented as map totals after a rotation. The count of layers parked awaiting a release belongs to neither, and is carried through unchanged.

func NewMetricsFactory

func NewMetricsFactory(extra ...grpcmetrics.Option) grpcmetrics.Factory

NewMetricsFactory returns a grpcmetrics.Factory scoped to the FWStateMapService.

func ResolveCreateWorkerCount

func ResolveCreateWorkerCount(
	workerCount uint32,
	dpWorkerCount func() uint32,
) (uint16, error)

ResolveCreateWorkerCount settles the per-worker sizing for CreateMap and InsertLayer, deriving a zero count from the dataplane's worker count and rejecting an explicit mismatch.

func ValidateWorkerCount

func ValidateWorkerCount(workerCount uint32) error

ValidateWorkerCount rejects zero and out-of-range worker_count values.

Types

type FWStateMap

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

FWStateMap manages a standalone named fwstate-map object in shared memory.

func (*FWStateMap) Config

func (m *FWStateMap) Config() *cfwstate.MapObjectConfig

Config returns the underlying map object config handle.

func (*FWStateMap) Name

func (m *FWStateMap) Name() string

Name returns the map name.

type FWStateMapService

type FWStateMapService struct {
	fwstatemappb.UnimplementedFWStateMapServiceServer
	// contains filtered or unexported fields
}

FWStateMapService implements the gRPC service for standalone named fwstate-map management.

func NewFWStateMapService

func NewFWStateMapService(
	agent *ffi.Agent,
	options ...MapOption,
) *FWStateMapService

NewFWStateMapService creates a new FWStateMapService.

func NewFWStateMapServiceForTest

func NewFWStateMapServiceForTest(
	barrier func(cfwstate.MapObjectConfig) error,
	options ...MapOption,
) *FWStateMapService

NewFWStateMapServiceForTest creates an FWStateMapService without a live agent, for exercising stale-layer reclamation and the in-memory map registry.

barrier may be nil when the test does not exercise stale-layer reclamation. A logger is supplied via the options pattern (WithLog); it defaults to a no-op logger when omitted.

func (*FWStateMapService) CreateMap

CreateMap creates a new named fwstate-map for one address family and publishes it to the dataplane.

func (*FWStateMapService) DeleteMap

DeleteMap removes a named fwstate-map.

Deletion is refused at the config level while any published module config — an fwstate or ACL module of any agent on this instance — declares a link to the name: with the object gone, building that module's execution context would fail and wedge every later config change. Updating or deleting the linking module unpins the map, after which deletion succeeds.

func (*FWStateMapService) GetMapStats

GetMapStats returns statistics for the single fwtable of a named fwstate-map.

func (*FWStateMapService) InsertLayer

InsertLayer inserts a new layer into the fwtable chain of a named fwstate-map.

The insert and the stale-layer reclamation are separate routines bracketed by config generation updates: the new layer becomes the active head, a first published generation that every worker advanced past waits out inserts still targeting the previous head, expired tails are unlinked, and a second generation gates the release of the unlinked layers. mu stays held across both.

func (*FWStateMapService) ListEntries

ListEntries reads one cursor batch of entries from a named fwstate-map's fwtable. The batch protocol is stateless: the request carries the full cursor, and the response's index feeds the next call until has_more is false.

func (*FWStateMapService) ListMaps

ListMaps returns the names of all registered fwstate-map objects, with each map's address family alongside its name.

func (*FWStateMapService) Metrics

func (m *FWStateMapService) Metrics(tags ...*commonpb.MetricTag) ([]*commonpb.Metric, error)

Metrics returns the map service's metrics matching tags: gauge series derived from each live map's table statistics, plus the gRPC series its interceptor records.

The gRPC series carry the map service's own service and method labels; the module's metrics RPC aggregates them with the fwstate module metrics so map RPC traffic is reachable from a single endpoint.

func (*FWStateMapService) ReclaimDeferred

func (m *FWStateMapService) ReclaimDeferred()

ReclaimDeferred retries every deferred map object, dropping the ones whose generations have drained and keeping the rest deferred.

The service runs it before every create, delete and layer insert, after a successful delete and on every stale-layer sweep; anything else may call it at any time.

func (*FWStateMapService) ReclaimStaleLayers

func (m *FWStateMapService) ReclaimStaleLayers(
	reclaimer MapLayerReclaimer,
	mapCP cfwstate.MapObjectConfig,
	now uint64,
)

ReclaimStaleLayers waits out in-flight inserts, unlinks expired layers, advances every dataplane worker past a new config generation, then frees the unlinked layers.

The first barrier is the rotation grace period: a worker that loaded the previous head before a layer insert published the new one may still be inside fwtable_insert on that layer, before its deadline is visible. Deciding expiry while such an insert is in flight can unlink the layer as an expired tail; the worker then completes into a layer no reader reaches, silently losing the synchronized state. Publishing a generation and waiting for every worker to advance proves those inserts finished, so the unlink decision sees final deadlines.

UnlinkStaleLayers then atomically moves stale layers from the active chain to the fwtable's stale chain so new walks skip them, but a worker already mid-chain-walk can still be reading a just-unlinked layer. A second barrier proves no in-flight walk can touch the unlinked memory; only then does FreeStaleLayers release it.

If either barrier fails the parked layers stay allocated and are freed by a later round after a successful barrier — a rare leak, never a use-after-free. The caller must hold mu because unlink mutates the shared map head chain. now is real-time nanoseconds, matching the domain the dataplane stamps layer deadlines in.

func (*FWStateMapService) Register

func (m *FWStateMapService) Register(server *grpc.Server)

Register registers the map service on the given gRPC server.

func (*FWStateMapService) RunStaleLayerSweeper added in v0.1.0

func (m *FWStateMapService) RunStaleLayerSweeper(
	ctx context.Context, interval time.Duration,
)

RunStaleLayerSweeper reclaims stale layers and retries the deferred map objects until the context is cancelled.

func (*FWStateMapService) ServicesNames

func (m *FWStateMapService) ServicesNames() []string

ServicesNames returns the gRPC service names this controlplane serves.

func (*FWStateMapService) SweepMap added in v0.1.0

func (m *FWStateMapService) SweepMap(
	name string,
	source StaleLayerSource,
	mapCP cfwstate.MapObjectConfig,
	now uint64,
)

SweepMap reclaims one map, or declines to.

The precheck is what keeps a periodic sweep affordable: a settled table answers it from its own pointers, where reclaiming would publish two config generations and wait for every worker to pick them up.

func (*FWStateMapService) SweepStaleLayers added in v0.1.0

func (m *FWStateMapService) SweepStaleLayers(ctx context.Context)

SweepStaleLayers reclaims every map that has something to reclaim.

A map with nothing parked and no drained layer behind its head is skipped without publishing a generation, so a settled table costs a pointer test rather than two barriers. The whole round is skipped while the instance has published no time, since deciding a layer has drained means deciding every deadline in it has passed.

func (*FWStateMapService) UnaryServerInterceptor

func (m *FWStateMapService) UnaryServerInterceptor() grpc.UnaryServerInterceptor

UnaryServerInterceptor returns the service's gRPC metrics interceptor, or nil when metrics are not configured.

type MapLayerReclaimer

type MapLayerReclaimer interface {
	UnlinkStaleLayers(now uint64) error
	FreeStaleLayers() error
}

MapLayerReclaimer parks and releases a fwtable's stale layers around the generation barrier.

cfwstate.MapObjectConfig satisfies this interface; the seam lets unit tests record reclamation calls without a live shared-memory handle.

type MapOption

type MapOption func(*mapOptions)

MapOption configures an FWStateMapService.

func WithLog

func WithLog(log *zap.Logger) MapOption

WithLog sets the map service logger.

func WithMetrics

func WithMetrics(factory grpcmetrics.Factory) MapOption

WithMetrics enables collection of gRPC call metrics for the map service using the supplied factory.

type StaleLayerSource added in v0.1.0

type StaleLayerSource interface {
	MapLayerReclaimer

	HasReclaimable(now uint64) bool
	StaleLayerCount() uint32
}

StaleLayerSource is one map a sweep can consider: what it holds, and whether reclaiming it is worth the barriers.

Directories

Path Synopsis
fwstatemappb
v1

Jump to

Keyboard shortcuts

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