fix(routes): read route types and multipath next hops in ip route
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m16s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / test (3.13) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 36s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 2m57s
driver / publish (pull_request) Skipped

get_route_to took the first word of every "ip route show" line as the
destination. For unreachable, blackhole, prohibit and throw routes that
word is the route type, so they came back as "unreachable/32" and the
like; a multipath route's continuation lines became "nexthop/32"
routes, and the route itself had no next hop.

The type word is now read as the type and reported in
protocol_attributes, without a next hop or outgoing interface for the
types that forward nothing (IPv6 names lo for them). nexthop lines
attach to the route before them, one entry per next hop. The test
fixture is ip's output (iproute2 6.1.0) recorded in a network
namespace set up with these routes.

Closes #57
This commit is contained in:
Christian Manivong committed 2026-11-02 08:41:56 +01:00
1 parent d3134c83b8
commit e4c65945b9
4 files changed
+205 -20

No files matched your search

+56 -12
View File
@@ -12,6 +12,29 @@ IP_ROUTE_COMMANDS = {"ipv4": "ip -4 route show", "ipv6": "ip -6 route show"}
_DEFAULT = {"ipv4": "0.0.0.0/0", "ipv6": "::/0"}
_HOST_PREFIX = {"ipv4": "/32", "ipv6": "/128"}
#: The route types ``ip route`` prints in front of the destination; a unicast route
#: carries none (#57).
_ROUTE_TYPES = frozenset(
{
"unicast",
"local",
"broadcast",
"anycast",
"multicast",
"blackhole",
"unreachable",
"prohibit",
"throw",
"nat",
"xresolve",
}
)
#: Route types that send nothing out of an interface: no next hop, and no outgoing
#: interface either, though IPv6 names ``lo`` for them.
_NO_FORWARDING = frozenset({"blackhole", "unreachable", "prohibit", "throw"})
#: The line a multipath route's next hops start with, one each, after the route.
_NEXTHOP = "nexthop"
_VIA = re.compile(r"via\s+(\S+)")
_DEV = re.compile(r"dev\s+(\S+)")
_PROTO = re.compile(r"proto\s+(\S+)")
@@ -19,32 +42,53 @@ _METRIC = re.compile(r"metric\s+(\d+)")
class IpRoute(NamedTuple):
"""One line of ``ip route show``."""
"""One route of ``ip route show``; a multipath route is one per next hop."""
network: str
proto: str
nexthop: str
iface: str
metric: int
route_type: str = "unicast"
def parse_ip_route(out: str, family: str) -> Iterator[IpRoute]:
"""The routes in *out*, the output of ``IP_ROUTE_COMMANDS[family]``."""
route = ""
hops: list[str] = []
for line in out.splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
dest = line.split(None, 1)[0]
network = _DEFAULT[family] if dest == "default" else dest
if "/" not in network:
network += _HOST_PREFIX[family]
yield IpRoute(
network,
_first(_PROTO, line) or "kernel",
_first(_VIA, line),
_first(_DEV, line),
int(_first(_METRIC, line) or 0),
)
if line.split(None, 1)[0] == _NEXTHOP:
hops.append(line)
continue
if route:
yield from _routes(route, hops, family)
route, hops = line, []
if route:
yield from _routes(route, hops, family)
def route_attributes(route_type: str) -> dict[str, str]:
"""NAPALM's ``protocol_attributes`` for a route of *route_type*: its type, unless unicast."""
return {} if route_type == "unicast" else {"type": route_type}
def _routes(line: str, hops: list[str], family: str) -> Iterator[IpRoute]:
"""The route *line* with its multipath next-hop lines *hops*: one per next hop."""
words = line.split()
route_type = words.pop(0) if words[0] in _ROUTE_TYPES and len(words) > 1 else "unicast"
network = _DEFAULT[family] if words[0] == "default" else words[0]
if "/" not in network:
network += _HOST_PREFIX[family]
proto = _first(_PROTO, line) or "kernel"
metric = int(_first(_METRIC, line) or 0)
if route_type in _NO_FORWARDING:
yield IpRoute(network, proto, "", "", metric, route_type)
return
for hop in hops or [line]:
yield IpRoute(network, proto, _first(_VIA, hop), _first(_DEV, hop), metric, route_type)
def _first(pattern: re.Pattern[str], line: str) -> str:
+8 -8
View File
@@ -75,7 +75,7 @@ from napalm_linux._dpkg import DPKG_QUERY_COMMAND, parse_dpkg_query
from napalm_linux._hostname import HostnameMixin
from napalm_linux._local_accounts import LinuxLocalAccountsMixin
from napalm_linux._ping import parse_ping, ping_command
from napalm_linux._routes import IP_ROUTE_COMMANDS, parse_ip_route
from napalm_linux._routes import IP_ROUTE_COMMANDS, IpRoute, parse_ip_route, route_attributes
from napalm_linux._shell import expect_for, prepare_shell, sudo_without_password
from napalm_linux._sudo_history import LinuxSudoHistoryMixin
@@ -948,19 +948,19 @@ class LinuxDriver(
"protocol_attributes": {},
}
def _add(network: str, proto: str, nexthop: str, iface: str, metric: int) -> None:
if destination and network != destination:
def _add(route: IpRoute) -> None:
if destination and route.network != destination:
return
mapped = proto_map.get(proto, proto)
mapped = proto_map.get(route.proto, route.proto)
if protocol and mapped != protocol.lower():
return
routes.setdefault(network, []).append(
_make_entry(mapped, nexthop, iface, metric, network)
)
entry = _make_entry(mapped, route.nexthop, route.iface, route.metric, route.network)
entry["protocol_attributes"] = route_attributes(route.route_type)
routes.setdefault(route.network, []).append(entry)
for family, command in IP_ROUTE_COMMANDS.items():
for route in parse_ip_route(self._send(command), family):
_add(*route)
_add(route)
# FRR/Zebra enrichment via vtysh — properly attributes OSPF/BGP/RIP protocols.
# FRR installs routes into the kernel as "proto zebra"; vtysh gives the real source.