Commit 495aa5f6c1 for frr

commit 495aa5f6c10e049e24f1f787eaba7a85df53d16a
Author: Nageswara Soma <nsoma@cisco.com>
Date:   Sun Nov 15 19:54:39 2026 -0800

    tests: cover OSPFv3 MaxAge restart and replay

    Add four routers across areas 1, 0 and 2 using unified
    frr.conf files. Redistribute two static IPv6 routes
    so withdrawing one preserves the ASBR role.

    Stop and restart the ASBR with graceful restart
    disabled, both immediately and after two seconds.
    Require instances newer than peers' retained MaxAge
    copies, comparing sequence and checksum by LSA ID
    because cold restarts can change prefix-to-ID mapping.

    Drain older MaxAge-removal timers before each case.
    Reuse poll results for diagnostics and explicitly
    reject collection of the retained copies during
    restart instead of relying on a 55-second budget.

    Replay a valid pre-withdrawal external LSA and verify
    receipt in the receiver log. Read router ID and area
    from running state. Filter originator output briefly,
    then require the route to remain absent on every
    peer and the receiver to retain MaxAge. Restore the
    filter and static route in a finally block and
    verify recovery after fresh origination.

    Keep protocol filtering in common_config so kernel
    commands and platform handling stay in the library.

    Signed-off-by: Nageswara Soma <nsoma@cisco.com>

diff --git a/tests/topotests/lib/common_config.py b/tests/topotests/lib/common_config.py
index 8fd7f02bee..0c9f71db56 100644
--- a/tests/topotests/lib/common_config.py
+++ b/tests/topotests/lib/common_config.py
@@ -21,6 +21,7 @@ from functools import wraps
 from re import search as re_search
 from time import sleep

+import pytest

 from lib.micronet import comm_error
 from lib.topogen import TopoRouter, get_topogen
@@ -1812,6 +1813,16 @@ def delete_address_on_interface_freebsd(
         logger.debug("[DUT: %s]: %s stays in the base FIB (vrf %s)", dut, name, vrf)


+def set_protocol_output_filter(router, protocol, enable, family):
+    """Add/remove an outgoing-protocol DROP rule; callers must restore it."""
+    if not topotest.is_linux():
+        pytest.skip("Output protocol filtering is implemented only on Linux")
+    tool = {"ipv4": "iptables", "ipv6": "ip6tables"}[family]
+    action = "-I" if enable else "-D"
+    rule = ["OUTPUT", "-p", str(protocol), "-j", "DROP"]
+    router.cmd_raises([tool, "-w", "5", action, *rule])
+
+
 def shutdown_bringup_interface_in_kernel(tgen, dut, intf_name, ifaceaction=False):
     """
     Shut down or bring up a kernel interface.
diff --git a/tests/topotests/ospf6_maxage_recovery/__init__.py b/tests/topotests/ospf6_maxage_recovery/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/topotests/ospf6_maxage_recovery/r1/frr.conf b/tests/topotests/ospf6_maxage_recovery/r1/frr.conf
new file mode 100644
index 0000000000..ea7fa8b7f7
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/r1/frr.conf
@@ -0,0 +1,22 @@
+frr defaults traditional
+hostname r1
+ipv6 forwarding
+debug ospf6 flooding
+debug ospf6 lsa as-external
+!
+ipv6 route 2001:db8:100::/64 Null0
+ipv6 route 2001:db8:101::/64 Null0
+!
+interface eth-r2
+ ipv6 address 2001:db8:12::1/64
+ ipv6 ospf6 area 0.0.0.1
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+router ospf6
+ ospf6 router-id 1.1.1.1
+ redistribute static metric 20
+exit
+!
diff --git a/tests/topotests/ospf6_maxage_recovery/r2/frr.conf b/tests/topotests/ospf6_maxage_recovery/r2/frr.conf
new file mode 100644
index 0000000000..fa8d867a96
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/r2/frr.conf
@@ -0,0 +1,26 @@
+frr defaults traditional
+hostname r2
+ipv6 forwarding
+debug ospf6 flooding
+debug ospf6 lsa as-external
+!
+interface eth-r1
+ ipv6 address 2001:db8:12::2/64
+ ipv6 ospf6 area 0.0.0.1
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+interface eth-r3
+ ipv6 address 2001:db8:23::2/64
+ ipv6 ospf6 area 0.0.0.0
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+router ospf6
+ ospf6 router-id 2.2.2.2
+exit
+!
diff --git a/tests/topotests/ospf6_maxage_recovery/r3/frr.conf b/tests/topotests/ospf6_maxage_recovery/r3/frr.conf
new file mode 100644
index 0000000000..b85a0c6432
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/r3/frr.conf
@@ -0,0 +1,26 @@
+frr defaults traditional
+hostname r3
+ipv6 forwarding
+debug ospf6 flooding
+debug ospf6 lsa as-external
+!
+interface eth-r2
+ ipv6 address 2001:db8:23::3/64
+ ipv6 ospf6 area 0.0.0.0
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+interface eth-r4
+ ipv6 address 2001:db8:34::3/64
+ ipv6 ospf6 area 0.0.0.2
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+router ospf6
+ ospf6 router-id 3.3.3.3
+exit
+!
diff --git a/tests/topotests/ospf6_maxage_recovery/r4/frr.conf b/tests/topotests/ospf6_maxage_recovery/r4/frr.conf
new file mode 100644
index 0000000000..6b42cbeda4
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/r4/frr.conf
@@ -0,0 +1,18 @@
+frr defaults traditional
+hostname r4
+ipv6 forwarding
+debug ospf6 flooding
+debug ospf6 lsa as-external
+!
+interface eth-r3
+ ipv6 address 2001:db8:34::4/64
+ ipv6 ospf6 area 0.0.0.2
+ ipv6 ospf6 network point-to-point
+ ipv6 ospf6 hello-interval 1
+ ipv6 ospf6 dead-interval 40
+exit
+!
+router ospf6
+ ospf6 router-id 4.4.4.4
+exit
+!
diff --git a/tests/topotests/ospf6_maxage_recovery/replay_lsa.py b/tests/topotests/ospf6_maxage_recovery/replay_lsa.py
new file mode 100644
index 0000000000..e95b3f0ef3
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/replay_lsa.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: ISC
+"""Replay one captured OSPFv3 LSA from an established neighbor."""
+
+import sys
+
+from scapy.all import Ether, IPv6, get_if_hwaddr, sendp, wrpcap
+from scapy.contrib.ospf import OSPFv3_Hdr, OSPFv3_LSA_Hdr, OSPFv3_LSUpd
+
+iface, address, router_id, area_id, lsa, capture = sys.argv[1:]
+packet = (
+    Ether(src=get_if_hwaddr(iface), dst="33:33:00:00:00:05")
+    / IPv6(src=address, dst="ff02::5", hlim=1)
+    / OSPFv3_Hdr(src=router_id, area=area_id)
+    / OSPFv3_LSUpd(lsalist=[OSPFv3_LSA_Hdr(bytes.fromhex(lsa))])
+)
+wrpcap(capture, packet)
+sendp(packet, iface=iface, verbose=False)
diff --git a/tests/topotests/ospf6_maxage_recovery/test_ospf6_maxage_recovery.py b/tests/topotests/ospf6_maxage_recovery/test_ospf6_maxage_recovery.py
new file mode 100644
index 0000000000..d67ed19595
--- /dev/null
+++ b/tests/topotests/ospf6_maxage_recovery/test_ospf6_maxage_recovery.py
@@ -0,0 +1,377 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: ISC
+
+"""Exercise MaxAge external LSAs during restart and delayed flooding.
+
+An ASBR r1 is connected through areas 1, 0 and 2 to r4:
+    r1 -- area 1 -- r2 -- area 0 -- r3 -- area 2 -- r4
+The ASBR redistributes two static routes so withdrawing one does not
+change its ASBR status. Graceful restart is not enabled.
+"""
+
+import json
+import os
+import re
+import struct
+import sys
+import time
+from pathlib import Path
+
+import pytest
+
+CWD = os.path.dirname(os.path.realpath(__file__))
+sys.path.append(os.path.join(CWD, ".."))
+
+from lib import topotest
+from lib.common_config import get_frr_ipv6_linklocal, set_protocol_output_filter
+from lib.topogen import Topogen, get_topogen
+from lib.topolog import logger
+
+pytestmark = [pytest.mark.ospf6d]
+PREFIX = "2001:db8:100::/64"
+KEEP_PREFIX = "2001:db8:101::/64"
+REMOTE_ROUTERS = ("r2", "r3", "r4")
+
+
+def build_topo(tgen):
+    for number in range(1, 5):
+        tgen.add_router(f"r{number}")
+    for left in range(1, 4):
+        switch = tgen.add_switch(f"s{left}")
+        switch.add_link(tgen.gears[f"r{left}"], nodeif=f"eth-r{left + 1}")
+        switch.add_link(tgen.gears[f"r{left + 1}"], nodeif=f"eth-r{left}")
+
+
+def setup_module(mod):
+    tgen = Topogen(build_topo, mod.__name__)
+    tgen.start_topology()
+    for name, router in tgen.routers().items():
+        router.load_frr_config(os.path.join(CWD, name, "frr.conf"))
+    tgen.start_router()
+
+
+def teardown_module():
+    get_topogen().stop_topology()
+
+
+@pytest.fixture(autouse=True)
+def check_router_status():
+    tgen = get_topogen()
+    if tgen.routers_have_failure():
+        pytest.skip(tgen.errors)
+
+
+def wait_for(check, description, count=30, wait=1):
+    _, result = topotest.run_and_expect(check, True, count=count, wait=wait)
+    assert result is True, description
+
+
+def neighbors_full():
+    tgen = get_topogen()
+    for name, expected in (("r1", 1), ("r2", 2), ("r3", 2), ("r4", 1)):
+        neighbors = (
+            tgen.gears[name]
+            .vtysh_cmd("show ipv6 ospf6 neighbor json", isjson=True)
+            .get("neighbors", [])
+        )
+        if len(neighbors) != expected or any(n["state"] != "Full" for n in neighbors):
+            return False
+    return True
+
+
+def routes_present(prefix=PREFIX, present=True):
+    for name in REMOTE_ROUTERS:
+        routes = (
+            get_topogen()
+            .gears[name]
+            .vtysh_cmd("show ipv6 route ospf json", isjson=True)
+        )
+        if bool(routes.get(prefix)) != present:
+            return False
+    return True
+
+
+def external_lsa(name, prefix=PREFIX, output=None):
+    if output is None:
+        output = (
+            get_topogen()
+            .gears[name]
+            .vtysh_cmd("show ipv6 ospf6 database as-external detail")
+        )
+    for block in re.split(r"(?=Age:)", output):
+        if not re.search(rf"Prefix: {re.escape(prefix)}\s", block):
+            continue
+        return {
+            "age": int(re.search(r"Age:\s*(\d+)", block).group(1)),
+            "id": re.search(r"Link State ID: (\S+)", block).group(1),
+            "advertiser": re.search(r"Advertising Router: (\S+)", block).group(1),
+            "sequence": int(
+                re.search(r"LS Sequence Number: (0x[\da-fA-F]+)", block).group(1), 16
+            ),
+            "checksum": int(
+                re.search(r"CheckSum: (0x[\da-fA-F]+)", block).group(1), 16
+            ),
+        }
+    return None
+
+
+def newer_than_maxage(lsa, old):
+    # At equal sequence and checksum, a live instance cannot beat MaxAge.
+    if lsa["sequence"] == old["sequence"]:
+        return lsa["checksum"] > old["checksum"]
+    return (
+        struct.unpack("!i", struct.pack("!I", lsa["sequence"]))[0]
+        > struct.unpack("!i", struct.pack("!I", old["sequence"]))[0]
+    )
+
+
+def snapshot(label, state=None):
+    if state is None:
+        state = {}
+        for name in ("r1",) + REMOTE_ROUTERS:
+            router = get_topogen().gears[name]
+            database = router.vtysh_cmd("show ipv6 ospf6 database as-external detail")
+            state[name] = {
+                "lsa": external_lsa(name, output=database),
+                "keep_lsa": external_lsa(name, KEEP_PREFIX, output=database),
+                "routes": router.vtysh_cmd("show ipv6 route ospf json", isjson=True),
+                "database": database,
+            }
+    Path(get_topogen().logdir, f"{label}.json").write_text(json.dumps(state, indent=2))
+
+
+def converge():
+    wait_for(neighbors_full, "OSPFv3 adjacencies did not become Full", count=120)
+    wait_for(routes_present, "External prefix did not reach every area")
+    wait_for(lambda: routes_present(KEEP_PREFIX), "Second external prefix is missing")
+
+
+def wait_maxage_removers_idle():
+    def removers_idle():
+        for name in REMOTE_ROUTERS:
+            timers = (
+                get_topogen()
+                .gears[name]
+                .vtysh_cmd("show event timers", daemon="ospf6d", raises=True)
+            )
+            assert "Showing timers for" in timers, timers
+            if "ospf6_maxage_remover" in timers:
+                return False
+        return True
+
+    # An older, process-wide timer can collect a new withdrawal before 60s.
+    # Drain it before the stop instead of assuming a fresh removal interval.
+    wait_for(
+        removers_idle, "An earlier MaxAge-removal timer is still pending", count=120
+    )
+
+
+def test_initial_convergence():
+    converge()
+
+
+@pytest.mark.parametrize("restart_delay", [0, 2])
+def test_normal_restart_with_retained_maxage(restart_delay):
+    """A normal FRR stop/start must recover with newer LSA instances."""
+    converge()
+    tgen = get_topogen()
+    r1 = tgen.gears["r1"]
+    # Keep the shutdown flush outside MinLSArrival after a preceding restart.
+    time.sleep(1.2)
+    snapshot(f"restart-{restart_delay}-before-stop")
+
+    wait_maxage_removers_idle()
+    receive_log = Path(tgen.logdir, "r2", "ospf6d.log")
+    receive_offset = receive_log.stat().st_size
+    stopped_state = {}
+
+    def withdrawn():
+        stopped_state.clear()
+        for name in REMOTE_ROUTERS:
+            router = tgen.gears[name]
+            database = router.vtysh_cmd("show ipv6 ospf6 database as-external detail")
+            lsa = external_lsa(name, output=database)
+            keep_lsa = external_lsa(name, KEEP_PREFIX, output=database)
+            routes = router.vtysh_cmd("show ipv6 route ospf json", isjson=True)
+            stopped_state[name] = {
+                "lsa": lsa,
+                "keep_lsa": keep_lsa,
+                "routes": routes,
+                "database": database,
+            }
+            if (
+                lsa is None
+                or keep_lsa is None
+                or lsa["age"] != 3600
+                or keep_lsa["age"] != 3600
+                or routes.get(PREFIX)
+                or routes.get(KEEP_PREFIX)
+            ):
+                return False
+        return True
+
+    stopped = time.monotonic()
+    assert r1.stop() == ""
+    try:
+        wait_for(withdrawn, "Peers did not retain the withdrawn MaxAge external LSAs")
+        # A cold restart can assign the two prefixes different LSA IDs.
+        # Compare instances by LSA identity, independently of prefix order.
+        retained = {
+            lsa["id"]: lsa
+            for lsa in (stopped_state["r2"]["lsa"], stopped_state["r2"]["keep_lsa"])
+        }
+        # Reuse the successful poll; live diagnostic commands delay restart.
+        snapshot(f"restart-{restart_delay}-stopped", state=stopped_state)
+        # Exercise immediate and delayed restart while peers retain MaxAge.
+        time.sleep(restart_delay)
+    finally:
+        assert r1.start() == ""
+
+    identifiers = "|".join(re.escape(lsa_id) for lsa_id in retained)
+    advertiser = re.escape(next(iter(retained.values()))["advertiser"])
+    collected = re.compile(
+        rf"Remove MaxAge \[AS-External Id:(?:{identifiers}) Adv:{advertiser}\]$",
+        re.MULTILINE,
+    )
+
+    def assert_retained():
+        # A skipped removal logs a retrans_count suffix; match real removals only.
+        assert (
+            collected.search(receive_log.read_text()[receive_offset:]) is None
+        ), "Retained MaxAge copies were garbage-collected during restart"
+
+    def recovered():
+        assert_retained()
+        if not neighbors_full():
+            return False
+        for name in ("r1",) + REMOTE_ROUTERS:
+            router = tgen.gears[name]
+            database = router.vtysh_cmd("show ipv6 ospf6 database as-external detail")
+            for prefix in (PREFIX, KEEP_PREFIX):
+                lsa = external_lsa(name, prefix, output=database)
+                if (
+                    lsa is None
+                    or lsa["age"] == 3600
+                    or lsa["id"] not in retained
+                    or not newer_than_maxage(lsa, retained[lsa["id"]])
+                ):
+                    return False
+            if name in REMOTE_ROUTERS:
+                routes = router.vtysh_cmd("show ipv6 route ospf json", isjson=True)
+                if not routes.get(PREFIX) or not routes.get(KEEP_PREFIX):
+                    return False
+        return True
+
+    wait_for(
+        recovered, "Normal restart did not recover above retained MaxAge", count=120
+    )
+    assert_retained()
+    elapsed = time.monotonic() - stopped
+    logger.info(
+        "Normal restart recovered in %.3fs above retained instances %s",
+        elapsed,
+        {
+            key: (hex(value["sequence"]), hex(value["checksum"]))
+            for key, value in retained.items()
+        },
+    )
+    snapshot(f"restart-{restart_delay}-recovered")
+
+
+def test_stale_external_lsa_after_withdrawal():
+    """Replaying a valid pre-withdrawal LSA must not restore its route."""
+    converge()
+    tgen = get_topogen()
+    r1 = tgen.gears["r1"]
+    original = external_lsa("r1")
+    router_id = original["advertiser"]
+    area_id = r1.vtysh_cmd("show ipv6 ospf6 interface eth-r2 json", isjson=True)[
+        "eth-r2"
+    ]["areaId"]
+    dump = r1.vtysh_cmd("show ipv6 ospf6 database as-external dump")
+    match = re.search(
+        rf"\[[^\]\n]*Id:{re.escape(original['id'])} Adv:{re.escape(router_id)}\]:\s*"
+        r"((?:[0-9a-fA-F]{8}[ \n]*)+)",
+        dump,
+    )
+    assert match, dump
+    raw_lsa = "".join(match.group(1).split())
+    linklocal = get_frr_ipv6_linklocal(tgen, "r1", intf="eth-r2")
+    wait_maxage_removers_idle()
+    time.sleep(1.2)  # Let MinLSArrival elapse before the legitimate withdrawal.
+    r1.vtysh_cmd(f"configure terminal\nno ipv6 route {PREFIX} Null0")
+    wait_for(lambda: routes_present(present=False), "Prefix was not withdrawn")
+    wait_for(
+        lambda: all(
+            (lsa := external_lsa(name)) is not None
+            and lsa["age"] == 3600
+            and lsa["sequence"] == original["sequence"]
+            and lsa["checksum"] == original["checksum"]
+            for name in REMOTE_ROUTERS
+        ),
+        "Withdrawal did not retain the original LSA at MaxAge",
+    )
+    snapshot("stale-before-replay")
+    # Delay the originator's repairs while replaying through its established
+    # adjacency. The 40s dead interval keeps this short filter from tearing
+    # down the neighbor relationship; sendp bypasses the OUTPUT filter.
+    receive_log = Path(tgen.logdir, "r2", "ospf6d.log")
+    receive_offset = receive_log.stat().st_size
+    replay_age = struct.unpack("!H", bytes.fromhex(raw_lsa)[:2])[0]
+    set_protocol_output_filter(r1, 89, True, "ipv6")
+    try:
+        # Keep the replay outside MinLSArrival after the MaxAge withdrawal.
+        time.sleep(1.2)
+        r1.cmd_raises(
+            [
+                "python3",
+                os.path.join(CWD, "replay_lsa.py"),
+                "eth-r2",
+                linklocal,
+                router_id,
+                area_id,
+                raw_lsa,
+                os.path.join(tgen.logdir, "replayed-lsa.pcap"),
+            ]
+        )
+        wait_for(
+            lambda: re.search(
+                rf"\[AS-External Id:{re.escape(original['id'])} Adv:{re.escape(router_id)}\]\n"
+                rf"[^\n]*Age:\s*{replay_age} SeqNum: 0x{original['sequence']:08x}",
+                receive_log.read_text()[receive_offset:],
+            )
+            is not None,
+            "Receiver did not process the replayed external LSA",
+            count=15,
+            wait=1,
+        )
+        state = {}
+        for _ in range(5):
+            for name in REMOTE_ROUTERS:
+                routes = tgen.gears[name].vtysh_cmd(
+                    "show ipv6 route ospf json", isjson=True
+                )
+                assert not routes.get(PREFIX), "Stale LSA restored a withdrawn route"
+                state[name] = {"routes": routes}
+            database = tgen.gears["r2"].vtysh_cmd(
+                "show ipv6 ospf6 database as-external detail"
+            )
+            lsa = external_lsa("r2", output=database)
+            assert lsa is not None and lsa["age"] == 3600, "Stale LSA replaced MaxAge"
+            state["r2"].update(
+                {
+                    "lsa": lsa,
+                    "keep_lsa": external_lsa("r2", KEEP_PREFIX, output=database),
+                    "database": database,
+                }
+            )
+            # Sample for one second to catch delayed stale-route installation.
+            time.sleep(0.2)
+        # The blocked peer cannot regain Full while the OUTPUT filter is held.
+        assert neighbors_full(), "Replay lost an established adjacency"
+        snapshot("stale-after-replay", state=state)
+    finally:
+        set_protocol_output_filter(r1, 89, False, "ipv6")
+        r1.vtysh_cmd(f"configure terminal\nipv6 route {PREFIX} Null0")
+    wait_for(routes_present, "A fresh origination failed to restore the route")
+    snapshot("stale-fresh-origination")