netplan.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/log"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  19. "yunion.io/x/onecloud/pkg/util/netplan"
  20. "yunion.io/x/onecloud/pkg/util/netutils2"
  21. )
  22. func NewNetplanConfig(allNics []*types.SServerNic, bondNics []*types.SServerNic, mainIp, mainIp6 string) *netplan.Configuration {
  23. network := newNetplanNetwork(allNics, bondNics, mainIp, mainIp6)
  24. return netplan.NewConfiguration(network)
  25. }
  26. func newNetplanNetwork(allNics []*types.SServerNic, bondNics []*types.SServerNic, mainIp, mainIp6 string) *netplan.Network {
  27. network := netplan.NewNetwork()
  28. nicCnt := len(allNics) - len(bondNics)
  29. for _, nic := range allNics {
  30. nicConf := getNetplanEthernetConfig(nic, false, mainIp, mainIp6, nicCnt)
  31. if nicConf == nil {
  32. continue
  33. }
  34. if nic.VlanInterface {
  35. ifname := fmt.Sprintf("%s.%d", nic.Name, nic.Vlan)
  36. vlanConfig := &netplan.VlanConfig{
  37. EthernetConfig: *nicConf,
  38. Link: nic.Name,
  39. Id: nic.Vlan,
  40. }
  41. network.AddVlan(ifname, vlanConfig)
  42. ethConfig := &netplan.EthernetConfig{
  43. DHCP4: false,
  44. DHCP6: false,
  45. MacAddress: nic.Mac,
  46. Match: netplan.NewEthernetConfigMatchMac(nic.Mac),
  47. }
  48. network.AddEthernet(nic.Name, ethConfig)
  49. } else {
  50. network.AddEthernet(nic.Name, nicConf)
  51. }
  52. }
  53. for _, bondNic := range bondNics {
  54. if len(bondNic.TeamingSlaves) < 2 {
  55. log.Warningf("BondNic %s slaves nic %#v less than 2", bondNic.Name, bondNic.TeamingSlaves)
  56. continue
  57. }
  58. var defaultMtu = int16(1442)
  59. interfaces := make([]string, len(bondNic.TeamingSlaves))
  60. for i, sn := range bondNic.TeamingSlaves {
  61. interfaces[i] = sn.Name
  62. nicConf := &netplan.EthernetConfig{
  63. DHCP4: false,
  64. MacAddress: sn.Mac,
  65. Match: netplan.NewEthernetConfigMatchMac(sn.Mac),
  66. }
  67. if sn.Mtu > 0 {
  68. nicConf.Mtu = sn.Mtu
  69. } else {
  70. nicConf.Mtu = defaultMtu
  71. }
  72. network.AddEthernet(sn.Name, nicConf)
  73. }
  74. netConf := getNetplanEthernetConfig(bondNic, true, mainIp, mainIp6, nicCnt)
  75. if netConf.Mtu == 0 {
  76. netConf.Mtu = defaultMtu
  77. }
  78. // TODO: implement kinds of bond mode config
  79. bondConf := netplan.NewBondMode4(netConf, interfaces)
  80. network.AddBond(bondNic.Name, bondConf)
  81. }
  82. return network
  83. }
  84. func getNetplanEthernetConfig(nic *types.SServerNic, isBond bool, mainIp, mainIp6 string, nicCnt int) *netplan.EthernetConfig {
  85. var nicConf *netplan.EthernetConfig
  86. if !isBond && (nic.TeamingMaster != nil || nic.TeamingSlaves != nil) {
  87. return nil
  88. } else if nic.Virtual {
  89. addr := fmt.Sprintf("%s/32", netutils2.PSEUDO_VIP)
  90. nicConf = netplan.NewStaticEthernetConfig(addr, "", "", "", nil, nil, nil)
  91. } else if nic.Manual {
  92. addr := fmt.Sprintf("%s/%d", nic.Ip, nic.Masklen)
  93. gateway := ""
  94. if nic.Ip == mainIp && len(mainIp) > 0 {
  95. gateway = nic.Gateway
  96. }
  97. addr6 := ""
  98. gateway6 := ""
  99. if len(nic.Ip6) > 0 {
  100. addr6 = fmt.Sprintf("%s/%d", nic.Ip6, nic.Masklen6)
  101. if nic.Ip6 == mainIp6 && len(mainIp6) > 0 {
  102. gateway6 = nic.Gateway6
  103. }
  104. }
  105. routeArrs4 := make([]netutils2.SRouteInfo, 0)
  106. routeArrs6 := make([]netutils2.SRouteInfo, 0)
  107. routeArrs4, routeArrs6 = netutils2.AddNicRoutes(routeArrs4, routeArrs6, nic, mainIp, mainIp6, nicCnt)
  108. var routes []*netplan.Route
  109. for _, route := range routeArrs4 {
  110. routes = append(routes, &netplan.Route{
  111. To: fmt.Sprintf("%s/%d", route.Prefix, route.PrefixLen),
  112. Via: route.Gateway.String(),
  113. })
  114. }
  115. for _, route := range routeArrs6 {
  116. routes = append(routes, &netplan.Route{
  117. To: fmt.Sprintf("%s/%d", route.Prefix, route.PrefixLen),
  118. Via: route.Gateway.String(),
  119. })
  120. }
  121. dns4list, dns6list := netutils2.GetNicDns(nic)
  122. dnslist := append(dns4list, dns6list...)
  123. nicConf = netplan.NewStaticEthernetConfig(
  124. addr, addr6, gateway, gateway6,
  125. []string{nic.Domain},
  126. dnslist,
  127. routes,
  128. )
  129. nicConf.MacAddress = nic.Mac
  130. if nic.Mtu > 0 {
  131. nicConf.Mtu = nic.Mtu
  132. }
  133. } else {
  134. // dhcp
  135. nicConf = netplan.NewDHCPEthernetConfig()
  136. if len(nic.Ip) > 0 {
  137. nicConf.EnableDHCP4()
  138. }
  139. if len(nic.Ip6) > 0 {
  140. nicConf.EnableDHCP6()
  141. }
  142. }
  143. return nicConf
  144. }