cph_amd_gpu.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "strings"
  17. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  18. "yunion.io/x/pkg/errors"
  19. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  20. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  21. )
  22. func init() {
  23. isolated_device.RegisterContainerDeviceManager(newCphAMDGPUManager())
  24. }
  25. type cphAMDGPUManager struct{}
  26. func newCphAMDGPUManager() *cphAMDGPUManager {
  27. return &cphAMDGPUManager{}
  28. }
  29. func (m *cphAMDGPUManager) ProbeDevices() ([]isolated_device.IDevice, error) {
  30. return nil, nil
  31. }
  32. func (m *cphAMDGPUManager) GetType() isolated_device.ContainerDeviceType {
  33. return isolated_device.ContainerDeviceTypeCphAMDGPU
  34. }
  35. func (m *cphAMDGPUManager) NewDevices(dev *isolated_device.ContainerDevice) ([]isolated_device.IDevice, error) {
  36. if !strings.HasPrefix(dev.Path, "/dev/dri/renderD") {
  37. return nil, errors.Errorf("device path %q doesn't start with /dev/dri/renderD", dev.Path)
  38. }
  39. if err := CheckVirtualNumber(dev); err != nil {
  40. return nil, err
  41. }
  42. gpuDevs := make([]isolated_device.IDevice, 0)
  43. for i := 0; i < dev.VirtualNumber; i++ {
  44. gpuDev, err := newCphAMDGPU(dev.Path, i)
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "new CPH AMD GPU with index %d", i)
  47. }
  48. gpuDevs = append(gpuDevs, gpuDev)
  49. }
  50. return gpuDevs, nil
  51. }
  52. func (m *cphAMDGPUManager) getDeviceHostPathByAddr(dev *hostapi.ContainerDevice) (string, error) {
  53. return dev.IsolatedDevice.Path, nil
  54. }
  55. func (m *cphAMDGPUManager) NewContainerDevices(input *hostapi.ContainerCreateInput, dev *hostapi.ContainerDevice) ([]*runtimeapi.Device, []*runtimeapi.Device, error) {
  56. hostPath, err := m.getDeviceHostPathByAddr(dev)
  57. if err != nil {
  58. return nil, nil, errors.Wrap(err, "get device host path")
  59. }
  60. cDev := &runtimeapi.Device{
  61. ContainerPath: "/dev/dri/renderD128",
  62. HostPath: hostPath,
  63. Permissions: "rwm",
  64. }
  65. return []*runtimeapi.Device{cDev}, nil, nil
  66. }
  67. func (m *cphAMDGPUManager) GetContainerExtraConfigures(devs []*hostapi.ContainerDevice) ([]*runtimeapi.KeyValue, []*runtimeapi.Mount) {
  68. return nil, nil
  69. }
  70. type cphAMDGPU struct {
  71. *BaseDevice
  72. }
  73. func newCphAMDGPU(devPath string, index int) (*cphAMDGPU, error) {
  74. dev, err := newPCIGPURenderBaseDevice(devPath, index, isolated_device.ContainerDeviceTypeCphAMDGPU)
  75. if err != nil {
  76. return nil, errors.Wrap(err, "new PCIGPURenderBaseDevice")
  77. }
  78. return &cphAMDGPU{
  79. BaseDevice: dev,
  80. }, nil
  81. }