diff --git a/feature/conn25/client.go b/feature/conn25/client.go index 2f55722ee..51d5a5e36 100644 --- a/feature/conn25/client.go +++ b/feature/conn25/client.go @@ -138,29 +138,27 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip } } - var mip, tip netip.Addr - var err error + var magicPool, transitPool *ippool if dst.Is4() { - mip, err = c.v4MagicIPPool.next() - if err != nil { - return nil, err - } - tip, err = c.v4TransitIPPool.next() - if err != nil { - return nil, err - } + magicPool, transitPool = c.v4MagicIPPool, c.v4TransitIPPool } else if dst.Is6() { - mip, err = c.v6MagicIPPool.next() - if err != nil { - return nil, err - } - tip, err = c.v6TransitIPPool.next() - if err != nil { - return nil, err - } + magicPool, transitPool = c.v6MagicIPPool, c.v6TransitIPPool } else { return nil, errors.New("unexpected neither 4 nor 6") } + // Addresses are only returned to the pools by the popExpired loop above, + // which can only see them once they're in c.assignments. So any failure + // before the insert below has to put them back itself, or they're leaked + // from the pool for the lifetime of the process. + mip, err := magicPool.next() + if err != nil { + return nil, err + } + tip, err := transitPool.next() + if err != nil { + magicPool.returnAddr(mip) + return nil, err + } as := &addrs{ dst: dst, magic: mip, @@ -169,6 +167,8 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip domain: domain, } if err := c.assignments.insertFromTTL(as, ttl); err != nil { + magicPool.returnAddr(mip) + transitPool.returnAddr(tip) return nil, err } err = c.enqueueAddressAssignment(as) diff --git a/feature/conn25/client_test.go b/feature/conn25/client_test.go index 6c9af69f9..f0c391e04 100644 --- a/feature/conn25/client_test.go +++ b/feature/conn25/client_test.go @@ -135,6 +135,83 @@ func TestReserveAddressesDeduplicated(t *testing.T) { } } +// TestReserveAddressesNoLeakOnFailure checks that a magic IP isn't lost from +// its pool when the reservation fails after the magic IP was handed out. Once +// an address is marked in use it can only be returned via popExpired, which +// never sees assignments that failed to be inserted, so a leak here is permanent. +func TestReserveAddressesNoLeakOnFailure(t *testing.T) { + // An empty transit pool is reachable from config: the magic and transit + // pools come from independent node capabilities. A single-address transit + // pool exhausts itself after one reservation. + for _, tt := range []struct { + name string + is6 bool + magic string + transit *netipx.IPSet + }{ + { + name: "v4-empty-transit-pool", + magic: "100.64.0.0/24", + transit: &netipx.IPSet{}, + }, + { + name: "v4-exhausted-transit-pool", + magic: "100.64.0.0/24", + transit: mustIPSetFromPrefix("169.254.0.0/30"), + }, + { + name: "v6-empty-transit-pool", + is6: true, + magic: "fd7a:115c:a1e0:a99c:0100::/80", + transit: &netipx.IPSet{}, + }, + { + name: "v6-exhausted-transit-pool", + is6: true, + magic: "fd7a:115c:a1e0:a99c:0100::/80", + transit: mustIPSetFromPrefix("fd7a:115c:a1e0:a99c:0200::/127"), + }, + } { + t.Run(tt.name, func(t *testing.T) { + const appName = "a" + c := newConn25(t.Logf).client + + magicPool, transitPool := &c.v4MagicIPPool, &c.v4TransitIPPool + if tt.is6 { + magicPool, transitPool = &c.v6MagicIPPool, &c.v6TransitIPPool + } + *magicPool = newIPPool(mustIPSetFromPrefix(tt.magic)) + *transitPool = newIPPool(tt.transit) + + // Reserve until the transit pool runs out, then keep going. Every + // failure past that point must leave the magic pool untouched. + var gotErr error + var reserved int + for i := range 5 { + dst := netip.AddrFrom4([4]byte{0, 0, 0, byte(i + 1)}) + if tt.is6 { + dst = netip.MustParseAddr(fmt.Sprintf("::%d", i+1)) + } + domain := must.Get(dnsname.ToFQDN(fmt.Sprintf("e%d.example.com.", i))) + if _, err := c.reserveAddresses(appName, domain, dst, 10); err != nil { + gotErr = err + } else { + reserved++ + } + } + if !errors.Is(gotErr, errPoolExhausted) { + t.Fatalf("want errPoolExhausted from the transit pool, got %v", gotErr) + } + if got := (*magicPool).inUseCount(); got != int64(reserved) { + t.Errorf("magic pool has %d addresses in use, want %d (one per successful reservation)", got, reserved) + } + if got := (*transitPool).inUseCount(); got != int64(reserved) { + t.Errorf("transit pool has %d addresses in use, want %d (one per successful reservation)", got, reserved) + } + }) + } +} + func TestTransitIPConnMapping(t *testing.T) { conn25 := newConn25(t.Logf)