vxlan_network_windows.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. log "github.com/golang/glog"
  17. "golang.org/x/net/context"
  18. "sync"
  19. "github.com/coreos/flannel/backend"
  20. "github.com/coreos/flannel/subnet"
  21. "encoding/json"
  22. "github.com/Microsoft/hcsshim/hcn"
  23. "github.com/coreos/flannel/pkg/ip"
  24. "net"
  25. "strings"
  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. hnsnetwork, err := hcn.GetNetworkByName(nw.dev.link.Name)
  87. if err != nil {
  88. log.Errorf("Unable to find network %v, error: %v", nw.dev.link.Name, err)
  89. continue
  90. }
  91. managementIp := event.Lease.Attrs.PublicIP.String()
  92. networkPolicySettings := hcn.RemoteSubnetRoutePolicySetting{
  93. IsolationId: 4096,
  94. DistributedRouterMacAddress: net.HardwareAddr(vxlanAttrs.VtepMAC).String(),
  95. ProviderAddress: managementIp,
  96. DestinationPrefix: event.Lease.Subnet.String(),
  97. }
  98. rawJSON, err := json.Marshal(networkPolicySettings)
  99. networkPolicy := hcn.NetworkPolicy{
  100. Type: hcn.RemoteSubnetRoute,
  101. Settings: rawJSON,
  102. }
  103. policyNetworkRequest := hcn.PolicyNetworkRequest{
  104. Policies: []hcn.NetworkPolicy{networkPolicy},
  105. }
  106. switch event.Type {
  107. case subnet.EventAdded:
  108. for _, policy := range hnsnetwork.Policies {
  109. if policy.Type == hcn.RemoteSubnetRoute {
  110. existingPolicySettings := hcn.RemoteSubnetRoutePolicySetting{}
  111. err = json.Unmarshal(policy.Settings, &existingPolicySettings)
  112. if err != nil {
  113. log.Error("Failed to unmarshal settings")
  114. }
  115. if existingPolicySettings.DestinationPrefix == networkPolicySettings.DestinationPrefix {
  116. existingJson, err := json.Marshal(existingPolicySettings)
  117. if err != nil {
  118. log.Error("Failed to marshal settings")
  119. }
  120. existingPolicy := hcn.NetworkPolicy{
  121. Type: hcn.RemoteSubnetRoute,
  122. Settings: existingJson,
  123. }
  124. existingPolicyNetworkRequest := hcn.PolicyNetworkRequest{
  125. Policies: []hcn.NetworkPolicy{existingPolicy},
  126. }
  127. hnsnetwork.RemovePolicy(existingPolicyNetworkRequest)
  128. }
  129. }
  130. }
  131. if networkPolicySettings.DistributedRouterMacAddress != "" {
  132. hnsnetwork.AddPolicy(policyNetworkRequest)
  133. }
  134. case subnet.EventRemoved:
  135. if networkPolicySettings.DistributedRouterMacAddress != "" {
  136. hnsnetwork.RemovePolicy(policyNetworkRequest)
  137. }
  138. default:
  139. log.Error("internal error: unknown event type: ", int(event.Type))
  140. }
  141. }
  142. }