vxlan_windows.go 6.6 KB

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