Skip to contentSkip to conversation

mptcp: add SOL_MPTCP getsockopt support

PATCH In review Florian Westphal 4 participants 9 messages Aug 18, 2021 – Aug 30, 2021

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

v3 cover mptcp: add SOL_MPTCP getsockopt support

Changes in v3:
- address comments from Mat.
This only affects selftest program (init 'fd' to -1). Other than that, the only changes are in commit messages or re-format of code (reverse xmas).

Changes in v2:
- move #define to start of file (Mat)
- refer to '"multipath-tcp.org"', not mptcp.org (Mat)
- use __kernel_sa_family_t in uapi header (test robot)
- add test cases with invalid inputs (Paolo)
- avoid duplicate mptcp_sockopt.h in Makefile (Paolo)
- remove _u_optlen (Paolo)

original cover letter:

This adds the MPTCP_INFO, MPTCP_TCPINFO and MPTCP_SUBFLOW_ADDRS mptcp getsockopt optnames.

MPTCP_INFO exposes the mptcp_info struct as an alternative to the existing netlink diag interface.

MPTCP_TCPINFO exposes the tcp_info struct.
Unlike SOL_TCP/TCP_INFO, this returns one struct for each active subflow.

MPTCP_SUBFLOW_ADDRS allows userspace to discover the ip addresses/ports used by the local and remote endpoints, one for each active tcp subflow.

MPTCP_TCPINFO and MPTCP_SUBFLOW_ADDRS share the same meta-header that needs to be pre-filled by userspace with the size of the data structures it expects. This is done to allow extension of the involved structs later on, without breaking backwards compatibility.

The meta-structure can also be used to discover the required space to obtain all information, as kernel will fill in the number of active subflows even if there is not enough room for the requested info itself.

More information is available in the individual patches. Last patch adds test cases for the three optnames.

Florian Westphal (5):
mptcp: add new mptcp_fill_diag helper
mptcp: add MPTCP_INFO getsockopt
mptcp: add MPTCP_TCPINFO getsockopt support
mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support
selftests: mptcp: add mptcp getsockopt test cases

 include/linux/socket.h                        |   1 +
 include/net/mptcp.h                           |   4 +
 include/uapi/linux/mptcp.h                    |  29 +
 net/mptcp/mptcp_diag.c                        |  26 +-
 net/mptcp/sockopt.c                           | 276 ++++++++
 tools/testing/selftests/net/mptcp/Makefile    |   2 +-
 .../selftests/net/mptcp/mptcp_sockopt.c       | 644 ++++++++++++++++++
 .../selftests/net/mptcp/mptcp_sockopt.sh      |  31 +-
 8 files changed, 985 insertions(+), 28 deletions(-)
 create mode 100644 tools/testing/selftests/net/mptcp/mptcp_sockopt.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/5 mptcp: add new mptcp_fill_diag helper

Will be re-used from getsockopt path.
Since diag can be a module, we can't export the helper from diag, it needs to be moved to core.

---
 include/net/mptcp.h    |  4 ++++
 net/mptcp/mptcp_diag.c | 26 +-------------------------
 net/mptcp/sockopt.c    | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 25 deletions(-)
include/net/mptcp.h +4 -0
index 6026bbefbffd..f83fa48408b3 100644
@@ -12,6 +12,8 @@
 #include <linux/tcp.h>
 #include <linux/types.h>
 
+struct mptcp_info;
+struct mptcp_sock;
 struct seq_file;
 
 /* MPTCP sk_buff extension data */
@@ -121,6 +123,8 @@ bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
 void mptcp_write_options(__be32 *ptr, const struct tcp_sock *tp,
 			 struct mptcp_out_options *opts);
 
+void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info);
+
 /* move the skb extension owership, with the assumption that 'to' is
  * newly allocated
  */
net/mptcp/mptcp_diag.c +1 -25
index f48eb6315bbb..fdf0c1f15a65 100644
@@ -113,37 +113,13 @@ static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	struct mptcp_info *info = _info;
-	u32 flags = 0;
-	bool slow;
-	u8 val;
 
 	r->idiag_rqueue = sk_rmem_alloc_get(sk);
 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
 	if (!info)
 		return;
 
-	slow = lock_sock_fast(sk);
-	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
-	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
-	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
-	info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
-	info->mptcpi_subflows_max = mptcp_pm_get_subflows_max(msk);
-	val = mptcp_pm_get_add_addr_signal_max(msk);
-	info->mptcpi_add_addr_signal_max = val;
-	val = mptcp_pm_get_add_addr_accept_max(msk);
-	info->mptcpi_add_addr_accepted_max = val;
-	info->mptcpi_local_addr_max = mptcp_pm_get_local_addr_max(msk);
-	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
-		flags |= MPTCP_INFO_FLAG_FALLBACK;
-	if (READ_ONCE(msk->can_ack))
-		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
-	info->mptcpi_flags = flags;
-	info->mptcpi_token = READ_ONCE(msk->token);
-	info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
-	info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
-	info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
-	info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
-	unlock_sock_fast(sk, slow);
+	mptcp_diag_fill_info(msk, info);
 }
 
 static const struct inet_diag_handler mptcp_diag_handler = {
net/mptcp/sockopt.c +32 -0
index 8c03afac5ca0..54f0d521a399 100644
@@ -670,6 +670,38 @@ static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int
 	return ret;
 }
 
+void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
+{
+	struct sock *sk = &msk->sk.icsk_inet.sk;
+	bool slow = lock_sock_fast(sk);
+	u32 flags = 0;
+	u8 val;
+
+	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
+	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
+	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
+	info->mptcpi_local_addr_used = READ_ONCE(msk->pm.local_addr_used);
+	info->mptcpi_subflows_max = mptcp_pm_get_subflows_max(msk);
+	val = mptcp_pm_get_add_addr_signal_max(msk);
+	info->mptcpi_add_addr_signal_max = val;
+	val = mptcp_pm_get_add_addr_accept_max(msk);
+	info->mptcpi_add_addr_accepted_max = val;
+	info->mptcpi_local_addr_max = mptcp_pm_get_local_addr_max(msk);
+	if (test_bit(MPTCP_FALLBACK_DONE, &msk->flags))
+		flags |= MPTCP_INFO_FLAG_FALLBACK;
+	if (READ_ONCE(msk->can_ack))
+		flags |= MPTCP_INFO_FLAG_REMOTE_KEY_RECEIVED;
+	info->mptcpi_flags = flags;
+	info->mptcpi_token = READ_ONCE(msk->token);
+	info->mptcpi_write_seq = READ_ONCE(msk->write_seq);
+	info->mptcpi_snd_una = READ_ONCE(msk->snd_una);
+	info->mptcpi_rcv_nxt = READ_ONCE(msk->ack_seq);
+	info->mptcpi_csum_enabled = READ_ONCE(msk->csum_enabled);
+
+	unlock_sock_fast(sk, slow);
+}
+EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
+
 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 				    char __user *optval, int __user *optlen)
 {
Signature · 1 line
2.31.1
#2 lore raw

PATCH v3 2/5 mptcp: add MPTCP_INFO getsockopt

In reply to Florian Westphal

Its not compatible with multipath-tcp.org kernel one.

1. The out-of-tree implementation defines a different 'struct mptcp_info', with embedded __user addresses for additional data such as endpoint addresses.

2. Mat Martineau points out that embedded __user addresses doesn't work with BPF_CGROUP_RUN_PROG_GETSOCKOPT() which assumes that copying in optsize bytes from optval provides all data that got copied to userspace.

This provides mptcp_info data for the given mptcp socket.

Userspace sets optlen to the size of the structure it expects. The kernel updates it to contain the number of bytes that it copied.

This allows to append more information to the structure later.

---
 include/linux/socket.h     |  1 +
 include/uapi/linux/mptcp.h |  4 ++++
 net/mptcp/sockopt.c        | 40 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 44 insertions(+), 1 deletion(-)
include/linux/socket.h +1 -0
index fd9ce51582d8..a2326a963232 100644
@@ -364,6 +364,7 @@ struct ucred {
 #define SOL_KCM		281
 #define SOL_TLS		282
 #define SOL_XDP		283
+#define SOL_MPTCP	284
 
 /* IPX options */
 #define IPX_TYPE	1
include/uapi/linux/mptcp.h +4 -0
index f66038b9551f..c450feba6f9c 100644
@@ -4,6 +4,7 @@
 
 #include <linux/const.h>
 #include <linux/types.h>
+#include <linux/in.h>
 
 #define MPTCP_SUBFLOW_FLAG_MCAP_REM		_BITUL(0)
 #define MPTCP_SUBFLOW_FLAG_MCAP_LOC		_BITUL(1)
@@ -193,4 +194,7 @@ enum mptcp_event_attr {
 #define MPTCP_RST_EBADPERF	5
 #define MPTCP_RST_EMIDDLEBOX	6
 
+/* MPTCP socket options */
+#define MPTCP_INFO 1
+
 #endif /* _UAPI_MPTCP_H */
net/mptcp/sockopt.c +39 -1
index 54f0d521a399..f7683c22911f 100644
@@ -673,10 +673,14 @@ static int mptcp_getsockopt_first_sf_only(struct mptcp_sock *msk, int level, int
 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
 {
 	struct sock *sk = &msk->sk.icsk_inet.sk;
-	bool slow = lock_sock_fast(sk);
 	u32 flags = 0;
+	bool slow;
 	u8 val;
 
+	memset(info, 0, sizeof(*info));
+
+	slow = lock_sock_fast(sk);
+
 	info->mptcpi_subflows = READ_ONCE(msk->pm.subflows);
 	info->mptcpi_add_addr_signal = READ_ONCE(msk->pm.add_addr_signaled);
 	info->mptcpi_add_addr_accepted = READ_ONCE(msk->pm.add_addr_accepted);
@@ -702,6 +706,27 @@ void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info)
 }
 EXPORT_SYMBOL_GPL(mptcp_diag_fill_info);
 
+static int mptcp_getsockopt_info(struct mptcp_sock *msk, char __user *optval, int __user *optlen)
+{
+	struct mptcp_info m_info;
+	int len;
+
+	if (get_user(len, optlen))
+		return -EFAULT;
+
+	len = min_t(unsigned int, len, sizeof(struct mptcp_info));
+
+	mptcp_diag_fill_info(msk, &m_info);
+
+	if (put_user(len, optlen))
+		return -EFAULT;
+
+	if (copy_to_user(optval, &m_info, len))
+		return -EFAULT;
+
+	return 0;
+}
+
 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 				    char __user *optval, int __user *optlen)
 {
@@ -716,6 +741,17 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 	return -EOPNOTSUPP;
 }
 
+static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
+				      char __user *optval, int __user *optlen)
+{
+	switch (optname) {
+	case MPTCP_INFO:
+		return mptcp_getsockopt_info(msk, optval, optlen);
+	}
+
+	return -EOPNOTSUPP;
+}
+
 int mptcp_getsockopt(struct sock *sk, int level, int optname,
 		     char __user *optval, int __user *option)
 {
@@ -738,6 +774,8 @@ int mptcp_getsockopt(struct sock *sk, int level, int optname,
 
 	if (level == SOL_TCP)
 		return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
+	if (level == SOL_MPTCP)
+		return mptcp_getsockopt_sol_mptcp(msk, optname, optval, option);
 	return -EOPNOTSUPP;
 }
 
Signature · 1 line
2.31.1
#3 lore raw

PATCH v3 3/5 mptcp: add MPTCP_TCPINFO getsockopt support

In reply to Florian Westphal

Allow users to retrieve TCP_INFO data of all subflows.

Users need to pre-initialize a meta header that has to be prepended to the data buffer that will be filled with the tcp info data.

The meta header looks like this:

struct mptcp_subflow_data {
__u32 size_subflow_data;/* size of this structure in userspace */
__u32 num_subflows; /* must be 0, set by kernel */
__u32 size_kernel; /* must be 0, set by kernel */
__u32 size_user; /* size of one element in data[] */
} __attribute__((aligned(8)));

size_subflow_data has to be set to 'sizeof(struct mptcp_subflow_data)'. This allows to extend mptcp_subflow_data structure later on without breaking backwards compatibility.

If the structure is extended later on, kernel knows where the userspace-provided meta header ends, even if userspace uses an older (smaller) version of the structure.

num_subflows must be set to 0. If the getsockopt request succeeds (return value is 0), it will be updated to contain the number of active subflows for the given logical connection.

size_kernel must be set to 0. If the getsockopt request is successful, it will contain the size of the 'struct tcp_info' as known by the kernel. This is informational only.

size_user must be set to 'sizeof(struct tcp_info)'.

This allows the kernel to only fill in the space reserved/expected by userspace.

Example:

struct my_tcp_info {
struct mptcp_subflow_data d;
struct tcp_info ti[2];
};
struct my_tcp_info ti;
socklen_t olen;

memset(&ti, 0, sizeof(ti));

ti.d.size_subflow_data = sizeof(struct mptcp_subflow_data); ti.d.size_user = sizeof(struct tcp_info);
olen = sizeof(ti);

ret = getsockopt(fd, SOL_MPTCP, MPTCP_TCPINFO, &ti, &olen); if (ret < 0)

	die_perror("getsockopt MPTCP_TCPINFO");

mptcp_subflow_data.num_subflows is populated with the number of subflows that exist on the kernel side for the logical mptcp connection.

This allows userspace to re-try with a larger tcp_info array if the number of subflows was larger than the available space in the ti[] array.

olen has to be set to the number of bytes that userspace has allocated to receive the kernel data. It will be updated to contain the real number bytes that have been copied to by the kernel.

In the above example, if the number if subflows was 1, olen is equal to 'sizeof(struct mptcp_subflow_data) + sizeof(struct tcp_info). For 2 or more subflows olen is equal to 'sizeof(struct my_tcp_info)'.

If there was more data that could not be copied due to lack of space in the option buffer, userspace can detect this by checking mptcp_subflow_data->num_subflows.

---
 include/uapi/linux/mptcp.h |  10 +++-
 net/mptcp/sockopt.c        | 115 +++++++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+), 1 deletion(-)
include/uapi/linux/mptcp.h +9 -1
index c450feba6f9c..7c368fccd83e 100644
@@ -194,7 +194,15 @@ enum mptcp_event_attr {
 #define MPTCP_RST_EBADPERF	5
 #define MPTCP_RST_EMIDDLEBOX	6
 
+struct mptcp_subflow_data {
+	__u32		size_subflow_data;		/* size of this structure in userspace */
+	__u32		num_subflows;			/* must be 0, set by kernel */
+	__u32		size_kernel;			/* must be 0, set by kernel */
+	__u32		size_user;			/* size of one element in data[] */
+} __attribute__((aligned(8)));
+
 /* MPTCP socket options */
-#define MPTCP_INFO 1
+#define MPTCP_INFO		1
+#define MPTCP_TCPINFO		2
 
 #endif /* _UAPI_MPTCP_H */
net/mptcp/sockopt.c +115 -0

133 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

PATCH v3 4/5 mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support

In reply to Florian Westphal

This retrieves the address pairs of all subflows currently active for a given mptcp connection.

It re-uses the same meta-header as for MPTCP_TCPINFO.

A new structure is provided to hold the subflow address data:

struct mptcp_subflow_addrs {
	union {
		__kernel_sa_family_t sa_family;
		struct sockaddr sa_local;
		struct sockaddr_in sin_local;
		struct sockaddr_in6 sin6_local;
		struct sockaddr_storage ss_local;
	};
	union {
		struct sockaddr sa_remote;
		struct sockaddr_in sin_remote;
		struct sockaddr_in6 sin6_remote;
		struct sockaddr_storage ss_remote;
	};
};

Usage of the new getsockopt is very similar to MPTCP_TCPINFO one.

Userspace allocates a
'struct mptcp_subflow_data', followed by one or more 'struct mptcp_subflow_addrs', then inits the mptcp_subflow_data structure as follows:

struct mptcp_subflow_addrs *sf_addr;
struct mptcp_subflow_data *addr;
socklen_t olen = sizeof(*addr) + (8 * sizeof(*sf_addr));

addr = malloc(olen);
addr->size_subflow_data = sizeof(*addr); addr->num_subflows = 0;
addr->size_kernel = 0;
addr->size_user = sizeof(struct mptcp_subflow_addrs);

sf_addr = (struct mptcp_subflow_addrs *)(addr + 1);

and then retrieves the endpoint addresses via:
ret = getsockopt(fd, SOL_MPTCP, MPTCP_SUBFLOW_ADDRS,

		 addr, &olen);

If the call succeeds, kernel will have added up to 8 endpoint addresses after the 'mptcp_subflow_data' header.

Userspace needs to re-check 'olen' value to detect how many bytes have been filled in by the kernel.

Userspace can check addr->num_subflows to discover when there were more subflows that available data space.

---
 include/uapi/linux/mptcp.h | 17 +++++++
 net/mptcp/sockopt.c        | 91 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)
include/uapi/linux/mptcp.h +17 -0
index 7c368fccd83e..c368c11a0ed9 100644
@@ -201,8 +201,25 @@ struct mptcp_subflow_data {
 	__u32		size_user;			/* size of one element in data[] */
 } __attribute__((aligned(8)));
 
+struct mptcp_subflow_addrs {
+	union {
+		__kernel_sa_family_t sa_family;
+		struct sockaddr sa_local;
+		struct sockaddr_in sin_local;
+		struct sockaddr_in6 sin6_local;
+		struct sockaddr_storage ss_local;
+	};
+	union {
+		struct sockaddr sa_remote;
+		struct sockaddr_in sin_remote;
+		struct sockaddr_in6 sin6_remote;
+		struct sockaddr_storage ss_remote;
+	};
+};
+
 /* MPTCP socket options */
 #define MPTCP_INFO		1
 #define MPTCP_TCPINFO		2
+#define MPTCP_SUBFLOW_ADDRS	3
 
 #endif /* _UAPI_MPTCP_H */
net/mptcp/sockopt.c +91 -0

103 lines not on this page · raw message

Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH v3 mptcp-next 4/5] mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support

Hello,

I got this compile error in my Fedora-34 system (export/20210829T054626):

DESCEND objtool
DESCEND bpf/resolve_btfids

  CALL    scripts/atomic/check-atomics.sh
  CALL    scripts/checksyscalls.sh
  CHK     include/generated/compile.h

HDRTEST usr/include/linux/mptcp.h
In file included from <command-line>:
./usr/include/linux/mptcp.h:207:33: error: field ‘sa_local’ has incomplete type
207 | struct sockaddr sa_local;
| ^~~~~~~~
./usr/include/linux/mptcp.h:209:37: error: field ‘sin6_local’ has incomplete type
209 | struct sockaddr_in6 sin6_local;
| ^~~~~~~~~~
./usr/include/linux/mptcp.h:210:41: error: field ‘ss_local’ has incomplete type
210 | struct sockaddr_storage ss_local;
| ^~~~~~~~
./usr/include/linux/mptcp.h:213:33: error: field ‘sa_remote’ has incomplete type
213 | struct sockaddr sa_remote;
| ^~~~~~~~~
./usr/include/linux/mptcp.h:215:37: error: field ‘sin6_remote’ has incomplete type
215 | struct sockaddr_in6 sin6_remote;
| ^~~~~~~~~~~
./usr/include/linux/mptcp.h:216:41: error: field ‘ss_remote’ has incomplete type
216 | struct sockaddr_storage ss_remote;
| ^~~~~~~~~
make[2]: *** [usr/include/Makefile:106: usr/include/linux/mptcp.hdrtest] Error 1 make[1]: *** [scripts/Makefile.build:514: usr/include] Error 2
make: *** [Makefile:1851: usr] Error 2
make: *** Waiting for unfinished jobs....

How can I fix it, please give me some suggestion.

Thanks,
-Geliang

Quoted 207 lines · Florian Westphal <[email protected]> 于2021年8月18日周三 下午6:28写道:

This retrieves the address pairs of all subflows currently active for a given mptcp connection.

It re-uses the same meta-header as for MPTCP_TCPINFO.

A new structure is provided to hold the subflow address data:

struct mptcp_subflow_addrs {
        union {
                __kernel_sa_family_t sa_family;
                struct sockaddr sa_local;
                struct sockaddr_in sin_local;
                struct sockaddr_in6 sin6_local;
                struct sockaddr_storage ss_local;
        };
        union {
                struct sockaddr sa_remote;
                struct sockaddr_in sin_remote;
                struct sockaddr_in6 sin6_remote;
                struct sockaddr_storage ss_remote;
        };
};

Usage of the new getsockopt is very similar to MPTCP_TCPINFO one.

Userspace allocates a
'struct mptcp_subflow_data', followed by one or more 'struct mptcp_subflow_addrs', then inits the mptcp_subflow_data structure as follows:

struct mptcp_subflow_addrs *sf_addr;
struct mptcp_subflow_data *addr;
socklen_t olen = sizeof(*addr) + (8 * sizeof(*sf_addr));

addr = malloc(olen);
addr->size_subflow_data = sizeof(*addr); addr->num_subflows = 0;
addr->size_kernel = 0;
addr->size_user = sizeof(struct mptcp_subflow_addrs);

sf_addr = (struct mptcp_subflow_addrs *)(addr + 1);

and then retrieves the endpoint addresses via:
ret = getsockopt(fd, SOL_MPTCP, MPTCP_SUBFLOW_ADDRS,

                 addr, &olen);

If the call succeeds, kernel will have added up to 8 endpoint addresses after the 'mptcp_subflow_data' header.

Userspace needs to re-check 'olen' value to detect how many bytes have been filled in by the kernel.

Userspace can check addr->num_subflows to discover when there were more subflows that available data space.

---
 include/uapi/linux/mptcp.h | 17 +++++++
 net/mptcp/sockopt.c        | 91 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+)
include/uapi/linux/mptcp.h +17 -0
index 7c368fccd83e..c368c11a0ed9 100644
@@ -201,8 +201,25 @@ struct mptcp_subflow_data {
        __u32           size_user;                      /* size of one element in data[] */
 } __attribute__((aligned(8)));
 
+struct mptcp_subflow_addrs {
+       union {
+               __kernel_sa_family_t sa_family;
+               struct sockaddr sa_local;
+               struct sockaddr_in sin_local;
+               struct sockaddr_in6 sin6_local;
+               struct sockaddr_storage ss_local;
+       };
+       union {
+               struct sockaddr sa_remote;
+               struct sockaddr_in sin_remote;
+               struct sockaddr_in6 sin6_remote;
+               struct sockaddr_storage ss_remote;
+       };
+};
+
 /* MPTCP socket options */
 #define MPTCP_INFO             1
 #define MPTCP_TCPINFO          2
+#define MPTCP_SUBFLOW_ADDRS    3
 
 #endif /* _UAPI_MPTCP_H */
net/mptcp/sockopt.c +91 -1
index eb2905bfa089..8137cc3a4296 100644
@@ -840,6 +840,95 @@ static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
        return 0;
 }
 
+static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
+{
+       struct inet_sock *inet = inet_sk(sk);
+
+       memset(a, 0, sizeof(*a));
+
+       if (sk->sk_family == AF_INET) {
+               a->sin_local.sin_family = AF_INET;
+               a->sin_local.sin_port = inet->inet_sport;
+               a->sin_local.sin_addr.s_addr = inet->inet_rcv_saddr;
+
+               if (!a->sin_local.sin_addr.s_addr)
+                       a->sin_local.sin_addr.s_addr = inet->inet_saddr;
+
+               a->sin_remote.sin_family = AF_INET;
+               a->sin_remote.sin_port = inet->inet_dport;
+               a->sin_remote.sin_addr.s_addr = inet->inet_daddr;
+#if IS_ENABLED(CONFIG_IPV6)
+       } else if (sk->sk_family == AF_INET6) {
+               const struct ipv6_pinfo *np = inet6_sk(sk);
+
+               a->sin6_local.sin6_family = AF_INET6;
+               a->sin6_local.sin6_port = inet->inet_sport;
+
+               if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
+                       a->sin6_local.sin6_addr = np->saddr;
+               else
+                       a->sin6_local.sin6_addr = sk->sk_v6_rcv_saddr;
+
+               a->sin6_remote.sin6_family = AF_INET6;
+               a->sin6_remote.sin6_port = inet->inet_dport;
+               a->sin6_remote.sin6_addr = sk->sk_v6_daddr;
+#endif
+       }
+}
+
+static int mptcp_getsockopt_subflow_addrs(struct mptcp_sock *msk, char __user *optval,
+                                         int __user *optlen)
+{
+       struct sock *sk = &msk->sk.icsk_inet.sk;
+       struct mptcp_subflow_context *subflow;
+       unsigned int sfcount = 0, copied = 0;
+       struct mptcp_subflow_data sfd;
+       char __user *addrptr;
+       int len;
+
+       len = mptcp_get_subflow_data(&sfd, optval, optlen);
+       if (len < 0)
+               return len;
+
+       sfd.size_kernel = sizeof(struct mptcp_subflow_addrs);
+       sfd.size_user = min_t(unsigned int, sfd.size_user,
+                             sizeof(struct mptcp_subflow_addrs));
+
+       addrptr = optval + sfd.size_subflow_data;
+
+       lock_sock(sk);
+
+       mptcp_for_each_subflow(msk, subflow) {
+               struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
+
+               ++sfcount;
+
+               if (len && len >= sfd.size_user) {
+                       struct mptcp_subflow_addrs a;
+
+                       mptcp_get_sub_addrs(ssk, &a);
+
+                       if (copy_to_user(addrptr, &a, sfd.size_user)) {
+                               release_sock(sk);
+                               return -EFAULT;
+                       }
+
+                       addrptr += sfd.size_user;
+                       copied += sfd.size_user;
+                       len -= sfd.size_user;
+               }
+       }
+
+       release_sock(sk);
+
+       sfd.num_subflows = sfcount;
+
+       if (mptcp_put_subflow_data(&sfd, optval, copied, optlen))
+               return -EFAULT;
+
+       return 0;
+}
+
 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
                                    char __user *optval, int __user *optlen)
 {
@@ -862,6 +951,8 @@ static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
                return mptcp_getsockopt_info(msk, optval, optlen);
        case MPTCP_TCPINFO:
                return mptcp_getsockopt_tcpinfo(msk, optval, optlen);
+       case MPTCP_SUBFLOW_ADDRS:
+               return mptcp_getsockopt_subflow_addrs(msk, optval, optlen);
        }
 
        return -EOPNOTSUPP;
--

2.31.1

#6 lore raw

PATCH v3 5/5 selftests: mptcp: add mptcp getsockopt test cases

In reply to Florian Westphal

Add a test program that retrieves the three info types:
1. mptcp meta information
2. tcp info for subflow
3. subflow endpoint addresses

For all three rudimentary checks are added.

1. Meta information checks that the logical mptcp sequence numbers advance as expected, based on the bytes read (init seq + bytes_received/sent) and the connection state (after close, we should exect 1 extra byte due to FIN).

2. TCP info checks the number of bytes sent/received vs. sums of read/write syscall return values.

3. Subflow endpoint addresses are checked vs. getsockname/getpeername result.

Tests for forward compatibility (0-initialisation of output-only fields in mptcp_subflow_data structure) are added as well.

---
 tools/testing/selftests/net/mptcp/Makefile    |   2 +-
 .../selftests/net/mptcp/mptcp_sockopt.c       | 644 ++++++++++++++++++
 .../selftests/net/mptcp/mptcp_sockopt.sh      |  31 +-
 3 files changed, 674 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/net/mptcp/mptcp_sockopt.c
tools/testing/selftests/net/mptcp/Makefile +1 -1
index f1464f09b080..bbf4e448bad9 100644
@@ -8,7 +8,7 @@ CFLAGS =  -Wall -Wl,--no-as-needed -O2 -g  -I$(top_srcdir)/usr/include
 TEST_PROGS := mptcp_connect.sh pm_netlink.sh mptcp_join.sh diag.sh \
 	      simult_flows.sh mptcp_sockopt.sh
 
-TEST_GEN_FILES = mptcp_connect pm_nl_ctl
+TEST_GEN_FILES = mptcp_connect pm_nl_ctl mptcp_sockopt
 
 TEST_FILES := settings
 
tools/testing/selftests/net/mptcp/mptcp_sockopt.c new file +644 -0

644 lines not on this page · raw message

tools/testing/selftests/net/mptcp/mptcp_sockopt.sh +29 -2
index 1579e471a5e7..41de643788b8 100755
@@ -239,12 +239,35 @@ make_file()
 	echo "Created $name (size $size KB) containing data sent by $who"
 }
 
+do_mptcp_sockopt_tests()
+{
+	local lret=0
+
+	./mptcp_sockopt
+	lret=$?
+
+	if [ $lret -ne 0 ]; then
+		echo "FAIL: SOL_MPTCP getsockopt" 1>&2
+		ret=$lret
+		return
+	fi
+
+	./mptcp_sockopt -6
+	lret=$?
+
+	if [ $lret -ne 0 ]; then
+		echo "FAIL: SOL_MPTCP getsockopt (ipv6)" 1>&2
+		ret=$lret
+		return
+	fi
+}
+
 run_tests()
 {
 	listener_ns="$1"
 	connector_ns="$2"
 	connect_addr="$3"
-	lret=0
+	local lret=0
 
 	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr}
 
@@ -268,9 +291,13 @@ trap cleanup EXIT
 run_tests $ns1 $ns2 10.0.1.1
 run_tests $ns1 $ns2 dead:beef:1::1
 
-
 if [ $ret -eq 0 ];then
 	echo "PASS: all packets had packet mark set"
 fi
 
+do_mptcp_sockopt_tests
+if [ $ret -eq 0 ];then
+	echo "PASS: SOL_MPTCP getsockopt has expected information"
+fi
+
 exit $ret
Signature · 1 line
2.31.1
Reviewed-by on the v3 series
#7 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal

Changes in v3:
- address comments from Mat.
This only affects selftest program (init 'fd' to -1). Other than that, the only changes are in commit messages or re-format of code (reverse xmas).

Thanks for the final adjustments - looks ready for the export branch now.

Quoted 52 lines

Changes in v2:
- move #define to start of file (Mat)
- refer to '"multipath-tcp.org"', not mptcp.org (Mat)
- use __kernel_sa_family_t in uapi header (test robot)
- add test cases with invalid inputs (Paolo)
- avoid duplicate mptcp_sockopt.h in Makefile (Paolo)
- remove _u_optlen (Paolo)

original cover letter:

This adds the MPTCP_INFO, MPTCP_TCPINFO and MPTCP_SUBFLOW_ADDRS mptcp getsockopt optnames.

MPTCP_INFO exposes the mptcp_info struct as an alternative to the existing netlink diag interface.

MPTCP_TCPINFO exposes the tcp_info struct.
Unlike SOL_TCP/TCP_INFO, this returns one struct for each active subflow.

MPTCP_SUBFLOW_ADDRS allows userspace to discover the ip addresses/ports used by the local and remote endpoints, one for each active tcp subflow.

MPTCP_TCPINFO and MPTCP_SUBFLOW_ADDRS share the same meta-header that needs to be pre-filled by userspace with the size of the data structures it expects. This is done to allow extension of the involved structs later on, without breaking backwards compatibility.

The meta-structure can also be used to discover the required space to obtain all information, as kernel will fill in the number of active subflows even if there is not enough room for the requested info itself.

More information is available in the individual patches. Last patch adds test cases for the three optnames.

Florian Westphal (5):
mptcp: add new mptcp_fill_diag helper
mptcp: add MPTCP_INFO getsockopt
mptcp: add MPTCP_TCPINFO getsockopt support
mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support
selftests: mptcp: add mptcp getsockopt test cases

include/linux/socket.h                        |   1 +
include/net/mptcp.h                           |   4 +
include/uapi/linux/mptcp.h                    |  29 +
net/mptcp/mptcp_diag.c                        |  26 +-
net/mptcp/sockopt.c                           | 276 ++++++++
tools/testing/selftests/net/mptcp/Makefile    |   2 +-
.../selftests/net/mptcp/mptcp_sockopt.c       | 644 ++++++++++++++++++
.../selftests/net/mptcp/mptcp_sockopt.sh      |  31 +-
8 files changed, 985 insertions(+), 28 deletions(-)
Quoted 7 lines

create mode 100644 tools/testing/selftests/net/mptcp/mptcp_sockopt.c

Signature · 1 line
2.31.1

--
Mat Martineau
Intel

#8 lore raw
In reply to Florian Westphal

Hi Florian, Mat,

On 18/08/2021 12:27, Florian Westphal wrote:

(...)

Quoted 6 lines

Florian Westphal (5):
mptcp: add new mptcp_fill_diag helper
mptcp: add MPTCP_INFO getsockopt
mptcp: add MPTCP_TCPINFO getsockopt support
mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support
selftests: mptcp: add mptcp getsockopt test cases

Thank you for the patches and the reviews!

Now in our tree with Mat's RvB tags:

- d6eeba5de1d0: mptcp: add new mptcp_fill_diag helper
- d21d692880d0: mptcp: add MPTCP_INFO getsockopt
- b3b5ec766eea: mptcp: add MPTCP_TCPINFO getsockopt support
- efd9c3320c75: mptcp: add MPTCP_SUBFLOW_ADDRS getsockopt support
- f8d31bf4060d: selftests: mptcp: add mptcp getsockopt test cases
- Results: e2ce4b28903e..ab36a40efeaf

Builds and tests are now in progress:

https://cirrus-ci.com/github/multipath-tcp/mptcp_net-next/export/20210818T191724
https://github.com/multipath-tcp/mptcp_net-next/actions/workflows/build-validation.yml?query=branch:export/20210818T191724

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net