Skip to content

Commit

Permalink
test: netns: Add Two-Port MAC Relay (TPMR)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wkz committed Nov 4, 2024
1 parent f6b24d9 commit 32dd354
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/infamy/netns.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,39 @@ def tcpdump(self, args=""):
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)

0 comments on commit 32dd354

Please sign in to comment.