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.
|
|
|
|
|
|
|
|
package wgengine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-07-25 20:59:53 +01:00
|
|
|
"os"
|
2020-02-05 22:16:58 +00:00
|
|
|
"runtime/pprof"
|
2020-07-25 20:59:53 +01:00
|
|
|
"strconv"
|
2020-02-11 23:21:24 +00:00
|
|
|
"strings"
|
2020-02-05 22:16:58 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-09 22:49:42 +01:00
|
|
|
"inet.af/netaddr"
|
2020-03-26 05:57:46 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-03-26 00:41:51 +00:00
|
|
|
"tailscale.com/net/dns"
|
2020-05-17 17:51:38 +01:00
|
|
|
"tailscale.com/tailcfg"
|
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-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 {
|
2020-07-25 20:59:53 +01:00
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("TS_DEBUG_DISABLE_WATCHDOG")); v {
|
|
|
|
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
|
|
|
|
logf func(format string, args ...interface{})
|
|
|
|
fatalf func(format string, args ...interface{})
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-12 08:08:52 +01:00
|
|
|
func (e *watchdogEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config) error {
|
2020-05-11 22:02:12 +01:00
|
|
|
return e.watchdogErr("Reconfig", func() error { return e.wrap.Reconfig(cfg, routerCfg) })
|
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) })
|
|
|
|
}
|
2021-03-25 21:50:21 +00:00
|
|
|
func (e *watchdogEngine) SetDNSMap(dm *dns.Map) {
|
2020-06-08 23:19:26 +01:00
|
|
|
e.watchdog("SetDNSMap", func() { e.wrap.SetDNSMap(dm) })
|
|
|
|
}
|
2020-02-05 22:16:58 +00:00
|
|
|
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) }
|
|
|
|
}
|
2020-07-06 20:10:39 +01:00
|
|
|
func (e *watchdogEngine) DiscoPublicKey() (k tailcfg.DiscoKey) {
|
|
|
|
e.watchdog("DiscoPublicKey", func() { k = e.wrap.DiscoPublicKey() })
|
|
|
|
return k
|
2020-06-19 20:06:49 +01:00
|
|
|
}
|
2021-03-23 22:16:15 +00:00
|
|
|
func (e *watchdogEngine) Ping(ip netaddr.IP, useTSMP bool, cb func(*ipnstate.PingResult)) {
|
|
|
|
e.watchdog("Ping", func() { e.wrap.Ping(ip, useTSMP, 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)
|
|
|
|
}
|
|
|
|
func (e *watchdogEngine) Wait() {
|
|
|
|
e.wrap.Wait()
|
|
|
|
}
|