implement mptcp read_sock
Subsystems (3)
Geliang Tang
reviewerv6 cover implement mptcp read_sock
From: Geliang Tang <[email protected]>
v6:
- address Paolo's comment for v4, v5 (thanks)
v5:
- extract the common code of __mptcp_recvmsg_mskq() and mptcp_read_sock() into a new helper __mptcp_recvmsg_desc() to reduce duplication code.
v4:
- v3 doesn't work for MPTCP fallback tests in mptcp_connect.sh, this set fix it.
- invoke __mptcp_move_skbs in mptcp_read_sock.
- use INDIRECT_CALL_INET_1 in __tcp_splice_read.
v3:
- merge the two squash-to patches.
- use sk->sk_rcvbuf instead of INT_MAX as the max len in mptcp_read_sock().
- add splice io mode for mptcp_connect and drop mptcp_splice.c test.
- the splice test for packetdrill is also added here:
https://github.com/multipath-tcp/packetdrill/pull/162
v2:
- set splice_read of mptcp
- add a splice selftest.
I have good news! I recently added MPTCP support to "NVME over TCP". And my RFC patches are under review by NVME maintainer Hannes.
Replacing "NVME over TCP" with MPTCP is very simple. I used IPPROTO_MPTCP instead of IPPROTO_TCP to create MPTCP sockets on both target and host sides, these sockets are created in Kernel space.
nvmet_tcp_add_port:
ret = sock_create(port->addr.ss_family, SOCK_STREAM, IPPROTO_MPTCP, &port->sock);
nvme_tcp_alloc_queue:
ret = sock_create_kern(current->nsproxy->net_ns, ctrl->addr.ss_family, SOCK_STREAM, IPPROTO_MPTCP, &queue->sock);
nvme_tcp_try_recv() needs to call .read_sock interface of struct proto_ops, but it is not implemented in MPTCP. So I implemented it with reference to __mptcp_recvmsg_mskq().
Since the NVME part patches are still under reviewing, I only send the MPTCP part patches in this set to MPTCP ML for your opinions.
Geliang Tang (4):
mptcp: use sk_eat_skb in recvmsg_mskq
mptcp: implement .read_sock
mptcp: implement .splice_read
selftests: mptcp: add splice io mode
net/mptcp/protocol.c | 207 +++++++++++++++++- .../selftests/net/mptcp/mptcp_connect.c | 63 +++++- 2 files changed, 267 insertions(+), 3 deletions(-)
Signature · 1 line
2.48.1
Geliang Tang
reviewerPATCH v6 1/4 mptcp: use sk_eat_skb in recvmsg_mskq
From: Geliang Tang <[email protected]>
This patch uses sk_eat_skb() helper in __mptcp_recvmsg_mskq() instead of open-coding it.
--- net/mptcp/protocol.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/mptcp/protocol.c +1 -2
@@ -1891,8 +1891,7 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, skb->destructor =NULL ;atomic_sub (skb->truesize, &sk->sk_rmem_alloc);sk_mem_uncharge (sk, skb->truesize); -__skb_unlink (skb, &sk->sk_receive_queue); -__kfree_skb (skb); +sk_eat_skb (sk, skb); msk->bytes_consumed += count; }
Signature · 1 line
2.48.1
Geliang Tang
reviewerPATCH v6 2/4 mptcp: implement .read_sock
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
nvme_tcp_try_recv() needs to call .read_sock interface of struct proto_ops, but it is not implemented in MPTCP.
This patch implements it with reference to __mptcp_recvmsg_mskq().
v2:
- first check the sk_state (Matt), but not look for the end of the
end of a connection like TCP in __tcp_read_sock():
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) break;
This will cause a use-after-free error:
BUG: KASAN: slab-use-after-free in mptcp_read_sock.
v3:
- Use sk->sk_rcvbuf instead of INT_MAX as the max len.
v4:
- invoke __mptcp_move_skbs.
--- net/mptcp/protocol.c | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
net/mptcp/protocol.c +68 -0
@@ -3957,6 +3957,72 @@ static __poll_t mptcp_poll(struct file *file, struct socket *sock,return mask; } +static int mptcp_recv_skb (struct sock *sk) +{ +if (skb_queue_empty (&sk->sk_receive_queue)) +__mptcp_move_skbs (sk); + +return 0 ; +} + +/* +* Note: +* - It is assumed that the socket was locked by the caller. +*/ +static int mptcp_read_sock (struct sock *sk,read_descriptor_t *desc, +sk_read_actor_t recv_actor) +{ +struct mptcp_sock *msk =mptcp_sk (sk); +size_t len = sk->sk_rcvbuf; +struct sk_buff *skb, *tmp; +int copied =0 ; + +if (sk->sk_state ==TCP_LISTEN ) +return -ENOTCONN ; + skb_queue_walk_safe(&sk->sk_receive_queue, skb, tmp) { +u32 offset =MPTCP_SKB_CB (skb)->offset; +u32 data_len = skb->len - offset; +u32 size =min_t (size_t, len - copied, data_len); +int count; + + count =recv_actor (desc, skb, offset, size); +if (count <=0 ) { +if (!copied) + copied = count; +break ; + } + + copied += count; + msk->bytes_consumed += count; + +if (count < data_len) { +MPTCP_SKB_CB (skb)->offset += count; +MPTCP_SKB_CB (skb)->map_seq += count; +break ; + } + +/* avoid the indirect call, we know the destructor is sock_wfree */ + skb->destructor =NULL ; +atomic_sub (skb->truesize, &sk->sk_rmem_alloc); +sk_mem_uncharge (sk, skb->truesize); +sk_eat_skb (sk, skb); + +mptcp_recv_skb (sk); + +if (copied >= len) +break ; + } + +mptcp_rcv_space_adjust (msk, copied); + +if (copied >0 ) { +mptcp_recv_skb (sk); +mptcp_cleanup_rbuf (msk, copied); + } + +return copied; +} +static const struct proto_ops mptcp_stream_ops = { .family =PF_INET , .owner =THIS_MODULE ,
@@ -3977,6 +4043,7 @@ static const struct proto_ops mptcp_stream_ops = { .recvmsg = inet_recvmsg, .mmap = sock_no_mmap, .set_rcvlowat = mptcp_set_rcvlowat, + .read_sock = mptcp_read_sock, };static struct inet_protosw mptcp_protosw = {
@@ -4081,6 +4148,7 @@ static const struct proto_ops mptcp_v6_stream_ops = { .compat_ioctl = inet6_compat_ioctl, #endif .set_rcvlowat = mptcp_set_rcvlowat, + .read_sock = mptcp_read_sock, };static struct proto mptcp_v6_prot;
Signature · 1 line
2.48.1
Geliang Tang
reviewerPATCH v6 3/4 mptcp: implement .splice_read
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
This patch implements .splice_read interface of mptcp struct proto_ops as mptcp_splice_read() with reference to tcp_splice_read().
--- net/mptcp/protocol.c | 136 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+)
net/mptcp/protocol.c +136 -0
@@ -4023,6 +4023,140 @@ static int mptcp_read_sock(struct sock *sk, read_descriptor_t *desc,return copied; } +/* +* MPTCP splice context +*/ +struct mptcp_splice_state { +struct pipe_inode_info *pipe; +size_t len; +unsigned int flags; +}; + +static int mptcp_splice_data_recv (read_descriptor_t *rd_desc,struct sk_buff *skb, +unsigned int offset,size_t len) +{ +struct mptcp_splice_state *mss = rd_desc->arg.data; +int ret; + + ret =skb_splice_bits (skb, skb->sk, offset, mss->pipe, +min (rd_desc->count, len), mss->flags); +if (ret >0 ) + rd_desc->count -= ret; +return ret; +} + +static int __mptcp_splice_read (struct sock *sk,struct mptcp_splice_state *mss) +{ +/* Store MPTCP splice context information in read_descriptor_t. */ +read_descriptor_t rd_desc = { + .arg.data = mss, + .count = mss->len, + }; + +return mptcp_read_sock (sk, &rd_desc, mptcp_splice_data_recv); +} + +/** +* mptcp_splice_read - splice data from MPTCP socket to a pipe +* @sock: socket to splice from +* @ppos: position (not valid) +* @pipe: pipe to splice to +* @len: number of bytes to splice +* @flags: splice modifier flags +* +* Description: +* Will read pages from given socket and fill them into a pipe. +* +**/ +static ssize_t mptcp_splice_read (struct socket *sock,loff_t *ppos, +struct pipe_inode_info *pipe,size_t len, +unsigned int flags) +{ +struct mptcp_splice_state mss = { + .pipe = pipe, + .len = len, + .flags = flags, + }; +struct sock *sk = sock->sk; +ssize_t spliced; +long timeo; +int ret; + +/* +* We can't seek on a socket input +*/ +if (unlikely (*ppos)) +return -ESPIPE ; + + spliced =0 ; + ret =0 ; + +lock_sock (sk); + + timeo =sock_rcvtimeo (sk, sock->file->f_flags &O_NONBLOCK ); +while (mss.len) { + ret =__mptcp_splice_read (sk, &mss); +if (ret <0 ) { +break ; + }else if (!ret) { +if (spliced) +break ; +if (sock_flag (sk,SOCK_DONE )) +break ; +if (sk->sk_err) { + ret =sock_error (sk); +break ; + } +if (sk->sk_shutdown &RCV_SHUTDOWN ) { +if (__mptcp_move_skbs (sk)) +continue ; +break ; + } +if (sk->sk_state ==TCP_CLOSE ) { + ret = -ENOTCONN ; +break ; + } +if (!timeo) { + ret = -EAGAIN ; +break ; + } +/* if __mptcp_splice_read() got nothing while we have +* an skb in receive queue, we do not want to loop. +* This might happen with URG data. +*/ +if (!skb_queue_empty (&sk->sk_receive_queue)) +break ; + ret =sk_wait_data (sk, &timeo,NULL ); +if (ret <0 ) +break ; +if (signal_pending (current)) { + ret =sock_intr_errno (timeo); +break ; + } +continue ; + } + mss.len -= ret; + spliced += ret; + +if (!mss.len || !timeo) +break ; +release_sock (sk); +lock_sock (sk); + +if (sk->sk_err || sk->sk_state ==TCP_CLOSE || + (sk->sk_shutdown &RCV_SHUTDOWN ) || +signal_pending (current)) +break ; + } + +release_sock (sk); + +if (spliced) +return spliced; + +return ret; +} +static const struct proto_ops mptcp_stream_ops = { .family =PF_INET , .owner =THIS_MODULE ,
@@ -4044,6 +4178,7 @@ static const struct proto_ops mptcp_stream_ops = { .mmap = sock_no_mmap, .set_rcvlowat = mptcp_set_rcvlowat, .read_sock = mptcp_read_sock, + .splice_read = mptcp_splice_read, };static struct inet_protosw mptcp_protosw = {
@@ -4149,6 +4284,7 @@ static const struct proto_ops mptcp_v6_stream_ops = {#endif .set_rcvlowat = mptcp_set_rcvlowat, .read_sock = mptcp_read_sock, + .splice_read = mptcp_splice_read, };static struct proto mptcp_v6_prot;
Signature · 1 line
2.48.1
Geliang Tang
reviewerPATCH v6 4/4 selftests: mptcp: add splice io mode
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
This patch adds a new 'splice' io mode for mptcp_connect to test the newly added read_sock() and splice() functions of MPTCP.
./mptcp_connect.sh -m splice
v2:
- only use 'splice' mode for MPTCP fallback tests too.
--- .../selftests/net/mptcp/mptcp_connect.c | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
tools/testing/selftests/net/mptcp/mptcp_connect.c +62 -1
@@ -51,6 +51,7 @@ enum cfg_mode {CFG_MODE_POLL ,CFG_MODE_MMAP ,CFG_MODE_SENDFILE , + CFG_MODE_SPLICE, }; enum cfg_peek {
@@ -123,7 +124,7 @@ static void die_usage(void)fprintf (stderr,"\t-j -- add additional sleep at connection start and tear down " "-- for MPJ tests\n" );fprintf (stderr,"\t-l -- listens mode, accepts incoming connection\n" ); -fprintf (stderr,"\t-m [poll|mmap|sendfile] -- use poll(default)/mmap+write/sendfile\n" ); +fprintf (stderr,"\t-m [poll|mmap|sendfile|splice] -- use poll(default)/mmap+write/sendfile/splice\n" );fprintf (stderr,"\t-M mark -- set socket packet mark\n" );fprintf (stderr,"\t-o option -- test sockopt <option>\n" );fprintf (stderr,"\t-p num -- use port num\n" );
@@ -925,6 +926,55 @@ static int copyfd_io_sendfile(int infd, int peerfd, int outfd,return err; } +static int do_splice (const int infd,const int outfd,const size_t len, +struct wstate *winfo) +{ +int pipefd[2 ]; +ssize_t bytes; +int err; + + err =pipe (pipefd); +if (err) +return err; + +while ((bytes =splice (infd,NULL , pipefd[1 ],NULL , + len - winfo->total_len, +SPLICE_F_MOVE |SPLICE_F_MORE )) >0 ) { +splice (pipefd[0 ],NULL , outfd,NULL , bytes, +SPLICE_F_MOVE |SPLICE_F_MORE ); + } + +close (pipefd[0 ]); +close (pipefd[1 ]); + +return 0 ; +} + +static int copyfd_io_splice (int infd,int peerfd,int outfd,unsigned int size, +bool *in_closed_after_out,struct wstate *winfo) +{ +int err; + +if (listen_mode) { + err =do_splice (peerfd, outfd, size, winfo); +if (err) +return err; + + err =do_splice (infd, peerfd, size, winfo); + }else { + err =do_splice (infd, peerfd, size, winfo); +if (err) +return err; + +shut_wr (peerfd); + + err =do_splice (peerfd, outfd, size, winfo); + *in_closed_after_out = true; + } + +return err; +} +static int copyfd_io (int infd,int peerfd,int outfd,bool close_peerfd,struct wstate *winfo) {bool in_closed_after_out = false;
@@ -957,6 +1007,14 @@ static int copyfd_io(int infd, int peerfd, int outfd, bool close_peerfd, struct &in_closed_after_out, winfo);break ; +case CFG_MODE_SPLICE : + file_size =get_infd_size (infd); +if (file_size <0 ) +return file_size; + ret =copyfd_io_splice (infd, peerfd, outfd, file_size, + &in_closed_after_out, winfo); +break ; +default :fprintf (stderr,"Invalid mode %d\n" , cfg_mode);
@@ -1361,12 +1419,15 @@ int parse_mode(const char *mode)return CFG_MODE_MMAP ;if (!strcasecmp (mode,"sendfile" ))return CFG_MODE_SENDFILE ; +if (!strcasecmp (mode,"splice" )) +return CFG_MODE_SPLICE ;fprintf (stderr,"Unknown test mode: %s\n" , mode);fprintf (stderr,"Supported modes are:\n" );fprintf (stderr,"\t\t\"poll\" - interleaved read/write using poll()\n" );fprintf (stderr,"\t\t\"mmap\" - send entire input file (mmap+write), then read response (-l will read input first)\n" );fprintf (stderr,"\t\t\"sendfile\" - send entire input file (sendfile), then read response (-l will read input first)\n" ); +fprintf (stderr,"\t\t\"splice\" - send entire input file (splice), then read response (-l will read input first)\n" );die_usage ();
Signature · 1 line
2.48.1
Hi Geliang,
Thank you for your modifications, that's great!
But sadly, our CI spotted some issues with it when trying to build it.
You can find more details there:
https://github.com/multipath-tcp/mptcp_net-next/actions/runs/16110713639
Status: failure
Initiator: Matthieu Baerts (NGI0)
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/1ecaaa711e0c
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=979322
Feel free to reply to this email if you cannot access logs, if you need some support to fix the error, if this doesn't seem to be caused by your modifications or if the error is a false positive one.
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)