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
82 changes: 47 additions & 35 deletions internal/plumbing/ebpf/attach/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,13 @@ func HasDeliver(ifaceName string) (bool, error) {
}

// Attach attaches program to the ingress hook of each named interface, through a
// clsact qdisc and a direct-action BPF filter, creating the qdisc if it does not
// exist.
// pinned tcx link at the head of the tcx chain, or through a clsact qdisc and a
// direct-action BPF filter on a kernel without tcx. tcx.go says why.
//
// Re-running against an interface that already carries this package's filter
// replaces it rather than stacking a duplicate, so repeated calls are
// idempotent. Every interface is attempted even if one fails, and the failures
// are joined and returned together.
// Re-running against an interface that already carries this package's link or
// filter updates it in place rather than stacking a duplicate, so repeated
// calls are idempotent. Every interface is attempted even if one fails, and the
// failures are joined and returned together.
func Attach(program *ebpf.Program, ifaceNames []string) error {
if program == nil {
return errors.New("attach: program is nil")
Expand All @@ -348,9 +348,10 @@ func Attach(program *ebpf.Program, ifaceNames []string) error {
return errors.New("attach: no interfaces to attach to")
}

sweepIngressLinks()
var errs []error
for _, name := range ifaceNames {
if err := attachOne(program, name, filterName, netlink.HANDLE_MIN_INGRESS); err != nil {
if err := attachIngressOne(program, name); err != nil {
errs = append(errs, fmt.Errorf("interface %q: %w", name, err))
}
}
Expand Down Expand Up @@ -688,10 +689,17 @@ func attachOneAtPriority(program *ebpf.Program, name, tcFilterName string, paren
err = fmt.Errorf("find link: %w", err)
return err
}
err = replaceFilter(program, link, tcFilterName, parent, priority, handle)
return err
}

if err = ensureClsact(link); err != nil {
err = fmt.Errorf("ensure clsact qdisc: %w", err)
return err
// replaceFilter attaches program to link as the direct-action filter
// tcFilterName, creating the clsact qdisc if needed. Unlike attachOneAtPriority
// it reports nothing to the attach hook, for callers that report on their own.
func replaceFilter(program *ebpf.Program, link netlink.Link, tcFilterName string, parent uint32,
priority uint16, handle uint32) error {
if err := ensureClsact(link); err != nil {
return fmt.Errorf("ensure clsact qdisc: %w", err)
}

filter := &netlink.BpfFilter{
Expand All @@ -706,63 +714,67 @@ func attachOneAtPriority(program *ebpf.Program, name, tcFilterName string, paren
Name: tcFilterName,
DirectAction: true,
}
if err = filterReplaceFn(filter); err != nil {
err = fmt.Errorf("attach tc-bpf filter: %w", err)
return err
if err := filterReplaceFn(filter); err != nil {
return fmt.Errorf("attach tc-bpf filter: %w", err)
}
return nil
}

// Detach removes this package's ingress filter from each named interface,
// leaving the clsact qdisc in place since another filter or a future attach may
// still need it.
// Detach removes this package's ingress tcx link and filter from each named
// interface, leaving the clsact qdisc in place since another filter or a future
// attach may still need it.
//
// An interface that already lacks the filter, or no longer exists at all, is not
// an error: the watch loop detaches interfaces that just left the resolved set,
// An interface that already lacks both, or no longer exists at all, is not an
// error: the watch loop detaches interfaces that just left the resolved set,
// by which time one may already be gone. Every interface is attempted even if
// one fails, and the failures are joined and returned together.
func Detach(ifaceNames []string) error {
var errs []error
for _, name := range ifaceNames {
if err := detachOne(name, filterName, netlink.HANDLE_MIN_INGRESS); err != nil {
if err := detachIngressOne(name); err != nil {
errs = append(errs, fmt.Errorf("interface %q: %w", name, err))
}
}
return errors.Join(errs...)
}

// detachOne removes the filter named tcFilterName from one interface's TC hook
// at parent, if present. Like attachOne it is the choke point every detach path
// goes through, so instrumenting it here observes every attempt whatever the
// caller. An interface that already lacks the filter, or no longer exists, is
// not an error.
func detachOne(name, tcFilterName string, parent uint32) (err error) {
// detachIngressOne removes usid_ingress from one interface, whichever way it
// was attached.
func detachIngressOne(name string) (err error) {
defer func() { detachHook(name, err) }()
err = errors.Join(detachIngressLink(name), detachFilter(name, filterName, netlink.HANDLE_MIN_INGRESS))
return err
}

// detachFilter removes the filter named tcFilterName from one interface's TC
// hook at parent, if present. An interface that already lacks the filter, or no
// longer exists, is not an error.
func detachFilter(name, tcFilterName string, parent uint32) error {
link, err := netlink.LinkByName(name)
if err != nil {
var notFound netlink.LinkNotFoundError
if errors.As(err, &notFound) {
err = nil
return nil
}
err = fmt.Errorf("find link: %w", err)
return err
return fmt.Errorf("find link: %w", err)
}
return deleteFilters(link, tcFilterName, parent)
}

filters, listErr := netlink.FilterList(link, parent)
if listErr != nil {
err = fmt.Errorf("list filters: %w", listErr)
return err
// deleteFilters removes every BPF filter named tcFilterName from link's hook at
// parent. It reports nothing to the detach hook.
func deleteFilters(link netlink.Link, tcFilterName string, parent uint32) error {
filters, err := filterListFn(link, parent)
if err != nil {
return fmt.Errorf("list filters: %w", err)
}
for _, f := range filters {
bpfFilter, ok := f.(*netlink.BpfFilter)
if !ok || bpfFilter.Name != tcFilterName {
continue
}
if delErr := filterDeleteFn(f); delErr != nil {
err = fmt.Errorf("delete tc-bpf filter: %w", delErr)
return err
if err := filterDeleteFn(f); err != nil {
return fmt.Errorf("delete tc-bpf filter: %w", err)
}
}
return nil
Expand Down
16 changes: 10 additions & 6 deletions internal/plumbing/ebpf/attach/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func TestLoadAttach_SurvivesRestartWithMapsIntact(t *testing.T) {
return fmt.Errorf("locator_table entry after restart: generation = %d, want 1", val.Generation)
}

// Re-attach must replace the existing filter, not stack a second
// one alongside it.
// Re-attach must update the existing tcx link or replace the
// existing filter, not stack a second attachment alongside it.
link, err := netlink.LinkByName(ifaceName)
if err != nil {
return fmt.Errorf("find link: %w", err)
Expand All @@ -162,11 +162,15 @@ func TestLoadAttach_SurvivesRestartWithMapsIntact(t *testing.T) {
if err != nil {
return fmt.Errorf("list filters: %w", err)
}
if len(filters) != 1 {
return fmt.Errorf("filter count after re-attach = %d, want 1 (idempotent replace, not a duplicate)",
len(filters))
tcx, err := tcxIngressIDs(ifaceName)
if err != nil && !errors.Is(err, ebpf.ErrNotSupported) {
return fmt.Errorf("query tcx ingress chain: %w", err)
}
return nil
if n := len(filters) + len(tcx); n != 1 {
return fmt.Errorf("attachments after re-attach = %d (%d filters, %d tcx), want 1 "+
"(idempotent replace, not a duplicate)", n, len(filters), len(tcx))
}
return Detach([]string{ifaceName})
})
if err != nil {
t.Fatalf("post-restart verification: %v", err)
Expand Down
21 changes: 11 additions & 10 deletions internal/plumbing/ebpf/attach/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
// pinned there instead of recreating them empty.
// 3. Resolves the interface set, from an explicit override or by
// auto-detection, and attaches usid_ingress to each one's ingress hook
// through a clsact qdisc and a direct-action filter. Classic TC-BPF rather
// than the newer tcx link mechanism, which keeps the attach path working
// across the kernel range the preflight check targets. Re-attaching
// replaces an existing galactic filter rather than stacking a duplicate,
// which together with the pins makes Start idempotent across a restart.
// through a pinned tcx link at the head of the tcx chain. A kernel without
// tcx gets a clsact qdisc and a direct-action filter instead. tcx.go says
// why tcx: a cluster CNI on the same uplink deletes classic filters it
// does not own. Re-attaching updates the existing link or replaces the
// existing filter rather than stacking a duplicate, which together with
// the pins makes Start idempotent across a restart.
//
// Once attached, the filter holds its own kernel reference to the program,
// independent of the process that loaded it. The returned objects can therefore
// stay open for the life of the process and be closed on shutdown without
// disrupting forwarding: closing releases this process's descriptors, and does
// not detach the filter or unpin the maps.
// Once attached, the pinned link or the filter holds its own kernel reference
// to the program, independent of the process that loaded it. The returned
// objects can therefore stay open for the life of the process and be closed on
// shutdown without disrupting forwarding: closing releases this process's
// descriptors, and does not detach the program or unpin the maps.
//
// This package populates none of the maps with real data and owns no
// health-check surface beyond Health itself. It builds the lifecycle the
Expand Down
4 changes: 2 additions & 2 deletions internal/plumbing/ebpf/attach/egress_reattach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func TestReattachEgress_MovesAttachmentsToTheNewProgram(t *testing.T) { //nolint
if tokenAfter != tokenBefore {
t.Errorf("idempotent CNI ADD rotated token from %d to %d", tokenBefore, tokenAfter)
}
if got, want := filterProgID(t, uplinkLink, filterName), progID(t, previous.UsidIngress); got != want {
t.Errorf("uplink0's usid_ingress filter runs program %d, want it untouched at %d", got, want)
if got, want := ingressProgID(t, uplinkLink), progID(t, previous.UsidIngress); got != want {
t.Errorf("uplink0's usid_ingress runs program %d, want it untouched at %d", got, want)
}

replaced, err = ReattachEgress(current.UsidServiceEgress, current.UsidEgress, current.AttachmentIdentityTable)
Expand Down
30 changes: 21 additions & 9 deletions internal/plumbing/ebpf/attach/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"log/slog"
"slices"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
Expand Down Expand Up @@ -89,9 +90,10 @@ func checkMapsReachable(objs *prog.UsidObjects) error {
return errors.Join(errs...)
}

// IngressAttachedTo reports whether usid_ingress's filter is on the ingress of
// iface, or of every member of iface where it is a bond or a VLAN on one,
// since tc ingress classifies a bond's traffic on its members. A nil error
// IngressAttachedTo reports whether usid_ingress, through its pinned tcx link or
// its classic filter, is on the ingress of iface, or of every member of iface
// where it is a bond or a VLAN on one, since tc ingress classifies a bond's
// traffic on its members. A nil error
// means a packet received on iface, or handed up from its XDP hook, reaches
// usid_ingress. galactic-gateway checks its public interface this way before
// it reports that it delivers to backends on its own node.
Expand Down Expand Up @@ -121,25 +123,31 @@ func checkAttached(ifaces []string) error {
return errors.Join(errs...)
}

// checkAttachedOne confirms the filter is present among name's ingress filters,
// matched by name rather than by descriptor number, which is process-local and
// not comparable against a netlink query result.
// checkAttachedOne confirms usid_ingress is on name's ingress, through this
// package's pinned tcx link or its classic filter. The filter is matched by
// name rather than by descriptor number, which is process-local and not
// comparable against a netlink query result.
func checkAttachedOne(name string) error {
iface, err := linkByNameFn(name)
if err != nil {
return fmt.Errorf("find link: %w", err)
}

onTCX, tcxErr := tcxIngressAttached(name, iface.Attrs().Index)
if onTCX {
return nil
}

filters, err := filterListFn(iface, netlink.HANDLE_MIN_INGRESS)
if err != nil {
return fmt.Errorf("list filters: %w", err)
return errors.Join(fmt.Errorf("list filters: %w", err), tcxErr)
}
for _, f := range filters {
if bpfFilter, ok := f.(*netlink.BpfFilter); ok && bpfFilter.Name == filterName {
return nil
}
}
return errors.New("galactic uSID ingress filter not attached")
return errors.Join(errors.New("galactic uSID ingress program not attached (no tcx link or tc filter)"), tcxErr)
}

// Handle bundles loaded objects with a health check, so a caller can query
Expand Down Expand Up @@ -193,7 +201,11 @@ func (h *Handle) Healthy() error {
reportPreemption(h.Objs)

healthErr := Health(h.Objs, ifaces)
if healthErr != nil {
if healthErr != nil || slices.ContainsFunc(ifaces, ingressLinkMisplaced) {
// A misplaced tcx link is still attached, so it is not unhealthy, but
// only a reconcile moves it back to the head. Nothing else asks for
// one when another program's link is created on an otherwise quiet
// node.
h.Watcher.Reconcile() // nil-safe no-op if no Watcher is wired up
}
if h.Watcher != nil && !h.Watcher.Alive() {
Expand Down
Loading
Loading