network.go 6.8 KB

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