mirror of
https://github.com/tailscale/tailscale.git
synced 2026-07-15 10:03:09 -04:00
Engine.PeerForIP was pure delegation to the callback that LocalBackend installs via SetPeerForIPFunc, so external callers going through the engine were taking a pointless round trip: LocalBackend called b.e.PeerForIP, which called right back into LocalBackend, and the netstack UseNetstackForIP hooks in tsnet and tailscaled did the same dance one layer removed. Export LocalBackend.PeerForIP and make those callers use it directly. The engine-internal cold paths (Ping, TSMP disco advertisements, pendopen diagnostics) still need the lookup and have no netmap of their own, so SetPeerForIPFunc stays on the interface, but the lookup method itself is now unexported and gone from the Engine interface. In tailscaled the UseNetstackForIP hook moves from netstack setup to just after the LocalBackend is created, since the backend doesn't exist yet when netstack is wired up. Updates #12542 Change-Id: Ib1e1a4fa5c84ee0dcb9ce5d1910047f2bab9453c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build !ts_omit_netstack
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"expvar"
|
|
"net"
|
|
"net/netip"
|
|
|
|
"tailscale.com/tsd"
|
|
"tailscale.com/types/logger"
|
|
"tailscale.com/wgengine/netstack"
|
|
)
|
|
|
|
func init() {
|
|
hookNewNetstack.Set(newNetstack)
|
|
}
|
|
|
|
func newNetstack(logf logger.Logf, sys *tsd.System, onlyNetstack bool) (tsd.NetstackImpl, error) {
|
|
ns, err := netstack.Create(logf,
|
|
sys.Tun.Get(),
|
|
sys.Engine.Get(),
|
|
sys.MagicSock.Get(),
|
|
sys.Dialer.Get(),
|
|
sys.DNSManager.Get(),
|
|
sys.ProxyMapper(),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Only register debug info if we have a debug mux
|
|
if debugMux != nil {
|
|
expvar.Publish("netstack", ns.ExpVar())
|
|
}
|
|
|
|
sys.Set(ns)
|
|
ns.ProcessLocalIPs = onlyNetstack
|
|
ns.ProcessSubnets = onlyNetstack || handleSubnetsInNetstack()
|
|
|
|
dialer := sys.Dialer.Get() // must be set by caller already
|
|
|
|
if onlyNetstack {
|
|
dialer.NetstackDialTCP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) {
|
|
// Note: don't just return ns.DialContextTCP or we'll return
|
|
// *gonet.TCPConn(nil) instead of a nil interface which trips up
|
|
// callers.
|
|
tcpConn, err := ns.DialContextTCP(ctx, dst)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return tcpConn, nil
|
|
}
|
|
dialer.NetstackDialUDP = func(ctx context.Context, dst netip.AddrPort) (net.Conn, error) {
|
|
// Note: don't just return ns.DialContextUDP or we'll return
|
|
// *gonet.UDPConn(nil) instead of a nil interface which trips up
|
|
// callers.
|
|
udpConn, err := ns.DialContextUDP(ctx, dst)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return udpConn, nil
|
|
}
|
|
}
|
|
|
|
return ns, nil
|
|
}
|