Netdev List
 help / color / mirror / Atom feed
From: Maxime Chevallier <maxime.chevallier@bootlin.com>
To: Michal Kubecek <mkubecek@suse.cz>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Jakub Kicinski <kuba@kernel.org>,
	davem@davemloft.net, Eric Dumazet <edumazet@google.com>,
	Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>
Cc: Maxime Chevallier <maxime.chevallier@bootlin.com>,
	netdev@vger.kernel.org,
	Christophe Leroy <christophe.leroy@csgroup.eu>,
	thomas.petazzoni@bootlin.com
Subject: [PATCH ethtool-next 2/2] ethtool: Introduce a command to list an interface's ports
Date: Wed, 18 Nov 2026 16:05:22 +0100	[thread overview]
Message-ID: <20261118150523.2762733-3-maxime.chevallier@bootlin.com> (raw)
In-Reply-To: <20261118150523.2762733-1-maxime.chevallier@bootlin.com>

Introduce the "--show-ports <dev>" command to list an interface's ports
using the ETHTOOL_MSG_PORT_GET netlink command.

It shows the supported linkmodes, interfaces, type and upstream info,
using either plain text or JSON mode.

Add a small manpage alongside it.

Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
---
 Makefile.am      |   1 +
 ethtool.8.in     |  30 +++++++++++
 ethtool.c        |   6 +++
 netlink/extapi.h |   2 +
 netlink/port.c   | 134 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 173 insertions(+)
 create mode 100644 netlink/port.c

diff --git a/Makefile.am b/Makefile.am
index db22be4..f253ba0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -56,6 +56,7 @@ ethtool_SOURCES += \
 		  netlink/phy.c \
 		  netlink/tsconfig.c \
 		  netlink/mse.c \
+		  netlink/port.c \
 		  uapi/linux/const.h uapi/linux/ethtool.h \
 		  uapi/linux/ethtool_netlink.h \
 		  uapi/linux/ethtool_netlink_generated.h \
diff --git a/ethtool.8.in b/ethtool.8.in
index b404b93..d8ce895 100644
--- a/ethtool.8.in
+++ b/ethtool.8.in
@@ -594,6 +594,9 @@ ethtool \- query or control network driver and hardware settings
 .HP
 .B ethtool \-\-show\-phys
 .I devname
+.HP
+.B ethtool \-\-show\-ports
+.I devname
 .
 .\" Adjust lines (i.e. full justification) and hyphenate.
 .ad
@@ -2149,6 +2152,33 @@ If the PHY's upstream_type is
 .B upstream_sfp_name
 If the PHY is within an SFP/SFF module, this field contains the name of the
 upstream SFP bus.
+.RE
+.TP
+.B \-\-show\-ports
+Show the ports of an interface, that is the physical connection points through
+which it can be linked to a medium, such as an RJ45 connector or an SFP cage.
+.RS 4
+.TP
+.B port\-id
+The port's identifier, unique within the network interface.
+.TP
+.B type \ mdi | sfp
+The nature of the port.
+.B mdi
+ports connect directly to a medium.
+.B sfp
+ports are either an SFP cage, or a port provided by the module sitting in a
+cage.
+.TP
+.B supported\-link\-modes
+The link modes this port can operate at. Not reported for SFP cages.
+.TP
+.B supported\-interfaces
+For SFP cages, the MII interfaces that can be used towards the module.
+.TP
+.B upstream\-port
+If any, the port\-id of the port that feeds into this one, such as the cage a
+module sits in.
 
 .SH BUGS
 Not supported (in part or whole) on all network drivers.
diff --git a/ethtool.c b/ethtool.c
index 30a1d97..8482a10 100644
--- a/ethtool.c
+++ b/ethtool.c
@@ -6514,6 +6514,12 @@ static const struct option args[] = {
 		.nlfunc	= nl_gmse,
 		.help	= "Show Mean Square Error (MSE) diagnostics",
 	},
+	{
+		.opts	= "--show-ports",
+		.json	= true,
+		.nlfunc	= nl_get_port,
+		.help	= "List ports"
+	},
 	{
 		.opts	= "-h|--help",
 		.no_dev	= true,
diff --git a/netlink/extapi.h b/netlink/extapi.h
index e10af62..e7b6140 100644
--- a/netlink/extapi.h
+++ b/netlink/extapi.h
@@ -61,6 +61,7 @@ int nl_spse(struct cmd_context *ctx);
 int nl_flash_module_fw(struct cmd_context *ctx);
 int nl_get_phy(struct cmd_context *ctx);
 int nl_gmse(struct cmd_context *ctx);
+int nl_get_port(struct cmd_context *ctx);
 
 void nl_monitor_usage(void);
 
@@ -142,6 +143,7 @@ nl_get_eeprom_page(struct cmd_context *ctx __maybe_unused,
 #define nl_flash_module_fw	NULL
 #define nl_get_phy		NULL
 #define nl_gmse			NULL
+#define nl_get_port		NULL
 
 #endif /* ETHTOOL_ENABLE_NETLINK */
 
diff --git a/netlink/port.c b/netlink/port.c
new file mode 100644
index 0000000..b845d01
--- /dev/null
+++ b/netlink/port.c
@@ -0,0 +1,134 @@
+/*
+ * port.c - List the ports of an interface and their parameters
+ *
+ * Implementation of "ethtool --show-ports <dev>"
+ */
+
+#include <errno.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "../internal.h"
+#include "../common.h"
+#include "netlink.h"
+#include "bitset.h"
+
+/* PORT_GET */
+
+static const char *port_type_str(uint32_t port_type)
+{
+	switch (port_type) {
+	case ETHTOOL_PORT_TYPE_MDI: return "mdi";
+	case ETHTOOL_PORT_TYPE_SFP: return "sfp";
+	default: return "unknown";
+	}
+}
+
+static void port_interfaces_walk_cb(unsigned int idx __maybe_unused,
+				    const char *name, bool val, void *data)
+{
+	bool *first = data;
+
+	if (!val)
+		return;
+
+	if (!is_json_context() && !*first)
+		putchar(' ');
+	*first = false;
+
+	print_string(PRINT_ANY, NULL, "%s", name);
+}
+
+static int port_dump_interfaces(const struct nlattr *bitset)
+{
+	bool first = true;
+	int ret;
+
+	open_json_array("supported-interfaces", "");
+	if (!is_json_context())
+		printf("\tSupported MII interfaces: ");
+	ret = walk_bitset(bitset, NULL, port_interfaces_walk_cb, &first);
+	close_json_array("\n");
+
+	return ret;
+}
+
+static int port_reply_cb(const struct nlmsghdr *nlhdr, void *data)
+{
+	const struct nlattr *tb[ETHTOOL_A_PORT_MAX + 1] = {};
+	struct nl_context *nlctx = data;
+	DECLARE_ATTR_TB_INFO(tb);
+	uint32_t port_type;
+	int ret;
+
+	ret = mnl_attr_parse(nlhdr, GENL_HDRLEN, attr_cb, &tb_info);
+	if (ret < 0)
+		return MNL_CB_OK;
+	nlctx->devname = get_dev_name(tb[ETHTOOL_A_PORT_HEADER]);
+	if (!dev_ok(nlctx))
+		return MNL_CB_OK;
+
+	print_nl();
+
+	open_json_object(NULL);
+
+	print_string(PRINT_ANY, "ifname", "Port for %s:\n", nlctx->devname);
+
+	show_u32("port-id", "\tPort id: ", tb[ETHTOOL_A_PORT_ID]);
+
+	if (tb[ETHTOOL_A_PORT_TYPE]) {
+		port_type = mnl_attr_get_u32(tb[ETHTOOL_A_PORT_TYPE]);
+		print_string(PRINT_ANY, "type", "\tPort type: %s\n",
+			     port_type_str(port_type));
+	}
+
+	if (tb[ETHTOOL_A_PORT_SUPPORTED_MODES]) {
+		ret = dump_link_modes(nlctx, tb[ETHTOOL_A_PORT_SUPPORTED_MODES],
+				      false, LM_CLASS_REAL,
+				      "Supported link modes:  ", NULL, "\n",
+				      "Not reported", "supported-link-modes");
+		if (ret < 0)
+			goto out;
+	}
+
+	if (tb[ETHTOOL_A_PORT_SUPPORTED_INTERFACES]) {
+		ret = port_dump_interfaces(tb[ETHTOOL_A_PORT_SUPPORTED_INTERFACES]);
+		if (ret < 0)
+			goto out;
+	}
+
+	if (tb[ETHTOOL_A_PORT_UPSTREAM_PORT])
+		show_u32("upstream-port", "\tUpstream port id: ",
+			 tb[ETHTOOL_A_PORT_UPSTREAM_PORT]);
+
+out:
+	close_json_object();
+
+	return MNL_CB_OK;
+}
+
+int nl_get_port(struct cmd_context *ctx)
+{
+	struct nl_context *nlctx = ctx->nlctx;
+	struct nl_socket *nlsk = nlctx->ethnl_socket;
+	int ret;
+
+	if (netlink_cmd_check(ctx, ETHTOOL_MSG_PORT_GET, true))
+		return -EOPNOTSUPP;
+	if (ctx->argc > 0) {
+		fprintf(stderr, "ethtool: unexpected parameter '%s'\n",
+			*ctx->argp);
+		return 1;
+	}
+
+	ret = nlsock_prep_filtered_dump_request(nlsk, ETHTOOL_MSG_PORT_GET,
+						ETHTOOL_A_PORT_HEADER, 0);
+	if (ret)
+		return ret;
+
+	new_json_obj(ctx->json);
+	ret = nlsock_send_get_request(nlsk, port_reply_cb);
+	delete_json_obj();
+	return ret;
+}
-- 
2.56.1


      parent reply	other threads:[~2026-11-18 15:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 15:05 [PATCH ethtool-next 0/2] ethtool: Introduce the --show-ports command Maxime Chevallier
2026-11-18 15:05 ` [PATCH ethtool-next 1/2] update UAPI header copies Maxime Chevallier
2026-11-18 15:05 ` Maxime Chevallier [this message]

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=20261118150523.2762733-3-maxime.chevallier@bootlin.com \
    --to=maxime.chevallier@bootlin.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=christophe.leroy@csgroup.eu \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=mkubecek@suse.cz \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=thomas.petazzoni@bootlin.com \
    /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