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
1 change: 1 addition & 0 deletions docs/agents/ARCHITECTURE-CNI.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ auto-detect directly (see Configuration above).

- **Tenant egress can spread across every egress shard in the cluster (hashed mode).** With `GALACTIC_CNI_EGRESS_MODE=hashed`, a tenant VRF's `::/0` and NAT64 entries in `egress_route_table` are not a single shard SID but a group sentinel: `link_ifindex == 0xFFFFFFFF`, the group ID in the first four bytes of `sid` and the tenant's Argument in bytes 8 and 9. There is one group per translation class, NAT66 (group 0) and each configured NAT64 prefix (groups 1-3), and a group holds only the `EgressShard`s whose status can translate its class (an IPv6 masquerade address for NAT66; an IPv4 address and that `nat64Prefix`, or `translatesWellKnownPrefix` for `64:ff9b::/96`, for NAT64), so traffic can never be placed on a shard that would drop it. `usid_egress` resolves the sentinel in `resolve_egress_group`: it looks the group up in `egress_shard_groups`, an `ARRAY_OF_MAPS` whose slot holds the group's current snapshot (one immutable `struct egress_shard_group_value`: Maglev table plus every member's SID, next hop, flags and slot generation), hashes the tenant's source address (or the 5-tuple, `GALACTIC_CNI_EGRESS_HASH=flow`) with FNV-1a over `struct egress_shard_pin_key`, follows a live pin in `egress_shard_pin` or else the Maglev table, completes the member's base SID with the Argument and encapsulates as for an ordinary entry. Every read comes from one snapshot. `egressroutemap.KernelGroupStore` publishes a change by creating a new inner map, filling it and swapping it into the outer slot in one update, so a packet sees the old snapshot or the new one and the kernel frees the old one after RCU; an ordinary array value updated in place was measured to hand out torn records (17 of 55,000 packets mixed one shard's SID with another's MACs under concurrent updates), the snapshot swap none. `egress_shard_group_template`, an unused instance of the inner map type, exists only so clang older than 21 emits the value's full BTF. A pin is honoured only while its slot still carries the generation it recorded; generations come from `next_slot_generation`, published in the snapshot, and an in-process high-water mark that also covers failed publications, so a slot's generation is never reused (see `ShardGroupTable`'s doc comment and `docs/nat/configuration.md#how-the-groups-are-published`). The groups are written only by `galactic-cni run` (`internal/installer/egressgroup.go`): it watches the cluster's `EgressShard`s (a candidate needs `status.shardSID` with `Programmed` and `Ready` true and no deletion timestamp; `spec.drain` makes it draining), resolves each SID to an uplink neighbor (only a resolvable shard is alive), and rebuilds on any EgressShard, route or neighbor change as well as the 30 s sweep. CNI ADD (`attachreg.installVRFEgress`, also used by the datapath repair's `FillVRF`) writes each route to its class's group when any group is open, skips a class with no eligible shard, and fails like an unresolvable ordered list when a class's shards are all unreachable. Leaving hashed mode is a three-step protocol: the sweep marks every group `CLOSING` (still forwarding, but CNI ADD writes no new sentinel), then `EgressRouteTable.Refresh` moves every sentinel back to the first resolvable `GALACTIC_CNI_EGRESS_SHARD_SIDS` entry, and the groups are disabled only after a sweep that read and wrote every route without error and found none left; an ADD that read the groups open re-checks after writing and rewrites its routes in ordered mode if they closed, so no sentinel is written after the last scan. `galactic_cni_egress_group_routes` reports the sentinels left. An empty or disabled group drops (`egress_shard_stats`, `galactic_cni_egress_shard_selections_total`), never fails open. Privileged tests run the datapath under BPF_PROG_RUN against snapshots the Go side published: placement against `ShardHash` and `internal/maglev` (`shardgroup_datapath_test.go`), publication faults at each step, restart, class isolation and concurrent swaps against live packets (`shardgroup_publication_test.go`). See [docs/nat/configuration.md](../nat/configuration.md#active-active-egress).
- **A packet too big for the fabric gets an ICMP error from the datapath, not a silent drop at the uplink.** `usid_egress` calls `send_too_big` after the MSS clamp and before it encapsulates. A packet longer than `encap_mtu_table`'s limit (the uplink MTU less 40, so 1460 at a 1500-byte fabric) is rewritten in place into an ICMPv6 Packet Too Big, or an ICMPv4 Fragmentation Needed for an IPv4 tenant with DF set, and redirected back out the interface it arrived on. The error carries the limit, comes from the attachment's own IPAM gateway (`tenant_gw_table`, keyed by host-side ifindex and written by `galactic-bgp` at ADD, removed by `galactic-veth`/`galactic-tap` at DEL), and quotes the packet's first 68 bytes with NPTv6 undone, so the tenant's path MTU discovery matches it to the socket that sent it. A GSO packet is judged by its segment length. No error is sent for an IPv4 packet without DF, for an ICMP error, past a per-CPU token bucket of 1,000 a second with a burst of 100 (`pmtu_icmp_bucket`), or for an attachment with no gateway in that family; those are dropped and counted as `fib_frag_needed`. Each outcome is counted in `pmtu_stats`, exported as `galactic_usid_pmtu_packets_total{result}`, and the limit as `galactic_usid_pmtu_limit_bytes`. The same `mssclamp.Reconciler` writes `encap_mtu_table` and `mss_clamp_table` from one `GALACTIC_CNI_TCP_MSS_CLAMP` setting, so the two cannot disagree. A pod's or guest's TCP and UDP packets usually arrive `CHECKSUM_PARTIAL`, which no BPF helper can clear, and a device without checksum offload (a tap whose guest did not negotiate it) would finish that checksum into the middle of the error. So the error is laid out with room pushed at the front until the kernel's checksum spot (the original transport header plus 16 for TCP, 6 otherwise) lands on a trailing slot word, with a pad word chosen so the sum the kernel writes there equals what is already there. `usid_pmtu_tap_test.go` checks this against the kernel through a `virtio_net_hdr` tap, with offload on and off.
- **Per-VRF traffic is counted in each direction in its own per-CPU map, not in `vrf_value`.** `vrf_traffic_counters` is a `BPF_MAP_TYPE_PERCPU_HASH` keyed like `vrf_table` (Block<<12|Argument, so one VPC per node) holding packets, tenant bytes and claimed drops for `ingress` and `egress`. Per CPU, because the hot path then increments its own copy with no atomic and no shared cache line; a new map, because changing `vrf_value`'s layout would make `attach.Load` recreate `vrf_table` empty. The datapath only looks rows up: `VRFTable.Register` creates a VRF's row with `BPF_NOEXIST`, so registering again never resets it, `Unregister` removes it, and `Reconcile` ends with `SyncTraffic`, which creates rows for VRFs that predate the map and removes orphans, re-checking `vrf_table` before each delete and again after it, so a VRF registered while it runs keeps its row. `galactic-cni run` also calls `SyncTraffic` once the datapath loads, so an upgraded node counts from its first packet rather than its first 5-minute GC sweep. Every writer of `vrf_table` rows (CNI ADD, the datapath repair, the GC sweep, the ingress sidecar) goes through `VRFTable`, so none needs code of its own. Inbound counts at `usid_ingress`'s two redirects (warm FIB and `bpf_redirect_neigh`). Outbound counts at `usid_egress`'s encapsulating redirect, which covers egress shards, and its VIP reply redirect, and at the service path's redirects, remote ones inside `service_remote_encap` because its caller cannot tell a forwarded packet from a Packet Too Big. Same-node traffic between two attachments never reaches `usid_ingress`: the sender's `usid_egress` matches the receiver's local pass-through entry and hands the packet to the kernel, so that hit counts once in each direction on the shared VPC row, except a destination equal to the sender's own `tenant_gw_table` gateway. Bytes are the tenant packet's own length in both directions, unlike `vrf_value.bytes`, which counts the whole frame at step 6 and stays for `galactic_usid_vrf_bytes_total` and make-before-break checks. Exported as `galactic_usid_vrf_{forwarded_packets,forwarded_bytes,dropped_packets}_total{direction}`; see [docs/metrics.md](../metrics.md#traffic). The gateway check reads the destination from `service_fib_scratch` itself rather than taking a pointer to it: passed a map-value pointer as a subprogram argument, `usid_egress`'s verifier cost went past the kernel's 1M-instruction limit.
- **An uplink that drops out of auto-detection keeps `usid_ingress` for 60 seconds.** `attach.Watch` re-resolves the uplink set on every netlink link or route event, and an uplink found only through BGP-learned routes leaves that set whenever FRR withdraws them, which it does for every restart (#778). Detaching on the first miss dropped SRv6 traffic still arriving on that bond for 6 to 14 seconds. An attached interface whose link still exists now keeps its hook, and its place in the published uplink list, until it has been missing for `detachHoldDown`. Watch re-evaluates on its own when the hold-down ends, so an uplink that really stops carrying fabric routes is still detached. A deleted link is dropped at once.
- **CRD-driven config, no sidecar gRPC.** `galactic-bgp` writes `BGPVRFInstance`/`BGPAdvertisement` CRDs directly via a bare controller-runtime client; `galactic-router`'s reconciler picks them up. No in-node gRPC calls between any of the CNI-chain binaries and `galactic-router`.
- **DEL is intentionally minimal everywhere in the CNI chain; GC reclaims shared state asynchronously.** Every binary's own `cmdDel` only cleans up what it itself created *and* is safe to release immediately per-container: IPAM deallocation via `galactic-ipam`'s own on-disk marker file; guest-netns flush/host-device DEL in `galactic-veth`; and, in both `galactic-veth`/`galactic-tap`, deleting this attachment's own host/guest veth pair or tap device outright, since that interface is private to one attachment and no sibling pod/VM can ever depend on it. None of them delete the VRF, routes, the eBPF `vrf_table` entry, or `BGPAdvertisement`/`BGPVRFInstance` CRDs — those are keyed by `(vpc, vpcAttachment)` or `(vpc, node)` and may be shared/reused by another pod/VM (deleting them in DEL would race with a concurrent ADD during restarts). `galactic-router`'s GC controller (see [ARCHITECTURE-ROUTER.md](ARCHITECTURE-ROUTER.md)) reclaims orphaned CRDs, stale kernel VRFs, and stale eBPF entries once no live container still references them — see [docs/cni/gc-cmd-sequence.md](../cni/gc-cmd-sequence.md) for why the host veth/tap interface has no GC path of its own at all.
Expand Down
71 changes: 57 additions & 14 deletions docs/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,71 @@ and tenant VRFs.

### Traffic

| Metric | Type | Labels | Meaning |
|-----------------------------------|---------|--------------------------------------------------------------|--------------------------------------------------------------------------|
| `galactic_usid_vrf_packets_total` | counter | `block`, `argument`, `vrf_table_id`, `vpc`, `vpc_attachment` | Packets forwarded into a tenant VRF for one (uSID Block, Argument) entry |
| `galactic_usid_vrf_bytes_total` | counter | same | Bytes for the same entry |

- Both reset when an entry is re-registered, not only on restart; `rate()`
copes with this.
Each node keeps one VRF per VPC, so every series below is one VPC on one
node, keyed by its uSID (Block, Argument). `vpc_attachment` names the
attachment that registered the VRF last, not each attachment's own traffic.

| Metric | Type | Labels | Meaning |
|---------------------------------------------|---------|---------------------------------------------------------------------------|---------------------------------------------------------------------------------|
| `galactic_usid_vrf_forwarded_packets_total` | counter | `block`, `argument`, `vrf_table_id`, `vpc`, `vpc_attachment`, `direction` | Packets forwarded into (`ingress`) or out of (`egress`) the VRF |
| `galactic_usid_vrf_forwarded_bytes_total` | counter | same | Tenant bytes of those packets: the IP header and payload, in both directions |
| `galactic_usid_vrf_dropped_packets_total` | counter | same | Packets dropped after the datapath resolved them to the VRF, by direction |
| `galactic_usid_vrf_packets_total` | counter | `block`, `argument`, `vrf_table_id`, `vpc`, `vpc_attachment` | Inbound packets that matched the VRF, counted before any later drop |
| `galactic_usid_vrf_bytes_total` | counter | same | Bytes of those packets, measured as the whole tunneled frame with outer headers |

What each `direction` counts:

| `direction` | Counted when |
|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `ingress` | `usid_ingress` decapsulates a packet and redirects it to an attachment, or another attachment on the same node sends one to it |
| `egress` | a tenant packet is encapsulated toward another node or an egress shard, sent unencapsulated out the public uplink (a VIP reply), or delivered to another attachment on the node |

- A packet counts once, when the datapath has committed to forwarding it.
Traffic the datapath hands to the kernel to route (no matching egress
route) and fail-open paths are not counted.
- A packet too big for the fabric, answered with a Packet Too Big or
Fragmentation Needed, counts as neither forwarded nor dropped; see
`galactic_usid_pmtu_packets_total`.
- Traffic between two attachments of one VPC on the same node counts once
as `egress` and once as `ingress` on that VPC's series, when it is handed
to the kernel for delivery, so the kernel can still drop it. Traffic to the
sender's own gateway address counts in neither; traffic to another
attachment's gateway address in the same VPC counts as delivered.
- A private service route delivering to another attachment on the node
counts `egress` on the sender's VPC and `ingress` on the receiver's, which
may be a different VPC.
- A GSO or GRO super-packet counts as one packet carrying all its bytes.
- Drop reasons are in `galactic_usid_drops_total`, which has no VRF.
`galactic_usid_vrf_dropped_packets_total` has the VRF but no reason, and
covers drops in decapsulation, delivery, encapsulation and the PMTU check,
including inbound private service tunnels. Outbound private service route
drops (`service_route_*` from the sending attachment) and failed
public-uplink redirects appear only in `galactic_usid_drops_total`.
- The two older series keep their meaning: inbound only, claimed before any
drop, and the full frame. Prefer the `forwarded` series for throughput.
- Registering a VRF again never resets its counters. They reset when the
datapath reloads, or when the VRF's rows are removed and created again,
which `rate()` copes with.
- `vpc` and `vpc_attachment` are empty when the VPC attribution map has no
entry for that Argument. Treat `vpc=""` traffic as unattributed and watch
for it.
- Cardinality is one series per active Argument per node: at most 4095 per
Block.
- Cardinality is one series per active Argument per node for the older pair,
at most 4095 per Block, and twice that for each `direction` metric.

```promql
# Throughput per VPC, bits/s
sum by (vpc) (rate(galactic_usid_vrf_bytes_total[5m])) * 8
# Throughput per VPC and direction, bits/s
sum by (vpc, direction) (rate(galactic_usid_vrf_forwarded_bytes_total[5m])) * 8

# Top 10 VPCs on a node by outbound packet rate
topk(10, sum by (node, vpc) (rate(galactic_usid_vrf_forwarded_packets_total{direction="egress"}[5m])))

# Top 10 attachments by packet rate
topk(10, sum by (vpc, vpc_attachment) (rate(galactic_usid_vrf_packets_total[5m])))
# Share of a VPC's packets dropped after the datapath claimed them
sum by (vpc, direction) (rate(galactic_usid_vrf_dropped_packets_total[5m]))
/ (sum by (vpc, direction) (rate(galactic_usid_vrf_forwarded_packets_total[5m]))
+ sum by (vpc, direction) (rate(galactic_usid_vrf_dropped_packets_total[5m])))

# Traffic the datapath can't attribute to a VPC
sum by (node) (rate(galactic_usid_vrf_bytes_total{vpc=""}[5m]))
sum by (node) (rate(galactic_usid_vrf_forwarded_bytes_total{vpc=""}[5m]))
```

### Drops
Expand Down
6 changes: 6 additions & 0 deletions internal/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"go.datum.net/galactic/internal/plumbing/ebpf/attachreg"
"go.datum.net/galactic/internal/plumbing/ebpf/metrics"
"go.datum.net/galactic/internal/plumbing/ebpf/prog"
"go.datum.net/galactic/internal/plumbing/ebpf/usidmap"
"go.datum.net/galactic/internal/plumbing/radv"
bgpv1alpha1 "go.datum.net/network/api/v1alpha1"
)
Expand Down Expand Up @@ -552,6 +553,11 @@ func startEBPFDatapath(ctx context.Context, m *metrics.Metrics) (ebpfDatapathSta
state.mssClamp = newMSSClampState(objs.MssClampTable, objs.EncapMtuTable)
state.mssClamp.reconcile()
state.egressGroup.counters = objs.EgressShardCounters
// Give every registered VRF its traffic counter row now, rather than
// at the first GC sweep, so counting starts with the first packet.
if err := usidmap.NewRegistryFromObjects(objs).VRF.SyncTraffic(); err != nil {
slog.Warn("Failed to create per-VRF traffic counter rows; the next GC sweep retries", "err", err)
}
if err := m.RegisterDatapathCollector(objs); err != nil {
slog.Warn("Failed to register eBPF datapath metrics collector", "err", err)
}
Expand Down
42 changes: 42 additions & 0 deletions internal/plumbing/ebpf/attachreg/fill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,45 @@ func TestFillNode_NotConfiguredWritesNothing(t *testing.T) {
t.Errorf("FillNode(zero) = %+v, %v, %v; want nothing", written, uplinkErr, err)
}
}

// memCounterTable is memTable as a usidmap.CounterTable, creating a row only
// when it is absent.
type memCounterTable struct{ memTable }

func (m memCounterTable) Create(key, value any) error {
if _, ok := m.memTable[key]; ok {
return ebpf.ErrKeyExist
}
m.memTable[key] = value
return nil
}

// TestFillVRF_TrafficRowFollowsVRFRow checks the datapath repair creates a
// VRF's vrf_traffic_counters row with its vrf_table row, and that removing the
// VRF's rows removes it too.
func TestFillVRF_TrafficRowFollowsVRFRow(t *testing.T) {
recordRouteAdds(t, nil)
m := newMemMaps(true)
traffic := memCounterTable{memTable{}}
m.Registry.VRF.WithTraffic(usidmap.NewVRFTrafficTable(traffic))
a := testAttachment()

written, err := m.FillVRF(a, EgressConfig{})
if err != nil {
t.Fatalf("FillVRF: %v", err)
}
key, err := uformat.NewVRFKey(a.Block, a.Argument)
if err != nil {
t.Fatal(err)
}
if _, ok := traffic.memTable[uint64(key)]; !ok || len(traffic.memTable) != 1 {
t.Fatalf("traffic rows after FillVRF = %v, want one row for the VRF", traffic.memTable)
}

if err := m.RemoveVRFRows(a, written, nil, false); err != nil {
t.Fatalf("RemoveVRFRows: %v", err)
}
if len(traffic.memTable) != 0 {
t.Errorf("traffic rows after RemoveVRFRows = %v, want none", traffic.memTable)
}
}
Loading
Loading