xref: /openbmc/linux/net/rds/tcp_send.c (revision 572efade)
170041088SAndy Grover /*
2eee2fa6aSKa-Cheong Poon  * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
370041088SAndy Grover  *
470041088SAndy Grover  * This software is available to you under a choice of one of two
570041088SAndy Grover  * licenses.  You may choose to be licensed under the terms of the GNU
670041088SAndy Grover  * General Public License (GPL) Version 2, available from the file
770041088SAndy Grover  * COPYING in the main directory of this source tree, or the
870041088SAndy Grover  * OpenIB.org BSD license below:
970041088SAndy Grover  *
1070041088SAndy Grover  *     Redistribution and use in source and binary forms, with or
1170041088SAndy Grover  *     without modification, are permitted provided that the following
1270041088SAndy Grover  *     conditions are met:
1370041088SAndy Grover  *
1470041088SAndy Grover  *      - Redistributions of source code must retain the above
1570041088SAndy Grover  *        copyright notice, this list of conditions and the following
1670041088SAndy Grover  *        disclaimer.
1770041088SAndy Grover  *
1870041088SAndy Grover  *      - Redistributions in binary form must reproduce the above
1970041088SAndy Grover  *        copyright notice, this list of conditions and the following
2070041088SAndy Grover  *        disclaimer in the documentation and/or other materials
2170041088SAndy Grover  *        provided with the distribution.
2270041088SAndy Grover  *
2370041088SAndy Grover  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2470041088SAndy Grover  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2570041088SAndy Grover  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2670041088SAndy Grover  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2770041088SAndy Grover  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2870041088SAndy Grover  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2970041088SAndy Grover  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3070041088SAndy Grover  * SOFTWARE.
3170041088SAndy Grover  *
3270041088SAndy Grover  */
3370041088SAndy Grover #include <linux/kernel.h>
3470041088SAndy Grover #include <linux/in.h>
3570041088SAndy Grover #include <net/tcp.h>
3670041088SAndy Grover 
370cb43965SSowmini Varadhan #include "rds_single_path.h"
3870041088SAndy Grover #include "rds.h"
3970041088SAndy Grover #include "tcp.h"
4070041088SAndy Grover 
rds_tcp_xmit_path_prepare(struct rds_conn_path * cp)41226f7a7dSSowmini Varadhan void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
4270041088SAndy Grover {
43226f7a7dSSowmini Varadhan 	struct rds_tcp_connection *tc = cp->cp_transport_data;
4470041088SAndy Grover 
45db10538aSChristoph Hellwig 	tcp_sock_set_cork(tc->t_sock->sk, true);
4670041088SAndy Grover }
4770041088SAndy Grover 
rds_tcp_xmit_path_complete(struct rds_conn_path * cp)48226f7a7dSSowmini Varadhan void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
4970041088SAndy Grover {
50226f7a7dSSowmini Varadhan 	struct rds_tcp_connection *tc = cp->cp_transport_data;
5170041088SAndy Grover 
52db10538aSChristoph Hellwig 	tcp_sock_set_cork(tc->t_sock->sk, false);
5370041088SAndy Grover }
5470041088SAndy Grover 
5570041088SAndy Grover /* the core send_sem serializes this with other xmit and shutdown */
rds_tcp_sendmsg(struct socket * sock,void * data,unsigned int len)56ff51bf84Sstephen hemminger static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
5770041088SAndy Grover {
5870041088SAndy Grover 	struct kvec vec = {
5970041088SAndy Grover 		.iov_base = data,
6070041088SAndy Grover 		.iov_len = len,
6170041088SAndy Grover 	};
6270041088SAndy Grover 	struct msghdr msg = {
6370041088SAndy Grover 		.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
6470041088SAndy Grover 	};
6570041088SAndy Grover 
6670041088SAndy Grover 	return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
6770041088SAndy Grover }
6870041088SAndy Grover 
6970041088SAndy Grover /* the core send_sem serializes this with other xmit and shutdown */
rds_tcp_xmit(struct rds_connection * conn,struct rds_message * rm,unsigned int hdr_off,unsigned int sg,unsigned int off)7070041088SAndy Grover int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
7170041088SAndy Grover 		 unsigned int hdr_off, unsigned int sg, unsigned int off)
7270041088SAndy Grover {
735916e2c1SSowmini Varadhan 	struct rds_conn_path *cp = rm->m_inc.i_conn_path;
745916e2c1SSowmini Varadhan 	struct rds_tcp_connection *tc = cp->cp_transport_data;
75*572efadeSDavid Howells 	struct msghdr msg = {};
76*572efadeSDavid Howells 	struct bio_vec bvec;
7770041088SAndy Grover 	int done = 0;
7870041088SAndy Grover 	int ret = 0;
7970041088SAndy Grover 
8070041088SAndy Grover 	if (hdr_off == 0) {
8170041088SAndy Grover 		/*
8270041088SAndy Grover 		 * m_ack_seq is set to the sequence number of the last byte of
8370041088SAndy Grover 		 * header and data.  see rds_tcp_is_acked().
8470041088SAndy Grover 		 */
85b589513eSSowmini Varadhan 		tc->t_last_sent_nxt = rds_tcp_write_seq(tc);
8670041088SAndy Grover 		rm->m_ack_seq = tc->t_last_sent_nxt +
8770041088SAndy Grover 				sizeof(struct rds_header) +
8870041088SAndy Grover 				be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
894e857c58SPeter Zijlstra 		smp_mb__before_atomic();
9070041088SAndy Grover 		set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
9170041088SAndy Grover 		tc->t_last_expected_una = rm->m_ack_seq + 1;
9270041088SAndy Grover 
93315ca6d9SSowmini Varadhan 		if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
94315ca6d9SSowmini Varadhan 			rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
95315ca6d9SSowmini Varadhan 
9670041088SAndy Grover 		rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
97b589513eSSowmini Varadhan 			 rm, rds_tcp_write_seq(tc),
9870041088SAndy Grover 			 (unsigned long long)rm->m_ack_seq);
9970041088SAndy Grover 	}
10070041088SAndy Grover 
10170041088SAndy Grover 	if (hdr_off < sizeof(struct rds_header)) {
10270041088SAndy Grover 		/* see rds_tcp_write_space() */
10370041088SAndy Grover 		set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
10470041088SAndy Grover 
10570041088SAndy Grover 		ret = rds_tcp_sendmsg(tc->t_sock,
10670041088SAndy Grover 				      (void *)&rm->m_inc.i_hdr + hdr_off,
10770041088SAndy Grover 				      sizeof(rm->m_inc.i_hdr) - hdr_off);
10870041088SAndy Grover 		if (ret < 0)
10970041088SAndy Grover 			goto out;
11070041088SAndy Grover 		done += ret;
11170041088SAndy Grover 		if (hdr_off + done != sizeof(struct rds_header))
11270041088SAndy Grover 			goto out;
11370041088SAndy Grover 	}
11470041088SAndy Grover 
1156c7cc6e4SAndy Grover 	while (sg < rm->data.op_nents) {
116*572efadeSDavid Howells 		msg.msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT | MSG_NOSIGNAL;
117*572efadeSDavid Howells 		if (sg + 1 < rm->data.op_nents)
118*572efadeSDavid Howells 			msg.msg_flags |= MSG_MORE;
11976b29ef1SSowmini Varadhan 
120*572efadeSDavid Howells 		bvec_set_page(&bvec, sg_page(&rm->data.op_sg[sg]),
1216c7cc6e4SAndy Grover 			      rm->data.op_sg[sg].length - off,
122*572efadeSDavid Howells 			      rm->data.op_sg[sg].offset + off);
123*572efadeSDavid Howells 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1,
124*572efadeSDavid Howells 			      rm->data.op_sg[sg].length - off);
125*572efadeSDavid Howells 		ret = sock_sendmsg(tc->t_sock, &msg);
1266c7cc6e4SAndy Grover 		rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
1276c7cc6e4SAndy Grover 			 rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
12870041088SAndy Grover 			 ret);
12970041088SAndy Grover 		if (ret <= 0)
13070041088SAndy Grover 			break;
13170041088SAndy Grover 
13270041088SAndy Grover 		off += ret;
13370041088SAndy Grover 		done += ret;
1346c7cc6e4SAndy Grover 		if (off == rm->data.op_sg[sg].length) {
13570041088SAndy Grover 			off = 0;
13670041088SAndy Grover 			sg++;
13770041088SAndy Grover 		}
13870041088SAndy Grover 	}
13970041088SAndy Grover 
14070041088SAndy Grover out:
14170041088SAndy Grover 	if (ret <= 0) {
14270041088SAndy Grover 		/* write_space will hit after EAGAIN, all else fatal */
14370041088SAndy Grover 		if (ret == -EAGAIN) {
14470041088SAndy Grover 			rds_tcp_stats_inc(s_tcp_sndbuf_full);
14570041088SAndy Grover 			ret = 0;
14670041088SAndy Grover 		} else {
1475916e2c1SSowmini Varadhan 			/* No need to disconnect/reconnect if path_drop
1485916e2c1SSowmini Varadhan 			 * has already been triggered, because, e.g., of
1495916e2c1SSowmini Varadhan 			 * an incoming RST.
1505916e2c1SSowmini Varadhan 			 */
1515916e2c1SSowmini Varadhan 			if (rds_conn_path_up(cp)) {
152eee2fa6aSKa-Cheong Poon 				pr_warn("RDS/tcp: send to %pI6c on cp [%d]"
1535916e2c1SSowmini Varadhan 					"returned %d, "
1545916e2c1SSowmini Varadhan 					"disconnecting and reconnecting\n",
1555916e2c1SSowmini Varadhan 					&conn->c_faddr, cp->cp_index, ret);
156aed20a53SSowmini Varadhan 				rds_conn_path_drop(cp, false);
1575916e2c1SSowmini Varadhan 			}
15870041088SAndy Grover 		}
15970041088SAndy Grover 	}
16070041088SAndy Grover 	if (done == 0)
16170041088SAndy Grover 		done = ret;
16270041088SAndy Grover 	return done;
16370041088SAndy Grover }
16470041088SAndy Grover 
16570041088SAndy Grover /*
16670041088SAndy Grover  * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
16770041088SAndy Grover  * last byte of the message, including the header.  This means that the
16870041088SAndy Grover  * entire message has been received if rm->m_ack_seq is "before" the next
16970041088SAndy Grover  * unacked byte of the TCP sequence space.  We have to do very careful
17070041088SAndy Grover  * wrapping 32bit comparisons here.
17170041088SAndy Grover  */
rds_tcp_is_acked(struct rds_message * rm,uint64_t ack)17270041088SAndy Grover static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
17370041088SAndy Grover {
17470041088SAndy Grover 	if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
17570041088SAndy Grover 		return 0;
17670041088SAndy Grover 	return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
17770041088SAndy Grover }
17870041088SAndy Grover 
rds_tcp_write_space(struct sock * sk)17970041088SAndy Grover void rds_tcp_write_space(struct sock *sk)
18070041088SAndy Grover {
18170041088SAndy Grover 	void (*write_space)(struct sock *sk);
182ea3b1ea5SSowmini Varadhan 	struct rds_conn_path *cp;
18370041088SAndy Grover 	struct rds_tcp_connection *tc;
18470041088SAndy Grover 
18538036629SEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
186ea3b1ea5SSowmini Varadhan 	cp = sk->sk_user_data;
187ea3b1ea5SSowmini Varadhan 	if (!cp) {
18870041088SAndy Grover 		write_space = sk->sk_write_space;
18970041088SAndy Grover 		goto out;
19070041088SAndy Grover 	}
19170041088SAndy Grover 
192ea3b1ea5SSowmini Varadhan 	tc = cp->cp_transport_data;
19370041088SAndy Grover 	rdsdebug("write_space for tc %p\n", tc);
19470041088SAndy Grover 	write_space = tc->t_orig_write_space;
19570041088SAndy Grover 	rds_tcp_stats_inc(s_tcp_write_space_calls);
19670041088SAndy Grover 
19770041088SAndy Grover 	rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
19870041088SAndy Grover 	tc->t_last_seen_una = rds_tcp_snd_una(tc);
199ea3b1ea5SSowmini Varadhan 	rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
20070041088SAndy Grover 
2013db6e0d1SSowmini Varadhan 	rcu_read_lock();
2023db6e0d1SSowmini Varadhan 	if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf &&
203ebeeb1adSSowmini Varadhan 	    !rds_destroy_pending(cp->cp_conn))
204ea3b1ea5SSowmini Varadhan 		queue_delayed_work(rds_wq, &cp->cp_send_w, 0);
2053db6e0d1SSowmini Varadhan 	rcu_read_unlock();
2068e82376eSAndy Grover 
20770041088SAndy Grover out:
20838036629SEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
20970041088SAndy Grover 
21070041088SAndy Grover 	/*
21170041088SAndy Grover 	 * write_space is only called when data leaves tcp's send queue if
21270041088SAndy Grover 	 * SOCK_NOSPACE is set.  We set SOCK_NOSPACE every time we put
21370041088SAndy Grover 	 * data in tcp's send queue because we use write_space to parse the
21470041088SAndy Grover 	 * sequence numbers and notice that rds messages have been fully
21570041088SAndy Grover 	 * received.
21670041088SAndy Grover 	 *
21770041088SAndy Grover 	 * tcp's write_space clears SOCK_NOSPACE if the send queue has more
21870041088SAndy Grover 	 * than a certain amount of space. So we need to set it again *after*
21970041088SAndy Grover 	 * we call tcp's write_space or else we might only get called on the
22070041088SAndy Grover 	 * first of a series of incoming tcp acks.
22170041088SAndy Grover 	 */
22270041088SAndy Grover 	write_space(sk);
22370041088SAndy Grover 
22470041088SAndy Grover 	if (sk->sk_socket)
22570041088SAndy Grover 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
22670041088SAndy Grover }
227