vxlan_network.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "encoding/json"
  17. "net"
  18. "sync"
  19. log "github.com/golang/glog"
  20. "github.com/vishvananda/netlink"
  21. "golang.org/x/net/context"
  22. "syscall"
  23. "github.com/coreos/flannel/backend"
  24. "github.com/coreos/flannel/pkg/ip"
  25. "github.com/coreos/flannel/subnet"
  26. )
  27. type network struct {
  28. backend.SimpleNetwork
  29. extIface *backend.ExternalInterface
  30. dev *vxlanDevice
  31. subnetMgr subnet.Manager
  32. }
  33. func newNetwork(subnetMgr subnet.Manager, extIface *backend.ExternalInterface, dev *vxlanDevice, _ ip.IP4Net, lease *subnet.Lease) (*network, error) {
  34. nw := &network{
  35. SimpleNetwork: backend.SimpleNetwork{
  36. SubnetLease: lease,
  37. ExtIface: extIface,
  38. },
  39. subnetMgr: subnetMgr,
  40. dev: dev,
  41. }
  42. return nw, nil
  43. }
  44. func (nw *network) Run(ctx context.Context) {
  45. wg := sync.WaitGroup{}
  46. log.V(0).Info("watching for new subnet leases")
  47. events := make(chan []subnet.Event)
  48. wg.Add(1)
  49. go func() {
  50. subnet.WatchLeases(ctx, nw.subnetMgr, nw.SubnetLease, events)
  51. log.V(1).Info("WatchLeases exited")
  52. wg.Done()
  53. }()
  54. defer wg.Wait()
  55. for {
  56. select {
  57. case evtBatch := <-events:
  58. nw.handleSubnetEvents(evtBatch)
  59. case <-ctx.Done():
  60. return
  61. }
  62. }
  63. }
  64. func (nw *network) MTU() int {
  65. return nw.dev.MTU()
  66. }
  67. type vxlanLeaseAttrs struct {
  68. VtepMAC hardwareAddr
  69. }
  70. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  71. for _, event := range batch {
  72. if event.Lease.Attrs.BackendType != "vxlan" {
  73. log.Warningf("ignoring non-vxlan subnet(%s): type=%v", event.Lease.Subnet, event.Lease.Attrs.BackendType)
  74. continue
  75. }
  76. var attrs vxlanLeaseAttrs
  77. if err := json.Unmarshal(event.Lease.Attrs.BackendData, &attrs); err != nil {
  78. log.Error("error decoding subnet lease JSON: ", err)
  79. continue
  80. }
  81. route := netlink.Route{
  82. LinkIndex: nw.dev.link.Attrs().Index,
  83. Scope: netlink.SCOPE_UNIVERSE,
  84. Dst: event.Lease.Subnet.ToIPNet(),
  85. Gw: event.Lease.Subnet.IP.ToIP(),
  86. }
  87. route.SetFlag(syscall.RTNH_F_ONLINK)
  88. switch event.Type {
  89. case subnet.EventAdded:
  90. log.V(2).Infof("adding subnet: %s PublicIP: %s VtepMAC: %s", event.Lease.Subnet, event.Lease.Attrs.PublicIP, net.HardwareAddr(attrs.VtepMAC))
  91. if err := nw.dev.AddARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  92. log.Error("AddARP failed: ", err)
  93. continue
  94. }
  95. if err := nw.dev.AddFDB(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  96. log.Error("AddFDB failed: ", err)
  97. // Try to clean up the ARP entry then continue
  98. if err := nw.dev.DelARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  99. log.Error("DelARP failed: ", err)
  100. }
  101. continue
  102. }
  103. // Set the route - the kernel would ARP for the Gw IP address if it hadn't already been set above so make sure
  104. // this is done last.
  105. if err := netlink.RouteReplace(&route); err != nil {
  106. log.Errorf("failed to add route (%s -> %s): %v", route.Dst, route.Gw, err)
  107. // Try to clean up both the ARP and FDB entries then continue
  108. if err := nw.dev.DelARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  109. log.Error("DelARP failed: ", err)
  110. }
  111. if err := nw.dev.DelFDB(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  112. log.Error("DelFDB failed: ", err)
  113. }
  114. continue
  115. }
  116. case subnet.EventRemoved:
  117. log.V(2).Infof("removing subnet: %s PublicIP: %s VtepMAC: %s", event.Lease.Subnet, event.Lease.Attrs.PublicIP, net.HardwareAddr(attrs.VtepMAC))
  118. // Try to remove all entries - don't bail out if one of them fails.
  119. if err := nw.dev.DelARP(neighbor{IP: event.Lease.Subnet.IP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  120. log.Error("DelARP failed: ", err)
  121. }
  122. if err := nw.dev.DelFDB(neighbor{IP: event.Lease.Attrs.PublicIP, MAC: net.HardwareAddr(attrs.VtepMAC)}); err != nil {
  123. log.Error("DelFDB failed: ", err)
  124. }
  125. if err := netlink.RouteDel(&route); err != nil {
  126. log.Errorf("failed to delete route (%s -> %s): %v", route.Dst, route.Gw, err)
  127. }
  128. default:
  129. log.Error("internal error: unknown event type: ", int(event.Type))
  130. }
  131. }
  132. }