nic.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 baremetal
  15. import (
  16. "net"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "time"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/netutils"
  23. "yunion.io/x/pkg/util/regutils"
  24. o "yunion.io/x/onecloud/pkg/baremetal/options"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/types"
  26. "yunion.io/x/onecloud/pkg/util/dhcp"
  27. "yunion.io/x/onecloud/pkg/util/netutils2"
  28. )
  29. func GetNicDHCPConfig(
  30. n *types.SNic,
  31. serverIP net.IP,
  32. serverMac net.HardwareAddr,
  33. hostName string,
  34. isPxe bool,
  35. arch uint16,
  36. osName string,
  37. ) (*dhcp.ResponseConfig, error) {
  38. if n == nil {
  39. return nil, errors.Wrap(errors.ErrEmpty, "Nic is nil")
  40. }
  41. if n.IpAddr == "" && n.Ip6Addr == "" {
  42. return nil, errors.Wrap(errors.ErrEmpty, "Nic no ip or ip6 address")
  43. }
  44. routes4 := make([]netutils2.SRouteInfo, 0)
  45. routes6 := make([]netutils2.SRouteInfo, 0)
  46. for _, route := range n.Routes {
  47. routeInfo, err := netutils2.ParseRouteInfo([]string{route[0], route[1]})
  48. if err != nil {
  49. return nil, errors.Wrapf(err, "Parse route %s error: %q", route, err)
  50. }
  51. if regutils.MatchCIDR6(route[0]) {
  52. routes6 = append(routes6, *routeInfo)
  53. } else {
  54. routes4 = append(routes4, *routeInfo)
  55. }
  56. }
  57. isDefaultGW := false
  58. if n.IsDefault == nil || *n.IsDefault {
  59. isDefaultGW = true
  60. if n.Gateway != "" && !strings.HasPrefix(strings.ToLower(osName), "win") {
  61. routes4 = append(routes4, netutils2.SRouteInfo{
  62. SPrefixInfo: netutils2.SPrefixInfo{
  63. Prefix: net.ParseIP("0.0.0.0"),
  64. PrefixLen: 0,
  65. },
  66. Gateway: net.ParseIP(n.Gateway),
  67. })
  68. }
  69. if n.Gateway6 != "" {
  70. routes6 = append(routes6, netutils2.SRouteInfo{
  71. SPrefixInfo: netutils2.SPrefixInfo{
  72. Prefix: net.ParseIP("::"),
  73. PrefixLen: 0,
  74. },
  75. Gateway: net.ParseIP(n.Gateway6),
  76. })
  77. }
  78. }
  79. conf := &dhcp.ResponseConfig{
  80. InterfaceMac: serverMac,
  81. VlanId: uint16(n.VlanId),
  82. ServerIP: serverIP,
  83. Domain: n.Domain,
  84. OsName: osName,
  85. Hostname: strings.ToLower(hostName),
  86. Routes: routes4,
  87. Routes6: routes6,
  88. LeaseTime: time.Duration(o.Options.DhcpLeaseTime) * time.Second,
  89. RenewalTime: time.Duration(o.Options.DhcpRenewalTime) * time.Second,
  90. IsDefaultGW: isDefaultGW,
  91. }
  92. if n.IpAddr != "" {
  93. ipAddr, err := netutils.NewIPV4Addr(n.IpAddr)
  94. if err != nil {
  95. return nil, errors.Wrapf(err, "Parse IP address error: %q", n.IpAddr)
  96. }
  97. subnetMask := net.ParseIP(netutils.Masklen2Mask(n.Masklen).String())
  98. conf.ClientIP = net.ParseIP(ipAddr.String())
  99. conf.SubnetMask = subnetMask
  100. conf.BroadcastAddr = net.ParseIP(ipAddr.BroadcastAddr(n.Masklen).String())
  101. conf.Gateway = net.ParseIP(n.Gateway)
  102. }
  103. if n.Ip6Addr != "" {
  104. ip6Addr, err := netutils.NewIPV6Addr(n.Ip6Addr)
  105. if err != nil {
  106. return nil, errors.Wrapf(err, "Parse IPv6 address error: %q", n.Ip6Addr)
  107. }
  108. conf.ClientIP6 = net.ParseIP(ip6Addr.String())
  109. conf.PrefixLen6 = n.Masklen6
  110. conf.Gateway6 = net.ParseIP(n.Gateway6)
  111. }
  112. if len(n.Dns) > 0 {
  113. conf.DNSServers = make([]net.IP, 0)
  114. conf.DNSServers6 = make([]net.IP, 0)
  115. for _, dns := range strings.Split(n.Dns, ",") {
  116. if regutils.MatchIP4Addr(dns) {
  117. conf.DNSServers = append(conf.DNSServers, net.ParseIP(dns))
  118. } else if regutils.MatchIP6Addr(dns) {
  119. conf.DNSServers6 = append(conf.DNSServers6, net.ParseIP(dns))
  120. }
  121. }
  122. }
  123. if len(n.Ntp) > 0 {
  124. conf.NTPServers = make([]net.IP, 0)
  125. conf.NTPServers6 = make([]net.IP, 0)
  126. for _, ntp := range strings.Split(n.Ntp, ",") {
  127. if regutils.MatchIP4Addr(ntp) {
  128. conf.NTPServers = append(conf.NTPServers, net.ParseIP(ntp))
  129. } else if regutils.MatchIP6Addr(ntp) {
  130. conf.NTPServers6 = append(conf.NTPServers6, net.ParseIP(ntp))
  131. } else if regutils.MatchDomainName(ntp) {
  132. ntpAddrs, _ := net.LookupHost(ntp)
  133. for _, ntpAddr := range ntpAddrs {
  134. if regutils.MatchIP4Addr(ntpAddr) {
  135. conf.NTPServers = append(conf.NTPServers, net.ParseIP(ntpAddr))
  136. } else if regutils.MatchIP6Addr(ntpAddr) {
  137. conf.NTPServers6 = append(conf.NTPServers6, net.ParseIP(ntpAddr))
  138. }
  139. }
  140. }
  141. }
  142. }
  143. if n.Mtu > 0 {
  144. conf.MTU = uint16(n.Mtu)
  145. }
  146. if isPxe {
  147. conf.BootServer = serverIP.String()
  148. if len(o.Options.TftpBootServer) > 0 {
  149. conf.BootServer = o.Options.TftpBootServer
  150. }
  151. switch arch {
  152. case dhcp.CLIENT_ARCH_EFI_BC, dhcp.CLIENT_ARCH_EFI_X86_64:
  153. if o.Options.BootLoader == o.BOOT_LOADER_SYSLINUX {
  154. conf.BootFile = "bootx64.efi"
  155. } else {
  156. conf.BootFile = "grub_bootx64.efi"
  157. }
  158. case dhcp.CLIENT_ARCH_EFI_IA32:
  159. conf.BootFile = "bootia32.efi"
  160. case dhcp.CLIENT_ARCH_EFI_ARM64:
  161. conf.BootFile = "grub_arm64.efi"
  162. default:
  163. //if o.Options.EnableTftpHttpDownload {
  164. // bootFile = "lpxelinux.0"
  165. //}else {
  166. // bootFile := "pxelinux.0"
  167. //}
  168. if o.Options.BootLoader == o.BOOT_LOADER_SYSLINUX {
  169. conf.BootFile = "lpxelinux.0"
  170. } else {
  171. conf.BootFile = "grub_booti386"
  172. }
  173. }
  174. if len(o.Options.TftpBootFilename) > 0 {
  175. conf.BootFile = o.Options.TftpBootFilename
  176. }
  177. if len(o.Options.TftpBootServer) > 0 {
  178. conf.BootBlock = getPxeBlockSize(o.Options.TftpBootFilesize)
  179. } else {
  180. pxePath := filepath.Join(o.Options.TftpRoot, conf.BootFile)
  181. if f, err := os.Open(pxePath); err != nil {
  182. return nil, err
  183. } else {
  184. if info, err := f.Stat(); err != nil {
  185. return nil, err
  186. } else {
  187. conf.BootBlock = getPxeBlockSize(info.Size())
  188. }
  189. }
  190. }
  191. }
  192. return conf, nil
  193. }
  194. func getPxeBlockSize(pxeSize int64) uint16 {
  195. pxeBlk := pxeSize / 512
  196. if pxeSize > pxeBlk*512 {
  197. pxeBlk += 1
  198. }
  199. return uint16(pxeBlk)
  200. }