From: Petr Machata <petrm@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>, <netdev@vger.kernel.org>
Cc: Andrew Lunn <andrew+netdev@lunn.ch>,
Nikolay Aleksandrov <razor@blackwall.org>,
<bridge@lists.linux.dev>, Ido Schimmel <idosch@nvidia.com>,
Petr Machata <petrm@nvidia.com>, Andy Roulin <aroulin@nvidia.com>,
<mlxsw@nvidia.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Shuah Khan <shuah@kernel.org>, <linux-kselftest@vger.kernel.org>
Subject: [PATCH net-next v3 13/13] selftests: net: Add a test for port ID in FDB notifications
Date: Fri, 4 Dec 2026 13:43:56 +0100 [thread overview]
Message-ID: <51ef4a33de413d02ec15fb9cb5cd115a5002b0e2.1796387296.git.petrm@nvidia.com> (raw)
In-Reply-To: <cover.1796387296.git.petrm@nvidia.com>
Add a test that checks that RTM_NEWNEIGH and RTM_DELNEIGH notifications
for FDB entries carry the netlink port ID of the requester in nlmsg_pid.
A listener socket subscribed to the neighbour group receives the
notifications, while two separate requester sockets, A and B, make the
changes. Entries are added through A, replaced through B and deleted
through A, and the port ID seen by the listener is compared to that of the
socket that made the particular change. The following cases are covered:
- bridge: a bridge port (NTF_MASTER) and the bridge device itself
(NTF_SELF), including a bridge with VLAN filtering, where one request
results in several notifications
- VXLAN, including replacing the remote
- macvlan, which does not send notifications by itself and thus exercises
the generic fallback in rtnetlink
- bulk delete (flush) of bridge and VXLAN entries
- bridge entries learned from traffic, which are not a result of a request
and should carry a port ID of zero
The test needs CONFIG_PACKET to send the frame that is learned.
fdb_notify.sh already covers some of the same device setups (bridge, VXLAN,
macvlan), but it checks for duplicate notifications and cannot check the
port ID, as the broadly available tools do not show nlmsg_pid. One option
would be to capture bridge monitor run with strace and parse out key tells.
But that felt like too much of an ugly hack. Another option considered was
netlink capture (nlmon plus tshark), which would add further tool
dependencies, and then, a capture does not say which socket a message was
sent through, which is needed to tell the requester's port ID from someone
else's.
Python with YNL, which the selftests already have, can read nlmsg_pid and
compare it to the requester's socket directly. So instead of extending /
consolidating with fdb_notify.sh, a new test was written.
Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Acked-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
---
Notes:
v3:
- Close the sockets explicitly: register YnlFamily.close() with
defer() in Env._socket(), and do the same for the raw socket.
Retain the pre-existing R-b and A-b for the fix.
---
CC: Shuah Khan <shuah@kernel.org>
CC: linux-kselftest@vger.kernel.org
tools/testing/selftests/net/Makefile | 1 +
tools/testing/selftests/net/config | 1 +
.../selftests/net/fdb_notify_portid.py | 301 ++++++++++++++++++
3 files changed, 303 insertions(+)
create mode 100755 tools/testing/selftests/net/fdb_notify_portid.py
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 4409ecaf0b5f..7f9f95094e0c 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -35,6 +35,7 @@ TEST_PROGS := \
fcnal-other.sh \
fdb_flush.sh \
fdb_notify.sh \
+ fdb_notify_portid.py \
fib-onlink-tests.sh \
fib_nexthop_multiprefix.sh \
fib_nexthop_nongw.sh \
diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config
index dab62cdfa27b..18dfb45fb914 100644
--- a/tools/testing/selftests/net/config
+++ b/tools/testing/selftests/net/config
@@ -126,6 +126,7 @@ CONFIG_NFT_COMPAT=m
CONFIG_NFT_NAT=m
CONFIG_NUMA=y
CONFIG_OPENVSWITCH=m
+CONFIG_PACKET=y
CONFIG_PAGE_POOL_STATS=y
CONFIG_PSAMPLE=m
CONFIG_RPS=y
diff --git a/tools/testing/selftests/net/fdb_notify_portid.py b/tools/testing/selftests/net/fdb_notify_portid.py
new file mode 100755
index 000000000000..b74123db80d0
--- /dev/null
+++ b/tools/testing/selftests/net/fdb_notify_portid.py
@@ -0,0 +1,301 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+"""
+Check that FDB notifications carry the netlink port ID of the requester in
+nlmsg_pid. A listener socket subscribed to the neighbour group receives the
+notifications, while separate requester sockets make the changes. The port ID
+the listener sees is expected to match that of the requester socket.
+"""
+
+import socket
+import struct
+from collections import namedtuple
+
+from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_ne, ksft_true
+from lib.py import ksft_busy_wait, defer
+from lib.py import ip, tool
+from lib.py import NetNS, NetNSEnter
+from lib.py import Netlink, RtnlNeighFamily
+
+RTM_NEWNEIGH = 28
+RTM_DELNEIGH = 29
+
+NTF_SELF = 0x02
+NTF_MASTER = 0x04
+
+NUD_NOARP = 0x40
+NUD_PERMANENT = 0x80
+
+ETH_P_EXP1 = 0x88b5 # Local Experimental EtherType 1
+
+Ntf = namedtuple("Ntf", "type portid addr")
+
+
+class Env:
+ """The listener socket, the two requester sockets A and B, and the
+ netlink port IDs of the latter. All sockets live in the given netns."""
+
+ @staticmethod
+ def _socket(ns):
+ with NetNSEnter(ns):
+ rtnl = RtnlNeighFamily()
+ # YnlFamily is in a reference cycle with itself, and an open socket
+ # pins the netns, so close it explicitly.
+ defer(rtnl.close)
+ return rtnl
+
+ def __init__(self, ns):
+ self.ns = str(ns)
+
+ self.listener = self._socket(ns)
+ self.listener.include_raw = True
+ self.listener.ntf_subscribe("rtnlgrp-neigh")
+
+ self.a = self._socket(ns)
+ self.a.sock.bind((0, 0))
+
+ self.b = self._socket(ns)
+ self.b.sock.bind((0, 0))
+
+ self.portids = {self.a: self.a.sock.getsockname()[0],
+ self.b: self.b.sock.getsockname()[0]}
+
+ ksft_ne(self.portids[self.a], self.portids[self.b])
+ ksft_ne(self.portids[self.a], 0)
+
+ def ifindex(self, name):
+ """Look up the ifindex of a device in the test netns."""
+ with NetNSEnter(self.ns):
+ return socket.if_nametoindex(name)
+
+ def ntfs(self):
+ """Return the bridge-family notifications received by the listener."""
+ self.listener.check_ntf()
+ ntfs = []
+ while self.listener.async_msg_queue.qsize() > 0:
+ msg = self.listener.async_msg_queue.get()
+ if msg['msg']['ndm-family'] == socket.AF_BRIDGE:
+ ntfs.append(Ntf(msg['raw'].nl_type, msg['raw'].nl_portid,
+ msg['msg']['lladdr']))
+ return ntfs
+
+ def check_ntfs(self, nl_type, addr, requester, count=1):
+ """Expect count notifications for addr, all from the requester."""
+ portids = [n.portid for n in self.ntfs()
+ if n.type == nl_type and n.addr == addr]
+ ksft_eq(portids, [self.portids[requester]] * count,
+ f"nlmsg_pid of {addr} notifications")
+
+
+def fdb_req(ifindex, ndm_flags, attrs):
+ """Build an AF_BRIDGE neighbour request with the given attributes."""
+ return {'ndm-family': socket.AF_BRIDGE, 'ndm-ifindex': ifindex,
+ 'ndm-flags': ndm_flags, **attrs}
+
+
+def test_msg(env, sock, message, message_type,
+ ifindex, addr, ndm_flags, flags, count, attrs):
+ """Send one request through SOCK, and expect COUNT notifications of
+ MESSAGE_TYPE about ADDR carrying the port ID of SOCK."""
+ # pylint: disable=too-many-arguments,too-many-positional-arguments
+ req = fdb_req(ifindex, ndm_flags, attrs | {"lladdr": addr})
+ sock.do(message, req, flags)
+ env.check_ntfs(message_type, addr, sock, count)
+
+
+def add_replace_del(env, ifname, addr, ndm_flags, *, add, rep, dlt, count=1):
+ """Add an entry through socket A, replace it through B, delete through A.
+ ADD, REP and DLT are the attributes of the respective requests. If REP is
+ None, the replace step is skipped."""
+ # pylint: disable=too-many-arguments
+ ifindex = env.ifindex(ifname)
+
+ test_msg(env, env.a, 'newneigh', RTM_NEWNEIGH, ifindex, addr, ndm_flags,
+ flags=[Netlink.NLM_F_CREATE | Netlink.NLM_F_EXCL],
+ count=count, attrs=add)
+
+ if rep is not None:
+ test_msg(env, env.b, 'newneigh', RTM_NEWNEIGH, ifindex, addr, ndm_flags,
+ flags=[Netlink.NLM_F_CREATE | Netlink.NLM_F_REPLACE],
+ count=count, attrs=rep)
+
+ test_msg(env, env.a, 'delneigh', RTM_DELNEIGH, ifindex, addr, ndm_flags,
+ flags=[],
+ count=count, attrs=dlt)
+
+
+def flush(env, ifname, addrs, add, dlt):
+ """Flush all permanent entries through socket A, and check that all the
+ notifications carry the port ID."""
+
+ ifindex = env.ifindex(ifname)
+
+ for addr in addrs:
+ attrs = add | {"ndm-state": NUD_PERMANENT,
+ "lladdr": addr}
+ env.a.do('newneigh', fdb_req(ifindex, NTF_SELF, attrs),
+ flags=[Netlink.NLM_F_CREATE | Netlink.NLM_F_EXCL])
+
+ env.ntfs()
+
+ attrs = dlt | {'ndm-state': NUD_PERMANENT,
+ 'ndm-state-mask': NUD_PERMANENT}
+ env.a.do('delneigh', fdb_req(ifindex, NTF_SELF, attrs),
+ flags=[Netlink.NLM_F_BULK])
+
+ ntfs = [n for n in env.ntfs() if n.type == RTM_DELNEIGH]
+
+ ksft_true({n.addr for n in ntfs} >= set(addrs),
+ "Flush did not notify about all the entries")
+ ksft_eq({n.portid for n in ntfs}, {env.portids[env.a]},
+ "nlmsg_pid of flush notifications")
+
+
+def test_bridge() -> None:
+ """Bridge port and bridge device entries: add, replace, delete."""
+ with NetNS() as ns:
+ env = Env(ns)
+
+ ip("link add br0 up type bridge vlan_default_pvid 0", ns=ns)
+ ip("link add d0 up type dummy", ns=ns)
+ ip("link set dev d0 master br0", ns=ns)
+
+ add_replace_del(env, "d0", "00:11:22:33:44:01", NTF_MASTER,
+ add={"ndm-state": NUD_PERMANENT},
+ rep={"ndm-state": NUD_NOARP},
+ dlt={})
+
+ add_replace_del(env, "br0", "00:11:22:33:44:02", NTF_SELF,
+ add={"ndm-state": NUD_PERMANENT},
+ rep=None,
+ dlt={})
+
+
+def test_bridge_vlan() -> None:
+ """Without a VID given, an entry is added and deleted in every VLAN."""
+ with NetNS() as ns:
+ env = Env(ns)
+
+ ip("link add br0 up type bridge vlan_filtering 1 vlan_default_pvid 0",
+ ns=ns)
+ ip("link add d0 up type dummy", ns=ns)
+ ip("link set dev d0 master br0", ns=ns)
+
+ tool("bridge", "vlan add dev d0 vid 10", ns=ns)
+ tool("bridge", "vlan add dev d0 vid 20", ns=ns)
+
+ # Three notifications: for VID 10 and 20, and for the entry
+ # without VLAN.
+ add_replace_del(env, "d0", "00:11:22:33:44:03", NTF_MASTER, count=3,
+ add={"ndm-state": NUD_PERMANENT},
+ rep=None,
+ dlt={})
+
+
+def test_bridge_flush() -> None:
+ """Bulk delete of bridge entries."""
+ with NetNS() as ns:
+ env = Env(ns)
+ ip("link add br0 up type bridge vlan_default_pvid 0", ns=ns)
+ ip("link add d0 up type dummy", ns=ns)
+ ip("link set dev d0 master br0", ns=ns)
+
+ flush(env, "br0", ["00:11:22:33:44:10",
+ "00:11:22:33:44:11",
+ "00:11:22:33:44:12"],
+ add={},
+ dlt={})
+
+
+def test_bridge_learned() -> None:
+ """Entries that are not a result of a request carry port ID of zero."""
+ with NetNS() as ns:
+ env = Env(ns)
+ ip("link add br0 up type bridge", ns=ns)
+ ip("link add v0 up type veth peer name v1", ns=ns)
+ ip("link set dev v0 master br0", ns=ns)
+ ip("link set dev v1 up", ns=ns)
+
+ addr = "00:11:22:33:44:31"
+ frame = (b"\xff" * 6 + bytes.fromhex(addr.replace(":", "")) +
+ struct.pack("!H", ETH_P_EXP1) + b"\0" * 46)
+ with NetNSEnter(env.ns):
+ tx = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
+ defer(tx.close)
+ tx.bind(("v1", 0))
+
+ portids = []
+
+ def learned():
+ nonlocal portids
+ tx.send(frame)
+ portids = [n.portid for n in env.ntfs()
+ if n.type == RTM_NEWNEIGH and n.addr == addr]
+ return portids != []
+
+ # Keep sending until the entry is learned: a frame that
+ # arrives while the bridge port is not yet up is dropped
+ # without triggering learning. Once the entry exists, repeated
+ # frames only refresh it and do not generate notifications.
+ ksft_busy_wait(learned, deadline=2)
+
+ ksft_eq(portids, [0], "nlmsg_pid of learned entry")
+
+
+def test_vxlan() -> None:
+ """VXLAN entries: add, replace the remote, delete."""
+ with NetNS() as ns:
+ env = Env(ns)
+ ip("link add vx0 up type vxlan id 2000 dstport 4789", ns=ns)
+
+ add_replace_del(env, "vx0", "00:11:22:33:44:41", NTF_SELF,
+ add={"dst": "192.0.2.1", "ndm-state": NUD_PERMANENT},
+ rep={"dst": "192.0.2.2", "ndm-state": NUD_PERMANENT},
+ dlt={"dst": "192.0.2.2"})
+
+
+def test_vxlan_flush() -> None:
+ """Bulk delete of VXLAN entries."""
+ with NetNS() as ns:
+ env = Env(ns)
+ ip("link add vx0 up type vxlan id 2000 dstport 4789", ns=ns)
+
+ flush(env, "vx0", ["00:11:22:33:44:50",
+ "00:11:22:33:44:51",
+ "00:11:22:33:44:52"],
+ add={"dst": "192.0.2.1"},
+ dlt={"dst": "192.0.2.1"})
+
+
+def test_macvlan() -> None:
+ """The notifications for macvlan, which does not notify by itself, are
+ sent by the generic code in rtnetlink."""
+ with NetNS() as ns:
+ env = Env(ns)
+ ip("link add dd up type dummy", ns=ns)
+ ip("link add mv0 up link dd type macvlan mode passthru", ns=ns)
+
+ # macvlan does not support replace.
+ add_replace_del(env, "mv0", "00:11:22:33:44:61", NTF_SELF,
+ add={"ndm-state": NUD_PERMANENT},
+ rep=None,
+ dlt={})
+
+
+def main() -> None:
+ """Run all the tests."""
+ ksft_run([
+ test_bridge,
+ test_bridge_vlan,
+ test_bridge_flush,
+ test_bridge_learned,
+ test_vxlan,
+ test_vxlan_flush,
+ test_macvlan,
+ ])
+ ksft_exit()
+
+
+if __name__ == "__main__":
+ main()
--
2.56.1
prev parent reply other threads:[~2026-12-04 12:45 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 12:43 [PATCH net-next v3 00/13] net: Report requester's port ID in FDB notifications Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 01/13] net: Introduce struct ndo_fdb_parms Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 02/13] net: Pass struct ndo_fdb_parms to ndo_dflt_fdb_{add,del}() Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 03/13] net: Pass portid to ndo_fdb_get through the structure Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 04/13] net: bridge: Pass struct ndo_fdb_parms a bit further Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 05/13] net: bridge: Pass portid through FDB notifications Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 06/13] net: vxlan: " Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 07/13] net: rtnetlink: Report port ID in bridge and VXLAN " Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 08/13] net: rtnetlink: Report port ID in bulk FDB delete notifications Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 09/13] net: rtnetlink: Report port ID in fallback FDB notifications Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 10/13] netlink: specs: rt-neigh: Extend the delneigh request attributes Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 11/13] tools: ynl: Add NLM_F_BULK Petr Machata
2026-12-04 12:43 ` [PATCH net-next v3 12/13] selftests: net: lib: py: Add RtnlNeighFamily Petr Machata
2026-12-04 12:43 ` Petr Machata [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=51ef4a33de413d02ec15fb9cb5cd115a5002b0e2.1796387296.git.petrm@nvidia.com \
--to=petrm@nvidia.com \
--cc=andrew+netdev@lunn.ch \
--cc=aroulin@nvidia.com \
--cc=bridge@lists.linux.dev \
--cc=davem@davemloft.net \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mlxsw@nvidia.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=razor@blackwall.org \
--cc=shuah@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox