oB]c@sddlmZddlmZddlmZddlZddlZddlZddl Z ej e Z dZ dZdZdZdZd Zd Zd Zd Zd ZdZdZdZdZe jeZe jeZeeZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)eddddgZ*edddgZ+edddddd gZ,d!e-fd"YZ.d#Z/d$Z0dd%Z2d&Z3d'Z4d(Z5dS()i(tlog(tutil(t namedtupleNiiiiiiiii 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.pyR8scCsyHtjtjtjtj}|jtjtf|jdWn,tj k rv}d|}t |nXt j 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_ROUTEtbindtostgetpidt RTMGRP_LINKt setblockingterrorRtLOGtdebug(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}|dkrltjdn|S(sSelect 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 = ) 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 $   cCsd}}d}y@tjd|d|d}tjd|d|dd}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%t unpack_fromRtRTA_DATA_START_OFFSETR(RR5RRt attr_data((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytunpack_rta_attrs $cCsd}}t}x|t|krt||}| sJ|jdkrNPnt|jtt}||j|7}|jtkrt|j }q|jt krt j |j d}|j d}qqW| s|dkrdStjd||t||S(sReads 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_OPERSTATEtordRt IFLA_IFNAMERt decode_binarytstripRR R (RR R R5tattrtpadlentinterface_name((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pytread_rta_oper_states$ c Cst}t}t}tjdxtrt|t}|d krLq%ntjdt|||7}tjdt|d}t|}xs||kr ||}t|t krtjdPnt |} t|| j krtjdPn| j t dt d@} || }tjd|| j ttgkrQqnt|} | d krtjd | qn| j|krtjd | j|qn| jttgkrqn|}| j}|tko|tk} | rtjd |d SqW||}q%Wd S( sBlock 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 t RTM_NEWLINKt RTM_DELLINKRFR R t OPER_DOWN( R!R tcarriert prevCarrierRt recv_dataR5tdatalentnl_msgtnlheaderRDtinterface_statet isVnetSwitch((sE/usr/lib/python2.7/site-packages/cloudinit/sources/helpers/netlink.pyt!wait_for_media_disconnect_connectsZ                (6t cloudinitRtloggingRt collectionsRRR,RR%t getLoggerRRRt NLMSG_NOOPt NLMSG_ERRORt NLMSG_DONERLRMt RTM_GETLINKt RTM_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.pyts\           #