Esempio n. 1
0
File: utils.py Progetto: eepalms/SoD
def get_vf_num_by_pci_address(pci_addr):
    """Get the VF number based on a VF's pci address

    A VF is associated with an VF number, which ip link command uses to
    configure it. This number can be obtained from the PCI device filesystem.
    """
    VIRTFN_RE = re.compile("virtfn(\d+)")
    virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr)
    vf_num = None
    try:
        for vf_path in glob.iglob(virtfns_path):
            if re.search(pci_addr, os.readlink(vf_path)):
                t = VIRTFN_RE.search(vf_path)
                vf_num = t.group(1)
                break
    except Exception:
        pass
    if vf_num is None:
        raise exception.PciDeviceNotFoundById(id=pci_addr)
    return vf_num
Esempio n. 2
0
def get_mac_by_pci_address(pci_addr, pf_interface=False):
    """Get the MAC address of the nic based on it's PCI address

    Raises PciDeviceNotFoundById in case the pci device is not a NIC
    """
    dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface)
    if_name = get_ifname_by_pci_address(pci_addr, pf_interface)
    addr_file = os.path.join(dev_path, if_name, 'address')

    try:
        with open(addr_file) as f:
            mac = next(f).strip()
            return mac
    except (IOError, StopIteration) as e:
        LOG.warning(
            _LW("Could not find the expected sysfs file for "
                "determining the MAC address of the PCI device "
                "%(addr)s. May not be a NIC. Error: %(e)s"), {
                    'addr': pci_addr,
                    'e': e
                })
        raise exception.PciDeviceNotFoundById(id=pci_addr)
Esempio n. 3
0
    def _init_dev_details(self):
        self.vendor_id = self.tags.pop("vendor_id", ANY)
        self.product_id = self.tags.pop("product_id", ANY)
        self.address = self.tags.pop("address", None)
        self.dev_name = self.tags.pop("devname", None)

        self.vendor_id = self.vendor_id.strip()
        get_pci_dev_info(self, 'vendor_id', MAX_VENDOR_ID, '%04x')
        get_pci_dev_info(self, 'product_id', MAX_PRODUCT_ID, '%04x')

        pf = False
        if self.address and self.dev_name:
            raise exception.PciDeviceInvalidDeviceName()
        if not self.address:
            if self.dev_name:
                self.address, pf = utils.get_function_by_ifname(self.dev_name)
                if not self.address:
                    raise exception.PciDeviceNotFoundById(id=self.dev_name)
            else:
                self.address = "*:*:*.*"

        self.address = PciAddress(self.address, pf)
Esempio n. 4
0
 def fake_pci_device_get_by_id(context, id):
     raise exception.PciDeviceNotFoundById(id=id)
Esempio n. 5
0
def set_irqs_affinity_by_pci_address(pci_addr,
                                     flavor=None,
                                     numa_topology=None):
    """Set cpu affinity for list of PCI IRQs with a VF's pci address,
    but restrict cpuset to the numa node of the PCI.

    Return list
    Raises PciDeviceNotFoundById in case the pci device is not found,
    or when there is an underlying problem getting associated irqs.
    :param pci_addr: PCI address
    :param flavor: flavor
    :param numa_topology: instance numa topology
    :return: irqs, msi_irqs, numa_node, cpulist
    """
    irqs = set()
    msi_irqs = set()
    numa_node = None
    cpulist = ''

    if numa_topology is None:
        return (irqs, msi_irqs, numa_node, cpulist)

    # Get the irqs associated with pci addr
    _irqs, _msi_irqs = get_irqs_by_pci_address(pci_addr)

    # Obtain physical numa_node for this pci addr
    numa_path = "/sys/bus/pci/devices/%s/numa_node" % (pci_addr)
    try:
        with open(numa_path) as f:
            numa_node = [int(x) for x in f.readline().split()][0]
    except Exception as e:
        LOG.error(
            'set_irqs_affinity_by_pci_address: '
            'pci_addr=%(A)s: numa_path=%(P)s; error=%(E)s', {
                'A': pci_addr,
                'P': numa_path,
                'E': e
            })
        raise exception.PciDeviceNotFoundById(id=pci_addr)

    # Skip irq configuration if there is no associated numa node
    if numa_node is None or numa_node < 0:
        return (irqs, msi_irqs, numa_node, cpulist)

    # Determine the pinned cpuset where irqs are to be affined
    cpuset, cpulist = get_pci_irqs_pinned_cpuset(flavor=flavor,
                                                 numa_topology=numa_topology,
                                                 pci_numa_node=numa_node)

    # Skip irq configuration if there are no pinned cpus
    if not cpuset:
        return (irqs, msi_irqs, numa_node, cpulist)

    # Set IRQ affinity, but do not treat errors as fatal.
    for irq in _irqs:
        irq_aff_path = "/proc/irq/%s/smp_affinity_list" % (irq)
        try:
            with open(irq_aff_path, 'w') as f:
                f.write(cpulist)
            irqs.update([irq])
        except Exception as e:
            LOG.warning(
                "Could not affine pci_addr:%(A)s, irq:%(I)s, "
                "error=%(E)s", {
                    "A": pci_addr,
                    "I": irq,
                    "E": e
                })
    for irq in _msi_irqs:
        irq_aff_path = "/proc/irq/%s/smp_affinity_list" % (irq)
        try:
            with open(irq_aff_path, 'w') as f:
                f.write(cpulist)
            msi_irqs.update([irq])
        except Exception as e:
            LOG.warning(
                "Could not affine pci_addr:%(A)s, irq:%(I)s, "
                "error=%(E)s", {
                    "A": pci_addr,
                    "I": irq,
                    "E": e
                })
    return (irqs, msi_irqs, numa_node, cpulist)
Esempio n. 6
0
def get_irqs_by_pci_address(pci_addr):
    """Get list of PCI IRQs based on a VF's pci address

    Raises PciDeviceNotFoundById in case the pci device is not found,
    or when there is an underlying problem getting associated irqs.
    :param pci_addr: PCI address
    :return: irqs, msi_irqs
    """
    irqs = set()
    msi_irqs = set()

    dev_path = "/sys/bus/pci/devices/%s" % (pci_addr)
    if not os.path.isdir(dev_path):
        raise exception.PciDeviceNotFoundById(id=pci_addr)

    _irqs = set()
    irq_path = "/sys/bus/pci/devices/%s/irq" % (pci_addr)
    try:
        with open(irq_path) as f:
            _irqs.update([int(x) for x in f.readline().split() if int(x) > 0])
    except Exception as e:
        LOG.error(
            'get_irqs_by_pci_address: '
            'pci_addr=%(A)s: irq_path=%(P)s; error=%(E)s', {
                'A': pci_addr,
                'P': irq_path,
                'E': e
            })
        raise exception.PciDeviceNotFoundById(id=pci_addr)

    _msi_irqs = set()
    msi_path = "/sys/bus/pci/devices/%s/msi_irqs" % (pci_addr)
    try:
        _msi_irqs.update([int(x) for x in os.listdir(msi_path) if int(x) > 0])
    except OSError as e:
        # msi_path disappears during configuration; do not treat
        # non-existance as fatal
        if e.errno == errno.ENOENT:
            return (irqs, msi_irqs)
        else:
            LOG.error(
                'get_irqs_by_pci_address: '
                'pci_addr=%(A)s: msi_path=%(P)s; error=%(E)s', {
                    'A': pci_addr,
                    'P': msi_path,
                    'E': e
                })
            raise exception.PciDeviceNotFoundById(id=pci_addr)
    except Exception as e:
        LOG.error(
            'get_irqs_by_pci_address: '
            'pci_addr=%(A)s: msi_path=%(P)s; error=%(E)s', {
                'A': pci_addr,
                'P': msi_path,
                'E': e
            })
        raise exception.PciDeviceNotFoundById(id=pci_addr)

    # Return only configured irqs, ignore any that are missing.
    for irq in _irqs:
        irq_path = "/proc/irq/%s" % (irq)
        if os.path.isdir(irq_path):
            irqs.update([irq])
    for irq in _msi_irqs:
        irq_path = "/proc/irq/%s" % (irq)
        if os.path.isdir(irq_path):
            msi_irqs.update([irq])
    return (irqs, msi_irqs)