vxlan_network_windows.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. log "github.com/golang/glog"
  17. "golang.org/x/net/context"
  18. "sync"
  19. "github.com/coreos/flannel/backend"
  20. "github.com/coreos/flannel/subnet"
  21. "github.com/coreos/flannel/pkg/ip"
  22. "strings"
  23. )
  24. type network struct {
  25. backend.SimpleNetwork
  26. dev *vxlanDevice
  27. subnetMgr subnet.Manager
  28. }
  29. const (
  30. encapOverhead = 50
  31. )
  32. func newNetwork(subnetMgr subnet.Manager, extIface *backend.ExternalInterface, dev *vxlanDevice, _ ip.IP4Net, lease *subnet.Lease) (*network, error) {
  33. nw := &network{
  34. SimpleNetwork: backend.SimpleNetwork{
  35. SubnetLease: lease,
  36. ExtIface: extIface,
  37. },
  38. subnetMgr: subnetMgr,
  39. dev: dev,
  40. }
  41. return nw, nil
  42. }
  43. func (nw *network) Run(ctx context.Context) {
  44. wg := sync.WaitGroup{}
  45. log.V(0).Info("Watching for new subnet leases")
  46. events := make(chan []subnet.Event)
  47. wg.Add(1)
  48. go func() {
  49. subnet.WatchLeases(ctx, nw.subnetMgr, nw.SubnetLease, events)
  50. log.V(1).Info("WatchLeases exited")
  51. wg.Done()
  52. }()
  53. defer wg.Wait()
  54. for {
  55. select {
  56. case evtBatch := <-events:
  57. nw.handleSubnetEvents(evtBatch)
  58. case <-ctx.Done():
  59. return
  60. }
  61. }
  62. }
  63. func (nw *network) MTU() int {
  64. return nw.ExtIface.Iface.MTU - encapOverhead
  65. }
  66. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  67. for _, event := range batch {
  68. leaseSubnet := event.Lease.Subnet
  69. leaseAttrs := event.Lease.Attrs
  70. if !strings.EqualFold(leaseAttrs.BackendType, "vxlan") {
  71. log.Warningf("ignoring non-vxlan subnet(%v): type=%v", leaseSubnet, leaseAttrs.BackendType)
  72. continue
  73. }
  74. publicIP := leaseAttrs.PublicIP.String()
  75. remoteIP := leaseSubnet.IP + 2
  76. lastIP := leaseSubnet.Next().IP - 1
  77. switch event.Type {
  78. case subnet.EventAdded:
  79. for ; remoteIP < lastIP; remoteIP++ {
  80. n := &neighbor{
  81. IP: remoteIP,
  82. MAC: nw.dev.ConjureMac(remoteIP),
  83. ManagementAddress: publicIP,
  84. }
  85. log.V(2).Infof("adding subnet: %v publicIP: %s vtepMAC: %s", leaseSubnet, n.ManagementAddress, n.MAC)
  86. if err := nw.dev.AddEndpoint(n); err != nil {
  87. log.Error(err)
  88. }
  89. }
  90. case subnet.EventRemoved:
  91. for ; remoteIP < lastIP; remoteIP++ {
  92. n := &neighbor{
  93. IP: remoteIP,
  94. MAC: nw.dev.ConjureMac(remoteIP),
  95. ManagementAddress: publicIP,
  96. }
  97. log.V(2).Infof("removing subnet: %v publicIP: %s vtepMAC: %s", leaseSubnet, n.ManagementAddress, n.MAC)
  98. if err := nw.dev.DelEndpoint(n); err != nil {
  99. log.Error(err)
  100. }
  101. }
  102. default:
  103. log.Error("internal error: unknown event type: ", int(event.Type))
  104. }
  105. }
  106. }