1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This software is available to you under a choice of one of two
5*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
6*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
7*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
8*4882a593Smuzhiyun * OpenIB.org BSD license below:
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
11*4882a593Smuzhiyun * without modification, are permitted provided that the following
12*4882a593Smuzhiyun * conditions are met:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * - Redistributions of source code must retain the above
15*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
16*4882a593Smuzhiyun * disclaimer.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
19*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
20*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
21*4882a593Smuzhiyun * provided with the distribution.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*4882a593Smuzhiyun * SOFTWARE.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun */
33*4882a593Smuzhiyun #include <linux/kernel.h>
34*4882a593Smuzhiyun #include <linux/in.h>
35*4882a593Smuzhiyun #include <net/tcp.h>
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include "rds_single_path.h"
38*4882a593Smuzhiyun #include "rds.h"
39*4882a593Smuzhiyun #include "tcp.h"
40*4882a593Smuzhiyun
rds_tcp_xmit_path_prepare(struct rds_conn_path * cp)41*4882a593Smuzhiyun void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun tcp_sock_set_cork(tc->t_sock->sk, true);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
rds_tcp_xmit_path_complete(struct rds_conn_path * cp)48*4882a593Smuzhiyun void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun tcp_sock_set_cork(tc->t_sock->sk, false);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* the core send_sem serializes this with other xmit and shutdown */
rds_tcp_sendmsg(struct socket * sock,void * data,unsigned int len)56*4882a593Smuzhiyun static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun struct kvec vec = {
59*4882a593Smuzhiyun .iov_base = data,
60*4882a593Smuzhiyun .iov_len = len,
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun struct msghdr msg = {
63*4882a593Smuzhiyun .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* 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)70*4882a593Smuzhiyun int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
71*4882a593Smuzhiyun unsigned int hdr_off, unsigned int sg, unsigned int off)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun struct rds_conn_path *cp = rm->m_inc.i_conn_path;
74*4882a593Smuzhiyun struct rds_tcp_connection *tc = cp->cp_transport_data;
75*4882a593Smuzhiyun int done = 0;
76*4882a593Smuzhiyun int ret = 0;
77*4882a593Smuzhiyun int more;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (hdr_off == 0) {
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * m_ack_seq is set to the sequence number of the last byte of
82*4882a593Smuzhiyun * header and data. see rds_tcp_is_acked().
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun tc->t_last_sent_nxt = rds_tcp_write_seq(tc);
85*4882a593Smuzhiyun rm->m_ack_seq = tc->t_last_sent_nxt +
86*4882a593Smuzhiyun sizeof(struct rds_header) +
87*4882a593Smuzhiyun be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
88*4882a593Smuzhiyun smp_mb__before_atomic();
89*4882a593Smuzhiyun set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
90*4882a593Smuzhiyun tc->t_last_expected_una = rm->m_ack_seq + 1;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
93*4882a593Smuzhiyun rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
96*4882a593Smuzhiyun rm, rds_tcp_write_seq(tc),
97*4882a593Smuzhiyun (unsigned long long)rm->m_ack_seq);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun if (hdr_off < sizeof(struct rds_header)) {
101*4882a593Smuzhiyun /* see rds_tcp_write_space() */
102*4882a593Smuzhiyun set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun ret = rds_tcp_sendmsg(tc->t_sock,
105*4882a593Smuzhiyun (void *)&rm->m_inc.i_hdr + hdr_off,
106*4882a593Smuzhiyun sizeof(rm->m_inc.i_hdr) - hdr_off);
107*4882a593Smuzhiyun if (ret < 0)
108*4882a593Smuzhiyun goto out;
109*4882a593Smuzhiyun done += ret;
110*4882a593Smuzhiyun if (hdr_off + done != sizeof(struct rds_header))
111*4882a593Smuzhiyun goto out;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun more = rm->data.op_nents > 1 ? (MSG_MORE | MSG_SENDPAGE_NOTLAST) : 0;
115*4882a593Smuzhiyun while (sg < rm->data.op_nents) {
116*4882a593Smuzhiyun int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun ret = tc->t_sock->ops->sendpage(tc->t_sock,
119*4882a593Smuzhiyun sg_page(&rm->data.op_sg[sg]),
120*4882a593Smuzhiyun rm->data.op_sg[sg].offset + off,
121*4882a593Smuzhiyun rm->data.op_sg[sg].length - off,
122*4882a593Smuzhiyun flags);
123*4882a593Smuzhiyun rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
124*4882a593Smuzhiyun rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
125*4882a593Smuzhiyun ret);
126*4882a593Smuzhiyun if (ret <= 0)
127*4882a593Smuzhiyun break;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun off += ret;
130*4882a593Smuzhiyun done += ret;
131*4882a593Smuzhiyun if (off == rm->data.op_sg[sg].length) {
132*4882a593Smuzhiyun off = 0;
133*4882a593Smuzhiyun sg++;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun if (sg == rm->data.op_nents - 1)
136*4882a593Smuzhiyun more = 0;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun out:
140*4882a593Smuzhiyun if (ret <= 0) {
141*4882a593Smuzhiyun /* write_space will hit after EAGAIN, all else fatal */
142*4882a593Smuzhiyun if (ret == -EAGAIN) {
143*4882a593Smuzhiyun rds_tcp_stats_inc(s_tcp_sndbuf_full);
144*4882a593Smuzhiyun ret = 0;
145*4882a593Smuzhiyun } else {
146*4882a593Smuzhiyun /* No need to disconnect/reconnect if path_drop
147*4882a593Smuzhiyun * has already been triggered, because, e.g., of
148*4882a593Smuzhiyun * an incoming RST.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun if (rds_conn_path_up(cp)) {
151*4882a593Smuzhiyun pr_warn("RDS/tcp: send to %pI6c on cp [%d]"
152*4882a593Smuzhiyun "returned %d, "
153*4882a593Smuzhiyun "disconnecting and reconnecting\n",
154*4882a593Smuzhiyun &conn->c_faddr, cp->cp_index, ret);
155*4882a593Smuzhiyun rds_conn_path_drop(cp, false);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun if (done == 0)
160*4882a593Smuzhiyun done = ret;
161*4882a593Smuzhiyun return done;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun * rm->m_ack_seq is set to the tcp sequence number that corresponds to the
166*4882a593Smuzhiyun * last byte of the message, including the header. This means that the
167*4882a593Smuzhiyun * entire message has been received if rm->m_ack_seq is "before" the next
168*4882a593Smuzhiyun * unacked byte of the TCP sequence space. We have to do very careful
169*4882a593Smuzhiyun * wrapping 32bit comparisons here.
170*4882a593Smuzhiyun */
rds_tcp_is_acked(struct rds_message * rm,uint64_t ack)171*4882a593Smuzhiyun static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
174*4882a593Smuzhiyun return 0;
175*4882a593Smuzhiyun return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
rds_tcp_write_space(struct sock * sk)178*4882a593Smuzhiyun void rds_tcp_write_space(struct sock *sk)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun void (*write_space)(struct sock *sk);
181*4882a593Smuzhiyun struct rds_conn_path *cp;
182*4882a593Smuzhiyun struct rds_tcp_connection *tc;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun read_lock_bh(&sk->sk_callback_lock);
185*4882a593Smuzhiyun cp = sk->sk_user_data;
186*4882a593Smuzhiyun if (!cp) {
187*4882a593Smuzhiyun write_space = sk->sk_write_space;
188*4882a593Smuzhiyun goto out;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun tc = cp->cp_transport_data;
192*4882a593Smuzhiyun rdsdebug("write_space for tc %p\n", tc);
193*4882a593Smuzhiyun write_space = tc->t_orig_write_space;
194*4882a593Smuzhiyun rds_tcp_stats_inc(s_tcp_write_space_calls);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
197*4882a593Smuzhiyun tc->t_last_seen_una = rds_tcp_snd_una(tc);
198*4882a593Smuzhiyun rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun rcu_read_lock();
201*4882a593Smuzhiyun if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf &&
202*4882a593Smuzhiyun !rds_destroy_pending(cp->cp_conn))
203*4882a593Smuzhiyun queue_delayed_work(rds_wq, &cp->cp_send_w, 0);
204*4882a593Smuzhiyun rcu_read_unlock();
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun out:
207*4882a593Smuzhiyun read_unlock_bh(&sk->sk_callback_lock);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * write_space is only called when data leaves tcp's send queue if
211*4882a593Smuzhiyun * SOCK_NOSPACE is set. We set SOCK_NOSPACE every time we put
212*4882a593Smuzhiyun * data in tcp's send queue because we use write_space to parse the
213*4882a593Smuzhiyun * sequence numbers and notice that rds messages have been fully
214*4882a593Smuzhiyun * received.
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * tcp's write_space clears SOCK_NOSPACE if the send queue has more
217*4882a593Smuzhiyun * than a certain amount of space. So we need to set it again *after*
218*4882a593Smuzhiyun * we call tcp's write_space or else we might only get called on the
219*4882a593Smuzhiyun * first of a series of incoming tcp acks.
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun write_space(sk);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun if (sk->sk_socket)
224*4882a593Smuzhiyun set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
225*4882a593Smuzhiyun }
226