debugknobs.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ios && !js
  4. package magicsock
  5. import (
  6. "log"
  7. "net/netip"
  8. "strings"
  9. "sync"
  10. "tailscale.com/envknob"
  11. )
  12. // Various debugging and experimental tweakables, set by environment
  13. // variable.
  14. var (
  15. // debugDisco prints verbose logs of active discovery events as
  16. // they happen.
  17. debugDisco = envknob.RegisterBool("TS_DEBUG_DISCO")
  18. // debugPeerMap prints verbose logs of changes to the peermap.
  19. debugPeerMap = envknob.RegisterBool("TS_DEBUG_MAGICSOCK_PEERMAP")
  20. // debugOmitLocalAddresses removes all local interface addresses
  21. // from magicsock's discovered local endpoints. Used in some tests.
  22. debugOmitLocalAddresses = envknob.RegisterBool("TS_DEBUG_OMIT_LOCAL_ADDRS")
  23. // logDerpVerbose logs all received DERP packets, including their
  24. // full payload.
  25. logDerpVerbose = envknob.RegisterBool("TS_DEBUG_DERP")
  26. // debugReSTUNStopOnIdle unconditionally enables the "shut down
  27. // STUN if magicsock is idle" behavior that normally only triggers
  28. // on mobile devices, lowers the shutdown interval, and logs more
  29. // verbosely about idle measurements.
  30. debugReSTUNStopOnIdle = envknob.RegisterBool("TS_DEBUG_RESTUN_STOP_ON_IDLE")
  31. // debugAlwaysDERP disables the use of UDP, forcing all peer communication over DERP.
  32. debugAlwaysDERP = envknob.RegisterBool("TS_DEBUG_ALWAYS_USE_DERP")
  33. // debugDERPAddr sets the derp address manually, overriding the DERP map from control.
  34. debugUseDERPAddr = envknob.RegisterString("TS_DEBUG_USE_DERP_ADDR")
  35. // debugDERPUseHTTP tells clients to connect to DERP via HTTP on port 3340 instead of
  36. // HTTPS on 443.
  37. debugUseDERPHTTP = envknob.RegisterBool("TS_DEBUG_USE_DERP_HTTP")
  38. // debugEnableSilentDisco disables the use of heartbeatTimer on the endpoint struct
  39. // and attempts to handle disco silently. See issue #540 for details.
  40. debugEnableSilentDisco = envknob.RegisterBool("TS_DEBUG_ENABLE_SILENT_DISCO")
  41. // debugSendCallMeUnknownPeer sends a CallMeMaybe to a non-existent destination every
  42. // time we send a real CallMeMaybe to test the PeerGoneNotHere logic.
  43. debugSendCallMeUnknownPeer = envknob.RegisterBool("TS_DEBUG_SEND_CALLME_UNKNOWN_PEER")
  44. // debugBindSocket prints extra debugging about socket rebinding in magicsock.
  45. debugBindSocket = envknob.RegisterBool("TS_DEBUG_MAGICSOCK_BIND_SOCKET")
  46. // debugRingBufferMaxSizeBytes overrides the default size of the endpoint
  47. // history ringbuffer.
  48. debugRingBufferMaxSizeBytes = envknob.RegisterInt("TS_DEBUG_MAGICSOCK_RING_BUFFER_MAX_SIZE_BYTES")
  49. // debugEnablePMTUD enables the peer MTU feature, which does path MTU
  50. // discovery on UDP connections between peers. Currently (2023-09-05)
  51. // this only turns on the don't fragment bit for the magicsock UDP
  52. // sockets.
  53. //
  54. //lint:ignore U1000 used on Linux/Darwin only
  55. debugEnablePMTUD = envknob.RegisterOptBool("TS_DEBUG_ENABLE_PMTUD")
  56. // debugPMTUD prints extra debugging about peer MTU path discovery.
  57. //
  58. //lint:ignore U1000 used on Linux/Darwin only
  59. debugPMTUD = envknob.RegisterBool("TS_DEBUG_PMTUD")
  60. // debugNeverDirectUDP disables the use of direct UDP connections, forcing
  61. // all peer communication over DERP or peer relay.
  62. debugNeverDirectUDP = envknob.RegisterBool("TS_DEBUG_NEVER_DIRECT_UDP")
  63. // Hey you! Adding a new debugknob? Make sure to stub it out in the
  64. // debugknobs_stubs.go file too.
  65. )
  66. // inTest reports whether the running program is a test that set the
  67. // IN_TS_TEST environment variable.
  68. //
  69. // Unlike the other debug tweakables above, this one needs to be
  70. // checked every time at runtime, because tests set this after program
  71. // startup.
  72. func inTest() bool { return envknob.Bool("IN_TS_TEST") }
  73. // pretendpoints returns TS_DEBUG_PRETENDPOINT as []AddrPort, if set.
  74. // See https://github.com/tailscale/tailscale/issues/12578 and
  75. // https://github.com/tailscale/tailscale/pull/12735.
  76. //
  77. // It can be between 0 and 3 comma-separated AddrPorts.
  78. var pretendpoints = sync.OnceValue(func() (ret []netip.AddrPort) {
  79. all := envknob.String("TS_DEBUG_PRETENDPOINT")
  80. const max = 3
  81. remain := all
  82. for remain != "" && len(ret) < max {
  83. var s string
  84. s, remain, _ = strings.Cut(remain, ",")
  85. ap, err := netip.ParseAddrPort(s)
  86. if err != nil {
  87. log.Printf("ignoring invalid AddrPort %q in TS_DEBUG_PRETENDPOINT %q: %v", s, all, err)
  88. continue
  89. }
  90. ret = append(ret, ap)
  91. }
  92. return
  93. })