hostgw.go 5.0 KB

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