vxlan.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // +build !windows
  15. package vxlan
  16. // Some design notes and history:
  17. // VXLAN encapsulates L2 packets (though flannel is L3 only so don't expect to be able to send L2 packets across hosts)
  18. // The first versions of vxlan for flannel registered the flannel daemon as a handler for both "L2" and "L3" misses
  19. // - When a container sends a packet to a new IP address on the flannel network (but on a different host) this generates
  20. // an L2 miss (i.e. an ARP lookup)
  21. // - The flannel daemon knows which flannel host the packet is destined for so it can supply the VTEP MAC to use.
  22. // This is stored in the ARP table (with a timeout) to avoid constantly looking it up.
  23. // - The packet can then be encapsulated but the host needs to know where to send it. This creates another callout from
  24. // the kernal vxlan code to the flannel daemon to get the public IP that should be used for that VTEP (this gets called
  25. // an L3 miss). The L2/L3 miss hooks are registered when the vxlan device is created. At the same time a device route
  26. // is created to the whole flannel network so that non-local traffic is sent over the vxlan device.
  27. //
  28. // In this scheme the scaling of table entries (per host) is:
  29. // - 1 route (for the configured network out the vxlan device)
  30. // - One arp entry for each remote container that this host has recently contacted
  31. // - One FDB entry for each remote host
  32. //
  33. // The second version of flannel vxlan removed the need for the L3MISS callout. When a new remote host is found (either
  34. // during startup or when it's created), flannel simply adds the required entries so that no further lookup/callout is required.
  35. //
  36. //
  37. // The latest version of the vxlan backend removes the need for the L2MISS too, which means that the flannel deamon is not
  38. // listening for any netlink messages anymore. This improves reliability (no problems with timeouts if
  39. // flannel crashes or restarts) and simplifies upgrades.
  40. //
  41. // How it works:
  42. // Create the vxlan device but don't register for any L2MISS or L3MISS messages
  43. // Then, as each remote host is discovered (either on startup or when they are added), do the following
  44. // 1) Create routing table entry for the remote subnet. It goes via the vxlan device but also specifies a next hop (of the remote flannel host).
  45. // 2) Create a static ARP entry for the remote flannel host IP address (and the VTEP MAC)
  46. // 3) Create an FDB entry with the VTEP MAC and the public IP of the remote flannel daemon.
  47. //
  48. // In this scheme the scaling of table entries is linear to the number of remote hosts - 1 route, 1 arp entry and 1 FDB entry per host
  49. //
  50. // In this newest scheme, there is also the option of skipping the use of vxlan for hosts that are on the same subnet,
  51. // this is called "directRouting"
  52. import (
  53. "encoding/json"
  54. "fmt"
  55. log "github.com/golang/glog"
  56. "net"
  57. "sync"
  58. "golang.org/x/net/context"
  59. "github.com/coreos/flannel/backend"
  60. "github.com/coreos/flannel/pkg/ip"
  61. "github.com/coreos/flannel/subnet"
  62. )
  63. func init() {
  64. backend.Register("vxlan", New)
  65. }
  66. const (
  67. defaultVNI = 1
  68. )
  69. type VXLANBackend struct {
  70. subnetMgr subnet.Manager
  71. extIface *backend.ExternalInterface
  72. }
  73. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  74. backend := &VXLANBackend{
  75. subnetMgr: sm,
  76. extIface: extIface,
  77. }
  78. return backend, nil
  79. }
  80. func newSubnetAttrs(publicIP net.IP, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  81. data, err := json.Marshal(&vxlanLeaseAttrs{hardwareAddr(mac)})
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &subnet.LeaseAttrs{
  86. PublicIP: ip.FromIP(publicIP),
  87. BackendType: "vxlan",
  88. BackendData: json.RawMessage(data),
  89. }, nil
  90. }
  91. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  92. // Parse our configuration
  93. cfg := struct {
  94. VNI int
  95. Port int
  96. GBP bool
  97. Learning bool
  98. DirectRouting bool
  99. }{
  100. VNI: defaultVNI,
  101. }
  102. if len(config.Backend) > 0 {
  103. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  104. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  105. }
  106. }
  107. log.Infof("VXLAN config: VNI=%d Port=%d GBP=%v Learning=%v DirectRouting=%v", cfg.VNI, cfg.Port, cfg.GBP, cfg.Learning, cfg.DirectRouting)
  108. devAttrs := vxlanDeviceAttrs{
  109. vni: uint32(cfg.VNI),
  110. name: fmt.Sprintf("flannel.%v", cfg.VNI),
  111. vtepIndex: be.extIface.Iface.Index,
  112. vtepAddr: be.extIface.IfaceAddr,
  113. vtepPort: cfg.Port,
  114. gbp: cfg.GBP,
  115. learning: cfg.Learning,
  116. }
  117. dev, err := newVXLANDevice(&devAttrs)
  118. if err != nil {
  119. return nil, err
  120. }
  121. dev.directRouting = cfg.DirectRouting
  122. subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  123. if err != nil {
  124. return nil, err
  125. }
  126. lease, err := be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  127. switch err {
  128. case nil:
  129. case context.Canceled, context.DeadlineExceeded:
  130. return nil, err
  131. default:
  132. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  133. }
  134. // Ensure that the device has a /32 address so that no broadcast routes are created.
  135. // This IP is just used as a source address for host to workload traffic (so
  136. // the return path for the traffic has an address on the flannel network to use as the destination)
  137. if err := dev.Configure(ip.IP4Net{IP: lease.Subnet.IP, PrefixLen: 32}); err != nil {
  138. return nil, fmt.Errorf("failed to configure interface %s: %s", dev.link.Attrs().Name, err)
  139. }
  140. return newNetwork(be.subnetMgr, be.extIface, dev, ip.IP4Net{}, lease)
  141. }
  142. // So we can make it JSON (un)marshalable
  143. type hardwareAddr net.HardwareAddr
  144. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  145. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  146. }
  147. func (hw *hardwareAddr) UnmarshalJSON(bytes []byte) error {
  148. if len(bytes) < 2 || bytes[0] != '"' || bytes[len(bytes)-1] != '"' {
  149. return fmt.Errorf("error parsing hardware addr")
  150. }
  151. bytes = bytes[1 : len(bytes)-1]
  152. mac, err := net.ParseMAC(string(bytes))
  153. if err != nil {
  154. return err
  155. }
  156. *hw = hardwareAddr(mac)
  157. return nil
  158. }