From 06f6aa3dccdd5c2f4273908d645e1b5d6ed615ab Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sat, 21 Nov 2026 06:00:21 -0500 Subject: [PATCH] fix(worker): Read veth peers from the link dump Local confirmed a NAT pair's peer, and a routed link's pod end, with netlink.VethPeerIndex, which asks ethtool through two SIOCETHTOOL ioctls. Under CPU load that call fails about once in 300,000 to 1,000,000 calls, returning "unexpected EOF". readPair then reported an existing pair missing. Read marked it Pending ("not created yet"), Plan wanted to write again, and Apply would have deleted and recreated the live pair. A veth reports its peer's ifindex as IFLA_LINK in the link dump Local already reads, and carries IFLA_LINK_NETNSID only when the peer is in another namespace. readPair now requires each end to name the other from this namespace. The routed-link read, creation and handoff report take the pod end's ifindex from the same attribute. galactic-nat's tenantEndMAC finds the tenant end the same way. TestSteadyStateRoot reads a ready rig and plans it 200 times, expecting Ready and an empty plan each pass. Without CPU stress it catches the old failure only by chance. Fixes #1471 --- cmd/galactic-nat/workerlocal.go | 25 ++++++++-- cmd/galactic-nat/workerlocal_test.go | 39 +++++++++++++++ internal/worker/local/kernel.go | 26 +++++----- internal/worker/local/lifecycle_root_test.go | 18 +++++++ internal/worker/local/peer_test.go | 52 ++++++++++++++++++++ internal/worker/local/read.go | 14 ++---- internal/worker/local/routed.go | 8 +-- 7 files changed, 153 insertions(+), 29 deletions(-) create mode 100644 internal/worker/local/peer_test.go diff --git a/cmd/galactic-nat/workerlocal.go b/cmd/galactic-nat/workerlocal.go index 455dde39d..8687f1a39 100644 --- a/cmd/galactic-nat/workerlocal.go +++ b/cmd/galactic-nat/workerlocal.go @@ -136,11 +136,7 @@ func tenantEndMAC(nat netlink.Link, tenantName string) (net.HardwareAddr, error) if tenantName != "" { tenant, err = netlink.LinkByName(tenantName) } else { - var peer int - peer, err = netlink.VethPeerIndex(&netlink.Veth{LinkAttrs: *nat.Attrs()}) - if err == nil { - tenant, err = netlink.LinkByIndex(peer) - } + tenant, err = vethPeer(nat, netlink.LinkByIndex) } if err != nil { return nil, fmt.Errorf("find the tenant end of %s: %w", nat.Attrs().Name, err) @@ -148,6 +144,25 @@ func tenantEndMAC(nat netlink.Link, tenantName string) (net.HardwareAddr, error) return tenant.Attrs().HardwareAddr, nil } +// vethPeer returns veth l's peer in this namespace, looked up by byIndex. A +// veth's IFLA_LINK is its peer's ifindex, and it carries IFLA_LINK_NETNSID +// only when the peer is in another namespace; reading them needs no ethtool +// ioctl, which under load failed intermittently. +func vethPeer(l netlink.Link, byIndex func(int) (netlink.Link, error)) (netlink.Link, error) { + a := l.Attrs() + if a.NetNsID >= 0 || a.ParentIndex <= 0 { + return nil, fmt.Errorf("%s has no veth peer in this namespace", a.Name) + } + peer, err := byIndex(a.ParentIndex) + if err != nil { + return nil, err + } + if peer.Attrs().ParentIndex != a.Index { + return nil, fmt.Errorf("link %d is not %s's veth peer", a.ParentIndex, a.Name) + } + return peer, nil +} + // syncVLANs writes a nat_vlan_egress row for every VLAN device on the host and // removes rows of devices that are gone. func (k *kernelLocal) syncVLANs() error { diff --git a/cmd/galactic-nat/workerlocal_test.go b/cmd/galactic-nat/workerlocal_test.go index 5f836c4d1..858edbe20 100644 --- a/cmd/galactic-nat/workerlocal_test.go +++ b/cmd/galactic-nat/workerlocal_test.go @@ -93,6 +93,9 @@ func TestKernelLocal_AttachesNativePinnedAndPrunes(t *testing.T) { } nat, tenant := addVeth(t, "gn0", "gt0") + if peer, err := vethPeer(nat, netlink.LinkByIndex); err != nil || peer.Attrs().Index != tenant.Attrs().Index { + t.Fatalf("vethPeer(gn0) = %v, %v; want gt0", peer, err) + } sid := netip.MustParseAddr("fc00:0:1:1:123::") target := localTarget{VPC: "A", NATName: "gn0", NATIfindex: nat.Attrs().Index, TenantName: "gt0", TenantSID: sid} if err := k.Program(target); err != nil { @@ -150,3 +153,39 @@ func TestKernelLocal_AttachesNativePinnedAndPrunes(t *testing.T) { // xdpAttachedDrv is IFLA_XDP_ATTACHED's value for a native program // (XDP_ATTACHED_DRV in linux/if_link.h). const xdpAttachedDrv = 1 + +// TestVethPeer checks that the NAT end's peer is found from its IFLA_LINK, +// and only when the peer names it back from this namespace. +func TestVethPeer(t *testing.T) { + link := func(idx, peer, netnsid int) netlink.Link { + attrs := netlink.NewLinkAttrs() + attrs.Name, attrs.Index, attrs.ParentIndex, attrs.NetNsID = "gn0", idx, peer, netnsid + return &netlink.Veth{LinkAttrs: attrs} + } + links := map[int]netlink.Link{10: link(10, 9, -1), 11: link(11, 12, -1)} + byIndex := func(i int) (netlink.Link, error) { + if l, ok := links[i]; ok { + return l, nil + } + return nil, unix.ENODEV + } + for _, c := range []struct { + name string + nat netlink.Link + want int + }{ + {"peer", link(9, 10, -1), 10}, + {"peer names another link", link(9, 11, -1), 0}, + {"peer is gone", link(9, 13, -1), 0}, + {"no peer reported", link(9, 0, -1), 0}, + {"peer in another namespace", link(9, 10, 2), 0}, + } { + got, err := vethPeer(c.nat, byIndex) + switch { + case c.want == 0 && err == nil: + t.Errorf("%s: vethPeer = %d, want an error", c.name, got.Attrs().Index) + case c.want != 0 && (err != nil || got.Attrs().Index != c.want): + t.Errorf("%s: vethPeer = %v, %v; want link %d", c.name, got, err, c.want) + } + } +} diff --git a/internal/worker/local/kernel.go b/internal/worker/local/kernel.go index c3213f726..93560e9ea 100644 --- a/internal/worker/local/kernel.go +++ b/internal/worker/local/kernel.go @@ -344,16 +344,23 @@ type pair struct { func (h *host) readPair(p pairWant) (pair, bool) { t, nat := h.byName[p.tenant], h.byName[p.nat] if t == nil || nat == nil || t.Type() != linkVeth || nat.Type() != linkVeth || - t.Attrs().Alias != alias(p.key) || nat.Attrs().Alias != alias(p.key) { - return pair{}, false - } - peer, err := netlink.VethPeerIndex(t.(*netlink.Veth)) - if err != nil || peer != nat.Attrs().Index { + t.Attrs().Alias != alias(p.key) || nat.Attrs().Alias != alias(p.key) || !vethPeers(t, nat) { return pair{}, false } return pair{tenant: t, nat: nat}, true } +// vethPeers reports whether veths a and b, both in this namespace, are each +// other's peer. A veth's IFLA_LINK is its peer's ifindex, and it carries +// IFLA_LINK_NETNSID only when the peer is in another namespace. Reading +// both from the link dump, rather than asking ethtool for the peer, keeps +// the answer to the one snapshot and needs no ioctl, which under load +// failed intermittently and made an existing pair look missing. +func vethPeers(a, b netlink.Link) bool { + aa, ba := a.Attrs(), b.Attrs() + return aa.NetNsID < 0 && ba.NetNsID < 0 && aa.ParentIndex == ba.Index && ba.ParentIndex == aa.Index +} + // collision returns an error when a link named name belongs to another claim // than the one alias a names. A Local link with no alias is what an // interrupted creation left, and is Local's to replace. @@ -524,14 +531,11 @@ func (h *host) readRouted(r routedWant, podNS netns.NsHandle) (routedLink, bool) return routedLink{}, false } want, err := netlink.GetNetNsIdByFd(int(podNS)) - if err != nil || want < 0 || l.Attrs().NetNsID != want { - return routedLink{}, false - } - peer, err := netlink.VethPeerIndex(l.(*netlink.Veth)) - if err != nil { + if err != nil || want < 0 || l.Attrs().NetNsID != want || l.Attrs().ParentIndex <= 0 { return routedLink{}, false } - return routedLink{host: l, podIndex: peer}, true + // IFLA_LINK is the pod end's ifindex in the pod's namespace. + return routedLink{host: l, podIndex: l.Attrs().ParentIndex}, true } // setUpInPod marks the pod end name in podNS with a and sets it up. The pod diff --git a/internal/worker/local/lifecycle_root_test.go b/internal/worker/local/lifecycle_root_test.go index 8646f7369..021537623 100644 --- a/internal/worker/local/lifecycle_root_test.go +++ b/internal/worker/local/lifecycle_root_test.go @@ -53,6 +53,24 @@ func (r *rig) read(vpc string) module.State { return st } +// TestSteadyStateRoot checks that a ready VPC whose links nothing changes +// reads Ready, and plans no writes, on every pass. A transient failure to +// confirm a pair's peer once read the pair as not created yet, and planned +// to recreate it; under CPU stress that took a few hundred passes to show, +// so without stress this test catches it only by chance. +func TestSteadyStateRoot(t *testing.T) { + r := readyRig(t, "steady") + for i := range 200 { + for _, v := range r.vpcs { + claims := r.claims(v) + wantReady(t, r.read(v.vpc), claims) + if !r.planEmpty(v.vpc, claims) { + t.Fatalf("pass %d: VPC %s: a plan would write again", i, v.vpc) + } + } + } +} + // TestUntrustedEntryRoot checks that a packet that is not claimed, or claims // a handoff it was not given, enters none: metadata with a wrong magic, as a // sender without the dispatcher could forge it at best; metadata naming a TAP diff --git a/internal/worker/local/peer_test.go b/internal/worker/local/peer_test.go new file mode 100644 index 000000000..c6883db83 --- /dev/null +++ b/internal/worker/local/peer_test.go @@ -0,0 +1,52 @@ +// Copyright 2026 Datum Cloud, Inc. +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package local + +import ( + "testing" + + "github.com/vishvananda/netlink" + + "go.datum.net/galactic/internal/worker/module" +) + +// veth is a veth as a link dump reports it: its peer's ifindex as +// IFLA_LINK, and IFLA_LINK_NETNSID only when the peer is elsewhere. +func veth(name, a string, idx, peer, netnsid int) *netlink.Veth { + attrs := netlink.NewLinkAttrs() + attrs.Name, attrs.Alias, attrs.Index, attrs.ParentIndex, attrs.NetNsID = name, a, idx, peer, netnsid + return &netlink.Veth{LinkAttrs: attrs} +} + +// TestReadPairPeers checks that readPair takes a pair as its own only when +// the link dump shows each end as the other's peer in this namespace. +func TestReadPairPeers(t *testing.T) { + p := pairWant{key: module.ClaimKey{Node: "n", VPC: "0000080", AttachmentUID: "u"}, nat: "nat", tenant: "ten"} + a := alias(p.key) + cases := []struct { + name string + tenant, nat *netlink.Veth + want bool + }{ + {"peers", veth("ten", a, 10, 9, -1), veth("nat", a, 9, 10, -1), true}, + {"tenant names another peer", veth("ten", a, 10, 11, -1), veth("nat", a, 9, 10, -1), false}, + {"NAT end names another peer", veth("ten", a, 10, 9, -1), veth("nat", a, 9, 11, -1), false}, + {"no peer reported", veth("ten", a, 10, 0, -1), veth("nat", a, 9, 0, -1), false}, + {"peers in another namespace", veth("ten", a, 10, 9, 3), veth("nat", a, 9, 10, 3), false}, + {"another claim's alias", veth("ten", a+"x", 10, 9, -1), veth("nat", a, 9, 10, -1), false}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + h := &host{byName: map[string]netlink.Link{"ten": c.tenant, "nat": c.nat}} + got, ok := h.readPair(p) + if ok != c.want { + t.Fatalf("readPair ok = %t, want %t", ok, c.want) + } + if ok && (got.tenant != c.tenant || got.nat != c.nat) { + t.Fatalf("readPair = %+v, want the tenant and NAT ends", got) + } + }) + } +} diff --git a/internal/worker/local/read.go b/internal/worker/local/read.go index 52f9f3902..a46b4ff33 100644 --- a/internal/worker/local/read.go +++ b/internal/worker/local/read.go @@ -402,21 +402,17 @@ func (k *Kernel) routedCheck(h *host, m *model, c module.Claim, vrfIdx int, objs // The link counts only while it reaches the owner's current pod. var podErr error present := false + var got routedLink if podNS, err := k.Pods.Open(r.podUID); err != nil { podErr = err } else { - _, present = h.readRouted(r, podNS) + got, present = h.readRouted(r, podNS) _ = podNS.Close() } if present { - out.handoffs[c.Key] = append(out.handoffs[c.Key], module.HandoffInterface{ - Role: workerv1alpha1.HandoffRoleHost, Name: r.host, Ifindex: int32(l.Attrs().Index)}) - if v, ok := l.(*netlink.Veth); ok { - if peer, err := netlink.VethPeerIndex(v); err == nil { - out.handoffs[c.Key] = append(out.handoffs[c.Key], module.HandoffInterface{ - Role: workerv1alpha1.HandoffRolePod, Name: r.pod, Ifindex: int32(peer)}) - } - } + out.handoffs[c.Key] = append(out.handoffs[c.Key], + module.HandoffInterface{Role: workerv1alpha1.HandoffRoleHost, Name: r.host, Ifindex: int32(l.Attrs().Index)}, + module.HandoffInterface{Role: workerv1alpha1.HandoffRolePod, Name: r.pod, Ifindex: int32(got.podIndex)}) } return func(ck *check, _ netlink.Link) { if podErr != nil { diff --git a/internal/worker/local/routed.go b/internal/worker/local/routed.go index 75d608c35..9b5827f29 100644 --- a/internal/worker/local/routed.go +++ b/internal/worker/local/routed.go @@ -124,11 +124,11 @@ func movePodEnd(r routedWant, podNS netns.NsHandle) (routedLink, error) { if err != nil { return routedLink{}, fmt.Errorf("local: look up new routed link %s: %w", r.host, err) } - peer, err := netlink.VethPeerIndex(l.(*netlink.Veth)) - if err != nil { - return routedLink{}, fmt.Errorf("local: read routed link peer: %w", err) + // IFLA_LINK is the pod end's ifindex in the pod's namespace. + if l.Attrs().ParentIndex <= 0 { + return routedLink{}, fmt.Errorf("local: routed link %s reports no peer", r.host) } - return routedLink{host: l, podIndex: peer}, nil + return routedLink{host: l, podIndex: l.Attrs().ParentIndex}, nil } // enslave enslaves l to vrfIdx.