vxlan.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright 2015 CoreOS, Inc.
  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. "encoding/json"
  17. "fmt"
  18. "net"
  19. "sync"
  20. "time"
  21. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  22. "github.com/coreos/flannel/Godeps/_workspace/src/github.com/vishvananda/netlink"
  23. "github.com/coreos/flannel/backend"
  24. "github.com/coreos/flannel/pkg/ip"
  25. "github.com/coreos/flannel/pkg/task"
  26. "github.com/coreos/flannel/subnet"
  27. )
  28. const (
  29. defaultVNI = 1
  30. )
  31. type VXLANBackend struct {
  32. sm *subnet.SubnetManager
  33. rawCfg json.RawMessage
  34. cfg struct {
  35. VNI int
  36. Port int
  37. }
  38. dev *vxlanDevice
  39. stop chan bool
  40. wg sync.WaitGroup
  41. rts routes
  42. }
  43. func New(sm *subnet.SubnetManager, config json.RawMessage) backend.Backend {
  44. vb := &VXLANBackend{
  45. sm: sm,
  46. rawCfg: config,
  47. stop: make(chan bool),
  48. }
  49. vb.cfg.VNI = defaultVNI
  50. return vb
  51. }
  52. func newSubnetAttrs(pubIP net.IP, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  53. data, err := json.Marshal(&vxlanLeaseAttrs{hardwareAddr(mac)})
  54. if err != nil {
  55. return nil, err
  56. }
  57. return &subnet.LeaseAttrs{
  58. PublicIP: ip.FromIP(pubIP),
  59. BackendType: "vxlan",
  60. BackendData: json.RawMessage(data),
  61. }, nil
  62. }
  63. func (vb *VXLANBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
  64. // Parse our configuration
  65. if len(vb.rawCfg) > 0 {
  66. if err := json.Unmarshal(vb.rawCfg, &vb.cfg); err != nil {
  67. return nil, fmt.Errorf("error decoding UDP backend config: %v", err)
  68. }
  69. }
  70. devAttrs := vxlanDeviceAttrs{
  71. vni: uint32(vb.cfg.VNI),
  72. name: fmt.Sprintf("flannel.%v", vb.cfg.VNI),
  73. vtepIndex: extIface.Index,
  74. vtepAddr: extIP,
  75. vtepPort: vb.cfg.Port,
  76. }
  77. var err error
  78. for {
  79. vb.dev, err = newVXLANDevice(&devAttrs)
  80. if err == nil {
  81. break
  82. } else {
  83. log.Error("VXLAN init: ", err)
  84. log.Info("Retrying in 1 second...")
  85. // wait 1 sec before retrying
  86. time.Sleep(1 * time.Second)
  87. }
  88. }
  89. sa, err := newSubnetAttrs(extIP, vb.dev.MACAddr())
  90. if err != nil {
  91. return nil, err
  92. }
  93. sn, err := vb.sm.AcquireLease(sa, vb.stop)
  94. if err != nil {
  95. if err == task.ErrCanceled {
  96. return nil, err
  97. } else {
  98. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  99. }
  100. }
  101. // vxlan's subnet is that of the whole overlay network (e.g. /16)
  102. // and not that of the individual host (e.g. /24)
  103. vxlanNet := ip.IP4Net{
  104. IP: sn.IP,
  105. PrefixLen: vb.sm.GetConfig().Network.PrefixLen,
  106. }
  107. if err = vb.dev.Configure(vxlanNet); err != nil {
  108. return nil, err
  109. }
  110. return &backend.SubnetDef{
  111. Net: sn,
  112. MTU: vb.dev.MTU(),
  113. }, nil
  114. }
  115. func (vb *VXLANBackend) Run() {
  116. vb.wg.Add(1)
  117. go func() {
  118. vb.sm.LeaseRenewer(vb.stop)
  119. vb.wg.Done()
  120. }()
  121. log.Info("Watching for L2/L3 misses")
  122. misses := make(chan *netlink.Neigh, 100)
  123. // Unfortunately MonitorMisses does not take a cancel channel
  124. // as there's no wait to interrupt netlink socket recv
  125. go vb.dev.MonitorMisses(misses)
  126. log.Info("Watching for new subnet leases")
  127. evts := make(chan subnet.EventBatch)
  128. vb.wg.Add(1)
  129. go func() {
  130. vb.sm.WatchLeases(evts, vb.stop)
  131. vb.wg.Done()
  132. }()
  133. defer vb.wg.Wait()
  134. for {
  135. select {
  136. case miss := <-misses:
  137. vb.handleMiss(miss)
  138. case evtBatch := <-evts:
  139. vb.handleSubnetEvents(evtBatch)
  140. case <-vb.stop:
  141. return
  142. }
  143. }
  144. }
  145. func (vb *VXLANBackend) Stop() {
  146. close(vb.stop)
  147. }
  148. func (vb *VXLANBackend) Name() string {
  149. return "VXLAN"
  150. }
  151. // So we can make it JSON (un)marshalable
  152. type hardwareAddr net.HardwareAddr
  153. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  154. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  155. }
  156. func (hw *hardwareAddr) UnmarshalJSON(b []byte) error {
  157. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  158. return fmt.Errorf("error parsing hardware addr")
  159. }
  160. b = b[1 : len(b)-1]
  161. mac, err := net.ParseMAC(string(b))
  162. if err != nil {
  163. return err
  164. }
  165. *hw = hardwareAddr(mac)
  166. return nil
  167. }
  168. type vxlanLeaseAttrs struct {
  169. VtepMAC hardwareAddr
  170. }
  171. func (vb *VXLANBackend) handleSubnetEvents(batch subnet.EventBatch) {
  172. for _, evt := range batch {
  173. switch evt.Type {
  174. case subnet.SubnetAdded:
  175. log.Info("Subnet added: ", evt.Lease.Network)
  176. if evt.Lease.Attrs.BackendType != "vxlan" {
  177. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  178. continue
  179. }
  180. var attrs vxlanLeaseAttrs
  181. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  182. log.Error("Error decoding subnet lease JSON: ", err)
  183. continue
  184. }
  185. vb.rts.set(evt.Lease.Network, evt.Lease.Attrs.PublicIP.ToIP(), net.HardwareAddr(attrs.VtepMAC))
  186. case subnet.SubnetRemoved:
  187. log.Info("Subnet removed: ", evt.Lease.Network)
  188. vb.rts.remove(evt.Lease.Network)
  189. if evt.Lease.Attrs.BackendType != "vxlan" {
  190. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  191. continue
  192. }
  193. var attrs vxlanLeaseAttrs
  194. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  195. log.Error("Error decoding subnet lease JSON: ", err)
  196. continue
  197. }
  198. if len(attrs.VtepMAC) > 0 {
  199. vb.dev.DelL2(net.HardwareAddr(attrs.VtepMAC), evt.Lease.Attrs.PublicIP.ToIP())
  200. }
  201. default:
  202. log.Error("Internal error: unknown event type: ", int(evt.Type))
  203. }
  204. }
  205. }
  206. func (vb *VXLANBackend) handleMiss(miss *netlink.Neigh) {
  207. switch {
  208. case len(miss.IP) == 0 && len(miss.HardwareAddr) == 0:
  209. log.Info("Ignoring nil miss")
  210. case len(miss.IP) == 0:
  211. vb.handleL2Miss(miss)
  212. case len(miss.HardwareAddr) == 0:
  213. vb.handleL3Miss(miss)
  214. default:
  215. log.Infof("Ignoring not a miss: %v, %v", miss.HardwareAddr, miss.IP)
  216. }
  217. }
  218. func (vb *VXLANBackend) handleL2Miss(miss *netlink.Neigh) {
  219. log.Infof("L2 miss: %v", miss.HardwareAddr)
  220. rt := vb.rts.findByVtepMAC(miss.HardwareAddr)
  221. if rt == nil {
  222. log.Infof("Route for %v not found", miss.HardwareAddr)
  223. return
  224. }
  225. if err := vb.dev.AddL2(miss.HardwareAddr, rt.vtepIP); err != nil {
  226. log.Errorf("AddL2 failed: %v", err)
  227. } else {
  228. log.Info("AddL2 succeeded")
  229. }
  230. }
  231. func (vb *VXLANBackend) handleL3Miss(miss *netlink.Neigh) {
  232. log.Infof("L3 miss: %v", miss.IP)
  233. rt := vb.rts.findByNetwork(ip.FromIP(miss.IP))
  234. if rt == nil {
  235. log.Infof("Route for %v not found", miss.IP)
  236. return
  237. }
  238. if err := vb.dev.AddL3(miss.IP, rt.vtepMAC); err != nil {
  239. log.Errorf("AddL3 failed: %v", err)
  240. } else {
  241. log.Info("AddL3 succeeded")
  242. }
  243. }