network.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. for _, fdbEntry := range fdbTable {
  138. log.V(1).Infof("fdb already populated with: %s %s ", fdbEntry.IP, fdbEntry.HardwareAddr)
  139. }
  140. evtMarker := make([]bool, len(batch))
  141. leaseAttrsList := make([]vxlanLeaseAttrs, len(batch))
  142. fdbEntryMarker := make([]bool, len(fdbTable))
  143. for i, evt := range batch {
  144. if evt.Lease.Attrs.BackendType != "vxlan" {
  145. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  146. evtMarker[i] = true
  147. continue
  148. }
  149. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &leaseAttrsList[i]); err != nil {
  150. log.Error("Error decoding subnet lease JSON: ", err)
  151. evtMarker[i] = true
  152. continue
  153. }
  154. for j, fdbEntry := range fdbTable {
  155. if evt.Lease.Attrs.PublicIP.ToIP().Equal(fdbEntry.IP) && bytes.Equal([]byte(leaseAttrsList[i].VtepMAC), []byte(fdbEntry.HardwareAddr)) {
  156. evtMarker[i] = true
  157. fdbEntryMarker[j] = true
  158. break
  159. }
  160. }
  161. n.rts.set(evt.Lease.Subnet, net.HardwareAddr(leaseAttrsList[i].VtepMAC))
  162. }
  163. for j, marker := range fdbEntryMarker {
  164. if !marker && fdbTable[j].IP != nil {
  165. err := n.dev.DelL2(neigh{IP: ip.FromIP(fdbTable[j].IP), MAC: fdbTable[j].HardwareAddr})
  166. if err != nil {
  167. log.Error("Delete L2 failed: ", err)
  168. }
  169. }
  170. }
  171. for i, marker := range evtMarker {
  172. if !marker {
  173. err := n.dev.AddL2(neigh{IP: batch[i].Lease.Attrs.PublicIP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  174. if err != nil {
  175. log.Error("Add L2 failed: ", err)
  176. }
  177. }
  178. }
  179. return nil
  180. }
  181. func (n *network) handleMiss(miss *netlink.Neigh) {
  182. switch {
  183. case len(miss.IP) == 0 && len(miss.HardwareAddr) == 0:
  184. log.V(2).Info("Ignoring nil miss")
  185. case len(miss.HardwareAddr) == 0:
  186. n.handleL3Miss(miss)
  187. default:
  188. log.V(2).Infof("Ignoring not a miss: %v, %v", miss.HardwareAddr, miss.IP)
  189. }
  190. }
  191. func (n *network) handleL3Miss(miss *netlink.Neigh) {
  192. log.V(2).Infof("L3 miss: %v", miss.IP)
  193. rt := n.rts.findByNetwork(ip.FromIP(miss.IP))
  194. if rt == nil {
  195. log.V(0).Infof("Route for %v not found", miss.IP)
  196. return
  197. }
  198. if err := n.dev.AddL3(neigh{IP: ip.FromIP(miss.IP), MAC: rt.vtepMAC}); err != nil {
  199. log.Errorf("AddL3 failed: %v", err)
  200. } else {
  201. log.V(2).Info("AddL3 succeeded")
  202. }
  203. }