From dc92db4e9faafa8a7488452cacd1dcf7e51f830a Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 1 Nov 2024 13:50:26 +0100 Subject: [PATCH] test: netns: Add Two-Port MAC Relay (TPMR) This is a specialized namespace, containing two ports, which (hopefully) acts as completely transparent "bump-on-the-wire" bridge. Useful in scenarios where you want to test what happens when the flow of packets between two nodes is disrupted. E.g., for testing fail-over behavior in dynamic routing protocols, L2 redundancy, VRRP etc. --- test/infamy/netns.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/infamy/netns.py b/test/infamy/netns.py index a6e97756..59a1f6af 100644 --- a/test/infamy/netns.py +++ b/test/infamy/netns.py @@ -360,3 +360,39 @@ class Pcap: stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=True) return tcpdump.stdout + +class TPMR(IsolatedMacVlans): + def __init__(self, a, b): + super().__init__(ifmap={ a: "a", b: "b" }, lo=False) + + def start(self, forward=True): + ret = super().start() + + for dev in ("a", "b"): + self.run(f"ip link set dev {dev} promisc on up".split()) + self.run(f"tc qdisc add dev {dev} clsact".split()) + + if forward: + self.forward() + + return ret + + def _clear_ingress(self, iface): + return self.run(f"tc filter del dev {iface} ingress".split()) + + def _add_redir(self, frm, to): + cmd = \ + "tc filter add dev".split() \ + + [frm] \ + + "ingress matchall action mirred egress redirect dev".split() \ + + [to] + return self.run(cmd) + + def forward(self): + for iface in ("a", "b"): + self._clear_ingress(iface) + self._add_redir(iface, "a" if iface == "b" else "b") + + def block(self): + for iface in ("a", "b"): + self._clear_ingress(iface)