Skip to contentSkip to conversation

mctp: Implement extended addressing

PATCH Applied Jeremy Kerr 4 participants 5 messages Oct 26, 2021

Subsystems (4)
lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

PATCH v6 1/1 mctp: Implement extended addressing

This change allows an extended address struct - struct sockaddr_mctp_ext
- to be passed to sendmsg/recvmsg. This allows userspace to specify output ifindex and physical address information (for sendmsg) or receive the input ifindex/physaddr for incoming messages (for recvmsg). This is typically used by userspace for MCTP address discovery and assignment operations.

The extended addressing facility is conditional on a new sockopt:
MCTP_OPT_ADDR_EXT; userspace must explicitly enable addressing before the kernel will consume/populate the extended address data.

Includes a fix for an uninitialised var:

---
v6:
- use MAX_ADDR_LEN for mctp_skb_cb.haddr, and add a BUILD_BUG_ON for checking suitable cb size.

v5:
- rebase to net-next, where we've added explicit padding to struct sockaddr_mctp. Consequently, pad sockaddr_mctp_ext too, and unify types.

v4:
- move SOL_MCTP to linux/sockets.h

v3:
- avoid uninitialised rc if we hit the !rt->dev WARN.

v2:
- non-RFC
- learn to spell "typically"

RFC: this patch adds a bit of an new ABI, in that the struct sockaddr for MCTP has been extended, with extra addressing data being available for applications after setting a sockopt.

Using something like IP_PKTINFO might be suitable instead, but that requires sendmsg/recvmsg, and control message setup, whereas this is a a simpler interface, and also allows extended addressing info in sendto/recvfrom too. Comments most welcome!

---
 include/linux/socket.h    |  1 +
 include/net/mctp.h        | 13 ++++--
 include/uapi/linux/mctp.h | 11 +++++
 net/mctp/af_mctp.c        | 86 ++++++++++++++++++++++++++++++----
 net/mctp/route.c          | 98 +++++++++++++++++++++++++++++----------
 5 files changed, 170 insertions(+), 39 deletions(-)
include/linux/socket.h +1 -0
index 7612d760b6a9..8ef26d89ef49 100644
@@ -365,6 +365,7 @@ struct ucred {
 #define SOL_TLS		282
 #define SOL_XDP		283
 #define SOL_MPTCP	284
+#define SOL_MCTP	285
 
 /* IPX options */
 #define IPX_TYPE	1
include/net/mctp.h +9 -4
index 2a83443bdfac..23bec708f4c7 100644
@@ -11,6 +11,7 @@
 
 #include <linux/bits.h>
 #include <linux/mctp.h>
+#include <linux/netdevice.h>
 #include <net/net_namespace.h>
 #include <net/sock.h>
 
@@ -58,6 +59,9 @@ struct mctp_sock {
 	mctp_eid_t	bind_addr;
 	__u8		bind_type;
 
+	/* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */
+	bool		addr_ext;
+
 	/* list of mctp_sk_key, for incoming tag lookup. updates protected
 	 * by sk->net->keys_lock
 	 */
@@ -153,7 +157,10 @@ struct mctp_sk_key {
 struct mctp_skb_cb {
 	unsigned int	magic;
 	unsigned int	net;
+	int		ifindex; /* extended/direct addressing if set */
 	mctp_eid_t	src;
+	unsigned char	halen;
+	unsigned char	haddr[MAX_ADDR_LEN];
 };
 
 /* skb control-block accessors with a little extra debugging for initial
@@ -177,6 +184,7 @@ static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
 {
 	struct mctp_skb_cb *cb = (void *)skb->cb;
 
+	BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb));
 	WARN_ON(cb->magic != 0x4d435450);
 	return (void *)(skb->cb);
 }
@@ -189,8 +197,7 @@ static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
  *
  * Updates to the route table are performed under rtnl; all reads under RCU,
  * so routes cannot be referenced over a RCU grace period. Specifically: A
- * caller cannot block between mctp_route_lookup and passing the route to
- * mctp_do_route.
+ * caller cannot block between mctp_route_lookup and mctp_route_release()
  */
 struct mctp_route {
 	mctp_eid_t		min, max;
@@ -210,8 +217,6 @@ struct mctp_route {
 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
 				     mctp_eid_t daddr);
 
-int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb);
-
 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
 
include/uapi/linux/mctp.h +11 -0
index 6acd4ccafbf7..07b0318716fc 100644
@@ -11,6 +11,7 @@
 
 #include <linux/types.h>
 #include <linux/socket.h>
+#include <linux/netdevice.h>
 
 typedef __u8			mctp_eid_t;
 
@@ -28,6 +29,14 @@ struct sockaddr_mctp {
 	__u8			__smctp_pad1;
 };
 
+struct sockaddr_mctp_ext {
+	struct sockaddr_mctp	smctp_base;
+	int			smctp_ifindex;
+	__u8			smctp_halen;
+	__u8			__smctp_pad0[3];
+	__u8			smctp_haddr[MAX_ADDR_LEN];
+};
+
 #define MCTP_NET_ANY		0x0
 
 #define MCTP_ADDR_NULL		0x00
@@ -36,4 +45,6 @@ struct sockaddr_mctp {
 #define MCTP_TAG_MASK		0x07
 #define MCTP_TAG_OWNER		0x08
 
+#define MCTP_OPT_ADDR_EXT	1
+
 #endif /* __UAPI_MCTP_H */
net/mctp/af_mctp.c +76 -10

136 lines not on this page · raw message

net/mctp/route.c +73 -25

178 lines not on this page · raw message

Signature · 1 line
2.33.0
#1 lore raw

From: Jeremy Kerr

Quoted 8 lines

This change allows an extended address struct - struct sockaddr_mctp_ext
- to be passed to sendmsg/recvmsg. This allows userspace to specify output ifindex and physical address information (for sendmsg) or receive the input ifindex/physaddr for incoming messages (for recvmsg). This is typically used by userspace for MCTP address discovery and assignment operations.

...

Quoted 13 lines
include/uapi/linux/mctp.h +1 -0
index 6acd4ccafbf7..07b0318716fc 100644
@@ -11,6 +11,7 @@
 
 #include <linux/types.h>
 #include <linux/socket.h>
+#include <linux/netdevice.h>
 
 typedef __u8			mctp_eid_t;
 

include/uapi/linux/mctp.h struct sockaddr_mctp { · line 38

Quoted 10 lines
include/uapi/linux/mctp.h +7 -0
@@ -28,6 +29,14 @@ struct sockaddr_mctp {
 	__u8			__smctp_pad1;
 };
 
+struct sockaddr_mctp_ext {
+	struct sockaddr_mctp	smctp_base;
+	int			smctp_ifindex;
+	__u8			smctp_halen;
+	__u8			__smctp_pad0[3];
+	__u8			smctp_haddr[MAX_ADDR_LEN];
+};

You'd be better off 8-byte aligning smctp_haddr.
I also suspect that always copying the 32 bytes will be faster and generate less code than the memset() + memcpy().

Oh and how did MAX_ADDR_LEN ever get into a uapi header??

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author maintainer
#2 lore raw

Hi David,

Quoted 11 lines

include/uapi/linux/mctp.h struct sockaddr_mctp { · line 38

Quoted 7 lines
include/uapi/linux/mctp.h +7 -0
+struct sockaddr_mctp_ext {
+       struct sockaddr_mctp    smctp_base;
+       int                     smctp_ifindex;
+       __u8                    smctp_halen;
+       __u8                    __smctp_pad0[3];
+       __u8                    smctp_haddr[MAX_ADDR_LEN];
+};

You'd be better off 8-byte aligning smctp_haddr.
I also suspect that always copying the 32 bytes will be faster and generate less code than the memset() + memcpy().

The padding here is more to avoid layout variations between ABIs rather than performance.

The largest current hardware address size that we need (for the i2c transport) is... 1 byte. If we were to implement the PCIe VDM binding for MCTP that'd then be the largest, now at 2 bytes. If anyone's crazy enough to do MCTP over ethernet, we're still only at 6.

So, we'll be a long way off needing to optimise for 8-byte aligned accesses here; I don't think the extra padding would be worth it.

Cheers,

Jeremy

#3 lore raw
In reply to David Laight
Quoted 1 line · David Laight

Oh and how did MAX_ADDR_LEN ever get into a uapi header??

It is defined in <linux/netdevice.h> since v1.1.4 and has its current value of 32 since v2.5.55~64^2^2~8 (before increasing for 7 to 8 in v2.4.6.9).

#4 lore raw
In reply to Jeremy Kerr
This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:

Quoted 8 lines · On Tue, 26 Oct 2021 09:57:28 +0800 you wrote:

This change allows an extended address struct - struct sockaddr_mctp_ext
- to be passed to sendmsg/recvmsg. This allows userspace to specify output ifindex and physical address information (for sendmsg) or receive the input ifindex/physaddr for incoming messages (for recvmsg). This is typically used by userspace for MCTP address discovery and assignment operations.

[...]

Here is the summary with links:
- [net-next,v6] mctp: Implement extended addressing

    https://git.kernel.org/netdev/net-next/c/99ce45d5e7db

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html