Skip to contentSkip to conversation

net: mctp: add MCTP_OPT_ROUTE_SRCADDR getsockopt

PATCH In review Faizan Ali 3 participants 5 messages Sep 21 – Sep 25

Subsystems (3)
lore
Share & tools
lore

Faizan Ali

author
#0 lore raw

PATCH 1/1 net: mctp: add MCTP_OPT_ROUTE_SRCADDR getsockopt

Add a new socket option MCTP_OPT_ROUTE_SRCADDR that allows applications to query which local EID the kernel would use as the source address when sending to a given destination EID.

Applications such as PLDM need to advertise a local EID as the event receiver address to remote endpoints. Previously this required a manual multi-step lookup via the mctp tool (route show + addr show). The kernel routing table is the authoritative
source for this mapping, so expose it directly via a socket option.

The caller fills in net and daddr in struct mctp_route_srcaddr before calling getsockopt(SOL_MCTP, MCTP_OPT_ROUTE_SRCADDR); the kernel performs the same route lookup used for actual packet output and returns the resolved local EID in saddr.

Verified with the included kunit tests, and manually validated on hardware that the resolved source EID matches mctp route show / addr show output for both reachable and unreachable destinations.

---
 include/uapi/linux/mctp.h | 13 +++++++++
 net/mctp/af_mctp.c        | 31 ++++++++++++++++++++
 net/mctp/test/sock-test.c | 61 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+)
include/uapi/linux/mctp.h +13 -0
index 19ad12a0c..7c3d4a936 100644
@@ -55,6 +55,19 @@ struct mctp_fq_addr {
 #define MCTP_TAG_PREALLOC	0x10
 
 #define MCTP_OPT_ADDR_EXT	1
+#define MCTP_OPT_ROUTE_SRCADDR	2
+
+/* Query structure for MCTP_OPT_ROUTE_SRCADDR getsockopt.
+ *
+ * Caller fills in @net and @daddr before calling getsockopt.
+ * Performs a route lookup and returns the local source EID in @saddr.
+ */
+struct mctp_route_srcaddr {
+	unsigned int	net;
+	mctp_eid_t	daddr;
+	mctp_eid_t	saddr;
+	__u8		__pad[2];
+};
 
 #define SIOCMCTPALLOCTAG	(SIOCPROTOPRIVATE + 0)
 #define SIOCMCTPDROPTAG		(SIOCPROTOPRIVATE + 1)
net/mctp/af_mctp.c +31 -0
index 8af5e2b3c..5c6077e03 100644
@@ -425,6 +425,37 @@ static int mctp_getsockopt(struct socket *sock, int level, int optname,
 		return 0;
 	}
 
+	if (optname == MCTP_OPT_ROUTE_SRCADDR) {
+		struct mctp_route_srcaddr rsa;
+		struct mctp_dst dst;
+		unsigned int net;
+		int rc;
+
+		if (len != sizeof(rsa))
+			return -EINVAL;
+
+		if (copy_from_iter(&rsa, len, &opt->iter_in) != len)
+			return -EFAULT;
+
+		net = rsa.net;
+		if (net == MCTP_NET_ANY)
+			net = mctp_default_net(sock_net(sock->sk));
+
+		rc = mctp_route_lookup(sock_net(sock->sk), net, rsa.daddr, &dst);
+		if (rc)
+			return rc;
+
+		rsa.saddr = dst.saddr;
+		mctp_dst_release(&dst);
+
+		if (rsa.saddr == MCTP_ADDR_NULL)
+			return -EADDRNOTAVAIL;
+
+		if (copy_to_iter(&rsa, len, &opt->iter_out) != len)
+			return -EFAULT;
+		return 0;
+	}
+
 	return -ENOPROTOOPT;
 }
 
net/mctp/test/sock-test.c +61 -0

71 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi Faizan,

Quoted 9 lines

Add a new socket option MCTP_OPT_ROUTE_SRCADDR that allows applications to query which local EID the kernel would use as the source address when sending to a given destination EID.

Applications such as PLDM need to advertise a local EID as the event receiver address to remote endpoints. Previously this required a manual multi-step lookup via the mctp tool (route show + addr show). The kernel routing table is the authoritative
source for this mapping, so expose it directly via a socket option.

As I had asked earlier, can you elaborate on why this is needed over choosing any local address? Is there a routing topology where this would not work?

I'm not against the idea, we just need a fairly solidy justification for adding user ABI that cannot be changed in future.

Cheers,

Jeremy

author
#2 lore raw

Hello Jeremy,

Sorry, I missed following up on this earlier - continuing our conversation from the earlier thread.

Quoted 6 lines

As I had asked earlier, can you elaborate on why this is needed over choosing any local address? Is there a routing topology where this would not work?

I'm not against the idea, we just need a fairly solidy justification for adding user ABI that cannot be changed in future.

In theory, yes - any incoming message destined to an active local EID should reach an application bound to that message type. That said, I still find these problems with simply picking any local EID:

1. It requires a snapshot of all available local EIDs first - information the kernel already has internally via the routing table, but which applications can today only get via ad-hoc mctp route/addr correlation.

2. Not every local EID is reachable from every peer, even on the same network. Example from our hardware:

BMC --USB(EID 8)--> SMA(EID 20) --I3C--> GPU(EID 30) BMC also has a separate mctpi2c0 (EID 9), unrelated to the SMA.

Reaching the GPU is a gateway route (30 -> via 20 -> via 8), which mctp_route_lookup() already resolves correctly today for sendmsg(). If PLDM instead picks "any" EID and gets 9, the GPU's event notifications go out via I3C to the SMA - which has no knowledge of EID 9 at all (it's on an unrelated bus). The packet is undeliverable at the SMA itself, one hop before it would even reach the BMC.

I acknowledge this is implementation-specific, and additional route provisioning on the bridge could help - but that still requires out-of-band configuration to stay in sync with every local EID. Separately, how device firmware handles Set Event Receiver packets carrying an EID different from the one it saw during Set Endpoint ID discovery is also implementation-defined.

3. A picked EID doesn't stay valid - interface teardown (hot unplug) removes its local EID from the available routes, and Set Event Receiver is a one-shot registration with no way to detect that drift later. Further events would then be silently dropped by the kernel, since it has no route for the removed EID, and would never reach the application.

The kernel already walks this resolution (including gateway chains) correctly for every sendmsg() - this just exposes that same lookup, rather than requiring applications to parse route/addr output and re-derive it themselves.

Regards,
Faizan

maintainer
#3 lore raw

Hi Faizan,

Quoted 11 lines
Quoted 2 lines

I'm not against the idea, we just need a fairly solidy justification for adding user ABI that cannot be changed in future.

In theory, yes - any incoming message destined to an active local EID should reach an application bound to that message type. That said, I still find these problems with simply picking any local EID:

1. It requires a snapshot of all available local EIDs first - information the kernel already has internally via the routing table, but which applications can today only get via ad-hoc mctp route/addr correlation.

That's mostly equivalent what you're adding here - a facility to extract a local address from the kernel. The mctp addr command just uses a netlink interface to do so, rather than a sockopt.

The important difference here is the addition of the routing calculation to filter that set of addresses to a single one.

Quoted 6 lines

2. Not every local EID is reachable from every peer, even on the same network. Example from our hardware:

     BMC --USB(EID 8)--> SMA(EID 20) --I3C--> GPU(EID 30) BMC also has a separate mctpi2c0 (EID 9), unrelated to the SMA.

Quoted 7 lines

   Reaching the GPU is a gateway route (30 -> via 20 -> via 8), which mctp_route_lookup() already resolves correctly today for sendmsg(). If PLDM instead picks "any" EID and gets 9, the GPU's event notifications go out via I3C to the SMA - which has no knowledge of EID 9 at all (it's on an unrelated bus). The packet is undeliverable at the SMA itself, one hop before it would even reach the BMC.

Is there any reason why the SMA can route to EID 8 but not 9?

Quoted 6 lines

   I acknowledge this is implementation-specific, and additional route provisioning on the bridge could help - but that still requires out-of-band configuration to stay in sync with every local EID. Separately, how device firmware handles Set Event Receiver packets carrying an EID different from the one it saw during Set Endpoint ID discovery is also implementation-defined.

If there was any expected correlation between the Set Event Receiver and the sender of Set Endpoint ID messages, we wouldn't need the concept of Set Event Receiver at all.

I wouldn't think that there would be any requirement for the current-level bus-owner to be the event receiver; it's valid for the events to be going to any other MCTP endpoint.

Quoted 6 lines

3. A picked EID doesn't stay valid - interface teardown (hot unplug) removes its local EID from the available routes, and Set Event Receiver is a one-shot registration with no way to detect that drift later. Further events would then be silently dropped by the kernel, since it has no route for the removed EID, and would never reach the application.

You have the same issues with this - the source EID returned by your sockopt may become stale too. In fact, if we're interested in solving that issue, the way to detect that would be to listen for local address add/remove events, which would require netlink, so we may as well use the existing netlink interface for the original query too.

The most compelling point here is (2) - if there are particular routing requirements that would require a specific EID for Set Event Receiver. But the only datapoint we have for that is that there is a device that - for some reason - is making one specific assumption about the routing topology. Can you expand on that maybe?

The other approach may be to implement something like IP_PKTINFO, where we would have visibility on the local EID for a *received* message. There is a bit more precedence for that design, but it may introduce an unnecessary send/receive operation before the EID can be determined (but it's likely we're doing initial PLDM messaging to the event source anyway...).

Cheers,

Jeremy

#4 lore raw
In reply to Faizan Ali
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] In mctp_getsockopt() (net/mctp/af_mctp.c:451-452) a *successful* route lookup whose resolved source EID is MCTP_ADDR_NULL…
- [Low] The patch adds a permanent MCTP socket ABI but adds no documentation to Documentation/networking/mctp.rst (verified: grep for…

--

net: mctp: add MCTP_OPT_ROUTE_SRCADDR getsockopt

This adds a new SOL_MCTP socket option that takes a destination EID and network in struct mctp_route_srcaddr and returns the local EID the kernel would use as the source address. The implementation reuses mctp_route_lookup() and new kunit cases cover the resolvable and no-route paths.

A couple of questions below about the semantics of the new option.

Quoted 11 lines
include/uapi/linux/mctp.h +3 -0
index 19ad12a0cd4b..7c3d4a936861 100644
@@ -55,6 +55,19 @@ struct mctp_fq_addr {
 #define MCTP_TAG_PREALLOC	0x10
 
 #define MCTP_OPT_ADDR_EXT	1
+#define MCTP_OPT_ROUTE_SRCADDR	2
+
+/* Query structure for MCTP_OPT_ROUTE_SRCADDR getsockopt.

include/uapi/linux/mctp.h struct mctp_fq_addr { · line 70

Quoted 10 lines
include/uapi/linux/mctp.h +10 -0
+ *
+ * Caller fills in @net and @daddr before calling getsockopt.
+ * Performs a route lookup and returns the local source EID in @saddr.
+ */
+struct mctp_route_srcaddr {
+	unsigned int	net;
+	mctp_eid_t	daddr;
+	mctp_eid_t	saddr;
+	__u8		__pad[2];
+};

[Severity: Low]
Should this new option also be described in Documentation/networking/mctp.rst? A grep for sockopt or MCTP_OPT in that file returns no matches, while the same file documents the other userspace-visible MCTP interfaces in detail, including ioctl(SIOCMCTPALLOCTAG) and ioctl(SIOCMCTPDROPTAG) along with their "other fields must be zero" rules.

The four-line comment here is the only in-tree description, and it leaves out several behaviours that the implementation has:

- optval is used as both input and output buffer
- optlen must be exactly sizeof(struct mctp_route_srcaddr)
- net == MCTP_NET_ANY is translated to mctp_default_net()
- the resolved net is not written back into @net
- -EHOSTUNREACH when no route exists
- -EADDRNOTAVAIL when the route resolves with no local EID

On the MCTP_NET_ANY case in particular: since @net is left as 0 on return, how is a caller on a multi-net system supposed to know which network the returned EID belongs to? That is the same ambiguity the deprecation note on struct mctp_ioc_tag_ctl warns about. Would writing the resolved net back into rsa.net be reasonable here?

Quoted 23 lines
net/mctp/af_mctp.c +15 -0
index 8af5e2b3c8d1..5c6077e0367e 100644
@@ -425,6 +425,37 @@ static int mctp_getsockopt(struct socket *sock, int level, int optname,
 		return 0;
 	}
 
+	if (optname == MCTP_OPT_ROUTE_SRCADDR) {
+		struct mctp_route_srcaddr rsa;
+		struct mctp_dst dst;
+		unsigned int net;
+		int rc;
+
+		if (len != sizeof(rsa))
+			return -EINVAL;
+
+		if (copy_from_iter(&rsa, len, &opt->iter_in) != len)
+			return -EFAULT;
+
+		net = rsa.net;
+		if (net == MCTP_NET_ANY)
+			net = mctp_default_net(sock_net(sock->sk));

net/mctp/af_mctp.c static int mctp_getsockopt(struct socket *sock, int level, int optname, · line 452

Quoted 10 lines
net/mctp/af_mctp.c +10 -0
+
+		rc = mctp_route_lookup(sock_net(sock->sk), net, rsa.daddr, &dst);
+		if (rc)
+			return rc;
+
+		rsa.saddr = dst.saddr;
+		mctp_dst_release(&dst);
+
+		if (rsa.saddr == MCTP_ADDR_NULL)
+			return -EADDRNOTAVAIL;

[Severity: Medium]
Is this -EADDRNOTAVAIL consistent with what the output path does for the same lookup result?

mctp_route_lookup() in net/mctp/route.c deliberately accepts saddr == MCTP_ADDR_NULL for a direct route, and only rejects it once a gateway has been traversed:

	if (rt->dst_type == MCTP_ROUTE_DIRECT) {
		mctp_eid_t saddr;
		...
		saddr = mctp_dev_saddr(rt->dev);

		/* cannot do gateway-ed routes without a src  */
		if (saddr == MCTP_ADDR_NULL && depth != 0) {
			mctp_dev_put(rt->dev);
			break;
		}

		if (dst)
			mctp_dst_from_route(dst, daddr, saddr, mtu, rt);

So for a direct route at depth 0 the lookup succeeds with dst.saddr == 0, and mctp_local_output() then uses it without complaint:

	hdr->dest = daddr;
	hdr->src = dst->saddr;

That state is exercised today by the existing kunit case mctp_test_route_output_direct_no_eids, where transmit over a direct route with no local EID is expected to succeed.

So if a direct route exists to daddr on a device with no local address assigned, sendmsg() transmits with src EID 0 while this new option reports -EADDRNOTAVAIL. Does that conflict with the commit message claim:

the kernel performs the same route lookup used for actual packet output and returns the resolved local EID in saddr

If returning an error for the unassigned-local-EID case is intentional, could the commit message and the uapi comment say so, and could a kunit case be added for it? Neither of the two new cases covers a successful lookup that yields MCTP_ADDR_NULL.

Quoted 8 lines
net/mctp/af_mctp.c +6 -0
+
+		if (copy_to_iter(&rsa, len, &opt->iter_out) != len)
+			return -EFAULT;
+		return 0;
+	}
+
 	return -ENOPROTOOPT;
 }

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260921035118.1649687-1-faizana%40nvidia.com