Commit 3c6f97e3bf for frr

commit 3c6f97e3bffa3197ef43230550026d12ac0ec5b2
Author: Philippe Guibert <philippe.guibert@6wind.com>
Date:   Thu Nov 26 16:23:00 2026 +0100

    topotests: add bgp_vrf_import_allowas_in test

    Check that a VPN route with the AS of the target VRF in its AS path is
    imported only with "allowas-in vpn import", and only when the AS appears
    at most the given number of times.

      ce2 (AS 65100) --- r2 VRF-A (AS 65300) --> r2 (AS 65300)
                                                      | eBGP VPNv4
                         r1 VRF-B (AS 65100) <-- r1 (AS 65200)

    ce2 advertises 10.0.0.1/32, 10.0.0.3/32 and 10.0.0.4/32, the last two
    with AS path prepends: r1 receives them in VPN with AS 65100 1, 3 and 4
    times. None is imported into VRF-B without "allowas-in vpn import". With
    1, 3 and 2, the routes with at most that many occurrences are imported,
    and lowering the number withdraws the others. Without a number, the
    default of 3 imports 10.0.0.1/32 and 10.0.0.3/32, not 10.0.0.4/32.
    "no allowas-in vpn import" removes the command from the configuration
    and withdraws the routes.

    r1 VRF-C (AS 65100) imports r1 VRF-D (AS 65400), which originates
    10.0.0.5/32 with AS path "65100". The route is imported into VRF-C only
    with "import vrf allowas-in". "allowas-in vpn import" is refused in
    VRF-C, and "import vrf allowas-in" is refused in VRF-B.

    As "import vrf route-map", "import vrf allowas-in" alone in r1 VRF-E
    (AS 65100), without "import vrf VRFNAME", is shown in the configuration
    and makes "import vpn" refused until its "no" form.

    Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>

diff --git a/tests/topotests/bgp_vrf_import_allowas_in/__init__.py b/tests/topotests/bgp_vrf_import_allowas_in/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/topotests/bgp_vrf_import_allowas_in/ce2/frr.conf b/tests/topotests/bgp_vrf_import_allowas_in/ce2/frr.conf
new file mode 100644
index 0000000000..0068dab5a5
--- /dev/null
+++ b/tests/topotests/bgp_vrf_import_allowas_in/ce2/frr.conf
@@ -0,0 +1,25 @@
+!
+interface ce2-eth0
+ ip address 192.168.2.2/24
+exit
+!
+router bgp 65100
+ bgp router-id 192.168.2.2
+ no bgp ebgp-requires-policy
+ no bgp network import-check
+ neighbor 192.168.2.1 remote-as external
+ !
+ address-family ipv4 unicast
+  network 10.0.0.1/32
+  network 10.0.0.3/32 route-map PREPEND_2
+  network 10.0.0.4/32 route-map PREPEND_3
+ exit-address-family
+!
+route-map PREPEND_2 permit 10
+ set as-path prepend 65100 65100
+exit
+!
+route-map PREPEND_3 permit 10
+ set as-path prepend 65100 65100 65100
+exit
+!
diff --git a/tests/topotests/bgp_vrf_import_allowas_in/r1/frr.conf b/tests/topotests/bgp_vrf_import_allowas_in/r1/frr.conf
new file mode 100644
index 0000000000..0639906d09
--- /dev/null
+++ b/tests/topotests/bgp_vrf_import_allowas_in/r1/frr.conf
@@ -0,0 +1,42 @@
+!
+interface r1-eth0
+ ip address 192.168.12.1/24
+ mpls bgp forwarding
+exit
+!
+router bgp 65200
+ bgp router-id 192.168.12.1
+ no bgp ebgp-requires-policy
+ neighbor 192.168.12.2 remote-as external
+ !
+ address-family ipv4 vpn
+  neighbor 192.168.12.2 activate
+ exit-address-family
+!
+router bgp 65100 vrf VRF-B
+ bgp router-id 192.168.12.1
+ !
+ address-family ipv4 unicast
+  rt vpn import 100:1
+  import vpn
+ exit-address-family
+!
+router bgp 65400 vrf VRF-D
+ bgp router-id 192.168.12.1
+ no bgp network import-check
+ !
+ address-family ipv4 unicast
+  network 10.0.0.5/32 route-map PREPEND_1
+ exit-address-family
+!
+router bgp 65100 vrf VRF-C
+ bgp router-id 192.168.12.1
+ !
+ address-family ipv4 unicast
+  import vrf VRF-D
+ exit-address-family
+!
+route-map PREPEND_1 permit 10
+ set as-path prepend 65100
+exit
+!
diff --git a/tests/topotests/bgp_vrf_import_allowas_in/r2/frr.conf b/tests/topotests/bgp_vrf_import_allowas_in/r2/frr.conf
new file mode 100644
index 0000000000..334e46c3e8
--- /dev/null
+++ b/tests/topotests/bgp_vrf_import_allowas_in/r2/frr.conf
@@ -0,0 +1,32 @@
+!
+interface r2-eth0
+ ip address 192.168.12.2/24
+exit
+!
+interface r2-eth1
+ ip address 192.168.2.1/24
+exit
+!
+router bgp 65300
+ bgp router-id 192.168.12.2
+ no bgp ebgp-requires-policy
+ neighbor 192.168.12.1 remote-as external
+ !
+ address-family ipv4 vpn
+  neighbor 192.168.12.1 activate
+ exit-address-family
+!
+router bgp 65300 vrf VRF-A
+ bgp router-id 192.168.2.1
+ no bgp ebgp-requires-policy
+ no bgp network import-check
+ neighbor 192.168.2.2 remote-as external
+ !
+ address-family ipv4 unicast
+  network 10.0.0.2/32
+  label vpn export auto
+  rd vpn export 100:1
+  rt vpn export 100:1
+  export vpn
+ exit-address-family
+!
diff --git a/tests/topotests/bgp_vrf_import_allowas_in/test_bgp_vrf_import_allowas_in.py b/tests/topotests/bgp_vrf_import_allowas_in/test_bgp_vrf_import_allowas_in.py
new file mode 100644
index 0000000000..e11581935f
--- /dev/null
+++ b/tests/topotests/bgp_vrf_import_allowas_in/test_bgp_vrf_import_allowas_in.py
@@ -0,0 +1,264 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: ISC
+
+#
+# Copyright 2026 6WIND
+#
+
+"""
+Check that a VPN route with the AS of the target VRF in its AS path is
+imported only with "allowas-in vpn import", up to the given number of
+occurrences, and that a route imported from another VRF is imported
+only with "import vrf allowas-in". "import vrf allowas-in" keeps the VRF in
+the VRF-to-VRF import mode when the last "import vrf" or the "import vrf
+route-map" is removed.
+
+ce2 (AS 65100) --- r2 VRF-A (AS 65300) --> r2 (AS 65300)
+                                                | eBGP VPNv4
+                   r1 VRF-B (AS 65100) <-- r1 (AS 65200)
+
+ce2 advertises 10.0.0.1/32, 10.0.0.3/32 and 10.0.0.4/32: their AS paths on
+r1 contain the AS of VRF-B 1, 3 and 4 times. r2 VRF-A originates 10.0.0.2/32.
+
+r1 VRF-C (AS 65100) imports r1 VRF-D (AS 65400), which originates
+10.0.0.5/32 with AS path "65100". r1 VRF-E (AS 65100) has no import.
+"""
+
+import os
+import sys
+import json
+import pytest
+
+CWD = os.path.dirname(os.path.realpath(__file__))
+sys.path.append(os.path.join(CWD, "../"))
+
+# pylint: disable=C0413
+from lib import topotest
+from lib.topogen import Topogen, get_topogen
+
+pytestmark = [pytest.mark.bgpd]
+
+CE2_AS_PATHS = {
+    "10.0.0.1/32": "65300 65100",
+    "10.0.0.3/32": "65300 65100 65100 65100",
+    "10.0.0.4/32": "65300 65100 65100 65100 65100",
+}
+
+
+def build_topo(tgen):
+    for rname in ["r1", "r2", "ce2"]:
+        tgen.add_router(rname)
+
+    switch = tgen.add_switch("s1")
+    switch.add_link(tgen.gears["r1"])
+    switch.add_link(tgen.gears["r2"])
+
+    switch = tgen.add_switch("s2")
+    switch.add_link(tgen.gears["r2"])
+    switch.add_link(tgen.gears["ce2"])
+
+
+def setup_module(mod):
+    if not topotest.platform_has_vrf():
+        pytest.skip("platform does not support VRF")
+
+    tgen = Topogen(build_topo, mod.__name__)
+    tgen.start_topology()
+
+    r1 = tgen.gears["r1"]
+    r1.run("ip link add VRF-B type vrf table 1002")
+    r1.run("ip link set VRF-B up")
+    r1.run("ip link add VRF-C type vrf table 1003")
+    r1.run("ip link set VRF-C up")
+    r1.run("ip link add VRF-D type vrf table 1004")
+    r1.run("ip link set VRF-D up")
+    r1.run("ip link add VRF-E type vrf table 1005")
+    r1.run("ip link set VRF-E up")
+
+    r2 = tgen.gears["r2"]
+    r2.run("ip link add VRF-A type vrf table 1001")
+    r2.run("ip link set VRF-A up")
+    r2.run("ip link set r2-eth1 master VRF-A")
+
+    for router in tgen.routers().values():
+        router.load_frr_config()
+
+    tgen.start_router()
+
+
+def teardown_module(mod):
+    tgen = get_topogen()
+    tgen.stop_topology()
+
+
+def check_vpn_route(prefix):
+    def _check_vpn_route():
+        r1 = get_topogen().gears["r1"]
+        output = json.loads(r1.vtysh_cmd("show bgp ipv4 vpn json"))
+        expected = {
+            "routes": {"routeDistinguishers": {"100:1": {prefix: [{"valid": True}]}}}
+        }
+        return topotest.json_cmp(output, expected)
+
+    _, result = topotest.run_and_expect(_check_vpn_route, None, count=30, wait=1)
+    assert result is None, "VPN {}: route not received".format(prefix)
+
+
+def check_vrf_route(vrf, prefix, paths):
+    def _check_vrf_route():
+        r1 = get_topogen().gears["r1"]
+        output = json.loads(
+            r1.vtysh_cmd("show bgp vrf {} ipv4 unicast {} json".format(vrf, prefix))
+        )
+        return topotest.json_cmp(output, {"paths": paths})
+
+    _, result = topotest.run_and_expect(_check_vrf_route, None, count=30, wait=1)
+    assert result is None, "{} {}: unexpected route".format(vrf, prefix)
+
+
+def configure_vrf(vrf, command):
+    r1 = get_topogen().gears["r1"]
+    return r1.vtysh_cmd(
+        """
+        configure
+        router bgp 65100 vrf {}
+         address-family ipv4 unicast
+          {}
+        """.format(
+            vrf, command
+        )
+    )
+
+
+def check_running_config(line, present=True):
+    output = get_topogen().gears["r1"].vtysh_cmd("show running-config bgpd")
+    lines = [config_line.strip() for config_line in output.splitlines()]
+    assert (line in lines) == present, output
+
+
+def test_bgp_vrf_import_loop_blocked():
+    tgen = get_topogen()
+    if tgen.routers_have_failure():
+        pytest.skip(tgen.errors)
+
+    for prefix in CE2_AS_PATHS:
+        check_vpn_route(prefix)
+        check_vrf_route("VRF-B", prefix, None)
+    check_vrf_route("VRF-B", "10.0.0.2/32", [{"valid": True}])
+    check_vrf_route("VRF-C", "10.0.0.5/32", None)
+
+
+def test_bgp_vrf_import_allowas_in():
+    tgen = get_topogen()
+    if tgen.routers_have_failure():
+        pytest.skip(tgen.errors)
+
+    def check_vrf_b_imports(imported):
+        for prefix, aspath in CE2_AS_PATHS.items():
+            if prefix in imported:
+                paths = [{"aspath": {"string": aspath}, "valid": True}]
+            else:
+                paths = None
+            check_vrf_route("VRF-B", prefix, paths)
+
+    try:
+        configure_vrf("VRF-B", "allowas-in vpn import 1")
+        check_running_config("allowas-in vpn import 1")
+        check_vrf_b_imports(["10.0.0.1/32"])
+
+        configure_vrf("VRF-B", "allowas-in vpn import 3")
+        check_running_config("allowas-in vpn import")
+        check_vrf_b_imports(["10.0.0.1/32", "10.0.0.3/32"])
+
+        configure_vrf("VRF-B", "allowas-in vpn import 2")
+        check_running_config("allowas-in vpn import 2")
+        check_vrf_b_imports(["10.0.0.1/32"])
+
+        # 3 occurrences by default
+        configure_vrf("VRF-B", "allowas-in vpn import")
+        check_running_config("allowas-in vpn import")
+        check_running_config("allowas-in vpn import 2", present=False)
+        check_vrf_b_imports(["10.0.0.1/32", "10.0.0.3/32"])
+
+        configure_vrf("VRF-B", "allowas-in vpn import 1")
+        configure_vrf("VRF-B", "no allowas-in vpn import")
+        check_running_config("allowas-in vpn import", present=False)
+        check_running_config("allowas-in vpn import 1", present=False)
+        check_vrf_b_imports([])
+    finally:
+        configure_vrf("VRF-B", "no allowas-in vpn import")
+
+    check_vrf_b_imports([])
+    check_vrf_route("VRF-B", "10.0.0.2/32", [{"valid": True}])
+
+
+def test_bgp_vrf_import_vrf_allowas_in():
+    tgen = get_topogen()
+    if tgen.routers_have_failure():
+        pytest.skip(tgen.errors)
+
+    output = configure_vrf("VRF-B", "import vrf allowas-in")
+    assert "Please unconfigure vpn to vrf commands" in output, output
+    output = configure_vrf("VRF-C", "allowas-in vpn import")
+    assert "Please unconfigure import vrf commands" in output, output
+    check_running_config("import vrf allowas-in", present=False)
+    check_running_config("allowas-in vpn import", present=False)
+
+    try:
+        configure_vrf("VRF-C", "import vrf allowas-in")
+        check_running_config("import vrf allowas-in")
+        check_vrf_route("VRF-C", "10.0.0.5/32", [{"valid": True}])
+
+        configure_vrf("VRF-C", "import vrf allowas-in 1")
+        check_running_config("import vrf allowas-in 1")
+        check_vrf_route("VRF-C", "10.0.0.5/32", [{"valid": True}])
+
+        configure_vrf("VRF-C", "no import vrf VRF-D")
+        check_running_config("import vrf allowas-in 1")
+        check_running_config("allowas-in vpn import 1", present=False)
+        output = configure_vrf("VRF-C", "import vpn")
+        assert "Please unconfigure import vrf commands" in output, output
+
+        configure_vrf("VRF-C", "import vrf VRF-D")
+        check_vrf_route("VRF-C", "10.0.0.5/32", [{"valid": True}])
+
+        configure_vrf("VRF-C", "no import vrf allowas-in")
+        check_running_config("import vrf allowas-in", present=False)
+        check_running_config("import vrf allowas-in 1", present=False)
+        check_vrf_route("VRF-C", "10.0.0.5/32", None)
+    finally:
+        configure_vrf("VRF-C", "import vrf VRF-D")
+        configure_vrf("VRF-C", "no import vrf allowas-in")
+
+
+def test_bgp_vrf_import_vrf_allowas_in_without_import_vrf():
+    tgen = get_topogen()
+    if tgen.routers_have_failure():
+        pytest.skip(tgen.errors)
+
+    try:
+        configure_vrf("VRF-E", "import vrf allowas-in 2")
+        check_running_config("import vrf allowas-in 2")
+        output = configure_vrf("VRF-E", "import vpn")
+        assert "Please unconfigure import vrf commands" in output, output
+
+        configure_vrf("VRF-E", "import vrf route-map VRF_E_IMPORT")
+        configure_vrf("VRF-E", "no import vrf route-map")
+        check_running_config("import vrf allowas-in 2")
+        check_running_config("allowas-in vpn import 2", present=False)
+        output = configure_vrf("VRF-E", "import vpn")
+        assert "Please unconfigure import vrf commands" in output, output
+
+        configure_vrf("VRF-E", "no import vrf allowas-in")
+        check_running_config("import vrf allowas-in 2", present=False)
+        output = configure_vrf("VRF-E", "import vpn")
+        assert "Please unconfigure" not in output, output
+    finally:
+        configure_vrf("VRF-E", "no import vrf route-map")
+        configure_vrf("VRF-E", "no import vrf allowas-in")
+        configure_vrf("VRF-E", "no import vpn")
+
+
+if __name__ == "__main__":
+    args = ["-s"] + sys.argv[1:]
+    sys.exit(pytest.main(args))