[PD] Support get local ip from NIC for PD disaggregation (#7237)
Signed-off-by: Shangming Cai <caishangming@linux.alibaba.com>
This commit is contained in:
@@ -2141,6 +2141,44 @@ def get_free_port():
|
||||
return s.getsockname()[1]
|
||||
|
||||
|
||||
def get_local_ip_auto() -> str:
|
||||
interface = os.environ.get("SGLANG_LOCAL_IP_NIC", None)
|
||||
return (
|
||||
get_local_ip_by_nic(interface)
|
||||
if interface is not None
|
||||
else get_local_ip_by_remote()
|
||||
)
|
||||
|
||||
|
||||
def get_local_ip_by_nic(interface: str) -> str:
|
||||
try:
|
||||
import netifaces
|
||||
except ImportError as e:
|
||||
raise ImportError(
|
||||
"Environment variable SGLANG_LOCAL_IP_NIC requires package netifaces, please install it through 'pip install netifaces'"
|
||||
) from e
|
||||
|
||||
try:
|
||||
addresses = netifaces.ifaddresses(interface)
|
||||
if netifaces.AF_INET in addresses:
|
||||
for addr_info in addresses[netifaces.AF_INET]:
|
||||
ip = addr_info.get("addr")
|
||||
if ip and ip != "127.0.0.1" and ip != "0.0.0.0":
|
||||
return ip
|
||||
if netifaces.AF_INET6 in addresses:
|
||||
for addr_info in addresses[netifaces.AF_INET6]:
|
||||
ip = addr_info.get("addr")
|
||||
if ip and not ip.startswith("fe80::") and ip != "::1":
|
||||
return ip.split("%")[0]
|
||||
except (ValueError, OSError) as e:
|
||||
raise ValueError(
|
||||
"Can not get local ip from NIC. Please verify whether SGLANG_LOCAL_IP_NIC is set correctly."
|
||||
)
|
||||
|
||||
# Fallback
|
||||
return get_local_ip_by_remote()
|
||||
|
||||
|
||||
def get_local_ip_by_remote() -> str:
|
||||
# try ipv4
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
Reference in New Issue
Block a user