o x[h>+@sddlZddlZddlZddlmZmZmZmZddlm Z ddl m Z e e ZeZeZGdddejdZGdd d eZGd d d eZGd d d eZdS)N)net performancesubputil)ifconfig)Iproute2c@seZdZdZdefddZdddeddfdd Zej d e ddfd d Z d e fddZ d e fddZd e defddZddZdddefddZdefddZdefddZd e defd d!Zd e fd"d#Zdefd$d%Zdefd&d'Zd e fd(d)Zdd*d e d+edefd,d-Zd e defd.d/Zd e defd0d1Zej d e defd2d3Zd e defd4d5Zd e defd6d7Zd e defd8d9Z d e defd:d;Z!ej dd<dDd=d>Z"d?d*d e d+eddfd@dAZ#ej d e defdBdCZ$dS)E NetworkingaThe root of the Networking hierarchy in cloud-init. This is part of an ongoing refactor in the cloud-init codebase, for more details see "``cloudinit.net`` -> ``cloudinit.distros.networking`` Hierarchy" in CONTRIBUTING.rst for full details. returncCtSN)r_get_current_rename_infoselfr>/usr/lib/python3/dist-packages/cloudinit/distros/networking.pyr z#Networking._get_current_rename_infoN current_inforenamescCtj||dS)Nr)r_rename_interfaces)rrrrrrrzNetworking._rename_interfacesnetcfgcCdS)z7Read the network config and rename devices accordingly.Nrrrrrrapply_network_config_names z%Networking.apply_network_config_namesdevnamecC t|Sr )r device_devidrrrrrr$ zNetworking.device_devidcCrr )r device_driverr rrrr"'r!zNetworking.device_drivercCrr )rextract_physdevsrrrrr#*r!zNetworking.extract_physdevscCr r )rfind_fallback_nicr rrrr$-rzNetworking.find_fallback_nicF config_driverr&cCs tj|dS)Nr%)rgenerate_fallback_config)rr&rrrr'0 z#Networking.generate_fallback_configcCr r )rget_devicelistr rrrr)3rzNetworking.get_devicelistcCr r )rget_ib_hwaddrs_by_interfacer rrrr*6rz&Networking.get_ib_hwaddrs_by_interfaceethernet_formatcCs t||Sr )rget_ib_interface_hwaddr)rrr+rrrr,9s z"Networking.get_ib_interface_hwaddrcCrr )rget_interface_macr rrrr->r!zNetworking.get_interface_maccCr r )rget_interfacesr rrrr.ArzNetworking.get_interfacescCr r )rget_interfaces_by_macr rrrr/Drz Networking.get_interfaces_by_maccCrr )r get_masterr rrrr0Gr!zNetworking.get_masterstrictr2cCr)Nr1)rinterface_has_own_mac)rrr2rrrr3Jsz Networking.interface_has_own_maccCrr )ris_bondr rrrr4Or!zNetworking.is_bondcCrr )r is_bridger rrrr5Rr!zNetworking.is_bridgecCr)z Is ``devname`` a physical network device? Examples of non-physical network devices: bonds, bridges, tunnels, loopback devices. Nrr rrr is_physicalUrzNetworking.is_physicalcCrr )r is_renamedr rrrr7^r!zNetworking.is_renamedcCrr )ris_upr rrrr8ar!zNetworking.is_upcCrr )ris_vlanr rrrr9dr!zNetworking.is_vlancCrr )rmaster_is_bridge_or_bondr rrrr:gr!z#Networking.master_is_bridge_or_bondexistscCr)aWait for device population in the system to complete. :param exists: An optional optimisation. If given, only perform as much of the settle process as is required for the given DeviceName to be present in the system. (This may include skipping the settle process entirely, if the device already exists.) :type exists: Optional[DeviceName] Nrrr<rrrsettlejrzNetworking.settleTc Cs||}tdd|D}t|}|}tdD]K}||r-tddS| |}td||D](} || } d | } t j | dd |j | d Wdn1s]wYq:|}qd |} t| |ryt| dS) aWait for all the physical devices in `netcfg` to exist on the system Specifically, this will call `self.settle` 5 times, and check after each one if the physical devices are now present in the system. :param netcfg: The NetworkConfig from which to extract physical devices to wait for. :param strict: Raise a `RuntimeError` if any physical devices are not present after waiting. cSsg|] }|d|dfqS)rr).0ifacerrr sz0Networking.wait_for_physdevs..z*net: all expected physical devices presentNz)net: waiting for expected net devices: %szWaiting for settle or {} existsalways)log_moder;z-Not all expected physical devices present: %s)r#dictsetkeysr/rangeissubsetLOGdebug differenceformatrTimedr>warning RuntimeError) rrr2physdevsexpected_ifaces expected_macs present_macs_missingmacrmsgrrrwait_for_physdevsvs.          zNetworking.wait_for_physdevscCr)z=Try setting the link to up explicitly and return if it is up.Nrr rrrtry_set_link_uprzNetworking.try_set_link_upr N)%__name__ __module__ __qualname____doc__rFr listrabcabstractmethod NetworkConfigr DeviceNamerr"r#r$boolr'r)r*r,r-r.r/r0r3r4r5r6r7r8r9r:r>rZr[rrrrrsd    2r) metaclasscseZdZdZfddZedefddZddZd e dd fd d Z d e de fddZ d e de fddZd e de fddZd e de fddZd e de fddZd ddddZd e de fddZZS) BSDNetworkingz>Implementation of networking functionality shared across BSDs.cst|_i|_tdSr )rIfconfigifc_ifssuper__init__r  __class__rrrms zBSDNetworking.__init__r cCs|js||jSr )rk _update_ifsr rrrifsszBSDNetworking.ifscCs0tddg}|dr|j|d|_dSdS)Nrz-ar)rrjparserk)rifconfrrrrpszBSDNetworking._update_ifsrNcCstddS)Nz Cannot rename network interface.)rKrLrrrrrrz(BSDNetworking.apply_network_config_namesrcC |j|jSr )rqr6r rrrr6r(zBSDNetworking.is_physicalcCrtr )rqr4r rrrr4r(zBSDNetworking.is_bondcCrtr )rqr5r rrrr5r(zBSDNetworking.is_bridgecCrtr )rqr9r rrrr9r(zBSDNetworking.is_vlancCrtr )rqupr rrrr8r(zBSDNetworking.is_upr;cCr)z0BSD has no equivalent to `udevadm settle`; noop.Nrr=rrrr>rzBSDNetworking.settlecCstd|dg||S)Try setting the link to up explicitly and return if it is up. Not guaranteed to bring the interface up. The caller is expected to add wait times before retrying.rru)rr8r rrrr[s zBSDNetworking.try_set_link_upr\)r]r^r_r`rmpropertyrFrqrprdrrerfr6r4r5r9r8r>r[ __classcell__rrrnrrhs rhc@s0eZdZdeddfddZdedefddZdS) FreeBSDNetworkingrr NcCsdSr rrrrrrsz,FreeBSDNetworking.apply_network_config_namesrcCsB|j|jsdStjdd|gddgd\}}|d|krdSdS) NFdevinfoz-prr?)rcszdevinfo: {}: Not found T)rqr6rrN)rrrVerrrrrr7s zFreeBSDNetworking.is_renamed)r]r^r_rdrrerfr7rrrrrysryc@seZdZdZdeddfddZdedefdd Zdede fd d Z dede fd d Z dede fddZ dede fddZ dede fddZdede fddZdddddZdede fddZdS)LinuxNetworkingzCImplementation of networking functionality common to Linux distros.rr Nc Cs>z |||WdSty}ztd||d}~ww)zRead the network config and rename devices accordingly. Renames are only attempted for interfaces of type 'physical'. It is expected that the network system will create other devices with the correct name in place. z(Failed to apply network config names: %sN)rr#rQ)rrerrrrsz*LinuxNetworking.apply_network_config_namesrcCrr )rget_dev_featuresr rrrrr!z LinuxNetworking.get_dev_featurescCrr )rhas_netfail_standby_featurer rrrrr!z+LinuxNetworking.has_netfail_standby_featurecCrr )ris_netfailoverr rrrrr!zLinuxNetworking.is_netfailovercCrr )ris_netfail_masterr rrrrr!z!LinuxNetworking.is_netfail_mastercCrr )ris_netfail_primaryr rrrrr!z"LinuxNetworking.is_netfail_primarycCrr )ris_netfail_standbyr rrrr!r!z"LinuxNetworking.is_netfail_standbycCstjt|dS)Ndevice)ospathr<r sys_dev_pathr rrrr6$szLinuxNetworking.is_physicalr;cCs"|dur t|}tj|ddS)Nr;)rrrudevadm_settler=rrrr>'s zLinuxNetworking.settlecCst|||S)rv)rlink_upr8r rrrr[,s  zLinuxNetworking.try_set_link_upr\)r]r^r_r`rdrrestrrrfrrrrrr6r>r[rrrrr}sr})rbloggingr cloudinitrrrrcloudinit.distros.parsersrcloudinit.net.netops.iproute2r getLoggerr]rKrrerFrdABCMetarrhryr}rrrrs   2!