From d87ca8e87237bd449a6f8b5fba125a617fff26ab Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Thu, 8 Oct 2026 22:34:26 -0400 Subject: [PATCH] fix: Restore pass-through routes for gateway-less attachments The startup and GC-tick datapath repair (#777) found an attachment's guest prefixes only in the pod-subnet routes out its host interface. The master plugin installs that route only for a family with a gateway, but CNI ADD registers the guest prefix as a local pass-through in egress_route_table either way. So after the maps were emptied, an attachment with a static address and no gateway got every row back except its pass-through routes, and traffic to its own address followed the VRF's default route until the workload was recreated. Each repair pass now lists this router's BGPAdvertisements once and adds each attachment's spec.prefixes, which ADD writes from the same IPAM result, to the prefixes found in the kernel, deduplicated by network. Where the advertisement shows an IPAM result but the kernel shows no gateway, the pass writes the all-zero tenant_gw_table row ADD writes for that case. An attachment with no advertisement, one marked no-addressing, or one belonging to another router keeps the kernel-only behavior. Advertisements named before 5a846f83 carry no node segment and are not found, so those attachments fall back the same way. The root repair test gains a gateway-less static attachment and the advertisement ADD publishes for every attachment. It fails on egress_route_table and tenant_gw_table without this change. galactic-cni's ClusterRole already allows listing BGPAdvertisements. Fixes #806 Co-Authored-By: Claude Opus 5.5 --- docs/agents/ARCHITECTURE-CNI.md | 7 +- internal/gc/datapath_repair.go | 96 ++++++++++++++++++-- internal/gc/datapath_repair_root_test.go | 37 +++++++- internal/gc/datapath_repair_test.go | 108 +++++++++++++++++++++++ 4 files changed, 237 insertions(+), 11 deletions(-) diff --git a/docs/agents/ARCHITECTURE-CNI.md b/docs/agents/ARCHITECTURE-CNI.md index 2e321e9d2..466eebc6c 100644 --- a/docs/agents/ARCHITECTURE-CNI.md +++ b/docs/agents/ARCHITECTURE-CNI.md @@ -84,8 +84,11 @@ attachments whose `BGPRouter` or `BGPVRFInstance` has not appeared yet (the latter for up to ten minutes), and again on every eBPF GC tick. It finds attachments by their host interface name and VRF master, takes the routing table and egress kind from the kernel, the guest prefixes from the pod-subnet -routes out that interface and the gateways from its own addresses, and the -Block and Argument from the `BGPRouter` and `BGPVRFInstance`. It only writes a +routes out that interface and from the attachment's `BGPAdvertisement`, the +gateways from its own addresses, and the Block and Argument from the +`BGPRouter` and `BGPVRFInstance`. The advertisement is needed because the +master plugin routes a family's guest prefix only when that family has a +gateway, while ADD registers it either way (#806). It only writes a row that is missing, and never one under the ingress sidecar's Block. The sidecar return path's tables are excluded by their reserved table ID range, since the sidecar's own `vrf_table` rows that also mark them may be among the diff --git a/internal/gc/datapath_repair.go b/internal/gc/datapath_repair.go index 696592cdf..dc55d4c1c 100644 --- a/internal/gc/datapath_repair.go +++ b/internal/gc/datapath_repair.go @@ -19,6 +19,7 @@ import ( "golang.org/x/sys/unix" "sigs.k8s.io/controller-runtime/pkg/client" + "go.datum.net/galactic/internal/crdnames" "go.datum.net/galactic/internal/plumbing/ebpf/attachreg" "go.datum.net/galactic/internal/plumbing/ebpf/egressroutemap" "go.datum.net/galactic/internal/plumbing/ebpf/uformat" @@ -99,7 +100,9 @@ type discoveredAttachment struct { // attachment interface and enslaved to a VRF. The VRF's table supplies the // routing table ID, the link type the egress kind, the routes in that table // out the interface and the interface's own non-link-local addresses the local -// prefixes and gateways. The Block and Node-ID come from nodeName's BGPRouter, +// prefixes and gateways. The attachment's BGPAdvertisement adds the guest +// prefixes ADD took from the IPAM result, which a family with no gateway never +// routes in the kernel. The Block and Node-ID come from nodeName's BGPRouter, // and the Argument from the VPC's BGPVRFInstance on that router. // // Only a missing row is written. A row that exists is left alone, whoever @@ -148,6 +151,10 @@ func RepairAttachmentDatapath( if err != nil { return result, err } + advertised, err := addressingForRouter(ctx, k8s, cfg.Namespace, routerName) + if err != nil { + return result, err + } maps, closer, err := openDatapathMapsFn(cfg.PinDir) if err != nil { @@ -166,7 +173,7 @@ func RepairAttachmentDatapath( return cfg.ForeignTableID != nil && cfg.ForeignTableID(tableID) } - attachments, counts, err := discoverAttachments(cfg.NodeName, block, vrfIDs, foreign) + attachments, counts, err := discoverAttachments(cfg.NodeName, block, vrfIDs, advertised, foreign) result.Skipped, result.Pending = counts.skipped, counts.pending if err != nil { return result, err @@ -335,6 +342,68 @@ func vrfIDsForRouter( return ids, nil } +// advertisedAddressing is what an attachment's BGPAdvertisement records of the +// IPAM result CNI ADD registered it from. +type advertisedAddressing struct { + // prefixes are the guest prefixes, the advertisement's spec.prefixes. + prefixes []string + // noAddressing is set when the attachment's config carries no ipam block, + // so ADD had no IPAM result at all. + noAddressing bool +} + +// addressingForRouter returns the addressing every BGPAdvertisement in +// namespace whose RouterRef names routerName records, keyed by advertisement +// name. +func addressingForRouter( + ctx context.Context, k8s client.Client, namespace, routerName string, +) (map[string]advertisedAddressing, error) { + list := &bgpv1alpha1.BGPAdvertisementList{} + if err := k8s.List(ctx, list, client.InNamespace(namespace)); err != nil { + return nil, fmt.Errorf("list BGPAdvertisements: %w", err) + } + out := make(map[string]advertisedAddressing, len(list.Items)) + for _, adv := range list.Items { + if adv.Spec.RouterRef.Name != routerName { + continue + } + a := advertisedAddressing{ + noAddressing: adv.Annotations[crdnames.AnnotationNoAddressing] == crdnames.AnnotationNoAddressingValue, + } + for _, p := range adv.Spec.Prefixes { + a.prefixes = append(a.prefixes, string(p)) + } + out[adv.Name] = a + } + return out, nil +} + +// mergePrefixes returns kernel followed by every prefix of advertised whose +// network kernel does not already hold. A prefix that does not parse is +// dropped, as registering it would fail. +func mergePrefixes(kernel, advertised []string) []string { + seen := make(map[string]struct{}, len(kernel)+len(advertised)) + for _, p := range kernel { + if _, n, err := net.ParseCIDR(p); err == nil { + seen[n.String()] = struct{}{} + } + } + merged := kernel + for _, p := range advertised { + _, n, err := net.ParseCIDR(p) + if err != nil { + slog.Debug("Datapath repair: ignoring unparsable advertised prefix", "prefix", p, "err", err) + continue + } + if _, ok := seen[n.String()]; ok { + continue + } + seen[n.String()] = struct{}{} + merged = append(merged, n.String()) + } + return merged +} + // unpad reverses the zero padding an interface name applies to a base62 // identifier, which CRD names do not carry. func unpad(s string) string { @@ -357,8 +426,11 @@ type discoveryCounts struct { // to a VRF, whose VRF table foreign claims, whose link type is neither veth // nor tap, or whose BGPVRFInstance's VRFID is out of range is counted as // skipped; one whose VPC has no BGPVRFInstance on this node yet as pending. +// advertised, keyed by BGPAdvertisement name, adds the guest prefixes the +// kernel does not route. func discoverAttachments( - nodeName string, block uint64, vrfIDs map[string]int32, foreign func(uint32) bool, + nodeName string, block uint64, vrfIDs map[string]int32, advertised map[string]advertisedAddressing, + foreign func(uint32) bool, ) (attachments []discoveredAttachment, counts discoveryCounts, err error) { links, err := listAllLinksFn() if err != nil { @@ -411,6 +483,14 @@ func discoverAttachments( errs = append(errs, fmt.Errorf("read addressing of %s: %w", attrs.Name, err)) continue } + if adv, ok := advertised[crdnames.BGPAdvertisementName(vpc, vpcAttachment, nodeName)]; ok { + prefixes = mergePrefixes(prefixes, adv.prefixes) + // ADD writes an all-zero tenant_gw_table row for an IPAM result + // with no gateway, and none only when there was no IPAM result. + if gateways == nil && !adv.noAddressing && len(adv.prefixes) > 0 { + gateways = &attachreg.Gateways{} + } + } attachments = append(attachments, discoveredAttachment{ Attachment: attachreg.Attachment{ VPC: vpc, @@ -477,10 +557,12 @@ func (c routeCache) get(tableID uint32) ([]netlink.Route, error) { // localAddressing recovers what CNI ADD registered as link's local prefixes and // gateways from the kernel. The guest prefixes are the pod-subnet routes the // master plugin installs in the VRF's table out link: unicast, not -// kernel-generated, with no gateway and not link-scoped. The gateways are -// link's own addresses that are not link-local, each also a host prefix. A link -// with no such address, a tap with no IPAM, has nil gateways. Where a family -// carries more than one address, the first the kernel lists is the gateway. +// kernel-generated, with no gateway and not link-scoped. The master plugin +// installs one only for a family with a gateway, so a family without one is +// missing here; discoverAttachments adds it from the BGPAdvertisement. The +// gateways are link's own addresses that are not link-local, each also a host +// prefix. A link with no such address has nil gateways. Where a family carries +// more than one address, the first the kernel lists is the gateway. func localAddressing(link netlink.Link, tableID uint32, routes routeCache) ([]string, *attachreg.Gateways, error) { tableRoutes, err := routes.get(tableID) if err != nil { diff --git a/internal/gc/datapath_repair_root_test.go b/internal/gc/datapath_repair_root_test.go index e7e67d8d8..468cb7b7a 100644 --- a/internal/gc/datapath_repair_root_test.go +++ b/internal/gc/datapath_repair_root_test.go @@ -84,6 +84,30 @@ func (a rootAttachment) localEgressPrefixes() []string { return prefixes } +// advertisement returns the BGPAdvertisement galactic-bgp's ADD publishes for +// a on nodeName: the guest prefixes of its IPAM result, or the no-addressing +// annotation when it has none. +func (a rootAttachment) advertisement(nodeName string) *bgpv1alpha1.BGPAdvertisement { + adv := &bgpv1alpha1.BGPAdvertisement{ + ObjectMeta: metav1.ObjectMeta{ + Name: crdnames.BGPAdvertisementName(a.vpc, a.vpcAttachment, nodeName), + Namespace: repairNamespace, + }, + Spec: bgpv1alpha1.BGPAdvertisementSpec{RouterRef: bgpv1alpha1.RouterRef{Name: repairRouter}}, + } + if a.ipam == nil { + adv.Annotations = map[string]string{crdnames.AnnotationNoAddressing: crdnames.AnnotationNoAddressingValue} + return adv + } + if a.ipam.IPv6Subnet != nil { + adv.Spec.Prefixes = append(adv.Spec.Prefixes, bgpv1alpha1.Prefix(a.ipam.IPv6Subnet.String())) + } + if a.ipam.IPv4Address != nil { + adv.Spec.Prefixes = append(adv.Spec.Prefixes, bgpv1alpha1.Prefix(a.ipam.IPv4Address.String()+"/32")) + } + return adv +} + func (a rootAttachment) gateways() *attachreg.Gateways { if a.ipam == nil { return nil @@ -287,9 +311,15 @@ func rebuildsWhatADDWrote(t *testing.T, reverse bool) { IPv4Gateway: net.ParseIP("172.21.1.129"), }}, {vpc: "kV", vpcAttachment: "ghi", ifaceType: attachreg.InterfaceTypeTap, vrfID: 0x22}, + // A static address with no gateway: the kernel has no route for it, + // so only its BGPAdvertisement records the guest prefixes (#806). + {vpc: "mW", vpcAttachment: "pq", ifaceType: attachreg.InterfaceTypeVeth, vrfID: 0x23, ipam: &cniipam.IPAMResult{ + IPv6Subnet: mustCIDR(t, "fd20:31:ff03::/96"), + IPv4Address: net.ParseIP("10.1.0.7"), + }}, } - objects := make([]client.Object, 0, 3) + objects := make([]client.Object, 0, 1+3+len(attachments)) objects = append(objects, &bgpv1alpha1.BGPRouter{ ObjectMeta: metav1.ObjectMeta{Name: repairRouter, Namespace: repairNamespace}, Spec: bgpv1alpha1.BGPRouterSpec{ @@ -300,7 +330,7 @@ func rebuildsWhatADDWrote(t *testing.T, reverse bool) { for _, vpc := range []struct { name string id int32 - }{{"jU", 0x21}, {"kV", 0x22}} { + }{{"jU", 0x21}, {"kV", 0x22}, {"mW", 0x23}} { objects = append(objects, &bgpv1alpha1.BGPVRFInstance{ ObjectMeta: metav1.ObjectMeta{Name: crdnames.BGPVRFInstanceName(vpc.name, nodeName), Namespace: repairNamespace}, Spec: bgpv1alpha1.BGPVRFInstanceSpec{ @@ -311,6 +341,9 @@ func rebuildsWhatADDWrote(t *testing.T, reverse bool) { }, }) } + for _, a := range attachments { + objects = append(objects, a.advertisement(nodeName)) + } k8s := fake.NewClientBuilder().WithScheme(gcTestScheme(t)).WithObjects(objects...).Build() pinDir := fmt.Sprintf("/sys/fs/bpf/galactic-repair-test-%d", os.Getpid()) diff --git a/internal/gc/datapath_repair_test.go b/internal/gc/datapath_repair_test.go index db93018eb..b6afafdad 100644 --- a/internal/gc/datapath_repair_test.go +++ b/internal/gc/datapath_repair_test.go @@ -237,6 +237,26 @@ func (f *repairFixture) addAddr(ifindex int, cidr string) { f.addrs[ifindex] = append(f.addrs[ifindex], *addr) } +// addAdvertisement adds the BGPAdvertisement galactic-bgp's ADD publishes for +// (vpc, vpcAttachment) on router, carrying prefixes, or the no-addressing +// annotation when noAddressing is set. +func (f *repairFixture) addAdvertisement(router, vpc, vpcAttachment string, noAddressing bool, prefixes ...string) { + adv := &bgpv1alpha1.BGPAdvertisement{ + ObjectMeta: metav1.ObjectMeta{ + Name: crdnames.BGPAdvertisementName(vpc, vpcAttachment, repairNode), + Namespace: repairNamespace, + }, + Spec: bgpv1alpha1.BGPAdvertisementSpec{RouterRef: bgpv1alpha1.RouterRef{Name: router}}, + } + if noAddressing { + adv.Annotations = map[string]string{crdnames.AnnotationNoAddressing: crdnames.AnnotationNoAddressingValue} + } + for _, p := range prefixes { + adv.Spec.Prefixes = append(adv.Spec.Prefixes, bgpv1alpha1.Prefix(p)) + } + f.objects = append(f.objects, adv) +} + // run installs the fixture's state behind every indirection and runs one // pass. func (f *repairFixture) run() (DatapathRepairResult, error) { @@ -492,6 +512,94 @@ func TestRepairAttachmentDatapath_TapWithoutIPAM(t *testing.T) { } } +// TestRepairAttachmentDatapath_AdvertisedPrefixes covers the guest prefixes +// only the attachment's BGPAdvertisement records: a static address with no +// gateway has no route in the kernel (#806). +func TestRepairAttachmentDatapath_AdvertisedPrefixes(t *testing.T) { + const ( + guest6 = "fd20:31:ff03::/96" + guest4 = "10.1.0.7/32" + ) + tests := []struct { + name string + // gateway gives the attachment the kernel state a gateway installs: + // the pod-subnet route and the host gateway address. + gateway bool + router string + noAddressing bool + advertised []string + wantRoutes []string + wantCount int + // wantGateway is whether a tenant_gw_table row is written at all. + wantGateway bool + }{ + { + name: "StaticNoGateway", + router: repairRouter, + // The IPv6 subnet as ADD records it, with the address's host bits. + advertised: []string{"fd20:31:ff03::5/96", guest4}, + wantRoutes: []string{guest6, guest4}, + wantCount: 2, + wantGateway: true, + }, + { + name: "AlreadyInKernel", + gateway: true, + router: repairRouter, + advertised: []string{guest6}, + wantRoutes: []string{guest6, "fd20:31::1/128"}, + wantCount: 2, + wantGateway: true, + }, + { + name: "NoAddressing", + router: repairRouter, + noAddressing: true, + }, + { + name: "OtherRouter", + router: "router-b", + advertised: []string{guest6}, + }, + { + name: "UnparsablePrefix", + router: repairRouter, + advertised: []string{"not-a-prefix", guest4}, + wantRoutes: []string{guest4}, + wantCount: 1, + // The advertisement still shows an IPAM result. + wantGateway: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := newRepairFixture(t) + vrf := f.addVRF("G0000000jUV", 10, 7) + f.addVRFInstance(crdnames.BGPVRFInstanceName("jU", repairNode), 0x21) + f.addHostLink(attachreg.InterfaceTypeVeth, "G0000000jUabcH", 11, vrf) + if tt.gateway { + f.addPodRoute(7, 11, guest6) + f.addAddr(11, "fd20:31::1/128") + } + f.addAdvertisement(tt.router, "jU", "abc", tt.noAddressing, tt.advertised...) + + result, err := f.run() + if err != nil { + t.Fatalf("RepairAttachmentDatapath: %v", err) + } + if result.Rebuilt.LocalEgressRoutes != tt.wantCount { + t.Errorf("rebuilt %d local pass-through routes, want %d", result.Rebuilt.LocalEgressRoutes, tt.wantCount) + } + wantPassThrough(t, f.mem, 7, tt.wantRoutes...) + + _, _, ok, err := f.mem.maps.Gateway.Get(11) + if err != nil || ok != tt.wantGateway { + t.Errorf("tenant_gw_table[11] present %v err %v, want present %v", ok, err, tt.wantGateway) + } + }) + } +} + // TestRepairAttachmentDatapath_SkipsUnusable covers every interface the pass // must leave alone, either for good or, with wantPending, until its // BGPVRFInstance appears. None of them leaves a row in any map.