HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux mail.btech-izolacje.pl 5.15.0-140-generic #150-Ubuntu SMP Sat Apr 12 06:00:09 UTC 2025 x86_64
User: pewna6876 (1017)
PHP: 8.2.28
Disabled: NONE
Upload Files
File: //usr/share/netplan/netplan/cli/__pycache__/sriov.cpython-310.pyc
o

�
ed�F�@s�ddlZddlZddlZddlZddlmZddlmmZddl	m
Z
ddlmZddl
Z
Gdd�de�Zdejefdd�Zdejed	ejefd
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zddd�Zddd�ZdS)�N)�defaultdict)�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/sriov.py�__init__'s
zPCIDevice.__init__�returncCsdS)zusysfs path (can be overridden for testing)
        :return: full path to /sys filesystem
        :rtype: str
        z/sysr�rrrr	�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�joinr
rrrrr	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)rrrrr	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)rrrrr	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�rrrrrrrr	�boundR�zPCIDevice.boundcCr)zwDetermine if device is a SR-IOV Physical Function
        :return: whether device is a PF
        :rtype: bool
        �sriov_numvfsrrrrr	�is_pfZrzPCIDevice.is_pfcCr)zvDetermine if device is a SR-IOV Virtual Function
        :return: whether device is a VF
        :rtype: bool
        �physfnrrrrr	�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{}�)�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#rrrr	�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)rr)r*r+rrr	�devlink_set�s	
��zPCIDevice.devlink_setcCs|jS)zbString represenation of object
        :return: PCI address of string
        :rtype: str
        rrrrr	�__str__�szPCIDevice.__str__N)�__name__�
__module__�__qualname__�__doc__�strr
�propertyr
rrr�boolrrr�listr#r(r0r1rrrr	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����rArc	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_vfsr?r@rrr	�
unbind_vfs�rBrDcCs�||vrD||}|jr<|j}|r||vr|||<n+|D]}|j|t�|�t�|�d�s,q||vr6td|��|||<qn||vrD|||<|�|d�S)N)�itf_name�
itf_driver�itf_macz3matched more than one interface for a PF device: %s)�	has_match�set_name�match_interface�utils�get_interface_driver_name�get_interface_macaddressr�get)�
interfaces�np_state�pf_link�pfs�pf_devrI�	interfacerrr	�_get_target_interface�s(
�
�rUc
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=�=�rz'failed parsing PCI slot name for %s: %s)rrrr<�	readlines�strip�
startswith�split�IOError�RuntimeErrorr6)�netdev�uevent_pathr@�
pci_slot_name�line�errr	�_get_pci_slot_name�s"

��&���rdc
Cs�|j��D]>\}}|jrt|||jj|�rd||<z|j}Wntjy0}zt|j	��d}~ww|dkr6qt||||�}	|	rC|||	<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.
    Nr)
�	ethernets�items�
sriov_linkrU�id�vf_count�
libnetplan�LibNetplanExceptionr�args)
rOrP�	vf_countsr(rR�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 > 256rV�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^r6�
ValueErrorr=�errno�logging�warning)
rqri�devdir�numvfs_path�
totalvfs_pathr@�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.
    rVrt�vendorrXNz2could not determine vendor and device ID of %s: %s�:r)	rrrr<rzrZr]r^r6)rqrr@�	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,r?�vlan)�stdout�stderrzJfailed setting SR-IOV VLAN filter for vlan %s (ip link set command failed))rrrrr�listdirr^r.r/r6�DEVNULL�CalledProcessError)rqr?�	vlan_name�vlan_id�prefix�vf_index�	vf_devdir�	vf_dev_id�	pf_devdirr@�dev_path�dev_idrrr	�apply_vlan_filter_for_vfNs<�
�
����r�c
CsRt��}|�|�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]D\}}|j#�r&|j$}|j%}|�&|j'�}|�st�(d||j'f�q�|�&|j)j'�}	||v�rtd||j'|f��t*|	|||�|�+|�q�d	S)
z
    Go through all interfaces, identify which ones are SR-IOV VFs, create
    them and perform all other necessary setup.
    )rEz3matched more than one interface for a VF device: %s)�	switchdev�legacyzFound VFs of {}: {}�eswitch�modezKSR-IOV vlan defined for %s but link %s is either not a VF or has no matcheszJinterface %s for netplan device %s (%s) already has an SR-IOV vlan definedN),rj�Parser�load_yaml_hierarchy�State�import_parser_results�parse�	netifacesrOrPrryrrrfr�r r�rHrJr�embedded_switch_moderdrrr}�debugr!r#r(�delay_virtual_functions_rebindrDrr0rAr-�vlans�has_sriov_vlan_filter�	vlan_linkr�rNrhr~rgr��add)�config_manager�rootdir�parserrPrOrmr(rR�vf_count_changedrqrir?rorT�	netdef_id�iface�eswitch_moder�pcidev�rebind_delayed�filtered_vlans_setr�r�r�rrr	�apply_sriov_configus�


�

��������
�
��r�)r�)r}rr.�typing�collectionsr�netplan.cli.utils�clirK�netplan.libnetplanrj�netplan.configmanagerrr��objectr�IterablerArDrUrdrrr�r�r�r�rrrr	�<module>s&!/
'