Current File : //usr/share/netplan/netplan_cli/cli/__pycache__/sriov.cpython-312.pyc
�

Z9
g�R����ddlZddlZddlZddlZddlZddlmZmZmZmZddl	m
Z
ddlmZddl
Z
Gd�de�Zd	ej efd
�Zd	ej edej efd�Zd
e
j&dedefd�Zd
e
j&deefd�Zd�Zde
j2deeeffd�Zde
j2deeeffd�Zde
j2deefd�Zd�Zd�Zdd�Z dd�Z!y)�N)�Dict�List�Optional�Set�)�utils�)�ConfigurationErrorc��eZdZdZdefd�Zedefd��Zedefd��Zdedefd�Z	edefd	��Z
edefd
��Zedefd��Z
edefd��Zedefd
��Zedefd��Zdededefd�Zdefd�Zdefd�Zy)�	PCIDevicez.Helper class for interaction with a PCI device�pci_addrc��||_y)znInitialise a new PCI device handler
        :param pci_addr: PCI address of device
        :type: str
        N�r
)�selfr
s  �+/usr/share/netplan/netplan_cli/cli/sriov.py�__init__zPCIDevice.__init__%s��
!��
��returnc��y)zusysfs path (can be overridden for testing)
        :return: full path to /sys filesystem
        :rtype: str
        z/sys��rs r�sysz
PCIDevice.sys,s��rc�l�tjj|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�joinrr
rs rrzPCIDevice.path4s$���w�w�|�|�D�H�H�&7����G�Gr�subpathc�V�tjj|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)rrs  rrzPCIDevice.subpath<s���w�w�|�|�D�I�I�w�/�/rc���d}tjj|jd��rAtjj	tj
|jd���}|S)zjKernel driver for PCI device
        :return: kernel driver in use for device
        :rtype: str
        ��driver)rr�existsr�basename�readlink)rr!s  rr!zPCIDevice.driverEsM����
�7�7�>�>�$�,�,�x�0�1��W�W�%�%�b�k�k�$�,�,�x�2H�&I�J�F��
rc�^�tjj|jd��S)z�Determine if device is bound to a kernel driver
        :return: whether device is bound to a kernel driver
        :rtype: bool
        r!�rrr"rrs r�boundzPCIDevice.boundP� ���w�w�~�~�d�l�l�8�4�5�5rc�^�tjj|jd��S)zwDetermine if device is a SR-IOV Physical Function
        :return: whether device is a PF
        :rtype: bool
        �sriov_numvfsr&rs r�is_pfzPCIDevice.is_pfXs ���w�w�~�~�d�l�l�>�:�;�;rc�^�tjj|jd��S)zvDetermine if device is a SR-IOV Virtual Function
        :return: whether device is a VF
        :rtype: bool
        �physfnr&rs r�is_vfzPCIDevice.is_vf`r(rc��g}d}		|jtjjtj|jdj
|�����|dz
}�f#t$rY|SwxYw)z�List Virtual Function addresses associated with a Physical Function
        :return: List of PCI addresses of Virtual Functions
        :rtype: list[str]
        rzvirtfn{}r)�appendrrr#r$r�format�FileNotFoundError)r�vf_addrs�is   rr3zPCIDevice.vf_addrshs}����
���
�����G�G�$�$����D�L�L��1B�1B�1�1E�$F�G���
��F�A���%�
����
�s�AA,�,	A9�8A9c�R�|jD�cgc]
}t|���c}Scc}w)z�List Virtual Function associated with a Physical Function
        :return: List of PCI devices of Virtual Functions
        :rtype: list[PCIDevice]
        )r3r)r�addrs  r�vfsz
PCIDevice.vfs|s ��-1�M�M�:�D�	�$��:�:��:s�$�obj_name�prop�valuec	�n�tjdd|ddj|j�||g�y)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
        �
/sbin/devlink�dev�setzpci/{}N)�
subprocess�
check_callr1r
)rr8r9r:s    r�devlink_setzPCIDevice.devlink_set�s9��	�����������
�
�.���
�
	
rc�8�d|j��}	tjddddd|gtj��}tj|�}|jdi�j|i�jd	d�S#tj$rYywxYw)
z�Query eswitch mode via devlink for the PCI device
        :return: the eswitch mode or '__undetermined' if it can't be retrieved
        :rtype: str
        zpci/r<z-jr=�eswitch�show)�stderr�__undetermined�mode)r
r?�check_output�DEVNULL�CalledProcessError�json�loads�get)r�pci�output�json_outputs    r�devlink_eswitch_modezPCIDevice.devlink_eswitch_mode�s���
�T�]�]�O�$��
	$��,�,�#������
�"�)�)�
�F��j�j��(�����u�b�)�-�-�c�2�6�:�:�6�CS�T�T���,�,�	$�#�	$�s�+B�B�Bc��|jS)zbString represenation of object
        :return: PCI address of string
        :rtype: str
        rrs r�__str__zPCIDevice.__str__�s��
�}�}�rN)�__name__�
__module__�__qualname__�__doc__�strr�propertyrrrr!�boolr'r+r.�listr3r7rArQrSrrrrr"s3��8�!��!���S�����H�c�H��H�0�s�0�s�0��������6�t�6��6��<�t�<��<��6�t�6��6���$����&�;�T�;��;�
�C�
�s�
�3�
�*U�c�U�:��rrr7c���g}|D]_}|jr�tdj|�d�5}|j|j�|j|�ddd��a|S#1swY�mxYw)zBind unbound VFs to driver.z/sys/bus/pci/drivers/{}/bind�wtN�r'�openr1�writer
r0)r7r!�	bound_vfs�vf�fs     r�bind_vfsrd�su���I��%���x�x��4�;�;�F�C�T�J�
%�a�������$�� � ��$�
%�
%�%�
��
%�
%���-A)�)A2	rc���g}|D]_}|js�tdj|�d�5}|j|j�|j|�ddd��a|S#1swY�mxYw)zUnbind bound VFs from driver.z/sys/bus/pci/drivers/{}/unbindr]Nr^)r7r!�unbound_vfsrbrcs     r�
unbind_vfsrh�sv���K��'��
�8�8��6�=�=�f�E�t�L�
'�PQ�������$��"�"�2�&�
'�
'�'�
��
'�
'�re�netdef�	interfacec�v�|j|tj|�tj|���S)N)�
iface_name�iface_driver�	iface_mac)�_match_interfacer�get_interface_driver_name�get_interface_macaddress)rirjs  r�_interface_matchesrr�s9���"�"���4�4�Y�?��0�0��;�#�=�=rc�l�tj�}|jrz|j}|r||vr|St	�}|D]F}t||�s�t
|�dkDrtd|jz��|j|��H|rt|�dSy|j|vr|jSy)z�
    Try to match a netdef with the real system network interface.
    Throws ConfigurationError if there is more than one match.
    rz3matched more than one interface for a PF device: %srN)r�get_interfaces�
_has_match�set_namer>rr�lenr
�id�addr[)ri�
interfacesrv�matchesrjs     r�_get_interface_name_for_netdefr|�s���
"�0�0�2�J�
���������J�.��O� #��G�(�
'�	�)�&�)�<���w�<�!�#�,�-b�ek�en�en�-n�o�o����I�&�
'���G�}�Q�'�'���9�9�
�"��9�9��rc	��tjjd|d�}	t|�5}d}|j	�D]E}|j�}|j
d�s�%|jdd�d}|ccddd�S	ddd�y#1swYyxYw#t$r}td|�d	t|�����d}~wwxYw)
z5
    Read PCI slot name for given interface name
    �/sys/class/netz
device/ueventNzPCI_SLOT_NAME=�=r	rz!failed parsing PCI slot name for �: )rrrr_�	readlines�strip�
startswith�split�IOError�RuntimeErrorrX)�netdev�uevent_pathrc�
pci_slot_name�line�es      r�_get_pci_slot_namer��s����'�'�,�,�/���I�K�	Y�
�+�
�	)�!� �M����
�
)���z�z�|���?�?�#3�4�$(�J�J�s�A�$6�q�$9�M�(�(�
	)�	)�
)�	)�	)�	)���Y���PS�TU�PV�W�X�X��Y�sF�B�7B�&B�>	B�B�
B�B�B�B�	C�(C�C�np_statec���i}|jj�D]�}|jjd�x}r+t	||j
�x}s�;|||j
<�K|jrt	|�x}r|||j
<	|j}|dkDs��t	|�x}s��|||j
<��|S#tj$r}tt|���d}~wwxYw)z�
    Go through the list of netplan ethernet devices and identify which are
    PFs matching them with actual network interfaces.
    �sriovNr)�	ethernets�values�linksrMr|rx�_embedded_switch_mode�	_vf_count�netplan�NetplanExceptionr
rX)r��pfsri�link�iface�countr�s       r�_get_physical_functionsr�s���

�C��$�$�+�+�-�+���<�<�#�#�G�,�,�4�,�6�x����7H�I�I�u�I�$��D�G�G���+�+�:�6�B�B�5�B�%*�C��	�	�N�
1��(�(���q�y�:�6�B�B�5�B�%*�C��	�	�N�'+�*�J��
�+�+�
1�(��Q��0�0��
1�s�C�C0�C+�+C0c���i}|jj�D](}	|j}|dkDs�t|�x}s�$|||<�*|S#tj$r}tt
|���d}~wwxYw)z�
    Go through the list of netplan ethernet devices and identify which ones
    have VFs. netdef._vf_count ultimately calls _netplan_state_get_vf_count_for_def
    from libnetplan which return MAX(sriov_explicit_vf_count, number of VF netdefs).
    Nr)r�r�r�r�r�r
rXr|)r��	vf_countsrir�r�r�s      r�_get_vf_number_per_pfr�.s����I��$�$�+�+�-�)��	-��$�$�E��1�9�6�v�>�>�u�>�#(�	�%� �)����
�'�'�	-�$�S��V�,�,��	-�s�A
�
A6�A1�1A6c��t�}|jj�D]T}|jj	d�x}s�!t||j�s�:|j|j��V|S)zk
    Go through the list of netplan ethernet devices and identify which ones
    are virtual functions
    r�)r>r�r�r�rMr|rxry)r�r7rir�s    r�_get_virtual_functionsr�Ask��
�%�C��$�$�+�+�-�#���<�<�#�#�G�,�,�4�,�-�h�t�w�w�.?�@�����	�	�"�	#�
�Jrc��|dkDrtd|�d|�d���tjjd|d�}tjj|d�}tjj|d�}	t	|�5}t|j
�j��}d	d	d	�|kDrtd|�d
|�d|�d���	t	|d�5}|jt|��d	d	d	�y#1swY�TxYw#t$r}td
|�dt|�����d	}~wt$rtd|z��wxYw#1swYyxYw#t$r�}d}|jdk(r�tjd|z�	t	|d�5}|jd�d	d	d	�n#1swYnxYwt	|d�5}|jt|��d	d	d	�n#1swYnxYwd}n#t$r}	|	}Yd	}	~	nd	}	~	wwxYw|rtd|�d|�dt|�����Yd	}~yd	}~wwxYw)zB
    Allocate the required number of VFs for the selected PF.
    �z cannot allocate more VFs for PF z than the SR-IOV maximum: z > 256r~�devicer*�sriov_totalvfsNz"failed parsing sriov_totalvfs for r�z#invalid sriov_totalvfs value for %sz than supported: z > z (sriov_totalvfs)�wT�zLdevice or resource busy while setting sriov_numvfs for %s, trying workaround�0Fzfailed setting sriov_numvfs to z for )r
rrrr_�int�readr�r�r�rX�
ValueErrorr`�errno�logging�warning)
�pf�vf_count�devdir�numvfs_path�
totalvfs_pathrc�vf_maxr��bail�e_inners
          r�set_numvfs_for_pfr�Os%���#�~� �VX�Zb�c�e�	e��W�W�\�\�*�B��
9�F��'�'�,�,�v�~�6�K��G�G�L�L��)9�:�M�G�
�-�
 �	+�A��������)�*�F�	+��&�� �]_�ai�kq�r�t�	t�h�
�+�s�
#�	#�q�
�G�G�C��M�"�	#�(�A	+�	+���V���S�QR�V�T�U�U���G��@�2�E�F�F�G��	#�(��%�h����7�7�b�=��O�O�j�mo�o�p�

��+�s�+�!�q��G�G�C�L�!�!�!���+�s�+�+�q��G�G�C��M�*�+�+�+��
����
����
����QY�[]�_b�cd�_e�f�g�g����%h�s��:D�(D�-D�E�E
�7E�D	�D�	E
�D/�/E
�
E�E�E�	H?�")H:�G>�F3�*	G>�3F<	�8G>�G/�&	G>�/G8	�4G>�;H:�>	H�H�	H:�H�"H:�:H?c	�N�tjjd|d�}	ttjj|d��5}|j	�j�dd}ddd�ttjj|d��5}|j	�j�dd}ddd�djg�}d	}||vryy#1swY�wxYw#1swY�0xYw#t$r}td|�dt|�����d}~wwxYw)
z�
    Perform any hardware-specific quirks for the given SR-IOV device to make
    sure all the VF-count changes are applied.
    r~r��vendorr	Nz,could not determine vendor and device ID of r��:r)	rrrr_r�r�r�r�rX)r�r�rc�	device_id�	vendor_idr��combined_id�
quirk_devicess        r� perform_hardware_specific_quirksr�~s��
�W�W�\�\�*�B��
9�F�`�
�"�'�'�,�,�v�x�0�
1�	-�Q�������(���,�I�	-�
�"�'�'�,�,�v�x�0�
1�	-�Q�������(���,�I�	-�
�(�(�I�y�1�2�K��M��m�#�	
�$�	-�	-��	-�	-���`��SU�WZ�[\�W]�^�_�_��`�sG�)C<�"C$�.1C<�"C0�C<�$C-�)C<�0C9�5C<�<	D$�D�D$c
���d}tjj|d|d�}tjjtj|��}tjj|d|d�}tj
|�D]e}	d|	vs�tjj||	�}
tjjtj|
��}||k(s�`|	dd}n|st
d|�d|����	tjdd	d
d|d|d
t|�g	tjtj��y#tj$rt
d|z��wxYw)z@
    Apply the hardware VLAN filtering for the selected VF.
    Nz
sys/class/netr��virtfn�z%could not determine the VF index for z while configuring vlan �ipr�r>r=rb�vlan)�stdoutrEzJfailed setting SR-IOV VLAN filter for vlan %s (ip link set command failed))rrrr#r$�listdirr�r?r@rXrIrJ)r�rb�	vlan_name�vlan_id�prefix�vf_index�	vf_devdir�	vf_dev_id�	pf_devdirrc�dev_path�dev_ids            r�apply_vlan_filter_for_vfr��sP���H������V�_�b�(�C�I���� � ����Y�!7�8�I������V�_�b�(�C�I�
�Z�Z�	�
"����q�=��w�w�|�|�I�q�1�H��W�W�%�%�b�k�k�(�&;�<�F���"��Q�R�5���
���SU�W`�a�c�	c�	f����t�V�U�$�b�#�X�%�s�7�|�5�&0�%7�%7�%/�%7�%7�	9���(�(�f��X�[d�d�f�	f�f�s
�
AE�"E3c
�*�|j�tj�}|j}t	|�}t|�}t
|�}g}|r6|j�D]#\}}	t||	�s�|j|��%|r&|D]
}t|��tj�}i}
|D]T}||}|jr7|D]1}
|j|
��s�||
vr|
|rtd|z��|
|
|<�3�K||vs�P||
|<�V|j�D�]\}}||}|j}|dvs�t|�}t!|�}|j#�}||k7s�I|j$s�Vt'j(dj+||j,��|j.r!	t1|j.|j2�t'j(d|�d	|�d
|���|j;dd|�|j.s��|j<r��t?|j.|j2���tA�}|jBj�D]�\}}|jDs�|jFjId
�}|jJ}|
jI|jL�}|s't'j6d|�d|jL�d���~|jI|jFjId�jL�}||vrtd|�d|jL�d|�d���tO||||�|jQ|���y#t4$r/}t'j6d|�dt9|����Yd}~���d}~wwxYw)z
    Go through all interfaces, identify which ones are SR-IOV VFs, create
    them and perform all other necessary setup.
    )rlz3matched more than one interface for a VF device: %s)�	switchdev�legacyzFound VFs of {}: {}zUnbinding of VFs for z	 failed: NzChanging eswitch mode from z to z for: rCrGr�zSR-IOV vlan defined for z
 but link z% is either not a VF or has no matchesr�z
interface z for netplan device z (z$) already has an SR-IOV vlan defined))�parserrtr�r�r�r��itemsr�r0r�ruror
r�r�rrQr+r��debugr1r3r7rhr!�	Exceptionr�rXrA�_delay_virtual_functions_rebindrdr>�vlans�_has_sriov_vlan_filterr�rM�_vlan_idrxr�ry)�config_manager�rootdirrzr�r��vfs_setr��vf_count_changedr�r�r7rbrirj�	netdef_idr��eswitch_moder
�pcidev�current_eswitch_mode_systemr��filtered_vlans_setr�r�r�s                         r�apply_sriov_configr��sz��
�����%�%�'�J��&�&�H�&�h�/�I�%�X�.�G�
!�(�
+�C�
���%�O�O�-�	(�L�B��$�R��2���#�#�B�'�		(��#�	1�B�,�R�0�	1�
�)�)�+�
�
�C�����"������
(�
(�	��*�*�i�*�@��S�y�S��W�0�1f�ik�1k�l�l�'�C��G�	
(��Z����B���" �I�I�K�@��	�5��)�$���3�3���2�2�)�%�0�H��x�(�F�*0�*E�*E�*G�'��:�:��<�<��M�M�"7�">�">�v�v���"W�X��z�z�b�&�v�z�z�6�=�=�A��M�M�$?�@[�?\�\`�am�`n�nt�u~�t�#A�B��&�&�y�&�,�G��z�z�%�E�E�$�V�Z�Z����?�+@�.��� ���,�,�.�'���f��(�(��<�<�#�#�F�+�D��o�o�G�������!�B�����ei�ko�kr�kr�s�u��
���������0�3�3�4�B��'�'�(�df�hl�ho�ho�qu�v�x�x�
%�R��T�7�;��"�"�2�&�9'�� )�b�#�O�O�.C�I�;�i�X[�\]�X^�W_�,`�a�a��b�s�1 M�	N�#$N
�
N)�/)"rKr�rr?�typingrrrrr r�
configmanagerr
r��objectr�Iterablerdrh�
NetDefinitionrXrZrrr|r��Stater�r�r�r�r�r�r�r�rrr�<module>r�s#��&��	��
�,�,��.��Y��Y�x�&�/�/�)�,���F�O�O�I�.��6�?�?�9�;U��=�w�4�4�=��=��=�!�7�+@�+@�!�X�c�]�!�HY�"�g�m�m���S�#�X���<�G�M�M��d�3��8�n��&�W�]�]��s�3�x��,�^
�:$f�Ns'r