Skip to contentSkip to conversation

ethtool: Generic loopback support

PATCH In review Björn Töpel 4 participants 24 messages Mar 25 – Mar 27

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

v2 cover ethtool: Generic loopback support

Hi!

Background
==========

This series adds a generic ethtool loopback framework with GET/SET netlink commands, using a component/name/id/depth model that represents loopback points across the network path.

This is v2.

Design
======

The loopback model uses four axes to identify a loopback point:

1. Component (MAC, PHY, MODULE) -- identifies the Linux driver boundary that owns the loopback. This maps to the Linux driver model, not to 802.3 sublayers directly.

2. Name -- identifies the sublayer within the component using IEEE 802.3 vocabulary (e.g. "mac", "pcs", "pma", "pmd", "mii" for MAC and PHY components; "cmis-host", "cmis-media" for MODULE). The name is a free-form string to avoid over-constraining the enum, but recommended names are documented with references to 802.3 clauses.

3. Id -- optional instance selector within a component type (e.g. PHY index from phy_link_topology, port number). Defaults to 0 when there is only one instance.

4. Depth -- ordering index within a component instance. When a component has multiple loopback points of the same type (e.g. two PCS blocks inside a rate-adaptation PHY), depth distinguishes them. Lower values are closer to the host, higher toward the line/media. Defaults to 0 when there is only one loopback point per (component, name) tuple.

Direction uses IEEE 802.3 terminology: "local" (host TX -> looped back -> host RX, traffic originating from host returns to host) and "remote" (line RX -> looped back -> line TX, traffic from far end returns to far end). Direction is defined from the component's own viewpoint -- this convention holds regardless of where the component sits in the system topology.

Cross-component ordering comes from existing infrastructure (PHY link topology, phy_index). Multi-netdev shared-resource loopbacks (QSGMII, port breakout) are out of scope; drivers should only expose loopbacks they can scope to a single netdev.

A filtered DUMP (with a dev-index in the header) lists all loopback entries for that netdev; an unfiltered DUMP iterates over all netdevs. The doit handler supports exact lookup by (component, name, id).

V2 design rationale
===================

Why keep name as a string for now? The review discussed replacing name with an enum. I decided not to do that in v2.

Pros:
- enforces more consistency across drivers
- makes userspace parsing simpler
- makes selftests stricter
- keeps the UAPI taxonomy explicit

Cons:
- freezes the stage taxonomy before we fully understand the MAC side
- risks forcing unlike hardware points into the same enum bucket
- makes later extension more expensive
- may standardize the spelling without standardizing the actual semantics

This series still does not attempt to model the full datapath as a DAG or expose a generic topology dump. That would likely be useful for future diagnostics, traffic generators, and deeper validation, but it also looks like a larger design problem than loopback itself.

The intent of v2 is not to solve that larger problem now, but also not to block it:

- component gives the Linux ownership boundary
- name keeps room for stage-specific vocabulary
- id selects the component instance
- depth orders loopback points within that instance

If a richer topology API is needed later, it should be additive rather than requiring this loopback API to be replaced.

Oleksij's more topology-heavy use cases are therefore not fully solved by this series, but the current model should still be extendable for them.

CMIS support
============

The Common Management Interface Specification (CMIS) defines four diagnostic loopback types, characterized by location (Host or Media Side) and signal direction:

- Host Side Input (Rx->Tx) -- local
- Host Side Output (Tx->Rx) -- remote
- Media Side Input (Rx->Tx) -- local
- Media Side Output (Tx->Rx) -- remote

Support is detected via Page 13h Byte 128, and loopback is controlled via Page 13h Bytes 180-183 (one byte per type, one bit per lane).

The CMIS helpers work entirely over get/set_module_eeprom_by_page, so any driver that already has EEPROM page access gets module loopback without new ethtool_ops or driver changes.

Currently, only mellanox/mlxsw, and broadcom/bnxt support CMIS operations. I'll follow-up with mlx5 support.

Implementation
==============

Patch 1/12 ethtool: Add dump_one_dev callback for per-device sub-iteration
Adds the dump_one_dev callback to ethnl_request_ops, the ifindex and pos_sub fields to ethnl_dump_ctx, and the dispatch logic in ethnl_default_start() and ethnl_default_dumpit(). No functional change; no command uses dump_one_dev yet.

Patch 2/12 ethtool: Convert per-PHY commands to dump_one_dev
Converts PSE, PLCA, PHY, and MSE commands from the separate ethnl_perphy_{start,dumpit,done} handlers to use the generic dump_one_dev callback via a shared ethnl_perphy_dump_one_dev() function.

Patch 3/12 ethtool: Add loopback netlink UAPI definitions
Adds the YAML spec and generated UAPI header for the new LOOPBACK_GET/SET commands. Each loopback entry carries a component type, optional id, name string, depth, supported directions bitmask, and current direction.

Patch 4/12 ethtool: Add loopback GET/SET netlink implementation
Implements GET/SET dispatch in a new loopback.c. GET uses the dump_one_dev infrastructure for dump enumeration (by flat index) and supports doit exact lookup by (component, id, name) via parse_request. SET switches on the component and calls the right handler per entry. No components are wired yet.

Patch 5/12 ethtool: Add CMIS loopback helpers for module loopback control
Adds cmis_loopback.c with the MODULE component handlers and wires them into loopback.c's dispatch. GET enumerates entries by index (ethtool_cmis_get_loopback_by_index) or looks up by name (ethtool_cmis_get_loopback). SET (ethtool_cmis_set_loopback) resolves name to control byte indices and enforces mutual exclusivity.

Patch 6/12 selftests: drv-net: Add loopback driver test
Adds loopback_drv.py with generic tests that work on any device with module loopback support: enable/disable, direction switching, idempotent enable, and rejection while interface is up.

Patch 7/12 ethtool: Add MAC loopback support via ethtool_ops
Extends struct ethtool_ops with three loopback callbacks for driver-level MAC loopback: get_loopback (exact lookup by name/id), get_loopback_by_index (dump enumeration), and set_loopback. Wires the MAC component into loopback.c's dispatch. For dump enumeration, MAC entries are tried first, then MODULE/CMIS entries follow at the next index offset.

Patch 8/12 netdevsim: Add MAC loopback simulation
Implements the three ethtool loopback ops in netdevsim. Exposes a single MAC loopback entry ("mac") with both local and remote support. State is stored in memory and exposed via debugfs under ethtool/mac_lb/{supported,direction}.

Patch 9/12 selftests: drv-net: Add MAC loopback netdevsim test
Adds loopback_nsim.py with netdevsim-specific tests for MAC
loopback: entry presence, SET/GET round-trip with debugfs verification, and error paths.

Patch 10/12 MAINTAINERS: Add entry for ethtool loopback
Adds a MAINTAINERS entry for the ethtool loopback subsystem covering the core loopback and CMIS loopback netlink implementation, and the associated selftests.

Patch 11/12 netdevsim: Add module EEPROM simulation via debugfs
Adds get/set_module_eeprom_by_page to netdevsim, backed by a 256-page x 128-byte array exposed via debugfs.

Patch 12/12 selftests: drv-net: Add CMIS loopback netdevsim test
Extends loopback_nsim.py with netdevsim-specific tests that seed the EEPROM via debugfs: capability reporting, EEPROM byte verification, combined MAC + MODULE dump, and error paths.

Changes since v1
================

- Split dump_one_dev infrastructure patch into two: one adding the generic callback infrastructure, one converting per-PHY commands to use it. (Jakub)

- Used Jakub's suggested pattern for filtered dumps: initialize both ifindex and pos_ifindex in _start(), then break early in the loop when pos_ifindex diverges, avoiding a separate single-device code path. (Jakub)

- Dropped PCS as a top-level component. Components now map to Linux driver boundaries: MAC, PHY, MODULE. Sublayer granularity (pcs, pma, pmd, mii) lives in the name attribute using IEEE 802.3 vocabulary. (Maxime)

- Renamed near-end/far-end to local/remote per IEEE 802.3 terminology. (Maxime)

- Added depth field to ETHTOOL_A_LOOPBACK_ENTRY for ordering multiple loopback points within a single component instance (e.g. rate-adaptation PHY with two PCS blocks). (Jakub)

- Documented the viewpoint convention: direction is always from the component's own perspective, local = toward host, remote = toward line, regardless of system topology. (Oleksij)

- Expanded component doc strings to explain that the name attribute uses IEEE 802.3 sublayer vocabulary. (Andrew, Maxime)

Changes since RFC v2
====================

- Switched LOOPBACK_GET from doit-with-array to dumpit, where each loopback entry is a separate netlink message. Uses the new generic dump_one_dev sub-iterator infrastructure instead of duplicating the perphy dump pattern. (Maxime)

- u32 to u8 to represent the enums in the YAML. (Maxime)

- Tried to document the YAML better. (Andrew)

- Added doit exact lookup by (component, id, name) via parse_request, so single-entry GET doesn't need a flat index.

- Added MAC loopback support via three new ethtool_ops callbacks (get_loopback(), get_loopback_by_index(), set_loopback()) with netdevsim implementation and tests.

- Added MAINTAINERS entry.

Opens/limitations
=================

- mlx5 CMIS support is still not part of the series.
- PHY loopback is defined in the UAPI but not yet implemented.
- No per-lane support -- loopback is all-or-nothing (0xff/0x00) across lanes.

Related work
============

[1] Generic loopback support, v1
https://lore.kernel.org/netdev/[email protected]/
[2] Generic loopback support, RFC v2
https://lore.kernel.org/netdev/[email protected]/
[3] New loopback modes
https://lore.kernel.org/netdev/[email protected]/
[4] PHY loopback
https://lore.kernel.org/netdev/[email protected]/
[5] bnxt_en: add .set_module_eeprom_by_page() support
https://lore.kernel.org/netdev/[email protected]/
[6] net/mlx5e: Implement set_module_eeprom_by_page ethtool callback
https://lore.kernel.org/netdev/[email protected]/

Björn Töpel (12):
ethtool: Add dump_one_dev callback for per-device sub-iteration
ethtool: Convert per-PHY commands to dump_one_dev
ethtool: Add loopback netlink UAPI definitions
ethtool: Add loopback GET/SET netlink implementation
ethtool: Add CMIS loopback helpers for module loopback control
selftests: drv-net: Add loopback driver test
ethtool: Add MAC loopback support via ethtool_ops
netdevsim: Add MAC loopback simulation
selftests: drv-net: Add MAC loopback netdevsim test
MAINTAINERS: Add entry for ethtool loopback
netdevsim: Add module EEPROM simulation via debugfs
selftests: drv-net: Add CMIS loopback netdevsim test

 Documentation/netlink/specs/ethtool.yaml      | 142 ++++++
 MAINTAINERS                                   |   6 +
 drivers/net/netdevsim/ethtool.c               | 147 +++++++
 drivers/net/netdevsim/netdevsim.h             |  15 +
 include/linux/ethtool.h                       |  28 ++
 .../uapi/linux/ethtool_netlink_generated.h    |  67 +++
 net/ethtool/Makefile                          |   2 +-
 net/ethtool/cmis_loopback.c                   | 407 ++++++++++++++++++
 net/ethtool/loopback.c                        | 351 +++++++++++++++
 net/ethtool/mse.c                             |   1 +
 net/ethtool/netlink.c                         | 284 ++++--------
 net/ethtool/netlink.h                         |  49 +++
 net/ethtool/phy.c                             |   1 +
 net/ethtool/plca.c                            |   2 +
 net/ethtool/pse-pd.c                          |   1 +
 .../testing/selftests/drivers/net/hw/Makefile |   2 +
 .../selftests/drivers/net/hw/loopback_drv.py  | 227 ++++++++++
 .../selftests/drivers/net/hw/loopback_nsim.py | 343 +++++++++++++++
 18 files changed, 1865 insertions(+), 210 deletions(-)
 create mode 100644 net/ethtool/cmis_loopback.c
 create mode 100644 net/ethtool/loopback.c
 create mode 100755 tools/testing/selftests/drivers/net/hw/loopback_drv.py
 create mode 100755 tools/testing/selftests/drivers/net/hw/loopback_nsim.py

base-commit: d1e59a46973719e458bec78d00dd767d7a7ba71f

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/12 ethtool: Add dump_one_dev callback for per-device sub-iteration

Add the dump_one_dev callback to ethnl_request_ops, allowing commands to provide custom per-device dump logic with sub-positioning. Extend ethnl_dump_ctx with ifindex and pos_sub fields.

No functional change; no command uses dump_one_dev yet.

---
 net/ethtool/netlink.c | 66 ++++++++++++++++++-------------------------
 net/ethtool/netlink.h | 31 ++++++++++++++++++++
 2 files changed, 58 insertions(+), 39 deletions(-)
net/ethtool/netlink.c +27 -39
index 5046023a30b1..8d161f0882d0 100644
@@ -346,36 +346,6 @@ int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
 
 /* GET request helpers */
 
-/**
- * struct ethnl_dump_ctx - context structure for generic dumpit() callback
- * @ops:        request ops of currently processed message type
- * @req_info:   parsed request header of processed request
- * @reply_data: data needed to compose the reply
- * @pos_ifindex: saved iteration position - ifindex
- *
- * These parameters are kept in struct netlink_callback as context preserved
- * between iterations. They are initialized by ethnl_default_start() and used
- * in ethnl_default_dumpit() and ethnl_default_done().
- */
-struct ethnl_dump_ctx {
-	const struct ethnl_request_ops	*ops;
-	struct ethnl_req_info		*req_info;
-	struct ethnl_reply_data		*reply_data;
-	unsigned long			pos_ifindex;
-};
-
-/**
- * struct ethnl_perphy_dump_ctx - context for dumpit() PHY-aware callbacks
- * @ethnl_ctx: generic ethnl context
- * @ifindex: For Filtered DUMP requests, the ifindex of the targeted netdev
- * @pos_phyindex: iterator position for multi-msg DUMP
- */
-struct ethnl_perphy_dump_ctx {
-	struct ethnl_dump_ctx	ethnl_ctx;
-	unsigned int		ifindex;
-	unsigned long		pos_phyindex;
-};
-
 static const struct ethnl_request_ops *
 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
 	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
@@ -618,6 +588,7 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
 				struct netlink_callback *cb)
 {
 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
+	const struct genl_info *info = genl_info_dump(cb);
 	struct net *net = sock_net(skb->sk);
 	netdevice_tracker dev_tracker;
 	struct net_device *dev;
@@ -625,10 +596,20 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
 
 	rcu_read_lock();
 	for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
+		if (ctx->ifindex && ctx->ifindex != ctx->pos_ifindex)
+			break;
+
 		netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
 		rcu_read_unlock();
 
-		ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
+		if (ctx->ops->dump_one_dev) {
+			ctx->req_info->dev = dev;
+			ret = ctx->ops->dump_one_dev(skb, ctx, &ctx->pos_sub,
+						     info);
+			ctx->req_info->dev = NULL;
+		} else {
+			ret = ethnl_default_dump_one(skb, dev, ctx, info);
+		}
 
 		rcu_read_lock();
 		netdev_put(dev, &dev_tracker);
@@ -674,19 +655,26 @@ static int ethnl_default_start(struct netlink_callback *cb)
 	ret = ethnl_default_parse(req_info, &info->info, ops, false);
 	if (ret < 0)
 		goto free_reply_data;
-	if (req_info->dev) {
-		/* We ignore device specification in dump requests but as the
-		 * same parser as for non-dump (doit) requests is used, it
-		 * would take reference to the device if it finds one
-		 */
-		netdev_put(req_info->dev, &req_info->dev_tracker);
-		req_info->dev = NULL;
-	}
 
 	ctx->ops = ops;
 	ctx->req_info = req_info;
 	ctx->reply_data = reply_data;
 	ctx->pos_ifindex = 0;
+	ctx->ifindex = 0;
+	ctx->pos_sub = 0;
+
+	if (req_info->dev) {
+		if (ops->dump_one_dev) {
+			/* Sub-iterator dumps keep track of the dev's ifindex
+			 * so the dumpit handler can grab/release the netdev
+			 * per iteration.
+			 */
+			ctx->ifindex = req_info->dev->ifindex;
+			ctx->pos_ifindex = ctx->ifindex;
+		}
+		netdev_put(req_info->dev, &req_info->dev_tracker);
+		req_info->dev = NULL;
+	}
 
 	return 0;
 
net/ethtool/netlink.h +31 -0
index aaf6f2468768..e01adc5db02f 100644
@@ -10,6 +10,28 @@
 
 struct ethnl_req_info;
 
+/**
+ * struct ethnl_dump_ctx - context structure for generic dumpit() callback
+ * @ops:        request ops of currently processed message type
+ * @req_info:   parsed request header of processed request
+ * @reply_data: data needed to compose the reply
+ * @pos_ifindex: saved iteration position - ifindex
+ * @ifindex:    for filtered dump requests, the ifindex of the targeted netdev
+ * @pos_sub:    iterator position for per-device iteration
+ *
+ * These parameters are kept in struct netlink_callback as context preserved
+ * between iterations. They are initialized by ethnl_default_start() and used
+ * in ethnl_default_dumpit() and ethnl_default_done().
+ */
+struct ethnl_dump_ctx {
+	const struct ethnl_request_ops	*ops;
+	struct ethnl_req_info		*req_info;
+	struct ethnl_reply_data		*reply_data;
+	unsigned long			pos_ifindex;
+	unsigned int			ifindex;
+	unsigned long			pos_sub;
+};
+
 u32 ethnl_bcast_seq_next(void);
 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
 			       const struct nlattr *nest, struct net *net,
@@ -365,6 +387,10 @@ int ethnl_sock_priv_set(struct sk_buff *skb, struct net_device *dev, u32 portid,
  *	used e.g. to free any additional data structures outside the main
  *	structure which were allocated by ->prepare_data(). When processing
  *	dump requests, ->cleanup() is called for each message.
+ * @dump_one_dev:
+ *	Optional callback for dumping data for a single device. When set,
+ *	overrides the default dump behavior for GET requests, allowing
+ *	per-device iteration with sub-positioning via @pos_sub.
  * @set_validate:
  *	Check if set operation is supported for a given device, and perform
  *	extra input checks. Expected return values:
@@ -409,6 +435,11 @@ struct ethnl_request_ops {
 			  const struct ethnl_reply_data *reply_data);
 	void (*cleanup_data)(struct ethnl_reply_data *reply_data);
 
+	int (*dump_one_dev)(struct sk_buff *skb,
+			    struct ethnl_dump_ctx *ctx,
+			    unsigned long *pos_sub,
+			    const struct genl_info *info);
+
 	int (*set_validate)(struct ethnl_req_info *req_info,
 			    struct genl_info *info);
 	int (*set)(struct ethnl_req_info *req_info,
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 2/12 ethtool: Convert per-PHY commands to dump_one_dev

In reply to Björn Töpel

Convert PSE, PLCA, PHY, and MSE commands from the separate ethnl_perphy_{start,dumpit,done} handlers to use the generic dump_one_dev callback. This removes the per-PHY specific dump infrastructure (ethnl_perphy_dump_ctx, ethnl_perphy_dump_context, ethnl_perphy_start, ethnl_perphy_dumpit, ethnl_perphy_done, and the internal helpers) in favor of a shared ethnl_perphy_dump_one_dev() function.

---
 net/ethtool/mse.c     |   1 +
 net/ethtool/netlink.c | 194 ++++++------------------------------------
 net/ethtool/netlink.h |   4 +
 net/ethtool/phy.c     |   1 +
 net/ethtool/plca.c    |   2 +
 net/ethtool/pse-pd.c  |   1 +
 6 files changed, 35 insertions(+), 168 deletions(-)
net/ethtool/mse.c +1 -0
index e91b74430f76..3f33182283ce 100644
@@ -325,4 +325,5 @@ const struct ethnl_request_ops ethnl_mse_request_ops = {
 	.cleanup_data = mse_cleanup_data,
 	.reply_size = mse_reply_size,
 	.fill_reply = mse_fill_reply,
+	.dump_one_dev = ethnl_perphy_dump_one_dev,
 };
net/ethtool/netlink.c +26 -168

252 lines not on this page · raw message

net/ethtool/netlink.h +4 -0
index e01adc5db02f..dda2f5593ed9 100644
@@ -546,6 +546,10 @@ int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int ethnl_tsinfo_done(struct netlink_callback *cb);
 int ethnl_rss_create_doit(struct sk_buff *skb, struct genl_info *info);
 int ethnl_rss_delete_doit(struct sk_buff *skb, struct genl_info *info);
+int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
+			      struct ethnl_dump_ctx *ctx,
+			      unsigned long *pos_sub,
+			      const struct genl_info *info);
 
 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
net/ethtool/phy.c +1 -0
index d4e6887055ab..4bb23a5d6ad5 100644
@@ -162,4 +162,5 @@ const struct ethnl_request_ops ethnl_phy_request_ops = {
 	.reply_size		= phy_reply_size,
 	.fill_reply		= phy_fill_reply,
 	.cleanup_data		= phy_cleanup_data,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 };
net/ethtool/plca.c +2 -0
index 91f0c4233298..84e902532617 100644
@@ -188,6 +188,7 @@ const struct ethnl_request_ops ethnl_plca_cfg_request_ops = {
 	.prepare_data		= plca_get_cfg_prepare_data,
 	.reply_size		= plca_get_cfg_reply_size,
 	.fill_reply		= plca_get_cfg_fill_reply,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 
 	.set			= ethnl_set_plca,
 	.set_ntf_cmd		= ETHTOOL_MSG_PLCA_NTF,
@@ -268,4 +269,5 @@ const struct ethnl_request_ops ethnl_plca_status_request_ops = {
 	.prepare_data		= plca_get_status_prepare_data,
 	.reply_size		= plca_get_status_reply_size,
 	.fill_reply		= plca_get_status_fill_reply,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 };
net/ethtool/pse-pd.c +1 -0
index 2eb9bdc2dcb9..83f0205053a3 100644
@@ -338,6 +338,7 @@ const struct ethnl_request_ops ethnl_pse_request_ops = {
 	.reply_size		= pse_reply_size,
 	.fill_reply		= pse_fill_reply,
 	.cleanup_data		= pse_cleanup_data,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 
 	.set			= ethnl_set_pse,
 	/* PSE has no notification */
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 3/12 ethtool: Add loopback netlink UAPI definitions

In reply to Björn Töpel

Add the netlink YAML spec and auto-generated UAPI header for a unified loopback interface covering MAC, PHY, and pluggable module components.

Each loopback point is described by a nested entry attribute containing:

 - component  where in the path (MAC, PHY, MODULE)
 - name       subsystem label, e.g. "cmis-host" or "cmis-media"
 - id         optional instance selector (e.g. PHY id, port id)
 - depth      ordering index within a component (0 = first/only)
 - supported  bitmask of supported directions
 - direction  LOCAL, REMOTE, or 0 (disabled)
---
 Documentation/netlink/specs/ethtool.yaml      | 142 ++++++++++++++++++
 .../uapi/linux/ethtool_netlink_generated.h    |  67 +++++++++
 2 files changed, 209 insertions(+)
Documentation/netlink/specs/ethtool.yaml +142 -0
index 5dd4d1b5d94b..fe7e644d1b10 100644
@@ -211,6 +211,58 @@ definitions:
         name: discard
         value: 31
 
+  -
+    name: loopback-component
+    type: enum
+    doc: |
+      Loopback component. Identifies where in the network path the
+      loopback is applied.
+    entries:
+      -
+        name: mac
+        doc: |
+          MAC component loopback. Covers loopback points owned by the
+          MAC / network-controller driver, including the MAC block itself,
+          any MAC-side PCS, and SoC SerDes. The name attribute identifies
+          the sublayer using IEEE 802.3 vocabulary (e.g. mac, pcs, mii).
+      -
+        name: phy
+        doc: |
+          Ethernet PHY loopback. Covers loopback points inside the
+          Ethernet PHY managed by phylib, such as the PHY-internal PCS,
+          PMA, or PMD. The name attribute identifies the sublayer using
+          IEEE 802.3 vocabulary (e.g. pcs, pma, pmd, mii). A Base-T SFP
+          module containing an Ethernet PHY driven by Linux should report
+          loopback under this component, not module.
+      -
+        name: module
+        doc: |
+          Pluggable module (e.g. CMIS (Q)SFP) loopback. Covers loopback
+          modes controlled via module firmware or EEPROM registers. When
+          Linux drives an Ethernet PHY inside the module via phylib, use
+          the phy component instead.
+  -
+    name: loopback-direction
+    type: flags
+    doc: |
+      Loopback direction flags. Direction is defined from the
+      component's own viewpoint: local loops traffic originating from
+      the host back to the host, remote loops traffic arriving from
+      the line back toward the line. This convention holds regardless
+      of where the component sits in the system topology. Used as a
+      bitmask in supported, and as a single value in direction.
+    entries:
+      -
+        name: local
+        doc: |
+          Local loopback (IEEE 802.3). Host TX -> looped back -> host
+          RX (traffic originating from host returns to host).
+      -
+        name: remote
+        doc: |
+          Remote loopback (IEEE 802.3). Line RX -> looped back -> line
+          TX (traffic from far end returns to far end).
+
 attribute-sets:
   -
     name: header
@@ -1905,6 +1957,68 @@ attribute-sets:
         name: link
         type: nest
         nested-attributes: mse-snapshot
+  -
+    name: loopback-entry
+    doc: Per-component loopback configuration entry.
+    attr-cnt-name: __ethtool-a-loopback-entry-cnt
+    attributes:
+      -
+        name: unspec
+        type: unused
+        value: 0
+      -
+        name: component
+        type: u32
+        enum: loopback-component
+        doc: Loopback component
+      -
+        name: name
+        type: string
+        doc: |
+          Subsystem-specific name for the loopback point within the
+          component.
+      -
+        name: id
+        type: u32
+        doc: Optional component instance identifier.
+      -
+        name: depth
+        type: u8
+        doc: |
+          Ordering index within a component instance. When a component
+          has multiple loopback points of the same type (e.g. two PCS
+          blocks inside a rate-adaptation PHY), depth distinguishes
+          them. Lower depth values are closer to the host side, higher
+          values are closer to the line/media side. Defaults to 0 when
+          there is only one loopback point per (component, name) tuple.
+      -
+        name: supported
+        type: u8
+        enum: loopback-direction
+        enum-as-flags: true
+        doc: Bitmask of supported loopback directions
+      -
+        name: direction
+        type: u8
+        enum: loopback-direction
+        doc: Current loopback direction, 0 means disabled
+  -
+    name: loopback
+    attr-cnt-name: __ethtool-a-loopback-cnt
+    attributes:
+      -
+        name: unspec
+        type: unused
+        value: 0
+      -
+        name: header
+        type: nest
+        nested-attributes: header
+      -
+        name: entry
+        type: nest
+        multi-attr: true
+        nested-attributes: loopback-entry
 
 operations:
   enum-model: directional
@@ -2859,6 +2973,34 @@ operations:
             - worst-channel
             - link
       dump: *mse-get-op
+    -
+      name: loopback-get
+      doc: Get loopback configuration and capabilities.
+
+      attribute-set: loopback
+
+      do: &loopback-get-op
+        request:
+          attributes:
+            - header
+        reply:
+          attributes: &loopback
+            - header
+            - entry
+      dump: *loopback-get-op
+    -
+      name: loopback-set
+      doc: Set loopback configuration.
+
+      attribute-set: loopback
+
+      do:
+        request:
+          attributes: *loopback
+    -
+      name: loopback-ntf
+      doc: Notification for change in loopback configuration.
+      notify: loopback-get
 
 mcast-groups:
   list:
include/uapi/linux/ethtool_netlink_generated.h +67 -0

91 lines not on this page · raw message

Signature · 1 line
2.53.0
#11 lore raw

PATCH v2 4/12 ethtool: Add loopback GET/SET netlink implementation

In reply to Björn Töpel

Add the kernel-side ETHTOOL_MSG_LOOPBACK_GET, ETHTOOL_MSG_LOOPBACK_SET, and ETHTOOL_MSG_LOOPBACK_NTF handlers using the standard ethnl_request_ops infrastructure.

GET collects loopback entries from per-component helpers via loopback_get_entries(). SET parses the nested entry attributes, dispatches each to loopback_set_one(), and only sends a notification when the state is changed.

No components are wired yet.

---
 include/linux/ethtool.h |  18 +++
 net/ethtool/Makefile    |   2 +-
 net/ethtool/loopback.c  | 315 ++++++++++++++++++++++++++++++++++++++++
 net/ethtool/netlink.c   |  24 ++-
 net/ethtool/netlink.h   |   6 +
 5 files changed, 362 insertions(+), 3 deletions(-)
 create mode 100644 net/ethtool/loopback.c
include/linux/ethtool.h +18 -0
index 1cb0740ba331..81a9c186564d 100644
@@ -860,6 +860,24 @@ void ethtool_mmsv_set_mm(struct ethtool_mmsv *mmsv, struct ethtool_mm_cfg *cfg);
 void ethtool_mmsv_init(struct ethtool_mmsv *mmsv, struct net_device *dev,
 		       const struct ethtool_mmsv_ops *ops);
 
+/**
+ * struct ethtool_loopback_entry - Per-component loopback configuration
+ * @component: Loopback component
+ * @name: Subsystem-specific name for the loopback point
+ * @id: Optional component instance identifier, 0 means not specified
+ * @depth: Ordering index within a component instance, 0 means first/only
+ * @supported: Bitmask of supported directions
+ * @direction: Current loopback direction, 0 means disabled
+ */
+struct ethtool_loopback_entry {
+	enum ethtool_loopback_component component;
+	char name[ETH_GSTRING_LEN];
+	u32 id;
+	u8 depth;
+	u8 supported;
+	u8 direction;
+};
+
 /**
  * struct ethtool_rxfh_param - RXFH (RSS) parameters
  * @hfunc: Defines the current RSS hash function used by HW (or to be set to).
net/ethtool/Makefile +1 -1
index 629c10916670..ef534b55d724 100644
@@ -9,4 +9,4 @@ ethtool_nl-y	:= netlink.o bitset.o strset.o linkinfo.o linkmodes.o rss.o \
 		   channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \
 		   tunnels.o fec.o eeprom.o stats.o phc_vclocks.o mm.o \
 		   module.o cmis_fw_update.o cmis_cdb.o pse-pd.o plca.o \
-		   phy.o tsconfig.o mse.o
+		   phy.o tsconfig.o mse.o loopback.o
net/ethtool/loopback.c new file +315 -0

315 lines not on this page · raw message

net/ethtool/netlink.c +22 -2
index edeeca67918a..d542978a683e 100644
@@ -392,6 +392,8 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
 	[ETHTOOL_MSG_TSCONFIG_SET]	= &ethnl_tsconfig_request_ops,
 	[ETHTOOL_MSG_PHY_GET]		= &ethnl_phy_request_ops,
 	[ETHTOOL_MSG_MSE_GET]		= &ethnl_mse_request_ops,
+	[ETHTOOL_MSG_LOOPBACK_GET]	= &ethnl_loopback_request_ops,
+	[ETHTOOL_MSG_LOOPBACK_SET]	= &ethnl_loopback_request_ops,
 };
 
 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -539,8 +541,8 @@ static int ethnl_default_doit(struct sk_buff *skb, struct genl_info *info)
 	return ret;
 }
 
-static int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
-				  const struct ethnl_dump_ctx *ctx,
+int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
+			   const struct ethnl_dump_ctx *ctx,
 				  const struct genl_info *info)
 {
 	void *ehdr;
@@ -810,6 +812,7 @@ ethnl_default_notify_ops[ETHTOOL_MSG_KERNEL_MAX + 1] = {
 	[ETHTOOL_MSG_MM_NTF]		= &ethnl_mm_request_ops,
 	[ETHTOOL_MSG_RSS_NTF]		= &ethnl_rss_request_ops,
 	[ETHTOOL_MSG_RSS_CREATE_NTF]	= &ethnl_rss_request_ops,
+	[ETHTOOL_MSG_LOOPBACK_NTF]	= &ethnl_loopback_request_ops,
 };
 
 /* default notification handler */
@@ -918,6 +921,7 @@ static const ethnl_notify_handler_t ethnl_notify_handlers[] = {
 	[ETHTOOL_MSG_MM_NTF]		= ethnl_default_notify,
 	[ETHTOOL_MSG_RSS_NTF]		= ethnl_default_notify,
 	[ETHTOOL_MSG_RSS_CREATE_NTF]	= ethnl_default_notify,
+	[ETHTOOL_MSG_LOOPBACK_NTF]	= ethnl_default_notify,
 };
 
 void ethnl_notify(struct net_device *dev, unsigned int cmd,
@@ -1392,6 +1396,22 @@ static const struct genl_ops ethtool_genl_ops[] = {
 		.policy = ethnl_mse_get_policy,
 		.maxattr = ARRAY_SIZE(ethnl_mse_get_policy) - 1,
 	},
+	{
+		.cmd	= ETHTOOL_MSG_LOOPBACK_GET,
+		.doit	= ethnl_default_doit,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
+		.policy = ethnl_loopback_get_policy,
+		.maxattr = ARRAY_SIZE(ethnl_loopback_get_policy) - 1,
+	},
+	{
+		.cmd	= ETHTOOL_MSG_LOOPBACK_SET,
+		.flags	= GENL_UNS_ADMIN_PERM,
+		.doit	= ethnl_default_set_doit,
+		.policy = ethnl_loopback_set_policy,
+		.maxattr = ARRAY_SIZE(ethnl_loopback_set_policy) - 1,
+	},
 };
 
 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
net/ethtool/netlink.h +6 -0
index dda2f5593ed9..693df12eef63 100644
@@ -475,6 +475,7 @@ extern const struct ethnl_request_ops ethnl_mm_request_ops;
 extern const struct ethnl_request_ops ethnl_phy_request_ops;
 extern const struct ethnl_request_ops ethnl_tsconfig_request_ops;
 extern const struct ethnl_request_ops ethnl_mse_request_ops;
+extern const struct ethnl_request_ops ethnl_loopback_request_ops;
 
 extern const struct nla_policy ethnl_header_policy[ETHTOOL_A_HEADER_FLAGS + 1];
 extern const struct nla_policy ethnl_header_policy_stats[ETHTOOL_A_HEADER_FLAGS + 1];
@@ -531,6 +532,8 @@ extern const struct nla_policy ethnl_phy_get_policy[ETHTOOL_A_PHY_HEADER + 1];
 extern const struct nla_policy ethnl_tsconfig_get_policy[ETHTOOL_A_TSCONFIG_HEADER + 1];
 extern const struct nla_policy ethnl_tsconfig_set_policy[ETHTOOL_A_TSCONFIG_MAX + 1];
 extern const struct nla_policy ethnl_mse_get_policy[ETHTOOL_A_MSE_HEADER + 1];
+extern const struct nla_policy ethnl_loopback_get_policy[ETHTOOL_A_LOOPBACK_MAX + 1];
+extern const struct nla_policy ethnl_loopback_set_policy[ETHTOOL_A_LOOPBACK_MAX + 1];
 
 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
 int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
@@ -546,6 +549,9 @@ int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int ethnl_tsinfo_done(struct netlink_callback *cb);
 int ethnl_rss_create_doit(struct sk_buff *skb, struct genl_info *info);
 int ethnl_rss_delete_doit(struct sk_buff *skb, struct genl_info *info);
+int ethnl_default_dump_one(struct sk_buff *skb, struct net_device *dev,
+			   const struct ethnl_dump_ctx *ctx,
+			   const struct genl_info *info);
 int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
 			      struct ethnl_dump_ctx *ctx,
 			      unsigned long *pos_sub,
Signature · 1 line
2.53.0
#12 lore raw

PATCH v2 5/12 ethtool: Add CMIS loopback helpers for module loopback control

In reply to Björn Töpel

Add CMIS loopback functions and wire them into loopback.c for the MODULE component:

- ethtool_cmis_get_loopback(): reads Page 13h capabilities and current state, appends one entry per supported loopback point ("cmis-host" and/or "cmis-media").

- ethtool_cmis_get_loopback_by_index(): used to enumerate what the module supports.

- ethtool_cmis_set_loopback(): resolves name to a pair of control byte indices, validates direction, and writes the Page 13h control bytes (0xff = all lanes on, 0x00 = off).

Directions are mutually exclusive: switching from local to remote first disables the active direction in a separate EEPROM write, then enables the new one. Requesting multiple direction flags is rejected.

CMIS register mapping (Page 13h, Bytes 180-183):

 - MODULE, "cmis-host",  local    ->  Host Side Input    (Byte 183)
 - MODULE, "cmis-host",  remote   ->  Host Side Output   (Byte 182)
 - MODULE, "cmis-media", local    ->  Media Side Input   (Byte 181)
 - MODULE, "cmis-media", remote   ->  Media Side Output  (Byte 180)

The helpers work entirely over get/set_module_eeprom_by_page, so any driver with EEPROM page access gets module loopback without new ethtool_ops or driver changes. SET is rejected when firmware flashing is in progress or the interface is UP.

---
 net/ethtool/Makefile        |   2 +-
 net/ethtool/cmis_loopback.c | 407 ++++++++++++++++++++++++++++++++++++
 net/ethtool/loopback.c      |   6 +-
 net/ethtool/netlink.h       |   8 +
 4 files changed, 421 insertions(+), 2 deletions(-)
 create mode 100644 net/ethtool/cmis_loopback.c
net/ethtool/Makefile +1 -1
index ef534b55d724..2f821c7875e1 100644
@@ -9,4 +9,4 @@ ethtool_nl-y	:= netlink.o bitset.o strset.o linkinfo.o linkmodes.o rss.o \
 		   channels.o coalesce.o pause.o eee.o tsinfo.o cabletest.o \
 		   tunnels.o fec.o eeprom.o stats.o phc_vclocks.o mm.o \
 		   module.o cmis_fw_update.o cmis_cdb.o pse-pd.o plca.o \
-		   phy.o tsconfig.o mse.o loopback.o
+		   phy.o tsconfig.o mse.o loopback.o cmis_loopback.o
net/ethtool/cmis_loopback.c new file +407 -0

407 lines not on this page · raw message

net/ethtool/loopback.c +5 -1
index 03b86662dc0f..2d0ad62ce42f 100644
@@ -88,6 +88,8 @@ static int loopback_get(struct net_device *dev,
 			struct ethtool_loopback_entry *entry)
 {
 	switch (component) {
+	case ETHTOOL_LOOPBACK_COMPONENT_MODULE:
+		return ethtool_cmis_get_loopback(dev, name, entry);
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -96,7 +98,7 @@ static int loopback_get(struct net_device *dev,
 static int loopback_get_by_index(struct net_device *dev, u32 index,
 				 struct ethtool_loopback_entry *entry)
 {
-	return -EOPNOTSUPP;
+	return ethtool_cmis_get_loopback_by_index(dev, index, entry);
 }
 
 static int loopback_prepare_data(const struct ethnl_req_info *req_base,
@@ -233,6 +235,8 @@ static int __loopback_set(struct net_device *dev,
 			  struct netlink_ext_ack *extack)
 {
 	switch (entry->component) {
+	case ETHTOOL_LOOPBACK_COMPONENT_MODULE:
+		return ethtool_cmis_set_loopback(dev, entry, extack);
 	default:
 		return -EOPNOTSUPP;
 	}
net/ethtool/netlink.h +8 -0
index 693df12eef63..1017d653673d 100644
@@ -556,6 +556,14 @@ int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
 			      struct ethnl_dump_ctx *ctx,
 			      unsigned long *pos_sub,
 			      const struct genl_info *info);
+int ethtool_cmis_get_loopback_by_index(struct net_device *dev, u32 index,
+				       struct ethtool_loopback_entry *entry);
+int ethtool_cmis_get_loopback(struct net_device *dev,
+			      const char *name,
+			      struct ethtool_loopback_entry *entry);
+int ethtool_cmis_set_loopback(struct net_device *dev,
+			      const struct ethtool_loopback_entry *entry,
+			      struct netlink_ext_ack *extack);
 
 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
Signature · 1 line
2.53.0
#13 lore raw

PATCH v2 6/12 selftests: drv-net: Add loopback driver test

In reply to Björn Töpel

Add a selftest for the ethtool loopback UAPI exercising module loopback via the loopback GET/SET netlink commands.

Works on any device that reports module loopback entries. Tests cover enable local and remote, disable, direction switching (mutual exclusivity), idempotent enable, and rejection while interface is up. Devices without module loopback support are skipped.

---
 .../testing/selftests/drivers/net/hw/Makefile |   1 +
 .../selftests/drivers/net/hw/loopback_drv.py  | 227 ++++++++++++++++++
 2 files changed, 228 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/hw/loopback_drv.py
tools/testing/selftests/drivers/net/hw/Makefile +1 -0
index 3c97dac9baaa..5a6037a71f8f 100644
@@ -32,6 +32,7 @@ TEST_PROGS = \
 	iou-zcrx.py \
 	irq.py \
 	loopback.sh \
+	loopback_drv.py \
 	nic_timestamp.py \
 	nk_netns.py \
 	pp_alloc_fail.py \
tools/testing/selftests/drivers/net/hw/loopback_drv.py new file +227 -0

227 lines not on this page · raw message

Signature · 1 line
2.53.0
#14 lore raw

PATCH v2 7/12 ethtool: Add MAC loopback support via ethtool_ops

In reply to Björn Töpel

Extend struct ethtool_ops with three loopback callbacks that drivers can implement for MAC-level loopback control:

- get_loopback(dev, name, id, entry): exact lookup by name and instance id, used by doit (single-entry GET) requests.

- get_loopback_by_index(dev, index, entry): flat enumeration by index, used by dumpit (multi-entry GET) requests to iterate all loopback points on a device.

- set_loopback(dev, entry, extack): apply a loopback configuration change. Returns 1 if hardware state changed, 0 if no-op.

Wire the MAC component into loopback.c's dispatch functions. For dump enumeration, MAC entries are tried first via the driver's get_loopback_by_index() op, then MODULE/CMIS entries follow at the next index offset.

---
 include/linux/ethtool.h | 10 ++++++++
 net/ethtool/loopback.c  | 56 ++++++++++++++++++++++++++++++++---------
 2 files changed, 54 insertions(+), 12 deletions(-)
include/linux/ethtool.h +10 -0
index 81a9c186564d..971be759a915 100644
@@ -1168,6 +1168,9 @@ struct kernel_ethtool_ts_info {
  * @get_mm: Query the 802.3 MAC Merge layer state.
  * @set_mm: Set the 802.3 MAC Merge layer parameters.
  * @get_mm_stats: Query the 802.3 MAC Merge layer statistics.
+ * @get_loopback: Get the state of a loopback entry identified by name and id.
+ * @get_loopback_by_index: Get the state of a loopback entry by its index.
+ * @set_loopback: Set the loopback mode for a given entry.
  *
  * All operations are optional (i.e. the function pointer may be set
  * to %NULL) and callers must take this into account.  Callers must
@@ -1337,6 +1340,13 @@ struct ethtool_ops {
 	int	(*set_mm)(struct net_device *dev, struct ethtool_mm_cfg *cfg,
 			  struct netlink_ext_ack *extack);
 	void	(*get_mm_stats)(struct net_device *dev, struct ethtool_mm_stats *stats);
+	int	(*get_loopback)(struct net_device *dev, const char *name,
+				u32 id, struct ethtool_loopback_entry *entry);
+	int	(*get_loopback_by_index)(struct net_device *dev, u32 index,
+					 struct ethtool_loopback_entry *entry);
+	int	(*set_loopback)(struct net_device *dev,
+				const struct ethtool_loopback_entry *entry,
+				struct netlink_ext_ack *extack);
 };
 
 int ethtool_check_ops(const struct ethtool_ops *ops);
net/ethtool/loopback.c +44 -12

93 lines not on this page · raw message

Signature · 1 line
2.53.0
#16 lore raw

PATCH v2 8/12 netdevsim: Add MAC loopback simulation

In reply to Björn Töpel

Implement the three ethtool loopback ops for MAC-level loopback simulation:

- get_loopback(): exact lookup by name ("mac")
- get_loopback_by()_index: enumerate (single entry at index 0)
- set_loopback(): update direction, return 1 if changed

The MAC loopback entry announces support for both local and remote support by default. State is stored in nsim_ethtool.mac_lb and exposed via debugfs under ethtool/mac_lb/{supported,direction} for test inspection and control.

---
 drivers/net/netdevsim/ethtool.c   | 64 +++++++++++++++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h |  4 ++
 2 files changed, 68 insertions(+)
drivers/net/netdevsim/ethtool.c +64 -0
index 36a201533aae..39c681284700 100644
@@ -195,6 +195,58 @@ nsim_get_fec_stats(struct net_device *dev, struct ethtool_fec_stats *fec_stats,
 	values[2].per_lane[3] = 0;
 }
 
+static void nsim_fill_mac_lb_entry(struct netdevsim *ns,
+				   struct ethtool_loopback_entry *entry)
+{
+	memset(entry, 0, sizeof(*entry));
+	entry->component = ETHTOOL_LOOPBACK_COMPONENT_MAC;
+	strscpy(entry->name, "mac", sizeof(entry->name));
+	entry->supported = ns->ethtool.mac_lb.supported;
+	entry->direction = ns->ethtool.mac_lb.direction;
+}
+
+static int nsim_get_loopback(struct net_device *dev, const char *name,
+			     u32 id, struct ethtool_loopback_entry *entry)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	if (strcmp(name, "mac"))
+		return -EOPNOTSUPP;
+
+	nsim_fill_mac_lb_entry(ns, entry);
+	return 0;
+}
+
+static int nsim_get_loopback_by_index(struct net_device *dev, u32 index,
+				      struct ethtool_loopback_entry *entry)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	if (index > 0)
+		return -EOPNOTSUPP;
+
+	nsim_fill_mac_lb_entry(ns, entry);
+	return 0;
+}
+
+static int nsim_set_loopback(struct net_device *dev,
+			     const struct ethtool_loopback_entry *entry,
+			     struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	if (strcmp(entry->name, "mac")) {
+		NL_SET_ERR_MSG(extack, "Unknown MAC loopback name");
+		return -EOPNOTSUPP;
+	}
+
+	if (ns->ethtool.mac_lb.direction == entry->direction)
+		return 0;
+
+	ns->ethtool.mac_lb.direction = entry->direction;
+	return 1;
+}
+
 static int nsim_get_ts_info(struct net_device *dev,
 			    struct kernel_ethtool_ts_info *info)
 {
@@ -222,6 +274,9 @@ static const struct ethtool_ops nsim_ethtool_ops = {
 	.set_fecparam			= nsim_set_fecparam,
 	.get_fec_stats			= nsim_get_fec_stats,
 	.get_ts_info			= nsim_get_ts_info,
+	.get_loopback			= nsim_get_loopback,
+	.get_loopback_by_index		= nsim_get_loopback_by_index,
+	.set_loopback			= nsim_set_loopback,
 };
 
 static void nsim_ethtool_ring_init(struct netdevsim *ns)
@@ -270,4 +325,13 @@ void nsim_ethtool_init(struct netdevsim *ns)
 			   &ns->ethtool.ring.rx_mini_max_pending);
 	debugfs_create_u32("tx_max_pending", 0600, dir,
 			   &ns->ethtool.ring.tx_max_pending);
+
+	ns->ethtool.mac_lb.supported = ETHTOOL_LOOPBACK_DIRECTION_LOCAL |
+				       ETHTOOL_LOOPBACK_DIRECTION_REMOTE;
+
+	dir = debugfs_create_dir("mac_lb", ethtool);
+	debugfs_create_u32("supported", 0600, dir,
+			   &ns->ethtool.mac_lb.supported);
+	debugfs_create_u32("direction", 0600, dir,
+			   &ns->ethtool.mac_lb.direction);
 }
drivers/net/netdevsim/netdevsim.h +4 -0
index c904e14f6b3f..7a3382562287 100644
@@ -90,6 +90,10 @@ struct nsim_ethtool {
 	struct ethtool_coalesce coalesce;
 	struct ethtool_ringparam ring;
 	struct ethtool_fecparam fec;
+	struct {
+		u32 supported;
+		u32 direction;
+	} mac_lb;
 };
 
 struct nsim_rq {
Signature · 1 line
2.53.0
#18 lore raw

PATCH v2 9/12 selftests: drv-net: Add MAC loopback netdevsim test

In reply to Björn Töpel

Add loopback_nsim.py with netdevsim-specific tests for MAC loopback via the new ethtool_ops loopback callbacks:

- test_get_mac_entry: verify MAC entry appears in dump with correct component, name, and supported directions
- test_set_mac_local: SET local, verify via GET and debugfs
- test_set_mac_disable: enable then disable
- test_set_mac_unknown_name: SET with wrong name, expect EOPNOTSUPP

---
 .../testing/selftests/drivers/net/hw/Makefile |   1 +
 .../selftests/drivers/net/hw/loopback_nsim.py | 138 ++++++++++++++++++
 2 files changed, 139 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/hw/loopback_nsim.py
tools/testing/selftests/drivers/net/hw/Makefile +1 -0
index 5a6037a71f8f..74b0e1937980 100644
@@ -33,6 +33,7 @@ TEST_PROGS = \
 	irq.py \
 	loopback.sh \
 	loopback_drv.py \
+	loopback_nsim.py \
 	nic_timestamp.py \
 	nk_netns.py \
 	pp_alloc_fail.py \
tools/testing/selftests/drivers/net/hw/loopback_nsim.py new file +138 -0

138 lines not on this page · raw message

Signature · 1 line
2.53.0
#21 lore raw

PATCH v2 10/12 MAINTAINERS: Add entry for ethtool loopback

In reply to Björn Töpel

Add a MAINTAINERS entry for the ethtool loopback subsystem covering the core loopback and CMIS loopback netlink implementation, and the associated selftests.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index a09bf30a057d..411c412975ed 100644
@@ -18389,6 +18389,12 @@ F:	net/ethtool/cabletest.c
 F:	tools/testing/selftests/drivers/net/*/ethtool*
 K:	cable_test
 
+NETWORKING [ETHTOOL LOOPBACK]
+M:	Björn Töpel <[email protected]>
+F:	net/ethtool/cmis_loopback.c
+F:	net/ethtool/loopback.c
+F:	tools/testing/selftests/drivers/net/hw/loopback*
+
 NETWORKING [ETHTOOL MAC MERGE]
 M:	Vladimir Oltean <[email protected]>
 F:	net/ethtool/mm.c
Signature · 1 line
2.53.0
#22 lore raw

PATCH v2 11/12 netdevsim: Add module EEPROM simulation via debugfs

In reply to Björn Töpel

Add get/set_module_eeprom_by_page ethtool ops to netdevsim, enabling testing of kernel features that depend on module EEPROM access (e.g. CMIS loopback) without real hardware.

The EEPROM is backed by a 256-page x 128-byte array exposed as binary debugfs files under ports/<N>/ethtool/module/pages/{0..255}. Offsets 0-127 map to page 0 (lower memory), 128-255 to the requested page's upper memory, following the CMIS layout. Error injection via get_err and set_err follows the existing netdevsim pattern.

---
 drivers/net/netdevsim/ethtool.c   | 83 +++++++++++++++++++++++++++++++
 drivers/net/netdevsim/netdevsim.h | 11 ++++
 2 files changed, 94 insertions(+)
drivers/net/netdevsim/ethtool.c +83 -0
index 39c681284700..1ef48802fcbd 100644
@@ -247,6 +247,68 @@ static int nsim_set_loopback(struct net_device *dev,
 	return 1;
 }
 
+static u8 *nsim_module_eeprom_ptr(struct netdevsim *ns,
+				  const struct ethtool_module_eeprom *page_data,
+				  u32 *len)
+{
+	u32 offset;
+	u8 page;
+
+	if (page_data->offset < NSIM_MODULE_EEPROM_PAGE_LEN) {
+		page = 0;
+		offset = page_data->offset;
+	} else {
+		page = page_data->page;
+		offset = page_data->offset - NSIM_MODULE_EEPROM_PAGE_LEN;
+	}
+
+	*len = min_t(u32, page_data->length,
+		     NSIM_MODULE_EEPROM_PAGE_LEN - offset);
+	return ns->ethtool.module.pages[page] + offset;
+}
+
+static int
+nsim_get_module_eeprom_by_page(struct net_device *dev,
+			       const struct ethtool_module_eeprom *page_data,
+			       struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+	u32 len;
+	u8 *ptr;
+
+	if (ns->ethtool.module.get_err)
+		return -ns->ethtool.module.get_err;
+
+	ptr = nsim_module_eeprom_ptr(ns, page_data, &len);
+	if (!ptr)
+		return -EINVAL;
+
+	memcpy(page_data->data, ptr, len);
+
+	return len;
+}
+
+static int
+nsim_set_module_eeprom_by_page(struct net_device *dev,
+			       const struct ethtool_module_eeprom *page_data,
+			       struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+	u32 len;
+	u8 *ptr;
+
+	if (ns->ethtool.module.set_err)
+		return -ns->ethtool.module.set_err;
+
+	ptr = nsim_module_eeprom_ptr(ns, page_data, &len);
+	if (!ptr)
+		return -EINVAL;
+
+	memcpy(ptr, page_data->data, len);
+
+	return 0;
+}
+
 static int nsim_get_ts_info(struct net_device *dev,
 			    struct kernel_ethtool_ts_info *info)
 {
@@ -274,6 +336,8 @@ static const struct ethtool_ops nsim_ethtool_ops = {
 	.set_fecparam			= nsim_set_fecparam,
 	.get_fec_stats			= nsim_get_fec_stats,
 	.get_ts_info			= nsim_get_ts_info,
+	.get_module_eeprom_by_page	= nsim_get_module_eeprom_by_page,
+	.set_module_eeprom_by_page	= nsim_set_module_eeprom_by_page,
 	.get_loopback			= nsim_get_loopback,
 	.get_loopback_by_index		= nsim_get_loopback_by_index,
 	.set_loopback			= nsim_set_loopback,
@@ -292,6 +356,7 @@ static void nsim_ethtool_ring_init(struct netdevsim *ns)
 void nsim_ethtool_init(struct netdevsim *ns)
 {
 	struct dentry *ethtool, *dir;
+	int i;
 
 	ns->netdev->ethtool_ops = &nsim_ethtool_ops;
 
@@ -326,6 +391,24 @@ void nsim_ethtool_init(struct netdevsim *ns)
 	debugfs_create_u32("tx_max_pending", 0600, dir,
 			   &ns->ethtool.ring.tx_max_pending);
 
+	dir = debugfs_create_dir("module", ethtool);
+	debugfs_create_u32("get_err", 0600, dir, &ns->ethtool.module.get_err);
+	debugfs_create_u32("set_err", 0600, dir, &ns->ethtool.module.set_err);
+
+	dir = debugfs_create_dir("pages", dir);
+	for (i = 0; i < NSIM_MODULE_EEPROM_PAGES; i++) {
+		char name[8];
+
+		ns->ethtool.module.page_blobs[i].data =
+			ns->ethtool.module.pages[i];
+		ns->ethtool.module.page_blobs[i].size =
+			NSIM_MODULE_EEPROM_PAGE_LEN;
+
+		snprintf(name, sizeof(name), "%u", i);
+		debugfs_create_blob(name, 0600, dir,
+				    &ns->ethtool.module.page_blobs[i]);
+	}
+
 	ns->ethtool.mac_lb.supported = ETHTOOL_LOOPBACK_DIRECTION_LOCAL |
 				       ETHTOOL_LOOPBACK_DIRECTION_REMOTE;
 
drivers/net/netdevsim/netdevsim.h +11 -0
index 7a3382562287..816e8ddd2551 100644
@@ -82,6 +82,16 @@ struct nsim_ethtool_pauseparam {
 	bool report_stats_tx;
 };
 
+#define NSIM_MODULE_EEPROM_PAGES	256
+#define NSIM_MODULE_EEPROM_PAGE_LEN	128
+
+struct nsim_ethtool_module {
+	u32 get_err;
+	u32 set_err;
+	u8 pages[NSIM_MODULE_EEPROM_PAGES][NSIM_MODULE_EEPROM_PAGE_LEN];
+	struct debugfs_blob_wrapper page_blobs[NSIM_MODULE_EEPROM_PAGES];
+};
+
 struct nsim_ethtool {
 	u32 get_err;
 	u32 set_err;
@@ -94,6 +104,7 @@ struct nsim_ethtool {
 		u32 supported;
 		u32 direction;
 	} mac_lb;
+	struct nsim_ethtool_module module;
 };
 
 struct nsim_rq {
Signature · 1 line
2.53.0
#23 lore raw

PATCH v2 12/12 selftests: drv-net: Add CMIS loopback netdevsim test

In reply to Björn Töpel

Extend loopback_nsim.py with netdevsim-specific tests for CMIS module loopback. These tests seed the EEPROM via debugfs and verify register-level behavior.

Tests cover: no-module GET, all/partial capability reporting, EEPROM byte verification for enable/disable and direction switching, rejection of unsupported directions, rejection without CMIS support, and combined MAC + MODULE dump.

---
 .../selftests/drivers/net/hw/loopback_nsim.py | 207 +++++++++++++++++-
 1 file changed, 206 insertions(+), 1 deletion(-)
tools/testing/selftests/drivers/net/hw/loopback_nsim.py +206 -1
index d05be09f6c14..f824f5f5d791 100755
@@ -1,10 +1,13 @@
 #!/usr/bin/env python3
 # SPDX-License-Identifier: GPL-2.0
 
-"""Netdevsim-specific tests for MAC loopback via ethtool_ops.
+"""Netdevsim-specific tests for ethtool loopback.
 
 Verifies that MAC loopback entries appear in dumps, that SET
 operations update state correctly (both via GET and debugfs).
+
+Seeds the CMIS EEPROM via debugfs and verifies register-level
+behavior that can only be checked with controlled EEPROM contents.
 """
 
 import errno
@@ -15,6 +18,20 @@ from lib.py import KsftFailEx, ksft_disruptive
 from lib.py import EthtoolFamily, NlError
 from lib.py import NetDrvEnv, ip, defer
 
+# CMIS register constants matching net/ethtool/cmis_loopback.c
+SFF8024_ID_QSFP_DD = 0x18
+
+# Page 01h, Byte 142: bit 5 = Page 13h supported
+CMIS_DIAG_PAGE13_BIT = 1 << 5
+
+# Page 13h, Byte 128: loopback capability bits
+CMIS_LB_CAP_MEDIA_OUTPUT = 1 << 0
+CMIS_LB_CAP_MEDIA_INPUT = 1 << 1
+CMIS_LB_CAP_HOST_OUTPUT = 1 << 2
+CMIS_LB_CAP_HOST_INPUT = 1 << 3
+CMIS_LB_CAP_ALL = (CMIS_LB_CAP_MEDIA_OUTPUT | CMIS_LB_CAP_MEDIA_INPUT |
+                    CMIS_LB_CAP_HOST_OUTPUT | CMIS_LB_CAP_HOST_INPUT)
+
 # Direction flags as YNL returns them
 DIR_NONE = set()
 DIR_LOCAL = {'local'}
@@ -37,6 +54,54 @@ def _dfs_write_u32(cfg, path, val):
         f.write(str(val))
 
 
+def _nsim_write_page_byte(cfg, page, offset, value):
+    """Write a single byte to a netdevsim EEPROM page via debugfs."""
+    if offset < 128:
+        page_file = os.path.join(_nsim_dfs_path(cfg),
+                                 "ethtool/module/pages/0")
+        file_offset = offset
+    else:
+        page_file = os.path.join(_nsim_dfs_path(cfg),
+                                 f"ethtool/module/pages/{page}")
+        file_offset = offset - 128
+
+    with open(page_file, "r+b") as f:
+        f.seek(file_offset)
+        f.write(bytes([value]))
+
+
+def _nsim_read_page_byte(cfg, page, offset):
+    """Read a single byte from a netdevsim EEPROM page via debugfs."""
+    if offset < 128:
+        page_file = os.path.join(_nsim_dfs_path(cfg),
+                                 "ethtool/module/pages/0")
+        file_offset = offset
+    else:
+        page_file = os.path.join(_nsim_dfs_path(cfg),
+                                 f"ethtool/module/pages/{page}")
+        file_offset = offset - 128
+
+    with open(page_file, "rb") as f:
+        f.seek(file_offset)
+        return f.read(1)[0]
+
+
+def _nsim_seed_cmis(cfg, caps=CMIS_LB_CAP_ALL):
+    """Seed the netdevsim EEPROM with CMIS module identity and
+    loopback capabilities.
+    """
+    _nsim_write_page_byte(cfg, 0x00, 0, SFF8024_ID_QSFP_DD)
+    _nsim_write_page_byte(cfg, 0x01, 0x8e, CMIS_DIAG_PAGE13_BIT)
+    _nsim_write_page_byte(cfg, 0x13, 0x80, caps)
+
+
+def _nsim_clear_cmis(cfg):
+    """Clear CMIS identity bytes left by previous tests."""
+    _nsim_write_page_byte(cfg, 0x00, 0, 0)
+    _nsim_write_page_byte(cfg, 0x01, 0x8e, 0)
+    _nsim_write_page_byte(cfg, 0x13, 0x80, 0)
+
+
 def _get_loopback(cfg):
     results = cfg.ethnl.loopback_get({
         'header': {'dev-index': cfg.ifindex}
@@ -120,6 +185,138 @@ def test_set_mac_unknown_name(cfg):
                 "Expected EOPNOTSUPP for unknown name")
 
 
+def test_get_no_module(cfg):
+    """GET on a device with no CMIS module returns no module entries."""
+    _nsim_clear_cmis(cfg)
+
+    entries = _get_loopback(cfg)
+    mod_entries = [e for e in entries if e['component'] == 'module']
+    ksft_eq(len(mod_entries), 0, "Expected no module entries without CMIS module")
+
+
+def test_get_all_caps(cfg):
+    """GET with all four CMIS loopback capabilities seeded."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_ALL)
+
+    entries = _get_loopback(cfg)
+    mod_entries = [e for e in entries if e['component'] == 'module']
+
+    # Expect 2 entries (one per name), each with both directions
+    ksft_eq(len(mod_entries), 2, "Expected 2 module loopback entries")
+
+    host = [e for e in mod_entries if e['name'] == 'cmis-host']
+    media = [e for e in mod_entries if e['name'] == 'cmis-media']
+    ksft_eq(len(host), 1, "Expected 1 cmis-host entry")
+    ksft_eq(len(media), 1, "Expected 1 cmis-media entry")
+
+    ksft_eq(host[0]['supported'], DIR_LOCAL | DIR_REMOTE)
+    ksft_eq(media[0]['supported'], DIR_LOCAL | DIR_REMOTE)
+
+    for e in mod_entries:
+        ksft_eq(e['direction'], DIR_NONE,
+                f"Expected direction=off for {e['name']}")
+
+
+def test_get_partial_caps(cfg):
+    """GET with only host-input capability advertised."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_HOST_INPUT)
+
+    entries = _get_loopback(cfg)
+    mod_entries = [e for e in entries if e['component'] == 'module']
+    ksft_eq(len(mod_entries), 1, "Expected 1 module loopback entry")
+    ksft_eq(mod_entries[0]['name'], 'cmis-host')
+    ksft_eq(mod_entries[0]['supported'], DIR_LOCAL)
+
+
+@ksft_disruptive
+def test_set_verify_eeprom(cfg):
+    """SET local loopback and verify the EEPROM control byte directly."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_ALL)
+
+    ip(f"link set dev {cfg.ifname} down")
+    defer(ip, f"link set dev {cfg.ifname} up")
+
+    _set_loopback(cfg, 'module', 'cmis-host', 'local')
+    defer(_set_loopback, cfg, 'module', 'cmis-host', 0)
+
+    # Host Side Input = Page 13h, Byte 183
+    val = _nsim_read_page_byte(cfg, 0x13, 183)
+    ksft_eq(val, 0xff, "Host Side Input control byte should be 0xff")
+
+    # Disable and verify
+    _set_loopback(cfg, 'module', 'cmis-host', 0)
+    val = _nsim_read_page_byte(cfg, 0x13, 183)
+    ksft_eq(val, 0x00, "Host Side Input should be 0x00 after disable")
+
+
+@ksft_disruptive
+def test_set_direction_switch_eeprom(cfg):
+    """Switch directions and verify both EEPROM bytes."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_ALL)
+
+    ip(f"link set dev {cfg.ifname} down")
+    defer(ip, f"link set dev {cfg.ifname} up")
+
+    _set_loopback(cfg, 'module', 'cmis-host', 'local')
+    defer(_set_loopback, cfg, 'module', 'cmis-host', 0)
+
+    # Switch to remote
+    _set_loopback(cfg, 'module', 'cmis-host', 'remote')
+
+    # Local (Host Input, Byte 183) should be disabled
+    val = _nsim_read_page_byte(cfg, 0x13, 183)
+    ksft_eq(val, 0x00, "Local should be disabled after switch")
+    # Remote (Host Output, Byte 182) should be enabled
+    val = _nsim_read_page_byte(cfg, 0x13, 182)
+    ksft_eq(val, 0xff, "Remote should be enabled after switch")
+
+
+@ksft_disruptive
+def test_set_unsupported_direction(cfg):
+    """SET with unsupported direction should fail."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_HOST_INPUT)  # only local
+
+    ip(f"link set dev {cfg.ifname} down")
+    defer(ip, f"link set dev {cfg.ifname} up")
+
+    try:
+        _set_loopback(cfg, 'module', 'cmis-host', 'remote')
+        raise KsftFailEx("Should have rejected unsupported direction")
+    except NlError as e:
+        ksft_eq(e.error, errno.EOPNOTSUPP,
+                "Expected EOPNOTSUPP for unsupported direction")
+
+
+@ksft_disruptive
+def test_set_no_cmis(cfg):
+    """SET on a device without CMIS loopback support should fail."""
+    _nsim_clear_cmis(cfg)
+
+    ip(f"link set dev {cfg.ifname} down")
+    defer(ip, f"link set dev {cfg.ifname} up")
+
+    try:
+        _set_loopback(cfg, 'module', 'cmis-host', 'local')
+        raise KsftFailEx("Should have rejected SET without CMIS support")
+    except NlError as e:
+        ksft_eq(e.error, errno.EOPNOTSUPP,
+                "Expected EOPNOTSUPP without CMIS support")
+
+
+def test_combined_dump(cfg):
+    """Dump should return both MAC and MODULE entries."""
+    _nsim_seed_cmis(cfg, CMIS_LB_CAP_ALL)
+    defer(_nsim_clear_cmis, cfg)
+
+    entries = _get_loopback(cfg)
+    mac_entries = [e for e in entries if e['component'] == 'mac']
+    mod_entries = [e for e in entries if e['component'] == 'module']
+
+    ksft_eq(len(mac_entries), 1, "Expected 1 MAC entry")
+    ksft_eq(len(mod_entries), 2, "Expected 2 MODULE entries")
+    ksft_eq(mac_entries[0]['name'], 'mac')
+
+
 def main() -> None:
     """Run netdevsim loopback tests."""
     with NetDrvEnv(__file__) as cfg:
@@ -130,6 +327,14 @@ def main() -> None:
             test_set_mac_local,
             test_set_mac_disable,
             test_set_mac_unknown_name,
+            test_get_no_module,
+            test_get_all_caps,
+            test_get_partial_caps,
+            test_set_verify_eeprom,
+            test_set_direction_switch_eeprom,
+            test_set_unsupported_direction,
+            test_set_no_cmis,
+            test_combined_dump,
         ], args=(cfg, ))
     ksft_exit()
 
Signature · 1 line
2.53.0
maintainer Reviewed-by Tested-by on v2 1/12
#2 lore raw

Re: [PATCH net-next v2 01/12] ethtool: Add dump_one_dev callback for per-device sub-iteration

In reply to Björn Töpel

Hi Björn,

Quoted 7 lines · Björn Töpel

Add the dump_one_dev callback to ethnl_request_ops, allowing commands to provide custom per-device dump logic with sub-positioning. Extend ethnl_dump_ctx with ifindex and pos_sub fields.

No functional change; no command uses dump_one_dev yet.

Awesome :) I confirm that the phy dump still works just fine, and this looks much simpler to me.

Maxime

Quoted 167 lines
---
 net/ethtool/netlink.c | 66 ++++++++++++++++++-------------------------
 net/ethtool/netlink.h | 31 ++++++++++++++++++++
 2 files changed, 58 insertions(+), 39 deletions(-)
net/ethtool/netlink.c +27 -39
index 5046023a30b1..8d161f0882d0 100644
@@ -346,36 +346,6 @@ int ethnl_multicast(struct sk_buff *skb, struct net_device *dev)
 
 /* GET request helpers */
 
-/**
- * struct ethnl_dump_ctx - context structure for generic dumpit() callback
- * @ops:        request ops of currently processed message type
- * @req_info:   parsed request header of processed request
- * @reply_data: data needed to compose the reply
- * @pos_ifindex: saved iteration position - ifindex
- *
- * These parameters are kept in struct netlink_callback as context preserved
- * between iterations. They are initialized by ethnl_default_start() and used
- * in ethnl_default_dumpit() and ethnl_default_done().
- */
-struct ethnl_dump_ctx {
-	const struct ethnl_request_ops	*ops;
-	struct ethnl_req_info		*req_info;
-	struct ethnl_reply_data		*reply_data;
-	unsigned long			pos_ifindex;
-};
-
-/**
- * struct ethnl_perphy_dump_ctx - context for dumpit() PHY-aware callbacks
- * @ethnl_ctx: generic ethnl context
- * @ifindex: For Filtered DUMP requests, the ifindex of the targeted netdev
- * @pos_phyindex: iterator position for multi-msg DUMP
- */
-struct ethnl_perphy_dump_ctx {
-	struct ethnl_dump_ctx	ethnl_ctx;
-	unsigned int		ifindex;
-	unsigned long		pos_phyindex;
-};
-
 static const struct ethnl_request_ops *
 ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
 	[ETHTOOL_MSG_STRSET_GET]	= &ethnl_strset_request_ops,
@@ -618,6 +588,7 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
 				struct netlink_callback *cb)
 {
 	struct ethnl_dump_ctx *ctx = ethnl_dump_context(cb);
+	const struct genl_info *info = genl_info_dump(cb);
 	struct net *net = sock_net(skb->sk);
 	netdevice_tracker dev_tracker;
 	struct net_device *dev;
@@ -625,10 +596,20 @@ static int ethnl_default_dumpit(struct sk_buff *skb,
 
 	rcu_read_lock();
 	for_each_netdev_dump(net, dev, ctx->pos_ifindex) {
+		if (ctx->ifindex && ctx->ifindex != ctx->pos_ifindex)
+			break;
+
 		netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
 		rcu_read_unlock();
 
-		ret = ethnl_default_dump_one(skb, dev, ctx, genl_info_dump(cb));
+		if (ctx->ops->dump_one_dev) {
+			ctx->req_info->dev = dev;
+			ret = ctx->ops->dump_one_dev(skb, ctx, &ctx->pos_sub,
+						     info);
+			ctx->req_info->dev = NULL;
+		} else {
+			ret = ethnl_default_dump_one(skb, dev, ctx, info);
+		}
 
 		rcu_read_lock();
 		netdev_put(dev, &dev_tracker);
@@ -674,19 +655,26 @@ static int ethnl_default_start(struct netlink_callback *cb)
 	ret = ethnl_default_parse(req_info, &info->info, ops, false);
 	if (ret < 0)
 		goto free_reply_data;
-	if (req_info->dev) {
-		/* We ignore device specification in dump requests but as the
-		 * same parser as for non-dump (doit) requests is used, it
-		 * would take reference to the device if it finds one
-		 */
-		netdev_put(req_info->dev, &req_info->dev_tracker);
-		req_info->dev = NULL;
-	}
 
 	ctx->ops = ops;
 	ctx->req_info = req_info;
 	ctx->reply_data = reply_data;
 	ctx->pos_ifindex = 0;
+	ctx->ifindex = 0;
+	ctx->pos_sub = 0;
+
+	if (req_info->dev) {
+		if (ops->dump_one_dev) {
+			/* Sub-iterator dumps keep track of the dev's ifindex
+			 * so the dumpit handler can grab/release the netdev
+			 * per iteration.
+			 */
+			ctx->ifindex = req_info->dev->ifindex;
+			ctx->pos_ifindex = ctx->ifindex;
+		}
+		netdev_put(req_info->dev, &req_info->dev_tracker);
+		req_info->dev = NULL;
+	}
 
 	return 0;
 
net/ethtool/netlink.h +31 -0
index aaf6f2468768..e01adc5db02f 100644
@@ -10,6 +10,28 @@
 
 struct ethnl_req_info;
 
+/**
+ * struct ethnl_dump_ctx - context structure for generic dumpit() callback
+ * @ops:        request ops of currently processed message type
+ * @req_info:   parsed request header of processed request
+ * @reply_data: data needed to compose the reply
+ * @pos_ifindex: saved iteration position - ifindex
+ * @ifindex:    for filtered dump requests, the ifindex of the targeted netdev
+ * @pos_sub:    iterator position for per-device iteration
+ *
+ * These parameters are kept in struct netlink_callback as context preserved
+ * between iterations. They are initialized by ethnl_default_start() and used
+ * in ethnl_default_dumpit() and ethnl_default_done().
+ */
+struct ethnl_dump_ctx {
+	const struct ethnl_request_ops	*ops;
+	struct ethnl_req_info		*req_info;
+	struct ethnl_reply_data		*reply_data;
+	unsigned long			pos_ifindex;
+	unsigned int			ifindex;
+	unsigned long			pos_sub;
+};
+
 u32 ethnl_bcast_seq_next(void);
 int ethnl_parse_header_dev_get(struct ethnl_req_info *req_info,
 			       const struct nlattr *nest, struct net *net,
@@ -365,6 +387,10 @@ int ethnl_sock_priv_set(struct sk_buff *skb, struct net_device *dev, u32 portid,
  *	used e.g. to free any additional data structures outside the main
  *	structure which were allocated by ->prepare_data(). When processing
  *	dump requests, ->cleanup() is called for each message.
+ * @dump_one_dev:
+ *	Optional callback for dumping data for a single device. When set,
+ *	overrides the default dump behavior for GET requests, allowing
+ *	per-device iteration with sub-positioning via @pos_sub.
  * @set_validate:
  *	Check if set operation is supported for a given device, and perform
  *	extra input checks. Expected return values:
@@ -409,6 +435,11 @@ struct ethnl_request_ops {
 			  const struct ethnl_reply_data *reply_data);
 	void (*cleanup_data)(struct ethnl_reply_data *reply_data);
 
+	int (*dump_one_dev)(struct sk_buff *skb,
+			    struct ethnl_dump_ctx *ctx,
+			    unsigned long *pos_sub,
+			    const struct genl_info *info);
+
 	int (*set_validate)(struct ethnl_req_info *req_info,
 			    struct genl_info *info);
 	int (*set)(struct ethnl_req_info *req_info,
maintainer Reviewed-by Tested-by on v2 2/12
#4 lore raw

Re: [PATCH net-next v2 02/12] ethtool: Convert per-PHY commands to dump_one_dev

In reply to Björn Töpel

Hi Björn

Quoted 9 lines · Björn Töpel

Convert PSE, PLCA, PHY, and MSE commands from the separate ethnl_perphy_{start,dumpit,done} handlers to use the generic dump_one_dev callback. This removes the per-PHY specific dump infrastructure (ethnl_perphy_dump_ctx, ethnl_perphy_dump_context, ethnl_perphy_start, ethnl_perphy_dumpit, ethnl_perphy_done, and the internal helpers) in favor of a shared ethnl_perphy_dump_one_dev() function.

Maxime

Quoted 337 lines
---
 net/ethtool/mse.c     |   1 +
 net/ethtool/netlink.c | 194 ++++++------------------------------------
 net/ethtool/netlink.h |   4 +
 net/ethtool/phy.c     |   1 +
 net/ethtool/plca.c    |   2 +
 net/ethtool/pse-pd.c  |   1 +
 6 files changed, 35 insertions(+), 168 deletions(-)
net/ethtool/mse.c +1 -0
index e91b74430f76..3f33182283ce 100644
@@ -325,4 +325,5 @@ const struct ethnl_request_ops ethnl_mse_request_ops = {
 	.cleanup_data = mse_cleanup_data,
 	.reply_size = mse_reply_size,
 	.fill_reply = mse_fill_reply,
+	.dump_one_dev = ethnl_perphy_dump_one_dev,
 };
net/ethtool/netlink.c +26 -168
index 8d161f0882d0..edeeca67918a 100644
@@ -399,12 +399,6 @@ static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
 	return (struct ethnl_dump_ctx *)cb->ctx;
 }
 
-static struct ethnl_perphy_dump_ctx *
-ethnl_perphy_dump_context(struct netlink_callback *cb)
-{
-	return (struct ethnl_perphy_dump_ctx *)cb->ctx;
-}
-
 /**
  * ethnl_default_parse() - Parse request message
  * @req_info:    pointer to structure to put data into
@@ -686,169 +680,33 @@ static int ethnl_default_start(struct netlink_callback *cb)
 	return ret;
 }
 
-/* per-PHY ->start() handler for GET requests */
-static int ethnl_perphy_start(struct netlink_callback *cb)
+/* Shared dump_one_dev for per-PHY commands (PSE, PLCA, PHY, MSE) */
+int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
+			      struct ethnl_dump_ctx *ctx,
+			      unsigned long *pos_sub,
+			      const struct genl_info *info)
 {
-	struct ethnl_perphy_dump_ctx *phy_ctx = ethnl_perphy_dump_context(cb);
-	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
-	struct ethnl_dump_ctx *ctx = &phy_ctx->ethnl_ctx;
-	struct ethnl_reply_data *reply_data;
-	const struct ethnl_request_ops *ops;
-	struct ethnl_req_info *req_info;
-	struct genlmsghdr *ghdr;
-	int ret;
-
-	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
-
-	ghdr = nlmsg_data(cb->nlh);
-	ops = ethnl_default_requests[ghdr->cmd];
-	if (WARN_ONCE(!ops, "cmd %u has no ethnl_request_ops\n", ghdr->cmd))
-		return -EOPNOTSUPP;
-	req_info = kzalloc(ops->req_info_size, GFP_KERNEL);
-	if (!req_info)
-		return -ENOMEM;
-	reply_data = kmalloc(ops->reply_data_size, GFP_KERNEL);
-	if (!reply_data) {
-		ret = -ENOMEM;
-		goto free_req_info;
-	}
-
-	/* Unlike per-dev dump, don't ignore dev. The dump handler
-	 * will notice it and dump PHYs from given dev. We only keep track of
-	 * the dev's ifindex, .dumpit() will grab and release the netdev itself.
-	 */
-	ret = ethnl_default_parse(req_info, &info->info, ops, false);
-	if (ret < 0)
-		goto free_reply_data;
-	if (req_info->dev) {
-		phy_ctx->ifindex = req_info->dev->ifindex;
-		netdev_put(req_info->dev, &req_info->dev_tracker);
-		req_info->dev = NULL;
-	}
-
-	ctx->ops = ops;
-	ctx->req_info = req_info;
-	ctx->reply_data = reply_data;
-	ctx->pos_ifindex = 0;
-
-	return 0;
-
-free_reply_data:
-	kfree(reply_data);
-free_req_info:
-	kfree(req_info);
-
-	return ret;
-}
-
-static int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
-				     struct ethnl_perphy_dump_ctx *ctx,
-				     const struct genl_info *info)
-{
-	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
-	struct net_device *dev = ethnl_ctx->req_info->dev;
+	struct net_device *dev = ctx->req_info->dev;
 	struct phy_device_node *pdn;
 	int ret;
 
 	if (!dev->link_topo)
 		return 0;
 
-	xa_for_each_start(&dev->link_topo->phys, ctx->pos_phyindex, pdn,
-			  ctx->pos_phyindex) {
-		ethnl_ctx->req_info->phy_index = ctx->pos_phyindex;
+	xa_for_each_start(&dev->link_topo->phys, *pos_sub, pdn,
+			  *pos_sub) {
+		ctx->req_info->phy_index = *pos_sub;
 
 		/* We can re-use the original dump_one as ->prepare_data in
 		 * commands use ethnl_req_get_phydev(), which gets the PHY from
 		 * the req_info->phy_index
 		 */
-		ret = ethnl_default_dump_one(skb, dev, ethnl_ctx, info);
+		ret = ethnl_default_dump_one(skb, dev, ctx, info);
 		if (ret)
 			return ret;
 	}
 
-	ctx->pos_phyindex = 0;
-
-	return 0;
-}
-
-static int ethnl_perphy_dump_all_dev(struct sk_buff *skb,
-				     struct ethnl_perphy_dump_ctx *ctx,
-				     const struct genl_info *info)
-{
-	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
-	struct net *net = sock_net(skb->sk);
-	netdevice_tracker dev_tracker;
-	struct net_device *dev;
-	int ret = 0;
-
-	rcu_read_lock();
-	for_each_netdev_dump(net, dev, ethnl_ctx->pos_ifindex) {
-		netdev_hold(dev, &dev_tracker, GFP_ATOMIC);
-		rcu_read_unlock();
-
-		/* per-PHY commands use ethnl_req_get_phydev(), which needs the
-		 * net_device in the req_info
-		 */
-		ethnl_ctx->req_info->dev = dev;
-		ret = ethnl_perphy_dump_one_dev(skb, ctx, info);
-
-		rcu_read_lock();
-		netdev_put(dev, &dev_tracker);
-		ethnl_ctx->req_info->dev = NULL;
-
-		if (ret < 0 && ret != -EOPNOTSUPP) {
-			if (likely(skb->len))
-				ret = skb->len;
-			break;
-		}
-		ret = 0;
-	}
-	rcu_read_unlock();
-
-	return ret;
-}
-
-/* per-PHY ->dumpit() handler for GET requests. */
-static int ethnl_perphy_dumpit(struct sk_buff *skb,
-			       struct netlink_callback *cb)
-{
-	struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb);
-	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
-	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
-	int ret = 0;
-
-	if (ctx->ifindex) {
-		netdevice_tracker dev_tracker;
-		struct net_device *dev;
-
-		dev = netdev_get_by_index(genl_info_net(&info->info),
-					  ctx->ifindex, &dev_tracker,
-					  GFP_KERNEL);
-		if (!dev)
-			return -ENODEV;
-
-		ethnl_ctx->req_info->dev = dev;
-		ret = ethnl_perphy_dump_one_dev(skb, ctx, genl_info_dump(cb));
-
-		if (ret < 0 && ret != -EOPNOTSUPP && likely(skb->len))
-			ret = skb->len;
-
-		netdev_put(dev, &dev_tracker);
-	} else {
-		ret = ethnl_perphy_dump_all_dev(skb, ctx, genl_info_dump(cb));
-	}
-
-	return ret;
-}
-
-/* per-PHY ->done() handler for GET requests */
-static int ethnl_perphy_done(struct netlink_callback *cb)
-{
-	struct ethnl_perphy_dump_ctx *ctx = ethnl_perphy_dump_context(cb);
-	struct ethnl_dump_ctx *ethnl_ctx = &ctx->ethnl_ctx;
-
-	kfree(ethnl_ctx->reply_data);
-	kfree(ethnl_ctx->req_info);
+	*pos_sub = 0;
 
 	return 0;
 }
@@ -1410,9 +1268,9 @@ static const struct genl_ops ethtool_genl_ops[] = {
 	{
 		.cmd	= ETHTOOL_MSG_PSE_GET,
 		.doit	= ethnl_default_doit,
-		.start	= ethnl_perphy_start,
-		.dumpit	= ethnl_perphy_dumpit,
-		.done	= ethnl_perphy_done,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
 		.policy = ethnl_pse_get_policy,
 		.maxattr = ARRAY_SIZE(ethnl_pse_get_policy) - 1,
 	},
@@ -1434,9 +1292,9 @@ static const struct genl_ops ethtool_genl_ops[] = {
 	{
 		.cmd	= ETHTOOL_MSG_PLCA_GET_CFG,
 		.doit	= ethnl_default_doit,
-		.start	= ethnl_perphy_start,
-		.dumpit	= ethnl_perphy_dumpit,
-		.done	= ethnl_perphy_done,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
 		.policy = ethnl_plca_get_cfg_policy,
 		.maxattr = ARRAY_SIZE(ethnl_plca_get_cfg_policy) - 1,
 	},
@@ -1450,9 +1308,9 @@ static const struct genl_ops ethtool_genl_ops[] = {
 	{
 		.cmd	= ETHTOOL_MSG_PLCA_GET_STATUS,
 		.doit	= ethnl_default_doit,
-		.start	= ethnl_perphy_start,
-		.dumpit	= ethnl_perphy_dumpit,
-		.done	= ethnl_perphy_done,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
 		.policy = ethnl_plca_get_status_policy,
 		.maxattr = ARRAY_SIZE(ethnl_plca_get_status_policy) - 1,
 	},
@@ -1482,9 +1340,9 @@ static const struct genl_ops ethtool_genl_ops[] = {
 	{
 		.cmd	= ETHTOOL_MSG_PHY_GET,
 		.doit	= ethnl_default_doit,
-		.start	= ethnl_perphy_start,
-		.dumpit	= ethnl_perphy_dumpit,
-		.done	= ethnl_perphy_done,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
 		.policy = ethnl_phy_get_policy,
 		.maxattr = ARRAY_SIZE(ethnl_phy_get_policy) - 1,
 	},
@@ -1528,9 +1386,9 @@ static const struct genl_ops ethtool_genl_ops[] = {
 	{
 		.cmd	= ETHTOOL_MSG_MSE_GET,
 		.doit	= ethnl_default_doit,
-		.start	= ethnl_perphy_start,
-		.dumpit	= ethnl_perphy_dumpit,
-		.done	= ethnl_perphy_done,
+		.start	= ethnl_default_start,
+		.dumpit	= ethnl_default_dumpit,
+		.done	= ethnl_default_done,
 		.policy = ethnl_mse_get_policy,
 		.maxattr = ARRAY_SIZE(ethnl_mse_get_policy) - 1,
 	},
net/ethtool/netlink.h +4 -0
index e01adc5db02f..dda2f5593ed9 100644
@@ -546,6 +546,10 @@ int ethnl_tsinfo_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
 int ethnl_tsinfo_done(struct netlink_callback *cb);
 int ethnl_rss_create_doit(struct sk_buff *skb, struct genl_info *info);
 int ethnl_rss_delete_doit(struct sk_buff *skb, struct genl_info *info);
+int ethnl_perphy_dump_one_dev(struct sk_buff *skb,
+			      struct ethnl_dump_ctx *ctx,
+			      unsigned long *pos_sub,
+			      const struct genl_info *info);
 
 extern const char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN];
 extern const char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN];
net/ethtool/phy.c +1 -0
index d4e6887055ab..4bb23a5d6ad5 100644
@@ -162,4 +162,5 @@ const struct ethnl_request_ops ethnl_phy_request_ops = {
 	.reply_size		= phy_reply_size,
 	.fill_reply		= phy_fill_reply,
 	.cleanup_data		= phy_cleanup_data,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 };
net/ethtool/plca.c +2 -0
index 91f0c4233298..84e902532617 100644
@@ -188,6 +188,7 @@ const struct ethnl_request_ops ethnl_plca_cfg_request_ops = {
 	.prepare_data		= plca_get_cfg_prepare_data,
 	.reply_size		= plca_get_cfg_reply_size,
 	.fill_reply		= plca_get_cfg_fill_reply,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 
 	.set			= ethnl_set_plca,
 	.set_ntf_cmd		= ETHTOOL_MSG_PLCA_NTF,
@@ -268,4 +269,5 @@ const struct ethnl_request_ops ethnl_plca_status_request_ops = {
 	.prepare_data		= plca_get_status_prepare_data,
 	.reply_size		= plca_get_status_reply_size,
 	.fill_reply		= plca_get_status_fill_reply,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 };
net/ethtool/pse-pd.c +1 -0
index 2eb9bdc2dcb9..83f0205053a3 100644
@@ -338,6 +338,7 @@ const struct ethnl_request_ops ethnl_pse_request_ops = {
 	.reply_size		= pse_reply_size,
 	.fill_reply		= pse_fill_reply,
 	.cleanup_data		= pse_cleanup_data,
+	.dump_one_dev		= ethnl_perphy_dump_one_dev,
 
 	.set			= ethnl_set_pse,
 	/* PSE has no notification */
#6 lore raw

Re: [PATCH net-next v2 03/12] ethtool: Add loopback netlink UAPI definitions

In reply to Björn Töpel

Hi Björn,

Quoted 4 lines · Björn Töpel

Add the netlink YAML spec and auto-generated UAPI header for a unified loopback interface covering MAC, PHY, and pluggable module components.

Each loopback point is described by a nested entry attribute

Is the nest actually needed ? if everything is under the nest, you might as well just drop it and return all the attributes directly ?

Maxime

Quoted 11 lines

containing:

 - component  where in the path (MAC, PHY, MODULE)
 - name       subsystem label, e.g. "cmis-host" or "cmis-media"
 - id         optional instance selector (e.g. PHY id, port id)
 - depth      ordering index within a component (0 = first/only)
 - supported  bitmask of supported directions
 - direction  LOCAL, REMOTE, or 0 (disabled)

Maxime

#7 lore raw

Re: [PATCH net-next v2 03/12] ethtool: Add loopback netlink UAPI definitions

Maxime!

Quoted 11 lines · Maxime Chevallier

Hi Björn,

Quoted 4 lines · Björn Töpel

Add the netlink YAML spec and auto-generated UAPI header for a unified loopback interface covering MAC, PHY, and pluggable module components.

Each loopback point is described by a nested entry attribute

Is the nest actually needed ? if everything is under the nest, you might as well just drop it and return all the attributes directly ?

Hmm, you're right! This is a leftover pre the dumpit change. GET/SET never needs nesting. I'll fix that for the next round!

Thanks!
Björn

Reviewed-by on v2 9/12
#19 lore raw

Re: [PATCH net-next v2 09/12] selftests: drv-net: Add MAC loopback netdevsim test

In reply to Björn Töpel

tools/testing/selftests/drivers/net/hw/loopback_nsim.py line 37

Quoted 4 lines · Björn Töpel
tools/testing/selftests/drivers/net/hw/loopback_nsim.py new file +4 -0
+def _dfs_write_u32(cfg, path, val):
+    with open(os.path.join(_nsim_dfs_path(cfg), path), "w",
+              encoding="utf-8") as f:
+        f.write(str(val))

This function doesn't seem to be called, right?

tools/testing/selftests/drivers/net/hw/loopback_nsim.py line 126

Quoted 4 lines
tools/testing/selftests/drivers/net/hw/loopback_nsim.py new file +4 -0
+def main() -> None:
+    """Run netdevsim loopback tests."""
+    with NetDrvEnv(__file__) as cfg:
+        cfg.ethnl = EthtoolFamily()

Given some functions above assume this is nsim, should you call cfg.require_nsim() ?

#17 lore raw

Re: [PATCH net-next v2 08/12] netdevsim: Add MAC loopback simulation

In reply to Björn Töpel
Quoted 5 lines · Björn Töpel

Implement the three ethtool loopback ops for MAC-level loopback simulation:

- get_loopback(): exact lookup by name ("mac")
- get_loopback_by()_index: enumerate (single entry at index 0)

- get_loopback_by_index(): enumerate (single entry at index 0)

#20 lore raw

Re: [PATCH net-next v2 09/12] selftests: drv-net: Add MAC loopback netdevsim test

In reply to Breno Leitao

Breno!

Quoted 9 lines · Breno Leitao

tools/testing/selftests/drivers/net/hw/loopback_nsim.py line 37

Quoted 4 lines · Björn Töpel
tools/testing/selftests/drivers/net/hw/loopback_nsim.py new file +4 -0
+def _dfs_write_u32(cfg, path, val):
+    with open(os.path.join(_nsim_dfs_path(cfg), path), "w",
+              encoding="utf-8") as f:
+        f.write(str(val))

This function doesn't seem to be called, right?

Indeed -- another leftover! Thank you!

Quoted 7 lines

tools/testing/selftests/drivers/net/hw/loopback_nsim.py line 126

Quoted 4 lines
tools/testing/selftests/drivers/net/hw/loopback_nsim.py new file +4 -0
+def main() -> None:
+    """Run netdevsim loopback tests."""
+    with NetDrvEnv(__file__) as cfg:
+        cfg.ethnl = EthtoolFamily()

Given some functions above assume this is nsim, should you call cfg.require_nsim() ?

Yeah, or directly from NetDrvEnv() statement above! Good point! Will fix!

Björn

#15 lore raw

Re: [PATCH net-next v2 07/12] ethtool: Add MAC loopback support via ethtool_ops

In reply to Björn Töpel

net/ethtool/loopback.c static int loopback_dump_one_dev(struct sk_buff *skb, · line 313

Quoted 10 lines · Björn Töpel
net/ethtool/loopback.c +6 -0
@@ -284,20 +305,31 @@ static int loopback_dump_one_dev(struct sk_buff *skb,
 {
 	struct loopback_req_info *req_info =
 		container_of(ctx->req_info, struct loopback_req_info, base);
+	/* pos_sub encodes: upper 16 bits = component phase, lower 16 = index
+	 * within that component. dump_one_dev is called repeatedly with
+	 * increasing pos_sub until all components are exhausted.
+	 */
+	enum ethtool_loopback_component phase = *pos_sub >> 16;
+	u32 idx = *pos_sub & 0xffff;

Consider introducing macros for these bit operations to improve code readability. Named macros would make the shift and mask operations more self-documenting and facilitate future changes, if we eventually get there.

maintainer
#8 lore raw

Re: [PATCH net-next v2 03/12] ethtool: Add loopback netlink UAPI definitions

In reply to Björn Töpel

Documentation/netlink/specs/ethtool.yaml attribute-sets: · line 1968

Quoted 4 lines · On Wed, 25 Mar 2026 15:50:10 +0100 Björn Töpel wrote:
Documentation/netlink/specs/ethtool.yaml +4 -0
+      -
+        name: unspec
+        type: unused
+        value: 0

I don't think we need to add unspec attrs for new sets ?

maintainer
#9 lore raw

Re: [PATCH net-next v2 03/12] ethtool: Add loopback netlink UAPI definitions

In reply to Björn Töpel
Quoted 11 lines · On Wed, 25 Mar 2026 15:50:10 +0100 Björn Töpel wrote:
Documentation/netlink/specs/ethtool.yaml +11 -0
+      -
+        name: depth
+        type: u8
+        doc: |
+          Ordering index within a component instance. When a component
+          has multiple loopback points of the same type (e.g. two PCS
+          blocks inside a rate-adaptation PHY), depth distinguishes
+          them. Lower depth values are closer to the host side, higher
+          values are closer to the line/media side. Defaults to 0 when
+          there is only one loopback point per (component, name) tuple.
+      -

Documentation/netlink/specs/ethtool.yaml attribute-sets: · line 2004

Quoted 10 lines
Documentation/netlink/specs/ethtool.yaml +10 -0
+        name: supported
+        type: u8
+        enum: loopback-direction
+        enum-as-flags: true
+        doc: Bitmask of supported loopback directions
+      -
+        name: direction
+        type: u8
+        enum: loopback-direction
+        doc: Current loopback direction, 0 means disabled

u32, Netlink attrs are padded to 4B anyway

#10 lore raw

Re: [PATCH net-next v2 03/12] ethtool: Add loopback netlink UAPI definitions

Quoted 23 lines · Jakub Kicinski
Quoted 11 lines · On Wed, 25 Mar 2026 15:50:10 +0100 Björn Töpel wrote:
Documentation/netlink/specs/ethtool.yaml +11 -0
+      -
+        name: depth
+        type: u8
+        doc: |
+          Ordering index within a component instance. When a component
+          has multiple loopback points of the same type (e.g. two PCS
+          blocks inside a rate-adaptation PHY), depth distinguishes
+          them. Lower depth values are closer to the host side, higher
+          values are closer to the line/media side. Defaults to 0 when
+          there is only one loopback point per (component, name) tuple.
+      -

Documentation/netlink/specs/ethtool.yaml attribute-sets: · line 2004

Quoted 10 lines
Documentation/netlink/specs/ethtool.yaml +10 -0
+        name: supported
+        type: u8
+        enum: loopback-direction
+        enum-as-flags: true
+        doc: Bitmask of supported loopback directions
+      -
+        name: direction
+        type: u8
+        enum: loopback-direction
+        doc: Current loopback direction, 0 means disabled
Quoted 1 line

u32, Netlink attrs are padded to 4B anyway

That's my bad, I was the one suggesting u8 :( I didn't have the padding thing in mind at that time :/

Maxime