wgengine/magicsock: use derp-region-as-magic-AddrPort hack in fewer places

And fix up a bogus comment and flesh out some other comments.

Updates #cleanup

Change-Id: Ia60a1c04b0f5e44e8d9587914af819df8e8f442a
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-07-06 19:20:15 -07:00 committed by Brad Fitzpatrick
parent e181f12a7b
commit 9df107f4f0
2 changed files with 19 additions and 22 deletions

View File

@ -258,14 +258,14 @@ func (c *Conn) startDerpHomeConnectLocked() {
} }
// goDerpConnect starts a goroutine to start connecting to the given // goDerpConnect starts a goroutine to start connecting to the given
// DERP node. // DERP region ID.
// //
// c.mu may be held, but does not need to be. // c.mu may be held, but does not need to be.
func (c *Conn) goDerpConnect(node int) { func (c *Conn) goDerpConnect(regionID int) {
if node == 0 { if regionID == 0 {
return return
} }
go c.derpWriteChanOfAddr(netip.AddrPortFrom(tailcfg.DerpMagicIPAddr, uint16(node)), key.NodePublic{}) go c.derpWriteChanForRegion(regionID, key.NodePublic{})
} }
var ( var (
@ -322,18 +322,15 @@ func bufferedDerpWritesBeforeDrop() int {
return bufferedDerpWrites return bufferedDerpWrites
} }
// derpWriteChanOfAddr returns a DERP client for fake UDP addresses that // derpWriteChanForRegion returns a channel to which to send DERP packet write
// represent DERP servers, creating them as necessary. For real UDP // requests. It creates a new DERP connection to regionID if necessary.
// addresses, it returns nil.
// //
// If peer is non-zero, it can be used to find an active reverse // If peer is non-zero, it can be used to find an active reverse path, without
// path, without using addr. // using regionID.
func (c *Conn) derpWriteChanOfAddr(addr netip.AddrPort, peer key.NodePublic) chan<- derpWriteRequest { //
if addr.Addr() != tailcfg.DerpMagicIPAddr { // It returns nil if the network is down, the Conn is closed, or the regionID is
return nil // not known.
} func (c *Conn) derpWriteChanForRegion(regionID int, peer key.NodePublic) chan<- derpWriteRequest {
regionID := int(addr.Port())
if c.networkDown() { if c.networkDown() {
return nil return nil
} }
@ -347,7 +344,7 @@ func (c *Conn) derpWriteChanOfAddr(addr netip.AddrPort, peer key.NodePublic) cha
return nil return nil
} }
if c.privateKey.IsZero() { if c.privateKey.IsZero() {
c.logf("magicsock: DERP lookup of %v with no private key; ignoring", addr) c.logf("magicsock: DERP lookup of region %v with no private key; ignoring", regionID)
return nil return nil
} }
@ -453,7 +450,7 @@ func (c *Conn) derpWriteChanOfAddr(addr netip.AddrPort, peer key.NodePublic) cha
}() }()
} }
go c.runDerpReader(ctx, addr, dc, wg, startGate) go c.runDerpReader(ctx, regionID, dc, wg, startGate)
go c.runDerpWriter(ctx, dc, ch, wg, startGate) go c.runDerpWriter(ctx, dc, ch, wg, startGate)
go c.derpActiveFunc() go c.derpActiveFunc()
@ -516,7 +513,7 @@ type derpReadResult struct {
// runDerpReader runs in a goroutine for the life of a DERP // runDerpReader runs in a goroutine for the life of a DERP
// connection, handling received packets. // connection, handling received packets.
func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netip.AddrPort, dc *derphttp.Client, wg *syncs.WaitGroupChan, startGate <-chan struct{}) { func (c *Conn) runDerpReader(ctx context.Context, regionID int, dc *derphttp.Client, wg *syncs.WaitGroupChan, startGate <-chan struct{}) {
defer wg.Decr() defer wg.Decr()
defer dc.Close() defer dc.Close()
@ -527,7 +524,6 @@ func (c *Conn) runDerpReader(ctx context.Context, derpFakeAddr netip.AddrPort, d
} }
didCopy := make(chan struct{}, 1) didCopy := make(chan struct{}, 1)
regionID := int(derpFakeAddr.Port())
res := derpReadResult{regionID: regionID} res := derpReadResult{regionID: regionID}
var pkt derp.ReceivedPacket var pkt derp.ReceivedPacket
res.copyBuf = func(dst []byte) int { res.copyBuf = func(dst []byte) int {

View File

@ -7,6 +7,7 @@
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -1154,7 +1155,8 @@ func (c *Conn) sendAddr(addr netip.AddrPort, pubKey key.NodePublic, b []byte) (s
return c.sendUDP(addr, b) return c.sendUDP(addr, b)
} }
ch := c.derpWriteChanOfAddr(addr, pubKey) regionID := int(addr.Port())
ch := c.derpWriteChanForRegion(regionID, pubKey)
if ch == nil { if ch == nil {
metricSendDERPErrorChan.Add(1) metricSendDERPErrorChan.Add(1)
return false, nil return false, nil
@ -1165,8 +1167,7 @@ func (c *Conn) sendAddr(addr netip.AddrPort, pubKey key.NodePublic, b []byte) (s
// to derpWriteRequest and waited for derphttp.Client.Send to // to derpWriteRequest and waited for derphttp.Client.Send to
// complete, but that's too slow while holding wireguard-go // complete, but that's too slow while holding wireguard-go
// internal locks. // internal locks.
pkt := make([]byte, len(b)) pkt := bytes.Clone(b)
copy(pkt, b)
select { select {
case <-c.donec: case <-c.donec: