vxlan_network_windows.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import (
  16. "encoding/json"
  17. "net"
  18. "strings"
  19. "sync"
  20. "golang.org/x/net/context"
  21. "github.com/Microsoft/hcsshim/hcn"
  22. "github.com/coreos/flannel/backend"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "github.com/coreos/flannel/subnet"
  25. log "k8s.io/klog"
  26. )
  27. type network struct {
  28. backend.SimpleNetwork
  29. dev *vxlanDevice
  30. subnetMgr subnet.Manager
  31. }
  32. type vxlanLeaseAttrs struct {
  33. VNI uint16
  34. VtepMAC hardwareAddr
  35. }
  36. const (
  37. encapOverhead = 50
  38. )
  39. func newNetwork(subnetMgr subnet.Manager, extIface *backend.ExternalInterface, dev *vxlanDevice, _ ip.IP4Net, lease *subnet.Lease) (*network, error) {
  40. nw := &network{
  41. SimpleNetwork: backend.SimpleNetwork{
  42. SubnetLease: lease,
  43. ExtIface: extIface,
  44. },
  45. subnetMgr: subnetMgr,
  46. dev: dev,
  47. }
  48. return nw, nil
  49. }
  50. func (nw *network) Run(ctx context.Context) {
  51. wg := sync.WaitGroup{}
  52. log.V(0).Info("Watching for new subnet leases")
  53. events := make(chan []subnet.Event)
  54. wg.Add(1)
  55. go func() {
  56. subnet.WatchLeases(ctx, nw.subnetMgr, nw.SubnetLease, events)
  57. log.V(1).Info("WatchLeases exited")
  58. wg.Done()
  59. }()
  60. defer wg.Wait()
  61. for {
  62. select {
  63. case evtBatch := <-events:
  64. nw.handleSubnetEvents(evtBatch)
  65. case <-ctx.Done():
  66. return
  67. }
  68. }
  69. }
  70. func (nw *network) MTU() int {
  71. return nw.ExtIface.Iface.MTU - encapOverhead
  72. }
  73. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  74. for _, event := range batch {
  75. leaseSubnet := event.Lease.Subnet
  76. leaseAttrs := event.Lease.Attrs
  77. if !strings.EqualFold(leaseAttrs.BackendType, "vxlan") {
  78. log.Warningf("ignoring non-vxlan subnet(%v): type=%v", leaseSubnet, leaseAttrs.BackendType)
  79. continue
  80. }
  81. var vxlanAttrs vxlanLeaseAttrs
  82. if err := json.Unmarshal(leaseAttrs.BackendData, &vxlanAttrs); err != nil {
  83. log.Error("error decoding subnet lease JSON: ", err)
  84. continue
  85. }
  86. if vxlanAttrs.VNI < 4096 {
  87. log.Error("VNI is required to greater than or equal to 4096 on Windows.")
  88. continue
  89. }
  90. hnsnetwork, err := hcn.GetNetworkByName(nw.dev.link.Name)
  91. if err != nil {
  92. log.Errorf("Unable to find network %v, error: %v", nw.dev.link.Name, err)
  93. continue
  94. }
  95. managementIp := event.Lease.Attrs.PublicIP.String()
  96. networkPolicySettings := hcn.RemoteSubnetRoutePolicySetting{
  97. IsolationId: vxlanAttrs.VNI,
  98. DistributedRouterMacAddress: net.HardwareAddr(vxlanAttrs.VtepMAC).String(),
  99. ProviderAddress: managementIp,
  100. DestinationPrefix: event.Lease.Subnet.String(),
  101. }
  102. rawJSON, err := json.Marshal(networkPolicySettings)
  103. networkPolicy := hcn.NetworkPolicy{
  104. Type: hcn.RemoteSubnetRoute,
  105. Settings: rawJSON,
  106. }
  107. policyNetworkRequest := hcn.PolicyNetworkRequest{
  108. Policies: []hcn.NetworkPolicy{networkPolicy},
  109. }
  110. switch event.Type {
  111. case subnet.EventAdded:
  112. for _, policy := range hnsnetwork.Policies {
  113. if policy.Type == hcn.RemoteSubnetRoute {
  114. existingPolicySettings := hcn.RemoteSubnetRoutePolicySetting{}
  115. err = json.Unmarshal(policy.Settings, &existingPolicySettings)
  116. if err != nil {
  117. log.Error("Failed to unmarshal settings")
  118. }
  119. if existingPolicySettings.DestinationPrefix == networkPolicySettings.DestinationPrefix {
  120. existingJson, err := json.Marshal(existingPolicySettings)
  121. if err != nil {
  122. log.Error("Failed to marshal settings")
  123. }
  124. existingPolicy := hcn.NetworkPolicy{
  125. Type: hcn.RemoteSubnetRoute,
  126. Settings: existingJson,
  127. }
  128. existingPolicyNetworkRequest := hcn.PolicyNetworkRequest{
  129. Policies: []hcn.NetworkPolicy{existingPolicy},
  130. }
  131. hnsnetwork.RemovePolicy(existingPolicyNetworkRequest)
  132. }
  133. }
  134. }
  135. if networkPolicySettings.DistributedRouterMacAddress != "" {
  136. hnsnetwork.AddPolicy(policyNetworkRequest)
  137. }
  138. case subnet.EventRemoved:
  139. if networkPolicySettings.DistributedRouterMacAddress != "" {
  140. hnsnetwork.RemovePolicy(policyNetworkRequest)
  141. }
  142. default:
  143. log.Error("internal error: unknown event type: ", int(event.Type))
  144. }
  145. }
  146. }