net: netdevsim: fix inconsistent carrier state after link/unlink
Subsystems (4)
v2 cover net: netdevsim: fix inconsistent carrier state after link/unlink
From: Yohei Kojima <[email protected]>
This series fixes netdevsim's inconsistent behavior between carrier and link/unlink state.
More specifically, this fixes a bug that the carrier goes DOWN although two netdevsim were peered, depending on the order of peering and ifup. Especially in a NetworkManager-enabled environment, netdevsim test fails because of this.
The first patch fixes the bug itself in netdevsim/bus.c by adding netif_carrier_on() into a proper function. The second patch adds a regression test for this bug.
Changelog
=========
v1 -> v2
- Rebase to the latest net/main
- Separate TFO tests from this series
- Separate netdevsim test improvement from this series
- v1: https://lore.kernel.org/netdev/[email protected]/
Yohei Kojima (2):
net: netdevsim: fix inconsistent carrier state after link/unlink
selftests: netdevsim: add carrier state consistency test
drivers/net/netdevsim/bus.c | 6 ++ .../selftests/drivers/net/netdevsim/peer.sh | 63 +++++++++++++++++++ 2 files changed, 69 insertions(+)
Signature · 1 line
2.51.2
PATCH v2 1/2 net: netdevsim: fix inconsistent carrier state after link/unlink
From: Yohei Kojima <[email protected]>
This patch fixes the edge case behavior on ifup/ifdown and linking/unlinking two netdevsim interfaces:
1. unlink two interfaces netdevsim1 and netdevsim2
2. ifdown netdevsim1
3. ifup netdevsim1
4. link two interfaces netdevsim1 and netdevsim2
5. (Now two interfaces are linked in terms of netdevsim peer, but carrier state of the two interfaces remains DOWN.)
This inconsistent behavior is caused by the current implementation, which only cares about the "link, then ifup" order, not "ifup, then link" order. This patch fixes the inconsistency by calling netif_carrier_on() when two netdevsim interfaces are linked.
This patch fixes buggy behavior on NetworkManager-based systems which causes the netdevsim test to fail with the following error:
# timeout set to 600 # selftests: drivers/net/netdevsim: peer.sh # 2025/12/25 00:54:03 socat[9115] W address is opened in read-write mode but only supports read-only # 2025/12/25 00:56:17 socat[9115] W connect(7, AF=2 192.168.1.1:1234, 16): Connection timed out # 2025/12/25 00:56:17 socat[9115] E TCP:192.168.1.1:1234: Connection timed out # expected 3 bytes, got 0 # 2025/12/25 00:56:17 socat[9109] W exiting on signal 15 not ok 13 selftests: drivers/net/netdevsim: peer.sh # exit=1
This patch also solves timeout on TCP Fast Open (TFO) test in NetworkManager-based systems because it also depends on netdevsim's carrier consistency.
--- drivers/net/netdevsim/bus.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/net/netdevsim/bus.c +6 -0
@@ -332,6 +332,9 @@ static ssize_t link_device_store(const struct bus_type *bus, const char *buf, sircu_assign_pointer (nsim_a->peer, nsim_b);rcu_assign_pointer (nsim_b->peer, nsim_a); +netif_carrier_on (dev_a); +netif_carrier_on (dev_b); + out_err:put_net (ns_b);put_net (ns_a);
@@ -381,6 +384,9 @@ static ssize_t unlink_device_store(const struct bus_type *bus, const char *buf,if (!peer) goto out_put_netns; +netif_carrier_off (dev); +netif_carrier_off (peer->netdev); + err =0 ;RCU_INIT_POINTER (nsim->peer,NULL );RCU_INIT_POINTER (peer->peer,NULL );
Signature · 1 line
2.51.2
Hello Yohei,
Quoted 5 lines · On Wed, Dec 31, 2025 at 01:03:29AM +0900, [email protected] wrote:
From: Yohei Kojima <[email protected]>
This patch fixes the edge case behavior on ifup/ifdown and linking/unlinking two netdevsim interfaces:
Quoted 7 lines
1. unlink two interfaces netdevsim1 and netdevsim2
2. ifdown netdevsim1
3. ifup netdevsim1
4. link two interfaces netdevsim1 and netdevsim25. (Now two interfaces are linked in terms of netdevsim peer, but carrier state of the two interfaces remains DOWN.)
That seems a real issue, in fact. The carriers are only getting up when opening the device, not when linking. Thus, this patch makes sense to me.
Quoted 17 lines
This inconsistent behavior is caused by the current implementation, which only cares about the "link, then ifup" order, not "ifup, then link" order. This patch fixes the inconsistency by calling
netif_carrier_on()when two netdevsim interfaces are linked.This patch fixes buggy behavior on NetworkManager-based systems which causes the netdevsim test to fail with the following error:
# timeout set to 600 # selftests: drivers/net/netdevsim: peer.sh # 2025/12/25 00:54:03 socat[9115] W address is opened in read-write mode but only supports read-only # 2025/12/25 00:56:17 socat[9115] W connect(7, AF=2 192.168.1.1:1234, 16): Connection timed out # 2025/12/25 00:56:17 socat[9115] E TCP:192.168.1.1:1234: Connection timed out # expected 3 bytes, got 0 # 2025/12/25 00:56:17 socat[9109] W exiting on signal 15 not ok 13 selftests: drivers/net/netdevsim: peer.sh # exit=1
Quoted 6 lines
This patch also solves timeout on TCP Fast Open (TFO) test in NetworkManager-based systems because it also depends on netdevsim's carrier consistency.
Fixes 1a8fed52f7be ("netdevsim: set the carrier when the device goes up")Signed-off-by Yohei Kojima <[email protected]>
Thanks for the fix!
Quoted 37 lines · Breno Leitao
Hello Yohei,
Quoted 5 lines · On Wed, Dec 31, 2025 at 01:03:29AM +0900, [email protected] wrote:
From: Yohei Kojima <[email protected]>
This patch fixes the edge case behavior on ifup/ifdown and linking/unlinking two netdevsim interfaces:
Quoted 7 lines
1. unlink two interfaces netdevsim1 and netdevsim2
2. ifdown netdevsim1
3. ifup netdevsim1
4. link two interfaces netdevsim1 and netdevsim25. (Now two interfaces are linked in terms of netdevsim peer, but carrier state of the two interfaces remains DOWN.)
That seems a real issue, in fact. The carriers are only getting up when opening the device, not when linking. Thus, this patch makes sense to me.
Quoted 17 lines
This inconsistent behavior is caused by the current implementation, which only cares about the "link, then ifup" order, not "ifup, then link" order. This patch fixes the inconsistency by calling
netif_carrier_on()when two netdevsim interfaces are linked.This patch fixes buggy behavior on NetworkManager-based systems which causes the netdevsim test to fail with the following error:
# timeout set to 600 # selftests: drivers/net/netdevsim: peer.sh # 2025/12/25 00:54:03 socat[9115] W address is opened in read-write mode but only supports read-only # 2025/12/25 00:56:17 socat[9115] W connect(7, AF=2 192.168.1.1:1234, 16): Connection timed out # 2025/12/25 00:56:17 socat[9115] E TCP:192.168.1.1:1234: Connection timed out # expected 3 bytes, got 0 # 2025/12/25 00:56:17 socat[9109] W exiting on signal 15 not ok 13 selftests: drivers/net/netdevsim: peer.sh # exit=1
Quoted 8 lines
Quoted 6 lines
This patch also solves timeout on TCP Fast Open (TFO) test in NetworkManager-based systems because it also depends on netdevsim's carrier consistency.
Fixes 1a8fed52f7be ("netdevsim: set the carrier when the device goes up")Signed-off-by Yohei Kojima <[email protected]>Reviewed-by Breno Leitao <[email protected]>
Thank you for the review!
Quoted 2 lines
Thanks for the fix!
When I encountered this bug, I could easily identify where to fix thanks to your previous commit. Thanks again for your support!
Best regards,
Yohei Kojima
Jakub Kicinski
maintainerdrivers/net/netdevsim/bus.c static ssize_t link_device_store(const struct bus_type *bus, const char *buf, si · line 336
Quoted 2 lines · On Wed, 31 Dec 2025 01:03:29 +0900 [email protected] wrote:
drivers/net/netdevsim/bus.c +2
-0+netif_carrier_on (dev_a); +netif_carrier_on (dev_b);
if (netif_running(dev_a) && netif_running(dev_b)) {
/// your code
}right? If one side is down there should be no carrier.
Signature · 1 line
pw-bot: cr
Quoted 9 lines · Jakub Kicinski
drivers/net/netdevsim/bus.c static ssize_t link_device_store(const struct bus_type *bus, const char *buf, si · line 336
Quoted 2 lines · On Wed, 31 Dec 2025 01:03:29 +0900 [email protected] wrote:
drivers/net/netdevsim/bus.c +2
-0+netif_carrier_on (dev_a); +netif_carrier_on (dev_b);if (netif_running(dev_a) && netif_running(dev_b)) { /// your code }right? If one side is down there should be no carrier.
That's right. I have updated the first patch with netif_running() checks accordingly.
Here is the v3 series including the fix:
https://lore.kernel.org/netdev/[email protected]/T/
Thank you,
Yohei Kojima
Quoted 2 lines
Signature · 1 line
pw-bot: cr
PATCH v2 2/2 selftests: netdevsim: add carrier state consistency test
In reply to [email protected]From: Yohei Kojima <[email protected]>
This commit adds a test case for netdevsim carrier state consistency. Specifically, the added test verifies the carrier state during the following operations:
1. Unlink two netdevsims
2. ifdown one netdevsim, then ifup again
3. Link the netdevsims again
4. ifdown one netdevsim, then ifup again
These steps verifies that the carrier is UP iff two netdevsims are linked and ifuped.
--- .../selftests/drivers/net/netdevsim/peer.sh | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+)
tools/testing/selftests/drivers/net/netdevsim/peer.sh +63 -0
@@ -52,6 +52,43 @@ cleanup_ns() ip netns del nssv } +is_carrier_up() +{ + local netns="$1" + local nsim_dev="$2" + + # 0: DOWN + # 1: UP + local is_up=$(ip netns exec "$netns" \ + cat /sys/class/net/"$nsim_dev"/carrier 2>/dev/null) + + test "$is_up" -eq 1 +} + +assert_carrier_up() +{ + local netns="$1" + local nsim_dev="$2" + + if ! is_carrier_up "$netns" "$nsim_dev"; then + echo "$nsim_dev's carrier should be UP, but it isn't" + cleanup_ns + exit 1 + fi +} + +assert_carrier_down() +{ + local netns="$1" + local nsim_dev="$2" + + if is_carrier_up "$netns" "$nsim_dev"; then + echo "$nsim_dev's carrier should be DOWN, but it isn't" + cleanup_ns + exit 1 + fi +} + ### ### Code start ###
@@ -113,6 +150,32 @@ if [ $? -eq 0 ]; then exit 1 fi +# netdevsim carrier state consistency checking +assert_carrier_up nssv "$NSIM_DEV_1_NAME" +assert_carrier_up nscl "$NSIM_DEV_2_NAME" + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK + +assert_carrier_down nssv "$NSIM_DEV_1_NAME" +assert_carrier_down nscl "$NSIM_DEV_2_NAME" + +ip netns exec nssv ip link set dev "$NSIM_DEV_1_NAME" down +ip netns exec nssv ip link set dev "$NSIM_DEV_1_NAME" up + +assert_carrier_down nssv "$NSIM_DEV_1_NAME" +assert_carrier_down nscl "$NSIM_DEV_2_NAME" + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK + +assert_carrier_up nssv "$NSIM_DEV_1_NAME" +assert_carrier_up nscl "$NSIM_DEV_2_NAME" + +ip netns exec nssv ip link set dev "$NSIM_DEV_1_NAME" down +ip netns exec nssv ip link set dev "$NSIM_DEV_1_NAME" up + +assert_carrier_up nssv "$NSIM_DEV_1_NAME" +assert_carrier_up nscl "$NSIM_DEV_2_NAME" + # send/recv packets tmp_file=$(mktemp)
Signature · 1 line
2.51.2