OpenStack Compute (Nova)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # Copyright (c) 2013 Intel, Inc.
  2. # Copyright (c) 2012 OpenStack Foundation
  3. # All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. import glob
  17. import os
  18. import re
  19. from oslo_log import log as logging
  20. import six
  21. from nova import exception
  22. LOG = logging.getLogger(__name__)
  23. PCI_VENDOR_PATTERN = "^(hex{4})$".replace("hex", r"[\da-fA-F]")
  24. _PCI_ADDRESS_PATTERN = ("^(hex{4}):(hex{2}):(hex{2}).(oct{1})$".
  25. replace("hex", r"[\da-fA-F]").
  26. replace("oct", "[0-7]"))
  27. _PCI_ADDRESS_REGEX = re.compile(_PCI_ADDRESS_PATTERN)
  28. _SRIOV_TOTALVFS = "sriov_totalvfs"
  29. def pci_device_prop_match(pci_dev, specs):
  30. """Check if the pci_dev meet spec requirement
  31. Specs is a list of PCI device property requirements.
  32. An example of device requirement that the PCI should be either:
  33. a) Device with vendor_id as 0x8086 and product_id as 0x8259, or
  34. b) Device with vendor_id as 0x10de and product_id as 0x10d8:
  35. [{"vendor_id":"8086", "product_id":"8259"},
  36. {"vendor_id":"10de", "product_id":"10d8",
  37. "capabilities_network": ["rx", "tx", "tso", "gso"]}]
  38. """
  39. def _matching_devices(spec):
  40. for k, v in spec.items():
  41. pci_dev_v = pci_dev.get(k)
  42. if isinstance(v, list) and isinstance(pci_dev_v, list):
  43. if not all(x in pci_dev.get(k) for x in v):
  44. return False
  45. else:
  46. # We don't need to check case for tags in order to avoid any
  47. # mismatch with the tags provided by users for port
  48. # binding profile and the ones configured by operators
  49. # with pci whitelist option.
  50. if isinstance(v, six.string_types):
  51. v = v.lower()
  52. if isinstance(pci_dev_v, six.string_types):
  53. pci_dev_v = pci_dev_v.lower()
  54. if pci_dev_v != v:
  55. return False
  56. return True
  57. return any(_matching_devices(spec) for spec in specs)
  58. def parse_address(address):
  59. """Returns (domain, bus, slot, function) from PCI address that is stored in
  60. PciDevice DB table.
  61. """
  62. m = _PCI_ADDRESS_REGEX.match(address)
  63. if not m:
  64. raise exception.PciDeviceWrongAddressFormat(address=address)
  65. return m.groups()
  66. def get_pci_address_fields(pci_addr):
  67. """Parse a fully-specified PCI device address.
  68. Does not validate that the components are valid hex or wildcard values.
  69. :param pci_addr: A string of the form "<domain>:<bus>:<slot>.<function>".
  70. :return: A 4-tuple of strings ("<domain>", "<bus>", "<slot>", "<function>")
  71. """
  72. dbs, sep, func = pci_addr.partition('.')
  73. domain, bus, slot = dbs.split(':')
  74. return domain, bus, slot, func
  75. def get_pci_address(domain, bus, slot, func):
  76. """Assembles PCI address components into a fully-specified PCI address.
  77. Does not validate that the components are valid hex or wildcard values.
  78. :param domain, bus, slot, func: Hex or wildcard strings.
  79. :return: A string of the form "<domain>:<bus>:<slot>.<function>".
  80. """
  81. return '%s:%s:%s.%s' % (domain, bus, slot, func)
  82. def get_function_by_ifname(ifname):
  83. """Given the device name, returns the PCI address of a device
  84. and returns True if the address is in a physical function.
  85. """
  86. dev_path = "/sys/class/net/%s/device" % ifname
  87. sriov_totalvfs = 0
  88. if os.path.isdir(dev_path):
  89. try:
  90. # sriov_totalvfs contains the maximum possible VFs for this PF
  91. with open(os.path.join(dev_path, _SRIOV_TOTALVFS)) as fd:
  92. sriov_totalvfs = int(fd.read())
  93. return (os.readlink(dev_path).strip("./"),
  94. sriov_totalvfs > 0)
  95. except (IOError, ValueError):
  96. return os.readlink(dev_path).strip("./"), False
  97. return None, False
  98. def is_physical_function(domain, bus, slot, function):
  99. dev_path = "/sys/bus/pci/devices/%(d)s:%(b)s:%(s)s.%(f)s/" % {
  100. "d": domain, "b": bus, "s": slot, "f": function}
  101. if os.path.isdir(dev_path):
  102. try:
  103. with open(dev_path + _SRIOV_TOTALVFS) as fd:
  104. sriov_totalvfs = int(fd.read())
  105. return sriov_totalvfs > 0
  106. except (IOError, ValueError):
  107. pass
  108. return False
  109. def _get_sysfs_netdev_path(pci_addr, pf_interface):
  110. """Get the sysfs path based on the PCI address of the device.
  111. Assumes a networking device - will not check for the existence of the path.
  112. """
  113. if pf_interface:
  114. return "/sys/bus/pci/devices/%s/physfn/net" % pci_addr
  115. return "/sys/bus/pci/devices/%s/net" % pci_addr
  116. def get_ifname_by_pci_address(pci_addr, pf_interface=False):
  117. """Get the interface name based on a VF's pci address.
  118. The returned interface name is either the parent PF's or that of the VF
  119. itself based on the argument of pf_interface.
  120. """
  121. dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface)
  122. try:
  123. dev_info = os.listdir(dev_path)
  124. return dev_info.pop()
  125. except Exception:
  126. raise exception.PciDeviceNotFoundById(id=pci_addr)
  127. def get_mac_by_pci_address(pci_addr, pf_interface=False):
  128. """Get the MAC address of the nic based on its PCI address.
  129. Raises PciDeviceNotFoundById in case the pci device is not a NIC
  130. """
  131. dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface)
  132. if_name = get_ifname_by_pci_address(pci_addr, pf_interface)
  133. addr_file = os.path.join(dev_path, if_name, 'address')
  134. try:
  135. with open(addr_file) as f:
  136. mac = next(f).strip()
  137. return mac
  138. except (IOError, StopIteration) as e:
  139. LOG.warning("Could not find the expected sysfs file for "
  140. "determining the MAC address of the PCI device "
  141. "%(addr)s. May not be a NIC. Error: %(e)s",
  142. {'addr': pci_addr, 'e': e})
  143. raise exception.PciDeviceNotFoundById(id=pci_addr)
  144. def get_vf_num_by_pci_address(pci_addr):
  145. """Get the VF number based on a VF's pci address
  146. A VF is associated with an VF number, which ip link command uses to
  147. configure it. This number can be obtained from the PCI device filesystem.
  148. """
  149. VIRTFN_RE = re.compile(r"virtfn(\d+)")
  150. virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr)
  151. vf_num = None
  152. try:
  153. for vf_path in glob.iglob(virtfns_path):
  154. if re.search(pci_addr, os.readlink(vf_path)):
  155. t = VIRTFN_RE.search(vf_path)
  156. vf_num = t.group(1)
  157. break
  158. except Exception:
  159. pass
  160. if vf_num is None:
  161. raise exception.PciDeviceNotFoundById(id=pci_addr)
  162. return vf_num
  163. def get_net_name_by_vf_pci_address(vfaddress):
  164. """Given the VF PCI address, returns the net device name.
  165. Every VF is associated to a PCI network device. This function
  166. returns the libvirt name given to this network device; e.g.:
  167. <device>
  168. <name>net_enp8s0f0_90_e2_ba_5e_a6_40</name>
  169. ...
  170. In the libvirt parser information tree, the network device stores the
  171. network capabilities associated to this device.
  172. """
  173. try:
  174. mac = get_mac_by_pci_address(vfaddress).split(':')
  175. ifname = get_ifname_by_pci_address(vfaddress)
  176. return ("net_%(ifname)s_%(mac)s" %
  177. {'ifname': ifname, 'mac': '_'.join(mac)})
  178. except Exception:
  179. LOG.warning("No net device was found for VF %(vfaddress)s",
  180. {'vfaddress': vfaddress})
  181. return