mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-28 12:36:26 -08:00
feature/conn25: return pool addresses when reserveAddresses fails
Addresses handed out by ippool.next() are marked in use, and the only path back to the pool is the popExpired loop in reserveAddresses, which can only see an addrs once it's in the client's assignments. So a reservation that fails after taking an address leaks it from the pool for the lifetime of the process. The leak is the result of interaction between the magic and transit pools. First an address from magic pool is picked. Then another address from the transit pool is picked. If an error happens during picking an address from transit pool, the address from the magic pool needs to be returned, otherwise that address is leaked, as the address in not in the client assignments and won't be released. Fixes https://github.com/tailscale/tailscale/pull/22019 Change-Id: I3d98282762576cc57c135761859902a1d0a63c31 Signed-off-by: Mazdak Nasab <[email protected]>
This commit is contained in:
1 parent
7ad13349f1
commit
3ad81d1908
2 files changed
+95
-18
No files matched your search
+18
-18
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user