userspace_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package wgengine
  4. import (
  5. "fmt"
  6. "net/netip"
  7. "os"
  8. "reflect"
  9. "runtime"
  10. "testing"
  11. "go4.org/mem"
  12. "tailscale.com/cmd/testwrapper/flakytest"
  13. "tailscale.com/control/controlknobs"
  14. "tailscale.com/envknob"
  15. "tailscale.com/net/dns"
  16. "tailscale.com/net/netaddr"
  17. "tailscale.com/net/tstun"
  18. "tailscale.com/tailcfg"
  19. "tailscale.com/tstest"
  20. "tailscale.com/tstime/mono"
  21. "tailscale.com/types/key"
  22. "tailscale.com/types/netmap"
  23. "tailscale.com/types/opt"
  24. "tailscale.com/wgengine/router"
  25. "tailscale.com/wgengine/wgcfg"
  26. )
  27. func TestNoteReceiveActivity(t *testing.T) {
  28. now := mono.Time(123456)
  29. var logBuf tstest.MemLogger
  30. confc := make(chan bool, 1)
  31. gotConf := func() bool {
  32. select {
  33. case <-confc:
  34. return true
  35. default:
  36. return false
  37. }
  38. }
  39. e := &userspaceEngine{
  40. timeNow: func() mono.Time { return now },
  41. recvActivityAt: map[key.NodePublic]mono.Time{},
  42. logf: logBuf.Logf,
  43. tundev: new(tstun.Wrapper),
  44. testMaybeReconfigHook: func() { confc <- true },
  45. trimmedNodes: map[key.NodePublic]bool{},
  46. }
  47. ra := e.recvActivityAt
  48. nk := key.NewNode().Public()
  49. // Activity on an untracked key should do nothing.
  50. e.noteRecvActivity(nk)
  51. if len(ra) != 0 {
  52. t.Fatalf("unexpected growth in map: now has %d keys; want 0", len(ra))
  53. }
  54. if logBuf.Len() != 0 {
  55. t.Fatalf("unexpected log write (and thus activity): %s", logBuf.Bytes())
  56. }
  57. // Now track it, but don't mark it trimmed, so shouldn't update.
  58. ra[nk] = 0
  59. e.noteRecvActivity(nk)
  60. if len(ra) != 1 {
  61. t.Fatalf("unexpected growth in map: now has %d keys; want 1", len(ra))
  62. }
  63. if got := ra[nk]; got != now {
  64. t.Fatalf("time in map = %v; want %v", got, now)
  65. }
  66. if gotConf() {
  67. t.Fatalf("unexpected reconfig")
  68. }
  69. // Now mark it trimmed and expect an update.
  70. e.trimmedNodes[nk] = true
  71. e.noteRecvActivity(nk)
  72. if len(ra) != 1 {
  73. t.Fatalf("unexpected growth in map: now has %d keys; want 1", len(ra))
  74. }
  75. if got := ra[nk]; got != now {
  76. t.Fatalf("time in map = %v; want %v", got, now)
  77. }
  78. if !gotConf() {
  79. t.Fatalf("didn't get expected reconfig")
  80. }
  81. }
  82. func nodeViews(v []*tailcfg.Node) []tailcfg.NodeView {
  83. nv := make([]tailcfg.NodeView, len(v))
  84. for i, n := range v {
  85. nv[i] = n.View()
  86. }
  87. return nv
  88. }
  89. func TestUserspaceEngineReconfig(t *testing.T) {
  90. e, err := NewFakeUserspaceEngine(t.Logf, 0)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. t.Cleanup(e.Close)
  95. ue := e.(*userspaceEngine)
  96. routerCfg := &router.Config{}
  97. for _, nodeHex := range []string{
  98. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  99. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  100. } {
  101. nm := &netmap.NetworkMap{
  102. Peers: nodeViews([]*tailcfg.Node{
  103. {
  104. ID: 1,
  105. Key: nkFromHex(nodeHex),
  106. },
  107. }),
  108. }
  109. nk, err := key.ParseNodePublicUntyped(mem.S(nodeHex))
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. cfg := &wgcfg.Config{
  114. Peers: []wgcfg.Peer{
  115. {
  116. PublicKey: nk,
  117. AllowedIPs: []netip.Prefix{
  118. netip.PrefixFrom(netaddr.IPv4(100, 100, 99, 1), 32),
  119. },
  120. },
  121. },
  122. }
  123. e.SetNetworkMap(nm)
  124. err = e.Reconfig(cfg, routerCfg, &dns.Config{})
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. wantRecvAt := map[key.NodePublic]mono.Time{
  129. nkFromHex(nodeHex): 0,
  130. }
  131. if got := ue.recvActivityAt; !reflect.DeepEqual(got, wantRecvAt) {
  132. t.Errorf("wrong recvActivityAt\n got: %v\nwant: %v\n", got, wantRecvAt)
  133. }
  134. wantTrimmedNodes := map[key.NodePublic]bool{
  135. nkFromHex(nodeHex): true,
  136. }
  137. if got := ue.trimmedNodes; !reflect.DeepEqual(got, wantTrimmedNodes) {
  138. t.Errorf("wrong wantTrimmedNodes\n got: %v\nwant: %v\n", got, wantTrimmedNodes)
  139. }
  140. }
  141. }
  142. func TestUserspaceEnginePortReconfig(t *testing.T) {
  143. flakytest.Mark(t, "https://github.com/tailscale/tailscale/issues/2855")
  144. const defaultPort = 49983
  145. var knobs controlknobs.Knobs
  146. // Keep making a wgengine until we find an unused port
  147. var ue *userspaceEngine
  148. for i := 0; i < 100; i++ {
  149. attempt := uint16(defaultPort + i)
  150. e, err := NewFakeUserspaceEngine(t.Logf, attempt, &knobs)
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. ue = e.(*userspaceEngine)
  155. if ue.magicConn.LocalPort() == attempt {
  156. break
  157. }
  158. ue.Close()
  159. ue = nil
  160. }
  161. if ue == nil {
  162. t.Fatal("could not create a wgengine with a specific port")
  163. }
  164. t.Cleanup(ue.Close)
  165. startingPort := ue.magicConn.LocalPort()
  166. nodeKey, err := key.ParseNodePublicUntyped(mem.S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. cfg := &wgcfg.Config{
  171. Peers: []wgcfg.Peer{
  172. {
  173. PublicKey: nodeKey,
  174. AllowedIPs: []netip.Prefix{
  175. netip.PrefixFrom(netaddr.IPv4(100, 100, 99, 1), 32),
  176. },
  177. },
  178. },
  179. }
  180. routerCfg := &router.Config{}
  181. if err := ue.Reconfig(cfg, routerCfg, &dns.Config{}); err != nil {
  182. t.Fatal(err)
  183. }
  184. if got := ue.magicConn.LocalPort(); got != startingPort {
  185. t.Errorf("no debug setting changed local port to %d from %d", got, startingPort)
  186. }
  187. knobs.RandomizeClientPort.Store(true)
  188. if err := ue.Reconfig(cfg, routerCfg, &dns.Config{}); err != nil {
  189. t.Fatal(err)
  190. }
  191. if got := ue.magicConn.LocalPort(); got == startingPort {
  192. t.Errorf("debug setting did not change local port from %d", startingPort)
  193. }
  194. lastPort := ue.magicConn.LocalPort()
  195. knobs.RandomizeClientPort.Store(false)
  196. if err := ue.Reconfig(cfg, routerCfg, &dns.Config{}); err != nil {
  197. t.Fatal(err)
  198. }
  199. if startingPort == defaultPort {
  200. // Only try this if we managed to bind defaultPort the first time.
  201. // Otherwise, assume someone else on the computer is using defaultPort
  202. // and so Reconfig would have caused magicSockt to bind some other port.
  203. if got := ue.magicConn.LocalPort(); got != defaultPort {
  204. t.Errorf("debug setting did not change local port from %d to %d", startingPort, defaultPort)
  205. }
  206. }
  207. if got := ue.magicConn.LocalPort(); got == lastPort {
  208. t.Errorf("Reconfig did not change local port from %d", lastPort)
  209. }
  210. }
  211. // Test that enabling and disabling peer path MTU discovery works correctly.
  212. func TestUserspaceEnginePeerMTUReconfig(t *testing.T) {
  213. if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
  214. t.Skipf("skipping on %q; peer MTU not supported", runtime.GOOS)
  215. }
  216. defer os.Setenv("TS_DEBUG_ENABLE_PMTUD", os.Getenv("TS_DEBUG_ENABLE_PMTUD"))
  217. envknob.Setenv("TS_DEBUG_ENABLE_PMTUD", "")
  218. // Turn on debugging to help diagnose problems.
  219. defer os.Setenv("TS_DEBUG_PMTUD", os.Getenv("TS_DEBUG_PMTUD"))
  220. envknob.Setenv("TS_DEBUG_PMTUD", "true")
  221. var knobs controlknobs.Knobs
  222. e, err := NewFakeUserspaceEngine(t.Logf, 0, &knobs)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. t.Cleanup(e.Close)
  227. ue := e.(*userspaceEngine)
  228. if ue.magicConn.PeerMTUEnabled() != false {
  229. t.Error("peer MTU enabled by default, should not be")
  230. }
  231. osDefaultDF, err := ue.magicConn.DontFragSetting()
  232. if err != nil {
  233. t.Errorf("get don't fragment bit failed: %v", err)
  234. }
  235. t.Logf("Info: OS default don't fragment bit(s) setting: %v", osDefaultDF)
  236. // Build a set of configs to use as we change the peer MTU settings.
  237. nodeKey, err := key.ParseNodePublicUntyped(mem.S("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
  238. if err != nil {
  239. t.Fatal(err)
  240. }
  241. cfg := &wgcfg.Config{
  242. Peers: []wgcfg.Peer{
  243. {
  244. PublicKey: nodeKey,
  245. AllowedIPs: []netip.Prefix{
  246. netip.PrefixFrom(netaddr.IPv4(100, 100, 99, 1), 32),
  247. },
  248. },
  249. },
  250. }
  251. routerCfg := &router.Config{}
  252. tests := []struct {
  253. desc string // test description
  254. wantP bool // desired value of PMTUD setting
  255. wantDF bool // desired value of don't fragment bits
  256. shouldP opt.Bool // if set, force peer MTU to this value
  257. }{
  258. {desc: "after_first_reconfig", wantP: false, wantDF: osDefaultDF, shouldP: ""},
  259. {desc: "enabling_PMTUD_first_time", wantP: true, wantDF: true, shouldP: "true"},
  260. {desc: "disabling_PMTUD", wantP: false, wantDF: false, shouldP: "false"},
  261. {desc: "enabling_PMTUD_second_time", wantP: true, wantDF: true, shouldP: "true"},
  262. {desc: "returning_to_default_PMTUD", wantP: false, wantDF: false, shouldP: ""},
  263. }
  264. for _, tt := range tests {
  265. t.Run(tt.desc, func(t *testing.T) {
  266. if v, ok := tt.shouldP.Get(); ok {
  267. knobs.PeerMTUEnable.Store(v)
  268. } else {
  269. knobs.PeerMTUEnable.Store(false)
  270. }
  271. if err := ue.Reconfig(cfg, routerCfg, &dns.Config{}); err != nil {
  272. t.Fatal(err)
  273. }
  274. if v := ue.magicConn.PeerMTUEnabled(); v != tt.wantP {
  275. t.Errorf("peer MTU set to %v, want %v", v, tt.wantP)
  276. }
  277. if v, err := ue.magicConn.DontFragSetting(); v != tt.wantDF || err != nil {
  278. t.Errorf("don't fragment bit set to %v, want %v, err %v", v, tt.wantP, err)
  279. }
  280. })
  281. }
  282. }
  283. func nkFromHex(hex string) key.NodePublic {
  284. if len(hex) != 64 {
  285. panic(fmt.Sprintf("%q is len %d; want 64", hex, len(hex)))
  286. }
  287. k, err := key.ParseNodePublicUntyped(mem.S(hex[:64]))
  288. if err != nil {
  289. panic(fmt.Sprintf("%q is not hex: %v", hex, err))
  290. }
  291. return k
  292. }
  293. // an experiment to see if genLocalAddrFunc was worth it. As of Go
  294. // 1.16, it still very much is. (30-40x faster)
  295. func BenchmarkGenLocalAddrFunc(b *testing.B) {
  296. la1 := netip.MustParseAddr("1.2.3.4")
  297. la2 := netip.MustParseAddr("::4")
  298. lanot := netip.MustParseAddr("5.5.5.5")
  299. var x bool
  300. b.Run("map1", func(b *testing.B) {
  301. b.ReportAllocs()
  302. b.ResetTimer()
  303. m := map[netip.Addr]bool{
  304. la1: true,
  305. }
  306. for i := 0; i < b.N; i++ {
  307. x = m[la1]
  308. x = m[lanot]
  309. }
  310. })
  311. b.Run("map2", func(b *testing.B) {
  312. b.ReportAllocs()
  313. b.ResetTimer()
  314. m := map[netip.Addr]bool{
  315. la1: true,
  316. la2: true,
  317. }
  318. for i := 0; i < b.N; i++ {
  319. x = m[la1]
  320. x = m[lanot]
  321. }
  322. })
  323. b.Run("or1", func(b *testing.B) {
  324. b.ReportAllocs()
  325. b.ResetTimer()
  326. f := func(t netip.Addr) bool {
  327. return t == la1
  328. }
  329. for i := 0; i < b.N; i++ {
  330. x = f(la1)
  331. x = f(lanot)
  332. }
  333. })
  334. b.Run("or2", func(b *testing.B) {
  335. b.ReportAllocs()
  336. b.ResetTimer()
  337. f := func(t netip.Addr) bool {
  338. return t == la1 || t == la2
  339. }
  340. for i := 0; i < b.N; i++ {
  341. x = f(la1)
  342. x = f(lanot)
  343. }
  344. })
  345. b.Logf("x = %v", x)
  346. }