pci_device.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/utils"
  21. )
  22. type sGeneralPCIDevice struct {
  23. *SBaseDevice
  24. }
  25. func (dev *sGeneralPCIDevice) GetVGACmd() string {
  26. return ""
  27. }
  28. func (dev *sGeneralPCIDevice) GetCPUCmd() string {
  29. return ""
  30. }
  31. func (dev *sGeneralPCIDevice) GetQemuId() string {
  32. return fmt.Sprintf("dev_%s", strings.ReplaceAll(dev.GetAddr(), ":", "_"))
  33. }
  34. func newGeneralPCIDevice(dev *PCIDevice, devType string) *sGeneralPCIDevice {
  35. return &sGeneralPCIDevice{
  36. SBaseDevice: NewBaseDevice(dev, devType),
  37. }
  38. }
  39. func getPassthroughPCIDevs(devModel IsolatedDeviceModel, filteredCodes []string) ([]*sGeneralPCIDevice, error) {
  40. ret, err := bashOutput(fmt.Sprintf("lspci -d %s:%s -nnmm", devModel.VendorId, devModel.DeviceId))
  41. if err != nil {
  42. return nil, err
  43. }
  44. lines := []string{}
  45. for _, l := range ret {
  46. if len(l) != 0 {
  47. lines = append(lines, l)
  48. }
  49. }
  50. devs := []*sGeneralPCIDevice{}
  51. errs := make([]error, 0)
  52. for _, line := range lines {
  53. dev := NewPCIDevice2(line)
  54. if dev.ModelName == "" {
  55. dev.ModelName = devModel.Model
  56. }
  57. if utils.IsInStringArray(dev.ClassCode, filteredCodes) {
  58. continue
  59. }
  60. if err := dev.checkSameIOMMUGroupDevice(); err != nil {
  61. errs = append(errs, errors.Wrapf(err, "get dev %s iommu group devices by model: %s", dev.Addr, jsonutils.Marshal(devModel)))
  62. continue
  63. }
  64. devs = append(devs, newGeneralPCIDevice(dev, devModel.DevType))
  65. }
  66. return devs, errors.NewAggregate(errs)
  67. }