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