Exemple #1
0
def get_ifname_by_pci_address(pci_addr, pf_interface=False, switchdev=False):
    """Get the interface name based on a VF's pci address

    :param pci_addr: the PCI address of the VF
    :param pf_interface: if True, look for the netdev of the parent PF
    :param switchdev: if True, ensure that phys_switch_id is valid

    :returns: netdev interface name

    The returned interface name is either the parent PF or that of the VF
    itself based on the argument of pf_interface.
    """
    dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface)
    # make the if statement later more readable
    ignore_switchdev = not switchdev
    try:
        for netdev in os.listdir(dev_path):
            if ignore_switchdev or _is_switchdev(netdev):
                return netdev
    except Exception:
        raise exception.PciDeviceNotFoundById(id=pci_addr)
    raise exception.PciDeviceNotFoundById(id=pci_addr)
Exemple #2
0
def get_ifname_by_pci_address(pci_addr, pf_interface=False, switchdev=False):
    """Get the interface name based on a VF's pci address

    :param pci_addr: the PCI address of the VF
    :param pf_interface: if True, look for the netdev of the parent PF
    :param switchdev: if True, ensure that phys_switch_id is valid

    :returns: netdev interface name

    The returned interface name is either the parent PF or that of the VF
    itself based on the argument of pf_interface.
    """
    dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface)
    try:
        devices = os.listdir(dev_path)

        # Return the first netdev in case of switchdev=False
        if not switchdev:
            return devices[0]
        elif pf_interface:
            fallback_netdev = None
            for netdev in devices:
                # Return the uplink representor in case of switchdev=True
                if _is_switchdev(netdev):
                    fallback_netdev = netdev if fallback_netdev is None \
                        else fallback_netdev
                    phys_port_name = _get_phys_port_name(netdev)
                    if phys_port_name is not None and \
                            UPLINK_PORT_RE.search(phys_port_name):
                        return netdev

            # Fallback to first switchdev netdev in case of switchdev=True
            if fallback_netdev is not None:
                return fallback_netdev

    except Exception:
        raise exception.PciDeviceNotFoundById(id=pci_addr)
    raise exception.PciDeviceNotFoundById(id=pci_addr)
Exemple #3
0
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.
    """
    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
Exemple #4
0
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.
    """
    virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr)
    vf_num = None
    try:
        #pci_addr是一个vf的pci地址
        #遍历vf对应的pf下所有virtfn,如果某一个vf的路径中有指定的pci_addr,则
        #通过virtfn*名称即可知道它是第几个vf
        #例如:
        #/sys/bus/pci/devices/0000:af:05.0/physfn/virtfn38 -> ../0000:af:05.0
        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