Netdev List
 help / color / mirror / Atom feed
From: "Íñigo Huguet" <ihuguet@riseup.net>
To: Jakub Kicinski <kuba@kernel.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Shuah Khan <shuah@kernel.org>
Cc: "Sabrina Dubroca" <sd@queasysnail.net>,
	"Filip Pokryvka" <fpokryvk@redhat.com>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	"Íñigo Huguet" <ihuguet@riseup.net>
Subject: [PATCH net-next 2/5] netdevsim: allow changing carrier state via sysfs
Date: Sun, 10 May 2026 18:43:34 +0200	[thread overview]
Message-ID: <20260510164337.7611-3-ihuguet@riseup.net> (raw)
In-Reply-To: <20260510164337.7611-1-ihuguet@riseup.net>

Currently it is only possible to change it by linking a peer netdevsim
and setting the peer's state to down.

Allow to change the carrier state when having a single netdevsim device
without any peer, by writing to it's carrier attribute in sysfs:
/sys/bus/netdevsim/devices/netdevsimX/net/eniYynpZ/carrier. This might
be useful for carrier related tests where a peer device is not needed
for anything else.

If it's linked with a peer, change the carrier of both, while they stay
administratively up. The alternative would be to return EOPNOTSUPP, but
that doesn't seem very intuitive. Also, there is no reason why it should
not be supported, it simulates the cable being cut or disconnected, for
example.

Signed-off-by: Íñigo Huguet <ihuguet@riseup.net>
---
 drivers/net/netdevsim/netdev.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index a750768912b5..4f77c9171b06 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -616,6 +616,28 @@ static void nsim_shaper_cap(struct net_shaper_binding *binding,
 	*flags = ULONG_MAX;
 }
 
+static int nsim_change_carrier(struct net_device *dev, bool new_carrier)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+	struct netdevsim *peer;
+
+	netdev_assert_locked(dev);
+
+	peer = rtnl_dereference(ns->peer);
+
+	if (new_carrier) {
+		netif_carrier_on(dev);
+		if (peer)
+			netif_carrier_on(peer->netdev);
+	} else {
+		netif_carrier_off(dev);
+		if (peer)
+			netif_carrier_off(peer->netdev);
+	}
+
+	return 0;
+}
+
 static const struct net_shaper_ops nsim_shaper_ops = {
 	.set			= nsim_shaper_set,
 	.delete			= nsim_shaper_del,
@@ -645,6 +667,7 @@ static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_stop		= nsim_stop,
 	.ndo_vlan_rx_add_vid	= nsim_vlan_rx_add_vid,
 	.ndo_vlan_rx_kill_vid	= nsim_vlan_rx_kill_vid,
+	.ndo_change_carrier     = nsim_change_carrier,
 	.net_shaper_ops		= &nsim_shaper_ops,
 };
 
-- 
2.53.0


  parent reply	other threads:[~2026-05-10 16:44 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-10 16:43 [PATCH net-next 0/5] netdevsim: get link via ethtool, set via sysfs Íñigo Huguet
2026-05-10 16:43 ` [PATCH net-next 1/5] netdevsim: Add ethtool get_link support Íñigo Huguet
2026-05-10 16:43 ` Íñigo Huguet [this message]
2026-05-10 16:43 ` [PATCH net-next 3/5] selftest: netdevsim: test carrier change via sysfs Íñigo Huguet
2026-05-10 16:43 ` [PATCH net-next 4/5] selftest: netdevsim: cleanup the devices and module on failure Íñigo Huguet
2026-05-10 16:43 ` [PATCH net-next 5/5] selftest: netdevsim: suppress socat warnings Íñigo Huguet
2026-05-10 16:52 ` [PATCH net-next 0/5] netdevsim: get link via ethtool, set via sysfs Jakub Kicinski
2026-05-11  9:50   ` Íñigo Huguet
2026-05-11 23:59     ` Jakub Kicinski
2026-05-12  7:33       ` Íñigo Huguet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260510164337.7611-3-ihuguet@riseup.net \
    --to=ihuguet@riseup.net \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fpokryvk@redhat.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox