vxlan.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. //
  49. // In this newest scheme, there is also the option of skipping the use of vxlan for hosts that are on the same subnet,
  50. // this is called "directRouting"
  51. import (
  52. "encoding/json"
  53. "fmt"
  54. "net"
  55. log "github.com/golang/glog"
  56. "golang.org/x/net/context"
  57. "github.com/coreos/flannel/backend"
  58. "github.com/coreos/flannel/pkg/ip"
  59. "github.com/coreos/flannel/subnet"
  60. )
  61. func init() {
  62. backend.Register("vxlan", New)
  63. }
  64. const (
  65. defaultVNI = 1
  66. )
  67. type VXLANBackend struct {
  68. subnetMgr subnet.Manager
  69. extIface *backend.ExternalInterface
  70. }
  71. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  72. backend := &VXLANBackend{
  73. subnetMgr: sm,
  74. extIface: extIface,
  75. }
  76. return backend, nil
  77. }
  78. func newSubnetAttrs(publicIP net.IP, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  79. data, err := json.Marshal(&vxlanLeaseAttrs{hardwareAddr(mac)})
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &subnet.LeaseAttrs{
  84. PublicIP: ip.FromIP(publicIP),
  85. BackendType: "vxlan",
  86. BackendData: json.RawMessage(data),
  87. }, nil
  88. }
  89. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, config *subnet.Config) (backend.Network, error) {
  90. // Parse our configuration
  91. cfg := struct {
  92. VNI int
  93. Port int
  94. GBP bool
  95. DirectRouting bool
  96. }{
  97. VNI: defaultVNI,
  98. }
  99. if len(config.Backend) > 0 {
  100. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  101. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  102. }
  103. }
  104. log.Infof("VXLAN config: VNI=%d Port=%d GBP=%v DirectRouting=%v", cfg.VNI, cfg.Port, cfg.GBP, cfg.DirectRouting)
  105. devAttrs := vxlanDeviceAttrs{
  106. vni: uint32(cfg.VNI),
  107. name: fmt.Sprintf("flannel.%v", cfg.VNI),
  108. vtepIndex: be.extIface.Iface.Index,
  109. vtepAddr: be.extIface.IfaceAddr,
  110. vtepPort: cfg.Port,
  111. gbp: cfg.GBP,
  112. }
  113. dev, err := newVXLANDevice(&devAttrs)
  114. if err != nil {
  115. return nil, err
  116. }
  117. dev.directRouting = cfg.DirectRouting
  118. subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, dev.MACAddr())
  119. if err != nil {
  120. return nil, err
  121. }
  122. lease, err := be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  123. switch err {
  124. case nil:
  125. case context.Canceled, context.DeadlineExceeded:
  126. return nil, err
  127. default:
  128. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  129. }
  130. // Ensure that the device has a /32 address so that no broadcast routes are created.
  131. // This IP is just used as a source address for host to workload traffic (so
  132. // the return path for the traffic has an address on the flannel network to use as the destination)
  133. if err := dev.Configure(ip.IP4Net{IP: lease.Subnet.IP, PrefixLen: 32}); err != nil {
  134. return nil, fmt.Errorf("failed to configure interface %s: %s", dev.link.Attrs().Name, err)
  135. }
  136. return newNetwork(be.subnetMgr, be.extIface, dev, ip.IP4Net{}, lease)
  137. }
  138. // So we can make it JSON (un)marshalable
  139. type hardwareAddr net.HardwareAddr
  140. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  141. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  142. }
  143. func (hw *hardwareAddr) UnmarshalJSON(bytes []byte) error {
  144. if len(bytes) < 2 || bytes[0] != '"' || bytes[len(bytes)-1] != '"' {
  145. return fmt.Errorf("error parsing hardware addr")
  146. }
  147. bytes = bytes[1 : len(bytes)-1]
  148. mac, err := net.ParseMAC(string(bytes))
  149. if err != nil {
  150. return err
  151. }
  152. *hw = hardwareAddr(mac)
  153. return nil
  154. }