vxlan_network_windows.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/flannel-io/flannel/backend"
  23. "github.com/flannel-io/flannel/pkg/ip"
  24. "github.com/flannel-io/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, ok := <-events:
  64. if !ok {
  65. log.Infof("evts chan closed")
  66. return
  67. }
  68. nw.handleSubnetEvents(evtBatch)
  69. }
  70. }
  71. }
  72. func (nw *network) MTU() int {
  73. return nw.ExtIface.Iface.MTU - encapOverhead
  74. }
  75. func (nw *network) handleSubnetEvents(batch []subnet.Event) {
  76. for _, event := range batch {
  77. leaseSubnet := event.Lease.Subnet
  78. leaseAttrs := event.Lease.Attrs
  79. if !strings.EqualFold(leaseAttrs.BackendType, "vxlan") {
  80. log.Warningf("ignoring non-vxlan subnet(%v): type=%v", leaseSubnet, leaseAttrs.BackendType)
  81. continue
  82. }
  83. var vxlanAttrs vxlanLeaseAttrs
  84. if err := json.Unmarshal(leaseAttrs.BackendData, &vxlanAttrs); err != nil {
  85. log.Error("error decoding subnet lease JSON: ", err)
  86. continue
  87. }
  88. if vxlanAttrs.VNI < 4096 {
  89. log.Error("VNI is required to greater than or equal to 4096 on Windows.")
  90. continue
  91. }
  92. hnsnetwork, err := hcn.GetNetworkByName(nw.dev.link.Name)
  93. if err != nil {
  94. log.Errorf("Unable to find network %v, error: %v", nw.dev.link.Name, err)
  95. continue
  96. }
  97. managementIp := event.Lease.Attrs.PublicIP.String()
  98. networkPolicySettings := hcn.RemoteSubnetRoutePolicySetting{
  99. IsolationId: vxlanAttrs.VNI,
  100. DistributedRouterMacAddress: net.HardwareAddr(vxlanAttrs.VtepMAC).String(),
  101. ProviderAddress: managementIp,
  102. DestinationPrefix: event.Lease.Subnet.String(),
  103. }
  104. rawJSON, err := json.Marshal(networkPolicySettings)
  105. networkPolicy := hcn.NetworkPolicy{
  106. Type: hcn.RemoteSubnetRoute,
  107. Settings: rawJSON,
  108. }
  109. policyNetworkRequest := hcn.PolicyNetworkRequest{
  110. Policies: []hcn.NetworkPolicy{networkPolicy},
  111. }
  112. switch event.Type {
  113. case subnet.EventAdded:
  114. for _, policy := range hnsnetwork.Policies {
  115. if policy.Type == hcn.RemoteSubnetRoute {
  116. existingPolicySettings := hcn.RemoteSubnetRoutePolicySetting{}
  117. err = json.Unmarshal(policy.Settings, &existingPolicySettings)
  118. if err != nil {
  119. log.Error("Failed to unmarshal settings")
  120. }
  121. if existingPolicySettings.DestinationPrefix == networkPolicySettings.DestinationPrefix {
  122. existingJson, err := json.Marshal(existingPolicySettings)
  123. if err != nil {
  124. log.Error("Failed to marshal settings")
  125. }
  126. existingPolicy := hcn.NetworkPolicy{
  127. Type: hcn.RemoteSubnetRoute,
  128. Settings: existingJson,
  129. }
  130. existingPolicyNetworkRequest := hcn.PolicyNetworkRequest{
  131. Policies: []hcn.NetworkPolicy{existingPolicy},
  132. }
  133. hnsnetwork.RemovePolicy(existingPolicyNetworkRequest)
  134. }
  135. }
  136. }
  137. if networkPolicySettings.DistributedRouterMacAddress != "" {
  138. hnsnetwork.AddPolicy(policyNetworkRequest)
  139. }
  140. case subnet.EventRemoved:
  141. if networkPolicySettings.DistributedRouterMacAddress != "" {
  142. hnsnetwork.RemovePolicy(policyNetworkRequest)
  143. }
  144. default:
  145. log.Error("internal error: unknown event type: ", int(event.Type))
  146. }
  147. }
  148. }