1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kernel.h>
34#include <linux/in.h>
35#include <net/tcp.h>
36
37#include "rds_single_path.h"
38#include "rds.h"
39#include "tcp.h"
40
41static void rds_tcp_cork(struct socket *sock, int val)
42{
43 kernel_setsockopt(sock, SOL_TCP, TCP_CORK, (void *)&val, sizeof(val));
44}
45
46void rds_tcp_xmit_path_prepare(struct rds_conn_path *cp)
47{
48 struct rds_tcp_connection *tc = cp->cp_transport_data;
49
50 rds_tcp_cork(tc->t_sock, 1);
51}
52
53void rds_tcp_xmit_path_complete(struct rds_conn_path *cp)
54{
55 struct rds_tcp_connection *tc = cp->cp_transport_data;
56
57 rds_tcp_cork(tc->t_sock, 0);
58}
59
60
61static int rds_tcp_sendmsg(struct socket *sock, void *data, unsigned int len)
62{
63 struct kvec vec = {
64 .iov_base = data,
65 .iov_len = len,
66 };
67 struct msghdr msg = {
68 .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL,
69 };
70
71 return kernel_sendmsg(sock, &msg, &vec, 1, vec.iov_len);
72}
73
74
75int rds_tcp_xmit(struct rds_connection *conn, struct rds_message *rm,
76 unsigned int hdr_off, unsigned int sg, unsigned int off)
77{
78 struct rds_conn_path *cp = rm->m_inc.i_conn_path;
79 struct rds_tcp_connection *tc = cp->cp_transport_data;
80 int done = 0;
81 int ret = 0;
82 int more;
83
84 if (hdr_off == 0) {
85
86
87
88
89 tc->t_last_sent_nxt = rds_tcp_snd_nxt(tc);
90 rm->m_ack_seq = tc->t_last_sent_nxt +
91 sizeof(struct rds_header) +
92 be32_to_cpu(rm->m_inc.i_hdr.h_len) - 1;
93 smp_mb__before_atomic();
94 set_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags);
95 tc->t_last_expected_una = rm->m_ack_seq + 1;
96
97 if (test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))
98 rm->m_inc.i_hdr.h_flags |= RDS_FLAG_RETRANSMITTED;
99
100 rdsdebug("rm %p tcp nxt %u ack_seq %llu\n",
101 rm, rds_tcp_snd_nxt(tc),
102 (unsigned long long)rm->m_ack_seq);
103 }
104
105 if (hdr_off < sizeof(struct rds_header)) {
106
107 set_bit(SOCK_NOSPACE, &tc->t_sock->sk->sk_socket->flags);
108
109 ret = rds_tcp_sendmsg(tc->t_sock,
110 (void *)&rm->m_inc.i_hdr + hdr_off,
111 sizeof(rm->m_inc.i_hdr) - hdr_off);
112 if (ret < 0)
113 goto out;
114 done += ret;
115 if (hdr_off + done != sizeof(struct rds_header))
116 goto out;
117 }
118
119 more = rm->data.op_nents > 1 ? (MSG_MORE | MSG_SENDPAGE_NOTLAST) : 0;
120 while (sg < rm->data.op_nents) {
121 int flags = MSG_DONTWAIT | MSG_NOSIGNAL | more;
122
123 ret = tc->t_sock->ops->sendpage(tc->t_sock,
124 sg_page(&rm->data.op_sg[sg]),
125 rm->data.op_sg[sg].offset + off,
126 rm->data.op_sg[sg].length - off,
127 flags);
128 rdsdebug("tcp sendpage %p:%u:%u ret %d\n", (void *)sg_page(&rm->data.op_sg[sg]),
129 rm->data.op_sg[sg].offset + off, rm->data.op_sg[sg].length - off,
130 ret);
131 if (ret <= 0)
132 break;
133
134 off += ret;
135 done += ret;
136 if (off == rm->data.op_sg[sg].length) {
137 off = 0;
138 sg++;
139 }
140 if (sg == rm->data.op_nents - 1)
141 more = 0;
142 }
143
144out:
145 if (ret <= 0) {
146
147 if (ret == -EAGAIN) {
148 rds_tcp_stats_inc(s_tcp_sndbuf_full);
149 ret = 0;
150 } else {
151
152
153
154
155 if (rds_conn_path_up(cp)) {
156 pr_warn("RDS/tcp: send to %pI4 on cp [%d]"
157 "returned %d, "
158 "disconnecting and reconnecting\n",
159 &conn->c_faddr, cp->cp_index, ret);
160 rds_conn_path_drop(cp, false);
161 }
162 }
163 }
164 if (done == 0)
165 done = ret;
166 return done;
167}
168
169
170
171
172
173
174
175
176static int rds_tcp_is_acked(struct rds_message *rm, uint64_t ack)
177{
178 if (!test_bit(RDS_MSG_HAS_ACK_SEQ, &rm->m_flags))
179 return 0;
180 return (__s32)((u32)rm->m_ack_seq - (u32)ack) < 0;
181}
182
183void rds_tcp_write_space(struct sock *sk)
184{
185 void (*write_space)(struct sock *sk);
186 struct rds_conn_path *cp;
187 struct rds_tcp_connection *tc;
188
189 read_lock_bh(&sk->sk_callback_lock);
190 cp = sk->sk_user_data;
191 if (!cp) {
192 write_space = sk->sk_write_space;
193 goto out;
194 }
195
196 tc = cp->cp_transport_data;
197 rdsdebug("write_space for tc %p\n", tc);
198 write_space = tc->t_orig_write_space;
199 rds_tcp_stats_inc(s_tcp_write_space_calls);
200
201 rdsdebug("tcp una %u\n", rds_tcp_snd_una(tc));
202 tc->t_last_seen_una = rds_tcp_snd_una(tc);
203 rds_send_path_drop_acked(cp, rds_tcp_snd_una(tc), rds_tcp_is_acked);
204
205 if ((refcount_read(&sk->sk_wmem_alloc) << 1) <= sk->sk_sndbuf)
206 queue_delayed_work(rds_wq, &cp->cp_send_w, 0);
207
208out:
209 read_unlock_bh(&sk->sk_callback_lock);
210
211
212
213
214
215
216
217
218
219
220
221
222
223 write_space(sk);
224
225 if (sk->sk_socket)
226 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
227}
228