vswitch.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 Yunion
  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 esxi
  15. import (
  16. "regexp"
  17. "github.com/coredns/coredns/plugin/pkg/log"
  18. "github.com/vmware/govmomi/property"
  19. "github.com/vmware/govmomi/vim25/mo"
  20. "github.com/vmware/govmomi/vim25/types"
  21. "yunion.io/x/pkg/errors"
  22. )
  23. var DVS_PROPS = []string{"name"}
  24. type IVirtualSwitch interface {
  25. FindNetworkByVlanID(vlanID int32) (IVMNetwork, error)
  26. }
  27. type SVirtualSwitch struct {
  28. Host *SHost
  29. HostVirtualSwitch types.HostVirtualSwitch
  30. }
  31. func (vs *SVirtualSwitch) FindNetworkByVlanID(vlanID int32) (IVMNetwork, error) {
  32. vsKey := vs.HostVirtualSwitch.Key
  33. pgs := vs.Host.getHostSystem().Config.Network.Portgroup
  34. var networkName string
  35. for i := range pgs {
  36. if pgs[i].Vswitch != vsKey {
  37. continue
  38. }
  39. if vlanEqual(pgs[i].Spec.VlanId, vlanID) {
  40. networkName = pgs[i].Spec.Name
  41. break
  42. }
  43. }
  44. if len(networkName) == 0 {
  45. return nil, nil
  46. }
  47. networks, err := vs.Host.GetNetworks()
  48. if err != nil {
  49. return nil, errors.Wrapf(err, "can't get networks of host %q", vs.Host.GetGlobalId())
  50. }
  51. for i := range networks {
  52. if networks[i].GetName() == networkName {
  53. return networks[i], nil
  54. }
  55. }
  56. return nil, nil
  57. }
  58. type SDistributedVirtualSwitch struct {
  59. Host *SHost
  60. DistributedVirtualSwitch mo.DistributedVirtualSwitch
  61. }
  62. func (vs *SDistributedVirtualSwitch) FindNetworkByVlanID(vlanID int32) (IVMNetwork, error) {
  63. var modvpgs []mo.DistributedVirtualPortgroup
  64. filter := property.Match{}
  65. filter["config.distributedVirtualSwitch"] = vs.DistributedVirtualSwitch.Self
  66. err := vs.Host.manager.scanMObjectsWithFilter(vs.Host.datacenter.object.Entity().Self, DVPORTGROUP_PROPS, &modvpgs, filter)
  67. if err != nil {
  68. return nil, errors.Wrapf(err, "can't fetch portgroup of DistributedVirtualSwitch %q", vs.DistributedVirtualSwitch.Name)
  69. }
  70. dvpgs := make([]*SDistributedVirtualPortgroup, 0, len(modvpgs))
  71. for i := range modvpgs {
  72. if modvpgs[i].Config.Uplink != nil && *modvpgs[i].Config.Uplink {
  73. log.Infof("dvpg %s is uplink, so skip", modvpgs[i].Name)
  74. continue
  75. }
  76. dvpgs = append(dvpgs, NewDistributedVirtualPortgroup(vs.Host.manager, &modvpgs[i], nil))
  77. }
  78. for i := range dvpgs {
  79. if vlanEqual(dvpgs[i].GetVlanId(), vlanID) {
  80. return dvpgs[i], nil
  81. }
  82. }
  83. return nil, nil
  84. }
  85. func vlanEqual(v1, v2 int32) bool {
  86. if v1 <= 1 && v2 <= 1 {
  87. return true
  88. }
  89. return v1 == v2
  90. }
  91. var (
  92. vsBridgeRegex = regexp.MustCompile(`^(host-\d+)/(.*)`)
  93. dvsBridgeRegex = regexp.MustCompile(`^dvs-\d+$`)
  94. )
  95. // config.distributedVirtualSwitch
  96. func findVirtualSwitch(host *SHost, bridge string) (IVirtualSwitch, error) {
  97. group := vsBridgeRegex.FindStringSubmatch(bridge)
  98. oHost := host.getHostSystem()
  99. if len(group) > 0 {
  100. // vswitch
  101. vsName := group[2]
  102. for _, vs := range oHost.Config.Network.Vswitch {
  103. if vs.Name != vsName {
  104. continue
  105. }
  106. return &SVirtualSwitch{
  107. Host: host,
  108. HostVirtualSwitch: vs,
  109. }, nil
  110. }
  111. return nil, nil
  112. }
  113. // distributed vswitch
  114. if !dvsBridgeRegex.MatchString(bridge) {
  115. return nil, nil
  116. }
  117. objRef := types.ManagedObjectReference{
  118. Type: "VmwareDistributedVirtualSwitch",
  119. Value: bridge,
  120. }
  121. var dvs mo.DistributedVirtualSwitch
  122. err := host.manager.reference2Object(objRef, DVS_PROPS, &dvs)
  123. if err != nil {
  124. return nil, errors.Wrapf(err, "can't fetch DistributedVirtualSwitch %q", objRef.String())
  125. }
  126. return &SDistributedVirtualSwitch{
  127. Host: host,
  128. DistributedVirtualSwitch: dvs,
  129. }, nil
  130. }