HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //usr/share/netplan/netplan_cli/cli/__pycache__/sriov.cpython-310.pyc
o

ϐxe�F�@s�ddlZddlZddlZddlZddlmZddlmZddlm	Z	ddl
Z
ddlZGdd�de�Z
d	eje
fd
d�Zd	eje
deje
fd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zddd�Zddd�ZdS)�N)�defaultdict�)�utils�)�ConfigurationErrorc@s�eZdZdZdefdd�Zedefdd��Zedefdd	��Zd
edefdd�Z	edefd
d��Z
edefdd��Zedefdd��Z
edefdd��Zedefdd��Zedefdd��Zdededefdd�Zdefdd�Zd S)!�	PCIDevicez.Helper class for interaction with a PCI device�pci_addrcCs
||_dS)znInitialise a new PCI device handler
        :param pci_addr: PCI address of device
        :type: str
        N�r)�selfr�r�+/usr/share/netplan/netplan_cli/cli/sriov.py�__init__'s
zPCIDevice.__init__�returncCsdS)zusysfs path (can be overridden for testing)
        :return: full path to /sys filesystem
        :rtype: str
        z/sysr�r
rrr�sys.sz
PCIDevice.syscCstj�|jd|j�S)zq/sys path for PCI device
        :return: full path to PCI device in /sys filesystem
        :rtype: str
        zbus/pci/devices)�os�path�joinrrrrrrr6szPCIDevice.path�subpathcCstj�|j|�S)z�/sys subpath helper for PCI device
        :param subpath: subpath to construct path for
        :type: str
        :return: self.path + subpath
        :rtype: str
        )rrr)r
rrrrr>szPCIDevice.subpathcCs2d}tj�|�d��rtj�t�|�d���}|S)zjKernel driver for PCI device
        :return: kernel driver in use for device
        :rtype: str
        ��driver)rr�existsr�basename�readlink)r
rrrrrGszPCIDevice.drivercC�tj�|�d��S)z�Determine if device is bound to a kernel driver
        :return: whether device is bound to a kernel driver
        :rtype: bool
        r�rrrrrrrr�boundR�zPCIDevice.boundcCr)zwDetermine if device is a SR-IOV Physical Function
        :return: whether device is a PF
        :rtype: bool
        �sriov_numvfsrrrrr�is_pfZrzPCIDevice.is_pfcCr)zvDetermine if device is a SR-IOV Virtual Function
        :return: whether device is a VF
        :rtype: bool
        �physfnrrrrr�is_vfbrzPCIDevice.is_vfcCsRg}d}	z|�tj�t�|�d�|�����Wn
ty#Y|Sw|d7}q)z�List Virtual Function addresses associated with a Physical Function
        :return: List of PCI addresses of Virtual Functions
        :rtype: list[str]
        rTzvirtfn{}r)�appendrrrrr�format�FileNotFoundError)r
�vf_addrs�irrrr%js����zPCIDevice.vf_addrscCsdd�|jD�S)z�List Virtual Function associated with a Physical Function
        :return: List of PCI devices of Virtual Functions
        :rtype: list[PCIDevice]
        cSsg|]}t|��qSr)r)�.0�addrrrr�
<listcomp>�sz!PCIDevice.vfs.<locals>.<listcomp>)r%rrrr�vfs~sz
PCIDevice.vfs�obj_name�prop�valuec	Cs$t�dd|dd�|j�||g�dS)z�Set devlink options for the PCI device
        :param obj_name: devlink object to set options on
        :type: str
        :param prop: property to set
        :type: str
        :param value: value to set for property
        :type: str
        z
/sbin/devlink�dev�setzpci/{}N)�
subprocess�
check_callr#r)r
r+r,r-rrr�devlink_set�s	
��zPCIDevice.devlink_setcCs|jS)zbString represenation of object
        :return: PCI address of string
        :rtype: str
        r	rrrr�__str__�szPCIDevice.__str__N)�__name__�
__module__�__qualname__�__doc__�strr
�propertyrrrr�boolrrr!�listr%r*r2r3rrrrr$s,	
rr*c	Cs^g}|D](}|js,td�|�d��}|�|j�|�|�Wd�n1s'wYq|S)zBind unbound VFs to driver.z/sys/bus/pci/drivers/{}/bind�wtN�r�openr#�writerr")r*r�	bound_vfs�vf�frrr�bind_vfs����rCrc	Cs^g}|D](}|jr,td�|�d��}|�|j�|�|�Wd�n1s'wYq|S)zUnbind bound VFs from driver.z/sys/bus/pci/drivers/{}/unbindr<Nr=)r*r�unbound_vfsrArBrrr�
unbind_vfs�rDrFcCs�||vrD||}|jr<|j}|r||vr|||<n+|D]}|j|t�|�t�|�d�s,q||vr6td|��|||<qn||vrD|||<|�|d�S)N)�
iface_name�iface_driver�	iface_macz3matched more than one interface for a PF device: %s)�
_has_match�set_name�_match_interfacer�get_interface_driver_name�get_interface_macaddressr�get)�
interfaces�np_state�pf_link�pfs�pf_devrK�	interfacerrr�_get_target_interface�s(
�
�rVc
Cs�tj�d|d�}z?t|��0}d}|��D]}|��}|�d�r3|�dd�d}|Wd�WSqWd�WdS1s@wYWdSty]}z
t	d|t
|�f��d}~ww)	z5
    Read PCI slot name for given interface name
    �/sys/class/netz
device/ueventNzPCI_SLOT_NAME=�=rrz'failed parsing PCI slot name for %s: %s)rrrr>�	readlines�strip�
startswith�split�IOError�RuntimeErrorr8)�netdev�uevent_pathrB�
pci_slot_name�line�errr�_get_pci_slot_name�s"

��&���rdc
Cs�|j��D]E\}}|j�d�rt|||j�d�j|�rd||<z|j}Wntjy7}zt	t
|���d}~ww|dkr=qt||||�}	|	rJ|||	<qdS)z�
    Go through the list of netplan ethernet devices and identify which are
    PFs and VFs, matching the former with actual networking interfaces.
    Count how many VFs each PF will need.
    �sriovNr)�	ethernets�items�linksrOrV�id�	_vf_count�netplan�NetplanExceptionrr8)
rPrQ�	vf_countsr*rS�nid�netdef�countrc�pfrrr�get_vf_count_and_functions�s $
����rrc

Cs"|dkrtd||f��tj�d|d�}tj�|d�}tj�|d�}zt|��}t|�����}Wd�n1s:wYWn"tyV}z
t	d|t
|�f��d}~wtybt	d	|��w||krptd
|||f��z!t|d��}|�t
|��Wd�WdS1s�wYWdSt�y}zrd}|j
d
kr�t�d|�z8t|d��
}|�d�Wd�n1s�wYt|d��}|�t
|��Wd�n1s�wYWnty�}	z|	}WYd}	~	nd}	~	wwd}|�rt	d||t
|�f��WYd}~dSd}~ww)zB
    Allocate the required number of VFs for the selected PF.
    �zDcannot allocate more VFs for PF %s than the SR-IOV maximum: %s > 256rW�devicer�sriov_totalvfsNz(failed parsing sriov_totalvfs for %s: %sz#invalid sriov_totalvfs value for %szKcannot allocate more VFs for PF %s than supported: %s > %s (sriov_totalvfs)�wT�zLdevice or resource busy while setting sriov_numvfs for %s, trying workaround�0Fz,failed setting sriov_numvfs to %s for %s: %s)rrrrr>�int�readrZr]r^r8�
ValueErrorr?�errno�logging�warning)
rq�vf_count�devdir�numvfs_path�
totalvfs_pathrB�vf_maxrc�bail�e_innerrrr�set_numvfs_for_pfsh
�
��������
�����
���r�c
Cs�tj�d|d�}zHttj�|d���}|����dd�}Wd�n1s'wYttj�|d���}|����dd�}Wd�n1sJwYWntyf}z
td|t|�f��d}~wwd�||g�}d}||vrw	dSdS)	z�
    Perform any hardware-specific quirks for the given SR-IOV device to make
    sure all the VF-count changes are applied.
    rWrt�vendorrNz2could not determine vendor and device ID of %s: %s�:r)	rrrr>rzrZr]r^r8)rqr�rB�	device_id�	vendor_idrc�combined_id�
quirk_devicesrrr� perform_hardware_specific_quirks1s$�����
�r��/cCs�d}tj�|d|d�}tj�t�|��}tj�|d|d�}t�|�D]"}	d|	vrDtj�||	�}
tj�t�|
��}||krD|	dd�}nq"|sOtd||f��ztjddd	d
|d|dt	|�g	tj
tj
d
�WdStjyutd|��w)z@
    Apply the hardware VLAN filtering for the selected VF.
    Nz
sys/class/netrt�virtfn�zAcould not determine the VF index for %s while configuring vlan %s�ip�linkr/r.rA�vlan)�stdout�stderrzJfailed setting SR-IOV VLAN filter for vlan %s (ip link set command failed))rrrrr�listdirr^r0r1r8�DEVNULL�CalledProcessError)rqrA�	vlan_name�vlan_id�prefix�vf_index�	vf_devdir�	vf_dev_id�	pf_devdirrB�dev_path�dev_idrrr�apply_vlan_filter_for_vfNs<�
�
����r�c
Cs^t��}|�|�t��}|�|�|��t��}|j}t	t
�}i}i}t|||||�g}|rE|��D]\}	}
t
|	|
�s?q5|�|	�q5|rT|D]}	t|	�qIt��}|D]/}||}|jr}|D]}
|j|
d�r{||vrw||rwtd|��|
||<qaqV||vr�|||<qV|��D]O\}}||}|j}|dvr�t|�}t|�}|jr�t�d�||j��|jr�|j}zt|j|j�|�dd|�W|s�t |j|j�q�|s�t |j|j�wwq�t!�}|j"��D]J\}}|j#�r,|j$�%d�}|j&}|�%|j'�}|�st�(d||j'f�q�|�%|j$�%d	�j'�}	||v�r td
||j'|f��t)|	|||�|�*|�q�dS)z
    Go through all interfaces, identify which ones are SR-IOV VFs, create
    them and perform all other necessary setup.
    )rGz3matched more than one interface for a VF device: %s)�	switchdev�legacyzFound VFs of {}: {}�eswitch�moder�zKSR-IOV vlan defined for %s but link %s is either not a VF or has no matchesrezJinterface %s for netplan device %s (%s) already has an SR-IOV vlan definedN)+rk�Parser�load_yaml_hierarchy�State�import_parser_results�parse�	netifacesrPrQrryrrrgr�r"r�rJrLr�_embedded_switch_moderdrrr}�debugr#r%r*�_delay_virtual_functions_rebindrFrr2rCr/�vlans�_has_sriov_vlan_filterrhrO�_vlan_idrir~r��add)�config_manager�rootdir�parserrQrPrmr*rS�vf_count_changedrqrrArorU�	netdef_id�iface�eswitch_moder�pcidev�rebind_delayed�filtered_vlans_setr�r�r�rrr�apply_sriov_configus�


�

��������
�
��r�)r�)r}rr0�typing�collectionsrrr�
configmanagerrrkr��objectr�IterablerCrFrVrdrrr�r�r�r�rrrr�<module>s&!/
'