pci.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 desc
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. )
  19. type PCI_CONTROLLER_TYPE string
  20. const (
  21. CONTROLLER_TYPE_PCI_ROOT PCI_CONTROLLER_TYPE = "pci-root"
  22. CONTROLLER_TYPE_PCIE_ROOT = "pcie-root"
  23. CONTROLLER_TYPE_PCI_BRIDGE = "pci-bridge"
  24. CONTROLLER_TYPE_DMI_TO_PCI_BRIDGE = "dmi-to-pci-bridge"
  25. CONTROLLER_TYPE_PCIE_TO_PCI_BRIDGE = "pcie-to-pci-bridge"
  26. CONTROLLER_TYPE_PCIE_ROOT_PORT = "pcie-root-port"
  27. CONTROLLER_TYPE_PCIE_SWITCH_UPSTREAM_PORT = "pcie-switch-upstream-port"
  28. CONTROLLER_TYPE_PCIE_SWITCH_DOWNSTREAM_PORT = "pcie-switch-downstream-port"
  29. CONTROLLER_TYPE_PCI_EXPANDER_BUS = "pci-expander-bus"
  30. CONTROLLER_TYPE_PCIE_EXPANDER_BUS = "pcie-expander-bus"
  31. )
  32. type PCIDevice struct {
  33. *PCIAddr `json:",omitempty"`
  34. DevType string
  35. Id string
  36. Controller PCI_CONTROLLER_TYPE
  37. Options map[string]string `json:",omitempty"`
  38. }
  39. // %04x:%02x:%02x.%x, domain, bus, slot, function
  40. type PCIAddr struct {
  41. Domain uint
  42. Bus uint
  43. Slot uint
  44. Function uint
  45. Multi *bool
  46. }
  47. func (addr *PCIAddr) String() string {
  48. return fmt.Sprintf("%04x:%02x:%02x.%x", addr.Domain, addr.Bus, addr.Slot, addr.Function)
  49. }
  50. func (addr *PCIAddr) Copy() *PCIAddr {
  51. return &PCIAddr{
  52. Domain: addr.Domain,
  53. Bus: addr.Bus,
  54. Slot: addr.Slot,
  55. Function: addr.Function,
  56. }
  57. }
  58. func (d *PCIDevice) BusStr() string {
  59. switch d.Controller {
  60. case CONTROLLER_TYPE_PCIE_ROOT, CONTROLLER_TYPE_PCIE_EXPANDER_BUS:
  61. return fmt.Sprintf("pcie.%d", d.Bus)
  62. default:
  63. return fmt.Sprintf("pci.%d", d.Bus)
  64. }
  65. }
  66. func (d *PCIDevice) SlotFunc() string {
  67. if d.Function > 0 {
  68. return fmt.Sprintf("0x%02x.%x", d.Slot, d.Function)
  69. } else {
  70. return fmt.Sprintf("0x%02x", d.Slot)
  71. }
  72. }
  73. func (d *PCIDevice) MultiFunction() string {
  74. if d.Multi == nil {
  75. return ""
  76. } else {
  77. if *d.Multi {
  78. return "multifunction=on"
  79. } else {
  80. return "multifunction=off"
  81. }
  82. }
  83. }
  84. func OptionsToString(options map[string]string) string {
  85. var cmd string
  86. for key, value := range options {
  87. if value != "" {
  88. cmd += fmt.Sprintf(",%s=%s", key, value)
  89. } else {
  90. cmd += fmt.Sprintf(",%s", key)
  91. }
  92. }
  93. return cmd
  94. }
  95. func (d *PCIDevice) OptionsStr() string {
  96. cmd := ""
  97. if d.PCIAddr != nil {
  98. cmd += fmt.Sprintf("bus=%s,addr=%s", d.BusStr(), d.SlotFunc())
  99. if d.Multi != nil {
  100. cmd += fmt.Sprintf(",%s", d.MultiFunction())
  101. }
  102. }
  103. cmd += OptionsToString(d.Options)
  104. return cmd
  105. }
  106. // pvscsi or virtio-scsi-pci
  107. type SCSIAddr struct {
  108. // The LUN identifies the specific logical unit
  109. // to the SCSI initiator when combined with
  110. // information such as the target port identifier.
  111. Lun uint
  112. Bus uint
  113. Controller uint
  114. }
  115. type SCSIDevice struct {
  116. *SCSIAddr `json:",omitempty"`
  117. Id string
  118. DevType string
  119. ControllerId string
  120. Options map[string]string `json:",omitempty"`
  121. }
  122. type IDEAddr struct {
  123. Unit uint
  124. Bus uint
  125. Controller uint
  126. }
  127. type IDEDevice struct {
  128. *IDEAddr
  129. Id string
  130. DevType string
  131. Options map[string]string `json:",omitempty"`
  132. }
  133. type FloppyAddr struct {
  134. Bus uint
  135. Controller uint
  136. }
  137. type FloppyDevice struct {
  138. *FloppyAddr
  139. Id string
  140. DevType string
  141. Options map[string]string `json:",omitempty"`
  142. }
  143. type Object struct {
  144. ObjType string
  145. Id string
  146. Options map[string]string `json:",omitempty"`
  147. }
  148. type CharDev struct {
  149. Backend string
  150. Id string
  151. Name string
  152. Options map[string]string `json:",omitempty"`
  153. }
  154. const (
  155. PCI_ADDRESS_SLOT_MAX = 31
  156. )
  157. const QEMU_GUEST_PCIE_BUS_MAX = 16
  158. type SGuestPCIAddressSlot struct {
  159. Function uint8
  160. }
  161. type SGuestPCIAddressBus struct {
  162. Slots []*SGuestPCIAddressSlot
  163. /* usually 0,0 or 0,31, or 1,31 */
  164. MinSlot uint
  165. MaxSlot uint
  166. Contorller PCI_CONTROLLER_TYPE
  167. // Hotplugable bool
  168. }
  169. type SGuestPCIAddresses struct {
  170. Buses []*SGuestPCIAddressBus
  171. }
  172. func (b *SGuestPCIAddresses) ReleasePCIAddress(addr *PCIAddr) error {
  173. if int(addr.Bus+1) > len(b.Buses) {
  174. return errors.Errorf("release pci address bus %02x out of range", addr.Bus)
  175. }
  176. bus := b.Buses[addr.Bus]
  177. return bus.ReleaseSlotFunction(addr.Slot, addr.Function)
  178. }
  179. func (b *SGuestPCIAddresses) IsAddrInUse(addr *PCIAddr) (error, bool) {
  180. if int(addr.Bus+1) > len(b.Buses) {
  181. return errors.Errorf("release pci address bus %02x out of range", addr.Bus), false
  182. }
  183. bus := b.Buses[addr.Bus]
  184. return bus.IsSlotFunctionInUse(addr.Slot, addr.Function)
  185. }
  186. func (b *SGuestPCIAddressBus) EnsureSlotFunction(slot, function uint) error {
  187. if b.Slots == nil {
  188. b.Slots = make([]*SGuestPCIAddressSlot, 0, b.MaxSlot+1)
  189. }
  190. if slot < b.MinSlot || slot > b.MaxSlot {
  191. return errors.Errorf("slot %02x out of range %02x~%02x", slot, b.MinSlot, b.MaxSlot)
  192. }
  193. if function >= 8 {
  194. return errors.Errorf("function %x out of range 0~7", function)
  195. }
  196. if int(slot+1) > len(b.Slots) {
  197. b.Slots = b.Slots[:slot+1]
  198. }
  199. if b.Slots[slot] == nil {
  200. b.Slots[slot] = new(SGuestPCIAddressSlot)
  201. }
  202. if (b.Slots[slot].Function & (1 << function)) > 0 {
  203. return errors.Errorf("slot %02x function %x is in use", slot, function)
  204. }
  205. b.setSlotFunction(slot, function)
  206. return nil
  207. }
  208. func (b *SGuestPCIAddressBus) setSlotFunction(slot, function uint) {
  209. b.Slots[slot].Function |= 1 << function
  210. }
  211. func (b *SGuestPCIAddressBus) IsSlotFunctionInUse(slot, function uint) (error, bool) {
  212. if slot < b.MinSlot || slot > b.MaxSlot {
  213. return errors.Errorf("slot %02x out of range %02x~%02x", slot, b.MinSlot, b.MaxSlot), false
  214. }
  215. if function >= 8 {
  216. return errors.Errorf("function %x out of range 0~7", function), false
  217. }
  218. return nil, (b.Slots[slot].Function & (1 << function)) > 0
  219. }
  220. func (b *SGuestPCIAddressBus) ReleaseSlotFunction(slot, function uint) error {
  221. if slot < b.MinSlot || slot > b.MaxSlot {
  222. return errors.Errorf("slot %02x out of range %02x~%02x", slot, b.MinSlot, b.MaxSlot)
  223. }
  224. if function >= 8 {
  225. return errors.Errorf("function %x out of range 0~7", function)
  226. }
  227. b.Slots[slot].Function &= ^(1 << function)
  228. return nil
  229. }
  230. func (b *SGuestPCIAddressBus) FindNextUnusedSlot(function uint) int {
  231. if b.Slots == nil {
  232. b.Slots = make([]*SGuestPCIAddressSlot, 0, b.MaxSlot+1)
  233. }
  234. var slot = b.MinSlot
  235. for ; slot <= b.MaxSlot; slot++ {
  236. if int(slot+1) > len(b.Slots) {
  237. b.Slots = b.Slots[:slot+1]
  238. }
  239. if b.Slots[slot] == nil {
  240. b.Slots[slot] = new(SGuestPCIAddressSlot)
  241. }
  242. if (b.Slots[slot].Function & (1 << function)) > 0 {
  243. continue
  244. } else {
  245. break
  246. }
  247. }
  248. if slot > b.MaxSlot {
  249. return -1
  250. }
  251. return int(slot)
  252. }
  253. func NewGuestPCIAddressBus(controller PCI_CONTROLLER_TYPE) (*SGuestPCIAddressBus, error) {
  254. bus := &SGuestPCIAddressBus{
  255. Contorller: controller,
  256. }
  257. switch controller {
  258. case CONTROLLER_TYPE_PCI_ROOT,
  259. CONTROLLER_TYPE_PCIE_ROOT,
  260. CONTROLLER_TYPE_PCI_BRIDGE,
  261. CONTROLLER_TYPE_PCIE_TO_PCI_BRIDGE:
  262. bus.MaxSlot = PCI_ADDRESS_SLOT_MAX
  263. bus.MinSlot = 1
  264. case CONTROLLER_TYPE_PCIE_ROOT_PORT, CONTROLLER_TYPE_PCIE_SWITCH_DOWNSTREAM_PORT:
  265. bus.MaxSlot = 0
  266. bus.MinSlot = 0
  267. case CONTROLLER_TYPE_DMI_TO_PCI_BRIDGE,
  268. CONTROLLER_TYPE_PCIE_SWITCH_UPSTREAM_PORT,
  269. CONTROLLER_TYPE_PCI_EXPANDER_BUS,
  270. CONTROLLER_TYPE_PCIE_EXPANDER_BUS:
  271. bus.MaxSlot = PCI_ADDRESS_SLOT_MAX
  272. bus.MinSlot = 0
  273. default:
  274. return nil, errors.Errorf("unknown controller type")
  275. }
  276. return bus, nil
  277. }
  278. func NewPCIController(controller PCI_CONTROLLER_TYPE) *PCIController {
  279. return &PCIController{
  280. CType: controller,
  281. }
  282. }
  283. func NewPCIDevice(controller PCI_CONTROLLER_TYPE, deviceType, id string) *PCIDevice {
  284. return &PCIDevice{
  285. Id: id,
  286. Controller: controller,
  287. DevType: deviceType,
  288. }
  289. }
  290. func NewVfioDevice(controller PCI_CONTROLLER_TYPE, deviceType, id, hostAddr string, hasXVga bool) *VFIODevice {
  291. return &VFIODevice{
  292. PCIDevice: NewPCIDevice(controller, deviceType, id),
  293. HostAddr: hostAddr,
  294. XVga: hasXVga,
  295. }
  296. }
  297. func NewScsiDevice(controller, deviceType, id string) *SCSIDevice {
  298. return &SCSIDevice{
  299. ControllerId: controller,
  300. DevType: deviceType,
  301. Id: id,
  302. }
  303. }
  304. func NewIdeDevice(deviceType, id string) *IDEDevice {
  305. return &IDEDevice{
  306. Id: id,
  307. DevType: deviceType,
  308. }
  309. }
  310. func NewUsbDevice(deviceType, id string) *UsbDevice {
  311. return &UsbDevice{
  312. Id: id,
  313. DevType: deviceType,
  314. }
  315. }
  316. func NewObject(objType, id string) *Object {
  317. return &Object{
  318. ObjType: objType,
  319. Id: id,
  320. }
  321. }
  322. func NewCharDev(backend, id, name string) *CharDev {
  323. return &CharDev{
  324. Backend: backend,
  325. Id: id,
  326. Name: name,
  327. }
  328. }
  329. func NewUsbController(masterbus string, port int) *UsbController {
  330. uc := &UsbController{}
  331. if len(masterbus) > 0 {
  332. uc.MasterBus = &UsbMasterBus{
  333. Masterbus: masterbus,
  334. Port: port,
  335. }
  336. }
  337. return uc
  338. }
  339. func (m *SGuestMem) GuestNumaNodeCount() int {
  340. if m.Mem == nil {
  341. return 0
  342. }
  343. return len(m.Mem.Mems) + 1
  344. }
  345. func NewMemDesc(objType, id string, nodeId *uint16, cpus *string) *SMemDesc {
  346. md := &SMemDesc{
  347. Object: NewObject(objType, id),
  348. NodeId: nodeId,
  349. Cpus: cpus,
  350. }
  351. return md
  352. }
  353. func (m *SMemDesc) SetHostNodes(hostNode int) {
  354. if hostNode >= 0 {
  355. m.Options["host-nodes"] = fmt.Sprintf("%d", hostNode)
  356. m.Options["policy"] = "bind"
  357. } else {
  358. delete(m.Options, "host-nodes")
  359. delete(m.Options, "policy")
  360. }
  361. }
  362. func NewMemsDesc(defaultDesc SMemDesc, appendDesc []SMemDesc) *SMemsDesc {
  363. return &SMemsDesc{
  364. SMemDesc: defaultDesc,
  365. Mems: appendDesc,
  366. }
  367. }