Skip to contentSkip to conversation

net: Fix protodown with macvlan

PATCH Superseded Ido Schimmel 1 participant 5 messages May 5 – May 6

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: Fix protodown with macvlan

When protodown is enabled on a macvlan, two bugs cause the macvlan to incorrectly report an UP operational state:

1. Toggling the lower device's carrier while protodown is enabled on the macvlan causes the macvlan to inherit the UP operational state, effectively bypassing the protodown mechanism.

2. Toggling protodown on and then off on the macvlan while the lower device has no carrier causes the macvlan to report UP instead of LOWERLAYERDOWN, since netif_change_proto_down() unconditionally turns the carrier on.

Patch #1 solves the first problem by making netif_stacked_transfer_operstate() return early when protodown is on.

Patch #2 solves the second problem by calling netif_stacked_transfer_operstate() instead of netif_carrier_on() when protodown is disabled on a net device that has a linked net device.

Patch #3 adds a selftest covering both bugs and the basic protodown functionality.

Targeting at net-next since these are not regressions (i.e., never worked).

Note that while these changes are in the core, they should only affect macvlan as protodown is only supported by macvlan and vxlan and only the former has a linked net device.

v2:
- Move protodown handling away from drivers to the core (Jakub).
- Add a new test case for vxlan.
v1: https://lore.kernel.org/netdev/[email protected]/

Ido Schimmel (3):
net: Do not inherit operational state when protodown is on
net: Do not unconditionally turn on carrier when turning off protodown
selftests: net: Add protodown tests

 net/core/dev.c                           |  28 +++-
 tools/testing/selftests/net/Makefile     |   1 +
 tools/testing/selftests/net/protodown.sh | 182 +++++++++++++++++++++++
 3 files changed, 209 insertions(+), 2 deletions(-)
 create mode 100755 tools/testing/selftests/net/protodown.sh
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 net: Do not inherit operational state when protodown is on

The protodown functionality allows user space to turn off the carrier of a net device:

 # ip link add name dummy1 up type dummy
 # ip link add name macvlan1 up link dummy1 type macvlan mode bridge
 # ip link set dev macvlan1 protodown on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  DOWN           0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>

Different applications can set different protodown reasons, which prevents an application from turning on the carrier of a net device as long as others want it down:

 # ip link set dev macvlan1 protodown_reason 1 on
 # ip link set dev macvlan1 protodown_reason 2 on
 # ip link set dev macvlan1 protodown off
 Error: Cannot clear protodown, active reasons.
 # ip link set dev macvlan1 protodown_reason 2 off
 # ip link set dev macvlan1 protodown off
 Error: Cannot clear protodown, active reasons.
 # ip link set dev macvlan1 protodown_reason 1 off
 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>

Unfortunately, this mechanism is not very useful when the carrier of a net device can be toggled by toggling the carrier of its lower device:

 # ip link set dev macvlan1 protodown on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  DOWN           0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev dummy1 carrier off
 # ip link set dev dummy1 carrier on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>

Obviously, this is not the intended behavior and it is unlikely to be relied on by anyone. In fact, it is a problem for applications like FRR that use protodown with macvlan on top of a bridge as part of Virtual Router Redundancy Protocol (VRRP).

Solve this by preventing a net device configured with protodown on from inheriting the operational state of its lower device. Note that READ_ONCE() is not needed as RTNL is held.

Output with the patch:

 # ip link add name dummy1 up type dummy
 # ip link add name macvlan1 up link dummy1 type macvlan mode bridge
 # ip link set dev macvlan1 protodown on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  DOWN           0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev dummy1 carrier off
 # ip link set dev dummy1 carrier on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  DOWN           0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>
---
 net/core/dev.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/dev.c +3 -0
index 06c195906231..bfb0f297b234 100644
@@ -11113,6 +11113,9 @@ EXPORT_SYMBOL(netdev_change_features);
 void netif_stacked_transfer_operstate(const struct net_device *rootdev,
 					struct net_device *dev)
 {
+	if (dev->proto_down)
+		return;
+
 	if (rootdev->operstate == IF_OPER_DORMANT)
 		netif_dormant_on(dev);
 	else
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/3 net: Do not unconditionally turn on carrier when turning off protodown

In reply to Ido Schimmel

The protodown functionality allows user space to turn off the carrier of a net device:

 # ip link add name dummy1 up type dummy
 # ip link add name macvlan1 up link dummy1 type macvlan mode bridge
 # ip link set dev macvlan1 protodown on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  DOWN           0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>

When protodown is turned off, the core unconditionally turns on the carrier of the net device:

 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>

This is wrong as it means that a macvlan can end up with a carrier when its lower device does not have a carrier:

 # ip link set dev dummy1 carrier off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  LOWERLAYERDOWN 0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev macvlan1 protodown on
 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>

Solve this by resolving the linked net device and if one exists, inherit its operational state when protodown is turned off. Otherwise, as before, simply turn on the carrier. Set 'dev->proto_down' before calling netif_stacked_transfer_operstate() as this function is a NOP when protodown is turned on.

Resolve the linked net device using a new helper and have it return the device itself (in a similar fashion to dev_get_iflink()) if the device does not implement both ndo_get_iflink() and get_link_net(). If the latter is not implemented, it is unclear in which network namespace we should look up the linked net device. Currently, this helper is only used for net devices that support protodown (macvlan and vxlan) and for both it returns the correct result.

Output with the patch:

 # ip link add name dummy1 up type dummy
 # ip link add name macvlan1 up link dummy1 type macvlan mode bridge
 # ip link set dev dummy1 carrier off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  LOWERLAYERDOWN 0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev macvlan1 protodown on
 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  LOWERLAYERDOWN 0a:5c:a3:05:c7:86 <NO-CARRIER,BROADCAST,MULTICAST,UP>
 # ip link set dev dummy1 carrier on
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>
 # ip link set dev macvlan1 protodown on
 # ip link set dev macvlan1 protodown off
 $ ip -br link show dev macvlan1
 macvlan1@dummy1  UP             0a:5c:a3:05:c7:86 <BROADCAST,MULTICAST,UP,LOWER_UP>
---
 net/core/dev.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
net/core/dev.c +23 -2
index bfb0f297b234..46f8a2efd982 100644
@@ -10141,17 +10141,38 @@ bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
 }
 EXPORT_SYMBOL(netdev_port_same_parent_id);
 
+static struct net_device *dev_get_iflink_dev(struct net_device *dev)
+{
+	struct net *net;
+
+	ASSERT_RTNL();
+
+	if (!dev->netdev_ops->ndo_get_iflink || !dev->rtnl_link_ops ||
+	    !dev->rtnl_link_ops->get_link_net)
+		return dev;
+
+	net = dev->rtnl_link_ops->get_link_net(dev);
+	return __dev_get_by_index(net, dev_get_iflink(dev));
+}
+
 int netif_change_proto_down(struct net_device *dev, bool proto_down)
 {
+	struct net_device *iflink_dev;
+
 	if (!dev->change_proto_down)
 		return -EOPNOTSUPP;
 	if (!netif_device_present(dev))
 		return -ENODEV;
+	iflink_dev = dev_get_iflink_dev(dev);
+	if (!iflink_dev)
+		return -ENODEV;
+	WRITE_ONCE(dev->proto_down, proto_down);
 	if (proto_down)
 		netif_carrier_off(dev);
-	else
+	else if (dev == iflink_dev)
 		netif_carrier_on(dev);
-	WRITE_ONCE(dev->proto_down, proto_down);
+	else
+		netif_stacked_transfer_operstate(iflink_dev, dev);
 	return 0;
 }
 
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 3/3 selftests: net: Add protodown tests

In reply to Ido Schimmel

Add a selftest for the protodown mechanism.

Five test cases are included:

1. Basic protodown toggling: Verify that setting protodown on macvlan results in DOWN operational state and clearing it restores UP.

2. Same as the previous test case, but with vxlan.

3. Protodown reasons: Verify that protodown cannot be cleared while there are active protodown reasons, but can be cleared once all reasons are removed.

4. Operational state inheritance: Verify that toggling the lower device's carrier while protodown is on does not cause the macvlan to inherit the UP operational state.

5. Lower layer down: Verify that toggling protodown while the lower device has no carrier does not cause the macvlan to transition to UP operational state.

Note that the last two test cases fail without "net: Do not inherit operational state when protodown is on" and "net: Do not unconditionally turn on carrier when turning off protodown":

 # ./protodown.sh
 TEST: Basic protodown on/off with macvlan                           [ OK ]
 TEST: Basic protodown on/off with vxlan                             [ OK ]
 TEST: Protodown reasons                                             [ OK ]
 TEST: Inheriting operational state with protodown                   [FAIL]
         Macvlan operational state is not DOWN despite protodown
 TEST: Protodown with lower layer down                               [FAIL]
         Macvlan is not LOWERLAYERDOWN after clearing protodown
Assisted-by Claude:claude-opus-4-6
---
 tools/testing/selftests/net/Makefile     |   1 +
 tools/testing/selftests/net/protodown.sh | 182 +++++++++++++++++++++++
 2 files changed, 183 insertions(+)
 create mode 100755 tools/testing/selftests/net/protodown.sh
tools/testing/selftests/net/Makefile +1 -0
index baa30287cf22..c6ff7b504e97 100644
@@ -69,6 +69,7 @@ TEST_PROGS := \
 	nl_netdev.py \
 	nl_nlctrl.py \
 	pmtu.sh \
+	protodown.sh \
 	psock_snd.sh \
 	reuseaddr_ports_exhausted.sh \
 	reuseport_addr_any.sh \
tools/testing/selftests/net/protodown.sh new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw
In reply to Ido Schimmel
Quoted 25 lines · Ido Schimmel

When protodown is enabled on a macvlan, two bugs cause the macvlan to incorrectly report an UP operational state:

1. Toggling the lower device's carrier while protodown is enabled on the macvlan causes the macvlan to inherit the UP operational state, effectively bypassing the protodown mechanism.

2. Toggling protodown on and then off on the macvlan while the lower device has no carrier causes the macvlan to report UP instead of LOWERLAYERDOWN, since netif_change_proto_down() unconditionally turns the carrier on.

Patch #1 solves the first problem by making netif_stacked_transfer_operstate() return early when protodown is on.

Patch #2 solves the second problem by calling netif_stacked_transfer_operstate() instead of netif_carrier_on() when protodown is disabled on a net device that has a linked net device.

Patch #3 adds a selftest covering both bugs and the basic protodown functionality.

Targeting at net-next since these are not regressions (i.e., never worked).

Quoted 8 lines

Note that while these changes are in the core, they should only affect macvlan as protodown is only supported by macvlan and vxlan and only the former has a linked net device.

v2:
- Move protodown handling away from drivers to the core (Jakub).
- Add a new test case for vxlan.
v1: https://lore.kernel.org/netdev/[email protected]/

I thought about this again and since protodown is only about carrier, I think it's best to avoid netif_stacked_transfer_operstate(). Instead, make netif_carrier_on() a NOP when protodown is turned on and when protodown is turned off, only turn on the carrier if the linked net device (assuming it exists) also has a carrier.

IOW, I will make these changes for v3:

net/core/dev.c +1 -6
index 46f8a2efd982..0c272f6e9aaa 100644
@@ -10169,10 +10169,8 @@ int netif_change_proto_down(struct net_device *dev, bool proto_down)
 	WRITE_ONCE(dev->proto_down, proto_down);
 	if (proto_down)
 		netif_carrier_off(dev);
-	else if (dev == iflink_dev)
+	else if (dev == iflink_dev || netif_carrier_ok(iflink_dev))
 		netif_carrier_on(dev);
-	else
-		netif_stacked_transfer_operstate(iflink_dev, dev);
 	return 0;
 }
 
@@ -11134,9 +11132,6 @@ EXPORT_SYMBOL(netdev_change_features);
 void netif_stacked_transfer_operstate(const struct net_device *rootdev,
 					struct net_device *dev)
 {
-	if (dev->proto_down)
-		return;
-
 	if (rootdev->operstate == IF_OPER_DORMANT)
 		netif_dormant_on(dev);
 	else
net/sched/sch_generic.c +3 -0
index a93321db8fd7..05c250c483f0 100644
@@ -609,6 +609,9 @@ static void netdev_watchdog_down(struct net_device *dev)
  */
 void netif_carrier_on(struct net_device *dev)
 {
+	if (READ_ONCE(dev->proto_down))
+		return;
+
 	if (test_and_clear_bit(__LINK_STATE_NOCARRIER, &dev->state)) {
 		if (dev->reg_state == NETREG_UNINITIALIZED)
 			return;