1
0

hostmap_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package nebula
  2. import (
  3. "net/netip"
  4. "testing"
  5. "github.com/slackhq/nebula/config"
  6. "github.com/slackhq/nebula/test"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func TestHostMap_MakePrimary(t *testing.T) {
  11. l := test.NewLogger()
  12. hm := newHostMap(l)
  13. f := &Interface{}
  14. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  15. h2 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 2}
  16. h3 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 3}
  17. h4 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 4}
  18. hm.unlockedAddHostInfo(h4, f)
  19. hm.unlockedAddHostInfo(h3, f)
  20. hm.unlockedAddHostInfo(h2, f)
  21. hm.unlockedAddHostInfo(h1, f)
  22. // Make sure we go h1 -> h2 -> h3 -> h4
  23. prim := hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  24. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  25. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  26. assert.Nil(t, prim.prev)
  27. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  28. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  29. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  30. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  31. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  32. assert.Nil(t, h4.next)
  33. // Swap h3/middle to primary
  34. hm.MakePrimary(h3)
  35. // Make sure we go h3 -> h1 -> h2 -> h4
  36. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  37. assert.Equal(t, h3.localIndexId, prim.localIndexId)
  38. assert.Equal(t, h1.localIndexId, prim.next.localIndexId)
  39. assert.Nil(t, prim.prev)
  40. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  41. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  42. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  43. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  44. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  45. assert.Nil(t, h4.next)
  46. // Swap h4/tail to primary
  47. hm.MakePrimary(h4)
  48. // Make sure we go h4 -> h3 -> h1 -> h2
  49. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  50. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  51. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  52. assert.Nil(t, prim.prev)
  53. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  54. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  55. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  56. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  57. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  58. assert.Nil(t, h2.next)
  59. // Swap h4 again should be no-op
  60. hm.MakePrimary(h4)
  61. // Make sure we go h4 -> h3 -> h1 -> h2
  62. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  63. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  64. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  65. assert.Nil(t, prim.prev)
  66. assert.Equal(t, h1.localIndexId, h3.next.localIndexId)
  67. assert.Equal(t, h4.localIndexId, h3.prev.localIndexId)
  68. assert.Equal(t, h2.localIndexId, h1.next.localIndexId)
  69. assert.Equal(t, h3.localIndexId, h1.prev.localIndexId)
  70. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  71. assert.Nil(t, h2.next)
  72. }
  73. func TestHostMap_DeleteHostInfo(t *testing.T) {
  74. l := test.NewLogger()
  75. hm := newHostMap(l)
  76. f := &Interface{}
  77. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  78. h2 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 2}
  79. h3 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 3}
  80. h4 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 4}
  81. h5 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 5}
  82. h6 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 6}
  83. hm.unlockedAddHostInfo(h6, f)
  84. hm.unlockedAddHostInfo(h5, f)
  85. hm.unlockedAddHostInfo(h4, f)
  86. hm.unlockedAddHostInfo(h3, f)
  87. hm.unlockedAddHostInfo(h2, f)
  88. hm.unlockedAddHostInfo(h1, f)
  89. // h6 should be deleted
  90. assert.Nil(t, h6.next)
  91. assert.Nil(t, h6.prev)
  92. h := hm.QueryIndex(h6.localIndexId)
  93. assert.Nil(t, h)
  94. // Make sure we go h1 -> h2 -> h3 -> h4 -> h5
  95. prim := hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  96. assert.Equal(t, h1.localIndexId, prim.localIndexId)
  97. assert.Equal(t, h2.localIndexId, prim.next.localIndexId)
  98. assert.Nil(t, prim.prev)
  99. assert.Equal(t, h1.localIndexId, h2.prev.localIndexId)
  100. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  101. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  102. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  103. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  104. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  105. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  106. assert.Nil(t, h5.next)
  107. // Delete primary
  108. hm.DeleteHostInfo(h1)
  109. assert.Nil(t, h1.prev)
  110. assert.Nil(t, h1.next)
  111. // Make sure we go h2 -> h3 -> h4 -> h5
  112. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  113. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  114. assert.Equal(t, h3.localIndexId, prim.next.localIndexId)
  115. assert.Nil(t, prim.prev)
  116. assert.Equal(t, h3.localIndexId, h2.next.localIndexId)
  117. assert.Equal(t, h2.localIndexId, h3.prev.localIndexId)
  118. assert.Equal(t, h4.localIndexId, h3.next.localIndexId)
  119. assert.Equal(t, h3.localIndexId, h4.prev.localIndexId)
  120. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  121. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  122. assert.Nil(t, h5.next)
  123. // Delete in the middle
  124. hm.DeleteHostInfo(h3)
  125. assert.Nil(t, h3.prev)
  126. assert.Nil(t, h3.next)
  127. // Make sure we go h2 -> h4 -> h5
  128. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  129. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  130. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  131. assert.Nil(t, prim.prev)
  132. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  133. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  134. assert.Equal(t, h5.localIndexId, h4.next.localIndexId)
  135. assert.Equal(t, h4.localIndexId, h5.prev.localIndexId)
  136. assert.Nil(t, h5.next)
  137. // Delete the tail
  138. hm.DeleteHostInfo(h5)
  139. assert.Nil(t, h5.prev)
  140. assert.Nil(t, h5.next)
  141. // Make sure we go h2 -> h4
  142. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  143. assert.Equal(t, h2.localIndexId, prim.localIndexId)
  144. assert.Equal(t, h4.localIndexId, prim.next.localIndexId)
  145. assert.Nil(t, prim.prev)
  146. assert.Equal(t, h4.localIndexId, h2.next.localIndexId)
  147. assert.Equal(t, h2.localIndexId, h4.prev.localIndexId)
  148. assert.Nil(t, h4.next)
  149. // Delete the head
  150. hm.DeleteHostInfo(h2)
  151. assert.Nil(t, h2.prev)
  152. assert.Nil(t, h2.next)
  153. // Make sure we only have h4
  154. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  155. assert.Equal(t, h4.localIndexId, prim.localIndexId)
  156. assert.Nil(t, prim.prev)
  157. assert.Nil(t, prim.next)
  158. assert.Nil(t, h4.next)
  159. // Delete the only item
  160. hm.DeleteHostInfo(h4)
  161. assert.Nil(t, h4.prev)
  162. assert.Nil(t, h4.next)
  163. // Make sure we have nil
  164. prim = hm.QueryVpnAddr(netip.MustParseAddr("0.0.0.1"))
  165. assert.Nil(t, prim)
  166. }
  167. // TestHostMap_DeleteHostInfo_MultipleVpnAddrs exercises the case where a hostinfo carries more than one
  168. // vpnAddr and shares its next/prev chain with a live sibling. Deleting the head must not corrupt the
  169. // sibling: every address the sibling owns has to keep pointing at it. The pre-fix code unlinked the shared
  170. // chain once per vpnAddr, so on the first address it nil'd next/prev, and on the second address the node
  171. // looked already-detached: it dropped the map entry instead of promoting the sibling (and tripped the
  172. // isLastHostinfo relay teardown). See unlockedDeleteHostInfo.
  173. func TestHostMap_DeleteHostInfo_MultipleVpnAddrs(t *testing.T) {
  174. l := test.NewLogger()
  175. hm := newHostMap(l)
  176. f := &Interface{}
  177. a := netip.MustParseAddr("0.0.0.1")
  178. b := netip.MustParseAddr("0.0.0.2")
  179. // Two tunnels for the same peer, each reachable at both a and b.
  180. other := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 1}
  181. head := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 2}
  182. hm.unlockedAddHostInfo(other, f)
  183. hm.unlockedAddHostInfo(head, f)
  184. // head is primary for both addresses, other is next in the shared chain
  185. assert.Equal(t, head.localIndexId, hm.QueryVpnAddr(a).localIndexId)
  186. assert.Equal(t, head.localIndexId, hm.QueryVpnAddr(b).localIndexId)
  187. assert.Equal(t, other.localIndexId, head.next.localIndexId)
  188. assert.Equal(t, head.localIndexId, other.prev.localIndexId)
  189. // Delete the head. other is still live, so it must become primary for BOTH addresses.
  190. hm.DeleteHostInfo(head)
  191. // Pre-fix: QueryVpnAddr(b) came back nil here because the second address was deleted rather than
  192. // promoted, leaving other unreachable at b.
  193. require.NotNil(t, hm.QueryVpnAddr(a))
  194. require.NotNil(t, hm.QueryVpnAddr(b))
  195. assert.Equal(t, other.localIndexId, hm.QueryVpnAddr(a).localIndexId)
  196. assert.Equal(t, other.localIndexId, hm.QueryVpnAddr(b).localIndexId)
  197. // other is now the only hostinfo in the chain
  198. assert.Nil(t, other.prev)
  199. assert.Nil(t, other.next)
  200. // head is fully detached
  201. assert.Nil(t, head.prev)
  202. assert.Nil(t, head.next)
  203. assert.Nil(t, hm.QueryIndex(head.localIndexId))
  204. }
  205. // TestHostMap_MaxHostInfosPerVpnIp_MultipleVpnAddrs verifies the MaxHostInfosPerVpnIp overflow prune
  206. // (unlockedInnerAddHostInfo calls unlockedDeleteHostInfo on the oldest node once the chain is too long)
  207. // still behaves when hostinfos carry more than one vpnAddr. The pruned node is always the tail, so it is
  208. // primary for none of the addresses, and both address chains must stay consistent afterwards.
  209. func TestHostMap_MaxHostInfosPerVpnIp_MultipleVpnAddrs(t *testing.T) {
  210. l := test.NewLogger()
  211. hm := newHostMap(l)
  212. f := &Interface{}
  213. a := netip.MustParseAddr("0.0.0.1")
  214. b := netip.MustParseAddr("0.0.0.2")
  215. // Add one more than the cap, newest last so it becomes head. Every hostinfo owns both a and b.
  216. hostinfos := make([]*HostInfo, 0, MaxHostInfosPerVpnIp+1)
  217. for i := 0; i <= MaxHostInfosPerVpnIp; i++ {
  218. hostinfos = append(hostinfos, &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: uint32(i + 1)})
  219. }
  220. // Add oldest first (highest index in our slice) so the very first one added is the overflow victim.
  221. for i := len(hostinfos) - 1; i >= 0; i-- {
  222. hm.unlockedAddHostInfo(hostinfos[i], f)
  223. }
  224. oldest := hostinfos[len(hostinfos)-1]
  225. // The oldest hostinfo should have been pruned and fully detached
  226. assert.Nil(t, oldest.next)
  227. assert.Nil(t, oldest.prev)
  228. assert.Nil(t, hm.QueryIndex(oldest.localIndexId))
  229. // Both addresses resolve to the same head, and that head is one of the survivors (not the pruned one)
  230. primA := hm.QueryVpnAddr(a)
  231. primB := hm.QueryVpnAddr(b)
  232. require.NotNil(t, primA)
  233. require.NotNil(t, primB)
  234. assert.Equal(t, primA.localIndexId, primB.localIndexId)
  235. assert.NotEqual(t, oldest.localIndexId, primA.localIndexId)
  236. // Walk the shared chain: exactly MaxHostInfosPerVpnIp survivors, no cycles, oldest absent
  237. seen := map[uint32]struct{}{}
  238. for h := primA; h != nil; h = h.next {
  239. _, dup := seen[h.localIndexId]
  240. require.False(t, dup, "cycle detected in hostinfo chain")
  241. seen[h.localIndexId] = struct{}{}
  242. if h.next != nil {
  243. assert.Equal(t, h.localIndexId, h.next.prev.localIndexId, "prev pointer must mirror next")
  244. }
  245. }
  246. assert.Len(t, seen, MaxHostInfosPerVpnIp)
  247. _, prunedStillPresent := seen[oldest.localIndexId]
  248. assert.False(t, prunedStillPresent)
  249. }
  250. func TestHostMap_reload(t *testing.T) {
  251. l := test.NewLogger()
  252. c := config.NewC(test.NewLogger())
  253. hm := NewHostMapFromConfig(l, c)
  254. toS := func(ipn []netip.Prefix) []string {
  255. var s []string
  256. for _, n := range ipn {
  257. s = append(s, n.String())
  258. }
  259. return s
  260. }
  261. assert.Empty(t, hm.GetPreferredRanges())
  262. c.ReloadConfigString("preferred_ranges: [1.1.1.0/24, 10.1.1.0/24]")
  263. assert.Equal(t, []string{"1.1.1.0/24", "10.1.1.0/24"}, toS(hm.GetPreferredRanges()))
  264. c.ReloadConfigString("preferred_ranges: [1.1.1.1/32]")
  265. assert.Equal(t, []string{"1.1.1.1/32"}, toS(hm.GetPreferredRanges()))
  266. }
  267. func TestHostMap_RelayState(t *testing.T) {
  268. h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1}
  269. a1 := netip.MustParseAddr("::1")
  270. a2 := netip.MustParseAddr("2001::1")
  271. h1.relayState.InsertRelayTo(a1)
  272. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  273. h1.relayState.InsertRelayTo(a2)
  274. assert.Equal(t, []netip.Addr{a1, a2}, h1.relayState.relays)
  275. // Ensure that the first relay added is the first one returned in the copy
  276. currentRelays := h1.relayState.CopyRelayIps()
  277. require.Len(t, currentRelays, 2)
  278. assert.Equal(t, a1, currentRelays[0])
  279. // Deleting the last one in the list works ok
  280. h1.relayState.DeleteRelay(a2)
  281. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  282. // Deleting an element not in the list works ok
  283. h1.relayState.DeleteRelay(a2)
  284. assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays)
  285. // Deleting the only element in the list works ok
  286. h1.relayState.DeleteRelay(a1)
  287. assert.Equal(t, []netip.Addr{}, h1.relayState.relays)
  288. }