vxlan_windows.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "net"
  29. "sync"
  30. "github.com/Microsoft/hcsshim/hcn"
  31. "github.com/flannel-io/flannel/backend"
  32. "github.com/flannel-io/flannel/pkg/ip"
  33. "github.com/flannel-io/flannel/subnet"
  34. "golang.org/x/net/context"
  35. log "k8s.io/klog"
  36. )
  37. func init() {
  38. backend.Register("vxlan", New)
  39. }
  40. const (
  41. defaultVNI = 4096
  42. vxlanPort = 4789
  43. )
  44. type VXLANBackend struct {
  45. subnetMgr subnet.Manager
  46. extIface *backend.ExternalInterface
  47. }
  48. func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
  49. backend := &VXLANBackend{
  50. subnetMgr: sm,
  51. extIface: extIface,
  52. }
  53. return backend, nil
  54. }
  55. func newSubnetAttrs(publicIP net.IP, vnid uint16, mac net.HardwareAddr) (*subnet.LeaseAttrs, error) {
  56. var hardwareAddress hardwareAddr
  57. if mac != nil {
  58. hardwareAddress = hardwareAddr(mac)
  59. }
  60. leaseAttrs := &vxlanLeaseAttrs{
  61. VNI: vnid,
  62. VtepMAC: hardwareAddress,
  63. }
  64. data, err := json.Marshal(&leaseAttrs)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &subnet.LeaseAttrs{
  69. PublicIP: ip.FromIP(publicIP),
  70. BackendType: "vxlan",
  71. BackendData: json.RawMessage(data),
  72. }, nil
  73. }
  74. func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
  75. // 1. Parse configuration
  76. cfg := struct {
  77. Name string
  78. MacPrefix string
  79. VNI int
  80. Port int
  81. GBP bool
  82. DirectRouting bool
  83. }{
  84. VNI: defaultVNI,
  85. Port: vxlanPort,
  86. MacPrefix: "0E-2A",
  87. }
  88. if len(config.Backend) > 0 {
  89. if err := json.Unmarshal(config.Backend, &cfg); err != nil {
  90. return nil, fmt.Errorf("error decoding VXLAN backend config: %v", err)
  91. }
  92. }
  93. // 2. Verify configuration
  94. if cfg.VNI < defaultVNI {
  95. return nil, fmt.Errorf("invalid VXLAN backend config. VNI [%v] must be greater than or equal to %v on Windows", cfg.VNI, defaultVNI)
  96. }
  97. if cfg.Port != vxlanPort {
  98. 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)
  99. }
  100. if cfg.DirectRouting {
  101. return nil, errors.New("invalid VXLAN backend config. DirectRouting is not supported on Windows")
  102. }
  103. if cfg.GBP {
  104. return nil, errors.New("invalid VXLAN backend config. GBP is not supported on Windows")
  105. }
  106. if len(cfg.MacPrefix) == 0 || len(cfg.MacPrefix) != 5 || cfg.MacPrefix[2] != '-' {
  107. 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)
  108. }
  109. if len(cfg.Name) == 0 {
  110. cfg.Name = fmt.Sprintf("flannel.%v", cfg.VNI)
  111. }
  112. 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)
  113. err := hcn.RemoteSubnetSupported()
  114. if err != nil {
  115. return nil, err
  116. }
  117. subnetAttrs, err := newSubnetAttrs(be.extIface.ExtAddr, uint16(cfg.VNI), nil)
  118. if err != nil {
  119. return nil, err
  120. }
  121. lease, err := be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  122. switch err {
  123. case nil:
  124. case context.Canceled, context.DeadlineExceeded:
  125. return nil, err
  126. default:
  127. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  128. }
  129. devAttrs := vxlanDeviceAttrs{
  130. vni: uint32(cfg.VNI),
  131. name: cfg.Name,
  132. addressPrefix: lease.Subnet,
  133. }
  134. dev, err := newVXLANDevice(&devAttrs)
  135. if err != nil {
  136. return nil, err
  137. }
  138. dev.directRouting = cfg.DirectRouting
  139. dev.macPrefix = cfg.MacPrefix
  140. network, err := newNetwork(be.subnetMgr, be.extIface, dev, ip.IP4Net{}, lease)
  141. if err != nil {
  142. return nil, err
  143. }
  144. hcnNetwork, err := hcn.GetNetworkByName(cfg.Name)
  145. if err != nil {
  146. return nil, err
  147. }
  148. var newDrMac string
  149. for _, policy := range hcnNetwork.Policies {
  150. if policy.Type == hcn.DrMacAddress {
  151. policySettings := hcn.DrMacAddressNetworkPolicySetting{}
  152. err = json.Unmarshal(policy.Settings, &policySettings)
  153. if err != nil {
  154. return nil, fmt.Errorf("Failed to unmarshal settings")
  155. }
  156. newDrMac = policySettings.Address
  157. }
  158. }
  159. mac, err := net.ParseMAC(string(newDrMac))
  160. if err != nil {
  161. return nil, fmt.Errorf("Cannot parse DR MAC %v: %+v", newDrMac, err)
  162. }
  163. subnetAttrs, err = newSubnetAttrs(be.extIface.ExtAddr, uint16(cfg.VNI), mac)
  164. if err != nil {
  165. return nil, err
  166. }
  167. lease, err = be.subnetMgr.AcquireLease(ctx, subnetAttrs)
  168. if err != nil {
  169. return nil, err
  170. }
  171. network.SubnetLease = lease
  172. return network, nil
  173. }
  174. // So we can make it JSON (un)marshalable
  175. type hardwareAddr net.HardwareAddr
  176. func (hw hardwareAddr) MarshalJSON() ([]byte, error) {
  177. return []byte(fmt.Sprintf("%q", net.HardwareAddr(hw))), nil
  178. }
  179. func (hw *hardwareAddr) UnmarshalJSON(bytes []byte) error {
  180. if len(bytes) < 2 || bytes[0] != '"' || bytes[len(bytes)-1] != '"' {
  181. return fmt.Errorf("error parsing hardware addr")
  182. }
  183. bytes = bytes[1 : len(bytes)-1]
  184. mac, err := net.ParseMAC(string(bytes))
  185. if err != nil {
  186. return err
  187. }
  188. *hw = hardwareAddr(mac)
  189. return nil
  190. }