vxlan_network.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2015 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 vxlan
  16. import (
  17. "encoding/json"
  18. "net"
  19. "sync"
  20. log "github.com/golang/glog"
  21. "github.com/vishvananda/netlink"
  22. "golang.org/x/net/context"
  23. "syscall"
  24. "github.com/coreos/flannel/backend"
  25. "github.com/coreos/flannel/pkg/ip"
  26. "github.com/coreos/flannel/subnet"
  27. )
  28. type network struct {
  29. backend.SimpleNetwork
  30. dev *vxlanDevice
  31. subnetMgr subnet.Manager
  32. }
  33. const (
  34. encapOverhead = 50
  35. )
  36. func newNetwork(subnetMgr subnet.Manager, extIface *backend.ExternalInterface, dev *vxlanDevice, _ ip.IP4Net, lease *subnet.Lease) (*network, error) {
  37. nw := &network{
  38. SimpleNetwork: backend.SimpleNetwork{
  39. SubnetLease: lease,
  40. ExtIface: extIface,
  41. },
  42. subnetMgr: subnetMgr,
  43. dev: dev,
  44. }
  45. return nw, nil
  46. }
  47. func (nw *network) Run(ctx context.Context) {
  48. wg := sync.WaitGroup{}
  49. log.V(0).Info("watching for new subnet leases")
  50. events := make(chan []subnet.Event)
  51. wg.Add(1)
  52. go func() {
  53. subnet.WatchLeases(ctx, nw.subnetMgr, nw.SubnetLease, events)
  54. log.V(1).Info("WatchLeases exited")
  55. wg.Done()
  56. }()
  57. defer wg.Wait()
  58. for {
  59. select {
  60. case evtBatch := <-events:
  61. nw.handleSubnetEvents(evtBatch)
  62. case <-ctx.Done():
  63. return
  64. }
  65. }
  66. }
  67. func (nw *network) MTU() int {
  68. return nw.ExtIface.Iface.MTU - encapOverhead
  69. }
  70. type vxlanLeaseAttrs struct {
  71. VtepMAC hardwareAddr
  72. }
  73. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  74. for _, event := range batch {
  75. sn := event.Lease.Subnet
  76. attrs := event.Lease.Attrs
  77. if attrs.BackendType != "vxlan" {
  78. log.Warningf("ignoring non-vxlan subnet(%s): type=%v", sn, attrs.BackendType)
  79. continue
  80. }
  81. var vxlanAttrs vxlanLeaseAttrs
  82. if err := json.Unmarshal(attrs.BackendData, &vxlanAttrs); err != nil {
  83. log.Error("error decoding subnet lease JSON: ", err)
  84. continue
  85. }
  86. // This route is used when traffic should be vxlan encapsulated
  87. vxlanRoute := netlink.Route{
  88. LinkIndex: nw.dev.link.Attrs().Index,
  89. Scope: netlink.SCOPE_UNIVERSE,
  90. Dst: sn.ToIPNet(),
  91. Gw: sn.IP.ToIP(),
  92. }
  93. vxlanRoute.SetFlag(syscall.RTNH_F_ONLINK)
  94. // directRouting is where the remote host is on the same subnet so vxlan isn't required.
  95. directRoute := netlink.Route{
  96. Dst: sn.ToIPNet(),
  97. Gw: attrs.PublicIP.ToIP(),
  98. }
  99. var directRoutingOK = false
  100. if nw.dev.directRouting {
  101. if dr, err := ip.DirectRouting(attrs.PublicIP.ToIP()); err != nil {
  102. log.Error(err)
  103. } else {
  104. directRoutingOK = dr
  105. }
  106. }
  107. switch event.Type {
  108. case subnet.EventAdded:
  109. if directRoutingOK {
  110. log.V(2).Infof("Adding direct route to subnet: %s PublicIP: %s", sn, attrs.PublicIP)
  111. if err := netlink.RouteReplace(&directRoute); err != nil {
  112. log.Errorf("Error adding route to %v via %v: %v", sn, attrs.PublicIP, err)
  113. continue
  114. }
  115. } else {
  116. log.V(2).Infof("adding subnet: %s PublicIP: %s VtepMAC: %s", sn, attrs.PublicIP, net.HardwareAddr(vxlanAttrs.VtepMAC))
  117. if err := nw.dev.AddARP(neighbor{IP: sn.IP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  118. log.Error("AddARP failed: ", err)
  119. continue
  120. }
  121. if err := nw.dev.AddFDB(neighbor{IP: attrs.PublicIP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  122. log.Error("AddFDB failed: ", err)
  123. // Try to clean up the ARP entry then continue
  124. if err := nw.dev.DelARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  125. log.Error("DelARP failed: ", err)
  126. }
  127. continue
  128. }
  129. // Set the route - the kernel would ARP for the Gw IP address if it hadn't already been set above so make sure
  130. // this is done last.
  131. if err := netlink.RouteReplace(&vxlanRoute); err != nil {
  132. log.Errorf("failed to add vxlanRoute (%s -> %s): %v", vxlanRoute.Dst, vxlanRoute.Gw, err)
  133. // Try to clean up both the ARP and FDB entries then continue
  134. if err := nw.dev.DelARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  135. log.Error("DelARP failed: ", err)
  136. }
  137. if err := nw.dev.DelFDB(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  138. log.Error("DelFDB failed: ", err)
  139. }
  140. continue
  141. }
  142. }
  143. case subnet.EventRemoved:
  144. if directRoutingOK {
  145. log.V(2).Infof("Removing direct route to subnet: %s PublicIP: %s", sn, attrs.PublicIP)
  146. if err := netlink.RouteDel(&directRoute); err != nil {
  147. log.Errorf("Error deleting route to %v via %v: %v", sn, attrs.PublicIP, err)
  148. }
  149. } else {
  150. log.V(2).Infof("removing subnet: %s PublicIP: %s VtepMAC: %s", sn, attrs.PublicIP, net.HardwareAddr(vxlanAttrs.VtepMAC))
  151. // Try to remove all entries - don't bail out if one of them fails.
  152. if err := nw.dev.DelARP(neighbor{IP: sn.IP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  153. log.Error("DelARP failed: ", err)
  154. }
  155. if err := nw.dev.DelFDB(neighbor{IP: attrs.PublicIP, MAC: net.HardwareAddr(vxlanAttrs.VtepMAC)}); err != nil {
  156. log.Error("DelFDB failed: ", err)
  157. }
  158. if err := netlink.RouteDel(&vxlanRoute); err != nil {
  159. log.Errorf("failed to delete vxlanRoute (%s -> %s): %v", vxlanRoute.Dst, vxlanRoute.Gw, err)
  160. }
  161. }
  162. default:
  163. log.Error("internal error: unknown event type: ", int(event.Type))
  164. }
  165. }
  166. }