hostgw.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2015 CoreOS, Inc.
  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 hostgw
  15. import (
  16. "bytes"
  17. "fmt"
  18. "net"
  19. "sync"
  20. "time"
  21. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  22. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/vishvananda/netlink"
  23. "github.com/coreos/flannel/backend"
  24. "github.com/coreos/flannel/pkg/ip"
  25. "github.com/coreos/flannel/pkg/task"
  26. "github.com/coreos/flannel/subnet"
  27. )
  28. const (
  29. routeCheckRetries = 10
  30. )
  31. type HostgwBackend struct {
  32. sm *subnet.SubnetManager
  33. extIface *net.Interface
  34. extIP net.IP
  35. stop chan bool
  36. wg sync.WaitGroup
  37. rl []netlink.Route
  38. }
  39. func New(sm *subnet.SubnetManager) backend.Backend {
  40. b := &HostgwBackend{
  41. sm: sm,
  42. stop: make(chan bool),
  43. }
  44. return b
  45. }
  46. func (rb *HostgwBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
  47. rb.extIface = extIface
  48. rb.extIP = extIP
  49. attrs := subnet.LeaseAttrs{
  50. PublicIP: ip.FromIP(extIP),
  51. BackendType: "host-gw",
  52. }
  53. sn, err := rb.sm.AcquireLease(&attrs, rb.stop)
  54. if err != nil {
  55. if err == task.ErrCanceled {
  56. return nil, err
  57. } else {
  58. return nil, fmt.Errorf("Failed to acquire lease: %v", err)
  59. }
  60. }
  61. /* NB: docker will create the local route to `sn` */
  62. return &backend.SubnetDef{
  63. Net: sn,
  64. MTU: extIface.MTU,
  65. }, nil
  66. }
  67. func (rb *HostgwBackend) Run() {
  68. rb.wg.Add(1)
  69. go func() {
  70. rb.sm.LeaseRenewer(rb.stop)
  71. rb.wg.Done()
  72. }()
  73. log.Info("Watching for new subnet leases")
  74. evts := make(chan subnet.EventBatch)
  75. rb.wg.Add(1)
  76. go func() {
  77. rb.sm.WatchLeases(evts, rb.stop)
  78. rb.wg.Done()
  79. }()
  80. rb.rl = make([]netlink.Route, 0, 10)
  81. rb.wg.Add(1)
  82. go func() {
  83. rb.routeCheck(rb.stop)
  84. rb.wg.Done()
  85. }()
  86. defer rb.wg.Wait()
  87. for {
  88. select {
  89. case evtBatch := <-evts:
  90. rb.handleSubnetEvents(evtBatch)
  91. case <-rb.stop:
  92. return
  93. }
  94. }
  95. }
  96. func (rb *HostgwBackend) Stop() {
  97. close(rb.stop)
  98. }
  99. func (rb *HostgwBackend) Name() string {
  100. return "host-gw"
  101. }
  102. func (rb *HostgwBackend) handleSubnetEvents(batch subnet.EventBatch) {
  103. for _, evt := range batch {
  104. switch evt.Type {
  105. case subnet.SubnetAdded:
  106. log.Infof("Subnet added: %v via %v", evt.Lease.Network, evt.Lease.Attrs.PublicIP)
  107. if evt.Lease.Attrs.BackendType != "host-gw" {
  108. log.Warningf("Ignoring non-host-gw subnet: type=%v", evt.Lease.Attrs.BackendType)
  109. continue
  110. }
  111. route := netlink.Route{
  112. Dst: evt.Lease.Network.ToIPNet(),
  113. Gw: evt.Lease.Attrs.PublicIP.ToIP(),
  114. LinkIndex: rb.extIface.Index,
  115. }
  116. if err := netlink.RouteAdd(&route); err != nil {
  117. log.Errorf("Error adding route to %v via %v: %v", evt.Lease.Network, evt.Lease.Attrs.PublicIP, err)
  118. continue
  119. }
  120. rb.addToRouteList(route)
  121. case subnet.SubnetRemoved:
  122. log.Info("Subnet removed: ", evt.Lease.Network)
  123. if evt.Lease.Attrs.BackendType != "host-gw" {
  124. log.Warningf("Ignoring non-host-gw subnet: type=%v", evt.Lease.Attrs.BackendType)
  125. continue
  126. }
  127. route := netlink.Route{
  128. Dst: evt.Lease.Network.ToIPNet(),
  129. Gw: evt.Lease.Attrs.PublicIP.ToIP(),
  130. LinkIndex: rb.extIface.Index,
  131. }
  132. if err := netlink.RouteDel(&route); err != nil {
  133. log.Errorf("Error deleting route to %v: %v", evt.Lease.Network, err)
  134. continue
  135. }
  136. rb.removeFromRouteList(route)
  137. default:
  138. log.Error("Internal error: unknown event type: ", int(evt.Type))
  139. }
  140. }
  141. }
  142. func (rb *HostgwBackend) addToRouteList(route netlink.Route) {
  143. rb.rl = append(rb.rl, route)
  144. }
  145. func (rb *HostgwBackend) removeFromRouteList(route netlink.Route) {
  146. for index, r := range rb.rl {
  147. if routeEqual(r, route) {
  148. rb.rl = append(rb.rl[:index], rb.rl[index+1:]...)
  149. return
  150. }
  151. }
  152. }
  153. func (rb *HostgwBackend) routeCheck(cancel chan bool) {
  154. for {
  155. select {
  156. case <-cancel:
  157. return
  158. case <-time.After(routeCheckRetries * time.Second):
  159. rb.checkSubnetExistInRoutes()
  160. }
  161. }
  162. }
  163. func (rb *HostgwBackend) checkSubnetExistInRoutes() {
  164. routeList, err := netlink.RouteList(nil, netlink.FAMILY_V4)
  165. if err == nil {
  166. for _, route := range rb.rl {
  167. exist := false
  168. for _, r := range routeList {
  169. if r.Dst == nil {
  170. continue
  171. }
  172. if routeEqual(r, route) {
  173. exist = true
  174. break
  175. }
  176. }
  177. if !exist {
  178. if err := netlink.RouteAdd(&route); err != nil {
  179. if nerr, ok := err.(net.Error); !ok {
  180. log.Errorf("Error recovering route to %v: %v, %v", route.Dst, route.Gw, nerr)
  181. }
  182. continue
  183. } else {
  184. log.Infof("Route recovered %v : %v", route.Dst, route.Gw)
  185. }
  186. }
  187. }
  188. }
  189. }
  190. func routeEqual(x, y netlink.Route) bool {
  191. if x.Dst.IP.Equal(y.Dst.IP) && x.Gw.Equal(y.Gw) && bytes.Equal(x.Dst.Mask, y.Dst.Mask) {
  192. return true
  193. }
  194. return false
  195. }