mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 09:11:18 -08:00
feature/conn25: name lock-requiring methods Locked (#22532)
Two methods read or write state guarded by their receiver's mutex without taking it, relying on every caller to hold it, but were not named to say so. enqueueAddressAssignment documented the requirement in prose only; lookupAddrBySrcIPAndTransitIP had no doc comment at all. Rename both with the Locked suffix the package already uses for drainAddrsLocked, transitIPRoutableLocked and removeFlowLocked, and give the connector lookup a doc comment stating the requirement. Fixes https://github.com/tailscale/corp/issues/38924 Change-Id: Ib8bcaf5809ae6f982a504ae554ad1304dbb53018 Signed-off-by: Mazdak Nasab <[email protected]>
This commit is contained in:
1 parent
05abd5d73e
commit
4f7439b415
5 files changed
+17
-14
No files matched your search
@@ -157,8 +157,8 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip
|
||||
if existing, ok := c.assignments.lookupByDomainDst(domain, dst); ok {
|
||||
// We have the domain/dst, but it's possible this came from another connector
|
||||
// and the original connector is dead, in which case we currently don't
|
||||
// call enqueueAddressAssignment and register the TransitIP with the other
|
||||
// connector. And we don't install the new connector's node key in WireGuard.
|
||||
// call enqueueAddressAssignmentLocked and register the TransitIP with the
|
||||
// other connector. And we don't install the new connector's node key in WireGuard.
|
||||
//
|
||||
// The correct action would be to allocate a new TransitIP associated
|
||||
// with that new connector, and then register the TransitIP address, and
|
||||
@@ -241,7 +241,7 @@ func (c *client) reserveAddresses(appName string, domain dnsname.FQDN, dst netip
|
||||
transitPool.returnAddr(tip)
|
||||
return nil, err
|
||||
}
|
||||
if err := c.enqueueAddressAssignment(as); err != nil {
|
||||
if err := c.enqueueAddressAssignmentLocked(as); err != nil {
|
||||
c.assignments.remove(as)
|
||||
magicPool.returnAddr(mip)
|
||||
transitPool.returnAddr(tip)
|
||||
@@ -269,9 +269,9 @@ var errQueueFull = errors.New("queue full")
|
||||
// no longer accepts address assignments.
|
||||
var errClientShutdown = errors.New("conn25 client shut down")
|
||||
|
||||
// enqueueAddressAssignment queues addrs to be registered with its connector by
|
||||
// sendLoop. c.mu must be held.
|
||||
func (c *client) enqueueAddressAssignment(addrs *addrs) error {
|
||||
// enqueueAddressAssignmentLocked queues addrs to be registered with its
|
||||
// connector by sendLoop. c.mu must be held.
|
||||
func (c *client) enqueueAddressAssignmentLocked(addrs *addrs) error {
|
||||
if c.closed {
|
||||
return errClientShutdown
|
||||
}
|
||||
@@ -489,7 +489,7 @@ func (c *client) resendTransitIPMapping(transitIP netip.Addr) {
|
||||
// We have no mappings for this transit IP, so nothing to resend.
|
||||
return
|
||||
}
|
||||
err := c.enqueueAddressAssignment(mapping)
|
||||
err := c.enqueueAddressAssignmentLocked(mapping)
|
||||
if err != nil {
|
||||
c.logf("error enqueueing address assignment for resend: %v", err)
|
||||
}
|
||||
|
||||
@@ -411,8 +411,8 @@ func (c *client) currentGeneration() uint64 {
|
||||
return c.generation
|
||||
}
|
||||
|
||||
// enqueueAddressAssignmentForTest is [client.enqueueAddressAssignment] with the
|
||||
// lock it expects callers to hold, plus the generation stamp that
|
||||
// enqueueAddressAssignmentForTest is [client.enqueueAddressAssignmentLocked]
|
||||
// with the lock it expects callers to hold, plus the generation stamp that
|
||||
// [client.reserveAddresses] would have applied. The production callers all
|
||||
// enqueue from inside a c.mu critical section; tests reach for it on its own,
|
||||
// and build the [addrs] by hand rather than reserving one. Without the stamp
|
||||
@@ -422,7 +422,7 @@ func (c *client) enqueueAddressAssignmentForTest(as *addrs) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
as.generation = c.generation
|
||||
return c.enqueueAddressAssignment(as)
|
||||
return c.enqueueAddressAssignmentLocked(as)
|
||||
}
|
||||
|
||||
func TestTransitIPConnMapping(t *testing.T) {
|
||||
|
||||
@@ -643,7 +643,7 @@ func TestHandleConnectorTransitIPRequest(t *testing.T) {
|
||||
i, j, len(wantLookup))
|
||||
}
|
||||
pip, tip, wantDip := wantLookup[0], wantLookup[1], wantLookup[2]
|
||||
gotDip, _ := c.connector.lookupAddrBySrcIPAndTransitIP(pip, tip)
|
||||
gotDip, _ := c.connector.lookupAddrBySrcIPAndTransitIPLocked(pip, tip)
|
||||
if gotDip != wantDip {
|
||||
t.Errorf("wrong result on lookup[%d][%d] ([%v], [%v]): got [%v] expected [%v]",
|
||||
i, j, pip, tip, gotDip, wantDip)
|
||||
|
||||
@@ -116,7 +116,7 @@ type transitIPExpiryEntry struct {
|
||||
func (c *connector) realIPForTransitIPConnection(srcIP netip.Addr, transitIP netip.Addr) (netip.Addr, bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.lookupAddrBySrcIPAndTransitIP(srcIP, transitIP)
|
||||
return c.lookupAddrBySrcIPAndTransitIPLocked(srcIP, transitIP)
|
||||
}
|
||||
|
||||
// transitIPInPool reports whether tip is within the transit IP pool of its
|
||||
@@ -164,7 +164,10 @@ func (c *connector) packetFilterAllow(p packet.Parsed) (bool, string) {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func (c *connector) lookupAddrBySrcIPAndTransitIP(srcIP, transitIP netip.Addr) (netip.Addr, bool) {
|
||||
// lookupAddrBySrcIPAndTransitIPLocked returns the real destination IP that the
|
||||
// peer at srcIP registered for transitIP, or (zero, false) if it registered
|
||||
// none. c.mu must be held.
|
||||
func (c *connector) lookupAddrBySrcIPAndTransitIPLocked(srcIP, transitIP netip.Addr) (netip.Addr, bool) {
|
||||
m, ok := c.transitIPs[srcIP]
|
||||
if !ok || m == nil {
|
||||
return netip.Addr{}, false
|
||||
|
||||
@@ -272,7 +272,7 @@ func TestConnectorDisallowedDestinations(t *testing.T) {
|
||||
if tt.wantCode == OK {
|
||||
wantDst = unmapped
|
||||
}
|
||||
if got, _ := c.connector.lookupAddrBySrcIPAndTransitIP(peerIP, transitIP); got != wantDst {
|
||||
if got, _ := c.connector.lookupAddrBySrcIPAndTransitIPLocked(peerIP, transitIP); got != wantDst {
|
||||
t.Errorf("lookup(%v, %v) = %v, want %v", peerIP, transitIP, got, wantDst)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user