mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 13:18:53 +00:00
net/netns, net/dns/resolver, etc: make netmon required in most places
The goal is to move more network state accessors to netmon.Monitor where they can be cheaper/cached. But first (this change and others) we need to make sure the one netmon.Monitor is plumbed everywhere. Some notable bits: * tsdial.NewDialer is added, taking a now-required netmon * because a tsdial.Dialer always has a netmon, anything taking both a Dialer and a NetMon is now redundant; take only the Dialer and get the NetMon from that if/when needed. * netmon.NewStatic is added, primarily for tests Updates tailscale/corp#10910 Updates tailscale/corp#18960 Updates #7967 Updates #3299 Change-Id: I877f9cb87618c4eb037cee098241d18da9c01691 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
4f73a26ea5
commit
3672f29a4e
@@ -26,13 +26,27 @@ import (
|
||||
"tailscale.com/types/netmap"
|
||||
"tailscale.com/util/clientmetric"
|
||||
"tailscale.com/util/mak"
|
||||
"tailscale.com/util/testenv"
|
||||
"tailscale.com/version"
|
||||
)
|
||||
|
||||
// NewDialer returns a new Dialer that can dial out of tailscaled.
|
||||
// Its exported fields should be set before use, if any.
|
||||
func NewDialer(netMon *netmon.Monitor) *Dialer {
|
||||
if netMon == nil {
|
||||
panic("NewDialer: netMon is nil")
|
||||
}
|
||||
d := &Dialer{}
|
||||
d.SetNetMon(netMon)
|
||||
return d
|
||||
}
|
||||
|
||||
// Dialer dials out of tailscaled, while taking care of details while
|
||||
// handling the dozens of edge cases depending on the server mode
|
||||
// (TUN, netstack), the OS network sandboxing style (macOS/iOS
|
||||
// Extension, none), user-selected route acceptance prefs, etc.
|
||||
//
|
||||
// Before use, SetNetMon should be called with a netmon.Monitor.
|
||||
type Dialer struct {
|
||||
Logf logger.Logf
|
||||
// UseNetstackForIP if non-nil is whether NetstackDialTCP (if
|
||||
@@ -130,9 +144,14 @@ func (d *Dialer) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetNetMon sets d's network monitor to netMon.
|
||||
// It is a no-op to call SetNetMon with the same netMon as the current one.
|
||||
func (d *Dialer) SetNetMon(netMon *netmon.Monitor) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if d.netMon == netMon {
|
||||
return
|
||||
}
|
||||
if d.netMonUnregister != nil {
|
||||
go d.netMonUnregister()
|
||||
d.netMonUnregister = nil
|
||||
@@ -141,6 +160,14 @@ func (d *Dialer) SetNetMon(netMon *netmon.Monitor) {
|
||||
d.netMonUnregister = d.netMon.RegisterChangeCallback(d.linkChanged)
|
||||
}
|
||||
|
||||
// NetMon returns the Dialer's network monitor.
|
||||
// It returns nil if SetNetMon has not been called.
|
||||
func (d *Dialer) NetMon() *netmon.Monitor {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
return d.netMon
|
||||
}
|
||||
|
||||
var (
|
||||
metricLinkChangeConnClosed = clientmetric.NewCounter("tsdial_linkchange_closes")
|
||||
metricChangeDeltaNoDefaultRoute = clientmetric.NewCounter("tsdial_changedelta_no_default_route")
|
||||
@@ -314,6 +341,13 @@ func (d *Dialer) logf(format string, args ...any) {
|
||||
// Control and (in the future, as of 2022-04-27) DERPs..
|
||||
func (d *Dialer) SystemDial(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
d.mu.Lock()
|
||||
if d.netMon == nil {
|
||||
d.mu.Unlock()
|
||||
if testenv.InTest() {
|
||||
panic("SystemDial requires a netmon.Monitor; call SetNetMon first")
|
||||
}
|
||||
return nil, errors.New("SystemDial requires a netmon.Monitor; call SetNetMon first")
|
||||
}
|
||||
closed := d.closed
|
||||
d.mu.Unlock()
|
||||
if closed {
|
||||
|
Reference in New Issue
Block a user