Skip to contentSkip to conversation

net: add Two-Port MAC Relay (TPMR) driver

RFC In review David Carlier 2 participants 4 messages May 16

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

cover net: add Two-Port MAC Relay (TPMR) driver

Hi,

This is an RFC for a small netdevice driver implementing the Two-Port MAC Relay from IEEE 802.1Q-2014 §6.7.1. A TPMR is a deliberately stripped-down L2 relay: exactly two ports, no FDB, no MAC learning, no STP, and -- by spec -- it forwards most of the 01:80:C2:00:00:0X reserved group that a regular bridge consumes (LACPDUs, LLDP, EAPOL, and so on).

The reason I started looking at this is that there is no clean way to put a Linux box in the middle of a link without breaking the control plane on either side. The obvious answer -- "use a bridge with learning and STP disabled" -- doesn't actually work, because br_input.c filters BPDUs / PAE / LACP before group_fwd_mask gets a chance to forward them, so anything on the wire that relies on those frames reaching the far end (link aggregation, MACsec setups, 802.1X) silently breaks. tc mirred only mirrors; macvlan passthru is single-master; an XDP redirect between two ifaces works but isn't a netdevice, so neither ip link nor any of the network managers can drive it. None of these are what the IEEE standard already describes.

OpenBSD's tpmr(4) [1] has filled this niche since 2019. The driver proposed here is inspired by that one but mapped onto rx_handler / rtnl_link_ops, which lets us drop a lot of the BSD bookkeeping since the Linux framework already provides it. Details are in the patch's commit message.

It's sent as an RFC because there are a handful of design choices I'd rather get pushback on now than after I've written the selftests:

1. New driver vs. a tpmr mode of the bridge. The fundamental question. A separate driver keeps the bridge code untouched and sidesteps br_input.c's filtering entirely; a bridge mode would reuse the slave management you already have. I lean toward the separate driver -- the semantics are different from what a bridge guarantees and switchdev offload would be cleaner -- but I'd genuinely rather hear from the bridge maintainers before going further.

2. MTU policy. Strict (reject mismatched slaves, refuse desyncing changes, bridge-like) or pass-through (bond-like). The current patch enforces strict at enslave time (a second slave whose MTU differs is rejected); runtime MTU policing via a notifier is left for v1 so the shape of that callback can be discussed first.

3. VLAN-tagged frames. The OpenBSD driver bails out on them. Here they are forwarded with the tag preserved -- in the MACsec / LAG cases that's almost certainly what users want -- but worth confirming.

4. Reserved-multicast forwarding table. The IEEE spec lists exactly which 01:80:C2:00:00:0X addresses a TPMR relays vs. terminates. Only 01:80:C2:00:00:01 (PAUSE) is terminated, as it must be by the MAC layer; the rest are relayed. The table is hardcoded. Making it per-instance configurable feels like premature flexibility, but I can be talked out of it.

5. Statistics. ndo_get_stats64 on the master via dev_get_tstats64 over pcpu_sw_netstats is what's implemented. Per-slave forwarded/dropped counters would be nice but add ABI surface; master-only for v1 unless someone has a stronger view.

6. Netlink shape. IFLA_INFO_KIND="tpmr" with an (initially empty) IFLA_INFO_DATA nest, leaving room for a policy flag if #4 ends up configurable. Flagging it so the shape can be argued before it's frozen.

Not in this RFC, planned for v1: selftest under tools/testing/selftests/net/ (bidirectional forwarding, link-state aggregation, MTU sync, third-slave rejection, rx_handler conflict rejection, reserved-multicast pass-through), Documentation/ networking/tpmr.rst, MAINTAINERS entry, and iproute2 support as a separate series once the kernel ABI is settled. switchdev/DSA offload and richer ethtool_ops are deliberately follow-ups -- only worth doing once the base lands and if there's demand.

Thanks for taking a look,
David

[1] https://man.openbsd.org/tpmr.4

David Carlier (1):
net: tpmr: add Two-Port MAC Relay driver

 drivers/net/Kconfig          |  14 ++
 drivers/net/Makefile         |   1 +
 drivers/net/tpmr.c           | 409 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/if_link.h |   8 +
 4 files changed, 432 insertions(+)
 create mode 100644 drivers/net/tpmr.c
Signature · 1 line
2.53.0
#1 lore raw

RFC 1/1 net: tpmr: add Two-Port MAC Relay driver

Add a driver implementing the Two-Port MAC Relay as defined by IEEE 802.1Q-2014 §6.7.1. A TPMR is a minimal L2 relay between exactly two member ports: no FDB, no MAC learning, no STP, and -- by specification -- it forwards most of the 01:80:C2:00:00:0X reserved group address range that a regular bridge would consume. This makes it suitable as a bump-in-the-wire element that is transparent to the control plane on both sides (LACP, LLDP, EAPOL, and so on continue to reach the far end as if the relay were not present).

The driver is created with "ip link add type tpmr" and slaves are attached through ndo_add_slave, with a hard cap of two members. Forwarding is implemented as an rx_handler: a frame arriving on one slave is sent out the other via dev_queue_xmit(), with no FDB lookup. The IEEE-permitted subset of reserved multicasts is relayed; the remainder is delivered to the host stack via RX_HANDLER_PASS, preserving today's behaviour for protocols that genuinely target the local machine. Only 01:80:C2:00:00:01 (IEEE 802.3x PAUSE) is terminated, as required by the MAC layer.

The master's carrier follows the logical AND of both slaves' carriers, propagated via a netdev notifier. Both slaves enter IFF_PROMISC on enslave (and the refcount is balanced on detach) so the relay sees all unicast on the wire. rx_handler_register() provides exclusivity for free: a netdevice that is already a member of a bridge, bond, team, or macvlan is rejected with -EBUSY at enslave time.

MTU consistency is enforced at enslave: a second slave whose MTU differs from the first is rejected.

Inspired by OpenBSD's tpmr(4) (David Gwynne, 2019), reimplemented against Linux's rx_handler / rtnl_link_ops infrastructure.

---
 drivers/net/Kconfig          |  14 ++
 drivers/net/Makefile         |   1 +
 drivers/net/tpmr.c           | 409 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/if_link.h |   8 +
 4 files changed, 432 insertions(+)
 create mode 100644 drivers/net/tpmr.c
drivers/net/Kconfig +14 -0
index ff79c466712d..f23de9f097e8 100644
@@ -242,6 +242,20 @@ config VXLAN
 	  To compile this driver as a module, choose M here: the module
 	  will be called vxlan.
 
+config TPMR
+        tristate "Two-Port MAC Relay (TPMR) driver"
+        help
+          This driver provides an IEEE 802.1Q-2014 §6.7.1 Two-Port MAC
+          Relay netdevice: a stripped-down L2 relay between exactly two
+          member ports, with no MAC learning, no FDB and no STP. Unlike
+          the bridge driver it forwards the IEEE-reserved
+          01:80:c2:00:00:0x group (LACPDUs, LLDP, EAPOL/802.1X, ...),
+          which makes it suitable as a transparent bump-in-the-wire in
+          deployments using link aggregation, MACsec, or 802.1X.
+
+          To compile this driver as a module, choose M here: the module
+          will be called tpmr.
+
 config GENEVE
 	tristate "Generic Network Virtualization Encapsulation"
 	depends on INET
drivers/net/Makefile +1 -0
index 88e4c485d6b2..9e9c84614b09 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_TUN) += tun.o
 obj-$(CONFIG_TAP) += tap.o
 obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
+obj-$(CONFIG_TPMR) += tpmr.o
 obj-$(CONFIG_VXLAN) += vxlan/
 obj-$(CONFIG_GENEVE) += geneve.o
 obj-$(CONFIG_BAREUDP) += bareudp.o
drivers/net/tpmr.c new file +409 -0

409 lines not on this page · raw message

include/uapi/linux/if_link.h +8 -0
index 46413392b402..98241bfcb6c7 100644
@@ -456,6 +456,14 @@ enum {
 
 #define IFLA_INET6_MAX	(__IFLA_INET6_MAX - 1)
 
+/* TPMR section */
+enum {
+	IFLA_TPMR_UNSPEC,
+	__IFLA_TPMR_MAX,
+};
+
+#define IFLA_TPMR_MAX (__IFLA_TPMR_MAX - 1)
+
 enum in6_addr_gen_mode {
 	IN6_ADDR_GEN_MODE_EUI64,
 	IN6_ADDR_GEN_MODE_NONE,
Signature · 1 line
2.53.0
#2 lore raw

Re: [RFC net-next 1/1] net: tpmr: add Two-Port MAC Relay driver

drivers/net/tpmr.c line 67

Quoted 8 lines
drivers/net/tpmr.c new file +8 -0
+static bool tpmr_should_relay_reserved(const u8 *dst)
+{
+	static const u8 prefix[5] = { 0x01, 0x80, 0xc2, 0x00, 0x00 };
+
+	if (memcmp(dst, prefix, sizeof(prefix)) != 0)
+		return true;
+
+	return dst[5] != 0x01;

I would assume the traffic is mostly unicast, with only a few multicast thrown in. So would it not make sense to first test for multicast? Only if it is multicast do you need to look deeper into the address?

Also, is_multicast_ether_addr() exists.

drivers/net/tpmr.c line 95

Quoted 4 lines
drivers/net/tpmr.c new file +4 -0
+	eh = eth_hdr(skb);
+	if (is_multicast_ether_addr(eh->h_dest) &&
+	    !tpmr_should_relay_reserved(eh->h_dest))
+		return RX_HANDLER_PASS;

Ah, you already have it here. I still think this can be optimized.

drivers/net/tpmr.c line 175

Quoted 11 lines
drivers/net/tpmr.c new file +11 -0
+	/* Enforce MTU consistency between the two members. */
+	if (tpmr->n_ports == 1) {
+		struct net_device *first;
+
+		first = rtnl_dereference(tpmr->ports[tpmr->ports[0].slot].dev);
+		if (first && first->mtu != slave_dev->mtu) {
+			NL_SET_ERR_MSG(extack,
+				       "Member MTU must match the other member's");
+			return -EINVAL;
+		}
+	}

This seems to assume that tpmr->ports[0] is filled first. But what about:

ip link eth0 set master tpmr0
ip link eth1 set master tpmr0
ip link eth0 set nomaster
ip link eth42 set master tpmr0

Quoted 4 lines
drivers/net/tpmr.c new file +4 -0
+static int tpmr_del_slave(struct net_device *dev, struct net_device *slave_dev)
+{
+	struct tpmr_priv *tpmr = netdev_priv(dev);
+	struct tpmr_port *port = NULL;

drivers/net/tpmr.c line 231

Quoted 10 lines
drivers/net/tpmr.c new file +10 -0
+	unsigned int slot;
+
+	ASSERT_RTNL();
+
+	for (slot = 0; slot < TPMR_MAX_PORTS; slot++) {
+		if (rtnl_dereference(tpmr->ports[slot].dev) == slave_dev) {
+			port = &tpmr->ports[slot];
+			break;
+		}
+	}

For the above to work, there needs to be some juggling here.

Quoted 18 lines
drivers/net/tpmr.c new file +18 -0
+static int tpmr_device_event(struct notifier_block *nb,
+			     unsigned long event, void *ptr)
+{
+	struct net_device *slave_dev = netdev_notifier_info_to_dev(ptr);
+	struct net_device *master;
+	struct tpmr_priv *tpmr;
+
+	if (slave_dev->reg_state == NETREG_REGISTERED) {
+		master = netdev_master_upper_dev_get(slave_dev);
+		if (!master || master->rtnl_link_ops != &tpmr_link_ops)
+			return NOTIFY_DONE;
+	} else {
+		return NOTIFY_DONE;
+	}
+
+	tpmr = netdev_priv(master);
+
+	switch (event) {

drivers/net/tpmr.c line 272

Quoted 10 lines
drivers/net/tpmr.c new file +10 -0
+	case NETDEV_CHANGE:
+	case NETDEV_UP:
+	case NETDEV_DOWN:
+		tpmr_update_carrier(tpmr);
+		break;
+	case NETDEV_UNREGISTER:
+		tpmr_del_slave(master, slave_dev);
+		break;
+	}
+	return NOTIFY_DONE;

Maybe you also want to act on NETDEV_PRECHANGEMTU and reject it?

drivers/net/tpmr.c line 289

Quoted 5 lines
drivers/net/tpmr.c new file +5 -0
+static void tpmr_get_drvinfo(struct net_device *dev,
+			     struct ethtool_drvinfo *drvinfo)
+{
+	strscpy(drvinfo->driver, TPMR_DRV_NAME, sizeof(drvinfo->driver));
+	strscpy(drvinfo->version, TPMR_DRV_VERSION, sizeof(drvinfo->version));

Driver version is meaningless because it never changes. Please leave it empty, and the core will fill it with the kernel version.

I also wounder if this is the correct home, or if it should be in net, next to bridge? I don't know...

Andrew

#3 lore raw

Re: [RFC net-next 1/1] net: tpmr: add Two-Port MAC Relay driver

In reply to David Carlier
Quoted 1 line

Ah, you already have it here. I still think this can be optimized.

Fair — the helper still memcmp's five bytes for every multicast frame, even plain IPv6 ND or IPv4 mcast. I'll fold the check into the caller with unlikely(), and gate the inner test on the 01:80:c2:00:00 OUI so ordinary multicast falls through with a single compare.

Quoted 1 line

This seems to assume that tpmr->ports[0] is filled first.

You're right, that's broken — after a detach, ports[0].slot is stale. I'll iterate ports[] to find the surviving member instead of trusting positional order. The del path is already slot-agnostic.

Quoted 1 line

Maybe you also want to act on NETDEV_PRECHANGEMTU and reject it?

Good catch, nothing stops a member MTU change from desyncing the pair today. I'll reject in NETDEV_PRECHANGEMTU.

Quoted 1 line

Driver version is meaningless

Will drop.

Quoted 1 line

I also wounder if this is the correct home

I went back and forth on that. Landed on drivers/net/ because TPMR is structurally closer to veth/macvlan/bonding than to bridge or openvswitch — no FDB, no port state, ~400 LOC. Happy to move it if you'd prefer net/tpmr/, but my preference is to keep it here.

Thanks for the review.

Cheers.