nicteaming.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 fsdriver
  15. import (
  16. "fmt"
  17. "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  22. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  23. )
  24. func unmarshalNicConfigs(jsonNics []jsonutils.JSONObject) []types.SServerNic {
  25. nics := make([]types.SServerNic, 0)
  26. for i := range jsonNics {
  27. nic := types.SServerNic{}
  28. if err := jsonNics[i].Unmarshal(&nic); err == nil {
  29. nics = append(nics, nic)
  30. }
  31. }
  32. return nics
  33. }
  34. func findTeamingNic(nics []*types.SServerNic, mac string) *types.SServerNic {
  35. for i := range nics {
  36. if nics[i].TeamWith == mac {
  37. return nics[i]
  38. }
  39. }
  40. return nil
  41. }
  42. func ToServerNics(guestDesc *deployapi.GuestDesc, nics []*deployapi.Nic) []*types.SServerNic {
  43. ret := make([]*types.SServerNic, len(nics))
  44. for i := 0; i < len(nics); i++ {
  45. domain := nics[i].Domain
  46. if apis.IsIllegalSearchDomain(domain) {
  47. domain = ""
  48. }
  49. ret[i] = &types.SServerNic{
  50. Name: nics[i].Name,
  51. Index: int(nics[i].Index),
  52. Bridge: nics[i].Bridge,
  53. Domain: domain,
  54. Ip: nics[i].Ip,
  55. Vlan: int(nics[i].Vlan),
  56. Driver: nics[i].Driver,
  57. Masklen: int(nics[i].Masklen),
  58. Virtual: nics[i].Virtual,
  59. Manual: nics[i].Manual,
  60. WireId: nics[i].WireId,
  61. NetId: nics[i].NetId,
  62. Mac: nics[i].Mac,
  63. BandWidth: int(nics[i].Bw),
  64. Dns: nics[i].Dns,
  65. Net: nics[i].Net,
  66. Interface: nics[i].Interface,
  67. Gateway: nics[i].Gateway,
  68. Ifname: nics[i].Ifname,
  69. Routes: deployapi.ConvertRoutes(nics[i].Routes),
  70. NicType: compute.TNicType(nics[i].NicType),
  71. LinkUp: nics[i].LinkUp,
  72. Mtu: int16(nics[i].Mtu),
  73. TeamWith: nics[i].TeamWith,
  74. IsDefault: nics[i].IsDefault,
  75. Ip6: nics[i].Ip6,
  76. Masklen6: int(nics[i].Masklen6),
  77. Gateway6: nics[i].Gateway6,
  78. }
  79. if guestDesc.Hypervisor == computeapi.HYPERVISOR_BAREMETAL && ret[i].Vlan > 1 {
  80. ret[i].VlanInterface = true
  81. }
  82. }
  83. return ret
  84. }
  85. func convertNicConfigs(nics []*types.SServerNic) ([]*types.SServerNic, []*types.SServerNic) {
  86. allNics := make([]*types.SServerNic, 0)
  87. bondNics := make([]*types.SServerNic, 0)
  88. var netDevPrefix = GetNetDevPrefix(nics)
  89. for i := range nics {
  90. // skip nics without mac
  91. if len(nics[i].Mac) == 0 {
  92. continue
  93. }
  94. // skip team slave nics
  95. if len(nics[i].TeamWith) > 0 {
  96. continue
  97. }
  98. teamNic := findTeamingNic(nics, nics[i].Mac)
  99. if teamNic == nil {
  100. // no teaming nic
  101. nnic := nics[i]
  102. if nnic.NicType == computeapi.NIC_TYPE_INFINIBAND {
  103. nnic.Name = fmt.Sprintf("%s%d", GetIBNetDevPrefix(), nnic.Index)
  104. } else {
  105. nnic.Name = fmt.Sprintf("%s%d", netDevPrefix, nnic.Index)
  106. }
  107. allNics = append(allNics, nnic)
  108. continue
  109. }
  110. // copy nic into master and nnic
  111. master := nics[i]
  112. nnic := *nics[i]
  113. tnic := *teamNic
  114. nnic.Name = fmt.Sprintf("%s%d", netDevPrefix, nnic.Index)
  115. nnic.TeamingMaster = master
  116. nnic.Ip = ""
  117. nnic.Masklen = 0
  118. nnic.Gateway = ""
  119. nnic.Ip6 = ""
  120. nnic.Masklen6 = 0
  121. nnic.Gateway6 = ""
  122. nnic.IsDefault = false
  123. tnic.Name = fmt.Sprintf("%s%d", netDevPrefix, tnic.Index)
  124. tnic.TeamingMaster = master
  125. tnic.Ip = ""
  126. tnic.Masklen = 0
  127. tnic.Gateway = ""
  128. tnic.Ip6 = ""
  129. tnic.Masklen6 = 0
  130. tnic.Gateway6 = ""
  131. tnic.IsDefault = false
  132. master.Name = fmt.Sprintf("bond%d", len(bondNics))
  133. master.TeamingSlaves = []*types.SServerNic{&nnic, &tnic}
  134. // why reset master.Mac?
  135. // master.Mac = ""
  136. allNics = append(allNics, &nnic, &tnic, master)
  137. bondNics = append(bondNics, master)
  138. }
  139. return allNics, bondNics
  140. }