net_int_device.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "regexp"
  18. "strconv"
  19. "strings"
  20. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. hostapi "yunion.io/x/onecloud/pkg/apis/host"
  24. "yunion.io/x/onecloud/pkg/hostman/isolated_device"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/util/fileutils2"
  27. "yunion.io/x/onecloud/pkg/util/procutils"
  28. )
  29. var (
  30. NetintCAASICReg = regexp.MustCompile("T4\\d\\d-.*")
  31. NetintCAQuadraReg = regexp.MustCompile("Quadra.*")
  32. )
  33. const (
  34. NETINT_VENDOR_ID = "0000"
  35. NETINT_DEVICE_ID = "0000"
  36. )
  37. func init() {
  38. isolated_device.RegisterContainerDeviceManager(newNetintDeviceManager(isolated_device.ContainerNetintCAASIC, NetintCAASICReg))
  39. isolated_device.RegisterContainerDeviceManager(newNetintDeviceManager(isolated_device.ContainerNetintCAQuadra, NetintCAQuadraReg))
  40. }
  41. type NetintDeviceInfo struct {
  42. Namespace int `json:"NameSpace"`
  43. DevicePath string `json:"DevicePath"`
  44. Firmware string `json:"Firmware"`
  45. Index int `json:"Index"`
  46. ModelNumber string `json:"ModelNumber"`
  47. ProductName string `json:"ProductName"`
  48. SerialNumber string `json:"SerialNumber"`
  49. UsedBytes int `json:"UsedBytes"`
  50. MaximumLBA int `json:"MaximumLBA"`
  51. PhysicalSize int `json:"PhysicalSize"`
  52. SectorSize int `json:"SectorSize"`
  53. }
  54. func (info NetintDeviceInfo) GetIndexFromPath() (int, error) {
  55. idxStr := strings.TrimSuffix(strings.TrimPrefix(info.DevicePath, "/dev/nvme"), "n1")
  56. idx, err := strconv.Atoi(idxStr)
  57. if err != nil {
  58. return -1, errors.Wrapf(err, "strconv.Atoi(%s) by %q", idxStr, info.DevicePath)
  59. }
  60. return idx, nil
  61. }
  62. func (info NetintDeviceInfo) GetIndex() (int, error) {
  63. if info.Index > 0 {
  64. return info.Index, nil
  65. }
  66. idx, err := info.GetIndexFromPath()
  67. if err != nil {
  68. return -1, err
  69. }
  70. return idx, nil
  71. }
  72. type netintDeviceManager struct {
  73. devType isolated_device.ContainerDeviceType
  74. devRegPattern *regexp.Regexp
  75. }
  76. func newNetintDeviceManager(devType isolated_device.ContainerDeviceType, reg *regexp.Regexp) *netintDeviceManager {
  77. return &netintDeviceManager{
  78. devType: devType,
  79. devRegPattern: reg,
  80. }
  81. }
  82. func (m *netintDeviceManager) GetType() isolated_device.ContainerDeviceType {
  83. return m.devType
  84. }
  85. func (m *netintDeviceManager) ProbeDevices() ([]isolated_device.IDevice, error) {
  86. return nil, nil
  87. }
  88. type NVMEListResult struct {
  89. Devices []*NetintDeviceInfo `json:"devices"`
  90. }
  91. func (m *netintDeviceManager) fetchNVMEDevices() ([]*NetintDeviceInfo, error) {
  92. out, err := procutils.NewRemoteCommandAsFarAsPossible("bash", "-c", "nvme list -o json").Output()
  93. if err != nil {
  94. return nil, errors.Wrap(err, "get nvme device json output")
  95. }
  96. obj, err := jsonutils.Parse(out)
  97. if err != nil {
  98. return nil, errors.Wrapf(err, "jsonutils.Parse %s", string(out))
  99. }
  100. output := new(NVMEListResult)
  101. if err := obj.Unmarshal(&output); err != nil {
  102. return nil, errors.Wrapf(err, "Unmarshal to NetIntDeviceInfo: %s", obj.String())
  103. }
  104. result := make([]*NetintDeviceInfo, 0)
  105. for _, dev := range output.Devices {
  106. if !m.devRegPattern.MatchString(dev.ModelNumber) {
  107. continue
  108. }
  109. tmpDev := dev
  110. result = append(result, tmpDev)
  111. }
  112. return result, nil
  113. }
  114. func (m *netintDeviceManager) checkDevPath(devs []*NetintDeviceInfo, dev *isolated_device.ContainerDevice) error {
  115. if dev.Path == "" {
  116. return nil
  117. }
  118. isFound := false
  119. for _, d := range devs {
  120. if d.DevicePath == dev.Path {
  121. isFound = true
  122. break
  123. }
  124. }
  125. if isFound {
  126. return nil
  127. }
  128. return fmt.Errorf("%s not found in %s", dev.Path, jsonutils.Marshal(devs))
  129. }
  130. func (m *netintDeviceManager) NewDevices(dev *isolated_device.ContainerDevice) ([]isolated_device.IDevice, error) {
  131. if err := CheckVirtualNumber(dev); err != nil {
  132. return nil, err
  133. }
  134. nvmeDevs, err := m.fetchNVMEDevices()
  135. if err != nil {
  136. return nil, errors.Wrap(err, "fetch nvme devices")
  137. }
  138. if err := m.checkDevPath(nvmeDevs, dev); err != nil {
  139. return nil, errors.Wrap(err, "check nvme devices")
  140. }
  141. result := make([]isolated_device.IDevice, 0)
  142. for _, nvmeDev := range nvmeDevs {
  143. for i := 0; i < dev.VirtualNumber; i++ {
  144. newDev, err := m.newDeviceByIndex(nvmeDev, i)
  145. if err != nil {
  146. return nil, errors.Wrapf(err, "newDeviceByIndex %#v %d", nvmeDev, i)
  147. }
  148. result = append(result, newDev)
  149. }
  150. }
  151. return result, nil
  152. }
  153. func (m *netintDeviceManager) newDeviceByIndex(dev *NetintDeviceInfo, idx int) (*netintDevice, error) {
  154. devIdx, err := dev.GetIndex()
  155. if err != nil {
  156. return nil, errors.Wrap(err, "dev.GetIndex")
  157. }
  158. devInfo := &isolated_device.PCIDevice{
  159. Addr: fmt.Sprintf("%d-%d", devIdx, idx),
  160. VendorId: NETINT_VENDOR_ID,
  161. DeviceId: NETINT_DEVICE_ID,
  162. ModelName: dev.ModelNumber,
  163. }
  164. nvmeDev := &netintDevice{
  165. BaseDevice: NewBaseDevice(devInfo, m.devType, dev.DevicePath),
  166. info: dev,
  167. }
  168. return nvmeDev, nil
  169. }
  170. func (m *netintDeviceManager) NewContainerDevices(_ *hostapi.ContainerCreateInput, input *hostapi.ContainerDevice) ([]*runtimeapi.Device, []*runtimeapi.Device, error) {
  171. dev := input.IsolatedDevice
  172. if !fileutils2.Exists(dev.Path) {
  173. return nil, nil, errors.Wrapf(httperrors.ErrNotFound, "device path %s doesn't exist", dev.Path)
  174. }
  175. charDevReg := regexp.MustCompile("(.*)n\\d+")
  176. charDevPath := charDevReg.FindAllStringSubmatch(dev.Path, -1)[0][1]
  177. ctrDevs := []*runtimeapi.Device{
  178. {
  179. HostPath: dev.Path,
  180. ContainerPath: dev.Path,
  181. Permissions: "rwm",
  182. },
  183. {
  184. HostPath: charDevPath,
  185. ContainerPath: charDevPath,
  186. Permissions: "rwm",
  187. },
  188. }
  189. return ctrDevs, nil, nil
  190. }
  191. func (m *netintDeviceManager) GetContainerExtraConfigures(devs []*hostapi.ContainerDevice) ([]*runtimeapi.KeyValue, []*runtimeapi.Mount) {
  192. return nil, nil
  193. }
  194. type netintDevice struct {
  195. *BaseDevice
  196. info *NetintDeviceInfo
  197. }
  198. func (d netintDevice) GetNVMESizeMB() int {
  199. return d.info.PhysicalSize / 1024 / 1024
  200. }