Skip to contentSkip to conversation

macsec: Add support for VLAN filtering in offload mode

PATCH In review Cosmin Ratiu 2 participants 7 messages Mar 13

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover macsec: Add support for VLAN filtering in offload mode

This short series adds support for VLANs in macsec devices when offload mode is enabled. This allows VLAN netdevs on top of macsec netdevs to function, which accidentally used to be the case in the past, but was broken. This series adds back proper support.

As part of this, the existing nsim-only macsec offload tests were translated to Python so they can run against real HW and new traffic-based tests were added for VLAN filter propagation, since there's currently no uAPI to check VLAN filters.

V4:
- Migrated nsim-only macsec tests to Python, usable against real hw.
- Ran these tests against both nsim and mlx5 devs.
- Gave up on nsim patches since the tests no longer use them.

V3: https://lore.kernel.org/netdev/[email protected]/t/#u
- Moved back to net.
- Added proper rollback support for VLAN filters in case of failure.
- Added VLAN as a requirement for the new macsec tests.

V2: https://lore.kernel.org/netdev/[email protected]/
- Sent to net-next instead of net because of apparent complexity.
- Changed VLAN filtering to only function in offload mode.
- Added tests.

V1: https://lore.kernel.org/netdev/[email protected]/

Cosmin Ratiu (3):
selftests: Migrate nsim-only MACsec tests to Python
selftests: Add MACsec VLAN propagation traffic test
macsec: Support VLAN-filtering lower devices

 drivers/net/macsec.c                          |  44 +++++-
 tools/testing/selftests/drivers/net/Makefile  |   6 +
 .../selftests/drivers/net/macsec_api.py       | 103 ++++++++++++++
 .../selftests/drivers/net/macsec_lib.py       |  20 +++
 .../selftests/drivers/net/macsec_traffic.py   | 126 ++++++++++++++++++
 .../selftests/drivers/net/netdevsim/Makefile  |   1 -
 .../drivers/net/netdevsim/macsec-offload.sh   | 117 ----------------
 7 files changed, 294 insertions(+), 123 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/macsec_api.py
 create mode 100644 tools/testing/selftests/drivers/net/macsec_lib.py
 create mode 100755 tools/testing/selftests/drivers/net/macsec_traffic.py
 delete mode 100755 tools/testing/selftests/drivers/net/netdevsim/macsec-offload.sh
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 selftests: Migrate nsim-only MACsec tests to Python

Move MACsec offload API and ethtool feature tests from tools/testing/selftests/drivers/net/netdevsim/macsec-offload.sh to tools/testing/selftests/drivers/net/macsec_api.py using the NetDrvEnv framework so tests can run against both netdevsim (default) and real hardware (NETIF=ethX). As some real hardware requires macsec to use encryption, add that to the tests.
Extract shared helpers into macsec_lib.py for reuse by upcoming traffic tests.

Netdevsim-specific limit checks (max SecY, max RX SC) are guarded behind cfg._ns checks to avoid failures on real hardware.

---
 tools/testing/selftests/drivers/net/Makefile  |   5 +
 .../selftests/drivers/net/macsec_api.py       | 103 +++++++++++++++
 .../selftests/drivers/net/macsec_lib.py       |  20 +++
 .../selftests/drivers/net/netdevsim/Makefile  |   1 -
 .../drivers/net/netdevsim/macsec-offload.sh   | 117 ------------------
 5 files changed, 128 insertions(+), 118 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/macsec_api.py
 create mode 100644 tools/testing/selftests/drivers/net/macsec_lib.py
 delete mode 100755 tools/testing/selftests/drivers/net/netdevsim/macsec-offload.sh
tools/testing/selftests/drivers/net/Makefile +5 -0
index 8154d6d429d3..511346b7d9ec 100644
@@ -10,9 +10,14 @@ TEST_GEN_FILES := \
 	napi_id_helper \
 # end of TEST_GEN_FILES
 
+TEST_FILES := \
+	macsec_lib.py \
+# end of TEST_FILES
+
 TEST_PROGS := \
 	gro.py \
 	hds.py \
+	macsec_api.py \
 	napi_id.py \
 	napi_threaded.py \
 	netpoll_basic.py \
tools/testing/selftests/drivers/net/macsec_api.py new file +103 -0

103 lines not on this page · raw message

tools/testing/selftests/drivers/net/macsec_lib.py new file +20 -0
new file mode 100644
index 000000000000..452b0f9026c8
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: GPL-2.0
+
+"""Shared helpers for MACsec offload tests."""
+
+from lib.py import KsftSkipEx, ethtool, ip
+
+MACSEC_KEY = "12345678901234567890123456789012"
+
+
+def get_macsec_offload(dev):
+    """Return macsec offload mode string from ip -d link show."""
+    info = ip(f"-d link show dev {dev}", json=True)[0]
+    return info.get("linkinfo", {}).get("info_data", {}).get("offload")
+
+
+def require_macsec_offload(cfg):
+    """SKIP if lower device doesn't support macsec-hw-offload."""
+    feat = ethtool(f"-k {cfg.ifname}", json=True)[0]
+    if not feat.get("macsec-hw-offload", {}).get("active"):
+        raise KsftSkipEx("macsec-hw-offload not supported")
tools/testing/selftests/drivers/net/netdevsim/Makefile +0 -1
index 1a228c5430f5..9808c2fbae9e 100644
@@ -11,7 +11,6 @@ TEST_PROGS := \
 	fib.sh \
 	fib_notifications.sh \
 	hw_stats_l3.sh \
-	macsec-offload.sh \
 	nexthop.sh \
 	peer.sh \
 	psample.sh \
tools/testing/selftests/drivers/net/netdevsim/macsec-offload.sh deleted +0 -117

117 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 selftests: Add MACsec VLAN propagation traffic test

In reply to Cosmin Ratiu

Add macsec_traffic.py using NetDrvEpEnv to verify VLAN filter propagation through offloaded macsec devices via actual traffic.

Test creates macsec tunnels with matching SAs on both endpoints, stacks VLANs on top, and verifies connectivity with ping. Covers:
- Offloaded macsec with VLAN (filters propagate to HW)
- Software macsec with VLAN (no HW filter propagation)
- Toggle offload on/off and verify traffic still works

On netdevsim this is a smoke test (stub offload, no real encryption). On real hardware this validates actual VLAN filter propagation.

---
 tools/testing/selftests/drivers/net/Makefile  |   1 +
 .../selftests/drivers/net/macsec_traffic.py   | 126 ++++++++++++++++++
 2 files changed, 127 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/macsec_traffic.py
tools/testing/selftests/drivers/net/Makefile +1 -0
index 511346b7d9ec..102aba3b2518 100644
@@ -18,6 +18,7 @@ TEST_PROGS := \
 	gro.py \
 	hds.py \
 	macsec_api.py \
+	macsec_traffic.py \
 	napi_id.py \
 	napi_threaded.py \
 	netpoll_basic.py \
tools/testing/selftests/drivers/net/macsec_traffic.py new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 macsec: Support VLAN-filtering lower devices

In reply to Cosmin Ratiu

VLAN-filtering is done through two netdev features (NETIF_F_HW_VLAN_CTAG_FILTER and NETIF_F_HW_VLAN_STAG_FILTER) and two netdev ops (ndo_vlan_rx_add_vid and ndo_vlan_rx_kill_vid).

Implement these and advertise the features if the lower device supports them. This allows proper VLAN filtering to work on top of macsec devices, when the lower device is capable of VLAN filtering. As a concrete example, having this chain of interfaces now works:
vlan_filtering_capable_dev(1) -> macsec_dev(2) -> macsec_vlan_dev(3)

Before commit [1] this used to accidentally work because the macsec device (and thus the lower device) was put in promiscuous mode and the VLAN filter was not used. But after commit [1] correctly made the macsec driver expose the IFF_UNICAST_FLT flag, promiscuous mode was no longer used and VLAN filters on dev 1 kicked in. Without support in dev 2 for propagating VLAN filters down, the register_vlan_dev -> vlan_vid_add -> __vlan_vid_add -> vlan_add_rx_filter_info call from dev 3 is silently eaten (because vlan_hw_filter_capable returns false and vlan_add_rx_filter_info silently succeeds).

For macsec, VLAN filters are only relevant for offload, otherwise the VLANs are encrypted and the lower devices don't care about them. So VLAN filters are only passed on to lower devices in offload mode. Flipping between offload modes now needs to offload/unoffload the filters with vlan_{get,drop}_rx_*_filter_info().

To avoid the back-and-forth filter updating during rollback, the setting of macsec->offload is moved after the add/del secy ops. This is safe since none of the code called from those requires macsec->offload.

[1] commit 0349659fd72f ("macsec: set IFF_UNICAST_FLT priv flag")

---
 drivers/net/macsec.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 39 insertions(+), 5 deletions(-)
drivers/net/macsec.c +39 -5
index f6cad0746a02..3bdb6f3fae8e 100644
@@ -2616,14 +2616,22 @@ static int macsec_update_offload(struct net_device *dev, enum macsec_offload off
 	if (!ops)
 		return -EOPNOTSUPP;
 
-	macsec->offload = offload;
-
 	ctx.secy = &macsec->secy;
 	ret = offload == MACSEC_OFFLOAD_OFF ? macsec_offload(ops->mdo_del_secy, &ctx)
 					    : macsec_offload(ops->mdo_add_secy, &ctx);
-	if (ret) {
-		macsec->offload = prev_offload;
+	if (ret)
 		return ret;
+
+	/* Remove VLAN filters when disabling offload. */
+	if (offload == MACSEC_OFFLOAD_OFF) {
+		vlan_drop_rx_ctag_filter_info(dev);
+		vlan_drop_rx_stag_filter_info(dev);
+	}
+	macsec->offload = offload;
+	/* Add VLAN filters when enabling offload. */
+	if (prev_offload == MACSEC_OFFLOAD_OFF) {
+		vlan_get_rx_ctag_filter_info(dev);
+		vlan_get_rx_stag_filter_info(dev);
 	}
 
 	macsec_set_head_tail_room(dev);
@@ -3486,7 +3494,8 @@ static netdev_tx_t macsec_start_xmit(struct sk_buff *skb,
 }
 
 #define MACSEC_FEATURES \
-	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST)
+	(NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
+	 NETIF_F_HW_VLAN_STAG_FILTER | NETIF_F_HW_VLAN_CTAG_FILTER)
 
 #define MACSEC_OFFLOAD_FEATURES \
 	(MACSEC_FEATURES | NETIF_F_GSO_SOFTWARE | NETIF_F_SOFT_FEATURES | \
@@ -3707,6 +3716,29 @@ static int macsec_set_mac_address(struct net_device *dev, void *p)
 	return err;
 }
 
+static int macsec_vlan_rx_add_vid(struct net_device *dev,
+				  __be16 proto, u16 vid)
+{
+	struct macsec_dev *macsec = netdev_priv(dev);
+
+	if (!macsec_is_offloaded(macsec))
+		return 0;
+
+	return vlan_vid_add(macsec->real_dev, proto, vid);
+}
+
+static int macsec_vlan_rx_kill_vid(struct net_device *dev,
+				   __be16 proto, u16 vid)
+{
+	struct macsec_dev *macsec = netdev_priv(dev);
+
+	if (!macsec_is_offloaded(macsec))
+		return 0;
+
+	vlan_vid_del(macsec->real_dev, proto, vid);
+	return 0;
+}
+
 static int macsec_change_mtu(struct net_device *dev, int new_mtu)
 {
 	struct macsec_dev *macsec = macsec_priv(dev);
@@ -3748,6 +3780,8 @@ static const struct net_device_ops macsec_netdev_ops = {
 	.ndo_set_rx_mode	= macsec_dev_set_rx_mode,
 	.ndo_change_rx_flags	= macsec_dev_change_rx_flags,
 	.ndo_set_mac_address	= macsec_set_mac_address,
+	.ndo_vlan_rx_add_vid	= macsec_vlan_rx_add_vid,
+	.ndo_vlan_rx_kill_vid	= macsec_vlan_rx_kill_vid,
 	.ndo_start_xmit		= macsec_start_xmit,
 	.ndo_get_stats64	= macsec_get_stats64,
 	.ndo_get_iflink		= macsec_get_iflink,
Signature · 1 line
2.43.0
#4 lore raw
In reply to Cosmin Ratiu
Quoted 2 lines · Cosmin Ratiu

This short series adds support for VLANs in macsec devices when offload mode is enabled.

Seems I forgot the "PATCH net" prefix, even though the series is based on and intended for net. Will that be a problem? Should I resend?

Cosmin.

#5 lore raw
Quoted 8 lines · Cosmin Ratiu
Quoted 2 lines · Cosmin Ratiu

This short series adds support for VLANs in macsec devices when offload mode is enabled.

Seems I forgot the "PATCH net" prefix, even though the series is based
on and intended for net. Will that be a problem? Should I resend?

To answer my own question (somewhat): I'll need to resend, as it seems I also forgot to run pylint (relied on checkpatch.pl only).

But please provide additional feedback on the tests if possible.

Thank you,
Cosmin.

maintainer
#6 lore raw
In reply to Cosmin Ratiu
Quoted 3 lines · On Fri, 13 Mar 2026 11:52:23 +0100 Cosmin Ratiu wrote:
 .../selftests/drivers/net/macsec_api.py       | 103 ++++++++++++++
 .../selftests/drivers/net/macsec_lib.py       |  20 +++
 .../selftests/drivers/net/macsec_traffic.py   | 126 ++++++++++++++++++

Thanks for converting! I think we're missing MACSEC from selftests/drivers/net/config cause kernels end up getting built without it. And we see:

# ok 1 macsec_traffic.test_vlan.offloaded # SKIP macsec-hw-offload not supported
# ok 2 macsec_traffic.test_vlan.software # SKIP macsec-hw-offload not supported
# ok 3 macsec_traffic.test_vlan_toggle.toggle_on # SKIP macsec-hw-offload not supported
# ok 4 macsec_traffic.test_vlan_toggle.toggle_off # SKIP macsec-hw-offload not supported
# # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:4 error:0

Also I'm slightly weary about "frameworkification" of the selftests so since the selftests aren't huge maybe avoid creating the lib and just squash all the macsec tests into one file? Or is there a reason? I know that it feels cleaner to separate things out but in case of tests which mostly get fly-by contributions having to teach people about libraries is quite a lift..