yanger: Refactor how ethtool is read from interfaces

Upgrade ethtool to get json support for main command:

ethtool --json eth1
This commit is contained in:
Mattias Walström
2025-02-13 20:27:00 +01:00
parent c5db34b491
commit 2d806de2cd
64 changed files with 848 additions and 179 deletions
@@ -68,41 +68,26 @@ def statistics(ifname):
return statistics
def link(ethtool):
def link(ifname):
"""Parse speed/duplex/autoneg from ethtool output"""
if data := HOST.run_json(["ethtool", "--json", ifname], {}):
data = data[0]
else:
return None
eth = {}
eth["auto-negotation"] = { "enable": data.get("auto-negotation", False) }
for line in ethtool:
kv = [s.strip() for s in line.split(":")]
if len(kv) != 2:
continue
key, val = kv
match key:
case "Auto-negotiation":
eth["auto-negotiation"] = { "enable": val == "on" }
case "Duplex":
match val:
case "Half":
eth["duplex"] = "half"
case "Full":
eth["duplex"] = "full"
case _:
eth["duplex"] = "unknown"
case "Speed":
mbps = "".join(filter(str.isdigit, val))
if mbps:
gbps = round((int(mbps) / 1000), 3)
eth["speed"] = str(gbps)
if data.get("speed"):
gbps = round((int(data["speed"]) / 1000), 3)
eth["speed"] = str(gbps)
if data.get("duplex"):
eth["duplex"] = data["duplex"].lower()
return eth
def ethernet(iplink):
ethtool = HOST.run_multiline(["ethtool", iplink["ifname"]])
eth = link(ethtool)
eth = link(iplink["ifname"])
if stats := statistics(iplink["ifname"]):
eth["statistics"] = stats
@@ -34,9 +34,6 @@ def iplink2yang_type(iplink):
case _:
return "infix-if-type:other"
if iplink.get("parentbus") == "virtio":
return "infix-if-type:etherlike"
match iplink.get("linkinfo", {}).get("info_kind"):
case "bond":
return "infix-if-type:lag"