HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/share/netplan/netplan/cli/__pycache__/sriov.cpython-38.pyc
U

��~f	N�@s�ddlZddlZddlZddlZddlmZddlmmZddl	m
Z
ddlmZddl
Z
Gdd�de�Zejed�dd�Zejeejed	�d
d�Zdd
�Zdd�Zdd�Zdd�Zdd�Zddd�Zddd�ZdS)�N)�defaultdict)�ConfigurationErrorc@s�eZdZdZed�dd�Zeed�dd��Zeed�dd	��Zeed
�dd�Z	eed�d
d��Z
eed�dd��Zeed�dd��Z
eed�dd��Zeed�dd��Zeed�dd��Zeeed�dd�Zed�dd�ZdS)�	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
        Nr)�selfr�r�'/usr/share/netplan/netplan/cli/sriov.py�__init__'szPCIDevice.__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)�subpathrcCstj�|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��r.tj�t�|�d���}|S)zjKernel driver for PCI device
        :return: kernel driver in use for device
        :rtype: str
        ��driver)rr�existsr�basename�readlink)rrrrr	rGszPCIDevice.drivercCstj�|�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	�boundRszPCIDevice.boundcCstj�|�d��S)zwDetermine if device is a SR-IOV Physical Function
        :return: whether device is a PF
        :rtype: bool
        �sriov_numvfsrrrrr	�is_pfZszPCIDevice.is_pfcCstj�|�d��S)zvDetermine if device is a SR-IOV Virtual Function
        :return: whether device is a VF
        :rtype: bool
        Zphysfnrrrrr	�is_vfbszPCIDevice.is_vfcCsXg}d}z(|�tj�t�|�d�|�����Wntk
rHYqTYnX|d7}q|S)z�List Virtual Function addresses associated with a Physical Function
        :return: List of PCI addresses of Virtual Functions
        :rtype: list[str]
        rzvirtfn{}�)�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_callrr)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%r-r.rrrr	r$s*	
r)r%c
CsJg}|D]<}|jstd�|�d��}|�|j�|�|�W5QRXq|S)zBind unbound VFs to driver.z/sys/bus/pci/drivers/{}/bind�wt�r�openr�writerr)r%rZ	bound_vfs�vf�frrr	�bind_vfs�sr=)r%rc
CsJg}|D]<}|jrtd�|�d��}|�|j�|�|�W5QRXq|S)zUnbind bound VFs from driver.z/sys/bus/pci/drivers/{}/unbindr7r8)r%rZunbound_vfsr;r<rrr	�
unbind_vfs�sr>cCs�||kr�|j|}|�d�}|r�|�d�}|r@||kr@|||<q�|�d�}|�d�}|�d�}	|D]R}
|rvt�|
|�rb|r�t�|
|�rb|	r�t�|
|	�s�qb||kr�td|��|
||<qbn||kr�|||<|�|d�S)N�matchzset-name�name�
macaddressrz3matched more than one interface for a PF device: %s)�	ethernets�get�utils�is_interface_matching_name� is_interface_matching_macaddress�!is_interface_matching_driver_namer)�
interfaces�config_manager�pf_link�pfsZpf_devZpf_match�set_name�by_nameZby_macZ	by_driver�	interfacerrr	�_get_target_interface�s6






�
��
�rOc
Cs�tj�d|d�}zbt|��P}d}|��D]<}|��}|�d�r(|�dd�d}|W5QR�WSq(W5QRXWn6tk
r�}zt	d|t
|�f��W5d}~XYnXdS)	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)rrrr9�	readlines�strip�
startswith�split�IOError�RuntimeErrorr3)ZnetdevZuevent_pathr<Z
pci_slot_name�line�errr	�_get_pci_slot_name�s

&r[cCs�i}|j��D]�\}}|sq|dkr&q|�d�}|rPt||||�}	|	r|||	<q|�d�}
|
r|
|jkrt|||
|�|
|kr�|||
d7<nt�d|
�qd||<q|��D]8\}	}|	|kr�||	|kr�td||	|f��|||	<q�dS)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.
    �rendererzvirtual-function-count�linkrz9could not match physical interface for the defined PF: %sNz;more VFs allocated than the explicit size declared: %s > %s)rB�itemsrCrO�logging�warningr)rHrI�	vf_countsr%rKZexplicit_countsZethernet�settingsZexplicit_num�pfrJ�countrrr	�get_vf_count_and_functions�s4


�rec

Cs�|dkrtd||f��tj�d|d�}tj�|d�}tj�|d�}z(t|��}t|�����}W5QRXWnTtk
r�}zt	d|t
|�f��W5d}~XYn tk
r�t	d	|��YnX||kr�td
|||f��z(t|d��}|�t
|��W5QRXWn�tk
�r�}z�d}|j
d
k�r�t�d|�zHt|d��}|�d�W5QRXt|d��}|�t
|��W5QRXWn(tk
�r�}	z|	}W5d}	~	XYnXd}|�r�t	d||t
|�f��W5d}~XYnXdS)zB
    Allocate the required number of VFs for the selected PF.
    �zDcannot allocate more VFs for PF %s than the SR-IOV maximum: %s > 256rP�devicerZsriov_totalvfsz(failed parsing sriov_totalvfs for %s: %sNz#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)rrrrr9�int�readrTrWrXr3�
ValueErrorr:�errnor_r`)
rc�vf_count�devdirZnumvfs_pathZ
totalvfs_pathr<Zvf_maxrZZbailZe_innerrrr	�set_numvfs_for_pf"sH
�
$�(rqc
Cs�tj�d|d�}zhttj�|d���}|����dd�}W5QRXttj�|d���}|����dd�}W5QRXWn6tk
r�}ztd|t|�f��W5d}~XYnXd�||g�}d}||kr�dS)	z�
    Perform any hardware-specific quirks for the given SR-IOV device to make
    sure all the VF-count changes are applied.
    rPrgZvendorrRNz2could not determine vendor and device ID of %s: %s�:r)	rrrr9rlrTrWrXr3)rcrpr<Z	device_idZ	vendor_idrZZcombined_idZ
quirk_devicesrrr	� perform_hardware_specific_quirksQs"&
rs�/cCs�d}tj�|d|d�}tj�t�|��}tj�|d|d�}t�|�D]D}	d|	krDtj�||	�}
tj�t�|
��}||krD|	dd�}q�qD|s�td||f��z.tjddd	d
|d|dt	|�g	tj
tj
d
�Wn"tjk
r�td|��YnXdS)z@
    Apply the hardware VLAN filtering for the selected VF.
    Nz
sys/class/netrgZvirtfn�zAcould not determine the VF index for %s while configuring vlan %s�ipr]r*r)r;�vlan)�stdout�stderrzJfailed setting SR-IOV VLAN filter for vlan %s (ip link set command failed))rrrrr�listdirrXr+r,r3�DEVNULL�CalledProcessError)rcr;Z	vlan_name�vlan_id�prefixZvf_indexZ	vf_devdirZ	vf_dev_idZ	pf_devdirr<Zdev_pathZdev_idrrr	�apply_vlan_filter_for_vfns>
�
��
�rc
Cs�t��}|�|�t��}|�|�|��t��}tt	�}i}i}t
|||||�g}|r�|��D]\}	}
t|	|
�sxqd|�
|	�qd|r�|D]}	t|	�q�t��}|D]�}|j�|�}|�d�}
|
�r|
�d�}|D]>}|r�t�||�s�q�||k�r
||�r
td|��|||<q�q�||kr�|||<q�|��D]�\}}||}|j}|dk�r0t|�}t|�}|j�r0t�d�||j��|j�r0|j}z t |j|j�|�!dd|�W5|�s�t|j|j�X�q0t"�}|j#��D]�\}}|�d�d	k�r�|�d
�}|�d�}|�|�}|�s(t�$d||f��q�|j�|��d
�}|�|�}	||k�r`td
|||f��t%|	|||�|�&|��q�dS)z
    Go through all interfaces, identify which ones are SR-IOV VFs, create
    them and perform all other necessary setup.
    r?r@z3matched more than one interface for a VF device: %s)Z	switchdevZlegacyzFound VFs of {}: {}Zeswitch�moder\Zsriovr]�idzKSR-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)'�
libnetplan�ParserZload_yaml_hierarchy�StateZimport_parser_results�parse�	netifacesrHrrkrer^rqrrsrBrCrDrErZembedded_switch_moder[rrr_�debugrr r%Zdelay_virtual_functions_rebindr=rr>r-r*�vlansr`r�add)rIZrootdir�parserZnp_staterHrar%rKZvf_count_changedrcror;rbr?rMrNZ	netdef_id�ifaceZnetdefZeswitch_moderZpcidevZrebind_delayedZfiltered_vlans_setrwr]r}Zvf_parent_entryrrr	�apply_sriov_config�s�

�









�

�r�)rt)rt)r_rr+�typing�collectionsr�netplan.cli.utils�clirDZnetplan.libnetplanr��netplan.configmanagerrr��objectr�Iterabler=r>rOr[rerqrsrr�rrrr	�<module>s$'1/
'