vxlan.go 6.0 KB

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