diff --git a/docs/cni/environment-variables.md b/docs/cni/environment-variables.md index f7ae6def3..335dd5ced 100644 --- a/docs/cni/environment-variables.md +++ b/docs/cni/environment-variables.md @@ -102,7 +102,9 @@ field; as of this writing it is still env-only. - **`GALACTIC_CNI_EBPF_INTERFACES`** — comma-separated list of interface names (whitespace trimmed, duplicates/empties removed) to attach the ingress hook to. When set, `attach.ResolveInterfaces` uses this list - directly and skips auto-detection entirely. When unset, the + directly and skips auto-detection entirely. A value that yields no + names, such as one made only of commas or only of whitespace, is an + error rather than a request to auto-detect. When unset or empty, the interface(s) carrying the default IPv6 route or a BGP-learned route are auto-detected, counting every nexthop of a multipath (ECMP) route. Either way, any resolved interface that is itself a Linux bonding master is diff --git a/internal/plumbing/ebpf/attach/interfaces.go b/internal/plumbing/ebpf/attach/interfaces.go index c4835f7c1..04687093e 100644 --- a/internal/plumbing/ebpf/attach/interfaces.go +++ b/internal/plumbing/ebpf/attach/interfaces.go @@ -46,12 +46,13 @@ var ( // ResolveInterfaces returns the interface names the uSID datapath should attach // its TC-BPF ingress hook to. // -// When config.EnvCNIEBPFInterfaces is set it is parsed as a comma-separated -// list, with whitespace trimmed and duplicates and empty entries removed, and -// used as-is. That is the explicit override for multi-homed nodes where -// auto-detection is ambiguous. +// When config.EnvCNIEBPFInterfaces is non-empty it is parsed as a +// comma-separated list, with whitespace trimmed and duplicates and empty +// entries removed, and used as-is. That is the explicit override for +// multi-homed nodes where auto-detection is ambiguous. A non-empty value that +// yields no names, such as only commas or only whitespace, is an error. // -// Otherwise interfaces are auto-detected: those carrying the default IPv6 +// When it is unset or empty, interfaces are auto-detected: those carrying the default IPv6 // route first, then those carrying a BGP-learned route (every nexthop of a // multipath route counts), which is where a fabric // peer's SRv6 traffic arrives when locators travel over a segment the default @@ -67,7 +68,10 @@ func ResolveInterfaces() ([]string, error) { names []string err error ) - if override := strings.TrimSpace(os.Getenv(config.EnvCNIEBPFInterfaces)); override != "" { + // Whether the override is set is decided on the raw value, not the trimmed + // one: a value made only of whitespace is a malformed override, rejected + // like one made only of commas, rather than a request to auto-detect. + if override := os.Getenv(config.EnvCNIEBPFInterfaces); override != "" { names = parseInterfaceList(override) if len(names) == 0 { return nil, fmt.Errorf("attach: %s is set to %q but contains no usable interface names", diff --git a/internal/plumbing/ebpf/attach/interfaces_test.go b/internal/plumbing/ebpf/attach/interfaces_test.go index c74b7e5b8..7868fda1c 100644 --- a/internal/plumbing/ebpf/attach/interfaces_test.go +++ b/internal/plumbing/ebpf/attach/interfaces_test.go @@ -5,6 +5,7 @@ package attach import ( + "errors" "fmt" "net" "slices" @@ -63,6 +64,23 @@ func stubNonBondLinks(t *testing.T) { func TestResolveInterfaces_EnvOverride(t *testing.T) { stubNonBondLinks(t) + // No override case may fall through to auto-detection, which would + // otherwise read the host's routing table and make the result depend on + // the machine running the test. The stubs record the call and fail. + origRouteListFn, origLinkByIndexFn := routeListFn, linkByIndexFn + t.Cleanup(func() { + routeListFn, linkByIndexFn = origRouteListFn, origLinkByIndexFn + }) + var autoDetected bool + routeListFn = func() ([]netlink.Route, error) { + autoDetected = true + return nil, errors.New("auto-detection reached from an override case") + } + linkByIndexFn = func(int) (netlink.Link, error) { + autoDetected = true + return nil, errFixtureNotFound + } + tests := []struct { name string envValue string @@ -96,12 +114,17 @@ func TestResolveInterfaces_EnvOverride(t *testing.T) { }, {"OnlyCommasIsError", ",,,", nil, true}, {"OnlyWhitespaceIsError", " ", nil, true}, + {"WhitespaceAndCommasIsError", " , \t, ", nil, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Setenv(config.EnvCNIEBPFInterfaces, tt.envValue) + autoDetected = false got, err := ResolveInterfaces() + if autoDetected { + t.Fatalf("ResolveInterfaces() fell through to auto-detection for override %q", tt.envValue) + } if (err != nil) != tt.wantError { t.Fatalf("ResolveInterfaces() error = %v, wantError = %v", err, tt.wantError) }