관리-도구
편집 파일: netlink.cpython-39.pyc
a ����'�Dg�.����������������������@���sB��d�dl�Z�d�dlZd�dlZd�dlZd�dlZd�dlmZ�d�dlmZ�e�� e �ZdZdZ dZdZdZd ZdZd ZdZdZe�e�Ze�e�Zee�Zd Zd ZdZdZd�ZdZdZ dZ!d Z"dZ#dZ$edg�d��Z%edddg�Z&edg�d��Z'G�dd��de(�Z)dd��Z*dd��Z+d-dd �Z,d!d"��Z-d#d$��Z.d%d&��Z/d'd(��Z0d)d*��Z1d+d,��Z2dS�).�����N)� namedtuple)�util��������������������i�����<���ZIHHIIZBHiII���������������������RTAAttr)�length�rta_type�data�InterfaceOperstate�ifname� operstate� NetlinkHeader)r����type�flags�seq�pidc�������������������@���s���e�Zd�ZdZdS�)�NetlinkCreateSocketErrorz5Raised if netlink socket fails during create or bind.N)�__name__� __module__�__qualname__�__doc__��r ���r ����E/usr/lib/python3.9/site-packages/cloudinit/sources/helpers/netlink.pyr���5���s���r���c��������������� ���C���s|���z4t���t�jt�jt�j�}�|��t���tf��|��d��W�n8�t�j yl�}�zd|�}t |�|�W�Y�d}~n d}~0�0�t�d��|�S�)au��Creates netlink socket and bind on netlink group to catch interface down/up events. The socket will bound only on RTMGRP_LINK (which only includes RTM_NEWLINK/RTM_DELLINK/RTM_GETLINK events). The socket is set to non-blocking mode since we're only receiving messages. :returns: netlink socket in non-blocking mode :raises: NetlinkCreateSocketError r���z*Exception during netlink socket create: %sNzCreated netlink socket) �socketZ AF_NETLINKZSOCK_RAWZ NETLINK_ROUTEZbind�os�getpid�RTMGRP_LINKZsetblocking�errorr����LOG�debug)�netlink_socket�e�msgr ���r ���r!����create_bound_netlink_socket9���s���� � r,���c�����������������C���s^���|�dusJ�d��t�|��tks$J�d��t�t|�dt���\}}}}}t�d|��t|||||�S�)a��Gets netlink message type and length :param: data read from netlink socket :returns: netlink message type :raises: AssertionError if data is None or data is not >= NLMSGHDR_SIZE struct nlmsghdr { __u32 nlmsg_len; /* Length of message including header */ __u16 nlmsg_type; /* Type of message content */ __u16 nlmsg_flags; /* Additional flags */ __u32 nlmsg_seq; /* Sequence number */ __u32 nlmsg_pid; /* Sender port ID */ }; N�data is nonez+data is smaller than netlink message headerzGot netlink msg of type %d) �len� NLMSGHDR_SIZE�struct�unpack�NLMSGHDR_FMT�MSG_TYPE_OFFSETr'���r(���r���)r���Zmsg_lenZmsg_typer���r���r���r ���r ���r!����get_netlink_msg_headerO���s���� ���r4���c�����������������C���s^���|�dusJ�d��t���|�gg�g�|�\}}}|�|vr4dS�t�d��|��t�}|du�rZt�d��|S�)a���Select and read from the netlink socket if ready. :param: netlink_socket: specify which socket object to read from :param: timeout: specify a timeout value (integer) to wait while reading, if none, it will block indefinitely until socket ready for read :returns: string of data read (max length = <MAX_SIZE>) from socket, if no data read, returns None :raises: AssertionError if netlink_socket is None N�netlink socket is noneznetlink socket ready for readz,Reading from Netlink socket returned no data)�selectr'���r(���Zrecv�MAX_SIZEr&���)r)����timeoutZread_set�_r���r ���r ���r!����read_netlink_socketh���s���� r:���c�����������������C���s����|�dusJ�d��t�|t�s"J�d��|tks2J�d��d�}}d}z0tjd|�|d�d�}tjd|�|d�d�d�}W�n�tjy����Y�dS�0�|�|t�||���}t|||�S�) a(��Unpack a single rta attribute. :param: data: string of data read from netlink socket :param: offset: starting offset of RTA Attribute :return: RTAAttr object with length, type and data. On error, return None. :raises: AssertionError if data is None or offset is not integer. Nr-���zoffset is not integerz'rta offset is less than expected lengthr����H)�offsetr���)� isinstance�int�RTATTR_START_OFFSETr0����unpack_fromr&����RTA_DATA_START_OFFSETr���)r���r<���r���r���Z attr_datar ���r ���r!����unpack_rta_attr���s������rB���c�����������������C���s����|�dusJ�d��t�|��tks$J�d��d�}}t}|t�|��kr�t|�|�}|r�|jdkrVq�t|jt��t�}||j|�7�}|jtkr�t|j�}q0|jt kr0t �|jd�}|�d�}q0|r�|du�r�dS�t �d||��t||�S�)a���Reads Interface name and operational state from RTA Data. :param: data: string of data read from netlink socket :returns: InterfaceOperstate object containing if_name and oper_state. None if data does not contain valid IFLA_OPERSTATE and IFLA_IFNAME messages. :raises: AssertionError if data is None or length of data is smaller than RTATTR_START_OFFSET. Nr-���z2length of data is smaller than RTATTR_START_OFFSETr���zutf-8��z!rta attrs: ifname %s operstate %d)r.���r?���rB���r���� PAD_ALIGNMENTr����IFLA_OPERSTATE�ordr����IFLA_IFNAMEr���Z decode_binary�stripr'���r(���r���)r���r���r���r<����attr�padlenZinterface_namer ���r ���r!����read_rta_oper_state����s0���� �� � rK���c��������������������s6���t��d��d����fdd�}t|�dtgttg|���S�)z�Block until a single nic is attached. :param: netlink_socket: netlink_socket to receive events :param: existing_nics: List of existing nics so that we can skip them. :raises: AssertionError if netlink_socket is none. z!Preparing to wait for nic attach.Nc��������������������s���|���v�rdS�|��dS�)NTFr �����iname�carrier�prevCarrier�� existing_nicsr���r ���r!����should_continue_cb����s����z5wait_for_nic_attach_event.<locals>.should_continue_cb)r'���r(����read_netlink_messages�RTM_NEWLINK�OPER_UP� OPER_DOWN)r)���rQ���rR���r ���rP���r!����wait_for_nic_attach_event����s���� �rW���c��������������������s2���t��d��d����fdd�}t|�dtgtg|����S�)z�Block until a single nic is detached and its operational state is down. :param: netlink_socket: netlink_socket to receive events. z!Preparing to wait for nic detach.Nc��������������������s���|���dS�)NFr ���rL����r���r ���r!���rR�������s����z5wait_for_nic_detach_event.<locals>.should_continue_cb)r'���r(���rS����RTM_DELLINKrV���)r)���rR���r ���rX���r!����wait_for_nic_detach_event����s���� �rZ���c��������������������sf���|�dusJ�d����dus J�d��t����dks4J�d����fdd�}t�d��t|���ttgttg|��dS�) a���Block until media disconnect and connect has happened on an interface. Listens on netlink socket to receive netlink events and when the carrier changes from 0 to 1, it considers event has happened and return from this function :param: netlink_socket: netlink_socket to receive events :param: ifname: Interface name to lookout for netlink events :raises: AssertionError if netlink_socket is None or ifname is None. Nr5���zinterface name is noner���zinterface name cannot be emptyc��������������������s(���|t�ko|tk}|r$t�d����dS�dS�)NzMedia switch happened on %s.FT)rV���rU���r'���r(���)rM���rN���rO���ZisVnetSwitchrX���r ���r!���rR�������s ����z=wait_for_media_disconnect_connect.<locals>.should_continue_cbz1Wait for media disconnect and reconnect to happen)r.���r'���r(���rS���rT���rY���rU���rV���)r)���r���rR���r ���rX���r!����!wait_for_media_disconnect_connect����s���� �r[���c�����������������C���sx��|�du�rt�d��t��}t}t}t|�t�}|du�r2qt�dt|���||7�}t�dt|���d} t|�} | | k��rf|| d��}t|�tk�r�t�d���qft |�}t|�|j k�r�t�d���qf|j t�d�td��@�} | | �} t�d | ��|j|vr�qft |�}|du��rt�d |��qf|du�r:|j|k�r:t�d|j|��qf|j|v�rHqf|}|j}||j||�sfdS�qf|| d��}qdS�)a���Reads from the netlink socket until the condition specified by the continuation callback is met. :param: netlink_socket: netlink_socket to receive events. :param: ifname_filter: if not None, will only listen for this interface. :param: rtm_types: Type of netlink events to listen for. :param: operstates: Operational states to listen. :param: should_continue_callback: Specifies when to stop listening. NzNetlink socket is nonezread %d bytes from socketzLength of data after concat %dr���z#Data is smaller than netlink headerz*Partial data. Smaller than netlink messager���z"offset to next netlink message: %dz!Failed to read rta attributes: %sz6Ignored netlink event on interface %s. Waiting for %s.)�RuntimeError�bytesrU���r:����SELECT_TIMEOUTr'���r(���r.���r/���r4���r���rD���r���rK���r���r���)r)���Z ifname_filterZ rtm_typesZ operstatesZshould_continue_callbackr���rN���rO���Z recv_datar<���ZdatalenZnl_msgZnlheaderrJ���Zinterface_stater ���r ���r!���rS�����sh���� � ����rS���)N)3Zloggingr#���r6���r"���r0����collectionsr���Z cloudinitr���Z getLoggerr���r'���r%���rT���rY���ZRTM_GETLINKZRTM_SETLINKr7���r3���r^���r2���Z IFINFOMSG_FMT�calcsizer/���ZIFINFOMSG_SIZEr?���rA���rD���rG���rE���ZOPER_UNKNOWNZOPER_NOTPRESENTrV���ZOPER_LOWERLAYERDOWNZOPER_TESTINGZOPER_DORMANTrU���r���r���r���r\���r���r,���r4���r:���rB���rK���rW���rZ���r[���rS���r ���r ���r ���r!����<module>���s\��� � %