From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.200.49 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796388352; b=gSvEgv4iuABpXlCdgV+cFst4zDzRrz89F4lSXHINjOA/tf1K6GTUWGDEFUvaV1m6/r39 D+Ykm97xN6Hw15f6a9FHybbK8YSIG93T65Ad23YFgzfyp1LNM3ZnUYWL20lRydIsySbm5 7H7DgZiQ92xfyz71VNKYVhq6Sr+fOksAEeDHlZmQ6BcTs//XuSt8QXX6MYNl9NxLlOUXi hgbxN8YaWkxxr6b64QpUUl2Hfip3LV5JHxYbphU7eFLCJT7IrQyZep6WKzGwDtdEpctjT Yy/WJ23m/cb/JWoapVp9SMdGZheQyZaBM8Cd2QEy1/vCxDBTW9lxAtKBRY9mCdqJEZQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388352; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy:X-EOPAttributedMessage: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=A4o1LAChC7HbmAqOcaLNqKhsIkE/WC6YwTDSFH6YtoM=; b=2u1CK7V61jQAbWY4VJWg+/a/YBygMNsewDmLyjFPG5RhXaCimnJJfG5bwoksgRx2CHUA GSj2nCgRe9f45V8DgwU8A+mJYWDW96RnwxWkPKUwBMeNL2WSMV5FJlC2Lw7veFPQsRwkP z8sWWdmXOy9Ax2Nwp4olP9v5SFapTN3Z/V03MiFQaDTh+puWlgIARNfbbicDuWP5lklav SDnR3xVMc9qbdfGFN9o1/LIYRwRxh/+fLOVikTcS/mEzRS7yxwX59KeWOqrtoz6EWSPOs 0WUVO/bzjlUCe4THXoELHThi5A2QG8EEeRvE2PdL5LH8LiWHzGIZPyZa37Bm89GMhdQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b="VbX/PdLg"; arc=pass header.oldest-pass=0 smtp.remote-ip=40.107.200.49 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012049.outbound.protection.outlook.com [40.107.200.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A5A9606A32D; Fri, 4 Dec 2026 12:45:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4A5A9606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=VbX/PdLg ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EZ9haD5o6Nu4e0sUpuN9CNv0G9Wb7582oP+Pc2O+LlIZgAJ0Y6/D/J9IJ4UZIiImqvxMznR9nsRCV6b8dzDeGrS+fdg49pOgog0o7PA/AfKA8t5bTSFKcVmHyq/K1CkULhFv3yoqL+Qs/zVJtlVLZJGnU9vwK2rquxfb0M9aF1lSpw15AjFuwi1Mq8nWGrCQBfVzpGyGKWr13PvkDWyrioGSRBY5KLtNRQqEoaaQLi24w5qnctYKVMBTvPNE8LLcKawzXlkM+dOVXmsJvqcO+m5EUBCkCj4/srDMY1Cmf9r6tcEsq02N89d2x4a8BWCYHB9cRIU8efAOK50TCl0WGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=A4o1LAChC7HbmAqOcaLNqKhsIkE/WC6YwTDSFH6YtoM=; b=F4uDPSPFvCTBhagyID5SRNCf2vu/NZPuWdXhsaxNF/9PrCRKJgd1RMRqsMHze2bSjixKtxLkNNe9D8eHvXxdiMBNR7mOJLNj/kR4aoD53Fq4hLRZQG6jPnlgEs/3z7sykdC2wOJREFWAPlZs5QVoekg2MPePp6Zha6kj+W2GvTE1vUtbvXjHlD25VeqXLdEQ3C5l1dFXrr8+cWX3MhE/cP6pINpWC05vVfR3X66TA5VOiJEFSPRvxZWXiO72RFlTA+3BXecFguajKgMnDx2XaRj0Mo7ZKzztFK575IgUDpzI1gFE0a7Dk9mIaYGzud9wVr0ofdWovlqbte1h/kFGdg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=davemloft.net smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=A4o1LAChC7HbmAqOcaLNqKhsIkE/WC6YwTDSFH6YtoM=; b=VbX/PdLgz6LnbRhP2blu0nV0A0G0THfLyAyYHyP7AltXAaqZ5s37yOF0gsFOdzRx0dKIH5wPBv6YNbIJ0W5EtDupwJZKx19xj3QhdyDeF8Ut+vnSfyIy52BlDSHu+q8pbVgDTgovBkfO+halXv9SvPO5S2VzNJ61hr8/ZeTcWmROgDEWrqDU6k3zRs4Vr4bad5NxMp4mkY4gcDjLZtbwtvxjNvb7Hb1j9ovKDuigYzrF/ZwLJHtFyRTnA+9yskPGX9KcbEBGkFO+fkiO73M2ibBpaGzmkIGKKQeO9Vq8SZUwC9GJ4rtdICWD/+fausQdpzrs7GNUkT/l+r9DqJF5wg== Received: from CH2PR07CA0049.namprd07.prod.outlook.com (2603:10b6:610:5b::23) by CH1PR12MB9694.namprd12.prod.outlook.com (2603:10b6:610:2af::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.12; Fri, 4 Dec 2026 12:45:45 +0000 Received: from CH2PEPF000166C7.namprd04.prod.outlook.com (2603:10b6:610:5b:cafe::17) by CH2PR07CA0049.outlook.office365.com (2603:10b6:610:5b::23) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.688.13 via Frontend Transport; Fri, 4 Dec 2026 12:45:45 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by CH2PEPF000166C7.mail.protection.outlook.com (10.167.244.229) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.709.6 via Frontend Transport; Fri, 4 Dec 2026 12:45:44 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:45:29 -0800 Received: from tuxedo.mtl.com (10.126.230.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 04:45:23 -0800 From: Petr Machata To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , CC: Andrew Lunn , Nikolay Aleksandrov , , Ido Schimmel , Petr Machata , Andy Roulin , , Przemek Kitszel , Shuah Khan , 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 Message-ID: <51ef4a33de413d02ec15fb9cb5cd115a5002b0e2.1796387296.git.petrm@nvidia.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: References: X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PEPF000166C7:EE_|CH1PR12MB9694:EE_ X-MS-Office365-Filtering-Correlation-Id: 26b42d39-b8b7-4565-bdc1-08df52048389 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|82310400026|376014|36860700016|23010399003|1800799024|11063799006|6133799003|261009223027099003|22082099003|18002099003|56012099006|10067099003|261022075727099003|261102123227099003|261028144727099003|261022105027099003|261026121527099003|261026140227099003|261021123027099003|261021121427099003; X-Microsoft-Antispam-Message-Info: dIuOo51NOi7qU2/EUmQxfVv/mkywI0Fip3Yywx8oMXVnE8Qn80YfBC4OPEWkodlcKykQV9AXpvM7WE1LQtkNPbqge0tP7uccAcOw1zo8bZWuPl1RIcF8qUSeQ8lbiJyCzneKVu99ElDFHRwKpjRpZA26YzyqU644ndaXjUYv1CRgGSxrOKeahnHnNdUBdhSF3jQOLeplip6/vwLY112PK677tqsnluxaBRv8rsqYUdHc4Wc+vJdY1e2SiS71DMy92jMsVVLuMmE1HPor8ejhgZBM1C3Y885PExW3PMPyVTvzKGAM4HU3BgU92HGUGUOkkPZWwAXOpLI4PPIEWr1TH+UlarrrPwAQS6d5Hiav/bBtt0LhK3+/e41y4k+uKZg4AMs1JTV5ZD5wWVxEDWKgBj4gPV10s+aKHQoZXqzeCtTF306m+REjHA/chphIZ6ybfpiX861EJNNIDRynCl49DYmVFEVfjTgDIN96RbnLSBQLqPyguxi8PZa4ymGkIEzsF55z6QMaDPZE9NnjTF47XroRKYYGZ54RzfNsoEpZUlmTuKojXW1tZjB9KTzIpKthDhc7sIXfmM1BPYL2iB/CbPGL2p5lgj31SHOQyOhv8xCpNqUAWNQcLVn/5r3T2/pFP2jBfezCJZnH037i8cLiExqvUBmwnnBcdhTaesdddcuK3Tp109buIEAJ3fTm/fsbufOZDWxxEeckhzH1tnK/xA== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(7416014)(82310400026)(376014)(36860700016)(23010399003)(1800799024)(11063799006)(6133799003)(261009223027099003)(22082099003)(18002099003)(56012099006)(10067099003)(261022075727099003)(261102123227099003)(261028144727099003)(261022105027099003)(261026121527099003)(261026140227099003)(261021123027099003)(261021121427099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7HrornMbCXwSMVTP3DHyjeCjwYC3eQO0W5XsOjFYwEQAjQJbNMTFBsdVHP+8l8cMrY6Xx1fwCwfPYW0Fp1h3Of/JgYObnqFqCs64z2FwxzKcTdWSJWlDUB9yNd5a3ywblHyB5Bf60BvP3Ry9NbHHitU3tfnFyl9Hpq5/n2MkSpk2JOK0YubWysaPJssuu6HJd3PXVbGdO1nd2K0DUG+X+xupIWyhPUi9eH0mcterDqU4mh61Ell3+lxdMvTNrbTo5SYgMg9UMjGc8PDQUsz+lMVQl8HGsUqKqe7c2tH4dfqoVQAohb9U+tiLqKRTOEoZ1HDv/528eGSsbKEH8+PczmdIV4e9A8SoG2J0zIYl4b0AMoHQXxdq5rji5c2ZtZ7b46LrUJPHo2ND3P9+TxBBb98zhL/rKxzX0lN3JUVdl66i/xmtJwvkUWfCByTY9iz9 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 12:45:44.8742 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 26b42d39-b8b7-4565-bdc1-08df52048389 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CH2PEPF000166C7.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH1PR12MB9694 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 Reviewed-by: Nikolay Aleksandrov Acked-by: Przemek Kitszel --- 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 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