Current File : //lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyo
�
oB�]c@s�ddlmZddlmZddlmZddlZddlZddlZddl	Z	ej
e�ZdZ
dZdZdZdZd	Zd
ZdZdZd
ZdZdZdZdZe	je�Ze	je�ZeeZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)eddddg�Z*edddg�Z+edddddd g�Z,d!e-fd"��YZ.d#�Z/d$�Z0dd%�Z2d&�Z3d'�Z4d(�Z5dS()i����(tlog(tutil(t
namedtupleNiiiiiiii��i i<tIHHIItBHiIIiiiitRTAAttrtlengthtrta_typetdatatInterfaceOperstatetifnamet	operstatet
NetlinkHeaderttypetflagstseqtpidtNetlinkCreateSocketErrorcBseZdZRS(s5Raised if netlink socket fails during create or bind.(t__name__t
__module__t__doc__(((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyR8scCs�yHtjtjtjtj�}|jtj�tf�|jd�Wn,tj	k
rv}d|}t
|��nXtjd�|S(suCreates 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
    is*Exception during netlink socket create: %ssCreated netlink socket(
tsockett
AF_NETLINKtSOCK_RAWt
NETLINK_ROUTEtbindtostgetpidtRTMGRP_LINKtsetblockingterrorRtLOGtdebug(tnetlink_sockettetmsg((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytcreate_bound_netlink_socket=s	

cCsKtjt|t �\}}}}}tjd|�t|||||�S(sGets 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 */
    };
    sGot netlink msg of type %d(tstructtunpacktNLMSGHDR_FMTtMSG_TYPE_OFFSETRR R(Rtmsg_lentmsg_typeRRR((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytget_netlink_msg_headerSs	cCsptj|ggg|�\}}}||kr4dStjd�|jt�}|dkrltjd�n|S(s�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
    snetlink socket ready for reads,Reading from Netlink socket returned no dataN(tselecttNoneRR trecvtMAX_SIZER(R!ttimeouttread_sett_R((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytread_netlink_socketjs$
cCs�d}}d}y@tjd|d|�d}tjd|d|d�d}Wntjk
rgdSX||t||!}t|||�S(s(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.
    itHtoffsetiN(R-R%tunpack_fromRtRTA_DATA_START_OFFSETR(RR5RRt	attr_data((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytunpack_rta_attr�s
$cCsd}}t}x�|t|�kr�t||�}|sJ|jdkrNPnt|jtt}||j|7}|jtkr�t|j	�}q|jt
krtj|j	d�}|j
d�}qqW|s�|dkr�dStjd||�t||�S(s�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.
    isutf-8ts!rta attrs: ifname %s operstate %dN(R-tRTATTR_START_OFFSETtlenR9Rt
PAD_ALIGNMENTRtIFLA_OPERSTATEtordRtIFLA_IFNAMERt
decode_binarytstripRR R	(RR
RR5tattrtpadlentinterface_name((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytread_rta_oper_state�s$
c
Cst}t}t�}tjd�x�trt|t�}|dkrLq%ntjdt|��||7}tjdt|��d}t|�}xs||kr	||}t|�t	kr�tjd�Pnt
|�}	t|�|	jkrtjd�Pn|	jtdtd@}
||
}tjd|�|	j
ttgkrQq�nt|�}|dkrtjd	|�q�n|j|kr�tjd
|j|�q�n|jttgkr�q�n|}|j}|tko�|tk}|r�tjd|�dSq�W||}q%WdS(
s�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.
    s1Wait for media disconnect and reconnect to happensread %d bytes from socketsLength of data after concat %dis#Data is smaller than netlink headers*Partial data. Smaller than netlink messageis"offset to next netlink message: %ds!Failed to read rta attributes: %ss6Ignored netlink event on interface %s. Waiting for %s.sMedia switch happened on %s.N(tOPER_UPtbytesRR tTrueR3tSELECT_TIMEOUTR-R<t
NLMSGHDR_SIZER+RR=R
tRTM_NEWLINKtRTM_DELLINKRFR
Rt	OPER_DOWN(
R!R
tcarriertprevCarrierRt	recv_dataR5tdatalentnl_msgtnlheaderRDtinterface_statetisVnetSwitch((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyt!wait_for_media_disconnect_connect�sZ
	
	





	(6t	cloudinitRtloggingRtcollectionsRRR,RR%t	getLoggerRRRt
NLMSG_NOOPtNLMSG_ERRORt
NLMSG_DONERLRMtRTM_GETLINKtRTM_SETLINKR/tRTA_DATA_OFFSETR(RJR't
IFINFOMSG_FMTtcalcsizeRKtIFINFOMSG_SIZER;R7R=R@R>tOPER_UNKNOWNtOPER_NOTPRESENTRNtOPER_LOWERLAYERDOWNtOPER_TESTINGtOPER_DORMANTRGRR	RtRuntimeErrorRR$R+R-R3R9RFRW(((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyt<module>s\
				#