2021-02-18 16:58:13 +00:00
|
|
|
// Copyright (c) 2021 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 health is a registry for other packages to report & check
|
|
|
|
// overall health status of the node.
|
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
2021-03-16 05:20:48 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-12-21 21:52:50 +00:00
|
|
|
"net/http"
|
2021-10-22 16:12:00 +00:00
|
|
|
"runtime"
|
2021-03-16 05:20:48 +00:00
|
|
|
"sort"
|
2021-02-18 16:58:13 +00:00
|
|
|
"sync"
|
2021-04-27 00:08:05 +00:00
|
|
|
"sync/atomic"
|
2021-02-25 05:29:51 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-24 18:52:57 +00:00
|
|
|
"tailscale.com/envknob"
|
2021-02-25 05:29:51 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-11-02 21:30:48 +00:00
|
|
|
"tailscale.com/util/multierr"
|
2022-11-28 18:34:35 +00:00
|
|
|
"tailscale.com/util/set"
|
2021-02-18 16:58:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-02-25 05:29:51 +00:00
|
|
|
// mu guards everything in this var block.
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2022-11-28 18:34:35 +00:00
|
|
|
sysErr = map[Subsystem]error{} // error key => err (or nil for no error)
|
|
|
|
watchers = set.HandleSet[func(Subsystem, error)]{} // opt func to run if error state changes
|
|
|
|
warnables = map[*Warnable]struct{}{} // set of warnables
|
2022-11-13 15:32:37 +00:00
|
|
|
timer *time.Timer
|
2021-02-25 05:29:51 +00:00
|
|
|
|
2021-12-21 21:52:50 +00:00
|
|
|
debugHandler = map[string]http.Handler{}
|
|
|
|
|
2021-02-25 05:29:51 +00:00
|
|
|
inMapPoll bool
|
|
|
|
inMapPollSince time.Time
|
|
|
|
lastMapPollEndedAt time.Time
|
|
|
|
lastStreamedMapResponse time.Time
|
|
|
|
derpHomeRegion int
|
|
|
|
derpRegionConnected = map[int]bool{}
|
2021-09-02 02:27:22 +00:00
|
|
|
derpRegionHealthProblem = map[int]string{}
|
2021-02-25 05:29:51 +00:00
|
|
|
derpRegionLastFrame = map[int]time.Time{}
|
|
|
|
lastMapRequestHeard time.Time // time we got a 200 from control for a MapRequest
|
|
|
|
ipnState string
|
|
|
|
ipnWantRunning bool
|
2021-03-23 04:41:53 +00:00
|
|
|
anyInterfaceUp = true // until told otherwise
|
2021-04-28 17:36:54 +00:00
|
|
|
udp4Unbound bool
|
2021-09-18 19:59:55 +00:00
|
|
|
controlHealth []string
|
2022-06-03 17:52:07 +00:00
|
|
|
lastLoginErr error
|
2022-10-18 20:54:07 +00:00
|
|
|
localLogConfigErr error
|
2021-02-18 16:58:13 +00:00
|
|
|
)
|
|
|
|
|
2021-03-16 05:20:48 +00:00
|
|
|
// Subsystem is the name of a subsystem whose health can be monitored.
|
|
|
|
type Subsystem string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SysOverall is the name representing the overall health of
|
|
|
|
// the system, rather than one particular subsystem.
|
|
|
|
SysOverall = Subsystem("overall")
|
|
|
|
|
2021-04-28 17:36:54 +00:00
|
|
|
// SysRouter is the name of the wgengine/router subsystem.
|
2021-03-16 05:20:48 +00:00
|
|
|
SysRouter = Subsystem("router")
|
|
|
|
|
2021-04-03 02:31:58 +00:00
|
|
|
// SysDNS is the name of the net/dns subsystem.
|
|
|
|
SysDNS = Subsystem("dns")
|
|
|
|
|
2021-11-18 23:52:21 +00:00
|
|
|
// SysDNSOS is the name of the net/dns OSConfigurator subsystem.
|
|
|
|
SysDNSOS = Subsystem("dns-os")
|
|
|
|
|
2022-02-15 14:59:15 +00:00
|
|
|
// SysDNSManager is the name of the net/dns manager subsystem.
|
|
|
|
SysDNSManager = Subsystem("dns-manager")
|
2023-01-04 18:36:07 +00:00
|
|
|
|
|
|
|
// SysTKA is the name of the tailnet key authority subsystem.
|
|
|
|
SysTKA = Subsystem("tailnet-lock")
|
2022-11-13 15:32:37 +00:00
|
|
|
)
|
2022-02-15 14:59:15 +00:00
|
|
|
|
2022-11-13 15:32:37 +00:00
|
|
|
// NewWarnable returns a new warnable item that the caller can mark
|
|
|
|
// as health or in warning state.
|
|
|
|
func NewWarnable(opts ...WarnableOpt) *Warnable {
|
|
|
|
w := new(Warnable)
|
|
|
|
for _, o := range opts {
|
|
|
|
o.mod(w)
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
warnables[w] = struct{}{}
|
|
|
|
return w
|
|
|
|
}
|
2022-11-02 20:13:26 +00:00
|
|
|
|
2022-11-13 15:32:37 +00:00
|
|
|
// WarnableOpt is an option passed to NewWarnable.
|
|
|
|
type WarnableOpt interface {
|
|
|
|
mod(*Warnable)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMapDebugFlag returns a WarnableOpt for NewWarnable that makes the returned
|
|
|
|
// Warnable report itself to the coordination server as broken with this
|
|
|
|
// string in MapRequest.DebugFlag when Set to a non-nil value.
|
|
|
|
func WithMapDebugFlag(name string) WarnableOpt {
|
|
|
|
return warnOptFunc(func(w *Warnable) {
|
|
|
|
w.debugFlag = name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type warnOptFunc func(*Warnable)
|
|
|
|
|
|
|
|
func (f warnOptFunc) mod(w *Warnable) { f(w) }
|
|
|
|
|
|
|
|
// Warnable is a health check item that may or may not be in a bad warning state.
|
|
|
|
// The caller of NewWarnable is responsible for calling Set to update the state.
|
|
|
|
type Warnable struct {
|
|
|
|
debugFlag string // optional MapRequest.DebugFlag to send when unhealthy
|
|
|
|
|
|
|
|
isSet atomic.Bool
|
|
|
|
mu sync.Mutex
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set updates the Warnable's state.
|
|
|
|
// If non-nil, it's considered unhealthy.
|
|
|
|
func (w *Warnable) Set(err error) {
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
w.err = err
|
|
|
|
w.isSet.Store(err != nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Warnable) get() error {
|
|
|
|
if !w.isSet.Load() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
w.mu.Lock()
|
|
|
|
defer w.mu.Unlock()
|
|
|
|
return w.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendWarnableDebugFlags appends to base any health items that are currently in failed
|
|
|
|
// state and were created with MapDebugFlag.
|
|
|
|
func AppendWarnableDebugFlags(base []string) []string {
|
|
|
|
ret := base
|
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
for w := range warnables {
|
|
|
|
if w.debugFlag == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := w.get(); err != nil {
|
|
|
|
ret = append(ret, w.debugFlag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(ret[len(base):]) // sort the new ones
|
|
|
|
return ret
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
|
2021-02-18 16:58:13 +00:00
|
|
|
// RegisterWatcher adds a function that will be called if an
|
|
|
|
// error changes state either to unhealthy or from unhealthy. It is
|
|
|
|
// not called on transition from unknown to healthy. It must be non-nil
|
|
|
|
// and is run in its own goroutine. The returned func unregisters it.
|
2021-03-16 05:20:48 +00:00
|
|
|
func RegisterWatcher(cb func(key Subsystem, err error)) (unregister func()) {
|
2021-02-18 16:58:13 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2022-11-28 18:34:35 +00:00
|
|
|
handle := watchers.Add(cb)
|
2021-03-16 05:20:48 +00:00
|
|
|
if timer == nil {
|
|
|
|
timer = time.AfterFunc(time.Minute, timerSelfCheck)
|
|
|
|
}
|
2021-02-18 16:58:13 +00:00
|
|
|
return func() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
delete(watchers, handle)
|
2021-03-16 05:20:48 +00:00
|
|
|
if len(watchers) == 0 && timer != nil {
|
|
|
|
timer.Stop()
|
|
|
|
timer = nil
|
|
|
|
}
|
2021-02-18 16:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 02:31:58 +00:00
|
|
|
// SetRouterHealth sets the state of the wgengine/router.Router.
|
2022-11-28 18:34:35 +00:00
|
|
|
func SetRouterHealth(err error) { setErr(SysRouter, err) }
|
2021-02-18 16:58:13 +00:00
|
|
|
|
|
|
|
// RouterHealth returns the wgengine/router.Router error state.
|
2021-03-16 05:20:48 +00:00
|
|
|
func RouterHealth() error { return get(SysRouter) }
|
2021-02-18 16:58:13 +00:00
|
|
|
|
2021-04-03 02:31:58 +00:00
|
|
|
// SetDNSHealth sets the state of the net/dns.Manager
|
2022-11-28 18:34:35 +00:00
|
|
|
func SetDNSHealth(err error) { setErr(SysDNS, err) }
|
2021-04-03 02:31:58 +00:00
|
|
|
|
|
|
|
// DNSHealth returns the net/dns.Manager error state.
|
|
|
|
func DNSHealth() error { return get(SysDNS) }
|
|
|
|
|
2021-11-18 23:52:21 +00:00
|
|
|
// SetDNSOSHealth sets the state of the net/dns.OSConfigurator
|
2022-11-28 18:34:35 +00:00
|
|
|
func SetDNSOSHealth(err error) { setErr(SysDNSOS, err) }
|
2021-11-18 23:52:21 +00:00
|
|
|
|
2022-02-15 14:59:15 +00:00
|
|
|
// SetDNSManagerHealth sets the state of the Linux net/dns manager's
|
|
|
|
// discovery of the /etc/resolv.conf situation.
|
2022-11-28 18:34:35 +00:00
|
|
|
func SetDNSManagerHealth(err error) { setErr(SysDNSManager, err) }
|
2022-02-15 14:59:15 +00:00
|
|
|
|
2021-11-18 23:52:21 +00:00
|
|
|
// DNSOSHealth returns the net/dns.OSConfigurator error state.
|
|
|
|
func DNSOSHealth() error { return get(SysDNSOS) }
|
|
|
|
|
2023-01-04 18:36:07 +00:00
|
|
|
// SetTKAHealth sets the health of the tailnet key authority.
|
|
|
|
func SetTKAHealth(err error) { setErr(SysTKA, err) }
|
|
|
|
|
|
|
|
// TKAHealth returns the tailnet key authority error state.
|
|
|
|
func TKAHealth() error { return get(SysTKA) }
|
|
|
|
|
2022-10-18 20:54:07 +00:00
|
|
|
// SetLocalLogConfigHealth sets the error state of this client's local log configuration.
|
|
|
|
func SetLocalLogConfigHealth(err error) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
localLogConfigErr = err
|
|
|
|
}
|
|
|
|
|
2021-12-21 21:52:50 +00:00
|
|
|
func RegisterDebugHandler(typ string, h http.Handler) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
debugHandler[typ] = h
|
|
|
|
}
|
|
|
|
|
|
|
|
func DebugHandler(typ string) http.Handler {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return debugHandler[typ]
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:20:48 +00:00
|
|
|
func get(key Subsystem) error {
|
2021-02-18 16:58:13 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2021-03-16 05:20:48 +00:00
|
|
|
return sysErr[key]
|
2021-02-18 16:58:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 18:34:35 +00:00
|
|
|
func setErr(key Subsystem, err error) {
|
2021-02-18 16:58:13 +00:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2021-03-16 05:20:48 +00:00
|
|
|
setLocked(key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setLocked(key Subsystem, err error) {
|
|
|
|
old, ok := sysErr[key]
|
2021-02-18 16:58:13 +00:00
|
|
|
if !ok && err == nil {
|
|
|
|
// Initial happy path.
|
2021-03-16 05:20:48 +00:00
|
|
|
sysErr[key] = nil
|
2021-02-25 05:29:51 +00:00
|
|
|
selfCheckLocked()
|
2021-02-18 16:58:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ok && (old == nil) == (err == nil) {
|
|
|
|
// No change in overall error status (nil-vs-not), so
|
|
|
|
// don't run callbacks, but exact error might've
|
|
|
|
// changed, so note it.
|
|
|
|
if err != nil {
|
2021-03-16 05:20:48 +00:00
|
|
|
sysErr[key] = err
|
2021-02-18 16:58:13 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
sysErr[key] = err
|
2021-02-25 05:29:51 +00:00
|
|
|
selfCheckLocked()
|
2021-02-18 16:58:13 +00:00
|
|
|
for _, cb := range watchers {
|
|
|
|
go cb(key, err)
|
|
|
|
}
|
|
|
|
}
|
2021-02-25 05:29:51 +00:00
|
|
|
|
2021-09-18 19:59:55 +00:00
|
|
|
func SetControlHealth(problems []string) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
controlHealth = problems
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:29:51 +00:00
|
|
|
// GotStreamedMapResponse notes that we got a tailcfg.MapResponse
|
|
|
|
// message in streaming mode, even if it's just a keep-alive message.
|
|
|
|
func GotStreamedMapResponse() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
lastStreamedMapResponse = time.Now()
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-12-16 16:06:32 +00:00
|
|
|
// SetInPollNetMap records whether the client has an open
|
|
|
|
// HTTP long poll open to the control plane.
|
2021-02-25 05:29:51 +00:00
|
|
|
func SetInPollNetMap(v bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if v == inMapPoll {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
inMapPoll = v
|
|
|
|
if v {
|
|
|
|
inMapPollSince = time.Now()
|
|
|
|
} else {
|
|
|
|
lastMapPollEndedAt = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 16:06:32 +00:00
|
|
|
// GetInPollNetMap reports whether the client has an open
|
|
|
|
// HTTP long poll open to the control plane.
|
|
|
|
func GetInPollNetMap() bool {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return inMapPoll
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:29:51 +00:00
|
|
|
// SetMagicSockDERPHome notes what magicsock's view of its home DERP is.
|
|
|
|
func SetMagicSockDERPHome(region int) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
derpHomeRegion = region
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoteMapRequestHeard notes whenever we successfully sent a map request
|
|
|
|
// to control for which we received a 200 response.
|
|
|
|
func NoteMapRequestHeard(mr *tailcfg.MapRequest) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
// TODO: extract mr.HostInfo.NetInfo.PreferredDERP, compare
|
|
|
|
// against SetMagicSockDERPHome and
|
|
|
|
// SetDERPRegionConnectedState
|
|
|
|
|
|
|
|
lastMapRequestHeard = time.Now()
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetDERPRegionConnectedState(region int, connected bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
derpRegionConnected[region] = connected
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-09-02 02:27:22 +00:00
|
|
|
// SetDERPRegionHealth sets or clears any problem associated with the
|
|
|
|
// provided DERP region.
|
|
|
|
func SetDERPRegionHealth(region int, problem string) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if problem == "" {
|
|
|
|
delete(derpRegionHealthProblem, region)
|
|
|
|
} else {
|
|
|
|
derpRegionHealthProblem[region] = problem
|
|
|
|
}
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:29:51 +00:00
|
|
|
func NoteDERPRegionReceivedFrame(region int) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
derpRegionLastFrame[region] = time.Now()
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
|
|
|
// state is an ipn.State.String() value: "Running", "Stopped", "NeedsLogin", etc.
|
|
|
|
func SetIPNState(state string, wantRunning bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
ipnState = state
|
|
|
|
ipnWantRunning = wantRunning
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-03-23 04:41:53 +00:00
|
|
|
// SetAnyInterfaceUp sets whether any network interface is up.
|
|
|
|
func SetAnyInterfaceUp(up bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
anyInterfaceUp = up
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:36:54 +00:00
|
|
|
// SetUDP4Unbound sets whether the udp4 bind failed completely.
|
|
|
|
func SetUDP4Unbound(unbound bool) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
udp4Unbound = unbound
|
|
|
|
selfCheckLocked()
|
|
|
|
}
|
|
|
|
|
2022-06-03 17:52:07 +00:00
|
|
|
// SetAuthRoutineInError records the latest error encountered as a result of a
|
|
|
|
// login attempt. Providing a nil error indicates successful login, or that
|
|
|
|
// being logged in w/coordination is not currently desired.
|
|
|
|
func SetAuthRoutineInError(err error) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
lastLoginErr = err
|
|
|
|
}
|
|
|
|
|
2021-03-16 05:20:48 +00:00
|
|
|
func timerSelfCheck() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2021-04-27 00:08:05 +00:00
|
|
|
checkReceiveFuncs()
|
2021-03-16 05:20:48 +00:00
|
|
|
selfCheckLocked()
|
|
|
|
if timer != nil {
|
|
|
|
timer.Reset(time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 05:29:51 +00:00
|
|
|
func selfCheckLocked() {
|
2021-03-16 05:20:48 +00:00
|
|
|
if ipnState == "" {
|
|
|
|
// Don't check yet.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setLocked(SysOverall, overallErrorLocked())
|
|
|
|
}
|
|
|
|
|
2021-09-02 02:27:22 +00:00
|
|
|
// OverallError returns a summary of the health state.
|
|
|
|
//
|
|
|
|
// If there are multiple problems, the error will be of type
|
2021-11-02 21:30:48 +00:00
|
|
|
// multierr.Error.
|
2021-09-02 02:27:22 +00:00
|
|
|
func OverallError() error {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return overallErrorLocked()
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:49:39 +00:00
|
|
|
var fakeErrForTesting = envknob.RegisterString("TS_DEBUG_FAKE_HEALTH_ERROR")
|
2021-09-16 18:24:44 +00:00
|
|
|
|
2021-03-16 05:20:48 +00:00
|
|
|
func overallErrorLocked() error {
|
2021-03-23 04:41:53 +00:00
|
|
|
if !anyInterfaceUp {
|
|
|
|
return errors.New("network down")
|
|
|
|
}
|
2022-10-18 20:54:07 +00:00
|
|
|
if localLogConfigErr != nil {
|
|
|
|
return localLogConfigErr
|
|
|
|
}
|
2022-06-03 17:52:07 +00:00
|
|
|
if !ipnWantRunning {
|
2021-03-16 05:20:48 +00:00
|
|
|
return fmt.Errorf("state=%v, wantRunning=%v", ipnState, ipnWantRunning)
|
|
|
|
}
|
2022-06-03 17:52:07 +00:00
|
|
|
if lastLoginErr != nil {
|
|
|
|
return fmt.Errorf("not logged in, last login error=%v", lastLoginErr)
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
now := time.Now()
|
|
|
|
if !inMapPoll && (lastMapPollEndedAt.IsZero() || now.Sub(lastMapPollEndedAt) > 10*time.Second) {
|
|
|
|
return errors.New("not in map poll")
|
|
|
|
}
|
|
|
|
const tooIdle = 2*time.Minute + 5*time.Second
|
|
|
|
if d := now.Sub(lastStreamedMapResponse).Round(time.Second); d > tooIdle {
|
|
|
|
return fmt.Errorf("no map response in %v", d)
|
|
|
|
}
|
|
|
|
rid := derpHomeRegion
|
|
|
|
if rid == 0 {
|
|
|
|
return errors.New("no DERP home")
|
|
|
|
}
|
|
|
|
if !derpRegionConnected[rid] {
|
|
|
|
return fmt.Errorf("not connected to home DERP region %v", rid)
|
|
|
|
}
|
|
|
|
if d := now.Sub(derpRegionLastFrame[rid]).Round(time.Second); d > tooIdle {
|
|
|
|
return fmt.Errorf("haven't heard from home DERP region %v in %v", rid, d)
|
|
|
|
}
|
2021-04-28 17:36:54 +00:00
|
|
|
if udp4Unbound {
|
|
|
|
return errors.New("no udp4 bind")
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
|
|
|
|
// TODO: use
|
2021-02-25 05:29:51 +00:00
|
|
|
_ = inMapPollSince
|
|
|
|
_ = lastMapPollEndedAt
|
|
|
|
_ = lastStreamedMapResponse
|
|
|
|
_ = lastMapRequestHeard
|
2021-03-16 05:20:48 +00:00
|
|
|
|
|
|
|
var errs []error
|
2021-04-27 00:08:05 +00:00
|
|
|
for _, recv := range receiveFuncs {
|
|
|
|
if recv.missing {
|
|
|
|
errs = append(errs, fmt.Errorf("%s is not running", recv.name))
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
for sys, err := range sysErr {
|
|
|
|
if err == nil || sys == SysOverall {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
errs = append(errs, fmt.Errorf("%v: %w", sys, err))
|
|
|
|
}
|
2022-11-13 15:32:37 +00:00
|
|
|
for w := range warnables {
|
|
|
|
if err := w.get(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-02 02:27:22 +00:00
|
|
|
for regionID, problem := range derpRegionHealthProblem {
|
|
|
|
errs = append(errs, fmt.Errorf("derp%d: %v", regionID, problem))
|
|
|
|
}
|
2021-09-18 19:59:55 +00:00
|
|
|
for _, s := range controlHealth {
|
|
|
|
errs = append(errs, errors.New(s))
|
|
|
|
}
|
2022-09-17 03:24:28 +00:00
|
|
|
if err := envknob.ApplyDiskConfigError(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2022-09-14 19:49:39 +00:00
|
|
|
if e := fakeErrForTesting(); len(errs) == 0 && e != "" {
|
2021-09-16 18:24:44 +00:00
|
|
|
return errors.New(e)
|
|
|
|
}
|
2021-03-16 05:20:48 +00:00
|
|
|
sort.Slice(errs, func(i, j int) bool {
|
|
|
|
// Not super efficient (stringifying these in a sort), but probably max 2 or 3 items.
|
|
|
|
return errs[i].Error() < errs[j].Error()
|
|
|
|
})
|
2021-11-02 21:30:48 +00:00
|
|
|
return multierr.New(errs...)
|
2021-02-25 05:29:51 +00:00
|
|
|
}
|
2021-04-27 00:08:05 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
ReceiveIPv4 = ReceiveFuncStats{name: "ReceiveIPv4"}
|
2021-04-28 17:43:51 +00:00
|
|
|
ReceiveIPv6 = ReceiveFuncStats{name: "ReceiveIPv6"}
|
2021-04-27 00:08:05 +00:00
|
|
|
ReceiveDERP = ReceiveFuncStats{name: "ReceiveDERP"}
|
|
|
|
|
2021-04-28 17:43:51 +00:00
|
|
|
receiveFuncs = []*ReceiveFuncStats{&ReceiveIPv4, &ReceiveIPv6, &ReceiveDERP}
|
2021-04-27 00:08:05 +00:00
|
|
|
)
|
|
|
|
|
2021-10-22 16:12:00 +00:00
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
receiveFuncs = receiveFuncs[2:] // ignore IPv4 and IPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 00:08:05 +00:00
|
|
|
// ReceiveFuncStats tracks the calls made to a wireguard-go receive func.
|
|
|
|
type ReceiveFuncStats struct {
|
|
|
|
// name is the name of the receive func.
|
|
|
|
name string
|
|
|
|
// numCalls is the number of times the receive func has ever been called.
|
|
|
|
// It is required because it is possible for a receive func's wireguard-go goroutine
|
|
|
|
// to be active even though the receive func isn't.
|
|
|
|
// The wireguard-go goroutine alternates between calling the receive func and
|
|
|
|
// processing what the func returned.
|
|
|
|
numCalls uint64 // accessed atomically
|
|
|
|
// prevNumCalls is the value of numCalls last time the health check examined it.
|
|
|
|
prevNumCalls uint64
|
|
|
|
// inCall indicates whether the receive func is currently running.
|
|
|
|
inCall uint32 // bool, accessed atomically
|
|
|
|
// missing indicates whether the receive func is not running.
|
|
|
|
missing bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ReceiveFuncStats) Enter() {
|
|
|
|
atomic.AddUint64(&s.numCalls, 1)
|
|
|
|
atomic.StoreUint32(&s.inCall, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ReceiveFuncStats) Exit() {
|
|
|
|
atomic.StoreUint32(&s.inCall, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkReceiveFuncs() {
|
|
|
|
for _, recv := range receiveFuncs {
|
|
|
|
recv.missing = false
|
|
|
|
prev := recv.prevNumCalls
|
|
|
|
numCalls := atomic.LoadUint64(&recv.numCalls)
|
|
|
|
recv.prevNumCalls = numCalls
|
|
|
|
if numCalls > prev {
|
|
|
|
// OK: the function has gotten called since last we checked
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if atomic.LoadUint32(&recv.inCall) == 1 {
|
|
|
|
// OK: the function is active, probably blocked due to inactivity
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Not OK: The function is not active, and not accumulating new calls.
|
|
|
|
// It is probably MIA.
|
|
|
|
recv.missing = true
|
|
|
|
}
|
|
|
|
}
|