sriov_vgpu.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 isolated_device
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "path"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/util/fileutils2"
  25. )
  26. type sSRIOVGpuDevice struct {
  27. *sSRIOVBaseDevice
  28. }
  29. func NewSRIOVGpuDevice(dev *PCIDevice, devType string) *sSRIOVGpuDevice {
  30. return &sSRIOVGpuDevice{
  31. sSRIOVBaseDevice: newSRIOVBaseDevice(dev, devType),
  32. }
  33. }
  34. func (dev *sSRIOVGpuDevice) GetPfName() string {
  35. return ""
  36. }
  37. func (dev *sSRIOVGpuDevice) GetVirtfn() int {
  38. return -1
  39. }
  40. func getSRIOVGpus(gpuPF string) ([]*sSRIOVGpuDevice, error) {
  41. devicePath := fmt.Sprintf("/sys/bus/pci/devices/0000:%s", gpuPF)
  42. if !fileutils2.Exists(devicePath) {
  43. return nil, errors.Errorf("unknown device %s", gpuPF)
  44. }
  45. files, err := ioutil.ReadDir(devicePath)
  46. if err != nil {
  47. return nil, errors.Wrap(err, "read device path")
  48. }
  49. sriovGPUs := make([]*sSRIOVGpuDevice, 0)
  50. for i := range files {
  51. if strings.HasPrefix(files[i].Name(), "virtfn") {
  52. _, err := strconv.Atoi(files[i].Name()[len("virtfn"):])
  53. if err != nil {
  54. return nil, err
  55. }
  56. vfPath, err := filepath.EvalSymlinks(path.Join(devicePath, files[i].Name()))
  57. if err != nil {
  58. return nil, err
  59. }
  60. vfBDF := path.Base(vfPath)
  61. vfDev, err := detectSRIOVDevice(vfBDF)
  62. if err != nil {
  63. return nil, err
  64. }
  65. sriovGPUs = append(sriovGPUs, NewSRIOVGpuDevice(vfDev, compute.SRIOV_VGPU_TYPE))
  66. }
  67. }
  68. return sriovGPUs, err
  69. }