sriov_base.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "path"
  18. "strings"
  19. "yunion.io/x/log"
  20. "yunion.io/x/onecloud/pkg/util/fileutils2"
  21. "yunion.io/x/onecloud/pkg/util/procutils"
  22. )
  23. type sSRIOVBaseDevice struct {
  24. *SBaseDevice
  25. }
  26. func ensureNumvfsEqualTotalvfs(devDir string) error {
  27. sriovNumvfs := path.Join(devDir, "sriov_numvfs")
  28. sriovTotalvfs := path.Join(devDir, "sriov_totalvfs")
  29. numvfs, err := fileutils2.FileGetContents(sriovNumvfs)
  30. if err != nil {
  31. return err
  32. }
  33. totalvfs, err := fileutils2.FileGetContents(sriovTotalvfs)
  34. if err != nil {
  35. return err
  36. }
  37. log.Infof("numvfs %s total vfs %s", numvfs, totalvfs)
  38. if numvfs != totalvfs {
  39. return fileutils2.FilePutContents(sriovNumvfs, fmt.Sprintf("%s", totalvfs), false)
  40. }
  41. return nil
  42. }
  43. func detectSRIOVDevice(vfBDF string) (*PCIDevice, error) {
  44. dev, err := detectPCIDevByAddrWithoutIOMMUGroup(vfBDF)
  45. if err != nil {
  46. return nil, err
  47. }
  48. driver, err := dev.getKernelDriver()
  49. if err != nil {
  50. return nil, err
  51. }
  52. if driver == VFIO_PCI_KERNEL_DRIVER {
  53. return dev, nil
  54. }
  55. if driver != "" {
  56. if err = dev.unbindDriver(); err != nil {
  57. return nil, err
  58. }
  59. }
  60. if err = dev.bindDriver(); err != nil {
  61. return nil, err
  62. }
  63. return dev, nil
  64. }
  65. func newSRIOVBaseDevice(dev *PCIDevice, devType string) *sSRIOVBaseDevice {
  66. return &sSRIOVBaseDevice{
  67. SBaseDevice: NewBaseDevice(dev, devType),
  68. }
  69. }
  70. func (dev *sSRIOVBaseDevice) GetVGACmd() string {
  71. return ""
  72. }
  73. func (dev *sSRIOVBaseDevice) GetCPUCmd() string {
  74. return ""
  75. }
  76. func (dev *sSRIOVBaseDevice) GetWireId() string {
  77. return ""
  78. }
  79. func (dev *sSRIOVBaseDevice) GetQemuId() string {
  80. return fmt.Sprintf("dev_%s", strings.ReplaceAll(dev.GetAddr(), ":", "_"))
  81. }
  82. func (dev *sSRIOVBaseDevice) CustomProbe(idx int) error {
  83. // check environments on first probe
  84. if idx == 0 {
  85. for _, driver := range []string{"vfio", "vfio_iommu_type1", "vfio-pci"} {
  86. if err := procutils.NewRemoteCommandAsFarAsPossible("modprobe", driver).Run(); err != nil {
  87. return fmt.Errorf("modprobe %s: %v", driver, err)
  88. }
  89. }
  90. }
  91. driver, err := dev.GetKernelDriver()
  92. if err != nil {
  93. return fmt.Errorf("Nic %s is occupied by another driver: %s", dev.GetAddr(), driver)
  94. }
  95. return nil
  96. }