vxlan_network.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. package vxlan
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "net"
  20. "sync"
  21. "time"
  22. log "github.com/golang/glog"
  23. "github.com/vishvananda/netlink"
  24. "golang.org/x/net/context"
  25. "github.com/coreos/flannel/backend"
  26. "github.com/coreos/flannel/pkg/ip"
  27. "github.com/coreos/flannel/subnet"
  28. )
  29. type network struct {
  30. backend.SimpleNetwork
  31. extIface *backend.ExternalInterface
  32. dev *vxlanDevice
  33. routes routes
  34. subnetMgr subnet.Manager
  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. log.V(0).Info("Watching for L3 misses")
  49. misses := make(chan *netlink.Neigh, 100)
  50. // Unfortunately MonitorMisses does not take a cancel channel
  51. // as there's no wait to interrupt netlink socket recv
  52. go nw.dev.MonitorMisses(misses)
  53. wg := sync.WaitGroup{}
  54. log.V(0).Info("Watching for new subnet leases")
  55. events := make(chan []subnet.Event)
  56. wg.Add(1)
  57. go func() {
  58. subnet.WatchLeases(ctx, nw.subnetMgr, nw.SubnetLease, events)
  59. log.V(1).Info("WatchLeases exited")
  60. wg.Done()
  61. }()
  62. defer wg.Wait()
  63. select {
  64. case initialEventsBatch := <-events:
  65. for {
  66. err := nw.handleInitialSubnetEvents(initialEventsBatch)
  67. if err == nil {
  68. break
  69. }
  70. log.Error(err, " About to retry")
  71. time.Sleep(time.Second)
  72. }
  73. case <-ctx.Done():
  74. return
  75. }
  76. for {
  77. select {
  78. case miss := <-misses:
  79. nw.handleMiss(miss)
  80. case evtBatch := <-events:
  81. nw.handleSubnetEvents(evtBatch)
  82. case <-ctx.Done():
  83. return
  84. }
  85. }
  86. }
  87. func (nw *network) MTU() int {
  88. return nw.dev.MTU()
  89. }
  90. type vxlanLeaseAttrs struct {
  91. VtepMAC hardwareAddr
  92. }
  93. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  94. for _, event := range batch {
  95. switch event.Type {
  96. case subnet.EventAdded:
  97. log.V(1).Info("Subnet added: ", event.Lease.Subnet)
  98. if event.Lease.Attrs.BackendType != "vxlan" {
  99. log.Warningf("Ignoring non-vxlan subnet: type=%v", event.Lease.Attrs.BackendType)
  100. continue
  101. }
  102. var attrs vxlanLeaseAttrs
  103. if err := json.Unmarshal(event.Lease.Attrs.BackendData, &attrs); err != nil {
  104. log.Error("Error decoding subnet lease JSON: ", err)
  105. continue
  106. }
  107. nw.routes.set(event.Lease.Subnet, net.HardwareAddr(attrs.VtepMAC))
  108. nw.dev.AddL2(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  109. case subnet.EventRemoved:
  110. log.V(1).Info("Subnet removed: ", event.Lease.Subnet)
  111. if event.Lease.Attrs.BackendType != "vxlan" {
  112. log.Warningf("Ignoring non-vxlan subnet: type=%v", event.Lease.Attrs.BackendType)
  113. continue
  114. }
  115. var attrs vxlanLeaseAttrs
  116. if err := json.Unmarshal(event.Lease.Attrs.BackendData, &attrs); err != nil {
  117. log.Error("Error decoding subnet lease JSON: ", err)
  118. continue
  119. }
  120. if len(attrs.VtepMAC) > 0 {
  121. nw.dev.DelL2(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  122. }
  123. nw.routes.remove(event.Lease.Subnet)
  124. default:
  125. log.Error("Internal error: unknown event type: ", int(event.Type))
  126. }
  127. }
  128. }
  129. func (nw *network) handleInitialSubnetEvents(batch []subnet.Event) error {
  130. log.V(1).Infof("Handling initial subnet events")
  131. fdbTable, err := nw.dev.GetL2List()
  132. if err != nil {
  133. return fmt.Errorf("error fetching L2 table: %v", err)
  134. }
  135. // Log the existing VTEP -> Public IP mappings
  136. for _, fdbEntry := range fdbTable {
  137. log.V(1).Infof("fdb already populated with: %s %s ", fdbEntry.IP, fdbEntry.HardwareAddr)
  138. }
  139. // "marked" events are skipped at the end.
  140. eventMarker := make([]bool, len(batch))
  141. leaseAttrsList := make([]vxlanLeaseAttrs, len(batch))
  142. fdbEntryMarker := make([]bool, len(fdbTable))
  143. // Run through the events "marking" ones that should be skipped
  144. for eventMarkerIndex, evt := range batch {
  145. if evt.Lease.Attrs.BackendType != "vxlan" {
  146. log.Warningf("Ignoring non-vxlan subnet(%s): type=%v", evt.Lease.Subnet, evt.Lease.Attrs.BackendType)
  147. eventMarker[eventMarkerIndex] = true
  148. continue
  149. }
  150. // Parse the vxlan specific backend data
  151. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &leaseAttrsList[eventMarkerIndex]); err != nil {
  152. log.Error("Error decoding subnet lease JSON: ", err)
  153. eventMarker[eventMarkerIndex] = true
  154. continue
  155. }
  156. // Check the existing VTEP->Public IP mappings.
  157. // If there's already an entry with the right VTEP and Public IP then the event can be skipped and the FDB entry can be retained
  158. for j, fdbEntry := range fdbTable {
  159. if evt.Lease.Attrs.PublicIP.ToIP().Equal(fdbEntry.IP) && bytes.Equal([]byte(leaseAttrsList[eventMarkerIndex].VtepMAC), []byte(fdbEntry.HardwareAddr)) {
  160. eventMarker[eventMarkerIndex] = true
  161. fdbEntryMarker[j] = true
  162. break
  163. }
  164. }
  165. // Store off the subnet lease and VTEP
  166. nw.routes.set(evt.Lease.Subnet, net.HardwareAddr(leaseAttrsList[eventMarkerIndex].VtepMAC))
  167. log.V(2).Infof("Adding subnet: %s PublicIP: %s VtepMAC: %s", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP, net.HardwareAddr(leaseAttrsList[eventMarkerIndex].VtepMAC))
  168. }
  169. // Loop over the existing FDB entries, deleting any that shouldn't be there
  170. for j, marker := range fdbEntryMarker {
  171. if !marker && fdbTable[j].IP != nil {
  172. err := nw.dev.DelL2(neighbor{IP: ip.FromIP(fdbTable[j].IP), MAC: fdbTable[j].HardwareAddr})
  173. if err != nil {
  174. log.Error("Delete L2 failed: ", err)
  175. }
  176. }
  177. }
  178. // Loop over the events (skipping marked ones), adding them to the FDB table.
  179. for i, marker := range eventMarker {
  180. if !marker {
  181. err := nw.dev.AddL2(neighbor{IP: batch[i].Lease.Attrs.PublicIP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  182. if err != nil {
  183. log.Error("Add L2 failed: ", err)
  184. }
  185. }
  186. }
  187. return nil
  188. }
  189. func (nw *network) handleMiss(miss *netlink.Neigh) {
  190. switch {
  191. case len(miss.IP) == 0 && len(miss.HardwareAddr) == 0:
  192. log.V(2).Info("Ignoring nil miss")
  193. case len(miss.HardwareAddr) == 0:
  194. nw.handleL3Miss(miss)
  195. default:
  196. log.V(4).Infof("Ignoring not a miss: %v, %v", miss.HardwareAddr, miss.IP)
  197. }
  198. }
  199. func (nw *network) handleL3Miss(miss *netlink.Neigh) {
  200. route := nw.routes.findByNetwork(ip.FromIP(miss.IP))
  201. if route == nil {
  202. log.V(0).Infof("L3 miss but route for %v not found", miss.IP)
  203. return
  204. }
  205. if err := nw.dev.AddL3(neighbor{IP: ip.FromIP(miss.IP), MAC: route.vtepMAC}); err != nil {
  206. log.Errorf("AddL3 failed: %v", err)
  207. } else {
  208. log.V(2).Infof("L3 miss: AddL3 for %s succeeded", miss.IP)
  209. }
  210. }