@@ -2286,7 +2286,7 @@ def get_dom0_device_name(session, vm_ref, net_ref):
22862286 "Instead, '%s' were returned." %
22872287 (pifs , device_names ))
22882288 device_name = device_names .pop ()
2289- return get_bridge_by_eth (device_name )
2289+ return get_bridge_by_eth (session , device_name )
22902290
22912291
22922292
@@ -2371,55 +2371,43 @@ def set_nic_device_status(session, interface, status):
23712371 wait_for_linkstate (session , interface , status )
23722372 time .sleep (5 )
23732373
2374- def get_eth_by_bridge (bridge : str ) -> str :
2374+ @log_exceptions
2375+ def get_eth_by_bridge (session , bridge : str ) -> str :
23752376 """
2376- Get the device name given a bridge name
2377+ Get the NIC device name given a bridge name.
23772378 """
2378- eth = ""
2379- call = f"xe network-list bridge={ bridge } params=uuid --minimal"
2380- res = make_local_call (call , shell = True )
2381- network_uuid = res ["stdout" ].strip ().split (',' )[0 ]
2382- if network_uuid :
2383- call = f"xe pif-list network-uuid={ network_uuid } params=device --minimal"
2384- res = make_local_call (call , shell = True )
2385- eth = res ["stdout" ].strip ().split (',' )[0 ]
2386-
2387- if not eth :
2388- log .debug (f"Device not found for bridge: { bridge } , use bridge name as device name" )
2389- eth = bridge
2390-
2391- return eth
2392-
2393- def get_bridge_by_eth (eth : str ) -> str :
2379+ host = session .xenapi .session .get_this_host (session .handle )
2380+ pifs = session .xenapi .host .get_PIFs (host )
2381+ for network in session .xenapi .network .get_all ():
2382+ if session .xenapi .network .get_bridge (network ) == bridge :
2383+ for pif in pifs :
2384+ if not session .xenapi .PIF .get_physical (pif ) and \
2385+ session .xenapi .PIF .get_sriov_logical_PIF_of (pif ):
2386+ continue
2387+ if session .xenapi .PIF .get_network (pif ) == network :
2388+ return session .xenapi .PIF .get_device (pif )
2389+
2390+ @log_exceptions
2391+ def get_bridge_by_eth (session , eth : str ) -> str :
23942392 """
2395- Get the bridge name given a device name
2393+ Get the bridge name given a NIC device name
23962394 """
2397- bridge = ""
2398- call = f"xe pif-list device={ eth } params=network-uuid --minimal"
2399- res = make_local_call (call , shell = True )
2400- network_uuid = res ["stdout" ].strip ().split (',' )[0 ]
2401- if network_uuid :
2402- call = f"xe network-list uuid={ network_uuid } params=bridge --minimal"
2403- res = make_local_call (call , shell = True )
2404- bridge = res ["stdout" ].strip ().split (',' )[0 ]
2405-
2406- if not bridge :
2407- log .debug (f"Bridge not found for device: { eth } , use device name as bridge name" )
2408- bridge = eth
2409-
2410- return bridge
2395+ host = session .xenapi .session .get_this_host (session .handle )
2396+ for pif in session .xenapi .host .get_PIFs (host ):
2397+ if not session .xenapi .PIF .get_physical (pif ) and \
2398+ session .xenapi .PIF .get_sriov_logical_PIF_of (pif ):
2399+ continue
2400+ if eth == session .xenapi .PIF .get_device (pif ):
2401+ network = session .xenapi .PIF .get_network (pif )
2402+ return session .xenapi .network .get_bridge (network )
24112403
24122404
24132405# Plase refer to
24142406# https://www.thomas-krenn.com/en/wiki/Predictable_Network_Interface_Names
24152407# https://systemd.io/PREDICTABLE_INTERFACE_NAMES/
2408+ # The nic interface names can start with: eth, eno, enp, ens, enx, end
24162409def is_nic_device_name (name ):
2417- return name .startswith ('eth' ) \
2418- or name .startswith ('eno' ) \
2419- or name .startswith ('enp' ) \
2420- or name .startswith ('ens' ) \
2421- or name .startswith ('enx' ) \
2422- or name .startswith ('end' ) \
2410+ return name [0 ] == 'e'
24232411
24242412class TestThread (threading .Thread ):
24252413 """Threading class that runs a function"""
0 commit comments