Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 126 additions & 60 deletions cmd/galactic-gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"go.miloapis.com/instrumentation/labels"
"go.miloapis.com/instrumentation/metrics"

"go.datum.net/galactic/internal/config"
Expand Down Expand Up @@ -160,6 +161,14 @@ func installGateway(
ipv4 *ipv4Forwarding, coverage *datapathCoverage,
build func(*edgeprog.EdgedsrObjects) (edgemetrics.VPCAttribution, error),
) error {
// Each side's logical interface must pass the transport contract before
// anything is resolved to hook targets or attached. A side that breaks a
// rule, including one that does not exist, fails startup naming the side,
// the interface and the rule.
if invalid, _ := syncVLANEgress(nil, publicInterface, internalInterfaces); invalid != nil {
return fmt.Errorf("edge gateway interfaces break the transport contract: %w", invalid)
}

withLB := publicInterface != ""
var targets []string
if withLB {
Expand Down Expand Up @@ -297,43 +306,57 @@ func installGateway(
missing = append(missing, returnSet.reconcile(ctx, returnTargets)...)
}
coverage.set(missing)
syncVLANEgress(objs.VlanEgress, publicInterface, internalInterfaces)
// A side that broke a rule since the check above turns readiness off, as
// a later re-resolution would.
if invalid, _ := syncVLANEgress(objs.VlanEgress, publicInterface, internalInterfaces); invalid != nil {
coverage.setTransport(invalid)
}
go watchTargets(ctx, publicInterface, internalInterfaces, publicSet, returnSet, coverage, ipv4, objs.VlanEgress)

return nil
}

// syncVLANEgress fills edgedsr.c's vlan_egress from the gateway's configured
// sides: the public interface and every internal interface that is a VLAN
// netdevice. A route the datapath resolves through one of them leaves through
// its lower device with that side's tag pushed in XDP, since a native redirect
// to a VLAN netdevice transmits nothing. A side that cannot be resolved keeps
// no row and is reported; the attachment itself reports a missing interface.
// syncVLANEgress resolves the gateway's configured sides through the
// transport contract, publishes them through gatewayTransport, and fills
// edgedsr.c's vlan_egress from them: the public interface and every internal
// interface that is a VLAN netdevice. A route the datapath resolves through
// one of them leaves through its lower device with that side's tag pushed in
// XDP, since a native redirect to a VLAN netdevice transmits nothing. A side
// that cannot be resolved keeps no row. A nil map skips it and only resolves
// and reports.
//
// Every call publishes the derived sides through gatewayTransport: the first
// logs each side's mode, VLAN ID, parent and members, later ones log and count
// what changed, such as a VLAN recreated with a new ifindex.
func syncVLANEgress(m *ebpf.Map, publicInterface string, internalInterfaces []string) {
set, ifindexes, err := vlanegress.Resolve(
// The first call logs each side's mode, VLAN ID, parent and members, later
// ones log and count what changed, such as a VLAN recreated with a new
// ifindex. A failure is logged and counted once while it persists.
//
// It returns the contract rule a side broke, nil when none, and whether that
// changed since the previous call.
func syncVLANEgress(m *ebpf.Map, publicInterface string, internalInterfaces []string,
) (*transport.ValidationError, bool) {
set, ifindexes, err := vlanegress.ResolveWith(transportLinks,
vlanegress.Side{Side: transport.SidePublic, Names: []string{publicInterface}},
vlanegress.Side{Side: transport.SideFabric, Names: internalInterfaces},
)
invalid, changed := gatewayTransport.Record(set, err)
if err != nil {
gatewayTransport.Fail(err)
slog.Warn("Cannot resolve every edge gateway interface for VLAN egress; routes through the ones "+
"left out are redirected to the VLAN device as the FIB names it", "err", err)
}
if err := vlanegress.Sync(m, set, ifindexes); err != nil {
slog.Warn("Cannot update the edge gateway's VLAN egress map", "err", err)
}
if set != nil {
gatewayTransport.Observe(set)
if m != nil {
if err := vlanegress.Sync(m, set, ifindexes); err != nil {
slog.Warn("Cannot update the edge gateway's VLAN egress map", "err", err)
}
}
return invalid, changed
}

// gatewayTransport publishes the gateway's derived transport sides.
var gatewayTransport = transport.NewTracker("galactic-gateway")

// transportLinks is the netlink view the sides are resolved through; tests
// substitute a fake.
var transportLinks = transport.Netlink

// targetSet keeps one of the gateway's programs on a set of interfaces and
// reports the ones it does not cover: xdpattach.Set in direct mode,
// xdpattach.DispatchSet in dispatch mode.
Expand Down Expand Up @@ -532,14 +555,17 @@ func turnOffDatapath(ctx context.Context, pinDir string, slots ...xdpdispatch.Sl
}

// datapathCoverage is which of the datapath's resolved targets it is not
// attached to, kept by watchTargets and read by the readiness reporter. Its
// onChange, when set, is called whenever that changes, from watchTargets'
// goroutine.
// attached to, and the transport contract rule a configured side breaks, kept
// by watchTargets and read by the readiness reporter. Its onChange, when set,
// is called whenever either changes, from watchTargets' goroutine.
type datapathCoverage struct {
onChange func()

mu sync.Mutex
missing []string
// invalid is the rule a side broke at the latest re-resolution, nil
// while every side passes.
invalid *transport.ValidationError
// configured is the targets whose forwarding sysctls have been applied.
// Only watchTargets touches it once setup returns.
configured map[string]bool
Expand All @@ -556,6 +582,31 @@ func (c *datapathCoverage) Missing() []string {
return slices.Clone(c.missing)
}

// TransportInvalid reports the transport contract rule a configured side
// broke at the latest re-resolution, nil while every side passes.
func (c *datapathCoverage) TransportInvalid() *transport.ValidationError {
c.mu.Lock()
defer c.mu.Unlock()
return c.invalid
}

// setTransport records invalid as the rule a side breaks, nil for none, and
// calls onChange.
func (c *datapathCoverage) setTransport(invalid *transport.ValidationError) {
c.mu.Lock()
c.invalid = invalid
c.mu.Unlock()
if invalid != nil {
slog.Error("Edge gateway interfaces break the transport contract; reporting not ready",
"side", string(invalid.Side), "interface", invalid.Interface, labels.Reason, string(invalid.Rule))
} else {
slog.Info("Edge gateway interfaces pass the transport contract again")
}
if c.onChange != nil {
c.onChange()
}
}

func (c *datapathCoverage) set(missing []string) {
c.mu.Lock()
changed := !slices.Equal(c.missing, missing)
Expand Down Expand Up @@ -599,52 +650,67 @@ func (c *datapathCoverage) configure(targets []string) []string {
// link or route change, until ctx is done, and attaches each set's program to
// any resolved target it does not hold yet. A nil set is skipped. A
// resolution failure, such as a bond in the moment between losing a member and
// enslaving its replacement, keeps every attachment and skips that pass.
// enslaving its replacement, keeps every attachment and skips that pass. So
// does a side that breaks the transport contract, which coverage then reports
// as not ready until the side passes again.
func watchTargets(ctx context.Context, publicInterface string, internalInterfaces []string,
publicSet, returnSet targetSet, coverage *datapathCoverage, ipv4 *ipv4Forwarding, vlanEgress *ebpf.Map,
) {
xdpattach.OnNetlinkChange(ctx, func() {
syncVLANEgress(vlanEgress, publicInterface, internalInterfaces)
var (
targets []string
err error
)
if publicSet != nil {
if targets, err = edgeattach.ResolveTargets(publicInterface); err != nil {
slog.Warn("Re-resolve edge gateway public interface failed; keeping current attachments",
"interface", publicInterface, "err", err)
return
}
}
var returnTargets []string
if returnSet != nil {
if returnTargets, err = resolveReturnTargets(internalInterfaces); err != nil {
slog.Warn("Re-resolve edge gateway internal interfaces failed; keeping current attachments",
"err", err)
return
}
if overlap := publicReturnOverlap(targets, returnTargets); len(overlap) > 0 {
slog.Warn("Internal interfaces now resolve to public ones; keeping the return program off them",
"interfaces", overlap)
returnTargets = slices.DeleteFunc(returnTargets, func(t string) bool { return slices.Contains(overlap, t) })
}
}
resyncTargets(ctx, publicInterface, internalInterfaces, publicSet, returnSet, coverage, ipv4, vlanEgress)
})
}

// A target whose forwarding sysctls failed is left out of the set and
// counted missing; configure retries it on the next pass.
var missing []string
if publicSet != nil {
ready := coverage.configure(targets)
missing = append(publicSet.reconcile(ctx, ready), uncovered(targets, ready)...)
// resyncTargets is one watchTargets pass.
func resyncTargets(ctx context.Context, publicInterface string, internalInterfaces []string,
publicSet, returnSet targetSet, coverage *datapathCoverage, ipv4 *ipv4Forwarding, vlanEgress *ebpf.Map,
) {
invalid, changed := syncVLANEgress(vlanEgress, publicInterface, internalInterfaces)
if changed {
coverage.setTransport(invalid)
}
if invalid != nil {
return
}
var (
targets []string
err error
)
if publicSet != nil {
if targets, err = edgeattach.ResolveTargets(publicInterface); err != nil {
slog.Warn("Re-resolve edge gateway public interface failed; keeping current attachments",
"interface", publicInterface, "err", err)
return
}
if returnSet != nil {
ready := coverage.configure(returnTargets)
missing = append(missing, returnSet.reconcile(ctx, ready)...)
missing = append(missing, uncovered(returnTargets, ready)...)
}
var returnTargets []string
if returnSet != nil {
if returnTargets, err = resolveReturnTargets(internalInterfaces); err != nil {
slog.Warn("Re-resolve edge gateway internal interfaces failed; keeping current attachments",
"err", err)
return
}
coverage.set(dedupe(missing))
ipv4.track(append(slices.Clone(targets), returnTargets...))
})
if overlap := publicReturnOverlap(targets, returnTargets); len(overlap) > 0 {
slog.Warn("Internal interfaces now resolve to public ones; keeping the return program off them",
"interfaces", overlap)
returnTargets = slices.DeleteFunc(returnTargets, func(t string) bool { return slices.Contains(overlap, t) })
}
}

// A target whose forwarding sysctls failed is left out of the set and
// counted missing; configure retries it on the next pass.
var missing []string
if publicSet != nil {
ready := coverage.configure(targets)
missing = append(publicSet.reconcile(ctx, ready), uncovered(targets, ready)...)
}
if returnSet != nil {
ready := coverage.configure(returnTargets)
missing = append(missing, returnSet.reconcile(ctx, ready)...)
missing = append(missing, uncovered(returnTargets, ready)...)
}
coverage.set(dedupe(missing))
ipv4.track(append(slices.Clone(targets), returnTargets...))
}

// dedupe drops repeated names, keeping the first of each.
Expand Down
6 changes: 3 additions & 3 deletions cmd/galactic-gateway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ func runCmd(cfg *config.GatewayConfig) error {
}

// followCoverage keeps the readiness service SERVING only while coverage
// reports no missing interface, until ctx is done: once at the start, then on
// every nudge from changed.
// reports no missing interface and no side breaking the transport contract,
// until ctx is done: once at the start, then on every nudge from changed.
func followCoverage(ctx context.Context, coverage *datapathCoverage, healthSrv *grpchealth.Server,
changed <-chan struct{},
) {
for {
status := grpc_health_v1.HealthCheckResponse_SERVING
if len(coverage.Missing()) > 0 {
if len(coverage.Missing()) > 0 || coverage.TransportInvalid() != nil {
status = grpc_health_v1.HealthCheckResponse_NOT_SERVING
}
healthSrv.SetServingStatus(config.GRPCReadinessService, status)
Expand Down
Loading
Loading