base_dev.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 container_device
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "strings"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  23. "yunion.io/x/onecloud/pkg/util/fileutils2"
  24. )
  25. type BaseDevice struct {
  26. *isolated_device.SBaseDevice
  27. Path string
  28. }
  29. func NewBaseDevice(dev *isolated_device.PCIDevice, devType isolated_device.ContainerDeviceType, devPath string) *BaseDevice {
  30. return &BaseDevice{
  31. SBaseDevice: isolated_device.NewBaseDevice(dev, string(devType)),
  32. Path: devPath,
  33. }
  34. }
  35. func (b BaseDevice) GetVGACmd() string {
  36. return ""
  37. }
  38. func (b BaseDevice) GetCPUCmd() string {
  39. return ""
  40. }
  41. func (b BaseDevice) GetQemuId() string {
  42. return ""
  43. }
  44. func (c BaseDevice) CustomProbe(idx int) error {
  45. return nil
  46. }
  47. func (c BaseDevice) GetDevicePath() string {
  48. return c.Path
  49. }
  50. func (c *BaseDevice) SetDevicePath(devPath string) {
  51. c.Path = devPath
  52. }
  53. func (c BaseDevice) GetNvidiaMpsMemoryLimit() int {
  54. return -1
  55. }
  56. func (c BaseDevice) GetNvidiaMpsMemoryTotal() int {
  57. return -1
  58. }
  59. func (c BaseDevice) GetNvidiaMpsThreadPercentage() int {
  60. return -1
  61. }
  62. func (c BaseDevice) GetCardPath() string {
  63. return ""
  64. }
  65. func (c BaseDevice) GetRenderPath() string {
  66. return ""
  67. }
  68. func (c BaseDevice) GetNumaNode() (int, error) {
  69. if c.SBaseDevice == nil {
  70. return -1, nil
  71. }
  72. numaNodePath := fmt.Sprintf("/sys/bus/pci/devices/0000:%s/numa_node", c.SBaseDevice.GetOriginAddr())
  73. numaNode, err := fileutils2.FileGetIntContent(numaNodePath)
  74. if err != nil {
  75. log.Debugf("failed get numa node %s: %s", c.SBaseDevice.GetOriginAddr(), err)
  76. return -1, nil
  77. }
  78. return numaNode, nil
  79. }
  80. func CheckVirtualNumber(dev *isolated_device.ContainerDevice) error {
  81. if dev.VirtualNumber <= 0 {
  82. return errors.Errorf("virtual_number must > 0")
  83. }
  84. return nil
  85. }
  86. func getGPUPCIAddr(linkPartName string) (string, error) {
  87. if !strings.HasPrefix(linkPartName, "pci-") {
  88. return "", errors.Errorf("wrong link name: %s", linkPartName)
  89. }
  90. segs := strings.Split(linkPartName, "-")
  91. if len(segs) < 3 {
  92. return "", errors.Errorf("%s: segments length is less than 3 after splited by -", linkPartName)
  93. }
  94. fullAddr := segs[1]
  95. return fullAddr, nil
  96. }
  97. func newPCIGPURenderBaseDevice(devPath string, index int, devType isolated_device.ContainerDeviceType) (*BaseDevice, error) {
  98. dir := "/dev/dri/by-path/"
  99. entries, err := os.ReadDir(dir)
  100. if err != nil {
  101. return nil, errors.Wrap(err, "read dir")
  102. }
  103. for _, entry := range entries {
  104. entryName := entry.Name()
  105. fp := path.Join(dir, entryName)
  106. linkPath, err := os.Readlink(fp)
  107. if err != nil {
  108. return nil, errors.Wrapf(err, "read link of %s", entry.Name())
  109. }
  110. linkDevPath := path.Join(dir, linkPath)
  111. if linkDevPath == devPath {
  112. // get pci address
  113. if !strings.HasSuffix(entryName, "-render") {
  114. return nil, errors.Errorf("%s isn't render device", devPath)
  115. }
  116. pciAddr, err := getGPUPCIAddr(entryName)
  117. if err != nil {
  118. return nil, errors.Wrapf(err, "get pci address of %s", devPath)
  119. }
  120. pciOutput, err := isolated_device.GetPCIStrByAddr(pciAddr)
  121. if err != nil {
  122. return nil, errors.Wrapf(err, "GetPCIStrByAddr %s", pciAddr)
  123. }
  124. dev := isolated_device.NewPCIDevice2(pciOutput[0])
  125. devAddr := dev.Addr
  126. baseDev := NewBaseDevice(dev, devType, devPath)
  127. baseDev.SetAddr(fmt.Sprintf("%s-%d", devAddr, index), devAddr)
  128. return baseDev, nil
  129. }
  130. }
  131. return nil, errors.Wrapf(errors.ErrNotFound, "%s doesn't exist in %s", devPath, dir)
  132. }