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
39 changes: 39 additions & 0 deletions api/network/v1alpha1/networkservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,33 @@ const (
NetworkServiceProtocolICMP NetworkServiceProtocol = "icmp"
)

// NetworkServiceSessionAffinity selects which parts of a flow a gateway hashes
// to pick its backend. Every gateway node hashes a service the same way, so a
// flow keeps its backend when it moves between nodes. Affinity is stateless:
// it holds only while the backend set is unchanged, and each change to that
// set moves about 1/N of clients, N being the number of backends.
//
// +kubebuilder:validation:Enum=None;ClientIP;ClientIPAndProtocol
type NetworkServiceSessionAffinity string

const (
// NetworkServiceSessionAffinityNone hashes the client's address and source
// port, so each connection is balanced on its own.
NetworkServiceSessionAffinityNone NetworkServiceSessionAffinity = "None"

// NetworkServiceSessionAffinityClientIP hashes the client's address only,
// so every connection from one address reaches the same backend, whatever
// its port, protocol or service among the services sharing that backend
// set.
NetworkServiceSessionAffinityClientIP NetworkServiceSessionAffinity = "ClientIP"

// NetworkServiceSessionAffinityClientIPAndProtocol hashes the client's
// address and the IP protocol, so every connection from one address over
// one protocol reaches the same backend, among the services sharing that
// backend set.
NetworkServiceSessionAffinityClientIPAndProtocol NetworkServiceSessionAffinity = "ClientIPAndProtocol"
)

// Ownership reasons for the Accepted condition on NetworkService and
// NetworkEgressPolicy, reserved for an admission check that verifies the
// requester is authorized for the VPC resources the object references. No such
Expand Down Expand Up @@ -196,6 +223,18 @@ type NetworkServiceSpec struct {
// +kubebuilder:validation:Minimum=1
// +kubebuilder:validation:Maximum=65535
BackendPort int32 `json:"backendPort,omitempty"`

// SessionAffinity selects which connections from one client share a
// backend: None, the default, balances each connection on its own;
// ClientIP pins a client address to one backend; ClientIPAndProtocol pins
// a client address and protocol to one. Two services pin a client to the
// same backend only if both set the same affinity and select the same
// backend addresses. A change takes effect on every gateway node without a
// restart, and rehashes every connection of the service, so most open
// connections reach a different backend afterwards.
// +optional
// +kubebuilder:default=None
SessionAffinity NetworkServiceSessionAffinity `json:"sessionAffinity,omitempty"`
}

// NetworkServiceStatus defines the observed state of a NetworkService.
Expand Down
16 changes: 16 additions & 0 deletions config/crd/galactic.datumapis.com_networkservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ spec:
- message: 'protocol is not served yet: the gateway datapath serves
tcp and udp only'
rule: self in ['tcp', 'udp']
sessionAffinity:
default: None
description: |-
SessionAffinity selects which connections from one client share a
backend: None, the default, balances each connection on its own;
ClientIP pins a client address to one backend; ClientIPAndProtocol pins
a client address and protocol to one. Two services pin a client to the
same backend only if both set the same affinity and select the same
backend addresses. A change takes effect on every gateway node without a
restart, and rehashes every connection of the service, so most open
connections reach a different backend afterwards.
enum:
- None
- ClientIP
- ClientIPAndProtocol
type: string
vipAddresses:
description: |-
VIPAddresses is the list of ingress VIP addresses this service provisions
Expand Down
27 changes: 18 additions & 9 deletions docs/agents/ARCHITECTURE-GATEWAY.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,24 @@ forward half, so it has exactly one branch, not two. Replies are
`vip_stats_table`'s hit counters (packets/bytes/last-seen), lazily
creating the row on first match. An empty backend list drops
(`DROP_REASON_EMPTY_BACKEND_LIST`).
4. **Maglev lookup.** Hash the client's own `(address, port)` and look up
the precomputed Maglev table (`vip_table`'s own `maglev_table` field,
populated by the Go control plane's `internal/maglev.Table` — see
`edgemap`'s doc comment) to get a backend index, deterministically and
statelessly: every gateway node computes the identical index for the
identical flow from the identical `(VIP, backend list)` input, which is
what makes this design safe under anycast/ECMP — a flow's packets
landing on a different gateway node mid-connection still resolve to the
same backend.
4. **Maglev lookup.** Hash the client's address with what the entry's
`affinity` byte selects (`EDGE_AFFINITY_*`, from
`NetworkService.spec.sessionAffinity`): its source port under `None`,
nothing more under `ClientIP`, its L4 protocol under
`ClientIPAndProtocol`. Look up the precomputed Maglev table
(`vip_table`'s own `maglev_table` field, populated by the Go control
plane's `internal/maglev.Table` — see `edgemap`'s doc comment) to get a
backend index, deterministically and statelessly: every gateway node
computes the identical index for the identical flow from the identical
`(VIP, backend list)` input, which is what makes this design safe under
anycast/ECMP — a flow's packets landing on a different gateway node
mid-connection still resolve to the same backend. Under either client-IP
affinity `KernelDatapath` keys the table by backend address alone rather
than address and port, so services over the same backend addresses on
different backend ports build the same table and pin a client to the
same backend. `affinity` occupies padding before `generation`, so it adds
nothing to `vip_value`'s size, and a zero byte reads as `None`. See
[configuration.md](../gateway/configuration.md#session-affinity).
5. **Push** a fresh 40-byte outer IPv6 header addressed to the chosen
backend's own worker-node SRv6 uSID (`vip_table`'s per-backend field,
resolved by the Go control plane the same way any other cross-node SRv6
Expand Down
24 changes: 24 additions & 0 deletions docs/api/network-gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,29 @@ _Appears in:_
| `icmp` | NetworkServiceProtocolICMP matches ICMP traffic: ICMPv6 (IP protocol 58)<br />on an IPv6 VIP and ICMP (IP protocol 1) on an IPv4 VIP. It has no ports.<br />It is declared but not served yet.<br /> |


#### NetworkServiceSessionAffinity

_Underlying type:_ _string_

NetworkServiceSessionAffinity selects which parts of a flow a gateway hashes
to pick its backend. Every gateway node hashes a service the same way, so a
flow keeps its backend when it moves between nodes. Affinity is stateless:
it holds only while the backend set is unchanged, and each change to that
set moves about 1/N of clients, N being the number of backends.

_Validation:_
- Enum: [None ClientIP ClientIPAndProtocol]

_Appears in:_
- [NetworkServiceSpec](#networkservicespec)

| Field | Description |
| --- | --- |
| `None` | NetworkServiceSessionAffinityNone hashes the client's address and source<br />port, so each connection is balanced on its own.<br /> |
| `ClientIP` | NetworkServiceSessionAffinityClientIP hashes the client's address only,<br />so every connection from one address reaches the same backend, whatever<br />its port, protocol or service among the services sharing that backend<br />set.<br /> |
| `ClientIPAndProtocol` | NetworkServiceSessionAffinityClientIPAndProtocol hashes the client's<br />address and the IP protocol, so every connection from one address over<br />one protocol reaches the same backend, among the services sharing that<br />backend set.<br /> |


#### NetworkServiceSpec


Expand All @@ -455,6 +478,7 @@ _Appears in:_
| `port` _integer_ | Port is the ingress port on VIPAddresses that this service load-balances,<br />1-65535. It is required for a protocol with ports (tcp, udp and sctp)<br />and must be unset for one without (esp, gre and icmp). Zero means unset<br />and is omitted when encoding, and an explicit 0 is refused. | | Maximum: 65535 <br />Minimum: 1 <br /> |
| `backendSelector` _[LabelSelector](https://kubernetes.io/docs/reference/generated/kubernetes-api/v/#labelselector-v1-meta)_ | BackendSelector selects the VPCAttachments (galactic.datumapis.com/v1alpha1,<br />in any namespace) that serve this service, matched against each<br />VPCAttachment object's own labels. Only attachments whose status.vpc<br />equals VPCRef are candidates, whatever their labels, so a selector cannot<br />reach into another tenant's VPC. Each selected attachment contributes the<br />IPv6 addresses in its spec.interface.addresses as backends, on<br />BackendPort, for both VIPs: its node translates the IPv4 VIP's flows to<br />and from IPv6, and its IPv4 addresses are never backends. An attachment<br />with no status.node, or no IPv6 address, is not a backend until it has<br />both. An empty selector is rejected rather than read as "every<br />attachment in the VPC". | | Required: \{\} <br /> |
| `backendPort` _integer_ | BackendPort is the destination port on every selected backend, 1-65535.<br />Like Port, it is required for tcp, udp and sctp and must be unset for<br />esp, gre and icmp. Zero means unset and is omitted when encoding, and an<br />explicit 0 is refused. | | Maximum: 65535 <br />Minimum: 1 <br /> |
| `sessionAffinity` _[NetworkServiceSessionAffinity](#networkservicesessionaffinity)_ | SessionAffinity selects which connections from one client share a<br />backend: None, the default, balances each connection on its own;<br />ClientIP pins a client address to one backend; ClientIPAndProtocol pins<br />a client address and protocol to one. Two services pin a client to the<br />same backend only if both set the same affinity and select the same<br />backend addresses. A change takes effect on every gateway node without a<br />restart, and rehashes every connection of the service, so most open<br />connections reach a different backend afterwards. | None | Enum: [None ClientIP ClientIPAndProtocol] <br /> |


#### NetworkServiceStatus
Expand Down
33 changes: 33 additions & 0 deletions docs/gateway/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ gateway node, named after that node (see the worked example above).
| `spec.port` | tcp, udp | `int32` (1–65535) | Ingress port on `vipAddresses` this rule load-balances. Required for a protocol with ports, refused for one without. |
| `spec.backendSelector` | Yes | `metav1.LabelSelector` | Selects the `VPCAttachment`s (`galactic.datumapis.com`, any namespace) that serve the rule. Each contributes its IPv6 interface addresses as backends of both VIPs; its IPv4 addresses are never backends. Must not be empty. |
| `spec.backendPort` | tcp, udp | `int32` (1–65535) | Destination port on every selected backend. Required for a protocol with ports, refused for one without. |
| `spec.sessionAffinity` | No | `string` enum | `None` (default), `ClientIP` or `ClientIPAndProtocol`: which connections from one client share a backend. See [Session affinity](#session-affinity). |
| `status.conditions` | — | — | `Accepted` (set `True` once gateway nodes exist in the namespace; not an ownership check, see below); the rule-wide `Programmed`, `Advertised`, `BackendsReady`, `Ready` and, with an IPv4 VIP, `IPv4Served` (see [Reading a rule's status](#reading-a-rules-status)); `<gateway-node>/Programmed` and `<gateway-node>/Advertised` per gateway node, `<gateway-node>/IPv4Served` per gateway node for a rule with an IPv4 VIP, and `<backend-node>/BackendsBound` per node hosting a backend. |

Example, the TCP rule from `deploy/containerlab/resources/galactic-gateway/iad/networkservices.yaml`:
Expand Down Expand Up @@ -632,6 +633,38 @@ cannot keep the backend nodes' IPv4 bindings alive. A node missing from the gate
uncached read of the API server first, so a new gateway node's conditions are
never removed while the cache has yet to see its `NetworkGateway`.

#### Session affinity

`spec.sessionAffinity` selects what a gateway hashes, besides the client's
address, to pick a backend:

| Value | Hashed | Connections from one client that share a backend |
| --------------------- | --------------------------- | ------------------------------------------------------- |
| `None` (default) | Address and source port | None: each connection is balanced on its own |
| `ClientIP` | Address | All, whatever their port, protocol or service |
| `ClientIPAndProtocol` | Address and the IP protocol | Those over one protocol, whatever their port or service |

Affinity keeps no state. Every gateway node hashes the same inputs over the
same Maglev table, so a client's flows reach the same backend whichever node
receives them. Under `ClientIP` and `ClientIPAndProtocol` the table depends
only on the backends' addresses, so services that set the same affinity and
select the same backend addresses pin a client to the same backend, even with
different `port`s and `backendPort`s. A rule's IPv4 and IPv6 VIPs are
separate tables over each family's backends, and a client's IPv4 and IPv6
addresses are different clients.

Affinity holds only while the backend set is unchanged. Adding or removing a
backend moves about 1 in N clients to another backend, N being the number of
backends, and each moved TCP connection is reset by its new backend. Clients
behind one NAT address share a backend under either client-IP mode.

A change to `sessionAffinity` takes effect on every gateway node at the next
reconcile, with no restart. It rehashes every connection of the service, so a
connection reaches a different backend afterwards with odds of (N-1)/N; change
it when the service can take that. Set it only once every gateway node runs a
release that serves it: a gateway without affinity support ignores the field
and balances by source port, so nodes would disagree on a client's backend.

#### Reading a rule's status

Rule-wide conditions say whether a rule serves, without reading
Expand Down
1 change: 1 addition & 0 deletions internal/controller/networkgateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,7 @@ func buildDesiredRule(
VIPAddresses: vips,
Protocol: string(rule.Spec.Protocol),
Port: port,
Affinity: string(rule.Spec.SessionAffinity),
Backends: v6Backends,
IPv4Backends: v4Backends,
}, unresolved, ipv4, nil
Expand Down
30 changes: 30 additions & 0 deletions internal/controller/networkgateway_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1984,3 +1984,33 @@ func TestNetworkGatewayReconciler_KeepsAdvertisementForRuleMissingFromCache(t *t
t.Errorf("BGPAdvertisement %s for a live rule was removed: %v", own.Name, err)
}
}

// TestBuildDesiredRule_CarriesSessionAffinity: the service's sessionAffinity
// reaches the engine as written, an unset one as empty, which the datapath
// treats as None.
func TestBuildDesiredRule_CarriesSessionAffinity(t *testing.T) {
backendRouter, backendAdv, backendVRF := newBackendFixtures("vpc-1")
idx := &backendSIDIndex{
routers: map[string]*bgpv1alpha1.BGPRouter{backendRouter.Name: backendRouter},
advs: []*bgpv1alpha1.BGPAdvertisement{backendAdv},
vrfInstances: map[string]*bgpv1alpha1.BGPVRFInstance{backendVRF.Name: backendVRF},
}
attachments := []*cloudv1alpha1.VPCAttachment{newBackendAttachment("vpc-1")}

for _, affinity := range []bgpv1alpha1.NetworkServiceSessionAffinity{
"",
bgpv1alpha1.NetworkServiceSessionAffinityNone,
bgpv1alpha1.NetworkServiceSessionAffinityClientIP,
bgpv1alpha1.NetworkServiceSessionAffinityClientIPAndProtocol,
} {
rule := newTestRule(testRuleName, "vpc-1", testVIP)
rule.Spec.SessionAffinity = affinity
dr, _, _, err := buildDesiredRule(rule, attachments, idx, nil, false, nil)
if err != nil {
t.Fatalf("affinity %q: %v", affinity, err)
}
if dr.Affinity != string(affinity) {
t.Errorf("affinity %q: DesiredRule.Affinity = %q", affinity, dr.Affinity)
}
}
}
Loading
Loading