vxlan.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. log "github.com/coreos/flannel/Godeps/_workspace/src/github.com/golang/glog"
  20. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/flannel/backend"
  22. "github.com/coreos/flannel/pkg/ip"
  23. "github.com/coreos/flannel/subnet"
  24. "net"
  25. "sync"
  26. "time"
  27. )
  28. const (
  29. defaultVNI = 1
  30. )
  31. type VXLANBackend struct {
  32. sm subnet.Manager
  33. network string
  34. config *subnet.Config
  35. cfg struct {
  36. VNI int
  37. Port int
  38. }
  39. lease *subnet.Lease
  40. dev *vxlanDevice
  41. ctx context.Context
  42. cancel context.CancelFunc
  43. wg sync.WaitGroup
  44. }
  45. func New(sm subnet.Manager, network string, config *subnet.Config) backend.Backend {
  46. ctx, cancel := context.WithCancel(context.Background())
  47. vb := &VXLANBackend{
  48. sm: sm,
  49. network: network,
  50. config: config,
  51. ctx: ctx,
  52. cancel: cancel,
  53. }
  54. vb.cfg.VNI = defaultVNI
  55. return vb
  56. }
  57. func newSubnetAttrs(pubIP net.IP, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  58. data, err := json.Marshal(&vxlanLeaseAttrs{hardwareAddr(mac)})
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &subnet.LeaseAttrs{
  63. PublicIP: ip.FromIP(pubIP),
  64. BackendType: "vxlan",
  65. BackendData: json.RawMessage(data),
  66. }, nil
  67. }
  68. func (vb *VXLANBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
  69. // Parse our configuration
  70. if len(vb.config.Backend) > 0 {
  71. if err := json.Unmarshal(vb.config.Backend, &vb.cfg); err != nil {
  72. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  73. }
  74. }
  75. devAttrs := vxlanDeviceAttrs{
  76. vni: uint32(vb.cfg.VNI),
  77. name: fmt.Sprintf("flannel.%v", vb.cfg.VNI),
  78. vtepIndex: extIface.Index,
  79. vtepAddr: extIP,
  80. vtepPort: vb.cfg.Port,
  81. }
  82. var err error
  83. for {
  84. vb.dev, err = newVXLANDevice(&devAttrs)
  85. if err == nil {
  86. break
  87. } else {
  88. log.Error("VXLAN init: ", err)
  89. log.Info("Retrying in 1 second...")
  90. // wait 1 sec before retrying
  91. time.Sleep(1 * time.Second)
  92. }
  93. }
  94. sa, err := newSubnetAttrs(extIP, vb.dev.MACAddr())
  95. if err != nil {
  96. return nil, err
  97. }
  98. l, err := vb.sm.AcquireLease(vb.ctx, vb.network, sa)
  99. switch err {
  100. case nil:
  101. vb.lease = l
  102. case context.Canceled, context.DeadlineExceeded:
  103. return nil, err
  104. default:
  105. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  106. }
  107. // vxlan's subnet is that of the whole overlay network (e.g. /16)
  108. // and not that of the individual host (e.g. /24)
  109. vxlanNet := ip.IP4Net{
  110. IP: l.Subnet.IP,
  111. PrefixLen: vb.config.Network.PrefixLen,
  112. }
  113. if err = vb.dev.Configure(vxlanNet); err != nil {
  114. return nil, err
  115. }
  116. return &backend.SubnetDef{
  117. Net: l.Subnet,
  118. MTU: vb.dev.MTU(),
  119. }, nil
  120. }
  121. func (vb *VXLANBackend) Run() {
  122. vb.wg.Add(1)
  123. go func() {
  124. subnet.LeaseRenewer(vb.ctx, vb.sm, vb.network, vb.lease)
  125. log.Info("LeaseRenewer exited")
  126. vb.wg.Done()
  127. }()
  128. log.Info("Watching for new subnet leases")
  129. evts := make(chan []subnet.Event)
  130. vb.wg.Add(1)
  131. go func() {
  132. subnet.WatchLeases(vb.ctx, vb.sm, vb.network, evts)
  133. log.Info("WatchLeases exited")
  134. vb.wg.Done()
  135. }()
  136. defer vb.wg.Wait()
  137. initialEvtsBatch := <-evts
  138. for {
  139. err := vb.handleInitialSubnetEvents(initialEvtsBatch)
  140. if err == nil {
  141. break
  142. }
  143. log.Error(err, " About to retry")
  144. time.Sleep(time.Second)
  145. }
  146. for {
  147. select {
  148. case evtBatch := <-evts:
  149. vb.handleSubnetEvents(evtBatch)
  150. case <-vb.ctx.Done():
  151. return
  152. }
  153. }
  154. }
  155. func (vb *VXLANBackend) Stop() {
  156. vb.cancel()
  157. }
  158. func (vb *VXLANBackend) Name() string {
  159. return "VXLAN"
  160. }
  161. // So we can make it JSON (un)marshalable
  162. type hardwareAddr net.HardwareAddr
  163. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  164. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  165. }
  166. func (hw *hardwareAddr) UnmarshalJSON(b []byte) error {
  167. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  168. return fmt.Errorf("error parsing hardware addr")
  169. }
  170. b = b[1 : len(b)-1]
  171. mac, err := net.ParseMAC(string(b))
  172. if err != nil {
  173. return err
  174. }
  175. *hw = hardwareAddr(mac)
  176. return nil
  177. }
  178. type vxlanLeaseAttrs struct {
  179. VtepMAC hardwareAddr
  180. }
  181. func (vb *VXLANBackend) handleSubnetEvents(batch []subnet.Event) {
  182. for _, evt := range batch {
  183. switch evt.Type {
  184. case subnet.SubnetAdded:
  185. log.Info("Subnet added: ", evt.Lease.Subnet)
  186. if evt.Lease.Attrs.BackendType != "vxlan" {
  187. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  188. continue
  189. }
  190. var attrs vxlanLeaseAttrs
  191. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  192. log.Error("Error decoding subnet lease JSON: ", err)
  193. continue
  194. }
  195. vb.dev.AddL2(neigh{IP: evt.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  196. vb.dev.AddL3(neigh{IP: evt.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  197. case subnet.SubnetRemoved:
  198. log.Info("Subnet removed: ", evt.Lease.Subnet)
  199. if evt.Lease.Attrs.BackendType != "vxlan" {
  200. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  201. continue
  202. }
  203. var attrs vxlanLeaseAttrs
  204. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &attrs); err != nil {
  205. log.Error("Error decoding subnet lease JSON: ", err)
  206. continue
  207. }
  208. if len(attrs.VtepMAC) > 0 {
  209. vb.dev.DelL2(neigh{IP: evt.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  210. vb.dev.DelL3(neigh{IP: evt.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)})
  211. }
  212. default:
  213. log.Error("Internal error: unknown event type: ", int(evt.Type))
  214. }
  215. }
  216. }
  217. func (vb *VXLANBackend) handleInitialSubnetEvents(batch []subnet.Event) error {
  218. log.Infof("Handling initial subnet events")
  219. fdbTable, err := vb.dev.GetL2List()
  220. if err != nil {
  221. return fmt.Errorf("Error fetching L2 table: %v", err)
  222. }
  223. l3Table, err := vb.dev.GetL3List()
  224. if err != nil {
  225. return fmt.Errorf("Error fetching L3 table: %v", err)
  226. }
  227. for _, fdbEntry := range fdbTable {
  228. log.Infof("fdb already populated with: %s %s ", fdbEntry.IP, fdbEntry.HardwareAddr)
  229. }
  230. for _, l3Entry := range l3Table {
  231. log.Infof("l3 table already populated with: %s %s", l3Entry.IP, l3Entry.HardwareAddr)
  232. }
  233. evtMarker := make([]bool, len(batch))
  234. leaseAttrsList := make([]vxlanLeaseAttrs, len(batch))
  235. fdbEntryMarker := make([]bool, len(fdbTable))
  236. for i, evt := range batch {
  237. if evt.Lease.Attrs.BackendType != "vxlan" {
  238. log.Warningf("Ignoring non-vxlan subnet: type=%v", evt.Lease.Attrs.BackendType)
  239. evtMarker[i] = true
  240. continue
  241. }
  242. if err := json.Unmarshal(evt.Lease.Attrs.BackendData, &leaseAttrsList[i]); err != nil {
  243. log.Error("Error decoding subnet lease JSON: ", err)
  244. evtMarker[i] = true
  245. continue
  246. }
  247. for j, fdbEntry := range fdbTable {
  248. if evt.Lease.Attrs.PublicIP.ToIP().Equal(fdbEntry.IP) && bytes.Equal([]byte(leaseAttrsList[i].VtepMAC), []byte(fdbEntry.HardwareAddr)) {
  249. evtMarker[i] = true
  250. fdbEntryMarker[j] = true
  251. break
  252. }
  253. }
  254. }
  255. for _, l3Entry := range l3Table {
  256. vb.dev.DelL3(neigh{IP: ip.FromIP(l3Entry.IP), MAC: l3Entry.HardwareAddr})
  257. }
  258. for j, marker := range fdbEntryMarker {
  259. if !marker {
  260. err := vb.dev.DelL2(neigh{IP: ip.FromIP(fdbTable[j].IP), MAC: fdbTable[j].HardwareAddr})
  261. if err != nil {
  262. log.Error("Delete L2 failed: ", err)
  263. }
  264. }
  265. }
  266. for i, marker := range evtMarker {
  267. if !marker {
  268. err := vb.dev.AddL2(neigh{IP: batch[i].Lease.Attrs.PublicIP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  269. if err != nil {
  270. log.Error("Add L2 failed: ", err)
  271. }
  272. }
  273. err := vb.dev.AddL3(neigh{IP: batch[i].Lease.Subnet.IP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  274. if err != nil {
  275. log.Error("Add L3 failed: ", err)
  276. err1 := vb.dev.DelL2(neigh{IP: batch[i].Lease.Attrs.PublicIP, MAC: net.HardwareAddr(leaseAttrsList[i].VtepMAC)})
  277. if err1 != nil {
  278. log.Error("Attempt to remove matching L2 entry failed: ", err1)
  279. }
  280. }
  281. }
  282. return nil
  283. }