2020-02-05 22:16:58 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-06-07 19:50:12 +01:00
|
|
|
//go:build !js
|
|
|
|
// +build !js
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
package wgengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"runtime/pprof"
|
2020-02-11 23:21:24 +00:00
|
|
|
"strings"
|
2020-02-05 22:16:58 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2020-03-26 05:57:46 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-04-02 08:34:32 +01:00
|
|
|
"tailscale.com/net/dns"
|
2021-11-23 05:45:34 +00:00
|
|
|
"tailscale.com/net/dns/resolver"
|
2022-07-25 04:08:42 +01:00
|
|
|
"tailscale.com/net/netaddr"
|
2021-03-29 23:17:05 +01:00
|
|
|
"tailscale.com/net/tstun"
|
2020-05-17 17:51:38 +01:00
|
|
|
"tailscale.com/tailcfg"
|
2021-11-02 21:41:56 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-02-05 23:44:46 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2020-02-05 22:16:58 +00:00
|
|
|
"tailscale.com/wgengine/filter"
|
2021-03-29 23:17:05 +01:00
|
|
|
"tailscale.com/wgengine/magicsock"
|
2021-03-01 20:56:03 +00:00
|
|
|
"tailscale.com/wgengine/monitor"
|
2020-05-11 22:02:12 +01:00
|
|
|
"tailscale.com/wgengine/router"
|
2021-01-29 20:16:36 +00:00
|
|
|
"tailscale.com/wgengine/wgcfg"
|
2020-02-05 22:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewWatchdog wraps an Engine and makes sure that all methods complete
|
|
|
|
// within a reasonable amount of time.
|
|
|
|
//
|
|
|
|
// If they do not, the watchdog crashes the process.
|
|
|
|
func NewWatchdog(e Engine) Engine {
|
2022-01-24 18:52:57 +00:00
|
|
|
if envknob.Bool("TS_DEBUG_DISABLE_WATCHDOG") {
|
2020-07-25 20:59:53 +01:00
|
|
|
return e
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
return &watchdogEngine{
|
|
|
|
wrap: e,
|
|
|
|
logf: log.Printf,
|
|
|
|
fatalf: log.Fatalf,
|
|
|
|
maxWait: 45 * time.Second,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type watchdogEngine struct {
|
|
|
|
wrap Engine
|
2022-03-16 23:27:57 +00:00
|
|
|
logf func(format string, args ...any)
|
|
|
|
fatalf func(format string, args ...any)
|
2020-02-05 22:16:58 +00:00
|
|
|
maxWait time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *watchdogEngine) watchdogErr(name string, fn func() error) error {
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- fn()
|
|
|
|
}()
|
|
|
|
t := time.NewTimer(e.maxWait)
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
t.Stop()
|
|
|
|
return err
|
|
|
|
case <-t.C:
|
2020-02-11 23:21:24 +00:00
|
|
|
buf := new(strings.Builder)
|
2020-02-05 22:16:58 +00:00
|
|
|
pprof.Lookup("goroutine").WriteTo(buf, 1)
|
|
|
|
e.logf("wgengine watchdog stacks:\n%s", buf.String())
|
|
|
|
e.fatalf("wgengine: watchdog timeout on %s", name)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *watchdogEngine) watchdog(name string, fn func()) {
|
|
|
|
e.watchdogErr(name, func() error {
|
|
|
|
fn()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-22 21:13:59 +01:00
|
|
|
func (e *watchdogEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, dnsCfg *dns.Config, debug *tailcfg.Debug) error {
|
|
|
|
return e.watchdogErr("Reconfig", func() error { return e.wrap.Reconfig(cfg, routerCfg, dnsCfg, debug) })
|
2020-02-05 22:16:58 +00:00
|
|
|
}
|
2021-03-01 20:56:03 +00:00
|
|
|
func (e *watchdogEngine) GetLinkMonitor() *monitor.Mon {
|
|
|
|
return e.wrap.GetLinkMonitor()
|
|
|
|
}
|
2020-03-25 07:47:55 +00:00
|
|
|
func (e *watchdogEngine) GetFilter() *filter.Filter {
|
2021-03-01 20:56:03 +00:00
|
|
|
return e.wrap.GetFilter()
|
2020-03-25 07:47:55 +00:00
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
func (e *watchdogEngine) SetFilter(filt *filter.Filter) {
|
|
|
|
e.watchdog("SetFilter", func() { e.wrap.SetFilter(filt) })
|
|
|
|
}
|
|
|
|
func (e *watchdogEngine) SetStatusCallback(cb StatusCallback) {
|
|
|
|
e.watchdog("SetStatusCallback", func() { e.wrap.SetStatusCallback(cb) })
|
|
|
|
}
|
2020-03-26 05:57:46 +00:00
|
|
|
func (e *watchdogEngine) UpdateStatus(sb *ipnstate.StatusBuilder) {
|
|
|
|
e.watchdog("UpdateStatus", func() { e.wrap.UpdateStatus(sb) })
|
|
|
|
}
|
2020-03-04 06:21:56 +00:00
|
|
|
func (e *watchdogEngine) SetNetInfoCallback(cb NetInfoCallback) {
|
|
|
|
e.watchdog("SetNetInfoCallback", func() { e.wrap.SetNetInfoCallback(cb) })
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
func (e *watchdogEngine) RequestStatus() {
|
|
|
|
e.watchdog("RequestStatus", func() { e.wrap.RequestStatus() })
|
|
|
|
}
|
2021-03-04 04:58:09 +00:00
|
|
|
func (e *watchdogEngine) LinkChange(isExpensive bool) {
|
|
|
|
e.watchdog("LinkChange", func() { e.wrap.LinkChange(isExpensive) })
|
|
|
|
}
|
2020-05-17 17:51:38 +01:00
|
|
|
func (e *watchdogEngine) SetDERPMap(m *tailcfg.DERPMap) {
|
|
|
|
e.watchdog("SetDERPMap", func() { e.wrap.SetDERPMap(m) })
|
2020-03-04 20:21:40 +00:00
|
|
|
}
|
2021-02-05 23:44:46 +00:00
|
|
|
func (e *watchdogEngine) SetNetworkMap(nm *netmap.NetworkMap) {
|
2020-06-25 19:04:52 +01:00
|
|
|
e.watchdog("SetNetworkMap", func() { e.wrap.SetNetworkMap(nm) })
|
|
|
|
}
|
2021-01-15 14:16:28 +00:00
|
|
|
func (e *watchdogEngine) AddNetworkMapCallback(callback NetworkMapCallback) func() {
|
|
|
|
var fn func()
|
|
|
|
e.watchdog("AddNetworkMapCallback", func() { fn = e.wrap.AddNetworkMapCallback(callback) })
|
|
|
|
return func() { e.watchdog("RemoveNetworkMapCallback", fn) }
|
|
|
|
}
|
2021-11-02 21:41:56 +00:00
|
|
|
func (e *watchdogEngine) DiscoPublicKey() (k key.DiscoPublic) {
|
2020-07-06 20:10:39 +01:00
|
|
|
e.watchdog("DiscoPublicKey", func() { k = e.wrap.DiscoPublicKey() })
|
|
|
|
return k
|
2020-06-19 20:06:49 +01:00
|
|
|
}
|
2022-04-22 02:49:01 +01:00
|
|
|
func (e *watchdogEngine) Ping(ip netaddr.IP, pingType tailcfg.PingType, cb func(*ipnstate.PingResult)) {
|
|
|
|
e.watchdog("Ping", func() { e.wrap.Ping(ip, pingType, cb) })
|
2020-08-09 22:49:42 +01:00
|
|
|
}
|
2021-03-15 21:59:35 +00:00
|
|
|
func (e *watchdogEngine) RegisterIPPortIdentity(ipp netaddr.IPPort, tsIP netaddr.IP) {
|
|
|
|
e.watchdog("RegisterIPPortIdentity", func() { e.wrap.RegisterIPPortIdentity(ipp, tsIP) })
|
|
|
|
}
|
|
|
|
func (e *watchdogEngine) UnregisterIPPortIdentity(ipp netaddr.IPPort) {
|
|
|
|
e.watchdog("UnregisterIPPortIdentity", func() { e.wrap.UnregisterIPPortIdentity(ipp) })
|
|
|
|
}
|
|
|
|
func (e *watchdogEngine) WhoIsIPPort(ipp netaddr.IPPort) (tsIP netaddr.IP, ok bool) {
|
|
|
|
e.watchdog("UnregisterIPPortIdentity", func() { tsIP, ok = e.wrap.WhoIsIPPort(ipp) })
|
|
|
|
return tsIP, ok
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
func (e *watchdogEngine) Close() {
|
|
|
|
e.watchdog("Close", e.wrap.Close)
|
|
|
|
}
|
2022-04-14 22:55:23 +01:00
|
|
|
func (e *watchdogEngine) GetInternals() (tw *tstun.Wrapper, c *magicsock.Conn, d *dns.Manager, ok bool) {
|
2021-03-29 23:17:05 +01:00
|
|
|
if ig, ok := e.wrap.(InternalsGetter); ok {
|
|
|
|
return ig.GetInternals()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-11-23 05:45:34 +00:00
|
|
|
func (e *watchdogEngine) GetResolver() (r *resolver.Resolver, ok bool) {
|
|
|
|
if re, ok := e.wrap.(ResolvingEngine); ok {
|
|
|
|
return re.GetResolver()
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-11-30 18:30:44 +00:00
|
|
|
func (e *watchdogEngine) PeerForIP(ip netaddr.IP) (ret PeerForIP, ok bool) {
|
|
|
|
e.watchdog("PeerForIP", func() { ret, ok = e.wrap.PeerForIP(ip) })
|
|
|
|
return ret, ok
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:16:58 +00:00
|
|
|
func (e *watchdogEngine) Wait() {
|
|
|
|
e.wrap.Wait()
|
|
|
|
}
|