Current File : //usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyc
�
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	

cCs�|dk	std��t|�tks6td��tjt|t �\}}}}}tj	d|�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 */
    };
    sdata is nones+data is smaller than netlink message headersGot netlink msg of type %dN(tNonetAssertionErrortlent
NLMSGHDR_SIZEtstructtunpacktNLMSGHDR_FMTtMSG_TYPE_OFFSETRR R(Rtmsg_lentmsg_typeRRR((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytget_netlink_msg_headerSs		cCs�|dk	std��tj|ggg|�\}}}||krLdStjd�|jt�}|dkr�tjd�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 is nonesnetlink socket ready for reads,Reading from Netlink socket returned no dataN(R%R&tselectRR trecvtMAX_SIZER(R!ttimeouttread_sett_R((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytread_netlink_socketjs
$
cCs�|dk	std��t|�tks6td��|tksNtd��d}}d}y@tjd|d|�d}tjd|d|d�d}Wntjk
r�dSX||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.
    sdata is nonesoffset is not integers'rta offset is less than expected lengthitHtoffsetiN(
R%R&R
tinttRTATTR_START_OFFSETR)tunpack_fromRtRTA_DATA_START_OFFSETR(RR8RRt	attr_data((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytunpack_rta_attr�s	
$cCs<|dk	std��t|�tks6td��d}}t}x�|t|�krt||�}|s�|jdkr�Pnt|jtt}||j|7}|jtkr�t	|j
�}qI|jtkrItj
|j
d�}|jd�}qIqIW|s|dkrdStjd||�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.
    sdata is nones2length of data is smaller than RTATTR_START_OFFSETisutf-8ts!rta attrs: ifname %s operstate %dN(R%R&R'R:R>Rt
PAD_ALIGNMENTRtIFLA_OPERSTATEtordRtIFLA_IFNAMERt
decode_binarytstripRR R	(RR
RR8tattrtpadlentinterface_name((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytread_rta_oper_state�s*
	
c
Csj|dk	std��|dk	s0td��t|�dksNtd��t}t}t�}tjd�x�tret|t	�}|dkr�qsntjdt|��||7}tjdt|��d}t|�}xs||krW||}t|�t
krtjd�Pnt|�}	t|�|	jkrPtjd	�Pn|	jt
d
t
d
@}
||
}tjd|�|	jttgkr�q�nt|�}|dkr�tjd|�q�n|j|kr�tjd
|j|�q�n|jttgkrq�n|}|j}|tko7|tk}|r�tjd|�dSq�W||}qsWdS(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.
    snetlink socket is nonesinterface name is noneisinterface name cannot be emptys1Wait for media disconnect and reconnect to happensread %d bytes from socketsLength of data after concat %ds#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(R%R&R'tOPER_UPtbytesRR tTrueR6tSELECT_TIMEOUTR(R/RR@R
tRTM_NEWLINKtRTM_DELLINKRIR
Rt	OPER_DOWN(
R!R
tcarriertprevCarrierRt	recv_dataR8tdatalentnl_msgtnlheaderRGtinterface_statetisVnetSwitch((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyt!wait_for_media_disconnect_connect�s`
	
	





	(6t	cloudinitRtloggingRtcollectionsRRR0RR)t	getLoggerRRRt
NLMSG_NOOPtNLMSG_ERRORt
NLMSG_DONERNROtRTM_GETLINKtRTM_SETLINKR2tRTA_DATA_OFFSETR,RMR+t
IFINFOMSG_FMTtcalcsizeR(tIFINFOMSG_SIZER:R<R@RCRAtOPER_UNKNOWNtOPER_NOTPRESENTRPtOPER_LOWERLAYERDOWNtOPER_TESTINGtOPER_DORMANTRJRR	RtRuntimeErrorRR$R/R%R6R>RIRY(((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyt<module>s\
				#