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
12 changes: 12 additions & 0 deletions api/network/v1alpha1/gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ type NetworkGatewayStatus struct {
// health, an older one included, writes its status without it.
// +optional
BackendHealthFiltering bool `json:"backendHealthFiltering,omitempty"`

// LocalDelivery reports that this node's gateway delivers a flow whose
// backend is on this node through the node's own uplink, rather than
// dropping it: GALACTIC_GATEWAY_LOCAL_DELIVERY is on, its encapsulation
// source is the node's locator address and usid_ingress is attached to
// its public interface. Core serves an
// Edge listener backend on a gateway's node only while that gateway
// reports it and is Ready. The gateway sets it on every status write;
// one that cannot deliver locally, an older one included, writes its
// status without it.
// +optional
LocalDelivery bool `json:"localDelivery,omitempty"`
}

// NetworkGatewayList is a list of NetworkGateway resources.
Expand Down
89 changes: 89 additions & 0 deletions cmd/galactic-gateway/encapsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"fmt"
"log/slog"
"net/netip"
"sync"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"

bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1"
"go.datum.net/galactic/internal/config"
"go.datum.net/galactic/internal/plumbing/ebpf/attach"
"go.datum.net/galactic/internal/plumbing/srv6"
)

Expand Down Expand Up @@ -144,3 +146,90 @@ func encapSourceFromRouters(ctx context.Context, reader client.Reader, nodeName
}
return found, nil
}

// encapSourceIsNodeLocator reports whether encapSrc, as resolveEncapSource
// returned it, names this node: whether its first 64 bits, the Block and Node
// ID, are those of this node's locator address. edge_lb treats a backend uSID
// with those bits as local and delivers it through the node's own uplink, so
// the gateway reports LocalDelivery only when this holds.
//
// A derived source is the node's locator address by construction. A
// configured one is compared against the BGPRouter targeting this node: one
// that differs, or a router not readable yet, reports false and is logged,
// and nodeLocatorCheck asks again later.
func encapSourceIsNodeLocator(
ctx context.Context, reader client.Reader, nodeName, configured, encapSrc string,
) bool {
if configured == "" {
return true
}
src, err := netip.ParseAddr(encapSrc)
if err != nil {
return false
}
node, err := encapSourceFromRouters(ctx, reader, nodeName)
if err != nil {
slog.Warn("Not delivering to backends on this node: its locator could not be read",
"nodeName", nodeName, "error", err)
return false
}
if a, b := src.As16(), node.As16(); [8]byte(a[:8]) != [8]byte(b[:8]) {
slog.Warn("Not delivering to backends on this node: the configured SRv6 encapsulation source "+
"is outside this node's locator", "configured", src, "locator", node)
return false
}
return true
}

// usidIngressAttached reports whether usid_ingress is attached on iface, the
// public interface edge_lb runs on, where a flow for a backend on this node
// is handed up. Without it the packet meets the discard route for the node's
// own locator, so the gateway must not report LocalDelivery. A failure is
// logged at debug level: it is asked on every status write and is expected
// until galactic-cni attaches.
func usidIngressAttached(iface string) bool {
if iface == "" {
return false
}
if err := attach.IngressAttachedTo(iface); err != nil {
slog.Debug("Not delivering to backends on this node: usid_ingress is not attached to the public interface",
"interface", iface, "error", err)
return false
}
return true
}

// nodeLocatorRecheck is how often nodeLocatorCheck asks again while its answer
// is false.
const nodeLocatorRecheck = 30 * time.Second

// nodeLocatorCheck answers encapSourceIsNodeLocator for the gateway's life,
// since the gateway asks on every status write: it keeps a true answer, and
// while the answer is false asks again at most every interval, so a router
// that becomes readable after startup turns local delivery on without a
// restart.
type nodeLocatorCheck struct {
check func() bool
interval time.Duration
now func() time.Time

mu sync.Mutex
ok bool
asked time.Time
}

// holds reports the current answer, asking check when it is due.
func (c *nodeLocatorCheck) holds() bool {
c.mu.Lock()
defer c.mu.Unlock()
if c.ok {
return true
}
now := c.now()
if !c.asked.IsZero() && now.Sub(c.asked) < c.interval {
return false
}
c.asked = now
c.ok = c.check()
return c.ok
}
65 changes: 63 additions & 2 deletions cmd/galactic-gateway/encapsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
testEncapNode = "dfw-worker2"
testEncapLocator = "2001:db8:ff01::/48"
testEncapDerived = "2001:db8:ff01:1002::"
// testEncapOutside is a configured source outside testEncapLocator.
testEncapOutside = "2001:db8:ffff::1"
)

func testEncapRouter(namespace, name, node, locator string, nodeID int32) *bgpv1alpha1.BGPRouter {
Expand Down Expand Up @@ -56,9 +58,9 @@ func TestResolveEncapSource(t *testing.T) {
}{
{
name: "configured value wins over the router",
configured: "2001:db8:ffff::1",
configured: testEncapOutside,
routers: []client.Object{testEncapRouter("galactic-system", "r", testEncapNode, testEncapLocator, 0x1002)},
want: "2001:db8:ffff::1",
want: testEncapOutside,
},
{
name: "derived from this node's router",
Expand Down Expand Up @@ -235,3 +237,62 @@ func TestResolveEncapSourceListErrorStopsWithContext(t *testing.T) {
t.Errorf("resolveEncapSource() error = %v, want it to wrap the last list error", err)
}
}

// TestEncapSourceIsNodeLocator covers when the gateway reports LocalDelivery:
// a derived source always names this node; a configured one only while it
// shares the Block and Node ID of this node's locator address.
func TestEncapSourceIsNodeLocator(t *testing.T) {
router := testEncapRouter("galactic-system", "r", testEncapNode, testEncapLocator, 0x1002)
for _, tt := range []struct {
name string
configured string
encapSrc string
routers []client.Object
want bool
}{
{name: "derived", encapSrc: testEncapDerived, want: true},
{name: "configured inside the node's locator", configured: "2001:db8:ff01:1002::7",
encapSrc: "2001:db8:ff01:1002::7", routers: []client.Object{router}, want: true},
{name: "configured outside the node's locator", configured: testEncapOutside,
encapSrc: testEncapOutside, routers: []client.Object{router}, want: false},
{name: "configured with no router yet", configured: testEncapDerived,
encapSrc: testEncapDerived, want: false},
} {
t.Run(tt.name, func(t *testing.T) {
got := encapSourceIsNodeLocator(context.Background(), testEncapReader(t, tt.routers...),
testEncapNode, tt.configured, tt.encapSrc)
if got != tt.want {
t.Errorf("encapSourceIsNodeLocator = %t, want %t", got, tt.want)
}
})
}
}

// TestNodeLocatorCheck: a false answer is asked again only once the interval
// has passed, so a router readable after startup turns local delivery on
// without a restart, and a true answer is kept without asking again.
func TestNodeLocatorCheck(t *testing.T) {
now := time.Unix(1_700_000_000, 0)
answer, asked := false, 0
c := &nodeLocatorCheck{
interval: nodeLocatorRecheck,
now: func() time.Time { return now },
check: func() bool { asked++; return answer },
}
if c.holds() || asked != 1 {
t.Fatalf("first answer = true or asked %d times, want false after 1", asked)
}
answer = true
now = now.Add(nodeLocatorRecheck / 2)
if c.holds() || asked != 1 {
t.Errorf("asked again %d times before the interval passed", asked-1)
}
now = now.Add(nodeLocatorRecheck)
if !c.holds() || asked != 2 {
t.Errorf("answer after the interval = false or asked %d times, want true after 2", asked)
}
answer = false
if !c.holds() || asked != 2 {
t.Errorf("a true answer was asked again (%d) or dropped", asked)
}
}
23 changes: 23 additions & 0 deletions cmd/galactic-gateway/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func runCmd(cfg *config.GatewayConfig) error {
if ipv4VIPs {
ipv4 = newIPv4Forwarding()
}
// locator answers whether edge_lb recognizes this node's own backends
// (encapSourceIsNodeLocator); it stays nil, never, with no datapath.
var locator *nodeLocatorCheck
publicInterface := cfg.PublicInterface
// localDeliveryOn is GALACTIC_GATEWAY_LOCAL_DELIVERY: off, the gateway
// never reports local delivery, whatever the checks below answer.
localDeliveryOn := cfg.LocalDelivery
if cfg.DatapathEnabled {
if err := sysctl.SetProcSysPath(cfg.ProcSysPath); err != nil {
return err
Expand All @@ -169,6 +176,16 @@ func runCmd(cfg *config.GatewayConfig) error {
if err != nil {
return err
}
reader, configured := mgr.GetAPIReader(), cfg.SRv6Address
locator = &nodeLocatorCheck{
interval: nodeLocatorRecheck,
now: time.Now,
check: func() bool {
checkCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return encapSourceIsNodeLocator(checkCtx, reader, nodeName, configured, encapSrc)
},
}
gwDatapath, err = setupGatewayDatapath(ctx, cfg.PublicInterface, cfg.InternalInterfaces, encapSrc,
cfg.XDPAttach, ipv4, coverage)
if err != nil {
Expand Down Expand Up @@ -213,6 +230,9 @@ func runCmd(cfg *config.GatewayConfig) error {
ReturnPath: returnPath,
IPv4VIPs: ipv4VIPs,
BackendHealth: backendHealth,
LocalDelivery: func() bool {
return localDeliveryOn && locator != nil && locator.holds() && usidIngressAttached(publicInterface)
},
// Forwarding follows need: on only once an IPv4 VIP is about to load.
EnsureIPv4Forwarding: ipv4.enable,
Scope: scope,
Expand Down Expand Up @@ -317,6 +337,9 @@ func newRootCommand() *cobra.Command {
cmd.Flags().Bool("gateway-backend-health", false,
"Leave a backend its node reports Unhealthy out of the rule's Maglev table, once every gateway "+
"in the namespace does the same")
cmd.Flags().Bool("gateway-local-delivery", false,
"Deliver flows to a backend on this node, reporting status.localDelivery once the SRv6 address "+
"is the node's locator address and usid_ingress is attached to the public interface")
cmd.Flags().String("gateway-proc-sys-path", config.DefaultProcSysPath,
"Procfs sysctl root to write the datapath's forwarding sysctls under, such as a writable mount "+
"of the host's /proc/sys/net at <path>/net")
Expand Down
12 changes: 12 additions & 0 deletions config/crd/galactic.datumapis.com_networkgateways.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ spec:
x-kubernetes-list-map-keys:
- type
x-kubernetes-list-type: map
localDelivery:
description: |-
LocalDelivery reports that this node's gateway delivers a flow whose
backend is on this node through the node's own uplink, rather than
dropping it: GALACTIC_GATEWAY_LOCAL_DELIVERY is on, its encapsulation
source is the node's locator address and usid_ingress is attached to
its public interface. Core serves an
Edge listener backend on a gateway's node only while that gateway
reports it and is Ready. The gateway sets it on every status write;
one that cannot deliver locally, an older one included, writes its
status without it.
type: boolean
observedGeneration:
description: ObservedGeneration is the .metadata.generation this status
was computed from.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ spec:
# access as IPv4 VIPs covers it.
- name: GALACTIC_GATEWAY_BACKEND_HEALTH
value: "true"
# Off by default. Every lab gateway delivers flows to a backend
# on its own node, which the listener conformance test's local
# delivery case requires (tests/edgeconformance).
- name: GALACTIC_GATEWAY_LOCAL_DELIVERY
value: "true"
1 change: 1 addition & 0 deletions docs/api/network-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ _Appears in:_
| `observedGeneration` _integer_ | ObservedGeneration is the .metadata.generation this status was computed from. | | |
| `conditions` _[Condition](https://kubernetes.io/docs/reference/generated/kubernetes-api/v/#condition-v1-meta) array_ | Conditions contains the standard conditions for this resource. | | |
| `backendHealthFiltering` _boolean_ | BackendHealthFiltering reports that this node's gateway can leave a<br />backend reported Unhealthy out of a NetworkService's Maglev table.<br />Every gateway in the namespace applies health only while every<br />NetworkGateway there reports it, so all of them build the same table.<br />The gateway sets it on every status write; a gateway that cannot apply<br />health, an older one included, writes its status without it. | | |
| `localDelivery` _boolean_ | LocalDelivery reports that this node's gateway delivers a flow whose<br />backend is on this node through the node's own uplink, rather than<br />dropping it: GALACTIC_GATEWAY_LOCAL_DELIVERY is on, its encapsulation<br />source is the node's locator address and usid_ingress is attached to<br />its public interface. Core serves an<br />Edge listener backend on a gateway's node only while that gateway<br />reports it and is Ready. The gateway sets it on every status write;<br />one that cannot deliver locally, an older one included, writes its<br />status without it. | | |


#### NetworkService
Expand Down
Loading
Loading