vxlan_windows.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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:
  16. // VXLAN encapsulates L2 packets (though flannel is L3 only so don't expect to be able to send L2 packets across hosts)
  17. // Windows overlay decap works at L2 and so it needs the correct destination MAC for the remote host to work.
  18. // Windows does not expose an L3Miss interface so for now all possible remote IP/MAC pairs have to be configured upfront.
  19. //
  20. // In this scheme the scaling of table entries (per host) is:
  21. // - 1 network entry for the overlay network
  22. // - 1 endpoint per local container
  23. // - N remote endpoints remote node (total endpoints =
  24. import (
  25. "encoding/json"
  26. "errors"
  27. "fmt"
  28. "sync"
  29. log "github.com/golang/glog"
  30. "golang.org/x/net/context"
  31. "github.com/coreos/flannel/backend"
  32. "github.com/coreos/flannel/pkg/ip"
  33. "github.com/coreos/flannel/subnet"
  34. "net"
  35. )
  36. func init() {
  37. backend.Register("vxlan", New)
  38. }
  39. const (
  40. defaultVNI = 4096
  41. vxlanPort = 4789
  42. )
  43. type VXLANBackend struct {
  44. subnetMgr subnet.Manager
  45. extIface *backend.ExternalInterface
  46. }
  47. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  48. backend := &VXLANBackend{
  49. subnetMgr: sm,
  50. extIface: extIface,
  51. }
  52. return backend, nil
  53. }
  54. func newSubnetAttrs(publicIP net.IP) (*subnet.LeaseAttrs, error) {
  55. return &subnet.LeaseAttrs{
  56. PublicIP: ip.FromIP(publicIP),
  57. BackendType: "vxlan",
  58. }, nil
  59. }
  60. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  61. // 1. Parse configuration
  62. cfg := struct {
  63. Name string
  64. MacPrefix string
  65. VNI int
  66. Port int
  67. GBP bool
  68. DirectRouting bool
  69. }{
  70. VNI: defaultVNI,
  71. Port: vxlanPort,
  72. MacPrefix: "0E-2A",
  73. }
  74. if len(config.Backend) > 0 {
  75. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  76. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  77. }
  78. }
  79. // 2. Verify configuration
  80. if cfg.VNI < defaultVNI {
  81. return nil, fmt.Errorf("invalid VXLAN backend config. VNI [%v] must be greater than or equal to %v on Windows", cfg.VNI, defaultVNI)
  82. }
  83. if cfg.Port != vxlanPort {
  84. return nil, fmt.Errorf("invalid VXLAN backend config. Port [%v] is not supported on Windows. Omit the setting to default to port %v", cfg.Port, vxlanPort)
  85. }
  86. if cfg.DirectRouting {
  87. return nil, errors.New("invalid VXLAN backend config. DirectRouting is not supported on Windows")
  88. }
  89. if cfg.GBP {
  90. return nil, errors.New("invalid VXLAN backend config. GBP is not supported on Windows")
  91. }
  92. if len(cfg.MacPrefix) == 0 || len(cfg.MacPrefix) != 5 || cfg.MacPrefix[2] != '-' {
  93. return nil, fmt.Errorf("invalid VXLAN backend config.MacPrefix [%v] is invalid, prefix must be of the format xx-xx e.g. 0E-2A", cfg.MacPrefix)
  94. }
  95. if len(cfg.Name) == 0 {
  96. cfg.Name = fmt.Sprintf("flannel.%v", cfg.VNI)
  97. }
  98. log.Infof("VXLAN config: Name=%s MacPrefix=%s VNI=%d Port=%d GBP=%v DirectRouting=%v", cfg.Name, cfg.MacPrefix, cfg.VNI, cfg.Port, cfg.GBP, cfg.DirectRouting)
  99. subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr)
  100. if err != nil {
  101. return nil, err
  102. }
  103. lease, err := be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  104. switch err {
  105. case nil:
  106. case context.Canceled, context.DeadlineExceeded:
  107. return nil, err
  108. default:
  109. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  110. }
  111. devAttrs := vxlanDeviceAttrs{
  112. vni: uint32(cfg.VNI),
  113. name: cfg.Name,
  114. addressPrefix: lease.Subnet,
  115. }
  116. dev, err := newVXLANDevice(&devAttrs)
  117. if err != nil {
  118. return nil, err
  119. }
  120. dev.directRouting = cfg.DirectRouting
  121. dev.macPrefix = cfg.MacPrefix
  122. return newNetwork(be.subnetMgr, be.extIface, dev, ip.IP4Net{}, lease)
  123. }