mirror of
https://github.com/torvalds/linux.git
synced 2026-03-08 01:04:41 +01:00
RDS/TCP differs from RDS/RDMA in that message acknowledgment
is done based on TCP sequence numbers:
As soon as the last byte of a message has been acknowledged by the
TCP stack of a peer, rds_tcp_write_space() goes on to discard
prior messages from the send queue.
Which is fine, for as long as the receiver never throws any messages
away.
The dequeuing of messages in RDS/TCP is done either from the
"sk_data_ready" callback pointing to rds_tcp_data_ready()
(the most common case), or from the receive worker pointing
to rds_tcp_recv_path() which is called for as long as the
connection is "RDS_CONN_UP".
However, as soon as rds_conn_path_drop() is called for whatever reason,
including "DR_USER_RESET", "cp_state" transitions to "RDS_CONN_ERROR",
and rds_tcp_restore_callbacks() ends up restoring the callbacks
and thereby disabling message receipt.
So messages already acknowledged to the sender were dropped.
Furthermore, the "->shutdown" callback was always called
with an invalid parameter ("RCV_SHUTDOWN | SEND_SHUTDOWN == 3"),
instead of the correct pre-increment value ("SHUT_RDWR == 2").
inet_shutdown() returns "-EINVAL" in such cases, rendering
this call a NOOP.
So we change rds_tcp_conn_path_shutdown() to do the proper
"->shutdown(SHUT_WR)" call in order to signal EOF to the peer
and make it transition to "TCP_CLOSE_WAIT" (RFC 793).
This should make the peer also enter rds_tcp_conn_path_shutdown()
and do the same.
This allows us to dequeue all messages already received
and acknowledged to the peer.
We do so, until we know that the receive queue no longer has data
(skb_queue_empty()) and that we couldn't have any data
in flight anymore, because the socket transitioned to
any of the states "CLOSING", "TIME_WAIT", "CLOSE_WAIT",
"LAST_ACK", or "CLOSE" (RFC 793).
However, if we do just that, we suddenly see duplicate RDS
messages being delivered to the application.
So what gives?
Turns out that with MPRDS and its multitude of backend connections,
retransmitted messages ("RDS_FLAG_RETRANSMITTED") can outrace
the dequeuing of their original counterparts.
And the duplicate check implemented in rds_recv_local() only
discards duplicates if flag "RDS_FLAG_RETRANSMITTED" is set.
Rather curious, because a duplicate is a duplicate; it shouldn't
matter which copy is looked at and delivered first.
To avoid this entire situation, we simply make the sender discard
messages from the send-queue right from within
rds_tcp_conn_path_shutdown(). Just like rds_tcp_write_space() would
have done, were it called in time or still called.
This makes sure that we no longer have messages that we know
the receiver already dequeued sitting in our send-queue,
and therefore avoid the entire "RDS_FLAG_RETRANSMITTED" fiasco.
Now we got rid of the duplicate RDS message delivery, but we
still run into cases where RDS messages are dropped.
This time it is due to the delayed setting of the socket-callbacks
in rds_tcp_accept_one() via either rds_tcp_reset_callbacks()
or rds_tcp_set_callbacks().
By the time rds_tcp_accept_one() gets there, the socket
may already have transitioned into state "TCP_CLOSE_WAIT",
but rds_tcp_state_change() was never called.
Subsequently, "->shutdown(SHUT_WR)" did not happen either.
So the peer ends up getting stuck in state "TCP_FIN_WAIT2".
We fix that by checking for states "TCP_CLOSE_WAIT", "TCP_LAST_ACK",
or "TCP_CLOSE" and drop the freshly accepted socket in that case.
This problem is observable by running "rds-stress --reset"
frequently on either of the two sides of a RDS connection,
or both while other "rds-stress" processes are exchanging data.
Those "rds-stress" processes reported out-of-sequence
errors, with the expected sequence number being smaller
than the one actually received (due to the dropped messages).
Signed-off-by: Gerd Rausch <gerd.rausch@oracle.com>
Signed-off-by: Allison Henderson <allison.henderson@oracle.com>
Link: https://patch.msgid.link/20260203055723.1085751-4-achender@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
226 lines
6.8 KiB
C
226 lines
6.8 KiB
C
/*
|
|
* Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
|
|
*
|
|
* This software is available to you under a choice of one of two
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
* General Public License (GPL) Version 2, available from the file
|
|
* COPYING in the main directory of this source tree, or the
|
|
* OpenIB.org BSD license below:
|
|
*
|
|
* Redistribution and use in source and binary forms, with or
|
|
* without modification, are permitted provided that the following
|
|
* conditions are met:
|
|
*
|
|
* - Redistributions of source code must retain the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer.
|
|
*
|
|
* - Redistributions in binary form must reproduce the above
|
|
* copyright notice, this list of conditions and the following
|
|
* disclaimer in the documentation and/or other materials
|
|
* provided with the distribution.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*/
|
|
#include <linux/kernel.h>
|
|
#include <linux/in.h>
|
|
#include <net/tcp.h>
|
|
|
|
#include "rds_single_path.h"
|
|
#include "rds.h"
|
|
#include "tcp.h"
|
|
|
|
void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
|
|
{
|
|
struct rds_tcp_connection *tc = cp->cp_transport_data;
|
|
|
|
tcp_sock_set_cork(tc->t_sock->sk, true);
|
|
}
|
|
|
|
void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
|
|
{
|
|
struct rds_tcp_connection *tc = cp->cp_transport_data;
|
|
|
|
tcp_sock_set_cork(tc->t_sock->sk, false);
|
|
}
|
|
|
|
/* the core send_sem serializes this with other xmit and shutdown */
|
|
static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
|
|
{
|
|
struct kvec vec = {
|
|
.iov_base = data,
|
|
.iov_len = len,
|
|
};
|
|
struct msghdr msg = {
|
|
.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
|
|
};
|
|
|
|
return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
|
|
}
|
|
|
|
/* the core send_sem serializes this with other xmit and shutdown */
|
|
int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
|
|
unsigned int hdr_off, unsigned int sg, unsigned int off)
|
|
{
|
|
struct rds_conn_path *cp = rm->m_inc.i_conn_path;
|
|
struct rds_tcp_connection *tc = cp->cp_transport_data;
|
|
struct msghdr msg = {};
|
|
struct bio_vec bvec;
|
|
int done = 0;
|
|
int ret = 0;
|
|
|
|
if (hdr_off == 0) {
|
|
/*
|
|
* m_ack_seq is set to the sequence number of the last byte of
|
|
* header and data. see rds_tcp_is_acked().
|
|
*/
|
|
tc->t_last_sent_nxt = rds_tcp_write_seq(tc);
|
|
rm->m_ack_seq = tc->t_last_sent_nxt +
|
|
sizeof(struct rds_header) +
|
|
be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
|
|
smp_mb__before_atomic();
|
|
set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
|
|
tc->t_last_expected_una = rm->m_ack_seq + 1;
|
|
|
|
if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
|
|
rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
|
|
|
|
rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
|
|
rm, rds_tcp_write_seq(tc),
|
|
(unsigned long long)rm->m_ack_seq);
|
|
}
|
|
|
|
if (hdr_off < sizeof(struct rds_header)) {
|
|
/* see rds_tcp_write_space() */
|
|
set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
|
|
|
|
ret = rds_tcp_sendmsg(tc->t_sock,
|
|
(void *)&rm->m_inc.i_hdr + hdr_off,
|
|
sizeof(rm->m_inc.i_hdr) - hdr_off);
|
|
if (ret < 0)
|
|
goto out;
|
|
done += ret;
|
|
if (hdr_off + done != sizeof(struct rds_header))
|
|
goto out;
|
|
}
|
|
|
|
while (sg < rm->data.op_nents) {
|
|
msg.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT | MSG_NOSIGNAL;
|
|
if (sg + 1 < rm->data.op_nents)
|
|
msg.msg_flags |= MSG_MORE;
|
|
|
|
bvec_set_page(&bvec, sg_page(&rm->data.op_sg[sg]),
|
|
rm->data.op_sg[sg].length - off,
|
|
rm->data.op_sg[sg].offset + off);
|
|
iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
|
|
rm->data.op_sg[sg].length - off);
|
|
ret = sock_sendmsg(tc->t_sock, &msg);
|
|
rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
|
|
rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
|
|
ret);
|
|
if (ret <= 0)
|
|
break;
|
|
|
|
off += ret;
|
|
done += ret;
|
|
if (off == rm->data.op_sg[sg].length) {
|
|
off = 0;
|
|
sg++;
|
|
}
|
|
}
|
|
|
|
out:
|
|
if (ret <= 0) {
|
|
/* write_space will hit after EAGAIN, all else fatal */
|
|
if (ret == -EAGAIN) {
|
|
rds_tcp_stats_inc(s_tcp_sndbuf_full);
|
|
ret = 0;
|
|
} else {
|
|
/* No need to disconnect/reconnect if path_drop
|
|
* has already been triggered, because, e.g., of
|
|
* an incoming RST.
|
|
*/
|
|
if (rds_conn_path_up(cp)) {
|
|
pr_warn("RDS/tcp: send to %pI6c on cp [%d]"
|
|
"returned %d, "
|
|
"disconnecting and reconnecting\n",
|
|
&conn->c_faddr, cp->cp_index, ret);
|
|
rds_conn_path_drop(cp, false);
|
|
}
|
|
}
|
|
}
|
|
if (done == 0)
|
|
done = ret;
|
|
return done;
|
|
}
|
|
|
|
/*
|
|
* rm->m_ack_seq is set to the tcp sequence number that corresponds to the
|
|
* last byte of the message, including the header. This means that the
|
|
* entire message has been received if rm->m_ack_seq is "before" the next
|
|
* unacked byte of the TCP sequence space. We have to do very careful
|
|
* wrapping 32bit comparisons here.
|
|
*/
|
|
int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
|
|
{
|
|
if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
|
|
return 0;
|
|
return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
|
|
}
|
|
|
|
void rds_tcp_write_space(struct sock *sk)
|
|
{
|
|
void (*write_space)(struct sock *sk);
|
|
struct rds_conn_path *cp;
|
|
struct rds_tcp_connection *tc;
|
|
|
|
read_lock_bh(&sk->sk_callback_lock);
|
|
cp = sk->sk_user_data;
|
|
if (!cp) {
|
|
write_space = sk->sk_write_space;
|
|
goto out;
|
|
}
|
|
|
|
tc = cp->cp_transport_data;
|
|
rdsdebug("write_space for tc %p\n", tc);
|
|
write_space = tc->t_orig_write_space;
|
|
rds_tcp_stats_inc(s_tcp_write_space_calls);
|
|
|
|
rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
|
|
tc->t_last_seen_una = rds_tcp_snd_una(tc);
|
|
rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
|
|
|
|
rcu_read_lock();
|
|
if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf &&
|
|
!rds_destroy_pending(cp->cp_conn))
|
|
queue_delayed_work(cp->cp_wq, &cp->cp_send_w, 0);
|
|
rcu_read_unlock();
|
|
|
|
out:
|
|
read_unlock_bh(&sk->sk_callback_lock);
|
|
|
|
/*
|
|
* write_space is only called when data leaves tcp's send queue if
|
|
* SOCK_NOSPACE is set. We set SOCK_NOSPACE every time we put
|
|
* data in tcp's send queue because we use write_space to parse the
|
|
* sequence numbers and notice that rds messages have been fully
|
|
* received.
|
|
*
|
|
* tcp's write_space clears SOCK_NOSPACE if the send queue has more
|
|
* than a certain amount of space. So we need to set it again *after*
|
|
* we call tcp's write_space or else we might only get called on the
|
|
* first of a series of incoming tcp acks.
|
|
*/
|
|
write_space(sk);
|
|
|
|
if (sk->sk_socket)
|
|
set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
|
|
}
|