Skip to content
Merged
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
25 changes: 20 additions & 5 deletions cmd/galactic-nat/workerlocal.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,33 @@ 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)
}
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 {
Expand Down
39 changes: 39 additions & 0 deletions cmd/galactic-nat/workerlocal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}
26 changes: 15 additions & 11 deletions internal/worker/local/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions internal/worker/local/lifecycle_root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions internal/worker/local/peer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
14 changes: 5 additions & 9 deletions internal/worker/local/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions internal/worker/local/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading