mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-27 18:57:35 +00:00
wgengine/magicsock: remove final alloc from ReceiveFrom
And now that we don't have to play escape analysis and inlining games, simplify the code. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
parent
463728a885
commit
1b57b0380d
@ -257,6 +257,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
|
|||||||
tailscale.com/util/groupmember from tailscale.com/ipn/ipnserver
|
tailscale.com/util/groupmember from tailscale.com/ipn/ipnserver
|
||||||
tailscale.com/util/lineread from tailscale.com/hostinfo+
|
tailscale.com/util/lineread from tailscale.com/hostinfo+
|
||||||
tailscale.com/util/multierr from tailscale.com/cmd/tailscaled+
|
tailscale.com/util/multierr from tailscale.com/cmd/tailscaled+
|
||||||
|
tailscale.com/util/netconv from tailscale.com/wgengine/magicsock
|
||||||
tailscale.com/util/osshare from tailscale.com/cmd/tailscaled+
|
tailscale.com/util/osshare from tailscale.com/cmd/tailscaled+
|
||||||
tailscale.com/util/pidowner from tailscale.com/ipn/ipnserver
|
tailscale.com/util/pidowner from tailscale.com/ipn/ipnserver
|
||||||
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
tailscale.com/util/racebuild from tailscale.com/logpolicy
|
||||||
@ -382,7 +383,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
|
|||||||
net/http/httputil from tailscale.com/cmd/tailscaled+
|
net/http/httputil from tailscale.com/cmd/tailscaled+
|
||||||
net/http/internal from net/http+
|
net/http/internal from net/http+
|
||||||
net/http/pprof from tailscale.com/cmd/tailscaled+
|
net/http/pprof from tailscale.com/cmd/tailscaled+
|
||||||
net/netip from net
|
net/netip from net+
|
||||||
net/textproto from golang.org/x/net/http/httpguts+
|
net/textproto from golang.org/x/net/http/httpguts+
|
||||||
net/url from crypto/x509+
|
net/url from crypto/x509+
|
||||||
os from crypto/rand+
|
os from crypto/rand+
|
||||||
|
@ -17,6 +17,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
@ -53,6 +54,7 @@ import (
|
|||||||
"tailscale.com/types/netmap"
|
"tailscale.com/types/netmap"
|
||||||
"tailscale.com/types/nettype"
|
"tailscale.com/types/nettype"
|
||||||
"tailscale.com/util/clientmetric"
|
"tailscale.com/util/clientmetric"
|
||||||
|
"tailscale.com/util/netconv"
|
||||||
"tailscale.com/util/uniq"
|
"tailscale.com/util/uniq"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
"tailscale.com/wgengine/monitor"
|
"tailscale.com/wgengine/monitor"
|
||||||
@ -3025,36 +3027,31 @@ func (c *RebindingUDPConn) ReadFromNetaddr(b []byte) (n int, ipp netaddr.IPPort,
|
|||||||
pconn := c.currentConn()
|
pconn := c.currentConn()
|
||||||
|
|
||||||
// Optimization: Treat *net.UDPConn specially.
|
// Optimization: Treat *net.UDPConn specially.
|
||||||
// ReadFromUDP gets partially inlined, avoiding allocating a *net.UDPAddr,
|
// This lets us avoid allocations by calling ReadFromUDPAddrPort.
|
||||||
// as long as pAddr itself doesn't escape.
|
|
||||||
// The non-*net.UDPConn case works, but it allocates.
|
// The non-*net.UDPConn case works, but it allocates.
|
||||||
var pAddr *net.UDPAddr
|
|
||||||
if udpConn, ok := pconn.(*net.UDPConn); ok {
|
if udpConn, ok := pconn.(*net.UDPConn); ok {
|
||||||
n, pAddr, err = udpConn.ReadFromUDP(b)
|
var ap netip.AddrPort
|
||||||
|
n, ap, err = udpConn.ReadFromUDPAddrPort(b)
|
||||||
|
ipp = netconv.AsIPPort(ap)
|
||||||
} else {
|
} else {
|
||||||
var addr net.Addr
|
var addr net.Addr
|
||||||
n, addr, err = pconn.ReadFrom(b)
|
n, addr, err = pconn.ReadFrom(b)
|
||||||
if addr != nil {
|
pAddr, ok := addr.(*net.UDPAddr)
|
||||||
pAddr, ok = addr.(*net.UDPAddr)
|
if addr != nil && !ok {
|
||||||
if !ok {
|
|
||||||
return 0, netaddr.IPPort{}, fmt.Errorf("RebindingUDPConn.ReadFromNetaddr: underlying connection returned address of type %T, want *netaddr.UDPAddr", addr)
|
return 0, netaddr.IPPort{}, fmt.Errorf("RebindingUDPConn.ReadFromNetaddr: underlying connection returned address of type %T, want *netaddr.UDPAddr", addr)
|
||||||
}
|
}
|
||||||
}
|
if pAddr != nil {
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
if pconn != c.currentConn() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Convert pAddr to a netaddr.IPPort.
|
|
||||||
// This prevents pAddr from escaping.
|
|
||||||
var ok bool
|
|
||||||
ipp, ok = netaddr.FromStdAddr(pAddr.IP, pAddr.Port, pAddr.Zone)
|
ipp, ok = netaddr.FromStdAddr(pAddr.IP, pAddr.Port, pAddr.Zone)
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, netaddr.IPPort{}, errors.New("netaddr.FromStdAddr failed")
|
return 0, netaddr.IPPort{}, errors.New("netaddr.FromStdAddr failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil && pconn != c.currentConn() {
|
||||||
|
// The connection changed underfoot. Try again.
|
||||||
|
continue
|
||||||
|
}
|
||||||
return n, ipp, err
|
return n, ipp, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1345,14 +1345,18 @@ func TestReceiveFromAllocs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
// Go 1.16 and before: allow 3 allocs.
|
// Go 1.16 and before: allow 3 allocs.
|
||||||
// Go 1.17: allow 2 allocs.
|
// Go 1.17: allow 2 allocs.
|
||||||
// Go Tailscale fork, Go 1.18+: allow 1 alloc.
|
// Go 1.17, Tailscale fork: allow 1 alloc.
|
||||||
|
// Go 1.18+: allow 0 allocs.
|
||||||
|
// Go 2.0: allow -1 allocs (projected).
|
||||||
major, ts := goMajorVersion(runtime.Version())
|
major, ts := goMajorVersion(runtime.Version())
|
||||||
maxAllocs := 3
|
maxAllocs := 3
|
||||||
switch {
|
switch {
|
||||||
case major == 17:
|
case major == 17 && !ts:
|
||||||
maxAllocs = 2
|
maxAllocs = 2
|
||||||
case major >= 18, ts:
|
case major == 17 && ts:
|
||||||
maxAllocs = 1
|
maxAllocs = 1
|
||||||
|
case major >= 18:
|
||||||
|
maxAllocs = 0
|
||||||
}
|
}
|
||||||
t.Logf("allowing %d allocs for Go version %q", maxAllocs, runtime.Version())
|
t.Logf("allowing %d allocs for Go version %q", maxAllocs, runtime.Version())
|
||||||
roundTrip := setUpReceiveFrom(t)
|
roundTrip := setUpReceiveFrom(t)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user