route_network_windows.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2018 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. package backend
  15. import (
  16. "sync"
  17. "time"
  18. log "github.com/golang/glog"
  19. "golang.org/x/net/context"
  20. "github.com/coreos/flannel/pkg/routing"
  21. "github.com/coreos/flannel/subnet"
  22. "strings"
  23. )
  24. const (
  25. routeCheckRetries = 10
  26. )
  27. type RouteNetwork struct {
  28. SimpleNetwork
  29. Name string
  30. BackendType string
  31. SM subnet.Manager
  32. GetRoute func(lease *subnet.Lease) *routing.Route
  33. Mtu int
  34. LinkIndex int
  35. routes []routing.Route
  36. }
  37. func (n *RouteNetwork) MTU() int {
  38. return n.Mtu
  39. }
  40. func (n *RouteNetwork) Run(ctx context.Context) {
  41. wg := sync.WaitGroup{}
  42. log.Info("Watching for new subnet leases")
  43. evts := make(chan []subnet.Event)
  44. wg.Add(1)
  45. go func() {
  46. subnet.WatchLeases(ctx, n.SM, n.SubnetLease, evts)
  47. wg.Done()
  48. }()
  49. n.routes = make([]routing.Route, 0, 10)
  50. wg.Add(1)
  51. go func() {
  52. n.routeCheck(ctx)
  53. wg.Done()
  54. }()
  55. defer wg.Wait()
  56. for {
  57. select {
  58. case evtBatch := <-evts:
  59. n.handleSubnetEvents(evtBatch)
  60. case <-ctx.Done():
  61. return
  62. }
  63. }
  64. }
  65. func (n *RouteNetwork) handleSubnetEvents(batch []subnet.Event) {
  66. router := routing.RouterWindows{}
  67. for _, evt := range batch {
  68. leaseSubnet := evt.Lease.Subnet
  69. leaseAttrs := evt.Lease.Attrs
  70. if !strings.EqualFold(leaseAttrs.BackendType, n.BackendType) {
  71. log.Warningf("Ignoring non-%v subnet(%v): type=%v", n.BackendType, leaseSubnet, leaseAttrs.BackendType)
  72. continue
  73. }
  74. expectedRoute := n.GetRoute(&evt.Lease)
  75. switch evt.Type {
  76. case subnet.EventAdded:
  77. log.Infof("Subnet added: %v via %v", leaseSubnet, leaseAttrs.PublicIP)
  78. existingRoutes, _ := router.GetRoutesFromInterfaceToSubnet(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet)
  79. if len(existingRoutes) > 0 {
  80. existingRoute := existingRoutes[0]
  81. if existingRoute.Equal(*expectedRoute) {
  82. continue
  83. }
  84. log.Warningf("Replacing existing route %v via %v with %v via %v", leaseSubnet, existingRoute.GatewayAddress, leaseSubnet, leaseAttrs.PublicIP)
  85. err := router.DeleteRoute(existingRoute.InterfaceIndex, existingRoute.DestinationSubnet, existingRoute.GatewayAddress)
  86. if err != nil {
  87. log.Errorf("Error removing route: %v", err)
  88. continue
  89. }
  90. }
  91. err := router.CreateRoute(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress)
  92. if err != nil {
  93. log.Errorf("Error creating route: %v", err)
  94. continue
  95. }
  96. n.addToRouteList(expectedRoute)
  97. case subnet.EventRemoved:
  98. log.Infof("Subnet removed: %v", leaseSubnet)
  99. existingRoutes, _ := router.GetRoutesFromInterfaceToSubnet(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet)
  100. if len(existingRoutes) > 0 {
  101. existingRoute := existingRoutes[0]
  102. if existingRoute.Equal(*expectedRoute) {
  103. log.Infof("Removing existing route %v via %v", leaseSubnet, existingRoute.GatewayAddress)
  104. err := router.DeleteRoute(existingRoute.InterfaceIndex, existingRoute.DestinationSubnet, existingRoute.GatewayAddress)
  105. if err != nil {
  106. log.Warningf("Error removing route: %v", err)
  107. }
  108. }
  109. }
  110. n.removeFromRouteList(expectedRoute)
  111. default:
  112. log.Error("Internal error: unknown event type: ", int(evt.Type))
  113. }
  114. }
  115. }
  116. func (n *RouteNetwork) addToRouteList(newRoute *routing.Route) {
  117. for _, route := range n.routes {
  118. if route.Equal(*newRoute) {
  119. return
  120. }
  121. }
  122. n.routes = append(n.routes, *newRoute)
  123. }
  124. func (n *RouteNetwork) removeFromRouteList(oldRoute *routing.Route) {
  125. for index, route := range n.routes {
  126. if route.Equal(*oldRoute) {
  127. n.routes = append(n.routes[:index], n.routes[index+1:]...)
  128. return
  129. }
  130. }
  131. }
  132. func (n *RouteNetwork) routeCheck(ctx context.Context) {
  133. for {
  134. select {
  135. case <-ctx.Done():
  136. return
  137. case <-time.After(routeCheckRetries * time.Second):
  138. n.checkSubnetExistInRoutes()
  139. }
  140. }
  141. }
  142. func (n *RouteNetwork) checkSubnetExistInRoutes() {
  143. router := routing.RouterWindows{}
  144. existingRoutes, err := router.GetAllRoutes()
  145. if err != nil {
  146. log.Errorf("Error enumerating routes: %v", err)
  147. return
  148. }
  149. for _, expectedRoute := range n.routes {
  150. exist := false
  151. for _, existingRoute := range existingRoutes {
  152. if expectedRoute.Equal(existingRoute) {
  153. exist = true
  154. break
  155. }
  156. }
  157. if !exist {
  158. err := router.CreateRoute(expectedRoute.InterfaceIndex, expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress)
  159. if err != nil {
  160. log.Warningf("Error recovering route to %v via %v on %v (%v).", expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress, expectedRoute.InterfaceIndex, err)
  161. continue
  162. }
  163. log.Infof("Recovered route to %v via %v on %v.", expectedRoute.DestinationSubnet, expectedRoute.GatewayAddress, expectedRoute.InterfaceIndex)
  164. }
  165. }
  166. }