HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/self/root/lib/python3/dist-packages/cloudinit/net/__pycache__/__init__.cpython-38.pyc
U

Ӈg���@sLddlZddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZddlm
Z
mZddlmZe�e�ZdZdZddd	d
gZddd
ddddddddgZe�d�fdd�Zdd�Zd�dd�Zd�dd�Zd�d d!�Zd"d#�Zd$d%�Zd&d'�Z d(d)�Z!d*d+�Z"d,d-�Z#d.d/�Z$d0d1�Z%ej&dd2�e'd3�d4d5��Z(ej&dd2�e)d3�d6d7��Z*e+e'd8�d9d:�Z,d�d;d<�Z-d=d>�Z.d?d@�Z/d�e'd3�dAdB�Z0d�dCdD�Z1d�dEdF�Z2dGdH�Z3dIdJ�Z4dKdL�Z5dMdN�Z6dOdP�Z7GdQdR�dRe8�Z9dSdT�Z:e	e+d3�dUdV�Z;e
e+d3�dWdX�Z<e	e+d3�dYdZ�Z=e
e+d3�d[d\�Z>e	e+d3�d]d^�Z?e
e+d3�d_d`�Z@e	e+d3�dadb�ZAe
e+d3�dcdd�ZBd�e
eCd3�dedf�ZDdgdh�ZEd�djdk�ZFd�dmdn�ZGd�dodp�ZHdqdr�ZIdsdt�ZJeCd3�dudv�ZKe+e
e+dw�dxdy�ZLeCd3�dzd{�ZMeCd3�d|d}�ZNeCd3�d~d�ZOeCd3�d�d��ZPd�e'e'e'e'e'e'e'e)d��d�d��ZQed�e	ee+e+e+e+fdd��d�d��ZRd�d��ZSee+d��d�d��ZTe+e'd��d�d��ZUe+e'd��d�d��ZVe+e'd��d�d��ZWe+e'd��d�d��ZXe+e'd��d�d��ZYe+e'd��d�d��ZZe+e+e'd��d�d��Z[e+e+e'd��d�d��Z\e'd3�d�d��Z]e+d3�d�d��Z^e_d3�d�d��Z`e_d3�d�d��Zae+e+e+d��d�d��ZbGd�d��d�ec�ZddS)��N)�Callable�Dict�List�Optional�Tuple)�subp�util)�Iproute2z/sys/class/net/Zeth0�dhcp6Z
ipv6_slaaczipv6_dhcpv6-statelesszipv6_dhcpv6-stateful�	ovs-vsctlz--formatZcsvz
--no-headingsz	--timeoutZ10z	--columns�name�find�	interfacez
type=internalz([0-9]+)cCsdd�t�||�D�S)aDSorting for Humans: natural sort order. Can be use as the key to sort
    functions.
    This will sort ['eth0', 'ens3', 'ens10', 'ens12', 'ens8', 'ens0'] as
    ['ens0', 'ens3', 'ens8', 'ens10', 'ens12', 'eth0'] instead of the simple
    python way which will produce ['ens0', 'ens10', 'ens12', 'ens3', 'ens8',
    'eth0'].cSs$g|]}|��rt|�n|���qS�)�isdigit�int�lower)�.0�textrr�8/usr/lib/python3/dist-packages/cloudinit/net/__init__.py�
<listcomp>2s�z$natural_sort_key.<locals>.<listcomp>)�re�split)�sZ_nsrerrr�natural_sort_key+s
�rcCstS)z3Simple function to return the global SYS_CLASS_NET.)�
SYS_CLASS_NETrrrr�get_sys_class_path8sr�cCst�|d|S)N�/)r��devname�pathrrr�sys_dev_path=sr"c

Cs
t||�}zt�|�}Wn�ttfk
r�}z^t|dd�}	|	tjtjfkrb|dk	rb||�WY�,S|	tj	fkr�|dk	r�||�WY�S�W5d}~XYnX|�
�}|dkr�|Sz
||WStk
�r}z,|dk	r�||�WY�St�
d||��W5d}~XYnXdS)N�errnoz5Found unexpected (not translatable) value '%s' in '%s)r"rZload_text_file�OSError�IOError�getattrr#�ENOENTZENOTDIRZEINVAL�strip�KeyError�LOG�debug)
r r!�	translate�	on_enoent�on_keyerror�	on_einvalZdev_path�contents�eZe_errnorrr�read_sys_netAs4

�r2cCsdd�}t||||||d�S)NcSsdS�NFr)r1rrr�
on_excp_falsegsz(read_sys_net_safe.<locals>.on_excp_false)r.r-r/r,)r2)�iface�fieldr,r4rrr�read_sys_net_safefs�r7cCs<t||�}|dkrdSz
t|�WStk
r6YdSXdSr3)r7r�
ValueError)r5r6�valrrr�read_sys_net_intts

r:cCsdddd�}t|d|d�S)NTF)�up�unknown�down�	operstate)r,�r7)r r,rrr�is_up~sr@cCstj�t|d��S)N�bridge��osr!�existsr"�r rrr�	is_bridge�srFcCstj�t|d��S)N�bondingrBrErrr�is_bond�srHcCs t|dd�}tj�|�r|SdS)z8Return the master path for devname, or None if no master�master�r!N)r"rCr!rDrrrr�
get_master�srKcCsHt|�}|dkrdStj�|d�}tj�|d�}tj�|�pFtj�|�S)z@Return a bool indicating if devname's master is a bridge or bondNFrGrA)rKrCr!�joinrD)r �master_pathZbonding_pathZbridge_pathrrr�master_is_bridge_or_bond�srNcCs,t|�}|dkrdSt|dd�}tj�|�S)z;Return a bool indicating if devname's master is openvswitchNFzupper_ovs-systemrJ)rKr"rCr!rD)r rMZovs_pathrrr�master_is_openvswitch�s
rOcCst|d�dkS)N�type�32r?rErrr�is_ib_interface�srR)�maxsize)�returncCs tt�d��}|st�d�|S)zDReturn a bool indicating if Open vSwitch is installed in the system.rz<ovs-vsctl not in PATH; not detecting Open vSwitch interfaces)�boolrZwhichr*r+)�retrrr�openvswitch_is_installed�s�rWc
Csfzt�t�\}}WnFtjk
rX}z&d|jkrFt�d�gWY�S�W5d}~XYn
X|��SdS)z�Return a list of the names of OVS internal interfaces on the system.

    These will all be strings, and are used to exclude OVS-specific interface
    from cloud-init's network configuration handling.
    zdatabase connection failedzJOpen vSwitch is not yet up; no interfaces will be detected as OVS-internalN)r�!OVS_INTERNAL_INTERFACE_LOOKUP_CMDZProcessExecutionError�stderrr*�info�
splitlines)�out�_err�excrrr�get_ovs_internal_interfaces�s
�r_)r rTcCs,t�s
dSt�}||kr(t�d|�dSdS)z�Returns True if this is an OVS internal interface.

    If OVS is not installed or not yet running, this will return False.
    FzDetected %s as an OVS interfaceT)rWr_r*r+)r Zovs_bridgesrrr�!is_openvswitch_internal_interface�sr`cCs,|dkrt|�}t||�s$t||�r(dSdS)z�netfailover driver uses 3 nics, master, primary and standby.
    this returns True if the device is either the primary or standby
    as these devices are to be ignored.
    NTF)�
device_driver�is_netfail_primary�is_netfail_standby�r �driverrrr�is_netfailover�s�rfcCs,d}zt|d�}Wntk
r&YnX|S)zDReturns a str from reading /sys/class/net/<devname>/device/features.rzdevice/features)r2�	Exception�r Zfeaturesrrr�get_dev_features�sricCs(t|�}|rt|�dkrdS|ddkS)z�Return True if VIRTIO_NET_F_STANDBY bit (62) is set.

    https://github.com/torvalds/linux/blob/         089cf7f6ecb266b6a4164919a2e69bd2f938374a/         include/uapi/linux/virtio_net.h#L60
    �@F�>�1)ri�lenrhrrr�has_netfail_standby_feature�srncCs<t|�dk	rdS|dkr t|�}|dkr,dSt|�s8dSdS)z�A device is a "netfail master" device if:

    - The device does NOT have the 'master' sysfs attribute
    - The device driver is 'virtio_net'
    - The device has the standby feature bit set

    Return True if all of the above is True.
    NF�
virtio_netT�rKrarnrdrrr�is_netfail_masters	rqcCstt|dd�}tj�|�sdS|dkr,t|�}|dkr8dStj�tj�|��}t|�}|dkr`dSt|�}|spdSdS)a7A device is a "netfail primary" device if:

    - the device has a 'master' sysfs file
    - the device driver is not 'virtio_net'
    - the 'master' sysfs file points to device with virtio_net driver
    - the 'master' device has the 'standby' feature bit set

    Return True if all of the above is True.
    rIrJFNroT)r"rCr!rDra�basename�realpathrn)r reZmaster_sysfs_pathZmaster_devnameZ
master_driverZmaster_has_standbyrrrrbsrbcCs<t|�dkrdS|dkr t|�}|dkr,dSt|�s8dSdS)z�A device is a "netfail standby" device if:

    - The device has a 'master' sysfs attribute
    - The device driver is 'virtio_net'
    - The device has the standby feature bit set

    Return True if all of the above is True.
    NFroTrprdrrrrc9s	rccCst|d�}|r|dkrdSdS)a�
    /* interface name assignment types (sysfs name_assign_type attribute) */
    #define NET_NAME_UNKNOWN      0  /* unknown origin (not exposed to user) */
    #define NET_NAME_ENUM         1  /* enumerated by kernel */
    #define NET_NAME_PREDICTABLE  2  /* predictably named by the kernel */
    #define NET_NAME_USER         3  /* provided by user-space */
    #define NET_NAME_RENAMED      4  /* renamed by user-space */
    �name_assign_type)�3�4TFr?)r rtrrr�
is_renamedQs	
rwcCstt|d��}d|��kS)N�ueventzDEVTYPE=vlan)�strr7r[)r rxrrr�is_vlan`srzcCs0d}t|d�}tj�|�r,tj�t�|��}|S)z8Return the device driver for net device named 'devname'.Nz
device/driver)r"rCr!�islinkrr�readlink)r reZdriver_pathrrrraes

racCst|d�}|dkrdS|S)z;Return the device id string for net device named 'devname'.z
device/deviceFNr?)r Zdev_idrrr�device_devidps
r}c
Csjt��st��rtt����Szt�t��}Wn6t	k
rd}z|j
t
jkrRg}n�W5d}~XYnX|S�N)r�
is_FreeBSD�is_DragonFlyBSD�list�get_interfaces_by_mac�valuesrC�listdirrr$r#r')�devsr1rrr�get_devicelistysr�c@seZdZdZdS)�ParserErrorz6Raised when a parser has issue parsing a file/content.N)�__name__�
__module__�__qualname__�__doc__rrrrr��sr�cCs |rt|t�sdS|�d�dkS)NF�configZdisabled)�
isinstance�dict�get)�cfgrrr�is_disabled_cfg�sr�cCs6t��st��rt�St��s&t��r,t�St�SdS)zqGet the list of network interfaces viable for networking.

    @return List of interfaces, sorted naturally.
    N)rrr��find_candidate_nics_on_freebsd�	is_NetBSD�
is_OpenBSD�(find_candidate_nics_on_netbsd_or_openbsd�find_candidate_nics_on_linuxrrrr�find_candidate_nics�s
r�cCs6t��st��rt�St��s&t��r,t�St�SdS)z.Get the name of the 'fallback' network device.N)rrr��find_fallback_nic_on_freebsdr�r��&find_fallback_nic_on_netbsd_or_openbsd�find_fallback_nic_on_linuxrrrr�find_fallback_nic�s
r�cCstt���td�S)zmGet the names of the candidate network devices on NetBSD/OpenBSD.

    @return list of sorted interfaces
    ��key)�sortedr�r�rrrrrr��sr�cCst�}|r|dSdS)zfGet the 'fallback' network device name on NetBSD/OpenBSD.

    @return default interface, or None
    rN)r���namesrrrr��sr�cCs8t�ddddg�\}}|��}|r&|Stt���td�S)zgGet the names of the candidate network devices on FreeBSD.

    @return List of sorted interfaces.
    �ifconfigz-lz-u�etherr�)rrr�r�r�r)�stdoutZ_stderrr�rrrr��s
r�cCst�}|r|dSdS)z_Get the 'fallback' network device name on FreeBSD.

    @return List of sorted interfaces.
    rN)r�r�rrrr��sr�c
CsFdt��krt�d�n,dd�t�D�}t|�rDt�d|�t��g}g}tddddddd�D]�\}}}}|d	krvq`|�d
�r�t�d|�q`t	|d�}|r�|�
|�q`t�d
|�t	|d�}|r�|�
|�q`t|d�}|dkr�|�
|�q`t�d|�q`g}||fD]:}	t|	t
d�}	t|	k�r6|	�t�|	�dt�||	7}�q|S)zeGet the names of the candidate network devices on Linux.

    @return List of sorted interfaces.
    z
net.ifnames=0z9Stable ifnames disabled by net.ifnames=0 in /proc/cmdlinecSs g|]}|dkrt|�s|�qS)�lo)rw)rZdevicerrrr�s�z0find_candidate_nics_on_linux.<locals>.<listcomp>z4Found unstable nic names: %s; calling udevadm settleFT)�filter_openvswitch_internal�2filter_slave_if_master_not_bridge_bond_openvswitch�filter_vlan�filter_without_own_mac�filter_zero_mac�log_filtered_reasonsr�ZvethzIgnoring veth interface: %s�carrierzInterface has no carrier: %s�dormantr>)r�r=Zlowerlayerdownr<zInterface ignored: %sr�r)rZget_cmdliner*r+r�rmZudevadm_settle�get_interfaces�
startswithr:�appendr7r�r�DEFAULT_PRIMARY_INTERFACE�remove�insert)
ZunstableZ	connectedZpossibly_connectedr�_r�r�r>Zsorted_interfaces�
interfacesrrrr��s`���








r�cCst�}|r|dSdS)z]Get the 'fallback' network device name on Linux.

    @return List of sorted interfaces.
    rN)r�r�rrrr�sr�cCsjt�}|sdSt|�r d|i}ndt|d���i}|rJt|�}|rJ||d<dd||d�}||idd	�}|S)
zBGenerate network cfg v2 for dhcp on the NIC most likely connected.Nr�
macaddress�addressreT)Zdhcp4r
�set-name�match�)�	ethernets�version)r�rqr7rra)Z
config_driverZtarget_namer�rer�Znconfrrr�generate_fallback_config+s&
��r�cCsJdd�}dd�}|�d�}|dkr*||�S|dkr:||�Std|��dS)	NcSs�g}|�di�D]|}|�d�dkr$q|�d�}|s4q|�d�}|�di��d�}|�di��d�}|snt|�}|szt|�}|�||||g�q|S)	Nr�rPZphysicalZmac_addressr�paramsre�	device_id)r�rar}r�)�netcfg�physdevs�ent�macrrer�rrr�
_version_1Is 

z$extract_physdevs.<locals>._version_1cSs�g}|�di���D]z}|�d�}|s(q|�di��d�}|s@q|�di��d�}|�di��d�}|spt|�}|s|t|�}|�||||g�q|S)Nr�r�r�r�rer�)r�r�rar}r�)r�r�r�rr�rer�rrr�
_version_2[s 
z$extract_physdevs.<locals>._version_2r��r�z"Unknown network config version: %s)r��RuntimeError)r�r�r�r�rrr�extract_physdevsHs
r�FcCs*t|d�}|dkr"|rtd��dS|dkS)a�return True if the provided interface has its own address.

    Based on addr_assign_type in /sys.  Return true for any interface
    that does not have a 'stolen' address. Examples of such devices
    are bonds or vlans that inherit their mac from another device.
    Possible values are:
      0: permanent address    2: stolen from another device
    1: randomly generated   3: set using dev_set_mac_addressZaddr_assign_typeNz%s had no addr_assign_type.T)rr��)r:r8)�ifname�strictZassign_typerrr�interface_has_own_macxs

r�Tc
Cs�i}t�D]*\}}}}d|||��|t|�d�||<q
|r�t�d�}tjdddddd	d
gdd�\}}tjdd
ddgdd�\}	}t�}
||	fD]}|
�|�|��q�|�	�D] }|ddkp�|d|
k|d<q�|S)a�Collect information necessary for rename_interfaces.

    returns a dictionary by mac address like:
       {name:
         {
          'downable': None or boolean indicating that the
                      device has only automatically assigned ip addrs.
          'device_id': Device id value (if it has one)
          'driver': Device driver (if it has one)
          'mac': mac address (in lower case)
          'name': name
          'up': boolean: is_up(name)
         }}
    N)�downabler�rer�rr;z[0-9]+:\s+(\w+)[@:]�ipz-6ZaddrZshowZ	permanentZscope�globalT)Zcapturez-4r;Frr�)
r�rr@r�compiler�set�update�findallr�)
Zcheck_downable�cur_inforr�rer�ZnmatchZipv6r]Zipv4Znics_with_addressesZ	bytes_out�drrr�_get_current_rename_info�s.�	
�
�r�cstt|�st�d�dS|dkr$t�}i�|��D]:\}}|��}|�d�rZ|d��|d<||d<|�|<q0t�d��dd�}g}g}	g}
|��}d}d}
d	d
����fdd�}|D�]�\}}}}|r�|��}g}||||�}|s�|r�|	�d
||f�q�|�d�}||k�rq�|�s0|r�|	�d
||f�q�|d�r�d}|d�sb|r�|	�||||f�q�d|d<|�d|||ff�|
�d|||ff�||k�r\||}|d�r�d}|d�s�|r�|	�||||f�q�n|�d|||ff�d}|dk�s||k�r|
d7}
||
}�q�|�d||||ff�||d<|��}|d�r\|
�d|||ff�|�d|||d|ff�||d<|��}||7}q�t	j
t	jt	jd�}t|�t|
�dk�r�t|	��r�t�
d||	�nt�d|�nxt�d|||
�||
D]\\}}}}z|||�Wn<tk
�rR}z|	�d|||||f�W5d}~XYnX�q�t|	��rptd�|	���dS)Nzno interfaces to renamer�rzDetected interfaces %scSstdd�|��D��S)Ncss|]}|d|fVqdS)rNr�r�datarrr�	<genexpr>�sz<_rename_interfaces.<locals>.update_byname.<locals>.<genexpr>)r�r�)Zbymacrrr�
update_byname�sz)_rename_interfaces.<locals>.update_bynamez
cirename%d���cSsd|r0|r0|r0|d|ko.|d|ko.|d|kS|rP|rP|d|koN|d|kS|r`|d|kSdS)zmatch if set and in datar�rer�Fr)r�r�rer�rrr�entry_match�s
�
�z'_rename_interfaces.<locals>.entry_matchcsT����fdd����D�}t|�rPt|�dkrHd|���f}t|��|dSdS)Ncsg|]}�|����r|�qSrrr�)r�rer�r�rrr�s�z:_rename_interfaces.<locals>.find_entry.<locals>.<listcomp>r�zjFailed to match a single device. Matched devices "%s" with search values "(mac:%s driver:%s device_id:%s)"r)r�rmr8)r�rer�r��msg�r�r�)r�rer�r�
find_entry�s�
��z&_rename_interfaces.<locals>.find_entryz<[nic not present] Cannot rename mac=%s to %s, not available.r;z*[busy] Error renaming mac=%s from %s to %sr�Fr=z2[busy-target] Error renaming mac=%s from %s to %s.r��rename)r�r=r;rz1Unable to rename interfaces: %s due to errors: %sz$no work necessary for renaming of %szRenamed %s with ops %sz.[unknown] Error performing %s%s for %s, %s: %s�
)rmr*r+r��items�copyr�rr�r	Zlink_renameZ	link_downZlink_up�warningrgr�rL)�renamesZstrict_presentZstrict_busyZcurrent_inforr�Zcurr�Zops�errorsZupsZ
cur_bynameZtmpname_fmtZtmpir�r��new_namerer�Zcur_opsZcur_namer��targetZtmp_nameZopmap�opr�r1rr�r�_rename_interfaces�s�


��

��






�
���
r�cCs$d}tj�t|d��rd}t||�S)z6Returns the string value of an interface's MAC Addressr�Z
bonding_slavezbonding_slave/perm_hwaddr)rCr!�isdirr"r7)r�r!rrr�get_interface_macXsr�cCs>t|d�dkr:t|�}|r6|r6|dd�|dd�}|SdS)z�Returns the string value of an Infiniband interface's hardware
    address. If ethernet_format is True, an Ethernet MAC-style 6 byte
    representation of the address will be returned.
    rPrQ�$i��3N)r7r�)r�Zethernet_formatr�rrr�get_ib_interface_hwaddrbs
r�cCs<t��st��rt�St��r$t�St��r2t�St�SdSr~)	rrr�� get_interfaces_by_mac_on_freebsdr��get_interfaces_by_mac_on_netbsdr�� get_interfaces_by_mac_on_openbsd�get_interfaces_by_mac_on_linuxrrrrr�psr�)r�rTcCs0t���D] \}}|��|��kr
|Sq
dSr~)r�r�r)r�Z
interface_macZinterface_namerrr�find_interface_name_from_mac{s
r�cCs>t�dddg�\}}dd�}dd�}dd	�|||��D�}|S)
Nr��-ar�css@d}|�d�D]&}|�d�r&||7}q|r0|V|}q|VdS)Nrr��	)rr�)r\Z
curr_block�linerrr�flatten�s

z1get_interfaces_by_mac_on_freebsd.<locals>.flattencss4|D]*}t�d|�}|r|�d�|�d�fVqdS)Nz2^(?P<ifname>\S*): .*ether\s(?P<mac>[\da-f:]{17}).*r�r�)r�search�group)Z	flat_list�block�mrrr�find_mac�s�z2get_interfaces_by_mac_on_freebsd.<locals>.find_maccSsi|]\}}||�qSrr)rr�r�rrr�
<dictcomp>�sz4get_interfaces_by_mac_on_freebsd.<locals>.<dictcomp>)r)r\r�r�r�Zresultsrrrr��s
r�cCsbi}d}t�ddg�\}}t�dd|���}|D],}t�||�}|r0|��}|d||d<q0|S)NzE(?P<ifname>\w+).*address:\s(?P<mac>([\da-f]{2}[:-]){5}([\da-f]{2})).*r�r��\n\s+� r�r��rr�subr[r��	groupdict�rVZre_field_matchr\r�Zif_linesr�r�Zfieldsrrrr��s�r�cCsbi}d}t�ddg�\}}t�dd|���}|D],}t�||�}|r0|��}|d||d<q0|S)NzC(?P<ifname>\w+).*lladdr\s(?P<mac>([\da-f]{2}[:-]){5}([\da-f]{2})).*r�r�r�r�r�r�rrrrrr��s�r�cCs�i}t�D]�\}}}}||krV|dkr<t�d||||�q
d||||f}t|��|||<t|d�}|r
||kr~|||<q
t�d||||�q
|S)zmBuild a dictionary of tuples {mac: name}.

    Bridges and any devices that have a 'stolen' mac are excluded.)Z	fsl_enetcZ
mscc_felixZqmi_wwanz>Ignoring duplicate macs from '%s' and '%s' due to driver '%s'.z6duplicate mac found! both '%s' and '%s' have mac '%s'.Tz^Ethernet and InfiniBand interfaces have the same address both '%s' and '%s' have address '%s'.)r�r*r+r�r�r�)rVrr�reZ_devidr��ib_macrrrr��s:��

�r�)�filter_hyperv_vf_with_syntheticr�r�r�r�r�r�rTcCs$|r
tjndd�}g}t�}	d�d�}
|	D]�}|r<t|�s<q*t|�rP|d|�q*|r^t|�r^q*t|�rr|d|�q*|r�t|�dk	r�t	|�s�t
|�s�q*t|�r�|d|�q*t|�}|s�|d	|�q*|r�|d
kr�||
dt
|��kr�q*|r�t|�r�q*t|�}
|�|||
t|�f�q*|�r t||�|S)z�Return list of interface tuples (name, mac, driver, device_id)

    Bridges and any devices that have a 'stolen' mac are excluded.cWsdSr~r)�argsrrr�<lambda>�z get_interfaces.<locals>.<lambda>�:)�00r
r
r
r
r
r
r
r
r
r
r
r
r
r
r
zIgnoring bridge interface: %szIgnoring bond interface: %sNzIgnoring failover interface: %sz"Ignoring interface without mac: %sr�)r*r+r�rLr�rFrzrHrKrNrOrfr�rmr`rar�r}�)filter_hyperv_vf_with_synthetic_interface)rr�r�r�r�r�r��filtered_loggerrVr�Zzero_macrr�rerrrr�sT


�
���

 �
r�).N)rr�rTcsXdd�|D���fdd�|D�}|D].}|\}}}}|d||�||�|�|�q$dS)a�Filter Hyper-V SR-IOV/VFs when used with synthetic hv_netvsc.

    Hyper-V's netvsc driver may register an SR-IOV/VF interface with a mac
    that matches the synthetic (hv_netvsc) interface.  This VF will be
    enslaved to the synthetic interface, but cloud-init may be racing this
    process.  The [perhaps-yet-to-be-enslaved] VF should never be directly
    configured, so we filter interfaces that duplicate any hv_netvsc mac
    address, as this the most reliable indicator that it is meant to be
    subordinate to the synthetic interface.

    VF drivers will be mlx4_core, mlx5_core, or mana.  However, given that
    this list of drivers has changed over time and mana's dependency on
    hv_netvsc is expected to be removed in the future, we no longer rely
    on these names. Note that this will not affect mlx4/5 instances outside
    of Hyper-V, as it only affects environments where hv_netvsc is present.
    cSs&i|]}|ddkr|d|d�qS)r��	hv_netvscr�rr�r�irrrr�Usz=filter_hyperv_vf_with_synthetic_interface.<locals>.<dictcomp>cs(g|] }|d�kr|ddkr|�qS)r�r�r
rr�Zhv_netvsc_mac_to_namerrrXs�z=filter_hyperv_vf_with_synthetic_interface.<locals>.<listcomp>zdIgnoring %r VF interface with driver %r due to synthetic hv_netvsc interface %r with mac address %r.N)r�)rr�Zinterfaces_to_removerrr�rer�rrrrAs �
��rcCsPi}t�D]@\}}}}t|d�}|r
||krBtd||||f��|||<q
|S)zTBuild a dictionary mapping Infiniband interface names to their hardware
    address.Fz5duplicate mac found! both '%s' and '%s' have mac '%s')r�r�r�)rVrr�rrrr�get_ib_hwaddrs_by_interfaceks
��
r)�convert_to_addressr�cKs*z||f|�WStk
r$YdSXdS)aUse a function to return an address. If conversion throws a ValueError
    exception return False.

    :param check_cb:
        Test function, must return a truthy value
    :param address:
        The string to test.

    :return:
        Address or False

    FN)r8)rr��kwargsrrr�maybe_get_address{s
r)r�rTcCstttj|��S)z�Returns a bool indicating if ``s`` is an IP address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IP address or not.
    )rUr�	ipaddress�
ip_address�r�rrr�
is_ip_address�s	rcCstttj|��S)z�Returns a bool indicating if ``s`` is an IPv4 address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    )rUrrZIPv4Addressrrrr�is_ipv4_address�s	rcCstttj|��S)z�Returns a bool indicating if ``s`` is an IPv6 address.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    )rUrrZIPv6Addressrrrr�is_ipv6_address�s	rcCstttj|dd��S)z�Returns a bool indicating if ``s`` is an IPv4 or IPv6 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    F�r�)rUrr�
ip_networkrrrr�
is_ip_network�s	rcCstttj|dd��S)z�Returns a bool indicating if ``s`` is an IPv4 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    Fr)rUrr�IPv4Networkrrrr�is_ipv4_network�s	�rcCstttj|dd��S)z�Returns a bool indicating if ``s`` is an IPv6 network.

    :param address:
        The string to test.

    :return:
        A bool indicating if the string is an IPv4 address or not.
    Fr)rUrrZIPv6Networkrrrr�is_ipv6_network�s	�r )r��subnetrTcCs t�|�}tj|dd�}||kS)z�Returns a bool indicating if ``s`` is in subnet.

    :param address:
        The string of IP address.

    :param subnet:
        The string of subnet.

    :return:
        A bool indicating if the string is in subnet.
    Fr)rrr)r�r!rZsubnet_networkrrr�is_ip_in_subnet�s
r")�gatewayr!rTc
CsLzt||�WStk
rF}zt�d|||�WY�dSd}~XYnXdS)z�Returns a bool indicating if should add gateway onlink flag.

    :param gateway:
        The string of gateway address.

    :param subnet:
        The string of subnet.

    :return:
        A bool indicating if the string is in subnet.
    zDFailed to check whether gateway %s is contained within subnet %s: %sFN)r"r8r*r�)r#r!r1rrr�should_add_gateway_onlink_flag�s�r$cCs@|d�d�s|dtkrdS|ddkr<t|�d��r<dSdS)z:Common helper for checking network_state subnets for ipv6.rP�6TZstaticr�F)�endswith�IPV6_DYNAMIC_TYPESrr�)r!rrr�subnet_is_ipv6s
r(cCstt�d|���j�S)z�Convert a network prefix to an ipv4 netmask.

    This is the inverse of ipv4_mask_to_net_prefix.
        24 -> "255.255.255.0"
    Also supports input as a string.�0.0.0.0/)ryrr�netmask)�prefixrrr�net_prefix_to_ipv4_masksr,cCst�d|���jS)aConvert an ipv4 netmask into a network prefix length.

    If the input is already an integer or a string representation of
    an integer, then int(mask) will be returned.
       "255.255.255.0" => 24
       str(24)         => 24
       "24"            => 24
    r))rr�	prefixlen)�maskrrr�ipv4_mask_to_net_prefixs	r/cCs�zt�d|���j}|WStk
r,YnXt�|�}t|�}|dkrL|Sttj||d@���}||?}tj|}d|>d}||kr�td|��|S)z�Convert an ipv6 netmask (very uncommon) or prefix (64) to prefix.

    If the input is already an integer or a string representation of
    an integer, then int(mask) will be returned.
       "ffff:ffff:ffff::"  => 48
       "48"                => 48
    z::/rr�zInvalid network mask '%s')	rrr-r8rr�minZ
IPV6LENGTH�
bit_length)r.r-r*Zmask_intZtrailing_zeroesZleading_onesZall_onesrrr�ipv6_mask_to_net_prefix's&
�
r2)r.r�rTcCsttj|�d|��dd�j�S)zCGet string representation of broadcast address from an ip/mask pairrFr)ryrrZbroadcast_address)r.r�rrr�mask_and_ipv4_to_bcast_addrJs�r3c@seZdZdS)�RendererNotFoundErrorN)r�r�r�rrrrr4Qsr4)r)NNNN)N)N)N)N)N)N)F)T)TTN)TTTTTTF)er#�	functoolsrZloggingrCr�typingrrrrrZ	cloudinitrrZcloudinit.net.netops.iproute2r	Z	getLoggerr�r*rr�r'rXr�rrr"r2r7r:r@rFrHrKrNrOrR�	lru_cacherUrWr�r_ryr`rfrirnrqrbrcrwrzrar}r�rgr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrrrrr r"r$r(r,rr/r2r3r�r4rrrr�<module>s�
��

�
%


	






!
	

H0

/�

H��=�*	#