network.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Info("Watching for L3 misses")
  51. misses := make(chan *netlink.Neigh, 100)
  52. // Unfrtunately 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.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.Info("WatchLeases exited")
  62. wg.Done()
  63. }()
  64. defer wg.Wait()
  65. initialEvtsBatch := <-evts
  66. for {
  67. err := n.handleInitialSubnetEvents(initialEvtsBatch)
  68. if err == nil {
  69. break
  70. }
  71. log.Error(err, " About to retry")
  72. time.Sleep(time.Second)
  73. }
  74. for {
  75. select {
  76. case miss := <-misses:
  77. n.handleMiss(miss)
  78. case evtBatch := <-evts:
  79. n.handleSubnetEvents(evtBatch)
  80. case <-ctx.Done():
  81. return
  82. }
  83. }
  84. }
  85. func (n *network) MTU() int {
  86. return n.dev.MTU()
  87. }
  88. type vxlanLeaseAttrs struct {
  89. VtepMAC hardwareAddr
  90. }
  91. func (n *network) handleSubnetEvents(batch []subnet.Event) {
  92. for _, evt := range batch {
  93. switch evt.Type {
  94. case subnet.EventAdded:
  95. log.Info("Subnet added: ", evt.Lease.Subnet)
  96. if evt.Lease.Attrs.BackendType != "vxlan" {
  97. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  98. continue
  99. }
  100. var attrs vxlanLeaseAttrs
  101. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  102. log.Error("Error decoding subnet lease JSON: ", err)
  103. continue
  104. }
  105. n.rts.set(evt.Lease.Subnet, net.HardwareAddr(attrs.VtepMAC))
  106. n.dev.AddL2(neigh{IP: evt.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  107. case subnet.EventRemoved:
  108. log.Info("Subnet removed: ", evt.Lease.Subnet)
  109. if evt.Lease.Attrs.BackendType != "vxlan" {
  110. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  111. continue
  112. }
  113. var attrs vxlanLeaseAttrs
  114. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  115. log.Error("Error decoding subnet lease JSON: ", err)
  116. continue
  117. }
  118. if len(attrs.VtepMAC) > 0 {
  119. n.dev.DelL2(neigh{IP: evt.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  120. }
  121. n.rts.remove(evt.Lease.Subnet)
  122. default:
  123. log.Error("Internal error: unknown event type: ", int(evt.Type))
  124. }
  125. }
  126. }
  127. func (n *network) handleInitialSubnetEvents(batch []subnet.Event) error {
  128. log.Infof("Handling initial subnet events")
  129. fdbTable, err := n.dev.GetL2List()
  130. if err != nil {
  131. return fmt.Errorf("error fetching L2 table: %v", err)
  132. }
  133. for _, fdbEntry := range fdbTable {
  134. log.Infof("fdb already populated with: %s %s ", fdbEntry.IP, fdbEntry.HardwareAddr)
  135. }
  136. evtMarker := make([]bool, len(batch))
  137. leaseAttrsList := make([]vxlanLeaseAttrs, len(batch))
  138. fdbEntryMarker := make([]bool, len(fdbTable))
  139. for i, evt := range batch {
  140. if evt.Lease.Attrs.BackendType != "vxlan" {
  141. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  142. evtMarker[i] = true
  143. continue
  144. }
  145. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &leaseAttrsList[i]); err != nil {
  146. log.Error("Error decoding subnet lease JSON: ", err)
  147. evtMarker[i] = true
  148. continue
  149. }
  150. for j, fdbEntry := range fdbTable {
  151. if evt.Lease.Attrs.PublicIP.ToIP().Equal(fdbEntry.IP) && bytes.Equal([]byte(leaseAttrsList[i].VtepMAC), []byte(fdbEntry.HardwareAddr)) {
  152. evtMarker[i] = true
  153. fdbEntryMarker[j] = true
  154. break
  155. }
  156. }
  157. n.rts.set(evt.Lease.Subnet, net.HardwareAddr(leaseAttrsList[i].VtepMAC))
  158. }
  159. for j, marker := range fdbEntryMarker {
  160. if !marker && fdbTable[j].IP != nil {
  161. err := n.dev.DelL2(neigh{IP: ip.FromIP(fdbTable[j].IP), MAC: fdbTable[j].HardwareAddr})
  162. if err != nil {
  163. log.Error("Delete L2 failed: ", err)
  164. }
  165. }
  166. }
  167. for i, marker := range evtMarker {
  168. if !marker {
  169. err := n.dev.AddL2(neigh{IP: batch[i].Lease.Attrs.PublicIP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  170. if err != nil {
  171. log.Error("Add L2 failed: ", err)
  172. }
  173. }
  174. }
  175. return nil
  176. }
  177. func (n *network) handleMiss(miss *netlink.Neigh) {
  178. switch {
  179. case len(miss.IP) == 0 && len(miss.HardwareAddr) == 0:
  180. log.Info("Ignoring nil miss")
  181. case len(miss.HardwareAddr) == 0:
  182. n.handleL3Miss(miss)
  183. default:
  184. log.Infof("Ignoring not a miss: %v, %v", miss.HardwareAddr, miss.IP)
  185. }
  186. }
  187. func (n *network) handleL3Miss(miss *netlink.Neigh) {
  188. log.Infof("L3 miss: %v", miss.IP)
  189. rt := n.rts.findByNetwork(ip.FromIP(miss.IP))
  190. if rt == nil {
  191. log.Infof("Route for %v not found", miss.IP)
  192. return
  193. }
  194. if err := n.dev.AddL3(neigh{IP: ip.FromIP(miss.IP), MAC: rt.vtepMAC}); err != nil {
  195. log.Errorf("AddL3 failed: %v", err)
  196. } else {
  197. log.Info("AddL3 succeeded")
  198. }
  199. }