route_network_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2017 flannel authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build !windows
  15. package backend
  16. import (
  17. "net"
  18. "testing"
  19. "github.com/flannel-io/flannel/pkg/ip"
  20. "github.com/flannel-io/flannel/pkg/ns"
  21. "github.com/flannel-io/flannel/subnet"
  22. "github.com/vishvananda/netlink"
  23. )
  24. func TestRouteCache(t *testing.T) {
  25. teardown := ns.SetUpNetlinkTest(t)
  26. defer teardown()
  27. lo, err := netlink.LinkByName("lo")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if err := netlink.AddrAdd(lo, &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP("127.0.0.1"), Mask: net.CIDRMask(32, 32)}}); err != nil {
  32. t.Fatal(err)
  33. }
  34. if err := netlink.LinkSetUp(lo); err != nil {
  35. t.Fatal(err)
  36. }
  37. nw := RouteNetwork{
  38. SimpleNetwork: SimpleNetwork{
  39. ExtIface: &ExternalInterface{Iface: &net.Interface{Index: lo.Attrs().Index}},
  40. },
  41. BackendType: "host-gw",
  42. LinkIndex: lo.Attrs().Index,
  43. }
  44. nw.GetRoute = func(lease *subnet.Lease) *netlink.Route {
  45. return &netlink.Route{
  46. Dst: lease.Subnet.ToIPNet(),
  47. Gw: lease.Attrs.PublicIP.ToIP(),
  48. LinkIndex: nw.LinkIndex,
  49. }
  50. }
  51. gw1, gw2 := ip.FromIP(net.ParseIP("127.0.0.1")), ip.FromIP(net.ParseIP("127.0.0.2"))
  52. subnet1 := ip.IP4Net{IP: ip.FromIP(net.ParseIP("192.168.0.0")), PrefixLen: 24}
  53. nw.handleSubnetEvents([]subnet.Event{
  54. {Type: subnet.EventAdded, Lease: subnet.Lease{
  55. Subnet: subnet1, EnableIPv4: true, Attrs: subnet.LeaseAttrs{PublicIP: gw1, BackendType: "host-gw"}}},
  56. })
  57. if len(nw.routes) != 1 {
  58. t.Fatal(nw.routes)
  59. }
  60. if !routeEqual(nw.routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw1.ToIP(), LinkIndex: lo.Attrs().Index}) {
  61. t.Fatal(nw.routes[0])
  62. }
  63. // change gateway of previous route
  64. nw.handleSubnetEvents([]subnet.Event{
  65. {Type: subnet.EventAdded, Lease: subnet.Lease{
  66. Subnet: subnet1, EnableIPv4: true, Attrs: subnet.LeaseAttrs{PublicIP: gw2, BackendType: "host-gw"}}}})
  67. if len(nw.routes) != 1 {
  68. t.Fatal(nw.routes)
  69. }
  70. if !routeEqual(nw.routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw2.ToIP(), LinkIndex: lo.Attrs().Index}) {
  71. t.Fatal(nw.routes[0])
  72. }
  73. }
  74. func TestV6RouteCache(t *testing.T) {
  75. teardown := ns.SetUpNetlinkTest(t)
  76. defer teardown()
  77. la := netlink.NewLinkAttrs()
  78. la.Name = "br"
  79. br := &netlink.Bridge{LinkAttrs: la}
  80. if err := netlink.LinkAdd(br); err != nil {
  81. t.Fatal(err)
  82. }
  83. if err := netlink.AddrAdd(br, &netlink.Addr{IPNet: &net.IPNet{IP: net.ParseIP("2001:db8:1::1"), Mask: net.CIDRMask(64, 128)}}); err != nil {
  84. t.Fatal(err)
  85. }
  86. if err := netlink.LinkSetUp(br); err != nil {
  87. t.Fatal(err)
  88. }
  89. nw := RouteNetwork{
  90. SimpleNetwork: SimpleNetwork{
  91. ExtIface: &ExternalInterface{Iface: &net.Interface{Index: br.Attrs().Index}},
  92. },
  93. BackendType: "host-gw",
  94. LinkIndex: br.Attrs().Index,
  95. }
  96. nw.GetV6Route = func(lease *subnet.Lease) *netlink.Route {
  97. return &netlink.Route{
  98. Dst: lease.IPv6Subnet.ToIPNet(),
  99. Gw: lease.Attrs.PublicIPv6.ToIP(),
  100. LinkIndex: nw.LinkIndex,
  101. }
  102. }
  103. gw1, gw2 := ip.FromIP6(net.ParseIP("2001:db8:1::2")), ip.FromIP6(net.ParseIP("::2"))
  104. subnet1 := ip.IP6Net{IP: ip.FromIP6(net.ParseIP("2001:db8:ffff::")), PrefixLen: 64}
  105. nw.handleSubnetEvents([]subnet.Event{
  106. {Type: subnet.EventAdded, Lease: subnet.Lease{
  107. IPv6Subnet: subnet1, EnableIPv6: true, Attrs: subnet.LeaseAttrs{PublicIPv6: gw1, BackendType: "host-gw"}}},
  108. })
  109. if len(nw.v6Routes) != 1 {
  110. t.Fatal(nw.v6Routes)
  111. }
  112. if !routeEqual(nw.v6Routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw1.ToIP(), LinkIndex: br.Attrs().Index}) {
  113. t.Fatal(nw.v6Routes[0])
  114. }
  115. // change gateway of previous route
  116. nw.handleSubnetEvents([]subnet.Event{
  117. {Type: subnet.EventAdded, Lease: subnet.Lease{
  118. IPv6Subnet: subnet1, EnableIPv6: true, Attrs: subnet.LeaseAttrs{PublicIPv6: gw2, BackendType: "host-gw"}}}})
  119. if len(nw.v6Routes) != 1 {
  120. t.Fatal(nw.v6Routes)
  121. }
  122. if !routeEqual(nw.v6Routes[0], netlink.Route{Dst: subnet1.ToIPNet(), Gw: gw2.ToIP(), LinkIndex: br.Attrs().Index}) {
  123. t.Fatal(nw.v6Routes[0])
  124. }
  125. }