Skip to content
Open
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
58 changes: 40 additions & 18 deletions docs/private-services/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,23 +366,45 @@ during an incompatible map replacement. A router restart is covered in

## CNI program reloads

A CNI restart preserves the service-policy gate and attachment incarnation
tokens when the live two-program chain uses exactly the same kernel map IDs
as the replacement programs. Both resolver frontends can continue forwarding
while those programs are replaced. Missing identities, incomplete chains and
changed map generations require new tokens; old authorization cannot transfer
to them. An incompatible map layout still drains authorization before replacing
maps, and the router must rebuild the policy before forwarding resumes. The
router does so on its next service-route resync, at most 30 seconds later, and
needs no restart. When a replaced map belongs to the service map set, the
router sees the new map IDs. When it does not, as when only the drop counters
grow, the router finds the gate it finalized closed, rebuilds its generation
behind it and reopens it.

This continuity applies to an unchanged set of referenced maps. A program that
starts referencing another map conservatively requires reauthorization even
if existing map layouts did not change. Router startup still performs its
cache-synchronized policy rebuild and stale-entry sweep.
Service authorization lives in the service policy map set, the maps listed in
`prog.ServicePolicyMapNames`:

| Map | Holds |
| ----------------------------- | ----------------------------------------- |
| `service_route_table` | Node-local service routes |
| `service_access_table` | Consumer access rows |
| `service_reverse_table` | Reply state |
| `service_remote_grant_table` | Grants for tunneled requests |
| `service_remote_target_table` | Producer interface of each remote grant |
| `service_deny_table` | Explicit denies |
| `service_peer_table` | Per-attachment next hops |
| `service_producer_set_table` | Producer sets of `LocalSet` routes |
| `service_policy_state_table` | The node's service-policy gate |
| `attachment_identity_table` | Attachment incarnation tokens |

A CNI restart keeps the service-policy gate and every attachment's identity
token when the live two-program chain references the same maps as the
replacement programs and shares every policy map with them. Both resolver
frontends keep forwarding while those programs are replaced. A map outside the
policy set may be recreated without either: when `drop_reasons` grows, the gate
stays open, the policy tables keep their rows and the tokens stay the same. A
router that restarts during such a rollout, in either order, keeps serving its
inherited generation. This covers service authorization only. A recreated
forwarding table starts empty and serves only what its writers put back.

An incompatible layout of a policy map drains authorization before the CNI
replaces any map, and so does a failure to tell which maps changed. The CNI
closes the gate, removes the access rows and empties the reverse and remote
grant tables, and the new programs get new tokens. Missing identities
and incomplete chains also get new tokens; old authorization cannot transfer
to them. The router must rebuild the policy before forwarding resumes. It sees
the new policy map IDs and does so on its next service-route resync, at most
30 seconds later, with no restart. A router restarting at the same time keeps
the gate closed until its startup sync finalizes.

A program that starts referencing another map conservatively requires new
tokens even if no existing map layout changed. Router startup still performs
its cache-synchronized policy rebuild and stale-entry sweep.

## Router restarts

Expand All @@ -399,7 +421,7 @@ sync.
The inherited gate closes, as it did before this behavior, when the startup
sync fails a dataplane mutation or cannot finalize. Only a finalizing router
opens the gate, and the CNI datapath only ever closes it, which it does when
it drains authorization to replace an incompatible map set. A compatible
it drains authorization to replace an incompatible policy map. Any other
reload keeps the pinned gate, so the router keeps the inherited generation
across it. A gate the previous process left closed, for example after a crash
during a rebuild, stays closed until the startup sync finalizes.
Expand Down
31 changes: 24 additions & 7 deletions internal/plumbing/ebpf/attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ func Start(pinDir string) (objs *prog.UsidObjects, ifaces []string, err error) {
return nil, nil, err
}
// Compatible reloads retain the pinned policy gate and attachment tokens.
// Load drains authorization before replacing incompatible maps, and a new
// gate defaults to disabled until the controller completes its startup sync.
// Load drains authorization before replacing an incompatible policy map,
// and a new gate defaults to disabled until the controller completes its
// startup sync.

ifaces, err = ResolveInterfaces()
if err != nil {
Expand Down Expand Up @@ -191,13 +192,29 @@ func Load(pinDir string) (objs *prog.UsidObjects, err error) {
// re-attaches, which blackholes every attachment on the node. An
// unchanged map keeps its rows.
// An already-attached old classifier keeps map FDs after their pins
// are removed. Revoke its service authorization before replacing any
// incompatible pin so it cannot keep forwarding against an orphaned
// policy generation if the subsequent reattach fails.
if drainErr := drainLegacyServiceAuthorization(pinDir); drainErr != nil {
err = fmt.Errorf("attach: revoke legacy service authorization before map replacement: %w", drainErr)
// are removed. When a policy map is replaced, revoke its service
// authorization first so it cannot keep forwarding against an
// orphaned policy generation if the subsequent reattach fails. A map
// outside the policy set holds no authorization: the old and new
// classifiers keep sharing one generation, so the gate and its tables
// stay as they are and a router restart in any order keeps serving.
incompatible, checkErr := mappin.Incompatible(spec, pinDir, nil)
if checkErr != nil {
// Which maps changed is unknown, so fail closed as for a policy map.
err = fmt.Errorf("attach: check pinned maps against the compiled map spec: %w", checkErr)
if drainErr := drainLegacyServiceAuthorization(pinDir); drainErr != nil {
err = errors.Join(err, fmt.Errorf("attach: revoke legacy service authorization: %w", drainErr))
}
return nil, err
}
if policy := policyMaps(incompatible); len(policy) > 0 {
slog.Warn("attach: service policy maps changed layout, draining service authorization",
"pinDir", pinDir, "maps", policy)
if drainErr := drainLegacyServiceAuthorization(pinDir); drainErr != nil {
err = fmt.Errorf("attach: revoke legacy service authorization before map replacement: %w", drainErr)
return nil, err
}
}
unpinned, unpinErr := mappin.UnpinIncompatible(spec, pinDir, nil)
if unpinErr != nil {
err = fmt.Errorf("attach: recreate incompatible pinned maps: %w", unpinErr)
Expand Down
83 changes: 78 additions & 5 deletions internal/plumbing/ebpf/attach/egress_reattach.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"golang.org/x/sys/unix"

"go.datum.net/galactic/internal/plumbing/ebpf/attachmentidentity"
"go.datum.net/galactic/internal/plumbing/ebpf/prog"
"go.datum.net/galactic/internal/plumbing/nldump"
)

Expand Down Expand Up @@ -122,8 +123,10 @@ func reattachEgressLinkLocked(link netlink.Link, serviceProgram, legacyProgram *
return false, nil
}
// A program replacement is not a new interface incarnation. Keep its
// authorization only when the complete old chain shares the new programs'
// map generation. Orphaned maps and incomplete chains require a new token.
// authorization only when the complete old chain references the same maps
// as the new programs and shares every service policy map with them. An
// orphaned policy map, a newly referenced map and an incomplete chain
// require a new token. A recreated map outside the policy set does not.
preserve, err := canPreserveEgressIdentity(filters, serviceProgram, legacyProgram, identityMap)
if err != nil {
return false, err
Expand Down Expand Up @@ -352,7 +355,77 @@ func sameProgramMaps(previous, current *ebpf.Program, identityID ebpf.MapID, ide
if identityRequired && !slices.Contains(newIDs, identityID) {
return false, nil
}
slices.Sort(oldIDs)
slices.Sort(newIDs)
return slices.Equal(oldIDs, newIDs), nil
oldKeys, err := authorizationMapKeys(oldIDs)
if err != nil {
return false, fmt.Errorf("inspect previous egress program maps: %w", err)
}
newKeys, err := authorizationMapKeys(newIDs)
if err != nil {
return false, fmt.Errorf("inspect replacement egress program maps: %w", err)
}
if oldKeys == nil || newKeys == nil {
// Without map names there is no telling a policy map from any other,
// so every map must be the same one.
slices.Sort(oldIDs)
slices.Sort(newIDs)
return slices.Equal(oldIDs, newIDs), nil
}
return slices.Equal(oldKeys, newKeys), nil
}

// authorizationMapKeys returns, sorted, one authorizationMapKey per map in
// ids. Two programs with equal keys reference the same set of maps and share
// every map that holds service authorization, while a map outside that set,
// such as a recreated drop counter, may differ. It returns nil when the kernel
// reports a map with no name.
func authorizationMapKeys(ids []ebpf.MapID) ([]string, error) {
keys := make([]string, 0, len(ids))
for _, id := range ids {
m, err := ebpf.NewMapFromID(id)
if err != nil {
return nil, fmt.Errorf("open map %d: %w", id, err)
}
info, err := m.Info()
_ = m.Close()
if err != nil {
return nil, fmt.Errorf("inspect map %d: %w", id, err)
}
if info.Name == "" {
return nil, nil
}
keys = append(keys, authorizationMapKey(info.Name, info.Type, info.KeySize, info.ValueSize, id))
}
slices.Sort(keys)
return keys, nil
}

// authorizationMapKey identifies one map a program references: its kernel
// name and shape, and for a service policy map its kernel ID too. The shape
// tells apart policy maps whose truncated kernel names collide, such as
// service_remote_grant_table and service_remote_target_table, so swapping
// them between roles cannot keep a token.
func authorizationMapKey(name string, typ ebpf.MapType, keySize, valueSize uint32, id ebpf.MapID) string {
key := fmt.Sprintf("%s/%d/%d/%d", name, typ, keySize, valueSize)
if isServicePolicyKernelName(name) {
key = fmt.Sprintf("%s#%d", key, id)
}
return key
}

// kernelObjNameLen is the longest object name the kernel keeps; it truncates
// longer ones.
const kernelObjNameLen = unix.BPF_OBJ_NAME_LEN - 1

// isServicePolicyKernelName reports whether name, as the kernel reports a map
// name, is a service policy map's.
func isServicePolicyKernelName(name string) bool {
for _, policy := range prog.ServicePolicyMapNames {
if len(policy) > kernelObjNameLen {
policy = policy[:kernelObjNameLen]
}
if name == policy {
return true
}
}
return false
}
Loading
Loading