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