Skip to content

feat(agent): per-port TCP data path — capture chains, clusters and inbound chains (037 Phase 2, Release B part 3) - #895

Merged
bpalermo merged 5 commits into
mainfrom
feat/037-phase2b-capture-dport
Sep 21, 2026
Merged

bpalermo merged 5 commits into
mainfrom
feat/037-phase2b-capture-dport

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Proposal 037 Phase 2, Release B part 3a. Follows #893 (part 2, merged). This is where the per-port machinery becomes real.

What ships

The cache derives each service's non-primary raw-TCP ports from its endpoints' port_protocols, and the capture listener emits one destination_port-qualified chain per port:

cap_tcp_<svc>_<p>:  prefix_ranges [<VIP>/32], destination_port p
                    -> tcp_proxy -> tcp:<fqdn>:<p>

It targets the port's own cluster, not the floor cluster — the floor forwards to the pod's primary port, so routing :9000 through it would deliver that traffic to :8080.

Why destination_port is the whole mechanism

Envoy evaluates destination_port ahead of prefix_ranges, and use_original_dst — already set on the capture listener — makes it the port the client dialed before the REDIRECT. A chain matching only the ClusterIP outranks the HCM catch-all's application_protocols match and swallows the VIP's HTTP traffic. That is precisely why a service could not be both protocols.

Classification moved off the annotation

Previously the capture reconciler decided, reading aether.io/app-protocol from the generated mesh Service — the registrar's projection of a per-pod fact, one hop from its source. #878 is what happens when the two copies disagree.

Deriving in the cache means the chain and the cluster that serves it come from one map in one snapshot generation. That is also the structural half of #877: a chain whose cluster was never built kills connections silently, and tcp_proxy has no ODCDS cold path to recover.

Risk 4 — the listener drain

Regenerating a per-pod capture listener drains its connections, and a registry reload happens on every pod ADD/DEL anywhere on the node. So the trigger compares the derived port set, not the fact that a reload occurred.

This reuses SetCaptureTCPServices' existing change detection rather than adding a second trigger — one comparison over derived state. Derived slices are sorted, so a rollout that replaces every pod while declaring the same ports produces an identical slice and regenerates nothing.

Both directions are tested. The negative control is the second: removing the port comparison from equalTCPEntries makes the "a real change MUST regenerate" assertion fail — so the comparison is not decoration.

No behaviour change today

The derivation excludes the primary port, and a pod with one declared protocol has exactly one class. Every service currently on the cluster yields an empty set and gets no extra chains.

Worth recording alongside that: :18082 already works for pure-TCP services after Release A alone — their cap_tcp chain matches /32 with no port criterion, so any port to the VIP reaches the floor. The destination_port work adds something only for mixed services, which is exactly what this enables.

Verification

bazel build //... clean, 57 tests pass, lint clean, --config=race green on the touched packages.

nonPrimaryTCPPorts was extracted from deriveTCPPorts because the nested loops pushed //agent/internal/xds/cache past the gocognit threshold of 15.

Remaining in Release B

Per-port inbound floor chains with the SNI rule, the 421 vhost for services with no HTTP port, the scoped-mode blackhole chain, the #877 WARN, and the workload-requirements.md rewrite.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

bpalermo and others added 4 commits September 21, 2026 13:05
… (037 Phase 2, Release B part 3a)

Per-port raw-TCP capture chains, and the classification move that makes them
possible.

The cache now derives each service's NON-PRIMARY raw-TCP ports from its
endpoints' port_protocols, and the capture listener emits one
destination_port-qualified chain per port, targeting that port's own
tcp:<fqdn>:<port> cluster rather than the floor cluster (the floor forwards to
the pod's PRIMARY port, so routing :9000 through it would deliver that traffic
to :8080).

destination_port is the mechanism the whole proposal turns on. Envoy evaluates
it ahead of prefix_ranges, and use_original_dst -- already set on the capture
listener -- makes it the port the client dialed before the REDIRECT. A chain
matching only the ClusterIP outranks the HCM catch-all's application_protocols
match and swallows the VIP's HTTP traffic, which is precisely why a service
could not be both protocols.

Classification is derived from the ENDPOINTS, not from the mesh Service's
aether.io/app-protocol annotation. That annotation is the registrar's
projection of a per-pod fact, one hop removed from its source, and #878 is what
happens when the two copies disagree. Deriving in the cache means the chain and
the cluster that serves it come from one map in one snapshot generation --
which is also the structural half of #877, since a chain whose cluster was
never built kills connections silently and tcp_proxy has no ODCDS cold path.

The primary port is deliberately excluded: the portless floor chain already
reaches it, so a chain for it would add nothing.

Risk 4, the listener drain, is handled by reusing SetCaptureTCPServices'
existing change detection rather than adding a second trigger. Regenerating a
per-pod capture listener drains its connections, and a registry reload happens
on every pod ADD/DEL anywhere on the node -- so the comparison has to be over
the DERIVED port set, not over the fact that a reload occurred. Derived slices
are sorted, so a rollout that replaces every pod while declaring the same ports
produces an identical slice and regenerates nothing.

Both halves of that are tested, and the negative control is the second one:
removing the port comparison from equalTCPEntries makes the "a real change MUST
regenerate" assertion fail, so the comparison is not decoration.

No behaviour change for any service today: deriving excludes the primary, and a
pod with one declared protocol has exactly one class, so every existing service
yields an empty set and no extra chains.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Completes the previous commit. It emitted destination_port-qualified chains
naming tcp:<fqdn>:<port>, but nothing built those clusters -- exactly the
hazard proposal 037 lists as Risk 1.

No service on the cluster has a non-primary TCP port yet, so no such chain is
emitted today and nothing is currently broken. But the registry half already
accepts `=tcp` and registers under both keys, so a user annotating
`ports: "8080,9000=tcp"` would have got a chain pointing at a cluster that does
not exist. tcp_proxy has no ODCDS cold path, so Envoy kills those connections
silently: no NACK, no stat, nothing to distinguish it from a backend that is
down.

Each non-primary TCP port now gets an entry keyed tcp:<fqdn>:<port> with its
OWN load assignment, named <fqdn>:<port> and filtered to the endpoints that
advertise that port AS TCP. Sharing the bare-name EDS would put every pod of
the service in the pool including ones that do not serve the port -- the
per-port membership filter is what makes adding a port to a rolling Deployment
safe, exactly as it already is for HTTP (proposal 005).

These clusters DO carry SNI, unlike the floor. The floor must carry none (#306)
so it lands on the destination's default inbound chain, which forwards to the
pod's primary port. A non-primary port has no such default to fall back on: the
SNI is how the destination demuxes to the right loopback port, and it is safe
precisely because only a post-037 agent advertises such a port -- the same
agent that builds the matching inbound chain.

Adds the gate proposal 037 asks for: resolve every cluster named by any
tcp_proxy, across every listener, single and weighted, against the snapshot's
CDS set. With the per-port clusters suppressed but the chains still emitted --
the exact state of the previous commit -- it fails with

  filter chain "cap_tcp_tcp:multi.aether-test.aether.internal_5432" names
  cluster "tcp:multi.aether-test.aether.internal:5432", which is not in the
  snapshot

which is the bug it exists to catch.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
… 3b)

The destination half of per-port TCP, and it has to ship WITH the source half
rather than after it.

A raw-TCP port now gets a tcp_proxy chain matched on the port as SNI, instead
of the HCM chain every served port gets today:

  in_tcp_<pod>_<p>:  server_names [<p>]  ->  tcp_proxy -> app_<pod>_<p>

Exactly one of the two per port. Two chains matching the same server_names is a
listener Envoy cannot disambiguate, and it rejects the whole LDS update rather
than the offending chain -- leaving the pod on its previous listener, or
unreachable if it is new.

Why this cannot land separately from the source side: that side sets SNI on a
non-primary TCP port's upstream socket so this chain can match it. Without this
commit, the destination's existing per-port HCM chain matches server_names
["9000"] instead, and a raw-TCP stream lands on an HTTP filter that cannot
parse it. That is the #306 failure mode, reached by shipping half of a
mechanism -- so both halves are in one change.

The PRIMARY port deliberately gets no SNI chain: it is what the default floor
chain already serves, reached by a no-SNI mesh connection (#306), and a chain
for it would be dead config.

A malformed `=tcp` suffix yields no TCP ports rather than a guess, so the pod
keeps the HCM chains it had yesterday. The registration path rejects the same
value outright, so a pod can only reach that state by being annotated after
registration.

Negative control: emitting BOTH chains for a TCP port -- the obvious wrong
implementation -- fails with

  server_names "9000" claimed by both "in_tcp_app-0_9000" and "in_app-0_9000";
  Envoy rejects the whole listener

which is the ambiguity the design exists to avoid.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo bpalermo changed the title feat(agent): destination_port capture chains from a cache-derived map (037 Phase 2, Release B part 3a) feat(agent): per-port TCP data path — capture chains, clusters and inbound chains (037 Phase 2, Release B part 3) Sep 21, 2026
The TCP floor is mTLS-only: captureTCPClusters returns nothing without a node
SVID and a validation context. generateCaptureListener had no matching gate, so
it emitted cap_tcp_* chains regardless — a listener that ACCEPTED connections
with no cluster behind them. tcp_proxy has no ODCDS cold path, so those
connections died silently: with SPIRE disabled a mesh looked configured and
swallowed every raw-TCP connection to a TCP service.

The chains are now gated on exactly the condition the clusters are, factored
into one predicate so the two cannot drift apart again.

Failing CLOSED is deliberate. Building a plaintext TCP floor when identity is
unavailable would be a silent authentication downgrade, the #832/#843 class.
Suppressing the chains instead lets the connection fall to the passthrough and
be REJECTed by kube-proxy at the mesh Service port — an immediate ECONNREFUSED
the caller can act on — and the agent WARNs, rate-limited to once a minute
because this is also the normal startup state for a few seconds.

The gate needed a way to REOPEN, and finding that out is what the test bought.
Identity arrives asynchronously, so a capture listener built before it lands
carries no chains, and SetNodeIdentity only regenerates the snapshot — it does
not rebuild pod listeners. The first version of this change was therefore a
PERMANENT outage for any node whose TCP services were configured before its
SVID: exactly the bug it set out to fix, with the sign flipped.

Readiness is instead reconciled from generateSnapshot, alongside
recomputeInboundReadyClusters and for the reason stated there: the SPIRE bridge
calls SetNodeIdentity exactly once ever (`if firstServe`), so a trigger hanging
off it can be missed permanently — which is how main-worker-05 ran a whole
agent lifetime with no probe clusters on 2026-09-19. A comparison and an early
return in the steady state.

Negative controls, both seen red:
  - with identity absent, a cap_tcp_ chain in the snapshot fails the first
    subtest (the #877 bug itself);
  - with the reconcile removed, the chains never come back and the second
    subtest fails (the permanent-outage inversion).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
@bpalermo
bpalermo merged commit c8db866 into main Sep 21, 2026
22 checks passed
@bpalermo
bpalermo deleted the feat/037-phase2b-capture-dport branch September 21, 2026 16:48
bpalermo added a commit that referenced this pull request Sep 22, 2026
…iagnostics) (#899)

Two diagnostics that replace errors describing the proxy's confusion with ones
describing the caller's mistake.

421 for `http://<svc>/` when <svc> serves no HTTP port. The cache emitted the
ordinary cap_http vhost for every in-scope service including TCP-only ones,
pointing at an h2 cluster it never builds for them, so the caller got a 503
with cluster_not_found: deterministic, but indistinguishable from a cluster
that vanished mid-reload, and it never reached ODCDS so even the coordinator's
404 did not occur. Such a service now gets a direct_response vhost — 421, the
status defined as "cannot produce a response for this combination of scheme and
authority", with x-aether-error: no-http-port and a body naming the TCP
spellings to use instead. No application behind the mesh emits 421, so it
cannot be mistaken for an app's own error, and it is immediate rather than
waiting out a 2s ODCDS lookup that was never going to succeed.

Whether a service has an HTTP port is read from the CACHE — it has one exactly
when the HTTP pass built it a default entry — not from the app-protocol
annotation. That is the same fact the vhost would point at, so the two cannot
disagree, which is what #878 was.

cap_tcp_blackhole for scoped capture. The CNI redirects :18082 unconditionally
there (it cannot know which VIPs have TCP ports) and the scoped HCM chain is a
catch-all with no match criteria, so raw TCP to :18082 on a service with no TCP
port would hit the HCM and get a fabricated `HTTP/1.1 400` — the #460 shape.
A last chain matching destination_port alone (less specific than every
"/32 + port" chain, so it only catches what none of them claimed) sends it to a
static endpoint-less cluster: immediate close, counted in
cluster.blackhole.upstream_cx_none_healthy. Not emitted under redirect-all,
where passthrough -> kube-proxy REJECT is already attributable.

The blackhole cluster is emitted BEFORE the #877 identity gate, because its
chain is likewise ungated: it carries no transport socket and terminates
connections rather than forwarding them, so it needs no SVID — and gating the
cluster alone would leave the chain naming something absent.

That last point was caught by the chain-to-CDS gate from #895, not by review:
adding the chain without the cluster failed immediately with "filter chain
cap_tcp_blackhole names cluster blackhole, which is not in the snapshot". Third
occurrence of that hazard in this proposal, first one a gate caught before I
did.

Negative controls: suppressing the 421 vhost fails the TCP-only case while the
HTTP case still passes — a 421 for a service that DOES serve HTTP would be an
outage, not a diagnostic.


Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

Co-authored-by: Claude Opus 5 <[email protected]>
bpalermo added a commit that referenced this pull request Sep 26, 2026
…PR plan

D2 decided: CaptureDivertFwMark = 0xae71, same 0xae7x family as the passthrough
mark so they read as related, a distinct value so neither rule matches the
other's traffic, disjoint from kube-proxy's masked marks. Phase 1 pins both.

Q2 restated. The first phrasing asked whether transport_socket_matches selects
a QUIC upstream transport -- but aether retired that shape in #842. Per-source
identity is ONE upstream TLS socket whose custom_tls_certificate_selector (the
filter_state_override cert mapper) resolves the client cert per connection from
a hashable filter-state key, which also partitions the pool per (host, source
identity). So the real questions are Q2a (does QuicUpstreamTransport honour the
mapper) and Q2b (does the HTTP/3 pool fold the hashable key). Q2b=no would
re-open the #831 leak class on QUIC, which fails open. Asked of the Envoy tree;
the plan carries both branches.

Plan added: 12 PRs across Phases 1-5 with sizes and gates, every gate named as
something that must be seen red. Notable: Phase 2a must extend
TestUDPCaptureListenerResolvesAgainstCDS by hand because the #895 chain->CDS
gate cannot see a connection-less listener; Phase 4a's envoy_validate gate must
FAIL a chain missing the two resumption falses; Phase 4b's //test/mtlspool gains
a QUIC arm. Phase 4 must not merge before Phase 3a's selection assertion exists,
or the two traffic classes share a path with no e2e proving they stay apart.

Refs #916

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
bpalermo added a commit that referenced this pull request Sep 26, 2026
…hole (#943)

* docs(038): fold east-west QUIC in as a requirement and reassess the whole

The proposal carried QUIC as a "does NOT deliver" appendix, deferred on a
security-model objection that upstream has since resolved in the direction that
preserves aether's invariant (envoyproxy/envoy #47076, in the pinned snapshot).
A capture path that is the foundation for a second, identity-bearing traffic
class is a different proposal from one that only fixes plaintext selection, so
this restructures rather than appends.

WHAT CHANGES

Requirements section (R1-R6). Two traffic classes on one path -- plaintext
per-service UDP and the QUIC mesh transport -- with the destination port class
as the discriminator, so a plaintext datagram can never be offered to a QUIC
chain or vice versa. The mesh ports are constants; the service ports come from
the registry's PORT_PROTOCOL_UDP (#933/#934), so the CNI rule set is derived
from declared facts.

Phases 4 and 5 added. Phase 4 is the QUIC transport itself: inbound QUIC on
UDP:18008 beside TCP:18008 with require_client_certificate and the SAME
validation_context and SDS secrets as the TCP inbound; an HTTP/3 upstream that
presents the source's client cert; the east-west gateway on UDP:18009 last.
Resumption and early data off, gated, until the pin passes #47219. Phase 5 is
TCP capture moving to TPROXY, gated on the 0xae7e mark analysis and on Phase 4
being on in a soak -- not for symmetry, because TCP and QUIC on two capture
mechanisms is the split that produced #916.

Phase 1 now builds the rule set with a capture-mode parameter (R6) so Phase 5
is a flag flip, and must choose a divert mark disjoint from 0xae7e.

Two decisions flagged rather than made (D1 the plaintext dial spelling --
recommendation: the application port, with :18081 kept one release; D2 the
divert mark) and four open questions, the sharpest being Q2: whether
transport_socket_matches selects a QUIC upstream transport, which decides if
Phase 4's outbound is one cluster or one per source.

Alternatives gains "a separate port for QUIC" (rejected, R3, pinned by #942)
and "QUIC via HBONE/CONNECT-UDP" (rejected on 031's grounds). "Do nothing" is
downgraded from legitimate to weaker-now: #931 showed the plaintext path had been
silently broken for its whole life.

Refs #916

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

* docs(038): plaintext UDP dials :18082, the L4 spelling shared with raw TCP

Folds the one-number-both-transports rule (R3) one level down, which decides D1
and simplifies two phases.

A client dials <svc>.<ns>.<mesh-domain>:18082 for a UDP service exactly as it
does for a raw-TCP one; only the transport differs. That gives plaintext UDP
exactly ONE dialed port, so:

  - Phase 2 collapses from "one transparent listener per distinct UDP service
    port" to ONE listener on UDP:18082 per pod, matcher on destination IP.
    Listener count no longer scales with services or ports.
  - Phase 1's rule set becomes three literals (18082, 18008, 18009). No
    declared-port-set plumbing.
  - Q1 (how the CNI learns the declared UDP port set) is dissolved.
  - The CNI never needs to know which application ports UDP services bind;
    UDPLoadAssignment already rewrites the udp: cluster to the app port, which
    is exactly how tcp: clusters reach a raw-TCP backend from the shared :18082.

This is the TCP floor's shape under 037, transport for transport. The 09-23
draft's "distinct UDP port set is small" reasoning is preserved in the text as
history; the folded rule makes that set exactly one.

:18081/udp stays captured and routed to the plaintext class for one release.
ProxyTCPOutboundPort's name and the CNI's "18082 is a TCP spelling" comment go
stale under this rule; Phase 1 corrects the comment and may rename the constant.
The east-west port gate extends to 18082 in Phase 1 and 18008 in Phase 4.

Refs #916

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

* docs(038): decide D2, restate Q2 against the real mechanism, add the PR plan

D2 decided: CaptureDivertFwMark = 0xae71, same 0xae7x family as the passthrough
mark so they read as related, a distinct value so neither rule matches the
other's traffic, disjoint from kube-proxy's masked marks. Phase 1 pins both.

Q2 restated. The first phrasing asked whether transport_socket_matches selects
a QUIC upstream transport -- but aether retired that shape in #842. Per-source
identity is ONE upstream TLS socket whose custom_tls_certificate_selector (the
filter_state_override cert mapper) resolves the client cert per connection from
a hashable filter-state key, which also partitions the pool per (host, source
identity). So the real questions are Q2a (does QuicUpstreamTransport honour the
mapper) and Q2b (does the HTTP/3 pool fold the hashable key). Q2b=no would
re-open the #831 leak class on QUIC, which fails open. Asked of the Envoy tree;
the plan carries both branches.

Plan added: 12 PRs across Phases 1-5 with sizes and gates, every gate named as
something that must be seen red. Notable: Phase 2a must extend
TestUDPCaptureListenerResolvesAgainstCDS by hand because the #895 chain->CDS
gate cannot see a connection-less listener; Phase 4a's envoy_validate gate must
FAIL a chain missing the two resumption falses; Phase 4b's //test/mtlspool gains
a QUIC arm. Phase 4 must not merge before Phase 3a's selection assertion exists,
or the two traffic classes share a path with no e2e proving they stay apart.

Refs #916

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

* docs(038): Q2 answered from source — Phase 4b is one QUIC cluster per source SA

Q2a = no, Q2b = yes, established at 13144fb and verified against the file at
that commit.

Q2a: QuicClientTransportSocketFactory::create rejects a certificate selector at
config load (quic_client_transport_socket_factory.cc:93-94, UnimplementedError
"Client certificate selector not supported on QUIC"). The client cert is
installed once per crypto config via SSL_CTX_set_chain_and_key on the shared
QUICHE SSL_CTX (line 80), not per connection, because the QUICHE handshaker needs
direct access to the private key. Loud, not silent.

Q2b: the HTTP pool key is protocol-agnostic (ClusterEntry::httpConnPool calls
transportSocketFactory().hashKey), and QuicClientTransportSocketFactory inherits
CommonUpstreamTransportSocketFactory::hashKey, so HTTP/3 pools partition per
(host, source identity) exactly as h2 pools do.

Net: pooling does not reopen #831, but partitioning alone does not give
per-source identity because the presented certificate is the cluster's. So
Phase 4b is one QUIC cluster per source ServiceAccount -- the pre-#842 shape,
for QUIC only, bounded by local ServiceAccounts rather than mesh services and
never touching the TCP clusters. The mtlspool QUIC arm still matters: the
pooling guarantee is real even though the cert is per cluster.

D3 added: whether to pursue a per-connection selector for QUIC upstream in
Envoy. It is an Envoy limitation, not a design constraint, and if landed Phase
4b collapses back to one cluster. Unsized; Bruno's call; the plan does not
depend on it.

Refs #916

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant